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, BYTE_TO_CHAR (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 /* Do nothing if called asynchronously. Inserting text into
9294 a buffer may call after-change-functions and alike and
9295 that would means running Lisp asynchronously. */
9296 if (handling_signal)
9297 return;
9298
9299 fmt = msg = Qnil;
9300 GCPRO4 (fmt, msg, arg1, arg2);
9301
9302 args[0] = fmt = build_string (format);
9303 args[1] = arg1;
9304 args[2] = arg2;
9305 msg = Fformat (3, args);
9306
9307 len = SBYTES (msg) + 1;
9308 buffer = SAFE_ALLOCA (len);
9309 memcpy (buffer, SDATA (msg), len);
9310
9311 message_dolog (buffer, len - 1, 1, 0);
9312 SAFE_FREE ();
9313
9314 UNGCPRO;
9315 }
9316
9317
9318 /* Output a newline in the *Messages* buffer if "needs" one. */
9319
9320 void
9321 message_log_maybe_newline (void)
9322 {
9323 if (message_log_need_newline)
9324 message_dolog ("", 0, 1, 0);
9325 }
9326
9327
9328 /* Add a string M of length NBYTES to the message log, optionally
9329 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9330 nonzero, means interpret the contents of M as multibyte. This
9331 function calls low-level routines in order to bypass text property
9332 hooks, etc. which might not be safe to run.
9333
9334 This may GC (insert may run before/after change hooks),
9335 so the buffer M must NOT point to a Lisp string. */
9336
9337 void
9338 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9339 {
9340 const unsigned char *msg = (const unsigned char *) m;
9341
9342 if (!NILP (Vmemory_full))
9343 return;
9344
9345 if (!NILP (Vmessage_log_max))
9346 {
9347 struct buffer *oldbuf;
9348 Lisp_Object oldpoint, oldbegv, oldzv;
9349 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9350 ptrdiff_t point_at_end = 0;
9351 ptrdiff_t zv_at_end = 0;
9352 Lisp_Object old_deactivate_mark, tem;
9353 struct gcpro gcpro1;
9354
9355 old_deactivate_mark = Vdeactivate_mark;
9356 oldbuf = current_buffer;
9357 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9358 bset_undo_list (current_buffer, Qt);
9359
9360 oldpoint = message_dolog_marker1;
9361 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9362 oldbegv = message_dolog_marker2;
9363 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9364 oldzv = message_dolog_marker3;
9365 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9366 GCPRO1 (old_deactivate_mark);
9367
9368 if (PT == Z)
9369 point_at_end = 1;
9370 if (ZV == Z)
9371 zv_at_end = 1;
9372
9373 BEGV = BEG;
9374 BEGV_BYTE = BEG_BYTE;
9375 ZV = Z;
9376 ZV_BYTE = Z_BYTE;
9377 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9378
9379 /* Insert the string--maybe converting multibyte to single byte
9380 or vice versa, so that all the text fits the buffer. */
9381 if (multibyte
9382 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9383 {
9384 ptrdiff_t i;
9385 int c, char_bytes;
9386 char work[1];
9387
9388 /* Convert a multibyte string to single-byte
9389 for the *Message* buffer. */
9390 for (i = 0; i < nbytes; i += char_bytes)
9391 {
9392 c = string_char_and_length (msg + i, &char_bytes);
9393 work[0] = (ASCII_CHAR_P (c)
9394 ? c
9395 : multibyte_char_to_unibyte (c));
9396 insert_1_both (work, 1, 1, 1, 0, 0);
9397 }
9398 }
9399 else if (! multibyte
9400 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9401 {
9402 ptrdiff_t i;
9403 int c, char_bytes;
9404 unsigned char str[MAX_MULTIBYTE_LENGTH];
9405 /* Convert a single-byte string to multibyte
9406 for the *Message* buffer. */
9407 for (i = 0; i < nbytes; i++)
9408 {
9409 c = msg[i];
9410 MAKE_CHAR_MULTIBYTE (c);
9411 char_bytes = CHAR_STRING (c, str);
9412 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9413 }
9414 }
9415 else if (nbytes)
9416 insert_1 (m, nbytes, 1, 0, 0);
9417
9418 if (nlflag)
9419 {
9420 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9421 printmax_t dups;
9422 insert_1 ("\n", 1, 1, 0, 0);
9423
9424 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9425 this_bol = PT;
9426 this_bol_byte = PT_BYTE;
9427
9428 /* See if this line duplicates the previous one.
9429 If so, combine duplicates. */
9430 if (this_bol > BEG)
9431 {
9432 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9433 prev_bol = PT;
9434 prev_bol_byte = PT_BYTE;
9435
9436 dups = message_log_check_duplicate (prev_bol_byte,
9437 this_bol_byte);
9438 if (dups)
9439 {
9440 del_range_both (prev_bol, prev_bol_byte,
9441 this_bol, this_bol_byte, 0);
9442 if (dups > 1)
9443 {
9444 char dupstr[sizeof " [ times]"
9445 + INT_STRLEN_BOUND (printmax_t)];
9446
9447 /* If you change this format, don't forget to also
9448 change message_log_check_duplicate. */
9449 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9450 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9451 insert_1 (dupstr, duplen, 1, 0, 1);
9452 }
9453 }
9454 }
9455
9456 /* If we have more than the desired maximum number of lines
9457 in the *Messages* buffer now, delete the oldest ones.
9458 This is safe because we don't have undo in this buffer. */
9459
9460 if (NATNUMP (Vmessage_log_max))
9461 {
9462 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9463 -XFASTINT (Vmessage_log_max) - 1, 0);
9464 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9465 }
9466 }
9467 BEGV = XMARKER (oldbegv)->charpos;
9468 BEGV_BYTE = marker_byte_position (oldbegv);
9469
9470 if (zv_at_end)
9471 {
9472 ZV = Z;
9473 ZV_BYTE = Z_BYTE;
9474 }
9475 else
9476 {
9477 ZV = XMARKER (oldzv)->charpos;
9478 ZV_BYTE = marker_byte_position (oldzv);
9479 }
9480
9481 if (point_at_end)
9482 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9483 else
9484 /* We can't do Fgoto_char (oldpoint) because it will run some
9485 Lisp code. */
9486 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9487 XMARKER (oldpoint)->bytepos);
9488
9489 UNGCPRO;
9490 unchain_marker (XMARKER (oldpoint));
9491 unchain_marker (XMARKER (oldbegv));
9492 unchain_marker (XMARKER (oldzv));
9493
9494 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9495 set_buffer_internal (oldbuf);
9496 if (NILP (tem))
9497 windows_or_buffers_changed = old_windows_or_buffers_changed;
9498 message_log_need_newline = !nlflag;
9499 Vdeactivate_mark = old_deactivate_mark;
9500 }
9501 }
9502
9503
9504 /* We are at the end of the buffer after just having inserted a newline.
9505 (Note: We depend on the fact we won't be crossing the gap.)
9506 Check to see if the most recent message looks a lot like the previous one.
9507 Return 0 if different, 1 if the new one should just replace it, or a
9508 value N > 1 if we should also append " [N times]". */
9509
9510 static intmax_t
9511 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9512 {
9513 ptrdiff_t i;
9514 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9515 int seen_dots = 0;
9516 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9517 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9518
9519 for (i = 0; i < len; i++)
9520 {
9521 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9522 seen_dots = 1;
9523 if (p1[i] != p2[i])
9524 return seen_dots;
9525 }
9526 p1 += len;
9527 if (*p1 == '\n')
9528 return 2;
9529 if (*p1++ == ' ' && *p1++ == '[')
9530 {
9531 char *pend;
9532 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9533 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9534 return n+1;
9535 }
9536 return 0;
9537 }
9538 \f
9539
9540 /* Display an echo area message M with a specified length of NBYTES
9541 bytes. The string may include null characters. If M is 0, clear
9542 out any existing message, and let the mini-buffer text show
9543 through.
9544
9545 This may GC, so the buffer M must NOT point to a Lisp string. */
9546
9547 void
9548 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9549 {
9550 /* First flush out any partial line written with print. */
9551 message_log_maybe_newline ();
9552 if (m)
9553 message_dolog (m, nbytes, 1, multibyte);
9554 message2_nolog (m, nbytes, multibyte);
9555 }
9556
9557
9558 /* The non-logging counterpart of message2. */
9559
9560 void
9561 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9562 {
9563 struct frame *sf = SELECTED_FRAME ();
9564 message_enable_multibyte = multibyte;
9565
9566 if (FRAME_INITIAL_P (sf))
9567 {
9568 if (noninteractive_need_newline)
9569 putc ('\n', stderr);
9570 noninteractive_need_newline = 0;
9571 if (m)
9572 fwrite (m, nbytes, 1, stderr);
9573 if (cursor_in_echo_area == 0)
9574 fprintf (stderr, "\n");
9575 fflush (stderr);
9576 }
9577 /* A null message buffer means that the frame hasn't really been
9578 initialized yet. Error messages get reported properly by
9579 cmd_error, so this must be just an informative message; toss it. */
9580 else if (INTERACTIVE
9581 && sf->glyphs_initialized_p
9582 && FRAME_MESSAGE_BUF (sf))
9583 {
9584 Lisp_Object mini_window;
9585 struct frame *f;
9586
9587 /* Get the frame containing the mini-buffer
9588 that the selected frame is using. */
9589 mini_window = FRAME_MINIBUF_WINDOW (sf);
9590 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9591
9592 FRAME_SAMPLE_VISIBILITY (f);
9593 if (FRAME_VISIBLE_P (sf)
9594 && ! FRAME_VISIBLE_P (f))
9595 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9596
9597 if (m)
9598 {
9599 set_message (m, Qnil, nbytes, multibyte);
9600 if (minibuffer_auto_raise)
9601 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9602 }
9603 else
9604 clear_message (1, 1);
9605
9606 do_pending_window_change (0);
9607 echo_area_display (1);
9608 do_pending_window_change (0);
9609 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9610 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9611 }
9612 }
9613
9614
9615 /* Display an echo area message M with a specified length of NBYTES
9616 bytes. The string may include null characters. If M is not a
9617 string, clear out any existing message, and let the mini-buffer
9618 text show through.
9619
9620 This function cancels echoing. */
9621
9622 void
9623 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9624 {
9625 struct gcpro gcpro1;
9626
9627 GCPRO1 (m);
9628 clear_message (1,1);
9629 cancel_echoing ();
9630
9631 /* First flush out any partial line written with print. */
9632 message_log_maybe_newline ();
9633 if (STRINGP (m))
9634 {
9635 USE_SAFE_ALLOCA;
9636 char *buffer = SAFE_ALLOCA (nbytes);
9637 memcpy (buffer, SDATA (m), nbytes);
9638 message_dolog (buffer, nbytes, 1, multibyte);
9639 SAFE_FREE ();
9640 }
9641 message3_nolog (m, nbytes, multibyte);
9642
9643 UNGCPRO;
9644 }
9645
9646
9647 /* The non-logging version of message3.
9648 This does not cancel echoing, because it is used for echoing.
9649 Perhaps we need to make a separate function for echoing
9650 and make this cancel echoing. */
9651
9652 void
9653 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9654 {
9655 struct frame *sf = SELECTED_FRAME ();
9656 message_enable_multibyte = multibyte;
9657
9658 if (FRAME_INITIAL_P (sf))
9659 {
9660 if (noninteractive_need_newline)
9661 putc ('\n', stderr);
9662 noninteractive_need_newline = 0;
9663 if (STRINGP (m))
9664 fwrite (SDATA (m), nbytes, 1, stderr);
9665 if (cursor_in_echo_area == 0)
9666 fprintf (stderr, "\n");
9667 fflush (stderr);
9668 }
9669 /* A null message buffer means that the frame hasn't really been
9670 initialized yet. Error messages get reported properly by
9671 cmd_error, so this must be just an informative message; toss it. */
9672 else if (INTERACTIVE
9673 && sf->glyphs_initialized_p
9674 && FRAME_MESSAGE_BUF (sf))
9675 {
9676 Lisp_Object mini_window;
9677 Lisp_Object frame;
9678 struct frame *f;
9679
9680 /* Get the frame containing the mini-buffer
9681 that the selected frame is using. */
9682 mini_window = FRAME_MINIBUF_WINDOW (sf);
9683 frame = XWINDOW (mini_window)->frame;
9684 f = XFRAME (frame);
9685
9686 FRAME_SAMPLE_VISIBILITY (f);
9687 if (FRAME_VISIBLE_P (sf)
9688 && !FRAME_VISIBLE_P (f))
9689 Fmake_frame_visible (frame);
9690
9691 if (STRINGP (m) && SCHARS (m) > 0)
9692 {
9693 set_message (NULL, m, nbytes, multibyte);
9694 if (minibuffer_auto_raise)
9695 Fraise_frame (frame);
9696 /* Assume we are not echoing.
9697 (If we are, echo_now will override this.) */
9698 echo_message_buffer = Qnil;
9699 }
9700 else
9701 clear_message (1, 1);
9702
9703 do_pending_window_change (0);
9704 echo_area_display (1);
9705 do_pending_window_change (0);
9706 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9707 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9708 }
9709 }
9710
9711
9712 /* Display a null-terminated echo area message M. If M is 0, clear
9713 out any existing message, and let the mini-buffer text show through.
9714
9715 The buffer M must continue to exist until after the echo area gets
9716 cleared or some other message gets displayed there. Do not pass
9717 text that is stored in a Lisp string. Do not pass text in a buffer
9718 that was alloca'd. */
9719
9720 void
9721 message1 (const char *m)
9722 {
9723 message2 (m, (m ? strlen (m) : 0), 0);
9724 }
9725
9726
9727 /* The non-logging counterpart of message1. */
9728
9729 void
9730 message1_nolog (const char *m)
9731 {
9732 message2_nolog (m, (m ? strlen (m) : 0), 0);
9733 }
9734
9735 /* Display a message M which contains a single %s
9736 which gets replaced with STRING. */
9737
9738 void
9739 message_with_string (const char *m, Lisp_Object string, int log)
9740 {
9741 CHECK_STRING (string);
9742
9743 if (noninteractive)
9744 {
9745 if (m)
9746 {
9747 if (noninteractive_need_newline)
9748 putc ('\n', stderr);
9749 noninteractive_need_newline = 0;
9750 fprintf (stderr, m, SDATA (string));
9751 if (!cursor_in_echo_area)
9752 fprintf (stderr, "\n");
9753 fflush (stderr);
9754 }
9755 }
9756 else if (INTERACTIVE)
9757 {
9758 /* The frame whose minibuffer we're going to display the message on.
9759 It may be larger than the selected frame, so we need
9760 to use its buffer, not the selected frame's buffer. */
9761 Lisp_Object mini_window;
9762 struct frame *f, *sf = SELECTED_FRAME ();
9763
9764 /* Get the frame containing the minibuffer
9765 that the selected frame is using. */
9766 mini_window = FRAME_MINIBUF_WINDOW (sf);
9767 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9768
9769 /* A null message buffer means that the frame hasn't really been
9770 initialized yet. Error messages get reported properly by
9771 cmd_error, so this must be just an informative message; toss it. */
9772 if (FRAME_MESSAGE_BUF (f))
9773 {
9774 Lisp_Object args[2], msg;
9775 struct gcpro gcpro1, gcpro2;
9776
9777 args[0] = build_string (m);
9778 args[1] = msg = string;
9779 GCPRO2 (args[0], msg);
9780 gcpro1.nvars = 2;
9781
9782 msg = Fformat (2, args);
9783
9784 if (log)
9785 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9786 else
9787 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9788
9789 UNGCPRO;
9790
9791 /* Print should start at the beginning of the message
9792 buffer next time. */
9793 message_buf_print = 0;
9794 }
9795 }
9796 }
9797
9798
9799 /* Dump an informative message to the minibuf. If M is 0, clear out
9800 any existing message, and let the mini-buffer text show through. */
9801
9802 static void
9803 vmessage (const char *m, va_list ap)
9804 {
9805 if (noninteractive)
9806 {
9807 if (m)
9808 {
9809 if (noninteractive_need_newline)
9810 putc ('\n', stderr);
9811 noninteractive_need_newline = 0;
9812 vfprintf (stderr, m, ap);
9813 if (cursor_in_echo_area == 0)
9814 fprintf (stderr, "\n");
9815 fflush (stderr);
9816 }
9817 }
9818 else if (INTERACTIVE)
9819 {
9820 /* The frame whose mini-buffer we're going to display the message
9821 on. It may be larger than the selected frame, so we need to
9822 use its buffer, not the selected frame's buffer. */
9823 Lisp_Object mini_window;
9824 struct frame *f, *sf = SELECTED_FRAME ();
9825
9826 /* Get the frame containing the mini-buffer
9827 that the selected frame is using. */
9828 mini_window = FRAME_MINIBUF_WINDOW (sf);
9829 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9830
9831 /* A null message buffer means that the frame hasn't really been
9832 initialized yet. Error messages get reported properly by
9833 cmd_error, so this must be just an informative message; toss
9834 it. */
9835 if (FRAME_MESSAGE_BUF (f))
9836 {
9837 if (m)
9838 {
9839 ptrdiff_t len;
9840
9841 len = doprnt (FRAME_MESSAGE_BUF (f),
9842 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9843
9844 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9845 }
9846 else
9847 message1 (0);
9848
9849 /* Print should start at the beginning of the message
9850 buffer next time. */
9851 message_buf_print = 0;
9852 }
9853 }
9854 }
9855
9856 void
9857 message (const char *m, ...)
9858 {
9859 va_list ap;
9860 va_start (ap, m);
9861 vmessage (m, ap);
9862 va_end (ap);
9863 }
9864
9865
9866 #if 0
9867 /* The non-logging version of message. */
9868
9869 void
9870 message_nolog (const char *m, ...)
9871 {
9872 Lisp_Object old_log_max;
9873 va_list ap;
9874 va_start (ap, m);
9875 old_log_max = Vmessage_log_max;
9876 Vmessage_log_max = Qnil;
9877 vmessage (m, ap);
9878 Vmessage_log_max = old_log_max;
9879 va_end (ap);
9880 }
9881 #endif
9882
9883
9884 /* Display the current message in the current mini-buffer. This is
9885 only called from error handlers in process.c, and is not time
9886 critical. */
9887
9888 void
9889 update_echo_area (void)
9890 {
9891 if (!NILP (echo_area_buffer[0]))
9892 {
9893 Lisp_Object string;
9894 string = Fcurrent_message ();
9895 message3 (string, SBYTES (string),
9896 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9897 }
9898 }
9899
9900
9901 /* Make sure echo area buffers in `echo_buffers' are live.
9902 If they aren't, make new ones. */
9903
9904 static void
9905 ensure_echo_area_buffers (void)
9906 {
9907 int i;
9908
9909 for (i = 0; i < 2; ++i)
9910 if (!BUFFERP (echo_buffer[i])
9911 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9912 {
9913 char name[30];
9914 Lisp_Object old_buffer;
9915 int j;
9916
9917 old_buffer = echo_buffer[i];
9918 echo_buffer[i] = Fget_buffer_create
9919 (make_formatted_string (name, " *Echo Area %d*", i));
9920 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9921 /* to force word wrap in echo area -
9922 it was decided to postpone this*/
9923 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9924
9925 for (j = 0; j < 2; ++j)
9926 if (EQ (old_buffer, echo_area_buffer[j]))
9927 echo_area_buffer[j] = echo_buffer[i];
9928 }
9929 }
9930
9931
9932 /* Call FN with args A1..A4 with either the current or last displayed
9933 echo_area_buffer as current buffer.
9934
9935 WHICH zero means use the current message buffer
9936 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9937 from echo_buffer[] and clear it.
9938
9939 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9940 suitable buffer from echo_buffer[] and clear it.
9941
9942 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9943 that the current message becomes the last displayed one, make
9944 choose a suitable buffer for echo_area_buffer[0], and clear it.
9945
9946 Value is what FN returns. */
9947
9948 static int
9949 with_echo_area_buffer (struct window *w, int which,
9950 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9951 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9952 {
9953 Lisp_Object buffer;
9954 int this_one, the_other, clear_buffer_p, rc;
9955 ptrdiff_t count = SPECPDL_INDEX ();
9956
9957 /* If buffers aren't live, make new ones. */
9958 ensure_echo_area_buffers ();
9959
9960 clear_buffer_p = 0;
9961
9962 if (which == 0)
9963 this_one = 0, the_other = 1;
9964 else if (which > 0)
9965 this_one = 1, the_other = 0;
9966 else
9967 {
9968 this_one = 0, the_other = 1;
9969 clear_buffer_p = 1;
9970
9971 /* We need a fresh one in case the current echo buffer equals
9972 the one containing the last displayed echo area message. */
9973 if (!NILP (echo_area_buffer[this_one])
9974 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9975 echo_area_buffer[this_one] = Qnil;
9976 }
9977
9978 /* Choose a suitable buffer from echo_buffer[] is we don't
9979 have one. */
9980 if (NILP (echo_area_buffer[this_one]))
9981 {
9982 echo_area_buffer[this_one]
9983 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9984 ? echo_buffer[the_other]
9985 : echo_buffer[this_one]);
9986 clear_buffer_p = 1;
9987 }
9988
9989 buffer = echo_area_buffer[this_one];
9990
9991 /* Don't get confused by reusing the buffer used for echoing
9992 for a different purpose. */
9993 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9994 cancel_echoing ();
9995
9996 record_unwind_protect (unwind_with_echo_area_buffer,
9997 with_echo_area_buffer_unwind_data (w));
9998
9999 /* Make the echo area buffer current. Note that for display
10000 purposes, it is not necessary that the displayed window's buffer
10001 == current_buffer, except for text property lookup. So, let's
10002 only set that buffer temporarily here without doing a full
10003 Fset_window_buffer. We must also change w->pointm, though,
10004 because otherwise an assertions in unshow_buffer fails, and Emacs
10005 aborts. */
10006 set_buffer_internal_1 (XBUFFER (buffer));
10007 if (w)
10008 {
10009 wset_buffer (w, buffer);
10010 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10011 }
10012
10013 bset_undo_list (current_buffer, Qt);
10014 bset_read_only (current_buffer, Qnil);
10015 specbind (Qinhibit_read_only, Qt);
10016 specbind (Qinhibit_modification_hooks, Qt);
10017
10018 if (clear_buffer_p && Z > BEG)
10019 del_range (BEG, Z);
10020
10021 eassert (BEGV >= BEG);
10022 eassert (ZV <= Z && ZV >= BEGV);
10023
10024 rc = fn (a1, a2, a3, a4);
10025
10026 eassert (BEGV >= BEG);
10027 eassert (ZV <= Z && ZV >= BEGV);
10028
10029 unbind_to (count, Qnil);
10030 return rc;
10031 }
10032
10033
10034 /* Save state that should be preserved around the call to the function
10035 FN called in with_echo_area_buffer. */
10036
10037 static Lisp_Object
10038 with_echo_area_buffer_unwind_data (struct window *w)
10039 {
10040 int i = 0;
10041 Lisp_Object vector, tmp;
10042
10043 /* Reduce consing by keeping one vector in
10044 Vwith_echo_area_save_vector. */
10045 vector = Vwith_echo_area_save_vector;
10046 Vwith_echo_area_save_vector = Qnil;
10047
10048 if (NILP (vector))
10049 vector = Fmake_vector (make_number (7), Qnil);
10050
10051 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10052 ASET (vector, i, Vdeactivate_mark); ++i;
10053 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10054
10055 if (w)
10056 {
10057 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10058 ASET (vector, i, w->buffer); ++i;
10059 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10060 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10061 }
10062 else
10063 {
10064 int end = i + 4;
10065 for (; i < end; ++i)
10066 ASET (vector, i, Qnil);
10067 }
10068
10069 eassert (i == ASIZE (vector));
10070 return vector;
10071 }
10072
10073
10074 /* Restore global state from VECTOR which was created by
10075 with_echo_area_buffer_unwind_data. */
10076
10077 static Lisp_Object
10078 unwind_with_echo_area_buffer (Lisp_Object vector)
10079 {
10080 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10081 Vdeactivate_mark = AREF (vector, 1);
10082 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10083
10084 if (WINDOWP (AREF (vector, 3)))
10085 {
10086 struct window *w;
10087 Lisp_Object buffer, charpos, bytepos;
10088
10089 w = XWINDOW (AREF (vector, 3));
10090 buffer = AREF (vector, 4);
10091 charpos = AREF (vector, 5);
10092 bytepos = AREF (vector, 6);
10093
10094 wset_buffer (w, buffer);
10095 set_marker_both (w->pointm, buffer,
10096 XFASTINT (charpos), XFASTINT (bytepos));
10097 }
10098
10099 Vwith_echo_area_save_vector = vector;
10100 return Qnil;
10101 }
10102
10103
10104 /* Set up the echo area for use by print functions. MULTIBYTE_P
10105 non-zero means we will print multibyte. */
10106
10107 void
10108 setup_echo_area_for_printing (int multibyte_p)
10109 {
10110 /* If we can't find an echo area any more, exit. */
10111 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10112 Fkill_emacs (Qnil);
10113
10114 ensure_echo_area_buffers ();
10115
10116 if (!message_buf_print)
10117 {
10118 /* A message has been output since the last time we printed.
10119 Choose a fresh echo area buffer. */
10120 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10121 echo_area_buffer[0] = echo_buffer[1];
10122 else
10123 echo_area_buffer[0] = echo_buffer[0];
10124
10125 /* Switch to that buffer and clear it. */
10126 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10127 bset_truncate_lines (current_buffer, Qnil);
10128
10129 if (Z > BEG)
10130 {
10131 ptrdiff_t count = SPECPDL_INDEX ();
10132 specbind (Qinhibit_read_only, Qt);
10133 /* Note that undo recording is always disabled. */
10134 del_range (BEG, Z);
10135 unbind_to (count, Qnil);
10136 }
10137 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10138
10139 /* Set up the buffer for the multibyteness we need. */
10140 if (multibyte_p
10141 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10142 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10143
10144 /* Raise the frame containing the echo area. */
10145 if (minibuffer_auto_raise)
10146 {
10147 struct frame *sf = SELECTED_FRAME ();
10148 Lisp_Object mini_window;
10149 mini_window = FRAME_MINIBUF_WINDOW (sf);
10150 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10151 }
10152
10153 message_log_maybe_newline ();
10154 message_buf_print = 1;
10155 }
10156 else
10157 {
10158 if (NILP (echo_area_buffer[0]))
10159 {
10160 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10161 echo_area_buffer[0] = echo_buffer[1];
10162 else
10163 echo_area_buffer[0] = echo_buffer[0];
10164 }
10165
10166 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10167 {
10168 /* Someone switched buffers between print requests. */
10169 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10170 bset_truncate_lines (current_buffer, Qnil);
10171 }
10172 }
10173 }
10174
10175
10176 /* Display an echo area message in window W. Value is non-zero if W's
10177 height is changed. If display_last_displayed_message_p is
10178 non-zero, display the message that was last displayed, otherwise
10179 display the current message. */
10180
10181 static int
10182 display_echo_area (struct window *w)
10183 {
10184 int i, no_message_p, window_height_changed_p;
10185
10186 /* Temporarily disable garbage collections while displaying the echo
10187 area. This is done because a GC can print a message itself.
10188 That message would modify the echo area buffer's contents while a
10189 redisplay of the buffer is going on, and seriously confuse
10190 redisplay. */
10191 ptrdiff_t count = inhibit_garbage_collection ();
10192
10193 /* If there is no message, we must call display_echo_area_1
10194 nevertheless because it resizes the window. But we will have to
10195 reset the echo_area_buffer in question to nil at the end because
10196 with_echo_area_buffer will sets it to an empty buffer. */
10197 i = display_last_displayed_message_p ? 1 : 0;
10198 no_message_p = NILP (echo_area_buffer[i]);
10199
10200 window_height_changed_p
10201 = with_echo_area_buffer (w, display_last_displayed_message_p,
10202 display_echo_area_1,
10203 (intptr_t) w, Qnil, 0, 0);
10204
10205 if (no_message_p)
10206 echo_area_buffer[i] = Qnil;
10207
10208 unbind_to (count, Qnil);
10209 return window_height_changed_p;
10210 }
10211
10212
10213 /* Helper for display_echo_area. Display the current buffer which
10214 contains the current echo area message in window W, a mini-window,
10215 a pointer to which is passed in A1. A2..A4 are currently not used.
10216 Change the height of W so that all of the message is displayed.
10217 Value is non-zero if height of W was changed. */
10218
10219 static int
10220 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10221 {
10222 intptr_t i1 = a1;
10223 struct window *w = (struct window *) i1;
10224 Lisp_Object window;
10225 struct text_pos start;
10226 int window_height_changed_p = 0;
10227
10228 /* Do this before displaying, so that we have a large enough glyph
10229 matrix for the display. If we can't get enough space for the
10230 whole text, display the last N lines. That works by setting w->start. */
10231 window_height_changed_p = resize_mini_window (w, 0);
10232
10233 /* Use the starting position chosen by resize_mini_window. */
10234 SET_TEXT_POS_FROM_MARKER (start, w->start);
10235
10236 /* Display. */
10237 clear_glyph_matrix (w->desired_matrix);
10238 XSETWINDOW (window, w);
10239 try_window (window, start, 0);
10240
10241 return window_height_changed_p;
10242 }
10243
10244
10245 /* Resize the echo area window to exactly the size needed for the
10246 currently displayed message, if there is one. If a mini-buffer
10247 is active, don't shrink it. */
10248
10249 void
10250 resize_echo_area_exactly (void)
10251 {
10252 if (BUFFERP (echo_area_buffer[0])
10253 && WINDOWP (echo_area_window))
10254 {
10255 struct window *w = XWINDOW (echo_area_window);
10256 int resized_p;
10257 Lisp_Object resize_exactly;
10258
10259 if (minibuf_level == 0)
10260 resize_exactly = Qt;
10261 else
10262 resize_exactly = Qnil;
10263
10264 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10265 (intptr_t) w, resize_exactly,
10266 0, 0);
10267 if (resized_p)
10268 {
10269 ++windows_or_buffers_changed;
10270 ++update_mode_lines;
10271 redisplay_internal ();
10272 }
10273 }
10274 }
10275
10276
10277 /* Callback function for with_echo_area_buffer, when used from
10278 resize_echo_area_exactly. A1 contains a pointer to the window to
10279 resize, EXACTLY non-nil means resize the mini-window exactly to the
10280 size of the text displayed. A3 and A4 are not used. Value is what
10281 resize_mini_window returns. */
10282
10283 static int
10284 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10285 {
10286 intptr_t i1 = a1;
10287 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10288 }
10289
10290
10291 /* Resize mini-window W to fit the size of its contents. EXACT_P
10292 means size the window exactly to the size needed. Otherwise, it's
10293 only enlarged until W's buffer is empty.
10294
10295 Set W->start to the right place to begin display. If the whole
10296 contents fit, start at the beginning. Otherwise, start so as
10297 to make the end of the contents appear. This is particularly
10298 important for y-or-n-p, but seems desirable generally.
10299
10300 Value is non-zero if the window height has been changed. */
10301
10302 int
10303 resize_mini_window (struct window *w, int exact_p)
10304 {
10305 struct frame *f = XFRAME (w->frame);
10306 int window_height_changed_p = 0;
10307
10308 eassert (MINI_WINDOW_P (w));
10309
10310 /* By default, start display at the beginning. */
10311 set_marker_both (w->start, w->buffer,
10312 BUF_BEGV (XBUFFER (w->buffer)),
10313 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10314
10315 /* Don't resize windows while redisplaying a window; it would
10316 confuse redisplay functions when the size of the window they are
10317 displaying changes from under them. Such a resizing can happen,
10318 for instance, when which-func prints a long message while
10319 we are running fontification-functions. We're running these
10320 functions with safe_call which binds inhibit-redisplay to t. */
10321 if (!NILP (Vinhibit_redisplay))
10322 return 0;
10323
10324 /* Nil means don't try to resize. */
10325 if (NILP (Vresize_mini_windows)
10326 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10327 return 0;
10328
10329 if (!FRAME_MINIBUF_ONLY_P (f))
10330 {
10331 struct it it;
10332 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10333 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10334 int height;
10335 EMACS_INT max_height;
10336 int unit = FRAME_LINE_HEIGHT (f);
10337 struct text_pos start;
10338 struct buffer *old_current_buffer = NULL;
10339
10340 if (current_buffer != XBUFFER (w->buffer))
10341 {
10342 old_current_buffer = current_buffer;
10343 set_buffer_internal (XBUFFER (w->buffer));
10344 }
10345
10346 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10347
10348 /* Compute the max. number of lines specified by the user. */
10349 if (FLOATP (Vmax_mini_window_height))
10350 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10351 else if (INTEGERP (Vmax_mini_window_height))
10352 max_height = XINT (Vmax_mini_window_height);
10353 else
10354 max_height = total_height / 4;
10355
10356 /* Correct that max. height if it's bogus. */
10357 max_height = max (1, max_height);
10358 max_height = min (total_height, max_height);
10359
10360 /* Find out the height of the text in the window. */
10361 if (it.line_wrap == TRUNCATE)
10362 height = 1;
10363 else
10364 {
10365 last_height = 0;
10366 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10367 if (it.max_ascent == 0 && it.max_descent == 0)
10368 height = it.current_y + last_height;
10369 else
10370 height = it.current_y + it.max_ascent + it.max_descent;
10371 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10372 height = (height + unit - 1) / unit;
10373 }
10374
10375 /* Compute a suitable window start. */
10376 if (height > max_height)
10377 {
10378 height = max_height;
10379 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10380 move_it_vertically_backward (&it, (height - 1) * unit);
10381 start = it.current.pos;
10382 }
10383 else
10384 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10385 SET_MARKER_FROM_TEXT_POS (w->start, start);
10386
10387 if (EQ (Vresize_mini_windows, Qgrow_only))
10388 {
10389 /* Let it grow only, until we display an empty message, in which
10390 case the window shrinks again. */
10391 if (height > WINDOW_TOTAL_LINES (w))
10392 {
10393 int old_height = WINDOW_TOTAL_LINES (w);
10394 freeze_window_starts (f, 1);
10395 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10396 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10397 }
10398 else if (height < WINDOW_TOTAL_LINES (w)
10399 && (exact_p || BEGV == ZV))
10400 {
10401 int old_height = WINDOW_TOTAL_LINES (w);
10402 freeze_window_starts (f, 0);
10403 shrink_mini_window (w);
10404 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10405 }
10406 }
10407 else
10408 {
10409 /* Always resize to exact size needed. */
10410 if (height > WINDOW_TOTAL_LINES (w))
10411 {
10412 int old_height = WINDOW_TOTAL_LINES (w);
10413 freeze_window_starts (f, 1);
10414 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10415 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10416 }
10417 else if (height < WINDOW_TOTAL_LINES (w))
10418 {
10419 int old_height = WINDOW_TOTAL_LINES (w);
10420 freeze_window_starts (f, 0);
10421 shrink_mini_window (w);
10422
10423 if (height)
10424 {
10425 freeze_window_starts (f, 1);
10426 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10427 }
10428
10429 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10430 }
10431 }
10432
10433 if (old_current_buffer)
10434 set_buffer_internal (old_current_buffer);
10435 }
10436
10437 return window_height_changed_p;
10438 }
10439
10440
10441 /* Value is the current message, a string, or nil if there is no
10442 current message. */
10443
10444 Lisp_Object
10445 current_message (void)
10446 {
10447 Lisp_Object msg;
10448
10449 if (!BUFFERP (echo_area_buffer[0]))
10450 msg = Qnil;
10451 else
10452 {
10453 with_echo_area_buffer (0, 0, current_message_1,
10454 (intptr_t) &msg, Qnil, 0, 0);
10455 if (NILP (msg))
10456 echo_area_buffer[0] = Qnil;
10457 }
10458
10459 return msg;
10460 }
10461
10462
10463 static int
10464 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10465 {
10466 intptr_t i1 = a1;
10467 Lisp_Object *msg = (Lisp_Object *) i1;
10468
10469 if (Z > BEG)
10470 *msg = make_buffer_string (BEG, Z, 1);
10471 else
10472 *msg = Qnil;
10473 return 0;
10474 }
10475
10476
10477 /* Push the current message on Vmessage_stack for later restoration
10478 by restore_message. Value is non-zero if the current message isn't
10479 empty. This is a relatively infrequent operation, so it's not
10480 worth optimizing. */
10481
10482 bool
10483 push_message (void)
10484 {
10485 Lisp_Object msg = current_message ();
10486 Vmessage_stack = Fcons (msg, Vmessage_stack);
10487 return STRINGP (msg);
10488 }
10489
10490
10491 /* Restore message display from the top of Vmessage_stack. */
10492
10493 void
10494 restore_message (void)
10495 {
10496 Lisp_Object msg;
10497
10498 eassert (CONSP (Vmessage_stack));
10499 msg = XCAR (Vmessage_stack);
10500 if (STRINGP (msg))
10501 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10502 else
10503 message3_nolog (msg, 0, 0);
10504 }
10505
10506
10507 /* Handler for record_unwind_protect calling pop_message. */
10508
10509 Lisp_Object
10510 pop_message_unwind (Lisp_Object dummy)
10511 {
10512 pop_message ();
10513 return Qnil;
10514 }
10515
10516 /* Pop the top-most entry off Vmessage_stack. */
10517
10518 static void
10519 pop_message (void)
10520 {
10521 eassert (CONSP (Vmessage_stack));
10522 Vmessage_stack = XCDR (Vmessage_stack);
10523 }
10524
10525
10526 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10527 exits. If the stack is not empty, we have a missing pop_message
10528 somewhere. */
10529
10530 void
10531 check_message_stack (void)
10532 {
10533 if (!NILP (Vmessage_stack))
10534 emacs_abort ();
10535 }
10536
10537
10538 /* Truncate to NCHARS what will be displayed in the echo area the next
10539 time we display it---but don't redisplay it now. */
10540
10541 void
10542 truncate_echo_area (ptrdiff_t nchars)
10543 {
10544 if (nchars == 0)
10545 echo_area_buffer[0] = Qnil;
10546 /* A null message buffer means that the frame hasn't really been
10547 initialized yet. Error messages get reported properly by
10548 cmd_error, so this must be just an informative message; toss it. */
10549 else if (!noninteractive
10550 && INTERACTIVE
10551 && !NILP (echo_area_buffer[0]))
10552 {
10553 struct frame *sf = SELECTED_FRAME ();
10554 if (FRAME_MESSAGE_BUF (sf))
10555 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10556 }
10557 }
10558
10559
10560 /* Helper function for truncate_echo_area. Truncate the current
10561 message to at most NCHARS characters. */
10562
10563 static int
10564 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10565 {
10566 if (BEG + nchars < Z)
10567 del_range (BEG + nchars, Z);
10568 if (Z == BEG)
10569 echo_area_buffer[0] = Qnil;
10570 return 0;
10571 }
10572
10573 /* Set the current message to a substring of S or STRING.
10574
10575 If STRING is a Lisp string, set the message to the first NBYTES
10576 bytes from STRING. NBYTES zero means use the whole string. If
10577 STRING is multibyte, the message will be displayed multibyte.
10578
10579 If S is not null, set the message to the first LEN bytes of S. LEN
10580 zero means use the whole string. MULTIBYTE_P non-zero means S is
10581 multibyte. Display the message multibyte in that case.
10582
10583 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10584 to t before calling set_message_1 (which calls insert).
10585 */
10586
10587 static void
10588 set_message (const char *s, Lisp_Object string,
10589 ptrdiff_t nbytes, int multibyte_p)
10590 {
10591 message_enable_multibyte
10592 = ((s && multibyte_p)
10593 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10594
10595 with_echo_area_buffer (0, -1, set_message_1,
10596 (intptr_t) s, string, nbytes, multibyte_p);
10597 message_buf_print = 0;
10598 help_echo_showing_p = 0;
10599
10600 if (STRINGP (Vdebug_on_message)
10601 && fast_string_match (Vdebug_on_message, string) >= 0)
10602 call_debugger (list2 (Qerror, string));
10603 }
10604
10605
10606 /* Helper function for set_message. Arguments have the same meaning
10607 as there, with A1 corresponding to S and A2 corresponding to STRING
10608 This function is called with the echo area buffer being
10609 current. */
10610
10611 static int
10612 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10613 {
10614 intptr_t i1 = a1;
10615 const char *s = (const char *) i1;
10616 const unsigned char *msg = (const unsigned char *) s;
10617 Lisp_Object string = a2;
10618
10619 /* Change multibyteness of the echo buffer appropriately. */
10620 if (message_enable_multibyte
10621 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10622 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10623
10624 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10625 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10626 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10627
10628 /* Insert new message at BEG. */
10629 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10630
10631 if (STRINGP (string))
10632 {
10633 ptrdiff_t nchars;
10634
10635 if (nbytes == 0)
10636 nbytes = SBYTES (string);
10637 nchars = string_byte_to_char (string, nbytes);
10638
10639 /* This function takes care of single/multibyte conversion. We
10640 just have to ensure that the echo area buffer has the right
10641 setting of enable_multibyte_characters. */
10642 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10643 }
10644 else if (s)
10645 {
10646 if (nbytes == 0)
10647 nbytes = strlen (s);
10648
10649 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10650 {
10651 /* Convert from multi-byte to single-byte. */
10652 ptrdiff_t i;
10653 int c, n;
10654 char work[1];
10655
10656 /* Convert a multibyte string to single-byte. */
10657 for (i = 0; i < nbytes; i += n)
10658 {
10659 c = string_char_and_length (msg + i, &n);
10660 work[0] = (ASCII_CHAR_P (c)
10661 ? c
10662 : multibyte_char_to_unibyte (c));
10663 insert_1_both (work, 1, 1, 1, 0, 0);
10664 }
10665 }
10666 else if (!multibyte_p
10667 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10668 {
10669 /* Convert from single-byte to multi-byte. */
10670 ptrdiff_t i;
10671 int c, n;
10672 unsigned char str[MAX_MULTIBYTE_LENGTH];
10673
10674 /* Convert a single-byte string to multibyte. */
10675 for (i = 0; i < nbytes; i++)
10676 {
10677 c = msg[i];
10678 MAKE_CHAR_MULTIBYTE (c);
10679 n = CHAR_STRING (c, str);
10680 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10681 }
10682 }
10683 else
10684 insert_1 (s, nbytes, 1, 0, 0);
10685 }
10686
10687 return 0;
10688 }
10689
10690
10691 /* Clear messages. CURRENT_P non-zero means clear the current
10692 message. LAST_DISPLAYED_P non-zero means clear the message
10693 last displayed. */
10694
10695 void
10696 clear_message (int current_p, int last_displayed_p)
10697 {
10698 if (current_p)
10699 {
10700 echo_area_buffer[0] = Qnil;
10701 message_cleared_p = 1;
10702 }
10703
10704 if (last_displayed_p)
10705 echo_area_buffer[1] = Qnil;
10706
10707 message_buf_print = 0;
10708 }
10709
10710 /* Clear garbaged frames.
10711
10712 This function is used where the old redisplay called
10713 redraw_garbaged_frames which in turn called redraw_frame which in
10714 turn called clear_frame. The call to clear_frame was a source of
10715 flickering. I believe a clear_frame is not necessary. It should
10716 suffice in the new redisplay to invalidate all current matrices,
10717 and ensure a complete redisplay of all windows. */
10718
10719 static void
10720 clear_garbaged_frames (void)
10721 {
10722 if (frame_garbaged)
10723 {
10724 Lisp_Object tail, frame;
10725 int changed_count = 0;
10726
10727 FOR_EACH_FRAME (tail, frame)
10728 {
10729 struct frame *f = XFRAME (frame);
10730
10731 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10732 {
10733 if (f->resized_p)
10734 {
10735 Fredraw_frame (frame);
10736 f->force_flush_display_p = 1;
10737 }
10738 clear_current_matrices (f);
10739 changed_count++;
10740 f->garbaged = 0;
10741 f->resized_p = 0;
10742 }
10743 }
10744
10745 frame_garbaged = 0;
10746 if (changed_count)
10747 ++windows_or_buffers_changed;
10748 }
10749 }
10750
10751
10752 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10753 is non-zero update selected_frame. Value is non-zero if the
10754 mini-windows height has been changed. */
10755
10756 static int
10757 echo_area_display (int update_frame_p)
10758 {
10759 Lisp_Object mini_window;
10760 struct window *w;
10761 struct frame *f;
10762 int window_height_changed_p = 0;
10763 struct frame *sf = SELECTED_FRAME ();
10764
10765 mini_window = FRAME_MINIBUF_WINDOW (sf);
10766 w = XWINDOW (mini_window);
10767 f = XFRAME (WINDOW_FRAME (w));
10768
10769 /* Don't display if frame is invisible or not yet initialized. */
10770 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10771 return 0;
10772
10773 #ifdef HAVE_WINDOW_SYSTEM
10774 /* When Emacs starts, selected_frame may be the initial terminal
10775 frame. If we let this through, a message would be displayed on
10776 the terminal. */
10777 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10778 return 0;
10779 #endif /* HAVE_WINDOW_SYSTEM */
10780
10781 /* Redraw garbaged frames. */
10782 if (frame_garbaged)
10783 clear_garbaged_frames ();
10784
10785 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10786 {
10787 echo_area_window = mini_window;
10788 window_height_changed_p = display_echo_area (w);
10789 w->must_be_updated_p = 1;
10790
10791 /* Update the display, unless called from redisplay_internal.
10792 Also don't update the screen during redisplay itself. The
10793 update will happen at the end of redisplay, and an update
10794 here could cause confusion. */
10795 if (update_frame_p && !redisplaying_p)
10796 {
10797 int n = 0;
10798
10799 /* If the display update has been interrupted by pending
10800 input, update mode lines in the frame. Due to the
10801 pending input, it might have been that redisplay hasn't
10802 been called, so that mode lines above the echo area are
10803 garbaged. This looks odd, so we prevent it here. */
10804 if (!display_completed)
10805 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10806
10807 if (window_height_changed_p
10808 /* Don't do this if Emacs is shutting down. Redisplay
10809 needs to run hooks. */
10810 && !NILP (Vrun_hooks))
10811 {
10812 /* Must update other windows. Likewise as in other
10813 cases, don't let this update be interrupted by
10814 pending input. */
10815 ptrdiff_t count = SPECPDL_INDEX ();
10816 specbind (Qredisplay_dont_pause, Qt);
10817 windows_or_buffers_changed = 1;
10818 redisplay_internal ();
10819 unbind_to (count, Qnil);
10820 }
10821 else if (FRAME_WINDOW_P (f) && n == 0)
10822 {
10823 /* Window configuration is the same as before.
10824 Can do with a display update of the echo area,
10825 unless we displayed some mode lines. */
10826 update_single_window (w, 1);
10827 FRAME_RIF (f)->flush_display (f);
10828 }
10829 else
10830 update_frame (f, 1, 1);
10831
10832 /* If cursor is in the echo area, make sure that the next
10833 redisplay displays the minibuffer, so that the cursor will
10834 be replaced with what the minibuffer wants. */
10835 if (cursor_in_echo_area)
10836 ++windows_or_buffers_changed;
10837 }
10838 }
10839 else if (!EQ (mini_window, selected_window))
10840 windows_or_buffers_changed++;
10841
10842 /* Last displayed message is now the current message. */
10843 echo_area_buffer[1] = echo_area_buffer[0];
10844 /* Inform read_char that we're not echoing. */
10845 echo_message_buffer = Qnil;
10846
10847 /* Prevent redisplay optimization in redisplay_internal by resetting
10848 this_line_start_pos. This is done because the mini-buffer now
10849 displays the message instead of its buffer text. */
10850 if (EQ (mini_window, selected_window))
10851 CHARPOS (this_line_start_pos) = 0;
10852
10853 return window_height_changed_p;
10854 }
10855
10856
10857 \f
10858 /***********************************************************************
10859 Mode Lines and Frame Titles
10860 ***********************************************************************/
10861
10862 /* A buffer for constructing non-propertized mode-line strings and
10863 frame titles in it; allocated from the heap in init_xdisp and
10864 resized as needed in store_mode_line_noprop_char. */
10865
10866 static char *mode_line_noprop_buf;
10867
10868 /* The buffer's end, and a current output position in it. */
10869
10870 static char *mode_line_noprop_buf_end;
10871 static char *mode_line_noprop_ptr;
10872
10873 #define MODE_LINE_NOPROP_LEN(start) \
10874 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10875
10876 static enum {
10877 MODE_LINE_DISPLAY = 0,
10878 MODE_LINE_TITLE,
10879 MODE_LINE_NOPROP,
10880 MODE_LINE_STRING
10881 } mode_line_target;
10882
10883 /* Alist that caches the results of :propertize.
10884 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10885 static Lisp_Object mode_line_proptrans_alist;
10886
10887 /* List of strings making up the mode-line. */
10888 static Lisp_Object mode_line_string_list;
10889
10890 /* Base face property when building propertized mode line string. */
10891 static Lisp_Object mode_line_string_face;
10892 static Lisp_Object mode_line_string_face_prop;
10893
10894
10895 /* Unwind data for mode line strings */
10896
10897 static Lisp_Object Vmode_line_unwind_vector;
10898
10899 static Lisp_Object
10900 format_mode_line_unwind_data (struct frame *target_frame,
10901 struct buffer *obuf,
10902 Lisp_Object owin,
10903 int save_proptrans)
10904 {
10905 Lisp_Object vector, tmp;
10906
10907 /* Reduce consing by keeping one vector in
10908 Vwith_echo_area_save_vector. */
10909 vector = Vmode_line_unwind_vector;
10910 Vmode_line_unwind_vector = Qnil;
10911
10912 if (NILP (vector))
10913 vector = Fmake_vector (make_number (10), Qnil);
10914
10915 ASET (vector, 0, make_number (mode_line_target));
10916 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10917 ASET (vector, 2, mode_line_string_list);
10918 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10919 ASET (vector, 4, mode_line_string_face);
10920 ASET (vector, 5, mode_line_string_face_prop);
10921
10922 if (obuf)
10923 XSETBUFFER (tmp, obuf);
10924 else
10925 tmp = Qnil;
10926 ASET (vector, 6, tmp);
10927 ASET (vector, 7, owin);
10928 if (target_frame)
10929 {
10930 /* Similarly to `with-selected-window', if the operation selects
10931 a window on another frame, we must restore that frame's
10932 selected window, and (for a tty) the top-frame. */
10933 ASET (vector, 8, target_frame->selected_window);
10934 if (FRAME_TERMCAP_P (target_frame))
10935 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10936 }
10937
10938 return vector;
10939 }
10940
10941 static Lisp_Object
10942 unwind_format_mode_line (Lisp_Object vector)
10943 {
10944 Lisp_Object old_window = AREF (vector, 7);
10945 Lisp_Object target_frame_window = AREF (vector, 8);
10946 Lisp_Object old_top_frame = AREF (vector, 9);
10947
10948 mode_line_target = XINT (AREF (vector, 0));
10949 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10950 mode_line_string_list = AREF (vector, 2);
10951 if (! EQ (AREF (vector, 3), Qt))
10952 mode_line_proptrans_alist = AREF (vector, 3);
10953 mode_line_string_face = AREF (vector, 4);
10954 mode_line_string_face_prop = AREF (vector, 5);
10955
10956 /* Select window before buffer, since it may change the buffer. */
10957 if (!NILP (old_window))
10958 {
10959 /* If the operation that we are unwinding had selected a window
10960 on a different frame, reset its frame-selected-window. For a
10961 text terminal, reset its top-frame if necessary. */
10962 if (!NILP (target_frame_window))
10963 {
10964 Lisp_Object frame
10965 = WINDOW_FRAME (XWINDOW (target_frame_window));
10966
10967 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10968 Fselect_window (target_frame_window, Qt);
10969
10970 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10971 Fselect_frame (old_top_frame, Qt);
10972 }
10973
10974 Fselect_window (old_window, Qt);
10975 }
10976
10977 if (!NILP (AREF (vector, 6)))
10978 {
10979 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10980 ASET (vector, 6, Qnil);
10981 }
10982
10983 Vmode_line_unwind_vector = vector;
10984 return Qnil;
10985 }
10986
10987
10988 /* Store a single character C for the frame title in mode_line_noprop_buf.
10989 Re-allocate mode_line_noprop_buf if necessary. */
10990
10991 static void
10992 store_mode_line_noprop_char (char c)
10993 {
10994 /* If output position has reached the end of the allocated buffer,
10995 increase the buffer's size. */
10996 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10997 {
10998 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10999 ptrdiff_t size = len;
11000 mode_line_noprop_buf =
11001 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11002 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11003 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11004 }
11005
11006 *mode_line_noprop_ptr++ = c;
11007 }
11008
11009
11010 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11011 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11012 characters that yield more columns than PRECISION; PRECISION <= 0
11013 means copy the whole string. Pad with spaces until FIELD_WIDTH
11014 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11015 pad. Called from display_mode_element when it is used to build a
11016 frame title. */
11017
11018 static int
11019 store_mode_line_noprop (const char *string, int field_width, int precision)
11020 {
11021 const unsigned char *str = (const unsigned char *) string;
11022 int n = 0;
11023 ptrdiff_t dummy, nbytes;
11024
11025 /* Copy at most PRECISION chars from STR. */
11026 nbytes = strlen (string);
11027 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11028 while (nbytes--)
11029 store_mode_line_noprop_char (*str++);
11030
11031 /* Fill up with spaces until FIELD_WIDTH reached. */
11032 while (field_width > 0
11033 && n < field_width)
11034 {
11035 store_mode_line_noprop_char (' ');
11036 ++n;
11037 }
11038
11039 return n;
11040 }
11041
11042 /***********************************************************************
11043 Frame Titles
11044 ***********************************************************************/
11045
11046 #ifdef HAVE_WINDOW_SYSTEM
11047
11048 /* Set the title of FRAME, if it has changed. The title format is
11049 Vicon_title_format if FRAME is iconified, otherwise it is
11050 frame_title_format. */
11051
11052 static void
11053 x_consider_frame_title (Lisp_Object frame)
11054 {
11055 struct frame *f = XFRAME (frame);
11056
11057 if (FRAME_WINDOW_P (f)
11058 || FRAME_MINIBUF_ONLY_P (f)
11059 || f->explicit_name)
11060 {
11061 /* Do we have more than one visible frame on this X display? */
11062 Lisp_Object tail;
11063 Lisp_Object fmt;
11064 ptrdiff_t title_start;
11065 char *title;
11066 ptrdiff_t len;
11067 struct it it;
11068 ptrdiff_t count = SPECPDL_INDEX ();
11069
11070 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11071 {
11072 Lisp_Object other_frame = XCAR (tail);
11073 struct frame *tf = XFRAME (other_frame);
11074
11075 if (tf != f
11076 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11077 && !FRAME_MINIBUF_ONLY_P (tf)
11078 && !EQ (other_frame, tip_frame)
11079 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11080 break;
11081 }
11082
11083 /* Set global variable indicating that multiple frames exist. */
11084 multiple_frames = CONSP (tail);
11085
11086 /* Switch to the buffer of selected window of the frame. Set up
11087 mode_line_target so that display_mode_element will output into
11088 mode_line_noprop_buf; then display the title. */
11089 record_unwind_protect (unwind_format_mode_line,
11090 format_mode_line_unwind_data
11091 (f, current_buffer, selected_window, 0));
11092
11093 Fselect_window (f->selected_window, Qt);
11094 set_buffer_internal_1
11095 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11096 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11097
11098 mode_line_target = MODE_LINE_TITLE;
11099 title_start = MODE_LINE_NOPROP_LEN (0);
11100 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11101 NULL, DEFAULT_FACE_ID);
11102 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11103 len = MODE_LINE_NOPROP_LEN (title_start);
11104 title = mode_line_noprop_buf + title_start;
11105 unbind_to (count, Qnil);
11106
11107 /* Set the title only if it's changed. This avoids consing in
11108 the common case where it hasn't. (If it turns out that we've
11109 already wasted too much time by walking through the list with
11110 display_mode_element, then we might need to optimize at a
11111 higher level than this.) */
11112 if (! STRINGP (f->name)
11113 || SBYTES (f->name) != len
11114 || memcmp (title, SDATA (f->name), len) != 0)
11115 x_implicitly_set_name (f, make_string (title, len), Qnil);
11116 }
11117 }
11118
11119 #endif /* not HAVE_WINDOW_SYSTEM */
11120
11121 \f
11122 /***********************************************************************
11123 Menu Bars
11124 ***********************************************************************/
11125
11126
11127 /* Prepare for redisplay by updating menu-bar item lists when
11128 appropriate. This can call eval. */
11129
11130 void
11131 prepare_menu_bars (void)
11132 {
11133 int all_windows;
11134 struct gcpro gcpro1, gcpro2;
11135 struct frame *f;
11136 Lisp_Object tooltip_frame;
11137
11138 #ifdef HAVE_WINDOW_SYSTEM
11139 tooltip_frame = tip_frame;
11140 #else
11141 tooltip_frame = Qnil;
11142 #endif
11143
11144 /* Update all frame titles based on their buffer names, etc. We do
11145 this before the menu bars so that the buffer-menu will show the
11146 up-to-date frame titles. */
11147 #ifdef HAVE_WINDOW_SYSTEM
11148 if (windows_or_buffers_changed || update_mode_lines)
11149 {
11150 Lisp_Object tail, frame;
11151
11152 FOR_EACH_FRAME (tail, frame)
11153 {
11154 f = XFRAME (frame);
11155 if (!EQ (frame, tooltip_frame)
11156 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11157 x_consider_frame_title (frame);
11158 }
11159 }
11160 #endif /* HAVE_WINDOW_SYSTEM */
11161
11162 /* Update the menu bar item lists, if appropriate. This has to be
11163 done before any actual redisplay or generation of display lines. */
11164 all_windows = (update_mode_lines
11165 || buffer_shared > 1
11166 || windows_or_buffers_changed);
11167 if (all_windows)
11168 {
11169 Lisp_Object tail, frame;
11170 ptrdiff_t count = SPECPDL_INDEX ();
11171 /* 1 means that update_menu_bar has run its hooks
11172 so any further calls to update_menu_bar shouldn't do so again. */
11173 int menu_bar_hooks_run = 0;
11174
11175 record_unwind_save_match_data ();
11176
11177 FOR_EACH_FRAME (tail, frame)
11178 {
11179 f = XFRAME (frame);
11180
11181 /* Ignore tooltip frame. */
11182 if (EQ (frame, tooltip_frame))
11183 continue;
11184
11185 /* If a window on this frame changed size, report that to
11186 the user and clear the size-change flag. */
11187 if (FRAME_WINDOW_SIZES_CHANGED (f))
11188 {
11189 Lisp_Object functions;
11190
11191 /* Clear flag first in case we get an error below. */
11192 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11193 functions = Vwindow_size_change_functions;
11194 GCPRO2 (tail, functions);
11195
11196 while (CONSP (functions))
11197 {
11198 if (!EQ (XCAR (functions), Qt))
11199 call1 (XCAR (functions), frame);
11200 functions = XCDR (functions);
11201 }
11202 UNGCPRO;
11203 }
11204
11205 GCPRO1 (tail);
11206 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11207 #ifdef HAVE_WINDOW_SYSTEM
11208 update_tool_bar (f, 0);
11209 #endif
11210 #ifdef HAVE_NS
11211 if (windows_or_buffers_changed
11212 && FRAME_NS_P (f))
11213 ns_set_doc_edited
11214 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11215 #endif
11216 UNGCPRO;
11217 }
11218
11219 unbind_to (count, Qnil);
11220 }
11221 else
11222 {
11223 struct frame *sf = SELECTED_FRAME ();
11224 update_menu_bar (sf, 1, 0);
11225 #ifdef HAVE_WINDOW_SYSTEM
11226 update_tool_bar (sf, 1);
11227 #endif
11228 }
11229 }
11230
11231
11232 /* Update the menu bar item list for frame F. This has to be done
11233 before we start to fill in any display lines, because it can call
11234 eval.
11235
11236 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11237
11238 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11239 already ran the menu bar hooks for this redisplay, so there
11240 is no need to run them again. The return value is the
11241 updated value of this flag, to pass to the next call. */
11242
11243 static int
11244 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11245 {
11246 Lisp_Object window;
11247 register struct window *w;
11248
11249 /* If called recursively during a menu update, do nothing. This can
11250 happen when, for instance, an activate-menubar-hook causes a
11251 redisplay. */
11252 if (inhibit_menubar_update)
11253 return hooks_run;
11254
11255 window = FRAME_SELECTED_WINDOW (f);
11256 w = XWINDOW (window);
11257
11258 if (FRAME_WINDOW_P (f)
11259 ?
11260 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11261 || defined (HAVE_NS) || defined (USE_GTK)
11262 FRAME_EXTERNAL_MENU_BAR (f)
11263 #else
11264 FRAME_MENU_BAR_LINES (f) > 0
11265 #endif
11266 : FRAME_MENU_BAR_LINES (f) > 0)
11267 {
11268 /* If the user has switched buffers or windows, we need to
11269 recompute to reflect the new bindings. But we'll
11270 recompute when update_mode_lines is set too; that means
11271 that people can use force-mode-line-update to request
11272 that the menu bar be recomputed. The adverse effect on
11273 the rest of the redisplay algorithm is about the same as
11274 windows_or_buffers_changed anyway. */
11275 if (windows_or_buffers_changed
11276 /* This used to test w->update_mode_line, but we believe
11277 there is no need to recompute the menu in that case. */
11278 || update_mode_lines
11279 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11280 < BUF_MODIFF (XBUFFER (w->buffer)))
11281 != w->last_had_star)
11282 || ((!NILP (Vtransient_mark_mode)
11283 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11284 != !NILP (w->region_showing)))
11285 {
11286 struct buffer *prev = current_buffer;
11287 ptrdiff_t count = SPECPDL_INDEX ();
11288
11289 specbind (Qinhibit_menubar_update, Qt);
11290
11291 set_buffer_internal_1 (XBUFFER (w->buffer));
11292 if (save_match_data)
11293 record_unwind_save_match_data ();
11294 if (NILP (Voverriding_local_map_menu_flag))
11295 {
11296 specbind (Qoverriding_terminal_local_map, Qnil);
11297 specbind (Qoverriding_local_map, Qnil);
11298 }
11299
11300 if (!hooks_run)
11301 {
11302 /* Run the Lucid hook. */
11303 safe_run_hooks (Qactivate_menubar_hook);
11304
11305 /* If it has changed current-menubar from previous value,
11306 really recompute the menu-bar from the value. */
11307 if (! NILP (Vlucid_menu_bar_dirty_flag))
11308 call0 (Qrecompute_lucid_menubar);
11309
11310 safe_run_hooks (Qmenu_bar_update_hook);
11311
11312 hooks_run = 1;
11313 }
11314
11315 XSETFRAME (Vmenu_updating_frame, f);
11316 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11317
11318 /* Redisplay the menu bar in case we changed it. */
11319 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11320 || defined (HAVE_NS) || defined (USE_GTK)
11321 if (FRAME_WINDOW_P (f))
11322 {
11323 #if defined (HAVE_NS)
11324 /* All frames on Mac OS share the same menubar. So only
11325 the selected frame should be allowed to set it. */
11326 if (f == SELECTED_FRAME ())
11327 #endif
11328 set_frame_menubar (f, 0, 0);
11329 }
11330 else
11331 /* On a terminal screen, the menu bar is an ordinary screen
11332 line, and this makes it get updated. */
11333 w->update_mode_line = 1;
11334 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11335 /* In the non-toolkit version, the menu bar is an ordinary screen
11336 line, and this makes it get updated. */
11337 w->update_mode_line = 1;
11338 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11339
11340 unbind_to (count, Qnil);
11341 set_buffer_internal_1 (prev);
11342 }
11343 }
11344
11345 return hooks_run;
11346 }
11347
11348
11349 \f
11350 /***********************************************************************
11351 Output Cursor
11352 ***********************************************************************/
11353
11354 #ifdef HAVE_WINDOW_SYSTEM
11355
11356 /* EXPORT:
11357 Nominal cursor position -- where to draw output.
11358 HPOS and VPOS are window relative glyph matrix coordinates.
11359 X and Y are window relative pixel coordinates. */
11360
11361 struct cursor_pos output_cursor;
11362
11363
11364 /* EXPORT:
11365 Set the global variable output_cursor to CURSOR. All cursor
11366 positions are relative to updated_window. */
11367
11368 void
11369 set_output_cursor (struct cursor_pos *cursor)
11370 {
11371 output_cursor.hpos = cursor->hpos;
11372 output_cursor.vpos = cursor->vpos;
11373 output_cursor.x = cursor->x;
11374 output_cursor.y = cursor->y;
11375 }
11376
11377
11378 /* EXPORT for RIF:
11379 Set a nominal cursor position.
11380
11381 HPOS and VPOS are column/row positions in a window glyph matrix. X
11382 and Y are window text area relative pixel positions.
11383
11384 If this is done during an update, updated_window will contain the
11385 window that is being updated and the position is the future output
11386 cursor position for that window. If updated_window is null, use
11387 selected_window and display the cursor at the given position. */
11388
11389 void
11390 x_cursor_to (int vpos, int hpos, int y, int x)
11391 {
11392 struct window *w;
11393
11394 /* If updated_window is not set, work on selected_window. */
11395 if (updated_window)
11396 w = updated_window;
11397 else
11398 w = XWINDOW (selected_window);
11399
11400 /* Set the output cursor. */
11401 output_cursor.hpos = hpos;
11402 output_cursor.vpos = vpos;
11403 output_cursor.x = x;
11404 output_cursor.y = y;
11405
11406 /* If not called as part of an update, really display the cursor.
11407 This will also set the cursor position of W. */
11408 if (updated_window == NULL)
11409 {
11410 BLOCK_INPUT;
11411 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11412 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11413 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11414 UNBLOCK_INPUT;
11415 }
11416 }
11417
11418 #endif /* HAVE_WINDOW_SYSTEM */
11419
11420 \f
11421 /***********************************************************************
11422 Tool-bars
11423 ***********************************************************************/
11424
11425 #ifdef HAVE_WINDOW_SYSTEM
11426
11427 /* Where the mouse was last time we reported a mouse event. */
11428
11429 FRAME_PTR last_mouse_frame;
11430
11431 /* Tool-bar item index of the item on which a mouse button was pressed
11432 or -1. */
11433
11434 int last_tool_bar_item;
11435
11436
11437 static Lisp_Object
11438 update_tool_bar_unwind (Lisp_Object frame)
11439 {
11440 selected_frame = frame;
11441 return Qnil;
11442 }
11443
11444 /* Update the tool-bar item list for frame F. This has to be done
11445 before we start to fill in any display lines. Called from
11446 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11447 and restore it here. */
11448
11449 static void
11450 update_tool_bar (struct frame *f, int save_match_data)
11451 {
11452 #if defined (USE_GTK) || defined (HAVE_NS)
11453 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11454 #else
11455 int do_update = WINDOWP (f->tool_bar_window)
11456 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11457 #endif
11458
11459 if (do_update)
11460 {
11461 Lisp_Object window;
11462 struct window *w;
11463
11464 window = FRAME_SELECTED_WINDOW (f);
11465 w = XWINDOW (window);
11466
11467 /* If the user has switched buffers or windows, we need to
11468 recompute to reflect the new bindings. But we'll
11469 recompute when update_mode_lines is set too; that means
11470 that people can use force-mode-line-update to request
11471 that the menu bar be recomputed. The adverse effect on
11472 the rest of the redisplay algorithm is about the same as
11473 windows_or_buffers_changed anyway. */
11474 if (windows_or_buffers_changed
11475 || w->update_mode_line
11476 || update_mode_lines
11477 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11478 < BUF_MODIFF (XBUFFER (w->buffer)))
11479 != w->last_had_star)
11480 || ((!NILP (Vtransient_mark_mode)
11481 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11482 != !NILP (w->region_showing)))
11483 {
11484 struct buffer *prev = current_buffer;
11485 ptrdiff_t count = SPECPDL_INDEX ();
11486 Lisp_Object frame, new_tool_bar;
11487 int new_n_tool_bar;
11488 struct gcpro gcpro1;
11489
11490 /* Set current_buffer to the buffer of the selected
11491 window of the frame, so that we get the right local
11492 keymaps. */
11493 set_buffer_internal_1 (XBUFFER (w->buffer));
11494
11495 /* Save match data, if we must. */
11496 if (save_match_data)
11497 record_unwind_save_match_data ();
11498
11499 /* Make sure that we don't accidentally use bogus keymaps. */
11500 if (NILP (Voverriding_local_map_menu_flag))
11501 {
11502 specbind (Qoverriding_terminal_local_map, Qnil);
11503 specbind (Qoverriding_local_map, Qnil);
11504 }
11505
11506 GCPRO1 (new_tool_bar);
11507
11508 /* We must temporarily set the selected frame to this frame
11509 before calling tool_bar_items, because the calculation of
11510 the tool-bar keymap uses the selected frame (see
11511 `tool-bar-make-keymap' in tool-bar.el). */
11512 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11513 XSETFRAME (frame, f);
11514 selected_frame = frame;
11515
11516 /* Build desired tool-bar items from keymaps. */
11517 new_tool_bar
11518 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11519 &new_n_tool_bar);
11520
11521 /* Redisplay the tool-bar if we changed it. */
11522 if (new_n_tool_bar != f->n_tool_bar_items
11523 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11524 {
11525 /* Redisplay that happens asynchronously due to an expose event
11526 may access f->tool_bar_items. Make sure we update both
11527 variables within BLOCK_INPUT so no such event interrupts. */
11528 BLOCK_INPUT;
11529 fset_tool_bar_items (f, new_tool_bar);
11530 f->n_tool_bar_items = new_n_tool_bar;
11531 w->update_mode_line = 1;
11532 UNBLOCK_INPUT;
11533 }
11534
11535 UNGCPRO;
11536
11537 unbind_to (count, Qnil);
11538 set_buffer_internal_1 (prev);
11539 }
11540 }
11541 }
11542
11543
11544 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11545 F's desired tool-bar contents. F->tool_bar_items must have
11546 been set up previously by calling prepare_menu_bars. */
11547
11548 static void
11549 build_desired_tool_bar_string (struct frame *f)
11550 {
11551 int i, size, size_needed;
11552 struct gcpro gcpro1, gcpro2, gcpro3;
11553 Lisp_Object image, plist, props;
11554
11555 image = plist = props = Qnil;
11556 GCPRO3 (image, plist, props);
11557
11558 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11559 Otherwise, make a new string. */
11560
11561 /* The size of the string we might be able to reuse. */
11562 size = (STRINGP (f->desired_tool_bar_string)
11563 ? SCHARS (f->desired_tool_bar_string)
11564 : 0);
11565
11566 /* We need one space in the string for each image. */
11567 size_needed = f->n_tool_bar_items;
11568
11569 /* Reuse f->desired_tool_bar_string, if possible. */
11570 if (size < size_needed || NILP (f->desired_tool_bar_string))
11571 fset_desired_tool_bar_string
11572 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11573 else
11574 {
11575 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11576 Fremove_text_properties (make_number (0), make_number (size),
11577 props, f->desired_tool_bar_string);
11578 }
11579
11580 /* Put a `display' property on the string for the images to display,
11581 put a `menu_item' property on tool-bar items with a value that
11582 is the index of the item in F's tool-bar item vector. */
11583 for (i = 0; i < f->n_tool_bar_items; ++i)
11584 {
11585 #define PROP(IDX) \
11586 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11587
11588 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11589 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11590 int hmargin, vmargin, relief, idx, end;
11591
11592 /* If image is a vector, choose the image according to the
11593 button state. */
11594 image = PROP (TOOL_BAR_ITEM_IMAGES);
11595 if (VECTORP (image))
11596 {
11597 if (enabled_p)
11598 idx = (selected_p
11599 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11600 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11601 else
11602 idx = (selected_p
11603 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11604 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11605
11606 eassert (ASIZE (image) >= idx);
11607 image = AREF (image, idx);
11608 }
11609 else
11610 idx = -1;
11611
11612 /* Ignore invalid image specifications. */
11613 if (!valid_image_p (image))
11614 continue;
11615
11616 /* Display the tool-bar button pressed, or depressed. */
11617 plist = Fcopy_sequence (XCDR (image));
11618
11619 /* Compute margin and relief to draw. */
11620 relief = (tool_bar_button_relief >= 0
11621 ? tool_bar_button_relief
11622 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11623 hmargin = vmargin = relief;
11624
11625 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11626 INT_MAX - max (hmargin, vmargin)))
11627 {
11628 hmargin += XFASTINT (Vtool_bar_button_margin);
11629 vmargin += XFASTINT (Vtool_bar_button_margin);
11630 }
11631 else if (CONSP (Vtool_bar_button_margin))
11632 {
11633 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11634 INT_MAX - hmargin))
11635 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11636
11637 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11638 INT_MAX - vmargin))
11639 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11640 }
11641
11642 if (auto_raise_tool_bar_buttons_p)
11643 {
11644 /* Add a `:relief' property to the image spec if the item is
11645 selected. */
11646 if (selected_p)
11647 {
11648 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11649 hmargin -= relief;
11650 vmargin -= relief;
11651 }
11652 }
11653 else
11654 {
11655 /* If image is selected, display it pressed, i.e. with a
11656 negative relief. If it's not selected, display it with a
11657 raised relief. */
11658 plist = Fplist_put (plist, QCrelief,
11659 (selected_p
11660 ? make_number (-relief)
11661 : make_number (relief)));
11662 hmargin -= relief;
11663 vmargin -= relief;
11664 }
11665
11666 /* Put a margin around the image. */
11667 if (hmargin || vmargin)
11668 {
11669 if (hmargin == vmargin)
11670 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11671 else
11672 plist = Fplist_put (plist, QCmargin,
11673 Fcons (make_number (hmargin),
11674 make_number (vmargin)));
11675 }
11676
11677 /* If button is not enabled, and we don't have special images
11678 for the disabled state, make the image appear disabled by
11679 applying an appropriate algorithm to it. */
11680 if (!enabled_p && idx < 0)
11681 plist = Fplist_put (plist, QCconversion, Qdisabled);
11682
11683 /* Put a `display' text property on the string for the image to
11684 display. Put a `menu-item' property on the string that gives
11685 the start of this item's properties in the tool-bar items
11686 vector. */
11687 image = Fcons (Qimage, plist);
11688 props = list4 (Qdisplay, image,
11689 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11690
11691 /* Let the last image hide all remaining spaces in the tool bar
11692 string. The string can be longer than needed when we reuse a
11693 previous string. */
11694 if (i + 1 == f->n_tool_bar_items)
11695 end = SCHARS (f->desired_tool_bar_string);
11696 else
11697 end = i + 1;
11698 Fadd_text_properties (make_number (i), make_number (end),
11699 props, f->desired_tool_bar_string);
11700 #undef PROP
11701 }
11702
11703 UNGCPRO;
11704 }
11705
11706
11707 /* Display one line of the tool-bar of frame IT->f.
11708
11709 HEIGHT specifies the desired height of the tool-bar line.
11710 If the actual height of the glyph row is less than HEIGHT, the
11711 row's height is increased to HEIGHT, and the icons are centered
11712 vertically in the new height.
11713
11714 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11715 count a final empty row in case the tool-bar width exactly matches
11716 the window width.
11717 */
11718
11719 static void
11720 display_tool_bar_line (struct it *it, int height)
11721 {
11722 struct glyph_row *row = it->glyph_row;
11723 int max_x = it->last_visible_x;
11724 struct glyph *last;
11725
11726 prepare_desired_row (row);
11727 row->y = it->current_y;
11728
11729 /* Note that this isn't made use of if the face hasn't a box,
11730 so there's no need to check the face here. */
11731 it->start_of_box_run_p = 1;
11732
11733 while (it->current_x < max_x)
11734 {
11735 int x, n_glyphs_before, i, nglyphs;
11736 struct it it_before;
11737
11738 /* Get the next display element. */
11739 if (!get_next_display_element (it))
11740 {
11741 /* Don't count empty row if we are counting needed tool-bar lines. */
11742 if (height < 0 && !it->hpos)
11743 return;
11744 break;
11745 }
11746
11747 /* Produce glyphs. */
11748 n_glyphs_before = row->used[TEXT_AREA];
11749 it_before = *it;
11750
11751 PRODUCE_GLYPHS (it);
11752
11753 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11754 i = 0;
11755 x = it_before.current_x;
11756 while (i < nglyphs)
11757 {
11758 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11759
11760 if (x + glyph->pixel_width > max_x)
11761 {
11762 /* Glyph doesn't fit on line. Backtrack. */
11763 row->used[TEXT_AREA] = n_glyphs_before;
11764 *it = it_before;
11765 /* If this is the only glyph on this line, it will never fit on the
11766 tool-bar, so skip it. But ensure there is at least one glyph,
11767 so we don't accidentally disable the tool-bar. */
11768 if (n_glyphs_before == 0
11769 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11770 break;
11771 goto out;
11772 }
11773
11774 ++it->hpos;
11775 x += glyph->pixel_width;
11776 ++i;
11777 }
11778
11779 /* Stop at line end. */
11780 if (ITERATOR_AT_END_OF_LINE_P (it))
11781 break;
11782
11783 set_iterator_to_next (it, 1);
11784 }
11785
11786 out:;
11787
11788 row->displays_text_p = row->used[TEXT_AREA] != 0;
11789
11790 /* Use default face for the border below the tool bar.
11791
11792 FIXME: When auto-resize-tool-bars is grow-only, there is
11793 no additional border below the possibly empty tool-bar lines.
11794 So to make the extra empty lines look "normal", we have to
11795 use the tool-bar face for the border too. */
11796 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11797 it->face_id = DEFAULT_FACE_ID;
11798
11799 extend_face_to_end_of_line (it);
11800 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11801 last->right_box_line_p = 1;
11802 if (last == row->glyphs[TEXT_AREA])
11803 last->left_box_line_p = 1;
11804
11805 /* Make line the desired height and center it vertically. */
11806 if ((height -= it->max_ascent + it->max_descent) > 0)
11807 {
11808 /* Don't add more than one line height. */
11809 height %= FRAME_LINE_HEIGHT (it->f);
11810 it->max_ascent += height / 2;
11811 it->max_descent += (height + 1) / 2;
11812 }
11813
11814 compute_line_metrics (it);
11815
11816 /* If line is empty, make it occupy the rest of the tool-bar. */
11817 if (!row->displays_text_p)
11818 {
11819 row->height = row->phys_height = it->last_visible_y - row->y;
11820 row->visible_height = row->height;
11821 row->ascent = row->phys_ascent = 0;
11822 row->extra_line_spacing = 0;
11823 }
11824
11825 row->full_width_p = 1;
11826 row->continued_p = 0;
11827 row->truncated_on_left_p = 0;
11828 row->truncated_on_right_p = 0;
11829
11830 it->current_x = it->hpos = 0;
11831 it->current_y += row->height;
11832 ++it->vpos;
11833 ++it->glyph_row;
11834 }
11835
11836
11837 /* Max tool-bar height. */
11838
11839 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11840 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11841
11842 /* Value is the number of screen lines needed to make all tool-bar
11843 items of frame F visible. The number of actual rows needed is
11844 returned in *N_ROWS if non-NULL. */
11845
11846 static int
11847 tool_bar_lines_needed (struct frame *f, int *n_rows)
11848 {
11849 struct window *w = XWINDOW (f->tool_bar_window);
11850 struct it it;
11851 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11852 the desired matrix, so use (unused) mode-line row as temporary row to
11853 avoid destroying the first tool-bar row. */
11854 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11855
11856 /* Initialize an iterator for iteration over
11857 F->desired_tool_bar_string in the tool-bar window of frame F. */
11858 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11859 it.first_visible_x = 0;
11860 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11861 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11862 it.paragraph_embedding = L2R;
11863
11864 while (!ITERATOR_AT_END_P (&it))
11865 {
11866 clear_glyph_row (temp_row);
11867 it.glyph_row = temp_row;
11868 display_tool_bar_line (&it, -1);
11869 }
11870 clear_glyph_row (temp_row);
11871
11872 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11873 if (n_rows)
11874 *n_rows = it.vpos > 0 ? it.vpos : -1;
11875
11876 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11877 }
11878
11879
11880 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11881 0, 1, 0,
11882 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11883 (Lisp_Object frame)
11884 {
11885 struct frame *f;
11886 struct window *w;
11887 int nlines = 0;
11888
11889 if (NILP (frame))
11890 frame = selected_frame;
11891 else
11892 CHECK_FRAME (frame);
11893 f = XFRAME (frame);
11894
11895 if (WINDOWP (f->tool_bar_window)
11896 && (w = XWINDOW (f->tool_bar_window),
11897 WINDOW_TOTAL_LINES (w) > 0))
11898 {
11899 update_tool_bar (f, 1);
11900 if (f->n_tool_bar_items)
11901 {
11902 build_desired_tool_bar_string (f);
11903 nlines = tool_bar_lines_needed (f, NULL);
11904 }
11905 }
11906
11907 return make_number (nlines);
11908 }
11909
11910
11911 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11912 height should be changed. */
11913
11914 static int
11915 redisplay_tool_bar (struct frame *f)
11916 {
11917 struct window *w;
11918 struct it it;
11919 struct glyph_row *row;
11920
11921 #if defined (USE_GTK) || defined (HAVE_NS)
11922 if (FRAME_EXTERNAL_TOOL_BAR (f))
11923 update_frame_tool_bar (f);
11924 return 0;
11925 #endif
11926
11927 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11928 do anything. This means you must start with tool-bar-lines
11929 non-zero to get the auto-sizing effect. Or in other words, you
11930 can turn off tool-bars by specifying tool-bar-lines zero. */
11931 if (!WINDOWP (f->tool_bar_window)
11932 || (w = XWINDOW (f->tool_bar_window),
11933 WINDOW_TOTAL_LINES (w) == 0))
11934 return 0;
11935
11936 /* Set up an iterator for the tool-bar window. */
11937 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11938 it.first_visible_x = 0;
11939 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11940 row = it.glyph_row;
11941
11942 /* Build a string that represents the contents of the tool-bar. */
11943 build_desired_tool_bar_string (f);
11944 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11945 /* FIXME: This should be controlled by a user option. But it
11946 doesn't make sense to have an R2L tool bar if the menu bar cannot
11947 be drawn also R2L, and making the menu bar R2L is tricky due
11948 toolkit-specific code that implements it. If an R2L tool bar is
11949 ever supported, display_tool_bar_line should also be augmented to
11950 call unproduce_glyphs like display_line and display_string
11951 do. */
11952 it.paragraph_embedding = L2R;
11953
11954 if (f->n_tool_bar_rows == 0)
11955 {
11956 int nlines;
11957
11958 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11959 nlines != WINDOW_TOTAL_LINES (w)))
11960 {
11961 Lisp_Object frame;
11962 int old_height = WINDOW_TOTAL_LINES (w);
11963
11964 XSETFRAME (frame, f);
11965 Fmodify_frame_parameters (frame,
11966 Fcons (Fcons (Qtool_bar_lines,
11967 make_number (nlines)),
11968 Qnil));
11969 if (WINDOW_TOTAL_LINES (w) != old_height)
11970 {
11971 clear_glyph_matrix (w->desired_matrix);
11972 fonts_changed_p = 1;
11973 return 1;
11974 }
11975 }
11976 }
11977
11978 /* Display as many lines as needed to display all tool-bar items. */
11979
11980 if (f->n_tool_bar_rows > 0)
11981 {
11982 int border, rows, height, extra;
11983
11984 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11985 border = XINT (Vtool_bar_border);
11986 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11987 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11988 else if (EQ (Vtool_bar_border, Qborder_width))
11989 border = f->border_width;
11990 else
11991 border = 0;
11992 if (border < 0)
11993 border = 0;
11994
11995 rows = f->n_tool_bar_rows;
11996 height = max (1, (it.last_visible_y - border) / rows);
11997 extra = it.last_visible_y - border - height * rows;
11998
11999 while (it.current_y < it.last_visible_y)
12000 {
12001 int h = 0;
12002 if (extra > 0 && rows-- > 0)
12003 {
12004 h = (extra + rows - 1) / rows;
12005 extra -= h;
12006 }
12007 display_tool_bar_line (&it, height + h);
12008 }
12009 }
12010 else
12011 {
12012 while (it.current_y < it.last_visible_y)
12013 display_tool_bar_line (&it, 0);
12014 }
12015
12016 /* It doesn't make much sense to try scrolling in the tool-bar
12017 window, so don't do it. */
12018 w->desired_matrix->no_scrolling_p = 1;
12019 w->must_be_updated_p = 1;
12020
12021 if (!NILP (Vauto_resize_tool_bars))
12022 {
12023 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12024 int change_height_p = 0;
12025
12026 /* If we couldn't display everything, change the tool-bar's
12027 height if there is room for more. */
12028 if (IT_STRING_CHARPOS (it) < it.end_charpos
12029 && it.current_y < max_tool_bar_height)
12030 change_height_p = 1;
12031
12032 row = it.glyph_row - 1;
12033
12034 /* If there are blank lines at the end, except for a partially
12035 visible blank line at the end that is smaller than
12036 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12037 if (!row->displays_text_p
12038 && row->height >= FRAME_LINE_HEIGHT (f))
12039 change_height_p = 1;
12040
12041 /* If row displays tool-bar items, but is partially visible,
12042 change the tool-bar's height. */
12043 if (row->displays_text_p
12044 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12045 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12046 change_height_p = 1;
12047
12048 /* Resize windows as needed by changing the `tool-bar-lines'
12049 frame parameter. */
12050 if (change_height_p)
12051 {
12052 Lisp_Object frame;
12053 int old_height = WINDOW_TOTAL_LINES (w);
12054 int nrows;
12055 int nlines = tool_bar_lines_needed (f, &nrows);
12056
12057 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12058 && !f->minimize_tool_bar_window_p)
12059 ? (nlines > old_height)
12060 : (nlines != old_height));
12061 f->minimize_tool_bar_window_p = 0;
12062
12063 if (change_height_p)
12064 {
12065 XSETFRAME (frame, f);
12066 Fmodify_frame_parameters (frame,
12067 Fcons (Fcons (Qtool_bar_lines,
12068 make_number (nlines)),
12069 Qnil));
12070 if (WINDOW_TOTAL_LINES (w) != old_height)
12071 {
12072 clear_glyph_matrix (w->desired_matrix);
12073 f->n_tool_bar_rows = nrows;
12074 fonts_changed_p = 1;
12075 return 1;
12076 }
12077 }
12078 }
12079 }
12080
12081 f->minimize_tool_bar_window_p = 0;
12082 return 0;
12083 }
12084
12085
12086 /* Get information about the tool-bar item which is displayed in GLYPH
12087 on frame F. Return in *PROP_IDX the index where tool-bar item
12088 properties start in F->tool_bar_items. Value is zero if
12089 GLYPH doesn't display a tool-bar item. */
12090
12091 static int
12092 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12093 {
12094 Lisp_Object prop;
12095 int success_p;
12096 int charpos;
12097
12098 /* This function can be called asynchronously, which means we must
12099 exclude any possibility that Fget_text_property signals an
12100 error. */
12101 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12102 charpos = max (0, charpos);
12103
12104 /* Get the text property `menu-item' at pos. The value of that
12105 property is the start index of this item's properties in
12106 F->tool_bar_items. */
12107 prop = Fget_text_property (make_number (charpos),
12108 Qmenu_item, f->current_tool_bar_string);
12109 if (INTEGERP (prop))
12110 {
12111 *prop_idx = XINT (prop);
12112 success_p = 1;
12113 }
12114 else
12115 success_p = 0;
12116
12117 return success_p;
12118 }
12119
12120 \f
12121 /* Get information about the tool-bar item at position X/Y on frame F.
12122 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12123 the current matrix of the tool-bar window of F, or NULL if not
12124 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12125 item in F->tool_bar_items. Value is
12126
12127 -1 if X/Y is not on a tool-bar item
12128 0 if X/Y is on the same item that was highlighted before.
12129 1 otherwise. */
12130
12131 static int
12132 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12133 int *hpos, int *vpos, int *prop_idx)
12134 {
12135 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12136 struct window *w = XWINDOW (f->tool_bar_window);
12137 int area;
12138
12139 /* Find the glyph under X/Y. */
12140 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12141 if (*glyph == NULL)
12142 return -1;
12143
12144 /* Get the start of this tool-bar item's properties in
12145 f->tool_bar_items. */
12146 if (!tool_bar_item_info (f, *glyph, prop_idx))
12147 return -1;
12148
12149 /* Is mouse on the highlighted item? */
12150 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12151 && *vpos >= hlinfo->mouse_face_beg_row
12152 && *vpos <= hlinfo->mouse_face_end_row
12153 && (*vpos > hlinfo->mouse_face_beg_row
12154 || *hpos >= hlinfo->mouse_face_beg_col)
12155 && (*vpos < hlinfo->mouse_face_end_row
12156 || *hpos < hlinfo->mouse_face_end_col
12157 || hlinfo->mouse_face_past_end))
12158 return 0;
12159
12160 return 1;
12161 }
12162
12163
12164 /* EXPORT:
12165 Handle mouse button event on the tool-bar of frame F, at
12166 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12167 0 for button release. MODIFIERS is event modifiers for button
12168 release. */
12169
12170 void
12171 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12172 int modifiers)
12173 {
12174 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12175 struct window *w = XWINDOW (f->tool_bar_window);
12176 int hpos, vpos, prop_idx;
12177 struct glyph *glyph;
12178 Lisp_Object enabled_p;
12179
12180 /* If not on the highlighted tool-bar item, return. */
12181 frame_to_window_pixel_xy (w, &x, &y);
12182 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12183 return;
12184
12185 /* If item is disabled, do nothing. */
12186 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12187 if (NILP (enabled_p))
12188 return;
12189
12190 if (down_p)
12191 {
12192 /* Show item in pressed state. */
12193 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12194 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12195 last_tool_bar_item = prop_idx;
12196 }
12197 else
12198 {
12199 Lisp_Object key, frame;
12200 struct input_event event;
12201 EVENT_INIT (event);
12202
12203 /* Show item in released state. */
12204 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12205 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12206
12207 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12208
12209 XSETFRAME (frame, f);
12210 event.kind = TOOL_BAR_EVENT;
12211 event.frame_or_window = frame;
12212 event.arg = frame;
12213 kbd_buffer_store_event (&event);
12214
12215 event.kind = TOOL_BAR_EVENT;
12216 event.frame_or_window = frame;
12217 event.arg = key;
12218 event.modifiers = modifiers;
12219 kbd_buffer_store_event (&event);
12220 last_tool_bar_item = -1;
12221 }
12222 }
12223
12224
12225 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12226 tool-bar window-relative coordinates X/Y. Called from
12227 note_mouse_highlight. */
12228
12229 static void
12230 note_tool_bar_highlight (struct frame *f, int x, int y)
12231 {
12232 Lisp_Object window = f->tool_bar_window;
12233 struct window *w = XWINDOW (window);
12234 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12235 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12236 int hpos, vpos;
12237 struct glyph *glyph;
12238 struct glyph_row *row;
12239 int i;
12240 Lisp_Object enabled_p;
12241 int prop_idx;
12242 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12243 int mouse_down_p, rc;
12244
12245 /* Function note_mouse_highlight is called with negative X/Y
12246 values when mouse moves outside of the frame. */
12247 if (x <= 0 || y <= 0)
12248 {
12249 clear_mouse_face (hlinfo);
12250 return;
12251 }
12252
12253 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12254 if (rc < 0)
12255 {
12256 /* Not on tool-bar item. */
12257 clear_mouse_face (hlinfo);
12258 return;
12259 }
12260 else if (rc == 0)
12261 /* On same tool-bar item as before. */
12262 goto set_help_echo;
12263
12264 clear_mouse_face (hlinfo);
12265
12266 /* Mouse is down, but on different tool-bar item? */
12267 mouse_down_p = (dpyinfo->grabbed
12268 && f == last_mouse_frame
12269 && FRAME_LIVE_P (f));
12270 if (mouse_down_p
12271 && last_tool_bar_item != prop_idx)
12272 return;
12273
12274 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12275 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12276
12277 /* If tool-bar item is not enabled, don't highlight it. */
12278 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12279 if (!NILP (enabled_p))
12280 {
12281 /* Compute the x-position of the glyph. In front and past the
12282 image is a space. We include this in the highlighted area. */
12283 row = MATRIX_ROW (w->current_matrix, vpos);
12284 for (i = x = 0; i < hpos; ++i)
12285 x += row->glyphs[TEXT_AREA][i].pixel_width;
12286
12287 /* Record this as the current active region. */
12288 hlinfo->mouse_face_beg_col = hpos;
12289 hlinfo->mouse_face_beg_row = vpos;
12290 hlinfo->mouse_face_beg_x = x;
12291 hlinfo->mouse_face_beg_y = row->y;
12292 hlinfo->mouse_face_past_end = 0;
12293
12294 hlinfo->mouse_face_end_col = hpos + 1;
12295 hlinfo->mouse_face_end_row = vpos;
12296 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12297 hlinfo->mouse_face_end_y = row->y;
12298 hlinfo->mouse_face_window = window;
12299 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12300
12301 /* Display it as active. */
12302 show_mouse_face (hlinfo, draw);
12303 hlinfo->mouse_face_image_state = draw;
12304 }
12305
12306 set_help_echo:
12307
12308 /* Set help_echo_string to a help string to display for this tool-bar item.
12309 XTread_socket does the rest. */
12310 help_echo_object = help_echo_window = Qnil;
12311 help_echo_pos = -1;
12312 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12313 if (NILP (help_echo_string))
12314 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12315 }
12316
12317 #endif /* HAVE_WINDOW_SYSTEM */
12318
12319
12320 \f
12321 /************************************************************************
12322 Horizontal scrolling
12323 ************************************************************************/
12324
12325 static int hscroll_window_tree (Lisp_Object);
12326 static int hscroll_windows (Lisp_Object);
12327
12328 /* For all leaf windows in the window tree rooted at WINDOW, set their
12329 hscroll value so that PT is (i) visible in the window, and (ii) so
12330 that it is not within a certain margin at the window's left and
12331 right border. Value is non-zero if any window's hscroll has been
12332 changed. */
12333
12334 static int
12335 hscroll_window_tree (Lisp_Object window)
12336 {
12337 int hscrolled_p = 0;
12338 int hscroll_relative_p = FLOATP (Vhscroll_step);
12339 int hscroll_step_abs = 0;
12340 double hscroll_step_rel = 0;
12341
12342 if (hscroll_relative_p)
12343 {
12344 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12345 if (hscroll_step_rel < 0)
12346 {
12347 hscroll_relative_p = 0;
12348 hscroll_step_abs = 0;
12349 }
12350 }
12351 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12352 {
12353 hscroll_step_abs = XINT (Vhscroll_step);
12354 if (hscroll_step_abs < 0)
12355 hscroll_step_abs = 0;
12356 }
12357 else
12358 hscroll_step_abs = 0;
12359
12360 while (WINDOWP (window))
12361 {
12362 struct window *w = XWINDOW (window);
12363
12364 if (WINDOWP (w->hchild))
12365 hscrolled_p |= hscroll_window_tree (w->hchild);
12366 else if (WINDOWP (w->vchild))
12367 hscrolled_p |= hscroll_window_tree (w->vchild);
12368 else if (w->cursor.vpos >= 0)
12369 {
12370 int h_margin;
12371 int text_area_width;
12372 struct glyph_row *current_cursor_row
12373 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12374 struct glyph_row *desired_cursor_row
12375 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12376 struct glyph_row *cursor_row
12377 = (desired_cursor_row->enabled_p
12378 ? desired_cursor_row
12379 : current_cursor_row);
12380 int row_r2l_p = cursor_row->reversed_p;
12381
12382 text_area_width = window_box_width (w, TEXT_AREA);
12383
12384 /* Scroll when cursor is inside this scroll margin. */
12385 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12386
12387 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12388 /* For left-to-right rows, hscroll when cursor is either
12389 (i) inside the right hscroll margin, or (ii) if it is
12390 inside the left margin and the window is already
12391 hscrolled. */
12392 && ((!row_r2l_p
12393 && ((w->hscroll
12394 && w->cursor.x <= h_margin)
12395 || (cursor_row->enabled_p
12396 && cursor_row->truncated_on_right_p
12397 && (w->cursor.x >= text_area_width - h_margin))))
12398 /* For right-to-left rows, the logic is similar,
12399 except that rules for scrolling to left and right
12400 are reversed. E.g., if cursor.x <= h_margin, we
12401 need to hscroll "to the right" unconditionally,
12402 and that will scroll the screen to the left so as
12403 to reveal the next portion of the row. */
12404 || (row_r2l_p
12405 && ((cursor_row->enabled_p
12406 /* FIXME: It is confusing to set the
12407 truncated_on_right_p flag when R2L rows
12408 are actually truncated on the left. */
12409 && cursor_row->truncated_on_right_p
12410 && w->cursor.x <= h_margin)
12411 || (w->hscroll
12412 && (w->cursor.x >= text_area_width - h_margin))))))
12413 {
12414 struct it it;
12415 ptrdiff_t hscroll;
12416 struct buffer *saved_current_buffer;
12417 ptrdiff_t pt;
12418 int wanted_x;
12419
12420 /* Find point in a display of infinite width. */
12421 saved_current_buffer = current_buffer;
12422 current_buffer = XBUFFER (w->buffer);
12423
12424 if (w == XWINDOW (selected_window))
12425 pt = PT;
12426 else
12427 {
12428 pt = marker_position (w->pointm);
12429 pt = max (BEGV, pt);
12430 pt = min (ZV, pt);
12431 }
12432
12433 /* Move iterator to pt starting at cursor_row->start in
12434 a line with infinite width. */
12435 init_to_row_start (&it, w, cursor_row);
12436 it.last_visible_x = INFINITY;
12437 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12438 current_buffer = saved_current_buffer;
12439
12440 /* Position cursor in window. */
12441 if (!hscroll_relative_p && hscroll_step_abs == 0)
12442 hscroll = max (0, (it.current_x
12443 - (ITERATOR_AT_END_OF_LINE_P (&it)
12444 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12445 : (text_area_width / 2))))
12446 / FRAME_COLUMN_WIDTH (it.f);
12447 else if ((!row_r2l_p
12448 && w->cursor.x >= text_area_width - h_margin)
12449 || (row_r2l_p && w->cursor.x <= h_margin))
12450 {
12451 if (hscroll_relative_p)
12452 wanted_x = text_area_width * (1 - hscroll_step_rel)
12453 - h_margin;
12454 else
12455 wanted_x = text_area_width
12456 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12457 - h_margin;
12458 hscroll
12459 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12460 }
12461 else
12462 {
12463 if (hscroll_relative_p)
12464 wanted_x = text_area_width * hscroll_step_rel
12465 + h_margin;
12466 else
12467 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12468 + h_margin;
12469 hscroll
12470 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12471 }
12472 hscroll = max (hscroll, w->min_hscroll);
12473
12474 /* Don't prevent redisplay optimizations if hscroll
12475 hasn't changed, as it will unnecessarily slow down
12476 redisplay. */
12477 if (w->hscroll != hscroll)
12478 {
12479 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12480 w->hscroll = hscroll;
12481 hscrolled_p = 1;
12482 }
12483 }
12484 }
12485
12486 window = w->next;
12487 }
12488
12489 /* Value is non-zero if hscroll of any leaf window has been changed. */
12490 return hscrolled_p;
12491 }
12492
12493
12494 /* Set hscroll so that cursor is visible and not inside horizontal
12495 scroll margins for all windows in the tree rooted at WINDOW. See
12496 also hscroll_window_tree above. Value is non-zero if any window's
12497 hscroll has been changed. If it has, desired matrices on the frame
12498 of WINDOW are cleared. */
12499
12500 static int
12501 hscroll_windows (Lisp_Object window)
12502 {
12503 int hscrolled_p = hscroll_window_tree (window);
12504 if (hscrolled_p)
12505 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12506 return hscrolled_p;
12507 }
12508
12509
12510 \f
12511 /************************************************************************
12512 Redisplay
12513 ************************************************************************/
12514
12515 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12516 to a non-zero value. This is sometimes handy to have in a debugger
12517 session. */
12518
12519 #ifdef GLYPH_DEBUG
12520
12521 /* First and last unchanged row for try_window_id. */
12522
12523 static int debug_first_unchanged_at_end_vpos;
12524 static int debug_last_unchanged_at_beg_vpos;
12525
12526 /* Delta vpos and y. */
12527
12528 static int debug_dvpos, debug_dy;
12529
12530 /* Delta in characters and bytes for try_window_id. */
12531
12532 static ptrdiff_t debug_delta, debug_delta_bytes;
12533
12534 /* Values of window_end_pos and window_end_vpos at the end of
12535 try_window_id. */
12536
12537 static ptrdiff_t debug_end_vpos;
12538
12539 /* Append a string to W->desired_matrix->method. FMT is a printf
12540 format string. If trace_redisplay_p is non-zero also printf the
12541 resulting string to stderr. */
12542
12543 static void debug_method_add (struct window *, char const *, ...)
12544 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12545
12546 static void
12547 debug_method_add (struct window *w, char const *fmt, ...)
12548 {
12549 char *method = w->desired_matrix->method;
12550 int len = strlen (method);
12551 int size = sizeof w->desired_matrix->method;
12552 int remaining = size - len - 1;
12553 va_list ap;
12554
12555 if (len && remaining)
12556 {
12557 method[len] = '|';
12558 --remaining, ++len;
12559 }
12560
12561 va_start (ap, fmt);
12562 vsnprintf (method + len, remaining + 1, fmt, ap);
12563 va_end (ap);
12564
12565 if (trace_redisplay_p)
12566 fprintf (stderr, "%p (%s): %s\n",
12567 w,
12568 ((BUFFERP (w->buffer)
12569 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12570 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12571 : "no buffer"),
12572 method + len);
12573 }
12574
12575 #endif /* GLYPH_DEBUG */
12576
12577
12578 /* Value is non-zero if all changes in window W, which displays
12579 current_buffer, are in the text between START and END. START is a
12580 buffer position, END is given as a distance from Z. Used in
12581 redisplay_internal for display optimization. */
12582
12583 static inline int
12584 text_outside_line_unchanged_p (struct window *w,
12585 ptrdiff_t start, ptrdiff_t end)
12586 {
12587 int unchanged_p = 1;
12588
12589 /* If text or overlays have changed, see where. */
12590 if (w->last_modified < MODIFF
12591 || w->last_overlay_modified < OVERLAY_MODIFF)
12592 {
12593 /* Gap in the line? */
12594 if (GPT < start || Z - GPT < end)
12595 unchanged_p = 0;
12596
12597 /* Changes start in front of the line, or end after it? */
12598 if (unchanged_p
12599 && (BEG_UNCHANGED < start - 1
12600 || END_UNCHANGED < end))
12601 unchanged_p = 0;
12602
12603 /* If selective display, can't optimize if changes start at the
12604 beginning of the line. */
12605 if (unchanged_p
12606 && INTEGERP (BVAR (current_buffer, selective_display))
12607 && XINT (BVAR (current_buffer, selective_display)) > 0
12608 && (BEG_UNCHANGED < start || GPT <= start))
12609 unchanged_p = 0;
12610
12611 /* If there are overlays at the start or end of the line, these
12612 may have overlay strings with newlines in them. A change at
12613 START, for instance, may actually concern the display of such
12614 overlay strings as well, and they are displayed on different
12615 lines. So, quickly rule out this case. (For the future, it
12616 might be desirable to implement something more telling than
12617 just BEG/END_UNCHANGED.) */
12618 if (unchanged_p)
12619 {
12620 if (BEG + BEG_UNCHANGED == start
12621 && overlay_touches_p (start))
12622 unchanged_p = 0;
12623 if (END_UNCHANGED == end
12624 && overlay_touches_p (Z - end))
12625 unchanged_p = 0;
12626 }
12627
12628 /* Under bidi reordering, adding or deleting a character in the
12629 beginning of a paragraph, before the first strong directional
12630 character, can change the base direction of the paragraph (unless
12631 the buffer specifies a fixed paragraph direction), which will
12632 require to redisplay the whole paragraph. It might be worthwhile
12633 to find the paragraph limits and widen the range of redisplayed
12634 lines to that, but for now just give up this optimization. */
12635 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12636 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12637 unchanged_p = 0;
12638 }
12639
12640 return unchanged_p;
12641 }
12642
12643
12644 /* Do a frame update, taking possible shortcuts into account. This is
12645 the main external entry point for redisplay.
12646
12647 If the last redisplay displayed an echo area message and that message
12648 is no longer requested, we clear the echo area or bring back the
12649 mini-buffer if that is in use. */
12650
12651 void
12652 redisplay (void)
12653 {
12654 redisplay_internal ();
12655 }
12656
12657
12658 static Lisp_Object
12659 overlay_arrow_string_or_property (Lisp_Object var)
12660 {
12661 Lisp_Object val;
12662
12663 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12664 return val;
12665
12666 return Voverlay_arrow_string;
12667 }
12668
12669 /* Return 1 if there are any overlay-arrows in current_buffer. */
12670 static int
12671 overlay_arrow_in_current_buffer_p (void)
12672 {
12673 Lisp_Object vlist;
12674
12675 for (vlist = Voverlay_arrow_variable_list;
12676 CONSP (vlist);
12677 vlist = XCDR (vlist))
12678 {
12679 Lisp_Object var = XCAR (vlist);
12680 Lisp_Object val;
12681
12682 if (!SYMBOLP (var))
12683 continue;
12684 val = find_symbol_value (var);
12685 if (MARKERP (val)
12686 && current_buffer == XMARKER (val)->buffer)
12687 return 1;
12688 }
12689 return 0;
12690 }
12691
12692
12693 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12694 has changed. */
12695
12696 static int
12697 overlay_arrows_changed_p (void)
12698 {
12699 Lisp_Object vlist;
12700
12701 for (vlist = Voverlay_arrow_variable_list;
12702 CONSP (vlist);
12703 vlist = XCDR (vlist))
12704 {
12705 Lisp_Object var = XCAR (vlist);
12706 Lisp_Object val, pstr;
12707
12708 if (!SYMBOLP (var))
12709 continue;
12710 val = find_symbol_value (var);
12711 if (!MARKERP (val))
12712 continue;
12713 if (! EQ (COERCE_MARKER (val),
12714 Fget (var, Qlast_arrow_position))
12715 || ! (pstr = overlay_arrow_string_or_property (var),
12716 EQ (pstr, Fget (var, Qlast_arrow_string))))
12717 return 1;
12718 }
12719 return 0;
12720 }
12721
12722 /* Mark overlay arrows to be updated on next redisplay. */
12723
12724 static void
12725 update_overlay_arrows (int up_to_date)
12726 {
12727 Lisp_Object vlist;
12728
12729 for (vlist = Voverlay_arrow_variable_list;
12730 CONSP (vlist);
12731 vlist = XCDR (vlist))
12732 {
12733 Lisp_Object var = XCAR (vlist);
12734
12735 if (!SYMBOLP (var))
12736 continue;
12737
12738 if (up_to_date > 0)
12739 {
12740 Lisp_Object val = find_symbol_value (var);
12741 Fput (var, Qlast_arrow_position,
12742 COERCE_MARKER (val));
12743 Fput (var, Qlast_arrow_string,
12744 overlay_arrow_string_or_property (var));
12745 }
12746 else if (up_to_date < 0
12747 || !NILP (Fget (var, Qlast_arrow_position)))
12748 {
12749 Fput (var, Qlast_arrow_position, Qt);
12750 Fput (var, Qlast_arrow_string, Qt);
12751 }
12752 }
12753 }
12754
12755
12756 /* Return overlay arrow string to display at row.
12757 Return integer (bitmap number) for arrow bitmap in left fringe.
12758 Return nil if no overlay arrow. */
12759
12760 static Lisp_Object
12761 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12762 {
12763 Lisp_Object vlist;
12764
12765 for (vlist = Voverlay_arrow_variable_list;
12766 CONSP (vlist);
12767 vlist = XCDR (vlist))
12768 {
12769 Lisp_Object var = XCAR (vlist);
12770 Lisp_Object val;
12771
12772 if (!SYMBOLP (var))
12773 continue;
12774
12775 val = find_symbol_value (var);
12776
12777 if (MARKERP (val)
12778 && current_buffer == XMARKER (val)->buffer
12779 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12780 {
12781 if (FRAME_WINDOW_P (it->f)
12782 /* FIXME: if ROW->reversed_p is set, this should test
12783 the right fringe, not the left one. */
12784 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12785 {
12786 #ifdef HAVE_WINDOW_SYSTEM
12787 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12788 {
12789 int fringe_bitmap;
12790 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12791 return make_number (fringe_bitmap);
12792 }
12793 #endif
12794 return make_number (-1); /* Use default arrow bitmap. */
12795 }
12796 return overlay_arrow_string_or_property (var);
12797 }
12798 }
12799
12800 return Qnil;
12801 }
12802
12803 /* Return 1 if point moved out of or into a composition. Otherwise
12804 return 0. PREV_BUF and PREV_PT are the last point buffer and
12805 position. BUF and PT are the current point buffer and position. */
12806
12807 static int
12808 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12809 struct buffer *buf, ptrdiff_t pt)
12810 {
12811 ptrdiff_t start, end;
12812 Lisp_Object prop;
12813 Lisp_Object buffer;
12814
12815 XSETBUFFER (buffer, buf);
12816 /* Check a composition at the last point if point moved within the
12817 same buffer. */
12818 if (prev_buf == buf)
12819 {
12820 if (prev_pt == pt)
12821 /* Point didn't move. */
12822 return 0;
12823
12824 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12825 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12826 && COMPOSITION_VALID_P (start, end, prop)
12827 && start < prev_pt && end > prev_pt)
12828 /* The last point was within the composition. Return 1 iff
12829 point moved out of the composition. */
12830 return (pt <= start || pt >= end);
12831 }
12832
12833 /* Check a composition at the current point. */
12834 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12835 && find_composition (pt, -1, &start, &end, &prop, buffer)
12836 && COMPOSITION_VALID_P (start, end, prop)
12837 && start < pt && end > pt);
12838 }
12839
12840
12841 /* Reconsider the setting of B->clip_changed which is displayed
12842 in window W. */
12843
12844 static inline void
12845 reconsider_clip_changes (struct window *w, struct buffer *b)
12846 {
12847 if (b->clip_changed
12848 && !NILP (w->window_end_valid)
12849 && w->current_matrix->buffer == b
12850 && w->current_matrix->zv == BUF_ZV (b)
12851 && w->current_matrix->begv == BUF_BEGV (b))
12852 b->clip_changed = 0;
12853
12854 /* If display wasn't paused, and W is not a tool bar window, see if
12855 point has been moved into or out of a composition. In that case,
12856 we set b->clip_changed to 1 to force updating the screen. If
12857 b->clip_changed has already been set to 1, we can skip this
12858 check. */
12859 if (!b->clip_changed
12860 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12861 {
12862 ptrdiff_t pt;
12863
12864 if (w == XWINDOW (selected_window))
12865 pt = PT;
12866 else
12867 pt = marker_position (w->pointm);
12868
12869 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12870 || pt != w->last_point)
12871 && check_point_in_composition (w->current_matrix->buffer,
12872 w->last_point,
12873 XBUFFER (w->buffer), pt))
12874 b->clip_changed = 1;
12875 }
12876 }
12877 \f
12878
12879 /* Select FRAME to forward the values of frame-local variables into C
12880 variables so that the redisplay routines can access those values
12881 directly. */
12882
12883 static void
12884 select_frame_for_redisplay (Lisp_Object frame)
12885 {
12886 Lisp_Object tail, tem;
12887 Lisp_Object old = selected_frame;
12888 struct Lisp_Symbol *sym;
12889
12890 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12891
12892 selected_frame = frame;
12893
12894 do {
12895 for (tail = XFRAME (frame)->param_alist;
12896 CONSP (tail); tail = XCDR (tail))
12897 if (CONSP (XCAR (tail))
12898 && (tem = XCAR (XCAR (tail)),
12899 SYMBOLP (tem))
12900 && (sym = indirect_variable (XSYMBOL (tem)),
12901 sym->redirect == SYMBOL_LOCALIZED)
12902 && sym->val.blv->frame_local)
12903 /* Use find_symbol_value rather than Fsymbol_value
12904 to avoid an error if it is void. */
12905 find_symbol_value (tem);
12906 } while (!EQ (frame, old) && (frame = old, 1));
12907 }
12908
12909
12910 #define STOP_POLLING \
12911 do { if (! polling_stopped_here) stop_polling (); \
12912 polling_stopped_here = 1; } while (0)
12913
12914 #define RESUME_POLLING \
12915 do { if (polling_stopped_here) start_polling (); \
12916 polling_stopped_here = 0; } while (0)
12917
12918
12919 /* Perhaps in the future avoid recentering windows if it
12920 is not necessary; currently that causes some problems. */
12921
12922 static void
12923 redisplay_internal (void)
12924 {
12925 struct window *w = XWINDOW (selected_window);
12926 struct window *sw;
12927 struct frame *fr;
12928 int pending;
12929 int must_finish = 0;
12930 struct text_pos tlbufpos, tlendpos;
12931 int number_of_visible_frames;
12932 ptrdiff_t count, count1;
12933 struct frame *sf;
12934 int polling_stopped_here = 0;
12935 Lisp_Object old_frame = selected_frame;
12936
12937 /* Non-zero means redisplay has to consider all windows on all
12938 frames. Zero means, only selected_window is considered. */
12939 int consider_all_windows_p;
12940
12941 /* Non-zero means redisplay has to redisplay the miniwindow */
12942 int update_miniwindow_p = 0;
12943
12944 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12945
12946 /* No redisplay if running in batch mode or frame is not yet fully
12947 initialized, or redisplay is explicitly turned off by setting
12948 Vinhibit_redisplay. */
12949 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12950 || !NILP (Vinhibit_redisplay))
12951 return;
12952
12953 /* Don't examine these until after testing Vinhibit_redisplay.
12954 When Emacs is shutting down, perhaps because its connection to
12955 X has dropped, we should not look at them at all. */
12956 fr = XFRAME (w->frame);
12957 sf = SELECTED_FRAME ();
12958
12959 if (!fr->glyphs_initialized_p)
12960 return;
12961
12962 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12963 if (popup_activated ())
12964 return;
12965 #endif
12966
12967 /* I don't think this happens but let's be paranoid. */
12968 if (redisplaying_p)
12969 return;
12970
12971 /* Record a function that clears redisplaying_p
12972 when we leave this function. */
12973 count = SPECPDL_INDEX ();
12974 record_unwind_protect (unwind_redisplay, selected_frame);
12975 redisplaying_p = 1;
12976 specbind (Qinhibit_free_realized_faces, Qnil);
12977
12978 {
12979 Lisp_Object tail, frame;
12980
12981 FOR_EACH_FRAME (tail, frame)
12982 {
12983 struct frame *f = XFRAME (frame);
12984 f->already_hscrolled_p = 0;
12985 }
12986 }
12987
12988 retry:
12989 /* Remember the currently selected window. */
12990 sw = w;
12991
12992 if (!EQ (old_frame, selected_frame)
12993 && FRAME_LIVE_P (XFRAME (old_frame)))
12994 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12995 selected_frame and selected_window to be temporarily out-of-sync so
12996 when we come back here via `goto retry', we need to resync because we
12997 may need to run Elisp code (via prepare_menu_bars). */
12998 select_frame_for_redisplay (old_frame);
12999
13000 pending = 0;
13001 reconsider_clip_changes (w, current_buffer);
13002 last_escape_glyph_frame = NULL;
13003 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13004 last_glyphless_glyph_frame = NULL;
13005 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13006
13007 /* If new fonts have been loaded that make a glyph matrix adjustment
13008 necessary, do it. */
13009 if (fonts_changed_p)
13010 {
13011 adjust_glyphs (NULL);
13012 ++windows_or_buffers_changed;
13013 fonts_changed_p = 0;
13014 }
13015
13016 /* If face_change_count is non-zero, init_iterator will free all
13017 realized faces, which includes the faces referenced from current
13018 matrices. So, we can't reuse current matrices in this case. */
13019 if (face_change_count)
13020 ++windows_or_buffers_changed;
13021
13022 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13023 && FRAME_TTY (sf)->previous_frame != sf)
13024 {
13025 /* Since frames on a single ASCII terminal share the same
13026 display area, displaying a different frame means redisplay
13027 the whole thing. */
13028 windows_or_buffers_changed++;
13029 SET_FRAME_GARBAGED (sf);
13030 #ifndef DOS_NT
13031 set_tty_color_mode (FRAME_TTY (sf), sf);
13032 #endif
13033 FRAME_TTY (sf)->previous_frame = sf;
13034 }
13035
13036 /* Set the visible flags for all frames. Do this before checking
13037 for resized or garbaged frames; they want to know if their frames
13038 are visible. See the comment in frame.h for
13039 FRAME_SAMPLE_VISIBILITY. */
13040 {
13041 Lisp_Object tail, frame;
13042
13043 number_of_visible_frames = 0;
13044
13045 FOR_EACH_FRAME (tail, frame)
13046 {
13047 struct frame *f = XFRAME (frame);
13048
13049 FRAME_SAMPLE_VISIBILITY (f);
13050 if (FRAME_VISIBLE_P (f))
13051 ++number_of_visible_frames;
13052 clear_desired_matrices (f);
13053 }
13054 }
13055
13056 /* Notice any pending interrupt request to change frame size. */
13057 do_pending_window_change (1);
13058
13059 /* do_pending_window_change could change the selected_window due to
13060 frame resizing which makes the selected window too small. */
13061 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13062 {
13063 sw = w;
13064 reconsider_clip_changes (w, current_buffer);
13065 }
13066
13067 /* Clear frames marked as garbaged. */
13068 if (frame_garbaged)
13069 clear_garbaged_frames ();
13070
13071 /* Build menubar and tool-bar items. */
13072 if (NILP (Vmemory_full))
13073 prepare_menu_bars ();
13074
13075 if (windows_or_buffers_changed)
13076 update_mode_lines++;
13077
13078 /* Detect case that we need to write or remove a star in the mode line. */
13079 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13080 {
13081 w->update_mode_line = 1;
13082 if (buffer_shared > 1)
13083 update_mode_lines++;
13084 }
13085
13086 /* Avoid invocation of point motion hooks by `current_column' below. */
13087 count1 = SPECPDL_INDEX ();
13088 specbind (Qinhibit_point_motion_hooks, Qt);
13089
13090 /* If %c is in the mode line, update it if needed. */
13091 if (!NILP (w->column_number_displayed)
13092 /* This alternative quickly identifies a common case
13093 where no change is needed. */
13094 && !(PT == w->last_point
13095 && w->last_modified >= MODIFF
13096 && w->last_overlay_modified >= OVERLAY_MODIFF)
13097 && (XFASTINT (w->column_number_displayed) != current_column ()))
13098 w->update_mode_line = 1;
13099
13100 unbind_to (count1, Qnil);
13101
13102 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13103
13104 /* The variable buffer_shared is set in redisplay_window and
13105 indicates that we redisplay a buffer in different windows. See
13106 there. */
13107 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13108 || cursor_type_changed);
13109
13110 /* If specs for an arrow have changed, do thorough redisplay
13111 to ensure we remove any arrow that should no longer exist. */
13112 if (overlay_arrows_changed_p ())
13113 consider_all_windows_p = windows_or_buffers_changed = 1;
13114
13115 /* Normally the message* functions will have already displayed and
13116 updated the echo area, but the frame may have been trashed, or
13117 the update may have been preempted, so display the echo area
13118 again here. Checking message_cleared_p captures the case that
13119 the echo area should be cleared. */
13120 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13121 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13122 || (message_cleared_p
13123 && minibuf_level == 0
13124 /* If the mini-window is currently selected, this means the
13125 echo-area doesn't show through. */
13126 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13127 {
13128 int window_height_changed_p = echo_area_display (0);
13129
13130 if (message_cleared_p)
13131 update_miniwindow_p = 1;
13132
13133 must_finish = 1;
13134
13135 /* If we don't display the current message, don't clear the
13136 message_cleared_p flag, because, if we did, we wouldn't clear
13137 the echo area in the next redisplay which doesn't preserve
13138 the echo area. */
13139 if (!display_last_displayed_message_p)
13140 message_cleared_p = 0;
13141
13142 if (fonts_changed_p)
13143 goto retry;
13144 else if (window_height_changed_p)
13145 {
13146 consider_all_windows_p = 1;
13147 ++update_mode_lines;
13148 ++windows_or_buffers_changed;
13149
13150 /* If window configuration was changed, frames may have been
13151 marked garbaged. Clear them or we will experience
13152 surprises wrt scrolling. */
13153 if (frame_garbaged)
13154 clear_garbaged_frames ();
13155 }
13156 }
13157 else if (EQ (selected_window, minibuf_window)
13158 && (current_buffer->clip_changed
13159 || w->last_modified < MODIFF
13160 || w->last_overlay_modified < OVERLAY_MODIFF)
13161 && resize_mini_window (w, 0))
13162 {
13163 /* Resized active mini-window to fit the size of what it is
13164 showing if its contents might have changed. */
13165 must_finish = 1;
13166 /* FIXME: this causes all frames to be updated, which seems unnecessary
13167 since only the current frame needs to be considered. This function needs
13168 to be rewritten with two variables, consider_all_windows and
13169 consider_all_frames. */
13170 consider_all_windows_p = 1;
13171 ++windows_or_buffers_changed;
13172 ++update_mode_lines;
13173
13174 /* If window configuration was changed, frames may have been
13175 marked garbaged. Clear them or we will experience
13176 surprises wrt scrolling. */
13177 if (frame_garbaged)
13178 clear_garbaged_frames ();
13179 }
13180
13181
13182 /* If showing the region, and mark has changed, we must redisplay
13183 the whole window. The assignment to this_line_start_pos prevents
13184 the optimization directly below this if-statement. */
13185 if (((!NILP (Vtransient_mark_mode)
13186 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13187 != !NILP (w->region_showing))
13188 || (!NILP (w->region_showing)
13189 && !EQ (w->region_showing,
13190 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13191 CHARPOS (this_line_start_pos) = 0;
13192
13193 /* Optimize the case that only the line containing the cursor in the
13194 selected window has changed. Variables starting with this_ are
13195 set in display_line and record information about the line
13196 containing the cursor. */
13197 tlbufpos = this_line_start_pos;
13198 tlendpos = this_line_end_pos;
13199 if (!consider_all_windows_p
13200 && CHARPOS (tlbufpos) > 0
13201 && !w->update_mode_line
13202 && !current_buffer->clip_changed
13203 && !current_buffer->prevent_redisplay_optimizations_p
13204 && FRAME_VISIBLE_P (XFRAME (w->frame))
13205 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13206 /* Make sure recorded data applies to current buffer, etc. */
13207 && this_line_buffer == current_buffer
13208 && current_buffer == XBUFFER (w->buffer)
13209 && !w->force_start
13210 && !w->optional_new_start
13211 /* Point must be on the line that we have info recorded about. */
13212 && PT >= CHARPOS (tlbufpos)
13213 && PT <= Z - CHARPOS (tlendpos)
13214 /* All text outside that line, including its final newline,
13215 must be unchanged. */
13216 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13217 CHARPOS (tlendpos)))
13218 {
13219 if (CHARPOS (tlbufpos) > BEGV
13220 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13221 && (CHARPOS (tlbufpos) == ZV
13222 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13223 /* Former continuation line has disappeared by becoming empty. */
13224 goto cancel;
13225 else if (w->last_modified < MODIFF
13226 || w->last_overlay_modified < OVERLAY_MODIFF
13227 || MINI_WINDOW_P (w))
13228 {
13229 /* We have to handle the case of continuation around a
13230 wide-column character (see the comment in indent.c around
13231 line 1340).
13232
13233 For instance, in the following case:
13234
13235 -------- Insert --------
13236 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13237 J_I_ ==> J_I_ `^^' are cursors.
13238 ^^ ^^
13239 -------- --------
13240
13241 As we have to redraw the line above, we cannot use this
13242 optimization. */
13243
13244 struct it it;
13245 int line_height_before = this_line_pixel_height;
13246
13247 /* Note that start_display will handle the case that the
13248 line starting at tlbufpos is a continuation line. */
13249 start_display (&it, w, tlbufpos);
13250
13251 /* Implementation note: It this still necessary? */
13252 if (it.current_x != this_line_start_x)
13253 goto cancel;
13254
13255 TRACE ((stderr, "trying display optimization 1\n"));
13256 w->cursor.vpos = -1;
13257 overlay_arrow_seen = 0;
13258 it.vpos = this_line_vpos;
13259 it.current_y = this_line_y;
13260 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13261 display_line (&it);
13262
13263 /* If line contains point, is not continued,
13264 and ends at same distance from eob as before, we win. */
13265 if (w->cursor.vpos >= 0
13266 /* Line is not continued, otherwise this_line_start_pos
13267 would have been set to 0 in display_line. */
13268 && CHARPOS (this_line_start_pos)
13269 /* Line ends as before. */
13270 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13271 /* Line has same height as before. Otherwise other lines
13272 would have to be shifted up or down. */
13273 && this_line_pixel_height == line_height_before)
13274 {
13275 /* If this is not the window's last line, we must adjust
13276 the charstarts of the lines below. */
13277 if (it.current_y < it.last_visible_y)
13278 {
13279 struct glyph_row *row
13280 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13281 ptrdiff_t delta, delta_bytes;
13282
13283 /* We used to distinguish between two cases here,
13284 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13285 when the line ends in a newline or the end of the
13286 buffer's accessible portion. But both cases did
13287 the same, so they were collapsed. */
13288 delta = (Z
13289 - CHARPOS (tlendpos)
13290 - MATRIX_ROW_START_CHARPOS (row));
13291 delta_bytes = (Z_BYTE
13292 - BYTEPOS (tlendpos)
13293 - MATRIX_ROW_START_BYTEPOS (row));
13294
13295 increment_matrix_positions (w->current_matrix,
13296 this_line_vpos + 1,
13297 w->current_matrix->nrows,
13298 delta, delta_bytes);
13299 }
13300
13301 /* If this row displays text now but previously didn't,
13302 or vice versa, w->window_end_vpos may have to be
13303 adjusted. */
13304 if ((it.glyph_row - 1)->displays_text_p)
13305 {
13306 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13307 wset_window_end_vpos (w, make_number (this_line_vpos));
13308 }
13309 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13310 && this_line_vpos > 0)
13311 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13312 wset_window_end_valid (w, Qnil);
13313
13314 /* Update hint: No need to try to scroll in update_window. */
13315 w->desired_matrix->no_scrolling_p = 1;
13316
13317 #ifdef GLYPH_DEBUG
13318 *w->desired_matrix->method = 0;
13319 debug_method_add (w, "optimization 1");
13320 #endif
13321 #ifdef HAVE_WINDOW_SYSTEM
13322 update_window_fringes (w, 0);
13323 #endif
13324 goto update;
13325 }
13326 else
13327 goto cancel;
13328 }
13329 else if (/* Cursor position hasn't changed. */
13330 PT == w->last_point
13331 /* Make sure the cursor was last displayed
13332 in this window. Otherwise we have to reposition it. */
13333 && 0 <= w->cursor.vpos
13334 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13335 {
13336 if (!must_finish)
13337 {
13338 do_pending_window_change (1);
13339 /* If selected_window changed, redisplay again. */
13340 if (WINDOWP (selected_window)
13341 && (w = XWINDOW (selected_window)) != sw)
13342 goto retry;
13343
13344 /* We used to always goto end_of_redisplay here, but this
13345 isn't enough if we have a blinking cursor. */
13346 if (w->cursor_off_p == w->last_cursor_off_p)
13347 goto end_of_redisplay;
13348 }
13349 goto update;
13350 }
13351 /* If highlighting the region, or if the cursor is in the echo area,
13352 then we can't just move the cursor. */
13353 else if (! (!NILP (Vtransient_mark_mode)
13354 && !NILP (BVAR (current_buffer, mark_active)))
13355 && (EQ (selected_window,
13356 BVAR (current_buffer, last_selected_window))
13357 || highlight_nonselected_windows)
13358 && NILP (w->region_showing)
13359 && NILP (Vshow_trailing_whitespace)
13360 && !cursor_in_echo_area)
13361 {
13362 struct it it;
13363 struct glyph_row *row;
13364
13365 /* Skip from tlbufpos to PT and see where it is. Note that
13366 PT may be in invisible text. If so, we will end at the
13367 next visible position. */
13368 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13369 NULL, DEFAULT_FACE_ID);
13370 it.current_x = this_line_start_x;
13371 it.current_y = this_line_y;
13372 it.vpos = this_line_vpos;
13373
13374 /* The call to move_it_to stops in front of PT, but
13375 moves over before-strings. */
13376 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13377
13378 if (it.vpos == this_line_vpos
13379 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13380 row->enabled_p))
13381 {
13382 eassert (this_line_vpos == it.vpos);
13383 eassert (this_line_y == it.current_y);
13384 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13385 #ifdef GLYPH_DEBUG
13386 *w->desired_matrix->method = 0;
13387 debug_method_add (w, "optimization 3");
13388 #endif
13389 goto update;
13390 }
13391 else
13392 goto cancel;
13393 }
13394
13395 cancel:
13396 /* Text changed drastically or point moved off of line. */
13397 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13398 }
13399
13400 CHARPOS (this_line_start_pos) = 0;
13401 consider_all_windows_p |= buffer_shared > 1;
13402 ++clear_face_cache_count;
13403 #ifdef HAVE_WINDOW_SYSTEM
13404 ++clear_image_cache_count;
13405 #endif
13406
13407 /* Build desired matrices, and update the display. If
13408 consider_all_windows_p is non-zero, do it for all windows on all
13409 frames. Otherwise do it for selected_window, only. */
13410
13411 if (consider_all_windows_p)
13412 {
13413 Lisp_Object tail, frame;
13414
13415 FOR_EACH_FRAME (tail, frame)
13416 XFRAME (frame)->updated_p = 0;
13417
13418 /* Recompute # windows showing selected buffer. This will be
13419 incremented each time such a window is displayed. */
13420 buffer_shared = 0;
13421
13422 FOR_EACH_FRAME (tail, frame)
13423 {
13424 struct frame *f = XFRAME (frame);
13425
13426 /* We don't have to do anything for unselected terminal
13427 frames. */
13428 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13429 && !EQ (FRAME_TTY (f)->top_frame, frame))
13430 continue;
13431
13432 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13433 {
13434 if (! EQ (frame, selected_frame))
13435 /* Select the frame, for the sake of frame-local
13436 variables. */
13437 select_frame_for_redisplay (frame);
13438
13439 /* Mark all the scroll bars to be removed; we'll redeem
13440 the ones we want when we redisplay their windows. */
13441 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13442 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13443
13444 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13445 redisplay_windows (FRAME_ROOT_WINDOW (f));
13446
13447 /* The X error handler may have deleted that frame. */
13448 if (!FRAME_LIVE_P (f))
13449 continue;
13450
13451 /* Any scroll bars which redisplay_windows should have
13452 nuked should now go away. */
13453 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13454 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13455
13456 /* If fonts changed, display again. */
13457 /* ??? rms: I suspect it is a mistake to jump all the way
13458 back to retry here. It should just retry this frame. */
13459 if (fonts_changed_p)
13460 goto retry;
13461
13462 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13463 {
13464 /* See if we have to hscroll. */
13465 if (!f->already_hscrolled_p)
13466 {
13467 f->already_hscrolled_p = 1;
13468 if (hscroll_windows (f->root_window))
13469 goto retry;
13470 }
13471
13472 /* Prevent various kinds of signals during display
13473 update. stdio is not robust about handling
13474 signals, which can cause an apparent I/O
13475 error. */
13476 if (interrupt_input)
13477 unrequest_sigio ();
13478 STOP_POLLING;
13479
13480 /* Update the display. */
13481 set_window_update_flags (XWINDOW (f->root_window), 1);
13482 pending |= update_frame (f, 0, 0);
13483 f->updated_p = 1;
13484 }
13485 }
13486 }
13487
13488 if (!EQ (old_frame, selected_frame)
13489 && FRAME_LIVE_P (XFRAME (old_frame)))
13490 /* We played a bit fast-and-loose above and allowed selected_frame
13491 and selected_window to be temporarily out-of-sync but let's make
13492 sure this stays contained. */
13493 select_frame_for_redisplay (old_frame);
13494 eassert (EQ (XFRAME (selected_frame)->selected_window,
13495 selected_window));
13496
13497 if (!pending)
13498 {
13499 /* Do the mark_window_display_accurate after all windows have
13500 been redisplayed because this call resets flags in buffers
13501 which are needed for proper redisplay. */
13502 FOR_EACH_FRAME (tail, frame)
13503 {
13504 struct frame *f = XFRAME (frame);
13505 if (f->updated_p)
13506 {
13507 mark_window_display_accurate (f->root_window, 1);
13508 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13509 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13510 }
13511 }
13512 }
13513 }
13514 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13515 {
13516 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13517 struct frame *mini_frame;
13518
13519 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13520 /* Use list_of_error, not Qerror, so that
13521 we catch only errors and don't run the debugger. */
13522 internal_condition_case_1 (redisplay_window_1, selected_window,
13523 list_of_error,
13524 redisplay_window_error);
13525 if (update_miniwindow_p)
13526 internal_condition_case_1 (redisplay_window_1, mini_window,
13527 list_of_error,
13528 redisplay_window_error);
13529
13530 /* Compare desired and current matrices, perform output. */
13531
13532 update:
13533 /* If fonts changed, display again. */
13534 if (fonts_changed_p)
13535 goto retry;
13536
13537 /* Prevent various kinds of signals during display update.
13538 stdio is not robust about handling signals,
13539 which can cause an apparent I/O error. */
13540 if (interrupt_input)
13541 unrequest_sigio ();
13542 STOP_POLLING;
13543
13544 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13545 {
13546 if (hscroll_windows (selected_window))
13547 goto retry;
13548
13549 XWINDOW (selected_window)->must_be_updated_p = 1;
13550 pending = update_frame (sf, 0, 0);
13551 }
13552
13553 /* We may have called echo_area_display at the top of this
13554 function. If the echo area is on another frame, that may
13555 have put text on a frame other than the selected one, so the
13556 above call to update_frame would not have caught it. Catch
13557 it here. */
13558 mini_window = FRAME_MINIBUF_WINDOW (sf);
13559 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13560
13561 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13562 {
13563 XWINDOW (mini_window)->must_be_updated_p = 1;
13564 pending |= update_frame (mini_frame, 0, 0);
13565 if (!pending && hscroll_windows (mini_window))
13566 goto retry;
13567 }
13568 }
13569
13570 /* If display was paused because of pending input, make sure we do a
13571 thorough update the next time. */
13572 if (pending)
13573 {
13574 /* Prevent the optimization at the beginning of
13575 redisplay_internal that tries a single-line update of the
13576 line containing the cursor in the selected window. */
13577 CHARPOS (this_line_start_pos) = 0;
13578
13579 /* Let the overlay arrow be updated the next time. */
13580 update_overlay_arrows (0);
13581
13582 /* If we pause after scrolling, some rows in the current
13583 matrices of some windows are not valid. */
13584 if (!WINDOW_FULL_WIDTH_P (w)
13585 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13586 update_mode_lines = 1;
13587 }
13588 else
13589 {
13590 if (!consider_all_windows_p)
13591 {
13592 /* This has already been done above if
13593 consider_all_windows_p is set. */
13594 mark_window_display_accurate_1 (w, 1);
13595
13596 /* Say overlay arrows are up to date. */
13597 update_overlay_arrows (1);
13598
13599 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13600 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13601 }
13602
13603 update_mode_lines = 0;
13604 windows_or_buffers_changed = 0;
13605 cursor_type_changed = 0;
13606 }
13607
13608 /* Start SIGIO interrupts coming again. Having them off during the
13609 code above makes it less likely one will discard output, but not
13610 impossible, since there might be stuff in the system buffer here.
13611 But it is much hairier to try to do anything about that. */
13612 if (interrupt_input)
13613 request_sigio ();
13614 RESUME_POLLING;
13615
13616 /* If a frame has become visible which was not before, redisplay
13617 again, so that we display it. Expose events for such a frame
13618 (which it gets when becoming visible) don't call the parts of
13619 redisplay constructing glyphs, so simply exposing a frame won't
13620 display anything in this case. So, we have to display these
13621 frames here explicitly. */
13622 if (!pending)
13623 {
13624 Lisp_Object tail, frame;
13625 int new_count = 0;
13626
13627 FOR_EACH_FRAME (tail, frame)
13628 {
13629 int this_is_visible = 0;
13630
13631 if (XFRAME (frame)->visible)
13632 this_is_visible = 1;
13633 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13634 if (XFRAME (frame)->visible)
13635 this_is_visible = 1;
13636
13637 if (this_is_visible)
13638 new_count++;
13639 }
13640
13641 if (new_count != number_of_visible_frames)
13642 windows_or_buffers_changed++;
13643 }
13644
13645 /* Change frame size now if a change is pending. */
13646 do_pending_window_change (1);
13647
13648 /* If we just did a pending size change, or have additional
13649 visible frames, or selected_window changed, redisplay again. */
13650 if ((windows_or_buffers_changed && !pending)
13651 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13652 goto retry;
13653
13654 /* Clear the face and image caches.
13655
13656 We used to do this only if consider_all_windows_p. But the cache
13657 needs to be cleared if a timer creates images in the current
13658 buffer (e.g. the test case in Bug#6230). */
13659
13660 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13661 {
13662 clear_face_cache (0);
13663 clear_face_cache_count = 0;
13664 }
13665
13666 #ifdef HAVE_WINDOW_SYSTEM
13667 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13668 {
13669 clear_image_caches (Qnil);
13670 clear_image_cache_count = 0;
13671 }
13672 #endif /* HAVE_WINDOW_SYSTEM */
13673
13674 end_of_redisplay:
13675 unbind_to (count, Qnil);
13676 RESUME_POLLING;
13677 }
13678
13679
13680 /* Redisplay, but leave alone any recent echo area message unless
13681 another message has been requested in its place.
13682
13683 This is useful in situations where you need to redisplay but no
13684 user action has occurred, making it inappropriate for the message
13685 area to be cleared. See tracking_off and
13686 wait_reading_process_output for examples of these situations.
13687
13688 FROM_WHERE is an integer saying from where this function was
13689 called. This is useful for debugging. */
13690
13691 void
13692 redisplay_preserve_echo_area (int from_where)
13693 {
13694 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13695
13696 if (!NILP (echo_area_buffer[1]))
13697 {
13698 /* We have a previously displayed message, but no current
13699 message. Redisplay the previous message. */
13700 display_last_displayed_message_p = 1;
13701 redisplay_internal ();
13702 display_last_displayed_message_p = 0;
13703 }
13704 else
13705 redisplay_internal ();
13706
13707 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13708 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13709 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13710 }
13711
13712
13713 /* Function registered with record_unwind_protect in redisplay_internal.
13714 Clear redisplaying_p. Also, select the previously
13715 selected frame, unless it has been deleted (by an X connection
13716 failure during redisplay, for example). */
13717
13718 static Lisp_Object
13719 unwind_redisplay (Lisp_Object old_frame)
13720 {
13721 redisplaying_p = 0;
13722 if (! EQ (old_frame, selected_frame)
13723 && FRAME_LIVE_P (XFRAME (old_frame)))
13724 select_frame_for_redisplay (old_frame);
13725 return Qnil;
13726 }
13727
13728
13729 /* Mark the display of window W as accurate or inaccurate. If
13730 ACCURATE_P is non-zero mark display of W as accurate. If
13731 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13732 redisplay_internal is called. */
13733
13734 static void
13735 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13736 {
13737 if (BUFFERP (w->buffer))
13738 {
13739 struct buffer *b = XBUFFER (w->buffer);
13740
13741 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13742 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13743 w->last_had_star
13744 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13745
13746 if (accurate_p)
13747 {
13748 b->clip_changed = 0;
13749 b->prevent_redisplay_optimizations_p = 0;
13750
13751 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13752 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13753 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13754 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13755
13756 w->current_matrix->buffer = b;
13757 w->current_matrix->begv = BUF_BEGV (b);
13758 w->current_matrix->zv = BUF_ZV (b);
13759
13760 w->last_cursor = w->cursor;
13761 w->last_cursor_off_p = w->cursor_off_p;
13762
13763 if (w == XWINDOW (selected_window))
13764 w->last_point = BUF_PT (b);
13765 else
13766 w->last_point = XMARKER (w->pointm)->charpos;
13767 }
13768 }
13769
13770 if (accurate_p)
13771 {
13772 wset_window_end_valid (w, w->buffer);
13773 w->update_mode_line = 0;
13774 }
13775 }
13776
13777
13778 /* Mark the display of windows in the window tree rooted at WINDOW as
13779 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13780 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13781 be redisplayed the next time redisplay_internal is called. */
13782
13783 void
13784 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13785 {
13786 struct window *w;
13787
13788 for (; !NILP (window); window = w->next)
13789 {
13790 w = XWINDOW (window);
13791 mark_window_display_accurate_1 (w, accurate_p);
13792
13793 if (!NILP (w->vchild))
13794 mark_window_display_accurate (w->vchild, accurate_p);
13795 if (!NILP (w->hchild))
13796 mark_window_display_accurate (w->hchild, accurate_p);
13797 }
13798
13799 if (accurate_p)
13800 {
13801 update_overlay_arrows (1);
13802 }
13803 else
13804 {
13805 /* Force a thorough redisplay the next time by setting
13806 last_arrow_position and last_arrow_string to t, which is
13807 unequal to any useful value of Voverlay_arrow_... */
13808 update_overlay_arrows (-1);
13809 }
13810 }
13811
13812
13813 /* Return value in display table DP (Lisp_Char_Table *) for character
13814 C. Since a display table doesn't have any parent, we don't have to
13815 follow parent. Do not call this function directly but use the
13816 macro DISP_CHAR_VECTOR. */
13817
13818 Lisp_Object
13819 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13820 {
13821 Lisp_Object val;
13822
13823 if (ASCII_CHAR_P (c))
13824 {
13825 val = dp->ascii;
13826 if (SUB_CHAR_TABLE_P (val))
13827 val = XSUB_CHAR_TABLE (val)->contents[c];
13828 }
13829 else
13830 {
13831 Lisp_Object table;
13832
13833 XSETCHAR_TABLE (table, dp);
13834 val = char_table_ref (table, c);
13835 }
13836 if (NILP (val))
13837 val = dp->defalt;
13838 return val;
13839 }
13840
13841
13842 \f
13843 /***********************************************************************
13844 Window Redisplay
13845 ***********************************************************************/
13846
13847 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13848
13849 static void
13850 redisplay_windows (Lisp_Object window)
13851 {
13852 while (!NILP (window))
13853 {
13854 struct window *w = XWINDOW (window);
13855
13856 if (!NILP (w->hchild))
13857 redisplay_windows (w->hchild);
13858 else if (!NILP (w->vchild))
13859 redisplay_windows (w->vchild);
13860 else if (!NILP (w->buffer))
13861 {
13862 displayed_buffer = XBUFFER (w->buffer);
13863 /* Use list_of_error, not Qerror, so that
13864 we catch only errors and don't run the debugger. */
13865 internal_condition_case_1 (redisplay_window_0, window,
13866 list_of_error,
13867 redisplay_window_error);
13868 }
13869
13870 window = w->next;
13871 }
13872 }
13873
13874 static Lisp_Object
13875 redisplay_window_error (Lisp_Object ignore)
13876 {
13877 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13878 return Qnil;
13879 }
13880
13881 static Lisp_Object
13882 redisplay_window_0 (Lisp_Object window)
13883 {
13884 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13885 redisplay_window (window, 0);
13886 return Qnil;
13887 }
13888
13889 static Lisp_Object
13890 redisplay_window_1 (Lisp_Object window)
13891 {
13892 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13893 redisplay_window (window, 1);
13894 return Qnil;
13895 }
13896 \f
13897
13898 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13899 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13900 which positions recorded in ROW differ from current buffer
13901 positions.
13902
13903 Return 0 if cursor is not on this row, 1 otherwise. */
13904
13905 static int
13906 set_cursor_from_row (struct window *w, struct glyph_row *row,
13907 struct glyph_matrix *matrix,
13908 ptrdiff_t delta, ptrdiff_t delta_bytes,
13909 int dy, int dvpos)
13910 {
13911 struct glyph *glyph = row->glyphs[TEXT_AREA];
13912 struct glyph *end = glyph + row->used[TEXT_AREA];
13913 struct glyph *cursor = NULL;
13914 /* The last known character position in row. */
13915 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13916 int x = row->x;
13917 ptrdiff_t pt_old = PT - delta;
13918 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13919 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13920 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13921 /* A glyph beyond the edge of TEXT_AREA which we should never
13922 touch. */
13923 struct glyph *glyphs_end = end;
13924 /* Non-zero means we've found a match for cursor position, but that
13925 glyph has the avoid_cursor_p flag set. */
13926 int match_with_avoid_cursor = 0;
13927 /* Non-zero means we've seen at least one glyph that came from a
13928 display string. */
13929 int string_seen = 0;
13930 /* Largest and smallest buffer positions seen so far during scan of
13931 glyph row. */
13932 ptrdiff_t bpos_max = pos_before;
13933 ptrdiff_t bpos_min = pos_after;
13934 /* Last buffer position covered by an overlay string with an integer
13935 `cursor' property. */
13936 ptrdiff_t bpos_covered = 0;
13937 /* Non-zero means the display string on which to display the cursor
13938 comes from a text property, not from an overlay. */
13939 int string_from_text_prop = 0;
13940
13941 /* Don't even try doing anything if called for a mode-line or
13942 header-line row, since the rest of the code isn't prepared to
13943 deal with such calamities. */
13944 eassert (!row->mode_line_p);
13945 if (row->mode_line_p)
13946 return 0;
13947
13948 /* Skip over glyphs not having an object at the start and the end of
13949 the row. These are special glyphs like truncation marks on
13950 terminal frames. */
13951 if (row->displays_text_p)
13952 {
13953 if (!row->reversed_p)
13954 {
13955 while (glyph < end
13956 && INTEGERP (glyph->object)
13957 && glyph->charpos < 0)
13958 {
13959 x += glyph->pixel_width;
13960 ++glyph;
13961 }
13962 while (end > glyph
13963 && INTEGERP ((end - 1)->object)
13964 /* CHARPOS is zero for blanks and stretch glyphs
13965 inserted by extend_face_to_end_of_line. */
13966 && (end - 1)->charpos <= 0)
13967 --end;
13968 glyph_before = glyph - 1;
13969 glyph_after = end;
13970 }
13971 else
13972 {
13973 struct glyph *g;
13974
13975 /* If the glyph row is reversed, we need to process it from back
13976 to front, so swap the edge pointers. */
13977 glyphs_end = end = glyph - 1;
13978 glyph += row->used[TEXT_AREA] - 1;
13979
13980 while (glyph > end + 1
13981 && INTEGERP (glyph->object)
13982 && glyph->charpos < 0)
13983 {
13984 --glyph;
13985 x -= glyph->pixel_width;
13986 }
13987 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13988 --glyph;
13989 /* By default, in reversed rows we put the cursor on the
13990 rightmost (first in the reading order) glyph. */
13991 for (g = end + 1; g < glyph; g++)
13992 x += g->pixel_width;
13993 while (end < glyph
13994 && INTEGERP ((end + 1)->object)
13995 && (end + 1)->charpos <= 0)
13996 ++end;
13997 glyph_before = glyph + 1;
13998 glyph_after = end;
13999 }
14000 }
14001 else if (row->reversed_p)
14002 {
14003 /* In R2L rows that don't display text, put the cursor on the
14004 rightmost glyph. Case in point: an empty last line that is
14005 part of an R2L paragraph. */
14006 cursor = end - 1;
14007 /* Avoid placing the cursor on the last glyph of the row, where
14008 on terminal frames we hold the vertical border between
14009 adjacent windows. */
14010 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14011 && !WINDOW_RIGHTMOST_P (w)
14012 && cursor == row->glyphs[LAST_AREA] - 1)
14013 cursor--;
14014 x = -1; /* will be computed below, at label compute_x */
14015 }
14016
14017 /* Step 1: Try to find the glyph whose character position
14018 corresponds to point. If that's not possible, find 2 glyphs
14019 whose character positions are the closest to point, one before
14020 point, the other after it. */
14021 if (!row->reversed_p)
14022 while (/* not marched to end of glyph row */
14023 glyph < end
14024 /* glyph was not inserted by redisplay for internal purposes */
14025 && !INTEGERP (glyph->object))
14026 {
14027 if (BUFFERP (glyph->object))
14028 {
14029 ptrdiff_t dpos = glyph->charpos - pt_old;
14030
14031 if (glyph->charpos > bpos_max)
14032 bpos_max = glyph->charpos;
14033 if (glyph->charpos < bpos_min)
14034 bpos_min = glyph->charpos;
14035 if (!glyph->avoid_cursor_p)
14036 {
14037 /* If we hit point, we've found the glyph on which to
14038 display the cursor. */
14039 if (dpos == 0)
14040 {
14041 match_with_avoid_cursor = 0;
14042 break;
14043 }
14044 /* See if we've found a better approximation to
14045 POS_BEFORE or to POS_AFTER. */
14046 if (0 > dpos && dpos > pos_before - pt_old)
14047 {
14048 pos_before = glyph->charpos;
14049 glyph_before = glyph;
14050 }
14051 else if (0 < dpos && dpos < pos_after - pt_old)
14052 {
14053 pos_after = glyph->charpos;
14054 glyph_after = glyph;
14055 }
14056 }
14057 else if (dpos == 0)
14058 match_with_avoid_cursor = 1;
14059 }
14060 else if (STRINGP (glyph->object))
14061 {
14062 Lisp_Object chprop;
14063 ptrdiff_t glyph_pos = glyph->charpos;
14064
14065 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14066 glyph->object);
14067 if (!NILP (chprop))
14068 {
14069 /* If the string came from a `display' text property,
14070 look up the buffer position of that property and
14071 use that position to update bpos_max, as if we
14072 actually saw such a position in one of the row's
14073 glyphs. This helps with supporting integer values
14074 of `cursor' property on the display string in
14075 situations where most or all of the row's buffer
14076 text is completely covered by display properties,
14077 so that no glyph with valid buffer positions is
14078 ever seen in the row. */
14079 ptrdiff_t prop_pos =
14080 string_buffer_position_lim (glyph->object, pos_before,
14081 pos_after, 0);
14082
14083 if (prop_pos >= pos_before)
14084 bpos_max = prop_pos - 1;
14085 }
14086 if (INTEGERP (chprop))
14087 {
14088 bpos_covered = bpos_max + XINT (chprop);
14089 /* If the `cursor' property covers buffer positions up
14090 to and including point, we should display cursor on
14091 this glyph. Note that, if a `cursor' property on one
14092 of the string's characters has an integer value, we
14093 will break out of the loop below _before_ we get to
14094 the position match above. IOW, integer values of
14095 the `cursor' property override the "exact match for
14096 point" strategy of positioning the cursor. */
14097 /* Implementation note: bpos_max == pt_old when, e.g.,
14098 we are in an empty line, where bpos_max is set to
14099 MATRIX_ROW_START_CHARPOS, see above. */
14100 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14101 {
14102 cursor = glyph;
14103 break;
14104 }
14105 }
14106
14107 string_seen = 1;
14108 }
14109 x += glyph->pixel_width;
14110 ++glyph;
14111 }
14112 else if (glyph > end) /* row is reversed */
14113 while (!INTEGERP (glyph->object))
14114 {
14115 if (BUFFERP (glyph->object))
14116 {
14117 ptrdiff_t dpos = glyph->charpos - pt_old;
14118
14119 if (glyph->charpos > bpos_max)
14120 bpos_max = glyph->charpos;
14121 if (glyph->charpos < bpos_min)
14122 bpos_min = glyph->charpos;
14123 if (!glyph->avoid_cursor_p)
14124 {
14125 if (dpos == 0)
14126 {
14127 match_with_avoid_cursor = 0;
14128 break;
14129 }
14130 if (0 > dpos && dpos > pos_before - pt_old)
14131 {
14132 pos_before = glyph->charpos;
14133 glyph_before = glyph;
14134 }
14135 else if (0 < dpos && dpos < pos_after - pt_old)
14136 {
14137 pos_after = glyph->charpos;
14138 glyph_after = glyph;
14139 }
14140 }
14141 else if (dpos == 0)
14142 match_with_avoid_cursor = 1;
14143 }
14144 else if (STRINGP (glyph->object))
14145 {
14146 Lisp_Object chprop;
14147 ptrdiff_t glyph_pos = glyph->charpos;
14148
14149 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14150 glyph->object);
14151 if (!NILP (chprop))
14152 {
14153 ptrdiff_t prop_pos =
14154 string_buffer_position_lim (glyph->object, pos_before,
14155 pos_after, 0);
14156
14157 if (prop_pos >= pos_before)
14158 bpos_max = prop_pos - 1;
14159 }
14160 if (INTEGERP (chprop))
14161 {
14162 bpos_covered = bpos_max + XINT (chprop);
14163 /* If the `cursor' property covers buffer positions up
14164 to and including point, we should display cursor on
14165 this glyph. */
14166 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14167 {
14168 cursor = glyph;
14169 break;
14170 }
14171 }
14172 string_seen = 1;
14173 }
14174 --glyph;
14175 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14176 {
14177 x--; /* can't use any pixel_width */
14178 break;
14179 }
14180 x -= glyph->pixel_width;
14181 }
14182
14183 /* Step 2: If we didn't find an exact match for point, we need to
14184 look for a proper place to put the cursor among glyphs between
14185 GLYPH_BEFORE and GLYPH_AFTER. */
14186 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14187 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14188 && bpos_covered < pt_old)
14189 {
14190 /* An empty line has a single glyph whose OBJECT is zero and
14191 whose CHARPOS is the position of a newline on that line.
14192 Note that on a TTY, there are more glyphs after that, which
14193 were produced by extend_face_to_end_of_line, but their
14194 CHARPOS is zero or negative. */
14195 int empty_line_p =
14196 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14197 && INTEGERP (glyph->object) && glyph->charpos > 0;
14198
14199 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14200 {
14201 ptrdiff_t ellipsis_pos;
14202
14203 /* Scan back over the ellipsis glyphs. */
14204 if (!row->reversed_p)
14205 {
14206 ellipsis_pos = (glyph - 1)->charpos;
14207 while (glyph > row->glyphs[TEXT_AREA]
14208 && (glyph - 1)->charpos == ellipsis_pos)
14209 glyph--, x -= glyph->pixel_width;
14210 /* That loop always goes one position too far, including
14211 the glyph before the ellipsis. So scan forward over
14212 that one. */
14213 x += glyph->pixel_width;
14214 glyph++;
14215 }
14216 else /* row is reversed */
14217 {
14218 ellipsis_pos = (glyph + 1)->charpos;
14219 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14220 && (glyph + 1)->charpos == ellipsis_pos)
14221 glyph++, x += glyph->pixel_width;
14222 x -= glyph->pixel_width;
14223 glyph--;
14224 }
14225 }
14226 else if (match_with_avoid_cursor)
14227 {
14228 cursor = glyph_after;
14229 x = -1;
14230 }
14231 else if (string_seen)
14232 {
14233 int incr = row->reversed_p ? -1 : +1;
14234
14235 /* Need to find the glyph that came out of a string which is
14236 present at point. That glyph is somewhere between
14237 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14238 positioned between POS_BEFORE and POS_AFTER in the
14239 buffer. */
14240 struct glyph *start, *stop;
14241 ptrdiff_t pos = pos_before;
14242
14243 x = -1;
14244
14245 /* If the row ends in a newline from a display string,
14246 reordering could have moved the glyphs belonging to the
14247 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14248 in this case we extend the search to the last glyph in
14249 the row that was not inserted by redisplay. */
14250 if (row->ends_in_newline_from_string_p)
14251 {
14252 glyph_after = end;
14253 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14254 }
14255
14256 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14257 correspond to POS_BEFORE and POS_AFTER, respectively. We
14258 need START and STOP in the order that corresponds to the
14259 row's direction as given by its reversed_p flag. If the
14260 directionality of characters between POS_BEFORE and
14261 POS_AFTER is the opposite of the row's base direction,
14262 these characters will have been reordered for display,
14263 and we need to reverse START and STOP. */
14264 if (!row->reversed_p)
14265 {
14266 start = min (glyph_before, glyph_after);
14267 stop = max (glyph_before, glyph_after);
14268 }
14269 else
14270 {
14271 start = max (glyph_before, glyph_after);
14272 stop = min (glyph_before, glyph_after);
14273 }
14274 for (glyph = start + incr;
14275 row->reversed_p ? glyph > stop : glyph < stop; )
14276 {
14277
14278 /* Any glyphs that come from the buffer are here because
14279 of bidi reordering. Skip them, and only pay
14280 attention to glyphs that came from some string. */
14281 if (STRINGP (glyph->object))
14282 {
14283 Lisp_Object str;
14284 ptrdiff_t tem;
14285 /* If the display property covers the newline, we
14286 need to search for it one position farther. */
14287 ptrdiff_t lim = pos_after
14288 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14289
14290 string_from_text_prop = 0;
14291 str = glyph->object;
14292 tem = string_buffer_position_lim (str, pos, lim, 0);
14293 if (tem == 0 /* from overlay */
14294 || pos <= tem)
14295 {
14296 /* If the string from which this glyph came is
14297 found in the buffer at point, or at position
14298 that is closer to point than pos_after, then
14299 we've found the glyph we've been looking for.
14300 If it comes from an overlay (tem == 0), and
14301 it has the `cursor' property on one of its
14302 glyphs, record that glyph as a candidate for
14303 displaying the cursor. (As in the
14304 unidirectional version, we will display the
14305 cursor on the last candidate we find.) */
14306 if (tem == 0
14307 || tem == pt_old
14308 || (tem - pt_old > 0 && tem < pos_after))
14309 {
14310 /* The glyphs from this string could have
14311 been reordered. Find the one with the
14312 smallest string position. Or there could
14313 be a character in the string with the
14314 `cursor' property, which means display
14315 cursor on that character's glyph. */
14316 ptrdiff_t strpos = glyph->charpos;
14317
14318 if (tem)
14319 {
14320 cursor = glyph;
14321 string_from_text_prop = 1;
14322 }
14323 for ( ;
14324 (row->reversed_p ? glyph > stop : glyph < stop)
14325 && EQ (glyph->object, str);
14326 glyph += incr)
14327 {
14328 Lisp_Object cprop;
14329 ptrdiff_t gpos = glyph->charpos;
14330
14331 cprop = Fget_char_property (make_number (gpos),
14332 Qcursor,
14333 glyph->object);
14334 if (!NILP (cprop))
14335 {
14336 cursor = glyph;
14337 break;
14338 }
14339 if (tem && glyph->charpos < strpos)
14340 {
14341 strpos = glyph->charpos;
14342 cursor = glyph;
14343 }
14344 }
14345
14346 if (tem == pt_old
14347 || (tem - pt_old > 0 && tem < pos_after))
14348 goto compute_x;
14349 }
14350 if (tem)
14351 pos = tem + 1; /* don't find previous instances */
14352 }
14353 /* This string is not what we want; skip all of the
14354 glyphs that came from it. */
14355 while ((row->reversed_p ? glyph > stop : glyph < stop)
14356 && EQ (glyph->object, str))
14357 glyph += incr;
14358 }
14359 else
14360 glyph += incr;
14361 }
14362
14363 /* If we reached the end of the line, and END was from a string,
14364 the cursor is not on this line. */
14365 if (cursor == NULL
14366 && (row->reversed_p ? glyph <= end : glyph >= end)
14367 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14368 && STRINGP (end->object)
14369 && row->continued_p)
14370 return 0;
14371 }
14372 /* A truncated row may not include PT among its character positions.
14373 Setting the cursor inside the scroll margin will trigger
14374 recalculation of hscroll in hscroll_window_tree. But if a
14375 display string covers point, defer to the string-handling
14376 code below to figure this out. */
14377 else if (row->truncated_on_left_p && pt_old < bpos_min)
14378 {
14379 cursor = glyph_before;
14380 x = -1;
14381 }
14382 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14383 /* Zero-width characters produce no glyphs. */
14384 || (!empty_line_p
14385 && (row->reversed_p
14386 ? glyph_after > glyphs_end
14387 : glyph_after < glyphs_end)))
14388 {
14389 cursor = glyph_after;
14390 x = -1;
14391 }
14392 }
14393
14394 compute_x:
14395 if (cursor != NULL)
14396 glyph = cursor;
14397 else if (glyph == glyphs_end
14398 && pos_before == pos_after
14399 && STRINGP ((row->reversed_p
14400 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14401 : row->glyphs[TEXT_AREA])->object))
14402 {
14403 /* If all the glyphs of this row came from strings, put the
14404 cursor on the first glyph of the row. This avoids having the
14405 cursor outside of the text area in this very rare and hard
14406 use case. */
14407 glyph =
14408 row->reversed_p
14409 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14410 : row->glyphs[TEXT_AREA];
14411 }
14412 if (x < 0)
14413 {
14414 struct glyph *g;
14415
14416 /* Need to compute x that corresponds to GLYPH. */
14417 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14418 {
14419 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14420 emacs_abort ();
14421 x += g->pixel_width;
14422 }
14423 }
14424
14425 /* ROW could be part of a continued line, which, under bidi
14426 reordering, might have other rows whose start and end charpos
14427 occlude point. Only set w->cursor if we found a better
14428 approximation to the cursor position than we have from previously
14429 examined candidate rows belonging to the same continued line. */
14430 if (/* we already have a candidate row */
14431 w->cursor.vpos >= 0
14432 /* that candidate is not the row we are processing */
14433 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14434 /* Make sure cursor.vpos specifies a row whose start and end
14435 charpos occlude point, and it is valid candidate for being a
14436 cursor-row. This is because some callers of this function
14437 leave cursor.vpos at the row where the cursor was displayed
14438 during the last redisplay cycle. */
14439 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14440 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14441 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14442 {
14443 struct glyph *g1 =
14444 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14445
14446 /* Don't consider glyphs that are outside TEXT_AREA. */
14447 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14448 return 0;
14449 /* Keep the candidate whose buffer position is the closest to
14450 point or has the `cursor' property. */
14451 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14452 w->cursor.hpos >= 0
14453 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14454 && ((BUFFERP (g1->object)
14455 && (g1->charpos == pt_old /* an exact match always wins */
14456 || (BUFFERP (glyph->object)
14457 && eabs (g1->charpos - pt_old)
14458 < eabs (glyph->charpos - pt_old))))
14459 /* previous candidate is a glyph from a string that has
14460 a non-nil `cursor' property */
14461 || (STRINGP (g1->object)
14462 && (!NILP (Fget_char_property (make_number (g1->charpos),
14463 Qcursor, g1->object))
14464 /* previous candidate is from the same display
14465 string as this one, and the display string
14466 came from a text property */
14467 || (EQ (g1->object, glyph->object)
14468 && string_from_text_prop)
14469 /* this candidate is from newline and its
14470 position is not an exact match */
14471 || (INTEGERP (glyph->object)
14472 && glyph->charpos != pt_old)))))
14473 return 0;
14474 /* If this candidate gives an exact match, use that. */
14475 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14476 /* If this candidate is a glyph created for the
14477 terminating newline of a line, and point is on that
14478 newline, it wins because it's an exact match. */
14479 || (!row->continued_p
14480 && INTEGERP (glyph->object)
14481 && glyph->charpos == 0
14482 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14483 /* Otherwise, keep the candidate that comes from a row
14484 spanning less buffer positions. This may win when one or
14485 both candidate positions are on glyphs that came from
14486 display strings, for which we cannot compare buffer
14487 positions. */
14488 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14489 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14490 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14491 return 0;
14492 }
14493 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14494 w->cursor.x = x;
14495 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14496 w->cursor.y = row->y + dy;
14497
14498 if (w == XWINDOW (selected_window))
14499 {
14500 if (!row->continued_p
14501 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14502 && row->x == 0)
14503 {
14504 this_line_buffer = XBUFFER (w->buffer);
14505
14506 CHARPOS (this_line_start_pos)
14507 = MATRIX_ROW_START_CHARPOS (row) + delta;
14508 BYTEPOS (this_line_start_pos)
14509 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14510
14511 CHARPOS (this_line_end_pos)
14512 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14513 BYTEPOS (this_line_end_pos)
14514 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14515
14516 this_line_y = w->cursor.y;
14517 this_line_pixel_height = row->height;
14518 this_line_vpos = w->cursor.vpos;
14519 this_line_start_x = row->x;
14520 }
14521 else
14522 CHARPOS (this_line_start_pos) = 0;
14523 }
14524
14525 return 1;
14526 }
14527
14528
14529 /* Run window scroll functions, if any, for WINDOW with new window
14530 start STARTP. Sets the window start of WINDOW to that position.
14531
14532 We assume that the window's buffer is really current. */
14533
14534 static inline struct text_pos
14535 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14536 {
14537 struct window *w = XWINDOW (window);
14538 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14539
14540 if (current_buffer != XBUFFER (w->buffer))
14541 emacs_abort ();
14542
14543 if (!NILP (Vwindow_scroll_functions))
14544 {
14545 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14546 make_number (CHARPOS (startp)));
14547 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14548 /* In case the hook functions switch buffers. */
14549 set_buffer_internal (XBUFFER (w->buffer));
14550 }
14551
14552 return startp;
14553 }
14554
14555
14556 /* Make sure the line containing the cursor is fully visible.
14557 A value of 1 means there is nothing to be done.
14558 (Either the line is fully visible, or it cannot be made so,
14559 or we cannot tell.)
14560
14561 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14562 is higher than window.
14563
14564 A value of 0 means the caller should do scrolling
14565 as if point had gone off the screen. */
14566
14567 static int
14568 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14569 {
14570 struct glyph_matrix *matrix;
14571 struct glyph_row *row;
14572 int window_height;
14573
14574 if (!make_cursor_line_fully_visible_p)
14575 return 1;
14576
14577 /* It's not always possible to find the cursor, e.g, when a window
14578 is full of overlay strings. Don't do anything in that case. */
14579 if (w->cursor.vpos < 0)
14580 return 1;
14581
14582 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14583 row = MATRIX_ROW (matrix, w->cursor.vpos);
14584
14585 /* If the cursor row is not partially visible, there's nothing to do. */
14586 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14587 return 1;
14588
14589 /* If the row the cursor is in is taller than the window's height,
14590 it's not clear what to do, so do nothing. */
14591 window_height = window_box_height (w);
14592 if (row->height >= window_height)
14593 {
14594 if (!force_p || MINI_WINDOW_P (w)
14595 || w->vscroll || w->cursor.vpos == 0)
14596 return 1;
14597 }
14598 return 0;
14599 }
14600
14601
14602 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14603 non-zero means only WINDOW is redisplayed in redisplay_internal.
14604 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14605 in redisplay_window to bring a partially visible line into view in
14606 the case that only the cursor has moved.
14607
14608 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14609 last screen line's vertical height extends past the end of the screen.
14610
14611 Value is
14612
14613 1 if scrolling succeeded
14614
14615 0 if scrolling didn't find point.
14616
14617 -1 if new fonts have been loaded so that we must interrupt
14618 redisplay, adjust glyph matrices, and try again. */
14619
14620 enum
14621 {
14622 SCROLLING_SUCCESS,
14623 SCROLLING_FAILED,
14624 SCROLLING_NEED_LARGER_MATRICES
14625 };
14626
14627 /* If scroll-conservatively is more than this, never recenter.
14628
14629 If you change this, don't forget to update the doc string of
14630 `scroll-conservatively' and the Emacs manual. */
14631 #define SCROLL_LIMIT 100
14632
14633 static int
14634 try_scrolling (Lisp_Object window, int just_this_one_p,
14635 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14636 int temp_scroll_step, int last_line_misfit)
14637 {
14638 struct window *w = XWINDOW (window);
14639 struct frame *f = XFRAME (w->frame);
14640 struct text_pos pos, startp;
14641 struct it it;
14642 int this_scroll_margin, scroll_max, rc, height;
14643 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14644 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14645 Lisp_Object aggressive;
14646 /* We will never try scrolling more than this number of lines. */
14647 int scroll_limit = SCROLL_LIMIT;
14648
14649 #ifdef GLYPH_DEBUG
14650 debug_method_add (w, "try_scrolling");
14651 #endif
14652
14653 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14654
14655 /* Compute scroll margin height in pixels. We scroll when point is
14656 within this distance from the top or bottom of the window. */
14657 if (scroll_margin > 0)
14658 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14659 * FRAME_LINE_HEIGHT (f);
14660 else
14661 this_scroll_margin = 0;
14662
14663 /* Force arg_scroll_conservatively to have a reasonable value, to
14664 avoid scrolling too far away with slow move_it_* functions. Note
14665 that the user can supply scroll-conservatively equal to
14666 `most-positive-fixnum', which can be larger than INT_MAX. */
14667 if (arg_scroll_conservatively > scroll_limit)
14668 {
14669 arg_scroll_conservatively = scroll_limit + 1;
14670 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14671 }
14672 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14673 /* Compute how much we should try to scroll maximally to bring
14674 point into view. */
14675 scroll_max = (max (scroll_step,
14676 max (arg_scroll_conservatively, temp_scroll_step))
14677 * FRAME_LINE_HEIGHT (f));
14678 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14679 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14680 /* We're trying to scroll because of aggressive scrolling but no
14681 scroll_step is set. Choose an arbitrary one. */
14682 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14683 else
14684 scroll_max = 0;
14685
14686 too_near_end:
14687
14688 /* Decide whether to scroll down. */
14689 if (PT > CHARPOS (startp))
14690 {
14691 int scroll_margin_y;
14692
14693 /* Compute the pixel ypos of the scroll margin, then move IT to
14694 either that ypos or PT, whichever comes first. */
14695 start_display (&it, w, startp);
14696 scroll_margin_y = it.last_visible_y - this_scroll_margin
14697 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14698 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14699 (MOVE_TO_POS | MOVE_TO_Y));
14700
14701 if (PT > CHARPOS (it.current.pos))
14702 {
14703 int y0 = line_bottom_y (&it);
14704 /* Compute how many pixels below window bottom to stop searching
14705 for PT. This avoids costly search for PT that is far away if
14706 the user limited scrolling by a small number of lines, but
14707 always finds PT if scroll_conservatively is set to a large
14708 number, such as most-positive-fixnum. */
14709 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14710 int y_to_move = it.last_visible_y + slack;
14711
14712 /* Compute the distance from the scroll margin to PT or to
14713 the scroll limit, whichever comes first. This should
14714 include the height of the cursor line, to make that line
14715 fully visible. */
14716 move_it_to (&it, PT, -1, y_to_move,
14717 -1, MOVE_TO_POS | MOVE_TO_Y);
14718 dy = line_bottom_y (&it) - y0;
14719
14720 if (dy > scroll_max)
14721 return SCROLLING_FAILED;
14722
14723 if (dy > 0)
14724 scroll_down_p = 1;
14725 }
14726 }
14727
14728 if (scroll_down_p)
14729 {
14730 /* Point is in or below the bottom scroll margin, so move the
14731 window start down. If scrolling conservatively, move it just
14732 enough down to make point visible. If scroll_step is set,
14733 move it down by scroll_step. */
14734 if (arg_scroll_conservatively)
14735 amount_to_scroll
14736 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14737 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14738 else if (scroll_step || temp_scroll_step)
14739 amount_to_scroll = scroll_max;
14740 else
14741 {
14742 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14743 height = WINDOW_BOX_TEXT_HEIGHT (w);
14744 if (NUMBERP (aggressive))
14745 {
14746 double float_amount = XFLOATINT (aggressive) * height;
14747 amount_to_scroll = float_amount;
14748 if (amount_to_scroll == 0 && float_amount > 0)
14749 amount_to_scroll = 1;
14750 /* Don't let point enter the scroll margin near top of
14751 the window. */
14752 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14753 amount_to_scroll = height - 2*this_scroll_margin + dy;
14754 }
14755 }
14756
14757 if (amount_to_scroll <= 0)
14758 return SCROLLING_FAILED;
14759
14760 start_display (&it, w, startp);
14761 if (arg_scroll_conservatively <= scroll_limit)
14762 move_it_vertically (&it, amount_to_scroll);
14763 else
14764 {
14765 /* Extra precision for users who set scroll-conservatively
14766 to a large number: make sure the amount we scroll
14767 the window start is never less than amount_to_scroll,
14768 which was computed as distance from window bottom to
14769 point. This matters when lines at window top and lines
14770 below window bottom have different height. */
14771 struct it it1;
14772 void *it1data = NULL;
14773 /* We use a temporary it1 because line_bottom_y can modify
14774 its argument, if it moves one line down; see there. */
14775 int start_y;
14776
14777 SAVE_IT (it1, it, it1data);
14778 start_y = line_bottom_y (&it1);
14779 do {
14780 RESTORE_IT (&it, &it, it1data);
14781 move_it_by_lines (&it, 1);
14782 SAVE_IT (it1, it, it1data);
14783 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14784 }
14785
14786 /* If STARTP is unchanged, move it down another screen line. */
14787 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14788 move_it_by_lines (&it, 1);
14789 startp = it.current.pos;
14790 }
14791 else
14792 {
14793 struct text_pos scroll_margin_pos = startp;
14794
14795 /* See if point is inside the scroll margin at the top of the
14796 window. */
14797 if (this_scroll_margin)
14798 {
14799 start_display (&it, w, startp);
14800 move_it_vertically (&it, this_scroll_margin);
14801 scroll_margin_pos = it.current.pos;
14802 }
14803
14804 if (PT < CHARPOS (scroll_margin_pos))
14805 {
14806 /* Point is in the scroll margin at the top of the window or
14807 above what is displayed in the window. */
14808 int y0, y_to_move;
14809
14810 /* Compute the vertical distance from PT to the scroll
14811 margin position. Move as far as scroll_max allows, or
14812 one screenful, or 10 screen lines, whichever is largest.
14813 Give up if distance is greater than scroll_max. */
14814 SET_TEXT_POS (pos, PT, PT_BYTE);
14815 start_display (&it, w, pos);
14816 y0 = it.current_y;
14817 y_to_move = max (it.last_visible_y,
14818 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14819 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14820 y_to_move, -1,
14821 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14822 dy = it.current_y - y0;
14823 if (dy > scroll_max)
14824 return SCROLLING_FAILED;
14825
14826 /* Compute new window start. */
14827 start_display (&it, w, startp);
14828
14829 if (arg_scroll_conservatively)
14830 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14831 max (scroll_step, temp_scroll_step));
14832 else if (scroll_step || temp_scroll_step)
14833 amount_to_scroll = scroll_max;
14834 else
14835 {
14836 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14837 height = WINDOW_BOX_TEXT_HEIGHT (w);
14838 if (NUMBERP (aggressive))
14839 {
14840 double float_amount = XFLOATINT (aggressive) * height;
14841 amount_to_scroll = float_amount;
14842 if (amount_to_scroll == 0 && float_amount > 0)
14843 amount_to_scroll = 1;
14844 amount_to_scroll -=
14845 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14846 /* Don't let point enter the scroll margin near
14847 bottom of the window. */
14848 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14849 amount_to_scroll = height - 2*this_scroll_margin + dy;
14850 }
14851 }
14852
14853 if (amount_to_scroll <= 0)
14854 return SCROLLING_FAILED;
14855
14856 move_it_vertically_backward (&it, amount_to_scroll);
14857 startp = it.current.pos;
14858 }
14859 }
14860
14861 /* Run window scroll functions. */
14862 startp = run_window_scroll_functions (window, startp);
14863
14864 /* Display the window. Give up if new fonts are loaded, or if point
14865 doesn't appear. */
14866 if (!try_window (window, startp, 0))
14867 rc = SCROLLING_NEED_LARGER_MATRICES;
14868 else if (w->cursor.vpos < 0)
14869 {
14870 clear_glyph_matrix (w->desired_matrix);
14871 rc = SCROLLING_FAILED;
14872 }
14873 else
14874 {
14875 /* Maybe forget recorded base line for line number display. */
14876 if (!just_this_one_p
14877 || current_buffer->clip_changed
14878 || BEG_UNCHANGED < CHARPOS (startp))
14879 wset_base_line_number (w, Qnil);
14880
14881 /* If cursor ends up on a partially visible line,
14882 treat that as being off the bottom of the screen. */
14883 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14884 /* It's possible that the cursor is on the first line of the
14885 buffer, which is partially obscured due to a vscroll
14886 (Bug#7537). In that case, avoid looping forever . */
14887 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14888 {
14889 clear_glyph_matrix (w->desired_matrix);
14890 ++extra_scroll_margin_lines;
14891 goto too_near_end;
14892 }
14893 rc = SCROLLING_SUCCESS;
14894 }
14895
14896 return rc;
14897 }
14898
14899
14900 /* Compute a suitable window start for window W if display of W starts
14901 on a continuation line. Value is non-zero if a new window start
14902 was computed.
14903
14904 The new window start will be computed, based on W's width, starting
14905 from the start of the continued line. It is the start of the
14906 screen line with the minimum distance from the old start W->start. */
14907
14908 static int
14909 compute_window_start_on_continuation_line (struct window *w)
14910 {
14911 struct text_pos pos, start_pos;
14912 int window_start_changed_p = 0;
14913
14914 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14915
14916 /* If window start is on a continuation line... Window start may be
14917 < BEGV in case there's invisible text at the start of the
14918 buffer (M-x rmail, for example). */
14919 if (CHARPOS (start_pos) > BEGV
14920 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14921 {
14922 struct it it;
14923 struct glyph_row *row;
14924
14925 /* Handle the case that the window start is out of range. */
14926 if (CHARPOS (start_pos) < BEGV)
14927 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14928 else if (CHARPOS (start_pos) > ZV)
14929 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14930
14931 /* Find the start of the continued line. This should be fast
14932 because scan_buffer is fast (newline cache). */
14933 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14934 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14935 row, DEFAULT_FACE_ID);
14936 reseat_at_previous_visible_line_start (&it);
14937
14938 /* If the line start is "too far" away from the window start,
14939 say it takes too much time to compute a new window start. */
14940 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14941 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14942 {
14943 int min_distance, distance;
14944
14945 /* Move forward by display lines to find the new window
14946 start. If window width was enlarged, the new start can
14947 be expected to be > the old start. If window width was
14948 decreased, the new window start will be < the old start.
14949 So, we're looking for the display line start with the
14950 minimum distance from the old window start. */
14951 pos = it.current.pos;
14952 min_distance = INFINITY;
14953 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14954 distance < min_distance)
14955 {
14956 min_distance = distance;
14957 pos = it.current.pos;
14958 move_it_by_lines (&it, 1);
14959 }
14960
14961 /* Set the window start there. */
14962 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14963 window_start_changed_p = 1;
14964 }
14965 }
14966
14967 return window_start_changed_p;
14968 }
14969
14970
14971 /* Try cursor movement in case text has not changed in window WINDOW,
14972 with window start STARTP. Value is
14973
14974 CURSOR_MOVEMENT_SUCCESS if successful
14975
14976 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14977
14978 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14979 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14980 we want to scroll as if scroll-step were set to 1. See the code.
14981
14982 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14983 which case we have to abort this redisplay, and adjust matrices
14984 first. */
14985
14986 enum
14987 {
14988 CURSOR_MOVEMENT_SUCCESS,
14989 CURSOR_MOVEMENT_CANNOT_BE_USED,
14990 CURSOR_MOVEMENT_MUST_SCROLL,
14991 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14992 };
14993
14994 static int
14995 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14996 {
14997 struct window *w = XWINDOW (window);
14998 struct frame *f = XFRAME (w->frame);
14999 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15000
15001 #ifdef GLYPH_DEBUG
15002 if (inhibit_try_cursor_movement)
15003 return rc;
15004 #endif
15005
15006 /* Previously, there was a check for Lisp integer in the
15007 if-statement below. Now, this field is converted to
15008 ptrdiff_t, thus zero means invalid position in a buffer. */
15009 eassert (w->last_point > 0);
15010
15011 /* Handle case where text has not changed, only point, and it has
15012 not moved off the frame. */
15013 if (/* Point may be in this window. */
15014 PT >= CHARPOS (startp)
15015 /* Selective display hasn't changed. */
15016 && !current_buffer->clip_changed
15017 /* Function force-mode-line-update is used to force a thorough
15018 redisplay. It sets either windows_or_buffers_changed or
15019 update_mode_lines. So don't take a shortcut here for these
15020 cases. */
15021 && !update_mode_lines
15022 && !windows_or_buffers_changed
15023 && !cursor_type_changed
15024 /* Can't use this case if highlighting a region. When a
15025 region exists, cursor movement has to do more than just
15026 set the cursor. */
15027 && !(!NILP (Vtransient_mark_mode)
15028 && !NILP (BVAR (current_buffer, mark_active)))
15029 && NILP (w->region_showing)
15030 && NILP (Vshow_trailing_whitespace)
15031 /* This code is not used for mini-buffer for the sake of the case
15032 of redisplaying to replace an echo area message; since in
15033 that case the mini-buffer contents per se are usually
15034 unchanged. This code is of no real use in the mini-buffer
15035 since the handling of this_line_start_pos, etc., in redisplay
15036 handles the same cases. */
15037 && !EQ (window, minibuf_window)
15038 /* When splitting windows or for new windows, it happens that
15039 redisplay is called with a nil window_end_vpos or one being
15040 larger than the window. This should really be fixed in
15041 window.c. I don't have this on my list, now, so we do
15042 approximately the same as the old redisplay code. --gerd. */
15043 && INTEGERP (w->window_end_vpos)
15044 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15045 && (FRAME_WINDOW_P (f)
15046 || !overlay_arrow_in_current_buffer_p ()))
15047 {
15048 int this_scroll_margin, top_scroll_margin;
15049 struct glyph_row *row = NULL;
15050
15051 #ifdef GLYPH_DEBUG
15052 debug_method_add (w, "cursor movement");
15053 #endif
15054
15055 /* Scroll if point within this distance from the top or bottom
15056 of the window. This is a pixel value. */
15057 if (scroll_margin > 0)
15058 {
15059 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15060 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15061 }
15062 else
15063 this_scroll_margin = 0;
15064
15065 top_scroll_margin = this_scroll_margin;
15066 if (WINDOW_WANTS_HEADER_LINE_P (w))
15067 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15068
15069 /* Start with the row the cursor was displayed during the last
15070 not paused redisplay. Give up if that row is not valid. */
15071 if (w->last_cursor.vpos < 0
15072 || w->last_cursor.vpos >= w->current_matrix->nrows)
15073 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15074 else
15075 {
15076 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15077 if (row->mode_line_p)
15078 ++row;
15079 if (!row->enabled_p)
15080 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15081 }
15082
15083 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15084 {
15085 int scroll_p = 0, must_scroll = 0;
15086 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15087
15088 if (PT > w->last_point)
15089 {
15090 /* Point has moved forward. */
15091 while (MATRIX_ROW_END_CHARPOS (row) < PT
15092 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15093 {
15094 eassert (row->enabled_p);
15095 ++row;
15096 }
15097
15098 /* If the end position of a row equals the start
15099 position of the next row, and PT is at that position,
15100 we would rather display cursor in the next line. */
15101 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15102 && MATRIX_ROW_END_CHARPOS (row) == PT
15103 && row < w->current_matrix->rows
15104 + w->current_matrix->nrows - 1
15105 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15106 && !cursor_row_p (row))
15107 ++row;
15108
15109 /* If within the scroll margin, scroll. Note that
15110 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15111 the next line would be drawn, and that
15112 this_scroll_margin can be zero. */
15113 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15114 || PT > MATRIX_ROW_END_CHARPOS (row)
15115 /* Line is completely visible last line in window
15116 and PT is to be set in the next line. */
15117 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15118 && PT == MATRIX_ROW_END_CHARPOS (row)
15119 && !row->ends_at_zv_p
15120 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15121 scroll_p = 1;
15122 }
15123 else if (PT < w->last_point)
15124 {
15125 /* Cursor has to be moved backward. Note that PT >=
15126 CHARPOS (startp) because of the outer if-statement. */
15127 while (!row->mode_line_p
15128 && (MATRIX_ROW_START_CHARPOS (row) > PT
15129 || (MATRIX_ROW_START_CHARPOS (row) == PT
15130 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15131 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15132 row > w->current_matrix->rows
15133 && (row-1)->ends_in_newline_from_string_p))))
15134 && (row->y > top_scroll_margin
15135 || CHARPOS (startp) == BEGV))
15136 {
15137 eassert (row->enabled_p);
15138 --row;
15139 }
15140
15141 /* Consider the following case: Window starts at BEGV,
15142 there is invisible, intangible text at BEGV, so that
15143 display starts at some point START > BEGV. It can
15144 happen that we are called with PT somewhere between
15145 BEGV and START. Try to handle that case. */
15146 if (row < w->current_matrix->rows
15147 || row->mode_line_p)
15148 {
15149 row = w->current_matrix->rows;
15150 if (row->mode_line_p)
15151 ++row;
15152 }
15153
15154 /* Due to newlines in overlay strings, we may have to
15155 skip forward over overlay strings. */
15156 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15157 && MATRIX_ROW_END_CHARPOS (row) == PT
15158 && !cursor_row_p (row))
15159 ++row;
15160
15161 /* If within the scroll margin, scroll. */
15162 if (row->y < top_scroll_margin
15163 && CHARPOS (startp) != BEGV)
15164 scroll_p = 1;
15165 }
15166 else
15167 {
15168 /* Cursor did not move. So don't scroll even if cursor line
15169 is partially visible, as it was so before. */
15170 rc = CURSOR_MOVEMENT_SUCCESS;
15171 }
15172
15173 if (PT < MATRIX_ROW_START_CHARPOS (row)
15174 || PT > MATRIX_ROW_END_CHARPOS (row))
15175 {
15176 /* if PT is not in the glyph row, give up. */
15177 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15178 must_scroll = 1;
15179 }
15180 else if (rc != CURSOR_MOVEMENT_SUCCESS
15181 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15182 {
15183 struct glyph_row *row1;
15184
15185 /* If rows are bidi-reordered and point moved, back up
15186 until we find a row that does not belong to a
15187 continuation line. This is because we must consider
15188 all rows of a continued line as candidates for the
15189 new cursor positioning, since row start and end
15190 positions change non-linearly with vertical position
15191 in such rows. */
15192 /* FIXME: Revisit this when glyph ``spilling'' in
15193 continuation lines' rows is implemented for
15194 bidi-reordered rows. */
15195 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15196 MATRIX_ROW_CONTINUATION_LINE_P (row);
15197 --row)
15198 {
15199 /* If we hit the beginning of the displayed portion
15200 without finding the first row of a continued
15201 line, give up. */
15202 if (row <= row1)
15203 {
15204 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15205 break;
15206 }
15207 eassert (row->enabled_p);
15208 }
15209 }
15210 if (must_scroll)
15211 ;
15212 else if (rc != CURSOR_MOVEMENT_SUCCESS
15213 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15214 /* Make sure this isn't a header line by any chance, since
15215 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15216 && !row->mode_line_p
15217 && make_cursor_line_fully_visible_p)
15218 {
15219 if (PT == MATRIX_ROW_END_CHARPOS (row)
15220 && !row->ends_at_zv_p
15221 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15222 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15223 else if (row->height > window_box_height (w))
15224 {
15225 /* If we end up in a partially visible line, let's
15226 make it fully visible, except when it's taller
15227 than the window, in which case we can't do much
15228 about it. */
15229 *scroll_step = 1;
15230 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15231 }
15232 else
15233 {
15234 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15235 if (!cursor_row_fully_visible_p (w, 0, 1))
15236 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15237 else
15238 rc = CURSOR_MOVEMENT_SUCCESS;
15239 }
15240 }
15241 else if (scroll_p)
15242 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15243 else if (rc != CURSOR_MOVEMENT_SUCCESS
15244 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15245 {
15246 /* With bidi-reordered rows, there could be more than
15247 one candidate row whose start and end positions
15248 occlude point. We need to let set_cursor_from_row
15249 find the best candidate. */
15250 /* FIXME: Revisit this when glyph ``spilling'' in
15251 continuation lines' rows is implemented for
15252 bidi-reordered rows. */
15253 int rv = 0;
15254
15255 do
15256 {
15257 int at_zv_p = 0, exact_match_p = 0;
15258
15259 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15260 && PT <= MATRIX_ROW_END_CHARPOS (row)
15261 && cursor_row_p (row))
15262 rv |= set_cursor_from_row (w, row, w->current_matrix,
15263 0, 0, 0, 0);
15264 /* As soon as we've found the exact match for point,
15265 or the first suitable row whose ends_at_zv_p flag
15266 is set, we are done. */
15267 at_zv_p =
15268 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15269 if (rv && !at_zv_p
15270 && w->cursor.hpos >= 0
15271 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15272 w->cursor.vpos))
15273 {
15274 struct glyph_row *candidate =
15275 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15276 struct glyph *g =
15277 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15278 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15279
15280 exact_match_p =
15281 (BUFFERP (g->object) && g->charpos == PT)
15282 || (INTEGERP (g->object)
15283 && (g->charpos == PT
15284 || (g->charpos == 0 && endpos - 1 == PT)));
15285 }
15286 if (rv && (at_zv_p || exact_match_p))
15287 {
15288 rc = CURSOR_MOVEMENT_SUCCESS;
15289 break;
15290 }
15291 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15292 break;
15293 ++row;
15294 }
15295 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15296 || row->continued_p)
15297 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15298 || (MATRIX_ROW_START_CHARPOS (row) == PT
15299 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15300 /* If we didn't find any candidate rows, or exited the
15301 loop before all the candidates were examined, signal
15302 to the caller that this method failed. */
15303 if (rc != CURSOR_MOVEMENT_SUCCESS
15304 && !(rv
15305 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15306 && !row->continued_p))
15307 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15308 else if (rv)
15309 rc = CURSOR_MOVEMENT_SUCCESS;
15310 }
15311 else
15312 {
15313 do
15314 {
15315 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15316 {
15317 rc = CURSOR_MOVEMENT_SUCCESS;
15318 break;
15319 }
15320 ++row;
15321 }
15322 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15323 && MATRIX_ROW_START_CHARPOS (row) == PT
15324 && cursor_row_p (row));
15325 }
15326 }
15327 }
15328
15329 return rc;
15330 }
15331
15332 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15333 static
15334 #endif
15335 void
15336 set_vertical_scroll_bar (struct window *w)
15337 {
15338 ptrdiff_t start, end, whole;
15339
15340 /* Calculate the start and end positions for the current window.
15341 At some point, it would be nice to choose between scrollbars
15342 which reflect the whole buffer size, with special markers
15343 indicating narrowing, and scrollbars which reflect only the
15344 visible region.
15345
15346 Note that mini-buffers sometimes aren't displaying any text. */
15347 if (!MINI_WINDOW_P (w)
15348 || (w == XWINDOW (minibuf_window)
15349 && NILP (echo_area_buffer[0])))
15350 {
15351 struct buffer *buf = XBUFFER (w->buffer);
15352 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15353 start = marker_position (w->start) - BUF_BEGV (buf);
15354 /* I don't think this is guaranteed to be right. For the
15355 moment, we'll pretend it is. */
15356 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15357
15358 if (end < start)
15359 end = start;
15360 if (whole < (end - start))
15361 whole = end - start;
15362 }
15363 else
15364 start = end = whole = 0;
15365
15366 /* Indicate what this scroll bar ought to be displaying now. */
15367 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15368 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15369 (w, end - start, whole, start);
15370 }
15371
15372
15373 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15374 selected_window is redisplayed.
15375
15376 We can return without actually redisplaying the window if
15377 fonts_changed_p. In that case, redisplay_internal will
15378 retry. */
15379
15380 static void
15381 redisplay_window (Lisp_Object window, int just_this_one_p)
15382 {
15383 struct window *w = XWINDOW (window);
15384 struct frame *f = XFRAME (w->frame);
15385 struct buffer *buffer = XBUFFER (w->buffer);
15386 struct buffer *old = current_buffer;
15387 struct text_pos lpoint, opoint, startp;
15388 int update_mode_line;
15389 int tem;
15390 struct it it;
15391 /* Record it now because it's overwritten. */
15392 int current_matrix_up_to_date_p = 0;
15393 int used_current_matrix_p = 0;
15394 /* This is less strict than current_matrix_up_to_date_p.
15395 It indicates that the buffer contents and narrowing are unchanged. */
15396 int buffer_unchanged_p = 0;
15397 int temp_scroll_step = 0;
15398 ptrdiff_t count = SPECPDL_INDEX ();
15399 int rc;
15400 int centering_position = -1;
15401 int last_line_misfit = 0;
15402 ptrdiff_t beg_unchanged, end_unchanged;
15403
15404 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15405 opoint = lpoint;
15406
15407 /* W must be a leaf window here. */
15408 eassert (!NILP (w->buffer));
15409 #ifdef GLYPH_DEBUG
15410 *w->desired_matrix->method = 0;
15411 #endif
15412
15413 restart:
15414 reconsider_clip_changes (w, buffer);
15415
15416 /* Has the mode line to be updated? */
15417 update_mode_line = (w->update_mode_line
15418 || update_mode_lines
15419 || buffer->clip_changed
15420 || buffer->prevent_redisplay_optimizations_p);
15421
15422 if (MINI_WINDOW_P (w))
15423 {
15424 if (w == XWINDOW (echo_area_window)
15425 && !NILP (echo_area_buffer[0]))
15426 {
15427 if (update_mode_line)
15428 /* We may have to update a tty frame's menu bar or a
15429 tool-bar. Example `M-x C-h C-h C-g'. */
15430 goto finish_menu_bars;
15431 else
15432 /* We've already displayed the echo area glyphs in this window. */
15433 goto finish_scroll_bars;
15434 }
15435 else if ((w != XWINDOW (minibuf_window)
15436 || minibuf_level == 0)
15437 /* When buffer is nonempty, redisplay window normally. */
15438 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15439 /* Quail displays non-mini buffers in minibuffer window.
15440 In that case, redisplay the window normally. */
15441 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15442 {
15443 /* W is a mini-buffer window, but it's not active, so clear
15444 it. */
15445 int yb = window_text_bottom_y (w);
15446 struct glyph_row *row;
15447 int y;
15448
15449 for (y = 0, row = w->desired_matrix->rows;
15450 y < yb;
15451 y += row->height, ++row)
15452 blank_row (w, row, y);
15453 goto finish_scroll_bars;
15454 }
15455
15456 clear_glyph_matrix (w->desired_matrix);
15457 }
15458
15459 /* Otherwise set up data on this window; select its buffer and point
15460 value. */
15461 /* Really select the buffer, for the sake of buffer-local
15462 variables. */
15463 set_buffer_internal_1 (XBUFFER (w->buffer));
15464
15465 current_matrix_up_to_date_p
15466 = (!NILP (w->window_end_valid)
15467 && !current_buffer->clip_changed
15468 && !current_buffer->prevent_redisplay_optimizations_p
15469 && w->last_modified >= MODIFF
15470 && w->last_overlay_modified >= OVERLAY_MODIFF);
15471
15472 /* Run the window-bottom-change-functions
15473 if it is possible that the text on the screen has changed
15474 (either due to modification of the text, or any other reason). */
15475 if (!current_matrix_up_to_date_p
15476 && !NILP (Vwindow_text_change_functions))
15477 {
15478 safe_run_hooks (Qwindow_text_change_functions);
15479 goto restart;
15480 }
15481
15482 beg_unchanged = BEG_UNCHANGED;
15483 end_unchanged = END_UNCHANGED;
15484
15485 SET_TEXT_POS (opoint, PT, PT_BYTE);
15486
15487 specbind (Qinhibit_point_motion_hooks, Qt);
15488
15489 buffer_unchanged_p
15490 = (!NILP (w->window_end_valid)
15491 && !current_buffer->clip_changed
15492 && w->last_modified >= MODIFF
15493 && w->last_overlay_modified >= OVERLAY_MODIFF);
15494
15495 /* When windows_or_buffers_changed is non-zero, we can't rely on
15496 the window end being valid, so set it to nil there. */
15497 if (windows_or_buffers_changed)
15498 {
15499 /* If window starts on a continuation line, maybe adjust the
15500 window start in case the window's width changed. */
15501 if (XMARKER (w->start)->buffer == current_buffer)
15502 compute_window_start_on_continuation_line (w);
15503
15504 wset_window_end_valid (w, Qnil);
15505 }
15506
15507 /* Some sanity checks. */
15508 CHECK_WINDOW_END (w);
15509 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15510 emacs_abort ();
15511 if (BYTEPOS (opoint) < CHARPOS (opoint))
15512 emacs_abort ();
15513
15514 /* If %c is in mode line, update it if needed. */
15515 if (!NILP (w->column_number_displayed)
15516 /* This alternative quickly identifies a common case
15517 where no change is needed. */
15518 && !(PT == w->last_point
15519 && w->last_modified >= MODIFF
15520 && w->last_overlay_modified >= OVERLAY_MODIFF)
15521 && (XFASTINT (w->column_number_displayed) != current_column ()))
15522 update_mode_line = 1;
15523
15524 /* Count number of windows showing the selected buffer. An indirect
15525 buffer counts as its base buffer. */
15526 if (!just_this_one_p)
15527 {
15528 struct buffer *current_base, *window_base;
15529 current_base = current_buffer;
15530 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15531 if (current_base->base_buffer)
15532 current_base = current_base->base_buffer;
15533 if (window_base->base_buffer)
15534 window_base = window_base->base_buffer;
15535 if (current_base == window_base)
15536 buffer_shared++;
15537 }
15538
15539 /* Point refers normally to the selected window. For any other
15540 window, set up appropriate value. */
15541 if (!EQ (window, selected_window))
15542 {
15543 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15544 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15545 if (new_pt < BEGV)
15546 {
15547 new_pt = BEGV;
15548 new_pt_byte = BEGV_BYTE;
15549 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15550 }
15551 else if (new_pt > (ZV - 1))
15552 {
15553 new_pt = ZV;
15554 new_pt_byte = ZV_BYTE;
15555 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15556 }
15557
15558 /* We don't use SET_PT so that the point-motion hooks don't run. */
15559 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15560 }
15561
15562 /* If any of the character widths specified in the display table
15563 have changed, invalidate the width run cache. It's true that
15564 this may be a bit late to catch such changes, but the rest of
15565 redisplay goes (non-fatally) haywire when the display table is
15566 changed, so why should we worry about doing any better? */
15567 if (current_buffer->width_run_cache)
15568 {
15569 struct Lisp_Char_Table *disptab = buffer_display_table ();
15570
15571 if (! disptab_matches_widthtab
15572 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15573 {
15574 invalidate_region_cache (current_buffer,
15575 current_buffer->width_run_cache,
15576 BEG, Z);
15577 recompute_width_table (current_buffer, disptab);
15578 }
15579 }
15580
15581 /* If window-start is screwed up, choose a new one. */
15582 if (XMARKER (w->start)->buffer != current_buffer)
15583 goto recenter;
15584
15585 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15586
15587 /* If someone specified a new starting point but did not insist,
15588 check whether it can be used. */
15589 if (w->optional_new_start
15590 && CHARPOS (startp) >= BEGV
15591 && CHARPOS (startp) <= ZV)
15592 {
15593 w->optional_new_start = 0;
15594 start_display (&it, w, startp);
15595 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15596 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15597 if (IT_CHARPOS (it) == PT)
15598 w->force_start = 1;
15599 /* IT may overshoot PT if text at PT is invisible. */
15600 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15601 w->force_start = 1;
15602 }
15603
15604 force_start:
15605
15606 /* Handle case where place to start displaying has been specified,
15607 unless the specified location is outside the accessible range. */
15608 if (w->force_start || w->frozen_window_start_p)
15609 {
15610 /* We set this later on if we have to adjust point. */
15611 int new_vpos = -1;
15612
15613 w->force_start = 0;
15614 w->vscroll = 0;
15615 wset_window_end_valid (w, Qnil);
15616
15617 /* Forget any recorded base line for line number display. */
15618 if (!buffer_unchanged_p)
15619 wset_base_line_number (w, Qnil);
15620
15621 /* Redisplay the mode line. Select the buffer properly for that.
15622 Also, run the hook window-scroll-functions
15623 because we have scrolled. */
15624 /* Note, we do this after clearing force_start because
15625 if there's an error, it is better to forget about force_start
15626 than to get into an infinite loop calling the hook functions
15627 and having them get more errors. */
15628 if (!update_mode_line
15629 || ! NILP (Vwindow_scroll_functions))
15630 {
15631 update_mode_line = 1;
15632 w->update_mode_line = 1;
15633 startp = run_window_scroll_functions (window, startp);
15634 }
15635
15636 w->last_modified = 0;
15637 w->last_overlay_modified = 0;
15638 if (CHARPOS (startp) < BEGV)
15639 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15640 else if (CHARPOS (startp) > ZV)
15641 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15642
15643 /* Redisplay, then check if cursor has been set during the
15644 redisplay. Give up if new fonts were loaded. */
15645 /* We used to issue a CHECK_MARGINS argument to try_window here,
15646 but this causes scrolling to fail when point begins inside
15647 the scroll margin (bug#148) -- cyd */
15648 if (!try_window (window, startp, 0))
15649 {
15650 w->force_start = 1;
15651 clear_glyph_matrix (w->desired_matrix);
15652 goto need_larger_matrices;
15653 }
15654
15655 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15656 {
15657 /* If point does not appear, try to move point so it does
15658 appear. The desired matrix has been built above, so we
15659 can use it here. */
15660 new_vpos = window_box_height (w) / 2;
15661 }
15662
15663 if (!cursor_row_fully_visible_p (w, 0, 0))
15664 {
15665 /* Point does appear, but on a line partly visible at end of window.
15666 Move it back to a fully-visible line. */
15667 new_vpos = window_box_height (w);
15668 }
15669
15670 /* If we need to move point for either of the above reasons,
15671 now actually do it. */
15672 if (new_vpos >= 0)
15673 {
15674 struct glyph_row *row;
15675
15676 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15677 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15678 ++row;
15679
15680 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15681 MATRIX_ROW_START_BYTEPOS (row));
15682
15683 if (w != XWINDOW (selected_window))
15684 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15685 else if (current_buffer == old)
15686 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15687
15688 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15689
15690 /* If we are highlighting the region, then we just changed
15691 the region, so redisplay to show it. */
15692 if (!NILP (Vtransient_mark_mode)
15693 && !NILP (BVAR (current_buffer, mark_active)))
15694 {
15695 clear_glyph_matrix (w->desired_matrix);
15696 if (!try_window (window, startp, 0))
15697 goto need_larger_matrices;
15698 }
15699 }
15700
15701 #ifdef GLYPH_DEBUG
15702 debug_method_add (w, "forced window start");
15703 #endif
15704 goto done;
15705 }
15706
15707 /* Handle case where text has not changed, only point, and it has
15708 not moved off the frame, and we are not retrying after hscroll.
15709 (current_matrix_up_to_date_p is nonzero when retrying.) */
15710 if (current_matrix_up_to_date_p
15711 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15712 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15713 {
15714 switch (rc)
15715 {
15716 case CURSOR_MOVEMENT_SUCCESS:
15717 used_current_matrix_p = 1;
15718 goto done;
15719
15720 case CURSOR_MOVEMENT_MUST_SCROLL:
15721 goto try_to_scroll;
15722
15723 default:
15724 emacs_abort ();
15725 }
15726 }
15727 /* If current starting point was originally the beginning of a line
15728 but no longer is, find a new starting point. */
15729 else if (w->start_at_line_beg
15730 && !(CHARPOS (startp) <= BEGV
15731 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15732 {
15733 #ifdef GLYPH_DEBUG
15734 debug_method_add (w, "recenter 1");
15735 #endif
15736 goto recenter;
15737 }
15738
15739 /* Try scrolling with try_window_id. Value is > 0 if update has
15740 been done, it is -1 if we know that the same window start will
15741 not work. It is 0 if unsuccessful for some other reason. */
15742 else if ((tem = try_window_id (w)) != 0)
15743 {
15744 #ifdef GLYPH_DEBUG
15745 debug_method_add (w, "try_window_id %d", tem);
15746 #endif
15747
15748 if (fonts_changed_p)
15749 goto need_larger_matrices;
15750 if (tem > 0)
15751 goto done;
15752
15753 /* Otherwise try_window_id has returned -1 which means that we
15754 don't want the alternative below this comment to execute. */
15755 }
15756 else if (CHARPOS (startp) >= BEGV
15757 && CHARPOS (startp) <= ZV
15758 && PT >= CHARPOS (startp)
15759 && (CHARPOS (startp) < ZV
15760 /* Avoid starting at end of buffer. */
15761 || CHARPOS (startp) == BEGV
15762 || (w->last_modified >= MODIFF
15763 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15764 {
15765 int d1, d2, d3, d4, d5, d6;
15766
15767 /* If first window line is a continuation line, and window start
15768 is inside the modified region, but the first change is before
15769 current window start, we must select a new window start.
15770
15771 However, if this is the result of a down-mouse event (e.g. by
15772 extending the mouse-drag-overlay), we don't want to select a
15773 new window start, since that would change the position under
15774 the mouse, resulting in an unwanted mouse-movement rather
15775 than a simple mouse-click. */
15776 if (!w->start_at_line_beg
15777 && NILP (do_mouse_tracking)
15778 && CHARPOS (startp) > BEGV
15779 && CHARPOS (startp) > BEG + beg_unchanged
15780 && CHARPOS (startp) <= Z - end_unchanged
15781 /* Even if w->start_at_line_beg is nil, a new window may
15782 start at a line_beg, since that's how set_buffer_window
15783 sets it. So, we need to check the return value of
15784 compute_window_start_on_continuation_line. (See also
15785 bug#197). */
15786 && XMARKER (w->start)->buffer == current_buffer
15787 && compute_window_start_on_continuation_line (w)
15788 /* It doesn't make sense to force the window start like we
15789 do at label force_start if it is already known that point
15790 will not be visible in the resulting window, because
15791 doing so will move point from its correct position
15792 instead of scrolling the window to bring point into view.
15793 See bug#9324. */
15794 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15795 {
15796 w->force_start = 1;
15797 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15798 goto force_start;
15799 }
15800
15801 #ifdef GLYPH_DEBUG
15802 debug_method_add (w, "same window start");
15803 #endif
15804
15805 /* Try to redisplay starting at same place as before.
15806 If point has not moved off frame, accept the results. */
15807 if (!current_matrix_up_to_date_p
15808 /* Don't use try_window_reusing_current_matrix in this case
15809 because a window scroll function can have changed the
15810 buffer. */
15811 || !NILP (Vwindow_scroll_functions)
15812 || MINI_WINDOW_P (w)
15813 || !(used_current_matrix_p
15814 = try_window_reusing_current_matrix (w)))
15815 {
15816 IF_DEBUG (debug_method_add (w, "1"));
15817 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15818 /* -1 means we need to scroll.
15819 0 means we need new matrices, but fonts_changed_p
15820 is set in that case, so we will detect it below. */
15821 goto try_to_scroll;
15822 }
15823
15824 if (fonts_changed_p)
15825 goto need_larger_matrices;
15826
15827 if (w->cursor.vpos >= 0)
15828 {
15829 if (!just_this_one_p
15830 || current_buffer->clip_changed
15831 || BEG_UNCHANGED < CHARPOS (startp))
15832 /* Forget any recorded base line for line number display. */
15833 wset_base_line_number (w, Qnil);
15834
15835 if (!cursor_row_fully_visible_p (w, 1, 0))
15836 {
15837 clear_glyph_matrix (w->desired_matrix);
15838 last_line_misfit = 1;
15839 }
15840 /* Drop through and scroll. */
15841 else
15842 goto done;
15843 }
15844 else
15845 clear_glyph_matrix (w->desired_matrix);
15846 }
15847
15848 try_to_scroll:
15849
15850 w->last_modified = 0;
15851 w->last_overlay_modified = 0;
15852
15853 /* Redisplay the mode line. Select the buffer properly for that. */
15854 if (!update_mode_line)
15855 {
15856 update_mode_line = 1;
15857 w->update_mode_line = 1;
15858 }
15859
15860 /* Try to scroll by specified few lines. */
15861 if ((scroll_conservatively
15862 || emacs_scroll_step
15863 || temp_scroll_step
15864 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15865 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15866 && CHARPOS (startp) >= BEGV
15867 && CHARPOS (startp) <= ZV)
15868 {
15869 /* The function returns -1 if new fonts were loaded, 1 if
15870 successful, 0 if not successful. */
15871 int ss = try_scrolling (window, just_this_one_p,
15872 scroll_conservatively,
15873 emacs_scroll_step,
15874 temp_scroll_step, last_line_misfit);
15875 switch (ss)
15876 {
15877 case SCROLLING_SUCCESS:
15878 goto done;
15879
15880 case SCROLLING_NEED_LARGER_MATRICES:
15881 goto need_larger_matrices;
15882
15883 case SCROLLING_FAILED:
15884 break;
15885
15886 default:
15887 emacs_abort ();
15888 }
15889 }
15890
15891 /* Finally, just choose a place to start which positions point
15892 according to user preferences. */
15893
15894 recenter:
15895
15896 #ifdef GLYPH_DEBUG
15897 debug_method_add (w, "recenter");
15898 #endif
15899
15900 /* w->vscroll = 0; */
15901
15902 /* Forget any previously recorded base line for line number display. */
15903 if (!buffer_unchanged_p)
15904 wset_base_line_number (w, Qnil);
15905
15906 /* Determine the window start relative to point. */
15907 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15908 it.current_y = it.last_visible_y;
15909 if (centering_position < 0)
15910 {
15911 int margin =
15912 scroll_margin > 0
15913 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15914 : 0;
15915 ptrdiff_t margin_pos = CHARPOS (startp);
15916 Lisp_Object aggressive;
15917 int scrolling_up;
15918
15919 /* If there is a scroll margin at the top of the window, find
15920 its character position. */
15921 if (margin
15922 /* Cannot call start_display if startp is not in the
15923 accessible region of the buffer. This can happen when we
15924 have just switched to a different buffer and/or changed
15925 its restriction. In that case, startp is initialized to
15926 the character position 1 (BEGV) because we did not yet
15927 have chance to display the buffer even once. */
15928 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15929 {
15930 struct it it1;
15931 void *it1data = NULL;
15932
15933 SAVE_IT (it1, it, it1data);
15934 start_display (&it1, w, startp);
15935 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15936 margin_pos = IT_CHARPOS (it1);
15937 RESTORE_IT (&it, &it, it1data);
15938 }
15939 scrolling_up = PT > margin_pos;
15940 aggressive =
15941 scrolling_up
15942 ? BVAR (current_buffer, scroll_up_aggressively)
15943 : BVAR (current_buffer, scroll_down_aggressively);
15944
15945 if (!MINI_WINDOW_P (w)
15946 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15947 {
15948 int pt_offset = 0;
15949
15950 /* Setting scroll-conservatively overrides
15951 scroll-*-aggressively. */
15952 if (!scroll_conservatively && NUMBERP (aggressive))
15953 {
15954 double float_amount = XFLOATINT (aggressive);
15955
15956 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15957 if (pt_offset == 0 && float_amount > 0)
15958 pt_offset = 1;
15959 if (pt_offset && margin > 0)
15960 margin -= 1;
15961 }
15962 /* Compute how much to move the window start backward from
15963 point so that point will be displayed where the user
15964 wants it. */
15965 if (scrolling_up)
15966 {
15967 centering_position = it.last_visible_y;
15968 if (pt_offset)
15969 centering_position -= pt_offset;
15970 centering_position -=
15971 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15972 + WINDOW_HEADER_LINE_HEIGHT (w);
15973 /* Don't let point enter the scroll margin near top of
15974 the window. */
15975 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15976 centering_position = margin * FRAME_LINE_HEIGHT (f);
15977 }
15978 else
15979 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15980 }
15981 else
15982 /* Set the window start half the height of the window backward
15983 from point. */
15984 centering_position = window_box_height (w) / 2;
15985 }
15986 move_it_vertically_backward (&it, centering_position);
15987
15988 eassert (IT_CHARPOS (it) >= BEGV);
15989
15990 /* The function move_it_vertically_backward may move over more
15991 than the specified y-distance. If it->w is small, e.g. a
15992 mini-buffer window, we may end up in front of the window's
15993 display area. Start displaying at the start of the line
15994 containing PT in this case. */
15995 if (it.current_y <= 0)
15996 {
15997 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15998 move_it_vertically_backward (&it, 0);
15999 it.current_y = 0;
16000 }
16001
16002 it.current_x = it.hpos = 0;
16003
16004 /* Set the window start position here explicitly, to avoid an
16005 infinite loop in case the functions in window-scroll-functions
16006 get errors. */
16007 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16008
16009 /* Run scroll hooks. */
16010 startp = run_window_scroll_functions (window, it.current.pos);
16011
16012 /* Redisplay the window. */
16013 if (!current_matrix_up_to_date_p
16014 || windows_or_buffers_changed
16015 || cursor_type_changed
16016 /* Don't use try_window_reusing_current_matrix in this case
16017 because it can have changed the buffer. */
16018 || !NILP (Vwindow_scroll_functions)
16019 || !just_this_one_p
16020 || MINI_WINDOW_P (w)
16021 || !(used_current_matrix_p
16022 = try_window_reusing_current_matrix (w)))
16023 try_window (window, startp, 0);
16024
16025 /* If new fonts have been loaded (due to fontsets), give up. We
16026 have to start a new redisplay since we need to re-adjust glyph
16027 matrices. */
16028 if (fonts_changed_p)
16029 goto need_larger_matrices;
16030
16031 /* If cursor did not appear assume that the middle of the window is
16032 in the first line of the window. Do it again with the next line.
16033 (Imagine a window of height 100, displaying two lines of height
16034 60. Moving back 50 from it->last_visible_y will end in the first
16035 line.) */
16036 if (w->cursor.vpos < 0)
16037 {
16038 if (!NILP (w->window_end_valid)
16039 && PT >= Z - XFASTINT (w->window_end_pos))
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 if (PT < IT_CHARPOS (it))
16046 {
16047 clear_glyph_matrix (w->desired_matrix);
16048 move_it_by_lines (&it, -1);
16049 try_window (window, it.current.pos, 0);
16050 }
16051 else
16052 {
16053 /* Not much we can do about it. */
16054 }
16055 }
16056
16057 /* Consider the following case: Window starts at BEGV, there is
16058 invisible, intangible text at BEGV, so that display starts at
16059 some point START > BEGV. It can happen that we are called with
16060 PT somewhere between BEGV and START. Try to handle that case. */
16061 if (w->cursor.vpos < 0)
16062 {
16063 struct glyph_row *row = w->current_matrix->rows;
16064 if (row->mode_line_p)
16065 ++row;
16066 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16067 }
16068
16069 if (!cursor_row_fully_visible_p (w, 0, 0))
16070 {
16071 /* If vscroll is enabled, disable it and try again. */
16072 if (w->vscroll)
16073 {
16074 w->vscroll = 0;
16075 clear_glyph_matrix (w->desired_matrix);
16076 goto recenter;
16077 }
16078
16079 /* Users who set scroll-conservatively to a large number want
16080 point just above/below the scroll margin. If we ended up
16081 with point's row partially visible, move the window start to
16082 make that row fully visible and out of the margin. */
16083 if (scroll_conservatively > SCROLL_LIMIT)
16084 {
16085 int margin =
16086 scroll_margin > 0
16087 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16088 : 0;
16089 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16090
16091 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16092 clear_glyph_matrix (w->desired_matrix);
16093 if (1 == try_window (window, it.current.pos,
16094 TRY_WINDOW_CHECK_MARGINS))
16095 goto done;
16096 }
16097
16098 /* If centering point failed to make the whole line visible,
16099 put point at the top instead. That has to make the whole line
16100 visible, if it can be done. */
16101 if (centering_position == 0)
16102 goto done;
16103
16104 clear_glyph_matrix (w->desired_matrix);
16105 centering_position = 0;
16106 goto recenter;
16107 }
16108
16109 done:
16110
16111 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16112 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16113 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16114
16115 /* Display the mode line, if we must. */
16116 if ((update_mode_line
16117 /* If window not full width, must redo its mode line
16118 if (a) the window to its side is being redone and
16119 (b) we do a frame-based redisplay. This is a consequence
16120 of how inverted lines are drawn in frame-based redisplay. */
16121 || (!just_this_one_p
16122 && !FRAME_WINDOW_P (f)
16123 && !WINDOW_FULL_WIDTH_P (w))
16124 /* Line number to display. */
16125 || INTEGERP (w->base_line_pos)
16126 /* Column number is displayed and different from the one displayed. */
16127 || (!NILP (w->column_number_displayed)
16128 && (XFASTINT (w->column_number_displayed) != current_column ())))
16129 /* This means that the window has a mode line. */
16130 && (WINDOW_WANTS_MODELINE_P (w)
16131 || WINDOW_WANTS_HEADER_LINE_P (w)))
16132 {
16133 display_mode_lines (w);
16134
16135 /* If mode line height has changed, arrange for a thorough
16136 immediate redisplay using the correct mode line height. */
16137 if (WINDOW_WANTS_MODELINE_P (w)
16138 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16139 {
16140 fonts_changed_p = 1;
16141 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16142 = DESIRED_MODE_LINE_HEIGHT (w);
16143 }
16144
16145 /* If header line height has changed, arrange for a thorough
16146 immediate redisplay using the correct header line height. */
16147 if (WINDOW_WANTS_HEADER_LINE_P (w)
16148 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16149 {
16150 fonts_changed_p = 1;
16151 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16152 = DESIRED_HEADER_LINE_HEIGHT (w);
16153 }
16154
16155 if (fonts_changed_p)
16156 goto need_larger_matrices;
16157 }
16158
16159 if (!line_number_displayed
16160 && !BUFFERP (w->base_line_pos))
16161 {
16162 wset_base_line_pos (w, Qnil);
16163 wset_base_line_number (w, Qnil);
16164 }
16165
16166 finish_menu_bars:
16167
16168 /* When we reach a frame's selected window, redo the frame's menu bar. */
16169 if (update_mode_line
16170 && EQ (FRAME_SELECTED_WINDOW (f), window))
16171 {
16172 int redisplay_menu_p = 0;
16173
16174 if (FRAME_WINDOW_P (f))
16175 {
16176 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16177 || defined (HAVE_NS) || defined (USE_GTK)
16178 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16179 #else
16180 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16181 #endif
16182 }
16183 else
16184 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16185
16186 if (redisplay_menu_p)
16187 display_menu_bar (w);
16188
16189 #ifdef HAVE_WINDOW_SYSTEM
16190 if (FRAME_WINDOW_P (f))
16191 {
16192 #if defined (USE_GTK) || defined (HAVE_NS)
16193 if (FRAME_EXTERNAL_TOOL_BAR (f))
16194 redisplay_tool_bar (f);
16195 #else
16196 if (WINDOWP (f->tool_bar_window)
16197 && (FRAME_TOOL_BAR_LINES (f) > 0
16198 || !NILP (Vauto_resize_tool_bars))
16199 && redisplay_tool_bar (f))
16200 ignore_mouse_drag_p = 1;
16201 #endif
16202 }
16203 #endif
16204 }
16205
16206 #ifdef HAVE_WINDOW_SYSTEM
16207 if (FRAME_WINDOW_P (f)
16208 && update_window_fringes (w, (just_this_one_p
16209 || (!used_current_matrix_p && !overlay_arrow_seen)
16210 || w->pseudo_window_p)))
16211 {
16212 update_begin (f);
16213 BLOCK_INPUT;
16214 if (draw_window_fringes (w, 1))
16215 x_draw_vertical_border (w);
16216 UNBLOCK_INPUT;
16217 update_end (f);
16218 }
16219 #endif /* HAVE_WINDOW_SYSTEM */
16220
16221 /* We go to this label, with fonts_changed_p set,
16222 if it is necessary to try again using larger glyph matrices.
16223 We have to redeem the scroll bar even in this case,
16224 because the loop in redisplay_internal expects that. */
16225 need_larger_matrices:
16226 ;
16227 finish_scroll_bars:
16228
16229 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16230 {
16231 /* Set the thumb's position and size. */
16232 set_vertical_scroll_bar (w);
16233
16234 /* Note that we actually used the scroll bar attached to this
16235 window, so it shouldn't be deleted at the end of redisplay. */
16236 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16237 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16238 }
16239
16240 /* Restore current_buffer and value of point in it. The window
16241 update may have changed the buffer, so first make sure `opoint'
16242 is still valid (Bug#6177). */
16243 if (CHARPOS (opoint) < BEGV)
16244 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16245 else if (CHARPOS (opoint) > ZV)
16246 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16247 else
16248 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16249
16250 set_buffer_internal_1 (old);
16251 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16252 shorter. This can be caused by log truncation in *Messages*. */
16253 if (CHARPOS (lpoint) <= ZV)
16254 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16255
16256 unbind_to (count, Qnil);
16257 }
16258
16259
16260 /* Build the complete desired matrix of WINDOW with a window start
16261 buffer position POS.
16262
16263 Value is 1 if successful. It is zero if fonts were loaded during
16264 redisplay which makes re-adjusting glyph matrices necessary, and -1
16265 if point would appear in the scroll margins.
16266 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16267 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16268 set in FLAGS.) */
16269
16270 int
16271 try_window (Lisp_Object window, struct text_pos pos, int flags)
16272 {
16273 struct window *w = XWINDOW (window);
16274 struct it it;
16275 struct glyph_row *last_text_row = NULL;
16276 struct frame *f = XFRAME (w->frame);
16277
16278 /* Make POS the new window start. */
16279 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16280
16281 /* Mark cursor position as unknown. No overlay arrow seen. */
16282 w->cursor.vpos = -1;
16283 overlay_arrow_seen = 0;
16284
16285 /* Initialize iterator and info to start at POS. */
16286 start_display (&it, w, pos);
16287
16288 /* Display all lines of W. */
16289 while (it.current_y < it.last_visible_y)
16290 {
16291 if (display_line (&it))
16292 last_text_row = it.glyph_row - 1;
16293 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16294 return 0;
16295 }
16296
16297 /* Don't let the cursor end in the scroll margins. */
16298 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16299 && !MINI_WINDOW_P (w))
16300 {
16301 int this_scroll_margin;
16302
16303 if (scroll_margin > 0)
16304 {
16305 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16306 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16307 }
16308 else
16309 this_scroll_margin = 0;
16310
16311 if ((w->cursor.y >= 0 /* not vscrolled */
16312 && w->cursor.y < this_scroll_margin
16313 && CHARPOS (pos) > BEGV
16314 && IT_CHARPOS (it) < ZV)
16315 /* rms: considering make_cursor_line_fully_visible_p here
16316 seems to give wrong results. We don't want to recenter
16317 when the last line is partly visible, we want to allow
16318 that case to be handled in the usual way. */
16319 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16320 {
16321 w->cursor.vpos = -1;
16322 clear_glyph_matrix (w->desired_matrix);
16323 return -1;
16324 }
16325 }
16326
16327 /* If bottom moved off end of frame, change mode line percentage. */
16328 if (XFASTINT (w->window_end_pos) <= 0
16329 && Z != IT_CHARPOS (it))
16330 w->update_mode_line = 1;
16331
16332 /* Set window_end_pos to the offset of the last character displayed
16333 on the window from the end of current_buffer. Set
16334 window_end_vpos to its row number. */
16335 if (last_text_row)
16336 {
16337 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16338 w->window_end_bytepos
16339 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16340 wset_window_end_pos
16341 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16342 wset_window_end_vpos
16343 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16344 eassert
16345 (MATRIX_ROW (w->desired_matrix,
16346 XFASTINT (w->window_end_vpos))->displays_text_p);
16347 }
16348 else
16349 {
16350 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16351 wset_window_end_pos (w, make_number (Z - ZV));
16352 wset_window_end_vpos (w, make_number (0));
16353 }
16354
16355 /* But that is not valid info until redisplay finishes. */
16356 wset_window_end_valid (w, Qnil);
16357 return 1;
16358 }
16359
16360
16361 \f
16362 /************************************************************************
16363 Window redisplay reusing current matrix when buffer has not changed
16364 ************************************************************************/
16365
16366 /* Try redisplay of window W showing an unchanged buffer with a
16367 different window start than the last time it was displayed by
16368 reusing its current matrix. Value is non-zero if successful.
16369 W->start is the new window start. */
16370
16371 static int
16372 try_window_reusing_current_matrix (struct window *w)
16373 {
16374 struct frame *f = XFRAME (w->frame);
16375 struct glyph_row *bottom_row;
16376 struct it it;
16377 struct run run;
16378 struct text_pos start, new_start;
16379 int nrows_scrolled, i;
16380 struct glyph_row *last_text_row;
16381 struct glyph_row *last_reused_text_row;
16382 struct glyph_row *start_row;
16383 int start_vpos, min_y, max_y;
16384
16385 #ifdef GLYPH_DEBUG
16386 if (inhibit_try_window_reusing)
16387 return 0;
16388 #endif
16389
16390 if (/* This function doesn't handle terminal frames. */
16391 !FRAME_WINDOW_P (f)
16392 /* Don't try to reuse the display if windows have been split
16393 or such. */
16394 || windows_or_buffers_changed
16395 || cursor_type_changed)
16396 return 0;
16397
16398 /* Can't do this if region may have changed. */
16399 if ((!NILP (Vtransient_mark_mode)
16400 && !NILP (BVAR (current_buffer, mark_active)))
16401 || !NILP (w->region_showing)
16402 || !NILP (Vshow_trailing_whitespace))
16403 return 0;
16404
16405 /* If top-line visibility has changed, give up. */
16406 if (WINDOW_WANTS_HEADER_LINE_P (w)
16407 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16408 return 0;
16409
16410 /* Give up if old or new display is scrolled vertically. We could
16411 make this function handle this, but right now it doesn't. */
16412 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16413 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16414 return 0;
16415
16416 /* The variable new_start now holds the new window start. The old
16417 start `start' can be determined from the current matrix. */
16418 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16419 start = start_row->minpos;
16420 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16421
16422 /* Clear the desired matrix for the display below. */
16423 clear_glyph_matrix (w->desired_matrix);
16424
16425 if (CHARPOS (new_start) <= CHARPOS (start))
16426 {
16427 /* Don't use this method if the display starts with an ellipsis
16428 displayed for invisible text. It's not easy to handle that case
16429 below, and it's certainly not worth the effort since this is
16430 not a frequent case. */
16431 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16432 return 0;
16433
16434 IF_DEBUG (debug_method_add (w, "twu1"));
16435
16436 /* Display up to a row that can be reused. The variable
16437 last_text_row is set to the last row displayed that displays
16438 text. Note that it.vpos == 0 if or if not there is a
16439 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16440 start_display (&it, w, new_start);
16441 w->cursor.vpos = -1;
16442 last_text_row = last_reused_text_row = NULL;
16443
16444 while (it.current_y < it.last_visible_y
16445 && !fonts_changed_p)
16446 {
16447 /* If we have reached into the characters in the START row,
16448 that means the line boundaries have changed. So we
16449 can't start copying with the row START. Maybe it will
16450 work to start copying with the following row. */
16451 while (IT_CHARPOS (it) > CHARPOS (start))
16452 {
16453 /* Advance to the next row as the "start". */
16454 start_row++;
16455 start = start_row->minpos;
16456 /* If there are no more rows to try, or just one, give up. */
16457 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16458 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16459 || CHARPOS (start) == ZV)
16460 {
16461 clear_glyph_matrix (w->desired_matrix);
16462 return 0;
16463 }
16464
16465 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16466 }
16467 /* If we have reached alignment, we can copy the rest of the
16468 rows. */
16469 if (IT_CHARPOS (it) == CHARPOS (start)
16470 /* Don't accept "alignment" inside a display vector,
16471 since start_row could have started in the middle of
16472 that same display vector (thus their character
16473 positions match), and we have no way of telling if
16474 that is the case. */
16475 && it.current.dpvec_index < 0)
16476 break;
16477
16478 if (display_line (&it))
16479 last_text_row = it.glyph_row - 1;
16480
16481 }
16482
16483 /* A value of current_y < last_visible_y means that we stopped
16484 at the previous window start, which in turn means that we
16485 have at least one reusable row. */
16486 if (it.current_y < it.last_visible_y)
16487 {
16488 struct glyph_row *row;
16489
16490 /* IT.vpos always starts from 0; it counts text lines. */
16491 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16492
16493 /* Find PT if not already found in the lines displayed. */
16494 if (w->cursor.vpos < 0)
16495 {
16496 int dy = it.current_y - start_row->y;
16497
16498 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16499 row = row_containing_pos (w, PT, row, NULL, dy);
16500 if (row)
16501 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16502 dy, nrows_scrolled);
16503 else
16504 {
16505 clear_glyph_matrix (w->desired_matrix);
16506 return 0;
16507 }
16508 }
16509
16510 /* Scroll the display. Do it before the current matrix is
16511 changed. The problem here is that update has not yet
16512 run, i.e. part of the current matrix is not up to date.
16513 scroll_run_hook will clear the cursor, and use the
16514 current matrix to get the height of the row the cursor is
16515 in. */
16516 run.current_y = start_row->y;
16517 run.desired_y = it.current_y;
16518 run.height = it.last_visible_y - it.current_y;
16519
16520 if (run.height > 0 && run.current_y != run.desired_y)
16521 {
16522 update_begin (f);
16523 FRAME_RIF (f)->update_window_begin_hook (w);
16524 FRAME_RIF (f)->clear_window_mouse_face (w);
16525 FRAME_RIF (f)->scroll_run_hook (w, &run);
16526 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16527 update_end (f);
16528 }
16529
16530 /* Shift current matrix down by nrows_scrolled lines. */
16531 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16532 rotate_matrix (w->current_matrix,
16533 start_vpos,
16534 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16535 nrows_scrolled);
16536
16537 /* Disable lines that must be updated. */
16538 for (i = 0; i < nrows_scrolled; ++i)
16539 (start_row + i)->enabled_p = 0;
16540
16541 /* Re-compute Y positions. */
16542 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16543 max_y = it.last_visible_y;
16544 for (row = start_row + nrows_scrolled;
16545 row < bottom_row;
16546 ++row)
16547 {
16548 row->y = it.current_y;
16549 row->visible_height = row->height;
16550
16551 if (row->y < min_y)
16552 row->visible_height -= min_y - row->y;
16553 if (row->y + row->height > max_y)
16554 row->visible_height -= row->y + row->height - max_y;
16555 if (row->fringe_bitmap_periodic_p)
16556 row->redraw_fringe_bitmaps_p = 1;
16557
16558 it.current_y += row->height;
16559
16560 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16561 last_reused_text_row = row;
16562 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16563 break;
16564 }
16565
16566 /* Disable lines in the current matrix which are now
16567 below the window. */
16568 for (++row; row < bottom_row; ++row)
16569 row->enabled_p = row->mode_line_p = 0;
16570 }
16571
16572 /* Update window_end_pos etc.; last_reused_text_row is the last
16573 reused row from the current matrix containing text, if any.
16574 The value of last_text_row is the last displayed line
16575 containing text. */
16576 if (last_reused_text_row)
16577 {
16578 w->window_end_bytepos
16579 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16580 wset_window_end_pos
16581 (w, make_number (Z
16582 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16583 wset_window_end_vpos
16584 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16585 w->current_matrix)));
16586 }
16587 else if (last_text_row)
16588 {
16589 w->window_end_bytepos
16590 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16591 wset_window_end_pos
16592 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16593 wset_window_end_vpos
16594 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16595 w->desired_matrix)));
16596 }
16597 else
16598 {
16599 /* This window must be completely empty. */
16600 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16601 wset_window_end_pos (w, make_number (Z - ZV));
16602 wset_window_end_vpos (w, make_number (0));
16603 }
16604 wset_window_end_valid (w, Qnil);
16605
16606 /* Update hint: don't try scrolling again in update_window. */
16607 w->desired_matrix->no_scrolling_p = 1;
16608
16609 #ifdef GLYPH_DEBUG
16610 debug_method_add (w, "try_window_reusing_current_matrix 1");
16611 #endif
16612 return 1;
16613 }
16614 else if (CHARPOS (new_start) > CHARPOS (start))
16615 {
16616 struct glyph_row *pt_row, *row;
16617 struct glyph_row *first_reusable_row;
16618 struct glyph_row *first_row_to_display;
16619 int dy;
16620 int yb = window_text_bottom_y (w);
16621
16622 /* Find the row starting at new_start, if there is one. Don't
16623 reuse a partially visible line at the end. */
16624 first_reusable_row = start_row;
16625 while (first_reusable_row->enabled_p
16626 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16627 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16628 < CHARPOS (new_start)))
16629 ++first_reusable_row;
16630
16631 /* Give up if there is no row to reuse. */
16632 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16633 || !first_reusable_row->enabled_p
16634 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16635 != CHARPOS (new_start)))
16636 return 0;
16637
16638 /* We can reuse fully visible rows beginning with
16639 first_reusable_row to the end of the window. Set
16640 first_row_to_display to the first row that cannot be reused.
16641 Set pt_row to the row containing point, if there is any. */
16642 pt_row = NULL;
16643 for (first_row_to_display = first_reusable_row;
16644 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16645 ++first_row_to_display)
16646 {
16647 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16648 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16649 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16650 && first_row_to_display->ends_at_zv_p
16651 && pt_row == NULL)))
16652 pt_row = first_row_to_display;
16653 }
16654
16655 /* Start displaying at the start of first_row_to_display. */
16656 eassert (first_row_to_display->y < yb);
16657 init_to_row_start (&it, w, first_row_to_display);
16658
16659 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16660 - start_vpos);
16661 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16662 - nrows_scrolled);
16663 it.current_y = (first_row_to_display->y - first_reusable_row->y
16664 + WINDOW_HEADER_LINE_HEIGHT (w));
16665
16666 /* Display lines beginning with first_row_to_display in the
16667 desired matrix. Set last_text_row to the last row displayed
16668 that displays text. */
16669 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16670 if (pt_row == NULL)
16671 w->cursor.vpos = -1;
16672 last_text_row = NULL;
16673 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16674 if (display_line (&it))
16675 last_text_row = it.glyph_row - 1;
16676
16677 /* If point is in a reused row, adjust y and vpos of the cursor
16678 position. */
16679 if (pt_row)
16680 {
16681 w->cursor.vpos -= nrows_scrolled;
16682 w->cursor.y -= first_reusable_row->y - start_row->y;
16683 }
16684
16685 /* Give up if point isn't in a row displayed or reused. (This
16686 also handles the case where w->cursor.vpos < nrows_scrolled
16687 after the calls to display_line, which can happen with scroll
16688 margins. See bug#1295.) */
16689 if (w->cursor.vpos < 0)
16690 {
16691 clear_glyph_matrix (w->desired_matrix);
16692 return 0;
16693 }
16694
16695 /* Scroll the display. */
16696 run.current_y = first_reusable_row->y;
16697 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16698 run.height = it.last_visible_y - run.current_y;
16699 dy = run.current_y - run.desired_y;
16700
16701 if (run.height)
16702 {
16703 update_begin (f);
16704 FRAME_RIF (f)->update_window_begin_hook (w);
16705 FRAME_RIF (f)->clear_window_mouse_face (w);
16706 FRAME_RIF (f)->scroll_run_hook (w, &run);
16707 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16708 update_end (f);
16709 }
16710
16711 /* Adjust Y positions of reused rows. */
16712 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16713 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16714 max_y = it.last_visible_y;
16715 for (row = first_reusable_row; row < first_row_to_display; ++row)
16716 {
16717 row->y -= dy;
16718 row->visible_height = row->height;
16719 if (row->y < min_y)
16720 row->visible_height -= min_y - row->y;
16721 if (row->y + row->height > max_y)
16722 row->visible_height -= row->y + row->height - max_y;
16723 if (row->fringe_bitmap_periodic_p)
16724 row->redraw_fringe_bitmaps_p = 1;
16725 }
16726
16727 /* Scroll the current matrix. */
16728 eassert (nrows_scrolled > 0);
16729 rotate_matrix (w->current_matrix,
16730 start_vpos,
16731 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16732 -nrows_scrolled);
16733
16734 /* Disable rows not reused. */
16735 for (row -= nrows_scrolled; row < bottom_row; ++row)
16736 row->enabled_p = 0;
16737
16738 /* Point may have moved to a different line, so we cannot assume that
16739 the previous cursor position is valid; locate the correct row. */
16740 if (pt_row)
16741 {
16742 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16743 row < bottom_row
16744 && PT >= MATRIX_ROW_END_CHARPOS (row)
16745 && !row->ends_at_zv_p;
16746 row++)
16747 {
16748 w->cursor.vpos++;
16749 w->cursor.y = row->y;
16750 }
16751 if (row < bottom_row)
16752 {
16753 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16754 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16755
16756 /* Can't use this optimization with bidi-reordered glyph
16757 rows, unless cursor is already at point. */
16758 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16759 {
16760 if (!(w->cursor.hpos >= 0
16761 && w->cursor.hpos < row->used[TEXT_AREA]
16762 && BUFFERP (glyph->object)
16763 && glyph->charpos == PT))
16764 return 0;
16765 }
16766 else
16767 for (; glyph < end
16768 && (!BUFFERP (glyph->object)
16769 || glyph->charpos < PT);
16770 glyph++)
16771 {
16772 w->cursor.hpos++;
16773 w->cursor.x += glyph->pixel_width;
16774 }
16775 }
16776 }
16777
16778 /* Adjust window end. A null value of last_text_row means that
16779 the window end is in reused rows which in turn means that
16780 only its vpos can have changed. */
16781 if (last_text_row)
16782 {
16783 w->window_end_bytepos
16784 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16785 wset_window_end_pos
16786 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16787 wset_window_end_vpos
16788 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16789 w->desired_matrix)));
16790 }
16791 else
16792 {
16793 wset_window_end_vpos
16794 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16795 }
16796
16797 wset_window_end_valid (w, Qnil);
16798 w->desired_matrix->no_scrolling_p = 1;
16799
16800 #ifdef GLYPH_DEBUG
16801 debug_method_add (w, "try_window_reusing_current_matrix 2");
16802 #endif
16803 return 1;
16804 }
16805
16806 return 0;
16807 }
16808
16809
16810 \f
16811 /************************************************************************
16812 Window redisplay reusing current matrix when buffer has changed
16813 ************************************************************************/
16814
16815 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16816 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16817 ptrdiff_t *, ptrdiff_t *);
16818 static struct glyph_row *
16819 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16820 struct glyph_row *);
16821
16822
16823 /* Return the last row in MATRIX displaying text. If row START is
16824 non-null, start searching with that row. IT gives the dimensions
16825 of the display. Value is null if matrix is empty; otherwise it is
16826 a pointer to the row found. */
16827
16828 static struct glyph_row *
16829 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16830 struct glyph_row *start)
16831 {
16832 struct glyph_row *row, *row_found;
16833
16834 /* Set row_found to the last row in IT->w's current matrix
16835 displaying text. The loop looks funny but think of partially
16836 visible lines. */
16837 row_found = NULL;
16838 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16839 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16840 {
16841 eassert (row->enabled_p);
16842 row_found = row;
16843 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16844 break;
16845 ++row;
16846 }
16847
16848 return row_found;
16849 }
16850
16851
16852 /* Return the last row in the current matrix of W that is not affected
16853 by changes at the start of current_buffer that occurred since W's
16854 current matrix was built. Value is null if no such row exists.
16855
16856 BEG_UNCHANGED us the number of characters unchanged at the start of
16857 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16858 first changed character in current_buffer. Characters at positions <
16859 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16860 when the current matrix was built. */
16861
16862 static struct glyph_row *
16863 find_last_unchanged_at_beg_row (struct window *w)
16864 {
16865 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16866 struct glyph_row *row;
16867 struct glyph_row *row_found = NULL;
16868 int yb = window_text_bottom_y (w);
16869
16870 /* Find the last row displaying unchanged text. */
16871 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16872 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16873 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16874 ++row)
16875 {
16876 if (/* If row ends before first_changed_pos, it is unchanged,
16877 except in some case. */
16878 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16879 /* When row ends in ZV and we write at ZV it is not
16880 unchanged. */
16881 && !row->ends_at_zv_p
16882 /* When first_changed_pos is the end of a continued line,
16883 row is not unchanged because it may be no longer
16884 continued. */
16885 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16886 && (row->continued_p
16887 || row->exact_window_width_line_p))
16888 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16889 needs to be recomputed, so don't consider this row as
16890 unchanged. This happens when the last line was
16891 bidi-reordered and was killed immediately before this
16892 redisplay cycle. In that case, ROW->end stores the
16893 buffer position of the first visual-order character of
16894 the killed text, which is now beyond ZV. */
16895 && CHARPOS (row->end.pos) <= ZV)
16896 row_found = row;
16897
16898 /* Stop if last visible row. */
16899 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16900 break;
16901 }
16902
16903 return row_found;
16904 }
16905
16906
16907 /* Find the first glyph row in the current matrix of W that is not
16908 affected by changes at the end of current_buffer since the
16909 time W's current matrix was built.
16910
16911 Return in *DELTA the number of chars by which buffer positions in
16912 unchanged text at the end of current_buffer must be adjusted.
16913
16914 Return in *DELTA_BYTES the corresponding number of bytes.
16915
16916 Value is null if no such row exists, i.e. all rows are affected by
16917 changes. */
16918
16919 static struct glyph_row *
16920 find_first_unchanged_at_end_row (struct window *w,
16921 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16922 {
16923 struct glyph_row *row;
16924 struct glyph_row *row_found = NULL;
16925
16926 *delta = *delta_bytes = 0;
16927
16928 /* Display must not have been paused, otherwise the current matrix
16929 is not up to date. */
16930 eassert (!NILP (w->window_end_valid));
16931
16932 /* A value of window_end_pos >= END_UNCHANGED means that the window
16933 end is in the range of changed text. If so, there is no
16934 unchanged row at the end of W's current matrix. */
16935 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16936 return NULL;
16937
16938 /* Set row to the last row in W's current matrix displaying text. */
16939 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16940
16941 /* If matrix is entirely empty, no unchanged row exists. */
16942 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16943 {
16944 /* The value of row is the last glyph row in the matrix having a
16945 meaningful buffer position in it. The end position of row
16946 corresponds to window_end_pos. This allows us to translate
16947 buffer positions in the current matrix to current buffer
16948 positions for characters not in changed text. */
16949 ptrdiff_t Z_old =
16950 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16951 ptrdiff_t Z_BYTE_old =
16952 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16953 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16954 struct glyph_row *first_text_row
16955 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16956
16957 *delta = Z - Z_old;
16958 *delta_bytes = Z_BYTE - Z_BYTE_old;
16959
16960 /* Set last_unchanged_pos to the buffer position of the last
16961 character in the buffer that has not been changed. Z is the
16962 index + 1 of the last character in current_buffer, i.e. by
16963 subtracting END_UNCHANGED we get the index of the last
16964 unchanged character, and we have to add BEG to get its buffer
16965 position. */
16966 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16967 last_unchanged_pos_old = last_unchanged_pos - *delta;
16968
16969 /* Search backward from ROW for a row displaying a line that
16970 starts at a minimum position >= last_unchanged_pos_old. */
16971 for (; row > first_text_row; --row)
16972 {
16973 /* This used to abort, but it can happen.
16974 It is ok to just stop the search instead here. KFS. */
16975 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16976 break;
16977
16978 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16979 row_found = row;
16980 }
16981 }
16982
16983 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16984
16985 return row_found;
16986 }
16987
16988
16989 /* Make sure that glyph rows in the current matrix of window W
16990 reference the same glyph memory as corresponding rows in the
16991 frame's frame matrix. This function is called after scrolling W's
16992 current matrix on a terminal frame in try_window_id and
16993 try_window_reusing_current_matrix. */
16994
16995 static void
16996 sync_frame_with_window_matrix_rows (struct window *w)
16997 {
16998 struct frame *f = XFRAME (w->frame);
16999 struct glyph_row *window_row, *window_row_end, *frame_row;
17000
17001 /* Preconditions: W must be a leaf window and full-width. Its frame
17002 must have a frame matrix. */
17003 eassert (NILP (w->hchild) && NILP (w->vchild));
17004 eassert (WINDOW_FULL_WIDTH_P (w));
17005 eassert (!FRAME_WINDOW_P (f));
17006
17007 /* If W is a full-width window, glyph pointers in W's current matrix
17008 have, by definition, to be the same as glyph pointers in the
17009 corresponding frame matrix. Note that frame matrices have no
17010 marginal areas (see build_frame_matrix). */
17011 window_row = w->current_matrix->rows;
17012 window_row_end = window_row + w->current_matrix->nrows;
17013 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17014 while (window_row < window_row_end)
17015 {
17016 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17017 struct glyph *end = window_row->glyphs[LAST_AREA];
17018
17019 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17020 frame_row->glyphs[TEXT_AREA] = start;
17021 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17022 frame_row->glyphs[LAST_AREA] = end;
17023
17024 /* Disable frame rows whose corresponding window rows have
17025 been disabled in try_window_id. */
17026 if (!window_row->enabled_p)
17027 frame_row->enabled_p = 0;
17028
17029 ++window_row, ++frame_row;
17030 }
17031 }
17032
17033
17034 /* Find the glyph row in window W containing CHARPOS. Consider all
17035 rows between START and END (not inclusive). END null means search
17036 all rows to the end of the display area of W. Value is the row
17037 containing CHARPOS or null. */
17038
17039 struct glyph_row *
17040 row_containing_pos (struct window *w, ptrdiff_t charpos,
17041 struct glyph_row *start, struct glyph_row *end, int dy)
17042 {
17043 struct glyph_row *row = start;
17044 struct glyph_row *best_row = NULL;
17045 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17046 int last_y;
17047
17048 /* If we happen to start on a header-line, skip that. */
17049 if (row->mode_line_p)
17050 ++row;
17051
17052 if ((end && row >= end) || !row->enabled_p)
17053 return NULL;
17054
17055 last_y = window_text_bottom_y (w) - dy;
17056
17057 while (1)
17058 {
17059 /* Give up if we have gone too far. */
17060 if (end && row >= end)
17061 return NULL;
17062 /* This formerly returned if they were equal.
17063 I think that both quantities are of a "last plus one" type;
17064 if so, when they are equal, the row is within the screen. -- rms. */
17065 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17066 return NULL;
17067
17068 /* If it is in this row, return this row. */
17069 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17070 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17071 /* The end position of a row equals the start
17072 position of the next row. If CHARPOS is there, we
17073 would rather display it in the next line, except
17074 when this line ends in ZV. */
17075 && !row->ends_at_zv_p
17076 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17077 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17078 {
17079 struct glyph *g;
17080
17081 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17082 || (!best_row && !row->continued_p))
17083 return row;
17084 /* In bidi-reordered rows, there could be several rows
17085 occluding point, all of them belonging to the same
17086 continued line. We need to find the row which fits
17087 CHARPOS the best. */
17088 for (g = row->glyphs[TEXT_AREA];
17089 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17090 g++)
17091 {
17092 if (!STRINGP (g->object))
17093 {
17094 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17095 {
17096 mindif = eabs (g->charpos - charpos);
17097 best_row = row;
17098 /* Exact match always wins. */
17099 if (mindif == 0)
17100 return best_row;
17101 }
17102 }
17103 }
17104 }
17105 else if (best_row && !row->continued_p)
17106 return best_row;
17107 ++row;
17108 }
17109 }
17110
17111
17112 /* Try to redisplay window W by reusing its existing display. W's
17113 current matrix must be up to date when this function is called,
17114 i.e. window_end_valid must not be nil.
17115
17116 Value is
17117
17118 1 if display has been updated
17119 0 if otherwise unsuccessful
17120 -1 if redisplay with same window start is known not to succeed
17121
17122 The following steps are performed:
17123
17124 1. Find the last row in the current matrix of W that is not
17125 affected by changes at the start of current_buffer. If no such row
17126 is found, give up.
17127
17128 2. Find the first row in W's current matrix that is not affected by
17129 changes at the end of current_buffer. Maybe there is no such row.
17130
17131 3. Display lines beginning with the row + 1 found in step 1 to the
17132 row found in step 2 or, if step 2 didn't find a row, to the end of
17133 the window.
17134
17135 4. If cursor is not known to appear on the window, give up.
17136
17137 5. If display stopped at the row found in step 2, scroll the
17138 display and current matrix as needed.
17139
17140 6. Maybe display some lines at the end of W, if we must. This can
17141 happen under various circumstances, like a partially visible line
17142 becoming fully visible, or because newly displayed lines are displayed
17143 in smaller font sizes.
17144
17145 7. Update W's window end information. */
17146
17147 static int
17148 try_window_id (struct window *w)
17149 {
17150 struct frame *f = XFRAME (w->frame);
17151 struct glyph_matrix *current_matrix = w->current_matrix;
17152 struct glyph_matrix *desired_matrix = w->desired_matrix;
17153 struct glyph_row *last_unchanged_at_beg_row;
17154 struct glyph_row *first_unchanged_at_end_row;
17155 struct glyph_row *row;
17156 struct glyph_row *bottom_row;
17157 int bottom_vpos;
17158 struct it it;
17159 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17160 int dvpos, dy;
17161 struct text_pos start_pos;
17162 struct run run;
17163 int first_unchanged_at_end_vpos = 0;
17164 struct glyph_row *last_text_row, *last_text_row_at_end;
17165 struct text_pos start;
17166 ptrdiff_t first_changed_charpos, last_changed_charpos;
17167
17168 #ifdef GLYPH_DEBUG
17169 if (inhibit_try_window_id)
17170 return 0;
17171 #endif
17172
17173 /* This is handy for debugging. */
17174 #if 0
17175 #define GIVE_UP(X) \
17176 do { \
17177 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17178 return 0; \
17179 } while (0)
17180 #else
17181 #define GIVE_UP(X) return 0
17182 #endif
17183
17184 SET_TEXT_POS_FROM_MARKER (start, w->start);
17185
17186 /* Don't use this for mini-windows because these can show
17187 messages and mini-buffers, and we don't handle that here. */
17188 if (MINI_WINDOW_P (w))
17189 GIVE_UP (1);
17190
17191 /* This flag is used to prevent redisplay optimizations. */
17192 if (windows_or_buffers_changed || cursor_type_changed)
17193 GIVE_UP (2);
17194
17195 /* Verify that narrowing has not changed.
17196 Also verify that we were not told to prevent redisplay optimizations.
17197 It would be nice to further
17198 reduce the number of cases where this prevents try_window_id. */
17199 if (current_buffer->clip_changed
17200 || current_buffer->prevent_redisplay_optimizations_p)
17201 GIVE_UP (3);
17202
17203 /* Window must either use window-based redisplay or be full width. */
17204 if (!FRAME_WINDOW_P (f)
17205 && (!FRAME_LINE_INS_DEL_OK (f)
17206 || !WINDOW_FULL_WIDTH_P (w)))
17207 GIVE_UP (4);
17208
17209 /* Give up if point is known NOT to appear in W. */
17210 if (PT < CHARPOS (start))
17211 GIVE_UP (5);
17212
17213 /* Another way to prevent redisplay optimizations. */
17214 if (w->last_modified == 0)
17215 GIVE_UP (6);
17216
17217 /* Verify that window is not hscrolled. */
17218 if (w->hscroll != 0)
17219 GIVE_UP (7);
17220
17221 /* Verify that display wasn't paused. */
17222 if (NILP (w->window_end_valid))
17223 GIVE_UP (8);
17224
17225 /* Can't use this if highlighting a region because a cursor movement
17226 will do more than just set the cursor. */
17227 if (!NILP (Vtransient_mark_mode)
17228 && !NILP (BVAR (current_buffer, mark_active)))
17229 GIVE_UP (9);
17230
17231 /* Likewise if highlighting trailing whitespace. */
17232 if (!NILP (Vshow_trailing_whitespace))
17233 GIVE_UP (11);
17234
17235 /* Likewise if showing a region. */
17236 if (!NILP (w->region_showing))
17237 GIVE_UP (10);
17238
17239 /* Can't use this if overlay arrow position and/or string have
17240 changed. */
17241 if (overlay_arrows_changed_p ())
17242 GIVE_UP (12);
17243
17244 /* When word-wrap is on, adding a space to the first word of a
17245 wrapped line can change the wrap position, altering the line
17246 above it. It might be worthwhile to handle this more
17247 intelligently, but for now just redisplay from scratch. */
17248 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17249 GIVE_UP (21);
17250
17251 /* Under bidi reordering, adding or deleting a character in the
17252 beginning of a paragraph, before the first strong directional
17253 character, can change the base direction of the paragraph (unless
17254 the buffer specifies a fixed paragraph direction), which will
17255 require to redisplay the whole paragraph. It might be worthwhile
17256 to find the paragraph limits and widen the range of redisplayed
17257 lines to that, but for now just give up this optimization and
17258 redisplay from scratch. */
17259 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17260 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17261 GIVE_UP (22);
17262
17263 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17264 only if buffer has really changed. The reason is that the gap is
17265 initially at Z for freshly visited files. The code below would
17266 set end_unchanged to 0 in that case. */
17267 if (MODIFF > SAVE_MODIFF
17268 /* This seems to happen sometimes after saving a buffer. */
17269 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17270 {
17271 if (GPT - BEG < BEG_UNCHANGED)
17272 BEG_UNCHANGED = GPT - BEG;
17273 if (Z - GPT < END_UNCHANGED)
17274 END_UNCHANGED = Z - GPT;
17275 }
17276
17277 /* The position of the first and last character that has been changed. */
17278 first_changed_charpos = BEG + BEG_UNCHANGED;
17279 last_changed_charpos = Z - END_UNCHANGED;
17280
17281 /* If window starts after a line end, and the last change is in
17282 front of that newline, then changes don't affect the display.
17283 This case happens with stealth-fontification. Note that although
17284 the display is unchanged, glyph positions in the matrix have to
17285 be adjusted, of course. */
17286 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17287 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17288 && ((last_changed_charpos < CHARPOS (start)
17289 && CHARPOS (start) == BEGV)
17290 || (last_changed_charpos < CHARPOS (start) - 1
17291 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17292 {
17293 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17294 struct glyph_row *r0;
17295
17296 /* Compute how many chars/bytes have been added to or removed
17297 from the buffer. */
17298 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17299 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17300 Z_delta = Z - Z_old;
17301 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17302
17303 /* Give up if PT is not in the window. Note that it already has
17304 been checked at the start of try_window_id that PT is not in
17305 front of the window start. */
17306 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17307 GIVE_UP (13);
17308
17309 /* If window start is unchanged, we can reuse the whole matrix
17310 as is, after adjusting glyph positions. No need to compute
17311 the window end again, since its offset from Z hasn't changed. */
17312 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17313 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17314 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17315 /* PT must not be in a partially visible line. */
17316 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17317 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17318 {
17319 /* Adjust positions in the glyph matrix. */
17320 if (Z_delta || Z_delta_bytes)
17321 {
17322 struct glyph_row *r1
17323 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17324 increment_matrix_positions (w->current_matrix,
17325 MATRIX_ROW_VPOS (r0, current_matrix),
17326 MATRIX_ROW_VPOS (r1, current_matrix),
17327 Z_delta, Z_delta_bytes);
17328 }
17329
17330 /* Set the cursor. */
17331 row = row_containing_pos (w, PT, r0, NULL, 0);
17332 if (row)
17333 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17334 else
17335 emacs_abort ();
17336 return 1;
17337 }
17338 }
17339
17340 /* Handle the case that changes are all below what is displayed in
17341 the window, and that PT is in the window. This shortcut cannot
17342 be taken if ZV is visible in the window, and text has been added
17343 there that is visible in the window. */
17344 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17345 /* ZV is not visible in the window, or there are no
17346 changes at ZV, actually. */
17347 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17348 || first_changed_charpos == last_changed_charpos))
17349 {
17350 struct glyph_row *r0;
17351
17352 /* Give up if PT is not in the window. Note that it already has
17353 been checked at the start of try_window_id that PT is not in
17354 front of the window start. */
17355 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17356 GIVE_UP (14);
17357
17358 /* If window start is unchanged, we can reuse the whole matrix
17359 as is, without changing glyph positions since no text has
17360 been added/removed in front of the window end. */
17361 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17362 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17363 /* PT must not be in a partially visible line. */
17364 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17365 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17366 {
17367 /* We have to compute the window end anew since text
17368 could have been added/removed after it. */
17369 wset_window_end_pos
17370 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17371 w->window_end_bytepos
17372 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17373
17374 /* Set the cursor. */
17375 row = row_containing_pos (w, PT, r0, NULL, 0);
17376 if (row)
17377 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17378 else
17379 emacs_abort ();
17380 return 2;
17381 }
17382 }
17383
17384 /* Give up if window start is in the changed area.
17385
17386 The condition used to read
17387
17388 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17389
17390 but why that was tested escapes me at the moment. */
17391 if (CHARPOS (start) >= first_changed_charpos
17392 && CHARPOS (start) <= last_changed_charpos)
17393 GIVE_UP (15);
17394
17395 /* Check that window start agrees with the start of the first glyph
17396 row in its current matrix. Check this after we know the window
17397 start is not in changed text, otherwise positions would not be
17398 comparable. */
17399 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17400 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17401 GIVE_UP (16);
17402
17403 /* Give up if the window ends in strings. Overlay strings
17404 at the end are difficult to handle, so don't try. */
17405 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17406 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17407 GIVE_UP (20);
17408
17409 /* Compute the position at which we have to start displaying new
17410 lines. Some of the lines at the top of the window might be
17411 reusable because they are not displaying changed text. Find the
17412 last row in W's current matrix not affected by changes at the
17413 start of current_buffer. Value is null if changes start in the
17414 first line of window. */
17415 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17416 if (last_unchanged_at_beg_row)
17417 {
17418 /* Avoid starting to display in the middle of a character, a TAB
17419 for instance. This is easier than to set up the iterator
17420 exactly, and it's not a frequent case, so the additional
17421 effort wouldn't really pay off. */
17422 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17423 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17424 && last_unchanged_at_beg_row > w->current_matrix->rows)
17425 --last_unchanged_at_beg_row;
17426
17427 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17428 GIVE_UP (17);
17429
17430 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17431 GIVE_UP (18);
17432 start_pos = it.current.pos;
17433
17434 /* Start displaying new lines in the desired matrix at the same
17435 vpos we would use in the current matrix, i.e. below
17436 last_unchanged_at_beg_row. */
17437 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17438 current_matrix);
17439 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17440 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17441
17442 eassert (it.hpos == 0 && it.current_x == 0);
17443 }
17444 else
17445 {
17446 /* There are no reusable lines at the start of the window.
17447 Start displaying in the first text line. */
17448 start_display (&it, w, start);
17449 it.vpos = it.first_vpos;
17450 start_pos = it.current.pos;
17451 }
17452
17453 /* Find the first row that is not affected by changes at the end of
17454 the buffer. Value will be null if there is no unchanged row, in
17455 which case we must redisplay to the end of the window. delta
17456 will be set to the value by which buffer positions beginning with
17457 first_unchanged_at_end_row have to be adjusted due to text
17458 changes. */
17459 first_unchanged_at_end_row
17460 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17461 IF_DEBUG (debug_delta = delta);
17462 IF_DEBUG (debug_delta_bytes = delta_bytes);
17463
17464 /* Set stop_pos to the buffer position up to which we will have to
17465 display new lines. If first_unchanged_at_end_row != NULL, this
17466 is the buffer position of the start of the line displayed in that
17467 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17468 that we don't stop at a buffer position. */
17469 stop_pos = 0;
17470 if (first_unchanged_at_end_row)
17471 {
17472 eassert (last_unchanged_at_beg_row == NULL
17473 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17474
17475 /* If this is a continuation line, move forward to the next one
17476 that isn't. Changes in lines above affect this line.
17477 Caution: this may move first_unchanged_at_end_row to a row
17478 not displaying text. */
17479 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17480 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17481 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17482 < it.last_visible_y))
17483 ++first_unchanged_at_end_row;
17484
17485 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17486 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17487 >= it.last_visible_y))
17488 first_unchanged_at_end_row = NULL;
17489 else
17490 {
17491 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17492 + delta);
17493 first_unchanged_at_end_vpos
17494 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17495 eassert (stop_pos >= Z - END_UNCHANGED);
17496 }
17497 }
17498 else if (last_unchanged_at_beg_row == NULL)
17499 GIVE_UP (19);
17500
17501
17502 #ifdef GLYPH_DEBUG
17503
17504 /* Either there is no unchanged row at the end, or the one we have
17505 now displays text. This is a necessary condition for the window
17506 end pos calculation at the end of this function. */
17507 eassert (first_unchanged_at_end_row == NULL
17508 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17509
17510 debug_last_unchanged_at_beg_vpos
17511 = (last_unchanged_at_beg_row
17512 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17513 : -1);
17514 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17515
17516 #endif /* GLYPH_DEBUG */
17517
17518
17519 /* Display new lines. Set last_text_row to the last new line
17520 displayed which has text on it, i.e. might end up as being the
17521 line where the window_end_vpos is. */
17522 w->cursor.vpos = -1;
17523 last_text_row = NULL;
17524 overlay_arrow_seen = 0;
17525 while (it.current_y < it.last_visible_y
17526 && !fonts_changed_p
17527 && (first_unchanged_at_end_row == NULL
17528 || IT_CHARPOS (it) < stop_pos))
17529 {
17530 if (display_line (&it))
17531 last_text_row = it.glyph_row - 1;
17532 }
17533
17534 if (fonts_changed_p)
17535 return -1;
17536
17537
17538 /* Compute differences in buffer positions, y-positions etc. for
17539 lines reused at the bottom of the window. Compute what we can
17540 scroll. */
17541 if (first_unchanged_at_end_row
17542 /* No lines reused because we displayed everything up to the
17543 bottom of the window. */
17544 && it.current_y < it.last_visible_y)
17545 {
17546 dvpos = (it.vpos
17547 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17548 current_matrix));
17549 dy = it.current_y - first_unchanged_at_end_row->y;
17550 run.current_y = first_unchanged_at_end_row->y;
17551 run.desired_y = run.current_y + dy;
17552 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17553 }
17554 else
17555 {
17556 delta = delta_bytes = dvpos = dy
17557 = run.current_y = run.desired_y = run.height = 0;
17558 first_unchanged_at_end_row = NULL;
17559 }
17560 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17561
17562
17563 /* Find the cursor if not already found. We have to decide whether
17564 PT will appear on this window (it sometimes doesn't, but this is
17565 not a very frequent case.) This decision has to be made before
17566 the current matrix is altered. A value of cursor.vpos < 0 means
17567 that PT is either in one of the lines beginning at
17568 first_unchanged_at_end_row or below the window. Don't care for
17569 lines that might be displayed later at the window end; as
17570 mentioned, this is not a frequent case. */
17571 if (w->cursor.vpos < 0)
17572 {
17573 /* Cursor in unchanged rows at the top? */
17574 if (PT < CHARPOS (start_pos)
17575 && last_unchanged_at_beg_row)
17576 {
17577 row = row_containing_pos (w, PT,
17578 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17579 last_unchanged_at_beg_row + 1, 0);
17580 if (row)
17581 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17582 }
17583
17584 /* Start from first_unchanged_at_end_row looking for PT. */
17585 else if (first_unchanged_at_end_row)
17586 {
17587 row = row_containing_pos (w, PT - delta,
17588 first_unchanged_at_end_row, NULL, 0);
17589 if (row)
17590 set_cursor_from_row (w, row, w->current_matrix, delta,
17591 delta_bytes, dy, dvpos);
17592 }
17593
17594 /* Give up if cursor was not found. */
17595 if (w->cursor.vpos < 0)
17596 {
17597 clear_glyph_matrix (w->desired_matrix);
17598 return -1;
17599 }
17600 }
17601
17602 /* Don't let the cursor end in the scroll margins. */
17603 {
17604 int this_scroll_margin, cursor_height;
17605
17606 this_scroll_margin =
17607 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17608 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17609 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17610
17611 if ((w->cursor.y < this_scroll_margin
17612 && CHARPOS (start) > BEGV)
17613 /* Old redisplay didn't take scroll margin into account at the bottom,
17614 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17615 || (w->cursor.y + (make_cursor_line_fully_visible_p
17616 ? cursor_height + this_scroll_margin
17617 : 1)) > it.last_visible_y)
17618 {
17619 w->cursor.vpos = -1;
17620 clear_glyph_matrix (w->desired_matrix);
17621 return -1;
17622 }
17623 }
17624
17625 /* Scroll the display. Do it before changing the current matrix so
17626 that xterm.c doesn't get confused about where the cursor glyph is
17627 found. */
17628 if (dy && run.height)
17629 {
17630 update_begin (f);
17631
17632 if (FRAME_WINDOW_P (f))
17633 {
17634 FRAME_RIF (f)->update_window_begin_hook (w);
17635 FRAME_RIF (f)->clear_window_mouse_face (w);
17636 FRAME_RIF (f)->scroll_run_hook (w, &run);
17637 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17638 }
17639 else
17640 {
17641 /* Terminal frame. In this case, dvpos gives the number of
17642 lines to scroll by; dvpos < 0 means scroll up. */
17643 int from_vpos
17644 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17645 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17646 int end = (WINDOW_TOP_EDGE_LINE (w)
17647 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17648 + window_internal_height (w));
17649
17650 #if defined (HAVE_GPM) || defined (MSDOS)
17651 x_clear_window_mouse_face (w);
17652 #endif
17653 /* Perform the operation on the screen. */
17654 if (dvpos > 0)
17655 {
17656 /* Scroll last_unchanged_at_beg_row to the end of the
17657 window down dvpos lines. */
17658 set_terminal_window (f, end);
17659
17660 /* On dumb terminals delete dvpos lines at the end
17661 before inserting dvpos empty lines. */
17662 if (!FRAME_SCROLL_REGION_OK (f))
17663 ins_del_lines (f, end - dvpos, -dvpos);
17664
17665 /* Insert dvpos empty lines in front of
17666 last_unchanged_at_beg_row. */
17667 ins_del_lines (f, from, dvpos);
17668 }
17669 else if (dvpos < 0)
17670 {
17671 /* Scroll up last_unchanged_at_beg_vpos to the end of
17672 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17673 set_terminal_window (f, end);
17674
17675 /* Delete dvpos lines in front of
17676 last_unchanged_at_beg_vpos. ins_del_lines will set
17677 the cursor to the given vpos and emit |dvpos| delete
17678 line sequences. */
17679 ins_del_lines (f, from + dvpos, dvpos);
17680
17681 /* On a dumb terminal insert dvpos empty lines at the
17682 end. */
17683 if (!FRAME_SCROLL_REGION_OK (f))
17684 ins_del_lines (f, end + dvpos, -dvpos);
17685 }
17686
17687 set_terminal_window (f, 0);
17688 }
17689
17690 update_end (f);
17691 }
17692
17693 /* Shift reused rows of the current matrix to the right position.
17694 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17695 text. */
17696 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17697 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17698 if (dvpos < 0)
17699 {
17700 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17701 bottom_vpos, dvpos);
17702 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17703 bottom_vpos);
17704 }
17705 else if (dvpos > 0)
17706 {
17707 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17708 bottom_vpos, dvpos);
17709 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17710 first_unchanged_at_end_vpos + dvpos);
17711 }
17712
17713 /* For frame-based redisplay, make sure that current frame and window
17714 matrix are in sync with respect to glyph memory. */
17715 if (!FRAME_WINDOW_P (f))
17716 sync_frame_with_window_matrix_rows (w);
17717
17718 /* Adjust buffer positions in reused rows. */
17719 if (delta || delta_bytes)
17720 increment_matrix_positions (current_matrix,
17721 first_unchanged_at_end_vpos + dvpos,
17722 bottom_vpos, delta, delta_bytes);
17723
17724 /* Adjust Y positions. */
17725 if (dy)
17726 shift_glyph_matrix (w, current_matrix,
17727 first_unchanged_at_end_vpos + dvpos,
17728 bottom_vpos, dy);
17729
17730 if (first_unchanged_at_end_row)
17731 {
17732 first_unchanged_at_end_row += dvpos;
17733 if (first_unchanged_at_end_row->y >= it.last_visible_y
17734 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17735 first_unchanged_at_end_row = NULL;
17736 }
17737
17738 /* If scrolling up, there may be some lines to display at the end of
17739 the window. */
17740 last_text_row_at_end = NULL;
17741 if (dy < 0)
17742 {
17743 /* Scrolling up can leave for example a partially visible line
17744 at the end of the window to be redisplayed. */
17745 /* Set last_row to the glyph row in the current matrix where the
17746 window end line is found. It has been moved up or down in
17747 the matrix by dvpos. */
17748 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17749 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17750
17751 /* If last_row is the window end line, it should display text. */
17752 eassert (last_row->displays_text_p);
17753
17754 /* If window end line was partially visible before, begin
17755 displaying at that line. Otherwise begin displaying with the
17756 line following it. */
17757 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17758 {
17759 init_to_row_start (&it, w, last_row);
17760 it.vpos = last_vpos;
17761 it.current_y = last_row->y;
17762 }
17763 else
17764 {
17765 init_to_row_end (&it, w, last_row);
17766 it.vpos = 1 + last_vpos;
17767 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17768 ++last_row;
17769 }
17770
17771 /* We may start in a continuation line. If so, we have to
17772 get the right continuation_lines_width and current_x. */
17773 it.continuation_lines_width = last_row->continuation_lines_width;
17774 it.hpos = it.current_x = 0;
17775
17776 /* Display the rest of the lines at the window end. */
17777 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17778 while (it.current_y < it.last_visible_y
17779 && !fonts_changed_p)
17780 {
17781 /* Is it always sure that the display agrees with lines in
17782 the current matrix? I don't think so, so we mark rows
17783 displayed invalid in the current matrix by setting their
17784 enabled_p flag to zero. */
17785 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17786 if (display_line (&it))
17787 last_text_row_at_end = it.glyph_row - 1;
17788 }
17789 }
17790
17791 /* Update window_end_pos and window_end_vpos. */
17792 if (first_unchanged_at_end_row
17793 && !last_text_row_at_end)
17794 {
17795 /* Window end line if one of the preserved rows from the current
17796 matrix. Set row to the last row displaying text in current
17797 matrix starting at first_unchanged_at_end_row, after
17798 scrolling. */
17799 eassert (first_unchanged_at_end_row->displays_text_p);
17800 row = find_last_row_displaying_text (w->current_matrix, &it,
17801 first_unchanged_at_end_row);
17802 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17803
17804 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17805 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17806 wset_window_end_vpos
17807 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17808 eassert (w->window_end_bytepos >= 0);
17809 IF_DEBUG (debug_method_add (w, "A"));
17810 }
17811 else if (last_text_row_at_end)
17812 {
17813 wset_window_end_pos
17814 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17815 w->window_end_bytepos
17816 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17817 wset_window_end_vpos
17818 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17819 desired_matrix)));
17820 eassert (w->window_end_bytepos >= 0);
17821 IF_DEBUG (debug_method_add (w, "B"));
17822 }
17823 else if (last_text_row)
17824 {
17825 /* We have displayed either to the end of the window or at the
17826 end of the window, i.e. the last row with text is to be found
17827 in the desired matrix. */
17828 wset_window_end_pos
17829 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17830 w->window_end_bytepos
17831 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17832 wset_window_end_vpos
17833 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17834 eassert (w->window_end_bytepos >= 0);
17835 }
17836 else if (first_unchanged_at_end_row == NULL
17837 && last_text_row == NULL
17838 && last_text_row_at_end == NULL)
17839 {
17840 /* Displayed to end of window, but no line containing text was
17841 displayed. Lines were deleted at the end of the window. */
17842 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17843 int vpos = XFASTINT (w->window_end_vpos);
17844 struct glyph_row *current_row = current_matrix->rows + vpos;
17845 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17846
17847 for (row = NULL;
17848 row == NULL && vpos >= first_vpos;
17849 --vpos, --current_row, --desired_row)
17850 {
17851 if (desired_row->enabled_p)
17852 {
17853 if (desired_row->displays_text_p)
17854 row = desired_row;
17855 }
17856 else if (current_row->displays_text_p)
17857 row = current_row;
17858 }
17859
17860 eassert (row != NULL);
17861 wset_window_end_vpos (w, make_number (vpos + 1));
17862 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17863 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17864 eassert (w->window_end_bytepos >= 0);
17865 IF_DEBUG (debug_method_add (w, "C"));
17866 }
17867 else
17868 emacs_abort ();
17869
17870 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17871 debug_end_vpos = XFASTINT (w->window_end_vpos));
17872
17873 /* Record that display has not been completed. */
17874 wset_window_end_valid (w, Qnil);
17875 w->desired_matrix->no_scrolling_p = 1;
17876 return 3;
17877
17878 #undef GIVE_UP
17879 }
17880
17881
17882 \f
17883 /***********************************************************************
17884 More debugging support
17885 ***********************************************************************/
17886
17887 #ifdef GLYPH_DEBUG
17888
17889 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17890 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17891 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17892
17893
17894 /* Dump the contents of glyph matrix MATRIX on stderr.
17895
17896 GLYPHS 0 means don't show glyph contents.
17897 GLYPHS 1 means show glyphs in short form
17898 GLYPHS > 1 means show glyphs in long form. */
17899
17900 void
17901 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17902 {
17903 int i;
17904 for (i = 0; i < matrix->nrows; ++i)
17905 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17906 }
17907
17908
17909 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17910 the glyph row and area where the glyph comes from. */
17911
17912 void
17913 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17914 {
17915 if (glyph->type == CHAR_GLYPH)
17916 {
17917 fprintf (stderr,
17918 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17919 glyph - row->glyphs[TEXT_AREA],
17920 'C',
17921 glyph->charpos,
17922 (BUFFERP (glyph->object)
17923 ? 'B'
17924 : (STRINGP (glyph->object)
17925 ? 'S'
17926 : '-')),
17927 glyph->pixel_width,
17928 glyph->u.ch,
17929 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17930 ? glyph->u.ch
17931 : '.'),
17932 glyph->face_id,
17933 glyph->left_box_line_p,
17934 glyph->right_box_line_p);
17935 }
17936 else if (glyph->type == STRETCH_GLYPH)
17937 {
17938 fprintf (stderr,
17939 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17940 glyph - row->glyphs[TEXT_AREA],
17941 'S',
17942 glyph->charpos,
17943 (BUFFERP (glyph->object)
17944 ? 'B'
17945 : (STRINGP (glyph->object)
17946 ? 'S'
17947 : '-')),
17948 glyph->pixel_width,
17949 0,
17950 '.',
17951 glyph->face_id,
17952 glyph->left_box_line_p,
17953 glyph->right_box_line_p);
17954 }
17955 else if (glyph->type == IMAGE_GLYPH)
17956 {
17957 fprintf (stderr,
17958 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17959 glyph - row->glyphs[TEXT_AREA],
17960 'I',
17961 glyph->charpos,
17962 (BUFFERP (glyph->object)
17963 ? 'B'
17964 : (STRINGP (glyph->object)
17965 ? 'S'
17966 : '-')),
17967 glyph->pixel_width,
17968 glyph->u.img_id,
17969 '.',
17970 glyph->face_id,
17971 glyph->left_box_line_p,
17972 glyph->right_box_line_p);
17973 }
17974 else if (glyph->type == COMPOSITE_GLYPH)
17975 {
17976 fprintf (stderr,
17977 " %5td %4c %6"pI"d %c %3d 0x%05x",
17978 glyph - row->glyphs[TEXT_AREA],
17979 '+',
17980 glyph->charpos,
17981 (BUFFERP (glyph->object)
17982 ? 'B'
17983 : (STRINGP (glyph->object)
17984 ? 'S'
17985 : '-')),
17986 glyph->pixel_width,
17987 glyph->u.cmp.id);
17988 if (glyph->u.cmp.automatic)
17989 fprintf (stderr,
17990 "[%d-%d]",
17991 glyph->slice.cmp.from, glyph->slice.cmp.to);
17992 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17993 glyph->face_id,
17994 glyph->left_box_line_p,
17995 glyph->right_box_line_p);
17996 }
17997 }
17998
17999
18000 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18001 GLYPHS 0 means don't show glyph contents.
18002 GLYPHS 1 means show glyphs in short form
18003 GLYPHS > 1 means show glyphs in long form. */
18004
18005 void
18006 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18007 {
18008 if (glyphs != 1)
18009 {
18010 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18011 fprintf (stderr, "======================================================================\n");
18012
18013 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18014 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18015 vpos,
18016 MATRIX_ROW_START_CHARPOS (row),
18017 MATRIX_ROW_END_CHARPOS (row),
18018 row->used[TEXT_AREA],
18019 row->contains_overlapping_glyphs_p,
18020 row->enabled_p,
18021 row->truncated_on_left_p,
18022 row->truncated_on_right_p,
18023 row->continued_p,
18024 MATRIX_ROW_CONTINUATION_LINE_P (row),
18025 row->displays_text_p,
18026 row->ends_at_zv_p,
18027 row->fill_line_p,
18028 row->ends_in_middle_of_char_p,
18029 row->starts_in_middle_of_char_p,
18030 row->mouse_face_p,
18031 row->x,
18032 row->y,
18033 row->pixel_width,
18034 row->height,
18035 row->visible_height,
18036 row->ascent,
18037 row->phys_ascent);
18038 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18039 row->end.overlay_string_index,
18040 row->continuation_lines_width);
18041 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18042 CHARPOS (row->start.string_pos),
18043 CHARPOS (row->end.string_pos));
18044 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18045 row->end.dpvec_index);
18046 }
18047
18048 if (glyphs > 1)
18049 {
18050 int area;
18051
18052 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18053 {
18054 struct glyph *glyph = row->glyphs[area];
18055 struct glyph *glyph_end = glyph + row->used[area];
18056
18057 /* Glyph for a line end in text. */
18058 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18059 ++glyph_end;
18060
18061 if (glyph < glyph_end)
18062 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18063
18064 for (; glyph < glyph_end; ++glyph)
18065 dump_glyph (row, glyph, area);
18066 }
18067 }
18068 else if (glyphs == 1)
18069 {
18070 int area;
18071
18072 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18073 {
18074 char *s = alloca (row->used[area] + 1);
18075 int i;
18076
18077 for (i = 0; i < row->used[area]; ++i)
18078 {
18079 struct glyph *glyph = row->glyphs[area] + i;
18080 if (glyph->type == CHAR_GLYPH
18081 && glyph->u.ch < 0x80
18082 && glyph->u.ch >= ' ')
18083 s[i] = glyph->u.ch;
18084 else
18085 s[i] = '.';
18086 }
18087
18088 s[i] = '\0';
18089 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18090 }
18091 }
18092 }
18093
18094
18095 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18096 Sdump_glyph_matrix, 0, 1, "p",
18097 doc: /* Dump the current matrix of the selected window to stderr.
18098 Shows contents of glyph row structures. With non-nil
18099 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18100 glyphs in short form, otherwise show glyphs in long form. */)
18101 (Lisp_Object glyphs)
18102 {
18103 struct window *w = XWINDOW (selected_window);
18104 struct buffer *buffer = XBUFFER (w->buffer);
18105
18106 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18107 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18108 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18109 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18110 fprintf (stderr, "=============================================\n");
18111 dump_glyph_matrix (w->current_matrix,
18112 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18113 return Qnil;
18114 }
18115
18116
18117 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18118 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18119 (void)
18120 {
18121 struct frame *f = XFRAME (selected_frame);
18122 dump_glyph_matrix (f->current_matrix, 1);
18123 return Qnil;
18124 }
18125
18126
18127 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18128 doc: /* Dump glyph row ROW to stderr.
18129 GLYPH 0 means don't dump glyphs.
18130 GLYPH 1 means dump glyphs in short form.
18131 GLYPH > 1 or omitted means dump glyphs in long form. */)
18132 (Lisp_Object row, Lisp_Object glyphs)
18133 {
18134 struct glyph_matrix *matrix;
18135 EMACS_INT vpos;
18136
18137 CHECK_NUMBER (row);
18138 matrix = XWINDOW (selected_window)->current_matrix;
18139 vpos = XINT (row);
18140 if (vpos >= 0 && vpos < matrix->nrows)
18141 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18142 vpos,
18143 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18144 return Qnil;
18145 }
18146
18147
18148 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18149 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18150 GLYPH 0 means don't dump glyphs.
18151 GLYPH 1 means dump glyphs in short form.
18152 GLYPH > 1 or omitted means dump glyphs in long form. */)
18153 (Lisp_Object row, Lisp_Object glyphs)
18154 {
18155 struct frame *sf = SELECTED_FRAME ();
18156 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18157 EMACS_INT vpos;
18158
18159 CHECK_NUMBER (row);
18160 vpos = XINT (row);
18161 if (vpos >= 0 && vpos < m->nrows)
18162 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18163 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18164 return Qnil;
18165 }
18166
18167
18168 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18169 doc: /* Toggle tracing of redisplay.
18170 With ARG, turn tracing on if and only if ARG is positive. */)
18171 (Lisp_Object arg)
18172 {
18173 if (NILP (arg))
18174 trace_redisplay_p = !trace_redisplay_p;
18175 else
18176 {
18177 arg = Fprefix_numeric_value (arg);
18178 trace_redisplay_p = XINT (arg) > 0;
18179 }
18180
18181 return Qnil;
18182 }
18183
18184
18185 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18186 doc: /* Like `format', but print result to stderr.
18187 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18188 (ptrdiff_t nargs, Lisp_Object *args)
18189 {
18190 Lisp_Object s = Fformat (nargs, args);
18191 fprintf (stderr, "%s", SDATA (s));
18192 return Qnil;
18193 }
18194
18195 #endif /* GLYPH_DEBUG */
18196
18197
18198 \f
18199 /***********************************************************************
18200 Building Desired Matrix Rows
18201 ***********************************************************************/
18202
18203 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18204 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18205
18206 static struct glyph_row *
18207 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18208 {
18209 struct frame *f = XFRAME (WINDOW_FRAME (w));
18210 struct buffer *buffer = XBUFFER (w->buffer);
18211 struct buffer *old = current_buffer;
18212 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18213 int arrow_len = SCHARS (overlay_arrow_string);
18214 const unsigned char *arrow_end = arrow_string + arrow_len;
18215 const unsigned char *p;
18216 struct it it;
18217 int multibyte_p;
18218 int n_glyphs_before;
18219
18220 set_buffer_temp (buffer);
18221 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18222 it.glyph_row->used[TEXT_AREA] = 0;
18223 SET_TEXT_POS (it.position, 0, 0);
18224
18225 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18226 p = arrow_string;
18227 while (p < arrow_end)
18228 {
18229 Lisp_Object face, ilisp;
18230
18231 /* Get the next character. */
18232 if (multibyte_p)
18233 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18234 else
18235 {
18236 it.c = it.char_to_display = *p, it.len = 1;
18237 if (! ASCII_CHAR_P (it.c))
18238 it.char_to_display = BYTE8_TO_CHAR (it.c);
18239 }
18240 p += it.len;
18241
18242 /* Get its face. */
18243 ilisp = make_number (p - arrow_string);
18244 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18245 it.face_id = compute_char_face (f, it.char_to_display, face);
18246
18247 /* Compute its width, get its glyphs. */
18248 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18249 SET_TEXT_POS (it.position, -1, -1);
18250 PRODUCE_GLYPHS (&it);
18251
18252 /* If this character doesn't fit any more in the line, we have
18253 to remove some glyphs. */
18254 if (it.current_x > it.last_visible_x)
18255 {
18256 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18257 break;
18258 }
18259 }
18260
18261 set_buffer_temp (old);
18262 return it.glyph_row;
18263 }
18264
18265
18266 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18267 glyphs to insert is determined by produce_special_glyphs. */
18268
18269 static void
18270 insert_left_trunc_glyphs (struct it *it)
18271 {
18272 struct it truncate_it;
18273 struct glyph *from, *end, *to, *toend;
18274
18275 eassert (!FRAME_WINDOW_P (it->f)
18276 || (!it->glyph_row->reversed_p
18277 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18278 || (it->glyph_row->reversed_p
18279 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18280
18281 /* Get the truncation glyphs. */
18282 truncate_it = *it;
18283 truncate_it.current_x = 0;
18284 truncate_it.face_id = DEFAULT_FACE_ID;
18285 truncate_it.glyph_row = &scratch_glyph_row;
18286 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18287 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18288 truncate_it.object = make_number (0);
18289 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18290
18291 /* Overwrite glyphs from IT with truncation glyphs. */
18292 if (!it->glyph_row->reversed_p)
18293 {
18294 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18295
18296 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18297 end = from + tused;
18298 to = it->glyph_row->glyphs[TEXT_AREA];
18299 toend = to + it->glyph_row->used[TEXT_AREA];
18300 if (FRAME_WINDOW_P (it->f))
18301 {
18302 /* On GUI frames, when variable-size fonts are displayed,
18303 the truncation glyphs may need more pixels than the row's
18304 glyphs they overwrite. We overwrite more glyphs to free
18305 enough screen real estate, and enlarge the stretch glyph
18306 on the right (see display_line), if there is one, to
18307 preserve the screen position of the truncation glyphs on
18308 the right. */
18309 int w = 0;
18310 struct glyph *g = to;
18311 short used;
18312
18313 /* The first glyph could be partially visible, in which case
18314 it->glyph_row->x will be negative. But we want the left
18315 truncation glyphs to be aligned at the left margin of the
18316 window, so we override the x coordinate at which the row
18317 will begin. */
18318 it->glyph_row->x = 0;
18319 while (g < toend && w < it->truncation_pixel_width)
18320 {
18321 w += g->pixel_width;
18322 ++g;
18323 }
18324 if (g - to - tused > 0)
18325 {
18326 memmove (to + tused, g, (toend - g) * sizeof(*g));
18327 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18328 }
18329 used = it->glyph_row->used[TEXT_AREA];
18330 if (it->glyph_row->truncated_on_right_p
18331 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18332 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18333 == STRETCH_GLYPH)
18334 {
18335 int extra = w - it->truncation_pixel_width;
18336
18337 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18338 }
18339 }
18340
18341 while (from < end)
18342 *to++ = *from++;
18343
18344 /* There may be padding glyphs left over. Overwrite them too. */
18345 if (!FRAME_WINDOW_P (it->f))
18346 {
18347 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18348 {
18349 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18350 while (from < end)
18351 *to++ = *from++;
18352 }
18353 }
18354
18355 if (to > toend)
18356 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18357 }
18358 else
18359 {
18360 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18361
18362 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18363 that back to front. */
18364 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18365 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18366 toend = it->glyph_row->glyphs[TEXT_AREA];
18367 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18368 if (FRAME_WINDOW_P (it->f))
18369 {
18370 int w = 0;
18371 struct glyph *g = to;
18372
18373 while (g >= toend && w < it->truncation_pixel_width)
18374 {
18375 w += g->pixel_width;
18376 --g;
18377 }
18378 if (to - g - tused > 0)
18379 to = g + tused;
18380 if (it->glyph_row->truncated_on_right_p
18381 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18382 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18383 {
18384 int extra = w - it->truncation_pixel_width;
18385
18386 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18387 }
18388 }
18389
18390 while (from >= end && to >= toend)
18391 *to-- = *from--;
18392 if (!FRAME_WINDOW_P (it->f))
18393 {
18394 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18395 {
18396 from =
18397 truncate_it.glyph_row->glyphs[TEXT_AREA]
18398 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18399 while (from >= end && to >= toend)
18400 *to-- = *from--;
18401 }
18402 }
18403 if (from >= end)
18404 {
18405 /* Need to free some room before prepending additional
18406 glyphs. */
18407 int move_by = from - end + 1;
18408 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18409 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18410
18411 for ( ; g >= g0; g--)
18412 g[move_by] = *g;
18413 while (from >= end)
18414 *to-- = *from--;
18415 it->glyph_row->used[TEXT_AREA] += move_by;
18416 }
18417 }
18418 }
18419
18420 /* Compute the hash code for ROW. */
18421 unsigned
18422 row_hash (struct glyph_row *row)
18423 {
18424 int area, k;
18425 unsigned hashval = 0;
18426
18427 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18428 for (k = 0; k < row->used[area]; ++k)
18429 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18430 + row->glyphs[area][k].u.val
18431 + row->glyphs[area][k].face_id
18432 + row->glyphs[area][k].padding_p
18433 + (row->glyphs[area][k].type << 2));
18434
18435 return hashval;
18436 }
18437
18438 /* Compute the pixel height and width of IT->glyph_row.
18439
18440 Most of the time, ascent and height of a display line will be equal
18441 to the max_ascent and max_height values of the display iterator
18442 structure. This is not the case if
18443
18444 1. We hit ZV without displaying anything. In this case, max_ascent
18445 and max_height will be zero.
18446
18447 2. We have some glyphs that don't contribute to the line height.
18448 (The glyph row flag contributes_to_line_height_p is for future
18449 pixmap extensions).
18450
18451 The first case is easily covered by using default values because in
18452 these cases, the line height does not really matter, except that it
18453 must not be zero. */
18454
18455 static void
18456 compute_line_metrics (struct it *it)
18457 {
18458 struct glyph_row *row = it->glyph_row;
18459
18460 if (FRAME_WINDOW_P (it->f))
18461 {
18462 int i, min_y, max_y;
18463
18464 /* The line may consist of one space only, that was added to
18465 place the cursor on it. If so, the row's height hasn't been
18466 computed yet. */
18467 if (row->height == 0)
18468 {
18469 if (it->max_ascent + it->max_descent == 0)
18470 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18471 row->ascent = it->max_ascent;
18472 row->height = it->max_ascent + it->max_descent;
18473 row->phys_ascent = it->max_phys_ascent;
18474 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18475 row->extra_line_spacing = it->max_extra_line_spacing;
18476 }
18477
18478 /* Compute the width of this line. */
18479 row->pixel_width = row->x;
18480 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18481 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18482
18483 eassert (row->pixel_width >= 0);
18484 eassert (row->ascent >= 0 && row->height > 0);
18485
18486 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18487 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18488
18489 /* If first line's physical ascent is larger than its logical
18490 ascent, use the physical ascent, and make the row taller.
18491 This makes accented characters fully visible. */
18492 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18493 && row->phys_ascent > row->ascent)
18494 {
18495 row->height += row->phys_ascent - row->ascent;
18496 row->ascent = row->phys_ascent;
18497 }
18498
18499 /* Compute how much of the line is visible. */
18500 row->visible_height = row->height;
18501
18502 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18503 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18504
18505 if (row->y < min_y)
18506 row->visible_height -= min_y - row->y;
18507 if (row->y + row->height > max_y)
18508 row->visible_height -= row->y + row->height - max_y;
18509 }
18510 else
18511 {
18512 row->pixel_width = row->used[TEXT_AREA];
18513 if (row->continued_p)
18514 row->pixel_width -= it->continuation_pixel_width;
18515 else if (row->truncated_on_right_p)
18516 row->pixel_width -= it->truncation_pixel_width;
18517 row->ascent = row->phys_ascent = 0;
18518 row->height = row->phys_height = row->visible_height = 1;
18519 row->extra_line_spacing = 0;
18520 }
18521
18522 /* Compute a hash code for this row. */
18523 row->hash = row_hash (row);
18524
18525 it->max_ascent = it->max_descent = 0;
18526 it->max_phys_ascent = it->max_phys_descent = 0;
18527 }
18528
18529
18530 /* Append one space to the glyph row of iterator IT if doing a
18531 window-based redisplay. The space has the same face as
18532 IT->face_id. Value is non-zero if a space was added.
18533
18534 This function is called to make sure that there is always one glyph
18535 at the end of a glyph row that the cursor can be set on under
18536 window-systems. (If there weren't such a glyph we would not know
18537 how wide and tall a box cursor should be displayed).
18538
18539 At the same time this space let's a nicely handle clearing to the
18540 end of the line if the row ends in italic text. */
18541
18542 static int
18543 append_space_for_newline (struct it *it, int default_face_p)
18544 {
18545 if (FRAME_WINDOW_P (it->f))
18546 {
18547 int n = it->glyph_row->used[TEXT_AREA];
18548
18549 if (it->glyph_row->glyphs[TEXT_AREA] + n
18550 < it->glyph_row->glyphs[1 + TEXT_AREA])
18551 {
18552 /* Save some values that must not be changed.
18553 Must save IT->c and IT->len because otherwise
18554 ITERATOR_AT_END_P wouldn't work anymore after
18555 append_space_for_newline has been called. */
18556 enum display_element_type saved_what = it->what;
18557 int saved_c = it->c, saved_len = it->len;
18558 int saved_char_to_display = it->char_to_display;
18559 int saved_x = it->current_x;
18560 int saved_face_id = it->face_id;
18561 struct text_pos saved_pos;
18562 Lisp_Object saved_object;
18563 struct face *face;
18564
18565 saved_object = it->object;
18566 saved_pos = it->position;
18567
18568 it->what = IT_CHARACTER;
18569 memset (&it->position, 0, sizeof it->position);
18570 it->object = make_number (0);
18571 it->c = it->char_to_display = ' ';
18572 it->len = 1;
18573
18574 /* If the default face was remapped, be sure to use the
18575 remapped face for the appended newline. */
18576 if (default_face_p)
18577 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18578 else if (it->face_before_selective_p)
18579 it->face_id = it->saved_face_id;
18580 face = FACE_FROM_ID (it->f, it->face_id);
18581 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18582
18583 PRODUCE_GLYPHS (it);
18584
18585 it->override_ascent = -1;
18586 it->constrain_row_ascent_descent_p = 0;
18587 it->current_x = saved_x;
18588 it->object = saved_object;
18589 it->position = saved_pos;
18590 it->what = saved_what;
18591 it->face_id = saved_face_id;
18592 it->len = saved_len;
18593 it->c = saved_c;
18594 it->char_to_display = saved_char_to_display;
18595 return 1;
18596 }
18597 }
18598
18599 return 0;
18600 }
18601
18602
18603 /* Extend the face of the last glyph in the text area of IT->glyph_row
18604 to the end of the display line. Called from display_line. If the
18605 glyph row is empty, add a space glyph to it so that we know the
18606 face to draw. Set the glyph row flag fill_line_p. If the glyph
18607 row is R2L, prepend a stretch glyph to cover the empty space to the
18608 left of the leftmost glyph. */
18609
18610 static void
18611 extend_face_to_end_of_line (struct it *it)
18612 {
18613 struct face *face, *default_face;
18614 struct frame *f = it->f;
18615
18616 /* If line is already filled, do nothing. Non window-system frames
18617 get a grace of one more ``pixel'' because their characters are
18618 1-``pixel'' wide, so they hit the equality too early. This grace
18619 is needed only for R2L rows that are not continued, to produce
18620 one extra blank where we could display the cursor. */
18621 if (it->current_x >= it->last_visible_x
18622 + (!FRAME_WINDOW_P (f)
18623 && it->glyph_row->reversed_p
18624 && !it->glyph_row->continued_p))
18625 return;
18626
18627 /* The default face, possibly remapped. */
18628 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18629
18630 /* Face extension extends the background and box of IT->face_id
18631 to the end of the line. If the background equals the background
18632 of the frame, we don't have to do anything. */
18633 if (it->face_before_selective_p)
18634 face = FACE_FROM_ID (f, it->saved_face_id);
18635 else
18636 face = FACE_FROM_ID (f, it->face_id);
18637
18638 if (FRAME_WINDOW_P (f)
18639 && it->glyph_row->displays_text_p
18640 && face->box == FACE_NO_BOX
18641 && face->background == FRAME_BACKGROUND_PIXEL (f)
18642 && !face->stipple
18643 && !it->glyph_row->reversed_p)
18644 return;
18645
18646 /* Set the glyph row flag indicating that the face of the last glyph
18647 in the text area has to be drawn to the end of the text area. */
18648 it->glyph_row->fill_line_p = 1;
18649
18650 /* If current character of IT is not ASCII, make sure we have the
18651 ASCII face. This will be automatically undone the next time
18652 get_next_display_element returns a multibyte character. Note
18653 that the character will always be single byte in unibyte
18654 text. */
18655 if (!ASCII_CHAR_P (it->c))
18656 {
18657 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18658 }
18659
18660 if (FRAME_WINDOW_P (f))
18661 {
18662 /* If the row is empty, add a space with the current face of IT,
18663 so that we know which face to draw. */
18664 if (it->glyph_row->used[TEXT_AREA] == 0)
18665 {
18666 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18667 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18668 it->glyph_row->used[TEXT_AREA] = 1;
18669 }
18670 #ifdef HAVE_WINDOW_SYSTEM
18671 if (it->glyph_row->reversed_p)
18672 {
18673 /* Prepend a stretch glyph to the row, such that the
18674 rightmost glyph will be drawn flushed all the way to the
18675 right margin of the window. The stretch glyph that will
18676 occupy the empty space, if any, to the left of the
18677 glyphs. */
18678 struct font *font = face->font ? face->font : FRAME_FONT (f);
18679 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18680 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18681 struct glyph *g;
18682 int row_width, stretch_ascent, stretch_width;
18683 struct text_pos saved_pos;
18684 int saved_face_id, saved_avoid_cursor;
18685
18686 for (row_width = 0, g = row_start; g < row_end; g++)
18687 row_width += g->pixel_width;
18688 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18689 if (stretch_width > 0)
18690 {
18691 stretch_ascent =
18692 (((it->ascent + it->descent)
18693 * FONT_BASE (font)) / FONT_HEIGHT (font));
18694 saved_pos = it->position;
18695 memset (&it->position, 0, sizeof it->position);
18696 saved_avoid_cursor = it->avoid_cursor_p;
18697 it->avoid_cursor_p = 1;
18698 saved_face_id = it->face_id;
18699 /* The last row's stretch glyph should get the default
18700 face, to avoid painting the rest of the window with
18701 the region face, if the region ends at ZV. */
18702 if (it->glyph_row->ends_at_zv_p)
18703 it->face_id = default_face->id;
18704 else
18705 it->face_id = face->id;
18706 append_stretch_glyph (it, make_number (0), stretch_width,
18707 it->ascent + it->descent, stretch_ascent);
18708 it->position = saved_pos;
18709 it->avoid_cursor_p = saved_avoid_cursor;
18710 it->face_id = saved_face_id;
18711 }
18712 }
18713 #endif /* HAVE_WINDOW_SYSTEM */
18714 }
18715 else
18716 {
18717 /* Save some values that must not be changed. */
18718 int saved_x = it->current_x;
18719 struct text_pos saved_pos;
18720 Lisp_Object saved_object;
18721 enum display_element_type saved_what = it->what;
18722 int saved_face_id = it->face_id;
18723
18724 saved_object = it->object;
18725 saved_pos = it->position;
18726
18727 it->what = IT_CHARACTER;
18728 memset (&it->position, 0, sizeof it->position);
18729 it->object = make_number (0);
18730 it->c = it->char_to_display = ' ';
18731 it->len = 1;
18732 /* The last row's blank glyphs should get the default face, to
18733 avoid painting the rest of the window with the region face,
18734 if the region ends at ZV. */
18735 if (it->glyph_row->ends_at_zv_p)
18736 it->face_id = default_face->id;
18737 else
18738 it->face_id = face->id;
18739
18740 PRODUCE_GLYPHS (it);
18741
18742 while (it->current_x <= it->last_visible_x)
18743 PRODUCE_GLYPHS (it);
18744
18745 /* Don't count these blanks really. It would let us insert a left
18746 truncation glyph below and make us set the cursor on them, maybe. */
18747 it->current_x = saved_x;
18748 it->object = saved_object;
18749 it->position = saved_pos;
18750 it->what = saved_what;
18751 it->face_id = saved_face_id;
18752 }
18753 }
18754
18755
18756 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18757 trailing whitespace. */
18758
18759 static int
18760 trailing_whitespace_p (ptrdiff_t charpos)
18761 {
18762 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18763 int c = 0;
18764
18765 while (bytepos < ZV_BYTE
18766 && (c = FETCH_CHAR (bytepos),
18767 c == ' ' || c == '\t'))
18768 ++bytepos;
18769
18770 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18771 {
18772 if (bytepos != PT_BYTE)
18773 return 1;
18774 }
18775 return 0;
18776 }
18777
18778
18779 /* Highlight trailing whitespace, if any, in ROW. */
18780
18781 static void
18782 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18783 {
18784 int used = row->used[TEXT_AREA];
18785
18786 if (used)
18787 {
18788 struct glyph *start = row->glyphs[TEXT_AREA];
18789 struct glyph *glyph = start + used - 1;
18790
18791 if (row->reversed_p)
18792 {
18793 /* Right-to-left rows need to be processed in the opposite
18794 direction, so swap the edge pointers. */
18795 glyph = start;
18796 start = row->glyphs[TEXT_AREA] + used - 1;
18797 }
18798
18799 /* Skip over glyphs inserted to display the cursor at the
18800 end of a line, for extending the face of the last glyph
18801 to the end of the line on terminals, and for truncation
18802 and continuation glyphs. */
18803 if (!row->reversed_p)
18804 {
18805 while (glyph >= start
18806 && glyph->type == CHAR_GLYPH
18807 && INTEGERP (glyph->object))
18808 --glyph;
18809 }
18810 else
18811 {
18812 while (glyph <= start
18813 && glyph->type == CHAR_GLYPH
18814 && INTEGERP (glyph->object))
18815 ++glyph;
18816 }
18817
18818 /* If last glyph is a space or stretch, and it's trailing
18819 whitespace, set the face of all trailing whitespace glyphs in
18820 IT->glyph_row to `trailing-whitespace'. */
18821 if ((row->reversed_p ? glyph <= start : glyph >= start)
18822 && BUFFERP (glyph->object)
18823 && (glyph->type == STRETCH_GLYPH
18824 || (glyph->type == CHAR_GLYPH
18825 && glyph->u.ch == ' '))
18826 && trailing_whitespace_p (glyph->charpos))
18827 {
18828 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18829 if (face_id < 0)
18830 return;
18831
18832 if (!row->reversed_p)
18833 {
18834 while (glyph >= start
18835 && BUFFERP (glyph->object)
18836 && (glyph->type == STRETCH_GLYPH
18837 || (glyph->type == CHAR_GLYPH
18838 && glyph->u.ch == ' ')))
18839 (glyph--)->face_id = face_id;
18840 }
18841 else
18842 {
18843 while (glyph <= start
18844 && BUFFERP (glyph->object)
18845 && (glyph->type == STRETCH_GLYPH
18846 || (glyph->type == CHAR_GLYPH
18847 && glyph->u.ch == ' ')))
18848 (glyph++)->face_id = face_id;
18849 }
18850 }
18851 }
18852 }
18853
18854
18855 /* Value is non-zero if glyph row ROW should be
18856 used to hold the cursor. */
18857
18858 static int
18859 cursor_row_p (struct glyph_row *row)
18860 {
18861 int result = 1;
18862
18863 if (PT == CHARPOS (row->end.pos)
18864 || PT == MATRIX_ROW_END_CHARPOS (row))
18865 {
18866 /* Suppose the row ends on a string.
18867 Unless the row is continued, that means it ends on a newline
18868 in the string. If it's anything other than a display string
18869 (e.g., a before-string from an overlay), we don't want the
18870 cursor there. (This heuristic seems to give the optimal
18871 behavior for the various types of multi-line strings.)
18872 One exception: if the string has `cursor' property on one of
18873 its characters, we _do_ want the cursor there. */
18874 if (CHARPOS (row->end.string_pos) >= 0)
18875 {
18876 if (row->continued_p)
18877 result = 1;
18878 else
18879 {
18880 /* Check for `display' property. */
18881 struct glyph *beg = row->glyphs[TEXT_AREA];
18882 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18883 struct glyph *glyph;
18884
18885 result = 0;
18886 for (glyph = end; glyph >= beg; --glyph)
18887 if (STRINGP (glyph->object))
18888 {
18889 Lisp_Object prop
18890 = Fget_char_property (make_number (PT),
18891 Qdisplay, Qnil);
18892 result =
18893 (!NILP (prop)
18894 && display_prop_string_p (prop, glyph->object));
18895 /* If there's a `cursor' property on one of the
18896 string's characters, this row is a cursor row,
18897 even though this is not a display string. */
18898 if (!result)
18899 {
18900 Lisp_Object s = glyph->object;
18901
18902 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18903 {
18904 ptrdiff_t gpos = glyph->charpos;
18905
18906 if (!NILP (Fget_char_property (make_number (gpos),
18907 Qcursor, s)))
18908 {
18909 result = 1;
18910 break;
18911 }
18912 }
18913 }
18914 break;
18915 }
18916 }
18917 }
18918 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18919 {
18920 /* If the row ends in middle of a real character,
18921 and the line is continued, we want the cursor here.
18922 That's because CHARPOS (ROW->end.pos) would equal
18923 PT if PT is before the character. */
18924 if (!row->ends_in_ellipsis_p)
18925 result = row->continued_p;
18926 else
18927 /* If the row ends in an ellipsis, then
18928 CHARPOS (ROW->end.pos) will equal point after the
18929 invisible text. We want that position to be displayed
18930 after the ellipsis. */
18931 result = 0;
18932 }
18933 /* If the row ends at ZV, display the cursor at the end of that
18934 row instead of at the start of the row below. */
18935 else if (row->ends_at_zv_p)
18936 result = 1;
18937 else
18938 result = 0;
18939 }
18940
18941 return result;
18942 }
18943
18944 \f
18945
18946 /* Push the property PROP so that it will be rendered at the current
18947 position in IT. Return 1 if PROP was successfully pushed, 0
18948 otherwise. Called from handle_line_prefix to handle the
18949 `line-prefix' and `wrap-prefix' properties. */
18950
18951 static int
18952 push_prefix_prop (struct it *it, Lisp_Object prop)
18953 {
18954 struct text_pos pos =
18955 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18956
18957 eassert (it->method == GET_FROM_BUFFER
18958 || it->method == GET_FROM_DISPLAY_VECTOR
18959 || it->method == GET_FROM_STRING);
18960
18961 /* We need to save the current buffer/string position, so it will be
18962 restored by pop_it, because iterate_out_of_display_property
18963 depends on that being set correctly, but some situations leave
18964 it->position not yet set when this function is called. */
18965 push_it (it, &pos);
18966
18967 if (STRINGP (prop))
18968 {
18969 if (SCHARS (prop) == 0)
18970 {
18971 pop_it (it);
18972 return 0;
18973 }
18974
18975 it->string = prop;
18976 it->string_from_prefix_prop_p = 1;
18977 it->multibyte_p = STRING_MULTIBYTE (it->string);
18978 it->current.overlay_string_index = -1;
18979 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18980 it->end_charpos = it->string_nchars = SCHARS (it->string);
18981 it->method = GET_FROM_STRING;
18982 it->stop_charpos = 0;
18983 it->prev_stop = 0;
18984 it->base_level_stop = 0;
18985
18986 /* Force paragraph direction to be that of the parent
18987 buffer/string. */
18988 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18989 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18990 else
18991 it->paragraph_embedding = L2R;
18992
18993 /* Set up the bidi iterator for this display string. */
18994 if (it->bidi_p)
18995 {
18996 it->bidi_it.string.lstring = it->string;
18997 it->bidi_it.string.s = NULL;
18998 it->bidi_it.string.schars = it->end_charpos;
18999 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19000 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19001 it->bidi_it.string.unibyte = !it->multibyte_p;
19002 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19003 }
19004 }
19005 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19006 {
19007 it->method = GET_FROM_STRETCH;
19008 it->object = prop;
19009 }
19010 #ifdef HAVE_WINDOW_SYSTEM
19011 else if (IMAGEP (prop))
19012 {
19013 it->what = IT_IMAGE;
19014 it->image_id = lookup_image (it->f, prop);
19015 it->method = GET_FROM_IMAGE;
19016 }
19017 #endif /* HAVE_WINDOW_SYSTEM */
19018 else
19019 {
19020 pop_it (it); /* bogus display property, give up */
19021 return 0;
19022 }
19023
19024 return 1;
19025 }
19026
19027 /* Return the character-property PROP at the current position in IT. */
19028
19029 static Lisp_Object
19030 get_it_property (struct it *it, Lisp_Object prop)
19031 {
19032 Lisp_Object position;
19033
19034 if (STRINGP (it->object))
19035 position = make_number (IT_STRING_CHARPOS (*it));
19036 else if (BUFFERP (it->object))
19037 position = make_number (IT_CHARPOS (*it));
19038 else
19039 return Qnil;
19040
19041 return Fget_char_property (position, prop, it->object);
19042 }
19043
19044 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19045
19046 static void
19047 handle_line_prefix (struct it *it)
19048 {
19049 Lisp_Object prefix;
19050
19051 if (it->continuation_lines_width > 0)
19052 {
19053 prefix = get_it_property (it, Qwrap_prefix);
19054 if (NILP (prefix))
19055 prefix = Vwrap_prefix;
19056 }
19057 else
19058 {
19059 prefix = get_it_property (it, Qline_prefix);
19060 if (NILP (prefix))
19061 prefix = Vline_prefix;
19062 }
19063 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19064 {
19065 /* If the prefix is wider than the window, and we try to wrap
19066 it, it would acquire its own wrap prefix, and so on till the
19067 iterator stack overflows. So, don't wrap the prefix. */
19068 it->line_wrap = TRUNCATE;
19069 it->avoid_cursor_p = 1;
19070 }
19071 }
19072
19073 \f
19074
19075 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19076 only for R2L lines from display_line and display_string, when they
19077 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19078 the line/string needs to be continued on the next glyph row. */
19079 static void
19080 unproduce_glyphs (struct it *it, int n)
19081 {
19082 struct glyph *glyph, *end;
19083
19084 eassert (it->glyph_row);
19085 eassert (it->glyph_row->reversed_p);
19086 eassert (it->area == TEXT_AREA);
19087 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19088
19089 if (n > it->glyph_row->used[TEXT_AREA])
19090 n = it->glyph_row->used[TEXT_AREA];
19091 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19092 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19093 for ( ; glyph < end; glyph++)
19094 glyph[-n] = *glyph;
19095 }
19096
19097 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19098 and ROW->maxpos. */
19099 static void
19100 find_row_edges (struct it *it, struct glyph_row *row,
19101 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19102 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19103 {
19104 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19105 lines' rows is implemented for bidi-reordered rows. */
19106
19107 /* ROW->minpos is the value of min_pos, the minimal buffer position
19108 we have in ROW, or ROW->start.pos if that is smaller. */
19109 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19110 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19111 else
19112 /* We didn't find buffer positions smaller than ROW->start, or
19113 didn't find _any_ valid buffer positions in any of the glyphs,
19114 so we must trust the iterator's computed positions. */
19115 row->minpos = row->start.pos;
19116 if (max_pos <= 0)
19117 {
19118 max_pos = CHARPOS (it->current.pos);
19119 max_bpos = BYTEPOS (it->current.pos);
19120 }
19121
19122 /* Here are the various use-cases for ending the row, and the
19123 corresponding values for ROW->maxpos:
19124
19125 Line ends in a newline from buffer eol_pos + 1
19126 Line is continued from buffer max_pos + 1
19127 Line is truncated on right it->current.pos
19128 Line ends in a newline from string max_pos + 1(*)
19129 (*) + 1 only when line ends in a forward scan
19130 Line is continued from string max_pos
19131 Line is continued from display vector max_pos
19132 Line is entirely from a string min_pos == max_pos
19133 Line is entirely from a display vector min_pos == max_pos
19134 Line that ends at ZV ZV
19135
19136 If you discover other use-cases, please add them here as
19137 appropriate. */
19138 if (row->ends_at_zv_p)
19139 row->maxpos = it->current.pos;
19140 else if (row->used[TEXT_AREA])
19141 {
19142 int seen_this_string = 0;
19143 struct glyph_row *r1 = row - 1;
19144
19145 /* Did we see the same display string on the previous row? */
19146 if (STRINGP (it->object)
19147 /* this is not the first row */
19148 && row > it->w->desired_matrix->rows
19149 /* previous row is not the header line */
19150 && !r1->mode_line_p
19151 /* previous row also ends in a newline from a string */
19152 && r1->ends_in_newline_from_string_p)
19153 {
19154 struct glyph *start, *end;
19155
19156 /* Search for the last glyph of the previous row that came
19157 from buffer or string. Depending on whether the row is
19158 L2R or R2L, we need to process it front to back or the
19159 other way round. */
19160 if (!r1->reversed_p)
19161 {
19162 start = r1->glyphs[TEXT_AREA];
19163 end = start + r1->used[TEXT_AREA];
19164 /* Glyphs inserted by redisplay have an integer (zero)
19165 as their object. */
19166 while (end > start
19167 && INTEGERP ((end - 1)->object)
19168 && (end - 1)->charpos <= 0)
19169 --end;
19170 if (end > start)
19171 {
19172 if (EQ ((end - 1)->object, it->object))
19173 seen_this_string = 1;
19174 }
19175 else
19176 /* If all the glyphs of the previous row were inserted
19177 by redisplay, it means the previous row was
19178 produced from a single newline, which is only
19179 possible if that newline came from the same string
19180 as the one which produced this ROW. */
19181 seen_this_string = 1;
19182 }
19183 else
19184 {
19185 end = r1->glyphs[TEXT_AREA] - 1;
19186 start = end + r1->used[TEXT_AREA];
19187 while (end < start
19188 && INTEGERP ((end + 1)->object)
19189 && (end + 1)->charpos <= 0)
19190 ++end;
19191 if (end < start)
19192 {
19193 if (EQ ((end + 1)->object, it->object))
19194 seen_this_string = 1;
19195 }
19196 else
19197 seen_this_string = 1;
19198 }
19199 }
19200 /* Take note of each display string that covers a newline only
19201 once, the first time we see it. This is for when a display
19202 string includes more than one newline in it. */
19203 if (row->ends_in_newline_from_string_p && !seen_this_string)
19204 {
19205 /* If we were scanning the buffer forward when we displayed
19206 the string, we want to account for at least one buffer
19207 position that belongs to this row (position covered by
19208 the display string), so that cursor positioning will
19209 consider this row as a candidate when point is at the end
19210 of the visual line represented by this row. This is not
19211 required when scanning back, because max_pos will already
19212 have a much larger value. */
19213 if (CHARPOS (row->end.pos) > max_pos)
19214 INC_BOTH (max_pos, max_bpos);
19215 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19216 }
19217 else if (CHARPOS (it->eol_pos) > 0)
19218 SET_TEXT_POS (row->maxpos,
19219 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19220 else if (row->continued_p)
19221 {
19222 /* If max_pos is different from IT's current position, it
19223 means IT->method does not belong to the display element
19224 at max_pos. However, it also means that the display
19225 element at max_pos was displayed in its entirety on this
19226 line, which is equivalent to saying that the next line
19227 starts at the next buffer position. */
19228 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19229 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19230 else
19231 {
19232 INC_BOTH (max_pos, max_bpos);
19233 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19234 }
19235 }
19236 else if (row->truncated_on_right_p)
19237 /* display_line already called reseat_at_next_visible_line_start,
19238 which puts the iterator at the beginning of the next line, in
19239 the logical order. */
19240 row->maxpos = it->current.pos;
19241 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19242 /* A line that is entirely from a string/image/stretch... */
19243 row->maxpos = row->minpos;
19244 else
19245 emacs_abort ();
19246 }
19247 else
19248 row->maxpos = it->current.pos;
19249 }
19250
19251 /* Construct the glyph row IT->glyph_row in the desired matrix of
19252 IT->w from text at the current position of IT. See dispextern.h
19253 for an overview of struct it. Value is non-zero if
19254 IT->glyph_row displays text, as opposed to a line displaying ZV
19255 only. */
19256
19257 static int
19258 display_line (struct it *it)
19259 {
19260 struct glyph_row *row = it->glyph_row;
19261 Lisp_Object overlay_arrow_string;
19262 struct it wrap_it;
19263 void *wrap_data = NULL;
19264 int may_wrap = 0, wrap_x IF_LINT (= 0);
19265 int wrap_row_used = -1;
19266 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19267 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19268 int wrap_row_extra_line_spacing IF_LINT (= 0);
19269 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19270 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19271 int cvpos;
19272 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19273 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19274
19275 /* We always start displaying at hpos zero even if hscrolled. */
19276 eassert (it->hpos == 0 && it->current_x == 0);
19277
19278 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19279 >= it->w->desired_matrix->nrows)
19280 {
19281 it->w->nrows_scale_factor++;
19282 fonts_changed_p = 1;
19283 return 0;
19284 }
19285
19286 /* Is IT->w showing the region? */
19287 wset_region_showing (it->w, it->region_beg_charpos > 0 ? Qt : Qnil);
19288
19289 /* Clear the result glyph row and enable it. */
19290 prepare_desired_row (row);
19291
19292 row->y = it->current_y;
19293 row->start = it->start;
19294 row->continuation_lines_width = it->continuation_lines_width;
19295 row->displays_text_p = 1;
19296 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19297 it->starts_in_middle_of_char_p = 0;
19298
19299 /* Arrange the overlays nicely for our purposes. Usually, we call
19300 display_line on only one line at a time, in which case this
19301 can't really hurt too much, or we call it on lines which appear
19302 one after another in the buffer, in which case all calls to
19303 recenter_overlay_lists but the first will be pretty cheap. */
19304 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19305
19306 /* Move over display elements that are not visible because we are
19307 hscrolled. This may stop at an x-position < IT->first_visible_x
19308 if the first glyph is partially visible or if we hit a line end. */
19309 if (it->current_x < it->first_visible_x)
19310 {
19311 enum move_it_result move_result;
19312
19313 this_line_min_pos = row->start.pos;
19314 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19315 MOVE_TO_POS | MOVE_TO_X);
19316 /* If we are under a large hscroll, move_it_in_display_line_to
19317 could hit the end of the line without reaching
19318 it->first_visible_x. Pretend that we did reach it. This is
19319 especially important on a TTY, where we will call
19320 extend_face_to_end_of_line, which needs to know how many
19321 blank glyphs to produce. */
19322 if (it->current_x < it->first_visible_x
19323 && (move_result == MOVE_NEWLINE_OR_CR
19324 || move_result == MOVE_POS_MATCH_OR_ZV))
19325 it->current_x = it->first_visible_x;
19326
19327 /* Record the smallest positions seen while we moved over
19328 display elements that are not visible. This is needed by
19329 redisplay_internal for optimizing the case where the cursor
19330 stays inside the same line. The rest of this function only
19331 considers positions that are actually displayed, so
19332 RECORD_MAX_MIN_POS will not otherwise record positions that
19333 are hscrolled to the left of the left edge of the window. */
19334 min_pos = CHARPOS (this_line_min_pos);
19335 min_bpos = BYTEPOS (this_line_min_pos);
19336 }
19337 else
19338 {
19339 /* We only do this when not calling `move_it_in_display_line_to'
19340 above, because move_it_in_display_line_to calls
19341 handle_line_prefix itself. */
19342 handle_line_prefix (it);
19343 }
19344
19345 /* Get the initial row height. This is either the height of the
19346 text hscrolled, if there is any, or zero. */
19347 row->ascent = it->max_ascent;
19348 row->height = it->max_ascent + it->max_descent;
19349 row->phys_ascent = it->max_phys_ascent;
19350 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19351 row->extra_line_spacing = it->max_extra_line_spacing;
19352
19353 /* Utility macro to record max and min buffer positions seen until now. */
19354 #define RECORD_MAX_MIN_POS(IT) \
19355 do \
19356 { \
19357 int composition_p = !STRINGP ((IT)->string) \
19358 && ((IT)->what == IT_COMPOSITION); \
19359 ptrdiff_t current_pos = \
19360 composition_p ? (IT)->cmp_it.charpos \
19361 : IT_CHARPOS (*(IT)); \
19362 ptrdiff_t current_bpos = \
19363 composition_p ? CHAR_TO_BYTE (current_pos) \
19364 : IT_BYTEPOS (*(IT)); \
19365 if (current_pos < min_pos) \
19366 { \
19367 min_pos = current_pos; \
19368 min_bpos = current_bpos; \
19369 } \
19370 if (IT_CHARPOS (*it) > max_pos) \
19371 { \
19372 max_pos = IT_CHARPOS (*it); \
19373 max_bpos = IT_BYTEPOS (*it); \
19374 } \
19375 } \
19376 while (0)
19377
19378 /* Loop generating characters. The loop is left with IT on the next
19379 character to display. */
19380 while (1)
19381 {
19382 int n_glyphs_before, hpos_before, x_before;
19383 int x, nglyphs;
19384 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19385
19386 /* Retrieve the next thing to display. Value is zero if end of
19387 buffer reached. */
19388 if (!get_next_display_element (it))
19389 {
19390 /* Maybe add a space at the end of this line that is used to
19391 display the cursor there under X. Set the charpos of the
19392 first glyph of blank lines not corresponding to any text
19393 to -1. */
19394 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19395 row->exact_window_width_line_p = 1;
19396 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19397 || row->used[TEXT_AREA] == 0)
19398 {
19399 row->glyphs[TEXT_AREA]->charpos = -1;
19400 row->displays_text_p = 0;
19401
19402 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19403 && (!MINI_WINDOW_P (it->w)
19404 || (minibuf_level && EQ (it->window, minibuf_window))))
19405 row->indicate_empty_line_p = 1;
19406 }
19407
19408 it->continuation_lines_width = 0;
19409 row->ends_at_zv_p = 1;
19410 /* A row that displays right-to-left text must always have
19411 its last face extended all the way to the end of line,
19412 even if this row ends in ZV, because we still write to
19413 the screen left to right. We also need to extend the
19414 last face if the default face is remapped to some
19415 different face, otherwise the functions that clear
19416 portions of the screen will clear with the default face's
19417 background color. */
19418 if (row->reversed_p
19419 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19420 extend_face_to_end_of_line (it);
19421 break;
19422 }
19423
19424 /* Now, get the metrics of what we want to display. This also
19425 generates glyphs in `row' (which is IT->glyph_row). */
19426 n_glyphs_before = row->used[TEXT_AREA];
19427 x = it->current_x;
19428
19429 /* Remember the line height so far in case the next element doesn't
19430 fit on the line. */
19431 if (it->line_wrap != TRUNCATE)
19432 {
19433 ascent = it->max_ascent;
19434 descent = it->max_descent;
19435 phys_ascent = it->max_phys_ascent;
19436 phys_descent = it->max_phys_descent;
19437
19438 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19439 {
19440 if (IT_DISPLAYING_WHITESPACE (it))
19441 may_wrap = 1;
19442 else if (may_wrap)
19443 {
19444 SAVE_IT (wrap_it, *it, wrap_data);
19445 wrap_x = x;
19446 wrap_row_used = row->used[TEXT_AREA];
19447 wrap_row_ascent = row->ascent;
19448 wrap_row_height = row->height;
19449 wrap_row_phys_ascent = row->phys_ascent;
19450 wrap_row_phys_height = row->phys_height;
19451 wrap_row_extra_line_spacing = row->extra_line_spacing;
19452 wrap_row_min_pos = min_pos;
19453 wrap_row_min_bpos = min_bpos;
19454 wrap_row_max_pos = max_pos;
19455 wrap_row_max_bpos = max_bpos;
19456 may_wrap = 0;
19457 }
19458 }
19459 }
19460
19461 PRODUCE_GLYPHS (it);
19462
19463 /* If this display element was in marginal areas, continue with
19464 the next one. */
19465 if (it->area != TEXT_AREA)
19466 {
19467 row->ascent = max (row->ascent, it->max_ascent);
19468 row->height = max (row->height, it->max_ascent + it->max_descent);
19469 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19470 row->phys_height = max (row->phys_height,
19471 it->max_phys_ascent + it->max_phys_descent);
19472 row->extra_line_spacing = max (row->extra_line_spacing,
19473 it->max_extra_line_spacing);
19474 set_iterator_to_next (it, 1);
19475 continue;
19476 }
19477
19478 /* Does the display element fit on the line? If we truncate
19479 lines, we should draw past the right edge of the window. If
19480 we don't truncate, we want to stop so that we can display the
19481 continuation glyph before the right margin. If lines are
19482 continued, there are two possible strategies for characters
19483 resulting in more than 1 glyph (e.g. tabs): Display as many
19484 glyphs as possible in this line and leave the rest for the
19485 continuation line, or display the whole element in the next
19486 line. Original redisplay did the former, so we do it also. */
19487 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19488 hpos_before = it->hpos;
19489 x_before = x;
19490
19491 if (/* Not a newline. */
19492 nglyphs > 0
19493 /* Glyphs produced fit entirely in the line. */
19494 && it->current_x < it->last_visible_x)
19495 {
19496 it->hpos += nglyphs;
19497 row->ascent = max (row->ascent, it->max_ascent);
19498 row->height = max (row->height, it->max_ascent + it->max_descent);
19499 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19500 row->phys_height = max (row->phys_height,
19501 it->max_phys_ascent + it->max_phys_descent);
19502 row->extra_line_spacing = max (row->extra_line_spacing,
19503 it->max_extra_line_spacing);
19504 if (it->current_x - it->pixel_width < it->first_visible_x)
19505 row->x = x - it->first_visible_x;
19506 /* Record the maximum and minimum buffer positions seen so
19507 far in glyphs that will be displayed by this row. */
19508 if (it->bidi_p)
19509 RECORD_MAX_MIN_POS (it);
19510 }
19511 else
19512 {
19513 int i, new_x;
19514 struct glyph *glyph;
19515
19516 for (i = 0; i < nglyphs; ++i, x = new_x)
19517 {
19518 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19519 new_x = x + glyph->pixel_width;
19520
19521 if (/* Lines are continued. */
19522 it->line_wrap != TRUNCATE
19523 && (/* Glyph doesn't fit on the line. */
19524 new_x > it->last_visible_x
19525 /* Or it fits exactly on a window system frame. */
19526 || (new_x == it->last_visible_x
19527 && FRAME_WINDOW_P (it->f)
19528 && (row->reversed_p
19529 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19530 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19531 {
19532 /* End of a continued line. */
19533
19534 if (it->hpos == 0
19535 || (new_x == it->last_visible_x
19536 && FRAME_WINDOW_P (it->f)
19537 && (row->reversed_p
19538 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19539 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19540 {
19541 /* Current glyph is the only one on the line or
19542 fits exactly on the line. We must continue
19543 the line because we can't draw the cursor
19544 after the glyph. */
19545 row->continued_p = 1;
19546 it->current_x = new_x;
19547 it->continuation_lines_width += new_x;
19548 ++it->hpos;
19549 if (i == nglyphs - 1)
19550 {
19551 /* If line-wrap is on, check if a previous
19552 wrap point was found. */
19553 if (wrap_row_used > 0
19554 /* Even if there is a previous wrap
19555 point, continue the line here as
19556 usual, if (i) the previous character
19557 was a space or tab AND (ii) the
19558 current character is not. */
19559 && (!may_wrap
19560 || IT_DISPLAYING_WHITESPACE (it)))
19561 goto back_to_wrap;
19562
19563 /* Record the maximum and minimum buffer
19564 positions seen so far in glyphs that will be
19565 displayed by this row. */
19566 if (it->bidi_p)
19567 RECORD_MAX_MIN_POS (it);
19568 set_iterator_to_next (it, 1);
19569 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19570 {
19571 if (!get_next_display_element (it))
19572 {
19573 row->exact_window_width_line_p = 1;
19574 it->continuation_lines_width = 0;
19575 row->continued_p = 0;
19576 row->ends_at_zv_p = 1;
19577 }
19578 else if (ITERATOR_AT_END_OF_LINE_P (it))
19579 {
19580 row->continued_p = 0;
19581 row->exact_window_width_line_p = 1;
19582 }
19583 }
19584 }
19585 else if (it->bidi_p)
19586 RECORD_MAX_MIN_POS (it);
19587 }
19588 else if (CHAR_GLYPH_PADDING_P (*glyph)
19589 && !FRAME_WINDOW_P (it->f))
19590 {
19591 /* A padding glyph that doesn't fit on this line.
19592 This means the whole character doesn't fit
19593 on the line. */
19594 if (row->reversed_p)
19595 unproduce_glyphs (it, row->used[TEXT_AREA]
19596 - n_glyphs_before);
19597 row->used[TEXT_AREA] = n_glyphs_before;
19598
19599 /* Fill the rest of the row with continuation
19600 glyphs like in 20.x. */
19601 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19602 < row->glyphs[1 + TEXT_AREA])
19603 produce_special_glyphs (it, IT_CONTINUATION);
19604
19605 row->continued_p = 1;
19606 it->current_x = x_before;
19607 it->continuation_lines_width += x_before;
19608
19609 /* Restore the height to what it was before the
19610 element not fitting on the line. */
19611 it->max_ascent = ascent;
19612 it->max_descent = descent;
19613 it->max_phys_ascent = phys_ascent;
19614 it->max_phys_descent = phys_descent;
19615 }
19616 else if (wrap_row_used > 0)
19617 {
19618 back_to_wrap:
19619 if (row->reversed_p)
19620 unproduce_glyphs (it,
19621 row->used[TEXT_AREA] - wrap_row_used);
19622 RESTORE_IT (it, &wrap_it, wrap_data);
19623 it->continuation_lines_width += wrap_x;
19624 row->used[TEXT_AREA] = wrap_row_used;
19625 row->ascent = wrap_row_ascent;
19626 row->height = wrap_row_height;
19627 row->phys_ascent = wrap_row_phys_ascent;
19628 row->phys_height = wrap_row_phys_height;
19629 row->extra_line_spacing = wrap_row_extra_line_spacing;
19630 min_pos = wrap_row_min_pos;
19631 min_bpos = wrap_row_min_bpos;
19632 max_pos = wrap_row_max_pos;
19633 max_bpos = wrap_row_max_bpos;
19634 row->continued_p = 1;
19635 row->ends_at_zv_p = 0;
19636 row->exact_window_width_line_p = 0;
19637 it->continuation_lines_width += x;
19638
19639 /* Make sure that a non-default face is extended
19640 up to the right margin of the window. */
19641 extend_face_to_end_of_line (it);
19642 }
19643 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19644 {
19645 /* A TAB that extends past the right edge of the
19646 window. This produces a single glyph on
19647 window system frames. We leave the glyph in
19648 this row and let it fill the row, but don't
19649 consume the TAB. */
19650 if ((row->reversed_p
19651 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19652 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19653 produce_special_glyphs (it, IT_CONTINUATION);
19654 it->continuation_lines_width += it->last_visible_x;
19655 row->ends_in_middle_of_char_p = 1;
19656 row->continued_p = 1;
19657 glyph->pixel_width = it->last_visible_x - x;
19658 it->starts_in_middle_of_char_p = 1;
19659 }
19660 else
19661 {
19662 /* Something other than a TAB that draws past
19663 the right edge of the window. Restore
19664 positions to values before the element. */
19665 if (row->reversed_p)
19666 unproduce_glyphs (it, row->used[TEXT_AREA]
19667 - (n_glyphs_before + i));
19668 row->used[TEXT_AREA] = n_glyphs_before + i;
19669
19670 /* Display continuation glyphs. */
19671 it->current_x = x_before;
19672 it->continuation_lines_width += x;
19673 if (!FRAME_WINDOW_P (it->f)
19674 || (row->reversed_p
19675 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19676 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19677 produce_special_glyphs (it, IT_CONTINUATION);
19678 row->continued_p = 1;
19679
19680 extend_face_to_end_of_line (it);
19681
19682 if (nglyphs > 1 && i > 0)
19683 {
19684 row->ends_in_middle_of_char_p = 1;
19685 it->starts_in_middle_of_char_p = 1;
19686 }
19687
19688 /* Restore the height to what it was before the
19689 element not fitting on the line. */
19690 it->max_ascent = ascent;
19691 it->max_descent = descent;
19692 it->max_phys_ascent = phys_ascent;
19693 it->max_phys_descent = phys_descent;
19694 }
19695
19696 break;
19697 }
19698 else if (new_x > it->first_visible_x)
19699 {
19700 /* Increment number of glyphs actually displayed. */
19701 ++it->hpos;
19702
19703 /* Record the maximum and minimum buffer positions
19704 seen so far in glyphs that will be displayed by
19705 this row. */
19706 if (it->bidi_p)
19707 RECORD_MAX_MIN_POS (it);
19708
19709 if (x < it->first_visible_x)
19710 /* Glyph is partially visible, i.e. row starts at
19711 negative X position. */
19712 row->x = x - it->first_visible_x;
19713 }
19714 else
19715 {
19716 /* Glyph is completely off the left margin of the
19717 window. This should not happen because of the
19718 move_it_in_display_line at the start of this
19719 function, unless the text display area of the
19720 window is empty. */
19721 eassert (it->first_visible_x <= it->last_visible_x);
19722 }
19723 }
19724 /* Even if this display element produced no glyphs at all,
19725 we want to record its position. */
19726 if (it->bidi_p && nglyphs == 0)
19727 RECORD_MAX_MIN_POS (it);
19728
19729 row->ascent = max (row->ascent, it->max_ascent);
19730 row->height = max (row->height, it->max_ascent + it->max_descent);
19731 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19732 row->phys_height = max (row->phys_height,
19733 it->max_phys_ascent + it->max_phys_descent);
19734 row->extra_line_spacing = max (row->extra_line_spacing,
19735 it->max_extra_line_spacing);
19736
19737 /* End of this display line if row is continued. */
19738 if (row->continued_p || row->ends_at_zv_p)
19739 break;
19740 }
19741
19742 at_end_of_line:
19743 /* Is this a line end? If yes, we're also done, after making
19744 sure that a non-default face is extended up to the right
19745 margin of the window. */
19746 if (ITERATOR_AT_END_OF_LINE_P (it))
19747 {
19748 int used_before = row->used[TEXT_AREA];
19749
19750 row->ends_in_newline_from_string_p = STRINGP (it->object);
19751
19752 /* Add a space at the end of the line that is used to
19753 display the cursor there. */
19754 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19755 append_space_for_newline (it, 0);
19756
19757 /* Extend the face to the end of the line. */
19758 extend_face_to_end_of_line (it);
19759
19760 /* Make sure we have the position. */
19761 if (used_before == 0)
19762 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19763
19764 /* Record the position of the newline, for use in
19765 find_row_edges. */
19766 it->eol_pos = it->current.pos;
19767
19768 /* Consume the line end. This skips over invisible lines. */
19769 set_iterator_to_next (it, 1);
19770 it->continuation_lines_width = 0;
19771 break;
19772 }
19773
19774 /* Proceed with next display element. Note that this skips
19775 over lines invisible because of selective display. */
19776 set_iterator_to_next (it, 1);
19777
19778 /* If we truncate lines, we are done when the last displayed
19779 glyphs reach past the right margin of the window. */
19780 if (it->line_wrap == TRUNCATE
19781 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19782 ? (it->current_x >= it->last_visible_x)
19783 : (it->current_x > it->last_visible_x)))
19784 {
19785 /* Maybe add truncation glyphs. */
19786 if (!FRAME_WINDOW_P (it->f)
19787 || (row->reversed_p
19788 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19789 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19790 {
19791 int i, n;
19792
19793 if (!row->reversed_p)
19794 {
19795 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19796 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19797 break;
19798 }
19799 else
19800 {
19801 for (i = 0; i < row->used[TEXT_AREA]; i++)
19802 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19803 break;
19804 /* Remove any padding glyphs at the front of ROW, to
19805 make room for the truncation glyphs we will be
19806 adding below. The loop below always inserts at
19807 least one truncation glyph, so also remove the
19808 last glyph added to ROW. */
19809 unproduce_glyphs (it, i + 1);
19810 /* Adjust i for the loop below. */
19811 i = row->used[TEXT_AREA] - (i + 1);
19812 }
19813
19814 it->current_x = x_before;
19815 if (!FRAME_WINDOW_P (it->f))
19816 {
19817 for (n = row->used[TEXT_AREA]; i < n; ++i)
19818 {
19819 row->used[TEXT_AREA] = i;
19820 produce_special_glyphs (it, IT_TRUNCATION);
19821 }
19822 }
19823 else
19824 {
19825 row->used[TEXT_AREA] = i;
19826 produce_special_glyphs (it, IT_TRUNCATION);
19827 }
19828 }
19829 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19830 {
19831 /* Don't truncate if we can overflow newline into fringe. */
19832 if (!get_next_display_element (it))
19833 {
19834 it->continuation_lines_width = 0;
19835 row->ends_at_zv_p = 1;
19836 row->exact_window_width_line_p = 1;
19837 break;
19838 }
19839 if (ITERATOR_AT_END_OF_LINE_P (it))
19840 {
19841 row->exact_window_width_line_p = 1;
19842 goto at_end_of_line;
19843 }
19844 it->current_x = x_before;
19845 }
19846
19847 row->truncated_on_right_p = 1;
19848 it->continuation_lines_width = 0;
19849 reseat_at_next_visible_line_start (it, 0);
19850 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19851 it->hpos = hpos_before;
19852 break;
19853 }
19854 }
19855
19856 if (wrap_data)
19857 bidi_unshelve_cache (wrap_data, 1);
19858
19859 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19860 at the left window margin. */
19861 if (it->first_visible_x
19862 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19863 {
19864 if (!FRAME_WINDOW_P (it->f)
19865 || (row->reversed_p
19866 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19867 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19868 insert_left_trunc_glyphs (it);
19869 row->truncated_on_left_p = 1;
19870 }
19871
19872 /* Remember the position at which this line ends.
19873
19874 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19875 cannot be before the call to find_row_edges below, since that is
19876 where these positions are determined. */
19877 row->end = it->current;
19878 if (!it->bidi_p)
19879 {
19880 row->minpos = row->start.pos;
19881 row->maxpos = row->end.pos;
19882 }
19883 else
19884 {
19885 /* ROW->minpos and ROW->maxpos must be the smallest and
19886 `1 + the largest' buffer positions in ROW. But if ROW was
19887 bidi-reordered, these two positions can be anywhere in the
19888 row, so we must determine them now. */
19889 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19890 }
19891
19892 /* If the start of this line is the overlay arrow-position, then
19893 mark this glyph row as the one containing the overlay arrow.
19894 This is clearly a mess with variable size fonts. It would be
19895 better to let it be displayed like cursors under X. */
19896 if ((row->displays_text_p || !overlay_arrow_seen)
19897 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19898 !NILP (overlay_arrow_string)))
19899 {
19900 /* Overlay arrow in window redisplay is a fringe bitmap. */
19901 if (STRINGP (overlay_arrow_string))
19902 {
19903 struct glyph_row *arrow_row
19904 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19905 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19906 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19907 struct glyph *p = row->glyphs[TEXT_AREA];
19908 struct glyph *p2, *end;
19909
19910 /* Copy the arrow glyphs. */
19911 while (glyph < arrow_end)
19912 *p++ = *glyph++;
19913
19914 /* Throw away padding glyphs. */
19915 p2 = p;
19916 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19917 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19918 ++p2;
19919 if (p2 > p)
19920 {
19921 while (p2 < end)
19922 *p++ = *p2++;
19923 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19924 }
19925 }
19926 else
19927 {
19928 eassert (INTEGERP (overlay_arrow_string));
19929 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19930 }
19931 overlay_arrow_seen = 1;
19932 }
19933
19934 /* Highlight trailing whitespace. */
19935 if (!NILP (Vshow_trailing_whitespace))
19936 highlight_trailing_whitespace (it->f, it->glyph_row);
19937
19938 /* Compute pixel dimensions of this line. */
19939 compute_line_metrics (it);
19940
19941 /* Implementation note: No changes in the glyphs of ROW or in their
19942 faces can be done past this point, because compute_line_metrics
19943 computes ROW's hash value and stores it within the glyph_row
19944 structure. */
19945
19946 /* Record whether this row ends inside an ellipsis. */
19947 row->ends_in_ellipsis_p
19948 = (it->method == GET_FROM_DISPLAY_VECTOR
19949 && it->ellipsis_p);
19950
19951 /* Save fringe bitmaps in this row. */
19952 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19953 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19954 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19955 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19956
19957 it->left_user_fringe_bitmap = 0;
19958 it->left_user_fringe_face_id = 0;
19959 it->right_user_fringe_bitmap = 0;
19960 it->right_user_fringe_face_id = 0;
19961
19962 /* Maybe set the cursor. */
19963 cvpos = it->w->cursor.vpos;
19964 if ((cvpos < 0
19965 /* In bidi-reordered rows, keep checking for proper cursor
19966 position even if one has been found already, because buffer
19967 positions in such rows change non-linearly with ROW->VPOS,
19968 when a line is continued. One exception: when we are at ZV,
19969 display cursor on the first suitable glyph row, since all
19970 the empty rows after that also have their position set to ZV. */
19971 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19972 lines' rows is implemented for bidi-reordered rows. */
19973 || (it->bidi_p
19974 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19975 && PT >= MATRIX_ROW_START_CHARPOS (row)
19976 && PT <= MATRIX_ROW_END_CHARPOS (row)
19977 && cursor_row_p (row))
19978 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19979
19980 /* Prepare for the next line. This line starts horizontally at (X
19981 HPOS) = (0 0). Vertical positions are incremented. As a
19982 convenience for the caller, IT->glyph_row is set to the next
19983 row to be used. */
19984 it->current_x = it->hpos = 0;
19985 it->current_y += row->height;
19986 SET_TEXT_POS (it->eol_pos, 0, 0);
19987 ++it->vpos;
19988 ++it->glyph_row;
19989 /* The next row should by default use the same value of the
19990 reversed_p flag as this one. set_iterator_to_next decides when
19991 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19992 the flag accordingly. */
19993 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19994 it->glyph_row->reversed_p = row->reversed_p;
19995 it->start = row->end;
19996 return row->displays_text_p;
19997
19998 #undef RECORD_MAX_MIN_POS
19999 }
20000
20001 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20002 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20003 doc: /* Return paragraph direction at point in BUFFER.
20004 Value is either `left-to-right' or `right-to-left'.
20005 If BUFFER is omitted or nil, it defaults to the current buffer.
20006
20007 Paragraph direction determines how the text in the paragraph is displayed.
20008 In left-to-right paragraphs, text begins at the left margin of the window
20009 and the reading direction is generally left to right. In right-to-left
20010 paragraphs, text begins at the right margin and is read from right to left.
20011
20012 See also `bidi-paragraph-direction'. */)
20013 (Lisp_Object buffer)
20014 {
20015 struct buffer *buf = current_buffer;
20016 struct buffer *old = buf;
20017
20018 if (! NILP (buffer))
20019 {
20020 CHECK_BUFFER (buffer);
20021 buf = XBUFFER (buffer);
20022 }
20023
20024 if (NILP (BVAR (buf, bidi_display_reordering))
20025 || NILP (BVAR (buf, enable_multibyte_characters))
20026 /* When we are loading loadup.el, the character property tables
20027 needed for bidi iteration are not yet available. */
20028 || !NILP (Vpurify_flag))
20029 return Qleft_to_right;
20030 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20031 return BVAR (buf, bidi_paragraph_direction);
20032 else
20033 {
20034 /* Determine the direction from buffer text. We could try to
20035 use current_matrix if it is up to date, but this seems fast
20036 enough as it is. */
20037 struct bidi_it itb;
20038 ptrdiff_t pos = BUF_PT (buf);
20039 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20040 int c;
20041 void *itb_data = bidi_shelve_cache ();
20042
20043 set_buffer_temp (buf);
20044 /* bidi_paragraph_init finds the base direction of the paragraph
20045 by searching forward from paragraph start. We need the base
20046 direction of the current or _previous_ paragraph, so we need
20047 to make sure we are within that paragraph. To that end, find
20048 the previous non-empty line. */
20049 if (pos >= ZV && pos > BEGV)
20050 {
20051 pos--;
20052 bytepos = CHAR_TO_BYTE (pos);
20053 }
20054 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20055 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20056 {
20057 while ((c = FETCH_BYTE (bytepos)) == '\n'
20058 || c == ' ' || c == '\t' || c == '\f')
20059 {
20060 if (bytepos <= BEGV_BYTE)
20061 break;
20062 bytepos--;
20063 pos--;
20064 }
20065 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20066 bytepos--;
20067 }
20068 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20069 itb.paragraph_dir = NEUTRAL_DIR;
20070 itb.string.s = NULL;
20071 itb.string.lstring = Qnil;
20072 itb.string.bufpos = 0;
20073 itb.string.unibyte = 0;
20074 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20075 bidi_unshelve_cache (itb_data, 0);
20076 set_buffer_temp (old);
20077 switch (itb.paragraph_dir)
20078 {
20079 case L2R:
20080 return Qleft_to_right;
20081 break;
20082 case R2L:
20083 return Qright_to_left;
20084 break;
20085 default:
20086 emacs_abort ();
20087 }
20088 }
20089 }
20090
20091
20092 \f
20093 /***********************************************************************
20094 Menu Bar
20095 ***********************************************************************/
20096
20097 /* Redisplay the menu bar in the frame for window W.
20098
20099 The menu bar of X frames that don't have X toolkit support is
20100 displayed in a special window W->frame->menu_bar_window.
20101
20102 The menu bar of terminal frames is treated specially as far as
20103 glyph matrices are concerned. Menu bar lines are not part of
20104 windows, so the update is done directly on the frame matrix rows
20105 for the menu bar. */
20106
20107 static void
20108 display_menu_bar (struct window *w)
20109 {
20110 struct frame *f = XFRAME (WINDOW_FRAME (w));
20111 struct it it;
20112 Lisp_Object items;
20113 int i;
20114
20115 /* Don't do all this for graphical frames. */
20116 #ifdef HAVE_NTGUI
20117 if (FRAME_W32_P (f))
20118 return;
20119 #endif
20120 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20121 if (FRAME_X_P (f))
20122 return;
20123 #endif
20124
20125 #ifdef HAVE_NS
20126 if (FRAME_NS_P (f))
20127 return;
20128 #endif /* HAVE_NS */
20129
20130 #ifdef USE_X_TOOLKIT
20131 eassert (!FRAME_WINDOW_P (f));
20132 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20133 it.first_visible_x = 0;
20134 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20135 #else /* not USE_X_TOOLKIT */
20136 if (FRAME_WINDOW_P (f))
20137 {
20138 /* Menu bar lines are displayed in the desired matrix of the
20139 dummy window menu_bar_window. */
20140 struct window *menu_w;
20141 eassert (WINDOWP (f->menu_bar_window));
20142 menu_w = XWINDOW (f->menu_bar_window);
20143 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20144 MENU_FACE_ID);
20145 it.first_visible_x = 0;
20146 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20147 }
20148 else
20149 {
20150 /* This is a TTY frame, i.e. character hpos/vpos are used as
20151 pixel x/y. */
20152 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20153 MENU_FACE_ID);
20154 it.first_visible_x = 0;
20155 it.last_visible_x = FRAME_COLS (f);
20156 }
20157 #endif /* not USE_X_TOOLKIT */
20158
20159 /* FIXME: This should be controlled by a user option. See the
20160 comments in redisplay_tool_bar and display_mode_line about
20161 this. */
20162 it.paragraph_embedding = L2R;
20163
20164 if (! mode_line_inverse_video)
20165 /* Force the menu-bar to be displayed in the default face. */
20166 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20167
20168 /* Clear all rows of the menu bar. */
20169 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20170 {
20171 struct glyph_row *row = it.glyph_row + i;
20172 clear_glyph_row (row);
20173 row->enabled_p = 1;
20174 row->full_width_p = 1;
20175 }
20176
20177 /* Display all items of the menu bar. */
20178 items = FRAME_MENU_BAR_ITEMS (it.f);
20179 for (i = 0; i < ASIZE (items); i += 4)
20180 {
20181 Lisp_Object string;
20182
20183 /* Stop at nil string. */
20184 string = AREF (items, i + 1);
20185 if (NILP (string))
20186 break;
20187
20188 /* Remember where item was displayed. */
20189 ASET (items, i + 3, make_number (it.hpos));
20190
20191 /* Display the item, pad with one space. */
20192 if (it.current_x < it.last_visible_x)
20193 display_string (NULL, string, Qnil, 0, 0, &it,
20194 SCHARS (string) + 1, 0, 0, -1);
20195 }
20196
20197 /* Fill out the line with spaces. */
20198 if (it.current_x < it.last_visible_x)
20199 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20200
20201 /* Compute the total height of the lines. */
20202 compute_line_metrics (&it);
20203 }
20204
20205
20206 \f
20207 /***********************************************************************
20208 Mode Line
20209 ***********************************************************************/
20210
20211 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20212 FORCE is non-zero, redisplay mode lines unconditionally.
20213 Otherwise, redisplay only mode lines that are garbaged. Value is
20214 the number of windows whose mode lines were redisplayed. */
20215
20216 static int
20217 redisplay_mode_lines (Lisp_Object window, int force)
20218 {
20219 int nwindows = 0;
20220
20221 while (!NILP (window))
20222 {
20223 struct window *w = XWINDOW (window);
20224
20225 if (WINDOWP (w->hchild))
20226 nwindows += redisplay_mode_lines (w->hchild, force);
20227 else if (WINDOWP (w->vchild))
20228 nwindows += redisplay_mode_lines (w->vchild, force);
20229 else if (force
20230 || FRAME_GARBAGED_P (XFRAME (w->frame))
20231 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20232 {
20233 struct text_pos lpoint;
20234 struct buffer *old = current_buffer;
20235
20236 /* Set the window's buffer for the mode line display. */
20237 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20238 set_buffer_internal_1 (XBUFFER (w->buffer));
20239
20240 /* Point refers normally to the selected window. For any
20241 other window, set up appropriate value. */
20242 if (!EQ (window, selected_window))
20243 {
20244 struct text_pos pt;
20245
20246 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20247 if (CHARPOS (pt) < BEGV)
20248 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20249 else if (CHARPOS (pt) > (ZV - 1))
20250 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20251 else
20252 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20253 }
20254
20255 /* Display mode lines. */
20256 clear_glyph_matrix (w->desired_matrix);
20257 if (display_mode_lines (w))
20258 {
20259 ++nwindows;
20260 w->must_be_updated_p = 1;
20261 }
20262
20263 /* Restore old settings. */
20264 set_buffer_internal_1 (old);
20265 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20266 }
20267
20268 window = w->next;
20269 }
20270
20271 return nwindows;
20272 }
20273
20274
20275 /* Display the mode and/or header line of window W. Value is the
20276 sum number of mode lines and header lines displayed. */
20277
20278 static int
20279 display_mode_lines (struct window *w)
20280 {
20281 Lisp_Object old_selected_window, old_selected_frame;
20282 int n = 0;
20283
20284 old_selected_frame = selected_frame;
20285 selected_frame = w->frame;
20286 old_selected_window = selected_window;
20287 XSETWINDOW (selected_window, w);
20288
20289 /* These will be set while the mode line specs are processed. */
20290 line_number_displayed = 0;
20291 wset_column_number_displayed (w, Qnil);
20292
20293 if (WINDOW_WANTS_MODELINE_P (w))
20294 {
20295 struct window *sel_w = XWINDOW (old_selected_window);
20296
20297 /* Select mode line face based on the real selected window. */
20298 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20299 BVAR (current_buffer, mode_line_format));
20300 ++n;
20301 }
20302
20303 if (WINDOW_WANTS_HEADER_LINE_P (w))
20304 {
20305 display_mode_line (w, HEADER_LINE_FACE_ID,
20306 BVAR (current_buffer, header_line_format));
20307 ++n;
20308 }
20309
20310 selected_frame = old_selected_frame;
20311 selected_window = old_selected_window;
20312 return n;
20313 }
20314
20315
20316 /* Display mode or header line of window W. FACE_ID specifies which
20317 line to display; it is either MODE_LINE_FACE_ID or
20318 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20319 display. Value is the pixel height of the mode/header line
20320 displayed. */
20321
20322 static int
20323 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20324 {
20325 struct it it;
20326 struct face *face;
20327 ptrdiff_t count = SPECPDL_INDEX ();
20328
20329 init_iterator (&it, w, -1, -1, NULL, face_id);
20330 /* Don't extend on a previously drawn mode-line.
20331 This may happen if called from pos_visible_p. */
20332 it.glyph_row->enabled_p = 0;
20333 prepare_desired_row (it.glyph_row);
20334
20335 it.glyph_row->mode_line_p = 1;
20336
20337 if (! mode_line_inverse_video)
20338 /* Force the mode-line to be displayed in the default face. */
20339 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20340
20341 /* FIXME: This should be controlled by a user option. But
20342 supporting such an option is not trivial, since the mode line is
20343 made up of many separate strings. */
20344 it.paragraph_embedding = L2R;
20345
20346 record_unwind_protect (unwind_format_mode_line,
20347 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20348
20349 mode_line_target = MODE_LINE_DISPLAY;
20350
20351 /* Temporarily make frame's keyboard the current kboard so that
20352 kboard-local variables in the mode_line_format will get the right
20353 values. */
20354 push_kboard (FRAME_KBOARD (it.f));
20355 record_unwind_save_match_data ();
20356 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20357 pop_kboard ();
20358
20359 unbind_to (count, Qnil);
20360
20361 /* Fill up with spaces. */
20362 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20363
20364 compute_line_metrics (&it);
20365 it.glyph_row->full_width_p = 1;
20366 it.glyph_row->continued_p = 0;
20367 it.glyph_row->truncated_on_left_p = 0;
20368 it.glyph_row->truncated_on_right_p = 0;
20369
20370 /* Make a 3D mode-line have a shadow at its right end. */
20371 face = FACE_FROM_ID (it.f, face_id);
20372 extend_face_to_end_of_line (&it);
20373 if (face->box != FACE_NO_BOX)
20374 {
20375 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20376 + it.glyph_row->used[TEXT_AREA] - 1);
20377 last->right_box_line_p = 1;
20378 }
20379
20380 return it.glyph_row->height;
20381 }
20382
20383 /* Move element ELT in LIST to the front of LIST.
20384 Return the updated list. */
20385
20386 static Lisp_Object
20387 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20388 {
20389 register Lisp_Object tail, prev;
20390 register Lisp_Object tem;
20391
20392 tail = list;
20393 prev = Qnil;
20394 while (CONSP (tail))
20395 {
20396 tem = XCAR (tail);
20397
20398 if (EQ (elt, tem))
20399 {
20400 /* Splice out the link TAIL. */
20401 if (NILP (prev))
20402 list = XCDR (tail);
20403 else
20404 Fsetcdr (prev, XCDR (tail));
20405
20406 /* Now make it the first. */
20407 Fsetcdr (tail, list);
20408 return tail;
20409 }
20410 else
20411 prev = tail;
20412 tail = XCDR (tail);
20413 QUIT;
20414 }
20415
20416 /* Not found--return unchanged LIST. */
20417 return list;
20418 }
20419
20420 /* Contribute ELT to the mode line for window IT->w. How it
20421 translates into text depends on its data type.
20422
20423 IT describes the display environment in which we display, as usual.
20424
20425 DEPTH is the depth in recursion. It is used to prevent
20426 infinite recursion here.
20427
20428 FIELD_WIDTH is the number of characters the display of ELT should
20429 occupy in the mode line, and PRECISION is the maximum number of
20430 characters to display from ELT's representation. See
20431 display_string for details.
20432
20433 Returns the hpos of the end of the text generated by ELT.
20434
20435 PROPS is a property list to add to any string we encounter.
20436
20437 If RISKY is nonzero, remove (disregard) any properties in any string
20438 we encounter, and ignore :eval and :propertize.
20439
20440 The global variable `mode_line_target' determines whether the
20441 output is passed to `store_mode_line_noprop',
20442 `store_mode_line_string', or `display_string'. */
20443
20444 static int
20445 display_mode_element (struct it *it, int depth, int field_width, int precision,
20446 Lisp_Object elt, Lisp_Object props, int risky)
20447 {
20448 int n = 0, field, prec;
20449 int literal = 0;
20450
20451 tail_recurse:
20452 if (depth > 100)
20453 elt = build_string ("*too-deep*");
20454
20455 depth++;
20456
20457 switch (XTYPE (elt))
20458 {
20459 case Lisp_String:
20460 {
20461 /* A string: output it and check for %-constructs within it. */
20462 unsigned char c;
20463 ptrdiff_t offset = 0;
20464
20465 if (SCHARS (elt) > 0
20466 && (!NILP (props) || risky))
20467 {
20468 Lisp_Object oprops, aelt;
20469 oprops = Ftext_properties_at (make_number (0), elt);
20470
20471 /* If the starting string's properties are not what
20472 we want, translate the string. Also, if the string
20473 is risky, do that anyway. */
20474
20475 if (NILP (Fequal (props, oprops)) || risky)
20476 {
20477 /* If the starting string has properties,
20478 merge the specified ones onto the existing ones. */
20479 if (! NILP (oprops) && !risky)
20480 {
20481 Lisp_Object tem;
20482
20483 oprops = Fcopy_sequence (oprops);
20484 tem = props;
20485 while (CONSP (tem))
20486 {
20487 oprops = Fplist_put (oprops, XCAR (tem),
20488 XCAR (XCDR (tem)));
20489 tem = XCDR (XCDR (tem));
20490 }
20491 props = oprops;
20492 }
20493
20494 aelt = Fassoc (elt, mode_line_proptrans_alist);
20495 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20496 {
20497 /* AELT is what we want. Move it to the front
20498 without consing. */
20499 elt = XCAR (aelt);
20500 mode_line_proptrans_alist
20501 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20502 }
20503 else
20504 {
20505 Lisp_Object tem;
20506
20507 /* If AELT has the wrong props, it is useless.
20508 so get rid of it. */
20509 if (! NILP (aelt))
20510 mode_line_proptrans_alist
20511 = Fdelq (aelt, mode_line_proptrans_alist);
20512
20513 elt = Fcopy_sequence (elt);
20514 Fset_text_properties (make_number (0), Flength (elt),
20515 props, elt);
20516 /* Add this item to mode_line_proptrans_alist. */
20517 mode_line_proptrans_alist
20518 = Fcons (Fcons (elt, props),
20519 mode_line_proptrans_alist);
20520 /* Truncate mode_line_proptrans_alist
20521 to at most 50 elements. */
20522 tem = Fnthcdr (make_number (50),
20523 mode_line_proptrans_alist);
20524 if (! NILP (tem))
20525 XSETCDR (tem, Qnil);
20526 }
20527 }
20528 }
20529
20530 offset = 0;
20531
20532 if (literal)
20533 {
20534 prec = precision - n;
20535 switch (mode_line_target)
20536 {
20537 case MODE_LINE_NOPROP:
20538 case MODE_LINE_TITLE:
20539 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20540 break;
20541 case MODE_LINE_STRING:
20542 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20543 break;
20544 case MODE_LINE_DISPLAY:
20545 n += display_string (NULL, elt, Qnil, 0, 0, it,
20546 0, prec, 0, STRING_MULTIBYTE (elt));
20547 break;
20548 }
20549
20550 break;
20551 }
20552
20553 /* Handle the non-literal case. */
20554
20555 while ((precision <= 0 || n < precision)
20556 && SREF (elt, offset) != 0
20557 && (mode_line_target != MODE_LINE_DISPLAY
20558 || it->current_x < it->last_visible_x))
20559 {
20560 ptrdiff_t last_offset = offset;
20561
20562 /* Advance to end of string or next format specifier. */
20563 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20564 ;
20565
20566 if (offset - 1 != last_offset)
20567 {
20568 ptrdiff_t nchars, nbytes;
20569
20570 /* Output to end of string or up to '%'. Field width
20571 is length of string. Don't output more than
20572 PRECISION allows us. */
20573 offset--;
20574
20575 prec = c_string_width (SDATA (elt) + last_offset,
20576 offset - last_offset, precision - n,
20577 &nchars, &nbytes);
20578
20579 switch (mode_line_target)
20580 {
20581 case MODE_LINE_NOPROP:
20582 case MODE_LINE_TITLE:
20583 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20584 break;
20585 case MODE_LINE_STRING:
20586 {
20587 ptrdiff_t bytepos = last_offset;
20588 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20589 ptrdiff_t endpos = (precision <= 0
20590 ? string_byte_to_char (elt, offset)
20591 : charpos + nchars);
20592
20593 n += store_mode_line_string (NULL,
20594 Fsubstring (elt, make_number (charpos),
20595 make_number (endpos)),
20596 0, 0, 0, Qnil);
20597 }
20598 break;
20599 case MODE_LINE_DISPLAY:
20600 {
20601 ptrdiff_t bytepos = last_offset;
20602 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20603
20604 if (precision <= 0)
20605 nchars = string_byte_to_char (elt, offset) - charpos;
20606 n += display_string (NULL, elt, Qnil, 0, charpos,
20607 it, 0, nchars, 0,
20608 STRING_MULTIBYTE (elt));
20609 }
20610 break;
20611 }
20612 }
20613 else /* c == '%' */
20614 {
20615 ptrdiff_t percent_position = offset;
20616
20617 /* Get the specified minimum width. Zero means
20618 don't pad. */
20619 field = 0;
20620 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20621 field = field * 10 + c - '0';
20622
20623 /* Don't pad beyond the total padding allowed. */
20624 if (field_width - n > 0 && field > field_width - n)
20625 field = field_width - n;
20626
20627 /* Note that either PRECISION <= 0 or N < PRECISION. */
20628 prec = precision - n;
20629
20630 if (c == 'M')
20631 n += display_mode_element (it, depth, field, prec,
20632 Vglobal_mode_string, props,
20633 risky);
20634 else if (c != 0)
20635 {
20636 int multibyte;
20637 ptrdiff_t bytepos, charpos;
20638 const char *spec;
20639 Lisp_Object string;
20640
20641 bytepos = percent_position;
20642 charpos = (STRING_MULTIBYTE (elt)
20643 ? string_byte_to_char (elt, bytepos)
20644 : bytepos);
20645 spec = decode_mode_spec (it->w, c, field, &string);
20646 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20647
20648 switch (mode_line_target)
20649 {
20650 case MODE_LINE_NOPROP:
20651 case MODE_LINE_TITLE:
20652 n += store_mode_line_noprop (spec, field, prec);
20653 break;
20654 case MODE_LINE_STRING:
20655 {
20656 Lisp_Object tem = build_string (spec);
20657 props = Ftext_properties_at (make_number (charpos), elt);
20658 /* Should only keep face property in props */
20659 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20660 }
20661 break;
20662 case MODE_LINE_DISPLAY:
20663 {
20664 int nglyphs_before, nwritten;
20665
20666 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20667 nwritten = display_string (spec, string, elt,
20668 charpos, 0, it,
20669 field, prec, 0,
20670 multibyte);
20671
20672 /* Assign to the glyphs written above the
20673 string where the `%x' came from, position
20674 of the `%'. */
20675 if (nwritten > 0)
20676 {
20677 struct glyph *glyph
20678 = (it->glyph_row->glyphs[TEXT_AREA]
20679 + nglyphs_before);
20680 int i;
20681
20682 for (i = 0; i < nwritten; ++i)
20683 {
20684 glyph[i].object = elt;
20685 glyph[i].charpos = charpos;
20686 }
20687
20688 n += nwritten;
20689 }
20690 }
20691 break;
20692 }
20693 }
20694 else /* c == 0 */
20695 break;
20696 }
20697 }
20698 }
20699 break;
20700
20701 case Lisp_Symbol:
20702 /* A symbol: process the value of the symbol recursively
20703 as if it appeared here directly. Avoid error if symbol void.
20704 Special case: if value of symbol is a string, output the string
20705 literally. */
20706 {
20707 register Lisp_Object tem;
20708
20709 /* If the variable is not marked as risky to set
20710 then its contents are risky to use. */
20711 if (NILP (Fget (elt, Qrisky_local_variable)))
20712 risky = 1;
20713
20714 tem = Fboundp (elt);
20715 if (!NILP (tem))
20716 {
20717 tem = Fsymbol_value (elt);
20718 /* If value is a string, output that string literally:
20719 don't check for % within it. */
20720 if (STRINGP (tem))
20721 literal = 1;
20722
20723 if (!EQ (tem, elt))
20724 {
20725 /* Give up right away for nil or t. */
20726 elt = tem;
20727 goto tail_recurse;
20728 }
20729 }
20730 }
20731 break;
20732
20733 case Lisp_Cons:
20734 {
20735 register Lisp_Object car, tem;
20736
20737 /* A cons cell: five distinct cases.
20738 If first element is :eval or :propertize, do something special.
20739 If first element is a string or a cons, process all the elements
20740 and effectively concatenate them.
20741 If first element is a negative number, truncate displaying cdr to
20742 at most that many characters. If positive, pad (with spaces)
20743 to at least that many characters.
20744 If first element is a symbol, process the cadr or caddr recursively
20745 according to whether the symbol's value is non-nil or nil. */
20746 car = XCAR (elt);
20747 if (EQ (car, QCeval))
20748 {
20749 /* An element of the form (:eval FORM) means evaluate FORM
20750 and use the result as mode line elements. */
20751
20752 if (risky)
20753 break;
20754
20755 if (CONSP (XCDR (elt)))
20756 {
20757 Lisp_Object spec;
20758 spec = safe_eval (XCAR (XCDR (elt)));
20759 n += display_mode_element (it, depth, field_width - n,
20760 precision - n, spec, props,
20761 risky);
20762 }
20763 }
20764 else if (EQ (car, QCpropertize))
20765 {
20766 /* An element of the form (:propertize ELT PROPS...)
20767 means display ELT but applying properties PROPS. */
20768
20769 if (risky)
20770 break;
20771
20772 if (CONSP (XCDR (elt)))
20773 n += display_mode_element (it, depth, field_width - n,
20774 precision - n, XCAR (XCDR (elt)),
20775 XCDR (XCDR (elt)), risky);
20776 }
20777 else if (SYMBOLP (car))
20778 {
20779 tem = Fboundp (car);
20780 elt = XCDR (elt);
20781 if (!CONSP (elt))
20782 goto invalid;
20783 /* elt is now the cdr, and we know it is a cons cell.
20784 Use its car if CAR has a non-nil value. */
20785 if (!NILP (tem))
20786 {
20787 tem = Fsymbol_value (car);
20788 if (!NILP (tem))
20789 {
20790 elt = XCAR (elt);
20791 goto tail_recurse;
20792 }
20793 }
20794 /* Symbol's value is nil (or symbol is unbound)
20795 Get the cddr of the original list
20796 and if possible find the caddr and use that. */
20797 elt = XCDR (elt);
20798 if (NILP (elt))
20799 break;
20800 else if (!CONSP (elt))
20801 goto invalid;
20802 elt = XCAR (elt);
20803 goto tail_recurse;
20804 }
20805 else if (INTEGERP (car))
20806 {
20807 register int lim = XINT (car);
20808 elt = XCDR (elt);
20809 if (lim < 0)
20810 {
20811 /* Negative int means reduce maximum width. */
20812 if (precision <= 0)
20813 precision = -lim;
20814 else
20815 precision = min (precision, -lim);
20816 }
20817 else if (lim > 0)
20818 {
20819 /* Padding specified. Don't let it be more than
20820 current maximum. */
20821 if (precision > 0)
20822 lim = min (precision, lim);
20823
20824 /* If that's more padding than already wanted, queue it.
20825 But don't reduce padding already specified even if
20826 that is beyond the current truncation point. */
20827 field_width = max (lim, field_width);
20828 }
20829 goto tail_recurse;
20830 }
20831 else if (STRINGP (car) || CONSP (car))
20832 {
20833 Lisp_Object halftail = elt;
20834 int len = 0;
20835
20836 while (CONSP (elt)
20837 && (precision <= 0 || n < precision))
20838 {
20839 n += display_mode_element (it, depth,
20840 /* Do padding only after the last
20841 element in the list. */
20842 (! CONSP (XCDR (elt))
20843 ? field_width - n
20844 : 0),
20845 precision - n, XCAR (elt),
20846 props, risky);
20847 elt = XCDR (elt);
20848 len++;
20849 if ((len & 1) == 0)
20850 halftail = XCDR (halftail);
20851 /* Check for cycle. */
20852 if (EQ (halftail, elt))
20853 break;
20854 }
20855 }
20856 }
20857 break;
20858
20859 default:
20860 invalid:
20861 elt = build_string ("*invalid*");
20862 goto tail_recurse;
20863 }
20864
20865 /* Pad to FIELD_WIDTH. */
20866 if (field_width > 0 && n < field_width)
20867 {
20868 switch (mode_line_target)
20869 {
20870 case MODE_LINE_NOPROP:
20871 case MODE_LINE_TITLE:
20872 n += store_mode_line_noprop ("", field_width - n, 0);
20873 break;
20874 case MODE_LINE_STRING:
20875 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20876 break;
20877 case MODE_LINE_DISPLAY:
20878 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20879 0, 0, 0);
20880 break;
20881 }
20882 }
20883
20884 return n;
20885 }
20886
20887 /* Store a mode-line string element in mode_line_string_list.
20888
20889 If STRING is non-null, display that C string. Otherwise, the Lisp
20890 string LISP_STRING is displayed.
20891
20892 FIELD_WIDTH is the minimum number of output glyphs to produce.
20893 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20894 with spaces. FIELD_WIDTH <= 0 means don't pad.
20895
20896 PRECISION is the maximum number of characters to output from
20897 STRING. PRECISION <= 0 means don't truncate the string.
20898
20899 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20900 properties to the string.
20901
20902 PROPS are the properties to add to the string.
20903 The mode_line_string_face face property is always added to the string.
20904 */
20905
20906 static int
20907 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20908 int field_width, int precision, Lisp_Object props)
20909 {
20910 ptrdiff_t len;
20911 int n = 0;
20912
20913 if (string != NULL)
20914 {
20915 len = strlen (string);
20916 if (precision > 0 && len > precision)
20917 len = precision;
20918 lisp_string = make_string (string, len);
20919 if (NILP (props))
20920 props = mode_line_string_face_prop;
20921 else if (!NILP (mode_line_string_face))
20922 {
20923 Lisp_Object face = Fplist_get (props, Qface);
20924 props = Fcopy_sequence (props);
20925 if (NILP (face))
20926 face = mode_line_string_face;
20927 else
20928 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20929 props = Fplist_put (props, Qface, face);
20930 }
20931 Fadd_text_properties (make_number (0), make_number (len),
20932 props, lisp_string);
20933 }
20934 else
20935 {
20936 len = XFASTINT (Flength (lisp_string));
20937 if (precision > 0 && len > precision)
20938 {
20939 len = precision;
20940 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20941 precision = -1;
20942 }
20943 if (!NILP (mode_line_string_face))
20944 {
20945 Lisp_Object face;
20946 if (NILP (props))
20947 props = Ftext_properties_at (make_number (0), lisp_string);
20948 face = Fplist_get (props, Qface);
20949 if (NILP (face))
20950 face = mode_line_string_face;
20951 else
20952 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20953 props = Fcons (Qface, Fcons (face, Qnil));
20954 if (copy_string)
20955 lisp_string = Fcopy_sequence (lisp_string);
20956 }
20957 if (!NILP (props))
20958 Fadd_text_properties (make_number (0), make_number (len),
20959 props, lisp_string);
20960 }
20961
20962 if (len > 0)
20963 {
20964 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20965 n += len;
20966 }
20967
20968 if (field_width > len)
20969 {
20970 field_width -= len;
20971 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20972 if (!NILP (props))
20973 Fadd_text_properties (make_number (0), make_number (field_width),
20974 props, lisp_string);
20975 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20976 n += field_width;
20977 }
20978
20979 return n;
20980 }
20981
20982
20983 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20984 1, 4, 0,
20985 doc: /* Format a string out of a mode line format specification.
20986 First arg FORMAT specifies the mode line format (see `mode-line-format'
20987 for details) to use.
20988
20989 By default, the format is evaluated for the currently selected window.
20990
20991 Optional second arg FACE specifies the face property to put on all
20992 characters for which no face is specified. The value nil means the
20993 default face. The value t means whatever face the window's mode line
20994 currently uses (either `mode-line' or `mode-line-inactive',
20995 depending on whether the window is the selected window or not).
20996 An integer value means the value string has no text
20997 properties.
20998
20999 Optional third and fourth args WINDOW and BUFFER specify the window
21000 and buffer to use as the context for the formatting (defaults
21001 are the selected window and the WINDOW's buffer). */)
21002 (Lisp_Object format, Lisp_Object face,
21003 Lisp_Object window, Lisp_Object buffer)
21004 {
21005 struct it it;
21006 int len;
21007 struct window *w;
21008 struct buffer *old_buffer = NULL;
21009 int face_id;
21010 int no_props = INTEGERP (face);
21011 ptrdiff_t count = SPECPDL_INDEX ();
21012 Lisp_Object str;
21013 int string_start = 0;
21014
21015 if (NILP (window))
21016 window = selected_window;
21017 CHECK_WINDOW (window);
21018 w = XWINDOW (window);
21019
21020 if (NILP (buffer))
21021 buffer = w->buffer;
21022 CHECK_BUFFER (buffer);
21023
21024 /* Make formatting the modeline a non-op when noninteractive, otherwise
21025 there will be problems later caused by a partially initialized frame. */
21026 if (NILP (format) || noninteractive)
21027 return empty_unibyte_string;
21028
21029 if (no_props)
21030 face = Qnil;
21031
21032 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21033 : EQ (face, Qt) ? (EQ (window, selected_window)
21034 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21035 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21036 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21037 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21038 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21039 : DEFAULT_FACE_ID;
21040
21041 old_buffer = current_buffer;
21042
21043 /* Save things including mode_line_proptrans_alist,
21044 and set that to nil so that we don't alter the outer value. */
21045 record_unwind_protect (unwind_format_mode_line,
21046 format_mode_line_unwind_data
21047 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21048 old_buffer, selected_window, 1));
21049 mode_line_proptrans_alist = Qnil;
21050
21051 Fselect_window (window, Qt);
21052 set_buffer_internal_1 (XBUFFER (buffer));
21053
21054 init_iterator (&it, w, -1, -1, NULL, face_id);
21055
21056 if (no_props)
21057 {
21058 mode_line_target = MODE_LINE_NOPROP;
21059 mode_line_string_face_prop = Qnil;
21060 mode_line_string_list = Qnil;
21061 string_start = MODE_LINE_NOPROP_LEN (0);
21062 }
21063 else
21064 {
21065 mode_line_target = MODE_LINE_STRING;
21066 mode_line_string_list = Qnil;
21067 mode_line_string_face = face;
21068 mode_line_string_face_prop
21069 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21070 }
21071
21072 push_kboard (FRAME_KBOARD (it.f));
21073 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21074 pop_kboard ();
21075
21076 if (no_props)
21077 {
21078 len = MODE_LINE_NOPROP_LEN (string_start);
21079 str = make_string (mode_line_noprop_buf + string_start, len);
21080 }
21081 else
21082 {
21083 mode_line_string_list = Fnreverse (mode_line_string_list);
21084 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21085 empty_unibyte_string);
21086 }
21087
21088 unbind_to (count, Qnil);
21089 return str;
21090 }
21091
21092 /* Write a null-terminated, right justified decimal representation of
21093 the positive integer D to BUF using a minimal field width WIDTH. */
21094
21095 static void
21096 pint2str (register char *buf, register int width, register ptrdiff_t d)
21097 {
21098 register char *p = buf;
21099
21100 if (d <= 0)
21101 *p++ = '0';
21102 else
21103 {
21104 while (d > 0)
21105 {
21106 *p++ = d % 10 + '0';
21107 d /= 10;
21108 }
21109 }
21110
21111 for (width -= (int) (p - buf); width > 0; --width)
21112 *p++ = ' ';
21113 *p-- = '\0';
21114 while (p > buf)
21115 {
21116 d = *buf;
21117 *buf++ = *p;
21118 *p-- = d;
21119 }
21120 }
21121
21122 /* Write a null-terminated, right justified decimal and "human
21123 readable" representation of the nonnegative integer D to BUF using
21124 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21125
21126 static const char power_letter[] =
21127 {
21128 0, /* no letter */
21129 'k', /* kilo */
21130 'M', /* mega */
21131 'G', /* giga */
21132 'T', /* tera */
21133 'P', /* peta */
21134 'E', /* exa */
21135 'Z', /* zetta */
21136 'Y' /* yotta */
21137 };
21138
21139 static void
21140 pint2hrstr (char *buf, int width, ptrdiff_t d)
21141 {
21142 /* We aim to represent the nonnegative integer D as
21143 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21144 ptrdiff_t quotient = d;
21145 int remainder = 0;
21146 /* -1 means: do not use TENTHS. */
21147 int tenths = -1;
21148 int exponent = 0;
21149
21150 /* Length of QUOTIENT.TENTHS as a string. */
21151 int length;
21152
21153 char * psuffix;
21154 char * p;
21155
21156 if (1000 <= quotient)
21157 {
21158 /* Scale to the appropriate EXPONENT. */
21159 do
21160 {
21161 remainder = quotient % 1000;
21162 quotient /= 1000;
21163 exponent++;
21164 }
21165 while (1000 <= quotient);
21166
21167 /* Round to nearest and decide whether to use TENTHS or not. */
21168 if (quotient <= 9)
21169 {
21170 tenths = remainder / 100;
21171 if (50 <= remainder % 100)
21172 {
21173 if (tenths < 9)
21174 tenths++;
21175 else
21176 {
21177 quotient++;
21178 if (quotient == 10)
21179 tenths = -1;
21180 else
21181 tenths = 0;
21182 }
21183 }
21184 }
21185 else
21186 if (500 <= remainder)
21187 {
21188 if (quotient < 999)
21189 quotient++;
21190 else
21191 {
21192 quotient = 1;
21193 exponent++;
21194 tenths = 0;
21195 }
21196 }
21197 }
21198
21199 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21200 if (tenths == -1 && quotient <= 99)
21201 if (quotient <= 9)
21202 length = 1;
21203 else
21204 length = 2;
21205 else
21206 length = 3;
21207 p = psuffix = buf + max (width, length);
21208
21209 /* Print EXPONENT. */
21210 *psuffix++ = power_letter[exponent];
21211 *psuffix = '\0';
21212
21213 /* Print TENTHS. */
21214 if (tenths >= 0)
21215 {
21216 *--p = '0' + tenths;
21217 *--p = '.';
21218 }
21219
21220 /* Print QUOTIENT. */
21221 do
21222 {
21223 int digit = quotient % 10;
21224 *--p = '0' + digit;
21225 }
21226 while ((quotient /= 10) != 0);
21227
21228 /* Print leading spaces. */
21229 while (buf < p)
21230 *--p = ' ';
21231 }
21232
21233 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21234 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21235 type of CODING_SYSTEM. Return updated pointer into BUF. */
21236
21237 static unsigned char invalid_eol_type[] = "(*invalid*)";
21238
21239 static char *
21240 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21241 {
21242 Lisp_Object val;
21243 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21244 const unsigned char *eol_str;
21245 int eol_str_len;
21246 /* The EOL conversion we are using. */
21247 Lisp_Object eoltype;
21248
21249 val = CODING_SYSTEM_SPEC (coding_system);
21250 eoltype = Qnil;
21251
21252 if (!VECTORP (val)) /* Not yet decided. */
21253 {
21254 *buf++ = multibyte ? '-' : ' ';
21255 if (eol_flag)
21256 eoltype = eol_mnemonic_undecided;
21257 /* Don't mention EOL conversion if it isn't decided. */
21258 }
21259 else
21260 {
21261 Lisp_Object attrs;
21262 Lisp_Object eolvalue;
21263
21264 attrs = AREF (val, 0);
21265 eolvalue = AREF (val, 2);
21266
21267 *buf++ = multibyte
21268 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21269 : ' ';
21270
21271 if (eol_flag)
21272 {
21273 /* The EOL conversion that is normal on this system. */
21274
21275 if (NILP (eolvalue)) /* Not yet decided. */
21276 eoltype = eol_mnemonic_undecided;
21277 else if (VECTORP (eolvalue)) /* Not yet decided. */
21278 eoltype = eol_mnemonic_undecided;
21279 else /* eolvalue is Qunix, Qdos, or Qmac. */
21280 eoltype = (EQ (eolvalue, Qunix)
21281 ? eol_mnemonic_unix
21282 : (EQ (eolvalue, Qdos) == 1
21283 ? eol_mnemonic_dos : eol_mnemonic_mac));
21284 }
21285 }
21286
21287 if (eol_flag)
21288 {
21289 /* Mention the EOL conversion if it is not the usual one. */
21290 if (STRINGP (eoltype))
21291 {
21292 eol_str = SDATA (eoltype);
21293 eol_str_len = SBYTES (eoltype);
21294 }
21295 else if (CHARACTERP (eoltype))
21296 {
21297 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21298 int c = XFASTINT (eoltype);
21299 eol_str_len = CHAR_STRING (c, tmp);
21300 eol_str = tmp;
21301 }
21302 else
21303 {
21304 eol_str = invalid_eol_type;
21305 eol_str_len = sizeof (invalid_eol_type) - 1;
21306 }
21307 memcpy (buf, eol_str, eol_str_len);
21308 buf += eol_str_len;
21309 }
21310
21311 return buf;
21312 }
21313
21314 /* Return a string for the output of a mode line %-spec for window W,
21315 generated by character C. FIELD_WIDTH > 0 means pad the string
21316 returned with spaces to that value. Return a Lisp string in
21317 *STRING if the resulting string is taken from that Lisp string.
21318
21319 Note we operate on the current buffer for most purposes,
21320 the exception being w->base_line_pos. */
21321
21322 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21323
21324 static const char *
21325 decode_mode_spec (struct window *w, register int c, int field_width,
21326 Lisp_Object *string)
21327 {
21328 Lisp_Object obj;
21329 struct frame *f = XFRAME (WINDOW_FRAME (w));
21330 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21331 struct buffer *b = current_buffer;
21332
21333 obj = Qnil;
21334 *string = Qnil;
21335
21336 switch (c)
21337 {
21338 case '*':
21339 if (!NILP (BVAR (b, read_only)))
21340 return "%";
21341 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21342 return "*";
21343 return "-";
21344
21345 case '+':
21346 /* This differs from %* only for a modified read-only buffer. */
21347 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21348 return "*";
21349 if (!NILP (BVAR (b, read_only)))
21350 return "%";
21351 return "-";
21352
21353 case '&':
21354 /* This differs from %* in ignoring read-only-ness. */
21355 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21356 return "*";
21357 return "-";
21358
21359 case '%':
21360 return "%";
21361
21362 case '[':
21363 {
21364 int i;
21365 char *p;
21366
21367 if (command_loop_level > 5)
21368 return "[[[... ";
21369 p = decode_mode_spec_buf;
21370 for (i = 0; i < command_loop_level; i++)
21371 *p++ = '[';
21372 *p = 0;
21373 return decode_mode_spec_buf;
21374 }
21375
21376 case ']':
21377 {
21378 int i;
21379 char *p;
21380
21381 if (command_loop_level > 5)
21382 return " ...]]]";
21383 p = decode_mode_spec_buf;
21384 for (i = 0; i < command_loop_level; i++)
21385 *p++ = ']';
21386 *p = 0;
21387 return decode_mode_spec_buf;
21388 }
21389
21390 case '-':
21391 {
21392 register int i;
21393
21394 /* Let lots_of_dashes be a string of infinite length. */
21395 if (mode_line_target == MODE_LINE_NOPROP ||
21396 mode_line_target == MODE_LINE_STRING)
21397 return "--";
21398 if (field_width <= 0
21399 || field_width > sizeof (lots_of_dashes))
21400 {
21401 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21402 decode_mode_spec_buf[i] = '-';
21403 decode_mode_spec_buf[i] = '\0';
21404 return decode_mode_spec_buf;
21405 }
21406 else
21407 return lots_of_dashes;
21408 }
21409
21410 case 'b':
21411 obj = BVAR (b, name);
21412 break;
21413
21414 case 'c':
21415 /* %c and %l are ignored in `frame-title-format'.
21416 (In redisplay_internal, the frame title is drawn _before_ the
21417 windows are updated, so the stuff which depends on actual
21418 window contents (such as %l) may fail to render properly, or
21419 even crash emacs.) */
21420 if (mode_line_target == MODE_LINE_TITLE)
21421 return "";
21422 else
21423 {
21424 ptrdiff_t col = current_column ();
21425 wset_column_number_displayed (w, make_number (col));
21426 pint2str (decode_mode_spec_buf, field_width, col);
21427 return decode_mode_spec_buf;
21428 }
21429
21430 case 'e':
21431 #ifndef SYSTEM_MALLOC
21432 {
21433 if (NILP (Vmemory_full))
21434 return "";
21435 else
21436 return "!MEM FULL! ";
21437 }
21438 #else
21439 return "";
21440 #endif
21441
21442 case 'F':
21443 /* %F displays the frame name. */
21444 if (!NILP (f->title))
21445 return SSDATA (f->title);
21446 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21447 return SSDATA (f->name);
21448 return "Emacs";
21449
21450 case 'f':
21451 obj = BVAR (b, filename);
21452 break;
21453
21454 case 'i':
21455 {
21456 ptrdiff_t size = ZV - BEGV;
21457 pint2str (decode_mode_spec_buf, field_width, size);
21458 return decode_mode_spec_buf;
21459 }
21460
21461 case 'I':
21462 {
21463 ptrdiff_t size = ZV - BEGV;
21464 pint2hrstr (decode_mode_spec_buf, field_width, size);
21465 return decode_mode_spec_buf;
21466 }
21467
21468 case 'l':
21469 {
21470 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21471 ptrdiff_t topline, nlines, height;
21472 ptrdiff_t junk;
21473
21474 /* %c and %l are ignored in `frame-title-format'. */
21475 if (mode_line_target == MODE_LINE_TITLE)
21476 return "";
21477
21478 startpos = XMARKER (w->start)->charpos;
21479 startpos_byte = marker_byte_position (w->start);
21480 height = WINDOW_TOTAL_LINES (w);
21481
21482 /* If we decided that this buffer isn't suitable for line numbers,
21483 don't forget that too fast. */
21484 if (EQ (w->base_line_pos, w->buffer))
21485 goto no_value;
21486 /* But do forget it, if the window shows a different buffer now. */
21487 else if (BUFFERP (w->base_line_pos))
21488 wset_base_line_pos (w, Qnil);
21489
21490 /* If the buffer is very big, don't waste time. */
21491 if (INTEGERP (Vline_number_display_limit)
21492 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21493 {
21494 wset_base_line_pos (w, Qnil);
21495 wset_base_line_number (w, Qnil);
21496 goto no_value;
21497 }
21498
21499 if (INTEGERP (w->base_line_number)
21500 && INTEGERP (w->base_line_pos)
21501 && XFASTINT (w->base_line_pos) <= startpos)
21502 {
21503 line = XFASTINT (w->base_line_number);
21504 linepos = XFASTINT (w->base_line_pos);
21505 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21506 }
21507 else
21508 {
21509 line = 1;
21510 linepos = BUF_BEGV (b);
21511 linepos_byte = BUF_BEGV_BYTE (b);
21512 }
21513
21514 /* Count lines from base line to window start position. */
21515 nlines = display_count_lines (linepos_byte,
21516 startpos_byte,
21517 startpos, &junk);
21518
21519 topline = nlines + line;
21520
21521 /* Determine a new base line, if the old one is too close
21522 or too far away, or if we did not have one.
21523 "Too close" means it's plausible a scroll-down would
21524 go back past it. */
21525 if (startpos == BUF_BEGV (b))
21526 {
21527 wset_base_line_number (w, make_number (topline));
21528 wset_base_line_pos (w, make_number (BUF_BEGV (b)));
21529 }
21530 else if (nlines < height + 25 || nlines > height * 3 + 50
21531 || linepos == BUF_BEGV (b))
21532 {
21533 ptrdiff_t limit = BUF_BEGV (b);
21534 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21535 ptrdiff_t position;
21536 ptrdiff_t distance =
21537 (height * 2 + 30) * line_number_display_limit_width;
21538
21539 if (startpos - distance > limit)
21540 {
21541 limit = startpos - distance;
21542 limit_byte = CHAR_TO_BYTE (limit);
21543 }
21544
21545 nlines = display_count_lines (startpos_byte,
21546 limit_byte,
21547 - (height * 2 + 30),
21548 &position);
21549 /* If we couldn't find the lines we wanted within
21550 line_number_display_limit_width chars per line,
21551 give up on line numbers for this window. */
21552 if (position == limit_byte && limit == startpos - distance)
21553 {
21554 wset_base_line_pos (w, w->buffer);
21555 wset_base_line_number (w, Qnil);
21556 goto no_value;
21557 }
21558
21559 wset_base_line_number (w, make_number (topline - nlines));
21560 wset_base_line_pos (w, make_number (BYTE_TO_CHAR (position)));
21561 }
21562
21563 /* Now count lines from the start pos to point. */
21564 nlines = display_count_lines (startpos_byte,
21565 PT_BYTE, PT, &junk);
21566
21567 /* Record that we did display the line number. */
21568 line_number_displayed = 1;
21569
21570 /* Make the string to show. */
21571 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21572 return decode_mode_spec_buf;
21573 no_value:
21574 {
21575 char* p = decode_mode_spec_buf;
21576 int pad = field_width - 2;
21577 while (pad-- > 0)
21578 *p++ = ' ';
21579 *p++ = '?';
21580 *p++ = '?';
21581 *p = '\0';
21582 return decode_mode_spec_buf;
21583 }
21584 }
21585 break;
21586
21587 case 'm':
21588 obj = BVAR (b, mode_name);
21589 break;
21590
21591 case 'n':
21592 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21593 return " Narrow";
21594 break;
21595
21596 case 'p':
21597 {
21598 ptrdiff_t pos = marker_position (w->start);
21599 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21600
21601 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21602 {
21603 if (pos <= BUF_BEGV (b))
21604 return "All";
21605 else
21606 return "Bottom";
21607 }
21608 else if (pos <= BUF_BEGV (b))
21609 return "Top";
21610 else
21611 {
21612 if (total > 1000000)
21613 /* Do it differently for a large value, to avoid overflow. */
21614 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21615 else
21616 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21617 /* We can't normally display a 3-digit number,
21618 so get us a 2-digit number that is close. */
21619 if (total == 100)
21620 total = 99;
21621 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21622 return decode_mode_spec_buf;
21623 }
21624 }
21625
21626 /* Display percentage of size above the bottom of the screen. */
21627 case 'P':
21628 {
21629 ptrdiff_t toppos = marker_position (w->start);
21630 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21631 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21632
21633 if (botpos >= BUF_ZV (b))
21634 {
21635 if (toppos <= BUF_BEGV (b))
21636 return "All";
21637 else
21638 return "Bottom";
21639 }
21640 else
21641 {
21642 if (total > 1000000)
21643 /* Do it differently for a large value, to avoid overflow. */
21644 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21645 else
21646 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21647 /* We can't normally display a 3-digit number,
21648 so get us a 2-digit number that is close. */
21649 if (total == 100)
21650 total = 99;
21651 if (toppos <= BUF_BEGV (b))
21652 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21653 else
21654 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21655 return decode_mode_spec_buf;
21656 }
21657 }
21658
21659 case 's':
21660 /* status of process */
21661 obj = Fget_buffer_process (Fcurrent_buffer ());
21662 if (NILP (obj))
21663 return "no process";
21664 #ifndef MSDOS
21665 obj = Fsymbol_name (Fprocess_status (obj));
21666 #endif
21667 break;
21668
21669 case '@':
21670 {
21671 ptrdiff_t count = inhibit_garbage_collection ();
21672 Lisp_Object val = call1 (intern ("file-remote-p"),
21673 BVAR (current_buffer, directory));
21674 unbind_to (count, Qnil);
21675
21676 if (NILP (val))
21677 return "-";
21678 else
21679 return "@";
21680 }
21681
21682 case 't': /* indicate TEXT or BINARY */
21683 return "T";
21684
21685 case 'z':
21686 /* coding-system (not including end-of-line format) */
21687 case 'Z':
21688 /* coding-system (including end-of-line type) */
21689 {
21690 int eol_flag = (c == 'Z');
21691 char *p = decode_mode_spec_buf;
21692
21693 if (! FRAME_WINDOW_P (f))
21694 {
21695 /* No need to mention EOL here--the terminal never needs
21696 to do EOL conversion. */
21697 p = decode_mode_spec_coding (CODING_ID_NAME
21698 (FRAME_KEYBOARD_CODING (f)->id),
21699 p, 0);
21700 p = decode_mode_spec_coding (CODING_ID_NAME
21701 (FRAME_TERMINAL_CODING (f)->id),
21702 p, 0);
21703 }
21704 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21705 p, eol_flag);
21706
21707 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21708 #ifdef subprocesses
21709 obj = Fget_buffer_process (Fcurrent_buffer ());
21710 if (PROCESSP (obj))
21711 {
21712 p = decode_mode_spec_coding
21713 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21714 p = decode_mode_spec_coding
21715 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21716 }
21717 #endif /* subprocesses */
21718 #endif /* 0 */
21719 *p = 0;
21720 return decode_mode_spec_buf;
21721 }
21722 }
21723
21724 if (STRINGP (obj))
21725 {
21726 *string = obj;
21727 return SSDATA (obj);
21728 }
21729 else
21730 return "";
21731 }
21732
21733
21734 /* Count up to COUNT lines starting from START_BYTE.
21735 But don't go beyond LIMIT_BYTE.
21736 Return the number of lines thus found (always nonnegative).
21737
21738 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21739
21740 static ptrdiff_t
21741 display_count_lines (ptrdiff_t start_byte,
21742 ptrdiff_t limit_byte, ptrdiff_t count,
21743 ptrdiff_t *byte_pos_ptr)
21744 {
21745 register unsigned char *cursor;
21746 unsigned char *base;
21747
21748 register ptrdiff_t ceiling;
21749 register unsigned char *ceiling_addr;
21750 ptrdiff_t orig_count = count;
21751
21752 /* If we are not in selective display mode,
21753 check only for newlines. */
21754 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21755 && !INTEGERP (BVAR (current_buffer, selective_display)));
21756
21757 if (count > 0)
21758 {
21759 while (start_byte < limit_byte)
21760 {
21761 ceiling = BUFFER_CEILING_OF (start_byte);
21762 ceiling = min (limit_byte - 1, ceiling);
21763 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21764 base = (cursor = BYTE_POS_ADDR (start_byte));
21765 while (1)
21766 {
21767 if (selective_display)
21768 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21769 ;
21770 else
21771 while (*cursor != '\n' && ++cursor != ceiling_addr)
21772 ;
21773
21774 if (cursor != ceiling_addr)
21775 {
21776 if (--count == 0)
21777 {
21778 start_byte += cursor - base + 1;
21779 *byte_pos_ptr = start_byte;
21780 return orig_count;
21781 }
21782 else
21783 if (++cursor == ceiling_addr)
21784 break;
21785 }
21786 else
21787 break;
21788 }
21789 start_byte += cursor - base;
21790 }
21791 }
21792 else
21793 {
21794 while (start_byte > limit_byte)
21795 {
21796 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21797 ceiling = max (limit_byte, ceiling);
21798 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21799 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21800 while (1)
21801 {
21802 if (selective_display)
21803 while (--cursor != ceiling_addr
21804 && *cursor != '\n' && *cursor != 015)
21805 ;
21806 else
21807 while (--cursor != ceiling_addr && *cursor != '\n')
21808 ;
21809
21810 if (cursor != ceiling_addr)
21811 {
21812 if (++count == 0)
21813 {
21814 start_byte += cursor - base + 1;
21815 *byte_pos_ptr = start_byte;
21816 /* When scanning backwards, we should
21817 not count the newline posterior to which we stop. */
21818 return - orig_count - 1;
21819 }
21820 }
21821 else
21822 break;
21823 }
21824 /* Here we add 1 to compensate for the last decrement
21825 of CURSOR, which took it past the valid range. */
21826 start_byte += cursor - base + 1;
21827 }
21828 }
21829
21830 *byte_pos_ptr = limit_byte;
21831
21832 if (count < 0)
21833 return - orig_count + count;
21834 return orig_count - count;
21835
21836 }
21837
21838
21839 \f
21840 /***********************************************************************
21841 Displaying strings
21842 ***********************************************************************/
21843
21844 /* Display a NUL-terminated string, starting with index START.
21845
21846 If STRING is non-null, display that C string. Otherwise, the Lisp
21847 string LISP_STRING is displayed. There's a case that STRING is
21848 non-null and LISP_STRING is not nil. It means STRING is a string
21849 data of LISP_STRING. In that case, we display LISP_STRING while
21850 ignoring its text properties.
21851
21852 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21853 FACE_STRING. Display STRING or LISP_STRING with the face at
21854 FACE_STRING_POS in FACE_STRING:
21855
21856 Display the string in the environment given by IT, but use the
21857 standard display table, temporarily.
21858
21859 FIELD_WIDTH is the minimum number of output glyphs to produce.
21860 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21861 with spaces. If STRING has more characters, more than FIELD_WIDTH
21862 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21863
21864 PRECISION is the maximum number of characters to output from
21865 STRING. PRECISION < 0 means don't truncate the string.
21866
21867 This is roughly equivalent to printf format specifiers:
21868
21869 FIELD_WIDTH PRECISION PRINTF
21870 ----------------------------------------
21871 -1 -1 %s
21872 -1 10 %.10s
21873 10 -1 %10s
21874 20 10 %20.10s
21875
21876 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21877 display them, and < 0 means obey the current buffer's value of
21878 enable_multibyte_characters.
21879
21880 Value is the number of columns displayed. */
21881
21882 static int
21883 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21884 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21885 int field_width, int precision, int max_x, int multibyte)
21886 {
21887 int hpos_at_start = it->hpos;
21888 int saved_face_id = it->face_id;
21889 struct glyph_row *row = it->glyph_row;
21890 ptrdiff_t it_charpos;
21891
21892 /* Initialize the iterator IT for iteration over STRING beginning
21893 with index START. */
21894 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21895 precision, field_width, multibyte);
21896 if (string && STRINGP (lisp_string))
21897 /* LISP_STRING is the one returned by decode_mode_spec. We should
21898 ignore its text properties. */
21899 it->stop_charpos = it->end_charpos;
21900
21901 /* If displaying STRING, set up the face of the iterator from
21902 FACE_STRING, if that's given. */
21903 if (STRINGP (face_string))
21904 {
21905 ptrdiff_t endptr;
21906 struct face *face;
21907
21908 it->face_id
21909 = face_at_string_position (it->w, face_string, face_string_pos,
21910 0, it->region_beg_charpos,
21911 it->region_end_charpos,
21912 &endptr, it->base_face_id, 0);
21913 face = FACE_FROM_ID (it->f, it->face_id);
21914 it->face_box_p = face->box != FACE_NO_BOX;
21915 }
21916
21917 /* Set max_x to the maximum allowed X position. Don't let it go
21918 beyond the right edge of the window. */
21919 if (max_x <= 0)
21920 max_x = it->last_visible_x;
21921 else
21922 max_x = min (max_x, it->last_visible_x);
21923
21924 /* Skip over display elements that are not visible. because IT->w is
21925 hscrolled. */
21926 if (it->current_x < it->first_visible_x)
21927 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21928 MOVE_TO_POS | MOVE_TO_X);
21929
21930 row->ascent = it->max_ascent;
21931 row->height = it->max_ascent + it->max_descent;
21932 row->phys_ascent = it->max_phys_ascent;
21933 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21934 row->extra_line_spacing = it->max_extra_line_spacing;
21935
21936 if (STRINGP (it->string))
21937 it_charpos = IT_STRING_CHARPOS (*it);
21938 else
21939 it_charpos = IT_CHARPOS (*it);
21940
21941 /* This condition is for the case that we are called with current_x
21942 past last_visible_x. */
21943 while (it->current_x < max_x)
21944 {
21945 int x_before, x, n_glyphs_before, i, nglyphs;
21946
21947 /* Get the next display element. */
21948 if (!get_next_display_element (it))
21949 break;
21950
21951 /* Produce glyphs. */
21952 x_before = it->current_x;
21953 n_glyphs_before = row->used[TEXT_AREA];
21954 PRODUCE_GLYPHS (it);
21955
21956 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21957 i = 0;
21958 x = x_before;
21959 while (i < nglyphs)
21960 {
21961 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21962
21963 if (it->line_wrap != TRUNCATE
21964 && x + glyph->pixel_width > max_x)
21965 {
21966 /* End of continued line or max_x reached. */
21967 if (CHAR_GLYPH_PADDING_P (*glyph))
21968 {
21969 /* A wide character is unbreakable. */
21970 if (row->reversed_p)
21971 unproduce_glyphs (it, row->used[TEXT_AREA]
21972 - n_glyphs_before);
21973 row->used[TEXT_AREA] = n_glyphs_before;
21974 it->current_x = x_before;
21975 }
21976 else
21977 {
21978 if (row->reversed_p)
21979 unproduce_glyphs (it, row->used[TEXT_AREA]
21980 - (n_glyphs_before + i));
21981 row->used[TEXT_AREA] = n_glyphs_before + i;
21982 it->current_x = x;
21983 }
21984 break;
21985 }
21986 else if (x + glyph->pixel_width >= it->first_visible_x)
21987 {
21988 /* Glyph is at least partially visible. */
21989 ++it->hpos;
21990 if (x < it->first_visible_x)
21991 row->x = x - it->first_visible_x;
21992 }
21993 else
21994 {
21995 /* Glyph is off the left margin of the display area.
21996 Should not happen. */
21997 emacs_abort ();
21998 }
21999
22000 row->ascent = max (row->ascent, it->max_ascent);
22001 row->height = max (row->height, it->max_ascent + it->max_descent);
22002 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22003 row->phys_height = max (row->phys_height,
22004 it->max_phys_ascent + it->max_phys_descent);
22005 row->extra_line_spacing = max (row->extra_line_spacing,
22006 it->max_extra_line_spacing);
22007 x += glyph->pixel_width;
22008 ++i;
22009 }
22010
22011 /* Stop if max_x reached. */
22012 if (i < nglyphs)
22013 break;
22014
22015 /* Stop at line ends. */
22016 if (ITERATOR_AT_END_OF_LINE_P (it))
22017 {
22018 it->continuation_lines_width = 0;
22019 break;
22020 }
22021
22022 set_iterator_to_next (it, 1);
22023 if (STRINGP (it->string))
22024 it_charpos = IT_STRING_CHARPOS (*it);
22025 else
22026 it_charpos = IT_CHARPOS (*it);
22027
22028 /* Stop if truncating at the right edge. */
22029 if (it->line_wrap == TRUNCATE
22030 && it->current_x >= it->last_visible_x)
22031 {
22032 /* Add truncation mark, but don't do it if the line is
22033 truncated at a padding space. */
22034 if (it_charpos < it->string_nchars)
22035 {
22036 if (!FRAME_WINDOW_P (it->f))
22037 {
22038 int ii, n;
22039
22040 if (it->current_x > it->last_visible_x)
22041 {
22042 if (!row->reversed_p)
22043 {
22044 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22045 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22046 break;
22047 }
22048 else
22049 {
22050 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22051 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22052 break;
22053 unproduce_glyphs (it, ii + 1);
22054 ii = row->used[TEXT_AREA] - (ii + 1);
22055 }
22056 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22057 {
22058 row->used[TEXT_AREA] = ii;
22059 produce_special_glyphs (it, IT_TRUNCATION);
22060 }
22061 }
22062 produce_special_glyphs (it, IT_TRUNCATION);
22063 }
22064 row->truncated_on_right_p = 1;
22065 }
22066 break;
22067 }
22068 }
22069
22070 /* Maybe insert a truncation at the left. */
22071 if (it->first_visible_x
22072 && it_charpos > 0)
22073 {
22074 if (!FRAME_WINDOW_P (it->f)
22075 || (row->reversed_p
22076 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22077 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22078 insert_left_trunc_glyphs (it);
22079 row->truncated_on_left_p = 1;
22080 }
22081
22082 it->face_id = saved_face_id;
22083
22084 /* Value is number of columns displayed. */
22085 return it->hpos - hpos_at_start;
22086 }
22087
22088
22089 \f
22090 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22091 appears as an element of LIST or as the car of an element of LIST.
22092 If PROPVAL is a list, compare each element against LIST in that
22093 way, and return 1/2 if any element of PROPVAL is found in LIST.
22094 Otherwise return 0. This function cannot quit.
22095 The return value is 2 if the text is invisible but with an ellipsis
22096 and 1 if it's invisible and without an ellipsis. */
22097
22098 int
22099 invisible_p (register Lisp_Object propval, Lisp_Object list)
22100 {
22101 register Lisp_Object tail, proptail;
22102
22103 for (tail = list; CONSP (tail); tail = XCDR (tail))
22104 {
22105 register Lisp_Object tem;
22106 tem = XCAR (tail);
22107 if (EQ (propval, tem))
22108 return 1;
22109 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22110 return NILP (XCDR (tem)) ? 1 : 2;
22111 }
22112
22113 if (CONSP (propval))
22114 {
22115 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22116 {
22117 Lisp_Object propelt;
22118 propelt = XCAR (proptail);
22119 for (tail = list; CONSP (tail); tail = XCDR (tail))
22120 {
22121 register Lisp_Object tem;
22122 tem = XCAR (tail);
22123 if (EQ (propelt, tem))
22124 return 1;
22125 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22126 return NILP (XCDR (tem)) ? 1 : 2;
22127 }
22128 }
22129 }
22130
22131 return 0;
22132 }
22133
22134 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22135 doc: /* Non-nil if the property makes the text invisible.
22136 POS-OR-PROP can be a marker or number, in which case it is taken to be
22137 a position in the current buffer and the value of the `invisible' property
22138 is checked; or it can be some other value, which is then presumed to be the
22139 value of the `invisible' property of the text of interest.
22140 The non-nil value returned can be t for truly invisible text or something
22141 else if the text is replaced by an ellipsis. */)
22142 (Lisp_Object pos_or_prop)
22143 {
22144 Lisp_Object prop
22145 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22146 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22147 : pos_or_prop);
22148 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22149 return (invis == 0 ? Qnil
22150 : invis == 1 ? Qt
22151 : make_number (invis));
22152 }
22153
22154 /* Calculate a width or height in pixels from a specification using
22155 the following elements:
22156
22157 SPEC ::=
22158 NUM - a (fractional) multiple of the default font width/height
22159 (NUM) - specifies exactly NUM pixels
22160 UNIT - a fixed number of pixels, see below.
22161 ELEMENT - size of a display element in pixels, see below.
22162 (NUM . SPEC) - equals NUM * SPEC
22163 (+ SPEC SPEC ...) - add pixel values
22164 (- SPEC SPEC ...) - subtract pixel values
22165 (- SPEC) - negate pixel value
22166
22167 NUM ::=
22168 INT or FLOAT - a number constant
22169 SYMBOL - use symbol's (buffer local) variable binding.
22170
22171 UNIT ::=
22172 in - pixels per inch *)
22173 mm - pixels per 1/1000 meter *)
22174 cm - pixels per 1/100 meter *)
22175 width - width of current font in pixels.
22176 height - height of current font in pixels.
22177
22178 *) using the ratio(s) defined in display-pixels-per-inch.
22179
22180 ELEMENT ::=
22181
22182 left-fringe - left fringe width in pixels
22183 right-fringe - right fringe width in pixels
22184
22185 left-margin - left margin width in pixels
22186 right-margin - right margin width in pixels
22187
22188 scroll-bar - scroll-bar area width in pixels
22189
22190 Examples:
22191
22192 Pixels corresponding to 5 inches:
22193 (5 . in)
22194
22195 Total width of non-text areas on left side of window (if scroll-bar is on left):
22196 '(space :width (+ left-fringe left-margin scroll-bar))
22197
22198 Align to first text column (in header line):
22199 '(space :align-to 0)
22200
22201 Align to middle of text area minus half the width of variable `my-image'
22202 containing a loaded image:
22203 '(space :align-to (0.5 . (- text my-image)))
22204
22205 Width of left margin minus width of 1 character in the default font:
22206 '(space :width (- left-margin 1))
22207
22208 Width of left margin minus width of 2 characters in the current font:
22209 '(space :width (- left-margin (2 . width)))
22210
22211 Center 1 character over left-margin (in header line):
22212 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22213
22214 Different ways to express width of left fringe plus left margin minus one pixel:
22215 '(space :width (- (+ left-fringe left-margin) (1)))
22216 '(space :width (+ left-fringe left-margin (- (1))))
22217 '(space :width (+ left-fringe left-margin (-1)))
22218
22219 */
22220
22221 #define NUMVAL(X) \
22222 ((INTEGERP (X) || FLOATP (X)) \
22223 ? XFLOATINT (X) \
22224 : - 1)
22225
22226 static int
22227 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22228 struct font *font, int width_p, int *align_to)
22229 {
22230 double pixels;
22231
22232 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22233 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22234
22235 if (NILP (prop))
22236 return OK_PIXELS (0);
22237
22238 eassert (FRAME_LIVE_P (it->f));
22239
22240 if (SYMBOLP (prop))
22241 {
22242 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22243 {
22244 char *unit = SSDATA (SYMBOL_NAME (prop));
22245
22246 if (unit[0] == 'i' && unit[1] == 'n')
22247 pixels = 1.0;
22248 else if (unit[0] == 'm' && unit[1] == 'm')
22249 pixels = 25.4;
22250 else if (unit[0] == 'c' && unit[1] == 'm')
22251 pixels = 2.54;
22252 else
22253 pixels = 0;
22254 if (pixels > 0)
22255 {
22256 double ppi;
22257 #ifdef HAVE_WINDOW_SYSTEM
22258 if (FRAME_WINDOW_P (it->f)
22259 && (ppi = (width_p
22260 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22261 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22262 ppi > 0))
22263 return OK_PIXELS (ppi / pixels);
22264 #endif
22265
22266 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22267 || (CONSP (Vdisplay_pixels_per_inch)
22268 && (ppi = (width_p
22269 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22270 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22271 ppi > 0)))
22272 return OK_PIXELS (ppi / pixels);
22273
22274 return 0;
22275 }
22276 }
22277
22278 #ifdef HAVE_WINDOW_SYSTEM
22279 if (EQ (prop, Qheight))
22280 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22281 if (EQ (prop, Qwidth))
22282 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22283 #else
22284 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22285 return OK_PIXELS (1);
22286 #endif
22287
22288 if (EQ (prop, Qtext))
22289 return OK_PIXELS (width_p
22290 ? window_box_width (it->w, TEXT_AREA)
22291 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22292
22293 if (align_to && *align_to < 0)
22294 {
22295 *res = 0;
22296 if (EQ (prop, Qleft))
22297 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22298 if (EQ (prop, Qright))
22299 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22300 if (EQ (prop, Qcenter))
22301 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22302 + window_box_width (it->w, TEXT_AREA) / 2);
22303 if (EQ (prop, Qleft_fringe))
22304 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22305 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22306 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22307 if (EQ (prop, Qright_fringe))
22308 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22309 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22310 : window_box_right_offset (it->w, TEXT_AREA));
22311 if (EQ (prop, Qleft_margin))
22312 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22313 if (EQ (prop, Qright_margin))
22314 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22315 if (EQ (prop, Qscroll_bar))
22316 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22317 ? 0
22318 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22319 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22320 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22321 : 0)));
22322 }
22323 else
22324 {
22325 if (EQ (prop, Qleft_fringe))
22326 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22327 if (EQ (prop, Qright_fringe))
22328 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22329 if (EQ (prop, Qleft_margin))
22330 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22331 if (EQ (prop, Qright_margin))
22332 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22333 if (EQ (prop, Qscroll_bar))
22334 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22335 }
22336
22337 prop = buffer_local_value_1 (prop, it->w->buffer);
22338 if (EQ (prop, Qunbound))
22339 prop = Qnil;
22340 }
22341
22342 if (INTEGERP (prop) || FLOATP (prop))
22343 {
22344 int base_unit = (width_p
22345 ? FRAME_COLUMN_WIDTH (it->f)
22346 : FRAME_LINE_HEIGHT (it->f));
22347 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22348 }
22349
22350 if (CONSP (prop))
22351 {
22352 Lisp_Object car = XCAR (prop);
22353 Lisp_Object cdr = XCDR (prop);
22354
22355 if (SYMBOLP (car))
22356 {
22357 #ifdef HAVE_WINDOW_SYSTEM
22358 if (FRAME_WINDOW_P (it->f)
22359 && valid_image_p (prop))
22360 {
22361 ptrdiff_t id = lookup_image (it->f, prop);
22362 struct image *img = IMAGE_FROM_ID (it->f, id);
22363
22364 return OK_PIXELS (width_p ? img->width : img->height);
22365 }
22366 #endif
22367 if (EQ (car, Qplus) || EQ (car, Qminus))
22368 {
22369 int first = 1;
22370 double px;
22371
22372 pixels = 0;
22373 while (CONSP (cdr))
22374 {
22375 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22376 font, width_p, align_to))
22377 return 0;
22378 if (first)
22379 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22380 else
22381 pixels += px;
22382 cdr = XCDR (cdr);
22383 }
22384 if (EQ (car, Qminus))
22385 pixels = -pixels;
22386 return OK_PIXELS (pixels);
22387 }
22388
22389 car = buffer_local_value_1 (car, it->w->buffer);
22390 if (EQ (car, Qunbound))
22391 car = Qnil;
22392 }
22393
22394 if (INTEGERP (car) || FLOATP (car))
22395 {
22396 double fact;
22397 pixels = XFLOATINT (car);
22398 if (NILP (cdr))
22399 return OK_PIXELS (pixels);
22400 if (calc_pixel_width_or_height (&fact, it, cdr,
22401 font, width_p, align_to))
22402 return OK_PIXELS (pixels * fact);
22403 return 0;
22404 }
22405
22406 return 0;
22407 }
22408
22409 return 0;
22410 }
22411
22412 \f
22413 /***********************************************************************
22414 Glyph Display
22415 ***********************************************************************/
22416
22417 #ifdef HAVE_WINDOW_SYSTEM
22418
22419 #ifdef GLYPH_DEBUG
22420
22421 void
22422 dump_glyph_string (struct glyph_string *s)
22423 {
22424 fprintf (stderr, "glyph string\n");
22425 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22426 s->x, s->y, s->width, s->height);
22427 fprintf (stderr, " ybase = %d\n", s->ybase);
22428 fprintf (stderr, " hl = %d\n", s->hl);
22429 fprintf (stderr, " left overhang = %d, right = %d\n",
22430 s->left_overhang, s->right_overhang);
22431 fprintf (stderr, " nchars = %d\n", s->nchars);
22432 fprintf (stderr, " extends to end of line = %d\n",
22433 s->extends_to_end_of_line_p);
22434 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22435 fprintf (stderr, " bg width = %d\n", s->background_width);
22436 }
22437
22438 #endif /* GLYPH_DEBUG */
22439
22440 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22441 of XChar2b structures for S; it can't be allocated in
22442 init_glyph_string because it must be allocated via `alloca'. W
22443 is the window on which S is drawn. ROW and AREA are the glyph row
22444 and area within the row from which S is constructed. START is the
22445 index of the first glyph structure covered by S. HL is a
22446 face-override for drawing S. */
22447
22448 #ifdef HAVE_NTGUI
22449 #define OPTIONAL_HDC(hdc) HDC hdc,
22450 #define DECLARE_HDC(hdc) HDC hdc;
22451 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22452 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22453 #endif
22454
22455 #ifndef OPTIONAL_HDC
22456 #define OPTIONAL_HDC(hdc)
22457 #define DECLARE_HDC(hdc)
22458 #define ALLOCATE_HDC(hdc, f)
22459 #define RELEASE_HDC(hdc, f)
22460 #endif
22461
22462 static void
22463 init_glyph_string (struct glyph_string *s,
22464 OPTIONAL_HDC (hdc)
22465 XChar2b *char2b, struct window *w, struct glyph_row *row,
22466 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22467 {
22468 memset (s, 0, sizeof *s);
22469 s->w = w;
22470 s->f = XFRAME (w->frame);
22471 #ifdef HAVE_NTGUI
22472 s->hdc = hdc;
22473 #endif
22474 s->display = FRAME_X_DISPLAY (s->f);
22475 s->window = FRAME_X_WINDOW (s->f);
22476 s->char2b = char2b;
22477 s->hl = hl;
22478 s->row = row;
22479 s->area = area;
22480 s->first_glyph = row->glyphs[area] + start;
22481 s->height = row->height;
22482 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22483 s->ybase = s->y + row->ascent;
22484 }
22485
22486
22487 /* Append the list of glyph strings with head H and tail T to the list
22488 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22489
22490 static inline void
22491 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22492 struct glyph_string *h, struct glyph_string *t)
22493 {
22494 if (h)
22495 {
22496 if (*head)
22497 (*tail)->next = h;
22498 else
22499 *head = h;
22500 h->prev = *tail;
22501 *tail = t;
22502 }
22503 }
22504
22505
22506 /* Prepend the list of glyph strings with head H and tail T to the
22507 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22508 result. */
22509
22510 static inline void
22511 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22512 struct glyph_string *h, struct glyph_string *t)
22513 {
22514 if (h)
22515 {
22516 if (*head)
22517 (*head)->prev = t;
22518 else
22519 *tail = t;
22520 t->next = *head;
22521 *head = h;
22522 }
22523 }
22524
22525
22526 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22527 Set *HEAD and *TAIL to the resulting list. */
22528
22529 static inline void
22530 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22531 struct glyph_string *s)
22532 {
22533 s->next = s->prev = NULL;
22534 append_glyph_string_lists (head, tail, s, s);
22535 }
22536
22537
22538 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22539 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22540 make sure that X resources for the face returned are allocated.
22541 Value is a pointer to a realized face that is ready for display if
22542 DISPLAY_P is non-zero. */
22543
22544 static inline struct face *
22545 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22546 XChar2b *char2b, int display_p)
22547 {
22548 struct face *face = FACE_FROM_ID (f, face_id);
22549
22550 if (face->font)
22551 {
22552 unsigned code = face->font->driver->encode_char (face->font, c);
22553
22554 if (code != FONT_INVALID_CODE)
22555 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22556 else
22557 STORE_XCHAR2B (char2b, 0, 0);
22558 }
22559
22560 /* Make sure X resources of the face are allocated. */
22561 #ifdef HAVE_X_WINDOWS
22562 if (display_p)
22563 #endif
22564 {
22565 eassert (face != NULL);
22566 PREPARE_FACE_FOR_DISPLAY (f, face);
22567 }
22568
22569 return face;
22570 }
22571
22572
22573 /* Get face and two-byte form of character glyph GLYPH on frame F.
22574 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22575 a pointer to a realized face that is ready for display. */
22576
22577 static inline struct face *
22578 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22579 XChar2b *char2b, int *two_byte_p)
22580 {
22581 struct face *face;
22582
22583 eassert (glyph->type == CHAR_GLYPH);
22584 face = FACE_FROM_ID (f, glyph->face_id);
22585
22586 if (two_byte_p)
22587 *two_byte_p = 0;
22588
22589 if (face->font)
22590 {
22591 unsigned code;
22592
22593 if (CHAR_BYTE8_P (glyph->u.ch))
22594 code = CHAR_TO_BYTE8 (glyph->u.ch);
22595 else
22596 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22597
22598 if (code != FONT_INVALID_CODE)
22599 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22600 else
22601 STORE_XCHAR2B (char2b, 0, 0);
22602 }
22603
22604 /* Make sure X resources of the face are allocated. */
22605 eassert (face != NULL);
22606 PREPARE_FACE_FOR_DISPLAY (f, face);
22607 return face;
22608 }
22609
22610
22611 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22612 Return 1 if FONT has a glyph for C, otherwise return 0. */
22613
22614 static inline int
22615 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22616 {
22617 unsigned code;
22618
22619 if (CHAR_BYTE8_P (c))
22620 code = CHAR_TO_BYTE8 (c);
22621 else
22622 code = font->driver->encode_char (font, c);
22623
22624 if (code == FONT_INVALID_CODE)
22625 return 0;
22626 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22627 return 1;
22628 }
22629
22630
22631 /* Fill glyph string S with composition components specified by S->cmp.
22632
22633 BASE_FACE is the base face of the composition.
22634 S->cmp_from is the index of the first component for S.
22635
22636 OVERLAPS non-zero means S should draw the foreground only, and use
22637 its physical height for clipping. See also draw_glyphs.
22638
22639 Value is the index of a component not in S. */
22640
22641 static int
22642 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22643 int overlaps)
22644 {
22645 int i;
22646 /* For all glyphs of this composition, starting at the offset
22647 S->cmp_from, until we reach the end of the definition or encounter a
22648 glyph that requires the different face, add it to S. */
22649 struct face *face;
22650
22651 eassert (s);
22652
22653 s->for_overlaps = overlaps;
22654 s->face = NULL;
22655 s->font = NULL;
22656 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22657 {
22658 int c = COMPOSITION_GLYPH (s->cmp, i);
22659
22660 /* TAB in a composition means display glyphs with padding space
22661 on the left or right. */
22662 if (c != '\t')
22663 {
22664 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22665 -1, Qnil);
22666
22667 face = get_char_face_and_encoding (s->f, c, face_id,
22668 s->char2b + i, 1);
22669 if (face)
22670 {
22671 if (! s->face)
22672 {
22673 s->face = face;
22674 s->font = s->face->font;
22675 }
22676 else if (s->face != face)
22677 break;
22678 }
22679 }
22680 ++s->nchars;
22681 }
22682 s->cmp_to = i;
22683
22684 if (s->face == NULL)
22685 {
22686 s->face = base_face->ascii_face;
22687 s->font = s->face->font;
22688 }
22689
22690 /* All glyph strings for the same composition has the same width,
22691 i.e. the width set for the first component of the composition. */
22692 s->width = s->first_glyph->pixel_width;
22693
22694 /* If the specified font could not be loaded, use the frame's
22695 default font, but record the fact that we couldn't load it in
22696 the glyph string so that we can draw rectangles for the
22697 characters of the glyph string. */
22698 if (s->font == NULL)
22699 {
22700 s->font_not_found_p = 1;
22701 s->font = FRAME_FONT (s->f);
22702 }
22703
22704 /* Adjust base line for subscript/superscript text. */
22705 s->ybase += s->first_glyph->voffset;
22706
22707 /* This glyph string must always be drawn with 16-bit functions. */
22708 s->two_byte_p = 1;
22709
22710 return s->cmp_to;
22711 }
22712
22713 static int
22714 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22715 int start, int end, int overlaps)
22716 {
22717 struct glyph *glyph, *last;
22718 Lisp_Object lgstring;
22719 int i;
22720
22721 s->for_overlaps = overlaps;
22722 glyph = s->row->glyphs[s->area] + start;
22723 last = s->row->glyphs[s->area] + end;
22724 s->cmp_id = glyph->u.cmp.id;
22725 s->cmp_from = glyph->slice.cmp.from;
22726 s->cmp_to = glyph->slice.cmp.to + 1;
22727 s->face = FACE_FROM_ID (s->f, face_id);
22728 lgstring = composition_gstring_from_id (s->cmp_id);
22729 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22730 glyph++;
22731 while (glyph < last
22732 && glyph->u.cmp.automatic
22733 && glyph->u.cmp.id == s->cmp_id
22734 && s->cmp_to == glyph->slice.cmp.from)
22735 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22736
22737 for (i = s->cmp_from; i < s->cmp_to; i++)
22738 {
22739 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22740 unsigned code = LGLYPH_CODE (lglyph);
22741
22742 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22743 }
22744 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22745 return glyph - s->row->glyphs[s->area];
22746 }
22747
22748
22749 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22750 See the comment of fill_glyph_string for arguments.
22751 Value is the index of the first glyph not in S. */
22752
22753
22754 static int
22755 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22756 int start, int end, int overlaps)
22757 {
22758 struct glyph *glyph, *last;
22759 int voffset;
22760
22761 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22762 s->for_overlaps = overlaps;
22763 glyph = s->row->glyphs[s->area] + start;
22764 last = s->row->glyphs[s->area] + end;
22765 voffset = glyph->voffset;
22766 s->face = FACE_FROM_ID (s->f, face_id);
22767 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22768 s->nchars = 1;
22769 s->width = glyph->pixel_width;
22770 glyph++;
22771 while (glyph < last
22772 && glyph->type == GLYPHLESS_GLYPH
22773 && glyph->voffset == voffset
22774 && glyph->face_id == face_id)
22775 {
22776 s->nchars++;
22777 s->width += glyph->pixel_width;
22778 glyph++;
22779 }
22780 s->ybase += voffset;
22781 return glyph - s->row->glyphs[s->area];
22782 }
22783
22784
22785 /* Fill glyph string S from a sequence of character glyphs.
22786
22787 FACE_ID is the face id of the string. START is the index of the
22788 first glyph to consider, END is the index of the last + 1.
22789 OVERLAPS non-zero means S should draw the foreground only, and use
22790 its physical height for clipping. See also draw_glyphs.
22791
22792 Value is the index of the first glyph not in S. */
22793
22794 static int
22795 fill_glyph_string (struct glyph_string *s, int face_id,
22796 int start, int end, int overlaps)
22797 {
22798 struct glyph *glyph, *last;
22799 int voffset;
22800 int glyph_not_available_p;
22801
22802 eassert (s->f == XFRAME (s->w->frame));
22803 eassert (s->nchars == 0);
22804 eassert (start >= 0 && end > start);
22805
22806 s->for_overlaps = overlaps;
22807 glyph = s->row->glyphs[s->area] + start;
22808 last = s->row->glyphs[s->area] + end;
22809 voffset = glyph->voffset;
22810 s->padding_p = glyph->padding_p;
22811 glyph_not_available_p = glyph->glyph_not_available_p;
22812
22813 while (glyph < last
22814 && glyph->type == CHAR_GLYPH
22815 && glyph->voffset == voffset
22816 /* Same face id implies same font, nowadays. */
22817 && glyph->face_id == face_id
22818 && glyph->glyph_not_available_p == glyph_not_available_p)
22819 {
22820 int two_byte_p;
22821
22822 s->face = get_glyph_face_and_encoding (s->f, glyph,
22823 s->char2b + s->nchars,
22824 &two_byte_p);
22825 s->two_byte_p = two_byte_p;
22826 ++s->nchars;
22827 eassert (s->nchars <= end - start);
22828 s->width += glyph->pixel_width;
22829 if (glyph++->padding_p != s->padding_p)
22830 break;
22831 }
22832
22833 s->font = s->face->font;
22834
22835 /* If the specified font could not be loaded, use the frame's font,
22836 but record the fact that we couldn't load it in
22837 S->font_not_found_p so that we can draw rectangles for the
22838 characters of the glyph string. */
22839 if (s->font == NULL || glyph_not_available_p)
22840 {
22841 s->font_not_found_p = 1;
22842 s->font = FRAME_FONT (s->f);
22843 }
22844
22845 /* Adjust base line for subscript/superscript text. */
22846 s->ybase += voffset;
22847
22848 eassert (s->face && s->face->gc);
22849 return glyph - s->row->glyphs[s->area];
22850 }
22851
22852
22853 /* Fill glyph string S from image glyph S->first_glyph. */
22854
22855 static void
22856 fill_image_glyph_string (struct glyph_string *s)
22857 {
22858 eassert (s->first_glyph->type == IMAGE_GLYPH);
22859 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22860 eassert (s->img);
22861 s->slice = s->first_glyph->slice.img;
22862 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22863 s->font = s->face->font;
22864 s->width = s->first_glyph->pixel_width;
22865
22866 /* Adjust base line for subscript/superscript text. */
22867 s->ybase += s->first_glyph->voffset;
22868 }
22869
22870
22871 /* Fill glyph string S from a sequence of stretch glyphs.
22872
22873 START is the index of the first glyph to consider,
22874 END is the index of the last + 1.
22875
22876 Value is the index of the first glyph not in S. */
22877
22878 static int
22879 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22880 {
22881 struct glyph *glyph, *last;
22882 int voffset, face_id;
22883
22884 eassert (s->first_glyph->type == STRETCH_GLYPH);
22885
22886 glyph = s->row->glyphs[s->area] + start;
22887 last = s->row->glyphs[s->area] + end;
22888 face_id = glyph->face_id;
22889 s->face = FACE_FROM_ID (s->f, face_id);
22890 s->font = s->face->font;
22891 s->width = glyph->pixel_width;
22892 s->nchars = 1;
22893 voffset = glyph->voffset;
22894
22895 for (++glyph;
22896 (glyph < last
22897 && glyph->type == STRETCH_GLYPH
22898 && glyph->voffset == voffset
22899 && glyph->face_id == face_id);
22900 ++glyph)
22901 s->width += glyph->pixel_width;
22902
22903 /* Adjust base line for subscript/superscript text. */
22904 s->ybase += voffset;
22905
22906 /* The case that face->gc == 0 is handled when drawing the glyph
22907 string by calling PREPARE_FACE_FOR_DISPLAY. */
22908 eassert (s->face);
22909 return glyph - s->row->glyphs[s->area];
22910 }
22911
22912 static struct font_metrics *
22913 get_per_char_metric (struct font *font, XChar2b *char2b)
22914 {
22915 static struct font_metrics metrics;
22916 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22917
22918 if (! font || code == FONT_INVALID_CODE)
22919 return NULL;
22920 font->driver->text_extents (font, &code, 1, &metrics);
22921 return &metrics;
22922 }
22923
22924 /* EXPORT for RIF:
22925 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22926 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22927 assumed to be zero. */
22928
22929 void
22930 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22931 {
22932 *left = *right = 0;
22933
22934 if (glyph->type == CHAR_GLYPH)
22935 {
22936 struct face *face;
22937 XChar2b char2b;
22938 struct font_metrics *pcm;
22939
22940 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22941 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22942 {
22943 if (pcm->rbearing > pcm->width)
22944 *right = pcm->rbearing - pcm->width;
22945 if (pcm->lbearing < 0)
22946 *left = -pcm->lbearing;
22947 }
22948 }
22949 else if (glyph->type == COMPOSITE_GLYPH)
22950 {
22951 if (! glyph->u.cmp.automatic)
22952 {
22953 struct composition *cmp = composition_table[glyph->u.cmp.id];
22954
22955 if (cmp->rbearing > cmp->pixel_width)
22956 *right = cmp->rbearing - cmp->pixel_width;
22957 if (cmp->lbearing < 0)
22958 *left = - cmp->lbearing;
22959 }
22960 else
22961 {
22962 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22963 struct font_metrics metrics;
22964
22965 composition_gstring_width (gstring, glyph->slice.cmp.from,
22966 glyph->slice.cmp.to + 1, &metrics);
22967 if (metrics.rbearing > metrics.width)
22968 *right = metrics.rbearing - metrics.width;
22969 if (metrics.lbearing < 0)
22970 *left = - metrics.lbearing;
22971 }
22972 }
22973 }
22974
22975
22976 /* Return the index of the first glyph preceding glyph string S that
22977 is overwritten by S because of S's left overhang. Value is -1
22978 if no glyphs are overwritten. */
22979
22980 static int
22981 left_overwritten (struct glyph_string *s)
22982 {
22983 int k;
22984
22985 if (s->left_overhang)
22986 {
22987 int x = 0, i;
22988 struct glyph *glyphs = s->row->glyphs[s->area];
22989 int first = s->first_glyph - glyphs;
22990
22991 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22992 x -= glyphs[i].pixel_width;
22993
22994 k = i + 1;
22995 }
22996 else
22997 k = -1;
22998
22999 return k;
23000 }
23001
23002
23003 /* Return the index of the first glyph preceding glyph string S that
23004 is overwriting S because of its right overhang. Value is -1 if no
23005 glyph in front of S overwrites S. */
23006
23007 static int
23008 left_overwriting (struct glyph_string *s)
23009 {
23010 int i, k, x;
23011 struct glyph *glyphs = s->row->glyphs[s->area];
23012 int first = s->first_glyph - glyphs;
23013
23014 k = -1;
23015 x = 0;
23016 for (i = first - 1; i >= 0; --i)
23017 {
23018 int left, right;
23019 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23020 if (x + right > 0)
23021 k = i;
23022 x -= glyphs[i].pixel_width;
23023 }
23024
23025 return k;
23026 }
23027
23028
23029 /* Return the index of the last glyph following glyph string S that is
23030 overwritten by S because of S's right overhang. Value is -1 if
23031 no such glyph is found. */
23032
23033 static int
23034 right_overwritten (struct glyph_string *s)
23035 {
23036 int k = -1;
23037
23038 if (s->right_overhang)
23039 {
23040 int x = 0, i;
23041 struct glyph *glyphs = s->row->glyphs[s->area];
23042 int first = (s->first_glyph - glyphs
23043 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23044 int end = s->row->used[s->area];
23045
23046 for (i = first; i < end && s->right_overhang > x; ++i)
23047 x += glyphs[i].pixel_width;
23048
23049 k = i;
23050 }
23051
23052 return k;
23053 }
23054
23055
23056 /* Return the index of the last glyph following glyph string S that
23057 overwrites S because of its left overhang. Value is negative
23058 if no such glyph is found. */
23059
23060 static int
23061 right_overwriting (struct glyph_string *s)
23062 {
23063 int i, k, x;
23064 int end = s->row->used[s->area];
23065 struct glyph *glyphs = s->row->glyphs[s->area];
23066 int first = (s->first_glyph - glyphs
23067 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23068
23069 k = -1;
23070 x = 0;
23071 for (i = first; i < end; ++i)
23072 {
23073 int left, right;
23074 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23075 if (x - left < 0)
23076 k = i;
23077 x += glyphs[i].pixel_width;
23078 }
23079
23080 return k;
23081 }
23082
23083
23084 /* Set background width of glyph string S. START is the index of the
23085 first glyph following S. LAST_X is the right-most x-position + 1
23086 in the drawing area. */
23087
23088 static inline void
23089 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23090 {
23091 /* If the face of this glyph string has to be drawn to the end of
23092 the drawing area, set S->extends_to_end_of_line_p. */
23093
23094 if (start == s->row->used[s->area]
23095 && s->area == TEXT_AREA
23096 && ((s->row->fill_line_p
23097 && (s->hl == DRAW_NORMAL_TEXT
23098 || s->hl == DRAW_IMAGE_RAISED
23099 || s->hl == DRAW_IMAGE_SUNKEN))
23100 || s->hl == DRAW_MOUSE_FACE))
23101 s->extends_to_end_of_line_p = 1;
23102
23103 /* If S extends its face to the end of the line, set its
23104 background_width to the distance to the right edge of the drawing
23105 area. */
23106 if (s->extends_to_end_of_line_p)
23107 s->background_width = last_x - s->x + 1;
23108 else
23109 s->background_width = s->width;
23110 }
23111
23112
23113 /* Compute overhangs and x-positions for glyph string S and its
23114 predecessors, or successors. X is the starting x-position for S.
23115 BACKWARD_P non-zero means process predecessors. */
23116
23117 static void
23118 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23119 {
23120 if (backward_p)
23121 {
23122 while (s)
23123 {
23124 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23125 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23126 x -= s->width;
23127 s->x = x;
23128 s = s->prev;
23129 }
23130 }
23131 else
23132 {
23133 while (s)
23134 {
23135 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23136 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23137 s->x = x;
23138 x += s->width;
23139 s = s->next;
23140 }
23141 }
23142 }
23143
23144
23145
23146 /* The following macros are only called from draw_glyphs below.
23147 They reference the following parameters of that function directly:
23148 `w', `row', `area', and `overlap_p'
23149 as well as the following local variables:
23150 `s', `f', and `hdc' (in W32) */
23151
23152 #ifdef HAVE_NTGUI
23153 /* On W32, silently add local `hdc' variable to argument list of
23154 init_glyph_string. */
23155 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23156 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23157 #else
23158 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23159 init_glyph_string (s, char2b, w, row, area, start, hl)
23160 #endif
23161
23162 /* Add a glyph string for a stretch glyph to the list of strings
23163 between HEAD and TAIL. START is the index of the stretch glyph in
23164 row area AREA of glyph row ROW. END is the index of the last glyph
23165 in that glyph row area. X is the current output position assigned
23166 to the new glyph string constructed. HL overrides that face of the
23167 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23168 is the right-most x-position of the drawing area. */
23169
23170 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23171 and below -- keep them on one line. */
23172 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23173 do \
23174 { \
23175 s = alloca (sizeof *s); \
23176 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23177 START = fill_stretch_glyph_string (s, START, END); \
23178 append_glyph_string (&HEAD, &TAIL, s); \
23179 s->x = (X); \
23180 } \
23181 while (0)
23182
23183
23184 /* Add a glyph string for an image glyph to the list of strings
23185 between HEAD and TAIL. START is the index of the image glyph in
23186 row area AREA of glyph row ROW. END is the index of the last glyph
23187 in that glyph row area. X is the current output position assigned
23188 to the new glyph string constructed. HL overrides that face of the
23189 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23190 is the right-most x-position of the drawing area. */
23191
23192 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23193 do \
23194 { \
23195 s = alloca (sizeof *s); \
23196 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23197 fill_image_glyph_string (s); \
23198 append_glyph_string (&HEAD, &TAIL, s); \
23199 ++START; \
23200 s->x = (X); \
23201 } \
23202 while (0)
23203
23204
23205 /* Add a glyph string for a sequence of character glyphs to the list
23206 of strings between HEAD and TAIL. START is the index of the first
23207 glyph in row area AREA of glyph row ROW that is part of the new
23208 glyph string. END is the index of the last glyph in that glyph row
23209 area. X is the current output position assigned to the new glyph
23210 string constructed. HL overrides that face of the glyph; e.g. it
23211 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23212 right-most x-position of the drawing area. */
23213
23214 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23215 do \
23216 { \
23217 int face_id; \
23218 XChar2b *char2b; \
23219 \
23220 face_id = (row)->glyphs[area][START].face_id; \
23221 \
23222 s = alloca (sizeof *s); \
23223 char2b = alloca ((END - START) * sizeof *char2b); \
23224 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23225 append_glyph_string (&HEAD, &TAIL, s); \
23226 s->x = (X); \
23227 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23228 } \
23229 while (0)
23230
23231
23232 /* Add a glyph string for a composite sequence to the list of strings
23233 between HEAD and TAIL. START is the index of the first glyph in
23234 row area AREA of glyph row ROW that is part of the new glyph
23235 string. END is the index of the last glyph in that glyph row area.
23236 X is the current output position assigned to the new glyph string
23237 constructed. HL overrides that face of the glyph; e.g. it is
23238 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23239 x-position of the drawing area. */
23240
23241 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23242 do { \
23243 int face_id = (row)->glyphs[area][START].face_id; \
23244 struct face *base_face = FACE_FROM_ID (f, face_id); \
23245 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23246 struct composition *cmp = composition_table[cmp_id]; \
23247 XChar2b *char2b; \
23248 struct glyph_string *first_s = NULL; \
23249 int n; \
23250 \
23251 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23252 \
23253 /* Make glyph_strings for each glyph sequence that is drawable by \
23254 the same face, and append them to HEAD/TAIL. */ \
23255 for (n = 0; n < cmp->glyph_len;) \
23256 { \
23257 s = alloca (sizeof *s); \
23258 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23259 append_glyph_string (&(HEAD), &(TAIL), s); \
23260 s->cmp = cmp; \
23261 s->cmp_from = n; \
23262 s->x = (X); \
23263 if (n == 0) \
23264 first_s = s; \
23265 n = fill_composite_glyph_string (s, base_face, overlaps); \
23266 } \
23267 \
23268 ++START; \
23269 s = first_s; \
23270 } while (0)
23271
23272
23273 /* Add a glyph string for a glyph-string sequence to the list of strings
23274 between HEAD and TAIL. */
23275
23276 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23277 do { \
23278 int face_id; \
23279 XChar2b *char2b; \
23280 Lisp_Object gstring; \
23281 \
23282 face_id = (row)->glyphs[area][START].face_id; \
23283 gstring = (composition_gstring_from_id \
23284 ((row)->glyphs[area][START].u.cmp.id)); \
23285 s = alloca (sizeof *s); \
23286 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23287 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23288 append_glyph_string (&(HEAD), &(TAIL), s); \
23289 s->x = (X); \
23290 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23291 } while (0)
23292
23293
23294 /* Add a glyph string for a sequence of glyphless character's glyphs
23295 to the list of strings between HEAD and TAIL. The meanings of
23296 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23297
23298 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23299 do \
23300 { \
23301 int face_id; \
23302 \
23303 face_id = (row)->glyphs[area][START].face_id; \
23304 \
23305 s = alloca (sizeof *s); \
23306 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23307 append_glyph_string (&HEAD, &TAIL, s); \
23308 s->x = (X); \
23309 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23310 overlaps); \
23311 } \
23312 while (0)
23313
23314
23315 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23316 of AREA of glyph row ROW on window W between indices START and END.
23317 HL overrides the face for drawing glyph strings, e.g. it is
23318 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23319 x-positions of the drawing area.
23320
23321 This is an ugly monster macro construct because we must use alloca
23322 to allocate glyph strings (because draw_glyphs can be called
23323 asynchronously). */
23324
23325 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23326 do \
23327 { \
23328 HEAD = TAIL = NULL; \
23329 while (START < END) \
23330 { \
23331 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23332 switch (first_glyph->type) \
23333 { \
23334 case CHAR_GLYPH: \
23335 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23336 HL, X, LAST_X); \
23337 break; \
23338 \
23339 case COMPOSITE_GLYPH: \
23340 if (first_glyph->u.cmp.automatic) \
23341 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23342 HL, X, LAST_X); \
23343 else \
23344 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23345 HL, X, LAST_X); \
23346 break; \
23347 \
23348 case STRETCH_GLYPH: \
23349 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23350 HL, X, LAST_X); \
23351 break; \
23352 \
23353 case IMAGE_GLYPH: \
23354 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23355 HL, X, LAST_X); \
23356 break; \
23357 \
23358 case GLYPHLESS_GLYPH: \
23359 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23360 HL, X, LAST_X); \
23361 break; \
23362 \
23363 default: \
23364 emacs_abort (); \
23365 } \
23366 \
23367 if (s) \
23368 { \
23369 set_glyph_string_background_width (s, START, LAST_X); \
23370 (X) += s->width; \
23371 } \
23372 } \
23373 } while (0)
23374
23375
23376 /* Draw glyphs between START and END in AREA of ROW on window W,
23377 starting at x-position X. X is relative to AREA in W. HL is a
23378 face-override with the following meaning:
23379
23380 DRAW_NORMAL_TEXT draw normally
23381 DRAW_CURSOR draw in cursor face
23382 DRAW_MOUSE_FACE draw in mouse face.
23383 DRAW_INVERSE_VIDEO draw in mode line face
23384 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23385 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23386
23387 If OVERLAPS is non-zero, draw only the foreground of characters and
23388 clip to the physical height of ROW. Non-zero value also defines
23389 the overlapping part to be drawn:
23390
23391 OVERLAPS_PRED overlap with preceding rows
23392 OVERLAPS_SUCC overlap with succeeding rows
23393 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23394 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23395
23396 Value is the x-position reached, relative to AREA of W. */
23397
23398 static int
23399 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23400 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23401 enum draw_glyphs_face hl, int overlaps)
23402 {
23403 struct glyph_string *head, *tail;
23404 struct glyph_string *s;
23405 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23406 int i, j, x_reached, last_x, area_left = 0;
23407 struct frame *f = XFRAME (WINDOW_FRAME (w));
23408 DECLARE_HDC (hdc);
23409
23410 ALLOCATE_HDC (hdc, f);
23411
23412 /* Let's rather be paranoid than getting a SEGV. */
23413 end = min (end, row->used[area]);
23414 start = max (0, start);
23415 start = min (end, start);
23416
23417 /* Translate X to frame coordinates. Set last_x to the right
23418 end of the drawing area. */
23419 if (row->full_width_p)
23420 {
23421 /* X is relative to the left edge of W, without scroll bars
23422 or fringes. */
23423 area_left = WINDOW_LEFT_EDGE_X (w);
23424 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23425 }
23426 else
23427 {
23428 area_left = window_box_left (w, area);
23429 last_x = area_left + window_box_width (w, area);
23430 }
23431 x += area_left;
23432
23433 /* Build a doubly-linked list of glyph_string structures between
23434 head and tail from what we have to draw. Note that the macro
23435 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23436 the reason we use a separate variable `i'. */
23437 i = start;
23438 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23439 if (tail)
23440 x_reached = tail->x + tail->background_width;
23441 else
23442 x_reached = x;
23443
23444 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23445 the row, redraw some glyphs in front or following the glyph
23446 strings built above. */
23447 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23448 {
23449 struct glyph_string *h, *t;
23450 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23451 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23452 int check_mouse_face = 0;
23453 int dummy_x = 0;
23454
23455 /* If mouse highlighting is on, we may need to draw adjacent
23456 glyphs using mouse-face highlighting. */
23457 if (area == TEXT_AREA && row->mouse_face_p)
23458 {
23459 struct glyph_row *mouse_beg_row, *mouse_end_row;
23460
23461 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23462 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23463
23464 if (row >= mouse_beg_row && row <= mouse_end_row)
23465 {
23466 check_mouse_face = 1;
23467 mouse_beg_col = (row == mouse_beg_row)
23468 ? hlinfo->mouse_face_beg_col : 0;
23469 mouse_end_col = (row == mouse_end_row)
23470 ? hlinfo->mouse_face_end_col
23471 : row->used[TEXT_AREA];
23472 }
23473 }
23474
23475 /* Compute overhangs for all glyph strings. */
23476 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23477 for (s = head; s; s = s->next)
23478 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23479
23480 /* Prepend glyph strings for glyphs in front of the first glyph
23481 string that are overwritten because of the first glyph
23482 string's left overhang. The background of all strings
23483 prepended must be drawn because the first glyph string
23484 draws over it. */
23485 i = left_overwritten (head);
23486 if (i >= 0)
23487 {
23488 enum draw_glyphs_face overlap_hl;
23489
23490 /* If this row contains mouse highlighting, attempt to draw
23491 the overlapped glyphs with the correct highlight. This
23492 code fails if the overlap encompasses more than one glyph
23493 and mouse-highlight spans only some of these glyphs.
23494 However, making it work perfectly involves a lot more
23495 code, and I don't know if the pathological case occurs in
23496 practice, so we'll stick to this for now. --- cyd */
23497 if (check_mouse_face
23498 && mouse_beg_col < start && mouse_end_col > i)
23499 overlap_hl = DRAW_MOUSE_FACE;
23500 else
23501 overlap_hl = DRAW_NORMAL_TEXT;
23502
23503 j = i;
23504 BUILD_GLYPH_STRINGS (j, start, h, t,
23505 overlap_hl, dummy_x, last_x);
23506 start = i;
23507 compute_overhangs_and_x (t, head->x, 1);
23508 prepend_glyph_string_lists (&head, &tail, h, t);
23509 clip_head = head;
23510 }
23511
23512 /* Prepend glyph strings for glyphs in front of the first glyph
23513 string that overwrite that glyph string because of their
23514 right overhang. For these strings, only the foreground must
23515 be drawn, because it draws over the glyph string at `head'.
23516 The background must not be drawn because this would overwrite
23517 right overhangs of preceding glyphs for which no glyph
23518 strings exist. */
23519 i = left_overwriting (head);
23520 if (i >= 0)
23521 {
23522 enum draw_glyphs_face overlap_hl;
23523
23524 if (check_mouse_face
23525 && mouse_beg_col < start && mouse_end_col > i)
23526 overlap_hl = DRAW_MOUSE_FACE;
23527 else
23528 overlap_hl = DRAW_NORMAL_TEXT;
23529
23530 clip_head = head;
23531 BUILD_GLYPH_STRINGS (i, start, h, t,
23532 overlap_hl, dummy_x, last_x);
23533 for (s = h; s; s = s->next)
23534 s->background_filled_p = 1;
23535 compute_overhangs_and_x (t, head->x, 1);
23536 prepend_glyph_string_lists (&head, &tail, h, t);
23537 }
23538
23539 /* Append glyphs strings for glyphs following the last glyph
23540 string tail that are overwritten by tail. The background of
23541 these strings has to be drawn because tail's foreground draws
23542 over it. */
23543 i = right_overwritten (tail);
23544 if (i >= 0)
23545 {
23546 enum draw_glyphs_face overlap_hl;
23547
23548 if (check_mouse_face
23549 && mouse_beg_col < i && mouse_end_col > end)
23550 overlap_hl = DRAW_MOUSE_FACE;
23551 else
23552 overlap_hl = DRAW_NORMAL_TEXT;
23553
23554 BUILD_GLYPH_STRINGS (end, i, h, t,
23555 overlap_hl, x, last_x);
23556 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23557 we don't have `end = i;' here. */
23558 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23559 append_glyph_string_lists (&head, &tail, h, t);
23560 clip_tail = tail;
23561 }
23562
23563 /* Append glyph strings for glyphs following the last glyph
23564 string tail that overwrite tail. The foreground of such
23565 glyphs has to be drawn because it writes into the background
23566 of tail. The background must not be drawn because it could
23567 paint over the foreground of following glyphs. */
23568 i = right_overwriting (tail);
23569 if (i >= 0)
23570 {
23571 enum draw_glyphs_face overlap_hl;
23572 if (check_mouse_face
23573 && mouse_beg_col < i && mouse_end_col > end)
23574 overlap_hl = DRAW_MOUSE_FACE;
23575 else
23576 overlap_hl = DRAW_NORMAL_TEXT;
23577
23578 clip_tail = tail;
23579 i++; /* We must include the Ith glyph. */
23580 BUILD_GLYPH_STRINGS (end, i, h, t,
23581 overlap_hl, x, last_x);
23582 for (s = h; s; s = s->next)
23583 s->background_filled_p = 1;
23584 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23585 append_glyph_string_lists (&head, &tail, h, t);
23586 }
23587 if (clip_head || clip_tail)
23588 for (s = head; s; s = s->next)
23589 {
23590 s->clip_head = clip_head;
23591 s->clip_tail = clip_tail;
23592 }
23593 }
23594
23595 /* Draw all strings. */
23596 for (s = head; s; s = s->next)
23597 FRAME_RIF (f)->draw_glyph_string (s);
23598
23599 #ifndef HAVE_NS
23600 /* When focus a sole frame and move horizontally, this sets on_p to 0
23601 causing a failure to erase prev cursor position. */
23602 if (area == TEXT_AREA
23603 && !row->full_width_p
23604 /* When drawing overlapping rows, only the glyph strings'
23605 foreground is drawn, which doesn't erase a cursor
23606 completely. */
23607 && !overlaps)
23608 {
23609 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23610 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23611 : (tail ? tail->x + tail->background_width : x));
23612 x0 -= area_left;
23613 x1 -= area_left;
23614
23615 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23616 row->y, MATRIX_ROW_BOTTOM_Y (row));
23617 }
23618 #endif
23619
23620 /* Value is the x-position up to which drawn, relative to AREA of W.
23621 This doesn't include parts drawn because of overhangs. */
23622 if (row->full_width_p)
23623 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23624 else
23625 x_reached -= area_left;
23626
23627 RELEASE_HDC (hdc, f);
23628
23629 return x_reached;
23630 }
23631
23632 /* Expand row matrix if too narrow. Don't expand if area
23633 is not present. */
23634
23635 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23636 { \
23637 if (!fonts_changed_p \
23638 && (it->glyph_row->glyphs[area] \
23639 < it->glyph_row->glyphs[area + 1])) \
23640 { \
23641 it->w->ncols_scale_factor++; \
23642 fonts_changed_p = 1; \
23643 } \
23644 }
23645
23646 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23647 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23648
23649 static inline void
23650 append_glyph (struct it *it)
23651 {
23652 struct glyph *glyph;
23653 enum glyph_row_area area = it->area;
23654
23655 eassert (it->glyph_row);
23656 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23657
23658 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23659 if (glyph < it->glyph_row->glyphs[area + 1])
23660 {
23661 /* If the glyph row is reversed, we need to prepend the glyph
23662 rather than append it. */
23663 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23664 {
23665 struct glyph *g;
23666
23667 /* Make room for the additional glyph. */
23668 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23669 g[1] = *g;
23670 glyph = it->glyph_row->glyphs[area];
23671 }
23672 glyph->charpos = CHARPOS (it->position);
23673 glyph->object = it->object;
23674 if (it->pixel_width > 0)
23675 {
23676 glyph->pixel_width = it->pixel_width;
23677 glyph->padding_p = 0;
23678 }
23679 else
23680 {
23681 /* Assure at least 1-pixel width. Otherwise, cursor can't
23682 be displayed correctly. */
23683 glyph->pixel_width = 1;
23684 glyph->padding_p = 1;
23685 }
23686 glyph->ascent = it->ascent;
23687 glyph->descent = it->descent;
23688 glyph->voffset = it->voffset;
23689 glyph->type = CHAR_GLYPH;
23690 glyph->avoid_cursor_p = it->avoid_cursor_p;
23691 glyph->multibyte_p = it->multibyte_p;
23692 glyph->left_box_line_p = it->start_of_box_run_p;
23693 glyph->right_box_line_p = it->end_of_box_run_p;
23694 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23695 || it->phys_descent > it->descent);
23696 glyph->glyph_not_available_p = it->glyph_not_available_p;
23697 glyph->face_id = it->face_id;
23698 glyph->u.ch = it->char_to_display;
23699 glyph->slice.img = null_glyph_slice;
23700 glyph->font_type = FONT_TYPE_UNKNOWN;
23701 if (it->bidi_p)
23702 {
23703 glyph->resolved_level = it->bidi_it.resolved_level;
23704 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23705 emacs_abort ();
23706 glyph->bidi_type = it->bidi_it.type;
23707 }
23708 else
23709 {
23710 glyph->resolved_level = 0;
23711 glyph->bidi_type = UNKNOWN_BT;
23712 }
23713 ++it->glyph_row->used[area];
23714 }
23715 else
23716 IT_EXPAND_MATRIX_WIDTH (it, area);
23717 }
23718
23719 /* Store one glyph for the composition IT->cmp_it.id in
23720 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23721 non-null. */
23722
23723 static inline void
23724 append_composite_glyph (struct it *it)
23725 {
23726 struct glyph *glyph;
23727 enum glyph_row_area area = it->area;
23728
23729 eassert (it->glyph_row);
23730
23731 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23732 if (glyph < it->glyph_row->glyphs[area + 1])
23733 {
23734 /* If the glyph row is reversed, we need to prepend the glyph
23735 rather than append it. */
23736 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23737 {
23738 struct glyph *g;
23739
23740 /* Make room for the new glyph. */
23741 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23742 g[1] = *g;
23743 glyph = it->glyph_row->glyphs[it->area];
23744 }
23745 glyph->charpos = it->cmp_it.charpos;
23746 glyph->object = it->object;
23747 glyph->pixel_width = it->pixel_width;
23748 glyph->ascent = it->ascent;
23749 glyph->descent = it->descent;
23750 glyph->voffset = it->voffset;
23751 glyph->type = COMPOSITE_GLYPH;
23752 if (it->cmp_it.ch < 0)
23753 {
23754 glyph->u.cmp.automatic = 0;
23755 glyph->u.cmp.id = it->cmp_it.id;
23756 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23757 }
23758 else
23759 {
23760 glyph->u.cmp.automatic = 1;
23761 glyph->u.cmp.id = it->cmp_it.id;
23762 glyph->slice.cmp.from = it->cmp_it.from;
23763 glyph->slice.cmp.to = it->cmp_it.to - 1;
23764 }
23765 glyph->avoid_cursor_p = it->avoid_cursor_p;
23766 glyph->multibyte_p = it->multibyte_p;
23767 glyph->left_box_line_p = it->start_of_box_run_p;
23768 glyph->right_box_line_p = it->end_of_box_run_p;
23769 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23770 || it->phys_descent > it->descent);
23771 glyph->padding_p = 0;
23772 glyph->glyph_not_available_p = 0;
23773 glyph->face_id = it->face_id;
23774 glyph->font_type = FONT_TYPE_UNKNOWN;
23775 if (it->bidi_p)
23776 {
23777 glyph->resolved_level = it->bidi_it.resolved_level;
23778 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23779 emacs_abort ();
23780 glyph->bidi_type = it->bidi_it.type;
23781 }
23782 ++it->glyph_row->used[area];
23783 }
23784 else
23785 IT_EXPAND_MATRIX_WIDTH (it, area);
23786 }
23787
23788
23789 /* Change IT->ascent and IT->height according to the setting of
23790 IT->voffset. */
23791
23792 static inline void
23793 take_vertical_position_into_account (struct it *it)
23794 {
23795 if (it->voffset)
23796 {
23797 if (it->voffset < 0)
23798 /* Increase the ascent so that we can display the text higher
23799 in the line. */
23800 it->ascent -= it->voffset;
23801 else
23802 /* Increase the descent so that we can display the text lower
23803 in the line. */
23804 it->descent += it->voffset;
23805 }
23806 }
23807
23808
23809 /* Produce glyphs/get display metrics for the image IT is loaded with.
23810 See the description of struct display_iterator in dispextern.h for
23811 an overview of struct display_iterator. */
23812
23813 static void
23814 produce_image_glyph (struct it *it)
23815 {
23816 struct image *img;
23817 struct face *face;
23818 int glyph_ascent, crop;
23819 struct glyph_slice slice;
23820
23821 eassert (it->what == IT_IMAGE);
23822
23823 face = FACE_FROM_ID (it->f, it->face_id);
23824 eassert (face);
23825 /* Make sure X resources of the face is loaded. */
23826 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23827
23828 if (it->image_id < 0)
23829 {
23830 /* Fringe bitmap. */
23831 it->ascent = it->phys_ascent = 0;
23832 it->descent = it->phys_descent = 0;
23833 it->pixel_width = 0;
23834 it->nglyphs = 0;
23835 return;
23836 }
23837
23838 img = IMAGE_FROM_ID (it->f, it->image_id);
23839 eassert (img);
23840 /* Make sure X resources of the image is loaded. */
23841 prepare_image_for_display (it->f, img);
23842
23843 slice.x = slice.y = 0;
23844 slice.width = img->width;
23845 slice.height = img->height;
23846
23847 if (INTEGERP (it->slice.x))
23848 slice.x = XINT (it->slice.x);
23849 else if (FLOATP (it->slice.x))
23850 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23851
23852 if (INTEGERP (it->slice.y))
23853 slice.y = XINT (it->slice.y);
23854 else if (FLOATP (it->slice.y))
23855 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23856
23857 if (INTEGERP (it->slice.width))
23858 slice.width = XINT (it->slice.width);
23859 else if (FLOATP (it->slice.width))
23860 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23861
23862 if (INTEGERP (it->slice.height))
23863 slice.height = XINT (it->slice.height);
23864 else if (FLOATP (it->slice.height))
23865 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23866
23867 if (slice.x >= img->width)
23868 slice.x = img->width;
23869 if (slice.y >= img->height)
23870 slice.y = img->height;
23871 if (slice.x + slice.width >= img->width)
23872 slice.width = img->width - slice.x;
23873 if (slice.y + slice.height > img->height)
23874 slice.height = img->height - slice.y;
23875
23876 if (slice.width == 0 || slice.height == 0)
23877 return;
23878
23879 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23880
23881 it->descent = slice.height - glyph_ascent;
23882 if (slice.y == 0)
23883 it->descent += img->vmargin;
23884 if (slice.y + slice.height == img->height)
23885 it->descent += img->vmargin;
23886 it->phys_descent = it->descent;
23887
23888 it->pixel_width = slice.width;
23889 if (slice.x == 0)
23890 it->pixel_width += img->hmargin;
23891 if (slice.x + slice.width == img->width)
23892 it->pixel_width += img->hmargin;
23893
23894 /* It's quite possible for images to have an ascent greater than
23895 their height, so don't get confused in that case. */
23896 if (it->descent < 0)
23897 it->descent = 0;
23898
23899 it->nglyphs = 1;
23900
23901 if (face->box != FACE_NO_BOX)
23902 {
23903 if (face->box_line_width > 0)
23904 {
23905 if (slice.y == 0)
23906 it->ascent += face->box_line_width;
23907 if (slice.y + slice.height == img->height)
23908 it->descent += face->box_line_width;
23909 }
23910
23911 if (it->start_of_box_run_p && slice.x == 0)
23912 it->pixel_width += eabs (face->box_line_width);
23913 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23914 it->pixel_width += eabs (face->box_line_width);
23915 }
23916
23917 take_vertical_position_into_account (it);
23918
23919 /* Automatically crop wide image glyphs at right edge so we can
23920 draw the cursor on same display row. */
23921 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23922 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23923 {
23924 it->pixel_width -= crop;
23925 slice.width -= crop;
23926 }
23927
23928 if (it->glyph_row)
23929 {
23930 struct glyph *glyph;
23931 enum glyph_row_area area = it->area;
23932
23933 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23934 if (glyph < it->glyph_row->glyphs[area + 1])
23935 {
23936 glyph->charpos = CHARPOS (it->position);
23937 glyph->object = it->object;
23938 glyph->pixel_width = it->pixel_width;
23939 glyph->ascent = glyph_ascent;
23940 glyph->descent = it->descent;
23941 glyph->voffset = it->voffset;
23942 glyph->type = IMAGE_GLYPH;
23943 glyph->avoid_cursor_p = it->avoid_cursor_p;
23944 glyph->multibyte_p = it->multibyte_p;
23945 glyph->left_box_line_p = it->start_of_box_run_p;
23946 glyph->right_box_line_p = it->end_of_box_run_p;
23947 glyph->overlaps_vertically_p = 0;
23948 glyph->padding_p = 0;
23949 glyph->glyph_not_available_p = 0;
23950 glyph->face_id = it->face_id;
23951 glyph->u.img_id = img->id;
23952 glyph->slice.img = slice;
23953 glyph->font_type = FONT_TYPE_UNKNOWN;
23954 if (it->bidi_p)
23955 {
23956 glyph->resolved_level = it->bidi_it.resolved_level;
23957 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23958 emacs_abort ();
23959 glyph->bidi_type = it->bidi_it.type;
23960 }
23961 ++it->glyph_row->used[area];
23962 }
23963 else
23964 IT_EXPAND_MATRIX_WIDTH (it, area);
23965 }
23966 }
23967
23968
23969 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23970 of the glyph, WIDTH and HEIGHT are the width and height of the
23971 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23972
23973 static void
23974 append_stretch_glyph (struct it *it, Lisp_Object object,
23975 int width, int height, int ascent)
23976 {
23977 struct glyph *glyph;
23978 enum glyph_row_area area = it->area;
23979
23980 eassert (ascent >= 0 && ascent <= height);
23981
23982 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23983 if (glyph < it->glyph_row->glyphs[area + 1])
23984 {
23985 /* If the glyph row is reversed, we need to prepend the glyph
23986 rather than append it. */
23987 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23988 {
23989 struct glyph *g;
23990
23991 /* Make room for the additional glyph. */
23992 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23993 g[1] = *g;
23994 glyph = it->glyph_row->glyphs[area];
23995 }
23996 glyph->charpos = CHARPOS (it->position);
23997 glyph->object = object;
23998 glyph->pixel_width = width;
23999 glyph->ascent = ascent;
24000 glyph->descent = height - ascent;
24001 glyph->voffset = it->voffset;
24002 glyph->type = STRETCH_GLYPH;
24003 glyph->avoid_cursor_p = it->avoid_cursor_p;
24004 glyph->multibyte_p = it->multibyte_p;
24005 glyph->left_box_line_p = it->start_of_box_run_p;
24006 glyph->right_box_line_p = it->end_of_box_run_p;
24007 glyph->overlaps_vertically_p = 0;
24008 glyph->padding_p = 0;
24009 glyph->glyph_not_available_p = 0;
24010 glyph->face_id = it->face_id;
24011 glyph->u.stretch.ascent = ascent;
24012 glyph->u.stretch.height = height;
24013 glyph->slice.img = null_glyph_slice;
24014 glyph->font_type = FONT_TYPE_UNKNOWN;
24015 if (it->bidi_p)
24016 {
24017 glyph->resolved_level = it->bidi_it.resolved_level;
24018 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24019 emacs_abort ();
24020 glyph->bidi_type = it->bidi_it.type;
24021 }
24022 else
24023 {
24024 glyph->resolved_level = 0;
24025 glyph->bidi_type = UNKNOWN_BT;
24026 }
24027 ++it->glyph_row->used[area];
24028 }
24029 else
24030 IT_EXPAND_MATRIX_WIDTH (it, area);
24031 }
24032
24033 #endif /* HAVE_WINDOW_SYSTEM */
24034
24035 /* Produce a stretch glyph for iterator IT. IT->object is the value
24036 of the glyph property displayed. The value must be a list
24037 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24038 being recognized:
24039
24040 1. `:width WIDTH' specifies that the space should be WIDTH *
24041 canonical char width wide. WIDTH may be an integer or floating
24042 point number.
24043
24044 2. `:relative-width FACTOR' specifies that the width of the stretch
24045 should be computed from the width of the first character having the
24046 `glyph' property, and should be FACTOR times that width.
24047
24048 3. `:align-to HPOS' specifies that the space should be wide enough
24049 to reach HPOS, a value in canonical character units.
24050
24051 Exactly one of the above pairs must be present.
24052
24053 4. `:height HEIGHT' specifies that the height of the stretch produced
24054 should be HEIGHT, measured in canonical character units.
24055
24056 5. `:relative-height FACTOR' specifies that the height of the
24057 stretch should be FACTOR times the height of the characters having
24058 the glyph property.
24059
24060 Either none or exactly one of 4 or 5 must be present.
24061
24062 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24063 of the stretch should be used for the ascent of the stretch.
24064 ASCENT must be in the range 0 <= ASCENT <= 100. */
24065
24066 void
24067 produce_stretch_glyph (struct it *it)
24068 {
24069 /* (space :width WIDTH :height HEIGHT ...) */
24070 Lisp_Object prop, plist;
24071 int width = 0, height = 0, align_to = -1;
24072 int zero_width_ok_p = 0;
24073 int ascent = 0;
24074 double tem;
24075 struct face *face = NULL;
24076 struct font *font = NULL;
24077
24078 #ifdef HAVE_WINDOW_SYSTEM
24079 int zero_height_ok_p = 0;
24080
24081 if (FRAME_WINDOW_P (it->f))
24082 {
24083 face = FACE_FROM_ID (it->f, it->face_id);
24084 font = face->font ? face->font : FRAME_FONT (it->f);
24085 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24086 }
24087 #endif
24088
24089 /* List should start with `space'. */
24090 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24091 plist = XCDR (it->object);
24092
24093 /* Compute the width of the stretch. */
24094 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24095 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24096 {
24097 /* Absolute width `:width WIDTH' specified and valid. */
24098 zero_width_ok_p = 1;
24099 width = (int)tem;
24100 }
24101 #ifdef HAVE_WINDOW_SYSTEM
24102 else if (FRAME_WINDOW_P (it->f)
24103 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24104 {
24105 /* Relative width `:relative-width FACTOR' specified and valid.
24106 Compute the width of the characters having the `glyph'
24107 property. */
24108 struct it it2;
24109 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24110
24111 it2 = *it;
24112 if (it->multibyte_p)
24113 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24114 else
24115 {
24116 it2.c = it2.char_to_display = *p, it2.len = 1;
24117 if (! ASCII_CHAR_P (it2.c))
24118 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24119 }
24120
24121 it2.glyph_row = NULL;
24122 it2.what = IT_CHARACTER;
24123 x_produce_glyphs (&it2);
24124 width = NUMVAL (prop) * it2.pixel_width;
24125 }
24126 #endif /* HAVE_WINDOW_SYSTEM */
24127 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24128 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24129 {
24130 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24131 align_to = (align_to < 0
24132 ? 0
24133 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24134 else if (align_to < 0)
24135 align_to = window_box_left_offset (it->w, TEXT_AREA);
24136 width = max (0, (int)tem + align_to - it->current_x);
24137 zero_width_ok_p = 1;
24138 }
24139 else
24140 /* Nothing specified -> width defaults to canonical char width. */
24141 width = FRAME_COLUMN_WIDTH (it->f);
24142
24143 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24144 width = 1;
24145
24146 #ifdef HAVE_WINDOW_SYSTEM
24147 /* Compute height. */
24148 if (FRAME_WINDOW_P (it->f))
24149 {
24150 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24151 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24152 {
24153 height = (int)tem;
24154 zero_height_ok_p = 1;
24155 }
24156 else if (prop = Fplist_get (plist, QCrelative_height),
24157 NUMVAL (prop) > 0)
24158 height = FONT_HEIGHT (font) * NUMVAL (prop);
24159 else
24160 height = FONT_HEIGHT (font);
24161
24162 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24163 height = 1;
24164
24165 /* Compute percentage of height used for ascent. If
24166 `:ascent ASCENT' is present and valid, use that. Otherwise,
24167 derive the ascent from the font in use. */
24168 if (prop = Fplist_get (plist, QCascent),
24169 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24170 ascent = height * NUMVAL (prop) / 100.0;
24171 else if (!NILP (prop)
24172 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24173 ascent = min (max (0, (int)tem), height);
24174 else
24175 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24176 }
24177 else
24178 #endif /* HAVE_WINDOW_SYSTEM */
24179 height = 1;
24180
24181 if (width > 0 && it->line_wrap != TRUNCATE
24182 && it->current_x + width > it->last_visible_x)
24183 {
24184 width = it->last_visible_x - it->current_x;
24185 #ifdef HAVE_WINDOW_SYSTEM
24186 /* Subtract one more pixel from the stretch width, but only on
24187 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24188 width -= FRAME_WINDOW_P (it->f);
24189 #endif
24190 }
24191
24192 if (width > 0 && height > 0 && it->glyph_row)
24193 {
24194 Lisp_Object o_object = it->object;
24195 Lisp_Object object = it->stack[it->sp - 1].string;
24196 int n = width;
24197
24198 if (!STRINGP (object))
24199 object = it->w->buffer;
24200 #ifdef HAVE_WINDOW_SYSTEM
24201 if (FRAME_WINDOW_P (it->f))
24202 append_stretch_glyph (it, object, width, height, ascent);
24203 else
24204 #endif
24205 {
24206 it->object = object;
24207 it->char_to_display = ' ';
24208 it->pixel_width = it->len = 1;
24209 while (n--)
24210 tty_append_glyph (it);
24211 it->object = o_object;
24212 }
24213 }
24214
24215 it->pixel_width = width;
24216 #ifdef HAVE_WINDOW_SYSTEM
24217 if (FRAME_WINDOW_P (it->f))
24218 {
24219 it->ascent = it->phys_ascent = ascent;
24220 it->descent = it->phys_descent = height - it->ascent;
24221 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24222 take_vertical_position_into_account (it);
24223 }
24224 else
24225 #endif
24226 it->nglyphs = width;
24227 }
24228
24229 /* Get information about special display element WHAT in an
24230 environment described by IT. WHAT is one of IT_TRUNCATION or
24231 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24232 non-null glyph_row member. This function ensures that fields like
24233 face_id, c, len of IT are left untouched. */
24234
24235 static void
24236 produce_special_glyphs (struct it *it, enum display_element_type what)
24237 {
24238 struct it temp_it;
24239 Lisp_Object gc;
24240 GLYPH glyph;
24241
24242 temp_it = *it;
24243 temp_it.object = make_number (0);
24244 memset (&temp_it.current, 0, sizeof temp_it.current);
24245
24246 if (what == IT_CONTINUATION)
24247 {
24248 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24249 if (it->bidi_it.paragraph_dir == R2L)
24250 SET_GLYPH_FROM_CHAR (glyph, '/');
24251 else
24252 SET_GLYPH_FROM_CHAR (glyph, '\\');
24253 if (it->dp
24254 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24255 {
24256 /* FIXME: Should we mirror GC for R2L lines? */
24257 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24258 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24259 }
24260 }
24261 else if (what == IT_TRUNCATION)
24262 {
24263 /* Truncation glyph. */
24264 SET_GLYPH_FROM_CHAR (glyph, '$');
24265 if (it->dp
24266 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24267 {
24268 /* FIXME: Should we mirror GC for R2L lines? */
24269 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24270 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24271 }
24272 }
24273 else
24274 emacs_abort ();
24275
24276 #ifdef HAVE_WINDOW_SYSTEM
24277 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24278 is turned off, we precede the truncation/continuation glyphs by a
24279 stretch glyph whose width is computed such that these special
24280 glyphs are aligned at the window margin, even when very different
24281 fonts are used in different glyph rows. */
24282 if (FRAME_WINDOW_P (temp_it.f)
24283 /* init_iterator calls this with it->glyph_row == NULL, and it
24284 wants only the pixel width of the truncation/continuation
24285 glyphs. */
24286 && temp_it.glyph_row
24287 /* insert_left_trunc_glyphs calls us at the beginning of the
24288 row, and it has its own calculation of the stretch glyph
24289 width. */
24290 && temp_it.glyph_row->used[TEXT_AREA] > 0
24291 && (temp_it.glyph_row->reversed_p
24292 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24293 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24294 {
24295 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24296
24297 if (stretch_width > 0)
24298 {
24299 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24300 struct font *font =
24301 face->font ? face->font : FRAME_FONT (temp_it.f);
24302 int stretch_ascent =
24303 (((temp_it.ascent + temp_it.descent)
24304 * FONT_BASE (font)) / FONT_HEIGHT (font));
24305
24306 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24307 temp_it.ascent + temp_it.descent,
24308 stretch_ascent);
24309 }
24310 }
24311 #endif
24312
24313 temp_it.dp = NULL;
24314 temp_it.what = IT_CHARACTER;
24315 temp_it.len = 1;
24316 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24317 temp_it.face_id = GLYPH_FACE (glyph);
24318 temp_it.len = CHAR_BYTES (temp_it.c);
24319
24320 PRODUCE_GLYPHS (&temp_it);
24321 it->pixel_width = temp_it.pixel_width;
24322 it->nglyphs = temp_it.pixel_width;
24323 }
24324
24325 #ifdef HAVE_WINDOW_SYSTEM
24326
24327 /* Calculate line-height and line-spacing properties.
24328 An integer value specifies explicit pixel value.
24329 A float value specifies relative value to current face height.
24330 A cons (float . face-name) specifies relative value to
24331 height of specified face font.
24332
24333 Returns height in pixels, or nil. */
24334
24335
24336 static Lisp_Object
24337 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24338 int boff, int override)
24339 {
24340 Lisp_Object face_name = Qnil;
24341 int ascent, descent, height;
24342
24343 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24344 return val;
24345
24346 if (CONSP (val))
24347 {
24348 face_name = XCAR (val);
24349 val = XCDR (val);
24350 if (!NUMBERP (val))
24351 val = make_number (1);
24352 if (NILP (face_name))
24353 {
24354 height = it->ascent + it->descent;
24355 goto scale;
24356 }
24357 }
24358
24359 if (NILP (face_name))
24360 {
24361 font = FRAME_FONT (it->f);
24362 boff = FRAME_BASELINE_OFFSET (it->f);
24363 }
24364 else if (EQ (face_name, Qt))
24365 {
24366 override = 0;
24367 }
24368 else
24369 {
24370 int face_id;
24371 struct face *face;
24372
24373 face_id = lookup_named_face (it->f, face_name, 0);
24374 if (face_id < 0)
24375 return make_number (-1);
24376
24377 face = FACE_FROM_ID (it->f, face_id);
24378 font = face->font;
24379 if (font == NULL)
24380 return make_number (-1);
24381 boff = font->baseline_offset;
24382 if (font->vertical_centering)
24383 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24384 }
24385
24386 ascent = FONT_BASE (font) + boff;
24387 descent = FONT_DESCENT (font) - boff;
24388
24389 if (override)
24390 {
24391 it->override_ascent = ascent;
24392 it->override_descent = descent;
24393 it->override_boff = boff;
24394 }
24395
24396 height = ascent + descent;
24397
24398 scale:
24399 if (FLOATP (val))
24400 height = (int)(XFLOAT_DATA (val) * height);
24401 else if (INTEGERP (val))
24402 height *= XINT (val);
24403
24404 return make_number (height);
24405 }
24406
24407
24408 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24409 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24410 and only if this is for a character for which no font was found.
24411
24412 If the display method (it->glyphless_method) is
24413 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24414 length of the acronym or the hexadecimal string, UPPER_XOFF and
24415 UPPER_YOFF are pixel offsets for the upper part of the string,
24416 LOWER_XOFF and LOWER_YOFF are for the lower part.
24417
24418 For the other display methods, LEN through LOWER_YOFF are zero. */
24419
24420 static void
24421 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24422 short upper_xoff, short upper_yoff,
24423 short lower_xoff, short lower_yoff)
24424 {
24425 struct glyph *glyph;
24426 enum glyph_row_area area = it->area;
24427
24428 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24429 if (glyph < it->glyph_row->glyphs[area + 1])
24430 {
24431 /* If the glyph row is reversed, we need to prepend the glyph
24432 rather than append it. */
24433 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24434 {
24435 struct glyph *g;
24436
24437 /* Make room for the additional glyph. */
24438 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24439 g[1] = *g;
24440 glyph = it->glyph_row->glyphs[area];
24441 }
24442 glyph->charpos = CHARPOS (it->position);
24443 glyph->object = it->object;
24444 glyph->pixel_width = it->pixel_width;
24445 glyph->ascent = it->ascent;
24446 glyph->descent = it->descent;
24447 glyph->voffset = it->voffset;
24448 glyph->type = GLYPHLESS_GLYPH;
24449 glyph->u.glyphless.method = it->glyphless_method;
24450 glyph->u.glyphless.for_no_font = for_no_font;
24451 glyph->u.glyphless.len = len;
24452 glyph->u.glyphless.ch = it->c;
24453 glyph->slice.glyphless.upper_xoff = upper_xoff;
24454 glyph->slice.glyphless.upper_yoff = upper_yoff;
24455 glyph->slice.glyphless.lower_xoff = lower_xoff;
24456 glyph->slice.glyphless.lower_yoff = lower_yoff;
24457 glyph->avoid_cursor_p = it->avoid_cursor_p;
24458 glyph->multibyte_p = it->multibyte_p;
24459 glyph->left_box_line_p = it->start_of_box_run_p;
24460 glyph->right_box_line_p = it->end_of_box_run_p;
24461 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24462 || it->phys_descent > it->descent);
24463 glyph->padding_p = 0;
24464 glyph->glyph_not_available_p = 0;
24465 glyph->face_id = face_id;
24466 glyph->font_type = FONT_TYPE_UNKNOWN;
24467 if (it->bidi_p)
24468 {
24469 glyph->resolved_level = it->bidi_it.resolved_level;
24470 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24471 emacs_abort ();
24472 glyph->bidi_type = it->bidi_it.type;
24473 }
24474 ++it->glyph_row->used[area];
24475 }
24476 else
24477 IT_EXPAND_MATRIX_WIDTH (it, area);
24478 }
24479
24480
24481 /* Produce a glyph for a glyphless character for iterator IT.
24482 IT->glyphless_method specifies which method to use for displaying
24483 the character. See the description of enum
24484 glyphless_display_method in dispextern.h for the detail.
24485
24486 FOR_NO_FONT is nonzero if and only if this is for a character for
24487 which no font was found. ACRONYM, if non-nil, is an acronym string
24488 for the character. */
24489
24490 static void
24491 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24492 {
24493 int face_id;
24494 struct face *face;
24495 struct font *font;
24496 int base_width, base_height, width, height;
24497 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24498 int len;
24499
24500 /* Get the metrics of the base font. We always refer to the current
24501 ASCII face. */
24502 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24503 font = face->font ? face->font : FRAME_FONT (it->f);
24504 it->ascent = FONT_BASE (font) + font->baseline_offset;
24505 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24506 base_height = it->ascent + it->descent;
24507 base_width = font->average_width;
24508
24509 /* Get a face ID for the glyph by utilizing a cache (the same way as
24510 done for `escape-glyph' in get_next_display_element). */
24511 if (it->f == last_glyphless_glyph_frame
24512 && it->face_id == last_glyphless_glyph_face_id)
24513 {
24514 face_id = last_glyphless_glyph_merged_face_id;
24515 }
24516 else
24517 {
24518 /* Merge the `glyphless-char' face into the current face. */
24519 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24520 last_glyphless_glyph_frame = it->f;
24521 last_glyphless_glyph_face_id = it->face_id;
24522 last_glyphless_glyph_merged_face_id = face_id;
24523 }
24524
24525 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24526 {
24527 it->pixel_width = THIN_SPACE_WIDTH;
24528 len = 0;
24529 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24530 }
24531 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24532 {
24533 width = CHAR_WIDTH (it->c);
24534 if (width == 0)
24535 width = 1;
24536 else if (width > 4)
24537 width = 4;
24538 it->pixel_width = base_width * width;
24539 len = 0;
24540 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24541 }
24542 else
24543 {
24544 char buf[7];
24545 const char *str;
24546 unsigned int code[6];
24547 int upper_len;
24548 int ascent, descent;
24549 struct font_metrics metrics_upper, metrics_lower;
24550
24551 face = FACE_FROM_ID (it->f, face_id);
24552 font = face->font ? face->font : FRAME_FONT (it->f);
24553 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24554
24555 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24556 {
24557 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24558 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24559 if (CONSP (acronym))
24560 acronym = XCAR (acronym);
24561 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24562 }
24563 else
24564 {
24565 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24566 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24567 str = buf;
24568 }
24569 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24570 code[len] = font->driver->encode_char (font, str[len]);
24571 upper_len = (len + 1) / 2;
24572 font->driver->text_extents (font, code, upper_len,
24573 &metrics_upper);
24574 font->driver->text_extents (font, code + upper_len, len - upper_len,
24575 &metrics_lower);
24576
24577
24578
24579 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24580 width = max (metrics_upper.width, metrics_lower.width) + 4;
24581 upper_xoff = upper_yoff = 2; /* the typical case */
24582 if (base_width >= width)
24583 {
24584 /* Align the upper to the left, the lower to the right. */
24585 it->pixel_width = base_width;
24586 lower_xoff = base_width - 2 - metrics_lower.width;
24587 }
24588 else
24589 {
24590 /* Center the shorter one. */
24591 it->pixel_width = width;
24592 if (metrics_upper.width >= metrics_lower.width)
24593 lower_xoff = (width - metrics_lower.width) / 2;
24594 else
24595 {
24596 /* FIXME: This code doesn't look right. It formerly was
24597 missing the "lower_xoff = 0;", which couldn't have
24598 been right since it left lower_xoff uninitialized. */
24599 lower_xoff = 0;
24600 upper_xoff = (width - metrics_upper.width) / 2;
24601 }
24602 }
24603
24604 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24605 top, bottom, and between upper and lower strings. */
24606 height = (metrics_upper.ascent + metrics_upper.descent
24607 + metrics_lower.ascent + metrics_lower.descent) + 5;
24608 /* Center vertically.
24609 H:base_height, D:base_descent
24610 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24611
24612 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24613 descent = D - H/2 + h/2;
24614 lower_yoff = descent - 2 - ld;
24615 upper_yoff = lower_yoff - la - 1 - ud; */
24616 ascent = - (it->descent - (base_height + height + 1) / 2);
24617 descent = it->descent - (base_height - height) / 2;
24618 lower_yoff = descent - 2 - metrics_lower.descent;
24619 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24620 - metrics_upper.descent);
24621 /* Don't make the height shorter than the base height. */
24622 if (height > base_height)
24623 {
24624 it->ascent = ascent;
24625 it->descent = descent;
24626 }
24627 }
24628
24629 it->phys_ascent = it->ascent;
24630 it->phys_descent = it->descent;
24631 if (it->glyph_row)
24632 append_glyphless_glyph (it, face_id, for_no_font, len,
24633 upper_xoff, upper_yoff,
24634 lower_xoff, lower_yoff);
24635 it->nglyphs = 1;
24636 take_vertical_position_into_account (it);
24637 }
24638
24639
24640 /* RIF:
24641 Produce glyphs/get display metrics for the display element IT is
24642 loaded with. See the description of struct it in dispextern.h
24643 for an overview of struct it. */
24644
24645 void
24646 x_produce_glyphs (struct it *it)
24647 {
24648 int extra_line_spacing = it->extra_line_spacing;
24649
24650 it->glyph_not_available_p = 0;
24651
24652 if (it->what == IT_CHARACTER)
24653 {
24654 XChar2b char2b;
24655 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24656 struct font *font = face->font;
24657 struct font_metrics *pcm = NULL;
24658 int boff; /* baseline offset */
24659
24660 if (font == NULL)
24661 {
24662 /* When no suitable font is found, display this character by
24663 the method specified in the first extra slot of
24664 Vglyphless_char_display. */
24665 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24666
24667 eassert (it->what == IT_GLYPHLESS);
24668 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24669 goto done;
24670 }
24671
24672 boff = font->baseline_offset;
24673 if (font->vertical_centering)
24674 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24675
24676 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24677 {
24678 int stretched_p;
24679
24680 it->nglyphs = 1;
24681
24682 if (it->override_ascent >= 0)
24683 {
24684 it->ascent = it->override_ascent;
24685 it->descent = it->override_descent;
24686 boff = it->override_boff;
24687 }
24688 else
24689 {
24690 it->ascent = FONT_BASE (font) + boff;
24691 it->descent = FONT_DESCENT (font) - boff;
24692 }
24693
24694 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24695 {
24696 pcm = get_per_char_metric (font, &char2b);
24697 if (pcm->width == 0
24698 && pcm->rbearing == 0 && pcm->lbearing == 0)
24699 pcm = NULL;
24700 }
24701
24702 if (pcm)
24703 {
24704 it->phys_ascent = pcm->ascent + boff;
24705 it->phys_descent = pcm->descent - boff;
24706 it->pixel_width = pcm->width;
24707 }
24708 else
24709 {
24710 it->glyph_not_available_p = 1;
24711 it->phys_ascent = it->ascent;
24712 it->phys_descent = it->descent;
24713 it->pixel_width = font->space_width;
24714 }
24715
24716 if (it->constrain_row_ascent_descent_p)
24717 {
24718 if (it->descent > it->max_descent)
24719 {
24720 it->ascent += it->descent - it->max_descent;
24721 it->descent = it->max_descent;
24722 }
24723 if (it->ascent > it->max_ascent)
24724 {
24725 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24726 it->ascent = it->max_ascent;
24727 }
24728 it->phys_ascent = min (it->phys_ascent, it->ascent);
24729 it->phys_descent = min (it->phys_descent, it->descent);
24730 extra_line_spacing = 0;
24731 }
24732
24733 /* If this is a space inside a region of text with
24734 `space-width' property, change its width. */
24735 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24736 if (stretched_p)
24737 it->pixel_width *= XFLOATINT (it->space_width);
24738
24739 /* If face has a box, add the box thickness to the character
24740 height. If character has a box line to the left and/or
24741 right, add the box line width to the character's width. */
24742 if (face->box != FACE_NO_BOX)
24743 {
24744 int thick = face->box_line_width;
24745
24746 if (thick > 0)
24747 {
24748 it->ascent += thick;
24749 it->descent += thick;
24750 }
24751 else
24752 thick = -thick;
24753
24754 if (it->start_of_box_run_p)
24755 it->pixel_width += thick;
24756 if (it->end_of_box_run_p)
24757 it->pixel_width += thick;
24758 }
24759
24760 /* If face has an overline, add the height of the overline
24761 (1 pixel) and a 1 pixel margin to the character height. */
24762 if (face->overline_p)
24763 it->ascent += overline_margin;
24764
24765 if (it->constrain_row_ascent_descent_p)
24766 {
24767 if (it->ascent > it->max_ascent)
24768 it->ascent = it->max_ascent;
24769 if (it->descent > it->max_descent)
24770 it->descent = it->max_descent;
24771 }
24772
24773 take_vertical_position_into_account (it);
24774
24775 /* If we have to actually produce glyphs, do it. */
24776 if (it->glyph_row)
24777 {
24778 if (stretched_p)
24779 {
24780 /* Translate a space with a `space-width' property
24781 into a stretch glyph. */
24782 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24783 / FONT_HEIGHT (font));
24784 append_stretch_glyph (it, it->object, it->pixel_width,
24785 it->ascent + it->descent, ascent);
24786 }
24787 else
24788 append_glyph (it);
24789
24790 /* If characters with lbearing or rbearing are displayed
24791 in this line, record that fact in a flag of the
24792 glyph row. This is used to optimize X output code. */
24793 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24794 it->glyph_row->contains_overlapping_glyphs_p = 1;
24795 }
24796 if (! stretched_p && it->pixel_width == 0)
24797 /* We assure that all visible glyphs have at least 1-pixel
24798 width. */
24799 it->pixel_width = 1;
24800 }
24801 else if (it->char_to_display == '\n')
24802 {
24803 /* A newline has no width, but we need the height of the
24804 line. But if previous part of the line sets a height,
24805 don't increase that height */
24806
24807 Lisp_Object height;
24808 Lisp_Object total_height = Qnil;
24809
24810 it->override_ascent = -1;
24811 it->pixel_width = 0;
24812 it->nglyphs = 0;
24813
24814 height = get_it_property (it, Qline_height);
24815 /* Split (line-height total-height) list */
24816 if (CONSP (height)
24817 && CONSP (XCDR (height))
24818 && NILP (XCDR (XCDR (height))))
24819 {
24820 total_height = XCAR (XCDR (height));
24821 height = XCAR (height);
24822 }
24823 height = calc_line_height_property (it, height, font, boff, 1);
24824
24825 if (it->override_ascent >= 0)
24826 {
24827 it->ascent = it->override_ascent;
24828 it->descent = it->override_descent;
24829 boff = it->override_boff;
24830 }
24831 else
24832 {
24833 it->ascent = FONT_BASE (font) + boff;
24834 it->descent = FONT_DESCENT (font) - boff;
24835 }
24836
24837 if (EQ (height, Qt))
24838 {
24839 if (it->descent > it->max_descent)
24840 {
24841 it->ascent += it->descent - it->max_descent;
24842 it->descent = it->max_descent;
24843 }
24844 if (it->ascent > it->max_ascent)
24845 {
24846 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24847 it->ascent = it->max_ascent;
24848 }
24849 it->phys_ascent = min (it->phys_ascent, it->ascent);
24850 it->phys_descent = min (it->phys_descent, it->descent);
24851 it->constrain_row_ascent_descent_p = 1;
24852 extra_line_spacing = 0;
24853 }
24854 else
24855 {
24856 Lisp_Object spacing;
24857
24858 it->phys_ascent = it->ascent;
24859 it->phys_descent = it->descent;
24860
24861 if ((it->max_ascent > 0 || it->max_descent > 0)
24862 && face->box != FACE_NO_BOX
24863 && face->box_line_width > 0)
24864 {
24865 it->ascent += face->box_line_width;
24866 it->descent += face->box_line_width;
24867 }
24868 if (!NILP (height)
24869 && XINT (height) > it->ascent + it->descent)
24870 it->ascent = XINT (height) - it->descent;
24871
24872 if (!NILP (total_height))
24873 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24874 else
24875 {
24876 spacing = get_it_property (it, Qline_spacing);
24877 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24878 }
24879 if (INTEGERP (spacing))
24880 {
24881 extra_line_spacing = XINT (spacing);
24882 if (!NILP (total_height))
24883 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24884 }
24885 }
24886 }
24887 else /* i.e. (it->char_to_display == '\t') */
24888 {
24889 if (font->space_width > 0)
24890 {
24891 int tab_width = it->tab_width * font->space_width;
24892 int x = it->current_x + it->continuation_lines_width;
24893 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24894
24895 /* If the distance from the current position to the next tab
24896 stop is less than a space character width, use the
24897 tab stop after that. */
24898 if (next_tab_x - x < font->space_width)
24899 next_tab_x += tab_width;
24900
24901 it->pixel_width = next_tab_x - x;
24902 it->nglyphs = 1;
24903 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24904 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24905
24906 if (it->glyph_row)
24907 {
24908 append_stretch_glyph (it, it->object, it->pixel_width,
24909 it->ascent + it->descent, it->ascent);
24910 }
24911 }
24912 else
24913 {
24914 it->pixel_width = 0;
24915 it->nglyphs = 1;
24916 }
24917 }
24918 }
24919 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24920 {
24921 /* A static composition.
24922
24923 Note: A composition is represented as one glyph in the
24924 glyph matrix. There are no padding glyphs.
24925
24926 Important note: pixel_width, ascent, and descent are the
24927 values of what is drawn by draw_glyphs (i.e. the values of
24928 the overall glyphs composed). */
24929 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24930 int boff; /* baseline offset */
24931 struct composition *cmp = composition_table[it->cmp_it.id];
24932 int glyph_len = cmp->glyph_len;
24933 struct font *font = face->font;
24934
24935 it->nglyphs = 1;
24936
24937 /* If we have not yet calculated pixel size data of glyphs of
24938 the composition for the current face font, calculate them
24939 now. Theoretically, we have to check all fonts for the
24940 glyphs, but that requires much time and memory space. So,
24941 here we check only the font of the first glyph. This may
24942 lead to incorrect display, but it's very rare, and C-l
24943 (recenter-top-bottom) can correct the display anyway. */
24944 if (! cmp->font || cmp->font != font)
24945 {
24946 /* Ascent and descent of the font of the first character
24947 of this composition (adjusted by baseline offset).
24948 Ascent and descent of overall glyphs should not be less
24949 than these, respectively. */
24950 int font_ascent, font_descent, font_height;
24951 /* Bounding box of the overall glyphs. */
24952 int leftmost, rightmost, lowest, highest;
24953 int lbearing, rbearing;
24954 int i, width, ascent, descent;
24955 int left_padded = 0, right_padded = 0;
24956 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24957 XChar2b char2b;
24958 struct font_metrics *pcm;
24959 int font_not_found_p;
24960 ptrdiff_t pos;
24961
24962 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24963 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24964 break;
24965 if (glyph_len < cmp->glyph_len)
24966 right_padded = 1;
24967 for (i = 0; i < glyph_len; i++)
24968 {
24969 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24970 break;
24971 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24972 }
24973 if (i > 0)
24974 left_padded = 1;
24975
24976 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24977 : IT_CHARPOS (*it));
24978 /* If no suitable font is found, use the default font. */
24979 font_not_found_p = font == NULL;
24980 if (font_not_found_p)
24981 {
24982 face = face->ascii_face;
24983 font = face->font;
24984 }
24985 boff = font->baseline_offset;
24986 if (font->vertical_centering)
24987 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24988 font_ascent = FONT_BASE (font) + boff;
24989 font_descent = FONT_DESCENT (font) - boff;
24990 font_height = FONT_HEIGHT (font);
24991
24992 cmp->font = font;
24993
24994 pcm = NULL;
24995 if (! font_not_found_p)
24996 {
24997 get_char_face_and_encoding (it->f, c, it->face_id,
24998 &char2b, 0);
24999 pcm = get_per_char_metric (font, &char2b);
25000 }
25001
25002 /* Initialize the bounding box. */
25003 if (pcm)
25004 {
25005 width = cmp->glyph_len > 0 ? pcm->width : 0;
25006 ascent = pcm->ascent;
25007 descent = pcm->descent;
25008 lbearing = pcm->lbearing;
25009 rbearing = pcm->rbearing;
25010 }
25011 else
25012 {
25013 width = cmp->glyph_len > 0 ? font->space_width : 0;
25014 ascent = FONT_BASE (font);
25015 descent = FONT_DESCENT (font);
25016 lbearing = 0;
25017 rbearing = width;
25018 }
25019
25020 rightmost = width;
25021 leftmost = 0;
25022 lowest = - descent + boff;
25023 highest = ascent + boff;
25024
25025 if (! font_not_found_p
25026 && font->default_ascent
25027 && CHAR_TABLE_P (Vuse_default_ascent)
25028 && !NILP (Faref (Vuse_default_ascent,
25029 make_number (it->char_to_display))))
25030 highest = font->default_ascent + boff;
25031
25032 /* Draw the first glyph at the normal position. It may be
25033 shifted to right later if some other glyphs are drawn
25034 at the left. */
25035 cmp->offsets[i * 2] = 0;
25036 cmp->offsets[i * 2 + 1] = boff;
25037 cmp->lbearing = lbearing;
25038 cmp->rbearing = rbearing;
25039
25040 /* Set cmp->offsets for the remaining glyphs. */
25041 for (i++; i < glyph_len; i++)
25042 {
25043 int left, right, btm, top;
25044 int ch = COMPOSITION_GLYPH (cmp, i);
25045 int face_id;
25046 struct face *this_face;
25047
25048 if (ch == '\t')
25049 ch = ' ';
25050 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25051 this_face = FACE_FROM_ID (it->f, face_id);
25052 font = this_face->font;
25053
25054 if (font == NULL)
25055 pcm = NULL;
25056 else
25057 {
25058 get_char_face_and_encoding (it->f, ch, face_id,
25059 &char2b, 0);
25060 pcm = get_per_char_metric (font, &char2b);
25061 }
25062 if (! pcm)
25063 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25064 else
25065 {
25066 width = pcm->width;
25067 ascent = pcm->ascent;
25068 descent = pcm->descent;
25069 lbearing = pcm->lbearing;
25070 rbearing = pcm->rbearing;
25071 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25072 {
25073 /* Relative composition with or without
25074 alternate chars. */
25075 left = (leftmost + rightmost - width) / 2;
25076 btm = - descent + boff;
25077 if (font->relative_compose
25078 && (! CHAR_TABLE_P (Vignore_relative_composition)
25079 || NILP (Faref (Vignore_relative_composition,
25080 make_number (ch)))))
25081 {
25082
25083 if (- descent >= font->relative_compose)
25084 /* One extra pixel between two glyphs. */
25085 btm = highest + 1;
25086 else if (ascent <= 0)
25087 /* One extra pixel between two glyphs. */
25088 btm = lowest - 1 - ascent - descent;
25089 }
25090 }
25091 else
25092 {
25093 /* A composition rule is specified by an integer
25094 value that encodes global and new reference
25095 points (GREF and NREF). GREF and NREF are
25096 specified by numbers as below:
25097
25098 0---1---2 -- ascent
25099 | |
25100 | |
25101 | |
25102 9--10--11 -- center
25103 | |
25104 ---3---4---5--- baseline
25105 | |
25106 6---7---8 -- descent
25107 */
25108 int rule = COMPOSITION_RULE (cmp, i);
25109 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25110
25111 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25112 grefx = gref % 3, nrefx = nref % 3;
25113 grefy = gref / 3, nrefy = nref / 3;
25114 if (xoff)
25115 xoff = font_height * (xoff - 128) / 256;
25116 if (yoff)
25117 yoff = font_height * (yoff - 128) / 256;
25118
25119 left = (leftmost
25120 + grefx * (rightmost - leftmost) / 2
25121 - nrefx * width / 2
25122 + xoff);
25123
25124 btm = ((grefy == 0 ? highest
25125 : grefy == 1 ? 0
25126 : grefy == 2 ? lowest
25127 : (highest + lowest) / 2)
25128 - (nrefy == 0 ? ascent + descent
25129 : nrefy == 1 ? descent - boff
25130 : nrefy == 2 ? 0
25131 : (ascent + descent) / 2)
25132 + yoff);
25133 }
25134
25135 cmp->offsets[i * 2] = left;
25136 cmp->offsets[i * 2 + 1] = btm + descent;
25137
25138 /* Update the bounding box of the overall glyphs. */
25139 if (width > 0)
25140 {
25141 right = left + width;
25142 if (left < leftmost)
25143 leftmost = left;
25144 if (right > rightmost)
25145 rightmost = right;
25146 }
25147 top = btm + descent + ascent;
25148 if (top > highest)
25149 highest = top;
25150 if (btm < lowest)
25151 lowest = btm;
25152
25153 if (cmp->lbearing > left + lbearing)
25154 cmp->lbearing = left + lbearing;
25155 if (cmp->rbearing < left + rbearing)
25156 cmp->rbearing = left + rbearing;
25157 }
25158 }
25159
25160 /* If there are glyphs whose x-offsets are negative,
25161 shift all glyphs to the right and make all x-offsets
25162 non-negative. */
25163 if (leftmost < 0)
25164 {
25165 for (i = 0; i < cmp->glyph_len; i++)
25166 cmp->offsets[i * 2] -= leftmost;
25167 rightmost -= leftmost;
25168 cmp->lbearing -= leftmost;
25169 cmp->rbearing -= leftmost;
25170 }
25171
25172 if (left_padded && cmp->lbearing < 0)
25173 {
25174 for (i = 0; i < cmp->glyph_len; i++)
25175 cmp->offsets[i * 2] -= cmp->lbearing;
25176 rightmost -= cmp->lbearing;
25177 cmp->rbearing -= cmp->lbearing;
25178 cmp->lbearing = 0;
25179 }
25180 if (right_padded && rightmost < cmp->rbearing)
25181 {
25182 rightmost = cmp->rbearing;
25183 }
25184
25185 cmp->pixel_width = rightmost;
25186 cmp->ascent = highest;
25187 cmp->descent = - lowest;
25188 if (cmp->ascent < font_ascent)
25189 cmp->ascent = font_ascent;
25190 if (cmp->descent < font_descent)
25191 cmp->descent = font_descent;
25192 }
25193
25194 if (it->glyph_row
25195 && (cmp->lbearing < 0
25196 || cmp->rbearing > cmp->pixel_width))
25197 it->glyph_row->contains_overlapping_glyphs_p = 1;
25198
25199 it->pixel_width = cmp->pixel_width;
25200 it->ascent = it->phys_ascent = cmp->ascent;
25201 it->descent = it->phys_descent = cmp->descent;
25202 if (face->box != FACE_NO_BOX)
25203 {
25204 int thick = face->box_line_width;
25205
25206 if (thick > 0)
25207 {
25208 it->ascent += thick;
25209 it->descent += thick;
25210 }
25211 else
25212 thick = - thick;
25213
25214 if (it->start_of_box_run_p)
25215 it->pixel_width += thick;
25216 if (it->end_of_box_run_p)
25217 it->pixel_width += thick;
25218 }
25219
25220 /* If face has an overline, add the height of the overline
25221 (1 pixel) and a 1 pixel margin to the character height. */
25222 if (face->overline_p)
25223 it->ascent += overline_margin;
25224
25225 take_vertical_position_into_account (it);
25226 if (it->ascent < 0)
25227 it->ascent = 0;
25228 if (it->descent < 0)
25229 it->descent = 0;
25230
25231 if (it->glyph_row && cmp->glyph_len > 0)
25232 append_composite_glyph (it);
25233 }
25234 else if (it->what == IT_COMPOSITION)
25235 {
25236 /* A dynamic (automatic) composition. */
25237 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25238 Lisp_Object gstring;
25239 struct font_metrics metrics;
25240
25241 it->nglyphs = 1;
25242
25243 gstring = composition_gstring_from_id (it->cmp_it.id);
25244 it->pixel_width
25245 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25246 &metrics);
25247 if (it->glyph_row
25248 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25249 it->glyph_row->contains_overlapping_glyphs_p = 1;
25250 it->ascent = it->phys_ascent = metrics.ascent;
25251 it->descent = it->phys_descent = metrics.descent;
25252 if (face->box != FACE_NO_BOX)
25253 {
25254 int thick = face->box_line_width;
25255
25256 if (thick > 0)
25257 {
25258 it->ascent += thick;
25259 it->descent += thick;
25260 }
25261 else
25262 thick = - thick;
25263
25264 if (it->start_of_box_run_p)
25265 it->pixel_width += thick;
25266 if (it->end_of_box_run_p)
25267 it->pixel_width += thick;
25268 }
25269 /* If face has an overline, add the height of the overline
25270 (1 pixel) and a 1 pixel margin to the character height. */
25271 if (face->overline_p)
25272 it->ascent += overline_margin;
25273 take_vertical_position_into_account (it);
25274 if (it->ascent < 0)
25275 it->ascent = 0;
25276 if (it->descent < 0)
25277 it->descent = 0;
25278
25279 if (it->glyph_row)
25280 append_composite_glyph (it);
25281 }
25282 else if (it->what == IT_GLYPHLESS)
25283 produce_glyphless_glyph (it, 0, Qnil);
25284 else if (it->what == IT_IMAGE)
25285 produce_image_glyph (it);
25286 else if (it->what == IT_STRETCH)
25287 produce_stretch_glyph (it);
25288
25289 done:
25290 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25291 because this isn't true for images with `:ascent 100'. */
25292 eassert (it->ascent >= 0 && it->descent >= 0);
25293 if (it->area == TEXT_AREA)
25294 it->current_x += it->pixel_width;
25295
25296 if (extra_line_spacing > 0)
25297 {
25298 it->descent += extra_line_spacing;
25299 if (extra_line_spacing > it->max_extra_line_spacing)
25300 it->max_extra_line_spacing = extra_line_spacing;
25301 }
25302
25303 it->max_ascent = max (it->max_ascent, it->ascent);
25304 it->max_descent = max (it->max_descent, it->descent);
25305 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25306 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25307 }
25308
25309 /* EXPORT for RIF:
25310 Output LEN glyphs starting at START at the nominal cursor position.
25311 Advance the nominal cursor over the text. The global variable
25312 updated_window contains the window being updated, updated_row is
25313 the glyph row being updated, and updated_area is the area of that
25314 row being updated. */
25315
25316 void
25317 x_write_glyphs (struct glyph *start, int len)
25318 {
25319 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25320
25321 eassert (updated_window && updated_row);
25322 /* When the window is hscrolled, cursor hpos can legitimately be out
25323 of bounds, but we draw the cursor at the corresponding window
25324 margin in that case. */
25325 if (!updated_row->reversed_p && chpos < 0)
25326 chpos = 0;
25327 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25328 chpos = updated_row->used[TEXT_AREA] - 1;
25329
25330 BLOCK_INPUT;
25331
25332 /* Write glyphs. */
25333
25334 hpos = start - updated_row->glyphs[updated_area];
25335 x = draw_glyphs (updated_window, output_cursor.x,
25336 updated_row, updated_area,
25337 hpos, hpos + len,
25338 DRAW_NORMAL_TEXT, 0);
25339
25340 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25341 if (updated_area == TEXT_AREA
25342 && updated_window->phys_cursor_on_p
25343 && updated_window->phys_cursor.vpos == output_cursor.vpos
25344 && chpos >= hpos
25345 && chpos < hpos + len)
25346 updated_window->phys_cursor_on_p = 0;
25347
25348 UNBLOCK_INPUT;
25349
25350 /* Advance the output cursor. */
25351 output_cursor.hpos += len;
25352 output_cursor.x = x;
25353 }
25354
25355
25356 /* EXPORT for RIF:
25357 Insert LEN glyphs from START at the nominal cursor position. */
25358
25359 void
25360 x_insert_glyphs (struct glyph *start, int len)
25361 {
25362 struct frame *f;
25363 struct window *w;
25364 int line_height, shift_by_width, shifted_region_width;
25365 struct glyph_row *row;
25366 struct glyph *glyph;
25367 int frame_x, frame_y;
25368 ptrdiff_t hpos;
25369
25370 eassert (updated_window && updated_row);
25371 BLOCK_INPUT;
25372 w = updated_window;
25373 f = XFRAME (WINDOW_FRAME (w));
25374
25375 /* Get the height of the line we are in. */
25376 row = updated_row;
25377 line_height = row->height;
25378
25379 /* Get the width of the glyphs to insert. */
25380 shift_by_width = 0;
25381 for (glyph = start; glyph < start + len; ++glyph)
25382 shift_by_width += glyph->pixel_width;
25383
25384 /* Get the width of the region to shift right. */
25385 shifted_region_width = (window_box_width (w, updated_area)
25386 - output_cursor.x
25387 - shift_by_width);
25388
25389 /* Shift right. */
25390 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25391 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25392
25393 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25394 line_height, shift_by_width);
25395
25396 /* Write the glyphs. */
25397 hpos = start - row->glyphs[updated_area];
25398 draw_glyphs (w, output_cursor.x, row, updated_area,
25399 hpos, hpos + len,
25400 DRAW_NORMAL_TEXT, 0);
25401
25402 /* Advance the output cursor. */
25403 output_cursor.hpos += len;
25404 output_cursor.x += shift_by_width;
25405 UNBLOCK_INPUT;
25406 }
25407
25408
25409 /* EXPORT for RIF:
25410 Erase the current text line from the nominal cursor position
25411 (inclusive) to pixel column TO_X (exclusive). The idea is that
25412 everything from TO_X onward is already erased.
25413
25414 TO_X is a pixel position relative to updated_area of
25415 updated_window. TO_X == -1 means clear to the end of this area. */
25416
25417 void
25418 x_clear_end_of_line (int to_x)
25419 {
25420 struct frame *f;
25421 struct window *w = updated_window;
25422 int max_x, min_y, max_y;
25423 int from_x, from_y, to_y;
25424
25425 eassert (updated_window && updated_row);
25426 f = XFRAME (w->frame);
25427
25428 if (updated_row->full_width_p)
25429 max_x = WINDOW_TOTAL_WIDTH (w);
25430 else
25431 max_x = window_box_width (w, updated_area);
25432 max_y = window_text_bottom_y (w);
25433
25434 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25435 of window. For TO_X > 0, truncate to end of drawing area. */
25436 if (to_x == 0)
25437 return;
25438 else if (to_x < 0)
25439 to_x = max_x;
25440 else
25441 to_x = min (to_x, max_x);
25442
25443 to_y = min (max_y, output_cursor.y + updated_row->height);
25444
25445 /* Notice if the cursor will be cleared by this operation. */
25446 if (!updated_row->full_width_p)
25447 notice_overwritten_cursor (w, updated_area,
25448 output_cursor.x, -1,
25449 updated_row->y,
25450 MATRIX_ROW_BOTTOM_Y (updated_row));
25451
25452 from_x = output_cursor.x;
25453
25454 /* Translate to frame coordinates. */
25455 if (updated_row->full_width_p)
25456 {
25457 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25458 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25459 }
25460 else
25461 {
25462 int area_left = window_box_left (w, updated_area);
25463 from_x += area_left;
25464 to_x += area_left;
25465 }
25466
25467 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25468 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25469 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25470
25471 /* Prevent inadvertently clearing to end of the X window. */
25472 if (to_x > from_x && to_y > from_y)
25473 {
25474 BLOCK_INPUT;
25475 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25476 to_x - from_x, to_y - from_y);
25477 UNBLOCK_INPUT;
25478 }
25479 }
25480
25481 #endif /* HAVE_WINDOW_SYSTEM */
25482
25483
25484 \f
25485 /***********************************************************************
25486 Cursor types
25487 ***********************************************************************/
25488
25489 /* Value is the internal representation of the specified cursor type
25490 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25491 of the bar cursor. */
25492
25493 static enum text_cursor_kinds
25494 get_specified_cursor_type (Lisp_Object arg, int *width)
25495 {
25496 enum text_cursor_kinds type;
25497
25498 if (NILP (arg))
25499 return NO_CURSOR;
25500
25501 if (EQ (arg, Qbox))
25502 return FILLED_BOX_CURSOR;
25503
25504 if (EQ (arg, Qhollow))
25505 return HOLLOW_BOX_CURSOR;
25506
25507 if (EQ (arg, Qbar))
25508 {
25509 *width = 2;
25510 return BAR_CURSOR;
25511 }
25512
25513 if (CONSP (arg)
25514 && EQ (XCAR (arg), Qbar)
25515 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25516 {
25517 *width = XINT (XCDR (arg));
25518 return BAR_CURSOR;
25519 }
25520
25521 if (EQ (arg, Qhbar))
25522 {
25523 *width = 2;
25524 return HBAR_CURSOR;
25525 }
25526
25527 if (CONSP (arg)
25528 && EQ (XCAR (arg), Qhbar)
25529 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25530 {
25531 *width = XINT (XCDR (arg));
25532 return HBAR_CURSOR;
25533 }
25534
25535 /* Treat anything unknown as "hollow box cursor".
25536 It was bad to signal an error; people have trouble fixing
25537 .Xdefaults with Emacs, when it has something bad in it. */
25538 type = HOLLOW_BOX_CURSOR;
25539
25540 return type;
25541 }
25542
25543 /* Set the default cursor types for specified frame. */
25544 void
25545 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25546 {
25547 int width = 1;
25548 Lisp_Object tem;
25549
25550 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25551 FRAME_CURSOR_WIDTH (f) = width;
25552
25553 /* By default, set up the blink-off state depending on the on-state. */
25554
25555 tem = Fassoc (arg, Vblink_cursor_alist);
25556 if (!NILP (tem))
25557 {
25558 FRAME_BLINK_OFF_CURSOR (f)
25559 = get_specified_cursor_type (XCDR (tem), &width);
25560 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25561 }
25562 else
25563 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25564 }
25565
25566
25567 #ifdef HAVE_WINDOW_SYSTEM
25568
25569 /* Return the cursor we want to be displayed in window W. Return
25570 width of bar/hbar cursor through WIDTH arg. Return with
25571 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25572 (i.e. if the `system caret' should track this cursor).
25573
25574 In a mini-buffer window, we want the cursor only to appear if we
25575 are reading input from this window. For the selected window, we
25576 want the cursor type given by the frame parameter or buffer local
25577 setting of cursor-type. If explicitly marked off, draw no cursor.
25578 In all other cases, we want a hollow box cursor. */
25579
25580 static enum text_cursor_kinds
25581 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25582 int *active_cursor)
25583 {
25584 struct frame *f = XFRAME (w->frame);
25585 struct buffer *b = XBUFFER (w->buffer);
25586 int cursor_type = DEFAULT_CURSOR;
25587 Lisp_Object alt_cursor;
25588 int non_selected = 0;
25589
25590 *active_cursor = 1;
25591
25592 /* Echo area */
25593 if (cursor_in_echo_area
25594 && FRAME_HAS_MINIBUF_P (f)
25595 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25596 {
25597 if (w == XWINDOW (echo_area_window))
25598 {
25599 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25600 {
25601 *width = FRAME_CURSOR_WIDTH (f);
25602 return FRAME_DESIRED_CURSOR (f);
25603 }
25604 else
25605 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25606 }
25607
25608 *active_cursor = 0;
25609 non_selected = 1;
25610 }
25611
25612 /* Detect a nonselected window or nonselected frame. */
25613 else if (w != XWINDOW (f->selected_window)
25614 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25615 {
25616 *active_cursor = 0;
25617
25618 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25619 return NO_CURSOR;
25620
25621 non_selected = 1;
25622 }
25623
25624 /* Never display a cursor in a window in which cursor-type is nil. */
25625 if (NILP (BVAR (b, cursor_type)))
25626 return NO_CURSOR;
25627
25628 /* Get the normal cursor type for this window. */
25629 if (EQ (BVAR (b, cursor_type), Qt))
25630 {
25631 cursor_type = FRAME_DESIRED_CURSOR (f);
25632 *width = FRAME_CURSOR_WIDTH (f);
25633 }
25634 else
25635 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25636
25637 /* Use cursor-in-non-selected-windows instead
25638 for non-selected window or frame. */
25639 if (non_selected)
25640 {
25641 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25642 if (!EQ (Qt, alt_cursor))
25643 return get_specified_cursor_type (alt_cursor, width);
25644 /* t means modify the normal cursor type. */
25645 if (cursor_type == FILLED_BOX_CURSOR)
25646 cursor_type = HOLLOW_BOX_CURSOR;
25647 else if (cursor_type == BAR_CURSOR && *width > 1)
25648 --*width;
25649 return cursor_type;
25650 }
25651
25652 /* Use normal cursor if not blinked off. */
25653 if (!w->cursor_off_p)
25654 {
25655 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25656 {
25657 if (cursor_type == FILLED_BOX_CURSOR)
25658 {
25659 /* Using a block cursor on large images can be very annoying.
25660 So use a hollow cursor for "large" images.
25661 If image is not transparent (no mask), also use hollow cursor. */
25662 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25663 if (img != NULL && IMAGEP (img->spec))
25664 {
25665 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25666 where N = size of default frame font size.
25667 This should cover most of the "tiny" icons people may use. */
25668 if (!img->mask
25669 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25670 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25671 cursor_type = HOLLOW_BOX_CURSOR;
25672 }
25673 }
25674 else if (cursor_type != NO_CURSOR)
25675 {
25676 /* Display current only supports BOX and HOLLOW cursors for images.
25677 So for now, unconditionally use a HOLLOW cursor when cursor is
25678 not a solid box cursor. */
25679 cursor_type = HOLLOW_BOX_CURSOR;
25680 }
25681 }
25682 return cursor_type;
25683 }
25684
25685 /* Cursor is blinked off, so determine how to "toggle" it. */
25686
25687 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25688 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25689 return get_specified_cursor_type (XCDR (alt_cursor), width);
25690
25691 /* Then see if frame has specified a specific blink off cursor type. */
25692 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25693 {
25694 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25695 return FRAME_BLINK_OFF_CURSOR (f);
25696 }
25697
25698 #if 0
25699 /* Some people liked having a permanently visible blinking cursor,
25700 while others had very strong opinions against it. So it was
25701 decided to remove it. KFS 2003-09-03 */
25702
25703 /* Finally perform built-in cursor blinking:
25704 filled box <-> hollow box
25705 wide [h]bar <-> narrow [h]bar
25706 narrow [h]bar <-> no cursor
25707 other type <-> no cursor */
25708
25709 if (cursor_type == FILLED_BOX_CURSOR)
25710 return HOLLOW_BOX_CURSOR;
25711
25712 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25713 {
25714 *width = 1;
25715 return cursor_type;
25716 }
25717 #endif
25718
25719 return NO_CURSOR;
25720 }
25721
25722
25723 /* Notice when the text cursor of window W has been completely
25724 overwritten by a drawing operation that outputs glyphs in AREA
25725 starting at X0 and ending at X1 in the line starting at Y0 and
25726 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25727 the rest of the line after X0 has been written. Y coordinates
25728 are window-relative. */
25729
25730 static void
25731 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25732 int x0, int x1, int y0, int y1)
25733 {
25734 int cx0, cx1, cy0, cy1;
25735 struct glyph_row *row;
25736
25737 if (!w->phys_cursor_on_p)
25738 return;
25739 if (area != TEXT_AREA)
25740 return;
25741
25742 if (w->phys_cursor.vpos < 0
25743 || w->phys_cursor.vpos >= w->current_matrix->nrows
25744 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25745 !(row->enabled_p && row->displays_text_p)))
25746 return;
25747
25748 if (row->cursor_in_fringe_p)
25749 {
25750 row->cursor_in_fringe_p = 0;
25751 draw_fringe_bitmap (w, row, row->reversed_p);
25752 w->phys_cursor_on_p = 0;
25753 return;
25754 }
25755
25756 cx0 = w->phys_cursor.x;
25757 cx1 = cx0 + w->phys_cursor_width;
25758 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25759 return;
25760
25761 /* The cursor image will be completely removed from the
25762 screen if the output area intersects the cursor area in
25763 y-direction. When we draw in [y0 y1[, and some part of
25764 the cursor is at y < y0, that part must have been drawn
25765 before. When scrolling, the cursor is erased before
25766 actually scrolling, so we don't come here. When not
25767 scrolling, the rows above the old cursor row must have
25768 changed, and in this case these rows must have written
25769 over the cursor image.
25770
25771 Likewise if part of the cursor is below y1, with the
25772 exception of the cursor being in the first blank row at
25773 the buffer and window end because update_text_area
25774 doesn't draw that row. (Except when it does, but
25775 that's handled in update_text_area.) */
25776
25777 cy0 = w->phys_cursor.y;
25778 cy1 = cy0 + w->phys_cursor_height;
25779 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25780 return;
25781
25782 w->phys_cursor_on_p = 0;
25783 }
25784
25785 #endif /* HAVE_WINDOW_SYSTEM */
25786
25787 \f
25788 /************************************************************************
25789 Mouse Face
25790 ************************************************************************/
25791
25792 #ifdef HAVE_WINDOW_SYSTEM
25793
25794 /* EXPORT for RIF:
25795 Fix the display of area AREA of overlapping row ROW in window W
25796 with respect to the overlapping part OVERLAPS. */
25797
25798 void
25799 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25800 enum glyph_row_area area, int overlaps)
25801 {
25802 int i, x;
25803
25804 BLOCK_INPUT;
25805
25806 x = 0;
25807 for (i = 0; i < row->used[area];)
25808 {
25809 if (row->glyphs[area][i].overlaps_vertically_p)
25810 {
25811 int start = i, start_x = x;
25812
25813 do
25814 {
25815 x += row->glyphs[area][i].pixel_width;
25816 ++i;
25817 }
25818 while (i < row->used[area]
25819 && row->glyphs[area][i].overlaps_vertically_p);
25820
25821 draw_glyphs (w, start_x, row, area,
25822 start, i,
25823 DRAW_NORMAL_TEXT, overlaps);
25824 }
25825 else
25826 {
25827 x += row->glyphs[area][i].pixel_width;
25828 ++i;
25829 }
25830 }
25831
25832 UNBLOCK_INPUT;
25833 }
25834
25835
25836 /* EXPORT:
25837 Draw the cursor glyph of window W in glyph row ROW. See the
25838 comment of draw_glyphs for the meaning of HL. */
25839
25840 void
25841 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25842 enum draw_glyphs_face hl)
25843 {
25844 /* If cursor hpos is out of bounds, don't draw garbage. This can
25845 happen in mini-buffer windows when switching between echo area
25846 glyphs and mini-buffer. */
25847 if ((row->reversed_p
25848 ? (w->phys_cursor.hpos >= 0)
25849 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25850 {
25851 int on_p = w->phys_cursor_on_p;
25852 int x1;
25853 int hpos = w->phys_cursor.hpos;
25854
25855 /* When the window is hscrolled, cursor hpos can legitimately be
25856 out of bounds, but we draw the cursor at the corresponding
25857 window margin in that case. */
25858 if (!row->reversed_p && hpos < 0)
25859 hpos = 0;
25860 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25861 hpos = row->used[TEXT_AREA] - 1;
25862
25863 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25864 hl, 0);
25865 w->phys_cursor_on_p = on_p;
25866
25867 if (hl == DRAW_CURSOR)
25868 w->phys_cursor_width = x1 - w->phys_cursor.x;
25869 /* When we erase the cursor, and ROW is overlapped by other
25870 rows, make sure that these overlapping parts of other rows
25871 are redrawn. */
25872 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25873 {
25874 w->phys_cursor_width = x1 - w->phys_cursor.x;
25875
25876 if (row > w->current_matrix->rows
25877 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25878 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25879 OVERLAPS_ERASED_CURSOR);
25880
25881 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25882 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25883 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25884 OVERLAPS_ERASED_CURSOR);
25885 }
25886 }
25887 }
25888
25889
25890 /* EXPORT:
25891 Erase the image of a cursor of window W from the screen. */
25892
25893 void
25894 erase_phys_cursor (struct window *w)
25895 {
25896 struct frame *f = XFRAME (w->frame);
25897 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25898 int hpos = w->phys_cursor.hpos;
25899 int vpos = w->phys_cursor.vpos;
25900 int mouse_face_here_p = 0;
25901 struct glyph_matrix *active_glyphs = w->current_matrix;
25902 struct glyph_row *cursor_row;
25903 struct glyph *cursor_glyph;
25904 enum draw_glyphs_face hl;
25905
25906 /* No cursor displayed or row invalidated => nothing to do on the
25907 screen. */
25908 if (w->phys_cursor_type == NO_CURSOR)
25909 goto mark_cursor_off;
25910
25911 /* VPOS >= active_glyphs->nrows means that window has been resized.
25912 Don't bother to erase the cursor. */
25913 if (vpos >= active_glyphs->nrows)
25914 goto mark_cursor_off;
25915
25916 /* If row containing cursor is marked invalid, there is nothing we
25917 can do. */
25918 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25919 if (!cursor_row->enabled_p)
25920 goto mark_cursor_off;
25921
25922 /* If line spacing is > 0, old cursor may only be partially visible in
25923 window after split-window. So adjust visible height. */
25924 cursor_row->visible_height = min (cursor_row->visible_height,
25925 window_text_bottom_y (w) - cursor_row->y);
25926
25927 /* If row is completely invisible, don't attempt to delete a cursor which
25928 isn't there. This can happen if cursor is at top of a window, and
25929 we switch to a buffer with a header line in that window. */
25930 if (cursor_row->visible_height <= 0)
25931 goto mark_cursor_off;
25932
25933 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25934 if (cursor_row->cursor_in_fringe_p)
25935 {
25936 cursor_row->cursor_in_fringe_p = 0;
25937 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25938 goto mark_cursor_off;
25939 }
25940
25941 /* This can happen when the new row is shorter than the old one.
25942 In this case, either draw_glyphs or clear_end_of_line
25943 should have cleared the cursor. Note that we wouldn't be
25944 able to erase the cursor in this case because we don't have a
25945 cursor glyph at hand. */
25946 if ((cursor_row->reversed_p
25947 ? (w->phys_cursor.hpos < 0)
25948 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25949 goto mark_cursor_off;
25950
25951 /* When the window is hscrolled, cursor hpos can legitimately be out
25952 of bounds, but we draw the cursor at the corresponding window
25953 margin in that case. */
25954 if (!cursor_row->reversed_p && hpos < 0)
25955 hpos = 0;
25956 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25957 hpos = cursor_row->used[TEXT_AREA] - 1;
25958
25959 /* If the cursor is in the mouse face area, redisplay that when
25960 we clear the cursor. */
25961 if (! NILP (hlinfo->mouse_face_window)
25962 && coords_in_mouse_face_p (w, hpos, vpos)
25963 /* Don't redraw the cursor's spot in mouse face if it is at the
25964 end of a line (on a newline). The cursor appears there, but
25965 mouse highlighting does not. */
25966 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25967 mouse_face_here_p = 1;
25968
25969 /* Maybe clear the display under the cursor. */
25970 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25971 {
25972 int x, y, left_x;
25973 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25974 int width;
25975
25976 cursor_glyph = get_phys_cursor_glyph (w);
25977 if (cursor_glyph == NULL)
25978 goto mark_cursor_off;
25979
25980 width = cursor_glyph->pixel_width;
25981 left_x = window_box_left_offset (w, TEXT_AREA);
25982 x = w->phys_cursor.x;
25983 if (x < left_x)
25984 width -= left_x - x;
25985 width = min (width, window_box_width (w, TEXT_AREA) - x);
25986 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25987 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25988
25989 if (width > 0)
25990 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25991 }
25992
25993 /* Erase the cursor by redrawing the character underneath it. */
25994 if (mouse_face_here_p)
25995 hl = DRAW_MOUSE_FACE;
25996 else
25997 hl = DRAW_NORMAL_TEXT;
25998 draw_phys_cursor_glyph (w, cursor_row, hl);
25999
26000 mark_cursor_off:
26001 w->phys_cursor_on_p = 0;
26002 w->phys_cursor_type = NO_CURSOR;
26003 }
26004
26005
26006 /* EXPORT:
26007 Display or clear cursor of window W. If ON is zero, clear the
26008 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26009 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26010
26011 void
26012 display_and_set_cursor (struct window *w, int on,
26013 int hpos, int vpos, int x, int y)
26014 {
26015 struct frame *f = XFRAME (w->frame);
26016 int new_cursor_type;
26017 int new_cursor_width;
26018 int active_cursor;
26019 struct glyph_row *glyph_row;
26020 struct glyph *glyph;
26021
26022 /* This is pointless on invisible frames, and dangerous on garbaged
26023 windows and frames; in the latter case, the frame or window may
26024 be in the midst of changing its size, and x and y may be off the
26025 window. */
26026 if (! FRAME_VISIBLE_P (f)
26027 || FRAME_GARBAGED_P (f)
26028 || vpos >= w->current_matrix->nrows
26029 || hpos >= w->current_matrix->matrix_w)
26030 return;
26031
26032 /* If cursor is off and we want it off, return quickly. */
26033 if (!on && !w->phys_cursor_on_p)
26034 return;
26035
26036 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26037 /* If cursor row is not enabled, we don't really know where to
26038 display the cursor. */
26039 if (!glyph_row->enabled_p)
26040 {
26041 w->phys_cursor_on_p = 0;
26042 return;
26043 }
26044
26045 glyph = NULL;
26046 if (!glyph_row->exact_window_width_line_p
26047 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26048 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26049
26050 eassert (interrupt_input_blocked);
26051
26052 /* Set new_cursor_type to the cursor we want to be displayed. */
26053 new_cursor_type = get_window_cursor_type (w, glyph,
26054 &new_cursor_width, &active_cursor);
26055
26056 /* If cursor is currently being shown and we don't want it to be or
26057 it is in the wrong place, or the cursor type is not what we want,
26058 erase it. */
26059 if (w->phys_cursor_on_p
26060 && (!on
26061 || w->phys_cursor.x != x
26062 || w->phys_cursor.y != y
26063 || new_cursor_type != w->phys_cursor_type
26064 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26065 && new_cursor_width != w->phys_cursor_width)))
26066 erase_phys_cursor (w);
26067
26068 /* Don't check phys_cursor_on_p here because that flag is only set
26069 to zero in some cases where we know that the cursor has been
26070 completely erased, to avoid the extra work of erasing the cursor
26071 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26072 still not be visible, or it has only been partly erased. */
26073 if (on)
26074 {
26075 w->phys_cursor_ascent = glyph_row->ascent;
26076 w->phys_cursor_height = glyph_row->height;
26077
26078 /* Set phys_cursor_.* before x_draw_.* is called because some
26079 of them may need the information. */
26080 w->phys_cursor.x = x;
26081 w->phys_cursor.y = glyph_row->y;
26082 w->phys_cursor.hpos = hpos;
26083 w->phys_cursor.vpos = vpos;
26084 }
26085
26086 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26087 new_cursor_type, new_cursor_width,
26088 on, active_cursor);
26089 }
26090
26091
26092 /* Switch the display of W's cursor on or off, according to the value
26093 of ON. */
26094
26095 static void
26096 update_window_cursor (struct window *w, int on)
26097 {
26098 /* Don't update cursor in windows whose frame is in the process
26099 of being deleted. */
26100 if (w->current_matrix)
26101 {
26102 int hpos = w->phys_cursor.hpos;
26103 int vpos = w->phys_cursor.vpos;
26104 struct glyph_row *row;
26105
26106 if (vpos >= w->current_matrix->nrows
26107 || hpos >= w->current_matrix->matrix_w)
26108 return;
26109
26110 row = MATRIX_ROW (w->current_matrix, vpos);
26111
26112 /* When the window is hscrolled, cursor hpos can legitimately be
26113 out of bounds, but we draw the cursor at the corresponding
26114 window margin in that case. */
26115 if (!row->reversed_p && hpos < 0)
26116 hpos = 0;
26117 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26118 hpos = row->used[TEXT_AREA] - 1;
26119
26120 BLOCK_INPUT;
26121 display_and_set_cursor (w, on, hpos, vpos,
26122 w->phys_cursor.x, w->phys_cursor.y);
26123 UNBLOCK_INPUT;
26124 }
26125 }
26126
26127
26128 /* Call update_window_cursor with parameter ON_P on all leaf windows
26129 in the window tree rooted at W. */
26130
26131 static void
26132 update_cursor_in_window_tree (struct window *w, int on_p)
26133 {
26134 while (w)
26135 {
26136 if (!NILP (w->hchild))
26137 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26138 else if (!NILP (w->vchild))
26139 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26140 else
26141 update_window_cursor (w, on_p);
26142
26143 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26144 }
26145 }
26146
26147
26148 /* EXPORT:
26149 Display the cursor on window W, or clear it, according to ON_P.
26150 Don't change the cursor's position. */
26151
26152 void
26153 x_update_cursor (struct frame *f, int on_p)
26154 {
26155 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26156 }
26157
26158
26159 /* EXPORT:
26160 Clear the cursor of window W to background color, and mark the
26161 cursor as not shown. This is used when the text where the cursor
26162 is about to be rewritten. */
26163
26164 void
26165 x_clear_cursor (struct window *w)
26166 {
26167 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26168 update_window_cursor (w, 0);
26169 }
26170
26171 #endif /* HAVE_WINDOW_SYSTEM */
26172
26173 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26174 and MSDOS. */
26175 static void
26176 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26177 int start_hpos, int end_hpos,
26178 enum draw_glyphs_face draw)
26179 {
26180 #ifdef HAVE_WINDOW_SYSTEM
26181 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26182 {
26183 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26184 return;
26185 }
26186 #endif
26187 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26188 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26189 #endif
26190 }
26191
26192 /* Display the active region described by mouse_face_* according to DRAW. */
26193
26194 static void
26195 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26196 {
26197 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26198 struct frame *f = XFRAME (WINDOW_FRAME (w));
26199
26200 if (/* If window is in the process of being destroyed, don't bother
26201 to do anything. */
26202 w->current_matrix != NULL
26203 /* Don't update mouse highlight if hidden */
26204 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26205 /* Recognize when we are called to operate on rows that don't exist
26206 anymore. This can happen when a window is split. */
26207 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26208 {
26209 int phys_cursor_on_p = w->phys_cursor_on_p;
26210 struct glyph_row *row, *first, *last;
26211
26212 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26213 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26214
26215 for (row = first; row <= last && row->enabled_p; ++row)
26216 {
26217 int start_hpos, end_hpos, start_x;
26218
26219 /* For all but the first row, the highlight starts at column 0. */
26220 if (row == first)
26221 {
26222 /* R2L rows have BEG and END in reversed order, but the
26223 screen drawing geometry is always left to right. So
26224 we need to mirror the beginning and end of the
26225 highlighted area in R2L rows. */
26226 if (!row->reversed_p)
26227 {
26228 start_hpos = hlinfo->mouse_face_beg_col;
26229 start_x = hlinfo->mouse_face_beg_x;
26230 }
26231 else if (row == last)
26232 {
26233 start_hpos = hlinfo->mouse_face_end_col;
26234 start_x = hlinfo->mouse_face_end_x;
26235 }
26236 else
26237 {
26238 start_hpos = 0;
26239 start_x = 0;
26240 }
26241 }
26242 else if (row->reversed_p && row == last)
26243 {
26244 start_hpos = hlinfo->mouse_face_end_col;
26245 start_x = hlinfo->mouse_face_end_x;
26246 }
26247 else
26248 {
26249 start_hpos = 0;
26250 start_x = 0;
26251 }
26252
26253 if (row == last)
26254 {
26255 if (!row->reversed_p)
26256 end_hpos = hlinfo->mouse_face_end_col;
26257 else if (row == first)
26258 end_hpos = hlinfo->mouse_face_beg_col;
26259 else
26260 {
26261 end_hpos = row->used[TEXT_AREA];
26262 if (draw == DRAW_NORMAL_TEXT)
26263 row->fill_line_p = 1; /* Clear to end of line */
26264 }
26265 }
26266 else if (row->reversed_p && row == first)
26267 end_hpos = hlinfo->mouse_face_beg_col;
26268 else
26269 {
26270 end_hpos = row->used[TEXT_AREA];
26271 if (draw == DRAW_NORMAL_TEXT)
26272 row->fill_line_p = 1; /* Clear to end of line */
26273 }
26274
26275 if (end_hpos > start_hpos)
26276 {
26277 draw_row_with_mouse_face (w, start_x, row,
26278 start_hpos, end_hpos, draw);
26279
26280 row->mouse_face_p
26281 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26282 }
26283 }
26284
26285 #ifdef HAVE_WINDOW_SYSTEM
26286 /* When we've written over the cursor, arrange for it to
26287 be displayed again. */
26288 if (FRAME_WINDOW_P (f)
26289 && phys_cursor_on_p && !w->phys_cursor_on_p)
26290 {
26291 int hpos = w->phys_cursor.hpos;
26292
26293 /* When the window is hscrolled, cursor hpos can legitimately be
26294 out of bounds, but we draw the cursor at the corresponding
26295 window margin in that case. */
26296 if (!row->reversed_p && hpos < 0)
26297 hpos = 0;
26298 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26299 hpos = row->used[TEXT_AREA] - 1;
26300
26301 BLOCK_INPUT;
26302 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26303 w->phys_cursor.x, w->phys_cursor.y);
26304 UNBLOCK_INPUT;
26305 }
26306 #endif /* HAVE_WINDOW_SYSTEM */
26307 }
26308
26309 #ifdef HAVE_WINDOW_SYSTEM
26310 /* Change the mouse cursor. */
26311 if (FRAME_WINDOW_P (f))
26312 {
26313 if (draw == DRAW_NORMAL_TEXT
26314 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26315 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26316 else if (draw == DRAW_MOUSE_FACE)
26317 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26318 else
26319 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26320 }
26321 #endif /* HAVE_WINDOW_SYSTEM */
26322 }
26323
26324 /* EXPORT:
26325 Clear out the mouse-highlighted active region.
26326 Redraw it un-highlighted first. Value is non-zero if mouse
26327 face was actually drawn unhighlighted. */
26328
26329 int
26330 clear_mouse_face (Mouse_HLInfo *hlinfo)
26331 {
26332 int cleared = 0;
26333
26334 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26335 {
26336 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26337 cleared = 1;
26338 }
26339
26340 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26341 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26342 hlinfo->mouse_face_window = Qnil;
26343 hlinfo->mouse_face_overlay = Qnil;
26344 return cleared;
26345 }
26346
26347 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26348 within the mouse face on that window. */
26349 static int
26350 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26351 {
26352 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26353
26354 /* Quickly resolve the easy cases. */
26355 if (!(WINDOWP (hlinfo->mouse_face_window)
26356 && XWINDOW (hlinfo->mouse_face_window) == w))
26357 return 0;
26358 if (vpos < hlinfo->mouse_face_beg_row
26359 || vpos > hlinfo->mouse_face_end_row)
26360 return 0;
26361 if (vpos > hlinfo->mouse_face_beg_row
26362 && vpos < hlinfo->mouse_face_end_row)
26363 return 1;
26364
26365 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26366 {
26367 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26368 {
26369 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26370 return 1;
26371 }
26372 else if ((vpos == hlinfo->mouse_face_beg_row
26373 && hpos >= hlinfo->mouse_face_beg_col)
26374 || (vpos == hlinfo->mouse_face_end_row
26375 && hpos < hlinfo->mouse_face_end_col))
26376 return 1;
26377 }
26378 else
26379 {
26380 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26381 {
26382 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26383 return 1;
26384 }
26385 else if ((vpos == hlinfo->mouse_face_beg_row
26386 && hpos <= hlinfo->mouse_face_beg_col)
26387 || (vpos == hlinfo->mouse_face_end_row
26388 && hpos > hlinfo->mouse_face_end_col))
26389 return 1;
26390 }
26391 return 0;
26392 }
26393
26394
26395 /* EXPORT:
26396 Non-zero if physical cursor of window W is within mouse face. */
26397
26398 int
26399 cursor_in_mouse_face_p (struct window *w)
26400 {
26401 int hpos = w->phys_cursor.hpos;
26402 int vpos = w->phys_cursor.vpos;
26403 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26404
26405 /* When the window is hscrolled, cursor hpos can legitimately be out
26406 of bounds, but we draw the cursor at the corresponding window
26407 margin in that case. */
26408 if (!row->reversed_p && hpos < 0)
26409 hpos = 0;
26410 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26411 hpos = row->used[TEXT_AREA] - 1;
26412
26413 return coords_in_mouse_face_p (w, hpos, vpos);
26414 }
26415
26416
26417 \f
26418 /* Find the glyph rows START_ROW and END_ROW of window W that display
26419 characters between buffer positions START_CHARPOS and END_CHARPOS
26420 (excluding END_CHARPOS). DISP_STRING is a display string that
26421 covers these buffer positions. This is similar to
26422 row_containing_pos, but is more accurate when bidi reordering makes
26423 buffer positions change non-linearly with glyph rows. */
26424 static void
26425 rows_from_pos_range (struct window *w,
26426 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26427 Lisp_Object disp_string,
26428 struct glyph_row **start, struct glyph_row **end)
26429 {
26430 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26431 int last_y = window_text_bottom_y (w);
26432 struct glyph_row *row;
26433
26434 *start = NULL;
26435 *end = NULL;
26436
26437 while (!first->enabled_p
26438 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26439 first++;
26440
26441 /* Find the START row. */
26442 for (row = first;
26443 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26444 row++)
26445 {
26446 /* A row can potentially be the START row if the range of the
26447 characters it displays intersects the range
26448 [START_CHARPOS..END_CHARPOS). */
26449 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26450 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26451 /* See the commentary in row_containing_pos, for the
26452 explanation of the complicated way to check whether
26453 some position is beyond the end of the characters
26454 displayed by a row. */
26455 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26456 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26457 && !row->ends_at_zv_p
26458 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26459 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26460 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26461 && !row->ends_at_zv_p
26462 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26463 {
26464 /* Found a candidate row. Now make sure at least one of the
26465 glyphs it displays has a charpos from the range
26466 [START_CHARPOS..END_CHARPOS).
26467
26468 This is not obvious because bidi reordering could make
26469 buffer positions of a row be 1,2,3,102,101,100, and if we
26470 want to highlight characters in [50..60), we don't want
26471 this row, even though [50..60) does intersect [1..103),
26472 the range of character positions given by the row's start
26473 and end positions. */
26474 struct glyph *g = row->glyphs[TEXT_AREA];
26475 struct glyph *e = g + row->used[TEXT_AREA];
26476
26477 while (g < e)
26478 {
26479 if (((BUFFERP (g->object) || INTEGERP (g->object))
26480 && start_charpos <= g->charpos && g->charpos < end_charpos)
26481 /* A glyph that comes from DISP_STRING is by
26482 definition to be highlighted. */
26483 || EQ (g->object, disp_string))
26484 *start = row;
26485 g++;
26486 }
26487 if (*start)
26488 break;
26489 }
26490 }
26491
26492 /* Find the END row. */
26493 if (!*start
26494 /* If the last row is partially visible, start looking for END
26495 from that row, instead of starting from FIRST. */
26496 && !(row->enabled_p
26497 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26498 row = first;
26499 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26500 {
26501 struct glyph_row *next = row + 1;
26502 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26503
26504 if (!next->enabled_p
26505 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26506 /* The first row >= START whose range of displayed characters
26507 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26508 is the row END + 1. */
26509 || (start_charpos < next_start
26510 && end_charpos < next_start)
26511 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26512 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26513 && !next->ends_at_zv_p
26514 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26515 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26516 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26517 && !next->ends_at_zv_p
26518 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26519 {
26520 *end = row;
26521 break;
26522 }
26523 else
26524 {
26525 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26526 but none of the characters it displays are in the range, it is
26527 also END + 1. */
26528 struct glyph *g = next->glyphs[TEXT_AREA];
26529 struct glyph *s = g;
26530 struct glyph *e = g + next->used[TEXT_AREA];
26531
26532 while (g < e)
26533 {
26534 if (((BUFFERP (g->object) || INTEGERP (g->object))
26535 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26536 /* If the buffer position of the first glyph in
26537 the row is equal to END_CHARPOS, it means
26538 the last character to be highlighted is the
26539 newline of ROW, and we must consider NEXT as
26540 END, not END+1. */
26541 || (((!next->reversed_p && g == s)
26542 || (next->reversed_p && g == e - 1))
26543 && (g->charpos == end_charpos
26544 /* Special case for when NEXT is an
26545 empty line at ZV. */
26546 || (g->charpos == -1
26547 && !row->ends_at_zv_p
26548 && next_start == end_charpos)))))
26549 /* A glyph that comes from DISP_STRING is by
26550 definition to be highlighted. */
26551 || EQ (g->object, disp_string))
26552 break;
26553 g++;
26554 }
26555 if (g == e)
26556 {
26557 *end = row;
26558 break;
26559 }
26560 /* The first row that ends at ZV must be the last to be
26561 highlighted. */
26562 else if (next->ends_at_zv_p)
26563 {
26564 *end = next;
26565 break;
26566 }
26567 }
26568 }
26569 }
26570
26571 /* This function sets the mouse_face_* elements of HLINFO, assuming
26572 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26573 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26574 for the overlay or run of text properties specifying the mouse
26575 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26576 before-string and after-string that must also be highlighted.
26577 DISP_STRING, if non-nil, is a display string that may cover some
26578 or all of the highlighted text. */
26579
26580 static void
26581 mouse_face_from_buffer_pos (Lisp_Object window,
26582 Mouse_HLInfo *hlinfo,
26583 ptrdiff_t mouse_charpos,
26584 ptrdiff_t start_charpos,
26585 ptrdiff_t end_charpos,
26586 Lisp_Object before_string,
26587 Lisp_Object after_string,
26588 Lisp_Object disp_string)
26589 {
26590 struct window *w = XWINDOW (window);
26591 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26592 struct glyph_row *r1, *r2;
26593 struct glyph *glyph, *end;
26594 ptrdiff_t ignore, pos;
26595 int x;
26596
26597 eassert (NILP (disp_string) || STRINGP (disp_string));
26598 eassert (NILP (before_string) || STRINGP (before_string));
26599 eassert (NILP (after_string) || STRINGP (after_string));
26600
26601 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26602 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26603 if (r1 == NULL)
26604 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26605 /* If the before-string or display-string contains newlines,
26606 rows_from_pos_range skips to its last row. Move back. */
26607 if (!NILP (before_string) || !NILP (disp_string))
26608 {
26609 struct glyph_row *prev;
26610 while ((prev = r1 - 1, prev >= first)
26611 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26612 && prev->used[TEXT_AREA] > 0)
26613 {
26614 struct glyph *beg = prev->glyphs[TEXT_AREA];
26615 glyph = beg + prev->used[TEXT_AREA];
26616 while (--glyph >= beg && INTEGERP (glyph->object));
26617 if (glyph < beg
26618 || !(EQ (glyph->object, before_string)
26619 || EQ (glyph->object, disp_string)))
26620 break;
26621 r1 = prev;
26622 }
26623 }
26624 if (r2 == NULL)
26625 {
26626 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26627 hlinfo->mouse_face_past_end = 1;
26628 }
26629 else if (!NILP (after_string))
26630 {
26631 /* If the after-string has newlines, advance to its last row. */
26632 struct glyph_row *next;
26633 struct glyph_row *last
26634 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26635
26636 for (next = r2 + 1;
26637 next <= last
26638 && next->used[TEXT_AREA] > 0
26639 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26640 ++next)
26641 r2 = next;
26642 }
26643 /* The rest of the display engine assumes that mouse_face_beg_row is
26644 either above mouse_face_end_row or identical to it. But with
26645 bidi-reordered continued lines, the row for START_CHARPOS could
26646 be below the row for END_CHARPOS. If so, swap the rows and store
26647 them in correct order. */
26648 if (r1->y > r2->y)
26649 {
26650 struct glyph_row *tem = r2;
26651
26652 r2 = r1;
26653 r1 = tem;
26654 }
26655
26656 hlinfo->mouse_face_beg_y = r1->y;
26657 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26658 hlinfo->mouse_face_end_y = r2->y;
26659 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26660
26661 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26662 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26663 could be anywhere in the row and in any order. The strategy
26664 below is to find the leftmost and the rightmost glyph that
26665 belongs to either of these 3 strings, or whose position is
26666 between START_CHARPOS and END_CHARPOS, and highlight all the
26667 glyphs between those two. This may cover more than just the text
26668 between START_CHARPOS and END_CHARPOS if the range of characters
26669 strides the bidi level boundary, e.g. if the beginning is in R2L
26670 text while the end is in L2R text or vice versa. */
26671 if (!r1->reversed_p)
26672 {
26673 /* This row is in a left to right paragraph. Scan it left to
26674 right. */
26675 glyph = r1->glyphs[TEXT_AREA];
26676 end = glyph + r1->used[TEXT_AREA];
26677 x = r1->x;
26678
26679 /* Skip truncation glyphs at the start of the glyph row. */
26680 if (r1->displays_text_p)
26681 for (; glyph < end
26682 && INTEGERP (glyph->object)
26683 && glyph->charpos < 0;
26684 ++glyph)
26685 x += glyph->pixel_width;
26686
26687 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26688 or DISP_STRING, and the first glyph from buffer whose
26689 position is between START_CHARPOS and END_CHARPOS. */
26690 for (; glyph < end
26691 && !INTEGERP (glyph->object)
26692 && !EQ (glyph->object, disp_string)
26693 && !(BUFFERP (glyph->object)
26694 && (glyph->charpos >= start_charpos
26695 && glyph->charpos < end_charpos));
26696 ++glyph)
26697 {
26698 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26699 are present at buffer positions between START_CHARPOS and
26700 END_CHARPOS, or if they come from an overlay. */
26701 if (EQ (glyph->object, before_string))
26702 {
26703 pos = string_buffer_position (before_string,
26704 start_charpos);
26705 /* If pos == 0, it means before_string came from an
26706 overlay, not from a buffer position. */
26707 if (!pos || (pos >= start_charpos && pos < end_charpos))
26708 break;
26709 }
26710 else if (EQ (glyph->object, after_string))
26711 {
26712 pos = string_buffer_position (after_string, end_charpos);
26713 if (!pos || (pos >= start_charpos && pos < end_charpos))
26714 break;
26715 }
26716 x += glyph->pixel_width;
26717 }
26718 hlinfo->mouse_face_beg_x = x;
26719 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26720 }
26721 else
26722 {
26723 /* This row is in a right to left paragraph. Scan it right to
26724 left. */
26725 struct glyph *g;
26726
26727 end = r1->glyphs[TEXT_AREA] - 1;
26728 glyph = end + r1->used[TEXT_AREA];
26729
26730 /* Skip truncation glyphs at the start of the glyph row. */
26731 if (r1->displays_text_p)
26732 for (; glyph > end
26733 && INTEGERP (glyph->object)
26734 && glyph->charpos < 0;
26735 --glyph)
26736 ;
26737
26738 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26739 or DISP_STRING, and the first glyph from buffer whose
26740 position is between START_CHARPOS and END_CHARPOS. */
26741 for (; glyph > end
26742 && !INTEGERP (glyph->object)
26743 && !EQ (glyph->object, disp_string)
26744 && !(BUFFERP (glyph->object)
26745 && (glyph->charpos >= start_charpos
26746 && glyph->charpos < end_charpos));
26747 --glyph)
26748 {
26749 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26750 are present at buffer positions between START_CHARPOS and
26751 END_CHARPOS, or if they come from an overlay. */
26752 if (EQ (glyph->object, before_string))
26753 {
26754 pos = string_buffer_position (before_string, start_charpos);
26755 /* If pos == 0, it means before_string came from an
26756 overlay, not from a buffer position. */
26757 if (!pos || (pos >= start_charpos && pos < end_charpos))
26758 break;
26759 }
26760 else if (EQ (glyph->object, after_string))
26761 {
26762 pos = string_buffer_position (after_string, end_charpos);
26763 if (!pos || (pos >= start_charpos && pos < end_charpos))
26764 break;
26765 }
26766 }
26767
26768 glyph++; /* first glyph to the right of the highlighted area */
26769 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26770 x += g->pixel_width;
26771 hlinfo->mouse_face_beg_x = x;
26772 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26773 }
26774
26775 /* If the highlight ends in a different row, compute GLYPH and END
26776 for the end row. Otherwise, reuse the values computed above for
26777 the row where the highlight begins. */
26778 if (r2 != r1)
26779 {
26780 if (!r2->reversed_p)
26781 {
26782 glyph = r2->glyphs[TEXT_AREA];
26783 end = glyph + r2->used[TEXT_AREA];
26784 x = r2->x;
26785 }
26786 else
26787 {
26788 end = r2->glyphs[TEXT_AREA] - 1;
26789 glyph = end + r2->used[TEXT_AREA];
26790 }
26791 }
26792
26793 if (!r2->reversed_p)
26794 {
26795 /* Skip truncation and continuation glyphs near the end of the
26796 row, and also blanks and stretch glyphs inserted by
26797 extend_face_to_end_of_line. */
26798 while (end > glyph
26799 && INTEGERP ((end - 1)->object))
26800 --end;
26801 /* Scan the rest of the glyph row from the end, looking for the
26802 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26803 DISP_STRING, or whose position is between START_CHARPOS
26804 and END_CHARPOS */
26805 for (--end;
26806 end > glyph
26807 && !INTEGERP (end->object)
26808 && !EQ (end->object, disp_string)
26809 && !(BUFFERP (end->object)
26810 && (end->charpos >= start_charpos
26811 && end->charpos < end_charpos));
26812 --end)
26813 {
26814 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26815 are present at buffer positions between START_CHARPOS and
26816 END_CHARPOS, or if they come from an overlay. */
26817 if (EQ (end->object, before_string))
26818 {
26819 pos = string_buffer_position (before_string, start_charpos);
26820 if (!pos || (pos >= start_charpos && pos < end_charpos))
26821 break;
26822 }
26823 else if (EQ (end->object, after_string))
26824 {
26825 pos = string_buffer_position (after_string, end_charpos);
26826 if (!pos || (pos >= start_charpos && pos < end_charpos))
26827 break;
26828 }
26829 }
26830 /* Find the X coordinate of the last glyph to be highlighted. */
26831 for (; glyph <= end; ++glyph)
26832 x += glyph->pixel_width;
26833
26834 hlinfo->mouse_face_end_x = x;
26835 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26836 }
26837 else
26838 {
26839 /* Skip truncation and continuation glyphs near the end of the
26840 row, and also blanks and stretch glyphs inserted by
26841 extend_face_to_end_of_line. */
26842 x = r2->x;
26843 end++;
26844 while (end < glyph
26845 && INTEGERP (end->object))
26846 {
26847 x += end->pixel_width;
26848 ++end;
26849 }
26850 /* Scan the rest of the glyph row from the end, looking for the
26851 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26852 DISP_STRING, or whose position is between START_CHARPOS
26853 and END_CHARPOS */
26854 for ( ;
26855 end < glyph
26856 && !INTEGERP (end->object)
26857 && !EQ (end->object, disp_string)
26858 && !(BUFFERP (end->object)
26859 && (end->charpos >= start_charpos
26860 && end->charpos < end_charpos));
26861 ++end)
26862 {
26863 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26864 are present at buffer positions between START_CHARPOS and
26865 END_CHARPOS, or if they come from an overlay. */
26866 if (EQ (end->object, before_string))
26867 {
26868 pos = string_buffer_position (before_string, start_charpos);
26869 if (!pos || (pos >= start_charpos && pos < end_charpos))
26870 break;
26871 }
26872 else if (EQ (end->object, after_string))
26873 {
26874 pos = string_buffer_position (after_string, end_charpos);
26875 if (!pos || (pos >= start_charpos && pos < end_charpos))
26876 break;
26877 }
26878 x += end->pixel_width;
26879 }
26880 /* If we exited the above loop because we arrived at the last
26881 glyph of the row, and its buffer position is still not in
26882 range, it means the last character in range is the preceding
26883 newline. Bump the end column and x values to get past the
26884 last glyph. */
26885 if (end == glyph
26886 && BUFFERP (end->object)
26887 && (end->charpos < start_charpos
26888 || end->charpos >= end_charpos))
26889 {
26890 x += end->pixel_width;
26891 ++end;
26892 }
26893 hlinfo->mouse_face_end_x = x;
26894 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26895 }
26896
26897 hlinfo->mouse_face_window = window;
26898 hlinfo->mouse_face_face_id
26899 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26900 mouse_charpos + 1,
26901 !hlinfo->mouse_face_hidden, -1);
26902 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26903 }
26904
26905 /* The following function is not used anymore (replaced with
26906 mouse_face_from_string_pos), but I leave it here for the time
26907 being, in case someone would. */
26908
26909 #if 0 /* not used */
26910
26911 /* Find the position of the glyph for position POS in OBJECT in
26912 window W's current matrix, and return in *X, *Y the pixel
26913 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26914
26915 RIGHT_P non-zero means return the position of the right edge of the
26916 glyph, RIGHT_P zero means return the left edge position.
26917
26918 If no glyph for POS exists in the matrix, return the position of
26919 the glyph with the next smaller position that is in the matrix, if
26920 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26921 exists in the matrix, return the position of the glyph with the
26922 next larger position in OBJECT.
26923
26924 Value is non-zero if a glyph was found. */
26925
26926 static int
26927 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26928 int *hpos, int *vpos, int *x, int *y, int right_p)
26929 {
26930 int yb = window_text_bottom_y (w);
26931 struct glyph_row *r;
26932 struct glyph *best_glyph = NULL;
26933 struct glyph_row *best_row = NULL;
26934 int best_x = 0;
26935
26936 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26937 r->enabled_p && r->y < yb;
26938 ++r)
26939 {
26940 struct glyph *g = r->glyphs[TEXT_AREA];
26941 struct glyph *e = g + r->used[TEXT_AREA];
26942 int gx;
26943
26944 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26945 if (EQ (g->object, object))
26946 {
26947 if (g->charpos == pos)
26948 {
26949 best_glyph = g;
26950 best_x = gx;
26951 best_row = r;
26952 goto found;
26953 }
26954 else if (best_glyph == NULL
26955 || ((eabs (g->charpos - pos)
26956 < eabs (best_glyph->charpos - pos))
26957 && (right_p
26958 ? g->charpos < pos
26959 : g->charpos > pos)))
26960 {
26961 best_glyph = g;
26962 best_x = gx;
26963 best_row = r;
26964 }
26965 }
26966 }
26967
26968 found:
26969
26970 if (best_glyph)
26971 {
26972 *x = best_x;
26973 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26974
26975 if (right_p)
26976 {
26977 *x += best_glyph->pixel_width;
26978 ++*hpos;
26979 }
26980
26981 *y = best_row->y;
26982 *vpos = best_row - w->current_matrix->rows;
26983 }
26984
26985 return best_glyph != NULL;
26986 }
26987 #endif /* not used */
26988
26989 /* Find the positions of the first and the last glyphs in window W's
26990 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26991 (assumed to be a string), and return in HLINFO's mouse_face_*
26992 members the pixel and column/row coordinates of those glyphs. */
26993
26994 static void
26995 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26996 Lisp_Object object,
26997 ptrdiff_t startpos, ptrdiff_t endpos)
26998 {
26999 int yb = window_text_bottom_y (w);
27000 struct glyph_row *r;
27001 struct glyph *g, *e;
27002 int gx;
27003 int found = 0;
27004
27005 /* Find the glyph row with at least one position in the range
27006 [STARTPOS..ENDPOS], and the first glyph in that row whose
27007 position belongs to that range. */
27008 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27009 r->enabled_p && r->y < yb;
27010 ++r)
27011 {
27012 if (!r->reversed_p)
27013 {
27014 g = r->glyphs[TEXT_AREA];
27015 e = g + r->used[TEXT_AREA];
27016 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27017 if (EQ (g->object, object)
27018 && startpos <= g->charpos && g->charpos <= endpos)
27019 {
27020 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27021 hlinfo->mouse_face_beg_y = r->y;
27022 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27023 hlinfo->mouse_face_beg_x = gx;
27024 found = 1;
27025 break;
27026 }
27027 }
27028 else
27029 {
27030 struct glyph *g1;
27031
27032 e = r->glyphs[TEXT_AREA];
27033 g = e + r->used[TEXT_AREA];
27034 for ( ; g > e; --g)
27035 if (EQ ((g-1)->object, object)
27036 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27037 {
27038 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27039 hlinfo->mouse_face_beg_y = r->y;
27040 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27041 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27042 gx += g1->pixel_width;
27043 hlinfo->mouse_face_beg_x = gx;
27044 found = 1;
27045 break;
27046 }
27047 }
27048 if (found)
27049 break;
27050 }
27051
27052 if (!found)
27053 return;
27054
27055 /* Starting with the next row, look for the first row which does NOT
27056 include any glyphs whose positions are in the range. */
27057 for (++r; r->enabled_p && r->y < yb; ++r)
27058 {
27059 g = r->glyphs[TEXT_AREA];
27060 e = g + r->used[TEXT_AREA];
27061 found = 0;
27062 for ( ; g < e; ++g)
27063 if (EQ (g->object, object)
27064 && startpos <= g->charpos && g->charpos <= endpos)
27065 {
27066 found = 1;
27067 break;
27068 }
27069 if (!found)
27070 break;
27071 }
27072
27073 /* The highlighted region ends on the previous row. */
27074 r--;
27075
27076 /* Set the end row and its vertical pixel coordinate. */
27077 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27078 hlinfo->mouse_face_end_y = r->y;
27079
27080 /* Compute and set the end column and the end column's horizontal
27081 pixel coordinate. */
27082 if (!r->reversed_p)
27083 {
27084 g = r->glyphs[TEXT_AREA];
27085 e = g + r->used[TEXT_AREA];
27086 for ( ; e > g; --e)
27087 if (EQ ((e-1)->object, object)
27088 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27089 break;
27090 hlinfo->mouse_face_end_col = e - g;
27091
27092 for (gx = r->x; g < e; ++g)
27093 gx += g->pixel_width;
27094 hlinfo->mouse_face_end_x = gx;
27095 }
27096 else
27097 {
27098 e = r->glyphs[TEXT_AREA];
27099 g = e + r->used[TEXT_AREA];
27100 for (gx = r->x ; e < g; ++e)
27101 {
27102 if (EQ (e->object, object)
27103 && startpos <= e->charpos && e->charpos <= endpos)
27104 break;
27105 gx += e->pixel_width;
27106 }
27107 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27108 hlinfo->mouse_face_end_x = gx;
27109 }
27110 }
27111
27112 #ifdef HAVE_WINDOW_SYSTEM
27113
27114 /* See if position X, Y is within a hot-spot of an image. */
27115
27116 static int
27117 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27118 {
27119 if (!CONSP (hot_spot))
27120 return 0;
27121
27122 if (EQ (XCAR (hot_spot), Qrect))
27123 {
27124 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27125 Lisp_Object rect = XCDR (hot_spot);
27126 Lisp_Object tem;
27127 if (!CONSP (rect))
27128 return 0;
27129 if (!CONSP (XCAR (rect)))
27130 return 0;
27131 if (!CONSP (XCDR (rect)))
27132 return 0;
27133 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27134 return 0;
27135 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27136 return 0;
27137 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27138 return 0;
27139 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27140 return 0;
27141 return 1;
27142 }
27143 else if (EQ (XCAR (hot_spot), Qcircle))
27144 {
27145 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27146 Lisp_Object circ = XCDR (hot_spot);
27147 Lisp_Object lr, lx0, ly0;
27148 if (CONSP (circ)
27149 && CONSP (XCAR (circ))
27150 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27151 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27152 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27153 {
27154 double r = XFLOATINT (lr);
27155 double dx = XINT (lx0) - x;
27156 double dy = XINT (ly0) - y;
27157 return (dx * dx + dy * dy <= r * r);
27158 }
27159 }
27160 else if (EQ (XCAR (hot_spot), Qpoly))
27161 {
27162 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27163 if (VECTORP (XCDR (hot_spot)))
27164 {
27165 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27166 Lisp_Object *poly = v->contents;
27167 ptrdiff_t n = v->header.size;
27168 ptrdiff_t i;
27169 int inside = 0;
27170 Lisp_Object lx, ly;
27171 int x0, y0;
27172
27173 /* Need an even number of coordinates, and at least 3 edges. */
27174 if (n < 6 || n & 1)
27175 return 0;
27176
27177 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27178 If count is odd, we are inside polygon. Pixels on edges
27179 may or may not be included depending on actual geometry of the
27180 polygon. */
27181 if ((lx = poly[n-2], !INTEGERP (lx))
27182 || (ly = poly[n-1], !INTEGERP (lx)))
27183 return 0;
27184 x0 = XINT (lx), y0 = XINT (ly);
27185 for (i = 0; i < n; i += 2)
27186 {
27187 int x1 = x0, y1 = y0;
27188 if ((lx = poly[i], !INTEGERP (lx))
27189 || (ly = poly[i+1], !INTEGERP (ly)))
27190 return 0;
27191 x0 = XINT (lx), y0 = XINT (ly);
27192
27193 /* Does this segment cross the X line? */
27194 if (x0 >= x)
27195 {
27196 if (x1 >= x)
27197 continue;
27198 }
27199 else if (x1 < x)
27200 continue;
27201 if (y > y0 && y > y1)
27202 continue;
27203 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27204 inside = !inside;
27205 }
27206 return inside;
27207 }
27208 }
27209 return 0;
27210 }
27211
27212 Lisp_Object
27213 find_hot_spot (Lisp_Object map, int x, int y)
27214 {
27215 while (CONSP (map))
27216 {
27217 if (CONSP (XCAR (map))
27218 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27219 return XCAR (map);
27220 map = XCDR (map);
27221 }
27222
27223 return Qnil;
27224 }
27225
27226 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27227 3, 3, 0,
27228 doc: /* Lookup in image map MAP coordinates X and Y.
27229 An image map is an alist where each element has the format (AREA ID PLIST).
27230 An AREA is specified as either a rectangle, a circle, or a polygon:
27231 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27232 pixel coordinates of the upper left and bottom right corners.
27233 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27234 and the radius of the circle; r may be a float or integer.
27235 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27236 vector describes one corner in the polygon.
27237 Returns the alist element for the first matching AREA in MAP. */)
27238 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27239 {
27240 if (NILP (map))
27241 return Qnil;
27242
27243 CHECK_NUMBER (x);
27244 CHECK_NUMBER (y);
27245
27246 return find_hot_spot (map,
27247 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27248 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27249 }
27250
27251
27252 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27253 static void
27254 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27255 {
27256 /* Do not change cursor shape while dragging mouse. */
27257 if (!NILP (do_mouse_tracking))
27258 return;
27259
27260 if (!NILP (pointer))
27261 {
27262 if (EQ (pointer, Qarrow))
27263 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27264 else if (EQ (pointer, Qhand))
27265 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27266 else if (EQ (pointer, Qtext))
27267 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27268 else if (EQ (pointer, intern ("hdrag")))
27269 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27270 #ifdef HAVE_X_WINDOWS
27271 else if (EQ (pointer, intern ("vdrag")))
27272 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27273 #endif
27274 else if (EQ (pointer, intern ("hourglass")))
27275 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27276 else if (EQ (pointer, Qmodeline))
27277 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27278 else
27279 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27280 }
27281
27282 if (cursor != No_Cursor)
27283 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27284 }
27285
27286 #endif /* HAVE_WINDOW_SYSTEM */
27287
27288 /* Take proper action when mouse has moved to the mode or header line
27289 or marginal area AREA of window W, x-position X and y-position Y.
27290 X is relative to the start of the text display area of W, so the
27291 width of bitmap areas and scroll bars must be subtracted to get a
27292 position relative to the start of the mode line. */
27293
27294 static void
27295 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27296 enum window_part area)
27297 {
27298 struct window *w = XWINDOW (window);
27299 struct frame *f = XFRAME (w->frame);
27300 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27301 #ifdef HAVE_WINDOW_SYSTEM
27302 Display_Info *dpyinfo;
27303 #endif
27304 Cursor cursor = No_Cursor;
27305 Lisp_Object pointer = Qnil;
27306 int dx, dy, width, height;
27307 ptrdiff_t charpos;
27308 Lisp_Object string, object = Qnil;
27309 Lisp_Object pos IF_LINT (= Qnil), help;
27310
27311 Lisp_Object mouse_face;
27312 int original_x_pixel = x;
27313 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27314 struct glyph_row *row IF_LINT (= 0);
27315
27316 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27317 {
27318 int x0;
27319 struct glyph *end;
27320
27321 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27322 returns them in row/column units! */
27323 string = mode_line_string (w, area, &x, &y, &charpos,
27324 &object, &dx, &dy, &width, &height);
27325
27326 row = (area == ON_MODE_LINE
27327 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27328 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27329
27330 /* Find the glyph under the mouse pointer. */
27331 if (row->mode_line_p && row->enabled_p)
27332 {
27333 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27334 end = glyph + row->used[TEXT_AREA];
27335
27336 for (x0 = original_x_pixel;
27337 glyph < end && x0 >= glyph->pixel_width;
27338 ++glyph)
27339 x0 -= glyph->pixel_width;
27340
27341 if (glyph >= end)
27342 glyph = NULL;
27343 }
27344 }
27345 else
27346 {
27347 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27348 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27349 returns them in row/column units! */
27350 string = marginal_area_string (w, area, &x, &y, &charpos,
27351 &object, &dx, &dy, &width, &height);
27352 }
27353
27354 help = Qnil;
27355
27356 #ifdef HAVE_WINDOW_SYSTEM
27357 if (IMAGEP (object))
27358 {
27359 Lisp_Object image_map, hotspot;
27360 if ((image_map = Fplist_get (XCDR (object), QCmap),
27361 !NILP (image_map))
27362 && (hotspot = find_hot_spot (image_map, dx, dy),
27363 CONSP (hotspot))
27364 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27365 {
27366 Lisp_Object plist;
27367
27368 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27369 If so, we could look for mouse-enter, mouse-leave
27370 properties in PLIST (and do something...). */
27371 hotspot = XCDR (hotspot);
27372 if (CONSP (hotspot)
27373 && (plist = XCAR (hotspot), CONSP (plist)))
27374 {
27375 pointer = Fplist_get (plist, Qpointer);
27376 if (NILP (pointer))
27377 pointer = Qhand;
27378 help = Fplist_get (plist, Qhelp_echo);
27379 if (!NILP (help))
27380 {
27381 help_echo_string = help;
27382 XSETWINDOW (help_echo_window, w);
27383 help_echo_object = w->buffer;
27384 help_echo_pos = charpos;
27385 }
27386 }
27387 }
27388 if (NILP (pointer))
27389 pointer = Fplist_get (XCDR (object), QCpointer);
27390 }
27391 #endif /* HAVE_WINDOW_SYSTEM */
27392
27393 if (STRINGP (string))
27394 pos = make_number (charpos);
27395
27396 /* Set the help text and mouse pointer. If the mouse is on a part
27397 of the mode line without any text (e.g. past the right edge of
27398 the mode line text), use the default help text and pointer. */
27399 if (STRINGP (string) || area == ON_MODE_LINE)
27400 {
27401 /* Arrange to display the help by setting the global variables
27402 help_echo_string, help_echo_object, and help_echo_pos. */
27403 if (NILP (help))
27404 {
27405 if (STRINGP (string))
27406 help = Fget_text_property (pos, Qhelp_echo, string);
27407
27408 if (!NILP (help))
27409 {
27410 help_echo_string = help;
27411 XSETWINDOW (help_echo_window, w);
27412 help_echo_object = string;
27413 help_echo_pos = charpos;
27414 }
27415 else if (area == ON_MODE_LINE)
27416 {
27417 Lisp_Object default_help
27418 = buffer_local_value_1 (Qmode_line_default_help_echo,
27419 w->buffer);
27420
27421 if (STRINGP (default_help))
27422 {
27423 help_echo_string = default_help;
27424 XSETWINDOW (help_echo_window, w);
27425 help_echo_object = Qnil;
27426 help_echo_pos = -1;
27427 }
27428 }
27429 }
27430
27431 #ifdef HAVE_WINDOW_SYSTEM
27432 /* Change the mouse pointer according to what is under it. */
27433 if (FRAME_WINDOW_P (f))
27434 {
27435 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27436 if (STRINGP (string))
27437 {
27438 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27439
27440 if (NILP (pointer))
27441 pointer = Fget_text_property (pos, Qpointer, string);
27442
27443 /* Change the mouse pointer according to what is under X/Y. */
27444 if (NILP (pointer)
27445 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27446 {
27447 Lisp_Object map;
27448 map = Fget_text_property (pos, Qlocal_map, string);
27449 if (!KEYMAPP (map))
27450 map = Fget_text_property (pos, Qkeymap, string);
27451 if (!KEYMAPP (map))
27452 cursor = dpyinfo->vertical_scroll_bar_cursor;
27453 }
27454 }
27455 else
27456 /* Default mode-line pointer. */
27457 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27458 }
27459 #endif
27460 }
27461
27462 /* Change the mouse face according to what is under X/Y. */
27463 if (STRINGP (string))
27464 {
27465 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27466 if (!NILP (mouse_face)
27467 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27468 && glyph)
27469 {
27470 Lisp_Object b, e;
27471
27472 struct glyph * tmp_glyph;
27473
27474 int gpos;
27475 int gseq_length;
27476 int total_pixel_width;
27477 ptrdiff_t begpos, endpos, ignore;
27478
27479 int vpos, hpos;
27480
27481 b = Fprevious_single_property_change (make_number (charpos + 1),
27482 Qmouse_face, string, Qnil);
27483 if (NILP (b))
27484 begpos = 0;
27485 else
27486 begpos = XINT (b);
27487
27488 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27489 if (NILP (e))
27490 endpos = SCHARS (string);
27491 else
27492 endpos = XINT (e);
27493
27494 /* Calculate the glyph position GPOS of GLYPH in the
27495 displayed string, relative to the beginning of the
27496 highlighted part of the string.
27497
27498 Note: GPOS is different from CHARPOS. CHARPOS is the
27499 position of GLYPH in the internal string object. A mode
27500 line string format has structures which are converted to
27501 a flattened string by the Emacs Lisp interpreter. The
27502 internal string is an element of those structures. The
27503 displayed string is the flattened string. */
27504 tmp_glyph = row_start_glyph;
27505 while (tmp_glyph < glyph
27506 && (!(EQ (tmp_glyph->object, glyph->object)
27507 && begpos <= tmp_glyph->charpos
27508 && tmp_glyph->charpos < endpos)))
27509 tmp_glyph++;
27510 gpos = glyph - tmp_glyph;
27511
27512 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27513 the highlighted part of the displayed string to which
27514 GLYPH belongs. Note: GSEQ_LENGTH is different from
27515 SCHARS (STRING), because the latter returns the length of
27516 the internal string. */
27517 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27518 tmp_glyph > glyph
27519 && (!(EQ (tmp_glyph->object, glyph->object)
27520 && begpos <= tmp_glyph->charpos
27521 && tmp_glyph->charpos < endpos));
27522 tmp_glyph--)
27523 ;
27524 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27525
27526 /* Calculate the total pixel width of all the glyphs between
27527 the beginning of the highlighted area and GLYPH. */
27528 total_pixel_width = 0;
27529 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27530 total_pixel_width += tmp_glyph->pixel_width;
27531
27532 /* Pre calculation of re-rendering position. Note: X is in
27533 column units here, after the call to mode_line_string or
27534 marginal_area_string. */
27535 hpos = x - gpos;
27536 vpos = (area == ON_MODE_LINE
27537 ? (w->current_matrix)->nrows - 1
27538 : 0);
27539
27540 /* If GLYPH's position is included in the region that is
27541 already drawn in mouse face, we have nothing to do. */
27542 if ( EQ (window, hlinfo->mouse_face_window)
27543 && (!row->reversed_p
27544 ? (hlinfo->mouse_face_beg_col <= hpos
27545 && hpos < hlinfo->mouse_face_end_col)
27546 /* In R2L rows we swap BEG and END, see below. */
27547 : (hlinfo->mouse_face_end_col <= hpos
27548 && hpos < hlinfo->mouse_face_beg_col))
27549 && hlinfo->mouse_face_beg_row == vpos )
27550 return;
27551
27552 if (clear_mouse_face (hlinfo))
27553 cursor = No_Cursor;
27554
27555 if (!row->reversed_p)
27556 {
27557 hlinfo->mouse_face_beg_col = hpos;
27558 hlinfo->mouse_face_beg_x = original_x_pixel
27559 - (total_pixel_width + dx);
27560 hlinfo->mouse_face_end_col = hpos + gseq_length;
27561 hlinfo->mouse_face_end_x = 0;
27562 }
27563 else
27564 {
27565 /* In R2L rows, show_mouse_face expects BEG and END
27566 coordinates to be swapped. */
27567 hlinfo->mouse_face_end_col = hpos;
27568 hlinfo->mouse_face_end_x = original_x_pixel
27569 - (total_pixel_width + dx);
27570 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27571 hlinfo->mouse_face_beg_x = 0;
27572 }
27573
27574 hlinfo->mouse_face_beg_row = vpos;
27575 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27576 hlinfo->mouse_face_beg_y = 0;
27577 hlinfo->mouse_face_end_y = 0;
27578 hlinfo->mouse_face_past_end = 0;
27579 hlinfo->mouse_face_window = window;
27580
27581 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27582 charpos,
27583 0, 0, 0,
27584 &ignore,
27585 glyph->face_id,
27586 1);
27587 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27588
27589 if (NILP (pointer))
27590 pointer = Qhand;
27591 }
27592 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27593 clear_mouse_face (hlinfo);
27594 }
27595 #ifdef HAVE_WINDOW_SYSTEM
27596 if (FRAME_WINDOW_P (f))
27597 define_frame_cursor1 (f, cursor, pointer);
27598 #endif
27599 }
27600
27601
27602 /* EXPORT:
27603 Take proper action when the mouse has moved to position X, Y on
27604 frame F as regards highlighting characters that have mouse-face
27605 properties. Also de-highlighting chars where the mouse was before.
27606 X and Y can be negative or out of range. */
27607
27608 void
27609 note_mouse_highlight (struct frame *f, int x, int y)
27610 {
27611 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27612 enum window_part part = ON_NOTHING;
27613 Lisp_Object window;
27614 struct window *w;
27615 Cursor cursor = No_Cursor;
27616 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27617 struct buffer *b;
27618
27619 /* When a menu is active, don't highlight because this looks odd. */
27620 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27621 if (popup_activated ())
27622 return;
27623 #endif
27624
27625 if (NILP (Vmouse_highlight)
27626 || !f->glyphs_initialized_p
27627 || f->pointer_invisible)
27628 return;
27629
27630 hlinfo->mouse_face_mouse_x = x;
27631 hlinfo->mouse_face_mouse_y = y;
27632 hlinfo->mouse_face_mouse_frame = f;
27633
27634 if (hlinfo->mouse_face_defer)
27635 return;
27636
27637 if (gc_in_progress)
27638 {
27639 hlinfo->mouse_face_deferred_gc = 1;
27640 return;
27641 }
27642
27643 /* Which window is that in? */
27644 window = window_from_coordinates (f, x, y, &part, 1);
27645
27646 /* If displaying active text in another window, clear that. */
27647 if (! EQ (window, hlinfo->mouse_face_window)
27648 /* Also clear if we move out of text area in same window. */
27649 || (!NILP (hlinfo->mouse_face_window)
27650 && !NILP (window)
27651 && part != ON_TEXT
27652 && part != ON_MODE_LINE
27653 && part != ON_HEADER_LINE))
27654 clear_mouse_face (hlinfo);
27655
27656 /* Not on a window -> return. */
27657 if (!WINDOWP (window))
27658 return;
27659
27660 /* Reset help_echo_string. It will get recomputed below. */
27661 help_echo_string = Qnil;
27662
27663 /* Convert to window-relative pixel coordinates. */
27664 w = XWINDOW (window);
27665 frame_to_window_pixel_xy (w, &x, &y);
27666
27667 #ifdef HAVE_WINDOW_SYSTEM
27668 /* Handle tool-bar window differently since it doesn't display a
27669 buffer. */
27670 if (EQ (window, f->tool_bar_window))
27671 {
27672 note_tool_bar_highlight (f, x, y);
27673 return;
27674 }
27675 #endif
27676
27677 /* Mouse is on the mode, header line or margin? */
27678 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27679 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27680 {
27681 note_mode_line_or_margin_highlight (window, x, y, part);
27682 return;
27683 }
27684
27685 #ifdef HAVE_WINDOW_SYSTEM
27686 if (part == ON_VERTICAL_BORDER)
27687 {
27688 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27689 help_echo_string = build_string ("drag-mouse-1: resize");
27690 }
27691 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27692 || part == ON_SCROLL_BAR)
27693 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27694 else
27695 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27696 #endif
27697
27698 /* Are we in a window whose display is up to date?
27699 And verify the buffer's text has not changed. */
27700 b = XBUFFER (w->buffer);
27701 if (part == ON_TEXT
27702 && EQ (w->window_end_valid, w->buffer)
27703 && w->last_modified == BUF_MODIFF (b)
27704 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27705 {
27706 int hpos, vpos, dx, dy, area = LAST_AREA;
27707 ptrdiff_t pos;
27708 struct glyph *glyph;
27709 Lisp_Object object;
27710 Lisp_Object mouse_face = Qnil, position;
27711 Lisp_Object *overlay_vec = NULL;
27712 ptrdiff_t i, noverlays;
27713 struct buffer *obuf;
27714 ptrdiff_t obegv, ozv;
27715 int same_region;
27716
27717 /* Find the glyph under X/Y. */
27718 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27719
27720 #ifdef HAVE_WINDOW_SYSTEM
27721 /* Look for :pointer property on image. */
27722 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27723 {
27724 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27725 if (img != NULL && IMAGEP (img->spec))
27726 {
27727 Lisp_Object image_map, hotspot;
27728 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27729 !NILP (image_map))
27730 && (hotspot = find_hot_spot (image_map,
27731 glyph->slice.img.x + dx,
27732 glyph->slice.img.y + dy),
27733 CONSP (hotspot))
27734 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27735 {
27736 Lisp_Object plist;
27737
27738 /* Could check XCAR (hotspot) to see if we enter/leave
27739 this hot-spot.
27740 If so, we could look for mouse-enter, mouse-leave
27741 properties in PLIST (and do something...). */
27742 hotspot = XCDR (hotspot);
27743 if (CONSP (hotspot)
27744 && (plist = XCAR (hotspot), CONSP (plist)))
27745 {
27746 pointer = Fplist_get (plist, Qpointer);
27747 if (NILP (pointer))
27748 pointer = Qhand;
27749 help_echo_string = Fplist_get (plist, Qhelp_echo);
27750 if (!NILP (help_echo_string))
27751 {
27752 help_echo_window = window;
27753 help_echo_object = glyph->object;
27754 help_echo_pos = glyph->charpos;
27755 }
27756 }
27757 }
27758 if (NILP (pointer))
27759 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27760 }
27761 }
27762 #endif /* HAVE_WINDOW_SYSTEM */
27763
27764 /* Clear mouse face if X/Y not over text. */
27765 if (glyph == NULL
27766 || area != TEXT_AREA
27767 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27768 /* Glyph's OBJECT is an integer for glyphs inserted by the
27769 display engine for its internal purposes, like truncation
27770 and continuation glyphs and blanks beyond the end of
27771 line's text on text terminals. If we are over such a
27772 glyph, we are not over any text. */
27773 || INTEGERP (glyph->object)
27774 /* R2L rows have a stretch glyph at their front, which
27775 stands for no text, whereas L2R rows have no glyphs at
27776 all beyond the end of text. Treat such stretch glyphs
27777 like we do with NULL glyphs in L2R rows. */
27778 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27779 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27780 && glyph->type == STRETCH_GLYPH
27781 && glyph->avoid_cursor_p))
27782 {
27783 if (clear_mouse_face (hlinfo))
27784 cursor = No_Cursor;
27785 #ifdef HAVE_WINDOW_SYSTEM
27786 if (FRAME_WINDOW_P (f) && NILP (pointer))
27787 {
27788 if (area != TEXT_AREA)
27789 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27790 else
27791 pointer = Vvoid_text_area_pointer;
27792 }
27793 #endif
27794 goto set_cursor;
27795 }
27796
27797 pos = glyph->charpos;
27798 object = glyph->object;
27799 if (!STRINGP (object) && !BUFFERP (object))
27800 goto set_cursor;
27801
27802 /* If we get an out-of-range value, return now; avoid an error. */
27803 if (BUFFERP (object) && pos > BUF_Z (b))
27804 goto set_cursor;
27805
27806 /* Make the window's buffer temporarily current for
27807 overlays_at and compute_char_face. */
27808 obuf = current_buffer;
27809 current_buffer = b;
27810 obegv = BEGV;
27811 ozv = ZV;
27812 BEGV = BEG;
27813 ZV = Z;
27814
27815 /* Is this char mouse-active or does it have help-echo? */
27816 position = make_number (pos);
27817
27818 if (BUFFERP (object))
27819 {
27820 /* Put all the overlays we want in a vector in overlay_vec. */
27821 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27822 /* Sort overlays into increasing priority order. */
27823 noverlays = sort_overlays (overlay_vec, noverlays, w);
27824 }
27825 else
27826 noverlays = 0;
27827
27828 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27829
27830 if (same_region)
27831 cursor = No_Cursor;
27832
27833 /* Check mouse-face highlighting. */
27834 if (! same_region
27835 /* If there exists an overlay with mouse-face overlapping
27836 the one we are currently highlighting, we have to
27837 check if we enter the overlapping overlay, and then
27838 highlight only that. */
27839 || (OVERLAYP (hlinfo->mouse_face_overlay)
27840 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27841 {
27842 /* Find the highest priority overlay with a mouse-face. */
27843 Lisp_Object overlay = Qnil;
27844 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27845 {
27846 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27847 if (!NILP (mouse_face))
27848 overlay = overlay_vec[i];
27849 }
27850
27851 /* If we're highlighting the same overlay as before, there's
27852 no need to do that again. */
27853 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27854 goto check_help_echo;
27855 hlinfo->mouse_face_overlay = overlay;
27856
27857 /* Clear the display of the old active region, if any. */
27858 if (clear_mouse_face (hlinfo))
27859 cursor = No_Cursor;
27860
27861 /* If no overlay applies, get a text property. */
27862 if (NILP (overlay))
27863 mouse_face = Fget_text_property (position, Qmouse_face, object);
27864
27865 /* Next, compute the bounds of the mouse highlighting and
27866 display it. */
27867 if (!NILP (mouse_face) && STRINGP (object))
27868 {
27869 /* The mouse-highlighting comes from a display string
27870 with a mouse-face. */
27871 Lisp_Object s, e;
27872 ptrdiff_t ignore;
27873
27874 s = Fprevious_single_property_change
27875 (make_number (pos + 1), Qmouse_face, object, Qnil);
27876 e = Fnext_single_property_change
27877 (position, Qmouse_face, object, Qnil);
27878 if (NILP (s))
27879 s = make_number (0);
27880 if (NILP (e))
27881 e = make_number (SCHARS (object) - 1);
27882 mouse_face_from_string_pos (w, hlinfo, object,
27883 XINT (s), XINT (e));
27884 hlinfo->mouse_face_past_end = 0;
27885 hlinfo->mouse_face_window = window;
27886 hlinfo->mouse_face_face_id
27887 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27888 glyph->face_id, 1);
27889 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27890 cursor = No_Cursor;
27891 }
27892 else
27893 {
27894 /* The mouse-highlighting, if any, comes from an overlay
27895 or text property in the buffer. */
27896 Lisp_Object buffer IF_LINT (= Qnil);
27897 Lisp_Object disp_string IF_LINT (= Qnil);
27898
27899 if (STRINGP (object))
27900 {
27901 /* If we are on a display string with no mouse-face,
27902 check if the text under it has one. */
27903 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27904 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27905 pos = string_buffer_position (object, start);
27906 if (pos > 0)
27907 {
27908 mouse_face = get_char_property_and_overlay
27909 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27910 buffer = w->buffer;
27911 disp_string = object;
27912 }
27913 }
27914 else
27915 {
27916 buffer = object;
27917 disp_string = Qnil;
27918 }
27919
27920 if (!NILP (mouse_face))
27921 {
27922 Lisp_Object before, after;
27923 Lisp_Object before_string, after_string;
27924 /* To correctly find the limits of mouse highlight
27925 in a bidi-reordered buffer, we must not use the
27926 optimization of limiting the search in
27927 previous-single-property-change and
27928 next-single-property-change, because
27929 rows_from_pos_range needs the real start and end
27930 positions to DTRT in this case. That's because
27931 the first row visible in a window does not
27932 necessarily display the character whose position
27933 is the smallest. */
27934 Lisp_Object lim1 =
27935 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27936 ? Fmarker_position (w->start)
27937 : Qnil;
27938 Lisp_Object lim2 =
27939 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27940 ? make_number (BUF_Z (XBUFFER (buffer))
27941 - XFASTINT (w->window_end_pos))
27942 : Qnil;
27943
27944 if (NILP (overlay))
27945 {
27946 /* Handle the text property case. */
27947 before = Fprevious_single_property_change
27948 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27949 after = Fnext_single_property_change
27950 (make_number (pos), Qmouse_face, buffer, lim2);
27951 before_string = after_string = Qnil;
27952 }
27953 else
27954 {
27955 /* Handle the overlay case. */
27956 before = Foverlay_start (overlay);
27957 after = Foverlay_end (overlay);
27958 before_string = Foverlay_get (overlay, Qbefore_string);
27959 after_string = Foverlay_get (overlay, Qafter_string);
27960
27961 if (!STRINGP (before_string)) before_string = Qnil;
27962 if (!STRINGP (after_string)) after_string = Qnil;
27963 }
27964
27965 mouse_face_from_buffer_pos (window, hlinfo, pos,
27966 NILP (before)
27967 ? 1
27968 : XFASTINT (before),
27969 NILP (after)
27970 ? BUF_Z (XBUFFER (buffer))
27971 : XFASTINT (after),
27972 before_string, after_string,
27973 disp_string);
27974 cursor = No_Cursor;
27975 }
27976 }
27977 }
27978
27979 check_help_echo:
27980
27981 /* Look for a `help-echo' property. */
27982 if (NILP (help_echo_string)) {
27983 Lisp_Object help, overlay;
27984
27985 /* Check overlays first. */
27986 help = overlay = Qnil;
27987 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27988 {
27989 overlay = overlay_vec[i];
27990 help = Foverlay_get (overlay, Qhelp_echo);
27991 }
27992
27993 if (!NILP (help))
27994 {
27995 help_echo_string = help;
27996 help_echo_window = window;
27997 help_echo_object = overlay;
27998 help_echo_pos = pos;
27999 }
28000 else
28001 {
28002 Lisp_Object obj = glyph->object;
28003 ptrdiff_t charpos = glyph->charpos;
28004
28005 /* Try text properties. */
28006 if (STRINGP (obj)
28007 && charpos >= 0
28008 && charpos < SCHARS (obj))
28009 {
28010 help = Fget_text_property (make_number (charpos),
28011 Qhelp_echo, obj);
28012 if (NILP (help))
28013 {
28014 /* If the string itself doesn't specify a help-echo,
28015 see if the buffer text ``under'' it does. */
28016 struct glyph_row *r
28017 = MATRIX_ROW (w->current_matrix, vpos);
28018 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28019 ptrdiff_t p = string_buffer_position (obj, start);
28020 if (p > 0)
28021 {
28022 help = Fget_char_property (make_number (p),
28023 Qhelp_echo, w->buffer);
28024 if (!NILP (help))
28025 {
28026 charpos = p;
28027 obj = w->buffer;
28028 }
28029 }
28030 }
28031 }
28032 else if (BUFFERP (obj)
28033 && charpos >= BEGV
28034 && charpos < ZV)
28035 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28036 obj);
28037
28038 if (!NILP (help))
28039 {
28040 help_echo_string = help;
28041 help_echo_window = window;
28042 help_echo_object = obj;
28043 help_echo_pos = charpos;
28044 }
28045 }
28046 }
28047
28048 #ifdef HAVE_WINDOW_SYSTEM
28049 /* Look for a `pointer' property. */
28050 if (FRAME_WINDOW_P (f) && NILP (pointer))
28051 {
28052 /* Check overlays first. */
28053 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28054 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28055
28056 if (NILP (pointer))
28057 {
28058 Lisp_Object obj = glyph->object;
28059 ptrdiff_t charpos = glyph->charpos;
28060
28061 /* Try text properties. */
28062 if (STRINGP (obj)
28063 && charpos >= 0
28064 && charpos < SCHARS (obj))
28065 {
28066 pointer = Fget_text_property (make_number (charpos),
28067 Qpointer, obj);
28068 if (NILP (pointer))
28069 {
28070 /* If the string itself doesn't specify a pointer,
28071 see if the buffer text ``under'' it does. */
28072 struct glyph_row *r
28073 = MATRIX_ROW (w->current_matrix, vpos);
28074 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28075 ptrdiff_t p = string_buffer_position (obj, start);
28076 if (p > 0)
28077 pointer = Fget_char_property (make_number (p),
28078 Qpointer, w->buffer);
28079 }
28080 }
28081 else if (BUFFERP (obj)
28082 && charpos >= BEGV
28083 && charpos < ZV)
28084 pointer = Fget_text_property (make_number (charpos),
28085 Qpointer, obj);
28086 }
28087 }
28088 #endif /* HAVE_WINDOW_SYSTEM */
28089
28090 BEGV = obegv;
28091 ZV = ozv;
28092 current_buffer = obuf;
28093 }
28094
28095 set_cursor:
28096
28097 #ifdef HAVE_WINDOW_SYSTEM
28098 if (FRAME_WINDOW_P (f))
28099 define_frame_cursor1 (f, cursor, pointer);
28100 #else
28101 /* This is here to prevent a compiler error, about "label at end of
28102 compound statement". */
28103 return;
28104 #endif
28105 }
28106
28107
28108 /* EXPORT for RIF:
28109 Clear any mouse-face on window W. This function is part of the
28110 redisplay interface, and is called from try_window_id and similar
28111 functions to ensure the mouse-highlight is off. */
28112
28113 void
28114 x_clear_window_mouse_face (struct window *w)
28115 {
28116 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28117 Lisp_Object window;
28118
28119 BLOCK_INPUT;
28120 XSETWINDOW (window, w);
28121 if (EQ (window, hlinfo->mouse_face_window))
28122 clear_mouse_face (hlinfo);
28123 UNBLOCK_INPUT;
28124 }
28125
28126
28127 /* EXPORT:
28128 Just discard the mouse face information for frame F, if any.
28129 This is used when the size of F is changed. */
28130
28131 void
28132 cancel_mouse_face (struct frame *f)
28133 {
28134 Lisp_Object window;
28135 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28136
28137 window = hlinfo->mouse_face_window;
28138 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28139 {
28140 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28141 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28142 hlinfo->mouse_face_window = Qnil;
28143 }
28144 }
28145
28146
28147 \f
28148 /***********************************************************************
28149 Exposure Events
28150 ***********************************************************************/
28151
28152 #ifdef HAVE_WINDOW_SYSTEM
28153
28154 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28155 which intersects rectangle R. R is in window-relative coordinates. */
28156
28157 static void
28158 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28159 enum glyph_row_area area)
28160 {
28161 struct glyph *first = row->glyphs[area];
28162 struct glyph *end = row->glyphs[area] + row->used[area];
28163 struct glyph *last;
28164 int first_x, start_x, x;
28165
28166 if (area == TEXT_AREA && row->fill_line_p)
28167 /* If row extends face to end of line write the whole line. */
28168 draw_glyphs (w, 0, row, area,
28169 0, row->used[area],
28170 DRAW_NORMAL_TEXT, 0);
28171 else
28172 {
28173 /* Set START_X to the window-relative start position for drawing glyphs of
28174 AREA. The first glyph of the text area can be partially visible.
28175 The first glyphs of other areas cannot. */
28176 start_x = window_box_left_offset (w, area);
28177 x = start_x;
28178 if (area == TEXT_AREA)
28179 x += row->x;
28180
28181 /* Find the first glyph that must be redrawn. */
28182 while (first < end
28183 && x + first->pixel_width < r->x)
28184 {
28185 x += first->pixel_width;
28186 ++first;
28187 }
28188
28189 /* Find the last one. */
28190 last = first;
28191 first_x = x;
28192 while (last < end
28193 && x < r->x + r->width)
28194 {
28195 x += last->pixel_width;
28196 ++last;
28197 }
28198
28199 /* Repaint. */
28200 if (last > first)
28201 draw_glyphs (w, first_x - start_x, row, area,
28202 first - row->glyphs[area], last - row->glyphs[area],
28203 DRAW_NORMAL_TEXT, 0);
28204 }
28205 }
28206
28207
28208 /* Redraw the parts of the glyph row ROW on window W intersecting
28209 rectangle R. R is in window-relative coordinates. Value is
28210 non-zero if mouse-face was overwritten. */
28211
28212 static int
28213 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28214 {
28215 eassert (row->enabled_p);
28216
28217 if (row->mode_line_p || w->pseudo_window_p)
28218 draw_glyphs (w, 0, row, TEXT_AREA,
28219 0, row->used[TEXT_AREA],
28220 DRAW_NORMAL_TEXT, 0);
28221 else
28222 {
28223 if (row->used[LEFT_MARGIN_AREA])
28224 expose_area (w, row, r, LEFT_MARGIN_AREA);
28225 if (row->used[TEXT_AREA])
28226 expose_area (w, row, r, TEXT_AREA);
28227 if (row->used[RIGHT_MARGIN_AREA])
28228 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28229 draw_row_fringe_bitmaps (w, row);
28230 }
28231
28232 return row->mouse_face_p;
28233 }
28234
28235
28236 /* Redraw those parts of glyphs rows during expose event handling that
28237 overlap other rows. Redrawing of an exposed line writes over parts
28238 of lines overlapping that exposed line; this function fixes that.
28239
28240 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28241 row in W's current matrix that is exposed and overlaps other rows.
28242 LAST_OVERLAPPING_ROW is the last such row. */
28243
28244 static void
28245 expose_overlaps (struct window *w,
28246 struct glyph_row *first_overlapping_row,
28247 struct glyph_row *last_overlapping_row,
28248 XRectangle *r)
28249 {
28250 struct glyph_row *row;
28251
28252 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28253 if (row->overlapping_p)
28254 {
28255 eassert (row->enabled_p && !row->mode_line_p);
28256
28257 row->clip = r;
28258 if (row->used[LEFT_MARGIN_AREA])
28259 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28260
28261 if (row->used[TEXT_AREA])
28262 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28263
28264 if (row->used[RIGHT_MARGIN_AREA])
28265 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28266 row->clip = NULL;
28267 }
28268 }
28269
28270
28271 /* Return non-zero if W's cursor intersects rectangle R. */
28272
28273 static int
28274 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28275 {
28276 XRectangle cr, result;
28277 struct glyph *cursor_glyph;
28278 struct glyph_row *row;
28279
28280 if (w->phys_cursor.vpos >= 0
28281 && w->phys_cursor.vpos < w->current_matrix->nrows
28282 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28283 row->enabled_p)
28284 && row->cursor_in_fringe_p)
28285 {
28286 /* Cursor is in the fringe. */
28287 cr.x = window_box_right_offset (w,
28288 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28289 ? RIGHT_MARGIN_AREA
28290 : TEXT_AREA));
28291 cr.y = row->y;
28292 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28293 cr.height = row->height;
28294 return x_intersect_rectangles (&cr, r, &result);
28295 }
28296
28297 cursor_glyph = get_phys_cursor_glyph (w);
28298 if (cursor_glyph)
28299 {
28300 /* r is relative to W's box, but w->phys_cursor.x is relative
28301 to left edge of W's TEXT area. Adjust it. */
28302 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28303 cr.y = w->phys_cursor.y;
28304 cr.width = cursor_glyph->pixel_width;
28305 cr.height = w->phys_cursor_height;
28306 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28307 I assume the effect is the same -- and this is portable. */
28308 return x_intersect_rectangles (&cr, r, &result);
28309 }
28310 /* If we don't understand the format, pretend we're not in the hot-spot. */
28311 return 0;
28312 }
28313
28314
28315 /* EXPORT:
28316 Draw a vertical window border to the right of window W if W doesn't
28317 have vertical scroll bars. */
28318
28319 void
28320 x_draw_vertical_border (struct window *w)
28321 {
28322 struct frame *f = XFRAME (WINDOW_FRAME (w));
28323
28324 /* We could do better, if we knew what type of scroll-bar the adjacent
28325 windows (on either side) have... But we don't :-(
28326 However, I think this works ok. ++KFS 2003-04-25 */
28327
28328 /* Redraw borders between horizontally adjacent windows. Don't
28329 do it for frames with vertical scroll bars because either the
28330 right scroll bar of a window, or the left scroll bar of its
28331 neighbor will suffice as a border. */
28332 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28333 return;
28334
28335 if (!WINDOW_RIGHTMOST_P (w)
28336 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28337 {
28338 int x0, x1, y0, y1;
28339
28340 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28341 y1 -= 1;
28342
28343 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28344 x1 -= 1;
28345
28346 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28347 }
28348 else if (!WINDOW_LEFTMOST_P (w)
28349 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28350 {
28351 int x0, x1, y0, y1;
28352
28353 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28354 y1 -= 1;
28355
28356 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28357 x0 -= 1;
28358
28359 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28360 }
28361 }
28362
28363
28364 /* Redraw the part of window W intersection rectangle FR. Pixel
28365 coordinates in FR are frame-relative. Call this function with
28366 input blocked. Value is non-zero if the exposure overwrites
28367 mouse-face. */
28368
28369 static int
28370 expose_window (struct window *w, XRectangle *fr)
28371 {
28372 struct frame *f = XFRAME (w->frame);
28373 XRectangle wr, r;
28374 int mouse_face_overwritten_p = 0;
28375
28376 /* If window is not yet fully initialized, do nothing. This can
28377 happen when toolkit scroll bars are used and a window is split.
28378 Reconfiguring the scroll bar will generate an expose for a newly
28379 created window. */
28380 if (w->current_matrix == NULL)
28381 return 0;
28382
28383 /* When we're currently updating the window, display and current
28384 matrix usually don't agree. Arrange for a thorough display
28385 later. */
28386 if (w == updated_window)
28387 {
28388 SET_FRAME_GARBAGED (f);
28389 return 0;
28390 }
28391
28392 /* Frame-relative pixel rectangle of W. */
28393 wr.x = WINDOW_LEFT_EDGE_X (w);
28394 wr.y = WINDOW_TOP_EDGE_Y (w);
28395 wr.width = WINDOW_TOTAL_WIDTH (w);
28396 wr.height = WINDOW_TOTAL_HEIGHT (w);
28397
28398 if (x_intersect_rectangles (fr, &wr, &r))
28399 {
28400 int yb = window_text_bottom_y (w);
28401 struct glyph_row *row;
28402 int cursor_cleared_p, phys_cursor_on_p;
28403 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28404
28405 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28406 r.x, r.y, r.width, r.height));
28407
28408 /* Convert to window coordinates. */
28409 r.x -= WINDOW_LEFT_EDGE_X (w);
28410 r.y -= WINDOW_TOP_EDGE_Y (w);
28411
28412 /* Turn off the cursor. */
28413 if (!w->pseudo_window_p
28414 && phys_cursor_in_rect_p (w, &r))
28415 {
28416 x_clear_cursor (w);
28417 cursor_cleared_p = 1;
28418 }
28419 else
28420 cursor_cleared_p = 0;
28421
28422 /* If the row containing the cursor extends face to end of line,
28423 then expose_area might overwrite the cursor outside the
28424 rectangle and thus notice_overwritten_cursor might clear
28425 w->phys_cursor_on_p. We remember the original value and
28426 check later if it is changed. */
28427 phys_cursor_on_p = w->phys_cursor_on_p;
28428
28429 /* Update lines intersecting rectangle R. */
28430 first_overlapping_row = last_overlapping_row = NULL;
28431 for (row = w->current_matrix->rows;
28432 row->enabled_p;
28433 ++row)
28434 {
28435 int y0 = row->y;
28436 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28437
28438 if ((y0 >= r.y && y0 < r.y + r.height)
28439 || (y1 > r.y && y1 < r.y + r.height)
28440 || (r.y >= y0 && r.y < y1)
28441 || (r.y + r.height > y0 && r.y + r.height < y1))
28442 {
28443 /* A header line may be overlapping, but there is no need
28444 to fix overlapping areas for them. KFS 2005-02-12 */
28445 if (row->overlapping_p && !row->mode_line_p)
28446 {
28447 if (first_overlapping_row == NULL)
28448 first_overlapping_row = row;
28449 last_overlapping_row = row;
28450 }
28451
28452 row->clip = fr;
28453 if (expose_line (w, row, &r))
28454 mouse_face_overwritten_p = 1;
28455 row->clip = NULL;
28456 }
28457 else if (row->overlapping_p)
28458 {
28459 /* We must redraw a row overlapping the exposed area. */
28460 if (y0 < r.y
28461 ? y0 + row->phys_height > r.y
28462 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28463 {
28464 if (first_overlapping_row == NULL)
28465 first_overlapping_row = row;
28466 last_overlapping_row = row;
28467 }
28468 }
28469
28470 if (y1 >= yb)
28471 break;
28472 }
28473
28474 /* Display the mode line if there is one. */
28475 if (WINDOW_WANTS_MODELINE_P (w)
28476 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28477 row->enabled_p)
28478 && row->y < r.y + r.height)
28479 {
28480 if (expose_line (w, row, &r))
28481 mouse_face_overwritten_p = 1;
28482 }
28483
28484 if (!w->pseudo_window_p)
28485 {
28486 /* Fix the display of overlapping rows. */
28487 if (first_overlapping_row)
28488 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28489 fr);
28490
28491 /* Draw border between windows. */
28492 x_draw_vertical_border (w);
28493
28494 /* Turn the cursor on again. */
28495 if (cursor_cleared_p
28496 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28497 update_window_cursor (w, 1);
28498 }
28499 }
28500
28501 return mouse_face_overwritten_p;
28502 }
28503
28504
28505
28506 /* Redraw (parts) of all windows in the window tree rooted at W that
28507 intersect R. R contains frame pixel coordinates. Value is
28508 non-zero if the exposure overwrites mouse-face. */
28509
28510 static int
28511 expose_window_tree (struct window *w, XRectangle *r)
28512 {
28513 struct frame *f = XFRAME (w->frame);
28514 int mouse_face_overwritten_p = 0;
28515
28516 while (w && !FRAME_GARBAGED_P (f))
28517 {
28518 if (!NILP (w->hchild))
28519 mouse_face_overwritten_p
28520 |= expose_window_tree (XWINDOW (w->hchild), r);
28521 else if (!NILP (w->vchild))
28522 mouse_face_overwritten_p
28523 |= expose_window_tree (XWINDOW (w->vchild), r);
28524 else
28525 mouse_face_overwritten_p |= expose_window (w, r);
28526
28527 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28528 }
28529
28530 return mouse_face_overwritten_p;
28531 }
28532
28533
28534 /* EXPORT:
28535 Redisplay an exposed area of frame F. X and Y are the upper-left
28536 corner of the exposed rectangle. W and H are width and height of
28537 the exposed area. All are pixel values. W or H zero means redraw
28538 the entire frame. */
28539
28540 void
28541 expose_frame (struct frame *f, int x, int y, int w, int h)
28542 {
28543 XRectangle r;
28544 int mouse_face_overwritten_p = 0;
28545
28546 TRACE ((stderr, "expose_frame "));
28547
28548 /* No need to redraw if frame will be redrawn soon. */
28549 if (FRAME_GARBAGED_P (f))
28550 {
28551 TRACE ((stderr, " garbaged\n"));
28552 return;
28553 }
28554
28555 /* If basic faces haven't been realized yet, there is no point in
28556 trying to redraw anything. This can happen when we get an expose
28557 event while Emacs is starting, e.g. by moving another window. */
28558 if (FRAME_FACE_CACHE (f) == NULL
28559 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28560 {
28561 TRACE ((stderr, " no faces\n"));
28562 return;
28563 }
28564
28565 if (w == 0 || h == 0)
28566 {
28567 r.x = r.y = 0;
28568 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28569 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28570 }
28571 else
28572 {
28573 r.x = x;
28574 r.y = y;
28575 r.width = w;
28576 r.height = h;
28577 }
28578
28579 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28580 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28581
28582 if (WINDOWP (f->tool_bar_window))
28583 mouse_face_overwritten_p
28584 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28585
28586 #ifdef HAVE_X_WINDOWS
28587 #ifndef MSDOS
28588 #ifndef USE_X_TOOLKIT
28589 if (WINDOWP (f->menu_bar_window))
28590 mouse_face_overwritten_p
28591 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28592 #endif /* not USE_X_TOOLKIT */
28593 #endif
28594 #endif
28595
28596 /* Some window managers support a focus-follows-mouse style with
28597 delayed raising of frames. Imagine a partially obscured frame,
28598 and moving the mouse into partially obscured mouse-face on that
28599 frame. The visible part of the mouse-face will be highlighted,
28600 then the WM raises the obscured frame. With at least one WM, KDE
28601 2.1, Emacs is not getting any event for the raising of the frame
28602 (even tried with SubstructureRedirectMask), only Expose events.
28603 These expose events will draw text normally, i.e. not
28604 highlighted. Which means we must redo the highlight here.
28605 Subsume it under ``we love X''. --gerd 2001-08-15 */
28606 /* Included in Windows version because Windows most likely does not
28607 do the right thing if any third party tool offers
28608 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28609 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28610 {
28611 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28612 if (f == hlinfo->mouse_face_mouse_frame)
28613 {
28614 int mouse_x = hlinfo->mouse_face_mouse_x;
28615 int mouse_y = hlinfo->mouse_face_mouse_y;
28616 clear_mouse_face (hlinfo);
28617 note_mouse_highlight (f, mouse_x, mouse_y);
28618 }
28619 }
28620 }
28621
28622
28623 /* EXPORT:
28624 Determine the intersection of two rectangles R1 and R2. Return
28625 the intersection in *RESULT. Value is non-zero if RESULT is not
28626 empty. */
28627
28628 int
28629 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28630 {
28631 XRectangle *left, *right;
28632 XRectangle *upper, *lower;
28633 int intersection_p = 0;
28634
28635 /* Rearrange so that R1 is the left-most rectangle. */
28636 if (r1->x < r2->x)
28637 left = r1, right = r2;
28638 else
28639 left = r2, right = r1;
28640
28641 /* X0 of the intersection is right.x0, if this is inside R1,
28642 otherwise there is no intersection. */
28643 if (right->x <= left->x + left->width)
28644 {
28645 result->x = right->x;
28646
28647 /* The right end of the intersection is the minimum of
28648 the right ends of left and right. */
28649 result->width = (min (left->x + left->width, right->x + right->width)
28650 - result->x);
28651
28652 /* Same game for Y. */
28653 if (r1->y < r2->y)
28654 upper = r1, lower = r2;
28655 else
28656 upper = r2, lower = r1;
28657
28658 /* The upper end of the intersection is lower.y0, if this is inside
28659 of upper. Otherwise, there is no intersection. */
28660 if (lower->y <= upper->y + upper->height)
28661 {
28662 result->y = lower->y;
28663
28664 /* The lower end of the intersection is the minimum of the lower
28665 ends of upper and lower. */
28666 result->height = (min (lower->y + lower->height,
28667 upper->y + upper->height)
28668 - result->y);
28669 intersection_p = 1;
28670 }
28671 }
28672
28673 return intersection_p;
28674 }
28675
28676 #endif /* HAVE_WINDOW_SYSTEM */
28677
28678 \f
28679 /***********************************************************************
28680 Initialization
28681 ***********************************************************************/
28682
28683 void
28684 syms_of_xdisp (void)
28685 {
28686 Vwith_echo_area_save_vector = Qnil;
28687 staticpro (&Vwith_echo_area_save_vector);
28688
28689 Vmessage_stack = Qnil;
28690 staticpro (&Vmessage_stack);
28691
28692 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28693
28694 message_dolog_marker1 = Fmake_marker ();
28695 staticpro (&message_dolog_marker1);
28696 message_dolog_marker2 = Fmake_marker ();
28697 staticpro (&message_dolog_marker2);
28698 message_dolog_marker3 = Fmake_marker ();
28699 staticpro (&message_dolog_marker3);
28700
28701 #ifdef GLYPH_DEBUG
28702 defsubr (&Sdump_frame_glyph_matrix);
28703 defsubr (&Sdump_glyph_matrix);
28704 defsubr (&Sdump_glyph_row);
28705 defsubr (&Sdump_tool_bar_row);
28706 defsubr (&Strace_redisplay);
28707 defsubr (&Strace_to_stderr);
28708 #endif
28709 #ifdef HAVE_WINDOW_SYSTEM
28710 defsubr (&Stool_bar_lines_needed);
28711 defsubr (&Slookup_image_map);
28712 #endif
28713 defsubr (&Sformat_mode_line);
28714 defsubr (&Sinvisible_p);
28715 defsubr (&Scurrent_bidi_paragraph_direction);
28716
28717 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28718 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28719 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28720 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28721 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28722 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28723 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28724 DEFSYM (Qeval, "eval");
28725 DEFSYM (QCdata, ":data");
28726 DEFSYM (Qdisplay, "display");
28727 DEFSYM (Qspace_width, "space-width");
28728 DEFSYM (Qraise, "raise");
28729 DEFSYM (Qslice, "slice");
28730 DEFSYM (Qspace, "space");
28731 DEFSYM (Qmargin, "margin");
28732 DEFSYM (Qpointer, "pointer");
28733 DEFSYM (Qleft_margin, "left-margin");
28734 DEFSYM (Qright_margin, "right-margin");
28735 DEFSYM (Qcenter, "center");
28736 DEFSYM (Qline_height, "line-height");
28737 DEFSYM (QCalign_to, ":align-to");
28738 DEFSYM (QCrelative_width, ":relative-width");
28739 DEFSYM (QCrelative_height, ":relative-height");
28740 DEFSYM (QCeval, ":eval");
28741 DEFSYM (QCpropertize, ":propertize");
28742 DEFSYM (QCfile, ":file");
28743 DEFSYM (Qfontified, "fontified");
28744 DEFSYM (Qfontification_functions, "fontification-functions");
28745 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28746 DEFSYM (Qescape_glyph, "escape-glyph");
28747 DEFSYM (Qnobreak_space, "nobreak-space");
28748 DEFSYM (Qimage, "image");
28749 DEFSYM (Qtext, "text");
28750 DEFSYM (Qboth, "both");
28751 DEFSYM (Qboth_horiz, "both-horiz");
28752 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28753 DEFSYM (QCmap, ":map");
28754 DEFSYM (QCpointer, ":pointer");
28755 DEFSYM (Qrect, "rect");
28756 DEFSYM (Qcircle, "circle");
28757 DEFSYM (Qpoly, "poly");
28758 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28759 DEFSYM (Qgrow_only, "grow-only");
28760 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28761 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28762 DEFSYM (Qposition, "position");
28763 DEFSYM (Qbuffer_position, "buffer-position");
28764 DEFSYM (Qobject, "object");
28765 DEFSYM (Qbar, "bar");
28766 DEFSYM (Qhbar, "hbar");
28767 DEFSYM (Qbox, "box");
28768 DEFSYM (Qhollow, "hollow");
28769 DEFSYM (Qhand, "hand");
28770 DEFSYM (Qarrow, "arrow");
28771 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28772
28773 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28774 Fcons (intern_c_string ("void-variable"), Qnil)),
28775 Qnil);
28776 staticpro (&list_of_error);
28777
28778 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28779 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28780 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28781 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28782
28783 echo_buffer[0] = echo_buffer[1] = Qnil;
28784 staticpro (&echo_buffer[0]);
28785 staticpro (&echo_buffer[1]);
28786
28787 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28788 staticpro (&echo_area_buffer[0]);
28789 staticpro (&echo_area_buffer[1]);
28790
28791 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28792 staticpro (&Vmessages_buffer_name);
28793
28794 mode_line_proptrans_alist = Qnil;
28795 staticpro (&mode_line_proptrans_alist);
28796 mode_line_string_list = Qnil;
28797 staticpro (&mode_line_string_list);
28798 mode_line_string_face = Qnil;
28799 staticpro (&mode_line_string_face);
28800 mode_line_string_face_prop = Qnil;
28801 staticpro (&mode_line_string_face_prop);
28802 Vmode_line_unwind_vector = Qnil;
28803 staticpro (&Vmode_line_unwind_vector);
28804
28805 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28806
28807 help_echo_string = Qnil;
28808 staticpro (&help_echo_string);
28809 help_echo_object = Qnil;
28810 staticpro (&help_echo_object);
28811 help_echo_window = Qnil;
28812 staticpro (&help_echo_window);
28813 previous_help_echo_string = Qnil;
28814 staticpro (&previous_help_echo_string);
28815 help_echo_pos = -1;
28816
28817 DEFSYM (Qright_to_left, "right-to-left");
28818 DEFSYM (Qleft_to_right, "left-to-right");
28819
28820 #ifdef HAVE_WINDOW_SYSTEM
28821 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28822 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28823 For example, if a block cursor is over a tab, it will be drawn as
28824 wide as that tab on the display. */);
28825 x_stretch_cursor_p = 0;
28826 #endif
28827
28828 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28829 doc: /* Non-nil means highlight trailing whitespace.
28830 The face used for trailing whitespace is `trailing-whitespace'. */);
28831 Vshow_trailing_whitespace = Qnil;
28832
28833 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28834 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28835 If the value is t, Emacs highlights non-ASCII chars which have the
28836 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28837 or `escape-glyph' face respectively.
28838
28839 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28840 U+2011 (non-breaking hyphen) are affected.
28841
28842 Any other non-nil value means to display these characters as a escape
28843 glyph followed by an ordinary space or hyphen.
28844
28845 A value of nil means no special handling of these characters. */);
28846 Vnobreak_char_display = Qt;
28847
28848 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28849 doc: /* The pointer shape to show in void text areas.
28850 A value of nil means to show the text pointer. Other options are `arrow',
28851 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28852 Vvoid_text_area_pointer = Qarrow;
28853
28854 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28855 doc: /* Non-nil means don't actually do any redisplay.
28856 This is used for internal purposes. */);
28857 Vinhibit_redisplay = Qnil;
28858
28859 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28860 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28861 Vglobal_mode_string = Qnil;
28862
28863 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28864 doc: /* Marker for where to display an arrow on top of the buffer text.
28865 This must be the beginning of a line in order to work.
28866 See also `overlay-arrow-string'. */);
28867 Voverlay_arrow_position = Qnil;
28868
28869 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28870 doc: /* String to display as an arrow in non-window frames.
28871 See also `overlay-arrow-position'. */);
28872 Voverlay_arrow_string = build_pure_c_string ("=>");
28873
28874 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28875 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28876 The symbols on this list are examined during redisplay to determine
28877 where to display overlay arrows. */);
28878 Voverlay_arrow_variable_list
28879 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28880
28881 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28882 doc: /* The number of lines to try scrolling a window by when point moves out.
28883 If that fails to bring point back on frame, point is centered instead.
28884 If this is zero, point is always centered after it moves off frame.
28885 If you want scrolling to always be a line at a time, you should set
28886 `scroll-conservatively' to a large value rather than set this to 1. */);
28887
28888 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28889 doc: /* Scroll up to this many lines, to bring point back on screen.
28890 If point moves off-screen, redisplay will scroll by up to
28891 `scroll-conservatively' lines in order to bring point just barely
28892 onto the screen again. If that cannot be done, then redisplay
28893 recenters point as usual.
28894
28895 If the value is greater than 100, redisplay will never recenter point,
28896 but will always scroll just enough text to bring point into view, even
28897 if you move far away.
28898
28899 A value of zero means always recenter point if it moves off screen. */);
28900 scroll_conservatively = 0;
28901
28902 DEFVAR_INT ("scroll-margin", scroll_margin,
28903 doc: /* Number of lines of margin at the top and bottom of a window.
28904 Recenter the window whenever point gets within this many lines
28905 of the top or bottom of the window. */);
28906 scroll_margin = 0;
28907
28908 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28909 doc: /* Pixels per inch value for non-window system displays.
28910 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28911 Vdisplay_pixels_per_inch = make_float (72.0);
28912
28913 #ifdef GLYPH_DEBUG
28914 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28915 #endif
28916
28917 DEFVAR_LISP ("truncate-partial-width-windows",
28918 Vtruncate_partial_width_windows,
28919 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28920 For an integer value, truncate lines in each window narrower than the
28921 full frame width, provided the window width is less than that integer;
28922 otherwise, respect the value of `truncate-lines'.
28923
28924 For any other non-nil value, truncate lines in all windows that do
28925 not span the full frame width.
28926
28927 A value of nil means to respect the value of `truncate-lines'.
28928
28929 If `word-wrap' is enabled, you might want to reduce this. */);
28930 Vtruncate_partial_width_windows = make_number (50);
28931
28932 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28933 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28934 Any other value means to use the appropriate face, `mode-line',
28935 `header-line', or `menu' respectively. */);
28936 mode_line_inverse_video = 1;
28937
28938 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28939 doc: /* Maximum buffer size for which line number should be displayed.
28940 If the buffer is bigger than this, the line number does not appear
28941 in the mode line. A value of nil means no limit. */);
28942 Vline_number_display_limit = Qnil;
28943
28944 DEFVAR_INT ("line-number-display-limit-width",
28945 line_number_display_limit_width,
28946 doc: /* Maximum line width (in characters) for line number display.
28947 If the average length of the lines near point is bigger than this, then the
28948 line number may be omitted from the mode line. */);
28949 line_number_display_limit_width = 200;
28950
28951 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28952 doc: /* Non-nil means highlight region even in nonselected windows. */);
28953 highlight_nonselected_windows = 0;
28954
28955 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28956 doc: /* Non-nil if more than one frame is visible on this display.
28957 Minibuffer-only frames don't count, but iconified frames do.
28958 This variable is not guaranteed to be accurate except while processing
28959 `frame-title-format' and `icon-title-format'. */);
28960
28961 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28962 doc: /* Template for displaying the title bar of visible frames.
28963 \(Assuming the window manager supports this feature.)
28964
28965 This variable has the same structure as `mode-line-format', except that
28966 the %c and %l constructs are ignored. It is used only on frames for
28967 which no explicit name has been set \(see `modify-frame-parameters'). */);
28968
28969 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28970 doc: /* Template for displaying the title bar of an iconified frame.
28971 \(Assuming the window manager supports this feature.)
28972 This variable has the same structure as `mode-line-format' (which see),
28973 and is used only on frames for which no explicit name has been set
28974 \(see `modify-frame-parameters'). */);
28975 Vicon_title_format
28976 = Vframe_title_format
28977 = listn (CONSTYPE_PURE, 3,
28978 intern_c_string ("multiple-frames"),
28979 build_pure_c_string ("%b"),
28980 listn (CONSTYPE_PURE, 4,
28981 empty_unibyte_string,
28982 intern_c_string ("invocation-name"),
28983 build_pure_c_string ("@"),
28984 intern_c_string ("system-name")));
28985
28986 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28987 doc: /* Maximum number of lines to keep in the message log buffer.
28988 If nil, disable message logging. If t, log messages but don't truncate
28989 the buffer when it becomes large. */);
28990 Vmessage_log_max = make_number (100);
28991
28992 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28993 doc: /* Functions called before redisplay, if window sizes have changed.
28994 The value should be a list of functions that take one argument.
28995 Just before redisplay, for each frame, if any of its windows have changed
28996 size since the last redisplay, or have been split or deleted,
28997 all the functions in the list are called, with the frame as argument. */);
28998 Vwindow_size_change_functions = Qnil;
28999
29000 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29001 doc: /* List of functions to call before redisplaying a window with scrolling.
29002 Each function is called with two arguments, the window and its new
29003 display-start position. Note that these functions are also called by
29004 `set-window-buffer'. Also note that the value of `window-end' is not
29005 valid when these functions are called.
29006
29007 Warning: Do not use this feature to alter the way the window
29008 is scrolled. It is not designed for that, and such use probably won't
29009 work. */);
29010 Vwindow_scroll_functions = Qnil;
29011
29012 DEFVAR_LISP ("window-text-change-functions",
29013 Vwindow_text_change_functions,
29014 doc: /* Functions to call in redisplay when text in the window might change. */);
29015 Vwindow_text_change_functions = Qnil;
29016
29017 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29018 doc: /* Functions called when redisplay of a window reaches the end trigger.
29019 Each function is called with two arguments, the window and the end trigger value.
29020 See `set-window-redisplay-end-trigger'. */);
29021 Vredisplay_end_trigger_functions = Qnil;
29022
29023 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29024 doc: /* Non-nil means autoselect window with mouse pointer.
29025 If nil, do not autoselect windows.
29026 A positive number means delay autoselection by that many seconds: a
29027 window is autoselected only after the mouse has remained in that
29028 window for the duration of the delay.
29029 A negative number has a similar effect, but causes windows to be
29030 autoselected only after the mouse has stopped moving. \(Because of
29031 the way Emacs compares mouse events, you will occasionally wait twice
29032 that time before the window gets selected.\)
29033 Any other value means to autoselect window instantaneously when the
29034 mouse pointer enters it.
29035
29036 Autoselection selects the minibuffer only if it is active, and never
29037 unselects the minibuffer if it is active.
29038
29039 When customizing this variable make sure that the actual value of
29040 `focus-follows-mouse' matches the behavior of your window manager. */);
29041 Vmouse_autoselect_window = Qnil;
29042
29043 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29044 doc: /* Non-nil means automatically resize tool-bars.
29045 This dynamically changes the tool-bar's height to the minimum height
29046 that is needed to make all tool-bar items visible.
29047 If value is `grow-only', the tool-bar's height is only increased
29048 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29049 Vauto_resize_tool_bars = Qt;
29050
29051 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29052 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29053 auto_raise_tool_bar_buttons_p = 1;
29054
29055 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29056 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29057 make_cursor_line_fully_visible_p = 1;
29058
29059 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29060 doc: /* Border below tool-bar in pixels.
29061 If an integer, use it as the height of the border.
29062 If it is one of `internal-border-width' or `border-width', use the
29063 value of the corresponding frame parameter.
29064 Otherwise, no border is added below the tool-bar. */);
29065 Vtool_bar_border = Qinternal_border_width;
29066
29067 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29068 doc: /* Margin around tool-bar buttons in pixels.
29069 If an integer, use that for both horizontal and vertical margins.
29070 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29071 HORZ specifying the horizontal margin, and VERT specifying the
29072 vertical margin. */);
29073 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29074
29075 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29076 doc: /* Relief thickness of tool-bar buttons. */);
29077 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29078
29079 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29080 doc: /* Tool bar style to use.
29081 It can be one of
29082 image - show images only
29083 text - show text only
29084 both - show both, text below image
29085 both-horiz - show text to the right of the image
29086 text-image-horiz - show text to the left of the image
29087 any other - use system default or image if no system default.
29088
29089 This variable only affects the GTK+ toolkit version of Emacs. */);
29090 Vtool_bar_style = Qnil;
29091
29092 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29093 doc: /* Maximum number of characters a label can have to be shown.
29094 The tool bar style must also show labels for this to have any effect, see
29095 `tool-bar-style'. */);
29096 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29097
29098 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29099 doc: /* List of functions to call to fontify regions of text.
29100 Each function is called with one argument POS. Functions must
29101 fontify a region starting at POS in the current buffer, and give
29102 fontified regions the property `fontified'. */);
29103 Vfontification_functions = Qnil;
29104 Fmake_variable_buffer_local (Qfontification_functions);
29105
29106 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29107 unibyte_display_via_language_environment,
29108 doc: /* Non-nil means display unibyte text according to language environment.
29109 Specifically, this means that raw bytes in the range 160-255 decimal
29110 are displayed by converting them to the equivalent multibyte characters
29111 according to the current language environment. As a result, they are
29112 displayed according to the current fontset.
29113
29114 Note that this variable affects only how these bytes are displayed,
29115 but does not change the fact they are interpreted as raw bytes. */);
29116 unibyte_display_via_language_environment = 0;
29117
29118 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29119 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29120 If a float, it specifies a fraction of the mini-window frame's height.
29121 If an integer, it specifies a number of lines. */);
29122 Vmax_mini_window_height = make_float (0.25);
29123
29124 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29125 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29126 A value of nil means don't automatically resize mini-windows.
29127 A value of t means resize them to fit the text displayed in them.
29128 A value of `grow-only', the default, means let mini-windows grow only;
29129 they return to their normal size when the minibuffer is closed, or the
29130 echo area becomes empty. */);
29131 Vresize_mini_windows = Qgrow_only;
29132
29133 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29134 doc: /* Alist specifying how to blink the cursor off.
29135 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29136 `cursor-type' frame-parameter or variable equals ON-STATE,
29137 comparing using `equal', Emacs uses OFF-STATE to specify
29138 how to blink it off. ON-STATE and OFF-STATE are values for
29139 the `cursor-type' frame parameter.
29140
29141 If a frame's ON-STATE has no entry in this list,
29142 the frame's other specifications determine how to blink the cursor off. */);
29143 Vblink_cursor_alist = Qnil;
29144
29145 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29146 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29147 If non-nil, windows are automatically scrolled horizontally to make
29148 point visible. */);
29149 automatic_hscrolling_p = 1;
29150 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29151
29152 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29153 doc: /* How many columns away from the window edge point is allowed to get
29154 before automatic hscrolling will horizontally scroll the window. */);
29155 hscroll_margin = 5;
29156
29157 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29158 doc: /* How many columns to scroll the window when point gets too close to the edge.
29159 When point is less than `hscroll-margin' columns from the window
29160 edge, automatic hscrolling will scroll the window by the amount of columns
29161 determined by this variable. If its value is a positive integer, scroll that
29162 many columns. If it's a positive floating-point number, it specifies the
29163 fraction of the window's width to scroll. If it's nil or zero, point will be
29164 centered horizontally after the scroll. Any other value, including negative
29165 numbers, are treated as if the value were zero.
29166
29167 Automatic hscrolling always moves point outside the scroll margin, so if
29168 point was more than scroll step columns inside the margin, the window will
29169 scroll more than the value given by the scroll step.
29170
29171 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29172 and `scroll-right' overrides this variable's effect. */);
29173 Vhscroll_step = make_number (0);
29174
29175 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29176 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29177 Bind this around calls to `message' to let it take effect. */);
29178 message_truncate_lines = 0;
29179
29180 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29181 doc: /* Normal hook run to update the menu bar definitions.
29182 Redisplay runs this hook before it redisplays the menu bar.
29183 This is used to update submenus such as Buffers,
29184 whose contents depend on various data. */);
29185 Vmenu_bar_update_hook = Qnil;
29186
29187 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29188 doc: /* Frame for which we are updating a menu.
29189 The enable predicate for a menu binding should check this variable. */);
29190 Vmenu_updating_frame = Qnil;
29191
29192 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29193 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29194 inhibit_menubar_update = 0;
29195
29196 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29197 doc: /* Prefix prepended to all continuation lines at display time.
29198 The value may be a string, an image, or a stretch-glyph; it is
29199 interpreted in the same way as the value of a `display' text property.
29200
29201 This variable is overridden by any `wrap-prefix' text or overlay
29202 property.
29203
29204 To add a prefix to non-continuation lines, use `line-prefix'. */);
29205 Vwrap_prefix = Qnil;
29206 DEFSYM (Qwrap_prefix, "wrap-prefix");
29207 Fmake_variable_buffer_local (Qwrap_prefix);
29208
29209 DEFVAR_LISP ("line-prefix", Vline_prefix,
29210 doc: /* Prefix prepended to all non-continuation lines at display time.
29211 The value may be a string, an image, or a stretch-glyph; it is
29212 interpreted in the same way as the value of a `display' text property.
29213
29214 This variable is overridden by any `line-prefix' text or overlay
29215 property.
29216
29217 To add a prefix to continuation lines, use `wrap-prefix'. */);
29218 Vline_prefix = Qnil;
29219 DEFSYM (Qline_prefix, "line-prefix");
29220 Fmake_variable_buffer_local (Qline_prefix);
29221
29222 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29223 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29224 inhibit_eval_during_redisplay = 0;
29225
29226 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29227 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29228 inhibit_free_realized_faces = 0;
29229
29230 #ifdef GLYPH_DEBUG
29231 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29232 doc: /* Inhibit try_window_id display optimization. */);
29233 inhibit_try_window_id = 0;
29234
29235 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29236 doc: /* Inhibit try_window_reusing display optimization. */);
29237 inhibit_try_window_reusing = 0;
29238
29239 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29240 doc: /* Inhibit try_cursor_movement display optimization. */);
29241 inhibit_try_cursor_movement = 0;
29242 #endif /* GLYPH_DEBUG */
29243
29244 DEFVAR_INT ("overline-margin", overline_margin,
29245 doc: /* Space between overline and text, in pixels.
29246 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29247 margin to the character height. */);
29248 overline_margin = 2;
29249
29250 DEFVAR_INT ("underline-minimum-offset",
29251 underline_minimum_offset,
29252 doc: /* Minimum distance between baseline and underline.
29253 This can improve legibility of underlined text at small font sizes,
29254 particularly when using variable `x-use-underline-position-properties'
29255 with fonts that specify an UNDERLINE_POSITION relatively close to the
29256 baseline. The default value is 1. */);
29257 underline_minimum_offset = 1;
29258
29259 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29260 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29261 This feature only works when on a window system that can change
29262 cursor shapes. */);
29263 display_hourglass_p = 1;
29264
29265 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29266 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29267 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29268
29269 hourglass_atimer = NULL;
29270 hourglass_shown_p = 0;
29271
29272 DEFSYM (Qglyphless_char, "glyphless-char");
29273 DEFSYM (Qhex_code, "hex-code");
29274 DEFSYM (Qempty_box, "empty-box");
29275 DEFSYM (Qthin_space, "thin-space");
29276 DEFSYM (Qzero_width, "zero-width");
29277
29278 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29279 /* Intern this now in case it isn't already done.
29280 Setting this variable twice is harmless.
29281 But don't staticpro it here--that is done in alloc.c. */
29282 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29283 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29284
29285 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29286 doc: /* Char-table defining glyphless characters.
29287 Each element, if non-nil, should be one of the following:
29288 an ASCII acronym string: display this string in a box
29289 `hex-code': display the hexadecimal code of a character in a box
29290 `empty-box': display as an empty box
29291 `thin-space': display as 1-pixel width space
29292 `zero-width': don't display
29293 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29294 display method for graphical terminals and text terminals respectively.
29295 GRAPHICAL and TEXT should each have one of the values listed above.
29296
29297 The char-table has one extra slot to control the display of a character for
29298 which no font is found. This slot only takes effect on graphical terminals.
29299 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29300 `thin-space'. The default is `empty-box'. */);
29301 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29302 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29303 Qempty_box);
29304
29305 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29306 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29307 Vdebug_on_message = Qnil;
29308 }
29309
29310
29311 /* Initialize this module when Emacs starts. */
29312
29313 void
29314 init_xdisp (void)
29315 {
29316 current_header_line_height = current_mode_line_height = -1;
29317
29318 CHARPOS (this_line_start_pos) = 0;
29319
29320 if (!noninteractive)
29321 {
29322 struct window *m = XWINDOW (minibuf_window);
29323 Lisp_Object frame = m->frame;
29324 struct frame *f = XFRAME (frame);
29325 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29326 struct window *r = XWINDOW (root);
29327 int i;
29328
29329 echo_area_window = minibuf_window;
29330
29331 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29332 wset_total_lines
29333 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29334 wset_total_cols (r, make_number (FRAME_COLS (f)));
29335 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29336 wset_total_lines (m, make_number (1));
29337 wset_total_cols (m, make_number (FRAME_COLS (f)));
29338
29339 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29340 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29341 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29342
29343 /* The default ellipsis glyphs `...'. */
29344 for (i = 0; i < 3; ++i)
29345 default_invis_vector[i] = make_number ('.');
29346 }
29347
29348 {
29349 /* Allocate the buffer for frame titles.
29350 Also used for `format-mode-line'. */
29351 int size = 100;
29352 mode_line_noprop_buf = xmalloc (size);
29353 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29354 mode_line_noprop_ptr = mode_line_noprop_buf;
29355 mode_line_target = MODE_LINE_DISPLAY;
29356 }
29357
29358 help_echo_showing_p = 0;
29359 }
29360
29361 /* Since w32 does not support atimers, it defines its own implementation of
29362 the following three functions in w32fns.c. */
29363 #ifndef WINDOWSNT
29364
29365 /* Platform-independent portion of hourglass implementation. */
29366
29367 /* Cancel a currently active hourglass timer, and start a new one. */
29368 void
29369 start_hourglass (void)
29370 {
29371 #if defined (HAVE_WINDOW_SYSTEM)
29372 EMACS_TIME delay;
29373
29374 cancel_hourglass ();
29375
29376 if (INTEGERP (Vhourglass_delay)
29377 && XINT (Vhourglass_delay) > 0)
29378 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29379 TYPE_MAXIMUM (time_t)),
29380 0);
29381 else if (FLOATP (Vhourglass_delay)
29382 && XFLOAT_DATA (Vhourglass_delay) > 0)
29383 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29384 else
29385 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29386
29387 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29388 show_hourglass, NULL);
29389 #endif
29390 }
29391
29392
29393 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29394 shown. */
29395 void
29396 cancel_hourglass (void)
29397 {
29398 #if defined (HAVE_WINDOW_SYSTEM)
29399 if (hourglass_atimer)
29400 {
29401 cancel_atimer (hourglass_atimer);
29402 hourglass_atimer = NULL;
29403 }
29404
29405 if (hourglass_shown_p)
29406 hide_hourglass ();
29407 #endif
29408 }
29409 #endif /* ! WINDOWSNT */