* xdisp.c (display_line): Avoid warning about implicit declaration
[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 #include <setjmp.h>
277
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT, or the underlying buffer
387 or string character, is a space or a TAB character. This is used
388 to determine where word wrapping can occur. */
389
390 #define IT_DISPLAYING_WHITESPACE(it) \
391 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
392 || ((STRINGP (it->string) \
393 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
394 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
395 || (it->s \
396 && (it->s[IT_BYTEPOS (*it)] == ' ' \
397 || it->s[IT_BYTEPOS (*it)] == '\t')) \
398 || (IT_BYTEPOS (*it) < ZV_BYTE \
399 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
400 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
401
402 /* Name of the face used to highlight trailing whitespace. */
403
404 static Lisp_Object Qtrailing_whitespace;
405
406 /* Name and number of the face used to highlight escape glyphs. */
407
408 static Lisp_Object Qescape_glyph;
409
410 /* Name and number of the face used to highlight non-breaking spaces. */
411
412 static Lisp_Object Qnobreak_space;
413
414 /* The symbol `image' which is the car of the lists used to represent
415 images in Lisp. Also a tool bar style. */
416
417 Lisp_Object Qimage;
418
419 /* The image map types. */
420 Lisp_Object QCmap;
421 static Lisp_Object QCpointer;
422 static Lisp_Object Qrect, Qcircle, Qpoly;
423
424 /* Tool bar styles */
425 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
426
427 /* Non-zero means print newline to stdout before next mini-buffer
428 message. */
429
430 int noninteractive_need_newline;
431
432 /* Non-zero means print newline to message log before next message. */
433
434 static int message_log_need_newline;
435
436 /* Three markers that message_dolog uses.
437 It could allocate them itself, but that causes trouble
438 in handling memory-full errors. */
439 static Lisp_Object message_dolog_marker1;
440 static Lisp_Object message_dolog_marker2;
441 static Lisp_Object message_dolog_marker3;
442 \f
443 /* The buffer position of the first character appearing entirely or
444 partially on the line of the selected window which contains the
445 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
446 redisplay optimization in redisplay_internal. */
447
448 static struct text_pos this_line_start_pos;
449
450 /* Number of characters past the end of the line above, including the
451 terminating newline. */
452
453 static struct text_pos this_line_end_pos;
454
455 /* The vertical positions and the height of this line. */
456
457 static int this_line_vpos;
458 static int this_line_y;
459 static int this_line_pixel_height;
460
461 /* X position at which this display line starts. Usually zero;
462 negative if first character is partially visible. */
463
464 static int this_line_start_x;
465
466 /* The smallest character position seen by move_it_* functions as they
467 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
468 hscrolled lines, see display_line. */
469
470 static struct text_pos this_line_min_pos;
471
472 /* Buffer that this_line_.* variables are referring to. */
473
474 static struct buffer *this_line_buffer;
475
476
477 /* Values of those variables at last redisplay are stored as
478 properties on `overlay-arrow-position' symbol. However, if
479 Voverlay_arrow_position is a marker, last-arrow-position is its
480 numerical position. */
481
482 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
483
484 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
485 properties on a symbol in overlay-arrow-variable-list. */
486
487 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
488
489 Lisp_Object Qmenu_bar_update_hook;
490
491 /* Nonzero if an overlay arrow has been displayed in this window. */
492
493 static int overlay_arrow_seen;
494
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
498
499 int buffer_shared;
500
501 /* Vector containing glyphs for an ellipsis `...'. */
502
503 static Lisp_Object default_invis_vector[3];
504
505 /* This is the window where the echo area message was displayed. It
506 is always a mini-buffer window, but it may not be the same window
507 currently active as a mini-buffer. */
508
509 Lisp_Object echo_area_window;
510
511 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
512 pushes the current message and the value of
513 message_enable_multibyte on the stack, the function restore_message
514 pops the stack and displays MESSAGE again. */
515
516 static Lisp_Object Vmessage_stack;
517
518 /* Nonzero means multibyte characters were enabled when the echo area
519 message was specified. */
520
521 static int message_enable_multibyte;
522
523 /* Nonzero if we should redraw the mode lines on the next redisplay. */
524
525 int update_mode_lines;
526
527 /* Nonzero if window sizes or contents have changed since last
528 redisplay that finished. */
529
530 int windows_or_buffers_changed;
531
532 /* Nonzero means a frame's cursor type has been changed. */
533
534 int cursor_type_changed;
535
536 /* Nonzero after display_mode_line if %l was used and it displayed a
537 line number. */
538
539 static int line_number_displayed;
540
541 /* The name of the *Messages* buffer, a string. */
542
543 static Lisp_Object Vmessages_buffer_name;
544
545 /* Current, index 0, and last displayed echo area message. Either
546 buffers from echo_buffers, or nil to indicate no message. */
547
548 Lisp_Object echo_area_buffer[2];
549
550 /* The buffers referenced from echo_area_buffer. */
551
552 static Lisp_Object echo_buffer[2];
553
554 /* A vector saved used in with_area_buffer to reduce consing. */
555
556 static Lisp_Object Vwith_echo_area_save_vector;
557
558 /* Non-zero means display_echo_area should display the last echo area
559 message again. Set by redisplay_preserve_echo_area. */
560
561 static int display_last_displayed_message_p;
562
563 /* Nonzero if echo area is being used by print; zero if being used by
564 message. */
565
566 static int message_buf_print;
567
568 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
569
570 static Lisp_Object Qinhibit_menubar_update;
571 static Lisp_Object Qmessage_truncate_lines;
572
573 /* Set to 1 in clear_message to make redisplay_internal aware
574 of an emptied echo area. */
575
576 static int message_cleared_p;
577
578 /* A scratch glyph row with contents used for generating truncation
579 glyphs. Also used in direct_output_for_insert. */
580
581 #define MAX_SCRATCH_GLYPHS 100
582 static struct glyph_row scratch_glyph_row;
583 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
584
585 /* Ascent and height of the last line processed by move_it_to. */
586
587 static int last_max_ascent, last_height;
588
589 /* Non-zero if there's a help-echo in the echo area. */
590
591 int help_echo_showing_p;
592
593 /* If >= 0, computed, exact values of mode-line and header-line height
594 to use in the macros CURRENT_MODE_LINE_HEIGHT and
595 CURRENT_HEADER_LINE_HEIGHT. */
596
597 int current_mode_line_height, current_header_line_height;
598
599 /* The maximum distance to look ahead for text properties. Values
600 that are too small let us call compute_char_face and similar
601 functions too often which is expensive. Values that are too large
602 let us call compute_char_face and alike too often because we
603 might not be interested in text properties that far away. */
604
605 #define TEXT_PROP_DISTANCE_LIMIT 100
606
607 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
608 iterator state and later restore it. This is needed because the
609 bidi iterator on bidi.c keeps a stacked cache of its states, which
610 is really a singleton. When we use scratch iterator objects to
611 move around the buffer, we can cause the bidi cache to be pushed or
612 popped, and therefore we need to restore the cache state when we
613 return to the original iterator. */
614 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
615 do { \
616 if (CACHE) \
617 bidi_unshelve_cache (CACHE, 1); \
618 ITCOPY = ITORIG; \
619 CACHE = bidi_shelve_cache (); \
620 } while (0)
621
622 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
623 do { \
624 if (pITORIG != pITCOPY) \
625 *(pITORIG) = *(pITCOPY); \
626 bidi_unshelve_cache (CACHE, 0); \
627 CACHE = NULL; \
628 } while (0)
629
630 #ifdef GLYPH_DEBUG
631
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG defined. */
634
635 int trace_redisplay_p;
636
637 #endif /* GLYPH_DEBUG */
638
639 #ifdef DEBUG_TRACE_MOVE
640 /* Non-zero means trace with TRACE_MOVE to stderr. */
641 int trace_move;
642
643 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
644 #else
645 #define TRACE_MOVE(x) (void) 0
646 #endif
647
648 static Lisp_Object Qauto_hscroll_mode;
649
650 /* Buffer being redisplayed -- for redisplay_window_error. */
651
652 static struct buffer *displayed_buffer;
653
654 /* Value returned from text property handlers (see below). */
655
656 enum prop_handled
657 {
658 HANDLED_NORMALLY,
659 HANDLED_RECOMPUTE_PROPS,
660 HANDLED_OVERLAY_STRING_CONSUMED,
661 HANDLED_RETURN
662 };
663
664 /* A description of text properties that redisplay is interested
665 in. */
666
667 struct props
668 {
669 /* The name of the property. */
670 Lisp_Object *name;
671
672 /* A unique index for the property. */
673 enum prop_idx idx;
674
675 /* A handler function called to set up iterator IT from the property
676 at IT's current position. Value is used to steer handle_stop. */
677 enum prop_handled (*handler) (struct it *it);
678 };
679
680 static enum prop_handled handle_face_prop (struct it *);
681 static enum prop_handled handle_invisible_prop (struct it *);
682 static enum prop_handled handle_display_prop (struct it *);
683 static enum prop_handled handle_composition_prop (struct it *);
684 static enum prop_handled handle_overlay_change (struct it *);
685 static enum prop_handled handle_fontified_prop (struct it *);
686
687 /* Properties handled by iterators. */
688
689 static struct props it_props[] =
690 {
691 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
692 /* Handle `face' before `display' because some sub-properties of
693 `display' need to know the face. */
694 {&Qface, FACE_PROP_IDX, handle_face_prop},
695 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
696 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
697 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
698 {NULL, 0, NULL}
699 };
700
701 /* Value is the position described by X. If X is a marker, value is
702 the marker_position of X. Otherwise, value is X. */
703
704 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
705
706 /* Enumeration returned by some move_it_.* functions internally. */
707
708 enum move_it_result
709 {
710 /* Not used. Undefined value. */
711 MOVE_UNDEFINED,
712
713 /* Move ended at the requested buffer position or ZV. */
714 MOVE_POS_MATCH_OR_ZV,
715
716 /* Move ended at the requested X pixel position. */
717 MOVE_X_REACHED,
718
719 /* Move within a line ended at the end of a line that must be
720 continued. */
721 MOVE_LINE_CONTINUED,
722
723 /* Move within a line ended at the end of a line that would
724 be displayed truncated. */
725 MOVE_LINE_TRUNCATED,
726
727 /* Move within a line ended at a line end. */
728 MOVE_NEWLINE_OR_CR
729 };
730
731 /* This counter is used to clear the face cache every once in a while
732 in redisplay_internal. It is incremented for each redisplay.
733 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
734 cleared. */
735
736 #define CLEAR_FACE_CACHE_COUNT 500
737 static int clear_face_cache_count;
738
739 /* Similarly for the image cache. */
740
741 #ifdef HAVE_WINDOW_SYSTEM
742 #define CLEAR_IMAGE_CACHE_COUNT 101
743 static int clear_image_cache_count;
744
745 /* Null glyph slice */
746 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
747 #endif
748
749 /* Non-zero while redisplay_internal is in progress. */
750
751 int redisplaying_p;
752
753 static Lisp_Object Qinhibit_free_realized_faces;
754 static Lisp_Object Qmode_line_default_help_echo;
755
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
758
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 ptrdiff_t help_echo_pos;
763
764 /* Temporary variable for XTread_socket. */
765
766 Lisp_Object previous_help_echo_string;
767
768 /* Platform-independent portion of hourglass implementation. */
769
770 /* Non-zero means an hourglass cursor is currently shown. */
771 int hourglass_shown_p;
772
773 /* If non-null, an asynchronous timer that, when it expires, displays
774 an hourglass cursor on all frames. */
775 struct atimer *hourglass_atimer;
776
777 /* Name of the face used to display glyphless characters. */
778 Lisp_Object Qglyphless_char;
779
780 /* Symbol for the purpose of Vglyphless_char_display. */
781 static Lisp_Object Qglyphless_char_display;
782
783 /* Method symbols for Vglyphless_char_display. */
784 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
785
786 /* Default pixel width of `thin-space' display method. */
787 #define THIN_SPACE_WIDTH 1
788
789 /* Default number of seconds to wait before displaying an hourglass
790 cursor. */
791 #define DEFAULT_HOURGLASS_DELAY 1
792
793 \f
794 /* Function prototypes. */
795
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, int);
798 static void mark_window_display_accurate_1 (struct window *, int);
799 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
800 static int display_prop_string_p (Lisp_Object, Lisp_Object);
801 static int cursor_row_p (struct glyph_row *);
802 static int redisplay_mode_lines (Lisp_Object, int);
803 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
804
805 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
806
807 static void handle_line_prefix (struct it *);
808
809 static void pint2str (char *, int, ptrdiff_t);
810 static void pint2hrstr (char *, int, ptrdiff_t);
811 static struct text_pos run_window_scroll_functions (Lisp_Object,
812 struct text_pos);
813 static void reconsider_clip_changes (struct window *, struct buffer *);
814 static int text_outside_line_unchanged_p (struct window *,
815 ptrdiff_t, ptrdiff_t);
816 static void store_mode_line_noprop_char (char);
817 static int store_mode_line_noprop (const char *, int, int);
818 static void handle_stop (struct it *);
819 static void handle_stop_backwards (struct it *, ptrdiff_t);
820 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
821 static void ensure_echo_area_buffers (void);
822 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
823 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
824 static int with_echo_area_buffer (struct window *, int,
825 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
826 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
827 static void clear_garbaged_frames (void);
828 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
829 static void pop_message (void);
830 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
831 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
832 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
833 static int display_echo_area (struct window *);
834 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
835 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
836 static Lisp_Object unwind_redisplay (Lisp_Object);
837 static int string_char_and_length (const unsigned char *, int *);
838 static struct text_pos display_prop_end (struct it *, Lisp_Object,
839 struct text_pos);
840 static int compute_window_start_on_continuation_line (struct window *);
841 static Lisp_Object safe_eval_handler (Lisp_Object);
842 static void insert_left_trunc_glyphs (struct it *);
843 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
844 Lisp_Object);
845 static void extend_face_to_end_of_line (struct it *);
846 static int append_space_for_newline (struct it *, int);
847 static int cursor_row_fully_visible_p (struct window *, int, int);
848 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
849 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
850 static int trailing_whitespace_p (ptrdiff_t);
851 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
852 static void push_it (struct it *, struct text_pos *);
853 static void iterate_out_of_display_property (struct it *);
854 static void pop_it (struct it *);
855 static void sync_frame_with_window_matrix_rows (struct window *);
856 static void select_frame_for_redisplay (Lisp_Object);
857 static void redisplay_internal (void);
858 static int echo_area_display (int);
859 static void redisplay_windows (Lisp_Object);
860 static void redisplay_window (Lisp_Object, int);
861 static Lisp_Object redisplay_window_error (Lisp_Object);
862 static Lisp_Object redisplay_window_0 (Lisp_Object);
863 static Lisp_Object redisplay_window_1 (Lisp_Object);
864 static int set_cursor_from_row (struct window *, struct glyph_row *,
865 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
866 int, int);
867 static int update_menu_bar (struct frame *, int, int);
868 static int try_window_reusing_current_matrix (struct window *);
869 static int try_window_id (struct window *);
870 static int display_line (struct it *);
871 static int display_mode_lines (struct window *);
872 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
873 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
874 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
875 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
876 static void display_menu_bar (struct window *);
877 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
878 ptrdiff_t *);
879 static int display_string (const char *, Lisp_Object, Lisp_Object,
880 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
881 static void compute_line_metrics (struct it *);
882 static void run_redisplay_end_trigger_hook (struct it *);
883 static int get_overlay_strings (struct it *, ptrdiff_t);
884 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
885 static void next_overlay_string (struct it *);
886 static void reseat (struct it *, struct text_pos, int);
887 static void reseat_1 (struct it *, struct text_pos, int);
888 static void back_to_previous_visible_line_start (struct it *);
889 void reseat_at_previous_visible_line_start (struct it *);
890 static void reseat_at_next_visible_line_start (struct it *, int);
891 static int next_element_from_ellipsis (struct it *);
892 static int next_element_from_display_vector (struct it *);
893 static int next_element_from_string (struct it *);
894 static int next_element_from_c_string (struct it *);
895 static int next_element_from_buffer (struct it *);
896 static int next_element_from_composition (struct it *);
897 static int next_element_from_image (struct it *);
898 static int next_element_from_stretch (struct it *);
899 static void load_overlay_strings (struct it *, ptrdiff_t);
900 static int init_from_display_pos (struct it *, struct window *,
901 struct display_pos *);
902 static void reseat_to_string (struct it *, const char *,
903 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
904 static int get_next_display_element (struct it *);
905 static enum move_it_result
906 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
907 enum move_operation_enum);
908 void move_it_vertically_backward (struct it *, int);
909 static void init_to_row_start (struct it *, struct window *,
910 struct glyph_row *);
911 static int init_to_row_end (struct it *, struct window *,
912 struct glyph_row *);
913 static void back_to_previous_line_start (struct it *);
914 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
915 static struct text_pos string_pos_nchars_ahead (struct text_pos,
916 Lisp_Object, ptrdiff_t);
917 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
918 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
919 static ptrdiff_t number_of_chars (const char *, int);
920 static void compute_stop_pos (struct it *);
921 static void compute_string_pos (struct text_pos *, struct text_pos,
922 Lisp_Object);
923 static int face_before_or_after_it_pos (struct it *, int);
924 static ptrdiff_t next_overlay_change (ptrdiff_t);
925 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
926 Lisp_Object, struct text_pos *, ptrdiff_t, int);
927 static int handle_single_display_spec (struct it *, Lisp_Object,
928 Lisp_Object, Lisp_Object,
929 struct text_pos *, ptrdiff_t, int, int);
930 static int underlying_face_id (struct it *);
931 static int in_ellipses_for_invisible_text_p (struct display_pos *,
932 struct window *);
933
934 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
935 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
936
937 #ifdef HAVE_WINDOW_SYSTEM
938
939 static void x_consider_frame_title (Lisp_Object);
940 static int tool_bar_lines_needed (struct frame *, int *);
941 static void update_tool_bar (struct frame *, int);
942 static void build_desired_tool_bar_string (struct frame *f);
943 static int redisplay_tool_bar (struct frame *);
944 static void display_tool_bar_line (struct it *, int);
945 static void notice_overwritten_cursor (struct window *,
946 enum glyph_row_area,
947 int, int, int, int);
948 static void append_stretch_glyph (struct it *, Lisp_Object,
949 int, int, int);
950
951
952 #endif /* HAVE_WINDOW_SYSTEM */
953
954 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
955 static int coords_in_mouse_face_p (struct window *, int, int);
956
957
958 \f
959 /***********************************************************************
960 Window display dimensions
961 ***********************************************************************/
962
963 /* Return the bottom boundary y-position for text lines in window W.
964 This is the first y position at which a line cannot start.
965 It is relative to the top of the window.
966
967 This is the height of W minus the height of a mode line, if any. */
968
969 int
970 window_text_bottom_y (struct window *w)
971 {
972 int height = WINDOW_TOTAL_HEIGHT (w);
973
974 if (WINDOW_WANTS_MODELINE_P (w))
975 height -= CURRENT_MODE_LINE_HEIGHT (w);
976 return height;
977 }
978
979 /* Return the pixel width of display area AREA of window W. AREA < 0
980 means return the total width of W, not including fringes to
981 the left and right of the window. */
982
983 int
984 window_box_width (struct window *w, int area)
985 {
986 int cols = XFASTINT (w->total_cols);
987 int pixels = 0;
988
989 if (!w->pseudo_window_p)
990 {
991 cols -= WINDOW_SCROLL_BAR_COLS (w);
992
993 if (area == TEXT_AREA)
994 {
995 if (INTEGERP (w->left_margin_cols))
996 cols -= XFASTINT (w->left_margin_cols);
997 if (INTEGERP (w->right_margin_cols))
998 cols -= XFASTINT (w->right_margin_cols);
999 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1000 }
1001 else if (area == LEFT_MARGIN_AREA)
1002 {
1003 cols = (INTEGERP (w->left_margin_cols)
1004 ? XFASTINT (w->left_margin_cols) : 0);
1005 pixels = 0;
1006 }
1007 else if (area == RIGHT_MARGIN_AREA)
1008 {
1009 cols = (INTEGERP (w->right_margin_cols)
1010 ? XFASTINT (w->right_margin_cols) : 0);
1011 pixels = 0;
1012 }
1013 }
1014
1015 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1016 }
1017
1018
1019 /* Return the pixel height of the display area of window W, not
1020 including mode lines of W, if any. */
1021
1022 int
1023 window_box_height (struct window *w)
1024 {
1025 struct frame *f = XFRAME (w->frame);
1026 int height = WINDOW_TOTAL_HEIGHT (w);
1027
1028 eassert (height >= 0);
1029
1030 /* Note: the code below that determines the mode-line/header-line
1031 height is essentially the same as that contained in the macro
1032 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1033 the appropriate glyph row has its `mode_line_p' flag set,
1034 and if it doesn't, uses estimate_mode_line_height instead. */
1035
1036 if (WINDOW_WANTS_MODELINE_P (w))
1037 {
1038 struct glyph_row *ml_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (ml_row && ml_row->mode_line_p)
1043 height -= ml_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1046 }
1047
1048 if (WINDOW_WANTS_HEADER_LINE_P (w))
1049 {
1050 struct glyph_row *hl_row
1051 = (w->current_matrix && w->current_matrix->rows
1052 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1053 : 0);
1054 if (hl_row && hl_row->mode_line_p)
1055 height -= hl_row->height;
1056 else
1057 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1058 }
1059
1060 /* With a very small font and a mode-line that's taller than
1061 default, we might end up with a negative height. */
1062 return max (0, height);
1063 }
1064
1065 /* Return the window-relative coordinate of the left edge of display
1066 area AREA of window W. AREA < 0 means return the left edge of the
1067 whole window, to the right of the left fringe of W. */
1068
1069 int
1070 window_box_left_offset (struct window *w, int area)
1071 {
1072 int x;
1073
1074 if (w->pseudo_window_p)
1075 return 0;
1076
1077 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1078
1079 if (area == TEXT_AREA)
1080 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1081 + window_box_width (w, LEFT_MARGIN_AREA));
1082 else if (area == RIGHT_MARGIN_AREA)
1083 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1084 + window_box_width (w, LEFT_MARGIN_AREA)
1085 + window_box_width (w, TEXT_AREA)
1086 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1087 ? 0
1088 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1089 else if (area == LEFT_MARGIN_AREA
1090 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1091 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1092
1093 return x;
1094 }
1095
1096
1097 /* Return the window-relative coordinate of the right edge of display
1098 area AREA of window W. AREA < 0 means return the right edge of the
1099 whole window, to the left of the right fringe of W. */
1100
1101 int
1102 window_box_right_offset (struct window *w, int area)
1103 {
1104 return window_box_left_offset (w, area) + window_box_width (w, area);
1105 }
1106
1107 /* Return the frame-relative coordinate of the left edge of display
1108 area AREA of window W. AREA < 0 means return the left edge of the
1109 whole window, to the right of the left fringe of W. */
1110
1111 int
1112 window_box_left (struct window *w, int area)
1113 {
1114 struct frame *f = XFRAME (w->frame);
1115 int x;
1116
1117 if (w->pseudo_window_p)
1118 return FRAME_INTERNAL_BORDER_WIDTH (f);
1119
1120 x = (WINDOW_LEFT_EDGE_X (w)
1121 + window_box_left_offset (w, area));
1122
1123 return x;
1124 }
1125
1126
1127 /* Return the frame-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the right edge of the
1129 whole window, to the left of the right fringe of W. */
1130
1131 int
1132 window_box_right (struct window *w, int area)
1133 {
1134 return window_box_left (w, area) + window_box_width (w, area);
1135 }
1136
1137 /* Get the bounding box of the display area AREA of window W, without
1138 mode lines, in frame-relative coordinates. AREA < 0 means the
1139 whole window, not including the left and right fringes of
1140 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1141 coordinates of the upper-left corner of the box. Return in
1142 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1143
1144 void
1145 window_box (struct window *w, int area, int *box_x, int *box_y,
1146 int *box_width, int *box_height)
1147 {
1148 if (box_width)
1149 *box_width = window_box_width (w, area);
1150 if (box_height)
1151 *box_height = window_box_height (w);
1152 if (box_x)
1153 *box_x = window_box_left (w, area);
1154 if (box_y)
1155 {
1156 *box_y = WINDOW_TOP_EDGE_Y (w);
1157 if (WINDOW_WANTS_HEADER_LINE_P (w))
1158 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1159 }
1160 }
1161
1162
1163 /* Get the bounding box of the display area AREA of window W, without
1164 mode lines. AREA < 0 means the whole window, not including the
1165 left and right fringe of the window. Return in *TOP_LEFT_X
1166 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1167 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1168 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1169 box. */
1170
1171 static inline void
1172 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1173 int *bottom_right_x, int *bottom_right_y)
1174 {
1175 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1176 bottom_right_y);
1177 *bottom_right_x += *top_left_x;
1178 *bottom_right_y += *top_left_y;
1179 }
1180
1181
1182 \f
1183 /***********************************************************************
1184 Utilities
1185 ***********************************************************************/
1186
1187 /* Return the bottom y-position of the line the iterator IT is in.
1188 This can modify IT's settings. */
1189
1190 int
1191 line_bottom_y (struct it *it)
1192 {
1193 int line_height = it->max_ascent + it->max_descent;
1194 int line_top_y = it->current_y;
1195
1196 if (line_height == 0)
1197 {
1198 if (last_height)
1199 line_height = last_height;
1200 else if (IT_CHARPOS (*it) < ZV)
1201 {
1202 move_it_by_lines (it, 1);
1203 line_height = (it->max_ascent || it->max_descent
1204 ? it->max_ascent + it->max_descent
1205 : last_height);
1206 }
1207 else
1208 {
1209 struct glyph_row *row = it->glyph_row;
1210
1211 /* Use the default character height. */
1212 it->glyph_row = NULL;
1213 it->what = IT_CHARACTER;
1214 it->c = ' ';
1215 it->len = 1;
1216 PRODUCE_GLYPHS (it);
1217 line_height = it->ascent + it->descent;
1218 it->glyph_row = row;
1219 }
1220 }
1221
1222 return line_top_y + line_height;
1223 }
1224
1225 /* Subroutine of pos_visible_p below. Extracts a display string, if
1226 any, from the display spec given as its argument. */
1227 static Lisp_Object
1228 string_from_display_spec (Lisp_Object spec)
1229 {
1230 if (CONSP (spec))
1231 {
1232 while (CONSP (spec))
1233 {
1234 if (STRINGP (XCAR (spec)))
1235 return XCAR (spec);
1236 spec = XCDR (spec);
1237 }
1238 }
1239 else if (VECTORP (spec))
1240 {
1241 ptrdiff_t i;
1242
1243 for (i = 0; i < ASIZE (spec); i++)
1244 {
1245 if (STRINGP (AREF (spec, i)))
1246 return AREF (spec, i);
1247 }
1248 return Qnil;
1249 }
1250
1251 return spec;
1252 }
1253
1254
1255 /* Limit insanely large values of W->hscroll on frame F to the largest
1256 value that will still prevent first_visible_x and last_visible_x of
1257 'struct it' from overflowing an int. */
1258 static inline int
1259 window_hscroll_limited (struct window *w, struct frame *f)
1260 {
1261 ptrdiff_t window_hscroll = w->hscroll;
1262 int window_text_width = window_box_width (w, TEXT_AREA);
1263 int colwidth = FRAME_COLUMN_WIDTH (f);
1264
1265 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1266 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1267
1268 return window_hscroll;
1269 }
1270
1271 /* Return 1 if position CHARPOS is visible in window W.
1272 CHARPOS < 0 means return info about WINDOW_END position.
1273 If visible, set *X and *Y to pixel coordinates of top left corner.
1274 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1275 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1276
1277 int
1278 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1279 int *rtop, int *rbot, int *rowh, int *vpos)
1280 {
1281 struct it it;
1282 void *itdata = bidi_shelve_cache ();
1283 struct text_pos top;
1284 int visible_p = 0;
1285 struct buffer *old_buffer = NULL;
1286
1287 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1288 return visible_p;
1289
1290 if (XBUFFER (w->buffer) != current_buffer)
1291 {
1292 old_buffer = current_buffer;
1293 set_buffer_internal_1 (XBUFFER (w->buffer));
1294 }
1295
1296 SET_TEXT_POS_FROM_MARKER (top, w->start);
1297 /* Scrolling a minibuffer window via scroll bar when the echo area
1298 shows long text sometimes resets the minibuffer contents behind
1299 our backs. */
1300 if (CHARPOS (top) > ZV)
1301 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1302
1303 /* Compute exact mode line heights. */
1304 if (WINDOW_WANTS_MODELINE_P (w))
1305 current_mode_line_height
1306 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1307 BVAR (current_buffer, mode_line_format));
1308
1309 if (WINDOW_WANTS_HEADER_LINE_P (w))
1310 current_header_line_height
1311 = display_mode_line (w, HEADER_LINE_FACE_ID,
1312 BVAR (current_buffer, header_line_format));
1313
1314 start_display (&it, w, top);
1315 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1316 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1317
1318 if (charpos >= 0
1319 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1320 && IT_CHARPOS (it) >= charpos)
1321 /* When scanning backwards under bidi iteration, move_it_to
1322 stops at or _before_ CHARPOS, because it stops at or to
1323 the _right_ of the character at CHARPOS. */
1324 || (it.bidi_p && it.bidi_it.scan_dir == -1
1325 && IT_CHARPOS (it) <= charpos)))
1326 {
1327 /* We have reached CHARPOS, or passed it. How the call to
1328 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1329 or covered by a display property, move_it_to stops at the end
1330 of the invisible text, to the right of CHARPOS. (ii) If
1331 CHARPOS is in a display vector, move_it_to stops on its last
1332 glyph. */
1333 int top_x = it.current_x;
1334 int top_y = it.current_y;
1335 /* Calling line_bottom_y may change it.method, it.position, etc. */
1336 enum it_method it_method = it.method;
1337 int bottom_y = (last_height = 0, line_bottom_y (&it));
1338 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1339
1340 if (top_y < window_top_y)
1341 visible_p = bottom_y > window_top_y;
1342 else if (top_y < it.last_visible_y)
1343 visible_p = 1;
1344 if (bottom_y >= it.last_visible_y
1345 && it.bidi_p && it.bidi_it.scan_dir == -1
1346 && IT_CHARPOS (it) < charpos)
1347 {
1348 /* When the last line of the window is scanned backwards
1349 under bidi iteration, we could be duped into thinking
1350 that we have passed CHARPOS, when in fact move_it_to
1351 simply stopped short of CHARPOS because it reached
1352 last_visible_y. To see if that's what happened, we call
1353 move_it_to again with a slightly larger vertical limit,
1354 and see if it actually moved vertically; if it did, we
1355 didn't really reach CHARPOS, which is beyond window end. */
1356 struct it save_it = it;
1357 /* Why 10? because we don't know how many canonical lines
1358 will the height of the next line(s) be. So we guess. */
1359 int ten_more_lines =
1360 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1361
1362 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1363 MOVE_TO_POS | MOVE_TO_Y);
1364 if (it.current_y > top_y)
1365 visible_p = 0;
1366
1367 it = save_it;
1368 }
1369 if (visible_p)
1370 {
1371 if (it_method == GET_FROM_DISPLAY_VECTOR)
1372 {
1373 /* We stopped on the last glyph of a display vector.
1374 Try and recompute. Hack alert! */
1375 if (charpos < 2 || top.charpos >= charpos)
1376 top_x = it.glyph_row->x;
1377 else
1378 {
1379 struct it it2;
1380 start_display (&it2, w, top);
1381 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1382 get_next_display_element (&it2);
1383 PRODUCE_GLYPHS (&it2);
1384 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1385 || it2.current_x > it2.last_visible_x)
1386 top_x = it.glyph_row->x;
1387 else
1388 {
1389 top_x = it2.current_x;
1390 top_y = it2.current_y;
1391 }
1392 }
1393 }
1394 else if (IT_CHARPOS (it) != charpos)
1395 {
1396 Lisp_Object cpos = make_number (charpos);
1397 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1398 Lisp_Object string = string_from_display_spec (spec);
1399 int newline_in_string = 0;
1400
1401 if (STRINGP (string))
1402 {
1403 const char *s = SSDATA (string);
1404 const char *e = s + SBYTES (string);
1405 while (s < e)
1406 {
1407 if (*s++ == '\n')
1408 {
1409 newline_in_string = 1;
1410 break;
1411 }
1412 }
1413 }
1414 /* The tricky code below is needed because there's a
1415 discrepancy between move_it_to and how we set cursor
1416 when the display line ends in a newline from a
1417 display string. move_it_to will stop _after_ such
1418 display strings, whereas set_cursor_from_row
1419 conspires with cursor_row_p to place the cursor on
1420 the first glyph produced from the display string. */
1421
1422 /* We have overshoot PT because it is covered by a
1423 display property whose value is a string. If the
1424 string includes embedded newlines, we are also in the
1425 wrong display line. Backtrack to the correct line,
1426 where the display string begins. */
1427 if (newline_in_string)
1428 {
1429 Lisp_Object startpos, endpos;
1430 EMACS_INT start, end;
1431 struct it it3;
1432 int it3_moved;
1433
1434 /* Find the first and the last buffer positions
1435 covered by the display string. */
1436 endpos =
1437 Fnext_single_char_property_change (cpos, Qdisplay,
1438 Qnil, Qnil);
1439 startpos =
1440 Fprevious_single_char_property_change (endpos, Qdisplay,
1441 Qnil, Qnil);
1442 start = XFASTINT (startpos);
1443 end = XFASTINT (endpos);
1444 /* Move to the last buffer position before the
1445 display property. */
1446 start_display (&it3, w, top);
1447 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1448 /* Move forward one more line if the position before
1449 the display string is a newline or if it is the
1450 rightmost character on a line that is
1451 continued or word-wrapped. */
1452 if (it3.method == GET_FROM_BUFFER
1453 && it3.c == '\n')
1454 move_it_by_lines (&it3, 1);
1455 else if (move_it_in_display_line_to (&it3, -1,
1456 it3.current_x
1457 + it3.pixel_width,
1458 MOVE_TO_X)
1459 == MOVE_LINE_CONTINUED)
1460 {
1461 move_it_by_lines (&it3, 1);
1462 /* When we are under word-wrap, the #$@%!
1463 move_it_by_lines moves 2 lines, so we need to
1464 fix that up. */
1465 if (it3.line_wrap == WORD_WRAP)
1466 move_it_by_lines (&it3, -1);
1467 }
1468
1469 /* Record the vertical coordinate of the display
1470 line where we wound up. */
1471 top_y = it3.current_y;
1472 if (it3.bidi_p)
1473 {
1474 /* When characters are reordered for display,
1475 the character displayed to the left of the
1476 display string could be _after_ the display
1477 property in the logical order. Use the
1478 smallest vertical position of these two. */
1479 start_display (&it3, w, top);
1480 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1481 if (it3.current_y < top_y)
1482 top_y = it3.current_y;
1483 }
1484 /* Move from the top of the window to the beginning
1485 of the display line where the display string
1486 begins. */
1487 start_display (&it3, w, top);
1488 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1489 /* If it3_moved stays zero after the 'while' loop
1490 below, that means we already were at a newline
1491 before the loop (e.g., the display string begins
1492 with a newline), so we don't need to (and cannot)
1493 inspect the glyphs of it3.glyph_row, because
1494 PRODUCE_GLYPHS will not produce anything for a
1495 newline, and thus it3.glyph_row stays at its
1496 stale content it got at top of the window. */
1497 it3_moved = 0;
1498 /* Finally, advance the iterator until we hit the
1499 first display element whose character position is
1500 CHARPOS, or until the first newline from the
1501 display string, which signals the end of the
1502 display line. */
1503 while (get_next_display_element (&it3))
1504 {
1505 PRODUCE_GLYPHS (&it3);
1506 if (IT_CHARPOS (it3) == charpos
1507 || ITERATOR_AT_END_OF_LINE_P (&it3))
1508 break;
1509 it3_moved = 1;
1510 set_iterator_to_next (&it3, 0);
1511 }
1512 top_x = it3.current_x - it3.pixel_width;
1513 /* Normally, we would exit the above loop because we
1514 found the display element whose character
1515 position is CHARPOS. For the contingency that we
1516 didn't, and stopped at the first newline from the
1517 display string, move back over the glyphs
1518 produced from the string, until we find the
1519 rightmost glyph not from the string. */
1520 if (it3_moved
1521 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1522 {
1523 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1524 + it3.glyph_row->used[TEXT_AREA];
1525
1526 while (EQ ((g - 1)->object, string))
1527 {
1528 --g;
1529 top_x -= g->pixel_width;
1530 }
1531 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1532 + it3.glyph_row->used[TEXT_AREA]);
1533 }
1534 }
1535 }
1536
1537 *x = top_x;
1538 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1539 *rtop = max (0, window_top_y - top_y);
1540 *rbot = max (0, bottom_y - it.last_visible_y);
1541 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1542 - max (top_y, window_top_y)));
1543 *vpos = it.vpos;
1544 }
1545 }
1546 else
1547 {
1548 /* We were asked to provide info about WINDOW_END. */
1549 struct it it2;
1550 void *it2data = NULL;
1551
1552 SAVE_IT (it2, it, it2data);
1553 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1554 move_it_by_lines (&it, 1);
1555 if (charpos < IT_CHARPOS (it)
1556 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1557 {
1558 visible_p = 1;
1559 RESTORE_IT (&it2, &it2, it2data);
1560 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1561 *x = it2.current_x;
1562 *y = it2.current_y + it2.max_ascent - it2.ascent;
1563 *rtop = max (0, -it2.current_y);
1564 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1565 - it.last_visible_y));
1566 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1567 it.last_visible_y)
1568 - max (it2.current_y,
1569 WINDOW_HEADER_LINE_HEIGHT (w))));
1570 *vpos = it2.vpos;
1571 }
1572 else
1573 bidi_unshelve_cache (it2data, 1);
1574 }
1575 bidi_unshelve_cache (itdata, 0);
1576
1577 if (old_buffer)
1578 set_buffer_internal_1 (old_buffer);
1579
1580 current_header_line_height = current_mode_line_height = -1;
1581
1582 if (visible_p && w->hscroll > 0)
1583 *x -=
1584 window_hscroll_limited (w, WINDOW_XFRAME (w))
1585 * WINDOW_FRAME_COLUMN_WIDTH (w);
1586
1587 #if 0
1588 /* Debugging code. */
1589 if (visible_p)
1590 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1591 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1592 else
1593 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1594 #endif
1595
1596 return visible_p;
1597 }
1598
1599
1600 /* Return the next character from STR. Return in *LEN the length of
1601 the character. This is like STRING_CHAR_AND_LENGTH but never
1602 returns an invalid character. If we find one, we return a `?', but
1603 with the length of the invalid character. */
1604
1605 static inline int
1606 string_char_and_length (const unsigned char *str, int *len)
1607 {
1608 int c;
1609
1610 c = STRING_CHAR_AND_LENGTH (str, *len);
1611 if (!CHAR_VALID_P (c))
1612 /* We may not change the length here because other places in Emacs
1613 don't use this function, i.e. they silently accept invalid
1614 characters. */
1615 c = '?';
1616
1617 return c;
1618 }
1619
1620
1621
1622 /* Given a position POS containing a valid character and byte position
1623 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1624
1625 static struct text_pos
1626 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1627 {
1628 eassert (STRINGP (string) && nchars >= 0);
1629
1630 if (STRING_MULTIBYTE (string))
1631 {
1632 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1633 int len;
1634
1635 while (nchars--)
1636 {
1637 string_char_and_length (p, &len);
1638 p += len;
1639 CHARPOS (pos) += 1;
1640 BYTEPOS (pos) += len;
1641 }
1642 }
1643 else
1644 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1645
1646 return pos;
1647 }
1648
1649
1650 /* Value is the text position, i.e. character and byte position,
1651 for character position CHARPOS in STRING. */
1652
1653 static inline struct text_pos
1654 string_pos (ptrdiff_t charpos, Lisp_Object string)
1655 {
1656 struct text_pos pos;
1657 eassert (STRINGP (string));
1658 eassert (charpos >= 0);
1659 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1660 return pos;
1661 }
1662
1663
1664 /* Value is a text position, i.e. character and byte position, for
1665 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1666 means recognize multibyte characters. */
1667
1668 static struct text_pos
1669 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1670 {
1671 struct text_pos pos;
1672
1673 eassert (s != NULL);
1674 eassert (charpos >= 0);
1675
1676 if (multibyte_p)
1677 {
1678 int len;
1679
1680 SET_TEXT_POS (pos, 0, 0);
1681 while (charpos--)
1682 {
1683 string_char_and_length ((const unsigned char *) s, &len);
1684 s += len;
1685 CHARPOS (pos) += 1;
1686 BYTEPOS (pos) += len;
1687 }
1688 }
1689 else
1690 SET_TEXT_POS (pos, charpos, charpos);
1691
1692 return pos;
1693 }
1694
1695
1696 /* Value is the number of characters in C string S. MULTIBYTE_P
1697 non-zero means recognize multibyte characters. */
1698
1699 static ptrdiff_t
1700 number_of_chars (const char *s, int multibyte_p)
1701 {
1702 ptrdiff_t nchars;
1703
1704 if (multibyte_p)
1705 {
1706 ptrdiff_t rest = strlen (s);
1707 int len;
1708 const unsigned char *p = (const unsigned char *) s;
1709
1710 for (nchars = 0; rest > 0; ++nchars)
1711 {
1712 string_char_and_length (p, &len);
1713 rest -= len, p += len;
1714 }
1715 }
1716 else
1717 nchars = strlen (s);
1718
1719 return nchars;
1720 }
1721
1722
1723 /* Compute byte position NEWPOS->bytepos corresponding to
1724 NEWPOS->charpos. POS is a known position in string STRING.
1725 NEWPOS->charpos must be >= POS.charpos. */
1726
1727 static void
1728 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1729 {
1730 eassert (STRINGP (string));
1731 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1732
1733 if (STRING_MULTIBYTE (string))
1734 *newpos = string_pos_nchars_ahead (pos, string,
1735 CHARPOS (*newpos) - CHARPOS (pos));
1736 else
1737 BYTEPOS (*newpos) = CHARPOS (*newpos);
1738 }
1739
1740 /* EXPORT:
1741 Return an estimation of the pixel height of mode or header lines on
1742 frame F. FACE_ID specifies what line's height to estimate. */
1743
1744 int
1745 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1746 {
1747 #ifdef HAVE_WINDOW_SYSTEM
1748 if (FRAME_WINDOW_P (f))
1749 {
1750 int height = FONT_HEIGHT (FRAME_FONT (f));
1751
1752 /* This function is called so early when Emacs starts that the face
1753 cache and mode line face are not yet initialized. */
1754 if (FRAME_FACE_CACHE (f))
1755 {
1756 struct face *face = FACE_FROM_ID (f, face_id);
1757 if (face)
1758 {
1759 if (face->font)
1760 height = FONT_HEIGHT (face->font);
1761 if (face->box_line_width > 0)
1762 height += 2 * face->box_line_width;
1763 }
1764 }
1765
1766 return height;
1767 }
1768 #endif
1769
1770 return 1;
1771 }
1772
1773 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1774 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1775 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1776 not force the value into range. */
1777
1778 void
1779 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1780 int *x, int *y, NativeRectangle *bounds, int noclip)
1781 {
1782
1783 #ifdef HAVE_WINDOW_SYSTEM
1784 if (FRAME_WINDOW_P (f))
1785 {
1786 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1787 even for negative values. */
1788 if (pix_x < 0)
1789 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1790 if (pix_y < 0)
1791 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1792
1793 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1794 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1795
1796 if (bounds)
1797 STORE_NATIVE_RECT (*bounds,
1798 FRAME_COL_TO_PIXEL_X (f, pix_x),
1799 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1800 FRAME_COLUMN_WIDTH (f) - 1,
1801 FRAME_LINE_HEIGHT (f) - 1);
1802
1803 if (!noclip)
1804 {
1805 if (pix_x < 0)
1806 pix_x = 0;
1807 else if (pix_x > FRAME_TOTAL_COLS (f))
1808 pix_x = FRAME_TOTAL_COLS (f);
1809
1810 if (pix_y < 0)
1811 pix_y = 0;
1812 else if (pix_y > FRAME_LINES (f))
1813 pix_y = FRAME_LINES (f);
1814 }
1815 }
1816 #endif
1817
1818 *x = pix_x;
1819 *y = pix_y;
1820 }
1821
1822
1823 /* Find the glyph under window-relative coordinates X/Y in window W.
1824 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1825 strings. Return in *HPOS and *VPOS the row and column number of
1826 the glyph found. Return in *AREA the glyph area containing X.
1827 Value is a pointer to the glyph found or null if X/Y is not on
1828 text, or we can't tell because W's current matrix is not up to
1829 date. */
1830
1831 static
1832 struct glyph *
1833 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1834 int *dx, int *dy, int *area)
1835 {
1836 struct glyph *glyph, *end;
1837 struct glyph_row *row = NULL;
1838 int x0, i;
1839
1840 /* Find row containing Y. Give up if some row is not enabled. */
1841 for (i = 0; i < w->current_matrix->nrows; ++i)
1842 {
1843 row = MATRIX_ROW (w->current_matrix, i);
1844 if (!row->enabled_p)
1845 return NULL;
1846 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1847 break;
1848 }
1849
1850 *vpos = i;
1851 *hpos = 0;
1852
1853 /* Give up if Y is not in the window. */
1854 if (i == w->current_matrix->nrows)
1855 return NULL;
1856
1857 /* Get the glyph area containing X. */
1858 if (w->pseudo_window_p)
1859 {
1860 *area = TEXT_AREA;
1861 x0 = 0;
1862 }
1863 else
1864 {
1865 if (x < window_box_left_offset (w, TEXT_AREA))
1866 {
1867 *area = LEFT_MARGIN_AREA;
1868 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1869 }
1870 else if (x < window_box_right_offset (w, TEXT_AREA))
1871 {
1872 *area = TEXT_AREA;
1873 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1874 }
1875 else
1876 {
1877 *area = RIGHT_MARGIN_AREA;
1878 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1879 }
1880 }
1881
1882 /* Find glyph containing X. */
1883 glyph = row->glyphs[*area];
1884 end = glyph + row->used[*area];
1885 x -= x0;
1886 while (glyph < end && x >= glyph->pixel_width)
1887 {
1888 x -= glyph->pixel_width;
1889 ++glyph;
1890 }
1891
1892 if (glyph == end)
1893 return NULL;
1894
1895 if (dx)
1896 {
1897 *dx = x;
1898 *dy = y - (row->y + row->ascent - glyph->ascent);
1899 }
1900
1901 *hpos = glyph - row->glyphs[*area];
1902 return glyph;
1903 }
1904
1905 /* Convert frame-relative x/y to coordinates relative to window W.
1906 Takes pseudo-windows into account. */
1907
1908 static void
1909 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1910 {
1911 if (w->pseudo_window_p)
1912 {
1913 /* A pseudo-window is always full-width, and starts at the
1914 left edge of the frame, plus a frame border. */
1915 struct frame *f = XFRAME (w->frame);
1916 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1917 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1918 }
1919 else
1920 {
1921 *x -= WINDOW_LEFT_EDGE_X (w);
1922 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1923 }
1924 }
1925
1926 #ifdef HAVE_WINDOW_SYSTEM
1927
1928 /* EXPORT:
1929 Return in RECTS[] at most N clipping rectangles for glyph string S.
1930 Return the number of stored rectangles. */
1931
1932 int
1933 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1934 {
1935 XRectangle r;
1936
1937 if (n <= 0)
1938 return 0;
1939
1940 if (s->row->full_width_p)
1941 {
1942 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1943 r.x = WINDOW_LEFT_EDGE_X (s->w);
1944 r.width = WINDOW_TOTAL_WIDTH (s->w);
1945
1946 /* Unless displaying a mode or menu bar line, which are always
1947 fully visible, clip to the visible part of the row. */
1948 if (s->w->pseudo_window_p)
1949 r.height = s->row->visible_height;
1950 else
1951 r.height = s->height;
1952 }
1953 else
1954 {
1955 /* This is a text line that may be partially visible. */
1956 r.x = window_box_left (s->w, s->area);
1957 r.width = window_box_width (s->w, s->area);
1958 r.height = s->row->visible_height;
1959 }
1960
1961 if (s->clip_head)
1962 if (r.x < s->clip_head->x)
1963 {
1964 if (r.width >= s->clip_head->x - r.x)
1965 r.width -= s->clip_head->x - r.x;
1966 else
1967 r.width = 0;
1968 r.x = s->clip_head->x;
1969 }
1970 if (s->clip_tail)
1971 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1972 {
1973 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1974 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1975 else
1976 r.width = 0;
1977 }
1978
1979 /* If S draws overlapping rows, it's sufficient to use the top and
1980 bottom of the window for clipping because this glyph string
1981 intentionally draws over other lines. */
1982 if (s->for_overlaps)
1983 {
1984 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1985 r.height = window_text_bottom_y (s->w) - r.y;
1986
1987 /* Alas, the above simple strategy does not work for the
1988 environments with anti-aliased text: if the same text is
1989 drawn onto the same place multiple times, it gets thicker.
1990 If the overlap we are processing is for the erased cursor, we
1991 take the intersection with the rectangle of the cursor. */
1992 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1993 {
1994 XRectangle rc, r_save = r;
1995
1996 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1997 rc.y = s->w->phys_cursor.y;
1998 rc.width = s->w->phys_cursor_width;
1999 rc.height = s->w->phys_cursor_height;
2000
2001 x_intersect_rectangles (&r_save, &rc, &r);
2002 }
2003 }
2004 else
2005 {
2006 /* Don't use S->y for clipping because it doesn't take partially
2007 visible lines into account. For example, it can be negative for
2008 partially visible lines at the top of a window. */
2009 if (!s->row->full_width_p
2010 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2011 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2012 else
2013 r.y = max (0, s->row->y);
2014 }
2015
2016 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2017
2018 /* If drawing the cursor, don't let glyph draw outside its
2019 advertised boundaries. Cleartype does this under some circumstances. */
2020 if (s->hl == DRAW_CURSOR)
2021 {
2022 struct glyph *glyph = s->first_glyph;
2023 int height, max_y;
2024
2025 if (s->x > r.x)
2026 {
2027 r.width -= s->x - r.x;
2028 r.x = s->x;
2029 }
2030 r.width = min (r.width, glyph->pixel_width);
2031
2032 /* If r.y is below window bottom, ensure that we still see a cursor. */
2033 height = min (glyph->ascent + glyph->descent,
2034 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2035 max_y = window_text_bottom_y (s->w) - height;
2036 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2037 if (s->ybase - glyph->ascent > max_y)
2038 {
2039 r.y = max_y;
2040 r.height = height;
2041 }
2042 else
2043 {
2044 /* Don't draw cursor glyph taller than our actual glyph. */
2045 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2046 if (height < r.height)
2047 {
2048 max_y = r.y + r.height;
2049 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2050 r.height = min (max_y - r.y, height);
2051 }
2052 }
2053 }
2054
2055 if (s->row->clip)
2056 {
2057 XRectangle r_save = r;
2058
2059 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2060 r.width = 0;
2061 }
2062
2063 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2064 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2065 {
2066 #ifdef CONVERT_FROM_XRECT
2067 CONVERT_FROM_XRECT (r, *rects);
2068 #else
2069 *rects = r;
2070 #endif
2071 return 1;
2072 }
2073 else
2074 {
2075 /* If we are processing overlapping and allowed to return
2076 multiple clipping rectangles, we exclude the row of the glyph
2077 string from the clipping rectangle. This is to avoid drawing
2078 the same text on the environment with anti-aliasing. */
2079 #ifdef CONVERT_FROM_XRECT
2080 XRectangle rs[2];
2081 #else
2082 XRectangle *rs = rects;
2083 #endif
2084 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2085
2086 if (s->for_overlaps & OVERLAPS_PRED)
2087 {
2088 rs[i] = r;
2089 if (r.y + r.height > row_y)
2090 {
2091 if (r.y < row_y)
2092 rs[i].height = row_y - r.y;
2093 else
2094 rs[i].height = 0;
2095 }
2096 i++;
2097 }
2098 if (s->for_overlaps & OVERLAPS_SUCC)
2099 {
2100 rs[i] = r;
2101 if (r.y < row_y + s->row->visible_height)
2102 {
2103 if (r.y + r.height > row_y + s->row->visible_height)
2104 {
2105 rs[i].y = row_y + s->row->visible_height;
2106 rs[i].height = r.y + r.height - rs[i].y;
2107 }
2108 else
2109 rs[i].height = 0;
2110 }
2111 i++;
2112 }
2113
2114 n = i;
2115 #ifdef CONVERT_FROM_XRECT
2116 for (i = 0; i < n; i++)
2117 CONVERT_FROM_XRECT (rs[i], rects[i]);
2118 #endif
2119 return n;
2120 }
2121 }
2122
2123 /* EXPORT:
2124 Return in *NR the clipping rectangle for glyph string S. */
2125
2126 void
2127 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2128 {
2129 get_glyph_string_clip_rects (s, nr, 1);
2130 }
2131
2132
2133 /* EXPORT:
2134 Return the position and height of the phys cursor in window W.
2135 Set w->phys_cursor_width to width of phys cursor.
2136 */
2137
2138 void
2139 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2140 struct glyph *glyph, int *xp, int *yp, int *heightp)
2141 {
2142 struct frame *f = XFRAME (WINDOW_FRAME (w));
2143 int x, y, wd, h, h0, y0;
2144
2145 /* Compute the width of the rectangle to draw. If on a stretch
2146 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2147 rectangle as wide as the glyph, but use a canonical character
2148 width instead. */
2149 wd = glyph->pixel_width - 1;
2150 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2151 wd++; /* Why? */
2152 #endif
2153
2154 x = w->phys_cursor.x;
2155 if (x < 0)
2156 {
2157 wd += x;
2158 x = 0;
2159 }
2160
2161 if (glyph->type == STRETCH_GLYPH
2162 && !x_stretch_cursor_p)
2163 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2164 w->phys_cursor_width = wd;
2165
2166 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2167
2168 /* If y is below window bottom, ensure that we still see a cursor. */
2169 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2170
2171 h = max (h0, glyph->ascent + glyph->descent);
2172 h0 = min (h0, glyph->ascent + glyph->descent);
2173
2174 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2175 if (y < y0)
2176 {
2177 h = max (h - (y0 - y) + 1, h0);
2178 y = y0 - 1;
2179 }
2180 else
2181 {
2182 y0 = window_text_bottom_y (w) - h0;
2183 if (y > y0)
2184 {
2185 h += y - y0;
2186 y = y0;
2187 }
2188 }
2189
2190 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2191 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2192 *heightp = h;
2193 }
2194
2195 /*
2196 * Remember which glyph the mouse is over.
2197 */
2198
2199 void
2200 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2201 {
2202 Lisp_Object window;
2203 struct window *w;
2204 struct glyph_row *r, *gr, *end_row;
2205 enum window_part part;
2206 enum glyph_row_area area;
2207 int x, y, width, height;
2208
2209 /* Try to determine frame pixel position and size of the glyph under
2210 frame pixel coordinates X/Y on frame F. */
2211
2212 if (!f->glyphs_initialized_p
2213 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2214 NILP (window)))
2215 {
2216 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2217 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2218 goto virtual_glyph;
2219 }
2220
2221 w = XWINDOW (window);
2222 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2223 height = WINDOW_FRAME_LINE_HEIGHT (w);
2224
2225 x = window_relative_x_coord (w, part, gx);
2226 y = gy - WINDOW_TOP_EDGE_Y (w);
2227
2228 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2229 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2230
2231 if (w->pseudo_window_p)
2232 {
2233 area = TEXT_AREA;
2234 part = ON_MODE_LINE; /* Don't adjust margin. */
2235 goto text_glyph;
2236 }
2237
2238 switch (part)
2239 {
2240 case ON_LEFT_MARGIN:
2241 area = LEFT_MARGIN_AREA;
2242 goto text_glyph;
2243
2244 case ON_RIGHT_MARGIN:
2245 area = RIGHT_MARGIN_AREA;
2246 goto text_glyph;
2247
2248 case ON_HEADER_LINE:
2249 case ON_MODE_LINE:
2250 gr = (part == ON_HEADER_LINE
2251 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2252 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2253 gy = gr->y;
2254 area = TEXT_AREA;
2255 goto text_glyph_row_found;
2256
2257 case ON_TEXT:
2258 area = TEXT_AREA;
2259
2260 text_glyph:
2261 gr = 0; gy = 0;
2262 for (; r <= end_row && r->enabled_p; ++r)
2263 if (r->y + r->height > y)
2264 {
2265 gr = r; gy = r->y;
2266 break;
2267 }
2268
2269 text_glyph_row_found:
2270 if (gr && gy <= y)
2271 {
2272 struct glyph *g = gr->glyphs[area];
2273 struct glyph *end = g + gr->used[area];
2274
2275 height = gr->height;
2276 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2277 if (gx + g->pixel_width > x)
2278 break;
2279
2280 if (g < end)
2281 {
2282 if (g->type == IMAGE_GLYPH)
2283 {
2284 /* Don't remember when mouse is over image, as
2285 image may have hot-spots. */
2286 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2287 return;
2288 }
2289 width = g->pixel_width;
2290 }
2291 else
2292 {
2293 /* Use nominal char spacing at end of line. */
2294 x -= gx;
2295 gx += (x / width) * width;
2296 }
2297
2298 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2299 gx += window_box_left_offset (w, area);
2300 }
2301 else
2302 {
2303 /* Use nominal line height at end of window. */
2304 gx = (x / width) * width;
2305 y -= gy;
2306 gy += (y / height) * height;
2307 }
2308 break;
2309
2310 case ON_LEFT_FRINGE:
2311 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2312 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2313 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2314 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2315 goto row_glyph;
2316
2317 case ON_RIGHT_FRINGE:
2318 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2319 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2320 : window_box_right_offset (w, TEXT_AREA));
2321 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2322 goto row_glyph;
2323
2324 case ON_SCROLL_BAR:
2325 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2326 ? 0
2327 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2328 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2329 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2330 : 0)));
2331 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2332
2333 row_glyph:
2334 gr = 0, gy = 0;
2335 for (; r <= end_row && r->enabled_p; ++r)
2336 if (r->y + r->height > y)
2337 {
2338 gr = r; gy = r->y;
2339 break;
2340 }
2341
2342 if (gr && gy <= y)
2343 height = gr->height;
2344 else
2345 {
2346 /* Use nominal line height at end of window. */
2347 y -= gy;
2348 gy += (y / height) * height;
2349 }
2350 break;
2351
2352 default:
2353 ;
2354 virtual_glyph:
2355 /* If there is no glyph under the mouse, then we divide the screen
2356 into a grid of the smallest glyph in the frame, and use that
2357 as our "glyph". */
2358
2359 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2360 round down even for negative values. */
2361 if (gx < 0)
2362 gx -= width - 1;
2363 if (gy < 0)
2364 gy -= height - 1;
2365
2366 gx = (gx / width) * width;
2367 gy = (gy / height) * height;
2368
2369 goto store_rect;
2370 }
2371
2372 gx += WINDOW_LEFT_EDGE_X (w);
2373 gy += WINDOW_TOP_EDGE_Y (w);
2374
2375 store_rect:
2376 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2377
2378 /* Visible feedback for debugging. */
2379 #if 0
2380 #if HAVE_X_WINDOWS
2381 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2382 f->output_data.x->normal_gc,
2383 gx, gy, width, height);
2384 #endif
2385 #endif
2386 }
2387
2388
2389 #endif /* HAVE_WINDOW_SYSTEM */
2390
2391 \f
2392 /***********************************************************************
2393 Lisp form evaluation
2394 ***********************************************************************/
2395
2396 /* Error handler for safe_eval and safe_call. */
2397
2398 static Lisp_Object
2399 safe_eval_handler (Lisp_Object arg)
2400 {
2401 add_to_log ("Error during redisplay: %S", arg, Qnil);
2402 return Qnil;
2403 }
2404
2405
2406 /* Evaluate SEXPR and return the result, or nil if something went
2407 wrong. Prevent redisplay during the evaluation. */
2408
2409 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2410 Return the result, or nil if something went wrong. Prevent
2411 redisplay during the evaluation. */
2412
2413 Lisp_Object
2414 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2415 {
2416 Lisp_Object val;
2417
2418 if (inhibit_eval_during_redisplay)
2419 val = Qnil;
2420 else
2421 {
2422 ptrdiff_t count = SPECPDL_INDEX ();
2423 struct gcpro gcpro1;
2424
2425 GCPRO1 (args[0]);
2426 gcpro1.nvars = nargs;
2427 specbind (Qinhibit_redisplay, Qt);
2428 /* Use Qt to ensure debugger does not run,
2429 so there is no possibility of wanting to redisplay. */
2430 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2431 safe_eval_handler);
2432 UNGCPRO;
2433 val = unbind_to (count, val);
2434 }
2435
2436 return val;
2437 }
2438
2439
2440 /* Call function FN with one argument ARG.
2441 Return the result, or nil if something went wrong. */
2442
2443 Lisp_Object
2444 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2445 {
2446 Lisp_Object args[2];
2447 args[0] = fn;
2448 args[1] = arg;
2449 return safe_call (2, args);
2450 }
2451
2452 static Lisp_Object Qeval;
2453
2454 Lisp_Object
2455 safe_eval (Lisp_Object sexpr)
2456 {
2457 return safe_call1 (Qeval, sexpr);
2458 }
2459
2460 /* Call function FN with one argument ARG.
2461 Return the result, or nil if something went wrong. */
2462
2463 Lisp_Object
2464 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2465 {
2466 Lisp_Object args[3];
2467 args[0] = fn;
2468 args[1] = arg1;
2469 args[2] = arg2;
2470 return safe_call (3, args);
2471 }
2472
2473
2474 \f
2475 /***********************************************************************
2476 Debugging
2477 ***********************************************************************/
2478
2479 #if 0
2480
2481 /* Define CHECK_IT to perform sanity checks on iterators.
2482 This is for debugging. It is too slow to do unconditionally. */
2483
2484 static void
2485 check_it (struct it *it)
2486 {
2487 if (it->method == GET_FROM_STRING)
2488 {
2489 eassert (STRINGP (it->string));
2490 eassert (IT_STRING_CHARPOS (*it) >= 0);
2491 }
2492 else
2493 {
2494 eassert (IT_STRING_CHARPOS (*it) < 0);
2495 if (it->method == GET_FROM_BUFFER)
2496 {
2497 /* Check that character and byte positions agree. */
2498 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2499 }
2500 }
2501
2502 if (it->dpvec)
2503 eassert (it->current.dpvec_index >= 0);
2504 else
2505 eassert (it->current.dpvec_index < 0);
2506 }
2507
2508 #define CHECK_IT(IT) check_it ((IT))
2509
2510 #else /* not 0 */
2511
2512 #define CHECK_IT(IT) (void) 0
2513
2514 #endif /* not 0 */
2515
2516
2517 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2518
2519 /* Check that the window end of window W is what we expect it
2520 to be---the last row in the current matrix displaying text. */
2521
2522 static void
2523 check_window_end (struct window *w)
2524 {
2525 if (!MINI_WINDOW_P (w)
2526 && !NILP (w->window_end_valid))
2527 {
2528 struct glyph_row *row;
2529 eassert ((row = MATRIX_ROW (w->current_matrix,
2530 XFASTINT (w->window_end_vpos)),
2531 !row->enabled_p
2532 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2533 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2534 }
2535 }
2536
2537 #define CHECK_WINDOW_END(W) check_window_end ((W))
2538
2539 #else
2540
2541 #define CHECK_WINDOW_END(W) (void) 0
2542
2543 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2544
2545
2546 \f
2547 /***********************************************************************
2548 Iterator initialization
2549 ***********************************************************************/
2550
2551 /* Initialize IT for displaying current_buffer in window W, starting
2552 at character position CHARPOS. CHARPOS < 0 means that no buffer
2553 position is specified which is useful when the iterator is assigned
2554 a position later. BYTEPOS is the byte position corresponding to
2555 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2556
2557 If ROW is not null, calls to produce_glyphs with IT as parameter
2558 will produce glyphs in that row.
2559
2560 BASE_FACE_ID is the id of a base face to use. It must be one of
2561 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2562 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2563 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2564
2565 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2566 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2567 will be initialized to use the corresponding mode line glyph row of
2568 the desired matrix of W. */
2569
2570 void
2571 init_iterator (struct it *it, struct window *w,
2572 ptrdiff_t charpos, ptrdiff_t bytepos,
2573 struct glyph_row *row, enum face_id base_face_id)
2574 {
2575 int highlight_region_p;
2576 enum face_id remapped_base_face_id = base_face_id;
2577
2578 /* Some precondition checks. */
2579 eassert (w != NULL && it != NULL);
2580 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2581 && charpos <= ZV));
2582
2583 /* If face attributes have been changed since the last redisplay,
2584 free realized faces now because they depend on face definitions
2585 that might have changed. Don't free faces while there might be
2586 desired matrices pending which reference these faces. */
2587 if (face_change_count && !inhibit_free_realized_faces)
2588 {
2589 face_change_count = 0;
2590 free_all_realized_faces (Qnil);
2591 }
2592
2593 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2594 if (! NILP (Vface_remapping_alist))
2595 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2596
2597 /* Use one of the mode line rows of W's desired matrix if
2598 appropriate. */
2599 if (row == NULL)
2600 {
2601 if (base_face_id == MODE_LINE_FACE_ID
2602 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2603 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2604 else if (base_face_id == HEADER_LINE_FACE_ID)
2605 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2606 }
2607
2608 /* Clear IT. */
2609 memset (it, 0, sizeof *it);
2610 it->current.overlay_string_index = -1;
2611 it->current.dpvec_index = -1;
2612 it->base_face_id = remapped_base_face_id;
2613 it->string = Qnil;
2614 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2615 it->paragraph_embedding = L2R;
2616 it->bidi_it.string.lstring = Qnil;
2617 it->bidi_it.string.s = NULL;
2618 it->bidi_it.string.bufpos = 0;
2619
2620 /* The window in which we iterate over current_buffer: */
2621 XSETWINDOW (it->window, w);
2622 it->w = w;
2623 it->f = XFRAME (w->frame);
2624
2625 it->cmp_it.id = -1;
2626
2627 /* Extra space between lines (on window systems only). */
2628 if (base_face_id == DEFAULT_FACE_ID
2629 && FRAME_WINDOW_P (it->f))
2630 {
2631 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2632 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2633 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2634 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2635 * FRAME_LINE_HEIGHT (it->f));
2636 else if (it->f->extra_line_spacing > 0)
2637 it->extra_line_spacing = it->f->extra_line_spacing;
2638 it->max_extra_line_spacing = 0;
2639 }
2640
2641 /* If realized faces have been removed, e.g. because of face
2642 attribute changes of named faces, recompute them. When running
2643 in batch mode, the face cache of the initial frame is null. If
2644 we happen to get called, make a dummy face cache. */
2645 if (FRAME_FACE_CACHE (it->f) == NULL)
2646 init_frame_faces (it->f);
2647 if (FRAME_FACE_CACHE (it->f)->used == 0)
2648 recompute_basic_faces (it->f);
2649
2650 /* Current value of the `slice', `space-width', and 'height' properties. */
2651 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2652 it->space_width = Qnil;
2653 it->font_height = Qnil;
2654 it->override_ascent = -1;
2655
2656 /* Are control characters displayed as `^C'? */
2657 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2658
2659 /* -1 means everything between a CR and the following line end
2660 is invisible. >0 means lines indented more than this value are
2661 invisible. */
2662 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2663 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2664 selective_display)),
2665 PTRDIFF_MAX)
2666 : (!NILP (BVAR (current_buffer, selective_display))
2667 ? -1 : 0));
2668 it->selective_display_ellipsis_p
2669 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2670
2671 /* Display table to use. */
2672 it->dp = window_display_table (w);
2673
2674 /* Are multibyte characters enabled in current_buffer? */
2675 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2676
2677 /* Non-zero if we should highlight the region. */
2678 highlight_region_p
2679 = (!NILP (Vtransient_mark_mode)
2680 && !NILP (BVAR (current_buffer, mark_active))
2681 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2682
2683 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2684 start and end of a visible region in window IT->w. Set both to
2685 -1 to indicate no region. */
2686 if (highlight_region_p
2687 /* Maybe highlight only in selected window. */
2688 && (/* Either show region everywhere. */
2689 highlight_nonselected_windows
2690 /* Or show region in the selected window. */
2691 || w == XWINDOW (selected_window)
2692 /* Or show the region if we are in the mini-buffer and W is
2693 the window the mini-buffer refers to. */
2694 || (MINI_WINDOW_P (XWINDOW (selected_window))
2695 && WINDOWP (minibuf_selected_window)
2696 && w == XWINDOW (minibuf_selected_window))))
2697 {
2698 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2699 it->region_beg_charpos = min (PT, markpos);
2700 it->region_end_charpos = max (PT, markpos);
2701 }
2702 else
2703 it->region_beg_charpos = it->region_end_charpos = -1;
2704
2705 /* Get the position at which the redisplay_end_trigger hook should
2706 be run, if it is to be run at all. */
2707 if (MARKERP (w->redisplay_end_trigger)
2708 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2709 it->redisplay_end_trigger_charpos
2710 = marker_position (w->redisplay_end_trigger);
2711 else if (INTEGERP (w->redisplay_end_trigger))
2712 it->redisplay_end_trigger_charpos =
2713 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2714
2715 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2716
2717 /* Are lines in the display truncated? */
2718 if (base_face_id != DEFAULT_FACE_ID
2719 || it->w->hscroll
2720 || (! WINDOW_FULL_WIDTH_P (it->w)
2721 && ((!NILP (Vtruncate_partial_width_windows)
2722 && !INTEGERP (Vtruncate_partial_width_windows))
2723 || (INTEGERP (Vtruncate_partial_width_windows)
2724 && (WINDOW_TOTAL_COLS (it->w)
2725 < XINT (Vtruncate_partial_width_windows))))))
2726 it->line_wrap = TRUNCATE;
2727 else if (NILP (BVAR (current_buffer, truncate_lines)))
2728 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2729 ? WINDOW_WRAP : WORD_WRAP;
2730 else
2731 it->line_wrap = TRUNCATE;
2732
2733 /* Get dimensions of truncation and continuation glyphs. These are
2734 displayed as fringe bitmaps under X, but we need them for such
2735 frames when the fringes are turned off. */
2736 if (it->line_wrap == TRUNCATE)
2737 {
2738 /* We will need the truncation glyph. */
2739 eassert (it->glyph_row == NULL);
2740 produce_special_glyphs (it, IT_TRUNCATION);
2741 it->truncation_pixel_width = it->pixel_width;
2742 }
2743 else
2744 {
2745 /* We will need the continuation glyph. */
2746 eassert (it->glyph_row == NULL);
2747 produce_special_glyphs (it, IT_CONTINUATION);
2748 it->continuation_pixel_width = it->pixel_width;
2749 }
2750
2751 /* Reset these values to zero because the produce_special_glyphs
2752 above has changed them. */
2753 it->pixel_width = it->ascent = it->descent = 0;
2754 it->phys_ascent = it->phys_descent = 0;
2755
2756 /* Set this after getting the dimensions of truncation and
2757 continuation glyphs, so that we don't produce glyphs when calling
2758 produce_special_glyphs, above. */
2759 it->glyph_row = row;
2760 it->area = TEXT_AREA;
2761
2762 /* Forget any previous info about this row being reversed. */
2763 if (it->glyph_row)
2764 it->glyph_row->reversed_p = 0;
2765
2766 /* Get the dimensions of the display area. The display area
2767 consists of the visible window area plus a horizontally scrolled
2768 part to the left of the window. All x-values are relative to the
2769 start of this total display area. */
2770 if (base_face_id != DEFAULT_FACE_ID)
2771 {
2772 /* Mode lines, menu bar in terminal frames. */
2773 it->first_visible_x = 0;
2774 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2775 }
2776 else
2777 {
2778 it->first_visible_x =
2779 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2780 it->last_visible_x = (it->first_visible_x
2781 + window_box_width (w, TEXT_AREA));
2782
2783 /* If we truncate lines, leave room for the truncation glyph(s) at
2784 the right margin. Otherwise, leave room for the continuation
2785 glyph(s). Done only if the window has no fringes. Since we
2786 don't know at this point whether there will be any R2L lines in
2787 the window, we reserve space for truncation/continuation glyphs
2788 even if only one of the fringes is absent. */
2789 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2790 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2791 {
2792 if (it->line_wrap == TRUNCATE)
2793 it->last_visible_x -= it->truncation_pixel_width;
2794 else
2795 it->last_visible_x -= it->continuation_pixel_width;
2796 }
2797
2798 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2799 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2800 }
2801
2802 /* Leave room for a border glyph. */
2803 if (!FRAME_WINDOW_P (it->f)
2804 && !WINDOW_RIGHTMOST_P (it->w))
2805 it->last_visible_x -= 1;
2806
2807 it->last_visible_y = window_text_bottom_y (w);
2808
2809 /* For mode lines and alike, arrange for the first glyph having a
2810 left box line if the face specifies a box. */
2811 if (base_face_id != DEFAULT_FACE_ID)
2812 {
2813 struct face *face;
2814
2815 it->face_id = remapped_base_face_id;
2816
2817 /* If we have a boxed mode line, make the first character appear
2818 with a left box line. */
2819 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2820 if (face->box != FACE_NO_BOX)
2821 it->start_of_box_run_p = 1;
2822 }
2823
2824 /* If a buffer position was specified, set the iterator there,
2825 getting overlays and face properties from that position. */
2826 if (charpos >= BUF_BEG (current_buffer))
2827 {
2828 it->end_charpos = ZV;
2829 IT_CHARPOS (*it) = charpos;
2830
2831 /* We will rely on `reseat' to set this up properly, via
2832 handle_face_prop. */
2833 it->face_id = it->base_face_id;
2834
2835 /* Compute byte position if not specified. */
2836 if (bytepos < charpos)
2837 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2838 else
2839 IT_BYTEPOS (*it) = bytepos;
2840
2841 it->start = it->current;
2842 /* Do we need to reorder bidirectional text? Not if this is a
2843 unibyte buffer: by definition, none of the single-byte
2844 characters are strong R2L, so no reordering is needed. And
2845 bidi.c doesn't support unibyte buffers anyway. Also, don't
2846 reorder while we are loading loadup.el, since the tables of
2847 character properties needed for reordering are not yet
2848 available. */
2849 it->bidi_p =
2850 NILP (Vpurify_flag)
2851 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2852 && it->multibyte_p;
2853
2854 /* If we are to reorder bidirectional text, init the bidi
2855 iterator. */
2856 if (it->bidi_p)
2857 {
2858 /* Note the paragraph direction that this buffer wants to
2859 use. */
2860 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2861 Qleft_to_right))
2862 it->paragraph_embedding = L2R;
2863 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2864 Qright_to_left))
2865 it->paragraph_embedding = R2L;
2866 else
2867 it->paragraph_embedding = NEUTRAL_DIR;
2868 bidi_unshelve_cache (NULL, 0);
2869 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2870 &it->bidi_it);
2871 }
2872
2873 /* Compute faces etc. */
2874 reseat (it, it->current.pos, 1);
2875 }
2876
2877 CHECK_IT (it);
2878 }
2879
2880
2881 /* Initialize IT for the display of window W with window start POS. */
2882
2883 void
2884 start_display (struct it *it, struct window *w, struct text_pos pos)
2885 {
2886 struct glyph_row *row;
2887 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2888
2889 row = w->desired_matrix->rows + first_vpos;
2890 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2891 it->first_vpos = first_vpos;
2892
2893 /* Don't reseat to previous visible line start if current start
2894 position is in a string or image. */
2895 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2896 {
2897 int start_at_line_beg_p;
2898 int first_y = it->current_y;
2899
2900 /* If window start is not at a line start, skip forward to POS to
2901 get the correct continuation lines width. */
2902 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2903 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2904 if (!start_at_line_beg_p)
2905 {
2906 int new_x;
2907
2908 reseat_at_previous_visible_line_start (it);
2909 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2910
2911 new_x = it->current_x + it->pixel_width;
2912
2913 /* If lines are continued, this line may end in the middle
2914 of a multi-glyph character (e.g. a control character
2915 displayed as \003, or in the middle of an overlay
2916 string). In this case move_it_to above will not have
2917 taken us to the start of the continuation line but to the
2918 end of the continued line. */
2919 if (it->current_x > 0
2920 && it->line_wrap != TRUNCATE /* Lines are continued. */
2921 && (/* And glyph doesn't fit on the line. */
2922 new_x > it->last_visible_x
2923 /* Or it fits exactly and we're on a window
2924 system frame. */
2925 || (new_x == it->last_visible_x
2926 && FRAME_WINDOW_P (it->f)
2927 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2928 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2929 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2930 {
2931 if ((it->current.dpvec_index >= 0
2932 || it->current.overlay_string_index >= 0)
2933 /* If we are on a newline from a display vector or
2934 overlay string, then we are already at the end of
2935 a screen line; no need to go to the next line in
2936 that case, as this line is not really continued.
2937 (If we do go to the next line, C-e will not DTRT.) */
2938 && it->c != '\n')
2939 {
2940 set_iterator_to_next (it, 1);
2941 move_it_in_display_line_to (it, -1, -1, 0);
2942 }
2943
2944 it->continuation_lines_width += it->current_x;
2945 }
2946 /* If the character at POS is displayed via a display
2947 vector, move_it_to above stops at the final glyph of
2948 IT->dpvec. To make the caller redisplay that character
2949 again (a.k.a. start at POS), we need to reset the
2950 dpvec_index to the beginning of IT->dpvec. */
2951 else if (it->current.dpvec_index >= 0)
2952 it->current.dpvec_index = 0;
2953
2954 /* We're starting a new display line, not affected by the
2955 height of the continued line, so clear the appropriate
2956 fields in the iterator structure. */
2957 it->max_ascent = it->max_descent = 0;
2958 it->max_phys_ascent = it->max_phys_descent = 0;
2959
2960 it->current_y = first_y;
2961 it->vpos = 0;
2962 it->current_x = it->hpos = 0;
2963 }
2964 }
2965 }
2966
2967
2968 /* Return 1 if POS is a position in ellipses displayed for invisible
2969 text. W is the window we display, for text property lookup. */
2970
2971 static int
2972 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2973 {
2974 Lisp_Object prop, window;
2975 int ellipses_p = 0;
2976 ptrdiff_t charpos = CHARPOS (pos->pos);
2977
2978 /* If POS specifies a position in a display vector, this might
2979 be for an ellipsis displayed for invisible text. We won't
2980 get the iterator set up for delivering that ellipsis unless
2981 we make sure that it gets aware of the invisible text. */
2982 if (pos->dpvec_index >= 0
2983 && pos->overlay_string_index < 0
2984 && CHARPOS (pos->string_pos) < 0
2985 && charpos > BEGV
2986 && (XSETWINDOW (window, w),
2987 prop = Fget_char_property (make_number (charpos),
2988 Qinvisible, window),
2989 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2990 {
2991 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2992 window);
2993 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2994 }
2995
2996 return ellipses_p;
2997 }
2998
2999
3000 /* Initialize IT for stepping through current_buffer in window W,
3001 starting at position POS that includes overlay string and display
3002 vector/ control character translation position information. Value
3003 is zero if there are overlay strings with newlines at POS. */
3004
3005 static int
3006 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3007 {
3008 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3009 int i, overlay_strings_with_newlines = 0;
3010
3011 /* If POS specifies a position in a display vector, this might
3012 be for an ellipsis displayed for invisible text. We won't
3013 get the iterator set up for delivering that ellipsis unless
3014 we make sure that it gets aware of the invisible text. */
3015 if (in_ellipses_for_invisible_text_p (pos, w))
3016 {
3017 --charpos;
3018 bytepos = 0;
3019 }
3020
3021 /* Keep in mind: the call to reseat in init_iterator skips invisible
3022 text, so we might end up at a position different from POS. This
3023 is only a problem when POS is a row start after a newline and an
3024 overlay starts there with an after-string, and the overlay has an
3025 invisible property. Since we don't skip invisible text in
3026 display_line and elsewhere immediately after consuming the
3027 newline before the row start, such a POS will not be in a string,
3028 but the call to init_iterator below will move us to the
3029 after-string. */
3030 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3031
3032 /* This only scans the current chunk -- it should scan all chunks.
3033 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3034 to 16 in 22.1 to make this a lesser problem. */
3035 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3036 {
3037 const char *s = SSDATA (it->overlay_strings[i]);
3038 const char *e = s + SBYTES (it->overlay_strings[i]);
3039
3040 while (s < e && *s != '\n')
3041 ++s;
3042
3043 if (s < e)
3044 {
3045 overlay_strings_with_newlines = 1;
3046 break;
3047 }
3048 }
3049
3050 /* If position is within an overlay string, set up IT to the right
3051 overlay string. */
3052 if (pos->overlay_string_index >= 0)
3053 {
3054 int relative_index;
3055
3056 /* If the first overlay string happens to have a `display'
3057 property for an image, the iterator will be set up for that
3058 image, and we have to undo that setup first before we can
3059 correct the overlay string index. */
3060 if (it->method == GET_FROM_IMAGE)
3061 pop_it (it);
3062
3063 /* We already have the first chunk of overlay strings in
3064 IT->overlay_strings. Load more until the one for
3065 pos->overlay_string_index is in IT->overlay_strings. */
3066 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3067 {
3068 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3069 it->current.overlay_string_index = 0;
3070 while (n--)
3071 {
3072 load_overlay_strings (it, 0);
3073 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3074 }
3075 }
3076
3077 it->current.overlay_string_index = pos->overlay_string_index;
3078 relative_index = (it->current.overlay_string_index
3079 % OVERLAY_STRING_CHUNK_SIZE);
3080 it->string = it->overlay_strings[relative_index];
3081 eassert (STRINGP (it->string));
3082 it->current.string_pos = pos->string_pos;
3083 it->method = GET_FROM_STRING;
3084 }
3085
3086 if (CHARPOS (pos->string_pos) >= 0)
3087 {
3088 /* Recorded position is not in an overlay string, but in another
3089 string. This can only be a string from a `display' property.
3090 IT should already be filled with that string. */
3091 it->current.string_pos = pos->string_pos;
3092 eassert (STRINGP (it->string));
3093 }
3094
3095 /* Restore position in display vector translations, control
3096 character translations or ellipses. */
3097 if (pos->dpvec_index >= 0)
3098 {
3099 if (it->dpvec == NULL)
3100 get_next_display_element (it);
3101 eassert (it->dpvec && it->current.dpvec_index == 0);
3102 it->current.dpvec_index = pos->dpvec_index;
3103 }
3104
3105 CHECK_IT (it);
3106 return !overlay_strings_with_newlines;
3107 }
3108
3109
3110 /* Initialize IT for stepping through current_buffer in window W
3111 starting at ROW->start. */
3112
3113 static void
3114 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3115 {
3116 init_from_display_pos (it, w, &row->start);
3117 it->start = row->start;
3118 it->continuation_lines_width = row->continuation_lines_width;
3119 CHECK_IT (it);
3120 }
3121
3122
3123 /* Initialize IT for stepping through current_buffer in window W
3124 starting in the line following ROW, i.e. starting at ROW->end.
3125 Value is zero if there are overlay strings with newlines at ROW's
3126 end position. */
3127
3128 static int
3129 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3130 {
3131 int success = 0;
3132
3133 if (init_from_display_pos (it, w, &row->end))
3134 {
3135 if (row->continued_p)
3136 it->continuation_lines_width
3137 = row->continuation_lines_width + row->pixel_width;
3138 CHECK_IT (it);
3139 success = 1;
3140 }
3141
3142 return success;
3143 }
3144
3145
3146
3147 \f
3148 /***********************************************************************
3149 Text properties
3150 ***********************************************************************/
3151
3152 /* Called when IT reaches IT->stop_charpos. Handle text property and
3153 overlay changes. Set IT->stop_charpos to the next position where
3154 to stop. */
3155
3156 static void
3157 handle_stop (struct it *it)
3158 {
3159 enum prop_handled handled;
3160 int handle_overlay_change_p;
3161 struct props *p;
3162
3163 it->dpvec = NULL;
3164 it->current.dpvec_index = -1;
3165 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3166 it->ignore_overlay_strings_at_pos_p = 0;
3167 it->ellipsis_p = 0;
3168
3169 /* Use face of preceding text for ellipsis (if invisible) */
3170 if (it->selective_display_ellipsis_p)
3171 it->saved_face_id = it->face_id;
3172
3173 do
3174 {
3175 handled = HANDLED_NORMALLY;
3176
3177 /* Call text property handlers. */
3178 for (p = it_props; p->handler; ++p)
3179 {
3180 handled = p->handler (it);
3181
3182 if (handled == HANDLED_RECOMPUTE_PROPS)
3183 break;
3184 else if (handled == HANDLED_RETURN)
3185 {
3186 /* We still want to show before and after strings from
3187 overlays even if the actual buffer text is replaced. */
3188 if (!handle_overlay_change_p
3189 || it->sp > 1
3190 /* Don't call get_overlay_strings_1 if we already
3191 have overlay strings loaded, because doing so
3192 will load them again and push the iterator state
3193 onto the stack one more time, which is not
3194 expected by the rest of the code that processes
3195 overlay strings. */
3196 || (it->current.overlay_string_index < 0
3197 ? !get_overlay_strings_1 (it, 0, 0)
3198 : 0))
3199 {
3200 if (it->ellipsis_p)
3201 setup_for_ellipsis (it, 0);
3202 /* When handling a display spec, we might load an
3203 empty string. In that case, discard it here. We
3204 used to discard it in handle_single_display_spec,
3205 but that causes get_overlay_strings_1, above, to
3206 ignore overlay strings that we must check. */
3207 if (STRINGP (it->string) && !SCHARS (it->string))
3208 pop_it (it);
3209 return;
3210 }
3211 else if (STRINGP (it->string) && !SCHARS (it->string))
3212 pop_it (it);
3213 else
3214 {
3215 it->ignore_overlay_strings_at_pos_p = 1;
3216 it->string_from_display_prop_p = 0;
3217 it->from_disp_prop_p = 0;
3218 handle_overlay_change_p = 0;
3219 }
3220 handled = HANDLED_RECOMPUTE_PROPS;
3221 break;
3222 }
3223 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3224 handle_overlay_change_p = 0;
3225 }
3226
3227 if (handled != HANDLED_RECOMPUTE_PROPS)
3228 {
3229 /* Don't check for overlay strings below when set to deliver
3230 characters from a display vector. */
3231 if (it->method == GET_FROM_DISPLAY_VECTOR)
3232 handle_overlay_change_p = 0;
3233
3234 /* Handle overlay changes.
3235 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3236 if it finds overlays. */
3237 if (handle_overlay_change_p)
3238 handled = handle_overlay_change (it);
3239 }
3240
3241 if (it->ellipsis_p)
3242 {
3243 setup_for_ellipsis (it, 0);
3244 break;
3245 }
3246 }
3247 while (handled == HANDLED_RECOMPUTE_PROPS);
3248
3249 /* Determine where to stop next. */
3250 if (handled == HANDLED_NORMALLY)
3251 compute_stop_pos (it);
3252 }
3253
3254
3255 /* Compute IT->stop_charpos from text property and overlay change
3256 information for IT's current position. */
3257
3258 static void
3259 compute_stop_pos (struct it *it)
3260 {
3261 register INTERVAL iv, next_iv;
3262 Lisp_Object object, limit, position;
3263 ptrdiff_t charpos, bytepos;
3264
3265 if (STRINGP (it->string))
3266 {
3267 /* Strings are usually short, so don't limit the search for
3268 properties. */
3269 it->stop_charpos = it->end_charpos;
3270 object = it->string;
3271 limit = Qnil;
3272 charpos = IT_STRING_CHARPOS (*it);
3273 bytepos = IT_STRING_BYTEPOS (*it);
3274 }
3275 else
3276 {
3277 ptrdiff_t pos;
3278
3279 /* If end_charpos is out of range for some reason, such as a
3280 misbehaving display function, rationalize it (Bug#5984). */
3281 if (it->end_charpos > ZV)
3282 it->end_charpos = ZV;
3283 it->stop_charpos = it->end_charpos;
3284
3285 /* If next overlay change is in front of the current stop pos
3286 (which is IT->end_charpos), stop there. Note: value of
3287 next_overlay_change is point-max if no overlay change
3288 follows. */
3289 charpos = IT_CHARPOS (*it);
3290 bytepos = IT_BYTEPOS (*it);
3291 pos = next_overlay_change (charpos);
3292 if (pos < it->stop_charpos)
3293 it->stop_charpos = pos;
3294
3295 /* If showing the region, we have to stop at the region
3296 start or end because the face might change there. */
3297 if (it->region_beg_charpos > 0)
3298 {
3299 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3300 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3301 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3302 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3303 }
3304
3305 /* Set up variables for computing the stop position from text
3306 property changes. */
3307 XSETBUFFER (object, current_buffer);
3308 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3309 }
3310
3311 /* Get the interval containing IT's position. Value is a null
3312 interval if there isn't such an interval. */
3313 position = make_number (charpos);
3314 iv = validate_interval_range (object, &position, &position, 0);
3315 if (!NULL_INTERVAL_P (iv))
3316 {
3317 Lisp_Object values_here[LAST_PROP_IDX];
3318 struct props *p;
3319
3320 /* Get properties here. */
3321 for (p = it_props; p->handler; ++p)
3322 values_here[p->idx] = textget (iv->plist, *p->name);
3323
3324 /* Look for an interval following iv that has different
3325 properties. */
3326 for (next_iv = next_interval (iv);
3327 (!NULL_INTERVAL_P (next_iv)
3328 && (NILP (limit)
3329 || XFASTINT (limit) > next_iv->position));
3330 next_iv = next_interval (next_iv))
3331 {
3332 for (p = it_props; p->handler; ++p)
3333 {
3334 Lisp_Object new_value;
3335
3336 new_value = textget (next_iv->plist, *p->name);
3337 if (!EQ (values_here[p->idx], new_value))
3338 break;
3339 }
3340
3341 if (p->handler)
3342 break;
3343 }
3344
3345 if (!NULL_INTERVAL_P (next_iv))
3346 {
3347 if (INTEGERP (limit)
3348 && next_iv->position >= XFASTINT (limit))
3349 /* No text property change up to limit. */
3350 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3351 else
3352 /* Text properties change in next_iv. */
3353 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3354 }
3355 }
3356
3357 if (it->cmp_it.id < 0)
3358 {
3359 ptrdiff_t stoppos = it->end_charpos;
3360
3361 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3362 stoppos = -1;
3363 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3364 stoppos, it->string);
3365 }
3366
3367 eassert (STRINGP (it->string)
3368 || (it->stop_charpos >= BEGV
3369 && it->stop_charpos >= IT_CHARPOS (*it)));
3370 }
3371
3372
3373 /* Return the position of the next overlay change after POS in
3374 current_buffer. Value is point-max if no overlay change
3375 follows. This is like `next-overlay-change' but doesn't use
3376 xmalloc. */
3377
3378 static ptrdiff_t
3379 next_overlay_change (ptrdiff_t pos)
3380 {
3381 ptrdiff_t i, noverlays;
3382 ptrdiff_t endpos;
3383 Lisp_Object *overlays;
3384
3385 /* Get all overlays at the given position. */
3386 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3387
3388 /* If any of these overlays ends before endpos,
3389 use its ending point instead. */
3390 for (i = 0; i < noverlays; ++i)
3391 {
3392 Lisp_Object oend;
3393 ptrdiff_t oendpos;
3394
3395 oend = OVERLAY_END (overlays[i]);
3396 oendpos = OVERLAY_POSITION (oend);
3397 endpos = min (endpos, oendpos);
3398 }
3399
3400 return endpos;
3401 }
3402
3403 /* How many characters forward to search for a display property or
3404 display string. Searching too far forward makes the bidi display
3405 sluggish, especially in small windows. */
3406 #define MAX_DISP_SCAN 250
3407
3408 /* Return the character position of a display string at or after
3409 position specified by POSITION. If no display string exists at or
3410 after POSITION, return ZV. A display string is either an overlay
3411 with `display' property whose value is a string, or a `display'
3412 text property whose value is a string. STRING is data about the
3413 string to iterate; if STRING->lstring is nil, we are iterating a
3414 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3415 on a GUI frame. DISP_PROP is set to zero if we searched
3416 MAX_DISP_SCAN characters forward without finding any display
3417 strings, non-zero otherwise. It is set to 2 if the display string
3418 uses any kind of `(space ...)' spec that will produce a stretch of
3419 white space in the text area. */
3420 ptrdiff_t
3421 compute_display_string_pos (struct text_pos *position,
3422 struct bidi_string_data *string,
3423 int frame_window_p, int *disp_prop)
3424 {
3425 /* OBJECT = nil means current buffer. */
3426 Lisp_Object object =
3427 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3428 Lisp_Object pos, spec, limpos;
3429 int string_p = (string && (STRINGP (string->lstring) || string->s));
3430 ptrdiff_t eob = string_p ? string->schars : ZV;
3431 ptrdiff_t begb = string_p ? 0 : BEGV;
3432 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3433 ptrdiff_t lim =
3434 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3435 struct text_pos tpos;
3436 int rv = 0;
3437
3438 *disp_prop = 1;
3439
3440 if (charpos >= eob
3441 /* We don't support display properties whose values are strings
3442 that have display string properties. */
3443 || string->from_disp_str
3444 /* C strings cannot have display properties. */
3445 || (string->s && !STRINGP (object)))
3446 {
3447 *disp_prop = 0;
3448 return eob;
3449 }
3450
3451 /* If the character at CHARPOS is where the display string begins,
3452 return CHARPOS. */
3453 pos = make_number (charpos);
3454 if (STRINGP (object))
3455 bufpos = string->bufpos;
3456 else
3457 bufpos = charpos;
3458 tpos = *position;
3459 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3460 && (charpos <= begb
3461 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3462 object),
3463 spec))
3464 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3465 frame_window_p)))
3466 {
3467 if (rv == 2)
3468 *disp_prop = 2;
3469 return charpos;
3470 }
3471
3472 /* Look forward for the first character with a `display' property
3473 that will replace the underlying text when displayed. */
3474 limpos = make_number (lim);
3475 do {
3476 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3477 CHARPOS (tpos) = XFASTINT (pos);
3478 if (CHARPOS (tpos) >= lim)
3479 {
3480 *disp_prop = 0;
3481 break;
3482 }
3483 if (STRINGP (object))
3484 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3485 else
3486 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3487 spec = Fget_char_property (pos, Qdisplay, object);
3488 if (!STRINGP (object))
3489 bufpos = CHARPOS (tpos);
3490 } while (NILP (spec)
3491 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3492 bufpos, frame_window_p)));
3493 if (rv == 2)
3494 *disp_prop = 2;
3495
3496 return CHARPOS (tpos);
3497 }
3498
3499 /* Return the character position of the end of the display string that
3500 started at CHARPOS. If there's no display string at CHARPOS,
3501 return -1. A display string is either an overlay with `display'
3502 property whose value is a string or a `display' text property whose
3503 value is a string. */
3504 ptrdiff_t
3505 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3506 {
3507 /* OBJECT = nil means current buffer. */
3508 Lisp_Object object =
3509 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3510 Lisp_Object pos = make_number (charpos);
3511 ptrdiff_t eob =
3512 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3513
3514 if (charpos >= eob || (string->s && !STRINGP (object)))
3515 return eob;
3516
3517 /* It could happen that the display property or overlay was removed
3518 since we found it in compute_display_string_pos above. One way
3519 this can happen is if JIT font-lock was called (through
3520 handle_fontified_prop), and jit-lock-functions remove text
3521 properties or overlays from the portion of buffer that includes
3522 CHARPOS. Muse mode is known to do that, for example. In this
3523 case, we return -1 to the caller, to signal that no display
3524 string is actually present at CHARPOS. See bidi_fetch_char for
3525 how this is handled.
3526
3527 An alternative would be to never look for display properties past
3528 it->stop_charpos. But neither compute_display_string_pos nor
3529 bidi_fetch_char that calls it know or care where the next
3530 stop_charpos is. */
3531 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3532 return -1;
3533
3534 /* Look forward for the first character where the `display' property
3535 changes. */
3536 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3537
3538 return XFASTINT (pos);
3539 }
3540
3541
3542 \f
3543 /***********************************************************************
3544 Fontification
3545 ***********************************************************************/
3546
3547 /* Handle changes in the `fontified' property of the current buffer by
3548 calling hook functions from Qfontification_functions to fontify
3549 regions of text. */
3550
3551 static enum prop_handled
3552 handle_fontified_prop (struct it *it)
3553 {
3554 Lisp_Object prop, pos;
3555 enum prop_handled handled = HANDLED_NORMALLY;
3556
3557 if (!NILP (Vmemory_full))
3558 return handled;
3559
3560 /* Get the value of the `fontified' property at IT's current buffer
3561 position. (The `fontified' property doesn't have a special
3562 meaning in strings.) If the value is nil, call functions from
3563 Qfontification_functions. */
3564 if (!STRINGP (it->string)
3565 && it->s == NULL
3566 && !NILP (Vfontification_functions)
3567 && !NILP (Vrun_hooks)
3568 && (pos = make_number (IT_CHARPOS (*it)),
3569 prop = Fget_char_property (pos, Qfontified, Qnil),
3570 /* Ignore the special cased nil value always present at EOB since
3571 no amount of fontifying will be able to change it. */
3572 NILP (prop) && IT_CHARPOS (*it) < Z))
3573 {
3574 ptrdiff_t count = SPECPDL_INDEX ();
3575 Lisp_Object val;
3576 struct buffer *obuf = current_buffer;
3577 int begv = BEGV, zv = ZV;
3578 int old_clip_changed = current_buffer->clip_changed;
3579
3580 val = Vfontification_functions;
3581 specbind (Qfontification_functions, Qnil);
3582
3583 eassert (it->end_charpos == ZV);
3584
3585 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3586 safe_call1 (val, pos);
3587 else
3588 {
3589 Lisp_Object fns, fn;
3590 struct gcpro gcpro1, gcpro2;
3591
3592 fns = Qnil;
3593 GCPRO2 (val, fns);
3594
3595 for (; CONSP (val); val = XCDR (val))
3596 {
3597 fn = XCAR (val);
3598
3599 if (EQ (fn, Qt))
3600 {
3601 /* A value of t indicates this hook has a local
3602 binding; it means to run the global binding too.
3603 In a global value, t should not occur. If it
3604 does, we must ignore it to avoid an endless
3605 loop. */
3606 for (fns = Fdefault_value (Qfontification_functions);
3607 CONSP (fns);
3608 fns = XCDR (fns))
3609 {
3610 fn = XCAR (fns);
3611 if (!EQ (fn, Qt))
3612 safe_call1 (fn, pos);
3613 }
3614 }
3615 else
3616 safe_call1 (fn, pos);
3617 }
3618
3619 UNGCPRO;
3620 }
3621
3622 unbind_to (count, Qnil);
3623
3624 /* Fontification functions routinely call `save-restriction'.
3625 Normally, this tags clip_changed, which can confuse redisplay
3626 (see discussion in Bug#6671). Since we don't perform any
3627 special handling of fontification changes in the case where
3628 `save-restriction' isn't called, there's no point doing so in
3629 this case either. So, if the buffer's restrictions are
3630 actually left unchanged, reset clip_changed. */
3631 if (obuf == current_buffer)
3632 {
3633 if (begv == BEGV && zv == ZV)
3634 current_buffer->clip_changed = old_clip_changed;
3635 }
3636 /* There isn't much we can reasonably do to protect against
3637 misbehaving fontification, but here's a fig leaf. */
3638 else if (!NILP (BVAR (obuf, name)))
3639 set_buffer_internal_1 (obuf);
3640
3641 /* The fontification code may have added/removed text.
3642 It could do even a lot worse, but let's at least protect against
3643 the most obvious case where only the text past `pos' gets changed',
3644 as is/was done in grep.el where some escapes sequences are turned
3645 into face properties (bug#7876). */
3646 it->end_charpos = ZV;
3647
3648 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3649 something. This avoids an endless loop if they failed to
3650 fontify the text for which reason ever. */
3651 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3652 handled = HANDLED_RECOMPUTE_PROPS;
3653 }
3654
3655 return handled;
3656 }
3657
3658
3659 \f
3660 /***********************************************************************
3661 Faces
3662 ***********************************************************************/
3663
3664 /* Set up iterator IT from face properties at its current position.
3665 Called from handle_stop. */
3666
3667 static enum prop_handled
3668 handle_face_prop (struct it *it)
3669 {
3670 int new_face_id;
3671 ptrdiff_t next_stop;
3672
3673 if (!STRINGP (it->string))
3674 {
3675 new_face_id
3676 = face_at_buffer_position (it->w,
3677 IT_CHARPOS (*it),
3678 it->region_beg_charpos,
3679 it->region_end_charpos,
3680 &next_stop,
3681 (IT_CHARPOS (*it)
3682 + TEXT_PROP_DISTANCE_LIMIT),
3683 0, it->base_face_id);
3684
3685 /* Is this a start of a run of characters with box face?
3686 Caveat: this can be called for a freshly initialized
3687 iterator; face_id is -1 in this case. We know that the new
3688 face will not change until limit, i.e. if the new face has a
3689 box, all characters up to limit will have one. But, as
3690 usual, we don't know whether limit is really the end. */
3691 if (new_face_id != it->face_id)
3692 {
3693 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3694
3695 /* If new face has a box but old face has not, this is
3696 the start of a run of characters with box, i.e. it has
3697 a shadow on the left side. The value of face_id of the
3698 iterator will be -1 if this is the initial call that gets
3699 the face. In this case, we have to look in front of IT's
3700 position and see whether there is a face != new_face_id. */
3701 it->start_of_box_run_p
3702 = (new_face->box != FACE_NO_BOX
3703 && (it->face_id >= 0
3704 || IT_CHARPOS (*it) == BEG
3705 || new_face_id != face_before_it_pos (it)));
3706 it->face_box_p = new_face->box != FACE_NO_BOX;
3707 }
3708 }
3709 else
3710 {
3711 int base_face_id;
3712 ptrdiff_t bufpos;
3713 int i;
3714 Lisp_Object from_overlay
3715 = (it->current.overlay_string_index >= 0
3716 ? it->string_overlays[it->current.overlay_string_index
3717 % OVERLAY_STRING_CHUNK_SIZE]
3718 : Qnil);
3719
3720 /* See if we got to this string directly or indirectly from
3721 an overlay property. That includes the before-string or
3722 after-string of an overlay, strings in display properties
3723 provided by an overlay, their text properties, etc.
3724
3725 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3726 if (! NILP (from_overlay))
3727 for (i = it->sp - 1; i >= 0; i--)
3728 {
3729 if (it->stack[i].current.overlay_string_index >= 0)
3730 from_overlay
3731 = it->string_overlays[it->stack[i].current.overlay_string_index
3732 % OVERLAY_STRING_CHUNK_SIZE];
3733 else if (! NILP (it->stack[i].from_overlay))
3734 from_overlay = it->stack[i].from_overlay;
3735
3736 if (!NILP (from_overlay))
3737 break;
3738 }
3739
3740 if (! NILP (from_overlay))
3741 {
3742 bufpos = IT_CHARPOS (*it);
3743 /* For a string from an overlay, the base face depends
3744 only on text properties and ignores overlays. */
3745 base_face_id
3746 = face_for_overlay_string (it->w,
3747 IT_CHARPOS (*it),
3748 it->region_beg_charpos,
3749 it->region_end_charpos,
3750 &next_stop,
3751 (IT_CHARPOS (*it)
3752 + TEXT_PROP_DISTANCE_LIMIT),
3753 0,
3754 from_overlay);
3755 }
3756 else
3757 {
3758 bufpos = 0;
3759
3760 /* For strings from a `display' property, use the face at
3761 IT's current buffer position as the base face to merge
3762 with, so that overlay strings appear in the same face as
3763 surrounding text, unless they specify their own
3764 faces. */
3765 base_face_id = it->string_from_prefix_prop_p
3766 ? DEFAULT_FACE_ID
3767 : underlying_face_id (it);
3768 }
3769
3770 new_face_id = face_at_string_position (it->w,
3771 it->string,
3772 IT_STRING_CHARPOS (*it),
3773 bufpos,
3774 it->region_beg_charpos,
3775 it->region_end_charpos,
3776 &next_stop,
3777 base_face_id, 0);
3778
3779 /* Is this a start of a run of characters with box? Caveat:
3780 this can be called for a freshly allocated iterator; face_id
3781 is -1 is this case. We know that the new face will not
3782 change until the next check pos, i.e. if the new face has a
3783 box, all characters up to that position will have a
3784 box. But, as usual, we don't know whether that position
3785 is really the end. */
3786 if (new_face_id != it->face_id)
3787 {
3788 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3789 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3790
3791 /* If new face has a box but old face hasn't, this is the
3792 start of a run of characters with box, i.e. it has a
3793 shadow on the left side. */
3794 it->start_of_box_run_p
3795 = new_face->box && (old_face == NULL || !old_face->box);
3796 it->face_box_p = new_face->box != FACE_NO_BOX;
3797 }
3798 }
3799
3800 it->face_id = new_face_id;
3801 return HANDLED_NORMALLY;
3802 }
3803
3804
3805 /* Return the ID of the face ``underlying'' IT's current position,
3806 which is in a string. If the iterator is associated with a
3807 buffer, return the face at IT's current buffer position.
3808 Otherwise, use the iterator's base_face_id. */
3809
3810 static int
3811 underlying_face_id (struct it *it)
3812 {
3813 int face_id = it->base_face_id, i;
3814
3815 eassert (STRINGP (it->string));
3816
3817 for (i = it->sp - 1; i >= 0; --i)
3818 if (NILP (it->stack[i].string))
3819 face_id = it->stack[i].face_id;
3820
3821 return face_id;
3822 }
3823
3824
3825 /* Compute the face one character before or after the current position
3826 of IT, in the visual order. BEFORE_P non-zero means get the face
3827 in front (to the left in L2R paragraphs, to the right in R2L
3828 paragraphs) of IT's screen position. Value is the ID of the face. */
3829
3830 static int
3831 face_before_or_after_it_pos (struct it *it, int before_p)
3832 {
3833 int face_id, limit;
3834 ptrdiff_t next_check_charpos;
3835 struct it it_copy;
3836 void *it_copy_data = NULL;
3837
3838 eassert (it->s == NULL);
3839
3840 if (STRINGP (it->string))
3841 {
3842 ptrdiff_t bufpos, charpos;
3843 int base_face_id;
3844
3845 /* No face change past the end of the string (for the case
3846 we are padding with spaces). No face change before the
3847 string start. */
3848 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3849 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3850 return it->face_id;
3851
3852 if (!it->bidi_p)
3853 {
3854 /* Set charpos to the position before or after IT's current
3855 position, in the logical order, which in the non-bidi
3856 case is the same as the visual order. */
3857 if (before_p)
3858 charpos = IT_STRING_CHARPOS (*it) - 1;
3859 else if (it->what == IT_COMPOSITION)
3860 /* For composition, we must check the character after the
3861 composition. */
3862 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3863 else
3864 charpos = IT_STRING_CHARPOS (*it) + 1;
3865 }
3866 else
3867 {
3868 if (before_p)
3869 {
3870 /* With bidi iteration, the character before the current
3871 in the visual order cannot be found by simple
3872 iteration, because "reverse" reordering is not
3873 supported. Instead, we need to use the move_it_*
3874 family of functions. */
3875 /* Ignore face changes before the first visible
3876 character on this display line. */
3877 if (it->current_x <= it->first_visible_x)
3878 return it->face_id;
3879 SAVE_IT (it_copy, *it, it_copy_data);
3880 /* Implementation note: Since move_it_in_display_line
3881 works in the iterator geometry, and thinks the first
3882 character is always the leftmost, even in R2L lines,
3883 we don't need to distinguish between the R2L and L2R
3884 cases here. */
3885 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3886 it_copy.current_x - 1, MOVE_TO_X);
3887 charpos = IT_STRING_CHARPOS (it_copy);
3888 RESTORE_IT (it, it, it_copy_data);
3889 }
3890 else
3891 {
3892 /* Set charpos to the string position of the character
3893 that comes after IT's current position in the visual
3894 order. */
3895 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3896
3897 it_copy = *it;
3898 while (n--)
3899 bidi_move_to_visually_next (&it_copy.bidi_it);
3900
3901 charpos = it_copy.bidi_it.charpos;
3902 }
3903 }
3904 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3905
3906 if (it->current.overlay_string_index >= 0)
3907 bufpos = IT_CHARPOS (*it);
3908 else
3909 bufpos = 0;
3910
3911 base_face_id = underlying_face_id (it);
3912
3913 /* Get the face for ASCII, or unibyte. */
3914 face_id = face_at_string_position (it->w,
3915 it->string,
3916 charpos,
3917 bufpos,
3918 it->region_beg_charpos,
3919 it->region_end_charpos,
3920 &next_check_charpos,
3921 base_face_id, 0);
3922
3923 /* Correct the face for charsets different from ASCII. Do it
3924 for the multibyte case only. The face returned above is
3925 suitable for unibyte text if IT->string is unibyte. */
3926 if (STRING_MULTIBYTE (it->string))
3927 {
3928 struct text_pos pos1 = string_pos (charpos, it->string);
3929 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3930 int c, len;
3931 struct face *face = FACE_FROM_ID (it->f, face_id);
3932
3933 c = string_char_and_length (p, &len);
3934 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3935 }
3936 }
3937 else
3938 {
3939 struct text_pos pos;
3940
3941 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3942 || (IT_CHARPOS (*it) <= BEGV && before_p))
3943 return it->face_id;
3944
3945 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3946 pos = it->current.pos;
3947
3948 if (!it->bidi_p)
3949 {
3950 if (before_p)
3951 DEC_TEXT_POS (pos, it->multibyte_p);
3952 else
3953 {
3954 if (it->what == IT_COMPOSITION)
3955 {
3956 /* For composition, we must check the position after
3957 the composition. */
3958 pos.charpos += it->cmp_it.nchars;
3959 pos.bytepos += it->len;
3960 }
3961 else
3962 INC_TEXT_POS (pos, it->multibyte_p);
3963 }
3964 }
3965 else
3966 {
3967 if (before_p)
3968 {
3969 /* With bidi iteration, the character before the current
3970 in the visual order cannot be found by simple
3971 iteration, because "reverse" reordering is not
3972 supported. Instead, we need to use the move_it_*
3973 family of functions. */
3974 /* Ignore face changes before the first visible
3975 character on this display line. */
3976 if (it->current_x <= it->first_visible_x)
3977 return it->face_id;
3978 SAVE_IT (it_copy, *it, it_copy_data);
3979 /* Implementation note: Since move_it_in_display_line
3980 works in the iterator geometry, and thinks the first
3981 character is always the leftmost, even in R2L lines,
3982 we don't need to distinguish between the R2L and L2R
3983 cases here. */
3984 move_it_in_display_line (&it_copy, ZV,
3985 it_copy.current_x - 1, MOVE_TO_X);
3986 pos = it_copy.current.pos;
3987 RESTORE_IT (it, it, it_copy_data);
3988 }
3989 else
3990 {
3991 /* Set charpos to the buffer position of the character
3992 that comes after IT's current position in the visual
3993 order. */
3994 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3995
3996 it_copy = *it;
3997 while (n--)
3998 bidi_move_to_visually_next (&it_copy.bidi_it);
3999
4000 SET_TEXT_POS (pos,
4001 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4002 }
4003 }
4004 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4005
4006 /* Determine face for CHARSET_ASCII, or unibyte. */
4007 face_id = face_at_buffer_position (it->w,
4008 CHARPOS (pos),
4009 it->region_beg_charpos,
4010 it->region_end_charpos,
4011 &next_check_charpos,
4012 limit, 0, -1);
4013
4014 /* Correct the face for charsets different from ASCII. Do it
4015 for the multibyte case only. The face returned above is
4016 suitable for unibyte text if current_buffer is unibyte. */
4017 if (it->multibyte_p)
4018 {
4019 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4020 struct face *face = FACE_FROM_ID (it->f, face_id);
4021 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4022 }
4023 }
4024
4025 return face_id;
4026 }
4027
4028
4029 \f
4030 /***********************************************************************
4031 Invisible text
4032 ***********************************************************************/
4033
4034 /* Set up iterator IT from invisible properties at its current
4035 position. Called from handle_stop. */
4036
4037 static enum prop_handled
4038 handle_invisible_prop (struct it *it)
4039 {
4040 enum prop_handled handled = HANDLED_NORMALLY;
4041
4042 if (STRINGP (it->string))
4043 {
4044 Lisp_Object prop, end_charpos, limit, charpos;
4045
4046 /* Get the value of the invisible text property at the
4047 current position. Value will be nil if there is no such
4048 property. */
4049 charpos = make_number (IT_STRING_CHARPOS (*it));
4050 prop = Fget_text_property (charpos, Qinvisible, it->string);
4051
4052 if (!NILP (prop)
4053 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4054 {
4055 ptrdiff_t endpos;
4056
4057 handled = HANDLED_RECOMPUTE_PROPS;
4058
4059 /* Get the position at which the next change of the
4060 invisible text property can be found in IT->string.
4061 Value will be nil if the property value is the same for
4062 all the rest of IT->string. */
4063 XSETINT (limit, SCHARS (it->string));
4064 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4065 it->string, limit);
4066
4067 /* Text at current position is invisible. The next
4068 change in the property is at position end_charpos.
4069 Move IT's current position to that position. */
4070 if (INTEGERP (end_charpos)
4071 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4072 {
4073 struct text_pos old;
4074 ptrdiff_t oldpos;
4075
4076 old = it->current.string_pos;
4077 oldpos = CHARPOS (old);
4078 if (it->bidi_p)
4079 {
4080 if (it->bidi_it.first_elt
4081 && it->bidi_it.charpos < SCHARS (it->string))
4082 bidi_paragraph_init (it->paragraph_embedding,
4083 &it->bidi_it, 1);
4084 /* Bidi-iterate out of the invisible text. */
4085 do
4086 {
4087 bidi_move_to_visually_next (&it->bidi_it);
4088 }
4089 while (oldpos <= it->bidi_it.charpos
4090 && it->bidi_it.charpos < endpos);
4091
4092 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4093 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4094 if (IT_CHARPOS (*it) >= endpos)
4095 it->prev_stop = endpos;
4096 }
4097 else
4098 {
4099 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4100 compute_string_pos (&it->current.string_pos, old, it->string);
4101 }
4102 }
4103 else
4104 {
4105 /* The rest of the string is invisible. If this is an
4106 overlay string, proceed with the next overlay string
4107 or whatever comes and return a character from there. */
4108 if (it->current.overlay_string_index >= 0)
4109 {
4110 next_overlay_string (it);
4111 /* Don't check for overlay strings when we just
4112 finished processing them. */
4113 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4114 }
4115 else
4116 {
4117 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4118 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4119 }
4120 }
4121 }
4122 }
4123 else
4124 {
4125 int invis_p;
4126 ptrdiff_t newpos, next_stop, start_charpos, tem;
4127 Lisp_Object pos, prop, overlay;
4128
4129 /* First of all, is there invisible text at this position? */
4130 tem = start_charpos = IT_CHARPOS (*it);
4131 pos = make_number (tem);
4132 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4133 &overlay);
4134 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4135
4136 /* If we are on invisible text, skip over it. */
4137 if (invis_p && start_charpos < it->end_charpos)
4138 {
4139 /* Record whether we have to display an ellipsis for the
4140 invisible text. */
4141 int display_ellipsis_p = invis_p == 2;
4142
4143 handled = HANDLED_RECOMPUTE_PROPS;
4144
4145 /* Loop skipping over invisible text. The loop is left at
4146 ZV or with IT on the first char being visible again. */
4147 do
4148 {
4149 /* Try to skip some invisible text. Return value is the
4150 position reached which can be equal to where we start
4151 if there is nothing invisible there. This skips both
4152 over invisible text properties and overlays with
4153 invisible property. */
4154 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4155
4156 /* If we skipped nothing at all we weren't at invisible
4157 text in the first place. If everything to the end of
4158 the buffer was skipped, end the loop. */
4159 if (newpos == tem || newpos >= ZV)
4160 invis_p = 0;
4161 else
4162 {
4163 /* We skipped some characters but not necessarily
4164 all there are. Check if we ended up on visible
4165 text. Fget_char_property returns the property of
4166 the char before the given position, i.e. if we
4167 get invis_p = 0, this means that the char at
4168 newpos is visible. */
4169 pos = make_number (newpos);
4170 prop = Fget_char_property (pos, Qinvisible, it->window);
4171 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4172 }
4173
4174 /* If we ended up on invisible text, proceed to
4175 skip starting with next_stop. */
4176 if (invis_p)
4177 tem = next_stop;
4178
4179 /* If there are adjacent invisible texts, don't lose the
4180 second one's ellipsis. */
4181 if (invis_p == 2)
4182 display_ellipsis_p = 1;
4183 }
4184 while (invis_p);
4185
4186 /* The position newpos is now either ZV or on visible text. */
4187 if (it->bidi_p)
4188 {
4189 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4190 int on_newline =
4191 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4192 int after_newline =
4193 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4194
4195 /* If the invisible text ends on a newline or on a
4196 character after a newline, we can avoid the costly,
4197 character by character, bidi iteration to NEWPOS, and
4198 instead simply reseat the iterator there. That's
4199 because all bidi reordering information is tossed at
4200 the newline. This is a big win for modes that hide
4201 complete lines, like Outline, Org, etc. */
4202 if (on_newline || after_newline)
4203 {
4204 struct text_pos tpos;
4205 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4206
4207 SET_TEXT_POS (tpos, newpos, bpos);
4208 reseat_1 (it, tpos, 0);
4209 /* If we reseat on a newline/ZV, we need to prep the
4210 bidi iterator for advancing to the next character
4211 after the newline/EOB, keeping the current paragraph
4212 direction (so that PRODUCE_GLYPHS does TRT wrt
4213 prepending/appending glyphs to a glyph row). */
4214 if (on_newline)
4215 {
4216 it->bidi_it.first_elt = 0;
4217 it->bidi_it.paragraph_dir = pdir;
4218 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4219 it->bidi_it.nchars = 1;
4220 it->bidi_it.ch_len = 1;
4221 }
4222 }
4223 else /* Must use the slow method. */
4224 {
4225 /* With bidi iteration, the region of invisible text
4226 could start and/or end in the middle of a
4227 non-base embedding level. Therefore, we need to
4228 skip invisible text using the bidi iterator,
4229 starting at IT's current position, until we find
4230 ourselves outside of the invisible text.
4231 Skipping invisible text _after_ bidi iteration
4232 avoids affecting the visual order of the
4233 displayed text when invisible properties are
4234 added or removed. */
4235 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4236 {
4237 /* If we were `reseat'ed to a new paragraph,
4238 determine the paragraph base direction. We
4239 need to do it now because
4240 next_element_from_buffer may not have a
4241 chance to do it, if we are going to skip any
4242 text at the beginning, which resets the
4243 FIRST_ELT flag. */
4244 bidi_paragraph_init (it->paragraph_embedding,
4245 &it->bidi_it, 1);
4246 }
4247 do
4248 {
4249 bidi_move_to_visually_next (&it->bidi_it);
4250 }
4251 while (it->stop_charpos <= it->bidi_it.charpos
4252 && it->bidi_it.charpos < newpos);
4253 IT_CHARPOS (*it) = it->bidi_it.charpos;
4254 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4255 /* If we overstepped NEWPOS, record its position in
4256 the iterator, so that we skip invisible text if
4257 later the bidi iteration lands us in the
4258 invisible region again. */
4259 if (IT_CHARPOS (*it) >= newpos)
4260 it->prev_stop = newpos;
4261 }
4262 }
4263 else
4264 {
4265 IT_CHARPOS (*it) = newpos;
4266 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4267 }
4268
4269 /* If there are before-strings at the start of invisible
4270 text, and the text is invisible because of a text
4271 property, arrange to show before-strings because 20.x did
4272 it that way. (If the text is invisible because of an
4273 overlay property instead of a text property, this is
4274 already handled in the overlay code.) */
4275 if (NILP (overlay)
4276 && get_overlay_strings (it, it->stop_charpos))
4277 {
4278 handled = HANDLED_RECOMPUTE_PROPS;
4279 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4280 }
4281 else if (display_ellipsis_p)
4282 {
4283 /* Make sure that the glyphs of the ellipsis will get
4284 correct `charpos' values. If we would not update
4285 it->position here, the glyphs would belong to the
4286 last visible character _before_ the invisible
4287 text, which confuses `set_cursor_from_row'.
4288
4289 We use the last invisible position instead of the
4290 first because this way the cursor is always drawn on
4291 the first "." of the ellipsis, whenever PT is inside
4292 the invisible text. Otherwise the cursor would be
4293 placed _after_ the ellipsis when the point is after the
4294 first invisible character. */
4295 if (!STRINGP (it->object))
4296 {
4297 it->position.charpos = newpos - 1;
4298 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4299 }
4300 it->ellipsis_p = 1;
4301 /* Let the ellipsis display before
4302 considering any properties of the following char.
4303 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4304 handled = HANDLED_RETURN;
4305 }
4306 }
4307 }
4308
4309 return handled;
4310 }
4311
4312
4313 /* Make iterator IT return `...' next.
4314 Replaces LEN characters from buffer. */
4315
4316 static void
4317 setup_for_ellipsis (struct it *it, int len)
4318 {
4319 /* Use the display table definition for `...'. Invalid glyphs
4320 will be handled by the method returning elements from dpvec. */
4321 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4322 {
4323 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4324 it->dpvec = v->contents;
4325 it->dpend = v->contents + v->header.size;
4326 }
4327 else
4328 {
4329 /* Default `...'. */
4330 it->dpvec = default_invis_vector;
4331 it->dpend = default_invis_vector + 3;
4332 }
4333
4334 it->dpvec_char_len = len;
4335 it->current.dpvec_index = 0;
4336 it->dpvec_face_id = -1;
4337
4338 /* Remember the current face id in case glyphs specify faces.
4339 IT's face is restored in set_iterator_to_next.
4340 saved_face_id was set to preceding char's face in handle_stop. */
4341 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4342 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4343
4344 it->method = GET_FROM_DISPLAY_VECTOR;
4345 it->ellipsis_p = 1;
4346 }
4347
4348
4349 \f
4350 /***********************************************************************
4351 'display' property
4352 ***********************************************************************/
4353
4354 /* Set up iterator IT from `display' property at its current position.
4355 Called from handle_stop.
4356 We return HANDLED_RETURN if some part of the display property
4357 overrides the display of the buffer text itself.
4358 Otherwise we return HANDLED_NORMALLY. */
4359
4360 static enum prop_handled
4361 handle_display_prop (struct it *it)
4362 {
4363 Lisp_Object propval, object, overlay;
4364 struct text_pos *position;
4365 ptrdiff_t bufpos;
4366 /* Nonzero if some property replaces the display of the text itself. */
4367 int display_replaced_p = 0;
4368
4369 if (STRINGP (it->string))
4370 {
4371 object = it->string;
4372 position = &it->current.string_pos;
4373 bufpos = CHARPOS (it->current.pos);
4374 }
4375 else
4376 {
4377 XSETWINDOW (object, it->w);
4378 position = &it->current.pos;
4379 bufpos = CHARPOS (*position);
4380 }
4381
4382 /* Reset those iterator values set from display property values. */
4383 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4384 it->space_width = Qnil;
4385 it->font_height = Qnil;
4386 it->voffset = 0;
4387
4388 /* We don't support recursive `display' properties, i.e. string
4389 values that have a string `display' property, that have a string
4390 `display' property etc. */
4391 if (!it->string_from_display_prop_p)
4392 it->area = TEXT_AREA;
4393
4394 propval = get_char_property_and_overlay (make_number (position->charpos),
4395 Qdisplay, object, &overlay);
4396 if (NILP (propval))
4397 return HANDLED_NORMALLY;
4398 /* Now OVERLAY is the overlay that gave us this property, or nil
4399 if it was a text property. */
4400
4401 if (!STRINGP (it->string))
4402 object = it->w->buffer;
4403
4404 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4405 position, bufpos,
4406 FRAME_WINDOW_P (it->f));
4407
4408 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4409 }
4410
4411 /* Subroutine of handle_display_prop. Returns non-zero if the display
4412 specification in SPEC is a replacing specification, i.e. it would
4413 replace the text covered by `display' property with something else,
4414 such as an image or a display string. If SPEC includes any kind or
4415 `(space ...) specification, the value is 2; this is used by
4416 compute_display_string_pos, which see.
4417
4418 See handle_single_display_spec for documentation of arguments.
4419 frame_window_p is non-zero if the window being redisplayed is on a
4420 GUI frame; this argument is used only if IT is NULL, see below.
4421
4422 IT can be NULL, if this is called by the bidi reordering code
4423 through compute_display_string_pos, which see. In that case, this
4424 function only examines SPEC, but does not otherwise "handle" it, in
4425 the sense that it doesn't set up members of IT from the display
4426 spec. */
4427 static int
4428 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4429 Lisp_Object overlay, struct text_pos *position,
4430 ptrdiff_t bufpos, int frame_window_p)
4431 {
4432 int replacing_p = 0;
4433 int rv;
4434
4435 if (CONSP (spec)
4436 /* Simple specifications. */
4437 && !EQ (XCAR (spec), Qimage)
4438 && !EQ (XCAR (spec), Qspace)
4439 && !EQ (XCAR (spec), Qwhen)
4440 && !EQ (XCAR (spec), Qslice)
4441 && !EQ (XCAR (spec), Qspace_width)
4442 && !EQ (XCAR (spec), Qheight)
4443 && !EQ (XCAR (spec), Qraise)
4444 /* Marginal area specifications. */
4445 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4446 && !EQ (XCAR (spec), Qleft_fringe)
4447 && !EQ (XCAR (spec), Qright_fringe)
4448 && !NILP (XCAR (spec)))
4449 {
4450 for (; CONSP (spec); spec = XCDR (spec))
4451 {
4452 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4453 overlay, position, bufpos,
4454 replacing_p, frame_window_p)))
4455 {
4456 replacing_p = rv;
4457 /* If some text in a string is replaced, `position' no
4458 longer points to the position of `object'. */
4459 if (!it || STRINGP (object))
4460 break;
4461 }
4462 }
4463 }
4464 else if (VECTORP (spec))
4465 {
4466 ptrdiff_t i;
4467 for (i = 0; i < ASIZE (spec); ++i)
4468 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4469 overlay, position, bufpos,
4470 replacing_p, frame_window_p)))
4471 {
4472 replacing_p = rv;
4473 /* If some text in a string is replaced, `position' no
4474 longer points to the position of `object'. */
4475 if (!it || STRINGP (object))
4476 break;
4477 }
4478 }
4479 else
4480 {
4481 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4482 position, bufpos, 0,
4483 frame_window_p)))
4484 replacing_p = rv;
4485 }
4486
4487 return replacing_p;
4488 }
4489
4490 /* Value is the position of the end of the `display' property starting
4491 at START_POS in OBJECT. */
4492
4493 static struct text_pos
4494 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4495 {
4496 Lisp_Object end;
4497 struct text_pos end_pos;
4498
4499 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4500 Qdisplay, object, Qnil);
4501 CHARPOS (end_pos) = XFASTINT (end);
4502 if (STRINGP (object))
4503 compute_string_pos (&end_pos, start_pos, it->string);
4504 else
4505 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4506
4507 return end_pos;
4508 }
4509
4510
4511 /* Set up IT from a single `display' property specification SPEC. OBJECT
4512 is the object in which the `display' property was found. *POSITION
4513 is the position in OBJECT at which the `display' property was found.
4514 BUFPOS is the buffer position of OBJECT (different from POSITION if
4515 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4516 previously saw a display specification which already replaced text
4517 display with something else, for example an image; we ignore such
4518 properties after the first one has been processed.
4519
4520 OVERLAY is the overlay this `display' property came from,
4521 or nil if it was a text property.
4522
4523 If SPEC is a `space' or `image' specification, and in some other
4524 cases too, set *POSITION to the position where the `display'
4525 property ends.
4526
4527 If IT is NULL, only examine the property specification in SPEC, but
4528 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4529 is intended to be displayed in a window on a GUI frame.
4530
4531 Value is non-zero if something was found which replaces the display
4532 of buffer or string text. */
4533
4534 static int
4535 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4536 Lisp_Object overlay, struct text_pos *position,
4537 ptrdiff_t bufpos, int display_replaced_p,
4538 int frame_window_p)
4539 {
4540 Lisp_Object form;
4541 Lisp_Object location, value;
4542 struct text_pos start_pos = *position;
4543 int valid_p;
4544
4545 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4546 If the result is non-nil, use VALUE instead of SPEC. */
4547 form = Qt;
4548 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4549 {
4550 spec = XCDR (spec);
4551 if (!CONSP (spec))
4552 return 0;
4553 form = XCAR (spec);
4554 spec = XCDR (spec);
4555 }
4556
4557 if (!NILP (form) && !EQ (form, Qt))
4558 {
4559 ptrdiff_t count = SPECPDL_INDEX ();
4560 struct gcpro gcpro1;
4561
4562 /* Bind `object' to the object having the `display' property, a
4563 buffer or string. Bind `position' to the position in the
4564 object where the property was found, and `buffer-position'
4565 to the current position in the buffer. */
4566
4567 if (NILP (object))
4568 XSETBUFFER (object, current_buffer);
4569 specbind (Qobject, object);
4570 specbind (Qposition, make_number (CHARPOS (*position)));
4571 specbind (Qbuffer_position, make_number (bufpos));
4572 GCPRO1 (form);
4573 form = safe_eval (form);
4574 UNGCPRO;
4575 unbind_to (count, Qnil);
4576 }
4577
4578 if (NILP (form))
4579 return 0;
4580
4581 /* Handle `(height HEIGHT)' specifications. */
4582 if (CONSP (spec)
4583 && EQ (XCAR (spec), Qheight)
4584 && CONSP (XCDR (spec)))
4585 {
4586 if (it)
4587 {
4588 if (!FRAME_WINDOW_P (it->f))
4589 return 0;
4590
4591 it->font_height = XCAR (XCDR (spec));
4592 if (!NILP (it->font_height))
4593 {
4594 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4595 int new_height = -1;
4596
4597 if (CONSP (it->font_height)
4598 && (EQ (XCAR (it->font_height), Qplus)
4599 || EQ (XCAR (it->font_height), Qminus))
4600 && CONSP (XCDR (it->font_height))
4601 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4602 {
4603 /* `(+ N)' or `(- N)' where N is an integer. */
4604 int steps = XINT (XCAR (XCDR (it->font_height)));
4605 if (EQ (XCAR (it->font_height), Qplus))
4606 steps = - steps;
4607 it->face_id = smaller_face (it->f, it->face_id, steps);
4608 }
4609 else if (FUNCTIONP (it->font_height))
4610 {
4611 /* Call function with current height as argument.
4612 Value is the new height. */
4613 Lisp_Object height;
4614 height = safe_call1 (it->font_height,
4615 face->lface[LFACE_HEIGHT_INDEX]);
4616 if (NUMBERP (height))
4617 new_height = XFLOATINT (height);
4618 }
4619 else if (NUMBERP (it->font_height))
4620 {
4621 /* Value is a multiple of the canonical char height. */
4622 struct face *f;
4623
4624 f = FACE_FROM_ID (it->f,
4625 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4626 new_height = (XFLOATINT (it->font_height)
4627 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4628 }
4629 else
4630 {
4631 /* Evaluate IT->font_height with `height' bound to the
4632 current specified height to get the new height. */
4633 ptrdiff_t count = SPECPDL_INDEX ();
4634
4635 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4636 value = safe_eval (it->font_height);
4637 unbind_to (count, Qnil);
4638
4639 if (NUMBERP (value))
4640 new_height = XFLOATINT (value);
4641 }
4642
4643 if (new_height > 0)
4644 it->face_id = face_with_height (it->f, it->face_id, new_height);
4645 }
4646 }
4647
4648 return 0;
4649 }
4650
4651 /* Handle `(space-width WIDTH)'. */
4652 if (CONSP (spec)
4653 && EQ (XCAR (spec), Qspace_width)
4654 && CONSP (XCDR (spec)))
4655 {
4656 if (it)
4657 {
4658 if (!FRAME_WINDOW_P (it->f))
4659 return 0;
4660
4661 value = XCAR (XCDR (spec));
4662 if (NUMBERP (value) && XFLOATINT (value) > 0)
4663 it->space_width = value;
4664 }
4665
4666 return 0;
4667 }
4668
4669 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4670 if (CONSP (spec)
4671 && EQ (XCAR (spec), Qslice))
4672 {
4673 Lisp_Object tem;
4674
4675 if (it)
4676 {
4677 if (!FRAME_WINDOW_P (it->f))
4678 return 0;
4679
4680 if (tem = XCDR (spec), CONSP (tem))
4681 {
4682 it->slice.x = XCAR (tem);
4683 if (tem = XCDR (tem), CONSP (tem))
4684 {
4685 it->slice.y = XCAR (tem);
4686 if (tem = XCDR (tem), CONSP (tem))
4687 {
4688 it->slice.width = XCAR (tem);
4689 if (tem = XCDR (tem), CONSP (tem))
4690 it->slice.height = XCAR (tem);
4691 }
4692 }
4693 }
4694 }
4695
4696 return 0;
4697 }
4698
4699 /* Handle `(raise FACTOR)'. */
4700 if (CONSP (spec)
4701 && EQ (XCAR (spec), Qraise)
4702 && CONSP (XCDR (spec)))
4703 {
4704 if (it)
4705 {
4706 if (!FRAME_WINDOW_P (it->f))
4707 return 0;
4708
4709 #ifdef HAVE_WINDOW_SYSTEM
4710 value = XCAR (XCDR (spec));
4711 if (NUMBERP (value))
4712 {
4713 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4714 it->voffset = - (XFLOATINT (value)
4715 * (FONT_HEIGHT (face->font)));
4716 }
4717 #endif /* HAVE_WINDOW_SYSTEM */
4718 }
4719
4720 return 0;
4721 }
4722
4723 /* Don't handle the other kinds of display specifications
4724 inside a string that we got from a `display' property. */
4725 if (it && it->string_from_display_prop_p)
4726 return 0;
4727
4728 /* Characters having this form of property are not displayed, so
4729 we have to find the end of the property. */
4730 if (it)
4731 {
4732 start_pos = *position;
4733 *position = display_prop_end (it, object, start_pos);
4734 }
4735 value = Qnil;
4736
4737 /* Stop the scan at that end position--we assume that all
4738 text properties change there. */
4739 if (it)
4740 it->stop_charpos = position->charpos;
4741
4742 /* Handle `(left-fringe BITMAP [FACE])'
4743 and `(right-fringe BITMAP [FACE])'. */
4744 if (CONSP (spec)
4745 && (EQ (XCAR (spec), Qleft_fringe)
4746 || EQ (XCAR (spec), Qright_fringe))
4747 && CONSP (XCDR (spec)))
4748 {
4749 int fringe_bitmap;
4750
4751 if (it)
4752 {
4753 if (!FRAME_WINDOW_P (it->f))
4754 /* If we return here, POSITION has been advanced
4755 across the text with this property. */
4756 {
4757 /* Synchronize the bidi iterator with POSITION. This is
4758 needed because we are not going to push the iterator
4759 on behalf of this display property, so there will be
4760 no pop_it call to do this synchronization for us. */
4761 if (it->bidi_p)
4762 {
4763 it->position = *position;
4764 iterate_out_of_display_property (it);
4765 *position = it->position;
4766 }
4767 return 1;
4768 }
4769 }
4770 else if (!frame_window_p)
4771 return 1;
4772
4773 #ifdef HAVE_WINDOW_SYSTEM
4774 value = XCAR (XCDR (spec));
4775 if (!SYMBOLP (value)
4776 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4777 /* If we return here, POSITION has been advanced
4778 across the text with this property. */
4779 {
4780 if (it && it->bidi_p)
4781 {
4782 it->position = *position;
4783 iterate_out_of_display_property (it);
4784 *position = it->position;
4785 }
4786 return 1;
4787 }
4788
4789 if (it)
4790 {
4791 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4792
4793 if (CONSP (XCDR (XCDR (spec))))
4794 {
4795 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4796 int face_id2 = lookup_derived_face (it->f, face_name,
4797 FRINGE_FACE_ID, 0);
4798 if (face_id2 >= 0)
4799 face_id = face_id2;
4800 }
4801
4802 /* Save current settings of IT so that we can restore them
4803 when we are finished with the glyph property value. */
4804 push_it (it, position);
4805
4806 it->area = TEXT_AREA;
4807 it->what = IT_IMAGE;
4808 it->image_id = -1; /* no image */
4809 it->position = start_pos;
4810 it->object = NILP (object) ? it->w->buffer : object;
4811 it->method = GET_FROM_IMAGE;
4812 it->from_overlay = Qnil;
4813 it->face_id = face_id;
4814 it->from_disp_prop_p = 1;
4815
4816 /* Say that we haven't consumed the characters with
4817 `display' property yet. The call to pop_it in
4818 set_iterator_to_next will clean this up. */
4819 *position = start_pos;
4820
4821 if (EQ (XCAR (spec), Qleft_fringe))
4822 {
4823 it->left_user_fringe_bitmap = fringe_bitmap;
4824 it->left_user_fringe_face_id = face_id;
4825 }
4826 else
4827 {
4828 it->right_user_fringe_bitmap = fringe_bitmap;
4829 it->right_user_fringe_face_id = face_id;
4830 }
4831 }
4832 #endif /* HAVE_WINDOW_SYSTEM */
4833 return 1;
4834 }
4835
4836 /* Prepare to handle `((margin left-margin) ...)',
4837 `((margin right-margin) ...)' and `((margin nil) ...)'
4838 prefixes for display specifications. */
4839 location = Qunbound;
4840 if (CONSP (spec) && CONSP (XCAR (spec)))
4841 {
4842 Lisp_Object tem;
4843
4844 value = XCDR (spec);
4845 if (CONSP (value))
4846 value = XCAR (value);
4847
4848 tem = XCAR (spec);
4849 if (EQ (XCAR (tem), Qmargin)
4850 && (tem = XCDR (tem),
4851 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4852 (NILP (tem)
4853 || EQ (tem, Qleft_margin)
4854 || EQ (tem, Qright_margin))))
4855 location = tem;
4856 }
4857
4858 if (EQ (location, Qunbound))
4859 {
4860 location = Qnil;
4861 value = spec;
4862 }
4863
4864 /* After this point, VALUE is the property after any
4865 margin prefix has been stripped. It must be a string,
4866 an image specification, or `(space ...)'.
4867
4868 LOCATION specifies where to display: `left-margin',
4869 `right-margin' or nil. */
4870
4871 valid_p = (STRINGP (value)
4872 #ifdef HAVE_WINDOW_SYSTEM
4873 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4874 && valid_image_p (value))
4875 #endif /* not HAVE_WINDOW_SYSTEM */
4876 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4877
4878 if (valid_p && !display_replaced_p)
4879 {
4880 int retval = 1;
4881
4882 if (!it)
4883 {
4884 /* Callers need to know whether the display spec is any kind
4885 of `(space ...)' spec that is about to affect text-area
4886 display. */
4887 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4888 retval = 2;
4889 return retval;
4890 }
4891
4892 /* Save current settings of IT so that we can restore them
4893 when we are finished with the glyph property value. */
4894 push_it (it, position);
4895 it->from_overlay = overlay;
4896 it->from_disp_prop_p = 1;
4897
4898 if (NILP (location))
4899 it->area = TEXT_AREA;
4900 else if (EQ (location, Qleft_margin))
4901 it->area = LEFT_MARGIN_AREA;
4902 else
4903 it->area = RIGHT_MARGIN_AREA;
4904
4905 if (STRINGP (value))
4906 {
4907 it->string = value;
4908 it->multibyte_p = STRING_MULTIBYTE (it->string);
4909 it->current.overlay_string_index = -1;
4910 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4911 it->end_charpos = it->string_nchars = SCHARS (it->string);
4912 it->method = GET_FROM_STRING;
4913 it->stop_charpos = 0;
4914 it->prev_stop = 0;
4915 it->base_level_stop = 0;
4916 it->string_from_display_prop_p = 1;
4917 /* Say that we haven't consumed the characters with
4918 `display' property yet. The call to pop_it in
4919 set_iterator_to_next will clean this up. */
4920 if (BUFFERP (object))
4921 *position = start_pos;
4922
4923 /* Force paragraph direction to be that of the parent
4924 object. If the parent object's paragraph direction is
4925 not yet determined, default to L2R. */
4926 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4927 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4928 else
4929 it->paragraph_embedding = L2R;
4930
4931 /* Set up the bidi iterator for this display string. */
4932 if (it->bidi_p)
4933 {
4934 it->bidi_it.string.lstring = it->string;
4935 it->bidi_it.string.s = NULL;
4936 it->bidi_it.string.schars = it->end_charpos;
4937 it->bidi_it.string.bufpos = bufpos;
4938 it->bidi_it.string.from_disp_str = 1;
4939 it->bidi_it.string.unibyte = !it->multibyte_p;
4940 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4941 }
4942 }
4943 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4944 {
4945 it->method = GET_FROM_STRETCH;
4946 it->object = value;
4947 *position = it->position = start_pos;
4948 retval = 1 + (it->area == TEXT_AREA);
4949 }
4950 #ifdef HAVE_WINDOW_SYSTEM
4951 else
4952 {
4953 it->what = IT_IMAGE;
4954 it->image_id = lookup_image (it->f, value);
4955 it->position = start_pos;
4956 it->object = NILP (object) ? it->w->buffer : object;
4957 it->method = GET_FROM_IMAGE;
4958
4959 /* Say that we haven't consumed the characters with
4960 `display' property yet. The call to pop_it in
4961 set_iterator_to_next will clean this up. */
4962 *position = start_pos;
4963 }
4964 #endif /* HAVE_WINDOW_SYSTEM */
4965
4966 return retval;
4967 }
4968
4969 /* Invalid property or property not supported. Restore
4970 POSITION to what it was before. */
4971 *position = start_pos;
4972 return 0;
4973 }
4974
4975 /* Check if PROP is a display property value whose text should be
4976 treated as intangible. OVERLAY is the overlay from which PROP
4977 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4978 specify the buffer position covered by PROP. */
4979
4980 int
4981 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4982 ptrdiff_t charpos, ptrdiff_t bytepos)
4983 {
4984 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4985 struct text_pos position;
4986
4987 SET_TEXT_POS (position, charpos, bytepos);
4988 return handle_display_spec (NULL, prop, Qnil, overlay,
4989 &position, charpos, frame_window_p);
4990 }
4991
4992
4993 /* Return 1 if PROP is a display sub-property value containing STRING.
4994
4995 Implementation note: this and the following function are really
4996 special cases of handle_display_spec and
4997 handle_single_display_spec, and should ideally use the same code.
4998 Until they do, these two pairs must be consistent and must be
4999 modified in sync. */
5000
5001 static int
5002 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5003 {
5004 if (EQ (string, prop))
5005 return 1;
5006
5007 /* Skip over `when FORM'. */
5008 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5009 {
5010 prop = XCDR (prop);
5011 if (!CONSP (prop))
5012 return 0;
5013 /* Actually, the condition following `when' should be eval'ed,
5014 like handle_single_display_spec does, and we should return
5015 zero if it evaluates to nil. However, this function is
5016 called only when the buffer was already displayed and some
5017 glyph in the glyph matrix was found to come from a display
5018 string. Therefore, the condition was already evaluated, and
5019 the result was non-nil, otherwise the display string wouldn't
5020 have been displayed and we would have never been called for
5021 this property. Thus, we can skip the evaluation and assume
5022 its result is non-nil. */
5023 prop = XCDR (prop);
5024 }
5025
5026 if (CONSP (prop))
5027 /* Skip over `margin LOCATION'. */
5028 if (EQ (XCAR (prop), Qmargin))
5029 {
5030 prop = XCDR (prop);
5031 if (!CONSP (prop))
5032 return 0;
5033
5034 prop = XCDR (prop);
5035 if (!CONSP (prop))
5036 return 0;
5037 }
5038
5039 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5040 }
5041
5042
5043 /* Return 1 if STRING appears in the `display' property PROP. */
5044
5045 static int
5046 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5047 {
5048 if (CONSP (prop)
5049 && !EQ (XCAR (prop), Qwhen)
5050 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5051 {
5052 /* A list of sub-properties. */
5053 while (CONSP (prop))
5054 {
5055 if (single_display_spec_string_p (XCAR (prop), string))
5056 return 1;
5057 prop = XCDR (prop);
5058 }
5059 }
5060 else if (VECTORP (prop))
5061 {
5062 /* A vector of sub-properties. */
5063 ptrdiff_t i;
5064 for (i = 0; i < ASIZE (prop); ++i)
5065 if (single_display_spec_string_p (AREF (prop, i), string))
5066 return 1;
5067 }
5068 else
5069 return single_display_spec_string_p (prop, string);
5070
5071 return 0;
5072 }
5073
5074 /* Look for STRING in overlays and text properties in the current
5075 buffer, between character positions FROM and TO (excluding TO).
5076 BACK_P non-zero means look back (in this case, TO is supposed to be
5077 less than FROM).
5078 Value is the first character position where STRING was found, or
5079 zero if it wasn't found before hitting TO.
5080
5081 This function may only use code that doesn't eval because it is
5082 called asynchronously from note_mouse_highlight. */
5083
5084 static ptrdiff_t
5085 string_buffer_position_lim (Lisp_Object string,
5086 ptrdiff_t from, ptrdiff_t to, int back_p)
5087 {
5088 Lisp_Object limit, prop, pos;
5089 int found = 0;
5090
5091 pos = make_number (max (from, BEGV));
5092
5093 if (!back_p) /* looking forward */
5094 {
5095 limit = make_number (min (to, ZV));
5096 while (!found && !EQ (pos, limit))
5097 {
5098 prop = Fget_char_property (pos, Qdisplay, Qnil);
5099 if (!NILP (prop) && display_prop_string_p (prop, string))
5100 found = 1;
5101 else
5102 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5103 limit);
5104 }
5105 }
5106 else /* looking back */
5107 {
5108 limit = make_number (max (to, BEGV));
5109 while (!found && !EQ (pos, limit))
5110 {
5111 prop = Fget_char_property (pos, Qdisplay, Qnil);
5112 if (!NILP (prop) && display_prop_string_p (prop, string))
5113 found = 1;
5114 else
5115 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5116 limit);
5117 }
5118 }
5119
5120 return found ? XINT (pos) : 0;
5121 }
5122
5123 /* Determine which buffer position in current buffer STRING comes from.
5124 AROUND_CHARPOS is an approximate position where it could come from.
5125 Value is the buffer position or 0 if it couldn't be determined.
5126
5127 This function is necessary because we don't record buffer positions
5128 in glyphs generated from strings (to keep struct glyph small).
5129 This function may only use code that doesn't eval because it is
5130 called asynchronously from note_mouse_highlight. */
5131
5132 static ptrdiff_t
5133 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5134 {
5135 const int MAX_DISTANCE = 1000;
5136 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5137 around_charpos + MAX_DISTANCE,
5138 0);
5139
5140 if (!found)
5141 found = string_buffer_position_lim (string, around_charpos,
5142 around_charpos - MAX_DISTANCE, 1);
5143 return found;
5144 }
5145
5146
5147 \f
5148 /***********************************************************************
5149 `composition' property
5150 ***********************************************************************/
5151
5152 /* Set up iterator IT from `composition' property at its current
5153 position. Called from handle_stop. */
5154
5155 static enum prop_handled
5156 handle_composition_prop (struct it *it)
5157 {
5158 Lisp_Object prop, string;
5159 ptrdiff_t pos, pos_byte, start, end;
5160
5161 if (STRINGP (it->string))
5162 {
5163 unsigned char *s;
5164
5165 pos = IT_STRING_CHARPOS (*it);
5166 pos_byte = IT_STRING_BYTEPOS (*it);
5167 string = it->string;
5168 s = SDATA (string) + pos_byte;
5169 it->c = STRING_CHAR (s);
5170 }
5171 else
5172 {
5173 pos = IT_CHARPOS (*it);
5174 pos_byte = IT_BYTEPOS (*it);
5175 string = Qnil;
5176 it->c = FETCH_CHAR (pos_byte);
5177 }
5178
5179 /* If there's a valid composition and point is not inside of the
5180 composition (in the case that the composition is from the current
5181 buffer), draw a glyph composed from the composition components. */
5182 if (find_composition (pos, -1, &start, &end, &prop, string)
5183 && COMPOSITION_VALID_P (start, end, prop)
5184 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5185 {
5186 if (start < pos)
5187 /* As we can't handle this situation (perhaps font-lock added
5188 a new composition), we just return here hoping that next
5189 redisplay will detect this composition much earlier. */
5190 return HANDLED_NORMALLY;
5191 if (start != pos)
5192 {
5193 if (STRINGP (it->string))
5194 pos_byte = string_char_to_byte (it->string, start);
5195 else
5196 pos_byte = CHAR_TO_BYTE (start);
5197 }
5198 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5199 prop, string);
5200
5201 if (it->cmp_it.id >= 0)
5202 {
5203 it->cmp_it.ch = -1;
5204 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5205 it->cmp_it.nglyphs = -1;
5206 }
5207 }
5208
5209 return HANDLED_NORMALLY;
5210 }
5211
5212
5213 \f
5214 /***********************************************************************
5215 Overlay strings
5216 ***********************************************************************/
5217
5218 /* The following structure is used to record overlay strings for
5219 later sorting in load_overlay_strings. */
5220
5221 struct overlay_entry
5222 {
5223 Lisp_Object overlay;
5224 Lisp_Object string;
5225 EMACS_INT priority;
5226 int after_string_p;
5227 };
5228
5229
5230 /* Set up iterator IT from overlay strings at its current position.
5231 Called from handle_stop. */
5232
5233 static enum prop_handled
5234 handle_overlay_change (struct it *it)
5235 {
5236 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5237 return HANDLED_RECOMPUTE_PROPS;
5238 else
5239 return HANDLED_NORMALLY;
5240 }
5241
5242
5243 /* Set up the next overlay string for delivery by IT, if there is an
5244 overlay string to deliver. Called by set_iterator_to_next when the
5245 end of the current overlay string is reached. If there are more
5246 overlay strings to display, IT->string and
5247 IT->current.overlay_string_index are set appropriately here.
5248 Otherwise IT->string is set to nil. */
5249
5250 static void
5251 next_overlay_string (struct it *it)
5252 {
5253 ++it->current.overlay_string_index;
5254 if (it->current.overlay_string_index == it->n_overlay_strings)
5255 {
5256 /* No more overlay strings. Restore IT's settings to what
5257 they were before overlay strings were processed, and
5258 continue to deliver from current_buffer. */
5259
5260 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5261 pop_it (it);
5262 eassert (it->sp > 0
5263 || (NILP (it->string)
5264 && it->method == GET_FROM_BUFFER
5265 && it->stop_charpos >= BEGV
5266 && it->stop_charpos <= it->end_charpos));
5267 it->current.overlay_string_index = -1;
5268 it->n_overlay_strings = 0;
5269 it->overlay_strings_charpos = -1;
5270 /* If there's an empty display string on the stack, pop the
5271 stack, to resync the bidi iterator with IT's position. Such
5272 empty strings are pushed onto the stack in
5273 get_overlay_strings_1. */
5274 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5275 pop_it (it);
5276
5277 /* If we're at the end of the buffer, record that we have
5278 processed the overlay strings there already, so that
5279 next_element_from_buffer doesn't try it again. */
5280 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5281 it->overlay_strings_at_end_processed_p = 1;
5282 }
5283 else
5284 {
5285 /* There are more overlay strings to process. If
5286 IT->current.overlay_string_index has advanced to a position
5287 where we must load IT->overlay_strings with more strings, do
5288 it. We must load at the IT->overlay_strings_charpos where
5289 IT->n_overlay_strings was originally computed; when invisible
5290 text is present, this might not be IT_CHARPOS (Bug#7016). */
5291 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5292
5293 if (it->current.overlay_string_index && i == 0)
5294 load_overlay_strings (it, it->overlay_strings_charpos);
5295
5296 /* Initialize IT to deliver display elements from the overlay
5297 string. */
5298 it->string = it->overlay_strings[i];
5299 it->multibyte_p = STRING_MULTIBYTE (it->string);
5300 SET_TEXT_POS (it->current.string_pos, 0, 0);
5301 it->method = GET_FROM_STRING;
5302 it->stop_charpos = 0;
5303 if (it->cmp_it.stop_pos >= 0)
5304 it->cmp_it.stop_pos = 0;
5305 it->prev_stop = 0;
5306 it->base_level_stop = 0;
5307
5308 /* Set up the bidi iterator for this overlay string. */
5309 if (it->bidi_p)
5310 {
5311 it->bidi_it.string.lstring = it->string;
5312 it->bidi_it.string.s = NULL;
5313 it->bidi_it.string.schars = SCHARS (it->string);
5314 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5315 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5316 it->bidi_it.string.unibyte = !it->multibyte_p;
5317 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5318 }
5319 }
5320
5321 CHECK_IT (it);
5322 }
5323
5324
5325 /* Compare two overlay_entry structures E1 and E2. Used as a
5326 comparison function for qsort in load_overlay_strings. Overlay
5327 strings for the same position are sorted so that
5328
5329 1. All after-strings come in front of before-strings, except
5330 when they come from the same overlay.
5331
5332 2. Within after-strings, strings are sorted so that overlay strings
5333 from overlays with higher priorities come first.
5334
5335 2. Within before-strings, strings are sorted so that overlay
5336 strings from overlays with higher priorities come last.
5337
5338 Value is analogous to strcmp. */
5339
5340
5341 static int
5342 compare_overlay_entries (const void *e1, const void *e2)
5343 {
5344 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5345 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5346 int result;
5347
5348 if (entry1->after_string_p != entry2->after_string_p)
5349 {
5350 /* Let after-strings appear in front of before-strings if
5351 they come from different overlays. */
5352 if (EQ (entry1->overlay, entry2->overlay))
5353 result = entry1->after_string_p ? 1 : -1;
5354 else
5355 result = entry1->after_string_p ? -1 : 1;
5356 }
5357 else if (entry1->priority != entry2->priority)
5358 {
5359 if (entry1->after_string_p)
5360 /* After-strings sorted in order of decreasing priority. */
5361 result = entry2->priority < entry1->priority ? -1 : 1;
5362 else
5363 /* Before-strings sorted in order of increasing priority. */
5364 result = entry1->priority < entry2->priority ? -1 : 1;
5365 }
5366 else
5367 result = 0;
5368
5369 return result;
5370 }
5371
5372
5373 /* Load the vector IT->overlay_strings with overlay strings from IT's
5374 current buffer position, or from CHARPOS if that is > 0. Set
5375 IT->n_overlays to the total number of overlay strings found.
5376
5377 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5378 a time. On entry into load_overlay_strings,
5379 IT->current.overlay_string_index gives the number of overlay
5380 strings that have already been loaded by previous calls to this
5381 function.
5382
5383 IT->add_overlay_start contains an additional overlay start
5384 position to consider for taking overlay strings from, if non-zero.
5385 This position comes into play when the overlay has an `invisible'
5386 property, and both before and after-strings. When we've skipped to
5387 the end of the overlay, because of its `invisible' property, we
5388 nevertheless want its before-string to appear.
5389 IT->add_overlay_start will contain the overlay start position
5390 in this case.
5391
5392 Overlay strings are sorted so that after-string strings come in
5393 front of before-string strings. Within before and after-strings,
5394 strings are sorted by overlay priority. See also function
5395 compare_overlay_entries. */
5396
5397 static void
5398 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5399 {
5400 Lisp_Object overlay, window, str, invisible;
5401 struct Lisp_Overlay *ov;
5402 ptrdiff_t start, end;
5403 ptrdiff_t size = 20;
5404 ptrdiff_t n = 0, i, j;
5405 int invis_p;
5406 struct overlay_entry *entries = alloca (size * sizeof *entries);
5407 USE_SAFE_ALLOCA;
5408
5409 if (charpos <= 0)
5410 charpos = IT_CHARPOS (*it);
5411
5412 /* Append the overlay string STRING of overlay OVERLAY to vector
5413 `entries' which has size `size' and currently contains `n'
5414 elements. AFTER_P non-zero means STRING is an after-string of
5415 OVERLAY. */
5416 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5417 do \
5418 { \
5419 Lisp_Object priority; \
5420 \
5421 if (n == size) \
5422 { \
5423 struct overlay_entry *old = entries; \
5424 SAFE_NALLOCA (entries, 2, size); \
5425 memcpy (entries, old, size * sizeof *entries); \
5426 size *= 2; \
5427 } \
5428 \
5429 entries[n].string = (STRING); \
5430 entries[n].overlay = (OVERLAY); \
5431 priority = Foverlay_get ((OVERLAY), Qpriority); \
5432 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5433 entries[n].after_string_p = (AFTER_P); \
5434 ++n; \
5435 } \
5436 while (0)
5437
5438 /* Process overlay before the overlay center. */
5439 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5440 {
5441 XSETMISC (overlay, ov);
5442 eassert (OVERLAYP (overlay));
5443 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5444 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5445
5446 if (end < charpos)
5447 break;
5448
5449 /* Skip this overlay if it doesn't start or end at IT's current
5450 position. */
5451 if (end != charpos && start != charpos)
5452 continue;
5453
5454 /* Skip this overlay if it doesn't apply to IT->w. */
5455 window = Foverlay_get (overlay, Qwindow);
5456 if (WINDOWP (window) && XWINDOW (window) != it->w)
5457 continue;
5458
5459 /* If the text ``under'' the overlay is invisible, both before-
5460 and after-strings from this overlay are visible; start and
5461 end position are indistinguishable. */
5462 invisible = Foverlay_get (overlay, Qinvisible);
5463 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5464
5465 /* If overlay has a non-empty before-string, record it. */
5466 if ((start == charpos || (end == charpos && invis_p))
5467 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5468 && SCHARS (str))
5469 RECORD_OVERLAY_STRING (overlay, str, 0);
5470
5471 /* If overlay has a non-empty after-string, record it. */
5472 if ((end == charpos || (start == charpos && invis_p))
5473 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5474 && SCHARS (str))
5475 RECORD_OVERLAY_STRING (overlay, str, 1);
5476 }
5477
5478 /* Process overlays after the overlay center. */
5479 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5480 {
5481 XSETMISC (overlay, ov);
5482 eassert (OVERLAYP (overlay));
5483 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5484 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5485
5486 if (start > charpos)
5487 break;
5488
5489 /* Skip this overlay if it doesn't start or end at IT's current
5490 position. */
5491 if (end != charpos && start != charpos)
5492 continue;
5493
5494 /* Skip this overlay if it doesn't apply to IT->w. */
5495 window = Foverlay_get (overlay, Qwindow);
5496 if (WINDOWP (window) && XWINDOW (window) != it->w)
5497 continue;
5498
5499 /* If the text ``under'' the overlay is invisible, it has a zero
5500 dimension, and both before- and after-strings apply. */
5501 invisible = Foverlay_get (overlay, Qinvisible);
5502 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5503
5504 /* If overlay has a non-empty before-string, record it. */
5505 if ((start == charpos || (end == charpos && invis_p))
5506 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5507 && SCHARS (str))
5508 RECORD_OVERLAY_STRING (overlay, str, 0);
5509
5510 /* If overlay has a non-empty after-string, record it. */
5511 if ((end == charpos || (start == charpos && invis_p))
5512 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5513 && SCHARS (str))
5514 RECORD_OVERLAY_STRING (overlay, str, 1);
5515 }
5516
5517 #undef RECORD_OVERLAY_STRING
5518
5519 /* Sort entries. */
5520 if (n > 1)
5521 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5522
5523 /* Record number of overlay strings, and where we computed it. */
5524 it->n_overlay_strings = n;
5525 it->overlay_strings_charpos = charpos;
5526
5527 /* IT->current.overlay_string_index is the number of overlay strings
5528 that have already been consumed by IT. Copy some of the
5529 remaining overlay strings to IT->overlay_strings. */
5530 i = 0;
5531 j = it->current.overlay_string_index;
5532 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5533 {
5534 it->overlay_strings[i] = entries[j].string;
5535 it->string_overlays[i++] = entries[j++].overlay;
5536 }
5537
5538 CHECK_IT (it);
5539 SAFE_FREE ();
5540 }
5541
5542
5543 /* Get the first chunk of overlay strings at IT's current buffer
5544 position, or at CHARPOS if that is > 0. Value is non-zero if at
5545 least one overlay string was found. */
5546
5547 static int
5548 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5549 {
5550 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5551 process. This fills IT->overlay_strings with strings, and sets
5552 IT->n_overlay_strings to the total number of strings to process.
5553 IT->pos.overlay_string_index has to be set temporarily to zero
5554 because load_overlay_strings needs this; it must be set to -1
5555 when no overlay strings are found because a zero value would
5556 indicate a position in the first overlay string. */
5557 it->current.overlay_string_index = 0;
5558 load_overlay_strings (it, charpos);
5559
5560 /* If we found overlay strings, set up IT to deliver display
5561 elements from the first one. Otherwise set up IT to deliver
5562 from current_buffer. */
5563 if (it->n_overlay_strings)
5564 {
5565 /* Make sure we know settings in current_buffer, so that we can
5566 restore meaningful values when we're done with the overlay
5567 strings. */
5568 if (compute_stop_p)
5569 compute_stop_pos (it);
5570 eassert (it->face_id >= 0);
5571
5572 /* Save IT's settings. They are restored after all overlay
5573 strings have been processed. */
5574 eassert (!compute_stop_p || it->sp == 0);
5575
5576 /* When called from handle_stop, there might be an empty display
5577 string loaded. In that case, don't bother saving it. But
5578 don't use this optimization with the bidi iterator, since we
5579 need the corresponding pop_it call to resync the bidi
5580 iterator's position with IT's position, after we are done
5581 with the overlay strings. (The corresponding call to pop_it
5582 in case of an empty display string is in
5583 next_overlay_string.) */
5584 if (!(!it->bidi_p
5585 && STRINGP (it->string) && !SCHARS (it->string)))
5586 push_it (it, NULL);
5587
5588 /* Set up IT to deliver display elements from the first overlay
5589 string. */
5590 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5591 it->string = it->overlay_strings[0];
5592 it->from_overlay = Qnil;
5593 it->stop_charpos = 0;
5594 eassert (STRINGP (it->string));
5595 it->end_charpos = SCHARS (it->string);
5596 it->prev_stop = 0;
5597 it->base_level_stop = 0;
5598 it->multibyte_p = STRING_MULTIBYTE (it->string);
5599 it->method = GET_FROM_STRING;
5600 it->from_disp_prop_p = 0;
5601
5602 /* Force paragraph direction to be that of the parent
5603 buffer. */
5604 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5605 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5606 else
5607 it->paragraph_embedding = L2R;
5608
5609 /* Set up the bidi iterator for this overlay string. */
5610 if (it->bidi_p)
5611 {
5612 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5613
5614 it->bidi_it.string.lstring = it->string;
5615 it->bidi_it.string.s = NULL;
5616 it->bidi_it.string.schars = SCHARS (it->string);
5617 it->bidi_it.string.bufpos = pos;
5618 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5619 it->bidi_it.string.unibyte = !it->multibyte_p;
5620 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5621 }
5622 return 1;
5623 }
5624
5625 it->current.overlay_string_index = -1;
5626 return 0;
5627 }
5628
5629 static int
5630 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5631 {
5632 it->string = Qnil;
5633 it->method = GET_FROM_BUFFER;
5634
5635 (void) get_overlay_strings_1 (it, charpos, 1);
5636
5637 CHECK_IT (it);
5638
5639 /* Value is non-zero if we found at least one overlay string. */
5640 return STRINGP (it->string);
5641 }
5642
5643
5644 \f
5645 /***********************************************************************
5646 Saving and restoring state
5647 ***********************************************************************/
5648
5649 /* Save current settings of IT on IT->stack. Called, for example,
5650 before setting up IT for an overlay string, to be able to restore
5651 IT's settings to what they were after the overlay string has been
5652 processed. If POSITION is non-NULL, it is the position to save on
5653 the stack instead of IT->position. */
5654
5655 static void
5656 push_it (struct it *it, struct text_pos *position)
5657 {
5658 struct iterator_stack_entry *p;
5659
5660 eassert (it->sp < IT_STACK_SIZE);
5661 p = it->stack + it->sp;
5662
5663 p->stop_charpos = it->stop_charpos;
5664 p->prev_stop = it->prev_stop;
5665 p->base_level_stop = it->base_level_stop;
5666 p->cmp_it = it->cmp_it;
5667 eassert (it->face_id >= 0);
5668 p->face_id = it->face_id;
5669 p->string = it->string;
5670 p->method = it->method;
5671 p->from_overlay = it->from_overlay;
5672 switch (p->method)
5673 {
5674 case GET_FROM_IMAGE:
5675 p->u.image.object = it->object;
5676 p->u.image.image_id = it->image_id;
5677 p->u.image.slice = it->slice;
5678 break;
5679 case GET_FROM_STRETCH:
5680 p->u.stretch.object = it->object;
5681 break;
5682 }
5683 p->position = position ? *position : it->position;
5684 p->current = it->current;
5685 p->end_charpos = it->end_charpos;
5686 p->string_nchars = it->string_nchars;
5687 p->area = it->area;
5688 p->multibyte_p = it->multibyte_p;
5689 p->avoid_cursor_p = it->avoid_cursor_p;
5690 p->space_width = it->space_width;
5691 p->font_height = it->font_height;
5692 p->voffset = it->voffset;
5693 p->string_from_display_prop_p = it->string_from_display_prop_p;
5694 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5695 p->display_ellipsis_p = 0;
5696 p->line_wrap = it->line_wrap;
5697 p->bidi_p = it->bidi_p;
5698 p->paragraph_embedding = it->paragraph_embedding;
5699 p->from_disp_prop_p = it->from_disp_prop_p;
5700 ++it->sp;
5701
5702 /* Save the state of the bidi iterator as well. */
5703 if (it->bidi_p)
5704 bidi_push_it (&it->bidi_it);
5705 }
5706
5707 static void
5708 iterate_out_of_display_property (struct it *it)
5709 {
5710 int buffer_p = !STRINGP (it->string);
5711 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5712 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5713
5714 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5715
5716 /* Maybe initialize paragraph direction. If we are at the beginning
5717 of a new paragraph, next_element_from_buffer may not have a
5718 chance to do that. */
5719 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5720 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5721 /* prev_stop can be zero, so check against BEGV as well. */
5722 while (it->bidi_it.charpos >= bob
5723 && it->prev_stop <= it->bidi_it.charpos
5724 && it->bidi_it.charpos < CHARPOS (it->position)
5725 && it->bidi_it.charpos < eob)
5726 bidi_move_to_visually_next (&it->bidi_it);
5727 /* Record the stop_pos we just crossed, for when we cross it
5728 back, maybe. */
5729 if (it->bidi_it.charpos > CHARPOS (it->position))
5730 it->prev_stop = CHARPOS (it->position);
5731 /* If we ended up not where pop_it put us, resync IT's
5732 positional members with the bidi iterator. */
5733 if (it->bidi_it.charpos != CHARPOS (it->position))
5734 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5735 if (buffer_p)
5736 it->current.pos = it->position;
5737 else
5738 it->current.string_pos = it->position;
5739 }
5740
5741 /* Restore IT's settings from IT->stack. Called, for example, when no
5742 more overlay strings must be processed, and we return to delivering
5743 display elements from a buffer, or when the end of a string from a
5744 `display' property is reached and we return to delivering display
5745 elements from an overlay string, or from a buffer. */
5746
5747 static void
5748 pop_it (struct it *it)
5749 {
5750 struct iterator_stack_entry *p;
5751 int from_display_prop = it->from_disp_prop_p;
5752
5753 eassert (it->sp > 0);
5754 --it->sp;
5755 p = it->stack + it->sp;
5756 it->stop_charpos = p->stop_charpos;
5757 it->prev_stop = p->prev_stop;
5758 it->base_level_stop = p->base_level_stop;
5759 it->cmp_it = p->cmp_it;
5760 it->face_id = p->face_id;
5761 it->current = p->current;
5762 it->position = p->position;
5763 it->string = p->string;
5764 it->from_overlay = p->from_overlay;
5765 if (NILP (it->string))
5766 SET_TEXT_POS (it->current.string_pos, -1, -1);
5767 it->method = p->method;
5768 switch (it->method)
5769 {
5770 case GET_FROM_IMAGE:
5771 it->image_id = p->u.image.image_id;
5772 it->object = p->u.image.object;
5773 it->slice = p->u.image.slice;
5774 break;
5775 case GET_FROM_STRETCH:
5776 it->object = p->u.stretch.object;
5777 break;
5778 case GET_FROM_BUFFER:
5779 it->object = it->w->buffer;
5780 break;
5781 case GET_FROM_STRING:
5782 it->object = it->string;
5783 break;
5784 case GET_FROM_DISPLAY_VECTOR:
5785 if (it->s)
5786 it->method = GET_FROM_C_STRING;
5787 else if (STRINGP (it->string))
5788 it->method = GET_FROM_STRING;
5789 else
5790 {
5791 it->method = GET_FROM_BUFFER;
5792 it->object = it->w->buffer;
5793 }
5794 }
5795 it->end_charpos = p->end_charpos;
5796 it->string_nchars = p->string_nchars;
5797 it->area = p->area;
5798 it->multibyte_p = p->multibyte_p;
5799 it->avoid_cursor_p = p->avoid_cursor_p;
5800 it->space_width = p->space_width;
5801 it->font_height = p->font_height;
5802 it->voffset = p->voffset;
5803 it->string_from_display_prop_p = p->string_from_display_prop_p;
5804 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5805 it->line_wrap = p->line_wrap;
5806 it->bidi_p = p->bidi_p;
5807 it->paragraph_embedding = p->paragraph_embedding;
5808 it->from_disp_prop_p = p->from_disp_prop_p;
5809 if (it->bidi_p)
5810 {
5811 bidi_pop_it (&it->bidi_it);
5812 /* Bidi-iterate until we get out of the portion of text, if any,
5813 covered by a `display' text property or by an overlay with
5814 `display' property. (We cannot just jump there, because the
5815 internal coherency of the bidi iterator state can not be
5816 preserved across such jumps.) We also must determine the
5817 paragraph base direction if the overlay we just processed is
5818 at the beginning of a new paragraph. */
5819 if (from_display_prop
5820 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5821 iterate_out_of_display_property (it);
5822
5823 eassert ((BUFFERP (it->object)
5824 && IT_CHARPOS (*it) == it->bidi_it.charpos
5825 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5826 || (STRINGP (it->object)
5827 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5828 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5829 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5830 }
5831 }
5832
5833
5834 \f
5835 /***********************************************************************
5836 Moving over lines
5837 ***********************************************************************/
5838
5839 /* Set IT's current position to the previous line start. */
5840
5841 static void
5842 back_to_previous_line_start (struct it *it)
5843 {
5844 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5845 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5846 }
5847
5848
5849 /* Move IT to the next line start.
5850
5851 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5852 we skipped over part of the text (as opposed to moving the iterator
5853 continuously over the text). Otherwise, don't change the value
5854 of *SKIPPED_P.
5855
5856 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5857 iterator on the newline, if it was found.
5858
5859 Newlines may come from buffer text, overlay strings, or strings
5860 displayed via the `display' property. That's the reason we can't
5861 simply use find_next_newline_no_quit.
5862
5863 Note that this function may not skip over invisible text that is so
5864 because of text properties and immediately follows a newline. If
5865 it would, function reseat_at_next_visible_line_start, when called
5866 from set_iterator_to_next, would effectively make invisible
5867 characters following a newline part of the wrong glyph row, which
5868 leads to wrong cursor motion. */
5869
5870 static int
5871 forward_to_next_line_start (struct it *it, int *skipped_p,
5872 struct bidi_it *bidi_it_prev)
5873 {
5874 ptrdiff_t old_selective;
5875 int newline_found_p, n;
5876 const int MAX_NEWLINE_DISTANCE = 500;
5877
5878 /* If already on a newline, just consume it to avoid unintended
5879 skipping over invisible text below. */
5880 if (it->what == IT_CHARACTER
5881 && it->c == '\n'
5882 && CHARPOS (it->position) == IT_CHARPOS (*it))
5883 {
5884 if (it->bidi_p && bidi_it_prev)
5885 *bidi_it_prev = it->bidi_it;
5886 set_iterator_to_next (it, 0);
5887 it->c = 0;
5888 return 1;
5889 }
5890
5891 /* Don't handle selective display in the following. It's (a)
5892 unnecessary because it's done by the caller, and (b) leads to an
5893 infinite recursion because next_element_from_ellipsis indirectly
5894 calls this function. */
5895 old_selective = it->selective;
5896 it->selective = 0;
5897
5898 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5899 from buffer text. */
5900 for (n = newline_found_p = 0;
5901 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5902 n += STRINGP (it->string) ? 0 : 1)
5903 {
5904 if (!get_next_display_element (it))
5905 return 0;
5906 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5907 if (newline_found_p && it->bidi_p && bidi_it_prev)
5908 *bidi_it_prev = it->bidi_it;
5909 set_iterator_to_next (it, 0);
5910 }
5911
5912 /* If we didn't find a newline near enough, see if we can use a
5913 short-cut. */
5914 if (!newline_found_p)
5915 {
5916 ptrdiff_t start = IT_CHARPOS (*it);
5917 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5918 Lisp_Object pos;
5919
5920 eassert (!STRINGP (it->string));
5921
5922 /* If there isn't any `display' property in sight, and no
5923 overlays, we can just use the position of the newline in
5924 buffer text. */
5925 if (it->stop_charpos >= limit
5926 || ((pos = Fnext_single_property_change (make_number (start),
5927 Qdisplay, Qnil,
5928 make_number (limit)),
5929 NILP (pos))
5930 && next_overlay_change (start) == ZV))
5931 {
5932 if (!it->bidi_p)
5933 {
5934 IT_CHARPOS (*it) = limit;
5935 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5936 }
5937 else
5938 {
5939 struct bidi_it bprev;
5940
5941 /* Help bidi.c avoid expensive searches for display
5942 properties and overlays, by telling it that there are
5943 none up to `limit'. */
5944 if (it->bidi_it.disp_pos < limit)
5945 {
5946 it->bidi_it.disp_pos = limit;
5947 it->bidi_it.disp_prop = 0;
5948 }
5949 do {
5950 bprev = it->bidi_it;
5951 bidi_move_to_visually_next (&it->bidi_it);
5952 } while (it->bidi_it.charpos != limit);
5953 IT_CHARPOS (*it) = limit;
5954 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5955 if (bidi_it_prev)
5956 *bidi_it_prev = bprev;
5957 }
5958 *skipped_p = newline_found_p = 1;
5959 }
5960 else
5961 {
5962 while (get_next_display_element (it)
5963 && !newline_found_p)
5964 {
5965 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5966 if (newline_found_p && it->bidi_p && bidi_it_prev)
5967 *bidi_it_prev = it->bidi_it;
5968 set_iterator_to_next (it, 0);
5969 }
5970 }
5971 }
5972
5973 it->selective = old_selective;
5974 return newline_found_p;
5975 }
5976
5977
5978 /* Set IT's current position to the previous visible line start. Skip
5979 invisible text that is so either due to text properties or due to
5980 selective display. Caution: this does not change IT->current_x and
5981 IT->hpos. */
5982
5983 static void
5984 back_to_previous_visible_line_start (struct it *it)
5985 {
5986 while (IT_CHARPOS (*it) > BEGV)
5987 {
5988 back_to_previous_line_start (it);
5989
5990 if (IT_CHARPOS (*it) <= BEGV)
5991 break;
5992
5993 /* If selective > 0, then lines indented more than its value are
5994 invisible. */
5995 if (it->selective > 0
5996 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5997 it->selective))
5998 continue;
5999
6000 /* Check the newline before point for invisibility. */
6001 {
6002 Lisp_Object prop;
6003 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6004 Qinvisible, it->window);
6005 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6006 continue;
6007 }
6008
6009 if (IT_CHARPOS (*it) <= BEGV)
6010 break;
6011
6012 {
6013 struct it it2;
6014 void *it2data = NULL;
6015 ptrdiff_t pos;
6016 ptrdiff_t beg, end;
6017 Lisp_Object val, overlay;
6018
6019 SAVE_IT (it2, *it, it2data);
6020
6021 /* If newline is part of a composition, continue from start of composition */
6022 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6023 && beg < IT_CHARPOS (*it))
6024 goto replaced;
6025
6026 /* If newline is replaced by a display property, find start of overlay
6027 or interval and continue search from that point. */
6028 pos = --IT_CHARPOS (it2);
6029 --IT_BYTEPOS (it2);
6030 it2.sp = 0;
6031 bidi_unshelve_cache (NULL, 0);
6032 it2.string_from_display_prop_p = 0;
6033 it2.from_disp_prop_p = 0;
6034 if (handle_display_prop (&it2) == HANDLED_RETURN
6035 && !NILP (val = get_char_property_and_overlay
6036 (make_number (pos), Qdisplay, Qnil, &overlay))
6037 && (OVERLAYP (overlay)
6038 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6039 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6040 {
6041 RESTORE_IT (it, it, it2data);
6042 goto replaced;
6043 }
6044
6045 /* Newline is not replaced by anything -- so we are done. */
6046 RESTORE_IT (it, it, it2data);
6047 break;
6048
6049 replaced:
6050 if (beg < BEGV)
6051 beg = BEGV;
6052 IT_CHARPOS (*it) = beg;
6053 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6054 }
6055 }
6056
6057 it->continuation_lines_width = 0;
6058
6059 eassert (IT_CHARPOS (*it) >= BEGV);
6060 eassert (IT_CHARPOS (*it) == BEGV
6061 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6062 CHECK_IT (it);
6063 }
6064
6065
6066 /* Reseat iterator IT at the previous visible line start. Skip
6067 invisible text that is so either due to text properties or due to
6068 selective display. At the end, update IT's overlay information,
6069 face information etc. */
6070
6071 void
6072 reseat_at_previous_visible_line_start (struct it *it)
6073 {
6074 back_to_previous_visible_line_start (it);
6075 reseat (it, it->current.pos, 1);
6076 CHECK_IT (it);
6077 }
6078
6079
6080 /* Reseat iterator IT on the next visible line start in the current
6081 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6082 preceding the line start. Skip over invisible text that is so
6083 because of selective display. Compute faces, overlays etc at the
6084 new position. Note that this function does not skip over text that
6085 is invisible because of text properties. */
6086
6087 static void
6088 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6089 {
6090 int newline_found_p, skipped_p = 0;
6091 struct bidi_it bidi_it_prev;
6092
6093 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6094
6095 /* Skip over lines that are invisible because they are indented
6096 more than the value of IT->selective. */
6097 if (it->selective > 0)
6098 while (IT_CHARPOS (*it) < ZV
6099 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6100 it->selective))
6101 {
6102 eassert (IT_BYTEPOS (*it) == BEGV
6103 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6104 newline_found_p =
6105 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6106 }
6107
6108 /* Position on the newline if that's what's requested. */
6109 if (on_newline_p && newline_found_p)
6110 {
6111 if (STRINGP (it->string))
6112 {
6113 if (IT_STRING_CHARPOS (*it) > 0)
6114 {
6115 if (!it->bidi_p)
6116 {
6117 --IT_STRING_CHARPOS (*it);
6118 --IT_STRING_BYTEPOS (*it);
6119 }
6120 else
6121 {
6122 /* We need to restore the bidi iterator to the state
6123 it had on the newline, and resync the IT's
6124 position with that. */
6125 it->bidi_it = bidi_it_prev;
6126 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6127 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6128 }
6129 }
6130 }
6131 else if (IT_CHARPOS (*it) > BEGV)
6132 {
6133 if (!it->bidi_p)
6134 {
6135 --IT_CHARPOS (*it);
6136 --IT_BYTEPOS (*it);
6137 }
6138 else
6139 {
6140 /* We need to restore the bidi iterator to the state it
6141 had on the newline and resync IT with that. */
6142 it->bidi_it = bidi_it_prev;
6143 IT_CHARPOS (*it) = it->bidi_it.charpos;
6144 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6145 }
6146 reseat (it, it->current.pos, 0);
6147 }
6148 }
6149 else if (skipped_p)
6150 reseat (it, it->current.pos, 0);
6151
6152 CHECK_IT (it);
6153 }
6154
6155
6156 \f
6157 /***********************************************************************
6158 Changing an iterator's position
6159 ***********************************************************************/
6160
6161 /* Change IT's current position to POS in current_buffer. If FORCE_P
6162 is non-zero, always check for text properties at the new position.
6163 Otherwise, text properties are only looked up if POS >=
6164 IT->check_charpos of a property. */
6165
6166 static void
6167 reseat (struct it *it, struct text_pos pos, int force_p)
6168 {
6169 ptrdiff_t original_pos = IT_CHARPOS (*it);
6170
6171 reseat_1 (it, pos, 0);
6172
6173 /* Determine where to check text properties. Avoid doing it
6174 where possible because text property lookup is very expensive. */
6175 if (force_p
6176 || CHARPOS (pos) > it->stop_charpos
6177 || CHARPOS (pos) < original_pos)
6178 {
6179 if (it->bidi_p)
6180 {
6181 /* For bidi iteration, we need to prime prev_stop and
6182 base_level_stop with our best estimations. */
6183 /* Implementation note: Of course, POS is not necessarily a
6184 stop position, so assigning prev_pos to it is a lie; we
6185 should have called compute_stop_backwards. However, if
6186 the current buffer does not include any R2L characters,
6187 that call would be a waste of cycles, because the
6188 iterator will never move back, and thus never cross this
6189 "fake" stop position. So we delay that backward search
6190 until the time we really need it, in next_element_from_buffer. */
6191 if (CHARPOS (pos) != it->prev_stop)
6192 it->prev_stop = CHARPOS (pos);
6193 if (CHARPOS (pos) < it->base_level_stop)
6194 it->base_level_stop = 0; /* meaning it's unknown */
6195 handle_stop (it);
6196 }
6197 else
6198 {
6199 handle_stop (it);
6200 it->prev_stop = it->base_level_stop = 0;
6201 }
6202
6203 }
6204
6205 CHECK_IT (it);
6206 }
6207
6208
6209 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6210 IT->stop_pos to POS, also. */
6211
6212 static void
6213 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6214 {
6215 /* Don't call this function when scanning a C string. */
6216 eassert (it->s == NULL);
6217
6218 /* POS must be a reasonable value. */
6219 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6220
6221 it->current.pos = it->position = pos;
6222 it->end_charpos = ZV;
6223 it->dpvec = NULL;
6224 it->current.dpvec_index = -1;
6225 it->current.overlay_string_index = -1;
6226 IT_STRING_CHARPOS (*it) = -1;
6227 IT_STRING_BYTEPOS (*it) = -1;
6228 it->string = Qnil;
6229 it->method = GET_FROM_BUFFER;
6230 it->object = it->w->buffer;
6231 it->area = TEXT_AREA;
6232 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6233 it->sp = 0;
6234 it->string_from_display_prop_p = 0;
6235 it->string_from_prefix_prop_p = 0;
6236
6237 it->from_disp_prop_p = 0;
6238 it->face_before_selective_p = 0;
6239 if (it->bidi_p)
6240 {
6241 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6242 &it->bidi_it);
6243 bidi_unshelve_cache (NULL, 0);
6244 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6245 it->bidi_it.string.s = NULL;
6246 it->bidi_it.string.lstring = Qnil;
6247 it->bidi_it.string.bufpos = 0;
6248 it->bidi_it.string.unibyte = 0;
6249 }
6250
6251 if (set_stop_p)
6252 {
6253 it->stop_charpos = CHARPOS (pos);
6254 it->base_level_stop = CHARPOS (pos);
6255 }
6256 }
6257
6258
6259 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6260 If S is non-null, it is a C string to iterate over. Otherwise,
6261 STRING gives a Lisp string to iterate over.
6262
6263 If PRECISION > 0, don't return more then PRECISION number of
6264 characters from the string.
6265
6266 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6267 characters have been returned. FIELD_WIDTH < 0 means an infinite
6268 field width.
6269
6270 MULTIBYTE = 0 means disable processing of multibyte characters,
6271 MULTIBYTE > 0 means enable it,
6272 MULTIBYTE < 0 means use IT->multibyte_p.
6273
6274 IT must be initialized via a prior call to init_iterator before
6275 calling this function. */
6276
6277 static void
6278 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6279 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6280 int multibyte)
6281 {
6282 /* No region in strings. */
6283 it->region_beg_charpos = it->region_end_charpos = -1;
6284
6285 /* No text property checks performed by default, but see below. */
6286 it->stop_charpos = -1;
6287
6288 /* Set iterator position and end position. */
6289 memset (&it->current, 0, sizeof it->current);
6290 it->current.overlay_string_index = -1;
6291 it->current.dpvec_index = -1;
6292 eassert (charpos >= 0);
6293
6294 /* If STRING is specified, use its multibyteness, otherwise use the
6295 setting of MULTIBYTE, if specified. */
6296 if (multibyte >= 0)
6297 it->multibyte_p = multibyte > 0;
6298
6299 /* Bidirectional reordering of strings is controlled by the default
6300 value of bidi-display-reordering. Don't try to reorder while
6301 loading loadup.el, as the necessary character property tables are
6302 not yet available. */
6303 it->bidi_p =
6304 NILP (Vpurify_flag)
6305 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6306
6307 if (s == NULL)
6308 {
6309 eassert (STRINGP (string));
6310 it->string = string;
6311 it->s = NULL;
6312 it->end_charpos = it->string_nchars = SCHARS (string);
6313 it->method = GET_FROM_STRING;
6314 it->current.string_pos = string_pos (charpos, string);
6315
6316 if (it->bidi_p)
6317 {
6318 it->bidi_it.string.lstring = string;
6319 it->bidi_it.string.s = NULL;
6320 it->bidi_it.string.schars = it->end_charpos;
6321 it->bidi_it.string.bufpos = 0;
6322 it->bidi_it.string.from_disp_str = 0;
6323 it->bidi_it.string.unibyte = !it->multibyte_p;
6324 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6325 FRAME_WINDOW_P (it->f), &it->bidi_it);
6326 }
6327 }
6328 else
6329 {
6330 it->s = (const unsigned char *) s;
6331 it->string = Qnil;
6332
6333 /* Note that we use IT->current.pos, not it->current.string_pos,
6334 for displaying C strings. */
6335 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6336 if (it->multibyte_p)
6337 {
6338 it->current.pos = c_string_pos (charpos, s, 1);
6339 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6340 }
6341 else
6342 {
6343 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6344 it->end_charpos = it->string_nchars = strlen (s);
6345 }
6346
6347 if (it->bidi_p)
6348 {
6349 it->bidi_it.string.lstring = Qnil;
6350 it->bidi_it.string.s = (const unsigned char *) s;
6351 it->bidi_it.string.schars = it->end_charpos;
6352 it->bidi_it.string.bufpos = 0;
6353 it->bidi_it.string.from_disp_str = 0;
6354 it->bidi_it.string.unibyte = !it->multibyte_p;
6355 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6356 &it->bidi_it);
6357 }
6358 it->method = GET_FROM_C_STRING;
6359 }
6360
6361 /* PRECISION > 0 means don't return more than PRECISION characters
6362 from the string. */
6363 if (precision > 0 && it->end_charpos - charpos > precision)
6364 {
6365 it->end_charpos = it->string_nchars = charpos + precision;
6366 if (it->bidi_p)
6367 it->bidi_it.string.schars = it->end_charpos;
6368 }
6369
6370 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6371 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6372 FIELD_WIDTH < 0 means infinite field width. This is useful for
6373 padding with `-' at the end of a mode line. */
6374 if (field_width < 0)
6375 field_width = INFINITY;
6376 /* Implementation note: We deliberately don't enlarge
6377 it->bidi_it.string.schars here to fit it->end_charpos, because
6378 the bidi iterator cannot produce characters out of thin air. */
6379 if (field_width > it->end_charpos - charpos)
6380 it->end_charpos = charpos + field_width;
6381
6382 /* Use the standard display table for displaying strings. */
6383 if (DISP_TABLE_P (Vstandard_display_table))
6384 it->dp = XCHAR_TABLE (Vstandard_display_table);
6385
6386 it->stop_charpos = charpos;
6387 it->prev_stop = charpos;
6388 it->base_level_stop = 0;
6389 if (it->bidi_p)
6390 {
6391 it->bidi_it.first_elt = 1;
6392 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6393 it->bidi_it.disp_pos = -1;
6394 }
6395 if (s == NULL && it->multibyte_p)
6396 {
6397 ptrdiff_t endpos = SCHARS (it->string);
6398 if (endpos > it->end_charpos)
6399 endpos = it->end_charpos;
6400 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6401 it->string);
6402 }
6403 CHECK_IT (it);
6404 }
6405
6406
6407 \f
6408 /***********************************************************************
6409 Iteration
6410 ***********************************************************************/
6411
6412 /* Map enum it_method value to corresponding next_element_from_* function. */
6413
6414 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6415 {
6416 next_element_from_buffer,
6417 next_element_from_display_vector,
6418 next_element_from_string,
6419 next_element_from_c_string,
6420 next_element_from_image,
6421 next_element_from_stretch
6422 };
6423
6424 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6425
6426
6427 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6428 (possibly with the following characters). */
6429
6430 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6431 ((IT)->cmp_it.id >= 0 \
6432 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6433 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6434 END_CHARPOS, (IT)->w, \
6435 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6436 (IT)->string)))
6437
6438
6439 /* Lookup the char-table Vglyphless_char_display for character C (-1
6440 if we want information for no-font case), and return the display
6441 method symbol. By side-effect, update it->what and
6442 it->glyphless_method. This function is called from
6443 get_next_display_element for each character element, and from
6444 x_produce_glyphs when no suitable font was found. */
6445
6446 Lisp_Object
6447 lookup_glyphless_char_display (int c, struct it *it)
6448 {
6449 Lisp_Object glyphless_method = Qnil;
6450
6451 if (CHAR_TABLE_P (Vglyphless_char_display)
6452 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6453 {
6454 if (c >= 0)
6455 {
6456 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6457 if (CONSP (glyphless_method))
6458 glyphless_method = FRAME_WINDOW_P (it->f)
6459 ? XCAR (glyphless_method)
6460 : XCDR (glyphless_method);
6461 }
6462 else
6463 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6464 }
6465
6466 retry:
6467 if (NILP (glyphless_method))
6468 {
6469 if (c >= 0)
6470 /* The default is to display the character by a proper font. */
6471 return Qnil;
6472 /* The default for the no-font case is to display an empty box. */
6473 glyphless_method = Qempty_box;
6474 }
6475 if (EQ (glyphless_method, Qzero_width))
6476 {
6477 if (c >= 0)
6478 return glyphless_method;
6479 /* This method can't be used for the no-font case. */
6480 glyphless_method = Qempty_box;
6481 }
6482 if (EQ (glyphless_method, Qthin_space))
6483 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6484 else if (EQ (glyphless_method, Qempty_box))
6485 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6486 else if (EQ (glyphless_method, Qhex_code))
6487 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6488 else if (STRINGP (glyphless_method))
6489 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6490 else
6491 {
6492 /* Invalid value. We use the default method. */
6493 glyphless_method = Qnil;
6494 goto retry;
6495 }
6496 it->what = IT_GLYPHLESS;
6497 return glyphless_method;
6498 }
6499
6500 /* Load IT's display element fields with information about the next
6501 display element from the current position of IT. Value is zero if
6502 end of buffer (or C string) is reached. */
6503
6504 static struct frame *last_escape_glyph_frame = NULL;
6505 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6506 static int last_escape_glyph_merged_face_id = 0;
6507
6508 struct frame *last_glyphless_glyph_frame = NULL;
6509 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6510 int last_glyphless_glyph_merged_face_id = 0;
6511
6512 static int
6513 get_next_display_element (struct it *it)
6514 {
6515 /* Non-zero means that we found a display element. Zero means that
6516 we hit the end of what we iterate over. Performance note: the
6517 function pointer `method' used here turns out to be faster than
6518 using a sequence of if-statements. */
6519 int success_p;
6520
6521 get_next:
6522 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6523
6524 if (it->what == IT_CHARACTER)
6525 {
6526 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6527 and only if (a) the resolved directionality of that character
6528 is R..." */
6529 /* FIXME: Do we need an exception for characters from display
6530 tables? */
6531 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6532 it->c = bidi_mirror_char (it->c);
6533 /* Map via display table or translate control characters.
6534 IT->c, IT->len etc. have been set to the next character by
6535 the function call above. If we have a display table, and it
6536 contains an entry for IT->c, translate it. Don't do this if
6537 IT->c itself comes from a display table, otherwise we could
6538 end up in an infinite recursion. (An alternative could be to
6539 count the recursion depth of this function and signal an
6540 error when a certain maximum depth is reached.) Is it worth
6541 it? */
6542 if (success_p && it->dpvec == NULL)
6543 {
6544 Lisp_Object dv;
6545 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6546 int nonascii_space_p = 0;
6547 int nonascii_hyphen_p = 0;
6548 int c = it->c; /* This is the character to display. */
6549
6550 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6551 {
6552 eassert (SINGLE_BYTE_CHAR_P (c));
6553 if (unibyte_display_via_language_environment)
6554 {
6555 c = DECODE_CHAR (unibyte, c);
6556 if (c < 0)
6557 c = BYTE8_TO_CHAR (it->c);
6558 }
6559 else
6560 c = BYTE8_TO_CHAR (it->c);
6561 }
6562
6563 if (it->dp
6564 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6565 VECTORP (dv)))
6566 {
6567 struct Lisp_Vector *v = XVECTOR (dv);
6568
6569 /* Return the first character from the display table
6570 entry, if not empty. If empty, don't display the
6571 current character. */
6572 if (v->header.size)
6573 {
6574 it->dpvec_char_len = it->len;
6575 it->dpvec = v->contents;
6576 it->dpend = v->contents + v->header.size;
6577 it->current.dpvec_index = 0;
6578 it->dpvec_face_id = -1;
6579 it->saved_face_id = it->face_id;
6580 it->method = GET_FROM_DISPLAY_VECTOR;
6581 it->ellipsis_p = 0;
6582 }
6583 else
6584 {
6585 set_iterator_to_next (it, 0);
6586 }
6587 goto get_next;
6588 }
6589
6590 if (! NILP (lookup_glyphless_char_display (c, it)))
6591 {
6592 if (it->what == IT_GLYPHLESS)
6593 goto done;
6594 /* Don't display this character. */
6595 set_iterator_to_next (it, 0);
6596 goto get_next;
6597 }
6598
6599 /* If `nobreak-char-display' is non-nil, we display
6600 non-ASCII spaces and hyphens specially. */
6601 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6602 {
6603 if (c == 0xA0)
6604 nonascii_space_p = 1;
6605 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6606 nonascii_hyphen_p = 1;
6607 }
6608
6609 /* Translate control characters into `\003' or `^C' form.
6610 Control characters coming from a display table entry are
6611 currently not translated because we use IT->dpvec to hold
6612 the translation. This could easily be changed but I
6613 don't believe that it is worth doing.
6614
6615 The characters handled by `nobreak-char-display' must be
6616 translated too.
6617
6618 Non-printable characters and raw-byte characters are also
6619 translated to octal form. */
6620 if (((c < ' ' || c == 127) /* ASCII control chars */
6621 ? (it->area != TEXT_AREA
6622 /* In mode line, treat \n, \t like other crl chars. */
6623 || (c != '\t'
6624 && it->glyph_row
6625 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6626 || (c != '\n' && c != '\t'))
6627 : (nonascii_space_p
6628 || nonascii_hyphen_p
6629 || CHAR_BYTE8_P (c)
6630 || ! CHAR_PRINTABLE_P (c))))
6631 {
6632 /* C is a control character, non-ASCII space/hyphen,
6633 raw-byte, or a non-printable character which must be
6634 displayed either as '\003' or as `^C' where the '\\'
6635 and '^' can be defined in the display table. Fill
6636 IT->ctl_chars with glyphs for what we have to
6637 display. Then, set IT->dpvec to these glyphs. */
6638 Lisp_Object gc;
6639 int ctl_len;
6640 int face_id;
6641 int lface_id = 0;
6642 int escape_glyph;
6643
6644 /* Handle control characters with ^. */
6645
6646 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6647 {
6648 int g;
6649
6650 g = '^'; /* default glyph for Control */
6651 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6652 if (it->dp
6653 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6654 {
6655 g = GLYPH_CODE_CHAR (gc);
6656 lface_id = GLYPH_CODE_FACE (gc);
6657 }
6658 if (lface_id)
6659 {
6660 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6661 }
6662 else if (it->f == last_escape_glyph_frame
6663 && it->face_id == last_escape_glyph_face_id)
6664 {
6665 face_id = last_escape_glyph_merged_face_id;
6666 }
6667 else
6668 {
6669 /* Merge the escape-glyph face into the current face. */
6670 face_id = merge_faces (it->f, Qescape_glyph, 0,
6671 it->face_id);
6672 last_escape_glyph_frame = it->f;
6673 last_escape_glyph_face_id = it->face_id;
6674 last_escape_glyph_merged_face_id = face_id;
6675 }
6676
6677 XSETINT (it->ctl_chars[0], g);
6678 XSETINT (it->ctl_chars[1], c ^ 0100);
6679 ctl_len = 2;
6680 goto display_control;
6681 }
6682
6683 /* Handle non-ascii space in the mode where it only gets
6684 highlighting. */
6685
6686 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6687 {
6688 /* Merge `nobreak-space' into the current face. */
6689 face_id = merge_faces (it->f, Qnobreak_space, 0,
6690 it->face_id);
6691 XSETINT (it->ctl_chars[0], ' ');
6692 ctl_len = 1;
6693 goto display_control;
6694 }
6695
6696 /* Handle sequences that start with the "escape glyph". */
6697
6698 /* the default escape glyph is \. */
6699 escape_glyph = '\\';
6700
6701 if (it->dp
6702 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6703 {
6704 escape_glyph = GLYPH_CODE_CHAR (gc);
6705 lface_id = GLYPH_CODE_FACE (gc);
6706 }
6707 if (lface_id)
6708 {
6709 /* The display table specified a face.
6710 Merge it into face_id and also into escape_glyph. */
6711 face_id = merge_faces (it->f, Qt, lface_id,
6712 it->face_id);
6713 }
6714 else if (it->f == last_escape_glyph_frame
6715 && it->face_id == last_escape_glyph_face_id)
6716 {
6717 face_id = last_escape_glyph_merged_face_id;
6718 }
6719 else
6720 {
6721 /* Merge the escape-glyph face into the current face. */
6722 face_id = merge_faces (it->f, Qescape_glyph, 0,
6723 it->face_id);
6724 last_escape_glyph_frame = it->f;
6725 last_escape_glyph_face_id = it->face_id;
6726 last_escape_glyph_merged_face_id = face_id;
6727 }
6728
6729 /* Draw non-ASCII hyphen with just highlighting: */
6730
6731 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6732 {
6733 XSETINT (it->ctl_chars[0], '-');
6734 ctl_len = 1;
6735 goto display_control;
6736 }
6737
6738 /* Draw non-ASCII space/hyphen with escape glyph: */
6739
6740 if (nonascii_space_p || nonascii_hyphen_p)
6741 {
6742 XSETINT (it->ctl_chars[0], escape_glyph);
6743 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6744 ctl_len = 2;
6745 goto display_control;
6746 }
6747
6748 {
6749 char str[10];
6750 int len, i;
6751
6752 if (CHAR_BYTE8_P (c))
6753 /* Display \200 instead of \17777600. */
6754 c = CHAR_TO_BYTE8 (c);
6755 len = sprintf (str, "%03o", c);
6756
6757 XSETINT (it->ctl_chars[0], escape_glyph);
6758 for (i = 0; i < len; i++)
6759 XSETINT (it->ctl_chars[i + 1], str[i]);
6760 ctl_len = len + 1;
6761 }
6762
6763 display_control:
6764 /* Set up IT->dpvec and return first character from it. */
6765 it->dpvec_char_len = it->len;
6766 it->dpvec = it->ctl_chars;
6767 it->dpend = it->dpvec + ctl_len;
6768 it->current.dpvec_index = 0;
6769 it->dpvec_face_id = face_id;
6770 it->saved_face_id = it->face_id;
6771 it->method = GET_FROM_DISPLAY_VECTOR;
6772 it->ellipsis_p = 0;
6773 goto get_next;
6774 }
6775 it->char_to_display = c;
6776 }
6777 else if (success_p)
6778 {
6779 it->char_to_display = it->c;
6780 }
6781 }
6782
6783 /* Adjust face id for a multibyte character. There are no multibyte
6784 character in unibyte text. */
6785 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6786 && it->multibyte_p
6787 && success_p
6788 && FRAME_WINDOW_P (it->f))
6789 {
6790 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6791
6792 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6793 {
6794 /* Automatic composition with glyph-string. */
6795 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6796
6797 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6798 }
6799 else
6800 {
6801 ptrdiff_t pos = (it->s ? -1
6802 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6803 : IT_CHARPOS (*it));
6804 int c;
6805
6806 if (it->what == IT_CHARACTER)
6807 c = it->char_to_display;
6808 else
6809 {
6810 struct composition *cmp = composition_table[it->cmp_it.id];
6811 int i;
6812
6813 c = ' ';
6814 for (i = 0; i < cmp->glyph_len; i++)
6815 /* TAB in a composition means display glyphs with
6816 padding space on the left or right. */
6817 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6818 break;
6819 }
6820 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6821 }
6822 }
6823
6824 done:
6825 /* Is this character the last one of a run of characters with
6826 box? If yes, set IT->end_of_box_run_p to 1. */
6827 if (it->face_box_p
6828 && it->s == NULL)
6829 {
6830 if (it->method == GET_FROM_STRING && it->sp)
6831 {
6832 int face_id = underlying_face_id (it);
6833 struct face *face = FACE_FROM_ID (it->f, face_id);
6834
6835 if (face)
6836 {
6837 if (face->box == FACE_NO_BOX)
6838 {
6839 /* If the box comes from face properties in a
6840 display string, check faces in that string. */
6841 int string_face_id = face_after_it_pos (it);
6842 it->end_of_box_run_p
6843 = (FACE_FROM_ID (it->f, string_face_id)->box
6844 == FACE_NO_BOX);
6845 }
6846 /* Otherwise, the box comes from the underlying face.
6847 If this is the last string character displayed, check
6848 the next buffer location. */
6849 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6850 && (it->current.overlay_string_index
6851 == it->n_overlay_strings - 1))
6852 {
6853 ptrdiff_t ignore;
6854 int next_face_id;
6855 struct text_pos pos = it->current.pos;
6856 INC_TEXT_POS (pos, it->multibyte_p);
6857
6858 next_face_id = face_at_buffer_position
6859 (it->w, CHARPOS (pos), it->region_beg_charpos,
6860 it->region_end_charpos, &ignore,
6861 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6862 -1);
6863 it->end_of_box_run_p
6864 = (FACE_FROM_ID (it->f, next_face_id)->box
6865 == FACE_NO_BOX);
6866 }
6867 }
6868 }
6869 else
6870 {
6871 int face_id = face_after_it_pos (it);
6872 it->end_of_box_run_p
6873 = (face_id != it->face_id
6874 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6875 }
6876 }
6877 /* If we reached the end of the object we've been iterating (e.g., a
6878 display string or an overlay string), and there's something on
6879 IT->stack, proceed with what's on the stack. It doesn't make
6880 sense to return zero if there's unprocessed stuff on the stack,
6881 because otherwise that stuff will never be displayed. */
6882 if (!success_p && it->sp > 0)
6883 {
6884 set_iterator_to_next (it, 0);
6885 success_p = get_next_display_element (it);
6886 }
6887
6888 /* Value is 0 if end of buffer or string reached. */
6889 return success_p;
6890 }
6891
6892
6893 /* Move IT to the next display element.
6894
6895 RESEAT_P non-zero means if called on a newline in buffer text,
6896 skip to the next visible line start.
6897
6898 Functions get_next_display_element and set_iterator_to_next are
6899 separate because I find this arrangement easier to handle than a
6900 get_next_display_element function that also increments IT's
6901 position. The way it is we can first look at an iterator's current
6902 display element, decide whether it fits on a line, and if it does,
6903 increment the iterator position. The other way around we probably
6904 would either need a flag indicating whether the iterator has to be
6905 incremented the next time, or we would have to implement a
6906 decrement position function which would not be easy to write. */
6907
6908 void
6909 set_iterator_to_next (struct it *it, int reseat_p)
6910 {
6911 /* Reset flags indicating start and end of a sequence of characters
6912 with box. Reset them at the start of this function because
6913 moving the iterator to a new position might set them. */
6914 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6915
6916 switch (it->method)
6917 {
6918 case GET_FROM_BUFFER:
6919 /* The current display element of IT is a character from
6920 current_buffer. Advance in the buffer, and maybe skip over
6921 invisible lines that are so because of selective display. */
6922 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6923 reseat_at_next_visible_line_start (it, 0);
6924 else if (it->cmp_it.id >= 0)
6925 {
6926 /* We are currently getting glyphs from a composition. */
6927 int i;
6928
6929 if (! it->bidi_p)
6930 {
6931 IT_CHARPOS (*it) += it->cmp_it.nchars;
6932 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6933 if (it->cmp_it.to < it->cmp_it.nglyphs)
6934 {
6935 it->cmp_it.from = it->cmp_it.to;
6936 }
6937 else
6938 {
6939 it->cmp_it.id = -1;
6940 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6941 IT_BYTEPOS (*it),
6942 it->end_charpos, Qnil);
6943 }
6944 }
6945 else if (! it->cmp_it.reversed_p)
6946 {
6947 /* Composition created while scanning forward. */
6948 /* Update IT's char/byte positions to point to the first
6949 character of the next grapheme cluster, or to the
6950 character visually after the current composition. */
6951 for (i = 0; i < it->cmp_it.nchars; i++)
6952 bidi_move_to_visually_next (&it->bidi_it);
6953 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6954 IT_CHARPOS (*it) = it->bidi_it.charpos;
6955
6956 if (it->cmp_it.to < it->cmp_it.nglyphs)
6957 {
6958 /* Proceed to the next grapheme cluster. */
6959 it->cmp_it.from = it->cmp_it.to;
6960 }
6961 else
6962 {
6963 /* No more grapheme clusters in this composition.
6964 Find the next stop position. */
6965 ptrdiff_t stop = it->end_charpos;
6966 if (it->bidi_it.scan_dir < 0)
6967 /* Now we are scanning backward and don't know
6968 where to stop. */
6969 stop = -1;
6970 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6971 IT_BYTEPOS (*it), stop, Qnil);
6972 }
6973 }
6974 else
6975 {
6976 /* Composition created while scanning backward. */
6977 /* Update IT's char/byte positions to point to the last
6978 character of the previous grapheme cluster, or the
6979 character visually after the current composition. */
6980 for (i = 0; i < it->cmp_it.nchars; i++)
6981 bidi_move_to_visually_next (&it->bidi_it);
6982 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6983 IT_CHARPOS (*it) = it->bidi_it.charpos;
6984 if (it->cmp_it.from > 0)
6985 {
6986 /* Proceed to the previous grapheme cluster. */
6987 it->cmp_it.to = it->cmp_it.from;
6988 }
6989 else
6990 {
6991 /* No more grapheme clusters in this composition.
6992 Find the next stop position. */
6993 ptrdiff_t stop = it->end_charpos;
6994 if (it->bidi_it.scan_dir < 0)
6995 /* Now we are scanning backward and don't know
6996 where to stop. */
6997 stop = -1;
6998 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6999 IT_BYTEPOS (*it), stop, Qnil);
7000 }
7001 }
7002 }
7003 else
7004 {
7005 eassert (it->len != 0);
7006
7007 if (!it->bidi_p)
7008 {
7009 IT_BYTEPOS (*it) += it->len;
7010 IT_CHARPOS (*it) += 1;
7011 }
7012 else
7013 {
7014 int prev_scan_dir = it->bidi_it.scan_dir;
7015 /* If this is a new paragraph, determine its base
7016 direction (a.k.a. its base embedding level). */
7017 if (it->bidi_it.new_paragraph)
7018 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7019 bidi_move_to_visually_next (&it->bidi_it);
7020 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7021 IT_CHARPOS (*it) = it->bidi_it.charpos;
7022 if (prev_scan_dir != it->bidi_it.scan_dir)
7023 {
7024 /* As the scan direction was changed, we must
7025 re-compute the stop position for composition. */
7026 ptrdiff_t stop = it->end_charpos;
7027 if (it->bidi_it.scan_dir < 0)
7028 stop = -1;
7029 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7030 IT_BYTEPOS (*it), stop, Qnil);
7031 }
7032 }
7033 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7034 }
7035 break;
7036
7037 case GET_FROM_C_STRING:
7038 /* Current display element of IT is from a C string. */
7039 if (!it->bidi_p
7040 /* If the string position is beyond string's end, it means
7041 next_element_from_c_string is padding the string with
7042 blanks, in which case we bypass the bidi iterator,
7043 because it cannot deal with such virtual characters. */
7044 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7045 {
7046 IT_BYTEPOS (*it) += it->len;
7047 IT_CHARPOS (*it) += 1;
7048 }
7049 else
7050 {
7051 bidi_move_to_visually_next (&it->bidi_it);
7052 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7053 IT_CHARPOS (*it) = it->bidi_it.charpos;
7054 }
7055 break;
7056
7057 case GET_FROM_DISPLAY_VECTOR:
7058 /* Current display element of IT is from a display table entry.
7059 Advance in the display table definition. Reset it to null if
7060 end reached, and continue with characters from buffers/
7061 strings. */
7062 ++it->current.dpvec_index;
7063
7064 /* Restore face of the iterator to what they were before the
7065 display vector entry (these entries may contain faces). */
7066 it->face_id = it->saved_face_id;
7067
7068 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7069 {
7070 int recheck_faces = it->ellipsis_p;
7071
7072 if (it->s)
7073 it->method = GET_FROM_C_STRING;
7074 else if (STRINGP (it->string))
7075 it->method = GET_FROM_STRING;
7076 else
7077 {
7078 it->method = GET_FROM_BUFFER;
7079 it->object = it->w->buffer;
7080 }
7081
7082 it->dpvec = NULL;
7083 it->current.dpvec_index = -1;
7084
7085 /* Skip over characters which were displayed via IT->dpvec. */
7086 if (it->dpvec_char_len < 0)
7087 reseat_at_next_visible_line_start (it, 1);
7088 else if (it->dpvec_char_len > 0)
7089 {
7090 if (it->method == GET_FROM_STRING
7091 && it->n_overlay_strings > 0)
7092 it->ignore_overlay_strings_at_pos_p = 1;
7093 it->len = it->dpvec_char_len;
7094 set_iterator_to_next (it, reseat_p);
7095 }
7096
7097 /* Maybe recheck faces after display vector */
7098 if (recheck_faces)
7099 it->stop_charpos = IT_CHARPOS (*it);
7100 }
7101 break;
7102
7103 case GET_FROM_STRING:
7104 /* Current display element is a character from a Lisp string. */
7105 eassert (it->s == NULL && STRINGP (it->string));
7106 /* Don't advance past string end. These conditions are true
7107 when set_iterator_to_next is called at the end of
7108 get_next_display_element, in which case the Lisp string is
7109 already exhausted, and all we want is pop the iterator
7110 stack. */
7111 if (it->current.overlay_string_index >= 0)
7112 {
7113 /* This is an overlay string, so there's no padding with
7114 spaces, and the number of characters in the string is
7115 where the string ends. */
7116 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7117 goto consider_string_end;
7118 }
7119 else
7120 {
7121 /* Not an overlay string. There could be padding, so test
7122 against it->end_charpos . */
7123 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7124 goto consider_string_end;
7125 }
7126 if (it->cmp_it.id >= 0)
7127 {
7128 int i;
7129
7130 if (! it->bidi_p)
7131 {
7132 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7133 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7134 if (it->cmp_it.to < it->cmp_it.nglyphs)
7135 it->cmp_it.from = it->cmp_it.to;
7136 else
7137 {
7138 it->cmp_it.id = -1;
7139 composition_compute_stop_pos (&it->cmp_it,
7140 IT_STRING_CHARPOS (*it),
7141 IT_STRING_BYTEPOS (*it),
7142 it->end_charpos, it->string);
7143 }
7144 }
7145 else if (! it->cmp_it.reversed_p)
7146 {
7147 for (i = 0; i < it->cmp_it.nchars; i++)
7148 bidi_move_to_visually_next (&it->bidi_it);
7149 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7150 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7151
7152 if (it->cmp_it.to < it->cmp_it.nglyphs)
7153 it->cmp_it.from = it->cmp_it.to;
7154 else
7155 {
7156 ptrdiff_t stop = it->end_charpos;
7157 if (it->bidi_it.scan_dir < 0)
7158 stop = -1;
7159 composition_compute_stop_pos (&it->cmp_it,
7160 IT_STRING_CHARPOS (*it),
7161 IT_STRING_BYTEPOS (*it), stop,
7162 it->string);
7163 }
7164 }
7165 else
7166 {
7167 for (i = 0; i < it->cmp_it.nchars; i++)
7168 bidi_move_to_visually_next (&it->bidi_it);
7169 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7170 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7171 if (it->cmp_it.from > 0)
7172 it->cmp_it.to = it->cmp_it.from;
7173 else
7174 {
7175 ptrdiff_t stop = it->end_charpos;
7176 if (it->bidi_it.scan_dir < 0)
7177 stop = -1;
7178 composition_compute_stop_pos (&it->cmp_it,
7179 IT_STRING_CHARPOS (*it),
7180 IT_STRING_BYTEPOS (*it), stop,
7181 it->string);
7182 }
7183 }
7184 }
7185 else
7186 {
7187 if (!it->bidi_p
7188 /* If the string position is beyond string's end, it
7189 means next_element_from_string is padding the string
7190 with blanks, in which case we bypass the bidi
7191 iterator, because it cannot deal with such virtual
7192 characters. */
7193 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7194 {
7195 IT_STRING_BYTEPOS (*it) += it->len;
7196 IT_STRING_CHARPOS (*it) += 1;
7197 }
7198 else
7199 {
7200 int prev_scan_dir = it->bidi_it.scan_dir;
7201
7202 bidi_move_to_visually_next (&it->bidi_it);
7203 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7204 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7205 if (prev_scan_dir != it->bidi_it.scan_dir)
7206 {
7207 ptrdiff_t stop = it->end_charpos;
7208
7209 if (it->bidi_it.scan_dir < 0)
7210 stop = -1;
7211 composition_compute_stop_pos (&it->cmp_it,
7212 IT_STRING_CHARPOS (*it),
7213 IT_STRING_BYTEPOS (*it), stop,
7214 it->string);
7215 }
7216 }
7217 }
7218
7219 consider_string_end:
7220
7221 if (it->current.overlay_string_index >= 0)
7222 {
7223 /* IT->string is an overlay string. Advance to the
7224 next, if there is one. */
7225 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7226 {
7227 it->ellipsis_p = 0;
7228 next_overlay_string (it);
7229 if (it->ellipsis_p)
7230 setup_for_ellipsis (it, 0);
7231 }
7232 }
7233 else
7234 {
7235 /* IT->string is not an overlay string. If we reached
7236 its end, and there is something on IT->stack, proceed
7237 with what is on the stack. This can be either another
7238 string, this time an overlay string, or a buffer. */
7239 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7240 && it->sp > 0)
7241 {
7242 pop_it (it);
7243 if (it->method == GET_FROM_STRING)
7244 goto consider_string_end;
7245 }
7246 }
7247 break;
7248
7249 case GET_FROM_IMAGE:
7250 case GET_FROM_STRETCH:
7251 /* The position etc with which we have to proceed are on
7252 the stack. The position may be at the end of a string,
7253 if the `display' property takes up the whole string. */
7254 eassert (it->sp > 0);
7255 pop_it (it);
7256 if (it->method == GET_FROM_STRING)
7257 goto consider_string_end;
7258 break;
7259
7260 default:
7261 /* There are no other methods defined, so this should be a bug. */
7262 abort ();
7263 }
7264
7265 eassert (it->method != GET_FROM_STRING
7266 || (STRINGP (it->string)
7267 && IT_STRING_CHARPOS (*it) >= 0));
7268 }
7269
7270 /* Load IT's display element fields with information about the next
7271 display element which comes from a display table entry or from the
7272 result of translating a control character to one of the forms `^C'
7273 or `\003'.
7274
7275 IT->dpvec holds the glyphs to return as characters.
7276 IT->saved_face_id holds the face id before the display vector--it
7277 is restored into IT->face_id in set_iterator_to_next. */
7278
7279 static int
7280 next_element_from_display_vector (struct it *it)
7281 {
7282 Lisp_Object gc;
7283
7284 /* Precondition. */
7285 eassert (it->dpvec && it->current.dpvec_index >= 0);
7286
7287 it->face_id = it->saved_face_id;
7288
7289 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7290 That seemed totally bogus - so I changed it... */
7291 gc = it->dpvec[it->current.dpvec_index];
7292
7293 if (GLYPH_CODE_P (gc))
7294 {
7295 it->c = GLYPH_CODE_CHAR (gc);
7296 it->len = CHAR_BYTES (it->c);
7297
7298 /* The entry may contain a face id to use. Such a face id is
7299 the id of a Lisp face, not a realized face. A face id of
7300 zero means no face is specified. */
7301 if (it->dpvec_face_id >= 0)
7302 it->face_id = it->dpvec_face_id;
7303 else
7304 {
7305 int lface_id = GLYPH_CODE_FACE (gc);
7306 if (lface_id > 0)
7307 it->face_id = merge_faces (it->f, Qt, lface_id,
7308 it->saved_face_id);
7309 }
7310 }
7311 else
7312 /* Display table entry is invalid. Return a space. */
7313 it->c = ' ', it->len = 1;
7314
7315 /* Don't change position and object of the iterator here. They are
7316 still the values of the character that had this display table
7317 entry or was translated, and that's what we want. */
7318 it->what = IT_CHARACTER;
7319 return 1;
7320 }
7321
7322 /* Get the first element of string/buffer in the visual order, after
7323 being reseated to a new position in a string or a buffer. */
7324 static void
7325 get_visually_first_element (struct it *it)
7326 {
7327 int string_p = STRINGP (it->string) || it->s;
7328 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7329 ptrdiff_t bob = (string_p ? 0 : BEGV);
7330
7331 if (STRINGP (it->string))
7332 {
7333 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7334 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7335 }
7336 else
7337 {
7338 it->bidi_it.charpos = IT_CHARPOS (*it);
7339 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7340 }
7341
7342 if (it->bidi_it.charpos == eob)
7343 {
7344 /* Nothing to do, but reset the FIRST_ELT flag, like
7345 bidi_paragraph_init does, because we are not going to
7346 call it. */
7347 it->bidi_it.first_elt = 0;
7348 }
7349 else if (it->bidi_it.charpos == bob
7350 || (!string_p
7351 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7352 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7353 {
7354 /* If we are at the beginning of a line/string, we can produce
7355 the next element right away. */
7356 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7357 bidi_move_to_visually_next (&it->bidi_it);
7358 }
7359 else
7360 {
7361 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7362
7363 /* We need to prime the bidi iterator starting at the line's or
7364 string's beginning, before we will be able to produce the
7365 next element. */
7366 if (string_p)
7367 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7368 else
7369 {
7370 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7371 -1);
7372 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7373 }
7374 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7375 do
7376 {
7377 /* Now return to buffer/string position where we were asked
7378 to get the next display element, and produce that. */
7379 bidi_move_to_visually_next (&it->bidi_it);
7380 }
7381 while (it->bidi_it.bytepos != orig_bytepos
7382 && it->bidi_it.charpos < eob);
7383 }
7384
7385 /* Adjust IT's position information to where we ended up. */
7386 if (STRINGP (it->string))
7387 {
7388 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7389 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7390 }
7391 else
7392 {
7393 IT_CHARPOS (*it) = it->bidi_it.charpos;
7394 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7395 }
7396
7397 if (STRINGP (it->string) || !it->s)
7398 {
7399 ptrdiff_t stop, charpos, bytepos;
7400
7401 if (STRINGP (it->string))
7402 {
7403 eassert (!it->s);
7404 stop = SCHARS (it->string);
7405 if (stop > it->end_charpos)
7406 stop = it->end_charpos;
7407 charpos = IT_STRING_CHARPOS (*it);
7408 bytepos = IT_STRING_BYTEPOS (*it);
7409 }
7410 else
7411 {
7412 stop = it->end_charpos;
7413 charpos = IT_CHARPOS (*it);
7414 bytepos = IT_BYTEPOS (*it);
7415 }
7416 if (it->bidi_it.scan_dir < 0)
7417 stop = -1;
7418 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7419 it->string);
7420 }
7421 }
7422
7423 /* Load IT with the next display element from Lisp string IT->string.
7424 IT->current.string_pos is the current position within the string.
7425 If IT->current.overlay_string_index >= 0, the Lisp string is an
7426 overlay string. */
7427
7428 static int
7429 next_element_from_string (struct it *it)
7430 {
7431 struct text_pos position;
7432
7433 eassert (STRINGP (it->string));
7434 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7435 eassert (IT_STRING_CHARPOS (*it) >= 0);
7436 position = it->current.string_pos;
7437
7438 /* With bidi reordering, the character to display might not be the
7439 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7440 that we were reseat()ed to a new string, whose paragraph
7441 direction is not known. */
7442 if (it->bidi_p && it->bidi_it.first_elt)
7443 {
7444 get_visually_first_element (it);
7445 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7446 }
7447
7448 /* Time to check for invisible text? */
7449 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7450 {
7451 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7452 {
7453 if (!(!it->bidi_p
7454 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7455 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7456 {
7457 /* With bidi non-linear iteration, we could find
7458 ourselves far beyond the last computed stop_charpos,
7459 with several other stop positions in between that we
7460 missed. Scan them all now, in buffer's logical
7461 order, until we find and handle the last stop_charpos
7462 that precedes our current position. */
7463 handle_stop_backwards (it, it->stop_charpos);
7464 return GET_NEXT_DISPLAY_ELEMENT (it);
7465 }
7466 else
7467 {
7468 if (it->bidi_p)
7469 {
7470 /* Take note of the stop position we just moved
7471 across, for when we will move back across it. */
7472 it->prev_stop = it->stop_charpos;
7473 /* If we are at base paragraph embedding level, take
7474 note of the last stop position seen at this
7475 level. */
7476 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7477 it->base_level_stop = it->stop_charpos;
7478 }
7479 handle_stop (it);
7480
7481 /* Since a handler may have changed IT->method, we must
7482 recurse here. */
7483 return GET_NEXT_DISPLAY_ELEMENT (it);
7484 }
7485 }
7486 else if (it->bidi_p
7487 /* If we are before prev_stop, we may have overstepped
7488 on our way backwards a stop_pos, and if so, we need
7489 to handle that stop_pos. */
7490 && IT_STRING_CHARPOS (*it) < it->prev_stop
7491 /* We can sometimes back up for reasons that have nothing
7492 to do with bidi reordering. E.g., compositions. The
7493 code below is only needed when we are above the base
7494 embedding level, so test for that explicitly. */
7495 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7496 {
7497 /* If we lost track of base_level_stop, we have no better
7498 place for handle_stop_backwards to start from than string
7499 beginning. This happens, e.g., when we were reseated to
7500 the previous screenful of text by vertical-motion. */
7501 if (it->base_level_stop <= 0
7502 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7503 it->base_level_stop = 0;
7504 handle_stop_backwards (it, it->base_level_stop);
7505 return GET_NEXT_DISPLAY_ELEMENT (it);
7506 }
7507 }
7508
7509 if (it->current.overlay_string_index >= 0)
7510 {
7511 /* Get the next character from an overlay string. In overlay
7512 strings, there is no field width or padding with spaces to
7513 do. */
7514 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7515 {
7516 it->what = IT_EOB;
7517 return 0;
7518 }
7519 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7520 IT_STRING_BYTEPOS (*it),
7521 it->bidi_it.scan_dir < 0
7522 ? -1
7523 : SCHARS (it->string))
7524 && next_element_from_composition (it))
7525 {
7526 return 1;
7527 }
7528 else if (STRING_MULTIBYTE (it->string))
7529 {
7530 const unsigned char *s = (SDATA (it->string)
7531 + IT_STRING_BYTEPOS (*it));
7532 it->c = string_char_and_length (s, &it->len);
7533 }
7534 else
7535 {
7536 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7537 it->len = 1;
7538 }
7539 }
7540 else
7541 {
7542 /* Get the next character from a Lisp string that is not an
7543 overlay string. Such strings come from the mode line, for
7544 example. We may have to pad with spaces, or truncate the
7545 string. See also next_element_from_c_string. */
7546 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7547 {
7548 it->what = IT_EOB;
7549 return 0;
7550 }
7551 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7552 {
7553 /* Pad with spaces. */
7554 it->c = ' ', it->len = 1;
7555 CHARPOS (position) = BYTEPOS (position) = -1;
7556 }
7557 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7558 IT_STRING_BYTEPOS (*it),
7559 it->bidi_it.scan_dir < 0
7560 ? -1
7561 : it->string_nchars)
7562 && next_element_from_composition (it))
7563 {
7564 return 1;
7565 }
7566 else if (STRING_MULTIBYTE (it->string))
7567 {
7568 const unsigned char *s = (SDATA (it->string)
7569 + IT_STRING_BYTEPOS (*it));
7570 it->c = string_char_and_length (s, &it->len);
7571 }
7572 else
7573 {
7574 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7575 it->len = 1;
7576 }
7577 }
7578
7579 /* Record what we have and where it came from. */
7580 it->what = IT_CHARACTER;
7581 it->object = it->string;
7582 it->position = position;
7583 return 1;
7584 }
7585
7586
7587 /* Load IT with next display element from C string IT->s.
7588 IT->string_nchars is the maximum number of characters to return
7589 from the string. IT->end_charpos may be greater than
7590 IT->string_nchars when this function is called, in which case we
7591 may have to return padding spaces. Value is zero if end of string
7592 reached, including padding spaces. */
7593
7594 static int
7595 next_element_from_c_string (struct it *it)
7596 {
7597 int success_p = 1;
7598
7599 eassert (it->s);
7600 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7601 it->what = IT_CHARACTER;
7602 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7603 it->object = Qnil;
7604
7605 /* With bidi reordering, the character to display might not be the
7606 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7607 we were reseated to a new string, whose paragraph direction is
7608 not known. */
7609 if (it->bidi_p && it->bidi_it.first_elt)
7610 get_visually_first_element (it);
7611
7612 /* IT's position can be greater than IT->string_nchars in case a
7613 field width or precision has been specified when the iterator was
7614 initialized. */
7615 if (IT_CHARPOS (*it) >= it->end_charpos)
7616 {
7617 /* End of the game. */
7618 it->what = IT_EOB;
7619 success_p = 0;
7620 }
7621 else if (IT_CHARPOS (*it) >= it->string_nchars)
7622 {
7623 /* Pad with spaces. */
7624 it->c = ' ', it->len = 1;
7625 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7626 }
7627 else if (it->multibyte_p)
7628 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7629 else
7630 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7631
7632 return success_p;
7633 }
7634
7635
7636 /* Set up IT to return characters from an ellipsis, if appropriate.
7637 The definition of the ellipsis glyphs may come from a display table
7638 entry. This function fills IT with the first glyph from the
7639 ellipsis if an ellipsis is to be displayed. */
7640
7641 static int
7642 next_element_from_ellipsis (struct it *it)
7643 {
7644 if (it->selective_display_ellipsis_p)
7645 setup_for_ellipsis (it, it->len);
7646 else
7647 {
7648 /* The face at the current position may be different from the
7649 face we find after the invisible text. Remember what it
7650 was in IT->saved_face_id, and signal that it's there by
7651 setting face_before_selective_p. */
7652 it->saved_face_id = it->face_id;
7653 it->method = GET_FROM_BUFFER;
7654 it->object = it->w->buffer;
7655 reseat_at_next_visible_line_start (it, 1);
7656 it->face_before_selective_p = 1;
7657 }
7658
7659 return GET_NEXT_DISPLAY_ELEMENT (it);
7660 }
7661
7662
7663 /* Deliver an image display element. The iterator IT is already
7664 filled with image information (done in handle_display_prop). Value
7665 is always 1. */
7666
7667
7668 static int
7669 next_element_from_image (struct it *it)
7670 {
7671 it->what = IT_IMAGE;
7672 it->ignore_overlay_strings_at_pos_p = 0;
7673 return 1;
7674 }
7675
7676
7677 /* Fill iterator IT with next display element from a stretch glyph
7678 property. IT->object is the value of the text property. Value is
7679 always 1. */
7680
7681 static int
7682 next_element_from_stretch (struct it *it)
7683 {
7684 it->what = IT_STRETCH;
7685 return 1;
7686 }
7687
7688 /* Scan backwards from IT's current position until we find a stop
7689 position, or until BEGV. This is called when we find ourself
7690 before both the last known prev_stop and base_level_stop while
7691 reordering bidirectional text. */
7692
7693 static void
7694 compute_stop_pos_backwards (struct it *it)
7695 {
7696 const int SCAN_BACK_LIMIT = 1000;
7697 struct text_pos pos;
7698 struct display_pos save_current = it->current;
7699 struct text_pos save_position = it->position;
7700 ptrdiff_t charpos = IT_CHARPOS (*it);
7701 ptrdiff_t where_we_are = charpos;
7702 ptrdiff_t save_stop_pos = it->stop_charpos;
7703 ptrdiff_t save_end_pos = it->end_charpos;
7704
7705 eassert (NILP (it->string) && !it->s);
7706 eassert (it->bidi_p);
7707 it->bidi_p = 0;
7708 do
7709 {
7710 it->end_charpos = min (charpos + 1, ZV);
7711 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7712 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7713 reseat_1 (it, pos, 0);
7714 compute_stop_pos (it);
7715 /* We must advance forward, right? */
7716 if (it->stop_charpos <= charpos)
7717 abort ();
7718 }
7719 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7720
7721 if (it->stop_charpos <= where_we_are)
7722 it->prev_stop = it->stop_charpos;
7723 else
7724 it->prev_stop = BEGV;
7725 it->bidi_p = 1;
7726 it->current = save_current;
7727 it->position = save_position;
7728 it->stop_charpos = save_stop_pos;
7729 it->end_charpos = save_end_pos;
7730 }
7731
7732 /* Scan forward from CHARPOS in the current buffer/string, until we
7733 find a stop position > current IT's position. Then handle the stop
7734 position before that. This is called when we bump into a stop
7735 position while reordering bidirectional text. CHARPOS should be
7736 the last previously processed stop_pos (or BEGV/0, if none were
7737 processed yet) whose position is less that IT's current
7738 position. */
7739
7740 static void
7741 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7742 {
7743 int bufp = !STRINGP (it->string);
7744 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7745 struct display_pos save_current = it->current;
7746 struct text_pos save_position = it->position;
7747 struct text_pos pos1;
7748 ptrdiff_t next_stop;
7749
7750 /* Scan in strict logical order. */
7751 eassert (it->bidi_p);
7752 it->bidi_p = 0;
7753 do
7754 {
7755 it->prev_stop = charpos;
7756 if (bufp)
7757 {
7758 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7759 reseat_1 (it, pos1, 0);
7760 }
7761 else
7762 it->current.string_pos = string_pos (charpos, it->string);
7763 compute_stop_pos (it);
7764 /* We must advance forward, right? */
7765 if (it->stop_charpos <= it->prev_stop)
7766 abort ();
7767 charpos = it->stop_charpos;
7768 }
7769 while (charpos <= where_we_are);
7770
7771 it->bidi_p = 1;
7772 it->current = save_current;
7773 it->position = save_position;
7774 next_stop = it->stop_charpos;
7775 it->stop_charpos = it->prev_stop;
7776 handle_stop (it);
7777 it->stop_charpos = next_stop;
7778 }
7779
7780 /* Load IT with the next display element from current_buffer. Value
7781 is zero if end of buffer reached. IT->stop_charpos is the next
7782 position at which to stop and check for text properties or buffer
7783 end. */
7784
7785 static int
7786 next_element_from_buffer (struct it *it)
7787 {
7788 int success_p = 1;
7789
7790 eassert (IT_CHARPOS (*it) >= BEGV);
7791 eassert (NILP (it->string) && !it->s);
7792 eassert (!it->bidi_p
7793 || (EQ (it->bidi_it.string.lstring, Qnil)
7794 && it->bidi_it.string.s == NULL));
7795
7796 /* With bidi reordering, the character to display might not be the
7797 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7798 we were reseat()ed to a new buffer position, which is potentially
7799 a different paragraph. */
7800 if (it->bidi_p && it->bidi_it.first_elt)
7801 {
7802 get_visually_first_element (it);
7803 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7804 }
7805
7806 if (IT_CHARPOS (*it) >= it->stop_charpos)
7807 {
7808 if (IT_CHARPOS (*it) >= it->end_charpos)
7809 {
7810 int overlay_strings_follow_p;
7811
7812 /* End of the game, except when overlay strings follow that
7813 haven't been returned yet. */
7814 if (it->overlay_strings_at_end_processed_p)
7815 overlay_strings_follow_p = 0;
7816 else
7817 {
7818 it->overlay_strings_at_end_processed_p = 1;
7819 overlay_strings_follow_p = get_overlay_strings (it, 0);
7820 }
7821
7822 if (overlay_strings_follow_p)
7823 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7824 else
7825 {
7826 it->what = IT_EOB;
7827 it->position = it->current.pos;
7828 success_p = 0;
7829 }
7830 }
7831 else if (!(!it->bidi_p
7832 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7833 || IT_CHARPOS (*it) == it->stop_charpos))
7834 {
7835 /* With bidi non-linear iteration, we could find ourselves
7836 far beyond the last computed stop_charpos, with several
7837 other stop positions in between that we missed. Scan
7838 them all now, in buffer's logical order, until we find
7839 and handle the last stop_charpos that precedes our
7840 current position. */
7841 handle_stop_backwards (it, it->stop_charpos);
7842 return GET_NEXT_DISPLAY_ELEMENT (it);
7843 }
7844 else
7845 {
7846 if (it->bidi_p)
7847 {
7848 /* Take note of the stop position we just moved across,
7849 for when we will move back across it. */
7850 it->prev_stop = it->stop_charpos;
7851 /* If we are at base paragraph embedding level, take
7852 note of the last stop position seen at this
7853 level. */
7854 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7855 it->base_level_stop = it->stop_charpos;
7856 }
7857 handle_stop (it);
7858 return GET_NEXT_DISPLAY_ELEMENT (it);
7859 }
7860 }
7861 else if (it->bidi_p
7862 /* If we are before prev_stop, we may have overstepped on
7863 our way backwards a stop_pos, and if so, we need to
7864 handle that stop_pos. */
7865 && IT_CHARPOS (*it) < it->prev_stop
7866 /* We can sometimes back up for reasons that have nothing
7867 to do with bidi reordering. E.g., compositions. The
7868 code below is only needed when we are above the base
7869 embedding level, so test for that explicitly. */
7870 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7871 {
7872 if (it->base_level_stop <= 0
7873 || IT_CHARPOS (*it) < it->base_level_stop)
7874 {
7875 /* If we lost track of base_level_stop, we need to find
7876 prev_stop by looking backwards. This happens, e.g., when
7877 we were reseated to the previous screenful of text by
7878 vertical-motion. */
7879 it->base_level_stop = BEGV;
7880 compute_stop_pos_backwards (it);
7881 handle_stop_backwards (it, it->prev_stop);
7882 }
7883 else
7884 handle_stop_backwards (it, it->base_level_stop);
7885 return GET_NEXT_DISPLAY_ELEMENT (it);
7886 }
7887 else
7888 {
7889 /* No face changes, overlays etc. in sight, so just return a
7890 character from current_buffer. */
7891 unsigned char *p;
7892 ptrdiff_t stop;
7893
7894 /* Maybe run the redisplay end trigger hook. Performance note:
7895 This doesn't seem to cost measurable time. */
7896 if (it->redisplay_end_trigger_charpos
7897 && it->glyph_row
7898 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7899 run_redisplay_end_trigger_hook (it);
7900
7901 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7902 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7903 stop)
7904 && next_element_from_composition (it))
7905 {
7906 return 1;
7907 }
7908
7909 /* Get the next character, maybe multibyte. */
7910 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7911 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7912 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7913 else
7914 it->c = *p, it->len = 1;
7915
7916 /* Record what we have and where it came from. */
7917 it->what = IT_CHARACTER;
7918 it->object = it->w->buffer;
7919 it->position = it->current.pos;
7920
7921 /* Normally we return the character found above, except when we
7922 really want to return an ellipsis for selective display. */
7923 if (it->selective)
7924 {
7925 if (it->c == '\n')
7926 {
7927 /* A value of selective > 0 means hide lines indented more
7928 than that number of columns. */
7929 if (it->selective > 0
7930 && IT_CHARPOS (*it) + 1 < ZV
7931 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7932 IT_BYTEPOS (*it) + 1,
7933 it->selective))
7934 {
7935 success_p = next_element_from_ellipsis (it);
7936 it->dpvec_char_len = -1;
7937 }
7938 }
7939 else if (it->c == '\r' && it->selective == -1)
7940 {
7941 /* A value of selective == -1 means that everything from the
7942 CR to the end of the line is invisible, with maybe an
7943 ellipsis displayed for it. */
7944 success_p = next_element_from_ellipsis (it);
7945 it->dpvec_char_len = -1;
7946 }
7947 }
7948 }
7949
7950 /* Value is zero if end of buffer reached. */
7951 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7952 return success_p;
7953 }
7954
7955
7956 /* Run the redisplay end trigger hook for IT. */
7957
7958 static void
7959 run_redisplay_end_trigger_hook (struct it *it)
7960 {
7961 Lisp_Object args[3];
7962
7963 /* IT->glyph_row should be non-null, i.e. we should be actually
7964 displaying something, or otherwise we should not run the hook. */
7965 eassert (it->glyph_row);
7966
7967 /* Set up hook arguments. */
7968 args[0] = Qredisplay_end_trigger_functions;
7969 args[1] = it->window;
7970 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7971 it->redisplay_end_trigger_charpos = 0;
7972
7973 /* Since we are *trying* to run these functions, don't try to run
7974 them again, even if they get an error. */
7975 it->w->redisplay_end_trigger = Qnil;
7976 Frun_hook_with_args (3, args);
7977
7978 /* Notice if it changed the face of the character we are on. */
7979 handle_face_prop (it);
7980 }
7981
7982
7983 /* Deliver a composition display element. Unlike the other
7984 next_element_from_XXX, this function is not registered in the array
7985 get_next_element[]. It is called from next_element_from_buffer and
7986 next_element_from_string when necessary. */
7987
7988 static int
7989 next_element_from_composition (struct it *it)
7990 {
7991 it->what = IT_COMPOSITION;
7992 it->len = it->cmp_it.nbytes;
7993 if (STRINGP (it->string))
7994 {
7995 if (it->c < 0)
7996 {
7997 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7998 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7999 return 0;
8000 }
8001 it->position = it->current.string_pos;
8002 it->object = it->string;
8003 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8004 IT_STRING_BYTEPOS (*it), it->string);
8005 }
8006 else
8007 {
8008 if (it->c < 0)
8009 {
8010 IT_CHARPOS (*it) += it->cmp_it.nchars;
8011 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8012 if (it->bidi_p)
8013 {
8014 if (it->bidi_it.new_paragraph)
8015 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8016 /* Resync the bidi iterator with IT's new position.
8017 FIXME: this doesn't support bidirectional text. */
8018 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8019 bidi_move_to_visually_next (&it->bidi_it);
8020 }
8021 return 0;
8022 }
8023 it->position = it->current.pos;
8024 it->object = it->w->buffer;
8025 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8026 IT_BYTEPOS (*it), Qnil);
8027 }
8028 return 1;
8029 }
8030
8031
8032 \f
8033 /***********************************************************************
8034 Moving an iterator without producing glyphs
8035 ***********************************************************************/
8036
8037 /* Check if iterator is at a position corresponding to a valid buffer
8038 position after some move_it_ call. */
8039
8040 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8041 ((it)->method == GET_FROM_STRING \
8042 ? IT_STRING_CHARPOS (*it) == 0 \
8043 : 1)
8044
8045
8046 /* Move iterator IT to a specified buffer or X position within one
8047 line on the display without producing glyphs.
8048
8049 OP should be a bit mask including some or all of these bits:
8050 MOVE_TO_X: Stop upon reaching x-position TO_X.
8051 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8052 Regardless of OP's value, stop upon reaching the end of the display line.
8053
8054 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8055 This means, in particular, that TO_X includes window's horizontal
8056 scroll amount.
8057
8058 The return value has several possible values that
8059 say what condition caused the scan to stop:
8060
8061 MOVE_POS_MATCH_OR_ZV
8062 - when TO_POS or ZV was reached.
8063
8064 MOVE_X_REACHED
8065 -when TO_X was reached before TO_POS or ZV were reached.
8066
8067 MOVE_LINE_CONTINUED
8068 - when we reached the end of the display area and the line must
8069 be continued.
8070
8071 MOVE_LINE_TRUNCATED
8072 - when we reached the end of the display area and the line is
8073 truncated.
8074
8075 MOVE_NEWLINE_OR_CR
8076 - when we stopped at a line end, i.e. a newline or a CR and selective
8077 display is on. */
8078
8079 static enum move_it_result
8080 move_it_in_display_line_to (struct it *it,
8081 ptrdiff_t to_charpos, int to_x,
8082 enum move_operation_enum op)
8083 {
8084 enum move_it_result result = MOVE_UNDEFINED;
8085 struct glyph_row *saved_glyph_row;
8086 struct it wrap_it, atpos_it, atx_it, ppos_it;
8087 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8088 void *ppos_data = NULL;
8089 int may_wrap = 0;
8090 enum it_method prev_method = it->method;
8091 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8092 int saw_smaller_pos = prev_pos < to_charpos;
8093
8094 /* Don't produce glyphs in produce_glyphs. */
8095 saved_glyph_row = it->glyph_row;
8096 it->glyph_row = NULL;
8097
8098 /* Use wrap_it to save a copy of IT wherever a word wrap could
8099 occur. Use atpos_it to save a copy of IT at the desired buffer
8100 position, if found, so that we can scan ahead and check if the
8101 word later overshoots the window edge. Use atx_it similarly, for
8102 pixel positions. */
8103 wrap_it.sp = -1;
8104 atpos_it.sp = -1;
8105 atx_it.sp = -1;
8106
8107 /* Use ppos_it under bidi reordering to save a copy of IT for the
8108 position > CHARPOS that is the closest to CHARPOS. We restore
8109 that position in IT when we have scanned the entire display line
8110 without finding a match for CHARPOS and all the character
8111 positions are greater than CHARPOS. */
8112 if (it->bidi_p)
8113 {
8114 SAVE_IT (ppos_it, *it, ppos_data);
8115 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8116 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8117 SAVE_IT (ppos_it, *it, ppos_data);
8118 }
8119
8120 #define BUFFER_POS_REACHED_P() \
8121 ((op & MOVE_TO_POS) != 0 \
8122 && BUFFERP (it->object) \
8123 && (IT_CHARPOS (*it) == to_charpos \
8124 || ((!it->bidi_p \
8125 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8126 && IT_CHARPOS (*it) > to_charpos) \
8127 || (it->what == IT_COMPOSITION \
8128 && ((IT_CHARPOS (*it) > to_charpos \
8129 && to_charpos >= it->cmp_it.charpos) \
8130 || (IT_CHARPOS (*it) < to_charpos \
8131 && to_charpos <= it->cmp_it.charpos)))) \
8132 && (it->method == GET_FROM_BUFFER \
8133 || (it->method == GET_FROM_DISPLAY_VECTOR \
8134 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8135
8136 /* If there's a line-/wrap-prefix, handle it. */
8137 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8138 && it->current_y < it->last_visible_y)
8139 handle_line_prefix (it);
8140
8141 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8142 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8143
8144 while (1)
8145 {
8146 int x, i, ascent = 0, descent = 0;
8147
8148 /* Utility macro to reset an iterator with x, ascent, and descent. */
8149 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8150 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8151 (IT)->max_descent = descent)
8152
8153 /* Stop if we move beyond TO_CHARPOS (after an image or a
8154 display string or stretch glyph). */
8155 if ((op & MOVE_TO_POS) != 0
8156 && BUFFERP (it->object)
8157 && it->method == GET_FROM_BUFFER
8158 && (((!it->bidi_p
8159 /* When the iterator is at base embedding level, we
8160 are guaranteed that characters are delivered for
8161 display in strictly increasing order of their
8162 buffer positions. */
8163 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8164 && IT_CHARPOS (*it) > to_charpos)
8165 || (it->bidi_p
8166 && (prev_method == GET_FROM_IMAGE
8167 || prev_method == GET_FROM_STRETCH
8168 || prev_method == GET_FROM_STRING)
8169 /* Passed TO_CHARPOS from left to right. */
8170 && ((prev_pos < to_charpos
8171 && IT_CHARPOS (*it) > to_charpos)
8172 /* Passed TO_CHARPOS from right to left. */
8173 || (prev_pos > to_charpos
8174 && IT_CHARPOS (*it) < to_charpos)))))
8175 {
8176 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8177 {
8178 result = MOVE_POS_MATCH_OR_ZV;
8179 break;
8180 }
8181 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8182 /* If wrap_it is valid, the current position might be in a
8183 word that is wrapped. So, save the iterator in
8184 atpos_it and continue to see if wrapping happens. */
8185 SAVE_IT (atpos_it, *it, atpos_data);
8186 }
8187
8188 /* Stop when ZV reached.
8189 We used to stop here when TO_CHARPOS reached as well, but that is
8190 too soon if this glyph does not fit on this line. So we handle it
8191 explicitly below. */
8192 if (!get_next_display_element (it))
8193 {
8194 result = MOVE_POS_MATCH_OR_ZV;
8195 break;
8196 }
8197
8198 if (it->line_wrap == TRUNCATE)
8199 {
8200 if (BUFFER_POS_REACHED_P ())
8201 {
8202 result = MOVE_POS_MATCH_OR_ZV;
8203 break;
8204 }
8205 }
8206 else
8207 {
8208 if (it->line_wrap == WORD_WRAP)
8209 {
8210 if (IT_DISPLAYING_WHITESPACE (it))
8211 may_wrap = 1;
8212 else if (may_wrap)
8213 {
8214 /* We have reached a glyph that follows one or more
8215 whitespace characters. If the position is
8216 already found, we are done. */
8217 if (atpos_it.sp >= 0)
8218 {
8219 RESTORE_IT (it, &atpos_it, atpos_data);
8220 result = MOVE_POS_MATCH_OR_ZV;
8221 goto done;
8222 }
8223 if (atx_it.sp >= 0)
8224 {
8225 RESTORE_IT (it, &atx_it, atx_data);
8226 result = MOVE_X_REACHED;
8227 goto done;
8228 }
8229 /* Otherwise, we can wrap here. */
8230 SAVE_IT (wrap_it, *it, wrap_data);
8231 may_wrap = 0;
8232 }
8233 }
8234 }
8235
8236 /* Remember the line height for the current line, in case
8237 the next element doesn't fit on the line. */
8238 ascent = it->max_ascent;
8239 descent = it->max_descent;
8240
8241 /* The call to produce_glyphs will get the metrics of the
8242 display element IT is loaded with. Record the x-position
8243 before this display element, in case it doesn't fit on the
8244 line. */
8245 x = it->current_x;
8246
8247 PRODUCE_GLYPHS (it);
8248
8249 if (it->area != TEXT_AREA)
8250 {
8251 prev_method = it->method;
8252 if (it->method == GET_FROM_BUFFER)
8253 prev_pos = IT_CHARPOS (*it);
8254 set_iterator_to_next (it, 1);
8255 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8256 SET_TEXT_POS (this_line_min_pos,
8257 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8258 if (it->bidi_p
8259 && (op & MOVE_TO_POS)
8260 && IT_CHARPOS (*it) > to_charpos
8261 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8262 SAVE_IT (ppos_it, *it, ppos_data);
8263 continue;
8264 }
8265
8266 /* The number of glyphs we get back in IT->nglyphs will normally
8267 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8268 character on a terminal frame, or (iii) a line end. For the
8269 second case, IT->nglyphs - 1 padding glyphs will be present.
8270 (On X frames, there is only one glyph produced for a
8271 composite character.)
8272
8273 The behavior implemented below means, for continuation lines,
8274 that as many spaces of a TAB as fit on the current line are
8275 displayed there. For terminal frames, as many glyphs of a
8276 multi-glyph character are displayed in the current line, too.
8277 This is what the old redisplay code did, and we keep it that
8278 way. Under X, the whole shape of a complex character must
8279 fit on the line or it will be completely displayed in the
8280 next line.
8281
8282 Note that both for tabs and padding glyphs, all glyphs have
8283 the same width. */
8284 if (it->nglyphs)
8285 {
8286 /* More than one glyph or glyph doesn't fit on line. All
8287 glyphs have the same width. */
8288 int single_glyph_width = it->pixel_width / it->nglyphs;
8289 int new_x;
8290 int x_before_this_char = x;
8291 int hpos_before_this_char = it->hpos;
8292
8293 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8294 {
8295 new_x = x + single_glyph_width;
8296
8297 /* We want to leave anything reaching TO_X to the caller. */
8298 if ((op & MOVE_TO_X) && new_x > to_x)
8299 {
8300 if (BUFFER_POS_REACHED_P ())
8301 {
8302 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8303 goto buffer_pos_reached;
8304 if (atpos_it.sp < 0)
8305 {
8306 SAVE_IT (atpos_it, *it, atpos_data);
8307 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8308 }
8309 }
8310 else
8311 {
8312 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8313 {
8314 it->current_x = x;
8315 result = MOVE_X_REACHED;
8316 break;
8317 }
8318 if (atx_it.sp < 0)
8319 {
8320 SAVE_IT (atx_it, *it, atx_data);
8321 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8322 }
8323 }
8324 }
8325
8326 if (/* Lines are continued. */
8327 it->line_wrap != TRUNCATE
8328 && (/* And glyph doesn't fit on the line. */
8329 new_x > it->last_visible_x
8330 /* Or it fits exactly and we're on a window
8331 system frame. */
8332 || (new_x == it->last_visible_x
8333 && FRAME_WINDOW_P (it->f)
8334 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8335 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8336 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8337 {
8338 if (/* IT->hpos == 0 means the very first glyph
8339 doesn't fit on the line, e.g. a wide image. */
8340 it->hpos == 0
8341 || (new_x == it->last_visible_x
8342 && FRAME_WINDOW_P (it->f)))
8343 {
8344 ++it->hpos;
8345 it->current_x = new_x;
8346
8347 /* The character's last glyph just barely fits
8348 in this row. */
8349 if (i == it->nglyphs - 1)
8350 {
8351 /* If this is the destination position,
8352 return a position *before* it in this row,
8353 now that we know it fits in this row. */
8354 if (BUFFER_POS_REACHED_P ())
8355 {
8356 if (it->line_wrap != WORD_WRAP
8357 || wrap_it.sp < 0)
8358 {
8359 it->hpos = hpos_before_this_char;
8360 it->current_x = x_before_this_char;
8361 result = MOVE_POS_MATCH_OR_ZV;
8362 break;
8363 }
8364 if (it->line_wrap == WORD_WRAP
8365 && atpos_it.sp < 0)
8366 {
8367 SAVE_IT (atpos_it, *it, atpos_data);
8368 atpos_it.current_x = x_before_this_char;
8369 atpos_it.hpos = hpos_before_this_char;
8370 }
8371 }
8372
8373 prev_method = it->method;
8374 if (it->method == GET_FROM_BUFFER)
8375 prev_pos = IT_CHARPOS (*it);
8376 set_iterator_to_next (it, 1);
8377 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8378 SET_TEXT_POS (this_line_min_pos,
8379 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8380 /* On graphical terminals, newlines may
8381 "overflow" into the fringe if
8382 overflow-newline-into-fringe is non-nil.
8383 On text terminals, newlines may overflow
8384 into the last glyph on the display
8385 line.*/
8386 if (!FRAME_WINDOW_P (it->f)
8387 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8388 {
8389 if (!get_next_display_element (it))
8390 {
8391 result = MOVE_POS_MATCH_OR_ZV;
8392 break;
8393 }
8394 if (BUFFER_POS_REACHED_P ())
8395 {
8396 if (ITERATOR_AT_END_OF_LINE_P (it))
8397 result = MOVE_POS_MATCH_OR_ZV;
8398 else
8399 result = MOVE_LINE_CONTINUED;
8400 break;
8401 }
8402 if (ITERATOR_AT_END_OF_LINE_P (it))
8403 {
8404 result = MOVE_NEWLINE_OR_CR;
8405 break;
8406 }
8407 }
8408 }
8409 }
8410 else
8411 IT_RESET_X_ASCENT_DESCENT (it);
8412
8413 if (wrap_it.sp >= 0)
8414 {
8415 RESTORE_IT (it, &wrap_it, wrap_data);
8416 atpos_it.sp = -1;
8417 atx_it.sp = -1;
8418 }
8419
8420 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8421 IT_CHARPOS (*it)));
8422 result = MOVE_LINE_CONTINUED;
8423 break;
8424 }
8425
8426 if (BUFFER_POS_REACHED_P ())
8427 {
8428 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8429 goto buffer_pos_reached;
8430 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8431 {
8432 SAVE_IT (atpos_it, *it, atpos_data);
8433 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8434 }
8435 }
8436
8437 if (new_x > it->first_visible_x)
8438 {
8439 /* Glyph is visible. Increment number of glyphs that
8440 would be displayed. */
8441 ++it->hpos;
8442 }
8443 }
8444
8445 if (result != MOVE_UNDEFINED)
8446 break;
8447 }
8448 else if (BUFFER_POS_REACHED_P ())
8449 {
8450 buffer_pos_reached:
8451 IT_RESET_X_ASCENT_DESCENT (it);
8452 result = MOVE_POS_MATCH_OR_ZV;
8453 break;
8454 }
8455 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8456 {
8457 /* Stop when TO_X specified and reached. This check is
8458 necessary here because of lines consisting of a line end,
8459 only. The line end will not produce any glyphs and we
8460 would never get MOVE_X_REACHED. */
8461 eassert (it->nglyphs == 0);
8462 result = MOVE_X_REACHED;
8463 break;
8464 }
8465
8466 /* Is this a line end? If yes, we're done. */
8467 if (ITERATOR_AT_END_OF_LINE_P (it))
8468 {
8469 /* If we are past TO_CHARPOS, but never saw any character
8470 positions smaller than TO_CHARPOS, return
8471 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8472 did. */
8473 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8474 {
8475 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8476 {
8477 if (IT_CHARPOS (ppos_it) < ZV)
8478 {
8479 RESTORE_IT (it, &ppos_it, ppos_data);
8480 result = MOVE_POS_MATCH_OR_ZV;
8481 }
8482 else
8483 goto buffer_pos_reached;
8484 }
8485 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8486 && IT_CHARPOS (*it) > to_charpos)
8487 goto buffer_pos_reached;
8488 else
8489 result = MOVE_NEWLINE_OR_CR;
8490 }
8491 else
8492 result = MOVE_NEWLINE_OR_CR;
8493 break;
8494 }
8495
8496 prev_method = it->method;
8497 if (it->method == GET_FROM_BUFFER)
8498 prev_pos = IT_CHARPOS (*it);
8499 /* The current display element has been consumed. Advance
8500 to the next. */
8501 set_iterator_to_next (it, 1);
8502 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8503 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8504 if (IT_CHARPOS (*it) < to_charpos)
8505 saw_smaller_pos = 1;
8506 if (it->bidi_p
8507 && (op & MOVE_TO_POS)
8508 && IT_CHARPOS (*it) >= to_charpos
8509 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8510 SAVE_IT (ppos_it, *it, ppos_data);
8511
8512 /* Stop if lines are truncated and IT's current x-position is
8513 past the right edge of the window now. */
8514 if (it->line_wrap == TRUNCATE
8515 && it->current_x >= it->last_visible_x)
8516 {
8517 if (!FRAME_WINDOW_P (it->f)
8518 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8519 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8520 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
8521 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8522 {
8523 int at_eob_p = 0;
8524
8525 if ((at_eob_p = !get_next_display_element (it))
8526 || BUFFER_POS_REACHED_P ()
8527 /* If we are past TO_CHARPOS, but never saw any
8528 character positions smaller than TO_CHARPOS,
8529 return MOVE_POS_MATCH_OR_ZV, like the
8530 unidirectional display did. */
8531 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8532 && !saw_smaller_pos
8533 && IT_CHARPOS (*it) > to_charpos))
8534 {
8535 if (it->bidi_p
8536 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8537 RESTORE_IT (it, &ppos_it, ppos_data);
8538 result = MOVE_POS_MATCH_OR_ZV;
8539 break;
8540 }
8541 if (ITERATOR_AT_END_OF_LINE_P (it))
8542 {
8543 result = MOVE_NEWLINE_OR_CR;
8544 break;
8545 }
8546 }
8547 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8548 && !saw_smaller_pos
8549 && IT_CHARPOS (*it) > to_charpos)
8550 {
8551 if (IT_CHARPOS (ppos_it) < ZV)
8552 RESTORE_IT (it, &ppos_it, ppos_data);
8553 result = MOVE_POS_MATCH_OR_ZV;
8554 break;
8555 }
8556 result = MOVE_LINE_TRUNCATED;
8557 break;
8558 }
8559 #undef IT_RESET_X_ASCENT_DESCENT
8560 }
8561
8562 #undef BUFFER_POS_REACHED_P
8563
8564 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8565 restore the saved iterator. */
8566 if (atpos_it.sp >= 0)
8567 RESTORE_IT (it, &atpos_it, atpos_data);
8568 else if (atx_it.sp >= 0)
8569 RESTORE_IT (it, &atx_it, atx_data);
8570
8571 done:
8572
8573 if (atpos_data)
8574 bidi_unshelve_cache (atpos_data, 1);
8575 if (atx_data)
8576 bidi_unshelve_cache (atx_data, 1);
8577 if (wrap_data)
8578 bidi_unshelve_cache (wrap_data, 1);
8579 if (ppos_data)
8580 bidi_unshelve_cache (ppos_data, 1);
8581
8582 /* Restore the iterator settings altered at the beginning of this
8583 function. */
8584 it->glyph_row = saved_glyph_row;
8585 return result;
8586 }
8587
8588 /* For external use. */
8589 void
8590 move_it_in_display_line (struct it *it,
8591 ptrdiff_t to_charpos, int to_x,
8592 enum move_operation_enum op)
8593 {
8594 if (it->line_wrap == WORD_WRAP
8595 && (op & MOVE_TO_X))
8596 {
8597 struct it save_it;
8598 void *save_data = NULL;
8599 int skip;
8600
8601 SAVE_IT (save_it, *it, save_data);
8602 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8603 /* When word-wrap is on, TO_X may lie past the end
8604 of a wrapped line. Then it->current is the
8605 character on the next line, so backtrack to the
8606 space before the wrap point. */
8607 if (skip == MOVE_LINE_CONTINUED)
8608 {
8609 int prev_x = max (it->current_x - 1, 0);
8610 RESTORE_IT (it, &save_it, save_data);
8611 move_it_in_display_line_to
8612 (it, -1, prev_x, MOVE_TO_X);
8613 }
8614 else
8615 bidi_unshelve_cache (save_data, 1);
8616 }
8617 else
8618 move_it_in_display_line_to (it, to_charpos, to_x, op);
8619 }
8620
8621
8622 /* Move IT forward until it satisfies one or more of the criteria in
8623 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8624
8625 OP is a bit-mask that specifies where to stop, and in particular,
8626 which of those four position arguments makes a difference. See the
8627 description of enum move_operation_enum.
8628
8629 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8630 screen line, this function will set IT to the next position that is
8631 displayed to the right of TO_CHARPOS on the screen. */
8632
8633 void
8634 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8635 {
8636 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8637 int line_height, line_start_x = 0, reached = 0;
8638 void *backup_data = NULL;
8639
8640 for (;;)
8641 {
8642 if (op & MOVE_TO_VPOS)
8643 {
8644 /* If no TO_CHARPOS and no TO_X specified, stop at the
8645 start of the line TO_VPOS. */
8646 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8647 {
8648 if (it->vpos == to_vpos)
8649 {
8650 reached = 1;
8651 break;
8652 }
8653 else
8654 skip = move_it_in_display_line_to (it, -1, -1, 0);
8655 }
8656 else
8657 {
8658 /* TO_VPOS >= 0 means stop at TO_X in the line at
8659 TO_VPOS, or at TO_POS, whichever comes first. */
8660 if (it->vpos == to_vpos)
8661 {
8662 reached = 2;
8663 break;
8664 }
8665
8666 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8667
8668 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8669 {
8670 reached = 3;
8671 break;
8672 }
8673 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8674 {
8675 /* We have reached TO_X but not in the line we want. */
8676 skip = move_it_in_display_line_to (it, to_charpos,
8677 -1, MOVE_TO_POS);
8678 if (skip == MOVE_POS_MATCH_OR_ZV)
8679 {
8680 reached = 4;
8681 break;
8682 }
8683 }
8684 }
8685 }
8686 else if (op & MOVE_TO_Y)
8687 {
8688 struct it it_backup;
8689
8690 if (it->line_wrap == WORD_WRAP)
8691 SAVE_IT (it_backup, *it, backup_data);
8692
8693 /* TO_Y specified means stop at TO_X in the line containing
8694 TO_Y---or at TO_CHARPOS if this is reached first. The
8695 problem is that we can't really tell whether the line
8696 contains TO_Y before we have completely scanned it, and
8697 this may skip past TO_X. What we do is to first scan to
8698 TO_X.
8699
8700 If TO_X is not specified, use a TO_X of zero. The reason
8701 is to make the outcome of this function more predictable.
8702 If we didn't use TO_X == 0, we would stop at the end of
8703 the line which is probably not what a caller would expect
8704 to happen. */
8705 skip = move_it_in_display_line_to
8706 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8707 (MOVE_TO_X | (op & MOVE_TO_POS)));
8708
8709 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8710 if (skip == MOVE_POS_MATCH_OR_ZV)
8711 reached = 5;
8712 else if (skip == MOVE_X_REACHED)
8713 {
8714 /* If TO_X was reached, we want to know whether TO_Y is
8715 in the line. We know this is the case if the already
8716 scanned glyphs make the line tall enough. Otherwise,
8717 we must check by scanning the rest of the line. */
8718 line_height = it->max_ascent + it->max_descent;
8719 if (to_y >= it->current_y
8720 && to_y < it->current_y + line_height)
8721 {
8722 reached = 6;
8723 break;
8724 }
8725 SAVE_IT (it_backup, *it, backup_data);
8726 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8727 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8728 op & MOVE_TO_POS);
8729 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8730 line_height = it->max_ascent + it->max_descent;
8731 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8732
8733 if (to_y >= it->current_y
8734 && to_y < it->current_y + line_height)
8735 {
8736 /* If TO_Y is in this line and TO_X was reached
8737 above, we scanned too far. We have to restore
8738 IT's settings to the ones before skipping. But
8739 keep the more accurate values of max_ascent and
8740 max_descent we've found while skipping the rest
8741 of the line, for the sake of callers, such as
8742 pos_visible_p, that need to know the line
8743 height. */
8744 int max_ascent = it->max_ascent;
8745 int max_descent = it->max_descent;
8746
8747 RESTORE_IT (it, &it_backup, backup_data);
8748 it->max_ascent = max_ascent;
8749 it->max_descent = max_descent;
8750 reached = 6;
8751 }
8752 else
8753 {
8754 skip = skip2;
8755 if (skip == MOVE_POS_MATCH_OR_ZV)
8756 reached = 7;
8757 }
8758 }
8759 else
8760 {
8761 /* Check whether TO_Y is in this line. */
8762 line_height = it->max_ascent + it->max_descent;
8763 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8764
8765 if (to_y >= it->current_y
8766 && to_y < it->current_y + line_height)
8767 {
8768 /* When word-wrap is on, TO_X may lie past the end
8769 of a wrapped line. Then it->current is the
8770 character on the next line, so backtrack to the
8771 space before the wrap point. */
8772 if (skip == MOVE_LINE_CONTINUED
8773 && it->line_wrap == WORD_WRAP)
8774 {
8775 int prev_x = max (it->current_x - 1, 0);
8776 RESTORE_IT (it, &it_backup, backup_data);
8777 skip = move_it_in_display_line_to
8778 (it, -1, prev_x, MOVE_TO_X);
8779 }
8780 reached = 6;
8781 }
8782 }
8783
8784 if (reached)
8785 break;
8786 }
8787 else if (BUFFERP (it->object)
8788 && (it->method == GET_FROM_BUFFER
8789 || it->method == GET_FROM_STRETCH)
8790 && IT_CHARPOS (*it) >= to_charpos
8791 /* Under bidi iteration, a call to set_iterator_to_next
8792 can scan far beyond to_charpos if the initial
8793 portion of the next line needs to be reordered. In
8794 that case, give move_it_in_display_line_to another
8795 chance below. */
8796 && !(it->bidi_p
8797 && it->bidi_it.scan_dir == -1))
8798 skip = MOVE_POS_MATCH_OR_ZV;
8799 else
8800 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8801
8802 switch (skip)
8803 {
8804 case MOVE_POS_MATCH_OR_ZV:
8805 reached = 8;
8806 goto out;
8807
8808 case MOVE_NEWLINE_OR_CR:
8809 set_iterator_to_next (it, 1);
8810 it->continuation_lines_width = 0;
8811 break;
8812
8813 case MOVE_LINE_TRUNCATED:
8814 it->continuation_lines_width = 0;
8815 reseat_at_next_visible_line_start (it, 0);
8816 if ((op & MOVE_TO_POS) != 0
8817 && IT_CHARPOS (*it) > to_charpos)
8818 {
8819 reached = 9;
8820 goto out;
8821 }
8822 break;
8823
8824 case MOVE_LINE_CONTINUED:
8825 /* For continued lines ending in a tab, some of the glyphs
8826 associated with the tab are displayed on the current
8827 line. Since it->current_x does not include these glyphs,
8828 we use it->last_visible_x instead. */
8829 if (it->c == '\t')
8830 {
8831 it->continuation_lines_width += it->last_visible_x;
8832 /* When moving by vpos, ensure that the iterator really
8833 advances to the next line (bug#847, bug#969). Fixme:
8834 do we need to do this in other circumstances? */
8835 if (it->current_x != it->last_visible_x
8836 && (op & MOVE_TO_VPOS)
8837 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8838 {
8839 line_start_x = it->current_x + it->pixel_width
8840 - it->last_visible_x;
8841 set_iterator_to_next (it, 0);
8842 }
8843 }
8844 else
8845 it->continuation_lines_width += it->current_x;
8846 break;
8847
8848 default:
8849 abort ();
8850 }
8851
8852 /* Reset/increment for the next run. */
8853 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8854 it->current_x = line_start_x;
8855 line_start_x = 0;
8856 it->hpos = 0;
8857 it->current_y += it->max_ascent + it->max_descent;
8858 ++it->vpos;
8859 last_height = it->max_ascent + it->max_descent;
8860 last_max_ascent = it->max_ascent;
8861 it->max_ascent = it->max_descent = 0;
8862 }
8863
8864 out:
8865
8866 /* On text terminals, we may stop at the end of a line in the middle
8867 of a multi-character glyph. If the glyph itself is continued,
8868 i.e. it is actually displayed on the next line, don't treat this
8869 stopping point as valid; move to the next line instead (unless
8870 that brings us offscreen). */
8871 if (!FRAME_WINDOW_P (it->f)
8872 && op & MOVE_TO_POS
8873 && IT_CHARPOS (*it) == to_charpos
8874 && it->what == IT_CHARACTER
8875 && it->nglyphs > 1
8876 && it->line_wrap == WINDOW_WRAP
8877 && it->current_x == it->last_visible_x - 1
8878 && it->c != '\n'
8879 && it->c != '\t'
8880 && it->vpos < XFASTINT (it->w->window_end_vpos))
8881 {
8882 it->continuation_lines_width += it->current_x;
8883 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8884 it->current_y += it->max_ascent + it->max_descent;
8885 ++it->vpos;
8886 last_height = it->max_ascent + it->max_descent;
8887 last_max_ascent = it->max_ascent;
8888 }
8889
8890 if (backup_data)
8891 bidi_unshelve_cache (backup_data, 1);
8892
8893 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8894 }
8895
8896
8897 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8898
8899 If DY > 0, move IT backward at least that many pixels. DY = 0
8900 means move IT backward to the preceding line start or BEGV. This
8901 function may move over more than DY pixels if IT->current_y - DY
8902 ends up in the middle of a line; in this case IT->current_y will be
8903 set to the top of the line moved to. */
8904
8905 void
8906 move_it_vertically_backward (struct it *it, int dy)
8907 {
8908 int nlines, h;
8909 struct it it2, it3;
8910 void *it2data = NULL, *it3data = NULL;
8911 ptrdiff_t start_pos;
8912
8913 move_further_back:
8914 eassert (dy >= 0);
8915
8916 start_pos = IT_CHARPOS (*it);
8917
8918 /* Estimate how many newlines we must move back. */
8919 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8920
8921 /* Set the iterator's position that many lines back. */
8922 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8923 back_to_previous_visible_line_start (it);
8924
8925 /* Reseat the iterator here. When moving backward, we don't want
8926 reseat to skip forward over invisible text, set up the iterator
8927 to deliver from overlay strings at the new position etc. So,
8928 use reseat_1 here. */
8929 reseat_1 (it, it->current.pos, 1);
8930
8931 /* We are now surely at a line start. */
8932 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8933 reordering is in effect. */
8934 it->continuation_lines_width = 0;
8935
8936 /* Move forward and see what y-distance we moved. First move to the
8937 start of the next line so that we get its height. We need this
8938 height to be able to tell whether we reached the specified
8939 y-distance. */
8940 SAVE_IT (it2, *it, it2data);
8941 it2.max_ascent = it2.max_descent = 0;
8942 do
8943 {
8944 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8945 MOVE_TO_POS | MOVE_TO_VPOS);
8946 }
8947 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8948 /* If we are in a display string which starts at START_POS,
8949 and that display string includes a newline, and we are
8950 right after that newline (i.e. at the beginning of a
8951 display line), exit the loop, because otherwise we will
8952 infloop, since move_it_to will see that it is already at
8953 START_POS and will not move. */
8954 || (it2.method == GET_FROM_STRING
8955 && IT_CHARPOS (it2) == start_pos
8956 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8957 eassert (IT_CHARPOS (*it) >= BEGV);
8958 SAVE_IT (it3, it2, it3data);
8959
8960 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8961 eassert (IT_CHARPOS (*it) >= BEGV);
8962 /* H is the actual vertical distance from the position in *IT
8963 and the starting position. */
8964 h = it2.current_y - it->current_y;
8965 /* NLINES is the distance in number of lines. */
8966 nlines = it2.vpos - it->vpos;
8967
8968 /* Correct IT's y and vpos position
8969 so that they are relative to the starting point. */
8970 it->vpos -= nlines;
8971 it->current_y -= h;
8972
8973 if (dy == 0)
8974 {
8975 /* DY == 0 means move to the start of the screen line. The
8976 value of nlines is > 0 if continuation lines were involved,
8977 or if the original IT position was at start of a line. */
8978 RESTORE_IT (it, it, it2data);
8979 if (nlines > 0)
8980 move_it_by_lines (it, nlines);
8981 /* The above code moves us to some position NLINES down,
8982 usually to its first glyph (leftmost in an L2R line), but
8983 that's not necessarily the start of the line, under bidi
8984 reordering. We want to get to the character position
8985 that is immediately after the newline of the previous
8986 line. */
8987 if (it->bidi_p
8988 && !it->continuation_lines_width
8989 && !STRINGP (it->string)
8990 && IT_CHARPOS (*it) > BEGV
8991 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8992 {
8993 ptrdiff_t nl_pos =
8994 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8995
8996 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8997 }
8998 bidi_unshelve_cache (it3data, 1);
8999 }
9000 else
9001 {
9002 /* The y-position we try to reach, relative to *IT.
9003 Note that H has been subtracted in front of the if-statement. */
9004 int target_y = it->current_y + h - dy;
9005 int y0 = it3.current_y;
9006 int y1;
9007 int line_height;
9008
9009 RESTORE_IT (&it3, &it3, it3data);
9010 y1 = line_bottom_y (&it3);
9011 line_height = y1 - y0;
9012 RESTORE_IT (it, it, it2data);
9013 /* If we did not reach target_y, try to move further backward if
9014 we can. If we moved too far backward, try to move forward. */
9015 if (target_y < it->current_y
9016 /* This is heuristic. In a window that's 3 lines high, with
9017 a line height of 13 pixels each, recentering with point
9018 on the bottom line will try to move -39/2 = 19 pixels
9019 backward. Try to avoid moving into the first line. */
9020 && (it->current_y - target_y
9021 > min (window_box_height (it->w), line_height * 2 / 3))
9022 && IT_CHARPOS (*it) > BEGV)
9023 {
9024 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9025 target_y - it->current_y));
9026 dy = it->current_y - target_y;
9027 goto move_further_back;
9028 }
9029 else if (target_y >= it->current_y + line_height
9030 && IT_CHARPOS (*it) < ZV)
9031 {
9032 /* Should move forward by at least one line, maybe more.
9033
9034 Note: Calling move_it_by_lines can be expensive on
9035 terminal frames, where compute_motion is used (via
9036 vmotion) to do the job, when there are very long lines
9037 and truncate-lines is nil. That's the reason for
9038 treating terminal frames specially here. */
9039
9040 if (!FRAME_WINDOW_P (it->f))
9041 move_it_vertically (it, target_y - (it->current_y + line_height));
9042 else
9043 {
9044 do
9045 {
9046 move_it_by_lines (it, 1);
9047 }
9048 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9049 }
9050 }
9051 }
9052 }
9053
9054
9055 /* Move IT by a specified amount of pixel lines DY. DY negative means
9056 move backwards. DY = 0 means move to start of screen line. At the
9057 end, IT will be on the start of a screen line. */
9058
9059 void
9060 move_it_vertically (struct it *it, int dy)
9061 {
9062 if (dy <= 0)
9063 move_it_vertically_backward (it, -dy);
9064 else
9065 {
9066 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9067 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9068 MOVE_TO_POS | MOVE_TO_Y);
9069 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9070
9071 /* If buffer ends in ZV without a newline, move to the start of
9072 the line to satisfy the post-condition. */
9073 if (IT_CHARPOS (*it) == ZV
9074 && ZV > BEGV
9075 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9076 move_it_by_lines (it, 0);
9077 }
9078 }
9079
9080
9081 /* Move iterator IT past the end of the text line it is in. */
9082
9083 void
9084 move_it_past_eol (struct it *it)
9085 {
9086 enum move_it_result rc;
9087
9088 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9089 if (rc == MOVE_NEWLINE_OR_CR)
9090 set_iterator_to_next (it, 0);
9091 }
9092
9093
9094 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9095 negative means move up. DVPOS == 0 means move to the start of the
9096 screen line.
9097
9098 Optimization idea: If we would know that IT->f doesn't use
9099 a face with proportional font, we could be faster for
9100 truncate-lines nil. */
9101
9102 void
9103 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9104 {
9105
9106 /* The commented-out optimization uses vmotion on terminals. This
9107 gives bad results, because elements like it->what, on which
9108 callers such as pos_visible_p rely, aren't updated. */
9109 /* struct position pos;
9110 if (!FRAME_WINDOW_P (it->f))
9111 {
9112 struct text_pos textpos;
9113
9114 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9115 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9116 reseat (it, textpos, 1);
9117 it->vpos += pos.vpos;
9118 it->current_y += pos.vpos;
9119 }
9120 else */
9121
9122 if (dvpos == 0)
9123 {
9124 /* DVPOS == 0 means move to the start of the screen line. */
9125 move_it_vertically_backward (it, 0);
9126 /* Let next call to line_bottom_y calculate real line height */
9127 last_height = 0;
9128 }
9129 else if (dvpos > 0)
9130 {
9131 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9132 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9133 {
9134 /* Only move to the next buffer position if we ended up in a
9135 string from display property, not in an overlay string
9136 (before-string or after-string). That is because the
9137 latter don't conceal the underlying buffer position, so
9138 we can ask to move the iterator to the exact position we
9139 are interested in. Note that, even if we are already at
9140 IT_CHARPOS (*it), the call below is not a no-op, as it
9141 will detect that we are at the end of the string, pop the
9142 iterator, and compute it->current_x and it->hpos
9143 correctly. */
9144 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9145 -1, -1, -1, MOVE_TO_POS);
9146 }
9147 }
9148 else
9149 {
9150 struct it it2;
9151 void *it2data = NULL;
9152 ptrdiff_t start_charpos, i;
9153
9154 /* Start at the beginning of the screen line containing IT's
9155 position. This may actually move vertically backwards,
9156 in case of overlays, so adjust dvpos accordingly. */
9157 dvpos += it->vpos;
9158 move_it_vertically_backward (it, 0);
9159 dvpos -= it->vpos;
9160
9161 /* Go back -DVPOS visible lines and reseat the iterator there. */
9162 start_charpos = IT_CHARPOS (*it);
9163 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9164 back_to_previous_visible_line_start (it);
9165 reseat (it, it->current.pos, 1);
9166
9167 /* Move further back if we end up in a string or an image. */
9168 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9169 {
9170 /* First try to move to start of display line. */
9171 dvpos += it->vpos;
9172 move_it_vertically_backward (it, 0);
9173 dvpos -= it->vpos;
9174 if (IT_POS_VALID_AFTER_MOVE_P (it))
9175 break;
9176 /* If start of line is still in string or image,
9177 move further back. */
9178 back_to_previous_visible_line_start (it);
9179 reseat (it, it->current.pos, 1);
9180 dvpos--;
9181 }
9182
9183 it->current_x = it->hpos = 0;
9184
9185 /* Above call may have moved too far if continuation lines
9186 are involved. Scan forward and see if it did. */
9187 SAVE_IT (it2, *it, it2data);
9188 it2.vpos = it2.current_y = 0;
9189 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9190 it->vpos -= it2.vpos;
9191 it->current_y -= it2.current_y;
9192 it->current_x = it->hpos = 0;
9193
9194 /* If we moved too far back, move IT some lines forward. */
9195 if (it2.vpos > -dvpos)
9196 {
9197 int delta = it2.vpos + dvpos;
9198
9199 RESTORE_IT (&it2, &it2, it2data);
9200 SAVE_IT (it2, *it, it2data);
9201 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9202 /* Move back again if we got too far ahead. */
9203 if (IT_CHARPOS (*it) >= start_charpos)
9204 RESTORE_IT (it, &it2, it2data);
9205 else
9206 bidi_unshelve_cache (it2data, 1);
9207 }
9208 else
9209 RESTORE_IT (it, it, it2data);
9210 }
9211 }
9212
9213 /* Return 1 if IT points into the middle of a display vector. */
9214
9215 int
9216 in_display_vector_p (struct it *it)
9217 {
9218 return (it->method == GET_FROM_DISPLAY_VECTOR
9219 && it->current.dpvec_index > 0
9220 && it->dpvec + it->current.dpvec_index != it->dpend);
9221 }
9222
9223 \f
9224 /***********************************************************************
9225 Messages
9226 ***********************************************************************/
9227
9228
9229 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9230 to *Messages*. */
9231
9232 void
9233 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9234 {
9235 Lisp_Object args[3];
9236 Lisp_Object msg, fmt;
9237 char *buffer;
9238 ptrdiff_t len;
9239 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9240 USE_SAFE_ALLOCA;
9241
9242 /* Do nothing if called asynchronously. Inserting text into
9243 a buffer may call after-change-functions and alike and
9244 that would means running Lisp asynchronously. */
9245 if (handling_signal)
9246 return;
9247
9248 fmt = msg = Qnil;
9249 GCPRO4 (fmt, msg, arg1, arg2);
9250
9251 args[0] = fmt = build_string (format);
9252 args[1] = arg1;
9253 args[2] = arg2;
9254 msg = Fformat (3, args);
9255
9256 len = SBYTES (msg) + 1;
9257 SAFE_ALLOCA (buffer, char *, len);
9258 memcpy (buffer, SDATA (msg), len);
9259
9260 message_dolog (buffer, len - 1, 1, 0);
9261 SAFE_FREE ();
9262
9263 UNGCPRO;
9264 }
9265
9266
9267 /* Output a newline in the *Messages* buffer if "needs" one. */
9268
9269 void
9270 message_log_maybe_newline (void)
9271 {
9272 if (message_log_need_newline)
9273 message_dolog ("", 0, 1, 0);
9274 }
9275
9276
9277 /* Add a string M of length NBYTES to the message log, optionally
9278 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9279 nonzero, means interpret the contents of M as multibyte. This
9280 function calls low-level routines in order to bypass text property
9281 hooks, etc. which might not be safe to run.
9282
9283 This may GC (insert may run before/after change hooks),
9284 so the buffer M must NOT point to a Lisp string. */
9285
9286 void
9287 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9288 {
9289 const unsigned char *msg = (const unsigned char *) m;
9290
9291 if (!NILP (Vmemory_full))
9292 return;
9293
9294 if (!NILP (Vmessage_log_max))
9295 {
9296 struct buffer *oldbuf;
9297 Lisp_Object oldpoint, oldbegv, oldzv;
9298 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9299 ptrdiff_t point_at_end = 0;
9300 ptrdiff_t zv_at_end = 0;
9301 Lisp_Object old_deactivate_mark, tem;
9302 struct gcpro gcpro1;
9303
9304 old_deactivate_mark = Vdeactivate_mark;
9305 oldbuf = current_buffer;
9306 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9307 BVAR (current_buffer, undo_list) = Qt;
9308
9309 oldpoint = message_dolog_marker1;
9310 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9311 oldbegv = message_dolog_marker2;
9312 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9313 oldzv = message_dolog_marker3;
9314 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9315 GCPRO1 (old_deactivate_mark);
9316
9317 if (PT == Z)
9318 point_at_end = 1;
9319 if (ZV == Z)
9320 zv_at_end = 1;
9321
9322 BEGV = BEG;
9323 BEGV_BYTE = BEG_BYTE;
9324 ZV = Z;
9325 ZV_BYTE = Z_BYTE;
9326 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9327
9328 /* Insert the string--maybe converting multibyte to single byte
9329 or vice versa, so that all the text fits the buffer. */
9330 if (multibyte
9331 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9332 {
9333 ptrdiff_t i;
9334 int c, char_bytes;
9335 char work[1];
9336
9337 /* Convert a multibyte string to single-byte
9338 for the *Message* buffer. */
9339 for (i = 0; i < nbytes; i += char_bytes)
9340 {
9341 c = string_char_and_length (msg + i, &char_bytes);
9342 work[0] = (ASCII_CHAR_P (c)
9343 ? c
9344 : multibyte_char_to_unibyte (c));
9345 insert_1_both (work, 1, 1, 1, 0, 0);
9346 }
9347 }
9348 else if (! multibyte
9349 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9350 {
9351 ptrdiff_t i;
9352 int c, char_bytes;
9353 unsigned char str[MAX_MULTIBYTE_LENGTH];
9354 /* Convert a single-byte string to multibyte
9355 for the *Message* buffer. */
9356 for (i = 0; i < nbytes; i++)
9357 {
9358 c = msg[i];
9359 MAKE_CHAR_MULTIBYTE (c);
9360 char_bytes = CHAR_STRING (c, str);
9361 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9362 }
9363 }
9364 else if (nbytes)
9365 insert_1 (m, nbytes, 1, 0, 0);
9366
9367 if (nlflag)
9368 {
9369 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9370 printmax_t dups;
9371 insert_1 ("\n", 1, 1, 0, 0);
9372
9373 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9374 this_bol = PT;
9375 this_bol_byte = PT_BYTE;
9376
9377 /* See if this line duplicates the previous one.
9378 If so, combine duplicates. */
9379 if (this_bol > BEG)
9380 {
9381 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9382 prev_bol = PT;
9383 prev_bol_byte = PT_BYTE;
9384
9385 dups = message_log_check_duplicate (prev_bol_byte,
9386 this_bol_byte);
9387 if (dups)
9388 {
9389 del_range_both (prev_bol, prev_bol_byte,
9390 this_bol, this_bol_byte, 0);
9391 if (dups > 1)
9392 {
9393 char dupstr[sizeof " [ times]"
9394 + INT_STRLEN_BOUND (printmax_t)];
9395
9396 /* If you change this format, don't forget to also
9397 change message_log_check_duplicate. */
9398 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9399 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9400 insert_1 (dupstr, duplen, 1, 0, 1);
9401 }
9402 }
9403 }
9404
9405 /* If we have more than the desired maximum number of lines
9406 in the *Messages* buffer now, delete the oldest ones.
9407 This is safe because we don't have undo in this buffer. */
9408
9409 if (NATNUMP (Vmessage_log_max))
9410 {
9411 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9412 -XFASTINT (Vmessage_log_max) - 1, 0);
9413 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9414 }
9415 }
9416 BEGV = XMARKER (oldbegv)->charpos;
9417 BEGV_BYTE = marker_byte_position (oldbegv);
9418
9419 if (zv_at_end)
9420 {
9421 ZV = Z;
9422 ZV_BYTE = Z_BYTE;
9423 }
9424 else
9425 {
9426 ZV = XMARKER (oldzv)->charpos;
9427 ZV_BYTE = marker_byte_position (oldzv);
9428 }
9429
9430 if (point_at_end)
9431 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9432 else
9433 /* We can't do Fgoto_char (oldpoint) because it will run some
9434 Lisp code. */
9435 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9436 XMARKER (oldpoint)->bytepos);
9437
9438 UNGCPRO;
9439 unchain_marker (XMARKER (oldpoint));
9440 unchain_marker (XMARKER (oldbegv));
9441 unchain_marker (XMARKER (oldzv));
9442
9443 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9444 set_buffer_internal (oldbuf);
9445 if (NILP (tem))
9446 windows_or_buffers_changed = old_windows_or_buffers_changed;
9447 message_log_need_newline = !nlflag;
9448 Vdeactivate_mark = old_deactivate_mark;
9449 }
9450 }
9451
9452
9453 /* We are at the end of the buffer after just having inserted a newline.
9454 (Note: We depend on the fact we won't be crossing the gap.)
9455 Check to see if the most recent message looks a lot like the previous one.
9456 Return 0 if different, 1 if the new one should just replace it, or a
9457 value N > 1 if we should also append " [N times]". */
9458
9459 static intmax_t
9460 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9461 {
9462 ptrdiff_t i;
9463 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9464 int seen_dots = 0;
9465 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9466 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9467
9468 for (i = 0; i < len; i++)
9469 {
9470 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9471 seen_dots = 1;
9472 if (p1[i] != p2[i])
9473 return seen_dots;
9474 }
9475 p1 += len;
9476 if (*p1 == '\n')
9477 return 2;
9478 if (*p1++ == ' ' && *p1++ == '[')
9479 {
9480 char *pend;
9481 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9482 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9483 return n+1;
9484 }
9485 return 0;
9486 }
9487 \f
9488
9489 /* Display an echo area message M with a specified length of NBYTES
9490 bytes. The string may include null characters. If M is 0, clear
9491 out any existing message, and let the mini-buffer text show
9492 through.
9493
9494 This may GC, so the buffer M must NOT point to a Lisp string. */
9495
9496 void
9497 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9498 {
9499 /* First flush out any partial line written with print. */
9500 message_log_maybe_newline ();
9501 if (m)
9502 message_dolog (m, nbytes, 1, multibyte);
9503 message2_nolog (m, nbytes, multibyte);
9504 }
9505
9506
9507 /* The non-logging counterpart of message2. */
9508
9509 void
9510 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9511 {
9512 struct frame *sf = SELECTED_FRAME ();
9513 message_enable_multibyte = multibyte;
9514
9515 if (FRAME_INITIAL_P (sf))
9516 {
9517 if (noninteractive_need_newline)
9518 putc ('\n', stderr);
9519 noninteractive_need_newline = 0;
9520 if (m)
9521 fwrite (m, nbytes, 1, stderr);
9522 if (cursor_in_echo_area == 0)
9523 fprintf (stderr, "\n");
9524 fflush (stderr);
9525 }
9526 /* A null message buffer means that the frame hasn't really been
9527 initialized yet. Error messages get reported properly by
9528 cmd_error, so this must be just an informative message; toss it. */
9529 else if (INTERACTIVE
9530 && sf->glyphs_initialized_p
9531 && FRAME_MESSAGE_BUF (sf))
9532 {
9533 Lisp_Object mini_window;
9534 struct frame *f;
9535
9536 /* Get the frame containing the mini-buffer
9537 that the selected frame is using. */
9538 mini_window = FRAME_MINIBUF_WINDOW (sf);
9539 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9540
9541 FRAME_SAMPLE_VISIBILITY (f);
9542 if (FRAME_VISIBLE_P (sf)
9543 && ! FRAME_VISIBLE_P (f))
9544 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9545
9546 if (m)
9547 {
9548 set_message (m, Qnil, nbytes, multibyte);
9549 if (minibuffer_auto_raise)
9550 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9551 }
9552 else
9553 clear_message (1, 1);
9554
9555 do_pending_window_change (0);
9556 echo_area_display (1);
9557 do_pending_window_change (0);
9558 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9559 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9560 }
9561 }
9562
9563
9564 /* Display an echo area message M with a specified length of NBYTES
9565 bytes. The string may include null characters. If M is not a
9566 string, clear out any existing message, and let the mini-buffer
9567 text show through.
9568
9569 This function cancels echoing. */
9570
9571 void
9572 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9573 {
9574 struct gcpro gcpro1;
9575
9576 GCPRO1 (m);
9577 clear_message (1,1);
9578 cancel_echoing ();
9579
9580 /* First flush out any partial line written with print. */
9581 message_log_maybe_newline ();
9582 if (STRINGP (m))
9583 {
9584 char *buffer;
9585 USE_SAFE_ALLOCA;
9586
9587 SAFE_ALLOCA (buffer, char *, nbytes);
9588 memcpy (buffer, SDATA (m), nbytes);
9589 message_dolog (buffer, nbytes, 1, multibyte);
9590 SAFE_FREE ();
9591 }
9592 message3_nolog (m, nbytes, multibyte);
9593
9594 UNGCPRO;
9595 }
9596
9597
9598 /* The non-logging version of message3.
9599 This does not cancel echoing, because it is used for echoing.
9600 Perhaps we need to make a separate function for echoing
9601 and make this cancel echoing. */
9602
9603 void
9604 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9605 {
9606 struct frame *sf = SELECTED_FRAME ();
9607 message_enable_multibyte = multibyte;
9608
9609 if (FRAME_INITIAL_P (sf))
9610 {
9611 if (noninteractive_need_newline)
9612 putc ('\n', stderr);
9613 noninteractive_need_newline = 0;
9614 if (STRINGP (m))
9615 fwrite (SDATA (m), nbytes, 1, stderr);
9616 if (cursor_in_echo_area == 0)
9617 fprintf (stderr, "\n");
9618 fflush (stderr);
9619 }
9620 /* A null message buffer means that the frame hasn't really been
9621 initialized yet. Error messages get reported properly by
9622 cmd_error, so this must be just an informative message; toss it. */
9623 else if (INTERACTIVE
9624 && sf->glyphs_initialized_p
9625 && FRAME_MESSAGE_BUF (sf))
9626 {
9627 Lisp_Object mini_window;
9628 Lisp_Object frame;
9629 struct frame *f;
9630
9631 /* Get the frame containing the mini-buffer
9632 that the selected frame is using. */
9633 mini_window = FRAME_MINIBUF_WINDOW (sf);
9634 frame = XWINDOW (mini_window)->frame;
9635 f = XFRAME (frame);
9636
9637 FRAME_SAMPLE_VISIBILITY (f);
9638 if (FRAME_VISIBLE_P (sf)
9639 && !FRAME_VISIBLE_P (f))
9640 Fmake_frame_visible (frame);
9641
9642 if (STRINGP (m) && SCHARS (m) > 0)
9643 {
9644 set_message (NULL, m, nbytes, multibyte);
9645 if (minibuffer_auto_raise)
9646 Fraise_frame (frame);
9647 /* Assume we are not echoing.
9648 (If we are, echo_now will override this.) */
9649 echo_message_buffer = Qnil;
9650 }
9651 else
9652 clear_message (1, 1);
9653
9654 do_pending_window_change (0);
9655 echo_area_display (1);
9656 do_pending_window_change (0);
9657 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9658 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9659 }
9660 }
9661
9662
9663 /* Display a null-terminated echo area message M. If M is 0, clear
9664 out any existing message, and let the mini-buffer text show through.
9665
9666 The buffer M must continue to exist until after the echo area gets
9667 cleared or some other message gets displayed there. Do not pass
9668 text that is stored in a Lisp string. Do not pass text in a buffer
9669 that was alloca'd. */
9670
9671 void
9672 message1 (const char *m)
9673 {
9674 message2 (m, (m ? strlen (m) : 0), 0);
9675 }
9676
9677
9678 /* The non-logging counterpart of message1. */
9679
9680 void
9681 message1_nolog (const char *m)
9682 {
9683 message2_nolog (m, (m ? strlen (m) : 0), 0);
9684 }
9685
9686 /* Display a message M which contains a single %s
9687 which gets replaced with STRING. */
9688
9689 void
9690 message_with_string (const char *m, Lisp_Object string, int log)
9691 {
9692 CHECK_STRING (string);
9693
9694 if (noninteractive)
9695 {
9696 if (m)
9697 {
9698 if (noninteractive_need_newline)
9699 putc ('\n', stderr);
9700 noninteractive_need_newline = 0;
9701 fprintf (stderr, m, SDATA (string));
9702 if (!cursor_in_echo_area)
9703 fprintf (stderr, "\n");
9704 fflush (stderr);
9705 }
9706 }
9707 else if (INTERACTIVE)
9708 {
9709 /* The frame whose minibuffer we're going to display the message on.
9710 It may be larger than the selected frame, so we need
9711 to use its buffer, not the selected frame's buffer. */
9712 Lisp_Object mini_window;
9713 struct frame *f, *sf = SELECTED_FRAME ();
9714
9715 /* Get the frame containing the minibuffer
9716 that the selected frame is using. */
9717 mini_window = FRAME_MINIBUF_WINDOW (sf);
9718 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9719
9720 /* A null message buffer means that the frame hasn't really been
9721 initialized yet. Error messages get reported properly by
9722 cmd_error, so this must be just an informative message; toss it. */
9723 if (FRAME_MESSAGE_BUF (f))
9724 {
9725 Lisp_Object args[2], msg;
9726 struct gcpro gcpro1, gcpro2;
9727
9728 args[0] = build_string (m);
9729 args[1] = msg = string;
9730 GCPRO2 (args[0], msg);
9731 gcpro1.nvars = 2;
9732
9733 msg = Fformat (2, args);
9734
9735 if (log)
9736 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9737 else
9738 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9739
9740 UNGCPRO;
9741
9742 /* Print should start at the beginning of the message
9743 buffer next time. */
9744 message_buf_print = 0;
9745 }
9746 }
9747 }
9748
9749
9750 /* Dump an informative message to the minibuf. If M is 0, clear out
9751 any existing message, and let the mini-buffer text show through. */
9752
9753 static void
9754 vmessage (const char *m, va_list ap)
9755 {
9756 if (noninteractive)
9757 {
9758 if (m)
9759 {
9760 if (noninteractive_need_newline)
9761 putc ('\n', stderr);
9762 noninteractive_need_newline = 0;
9763 vfprintf (stderr, m, ap);
9764 if (cursor_in_echo_area == 0)
9765 fprintf (stderr, "\n");
9766 fflush (stderr);
9767 }
9768 }
9769 else if (INTERACTIVE)
9770 {
9771 /* The frame whose mini-buffer we're going to display the message
9772 on. It may be larger than the selected frame, so we need to
9773 use its buffer, not the selected frame's buffer. */
9774 Lisp_Object mini_window;
9775 struct frame *f, *sf = SELECTED_FRAME ();
9776
9777 /* Get the frame containing the mini-buffer
9778 that the selected frame is using. */
9779 mini_window = FRAME_MINIBUF_WINDOW (sf);
9780 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9781
9782 /* A null message buffer means that the frame hasn't really been
9783 initialized yet. Error messages get reported properly by
9784 cmd_error, so this must be just an informative message; toss
9785 it. */
9786 if (FRAME_MESSAGE_BUF (f))
9787 {
9788 if (m)
9789 {
9790 ptrdiff_t len;
9791
9792 len = doprnt (FRAME_MESSAGE_BUF (f),
9793 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9794
9795 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9796 }
9797 else
9798 message1 (0);
9799
9800 /* Print should start at the beginning of the message
9801 buffer next time. */
9802 message_buf_print = 0;
9803 }
9804 }
9805 }
9806
9807 void
9808 message (const char *m, ...)
9809 {
9810 va_list ap;
9811 va_start (ap, m);
9812 vmessage (m, ap);
9813 va_end (ap);
9814 }
9815
9816
9817 #if 0
9818 /* The non-logging version of message. */
9819
9820 void
9821 message_nolog (const char *m, ...)
9822 {
9823 Lisp_Object old_log_max;
9824 va_list ap;
9825 va_start (ap, m);
9826 old_log_max = Vmessage_log_max;
9827 Vmessage_log_max = Qnil;
9828 vmessage (m, ap);
9829 Vmessage_log_max = old_log_max;
9830 va_end (ap);
9831 }
9832 #endif
9833
9834
9835 /* Display the current message in the current mini-buffer. This is
9836 only called from error handlers in process.c, and is not time
9837 critical. */
9838
9839 void
9840 update_echo_area (void)
9841 {
9842 if (!NILP (echo_area_buffer[0]))
9843 {
9844 Lisp_Object string;
9845 string = Fcurrent_message ();
9846 message3 (string, SBYTES (string),
9847 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9848 }
9849 }
9850
9851
9852 /* Make sure echo area buffers in `echo_buffers' are live.
9853 If they aren't, make new ones. */
9854
9855 static void
9856 ensure_echo_area_buffers (void)
9857 {
9858 int i;
9859
9860 for (i = 0; i < 2; ++i)
9861 if (!BUFFERP (echo_buffer[i])
9862 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9863 {
9864 char name[30];
9865 Lisp_Object old_buffer;
9866 int j;
9867
9868 old_buffer = echo_buffer[i];
9869 sprintf (name, " *Echo Area %d*", i);
9870 echo_buffer[i] = Fget_buffer_create (build_string (name));
9871 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9872 /* to force word wrap in echo area -
9873 it was decided to postpone this*/
9874 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9875
9876 for (j = 0; j < 2; ++j)
9877 if (EQ (old_buffer, echo_area_buffer[j]))
9878 echo_area_buffer[j] = echo_buffer[i];
9879 }
9880 }
9881
9882
9883 /* Call FN with args A1..A4 with either the current or last displayed
9884 echo_area_buffer as current buffer.
9885
9886 WHICH zero means use the current message buffer
9887 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9888 from echo_buffer[] and clear it.
9889
9890 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9891 suitable buffer from echo_buffer[] and clear it.
9892
9893 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9894 that the current message becomes the last displayed one, make
9895 choose a suitable buffer for echo_area_buffer[0], and clear it.
9896
9897 Value is what FN returns. */
9898
9899 static int
9900 with_echo_area_buffer (struct window *w, int which,
9901 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9902 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9903 {
9904 Lisp_Object buffer;
9905 int this_one, the_other, clear_buffer_p, rc;
9906 ptrdiff_t count = SPECPDL_INDEX ();
9907
9908 /* If buffers aren't live, make new ones. */
9909 ensure_echo_area_buffers ();
9910
9911 clear_buffer_p = 0;
9912
9913 if (which == 0)
9914 this_one = 0, the_other = 1;
9915 else if (which > 0)
9916 this_one = 1, the_other = 0;
9917 else
9918 {
9919 this_one = 0, the_other = 1;
9920 clear_buffer_p = 1;
9921
9922 /* We need a fresh one in case the current echo buffer equals
9923 the one containing the last displayed echo area message. */
9924 if (!NILP (echo_area_buffer[this_one])
9925 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9926 echo_area_buffer[this_one] = Qnil;
9927 }
9928
9929 /* Choose a suitable buffer from echo_buffer[] is we don't
9930 have one. */
9931 if (NILP (echo_area_buffer[this_one]))
9932 {
9933 echo_area_buffer[this_one]
9934 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9935 ? echo_buffer[the_other]
9936 : echo_buffer[this_one]);
9937 clear_buffer_p = 1;
9938 }
9939
9940 buffer = echo_area_buffer[this_one];
9941
9942 /* Don't get confused by reusing the buffer used for echoing
9943 for a different purpose. */
9944 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9945 cancel_echoing ();
9946
9947 record_unwind_protect (unwind_with_echo_area_buffer,
9948 with_echo_area_buffer_unwind_data (w));
9949
9950 /* Make the echo area buffer current. Note that for display
9951 purposes, it is not necessary that the displayed window's buffer
9952 == current_buffer, except for text property lookup. So, let's
9953 only set that buffer temporarily here without doing a full
9954 Fset_window_buffer. We must also change w->pointm, though,
9955 because otherwise an assertions in unshow_buffer fails, and Emacs
9956 aborts. */
9957 set_buffer_internal_1 (XBUFFER (buffer));
9958 if (w)
9959 {
9960 w->buffer = buffer;
9961 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9962 }
9963
9964 BVAR (current_buffer, undo_list) = Qt;
9965 BVAR (current_buffer, read_only) = Qnil;
9966 specbind (Qinhibit_read_only, Qt);
9967 specbind (Qinhibit_modification_hooks, Qt);
9968
9969 if (clear_buffer_p && Z > BEG)
9970 del_range (BEG, Z);
9971
9972 eassert (BEGV >= BEG);
9973 eassert (ZV <= Z && ZV >= BEGV);
9974
9975 rc = fn (a1, a2, a3, a4);
9976
9977 eassert (BEGV >= BEG);
9978 eassert (ZV <= Z && ZV >= BEGV);
9979
9980 unbind_to (count, Qnil);
9981 return rc;
9982 }
9983
9984
9985 /* Save state that should be preserved around the call to the function
9986 FN called in with_echo_area_buffer. */
9987
9988 static Lisp_Object
9989 with_echo_area_buffer_unwind_data (struct window *w)
9990 {
9991 int i = 0;
9992 Lisp_Object vector, tmp;
9993
9994 /* Reduce consing by keeping one vector in
9995 Vwith_echo_area_save_vector. */
9996 vector = Vwith_echo_area_save_vector;
9997 Vwith_echo_area_save_vector = Qnil;
9998
9999 if (NILP (vector))
10000 vector = Fmake_vector (make_number (7), Qnil);
10001
10002 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10003 ASET (vector, i, Vdeactivate_mark); ++i;
10004 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10005
10006 if (w)
10007 {
10008 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10009 ASET (vector, i, w->buffer); ++i;
10010 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10011 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10012 }
10013 else
10014 {
10015 int end = i + 4;
10016 for (; i < end; ++i)
10017 ASET (vector, i, Qnil);
10018 }
10019
10020 eassert (i == ASIZE (vector));
10021 return vector;
10022 }
10023
10024
10025 /* Restore global state from VECTOR which was created by
10026 with_echo_area_buffer_unwind_data. */
10027
10028 static Lisp_Object
10029 unwind_with_echo_area_buffer (Lisp_Object vector)
10030 {
10031 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10032 Vdeactivate_mark = AREF (vector, 1);
10033 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10034
10035 if (WINDOWP (AREF (vector, 3)))
10036 {
10037 struct window *w;
10038 Lisp_Object buffer, charpos, bytepos;
10039
10040 w = XWINDOW (AREF (vector, 3));
10041 buffer = AREF (vector, 4);
10042 charpos = AREF (vector, 5);
10043 bytepos = AREF (vector, 6);
10044
10045 w->buffer = buffer;
10046 set_marker_both (w->pointm, buffer,
10047 XFASTINT (charpos), XFASTINT (bytepos));
10048 }
10049
10050 Vwith_echo_area_save_vector = vector;
10051 return Qnil;
10052 }
10053
10054
10055 /* Set up the echo area for use by print functions. MULTIBYTE_P
10056 non-zero means we will print multibyte. */
10057
10058 void
10059 setup_echo_area_for_printing (int multibyte_p)
10060 {
10061 /* If we can't find an echo area any more, exit. */
10062 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10063 Fkill_emacs (Qnil);
10064
10065 ensure_echo_area_buffers ();
10066
10067 if (!message_buf_print)
10068 {
10069 /* A message has been output since the last time we printed.
10070 Choose a fresh echo area buffer. */
10071 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10072 echo_area_buffer[0] = echo_buffer[1];
10073 else
10074 echo_area_buffer[0] = echo_buffer[0];
10075
10076 /* Switch to that buffer and clear it. */
10077 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10078 BVAR (current_buffer, truncate_lines) = Qnil;
10079
10080 if (Z > BEG)
10081 {
10082 ptrdiff_t count = SPECPDL_INDEX ();
10083 specbind (Qinhibit_read_only, Qt);
10084 /* Note that undo recording is always disabled. */
10085 del_range (BEG, Z);
10086 unbind_to (count, Qnil);
10087 }
10088 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10089
10090 /* Set up the buffer for the multibyteness we need. */
10091 if (multibyte_p
10092 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10093 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10094
10095 /* Raise the frame containing the echo area. */
10096 if (minibuffer_auto_raise)
10097 {
10098 struct frame *sf = SELECTED_FRAME ();
10099 Lisp_Object mini_window;
10100 mini_window = FRAME_MINIBUF_WINDOW (sf);
10101 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10102 }
10103
10104 message_log_maybe_newline ();
10105 message_buf_print = 1;
10106 }
10107 else
10108 {
10109 if (NILP (echo_area_buffer[0]))
10110 {
10111 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10112 echo_area_buffer[0] = echo_buffer[1];
10113 else
10114 echo_area_buffer[0] = echo_buffer[0];
10115 }
10116
10117 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10118 {
10119 /* Someone switched buffers between print requests. */
10120 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10121 BVAR (current_buffer, truncate_lines) = Qnil;
10122 }
10123 }
10124 }
10125
10126
10127 /* Display an echo area message in window W. Value is non-zero if W's
10128 height is changed. If display_last_displayed_message_p is
10129 non-zero, display the message that was last displayed, otherwise
10130 display the current message. */
10131
10132 static int
10133 display_echo_area (struct window *w)
10134 {
10135 int i, no_message_p, window_height_changed_p;
10136
10137 /* Temporarily disable garbage collections while displaying the echo
10138 area. This is done because a GC can print a message itself.
10139 That message would modify the echo area buffer's contents while a
10140 redisplay of the buffer is going on, and seriously confuse
10141 redisplay. */
10142 ptrdiff_t count = inhibit_garbage_collection ();
10143
10144 /* If there is no message, we must call display_echo_area_1
10145 nevertheless because it resizes the window. But we will have to
10146 reset the echo_area_buffer in question to nil at the end because
10147 with_echo_area_buffer will sets it to an empty buffer. */
10148 i = display_last_displayed_message_p ? 1 : 0;
10149 no_message_p = NILP (echo_area_buffer[i]);
10150
10151 window_height_changed_p
10152 = with_echo_area_buffer (w, display_last_displayed_message_p,
10153 display_echo_area_1,
10154 (intptr_t) w, Qnil, 0, 0);
10155
10156 if (no_message_p)
10157 echo_area_buffer[i] = Qnil;
10158
10159 unbind_to (count, Qnil);
10160 return window_height_changed_p;
10161 }
10162
10163
10164 /* Helper for display_echo_area. Display the current buffer which
10165 contains the current echo area message in window W, a mini-window,
10166 a pointer to which is passed in A1. A2..A4 are currently not used.
10167 Change the height of W so that all of the message is displayed.
10168 Value is non-zero if height of W was changed. */
10169
10170 static int
10171 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10172 {
10173 intptr_t i1 = a1;
10174 struct window *w = (struct window *) i1;
10175 Lisp_Object window;
10176 struct text_pos start;
10177 int window_height_changed_p = 0;
10178
10179 /* Do this before displaying, so that we have a large enough glyph
10180 matrix for the display. If we can't get enough space for the
10181 whole text, display the last N lines. That works by setting w->start. */
10182 window_height_changed_p = resize_mini_window (w, 0);
10183
10184 /* Use the starting position chosen by resize_mini_window. */
10185 SET_TEXT_POS_FROM_MARKER (start, w->start);
10186
10187 /* Display. */
10188 clear_glyph_matrix (w->desired_matrix);
10189 XSETWINDOW (window, w);
10190 try_window (window, start, 0);
10191
10192 return window_height_changed_p;
10193 }
10194
10195
10196 /* Resize the echo area window to exactly the size needed for the
10197 currently displayed message, if there is one. If a mini-buffer
10198 is active, don't shrink it. */
10199
10200 void
10201 resize_echo_area_exactly (void)
10202 {
10203 if (BUFFERP (echo_area_buffer[0])
10204 && WINDOWP (echo_area_window))
10205 {
10206 struct window *w = XWINDOW (echo_area_window);
10207 int resized_p;
10208 Lisp_Object resize_exactly;
10209
10210 if (minibuf_level == 0)
10211 resize_exactly = Qt;
10212 else
10213 resize_exactly = Qnil;
10214
10215 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10216 (intptr_t) w, resize_exactly,
10217 0, 0);
10218 if (resized_p)
10219 {
10220 ++windows_or_buffers_changed;
10221 ++update_mode_lines;
10222 redisplay_internal ();
10223 }
10224 }
10225 }
10226
10227
10228 /* Callback function for with_echo_area_buffer, when used from
10229 resize_echo_area_exactly. A1 contains a pointer to the window to
10230 resize, EXACTLY non-nil means resize the mini-window exactly to the
10231 size of the text displayed. A3 and A4 are not used. Value is what
10232 resize_mini_window returns. */
10233
10234 static int
10235 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10236 {
10237 intptr_t i1 = a1;
10238 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10239 }
10240
10241
10242 /* Resize mini-window W to fit the size of its contents. EXACT_P
10243 means size the window exactly to the size needed. Otherwise, it's
10244 only enlarged until W's buffer is empty.
10245
10246 Set W->start to the right place to begin display. If the whole
10247 contents fit, start at the beginning. Otherwise, start so as
10248 to make the end of the contents appear. This is particularly
10249 important for y-or-n-p, but seems desirable generally.
10250
10251 Value is non-zero if the window height has been changed. */
10252
10253 int
10254 resize_mini_window (struct window *w, int exact_p)
10255 {
10256 struct frame *f = XFRAME (w->frame);
10257 int window_height_changed_p = 0;
10258
10259 eassert (MINI_WINDOW_P (w));
10260
10261 /* By default, start display at the beginning. */
10262 set_marker_both (w->start, w->buffer,
10263 BUF_BEGV (XBUFFER (w->buffer)),
10264 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10265
10266 /* Don't resize windows while redisplaying a window; it would
10267 confuse redisplay functions when the size of the window they are
10268 displaying changes from under them. Such a resizing can happen,
10269 for instance, when which-func prints a long message while
10270 we are running fontification-functions. We're running these
10271 functions with safe_call which binds inhibit-redisplay to t. */
10272 if (!NILP (Vinhibit_redisplay))
10273 return 0;
10274
10275 /* Nil means don't try to resize. */
10276 if (NILP (Vresize_mini_windows)
10277 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10278 return 0;
10279
10280 if (!FRAME_MINIBUF_ONLY_P (f))
10281 {
10282 struct it it;
10283 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10284 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10285 int height;
10286 EMACS_INT max_height;
10287 int unit = FRAME_LINE_HEIGHT (f);
10288 struct text_pos start;
10289 struct buffer *old_current_buffer = NULL;
10290
10291 if (current_buffer != XBUFFER (w->buffer))
10292 {
10293 old_current_buffer = current_buffer;
10294 set_buffer_internal (XBUFFER (w->buffer));
10295 }
10296
10297 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10298
10299 /* Compute the max. number of lines specified by the user. */
10300 if (FLOATP (Vmax_mini_window_height))
10301 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10302 else if (INTEGERP (Vmax_mini_window_height))
10303 max_height = XINT (Vmax_mini_window_height);
10304 else
10305 max_height = total_height / 4;
10306
10307 /* Correct that max. height if it's bogus. */
10308 max_height = max (1, max_height);
10309 max_height = min (total_height, max_height);
10310
10311 /* Find out the height of the text in the window. */
10312 if (it.line_wrap == TRUNCATE)
10313 height = 1;
10314 else
10315 {
10316 last_height = 0;
10317 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10318 if (it.max_ascent == 0 && it.max_descent == 0)
10319 height = it.current_y + last_height;
10320 else
10321 height = it.current_y + it.max_ascent + it.max_descent;
10322 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10323 height = (height + unit - 1) / unit;
10324 }
10325
10326 /* Compute a suitable window start. */
10327 if (height > max_height)
10328 {
10329 height = max_height;
10330 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10331 move_it_vertically_backward (&it, (height - 1) * unit);
10332 start = it.current.pos;
10333 }
10334 else
10335 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10336 SET_MARKER_FROM_TEXT_POS (w->start, start);
10337
10338 if (EQ (Vresize_mini_windows, Qgrow_only))
10339 {
10340 /* Let it grow only, until we display an empty message, in which
10341 case the window shrinks again. */
10342 if (height > WINDOW_TOTAL_LINES (w))
10343 {
10344 int old_height = WINDOW_TOTAL_LINES (w);
10345 freeze_window_starts (f, 1);
10346 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10347 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10348 }
10349 else if (height < WINDOW_TOTAL_LINES (w)
10350 && (exact_p || BEGV == ZV))
10351 {
10352 int old_height = WINDOW_TOTAL_LINES (w);
10353 freeze_window_starts (f, 0);
10354 shrink_mini_window (w);
10355 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10356 }
10357 }
10358 else
10359 {
10360 /* Always resize to exact size needed. */
10361 if (height > WINDOW_TOTAL_LINES (w))
10362 {
10363 int old_height = WINDOW_TOTAL_LINES (w);
10364 freeze_window_starts (f, 1);
10365 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10366 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10367 }
10368 else if (height < WINDOW_TOTAL_LINES (w))
10369 {
10370 int old_height = WINDOW_TOTAL_LINES (w);
10371 freeze_window_starts (f, 0);
10372 shrink_mini_window (w);
10373
10374 if (height)
10375 {
10376 freeze_window_starts (f, 1);
10377 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10378 }
10379
10380 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10381 }
10382 }
10383
10384 if (old_current_buffer)
10385 set_buffer_internal (old_current_buffer);
10386 }
10387
10388 return window_height_changed_p;
10389 }
10390
10391
10392 /* Value is the current message, a string, or nil if there is no
10393 current message. */
10394
10395 Lisp_Object
10396 current_message (void)
10397 {
10398 Lisp_Object msg;
10399
10400 if (!BUFFERP (echo_area_buffer[0]))
10401 msg = Qnil;
10402 else
10403 {
10404 with_echo_area_buffer (0, 0, current_message_1,
10405 (intptr_t) &msg, Qnil, 0, 0);
10406 if (NILP (msg))
10407 echo_area_buffer[0] = Qnil;
10408 }
10409
10410 return msg;
10411 }
10412
10413
10414 static int
10415 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10416 {
10417 intptr_t i1 = a1;
10418 Lisp_Object *msg = (Lisp_Object *) i1;
10419
10420 if (Z > BEG)
10421 *msg = make_buffer_string (BEG, Z, 1);
10422 else
10423 *msg = Qnil;
10424 return 0;
10425 }
10426
10427
10428 /* Push the current message on Vmessage_stack for later restoration
10429 by restore_message. Value is non-zero if the current message isn't
10430 empty. This is a relatively infrequent operation, so it's not
10431 worth optimizing. */
10432
10433 int
10434 push_message (void)
10435 {
10436 Lisp_Object msg;
10437 msg = current_message ();
10438 Vmessage_stack = Fcons (msg, Vmessage_stack);
10439 return STRINGP (msg);
10440 }
10441
10442
10443 /* Restore message display from the top of Vmessage_stack. */
10444
10445 void
10446 restore_message (void)
10447 {
10448 Lisp_Object msg;
10449
10450 eassert (CONSP (Vmessage_stack));
10451 msg = XCAR (Vmessage_stack);
10452 if (STRINGP (msg))
10453 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10454 else
10455 message3_nolog (msg, 0, 0);
10456 }
10457
10458
10459 /* Handler for record_unwind_protect calling pop_message. */
10460
10461 Lisp_Object
10462 pop_message_unwind (Lisp_Object dummy)
10463 {
10464 pop_message ();
10465 return Qnil;
10466 }
10467
10468 /* Pop the top-most entry off Vmessage_stack. */
10469
10470 static void
10471 pop_message (void)
10472 {
10473 eassert (CONSP (Vmessage_stack));
10474 Vmessage_stack = XCDR (Vmessage_stack);
10475 }
10476
10477
10478 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10479 exits. If the stack is not empty, we have a missing pop_message
10480 somewhere. */
10481
10482 void
10483 check_message_stack (void)
10484 {
10485 if (!NILP (Vmessage_stack))
10486 abort ();
10487 }
10488
10489
10490 /* Truncate to NCHARS what will be displayed in the echo area the next
10491 time we display it---but don't redisplay it now. */
10492
10493 void
10494 truncate_echo_area (ptrdiff_t nchars)
10495 {
10496 if (nchars == 0)
10497 echo_area_buffer[0] = Qnil;
10498 /* A null message buffer means that the frame hasn't really been
10499 initialized yet. Error messages get reported properly by
10500 cmd_error, so this must be just an informative message; toss it. */
10501 else if (!noninteractive
10502 && INTERACTIVE
10503 && !NILP (echo_area_buffer[0]))
10504 {
10505 struct frame *sf = SELECTED_FRAME ();
10506 if (FRAME_MESSAGE_BUF (sf))
10507 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10508 }
10509 }
10510
10511
10512 /* Helper function for truncate_echo_area. Truncate the current
10513 message to at most NCHARS characters. */
10514
10515 static int
10516 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10517 {
10518 if (BEG + nchars < Z)
10519 del_range (BEG + nchars, Z);
10520 if (Z == BEG)
10521 echo_area_buffer[0] = Qnil;
10522 return 0;
10523 }
10524
10525
10526 /* Set the current message to a substring of S or STRING.
10527
10528 If STRING is a Lisp string, set the message to the first NBYTES
10529 bytes from STRING. NBYTES zero means use the whole string. If
10530 STRING is multibyte, the message will be displayed multibyte.
10531
10532 If S is not null, set the message to the first LEN bytes of S. LEN
10533 zero means use the whole string. MULTIBYTE_P non-zero means S is
10534 multibyte. Display the message multibyte in that case.
10535
10536 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10537 to t before calling set_message_1 (which calls insert).
10538 */
10539
10540 static void
10541 set_message (const char *s, Lisp_Object string,
10542 ptrdiff_t nbytes, int multibyte_p)
10543 {
10544 message_enable_multibyte
10545 = ((s && multibyte_p)
10546 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10547
10548 with_echo_area_buffer (0, -1, set_message_1,
10549 (intptr_t) s, string, nbytes, multibyte_p);
10550 message_buf_print = 0;
10551 help_echo_showing_p = 0;
10552 }
10553
10554
10555 /* Helper function for set_message. Arguments have the same meaning
10556 as there, with A1 corresponding to S and A2 corresponding to STRING
10557 This function is called with the echo area buffer being
10558 current. */
10559
10560 static int
10561 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10562 {
10563 intptr_t i1 = a1;
10564 const char *s = (const char *) i1;
10565 const unsigned char *msg = (const unsigned char *) s;
10566 Lisp_Object string = a2;
10567
10568 /* Change multibyteness of the echo buffer appropriately. */
10569 if (message_enable_multibyte
10570 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10571 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10572
10573 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10574 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10575 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10576
10577 /* Insert new message at BEG. */
10578 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10579
10580 if (STRINGP (string))
10581 {
10582 ptrdiff_t nchars;
10583
10584 if (nbytes == 0)
10585 nbytes = SBYTES (string);
10586 nchars = string_byte_to_char (string, nbytes);
10587
10588 /* This function takes care of single/multibyte conversion. We
10589 just have to ensure that the echo area buffer has the right
10590 setting of enable_multibyte_characters. */
10591 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10592 }
10593 else if (s)
10594 {
10595 if (nbytes == 0)
10596 nbytes = strlen (s);
10597
10598 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10599 {
10600 /* Convert from multi-byte to single-byte. */
10601 ptrdiff_t i;
10602 int c, n;
10603 char work[1];
10604
10605 /* Convert a multibyte string to single-byte. */
10606 for (i = 0; i < nbytes; i += n)
10607 {
10608 c = string_char_and_length (msg + i, &n);
10609 work[0] = (ASCII_CHAR_P (c)
10610 ? c
10611 : multibyte_char_to_unibyte (c));
10612 insert_1_both (work, 1, 1, 1, 0, 0);
10613 }
10614 }
10615 else if (!multibyte_p
10616 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10617 {
10618 /* Convert from single-byte to multi-byte. */
10619 ptrdiff_t i;
10620 int c, n;
10621 unsigned char str[MAX_MULTIBYTE_LENGTH];
10622
10623 /* Convert a single-byte string to multibyte. */
10624 for (i = 0; i < nbytes; i++)
10625 {
10626 c = msg[i];
10627 MAKE_CHAR_MULTIBYTE (c);
10628 n = CHAR_STRING (c, str);
10629 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10630 }
10631 }
10632 else
10633 insert_1 (s, nbytes, 1, 0, 0);
10634 }
10635
10636 return 0;
10637 }
10638
10639
10640 /* Clear messages. CURRENT_P non-zero means clear the current
10641 message. LAST_DISPLAYED_P non-zero means clear the message
10642 last displayed. */
10643
10644 void
10645 clear_message (int current_p, int last_displayed_p)
10646 {
10647 if (current_p)
10648 {
10649 echo_area_buffer[0] = Qnil;
10650 message_cleared_p = 1;
10651 }
10652
10653 if (last_displayed_p)
10654 echo_area_buffer[1] = Qnil;
10655
10656 message_buf_print = 0;
10657 }
10658
10659 /* Clear garbaged frames.
10660
10661 This function is used where the old redisplay called
10662 redraw_garbaged_frames which in turn called redraw_frame which in
10663 turn called clear_frame. The call to clear_frame was a source of
10664 flickering. I believe a clear_frame is not necessary. It should
10665 suffice in the new redisplay to invalidate all current matrices,
10666 and ensure a complete redisplay of all windows. */
10667
10668 static void
10669 clear_garbaged_frames (void)
10670 {
10671 if (frame_garbaged)
10672 {
10673 Lisp_Object tail, frame;
10674 int changed_count = 0;
10675
10676 FOR_EACH_FRAME (tail, frame)
10677 {
10678 struct frame *f = XFRAME (frame);
10679
10680 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10681 {
10682 if (f->resized_p)
10683 {
10684 Fredraw_frame (frame);
10685 f->force_flush_display_p = 1;
10686 }
10687 clear_current_matrices (f);
10688 changed_count++;
10689 f->garbaged = 0;
10690 f->resized_p = 0;
10691 }
10692 }
10693
10694 frame_garbaged = 0;
10695 if (changed_count)
10696 ++windows_or_buffers_changed;
10697 }
10698 }
10699
10700
10701 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10702 is non-zero update selected_frame. Value is non-zero if the
10703 mini-windows height has been changed. */
10704
10705 static int
10706 echo_area_display (int update_frame_p)
10707 {
10708 Lisp_Object mini_window;
10709 struct window *w;
10710 struct frame *f;
10711 int window_height_changed_p = 0;
10712 struct frame *sf = SELECTED_FRAME ();
10713
10714 mini_window = FRAME_MINIBUF_WINDOW (sf);
10715 w = XWINDOW (mini_window);
10716 f = XFRAME (WINDOW_FRAME (w));
10717
10718 /* Don't display if frame is invisible or not yet initialized. */
10719 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10720 return 0;
10721
10722 #ifdef HAVE_WINDOW_SYSTEM
10723 /* When Emacs starts, selected_frame may be the initial terminal
10724 frame. If we let this through, a message would be displayed on
10725 the terminal. */
10726 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10727 return 0;
10728 #endif /* HAVE_WINDOW_SYSTEM */
10729
10730 /* Redraw garbaged frames. */
10731 if (frame_garbaged)
10732 clear_garbaged_frames ();
10733
10734 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10735 {
10736 echo_area_window = mini_window;
10737 window_height_changed_p = display_echo_area (w);
10738 w->must_be_updated_p = 1;
10739
10740 /* Update the display, unless called from redisplay_internal.
10741 Also don't update the screen during redisplay itself. The
10742 update will happen at the end of redisplay, and an update
10743 here could cause confusion. */
10744 if (update_frame_p && !redisplaying_p)
10745 {
10746 int n = 0;
10747
10748 /* If the display update has been interrupted by pending
10749 input, update mode lines in the frame. Due to the
10750 pending input, it might have been that redisplay hasn't
10751 been called, so that mode lines above the echo area are
10752 garbaged. This looks odd, so we prevent it here. */
10753 if (!display_completed)
10754 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10755
10756 if (window_height_changed_p
10757 /* Don't do this if Emacs is shutting down. Redisplay
10758 needs to run hooks. */
10759 && !NILP (Vrun_hooks))
10760 {
10761 /* Must update other windows. Likewise as in other
10762 cases, don't let this update be interrupted by
10763 pending input. */
10764 ptrdiff_t count = SPECPDL_INDEX ();
10765 specbind (Qredisplay_dont_pause, Qt);
10766 windows_or_buffers_changed = 1;
10767 redisplay_internal ();
10768 unbind_to (count, Qnil);
10769 }
10770 else if (FRAME_WINDOW_P (f) && n == 0)
10771 {
10772 /* Window configuration is the same as before.
10773 Can do with a display update of the echo area,
10774 unless we displayed some mode lines. */
10775 update_single_window (w, 1);
10776 FRAME_RIF (f)->flush_display (f);
10777 }
10778 else
10779 update_frame (f, 1, 1);
10780
10781 /* If cursor is in the echo area, make sure that the next
10782 redisplay displays the minibuffer, so that the cursor will
10783 be replaced with what the minibuffer wants. */
10784 if (cursor_in_echo_area)
10785 ++windows_or_buffers_changed;
10786 }
10787 }
10788 else if (!EQ (mini_window, selected_window))
10789 windows_or_buffers_changed++;
10790
10791 /* Last displayed message is now the current message. */
10792 echo_area_buffer[1] = echo_area_buffer[0];
10793 /* Inform read_char that we're not echoing. */
10794 echo_message_buffer = Qnil;
10795
10796 /* Prevent redisplay optimization in redisplay_internal by resetting
10797 this_line_start_pos. This is done because the mini-buffer now
10798 displays the message instead of its buffer text. */
10799 if (EQ (mini_window, selected_window))
10800 CHARPOS (this_line_start_pos) = 0;
10801
10802 return window_height_changed_p;
10803 }
10804
10805
10806 \f
10807 /***********************************************************************
10808 Mode Lines and Frame Titles
10809 ***********************************************************************/
10810
10811 /* A buffer for constructing non-propertized mode-line strings and
10812 frame titles in it; allocated from the heap in init_xdisp and
10813 resized as needed in store_mode_line_noprop_char. */
10814
10815 static char *mode_line_noprop_buf;
10816
10817 /* The buffer's end, and a current output position in it. */
10818
10819 static char *mode_line_noprop_buf_end;
10820 static char *mode_line_noprop_ptr;
10821
10822 #define MODE_LINE_NOPROP_LEN(start) \
10823 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10824
10825 static enum {
10826 MODE_LINE_DISPLAY = 0,
10827 MODE_LINE_TITLE,
10828 MODE_LINE_NOPROP,
10829 MODE_LINE_STRING
10830 } mode_line_target;
10831
10832 /* Alist that caches the results of :propertize.
10833 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10834 static Lisp_Object mode_line_proptrans_alist;
10835
10836 /* List of strings making up the mode-line. */
10837 static Lisp_Object mode_line_string_list;
10838
10839 /* Base face property when building propertized mode line string. */
10840 static Lisp_Object mode_line_string_face;
10841 static Lisp_Object mode_line_string_face_prop;
10842
10843
10844 /* Unwind data for mode line strings */
10845
10846 static Lisp_Object Vmode_line_unwind_vector;
10847
10848 static Lisp_Object
10849 format_mode_line_unwind_data (struct frame *target_frame,
10850 struct buffer *obuf,
10851 Lisp_Object owin,
10852 int save_proptrans)
10853 {
10854 Lisp_Object vector, tmp;
10855
10856 /* Reduce consing by keeping one vector in
10857 Vwith_echo_area_save_vector. */
10858 vector = Vmode_line_unwind_vector;
10859 Vmode_line_unwind_vector = Qnil;
10860
10861 if (NILP (vector))
10862 vector = Fmake_vector (make_number (10), Qnil);
10863
10864 ASET (vector, 0, make_number (mode_line_target));
10865 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10866 ASET (vector, 2, mode_line_string_list);
10867 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10868 ASET (vector, 4, mode_line_string_face);
10869 ASET (vector, 5, mode_line_string_face_prop);
10870
10871 if (obuf)
10872 XSETBUFFER (tmp, obuf);
10873 else
10874 tmp = Qnil;
10875 ASET (vector, 6, tmp);
10876 ASET (vector, 7, owin);
10877 if (target_frame)
10878 {
10879 /* Similarly to `with-selected-window', if the operation selects
10880 a window on another frame, we must restore that frame's
10881 selected window, and (for a tty) the top-frame. */
10882 ASET (vector, 8, target_frame->selected_window);
10883 if (FRAME_TERMCAP_P (target_frame))
10884 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10885 }
10886
10887 return vector;
10888 }
10889
10890 static Lisp_Object
10891 unwind_format_mode_line (Lisp_Object vector)
10892 {
10893 Lisp_Object old_window = AREF (vector, 7);
10894 Lisp_Object target_frame_window = AREF (vector, 8);
10895 Lisp_Object old_top_frame = AREF (vector, 9);
10896
10897 mode_line_target = XINT (AREF (vector, 0));
10898 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10899 mode_line_string_list = AREF (vector, 2);
10900 if (! EQ (AREF (vector, 3), Qt))
10901 mode_line_proptrans_alist = AREF (vector, 3);
10902 mode_line_string_face = AREF (vector, 4);
10903 mode_line_string_face_prop = AREF (vector, 5);
10904
10905 /* Select window before buffer, since it may change the buffer. */
10906 if (!NILP (old_window))
10907 {
10908 /* If the operation that we are unwinding had selected a window
10909 on a different frame, reset its frame-selected-window. For a
10910 text terminal, reset its top-frame if necessary. */
10911 if (!NILP (target_frame_window))
10912 {
10913 Lisp_Object frame
10914 = WINDOW_FRAME (XWINDOW (target_frame_window));
10915
10916 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10917 Fselect_window (target_frame_window, Qt);
10918
10919 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10920 Fselect_frame (old_top_frame, Qt);
10921 }
10922
10923 Fselect_window (old_window, Qt);
10924 }
10925
10926 if (!NILP (AREF (vector, 6)))
10927 {
10928 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10929 ASET (vector, 6, Qnil);
10930 }
10931
10932 Vmode_line_unwind_vector = vector;
10933 return Qnil;
10934 }
10935
10936
10937 /* Store a single character C for the frame title in mode_line_noprop_buf.
10938 Re-allocate mode_line_noprop_buf if necessary. */
10939
10940 static void
10941 store_mode_line_noprop_char (char c)
10942 {
10943 /* If output position has reached the end of the allocated buffer,
10944 increase the buffer's size. */
10945 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10946 {
10947 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10948 ptrdiff_t size = len;
10949 mode_line_noprop_buf =
10950 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10951 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10952 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10953 }
10954
10955 *mode_line_noprop_ptr++ = c;
10956 }
10957
10958
10959 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10960 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10961 characters that yield more columns than PRECISION; PRECISION <= 0
10962 means copy the whole string. Pad with spaces until FIELD_WIDTH
10963 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10964 pad. Called from display_mode_element when it is used to build a
10965 frame title. */
10966
10967 static int
10968 store_mode_line_noprop (const char *string, int field_width, int precision)
10969 {
10970 const unsigned char *str = (const unsigned char *) string;
10971 int n = 0;
10972 ptrdiff_t dummy, nbytes;
10973
10974 /* Copy at most PRECISION chars from STR. */
10975 nbytes = strlen (string);
10976 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10977 while (nbytes--)
10978 store_mode_line_noprop_char (*str++);
10979
10980 /* Fill up with spaces until FIELD_WIDTH reached. */
10981 while (field_width > 0
10982 && n < field_width)
10983 {
10984 store_mode_line_noprop_char (' ');
10985 ++n;
10986 }
10987
10988 return n;
10989 }
10990
10991 /***********************************************************************
10992 Frame Titles
10993 ***********************************************************************/
10994
10995 #ifdef HAVE_WINDOW_SYSTEM
10996
10997 /* Set the title of FRAME, if it has changed. The title format is
10998 Vicon_title_format if FRAME is iconified, otherwise it is
10999 frame_title_format. */
11000
11001 static void
11002 x_consider_frame_title (Lisp_Object frame)
11003 {
11004 struct frame *f = XFRAME (frame);
11005
11006 if (FRAME_WINDOW_P (f)
11007 || FRAME_MINIBUF_ONLY_P (f)
11008 || f->explicit_name)
11009 {
11010 /* Do we have more than one visible frame on this X display? */
11011 Lisp_Object tail;
11012 Lisp_Object fmt;
11013 ptrdiff_t title_start;
11014 char *title;
11015 ptrdiff_t len;
11016 struct it it;
11017 ptrdiff_t count = SPECPDL_INDEX ();
11018
11019 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11020 {
11021 Lisp_Object other_frame = XCAR (tail);
11022 struct frame *tf = XFRAME (other_frame);
11023
11024 if (tf != f
11025 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11026 && !FRAME_MINIBUF_ONLY_P (tf)
11027 && !EQ (other_frame, tip_frame)
11028 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11029 break;
11030 }
11031
11032 /* Set global variable indicating that multiple frames exist. */
11033 multiple_frames = CONSP (tail);
11034
11035 /* Switch to the buffer of selected window of the frame. Set up
11036 mode_line_target so that display_mode_element will output into
11037 mode_line_noprop_buf; then display the title. */
11038 record_unwind_protect (unwind_format_mode_line,
11039 format_mode_line_unwind_data
11040 (f, current_buffer, selected_window, 0));
11041
11042 Fselect_window (f->selected_window, Qt);
11043 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11044 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11045
11046 mode_line_target = MODE_LINE_TITLE;
11047 title_start = MODE_LINE_NOPROP_LEN (0);
11048 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11049 NULL, DEFAULT_FACE_ID);
11050 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11051 len = MODE_LINE_NOPROP_LEN (title_start);
11052 title = mode_line_noprop_buf + title_start;
11053 unbind_to (count, Qnil);
11054
11055 /* Set the title only if it's changed. This avoids consing in
11056 the common case where it hasn't. (If it turns out that we've
11057 already wasted too much time by walking through the list with
11058 display_mode_element, then we might need to optimize at a
11059 higher level than this.) */
11060 if (! STRINGP (f->name)
11061 || SBYTES (f->name) != len
11062 || memcmp (title, SDATA (f->name), len) != 0)
11063 x_implicitly_set_name (f, make_string (title, len), Qnil);
11064 }
11065 }
11066
11067 #endif /* not HAVE_WINDOW_SYSTEM */
11068
11069 \f
11070 /***********************************************************************
11071 Menu Bars
11072 ***********************************************************************/
11073
11074
11075 /* Prepare for redisplay by updating menu-bar item lists when
11076 appropriate. This can call eval. */
11077
11078 void
11079 prepare_menu_bars (void)
11080 {
11081 int all_windows;
11082 struct gcpro gcpro1, gcpro2;
11083 struct frame *f;
11084 Lisp_Object tooltip_frame;
11085
11086 #ifdef HAVE_WINDOW_SYSTEM
11087 tooltip_frame = tip_frame;
11088 #else
11089 tooltip_frame = Qnil;
11090 #endif
11091
11092 /* Update all frame titles based on their buffer names, etc. We do
11093 this before the menu bars so that the buffer-menu will show the
11094 up-to-date frame titles. */
11095 #ifdef HAVE_WINDOW_SYSTEM
11096 if (windows_or_buffers_changed || update_mode_lines)
11097 {
11098 Lisp_Object tail, frame;
11099
11100 FOR_EACH_FRAME (tail, frame)
11101 {
11102 f = XFRAME (frame);
11103 if (!EQ (frame, tooltip_frame)
11104 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11105 x_consider_frame_title (frame);
11106 }
11107 }
11108 #endif /* HAVE_WINDOW_SYSTEM */
11109
11110 /* Update the menu bar item lists, if appropriate. This has to be
11111 done before any actual redisplay or generation of display lines. */
11112 all_windows = (update_mode_lines
11113 || buffer_shared > 1
11114 || windows_or_buffers_changed);
11115 if (all_windows)
11116 {
11117 Lisp_Object tail, frame;
11118 ptrdiff_t count = SPECPDL_INDEX ();
11119 /* 1 means that update_menu_bar has run its hooks
11120 so any further calls to update_menu_bar shouldn't do so again. */
11121 int menu_bar_hooks_run = 0;
11122
11123 record_unwind_save_match_data ();
11124
11125 FOR_EACH_FRAME (tail, frame)
11126 {
11127 f = XFRAME (frame);
11128
11129 /* Ignore tooltip frame. */
11130 if (EQ (frame, tooltip_frame))
11131 continue;
11132
11133 /* If a window on this frame changed size, report that to
11134 the user and clear the size-change flag. */
11135 if (FRAME_WINDOW_SIZES_CHANGED (f))
11136 {
11137 Lisp_Object functions;
11138
11139 /* Clear flag first in case we get an error below. */
11140 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11141 functions = Vwindow_size_change_functions;
11142 GCPRO2 (tail, functions);
11143
11144 while (CONSP (functions))
11145 {
11146 if (!EQ (XCAR (functions), Qt))
11147 call1 (XCAR (functions), frame);
11148 functions = XCDR (functions);
11149 }
11150 UNGCPRO;
11151 }
11152
11153 GCPRO1 (tail);
11154 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11155 #ifdef HAVE_WINDOW_SYSTEM
11156 update_tool_bar (f, 0);
11157 #endif
11158 #ifdef HAVE_NS
11159 if (windows_or_buffers_changed
11160 && FRAME_NS_P (f))
11161 ns_set_doc_edited (f, Fbuffer_modified_p
11162 (XWINDOW (f->selected_window)->buffer));
11163 #endif
11164 UNGCPRO;
11165 }
11166
11167 unbind_to (count, Qnil);
11168 }
11169 else
11170 {
11171 struct frame *sf = SELECTED_FRAME ();
11172 update_menu_bar (sf, 1, 0);
11173 #ifdef HAVE_WINDOW_SYSTEM
11174 update_tool_bar (sf, 1);
11175 #endif
11176 }
11177 }
11178
11179
11180 /* Update the menu bar item list for frame F. This has to be done
11181 before we start to fill in any display lines, because it can call
11182 eval.
11183
11184 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11185
11186 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11187 already ran the menu bar hooks for this redisplay, so there
11188 is no need to run them again. The return value is the
11189 updated value of this flag, to pass to the next call. */
11190
11191 static int
11192 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11193 {
11194 Lisp_Object window;
11195 register struct window *w;
11196
11197 /* If called recursively during a menu update, do nothing. This can
11198 happen when, for instance, an activate-menubar-hook causes a
11199 redisplay. */
11200 if (inhibit_menubar_update)
11201 return hooks_run;
11202
11203 window = FRAME_SELECTED_WINDOW (f);
11204 w = XWINDOW (window);
11205
11206 if (FRAME_WINDOW_P (f)
11207 ?
11208 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11209 || defined (HAVE_NS) || defined (USE_GTK)
11210 FRAME_EXTERNAL_MENU_BAR (f)
11211 #else
11212 FRAME_MENU_BAR_LINES (f) > 0
11213 #endif
11214 : FRAME_MENU_BAR_LINES (f) > 0)
11215 {
11216 /* If the user has switched buffers or windows, we need to
11217 recompute to reflect the new bindings. But we'll
11218 recompute when update_mode_lines is set too; that means
11219 that people can use force-mode-line-update to request
11220 that the menu bar be recomputed. The adverse effect on
11221 the rest of the redisplay algorithm is about the same as
11222 windows_or_buffers_changed anyway. */
11223 if (windows_or_buffers_changed
11224 /* This used to test w->update_mode_line, but we believe
11225 there is no need to recompute the menu in that case. */
11226 || update_mode_lines
11227 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11228 < BUF_MODIFF (XBUFFER (w->buffer)))
11229 != w->last_had_star)
11230 || ((!NILP (Vtransient_mark_mode)
11231 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11232 != !NILP (w->region_showing)))
11233 {
11234 struct buffer *prev = current_buffer;
11235 ptrdiff_t count = SPECPDL_INDEX ();
11236
11237 specbind (Qinhibit_menubar_update, Qt);
11238
11239 set_buffer_internal_1 (XBUFFER (w->buffer));
11240 if (save_match_data)
11241 record_unwind_save_match_data ();
11242 if (NILP (Voverriding_local_map_menu_flag))
11243 {
11244 specbind (Qoverriding_terminal_local_map, Qnil);
11245 specbind (Qoverriding_local_map, Qnil);
11246 }
11247
11248 if (!hooks_run)
11249 {
11250 /* Run the Lucid hook. */
11251 safe_run_hooks (Qactivate_menubar_hook);
11252
11253 /* If it has changed current-menubar from previous value,
11254 really recompute the menu-bar from the value. */
11255 if (! NILP (Vlucid_menu_bar_dirty_flag))
11256 call0 (Qrecompute_lucid_menubar);
11257
11258 safe_run_hooks (Qmenu_bar_update_hook);
11259
11260 hooks_run = 1;
11261 }
11262
11263 XSETFRAME (Vmenu_updating_frame, f);
11264 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11265
11266 /* Redisplay the menu bar in case we changed it. */
11267 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11268 || defined (HAVE_NS) || defined (USE_GTK)
11269 if (FRAME_WINDOW_P (f))
11270 {
11271 #if defined (HAVE_NS)
11272 /* All frames on Mac OS share the same menubar. So only
11273 the selected frame should be allowed to set it. */
11274 if (f == SELECTED_FRAME ())
11275 #endif
11276 set_frame_menubar (f, 0, 0);
11277 }
11278 else
11279 /* On a terminal screen, the menu bar is an ordinary screen
11280 line, and this makes it get updated. */
11281 w->update_mode_line = 1;
11282 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11283 /* In the non-toolkit version, the menu bar is an ordinary screen
11284 line, and this makes it get updated. */
11285 w->update_mode_line = 1;
11286 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11287
11288 unbind_to (count, Qnil);
11289 set_buffer_internal_1 (prev);
11290 }
11291 }
11292
11293 return hooks_run;
11294 }
11295
11296
11297 \f
11298 /***********************************************************************
11299 Output Cursor
11300 ***********************************************************************/
11301
11302 #ifdef HAVE_WINDOW_SYSTEM
11303
11304 /* EXPORT:
11305 Nominal cursor position -- where to draw output.
11306 HPOS and VPOS are window relative glyph matrix coordinates.
11307 X and Y are window relative pixel coordinates. */
11308
11309 struct cursor_pos output_cursor;
11310
11311
11312 /* EXPORT:
11313 Set the global variable output_cursor to CURSOR. All cursor
11314 positions are relative to updated_window. */
11315
11316 void
11317 set_output_cursor (struct cursor_pos *cursor)
11318 {
11319 output_cursor.hpos = cursor->hpos;
11320 output_cursor.vpos = cursor->vpos;
11321 output_cursor.x = cursor->x;
11322 output_cursor.y = cursor->y;
11323 }
11324
11325
11326 /* EXPORT for RIF:
11327 Set a nominal cursor position.
11328
11329 HPOS and VPOS are column/row positions in a window glyph matrix. X
11330 and Y are window text area relative pixel positions.
11331
11332 If this is done during an update, updated_window will contain the
11333 window that is being updated and the position is the future output
11334 cursor position for that window. If updated_window is null, use
11335 selected_window and display the cursor at the given position. */
11336
11337 void
11338 x_cursor_to (int vpos, int hpos, int y, int x)
11339 {
11340 struct window *w;
11341
11342 /* If updated_window is not set, work on selected_window. */
11343 if (updated_window)
11344 w = updated_window;
11345 else
11346 w = XWINDOW (selected_window);
11347
11348 /* Set the output cursor. */
11349 output_cursor.hpos = hpos;
11350 output_cursor.vpos = vpos;
11351 output_cursor.x = x;
11352 output_cursor.y = y;
11353
11354 /* If not called as part of an update, really display the cursor.
11355 This will also set the cursor position of W. */
11356 if (updated_window == NULL)
11357 {
11358 BLOCK_INPUT;
11359 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11360 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11361 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11362 UNBLOCK_INPUT;
11363 }
11364 }
11365
11366 #endif /* HAVE_WINDOW_SYSTEM */
11367
11368 \f
11369 /***********************************************************************
11370 Tool-bars
11371 ***********************************************************************/
11372
11373 #ifdef HAVE_WINDOW_SYSTEM
11374
11375 /* Where the mouse was last time we reported a mouse event. */
11376
11377 FRAME_PTR last_mouse_frame;
11378
11379 /* Tool-bar item index of the item on which a mouse button was pressed
11380 or -1. */
11381
11382 int last_tool_bar_item;
11383
11384
11385 static Lisp_Object
11386 update_tool_bar_unwind (Lisp_Object frame)
11387 {
11388 selected_frame = frame;
11389 return Qnil;
11390 }
11391
11392 /* Update the tool-bar item list for frame F. This has to be done
11393 before we start to fill in any display lines. Called from
11394 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11395 and restore it here. */
11396
11397 static void
11398 update_tool_bar (struct frame *f, int save_match_data)
11399 {
11400 #if defined (USE_GTK) || defined (HAVE_NS)
11401 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11402 #else
11403 int do_update = WINDOWP (f->tool_bar_window)
11404 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11405 #endif
11406
11407 if (do_update)
11408 {
11409 Lisp_Object window;
11410 struct window *w;
11411
11412 window = FRAME_SELECTED_WINDOW (f);
11413 w = XWINDOW (window);
11414
11415 /* If the user has switched buffers or windows, we need to
11416 recompute to reflect the new bindings. But we'll
11417 recompute when update_mode_lines is set too; that means
11418 that people can use force-mode-line-update to request
11419 that the menu bar be recomputed. The adverse effect on
11420 the rest of the redisplay algorithm is about the same as
11421 windows_or_buffers_changed anyway. */
11422 if (windows_or_buffers_changed
11423 || w->update_mode_line
11424 || update_mode_lines
11425 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11426 < BUF_MODIFF (XBUFFER (w->buffer)))
11427 != w->last_had_star)
11428 || ((!NILP (Vtransient_mark_mode)
11429 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11430 != !NILP (w->region_showing)))
11431 {
11432 struct buffer *prev = current_buffer;
11433 ptrdiff_t count = SPECPDL_INDEX ();
11434 Lisp_Object frame, new_tool_bar;
11435 int new_n_tool_bar;
11436 struct gcpro gcpro1;
11437
11438 /* Set current_buffer to the buffer of the selected
11439 window of the frame, so that we get the right local
11440 keymaps. */
11441 set_buffer_internal_1 (XBUFFER (w->buffer));
11442
11443 /* Save match data, if we must. */
11444 if (save_match_data)
11445 record_unwind_save_match_data ();
11446
11447 /* Make sure that we don't accidentally use bogus keymaps. */
11448 if (NILP (Voverriding_local_map_menu_flag))
11449 {
11450 specbind (Qoverriding_terminal_local_map, Qnil);
11451 specbind (Qoverriding_local_map, Qnil);
11452 }
11453
11454 GCPRO1 (new_tool_bar);
11455
11456 /* We must temporarily set the selected frame to this frame
11457 before calling tool_bar_items, because the calculation of
11458 the tool-bar keymap uses the selected frame (see
11459 `tool-bar-make-keymap' in tool-bar.el). */
11460 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11461 XSETFRAME (frame, f);
11462 selected_frame = frame;
11463
11464 /* Build desired tool-bar items from keymaps. */
11465 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11466 &new_n_tool_bar);
11467
11468 /* Redisplay the tool-bar if we changed it. */
11469 if (new_n_tool_bar != f->n_tool_bar_items
11470 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11471 {
11472 /* Redisplay that happens asynchronously due to an expose event
11473 may access f->tool_bar_items. Make sure we update both
11474 variables within BLOCK_INPUT so no such event interrupts. */
11475 BLOCK_INPUT;
11476 f->tool_bar_items = new_tool_bar;
11477 f->n_tool_bar_items = new_n_tool_bar;
11478 w->update_mode_line = 1;
11479 UNBLOCK_INPUT;
11480 }
11481
11482 UNGCPRO;
11483
11484 unbind_to (count, Qnil);
11485 set_buffer_internal_1 (prev);
11486 }
11487 }
11488 }
11489
11490
11491 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11492 F's desired tool-bar contents. F->tool_bar_items must have
11493 been set up previously by calling prepare_menu_bars. */
11494
11495 static void
11496 build_desired_tool_bar_string (struct frame *f)
11497 {
11498 int i, size, size_needed;
11499 struct gcpro gcpro1, gcpro2, gcpro3;
11500 Lisp_Object image, plist, props;
11501
11502 image = plist = props = Qnil;
11503 GCPRO3 (image, plist, props);
11504
11505 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11506 Otherwise, make a new string. */
11507
11508 /* The size of the string we might be able to reuse. */
11509 size = (STRINGP (f->desired_tool_bar_string)
11510 ? SCHARS (f->desired_tool_bar_string)
11511 : 0);
11512
11513 /* We need one space in the string for each image. */
11514 size_needed = f->n_tool_bar_items;
11515
11516 /* Reuse f->desired_tool_bar_string, if possible. */
11517 if (size < size_needed || NILP (f->desired_tool_bar_string))
11518 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11519 make_number (' '));
11520 else
11521 {
11522 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11523 Fremove_text_properties (make_number (0), make_number (size),
11524 props, f->desired_tool_bar_string);
11525 }
11526
11527 /* Put a `display' property on the string for the images to display,
11528 put a `menu_item' property on tool-bar items with a value that
11529 is the index of the item in F's tool-bar item vector. */
11530 for (i = 0; i < f->n_tool_bar_items; ++i)
11531 {
11532 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11533
11534 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11535 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11536 int hmargin, vmargin, relief, idx, end;
11537
11538 /* If image is a vector, choose the image according to the
11539 button state. */
11540 image = PROP (TOOL_BAR_ITEM_IMAGES);
11541 if (VECTORP (image))
11542 {
11543 if (enabled_p)
11544 idx = (selected_p
11545 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11546 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11547 else
11548 idx = (selected_p
11549 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11550 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11551
11552 eassert (ASIZE (image) >= idx);
11553 image = AREF (image, idx);
11554 }
11555 else
11556 idx = -1;
11557
11558 /* Ignore invalid image specifications. */
11559 if (!valid_image_p (image))
11560 continue;
11561
11562 /* Display the tool-bar button pressed, or depressed. */
11563 plist = Fcopy_sequence (XCDR (image));
11564
11565 /* Compute margin and relief to draw. */
11566 relief = (tool_bar_button_relief >= 0
11567 ? tool_bar_button_relief
11568 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11569 hmargin = vmargin = relief;
11570
11571 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11572 INT_MAX - max (hmargin, vmargin)))
11573 {
11574 hmargin += XFASTINT (Vtool_bar_button_margin);
11575 vmargin += XFASTINT (Vtool_bar_button_margin);
11576 }
11577 else if (CONSP (Vtool_bar_button_margin))
11578 {
11579 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11580 INT_MAX - hmargin))
11581 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11582
11583 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11584 INT_MAX - vmargin))
11585 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11586 }
11587
11588 if (auto_raise_tool_bar_buttons_p)
11589 {
11590 /* Add a `:relief' property to the image spec if the item is
11591 selected. */
11592 if (selected_p)
11593 {
11594 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11595 hmargin -= relief;
11596 vmargin -= relief;
11597 }
11598 }
11599 else
11600 {
11601 /* If image is selected, display it pressed, i.e. with a
11602 negative relief. If it's not selected, display it with a
11603 raised relief. */
11604 plist = Fplist_put (plist, QCrelief,
11605 (selected_p
11606 ? make_number (-relief)
11607 : make_number (relief)));
11608 hmargin -= relief;
11609 vmargin -= relief;
11610 }
11611
11612 /* Put a margin around the image. */
11613 if (hmargin || vmargin)
11614 {
11615 if (hmargin == vmargin)
11616 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11617 else
11618 plist = Fplist_put (plist, QCmargin,
11619 Fcons (make_number (hmargin),
11620 make_number (vmargin)));
11621 }
11622
11623 /* If button is not enabled, and we don't have special images
11624 for the disabled state, make the image appear disabled by
11625 applying an appropriate algorithm to it. */
11626 if (!enabled_p && idx < 0)
11627 plist = Fplist_put (plist, QCconversion, Qdisabled);
11628
11629 /* Put a `display' text property on the string for the image to
11630 display. Put a `menu-item' property on the string that gives
11631 the start of this item's properties in the tool-bar items
11632 vector. */
11633 image = Fcons (Qimage, plist);
11634 props = list4 (Qdisplay, image,
11635 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11636
11637 /* Let the last image hide all remaining spaces in the tool bar
11638 string. The string can be longer than needed when we reuse a
11639 previous string. */
11640 if (i + 1 == f->n_tool_bar_items)
11641 end = SCHARS (f->desired_tool_bar_string);
11642 else
11643 end = i + 1;
11644 Fadd_text_properties (make_number (i), make_number (end),
11645 props, f->desired_tool_bar_string);
11646 #undef PROP
11647 }
11648
11649 UNGCPRO;
11650 }
11651
11652
11653 /* Display one line of the tool-bar of frame IT->f.
11654
11655 HEIGHT specifies the desired height of the tool-bar line.
11656 If the actual height of the glyph row is less than HEIGHT, the
11657 row's height is increased to HEIGHT, and the icons are centered
11658 vertically in the new height.
11659
11660 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11661 count a final empty row in case the tool-bar width exactly matches
11662 the window width.
11663 */
11664
11665 static void
11666 display_tool_bar_line (struct it *it, int height)
11667 {
11668 struct glyph_row *row = it->glyph_row;
11669 int max_x = it->last_visible_x;
11670 struct glyph *last;
11671
11672 prepare_desired_row (row);
11673 row->y = it->current_y;
11674
11675 /* Note that this isn't made use of if the face hasn't a box,
11676 so there's no need to check the face here. */
11677 it->start_of_box_run_p = 1;
11678
11679 while (it->current_x < max_x)
11680 {
11681 int x, n_glyphs_before, i, nglyphs;
11682 struct it it_before;
11683
11684 /* Get the next display element. */
11685 if (!get_next_display_element (it))
11686 {
11687 /* Don't count empty row if we are counting needed tool-bar lines. */
11688 if (height < 0 && !it->hpos)
11689 return;
11690 break;
11691 }
11692
11693 /* Produce glyphs. */
11694 n_glyphs_before = row->used[TEXT_AREA];
11695 it_before = *it;
11696
11697 PRODUCE_GLYPHS (it);
11698
11699 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11700 i = 0;
11701 x = it_before.current_x;
11702 while (i < nglyphs)
11703 {
11704 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11705
11706 if (x + glyph->pixel_width > max_x)
11707 {
11708 /* Glyph doesn't fit on line. Backtrack. */
11709 row->used[TEXT_AREA] = n_glyphs_before;
11710 *it = it_before;
11711 /* If this is the only glyph on this line, it will never fit on the
11712 tool-bar, so skip it. But ensure there is at least one glyph,
11713 so we don't accidentally disable the tool-bar. */
11714 if (n_glyphs_before == 0
11715 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11716 break;
11717 goto out;
11718 }
11719
11720 ++it->hpos;
11721 x += glyph->pixel_width;
11722 ++i;
11723 }
11724
11725 /* Stop at line end. */
11726 if (ITERATOR_AT_END_OF_LINE_P (it))
11727 break;
11728
11729 set_iterator_to_next (it, 1);
11730 }
11731
11732 out:;
11733
11734 row->displays_text_p = row->used[TEXT_AREA] != 0;
11735
11736 /* Use default face for the border below the tool bar.
11737
11738 FIXME: When auto-resize-tool-bars is grow-only, there is
11739 no additional border below the possibly empty tool-bar lines.
11740 So to make the extra empty lines look "normal", we have to
11741 use the tool-bar face for the border too. */
11742 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11743 it->face_id = DEFAULT_FACE_ID;
11744
11745 extend_face_to_end_of_line (it);
11746 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11747 last->right_box_line_p = 1;
11748 if (last == row->glyphs[TEXT_AREA])
11749 last->left_box_line_p = 1;
11750
11751 /* Make line the desired height and center it vertically. */
11752 if ((height -= it->max_ascent + it->max_descent) > 0)
11753 {
11754 /* Don't add more than one line height. */
11755 height %= FRAME_LINE_HEIGHT (it->f);
11756 it->max_ascent += height / 2;
11757 it->max_descent += (height + 1) / 2;
11758 }
11759
11760 compute_line_metrics (it);
11761
11762 /* If line is empty, make it occupy the rest of the tool-bar. */
11763 if (!row->displays_text_p)
11764 {
11765 row->height = row->phys_height = it->last_visible_y - row->y;
11766 row->visible_height = row->height;
11767 row->ascent = row->phys_ascent = 0;
11768 row->extra_line_spacing = 0;
11769 }
11770
11771 row->full_width_p = 1;
11772 row->continued_p = 0;
11773 row->truncated_on_left_p = 0;
11774 row->truncated_on_right_p = 0;
11775
11776 it->current_x = it->hpos = 0;
11777 it->current_y += row->height;
11778 ++it->vpos;
11779 ++it->glyph_row;
11780 }
11781
11782
11783 /* Max tool-bar height. */
11784
11785 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11786 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11787
11788 /* Value is the number of screen lines needed to make all tool-bar
11789 items of frame F visible. The number of actual rows needed is
11790 returned in *N_ROWS if non-NULL. */
11791
11792 static int
11793 tool_bar_lines_needed (struct frame *f, int *n_rows)
11794 {
11795 struct window *w = XWINDOW (f->tool_bar_window);
11796 struct it it;
11797 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11798 the desired matrix, so use (unused) mode-line row as temporary row to
11799 avoid destroying the first tool-bar row. */
11800 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11801
11802 /* Initialize an iterator for iteration over
11803 F->desired_tool_bar_string in the tool-bar window of frame F. */
11804 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11805 it.first_visible_x = 0;
11806 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11807 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11808 it.paragraph_embedding = L2R;
11809
11810 while (!ITERATOR_AT_END_P (&it))
11811 {
11812 clear_glyph_row (temp_row);
11813 it.glyph_row = temp_row;
11814 display_tool_bar_line (&it, -1);
11815 }
11816 clear_glyph_row (temp_row);
11817
11818 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11819 if (n_rows)
11820 *n_rows = it.vpos > 0 ? it.vpos : -1;
11821
11822 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11823 }
11824
11825
11826 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11827 0, 1, 0,
11828 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11829 (Lisp_Object frame)
11830 {
11831 struct frame *f;
11832 struct window *w;
11833 int nlines = 0;
11834
11835 if (NILP (frame))
11836 frame = selected_frame;
11837 else
11838 CHECK_FRAME (frame);
11839 f = XFRAME (frame);
11840
11841 if (WINDOWP (f->tool_bar_window)
11842 && (w = XWINDOW (f->tool_bar_window),
11843 WINDOW_TOTAL_LINES (w) > 0))
11844 {
11845 update_tool_bar (f, 1);
11846 if (f->n_tool_bar_items)
11847 {
11848 build_desired_tool_bar_string (f);
11849 nlines = tool_bar_lines_needed (f, NULL);
11850 }
11851 }
11852
11853 return make_number (nlines);
11854 }
11855
11856
11857 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11858 height should be changed. */
11859
11860 static int
11861 redisplay_tool_bar (struct frame *f)
11862 {
11863 struct window *w;
11864 struct it it;
11865 struct glyph_row *row;
11866
11867 #if defined (USE_GTK) || defined (HAVE_NS)
11868 if (FRAME_EXTERNAL_TOOL_BAR (f))
11869 update_frame_tool_bar (f);
11870 return 0;
11871 #endif
11872
11873 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11874 do anything. This means you must start with tool-bar-lines
11875 non-zero to get the auto-sizing effect. Or in other words, you
11876 can turn off tool-bars by specifying tool-bar-lines zero. */
11877 if (!WINDOWP (f->tool_bar_window)
11878 || (w = XWINDOW (f->tool_bar_window),
11879 WINDOW_TOTAL_LINES (w) == 0))
11880 return 0;
11881
11882 /* Set up an iterator for the tool-bar window. */
11883 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11884 it.first_visible_x = 0;
11885 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11886 row = it.glyph_row;
11887
11888 /* Build a string that represents the contents of the tool-bar. */
11889 build_desired_tool_bar_string (f);
11890 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11891 /* FIXME: This should be controlled by a user option. But it
11892 doesn't make sense to have an R2L tool bar if the menu bar cannot
11893 be drawn also R2L, and making the menu bar R2L is tricky due
11894 toolkit-specific code that implements it. If an R2L tool bar is
11895 ever supported, display_tool_bar_line should also be augmented to
11896 call unproduce_glyphs like display_line and display_string
11897 do. */
11898 it.paragraph_embedding = L2R;
11899
11900 if (f->n_tool_bar_rows == 0)
11901 {
11902 int nlines;
11903
11904 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11905 nlines != WINDOW_TOTAL_LINES (w)))
11906 {
11907 Lisp_Object frame;
11908 int old_height = WINDOW_TOTAL_LINES (w);
11909
11910 XSETFRAME (frame, f);
11911 Fmodify_frame_parameters (frame,
11912 Fcons (Fcons (Qtool_bar_lines,
11913 make_number (nlines)),
11914 Qnil));
11915 if (WINDOW_TOTAL_LINES (w) != old_height)
11916 {
11917 clear_glyph_matrix (w->desired_matrix);
11918 fonts_changed_p = 1;
11919 return 1;
11920 }
11921 }
11922 }
11923
11924 /* Display as many lines as needed to display all tool-bar items. */
11925
11926 if (f->n_tool_bar_rows > 0)
11927 {
11928 int border, rows, height, extra;
11929
11930 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11931 border = XINT (Vtool_bar_border);
11932 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11933 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11934 else if (EQ (Vtool_bar_border, Qborder_width))
11935 border = f->border_width;
11936 else
11937 border = 0;
11938 if (border < 0)
11939 border = 0;
11940
11941 rows = f->n_tool_bar_rows;
11942 height = max (1, (it.last_visible_y - border) / rows);
11943 extra = it.last_visible_y - border - height * rows;
11944
11945 while (it.current_y < it.last_visible_y)
11946 {
11947 int h = 0;
11948 if (extra > 0 && rows-- > 0)
11949 {
11950 h = (extra + rows - 1) / rows;
11951 extra -= h;
11952 }
11953 display_tool_bar_line (&it, height + h);
11954 }
11955 }
11956 else
11957 {
11958 while (it.current_y < it.last_visible_y)
11959 display_tool_bar_line (&it, 0);
11960 }
11961
11962 /* It doesn't make much sense to try scrolling in the tool-bar
11963 window, so don't do it. */
11964 w->desired_matrix->no_scrolling_p = 1;
11965 w->must_be_updated_p = 1;
11966
11967 if (!NILP (Vauto_resize_tool_bars))
11968 {
11969 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11970 int change_height_p = 0;
11971
11972 /* If we couldn't display everything, change the tool-bar's
11973 height if there is room for more. */
11974 if (IT_STRING_CHARPOS (it) < it.end_charpos
11975 && it.current_y < max_tool_bar_height)
11976 change_height_p = 1;
11977
11978 row = it.glyph_row - 1;
11979
11980 /* If there are blank lines at the end, except for a partially
11981 visible blank line at the end that is smaller than
11982 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11983 if (!row->displays_text_p
11984 && row->height >= FRAME_LINE_HEIGHT (f))
11985 change_height_p = 1;
11986
11987 /* If row displays tool-bar items, but is partially visible,
11988 change the tool-bar's height. */
11989 if (row->displays_text_p
11990 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11991 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11992 change_height_p = 1;
11993
11994 /* Resize windows as needed by changing the `tool-bar-lines'
11995 frame parameter. */
11996 if (change_height_p)
11997 {
11998 Lisp_Object frame;
11999 int old_height = WINDOW_TOTAL_LINES (w);
12000 int nrows;
12001 int nlines = tool_bar_lines_needed (f, &nrows);
12002
12003 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12004 && !f->minimize_tool_bar_window_p)
12005 ? (nlines > old_height)
12006 : (nlines != old_height));
12007 f->minimize_tool_bar_window_p = 0;
12008
12009 if (change_height_p)
12010 {
12011 XSETFRAME (frame, f);
12012 Fmodify_frame_parameters (frame,
12013 Fcons (Fcons (Qtool_bar_lines,
12014 make_number (nlines)),
12015 Qnil));
12016 if (WINDOW_TOTAL_LINES (w) != old_height)
12017 {
12018 clear_glyph_matrix (w->desired_matrix);
12019 f->n_tool_bar_rows = nrows;
12020 fonts_changed_p = 1;
12021 return 1;
12022 }
12023 }
12024 }
12025 }
12026
12027 f->minimize_tool_bar_window_p = 0;
12028 return 0;
12029 }
12030
12031
12032 /* Get information about the tool-bar item which is displayed in GLYPH
12033 on frame F. Return in *PROP_IDX the index where tool-bar item
12034 properties start in F->tool_bar_items. Value is zero if
12035 GLYPH doesn't display a tool-bar item. */
12036
12037 static int
12038 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12039 {
12040 Lisp_Object prop;
12041 int success_p;
12042 int charpos;
12043
12044 /* This function can be called asynchronously, which means we must
12045 exclude any possibility that Fget_text_property signals an
12046 error. */
12047 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12048 charpos = max (0, charpos);
12049
12050 /* Get the text property `menu-item' at pos. The value of that
12051 property is the start index of this item's properties in
12052 F->tool_bar_items. */
12053 prop = Fget_text_property (make_number (charpos),
12054 Qmenu_item, f->current_tool_bar_string);
12055 if (INTEGERP (prop))
12056 {
12057 *prop_idx = XINT (prop);
12058 success_p = 1;
12059 }
12060 else
12061 success_p = 0;
12062
12063 return success_p;
12064 }
12065
12066 \f
12067 /* Get information about the tool-bar item at position X/Y on frame F.
12068 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12069 the current matrix of the tool-bar window of F, or NULL if not
12070 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12071 item in F->tool_bar_items. Value is
12072
12073 -1 if X/Y is not on a tool-bar item
12074 0 if X/Y is on the same item that was highlighted before.
12075 1 otherwise. */
12076
12077 static int
12078 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12079 int *hpos, int *vpos, int *prop_idx)
12080 {
12081 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12082 struct window *w = XWINDOW (f->tool_bar_window);
12083 int area;
12084
12085 /* Find the glyph under X/Y. */
12086 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12087 if (*glyph == NULL)
12088 return -1;
12089
12090 /* Get the start of this tool-bar item's properties in
12091 f->tool_bar_items. */
12092 if (!tool_bar_item_info (f, *glyph, prop_idx))
12093 return -1;
12094
12095 /* Is mouse on the highlighted item? */
12096 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12097 && *vpos >= hlinfo->mouse_face_beg_row
12098 && *vpos <= hlinfo->mouse_face_end_row
12099 && (*vpos > hlinfo->mouse_face_beg_row
12100 || *hpos >= hlinfo->mouse_face_beg_col)
12101 && (*vpos < hlinfo->mouse_face_end_row
12102 || *hpos < hlinfo->mouse_face_end_col
12103 || hlinfo->mouse_face_past_end))
12104 return 0;
12105
12106 return 1;
12107 }
12108
12109
12110 /* EXPORT:
12111 Handle mouse button event on the tool-bar of frame F, at
12112 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12113 0 for button release. MODIFIERS is event modifiers for button
12114 release. */
12115
12116 void
12117 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12118 int modifiers)
12119 {
12120 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12121 struct window *w = XWINDOW (f->tool_bar_window);
12122 int hpos, vpos, prop_idx;
12123 struct glyph *glyph;
12124 Lisp_Object enabled_p;
12125
12126 /* If not on the highlighted tool-bar item, return. */
12127 frame_to_window_pixel_xy (w, &x, &y);
12128 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12129 return;
12130
12131 /* If item is disabled, do nothing. */
12132 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12133 if (NILP (enabled_p))
12134 return;
12135
12136 if (down_p)
12137 {
12138 /* Show item in pressed state. */
12139 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12140 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12141 last_tool_bar_item = prop_idx;
12142 }
12143 else
12144 {
12145 Lisp_Object key, frame;
12146 struct input_event event;
12147 EVENT_INIT (event);
12148
12149 /* Show item in released state. */
12150 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12151 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12152
12153 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12154
12155 XSETFRAME (frame, f);
12156 event.kind = TOOL_BAR_EVENT;
12157 event.frame_or_window = frame;
12158 event.arg = frame;
12159 kbd_buffer_store_event (&event);
12160
12161 event.kind = TOOL_BAR_EVENT;
12162 event.frame_or_window = frame;
12163 event.arg = key;
12164 event.modifiers = modifiers;
12165 kbd_buffer_store_event (&event);
12166 last_tool_bar_item = -1;
12167 }
12168 }
12169
12170
12171 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12172 tool-bar window-relative coordinates X/Y. Called from
12173 note_mouse_highlight. */
12174
12175 static void
12176 note_tool_bar_highlight (struct frame *f, int x, int y)
12177 {
12178 Lisp_Object window = f->tool_bar_window;
12179 struct window *w = XWINDOW (window);
12180 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12181 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12182 int hpos, vpos;
12183 struct glyph *glyph;
12184 struct glyph_row *row;
12185 int i;
12186 Lisp_Object enabled_p;
12187 int prop_idx;
12188 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12189 int mouse_down_p, rc;
12190
12191 /* Function note_mouse_highlight is called with negative X/Y
12192 values when mouse moves outside of the frame. */
12193 if (x <= 0 || y <= 0)
12194 {
12195 clear_mouse_face (hlinfo);
12196 return;
12197 }
12198
12199 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12200 if (rc < 0)
12201 {
12202 /* Not on tool-bar item. */
12203 clear_mouse_face (hlinfo);
12204 return;
12205 }
12206 else if (rc == 0)
12207 /* On same tool-bar item as before. */
12208 goto set_help_echo;
12209
12210 clear_mouse_face (hlinfo);
12211
12212 /* Mouse is down, but on different tool-bar item? */
12213 mouse_down_p = (dpyinfo->grabbed
12214 && f == last_mouse_frame
12215 && FRAME_LIVE_P (f));
12216 if (mouse_down_p
12217 && last_tool_bar_item != prop_idx)
12218 return;
12219
12220 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12221 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12222
12223 /* If tool-bar item is not enabled, don't highlight it. */
12224 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12225 if (!NILP (enabled_p))
12226 {
12227 /* Compute the x-position of the glyph. In front and past the
12228 image is a space. We include this in the highlighted area. */
12229 row = MATRIX_ROW (w->current_matrix, vpos);
12230 for (i = x = 0; i < hpos; ++i)
12231 x += row->glyphs[TEXT_AREA][i].pixel_width;
12232
12233 /* Record this as the current active region. */
12234 hlinfo->mouse_face_beg_col = hpos;
12235 hlinfo->mouse_face_beg_row = vpos;
12236 hlinfo->mouse_face_beg_x = x;
12237 hlinfo->mouse_face_beg_y = row->y;
12238 hlinfo->mouse_face_past_end = 0;
12239
12240 hlinfo->mouse_face_end_col = hpos + 1;
12241 hlinfo->mouse_face_end_row = vpos;
12242 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12243 hlinfo->mouse_face_end_y = row->y;
12244 hlinfo->mouse_face_window = window;
12245 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12246
12247 /* Display it as active. */
12248 show_mouse_face (hlinfo, draw);
12249 hlinfo->mouse_face_image_state = draw;
12250 }
12251
12252 set_help_echo:
12253
12254 /* Set help_echo_string to a help string to display for this tool-bar item.
12255 XTread_socket does the rest. */
12256 help_echo_object = help_echo_window = Qnil;
12257 help_echo_pos = -1;
12258 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12259 if (NILP (help_echo_string))
12260 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12261 }
12262
12263 #endif /* HAVE_WINDOW_SYSTEM */
12264
12265
12266 \f
12267 /************************************************************************
12268 Horizontal scrolling
12269 ************************************************************************/
12270
12271 static int hscroll_window_tree (Lisp_Object);
12272 static int hscroll_windows (Lisp_Object);
12273
12274 /* For all leaf windows in the window tree rooted at WINDOW, set their
12275 hscroll value so that PT is (i) visible in the window, and (ii) so
12276 that it is not within a certain margin at the window's left and
12277 right border. Value is non-zero if any window's hscroll has been
12278 changed. */
12279
12280 static int
12281 hscroll_window_tree (Lisp_Object window)
12282 {
12283 int hscrolled_p = 0;
12284 int hscroll_relative_p = FLOATP (Vhscroll_step);
12285 int hscroll_step_abs = 0;
12286 double hscroll_step_rel = 0;
12287
12288 if (hscroll_relative_p)
12289 {
12290 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12291 if (hscroll_step_rel < 0)
12292 {
12293 hscroll_relative_p = 0;
12294 hscroll_step_abs = 0;
12295 }
12296 }
12297 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12298 {
12299 hscroll_step_abs = XINT (Vhscroll_step);
12300 if (hscroll_step_abs < 0)
12301 hscroll_step_abs = 0;
12302 }
12303 else
12304 hscroll_step_abs = 0;
12305
12306 while (WINDOWP (window))
12307 {
12308 struct window *w = XWINDOW (window);
12309
12310 if (WINDOWP (w->hchild))
12311 hscrolled_p |= hscroll_window_tree (w->hchild);
12312 else if (WINDOWP (w->vchild))
12313 hscrolled_p |= hscroll_window_tree (w->vchild);
12314 else if (w->cursor.vpos >= 0)
12315 {
12316 int h_margin;
12317 int text_area_width;
12318 struct glyph_row *current_cursor_row
12319 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12320 struct glyph_row *desired_cursor_row
12321 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12322 struct glyph_row *cursor_row
12323 = (desired_cursor_row->enabled_p
12324 ? desired_cursor_row
12325 : current_cursor_row);
12326 int row_r2l_p = cursor_row->reversed_p;
12327
12328 text_area_width = window_box_width (w, TEXT_AREA);
12329
12330 /* Scroll when cursor is inside this scroll margin. */
12331 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12332
12333 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12334 /* For left-to-right rows, hscroll when cursor is either
12335 (i) inside the right hscroll margin, or (ii) if it is
12336 inside the left margin and the window is already
12337 hscrolled. */
12338 && ((!row_r2l_p
12339 && ((w->hscroll
12340 && w->cursor.x <= h_margin)
12341 || (cursor_row->enabled_p
12342 && cursor_row->truncated_on_right_p
12343 && (w->cursor.x >= text_area_width - h_margin))))
12344 /* For right-to-left rows, the logic is similar,
12345 except that rules for scrolling to left and right
12346 are reversed. E.g., if cursor.x <= h_margin, we
12347 need to hscroll "to the right" unconditionally,
12348 and that will scroll the screen to the left so as
12349 to reveal the next portion of the row. */
12350 || (row_r2l_p
12351 && ((cursor_row->enabled_p
12352 /* FIXME: It is confusing to set the
12353 truncated_on_right_p flag when R2L rows
12354 are actually truncated on the left. */
12355 && cursor_row->truncated_on_right_p
12356 && w->cursor.x <= h_margin)
12357 || (w->hscroll
12358 && (w->cursor.x >= text_area_width - h_margin))))))
12359 {
12360 struct it it;
12361 ptrdiff_t hscroll;
12362 struct buffer *saved_current_buffer;
12363 ptrdiff_t pt;
12364 int wanted_x;
12365
12366 /* Find point in a display of infinite width. */
12367 saved_current_buffer = current_buffer;
12368 current_buffer = XBUFFER (w->buffer);
12369
12370 if (w == XWINDOW (selected_window))
12371 pt = PT;
12372 else
12373 {
12374 pt = marker_position (w->pointm);
12375 pt = max (BEGV, pt);
12376 pt = min (ZV, pt);
12377 }
12378
12379 /* Move iterator to pt starting at cursor_row->start in
12380 a line with infinite width. */
12381 init_to_row_start (&it, w, cursor_row);
12382 it.last_visible_x = INFINITY;
12383 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12384 current_buffer = saved_current_buffer;
12385
12386 /* Position cursor in window. */
12387 if (!hscroll_relative_p && hscroll_step_abs == 0)
12388 hscroll = max (0, (it.current_x
12389 - (ITERATOR_AT_END_OF_LINE_P (&it)
12390 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12391 : (text_area_width / 2))))
12392 / FRAME_COLUMN_WIDTH (it.f);
12393 else if ((!row_r2l_p
12394 && w->cursor.x >= text_area_width - h_margin)
12395 || (row_r2l_p && w->cursor.x <= h_margin))
12396 {
12397 if (hscroll_relative_p)
12398 wanted_x = text_area_width * (1 - hscroll_step_rel)
12399 - h_margin;
12400 else
12401 wanted_x = text_area_width
12402 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12403 - h_margin;
12404 hscroll
12405 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12406 }
12407 else
12408 {
12409 if (hscroll_relative_p)
12410 wanted_x = text_area_width * hscroll_step_rel
12411 + h_margin;
12412 else
12413 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12414 + h_margin;
12415 hscroll
12416 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12417 }
12418 hscroll = max (hscroll, w->min_hscroll);
12419
12420 /* Don't prevent redisplay optimizations if hscroll
12421 hasn't changed, as it will unnecessarily slow down
12422 redisplay. */
12423 if (w->hscroll != hscroll)
12424 {
12425 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12426 w->hscroll = hscroll;
12427 hscrolled_p = 1;
12428 }
12429 }
12430 }
12431
12432 window = w->next;
12433 }
12434
12435 /* Value is non-zero if hscroll of any leaf window has been changed. */
12436 return hscrolled_p;
12437 }
12438
12439
12440 /* Set hscroll so that cursor is visible and not inside horizontal
12441 scroll margins for all windows in the tree rooted at WINDOW. See
12442 also hscroll_window_tree above. Value is non-zero if any window's
12443 hscroll has been changed. If it has, desired matrices on the frame
12444 of WINDOW are cleared. */
12445
12446 static int
12447 hscroll_windows (Lisp_Object window)
12448 {
12449 int hscrolled_p = hscroll_window_tree (window);
12450 if (hscrolled_p)
12451 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12452 return hscrolled_p;
12453 }
12454
12455
12456 \f
12457 /************************************************************************
12458 Redisplay
12459 ************************************************************************/
12460
12461 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12462 to a non-zero value. This is sometimes handy to have in a debugger
12463 session. */
12464
12465 #ifdef GLYPH_DEBUG
12466
12467 /* First and last unchanged row for try_window_id. */
12468
12469 static int debug_first_unchanged_at_end_vpos;
12470 static int debug_last_unchanged_at_beg_vpos;
12471
12472 /* Delta vpos and y. */
12473
12474 static int debug_dvpos, debug_dy;
12475
12476 /* Delta in characters and bytes for try_window_id. */
12477
12478 static ptrdiff_t debug_delta, debug_delta_bytes;
12479
12480 /* Values of window_end_pos and window_end_vpos at the end of
12481 try_window_id. */
12482
12483 static ptrdiff_t debug_end_vpos;
12484
12485 /* Append a string to W->desired_matrix->method. FMT is a printf
12486 format string. If trace_redisplay_p is non-zero also printf the
12487 resulting string to stderr. */
12488
12489 static void debug_method_add (struct window *, char const *, ...)
12490 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12491
12492 static void
12493 debug_method_add (struct window *w, char const *fmt, ...)
12494 {
12495 char buffer[512];
12496 char *method = w->desired_matrix->method;
12497 int len = strlen (method);
12498 int size = sizeof w->desired_matrix->method;
12499 int remaining = size - len - 1;
12500 va_list ap;
12501
12502 va_start (ap, fmt);
12503 vsprintf (buffer, fmt, ap);
12504 va_end (ap);
12505 if (len && remaining)
12506 {
12507 method[len] = '|';
12508 --remaining, ++len;
12509 }
12510
12511 strncpy (method + len, buffer, remaining);
12512
12513 if (trace_redisplay_p)
12514 fprintf (stderr, "%p (%s): %s\n",
12515 w,
12516 ((BUFFERP (w->buffer)
12517 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12518 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12519 : "no buffer"),
12520 buffer);
12521 }
12522
12523 #endif /* GLYPH_DEBUG */
12524
12525
12526 /* Value is non-zero if all changes in window W, which displays
12527 current_buffer, are in the text between START and END. START is a
12528 buffer position, END is given as a distance from Z. Used in
12529 redisplay_internal for display optimization. */
12530
12531 static inline int
12532 text_outside_line_unchanged_p (struct window *w,
12533 ptrdiff_t start, ptrdiff_t end)
12534 {
12535 int unchanged_p = 1;
12536
12537 /* If text or overlays have changed, see where. */
12538 if (w->last_modified < MODIFF
12539 || w->last_overlay_modified < OVERLAY_MODIFF)
12540 {
12541 /* Gap in the line? */
12542 if (GPT < start || Z - GPT < end)
12543 unchanged_p = 0;
12544
12545 /* Changes start in front of the line, or end after it? */
12546 if (unchanged_p
12547 && (BEG_UNCHANGED < start - 1
12548 || END_UNCHANGED < end))
12549 unchanged_p = 0;
12550
12551 /* If selective display, can't optimize if changes start at the
12552 beginning of the line. */
12553 if (unchanged_p
12554 && INTEGERP (BVAR (current_buffer, selective_display))
12555 && XINT (BVAR (current_buffer, selective_display)) > 0
12556 && (BEG_UNCHANGED < start || GPT <= start))
12557 unchanged_p = 0;
12558
12559 /* If there are overlays at the start or end of the line, these
12560 may have overlay strings with newlines in them. A change at
12561 START, for instance, may actually concern the display of such
12562 overlay strings as well, and they are displayed on different
12563 lines. So, quickly rule out this case. (For the future, it
12564 might be desirable to implement something more telling than
12565 just BEG/END_UNCHANGED.) */
12566 if (unchanged_p)
12567 {
12568 if (BEG + BEG_UNCHANGED == start
12569 && overlay_touches_p (start))
12570 unchanged_p = 0;
12571 if (END_UNCHANGED == end
12572 && overlay_touches_p (Z - end))
12573 unchanged_p = 0;
12574 }
12575
12576 /* Under bidi reordering, adding or deleting a character in the
12577 beginning of a paragraph, before the first strong directional
12578 character, can change the base direction of the paragraph (unless
12579 the buffer specifies a fixed paragraph direction), which will
12580 require to redisplay the whole paragraph. It might be worthwhile
12581 to find the paragraph limits and widen the range of redisplayed
12582 lines to that, but for now just give up this optimization. */
12583 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12584 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12585 unchanged_p = 0;
12586 }
12587
12588 return unchanged_p;
12589 }
12590
12591
12592 /* Do a frame update, taking possible shortcuts into account. This is
12593 the main external entry point for redisplay.
12594
12595 If the last redisplay displayed an echo area message and that message
12596 is no longer requested, we clear the echo area or bring back the
12597 mini-buffer if that is in use. */
12598
12599 void
12600 redisplay (void)
12601 {
12602 redisplay_internal ();
12603 }
12604
12605
12606 static Lisp_Object
12607 overlay_arrow_string_or_property (Lisp_Object var)
12608 {
12609 Lisp_Object val;
12610
12611 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12612 return val;
12613
12614 return Voverlay_arrow_string;
12615 }
12616
12617 /* Return 1 if there are any overlay-arrows in current_buffer. */
12618 static int
12619 overlay_arrow_in_current_buffer_p (void)
12620 {
12621 Lisp_Object vlist;
12622
12623 for (vlist = Voverlay_arrow_variable_list;
12624 CONSP (vlist);
12625 vlist = XCDR (vlist))
12626 {
12627 Lisp_Object var = XCAR (vlist);
12628 Lisp_Object val;
12629
12630 if (!SYMBOLP (var))
12631 continue;
12632 val = find_symbol_value (var);
12633 if (MARKERP (val)
12634 && current_buffer == XMARKER (val)->buffer)
12635 return 1;
12636 }
12637 return 0;
12638 }
12639
12640
12641 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12642 has changed. */
12643
12644 static int
12645 overlay_arrows_changed_p (void)
12646 {
12647 Lisp_Object vlist;
12648
12649 for (vlist = Voverlay_arrow_variable_list;
12650 CONSP (vlist);
12651 vlist = XCDR (vlist))
12652 {
12653 Lisp_Object var = XCAR (vlist);
12654 Lisp_Object val, pstr;
12655
12656 if (!SYMBOLP (var))
12657 continue;
12658 val = find_symbol_value (var);
12659 if (!MARKERP (val))
12660 continue;
12661 if (! EQ (COERCE_MARKER (val),
12662 Fget (var, Qlast_arrow_position))
12663 || ! (pstr = overlay_arrow_string_or_property (var),
12664 EQ (pstr, Fget (var, Qlast_arrow_string))))
12665 return 1;
12666 }
12667 return 0;
12668 }
12669
12670 /* Mark overlay arrows to be updated on next redisplay. */
12671
12672 static void
12673 update_overlay_arrows (int up_to_date)
12674 {
12675 Lisp_Object vlist;
12676
12677 for (vlist = Voverlay_arrow_variable_list;
12678 CONSP (vlist);
12679 vlist = XCDR (vlist))
12680 {
12681 Lisp_Object var = XCAR (vlist);
12682
12683 if (!SYMBOLP (var))
12684 continue;
12685
12686 if (up_to_date > 0)
12687 {
12688 Lisp_Object val = find_symbol_value (var);
12689 Fput (var, Qlast_arrow_position,
12690 COERCE_MARKER (val));
12691 Fput (var, Qlast_arrow_string,
12692 overlay_arrow_string_or_property (var));
12693 }
12694 else if (up_to_date < 0
12695 || !NILP (Fget (var, Qlast_arrow_position)))
12696 {
12697 Fput (var, Qlast_arrow_position, Qt);
12698 Fput (var, Qlast_arrow_string, Qt);
12699 }
12700 }
12701 }
12702
12703
12704 /* Return overlay arrow string to display at row.
12705 Return integer (bitmap number) for arrow bitmap in left fringe.
12706 Return nil if no overlay arrow. */
12707
12708 static Lisp_Object
12709 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12710 {
12711 Lisp_Object vlist;
12712
12713 for (vlist = Voverlay_arrow_variable_list;
12714 CONSP (vlist);
12715 vlist = XCDR (vlist))
12716 {
12717 Lisp_Object var = XCAR (vlist);
12718 Lisp_Object val;
12719
12720 if (!SYMBOLP (var))
12721 continue;
12722
12723 val = find_symbol_value (var);
12724
12725 if (MARKERP (val)
12726 && current_buffer == XMARKER (val)->buffer
12727 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12728 {
12729 if (FRAME_WINDOW_P (it->f)
12730 /* FIXME: if ROW->reversed_p is set, this should test
12731 the right fringe, not the left one. */
12732 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12733 {
12734 #ifdef HAVE_WINDOW_SYSTEM
12735 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12736 {
12737 int fringe_bitmap;
12738 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12739 return make_number (fringe_bitmap);
12740 }
12741 #endif
12742 return make_number (-1); /* Use default arrow bitmap */
12743 }
12744 return overlay_arrow_string_or_property (var);
12745 }
12746 }
12747
12748 return Qnil;
12749 }
12750
12751 /* Return 1 if point moved out of or into a composition. Otherwise
12752 return 0. PREV_BUF and PREV_PT are the last point buffer and
12753 position. BUF and PT are the current point buffer and position. */
12754
12755 static int
12756 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12757 struct buffer *buf, ptrdiff_t pt)
12758 {
12759 ptrdiff_t start, end;
12760 Lisp_Object prop;
12761 Lisp_Object buffer;
12762
12763 XSETBUFFER (buffer, buf);
12764 /* Check a composition at the last point if point moved within the
12765 same buffer. */
12766 if (prev_buf == buf)
12767 {
12768 if (prev_pt == pt)
12769 /* Point didn't move. */
12770 return 0;
12771
12772 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12773 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12774 && COMPOSITION_VALID_P (start, end, prop)
12775 && start < prev_pt && end > prev_pt)
12776 /* The last point was within the composition. Return 1 iff
12777 point moved out of the composition. */
12778 return (pt <= start || pt >= end);
12779 }
12780
12781 /* Check a composition at the current point. */
12782 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12783 && find_composition (pt, -1, &start, &end, &prop, buffer)
12784 && COMPOSITION_VALID_P (start, end, prop)
12785 && start < pt && end > pt);
12786 }
12787
12788
12789 /* Reconsider the setting of B->clip_changed which is displayed
12790 in window W. */
12791
12792 static inline void
12793 reconsider_clip_changes (struct window *w, struct buffer *b)
12794 {
12795 if (b->clip_changed
12796 && !NILP (w->window_end_valid)
12797 && w->current_matrix->buffer == b
12798 && w->current_matrix->zv == BUF_ZV (b)
12799 && w->current_matrix->begv == BUF_BEGV (b))
12800 b->clip_changed = 0;
12801
12802 /* If display wasn't paused, and W is not a tool bar window, see if
12803 point has been moved into or out of a composition. In that case,
12804 we set b->clip_changed to 1 to force updating the screen. If
12805 b->clip_changed has already been set to 1, we can skip this
12806 check. */
12807 if (!b->clip_changed
12808 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12809 {
12810 ptrdiff_t pt;
12811
12812 if (w == XWINDOW (selected_window))
12813 pt = PT;
12814 else
12815 pt = marker_position (w->pointm);
12816
12817 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12818 || pt != w->last_point)
12819 && check_point_in_composition (w->current_matrix->buffer,
12820 w->last_point,
12821 XBUFFER (w->buffer), pt))
12822 b->clip_changed = 1;
12823 }
12824 }
12825 \f
12826
12827 /* Select FRAME to forward the values of frame-local variables into C
12828 variables so that the redisplay routines can access those values
12829 directly. */
12830
12831 static void
12832 select_frame_for_redisplay (Lisp_Object frame)
12833 {
12834 Lisp_Object tail, tem;
12835 Lisp_Object old = selected_frame;
12836 struct Lisp_Symbol *sym;
12837
12838 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12839
12840 selected_frame = frame;
12841
12842 do {
12843 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12844 if (CONSP (XCAR (tail))
12845 && (tem = XCAR (XCAR (tail)),
12846 SYMBOLP (tem))
12847 && (sym = indirect_variable (XSYMBOL (tem)),
12848 sym->redirect == SYMBOL_LOCALIZED)
12849 && sym->val.blv->frame_local)
12850 /* Use find_symbol_value rather than Fsymbol_value
12851 to avoid an error if it is void. */
12852 find_symbol_value (tem);
12853 } while (!EQ (frame, old) && (frame = old, 1));
12854 }
12855
12856
12857 #define STOP_POLLING \
12858 do { if (! polling_stopped_here) stop_polling (); \
12859 polling_stopped_here = 1; } while (0)
12860
12861 #define RESUME_POLLING \
12862 do { if (polling_stopped_here) start_polling (); \
12863 polling_stopped_here = 0; } while (0)
12864
12865
12866 /* Perhaps in the future avoid recentering windows if it
12867 is not necessary; currently that causes some problems. */
12868
12869 static void
12870 redisplay_internal (void)
12871 {
12872 struct window *w = XWINDOW (selected_window);
12873 struct window *sw;
12874 struct frame *fr;
12875 int pending;
12876 int must_finish = 0;
12877 struct text_pos tlbufpos, tlendpos;
12878 int number_of_visible_frames;
12879 ptrdiff_t count, count1;
12880 struct frame *sf;
12881 int polling_stopped_here = 0;
12882 Lisp_Object old_frame = selected_frame;
12883
12884 /* Non-zero means redisplay has to consider all windows on all
12885 frames. Zero means, only selected_window is considered. */
12886 int consider_all_windows_p;
12887
12888 /* Non-zero means redisplay has to redisplay the miniwindow */
12889 int update_miniwindow_p = 0;
12890
12891 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12892
12893 /* No redisplay if running in batch mode or frame is not yet fully
12894 initialized, or redisplay is explicitly turned off by setting
12895 Vinhibit_redisplay. */
12896 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12897 || !NILP (Vinhibit_redisplay))
12898 return;
12899
12900 /* Don't examine these until after testing Vinhibit_redisplay.
12901 When Emacs is shutting down, perhaps because its connection to
12902 X has dropped, we should not look at them at all. */
12903 fr = XFRAME (w->frame);
12904 sf = SELECTED_FRAME ();
12905
12906 if (!fr->glyphs_initialized_p)
12907 return;
12908
12909 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12910 if (popup_activated ())
12911 return;
12912 #endif
12913
12914 /* I don't think this happens but let's be paranoid. */
12915 if (redisplaying_p)
12916 return;
12917
12918 /* Record a function that resets redisplaying_p to its old value
12919 when we leave this function. */
12920 count = SPECPDL_INDEX ();
12921 record_unwind_protect (unwind_redisplay,
12922 Fcons (make_number (redisplaying_p), selected_frame));
12923 ++redisplaying_p;
12924 specbind (Qinhibit_free_realized_faces, Qnil);
12925
12926 {
12927 Lisp_Object tail, frame;
12928
12929 FOR_EACH_FRAME (tail, frame)
12930 {
12931 struct frame *f = XFRAME (frame);
12932 f->already_hscrolled_p = 0;
12933 }
12934 }
12935
12936 retry:
12937 /* Remember the currently selected window. */
12938 sw = w;
12939
12940 if (!EQ (old_frame, selected_frame)
12941 && FRAME_LIVE_P (XFRAME (old_frame)))
12942 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12943 selected_frame and selected_window to be temporarily out-of-sync so
12944 when we come back here via `goto retry', we need to resync because we
12945 may need to run Elisp code (via prepare_menu_bars). */
12946 select_frame_for_redisplay (old_frame);
12947
12948 pending = 0;
12949 reconsider_clip_changes (w, current_buffer);
12950 last_escape_glyph_frame = NULL;
12951 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12952 last_glyphless_glyph_frame = NULL;
12953 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12954
12955 /* If new fonts have been loaded that make a glyph matrix adjustment
12956 necessary, do it. */
12957 if (fonts_changed_p)
12958 {
12959 adjust_glyphs (NULL);
12960 ++windows_or_buffers_changed;
12961 fonts_changed_p = 0;
12962 }
12963
12964 /* If face_change_count is non-zero, init_iterator will free all
12965 realized faces, which includes the faces referenced from current
12966 matrices. So, we can't reuse current matrices in this case. */
12967 if (face_change_count)
12968 ++windows_or_buffers_changed;
12969
12970 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12971 && FRAME_TTY (sf)->previous_frame != sf)
12972 {
12973 /* Since frames on a single ASCII terminal share the same
12974 display area, displaying a different frame means redisplay
12975 the whole thing. */
12976 windows_or_buffers_changed++;
12977 SET_FRAME_GARBAGED (sf);
12978 #ifndef DOS_NT
12979 set_tty_color_mode (FRAME_TTY (sf), sf);
12980 #endif
12981 FRAME_TTY (sf)->previous_frame = sf;
12982 }
12983
12984 /* Set the visible flags for all frames. Do this before checking
12985 for resized or garbaged frames; they want to know if their frames
12986 are visible. See the comment in frame.h for
12987 FRAME_SAMPLE_VISIBILITY. */
12988 {
12989 Lisp_Object tail, frame;
12990
12991 number_of_visible_frames = 0;
12992
12993 FOR_EACH_FRAME (tail, frame)
12994 {
12995 struct frame *f = XFRAME (frame);
12996
12997 FRAME_SAMPLE_VISIBILITY (f);
12998 if (FRAME_VISIBLE_P (f))
12999 ++number_of_visible_frames;
13000 clear_desired_matrices (f);
13001 }
13002 }
13003
13004 /* Notice any pending interrupt request to change frame size. */
13005 do_pending_window_change (1);
13006
13007 /* do_pending_window_change could change the selected_window due to
13008 frame resizing which makes the selected window too small. */
13009 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13010 {
13011 sw = w;
13012 reconsider_clip_changes (w, current_buffer);
13013 }
13014
13015 /* Clear frames marked as garbaged. */
13016 if (frame_garbaged)
13017 clear_garbaged_frames ();
13018
13019 /* Build menubar and tool-bar items. */
13020 if (NILP (Vmemory_full))
13021 prepare_menu_bars ();
13022
13023 if (windows_or_buffers_changed)
13024 update_mode_lines++;
13025
13026 /* Detect case that we need to write or remove a star in the mode line. */
13027 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13028 {
13029 w->update_mode_line = 1;
13030 if (buffer_shared > 1)
13031 update_mode_lines++;
13032 }
13033
13034 /* Avoid invocation of point motion hooks by `current_column' below. */
13035 count1 = SPECPDL_INDEX ();
13036 specbind (Qinhibit_point_motion_hooks, Qt);
13037
13038 /* If %c is in the mode line, update it if needed. */
13039 if (!NILP (w->column_number_displayed)
13040 /* This alternative quickly identifies a common case
13041 where no change is needed. */
13042 && !(PT == w->last_point
13043 && w->last_modified >= MODIFF
13044 && w->last_overlay_modified >= OVERLAY_MODIFF)
13045 && (XFASTINT (w->column_number_displayed) != current_column ()))
13046 w->update_mode_line = 1;
13047
13048 unbind_to (count1, Qnil);
13049
13050 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13051
13052 /* The variable buffer_shared is set in redisplay_window and
13053 indicates that we redisplay a buffer in different windows. See
13054 there. */
13055 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13056 || cursor_type_changed);
13057
13058 /* If specs for an arrow have changed, do thorough redisplay
13059 to ensure we remove any arrow that should no longer exist. */
13060 if (overlay_arrows_changed_p ())
13061 consider_all_windows_p = windows_or_buffers_changed = 1;
13062
13063 /* Normally the message* functions will have already displayed and
13064 updated the echo area, but the frame may have been trashed, or
13065 the update may have been preempted, so display the echo area
13066 again here. Checking message_cleared_p captures the case that
13067 the echo area should be cleared. */
13068 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13069 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13070 || (message_cleared_p
13071 && minibuf_level == 0
13072 /* If the mini-window is currently selected, this means the
13073 echo-area doesn't show through. */
13074 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13075 {
13076 int window_height_changed_p = echo_area_display (0);
13077
13078 if (message_cleared_p)
13079 update_miniwindow_p = 1;
13080
13081 must_finish = 1;
13082
13083 /* If we don't display the current message, don't clear the
13084 message_cleared_p flag, because, if we did, we wouldn't clear
13085 the echo area in the next redisplay which doesn't preserve
13086 the echo area. */
13087 if (!display_last_displayed_message_p)
13088 message_cleared_p = 0;
13089
13090 if (fonts_changed_p)
13091 goto retry;
13092 else if (window_height_changed_p)
13093 {
13094 consider_all_windows_p = 1;
13095 ++update_mode_lines;
13096 ++windows_or_buffers_changed;
13097
13098 /* If window configuration was changed, frames may have been
13099 marked garbaged. Clear them or we will experience
13100 surprises wrt scrolling. */
13101 if (frame_garbaged)
13102 clear_garbaged_frames ();
13103 }
13104 }
13105 else if (EQ (selected_window, minibuf_window)
13106 && (current_buffer->clip_changed
13107 || w->last_modified < MODIFF
13108 || w->last_overlay_modified < OVERLAY_MODIFF)
13109 && resize_mini_window (w, 0))
13110 {
13111 /* Resized active mini-window to fit the size of what it is
13112 showing if its contents might have changed. */
13113 must_finish = 1;
13114 /* FIXME: this causes all frames to be updated, which seems unnecessary
13115 since only the current frame needs to be considered. This function needs
13116 to be rewritten with two variables, consider_all_windows and
13117 consider_all_frames. */
13118 consider_all_windows_p = 1;
13119 ++windows_or_buffers_changed;
13120 ++update_mode_lines;
13121
13122 /* If window configuration was changed, frames may have been
13123 marked garbaged. Clear them or we will experience
13124 surprises wrt scrolling. */
13125 if (frame_garbaged)
13126 clear_garbaged_frames ();
13127 }
13128
13129
13130 /* If showing the region, and mark has changed, we must redisplay
13131 the whole window. The assignment to this_line_start_pos prevents
13132 the optimization directly below this if-statement. */
13133 if (((!NILP (Vtransient_mark_mode)
13134 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13135 != !NILP (w->region_showing))
13136 || (!NILP (w->region_showing)
13137 && !EQ (w->region_showing,
13138 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13139 CHARPOS (this_line_start_pos) = 0;
13140
13141 /* Optimize the case that only the line containing the cursor in the
13142 selected window has changed. Variables starting with this_ are
13143 set in display_line and record information about the line
13144 containing the cursor. */
13145 tlbufpos = this_line_start_pos;
13146 tlendpos = this_line_end_pos;
13147 if (!consider_all_windows_p
13148 && CHARPOS (tlbufpos) > 0
13149 && !w->update_mode_line
13150 && !current_buffer->clip_changed
13151 && !current_buffer->prevent_redisplay_optimizations_p
13152 && FRAME_VISIBLE_P (XFRAME (w->frame))
13153 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13154 /* Make sure recorded data applies to current buffer, etc. */
13155 && this_line_buffer == current_buffer
13156 && current_buffer == XBUFFER (w->buffer)
13157 && !w->force_start
13158 && !w->optional_new_start
13159 /* Point must be on the line that we have info recorded about. */
13160 && PT >= CHARPOS (tlbufpos)
13161 && PT <= Z - CHARPOS (tlendpos)
13162 /* All text outside that line, including its final newline,
13163 must be unchanged. */
13164 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13165 CHARPOS (tlendpos)))
13166 {
13167 if (CHARPOS (tlbufpos) > BEGV
13168 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13169 && (CHARPOS (tlbufpos) == ZV
13170 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13171 /* Former continuation line has disappeared by becoming empty. */
13172 goto cancel;
13173 else if (w->last_modified < MODIFF
13174 || w->last_overlay_modified < OVERLAY_MODIFF
13175 || MINI_WINDOW_P (w))
13176 {
13177 /* We have to handle the case of continuation around a
13178 wide-column character (see the comment in indent.c around
13179 line 1340).
13180
13181 For instance, in the following case:
13182
13183 -------- Insert --------
13184 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13185 J_I_ ==> J_I_ `^^' are cursors.
13186 ^^ ^^
13187 -------- --------
13188
13189 As we have to redraw the line above, we cannot use this
13190 optimization. */
13191
13192 struct it it;
13193 int line_height_before = this_line_pixel_height;
13194
13195 /* Note that start_display will handle the case that the
13196 line starting at tlbufpos is a continuation line. */
13197 start_display (&it, w, tlbufpos);
13198
13199 /* Implementation note: It this still necessary? */
13200 if (it.current_x != this_line_start_x)
13201 goto cancel;
13202
13203 TRACE ((stderr, "trying display optimization 1\n"));
13204 w->cursor.vpos = -1;
13205 overlay_arrow_seen = 0;
13206 it.vpos = this_line_vpos;
13207 it.current_y = this_line_y;
13208 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13209 display_line (&it);
13210
13211 /* If line contains point, is not continued,
13212 and ends at same distance from eob as before, we win. */
13213 if (w->cursor.vpos >= 0
13214 /* Line is not continued, otherwise this_line_start_pos
13215 would have been set to 0 in display_line. */
13216 && CHARPOS (this_line_start_pos)
13217 /* Line ends as before. */
13218 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13219 /* Line has same height as before. Otherwise other lines
13220 would have to be shifted up or down. */
13221 && this_line_pixel_height == line_height_before)
13222 {
13223 /* If this is not the window's last line, we must adjust
13224 the charstarts of the lines below. */
13225 if (it.current_y < it.last_visible_y)
13226 {
13227 struct glyph_row *row
13228 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13229 ptrdiff_t delta, delta_bytes;
13230
13231 /* We used to distinguish between two cases here,
13232 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13233 when the line ends in a newline or the end of the
13234 buffer's accessible portion. But both cases did
13235 the same, so they were collapsed. */
13236 delta = (Z
13237 - CHARPOS (tlendpos)
13238 - MATRIX_ROW_START_CHARPOS (row));
13239 delta_bytes = (Z_BYTE
13240 - BYTEPOS (tlendpos)
13241 - MATRIX_ROW_START_BYTEPOS (row));
13242
13243 increment_matrix_positions (w->current_matrix,
13244 this_line_vpos + 1,
13245 w->current_matrix->nrows,
13246 delta, delta_bytes);
13247 }
13248
13249 /* If this row displays text now but previously didn't,
13250 or vice versa, w->window_end_vpos may have to be
13251 adjusted. */
13252 if ((it.glyph_row - 1)->displays_text_p)
13253 {
13254 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13255 XSETINT (w->window_end_vpos, this_line_vpos);
13256 }
13257 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13258 && this_line_vpos > 0)
13259 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13260 w->window_end_valid = Qnil;
13261
13262 /* Update hint: No need to try to scroll in update_window. */
13263 w->desired_matrix->no_scrolling_p = 1;
13264
13265 #ifdef GLYPH_DEBUG
13266 *w->desired_matrix->method = 0;
13267 debug_method_add (w, "optimization 1");
13268 #endif
13269 #ifdef HAVE_WINDOW_SYSTEM
13270 update_window_fringes (w, 0);
13271 #endif
13272 goto update;
13273 }
13274 else
13275 goto cancel;
13276 }
13277 else if (/* Cursor position hasn't changed. */
13278 PT == w->last_point
13279 /* Make sure the cursor was last displayed
13280 in this window. Otherwise we have to reposition it. */
13281 && 0 <= w->cursor.vpos
13282 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13283 {
13284 if (!must_finish)
13285 {
13286 do_pending_window_change (1);
13287 /* If selected_window changed, redisplay again. */
13288 if (WINDOWP (selected_window)
13289 && (w = XWINDOW (selected_window)) != sw)
13290 goto retry;
13291
13292 /* We used to always goto end_of_redisplay here, but this
13293 isn't enough if we have a blinking cursor. */
13294 if (w->cursor_off_p == w->last_cursor_off_p)
13295 goto end_of_redisplay;
13296 }
13297 goto update;
13298 }
13299 /* If highlighting the region, or if the cursor is in the echo area,
13300 then we can't just move the cursor. */
13301 else if (! (!NILP (Vtransient_mark_mode)
13302 && !NILP (BVAR (current_buffer, mark_active)))
13303 && (EQ (selected_window,
13304 BVAR (current_buffer, last_selected_window))
13305 || highlight_nonselected_windows)
13306 && NILP (w->region_showing)
13307 && NILP (Vshow_trailing_whitespace)
13308 && !cursor_in_echo_area)
13309 {
13310 struct it it;
13311 struct glyph_row *row;
13312
13313 /* Skip from tlbufpos to PT and see where it is. Note that
13314 PT may be in invisible text. If so, we will end at the
13315 next visible position. */
13316 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13317 NULL, DEFAULT_FACE_ID);
13318 it.current_x = this_line_start_x;
13319 it.current_y = this_line_y;
13320 it.vpos = this_line_vpos;
13321
13322 /* The call to move_it_to stops in front of PT, but
13323 moves over before-strings. */
13324 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13325
13326 if (it.vpos == this_line_vpos
13327 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13328 row->enabled_p))
13329 {
13330 eassert (this_line_vpos == it.vpos);
13331 eassert (this_line_y == it.current_y);
13332 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13333 #ifdef GLYPH_DEBUG
13334 *w->desired_matrix->method = 0;
13335 debug_method_add (w, "optimization 3");
13336 #endif
13337 goto update;
13338 }
13339 else
13340 goto cancel;
13341 }
13342
13343 cancel:
13344 /* Text changed drastically or point moved off of line. */
13345 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13346 }
13347
13348 CHARPOS (this_line_start_pos) = 0;
13349 consider_all_windows_p |= buffer_shared > 1;
13350 ++clear_face_cache_count;
13351 #ifdef HAVE_WINDOW_SYSTEM
13352 ++clear_image_cache_count;
13353 #endif
13354
13355 /* Build desired matrices, and update the display. If
13356 consider_all_windows_p is non-zero, do it for all windows on all
13357 frames. Otherwise do it for selected_window, only. */
13358
13359 if (consider_all_windows_p)
13360 {
13361 Lisp_Object tail, frame;
13362
13363 FOR_EACH_FRAME (tail, frame)
13364 XFRAME (frame)->updated_p = 0;
13365
13366 /* Recompute # windows showing selected buffer. This will be
13367 incremented each time such a window is displayed. */
13368 buffer_shared = 0;
13369
13370 FOR_EACH_FRAME (tail, frame)
13371 {
13372 struct frame *f = XFRAME (frame);
13373
13374 /* We don't have to do anything for unselected terminal
13375 frames. */
13376 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13377 && !EQ (FRAME_TTY (f)->top_frame, frame))
13378 continue;
13379
13380 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13381 {
13382 if (! EQ (frame, selected_frame))
13383 /* Select the frame, for the sake of frame-local
13384 variables. */
13385 select_frame_for_redisplay (frame);
13386
13387 /* Mark all the scroll bars to be removed; we'll redeem
13388 the ones we want when we redisplay their windows. */
13389 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13390 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13391
13392 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13393 redisplay_windows (FRAME_ROOT_WINDOW (f));
13394
13395 /* The X error handler may have deleted that frame. */
13396 if (!FRAME_LIVE_P (f))
13397 continue;
13398
13399 /* Any scroll bars which redisplay_windows should have
13400 nuked should now go away. */
13401 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13402 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13403
13404 /* If fonts changed, display again. */
13405 /* ??? rms: I suspect it is a mistake to jump all the way
13406 back to retry here. It should just retry this frame. */
13407 if (fonts_changed_p)
13408 goto retry;
13409
13410 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13411 {
13412 /* See if we have to hscroll. */
13413 if (!f->already_hscrolled_p)
13414 {
13415 f->already_hscrolled_p = 1;
13416 if (hscroll_windows (f->root_window))
13417 goto retry;
13418 }
13419
13420 /* Prevent various kinds of signals during display
13421 update. stdio is not robust about handling
13422 signals, which can cause an apparent I/O
13423 error. */
13424 if (interrupt_input)
13425 unrequest_sigio ();
13426 STOP_POLLING;
13427
13428 /* Update the display. */
13429 set_window_update_flags (XWINDOW (f->root_window), 1);
13430 pending |= update_frame (f, 0, 0);
13431 f->updated_p = 1;
13432 }
13433 }
13434 }
13435
13436 if (!EQ (old_frame, selected_frame)
13437 && FRAME_LIVE_P (XFRAME (old_frame)))
13438 /* We played a bit fast-and-loose above and allowed selected_frame
13439 and selected_window to be temporarily out-of-sync but let's make
13440 sure this stays contained. */
13441 select_frame_for_redisplay (old_frame);
13442 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13443
13444 if (!pending)
13445 {
13446 /* Do the mark_window_display_accurate after all windows have
13447 been redisplayed because this call resets flags in buffers
13448 which are needed for proper redisplay. */
13449 FOR_EACH_FRAME (tail, frame)
13450 {
13451 struct frame *f = XFRAME (frame);
13452 if (f->updated_p)
13453 {
13454 mark_window_display_accurate (f->root_window, 1);
13455 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13456 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13457 }
13458 }
13459 }
13460 }
13461 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13462 {
13463 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13464 struct frame *mini_frame;
13465
13466 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13467 /* Use list_of_error, not Qerror, so that
13468 we catch only errors and don't run the debugger. */
13469 internal_condition_case_1 (redisplay_window_1, selected_window,
13470 list_of_error,
13471 redisplay_window_error);
13472 if (update_miniwindow_p)
13473 internal_condition_case_1 (redisplay_window_1, mini_window,
13474 list_of_error,
13475 redisplay_window_error);
13476
13477 /* Compare desired and current matrices, perform output. */
13478
13479 update:
13480 /* If fonts changed, display again. */
13481 if (fonts_changed_p)
13482 goto retry;
13483
13484 /* Prevent various kinds of signals during display update.
13485 stdio is not robust about handling signals,
13486 which can cause an apparent I/O error. */
13487 if (interrupt_input)
13488 unrequest_sigio ();
13489 STOP_POLLING;
13490
13491 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13492 {
13493 if (hscroll_windows (selected_window))
13494 goto retry;
13495
13496 XWINDOW (selected_window)->must_be_updated_p = 1;
13497 pending = update_frame (sf, 0, 0);
13498 }
13499
13500 /* We may have called echo_area_display at the top of this
13501 function. If the echo area is on another frame, that may
13502 have put text on a frame other than the selected one, so the
13503 above call to update_frame would not have caught it. Catch
13504 it here. */
13505 mini_window = FRAME_MINIBUF_WINDOW (sf);
13506 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13507
13508 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13509 {
13510 XWINDOW (mini_window)->must_be_updated_p = 1;
13511 pending |= update_frame (mini_frame, 0, 0);
13512 if (!pending && hscroll_windows (mini_window))
13513 goto retry;
13514 }
13515 }
13516
13517 /* If display was paused because of pending input, make sure we do a
13518 thorough update the next time. */
13519 if (pending)
13520 {
13521 /* Prevent the optimization at the beginning of
13522 redisplay_internal that tries a single-line update of the
13523 line containing the cursor in the selected window. */
13524 CHARPOS (this_line_start_pos) = 0;
13525
13526 /* Let the overlay arrow be updated the next time. */
13527 update_overlay_arrows (0);
13528
13529 /* If we pause after scrolling, some rows in the current
13530 matrices of some windows are not valid. */
13531 if (!WINDOW_FULL_WIDTH_P (w)
13532 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13533 update_mode_lines = 1;
13534 }
13535 else
13536 {
13537 if (!consider_all_windows_p)
13538 {
13539 /* This has already been done above if
13540 consider_all_windows_p is set. */
13541 mark_window_display_accurate_1 (w, 1);
13542
13543 /* Say overlay arrows are up to date. */
13544 update_overlay_arrows (1);
13545
13546 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13547 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13548 }
13549
13550 update_mode_lines = 0;
13551 windows_or_buffers_changed = 0;
13552 cursor_type_changed = 0;
13553 }
13554
13555 /* Start SIGIO interrupts coming again. Having them off during the
13556 code above makes it less likely one will discard output, but not
13557 impossible, since there might be stuff in the system buffer here.
13558 But it is much hairier to try to do anything about that. */
13559 if (interrupt_input)
13560 request_sigio ();
13561 RESUME_POLLING;
13562
13563 /* If a frame has become visible which was not before, redisplay
13564 again, so that we display it. Expose events for such a frame
13565 (which it gets when becoming visible) don't call the parts of
13566 redisplay constructing glyphs, so simply exposing a frame won't
13567 display anything in this case. So, we have to display these
13568 frames here explicitly. */
13569 if (!pending)
13570 {
13571 Lisp_Object tail, frame;
13572 int new_count = 0;
13573
13574 FOR_EACH_FRAME (tail, frame)
13575 {
13576 int this_is_visible = 0;
13577
13578 if (XFRAME (frame)->visible)
13579 this_is_visible = 1;
13580 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13581 if (XFRAME (frame)->visible)
13582 this_is_visible = 1;
13583
13584 if (this_is_visible)
13585 new_count++;
13586 }
13587
13588 if (new_count != number_of_visible_frames)
13589 windows_or_buffers_changed++;
13590 }
13591
13592 /* Change frame size now if a change is pending. */
13593 do_pending_window_change (1);
13594
13595 /* If we just did a pending size change, or have additional
13596 visible frames, or selected_window changed, redisplay again. */
13597 if ((windows_or_buffers_changed && !pending)
13598 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13599 goto retry;
13600
13601 /* Clear the face and image caches.
13602
13603 We used to do this only if consider_all_windows_p. But the cache
13604 needs to be cleared if a timer creates images in the current
13605 buffer (e.g. the test case in Bug#6230). */
13606
13607 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13608 {
13609 clear_face_cache (0);
13610 clear_face_cache_count = 0;
13611 }
13612
13613 #ifdef HAVE_WINDOW_SYSTEM
13614 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13615 {
13616 clear_image_caches (Qnil);
13617 clear_image_cache_count = 0;
13618 }
13619 #endif /* HAVE_WINDOW_SYSTEM */
13620
13621 end_of_redisplay:
13622 unbind_to (count, Qnil);
13623 RESUME_POLLING;
13624 }
13625
13626
13627 /* Redisplay, but leave alone any recent echo area message unless
13628 another message has been requested in its place.
13629
13630 This is useful in situations where you need to redisplay but no
13631 user action has occurred, making it inappropriate for the message
13632 area to be cleared. See tracking_off and
13633 wait_reading_process_output for examples of these situations.
13634
13635 FROM_WHERE is an integer saying from where this function was
13636 called. This is useful for debugging. */
13637
13638 void
13639 redisplay_preserve_echo_area (int from_where)
13640 {
13641 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13642
13643 if (!NILP (echo_area_buffer[1]))
13644 {
13645 /* We have a previously displayed message, but no current
13646 message. Redisplay the previous message. */
13647 display_last_displayed_message_p = 1;
13648 redisplay_internal ();
13649 display_last_displayed_message_p = 0;
13650 }
13651 else
13652 redisplay_internal ();
13653
13654 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13655 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13656 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13657 }
13658
13659
13660 /* Function registered with record_unwind_protect in
13661 redisplay_internal. Reset redisplaying_p to the value it had
13662 before redisplay_internal was called, and clear
13663 prevent_freeing_realized_faces_p. It also selects the previously
13664 selected frame, unless it has been deleted (by an X connection
13665 failure during redisplay, for example). */
13666
13667 static Lisp_Object
13668 unwind_redisplay (Lisp_Object val)
13669 {
13670 Lisp_Object old_redisplaying_p, old_frame;
13671
13672 old_redisplaying_p = XCAR (val);
13673 redisplaying_p = XFASTINT (old_redisplaying_p);
13674 old_frame = XCDR (val);
13675 if (! EQ (old_frame, selected_frame)
13676 && FRAME_LIVE_P (XFRAME (old_frame)))
13677 select_frame_for_redisplay (old_frame);
13678 return Qnil;
13679 }
13680
13681
13682 /* Mark the display of window W as accurate or inaccurate. If
13683 ACCURATE_P is non-zero mark display of W as accurate. If
13684 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13685 redisplay_internal is called. */
13686
13687 static void
13688 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13689 {
13690 if (BUFFERP (w->buffer))
13691 {
13692 struct buffer *b = XBUFFER (w->buffer);
13693
13694 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13695 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13696 w->last_had_star
13697 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13698
13699 if (accurate_p)
13700 {
13701 b->clip_changed = 0;
13702 b->prevent_redisplay_optimizations_p = 0;
13703
13704 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13705 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13706 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13707 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13708
13709 w->current_matrix->buffer = b;
13710 w->current_matrix->begv = BUF_BEGV (b);
13711 w->current_matrix->zv = BUF_ZV (b);
13712
13713 w->last_cursor = w->cursor;
13714 w->last_cursor_off_p = w->cursor_off_p;
13715
13716 if (w == XWINDOW (selected_window))
13717 w->last_point = BUF_PT (b);
13718 else
13719 w->last_point = XMARKER (w->pointm)->charpos;
13720 }
13721 }
13722
13723 if (accurate_p)
13724 {
13725 w->window_end_valid = w->buffer;
13726 w->update_mode_line = 0;
13727 }
13728 }
13729
13730
13731 /* Mark the display of windows in the window tree rooted at WINDOW as
13732 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13733 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13734 be redisplayed the next time redisplay_internal is called. */
13735
13736 void
13737 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13738 {
13739 struct window *w;
13740
13741 for (; !NILP (window); window = w->next)
13742 {
13743 w = XWINDOW (window);
13744 mark_window_display_accurate_1 (w, accurate_p);
13745
13746 if (!NILP (w->vchild))
13747 mark_window_display_accurate (w->vchild, accurate_p);
13748 if (!NILP (w->hchild))
13749 mark_window_display_accurate (w->hchild, accurate_p);
13750 }
13751
13752 if (accurate_p)
13753 {
13754 update_overlay_arrows (1);
13755 }
13756 else
13757 {
13758 /* Force a thorough redisplay the next time by setting
13759 last_arrow_position and last_arrow_string to t, which is
13760 unequal to any useful value of Voverlay_arrow_... */
13761 update_overlay_arrows (-1);
13762 }
13763 }
13764
13765
13766 /* Return value in display table DP (Lisp_Char_Table *) for character
13767 C. Since a display table doesn't have any parent, we don't have to
13768 follow parent. Do not call this function directly but use the
13769 macro DISP_CHAR_VECTOR. */
13770
13771 Lisp_Object
13772 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13773 {
13774 Lisp_Object val;
13775
13776 if (ASCII_CHAR_P (c))
13777 {
13778 val = dp->ascii;
13779 if (SUB_CHAR_TABLE_P (val))
13780 val = XSUB_CHAR_TABLE (val)->contents[c];
13781 }
13782 else
13783 {
13784 Lisp_Object table;
13785
13786 XSETCHAR_TABLE (table, dp);
13787 val = char_table_ref (table, c);
13788 }
13789 if (NILP (val))
13790 val = dp->defalt;
13791 return val;
13792 }
13793
13794
13795 \f
13796 /***********************************************************************
13797 Window Redisplay
13798 ***********************************************************************/
13799
13800 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13801
13802 static void
13803 redisplay_windows (Lisp_Object window)
13804 {
13805 while (!NILP (window))
13806 {
13807 struct window *w = XWINDOW (window);
13808
13809 if (!NILP (w->hchild))
13810 redisplay_windows (w->hchild);
13811 else if (!NILP (w->vchild))
13812 redisplay_windows (w->vchild);
13813 else if (!NILP (w->buffer))
13814 {
13815 displayed_buffer = XBUFFER (w->buffer);
13816 /* Use list_of_error, not Qerror, so that
13817 we catch only errors and don't run the debugger. */
13818 internal_condition_case_1 (redisplay_window_0, window,
13819 list_of_error,
13820 redisplay_window_error);
13821 }
13822
13823 window = w->next;
13824 }
13825 }
13826
13827 static Lisp_Object
13828 redisplay_window_error (Lisp_Object ignore)
13829 {
13830 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13831 return Qnil;
13832 }
13833
13834 static Lisp_Object
13835 redisplay_window_0 (Lisp_Object window)
13836 {
13837 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13838 redisplay_window (window, 0);
13839 return Qnil;
13840 }
13841
13842 static Lisp_Object
13843 redisplay_window_1 (Lisp_Object window)
13844 {
13845 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13846 redisplay_window (window, 1);
13847 return Qnil;
13848 }
13849 \f
13850
13851 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13852 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13853 which positions recorded in ROW differ from current buffer
13854 positions.
13855
13856 Return 0 if cursor is not on this row, 1 otherwise. */
13857
13858 static int
13859 set_cursor_from_row (struct window *w, struct glyph_row *row,
13860 struct glyph_matrix *matrix,
13861 ptrdiff_t delta, ptrdiff_t delta_bytes,
13862 int dy, int dvpos)
13863 {
13864 struct glyph *glyph = row->glyphs[TEXT_AREA];
13865 struct glyph *end = glyph + row->used[TEXT_AREA];
13866 struct glyph *cursor = NULL;
13867 /* The last known character position in row. */
13868 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13869 int x = row->x;
13870 ptrdiff_t pt_old = PT - delta;
13871 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13872 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13873 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13874 /* A glyph beyond the edge of TEXT_AREA which we should never
13875 touch. */
13876 struct glyph *glyphs_end = end;
13877 /* Non-zero means we've found a match for cursor position, but that
13878 glyph has the avoid_cursor_p flag set. */
13879 int match_with_avoid_cursor = 0;
13880 /* Non-zero means we've seen at least one glyph that came from a
13881 display string. */
13882 int string_seen = 0;
13883 /* Largest and smallest buffer positions seen so far during scan of
13884 glyph row. */
13885 ptrdiff_t bpos_max = pos_before;
13886 ptrdiff_t bpos_min = pos_after;
13887 /* Last buffer position covered by an overlay string with an integer
13888 `cursor' property. */
13889 ptrdiff_t bpos_covered = 0;
13890 /* Non-zero means the display string on which to display the cursor
13891 comes from a text property, not from an overlay. */
13892 int string_from_text_prop = 0;
13893
13894 /* Don't even try doing anything if called for a mode-line or
13895 header-line row, since the rest of the code isn't prepared to
13896 deal with such calamities. */
13897 eassert (!row->mode_line_p);
13898 if (row->mode_line_p)
13899 return 0;
13900
13901 /* Skip over glyphs not having an object at the start and the end of
13902 the row. These are special glyphs like truncation marks on
13903 terminal frames. */
13904 if (row->displays_text_p)
13905 {
13906 if (!row->reversed_p)
13907 {
13908 while (glyph < end
13909 && INTEGERP (glyph->object)
13910 && glyph->charpos < 0)
13911 {
13912 x += glyph->pixel_width;
13913 ++glyph;
13914 }
13915 while (end > glyph
13916 && INTEGERP ((end - 1)->object)
13917 /* CHARPOS is zero for blanks and stretch glyphs
13918 inserted by extend_face_to_end_of_line. */
13919 && (end - 1)->charpos <= 0)
13920 --end;
13921 glyph_before = glyph - 1;
13922 glyph_after = end;
13923 }
13924 else
13925 {
13926 struct glyph *g;
13927
13928 /* If the glyph row is reversed, we need to process it from back
13929 to front, so swap the edge pointers. */
13930 glyphs_end = end = glyph - 1;
13931 glyph += row->used[TEXT_AREA] - 1;
13932
13933 while (glyph > end + 1
13934 && INTEGERP (glyph->object)
13935 && glyph->charpos < 0)
13936 {
13937 --glyph;
13938 x -= glyph->pixel_width;
13939 }
13940 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13941 --glyph;
13942 /* By default, in reversed rows we put the cursor on the
13943 rightmost (first in the reading order) glyph. */
13944 for (g = end + 1; g < glyph; g++)
13945 x += g->pixel_width;
13946 while (end < glyph
13947 && INTEGERP ((end + 1)->object)
13948 && (end + 1)->charpos <= 0)
13949 ++end;
13950 glyph_before = glyph + 1;
13951 glyph_after = end;
13952 }
13953 }
13954 else if (row->reversed_p)
13955 {
13956 /* In R2L rows that don't display text, put the cursor on the
13957 rightmost glyph. Case in point: an empty last line that is
13958 part of an R2L paragraph. */
13959 cursor = end - 1;
13960 /* Avoid placing the cursor on the last glyph of the row, where
13961 on terminal frames we hold the vertical border between
13962 adjacent windows. */
13963 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13964 && !WINDOW_RIGHTMOST_P (w)
13965 && cursor == row->glyphs[LAST_AREA] - 1)
13966 cursor--;
13967 x = -1; /* will be computed below, at label compute_x */
13968 }
13969
13970 /* Step 1: Try to find the glyph whose character position
13971 corresponds to point. If that's not possible, find 2 glyphs
13972 whose character positions are the closest to point, one before
13973 point, the other after it. */
13974 if (!row->reversed_p)
13975 while (/* not marched to end of glyph row */
13976 glyph < end
13977 /* glyph was not inserted by redisplay for internal purposes */
13978 && !INTEGERP (glyph->object))
13979 {
13980 if (BUFFERP (glyph->object))
13981 {
13982 ptrdiff_t dpos = glyph->charpos - pt_old;
13983
13984 if (glyph->charpos > bpos_max)
13985 bpos_max = glyph->charpos;
13986 if (glyph->charpos < bpos_min)
13987 bpos_min = glyph->charpos;
13988 if (!glyph->avoid_cursor_p)
13989 {
13990 /* If we hit point, we've found the glyph on which to
13991 display the cursor. */
13992 if (dpos == 0)
13993 {
13994 match_with_avoid_cursor = 0;
13995 break;
13996 }
13997 /* See if we've found a better approximation to
13998 POS_BEFORE or to POS_AFTER. */
13999 if (0 > dpos && dpos > pos_before - pt_old)
14000 {
14001 pos_before = glyph->charpos;
14002 glyph_before = glyph;
14003 }
14004 else if (0 < dpos && dpos < pos_after - pt_old)
14005 {
14006 pos_after = glyph->charpos;
14007 glyph_after = glyph;
14008 }
14009 }
14010 else if (dpos == 0)
14011 match_with_avoid_cursor = 1;
14012 }
14013 else if (STRINGP (glyph->object))
14014 {
14015 Lisp_Object chprop;
14016 ptrdiff_t glyph_pos = glyph->charpos;
14017
14018 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14019 glyph->object);
14020 if (!NILP (chprop))
14021 {
14022 /* If the string came from a `display' text property,
14023 look up the buffer position of that property and
14024 use that position to update bpos_max, as if we
14025 actually saw such a position in one of the row's
14026 glyphs. This helps with supporting integer values
14027 of `cursor' property on the display string in
14028 situations where most or all of the row's buffer
14029 text is completely covered by display properties,
14030 so that no glyph with valid buffer positions is
14031 ever seen in the row. */
14032 ptrdiff_t prop_pos =
14033 string_buffer_position_lim (glyph->object, pos_before,
14034 pos_after, 0);
14035
14036 if (prop_pos >= pos_before)
14037 bpos_max = prop_pos - 1;
14038 }
14039 if (INTEGERP (chprop))
14040 {
14041 bpos_covered = bpos_max + XINT (chprop);
14042 /* If the `cursor' property covers buffer positions up
14043 to and including point, we should display cursor on
14044 this glyph. Note that, if a `cursor' property on one
14045 of the string's characters has an integer value, we
14046 will break out of the loop below _before_ we get to
14047 the position match above. IOW, integer values of
14048 the `cursor' property override the "exact match for
14049 point" strategy of positioning the cursor. */
14050 /* Implementation note: bpos_max == pt_old when, e.g.,
14051 we are in an empty line, where bpos_max is set to
14052 MATRIX_ROW_START_CHARPOS, see above. */
14053 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14054 {
14055 cursor = glyph;
14056 break;
14057 }
14058 }
14059
14060 string_seen = 1;
14061 }
14062 x += glyph->pixel_width;
14063 ++glyph;
14064 }
14065 else if (glyph > end) /* row is reversed */
14066 while (!INTEGERP (glyph->object))
14067 {
14068 if (BUFFERP (glyph->object))
14069 {
14070 ptrdiff_t dpos = glyph->charpos - pt_old;
14071
14072 if (glyph->charpos > bpos_max)
14073 bpos_max = glyph->charpos;
14074 if (glyph->charpos < bpos_min)
14075 bpos_min = glyph->charpos;
14076 if (!glyph->avoid_cursor_p)
14077 {
14078 if (dpos == 0)
14079 {
14080 match_with_avoid_cursor = 0;
14081 break;
14082 }
14083 if (0 > dpos && dpos > pos_before - pt_old)
14084 {
14085 pos_before = glyph->charpos;
14086 glyph_before = glyph;
14087 }
14088 else if (0 < dpos && dpos < pos_after - pt_old)
14089 {
14090 pos_after = glyph->charpos;
14091 glyph_after = glyph;
14092 }
14093 }
14094 else if (dpos == 0)
14095 match_with_avoid_cursor = 1;
14096 }
14097 else if (STRINGP (glyph->object))
14098 {
14099 Lisp_Object chprop;
14100 ptrdiff_t glyph_pos = glyph->charpos;
14101
14102 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14103 glyph->object);
14104 if (!NILP (chprop))
14105 {
14106 ptrdiff_t prop_pos =
14107 string_buffer_position_lim (glyph->object, pos_before,
14108 pos_after, 0);
14109
14110 if (prop_pos >= pos_before)
14111 bpos_max = prop_pos - 1;
14112 }
14113 if (INTEGERP (chprop))
14114 {
14115 bpos_covered = bpos_max + XINT (chprop);
14116 /* If the `cursor' property covers buffer positions up
14117 to and including point, we should display cursor on
14118 this glyph. */
14119 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14120 {
14121 cursor = glyph;
14122 break;
14123 }
14124 }
14125 string_seen = 1;
14126 }
14127 --glyph;
14128 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14129 {
14130 x--; /* can't use any pixel_width */
14131 break;
14132 }
14133 x -= glyph->pixel_width;
14134 }
14135
14136 /* Step 2: If we didn't find an exact match for point, we need to
14137 look for a proper place to put the cursor among glyphs between
14138 GLYPH_BEFORE and GLYPH_AFTER. */
14139 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14140 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14141 && bpos_covered < pt_old)
14142 {
14143 /* An empty line has a single glyph whose OBJECT is zero and
14144 whose CHARPOS is the position of a newline on that line.
14145 Note that on a TTY, there are more glyphs after that, which
14146 were produced by extend_face_to_end_of_line, but their
14147 CHARPOS is zero or negative. */
14148 int empty_line_p =
14149 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14150 && INTEGERP (glyph->object) && glyph->charpos > 0;
14151
14152 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14153 {
14154 ptrdiff_t ellipsis_pos;
14155
14156 /* Scan back over the ellipsis glyphs. */
14157 if (!row->reversed_p)
14158 {
14159 ellipsis_pos = (glyph - 1)->charpos;
14160 while (glyph > row->glyphs[TEXT_AREA]
14161 && (glyph - 1)->charpos == ellipsis_pos)
14162 glyph--, x -= glyph->pixel_width;
14163 /* That loop always goes one position too far, including
14164 the glyph before the ellipsis. So scan forward over
14165 that one. */
14166 x += glyph->pixel_width;
14167 glyph++;
14168 }
14169 else /* row is reversed */
14170 {
14171 ellipsis_pos = (glyph + 1)->charpos;
14172 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14173 && (glyph + 1)->charpos == ellipsis_pos)
14174 glyph++, x += glyph->pixel_width;
14175 x -= glyph->pixel_width;
14176 glyph--;
14177 }
14178 }
14179 else if (match_with_avoid_cursor)
14180 {
14181 cursor = glyph_after;
14182 x = -1;
14183 }
14184 else if (string_seen)
14185 {
14186 int incr = row->reversed_p ? -1 : +1;
14187
14188 /* Need to find the glyph that came out of a string which is
14189 present at point. That glyph is somewhere between
14190 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14191 positioned between POS_BEFORE and POS_AFTER in the
14192 buffer. */
14193 struct glyph *start, *stop;
14194 ptrdiff_t pos = pos_before;
14195
14196 x = -1;
14197
14198 /* If the row ends in a newline from a display string,
14199 reordering could have moved the glyphs belonging to the
14200 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14201 in this case we extend the search to the last glyph in
14202 the row that was not inserted by redisplay. */
14203 if (row->ends_in_newline_from_string_p)
14204 {
14205 glyph_after = end;
14206 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14207 }
14208
14209 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14210 correspond to POS_BEFORE and POS_AFTER, respectively. We
14211 need START and STOP in the order that corresponds to the
14212 row's direction as given by its reversed_p flag. If the
14213 directionality of characters between POS_BEFORE and
14214 POS_AFTER is the opposite of the row's base direction,
14215 these characters will have been reordered for display,
14216 and we need to reverse START and STOP. */
14217 if (!row->reversed_p)
14218 {
14219 start = min (glyph_before, glyph_after);
14220 stop = max (glyph_before, glyph_after);
14221 }
14222 else
14223 {
14224 start = max (glyph_before, glyph_after);
14225 stop = min (glyph_before, glyph_after);
14226 }
14227 for (glyph = start + incr;
14228 row->reversed_p ? glyph > stop : glyph < stop; )
14229 {
14230
14231 /* Any glyphs that come from the buffer are here because
14232 of bidi reordering. Skip them, and only pay
14233 attention to glyphs that came from some string. */
14234 if (STRINGP (glyph->object))
14235 {
14236 Lisp_Object str;
14237 ptrdiff_t tem;
14238 /* If the display property covers the newline, we
14239 need to search for it one position farther. */
14240 ptrdiff_t lim = pos_after
14241 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14242
14243 string_from_text_prop = 0;
14244 str = glyph->object;
14245 tem = string_buffer_position_lim (str, pos, lim, 0);
14246 if (tem == 0 /* from overlay */
14247 || pos <= tem)
14248 {
14249 /* If the string from which this glyph came is
14250 found in the buffer at point, or at position
14251 that is closer to point than pos_after, then
14252 we've found the glyph we've been looking for.
14253 If it comes from an overlay (tem == 0), and
14254 it has the `cursor' property on one of its
14255 glyphs, record that glyph as a candidate for
14256 displaying the cursor. (As in the
14257 unidirectional version, we will display the
14258 cursor on the last candidate we find.) */
14259 if (tem == 0
14260 || tem == pt_old
14261 || (tem - pt_old > 0 && tem < pos_after))
14262 {
14263 /* The glyphs from this string could have
14264 been reordered. Find the one with the
14265 smallest string position. Or there could
14266 be a character in the string with the
14267 `cursor' property, which means display
14268 cursor on that character's glyph. */
14269 ptrdiff_t strpos = glyph->charpos;
14270
14271 if (tem)
14272 {
14273 cursor = glyph;
14274 string_from_text_prop = 1;
14275 }
14276 for ( ;
14277 (row->reversed_p ? glyph > stop : glyph < stop)
14278 && EQ (glyph->object, str);
14279 glyph += incr)
14280 {
14281 Lisp_Object cprop;
14282 ptrdiff_t gpos = glyph->charpos;
14283
14284 cprop = Fget_char_property (make_number (gpos),
14285 Qcursor,
14286 glyph->object);
14287 if (!NILP (cprop))
14288 {
14289 cursor = glyph;
14290 break;
14291 }
14292 if (tem && glyph->charpos < strpos)
14293 {
14294 strpos = glyph->charpos;
14295 cursor = glyph;
14296 }
14297 }
14298
14299 if (tem == pt_old
14300 || (tem - pt_old > 0 && tem < pos_after))
14301 goto compute_x;
14302 }
14303 if (tem)
14304 pos = tem + 1; /* don't find previous instances */
14305 }
14306 /* This string is not what we want; skip all of the
14307 glyphs that came from it. */
14308 while ((row->reversed_p ? glyph > stop : glyph < stop)
14309 && EQ (glyph->object, str))
14310 glyph += incr;
14311 }
14312 else
14313 glyph += incr;
14314 }
14315
14316 /* If we reached the end of the line, and END was from a string,
14317 the cursor is not on this line. */
14318 if (cursor == NULL
14319 && (row->reversed_p ? glyph <= end : glyph >= end)
14320 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14321 && STRINGP (end->object)
14322 && row->continued_p)
14323 return 0;
14324 }
14325 /* A truncated row may not include PT among its character positions.
14326 Setting the cursor inside the scroll margin will trigger
14327 recalculation of hscroll in hscroll_window_tree. But if a
14328 display string covers point, defer to the string-handling
14329 code below to figure this out. */
14330 else if (row->truncated_on_left_p && pt_old < bpos_min)
14331 {
14332 cursor = glyph_before;
14333 x = -1;
14334 }
14335 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14336 /* Zero-width characters produce no glyphs. */
14337 || (!empty_line_p
14338 && (row->reversed_p
14339 ? glyph_after > glyphs_end
14340 : glyph_after < glyphs_end)))
14341 {
14342 cursor = glyph_after;
14343 x = -1;
14344 }
14345 }
14346
14347 compute_x:
14348 if (cursor != NULL)
14349 glyph = cursor;
14350 else if (glyph == glyphs_end
14351 && pos_before == pos_after
14352 && STRINGP ((row->reversed_p
14353 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14354 : row->glyphs[TEXT_AREA])->object))
14355 {
14356 /* If all the glyphs of this row came from strings, put the
14357 cursor on the first glyph of the row. This avoids having the
14358 cursor outside of the text area in this very rare and hard
14359 use case. */
14360 glyph =
14361 row->reversed_p
14362 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14363 : row->glyphs[TEXT_AREA];
14364 }
14365 if (x < 0)
14366 {
14367 struct glyph *g;
14368
14369 /* Need to compute x that corresponds to GLYPH. */
14370 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14371 {
14372 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14373 abort ();
14374 x += g->pixel_width;
14375 }
14376 }
14377
14378 /* ROW could be part of a continued line, which, under bidi
14379 reordering, might have other rows whose start and end charpos
14380 occlude point. Only set w->cursor if we found a better
14381 approximation to the cursor position than we have from previously
14382 examined candidate rows belonging to the same continued line. */
14383 if (/* we already have a candidate row */
14384 w->cursor.vpos >= 0
14385 /* that candidate is not the row we are processing */
14386 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14387 /* Make sure cursor.vpos specifies a row whose start and end
14388 charpos occlude point, and it is valid candidate for being a
14389 cursor-row. This is because some callers of this function
14390 leave cursor.vpos at the row where the cursor was displayed
14391 during the last redisplay cycle. */
14392 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14393 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14394 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14395 {
14396 struct glyph *g1 =
14397 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14398
14399 /* Don't consider glyphs that are outside TEXT_AREA. */
14400 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14401 return 0;
14402 /* Keep the candidate whose buffer position is the closest to
14403 point or has the `cursor' property. */
14404 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14405 w->cursor.hpos >= 0
14406 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14407 && ((BUFFERP (g1->object)
14408 && (g1->charpos == pt_old /* an exact match always wins */
14409 || (BUFFERP (glyph->object)
14410 && eabs (g1->charpos - pt_old)
14411 < eabs (glyph->charpos - pt_old))))
14412 /* previous candidate is a glyph from a string that has
14413 a non-nil `cursor' property */
14414 || (STRINGP (g1->object)
14415 && (!NILP (Fget_char_property (make_number (g1->charpos),
14416 Qcursor, g1->object))
14417 /* previous candidate is from the same display
14418 string as this one, and the display string
14419 came from a text property */
14420 || (EQ (g1->object, glyph->object)
14421 && string_from_text_prop)
14422 /* this candidate is from newline and its
14423 position is not an exact match */
14424 || (INTEGERP (glyph->object)
14425 && glyph->charpos != pt_old)))))
14426 return 0;
14427 /* If this candidate gives an exact match, use that. */
14428 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14429 /* If this candidate is a glyph created for the
14430 terminating newline of a line, and point is on that
14431 newline, it wins because it's an exact match. */
14432 || (!row->continued_p
14433 && INTEGERP (glyph->object)
14434 && glyph->charpos == 0
14435 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14436 /* Otherwise, keep the candidate that comes from a row
14437 spanning less buffer positions. This may win when one or
14438 both candidate positions are on glyphs that came from
14439 display strings, for which we cannot compare buffer
14440 positions. */
14441 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14442 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14443 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14444 return 0;
14445 }
14446 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14447 w->cursor.x = x;
14448 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14449 w->cursor.y = row->y + dy;
14450
14451 if (w == XWINDOW (selected_window))
14452 {
14453 if (!row->continued_p
14454 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14455 && row->x == 0)
14456 {
14457 this_line_buffer = XBUFFER (w->buffer);
14458
14459 CHARPOS (this_line_start_pos)
14460 = MATRIX_ROW_START_CHARPOS (row) + delta;
14461 BYTEPOS (this_line_start_pos)
14462 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14463
14464 CHARPOS (this_line_end_pos)
14465 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14466 BYTEPOS (this_line_end_pos)
14467 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14468
14469 this_line_y = w->cursor.y;
14470 this_line_pixel_height = row->height;
14471 this_line_vpos = w->cursor.vpos;
14472 this_line_start_x = row->x;
14473 }
14474 else
14475 CHARPOS (this_line_start_pos) = 0;
14476 }
14477
14478 return 1;
14479 }
14480
14481
14482 /* Run window scroll functions, if any, for WINDOW with new window
14483 start STARTP. Sets the window start of WINDOW to that position.
14484
14485 We assume that the window's buffer is really current. */
14486
14487 static inline struct text_pos
14488 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14489 {
14490 struct window *w = XWINDOW (window);
14491 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14492
14493 if (current_buffer != XBUFFER (w->buffer))
14494 abort ();
14495
14496 if (!NILP (Vwindow_scroll_functions))
14497 {
14498 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14499 make_number (CHARPOS (startp)));
14500 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14501 /* In case the hook functions switch buffers. */
14502 if (current_buffer != XBUFFER (w->buffer))
14503 set_buffer_internal_1 (XBUFFER (w->buffer));
14504 }
14505
14506 return startp;
14507 }
14508
14509
14510 /* Make sure the line containing the cursor is fully visible.
14511 A value of 1 means there is nothing to be done.
14512 (Either the line is fully visible, or it cannot be made so,
14513 or we cannot tell.)
14514
14515 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14516 is higher than window.
14517
14518 A value of 0 means the caller should do scrolling
14519 as if point had gone off the screen. */
14520
14521 static int
14522 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14523 {
14524 struct glyph_matrix *matrix;
14525 struct glyph_row *row;
14526 int window_height;
14527
14528 if (!make_cursor_line_fully_visible_p)
14529 return 1;
14530
14531 /* It's not always possible to find the cursor, e.g, when a window
14532 is full of overlay strings. Don't do anything in that case. */
14533 if (w->cursor.vpos < 0)
14534 return 1;
14535
14536 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14537 row = MATRIX_ROW (matrix, w->cursor.vpos);
14538
14539 /* If the cursor row is not partially visible, there's nothing to do. */
14540 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14541 return 1;
14542
14543 /* If the row the cursor is in is taller than the window's height,
14544 it's not clear what to do, so do nothing. */
14545 window_height = window_box_height (w);
14546 if (row->height >= window_height)
14547 {
14548 if (!force_p || MINI_WINDOW_P (w)
14549 || w->vscroll || w->cursor.vpos == 0)
14550 return 1;
14551 }
14552 return 0;
14553 }
14554
14555
14556 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14557 non-zero means only WINDOW is redisplayed in redisplay_internal.
14558 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14559 in redisplay_window to bring a partially visible line into view in
14560 the case that only the cursor has moved.
14561
14562 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14563 last screen line's vertical height extends past the end of the screen.
14564
14565 Value is
14566
14567 1 if scrolling succeeded
14568
14569 0 if scrolling didn't find point.
14570
14571 -1 if new fonts have been loaded so that we must interrupt
14572 redisplay, adjust glyph matrices, and try again. */
14573
14574 enum
14575 {
14576 SCROLLING_SUCCESS,
14577 SCROLLING_FAILED,
14578 SCROLLING_NEED_LARGER_MATRICES
14579 };
14580
14581 /* If scroll-conservatively is more than this, never recenter.
14582
14583 If you change this, don't forget to update the doc string of
14584 `scroll-conservatively' and the Emacs manual. */
14585 #define SCROLL_LIMIT 100
14586
14587 static int
14588 try_scrolling (Lisp_Object window, int just_this_one_p,
14589 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14590 int temp_scroll_step, int last_line_misfit)
14591 {
14592 struct window *w = XWINDOW (window);
14593 struct frame *f = XFRAME (w->frame);
14594 struct text_pos pos, startp;
14595 struct it it;
14596 int this_scroll_margin, scroll_max, rc, height;
14597 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14598 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14599 Lisp_Object aggressive;
14600 /* We will never try scrolling more than this number of lines. */
14601 int scroll_limit = SCROLL_LIMIT;
14602
14603 #ifdef GLYPH_DEBUG
14604 debug_method_add (w, "try_scrolling");
14605 #endif
14606
14607 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14608
14609 /* Compute scroll margin height in pixels. We scroll when point is
14610 within this distance from the top or bottom of the window. */
14611 if (scroll_margin > 0)
14612 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14613 * FRAME_LINE_HEIGHT (f);
14614 else
14615 this_scroll_margin = 0;
14616
14617 /* Force arg_scroll_conservatively to have a reasonable value, to
14618 avoid scrolling too far away with slow move_it_* functions. Note
14619 that the user can supply scroll-conservatively equal to
14620 `most-positive-fixnum', which can be larger than INT_MAX. */
14621 if (arg_scroll_conservatively > scroll_limit)
14622 {
14623 arg_scroll_conservatively = scroll_limit + 1;
14624 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14625 }
14626 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14627 /* Compute how much we should try to scroll maximally to bring
14628 point into view. */
14629 scroll_max = (max (scroll_step,
14630 max (arg_scroll_conservatively, temp_scroll_step))
14631 * FRAME_LINE_HEIGHT (f));
14632 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14633 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14634 /* We're trying to scroll because of aggressive scrolling but no
14635 scroll_step is set. Choose an arbitrary one. */
14636 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14637 else
14638 scroll_max = 0;
14639
14640 too_near_end:
14641
14642 /* Decide whether to scroll down. */
14643 if (PT > CHARPOS (startp))
14644 {
14645 int scroll_margin_y;
14646
14647 /* Compute the pixel ypos of the scroll margin, then move IT to
14648 either that ypos or PT, whichever comes first. */
14649 start_display (&it, w, startp);
14650 scroll_margin_y = it.last_visible_y - this_scroll_margin
14651 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14652 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14653 (MOVE_TO_POS | MOVE_TO_Y));
14654
14655 if (PT > CHARPOS (it.current.pos))
14656 {
14657 int y0 = line_bottom_y (&it);
14658 /* Compute how many pixels below window bottom to stop searching
14659 for PT. This avoids costly search for PT that is far away if
14660 the user limited scrolling by a small number of lines, but
14661 always finds PT if scroll_conservatively is set to a large
14662 number, such as most-positive-fixnum. */
14663 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14664 int y_to_move = it.last_visible_y + slack;
14665
14666 /* Compute the distance from the scroll margin to PT or to
14667 the scroll limit, whichever comes first. This should
14668 include the height of the cursor line, to make that line
14669 fully visible. */
14670 move_it_to (&it, PT, -1, y_to_move,
14671 -1, MOVE_TO_POS | MOVE_TO_Y);
14672 dy = line_bottom_y (&it) - y0;
14673
14674 if (dy > scroll_max)
14675 return SCROLLING_FAILED;
14676
14677 if (dy > 0)
14678 scroll_down_p = 1;
14679 }
14680 }
14681
14682 if (scroll_down_p)
14683 {
14684 /* Point is in or below the bottom scroll margin, so move the
14685 window start down. If scrolling conservatively, move it just
14686 enough down to make point visible. If scroll_step is set,
14687 move it down by scroll_step. */
14688 if (arg_scroll_conservatively)
14689 amount_to_scroll
14690 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14691 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14692 else if (scroll_step || temp_scroll_step)
14693 amount_to_scroll = scroll_max;
14694 else
14695 {
14696 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14697 height = WINDOW_BOX_TEXT_HEIGHT (w);
14698 if (NUMBERP (aggressive))
14699 {
14700 double float_amount = XFLOATINT (aggressive) * height;
14701 amount_to_scroll = float_amount;
14702 if (amount_to_scroll == 0 && float_amount > 0)
14703 amount_to_scroll = 1;
14704 /* Don't let point enter the scroll margin near top of
14705 the window. */
14706 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14707 amount_to_scroll = height - 2*this_scroll_margin + dy;
14708 }
14709 }
14710
14711 if (amount_to_scroll <= 0)
14712 return SCROLLING_FAILED;
14713
14714 start_display (&it, w, startp);
14715 if (arg_scroll_conservatively <= scroll_limit)
14716 move_it_vertically (&it, amount_to_scroll);
14717 else
14718 {
14719 /* Extra precision for users who set scroll-conservatively
14720 to a large number: make sure the amount we scroll
14721 the window start is never less than amount_to_scroll,
14722 which was computed as distance from window bottom to
14723 point. This matters when lines at window top and lines
14724 below window bottom have different height. */
14725 struct it it1;
14726 void *it1data = NULL;
14727 /* We use a temporary it1 because line_bottom_y can modify
14728 its argument, if it moves one line down; see there. */
14729 int start_y;
14730
14731 SAVE_IT (it1, it, it1data);
14732 start_y = line_bottom_y (&it1);
14733 do {
14734 RESTORE_IT (&it, &it, it1data);
14735 move_it_by_lines (&it, 1);
14736 SAVE_IT (it1, it, it1data);
14737 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14738 }
14739
14740 /* If STARTP is unchanged, move it down another screen line. */
14741 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14742 move_it_by_lines (&it, 1);
14743 startp = it.current.pos;
14744 }
14745 else
14746 {
14747 struct text_pos scroll_margin_pos = startp;
14748
14749 /* See if point is inside the scroll margin at the top of the
14750 window. */
14751 if (this_scroll_margin)
14752 {
14753 start_display (&it, w, startp);
14754 move_it_vertically (&it, this_scroll_margin);
14755 scroll_margin_pos = it.current.pos;
14756 }
14757
14758 if (PT < CHARPOS (scroll_margin_pos))
14759 {
14760 /* Point is in the scroll margin at the top of the window or
14761 above what is displayed in the window. */
14762 int y0, y_to_move;
14763
14764 /* Compute the vertical distance from PT to the scroll
14765 margin position. Move as far as scroll_max allows, or
14766 one screenful, or 10 screen lines, whichever is largest.
14767 Give up if distance is greater than scroll_max. */
14768 SET_TEXT_POS (pos, PT, PT_BYTE);
14769 start_display (&it, w, pos);
14770 y0 = it.current_y;
14771 y_to_move = max (it.last_visible_y,
14772 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14773 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14774 y_to_move, -1,
14775 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14776 dy = it.current_y - y0;
14777 if (dy > scroll_max)
14778 return SCROLLING_FAILED;
14779
14780 /* Compute new window start. */
14781 start_display (&it, w, startp);
14782
14783 if (arg_scroll_conservatively)
14784 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14785 max (scroll_step, temp_scroll_step));
14786 else if (scroll_step || temp_scroll_step)
14787 amount_to_scroll = scroll_max;
14788 else
14789 {
14790 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14791 height = WINDOW_BOX_TEXT_HEIGHT (w);
14792 if (NUMBERP (aggressive))
14793 {
14794 double float_amount = XFLOATINT (aggressive) * height;
14795 amount_to_scroll = float_amount;
14796 if (amount_to_scroll == 0 && float_amount > 0)
14797 amount_to_scroll = 1;
14798 amount_to_scroll -=
14799 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14800 /* Don't let point enter the scroll margin near
14801 bottom of the window. */
14802 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14803 amount_to_scroll = height - 2*this_scroll_margin + dy;
14804 }
14805 }
14806
14807 if (amount_to_scroll <= 0)
14808 return SCROLLING_FAILED;
14809
14810 move_it_vertically_backward (&it, amount_to_scroll);
14811 startp = it.current.pos;
14812 }
14813 }
14814
14815 /* Run window scroll functions. */
14816 startp = run_window_scroll_functions (window, startp);
14817
14818 /* Display the window. Give up if new fonts are loaded, or if point
14819 doesn't appear. */
14820 if (!try_window (window, startp, 0))
14821 rc = SCROLLING_NEED_LARGER_MATRICES;
14822 else if (w->cursor.vpos < 0)
14823 {
14824 clear_glyph_matrix (w->desired_matrix);
14825 rc = SCROLLING_FAILED;
14826 }
14827 else
14828 {
14829 /* Maybe forget recorded base line for line number display. */
14830 if (!just_this_one_p
14831 || current_buffer->clip_changed
14832 || BEG_UNCHANGED < CHARPOS (startp))
14833 w->base_line_number = Qnil;
14834
14835 /* If cursor ends up on a partially visible line,
14836 treat that as being off the bottom of the screen. */
14837 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14838 /* It's possible that the cursor is on the first line of the
14839 buffer, which is partially obscured due to a vscroll
14840 (Bug#7537). In that case, avoid looping forever . */
14841 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14842 {
14843 clear_glyph_matrix (w->desired_matrix);
14844 ++extra_scroll_margin_lines;
14845 goto too_near_end;
14846 }
14847 rc = SCROLLING_SUCCESS;
14848 }
14849
14850 return rc;
14851 }
14852
14853
14854 /* Compute a suitable window start for window W if display of W starts
14855 on a continuation line. Value is non-zero if a new window start
14856 was computed.
14857
14858 The new window start will be computed, based on W's width, starting
14859 from the start of the continued line. It is the start of the
14860 screen line with the minimum distance from the old start W->start. */
14861
14862 static int
14863 compute_window_start_on_continuation_line (struct window *w)
14864 {
14865 struct text_pos pos, start_pos;
14866 int window_start_changed_p = 0;
14867
14868 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14869
14870 /* If window start is on a continuation line... Window start may be
14871 < BEGV in case there's invisible text at the start of the
14872 buffer (M-x rmail, for example). */
14873 if (CHARPOS (start_pos) > BEGV
14874 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14875 {
14876 struct it it;
14877 struct glyph_row *row;
14878
14879 /* Handle the case that the window start is out of range. */
14880 if (CHARPOS (start_pos) < BEGV)
14881 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14882 else if (CHARPOS (start_pos) > ZV)
14883 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14884
14885 /* Find the start of the continued line. This should be fast
14886 because scan_buffer is fast (newline cache). */
14887 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14888 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14889 row, DEFAULT_FACE_ID);
14890 reseat_at_previous_visible_line_start (&it);
14891
14892 /* If the line start is "too far" away from the window start,
14893 say it takes too much time to compute a new window start. */
14894 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14895 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14896 {
14897 int min_distance, distance;
14898
14899 /* Move forward by display lines to find the new window
14900 start. If window width was enlarged, the new start can
14901 be expected to be > the old start. If window width was
14902 decreased, the new window start will be < the old start.
14903 So, we're looking for the display line start with the
14904 minimum distance from the old window start. */
14905 pos = it.current.pos;
14906 min_distance = INFINITY;
14907 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14908 distance < min_distance)
14909 {
14910 min_distance = distance;
14911 pos = it.current.pos;
14912 move_it_by_lines (&it, 1);
14913 }
14914
14915 /* Set the window start there. */
14916 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14917 window_start_changed_p = 1;
14918 }
14919 }
14920
14921 return window_start_changed_p;
14922 }
14923
14924
14925 /* Try cursor movement in case text has not changed in window WINDOW,
14926 with window start STARTP. Value is
14927
14928 CURSOR_MOVEMENT_SUCCESS if successful
14929
14930 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14931
14932 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14933 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14934 we want to scroll as if scroll-step were set to 1. See the code.
14935
14936 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14937 which case we have to abort this redisplay, and adjust matrices
14938 first. */
14939
14940 enum
14941 {
14942 CURSOR_MOVEMENT_SUCCESS,
14943 CURSOR_MOVEMENT_CANNOT_BE_USED,
14944 CURSOR_MOVEMENT_MUST_SCROLL,
14945 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14946 };
14947
14948 static int
14949 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14950 {
14951 struct window *w = XWINDOW (window);
14952 struct frame *f = XFRAME (w->frame);
14953 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14954
14955 #ifdef GLYPH_DEBUG
14956 if (inhibit_try_cursor_movement)
14957 return rc;
14958 #endif
14959
14960 /* Previously, there was a check for Lisp integer in the
14961 if-statement below. Now, this field is converted to
14962 ptrdiff_t, thus zero means invalid position in a buffer. */
14963 eassert (w->last_point > 0);
14964
14965 /* Handle case where text has not changed, only point, and it has
14966 not moved off the frame. */
14967 if (/* Point may be in this window. */
14968 PT >= CHARPOS (startp)
14969 /* Selective display hasn't changed. */
14970 && !current_buffer->clip_changed
14971 /* Function force-mode-line-update is used to force a thorough
14972 redisplay. It sets either windows_or_buffers_changed or
14973 update_mode_lines. So don't take a shortcut here for these
14974 cases. */
14975 && !update_mode_lines
14976 && !windows_or_buffers_changed
14977 && !cursor_type_changed
14978 /* Can't use this case if highlighting a region. When a
14979 region exists, cursor movement has to do more than just
14980 set the cursor. */
14981 && !(!NILP (Vtransient_mark_mode)
14982 && !NILP (BVAR (current_buffer, mark_active)))
14983 && NILP (w->region_showing)
14984 && NILP (Vshow_trailing_whitespace)
14985 /* This code is not used for mini-buffer for the sake of the case
14986 of redisplaying to replace an echo area message; since in
14987 that case the mini-buffer contents per se are usually
14988 unchanged. This code is of no real use in the mini-buffer
14989 since the handling of this_line_start_pos, etc., in redisplay
14990 handles the same cases. */
14991 && !EQ (window, minibuf_window)
14992 /* When splitting windows or for new windows, it happens that
14993 redisplay is called with a nil window_end_vpos or one being
14994 larger than the window. This should really be fixed in
14995 window.c. I don't have this on my list, now, so we do
14996 approximately the same as the old redisplay code. --gerd. */
14997 && INTEGERP (w->window_end_vpos)
14998 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14999 && (FRAME_WINDOW_P (f)
15000 || !overlay_arrow_in_current_buffer_p ()))
15001 {
15002 int this_scroll_margin, top_scroll_margin;
15003 struct glyph_row *row = NULL;
15004
15005 #ifdef GLYPH_DEBUG
15006 debug_method_add (w, "cursor movement");
15007 #endif
15008
15009 /* Scroll if point within this distance from the top or bottom
15010 of the window. This is a pixel value. */
15011 if (scroll_margin > 0)
15012 {
15013 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15014 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15015 }
15016 else
15017 this_scroll_margin = 0;
15018
15019 top_scroll_margin = this_scroll_margin;
15020 if (WINDOW_WANTS_HEADER_LINE_P (w))
15021 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15022
15023 /* Start with the row the cursor was displayed during the last
15024 not paused redisplay. Give up if that row is not valid. */
15025 if (w->last_cursor.vpos < 0
15026 || w->last_cursor.vpos >= w->current_matrix->nrows)
15027 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15028 else
15029 {
15030 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15031 if (row->mode_line_p)
15032 ++row;
15033 if (!row->enabled_p)
15034 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15035 }
15036
15037 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15038 {
15039 int scroll_p = 0, must_scroll = 0;
15040 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15041
15042 if (PT > w->last_point)
15043 {
15044 /* Point has moved forward. */
15045 while (MATRIX_ROW_END_CHARPOS (row) < PT
15046 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15047 {
15048 eassert (row->enabled_p);
15049 ++row;
15050 }
15051
15052 /* If the end position of a row equals the start
15053 position of the next row, and PT is at that position,
15054 we would rather display cursor in the next line. */
15055 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15056 && MATRIX_ROW_END_CHARPOS (row) == PT
15057 && row < w->current_matrix->rows
15058 + w->current_matrix->nrows - 1
15059 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15060 && !cursor_row_p (row))
15061 ++row;
15062
15063 /* If within the scroll margin, scroll. Note that
15064 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15065 the next line would be drawn, and that
15066 this_scroll_margin can be zero. */
15067 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15068 || PT > MATRIX_ROW_END_CHARPOS (row)
15069 /* Line is completely visible last line in window
15070 and PT is to be set in the next line. */
15071 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15072 && PT == MATRIX_ROW_END_CHARPOS (row)
15073 && !row->ends_at_zv_p
15074 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15075 scroll_p = 1;
15076 }
15077 else if (PT < w->last_point)
15078 {
15079 /* Cursor has to be moved backward. Note that PT >=
15080 CHARPOS (startp) because of the outer if-statement. */
15081 while (!row->mode_line_p
15082 && (MATRIX_ROW_START_CHARPOS (row) > PT
15083 || (MATRIX_ROW_START_CHARPOS (row) == PT
15084 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15085 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15086 row > w->current_matrix->rows
15087 && (row-1)->ends_in_newline_from_string_p))))
15088 && (row->y > top_scroll_margin
15089 || CHARPOS (startp) == BEGV))
15090 {
15091 eassert (row->enabled_p);
15092 --row;
15093 }
15094
15095 /* Consider the following case: Window starts at BEGV,
15096 there is invisible, intangible text at BEGV, so that
15097 display starts at some point START > BEGV. It can
15098 happen that we are called with PT somewhere between
15099 BEGV and START. Try to handle that case. */
15100 if (row < w->current_matrix->rows
15101 || row->mode_line_p)
15102 {
15103 row = w->current_matrix->rows;
15104 if (row->mode_line_p)
15105 ++row;
15106 }
15107
15108 /* Due to newlines in overlay strings, we may have to
15109 skip forward over overlay strings. */
15110 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15111 && MATRIX_ROW_END_CHARPOS (row) == PT
15112 && !cursor_row_p (row))
15113 ++row;
15114
15115 /* If within the scroll margin, scroll. */
15116 if (row->y < top_scroll_margin
15117 && CHARPOS (startp) != BEGV)
15118 scroll_p = 1;
15119 }
15120 else
15121 {
15122 /* Cursor did not move. So don't scroll even if cursor line
15123 is partially visible, as it was so before. */
15124 rc = CURSOR_MOVEMENT_SUCCESS;
15125 }
15126
15127 if (PT < MATRIX_ROW_START_CHARPOS (row)
15128 || PT > MATRIX_ROW_END_CHARPOS (row))
15129 {
15130 /* if PT is not in the glyph row, give up. */
15131 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15132 must_scroll = 1;
15133 }
15134 else if (rc != CURSOR_MOVEMENT_SUCCESS
15135 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15136 {
15137 struct glyph_row *row1;
15138
15139 /* If rows are bidi-reordered and point moved, back up
15140 until we find a row that does not belong to a
15141 continuation line. This is because we must consider
15142 all rows of a continued line as candidates for the
15143 new cursor positioning, since row start and end
15144 positions change non-linearly with vertical position
15145 in such rows. */
15146 /* FIXME: Revisit this when glyph ``spilling'' in
15147 continuation lines' rows is implemented for
15148 bidi-reordered rows. */
15149 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15150 MATRIX_ROW_CONTINUATION_LINE_P (row);
15151 --row)
15152 {
15153 /* If we hit the beginning of the displayed portion
15154 without finding the first row of a continued
15155 line, give up. */
15156 if (row <= row1)
15157 {
15158 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15159 break;
15160 }
15161 eassert (row->enabled_p);
15162 }
15163 }
15164 if (must_scroll)
15165 ;
15166 else if (rc != CURSOR_MOVEMENT_SUCCESS
15167 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15168 /* Make sure this isn't a header line by any chance, since
15169 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15170 && !row->mode_line_p
15171 && make_cursor_line_fully_visible_p)
15172 {
15173 if (PT == MATRIX_ROW_END_CHARPOS (row)
15174 && !row->ends_at_zv_p
15175 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15176 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15177 else if (row->height > window_box_height (w))
15178 {
15179 /* If we end up in a partially visible line, let's
15180 make it fully visible, except when it's taller
15181 than the window, in which case we can't do much
15182 about it. */
15183 *scroll_step = 1;
15184 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15185 }
15186 else
15187 {
15188 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15189 if (!cursor_row_fully_visible_p (w, 0, 1))
15190 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15191 else
15192 rc = CURSOR_MOVEMENT_SUCCESS;
15193 }
15194 }
15195 else if (scroll_p)
15196 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15197 else if (rc != CURSOR_MOVEMENT_SUCCESS
15198 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15199 {
15200 /* With bidi-reordered rows, there could be more than
15201 one candidate row whose start and end positions
15202 occlude point. We need to let set_cursor_from_row
15203 find the best candidate. */
15204 /* FIXME: Revisit this when glyph ``spilling'' in
15205 continuation lines' rows is implemented for
15206 bidi-reordered rows. */
15207 int rv = 0;
15208
15209 do
15210 {
15211 int at_zv_p = 0, exact_match_p = 0;
15212
15213 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15214 && PT <= MATRIX_ROW_END_CHARPOS (row)
15215 && cursor_row_p (row))
15216 rv |= set_cursor_from_row (w, row, w->current_matrix,
15217 0, 0, 0, 0);
15218 /* As soon as we've found the exact match for point,
15219 or the first suitable row whose ends_at_zv_p flag
15220 is set, we are done. */
15221 at_zv_p =
15222 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15223 if (rv && !at_zv_p
15224 && w->cursor.hpos >= 0
15225 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15226 w->cursor.vpos))
15227 {
15228 struct glyph_row *candidate =
15229 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15230 struct glyph *g =
15231 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15232 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15233
15234 exact_match_p =
15235 (BUFFERP (g->object) && g->charpos == PT)
15236 || (INTEGERP (g->object)
15237 && (g->charpos == PT
15238 || (g->charpos == 0 && endpos - 1 == PT)));
15239 }
15240 if (rv && (at_zv_p || exact_match_p))
15241 {
15242 rc = CURSOR_MOVEMENT_SUCCESS;
15243 break;
15244 }
15245 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15246 break;
15247 ++row;
15248 }
15249 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15250 || row->continued_p)
15251 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15252 || (MATRIX_ROW_START_CHARPOS (row) == PT
15253 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15254 /* If we didn't find any candidate rows, or exited the
15255 loop before all the candidates were examined, signal
15256 to the caller that this method failed. */
15257 if (rc != CURSOR_MOVEMENT_SUCCESS
15258 && !(rv
15259 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15260 && !row->continued_p))
15261 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15262 else if (rv)
15263 rc = CURSOR_MOVEMENT_SUCCESS;
15264 }
15265 else
15266 {
15267 do
15268 {
15269 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15270 {
15271 rc = CURSOR_MOVEMENT_SUCCESS;
15272 break;
15273 }
15274 ++row;
15275 }
15276 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15277 && MATRIX_ROW_START_CHARPOS (row) == PT
15278 && cursor_row_p (row));
15279 }
15280 }
15281 }
15282
15283 return rc;
15284 }
15285
15286 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15287 static
15288 #endif
15289 void
15290 set_vertical_scroll_bar (struct window *w)
15291 {
15292 ptrdiff_t start, end, whole;
15293
15294 /* Calculate the start and end positions for the current window.
15295 At some point, it would be nice to choose between scrollbars
15296 which reflect the whole buffer size, with special markers
15297 indicating narrowing, and scrollbars which reflect only the
15298 visible region.
15299
15300 Note that mini-buffers sometimes aren't displaying any text. */
15301 if (!MINI_WINDOW_P (w)
15302 || (w == XWINDOW (minibuf_window)
15303 && NILP (echo_area_buffer[0])))
15304 {
15305 struct buffer *buf = XBUFFER (w->buffer);
15306 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15307 start = marker_position (w->start) - BUF_BEGV (buf);
15308 /* I don't think this is guaranteed to be right. For the
15309 moment, we'll pretend it is. */
15310 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15311
15312 if (end < start)
15313 end = start;
15314 if (whole < (end - start))
15315 whole = end - start;
15316 }
15317 else
15318 start = end = whole = 0;
15319
15320 /* Indicate what this scroll bar ought to be displaying now. */
15321 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15322 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15323 (w, end - start, whole, start);
15324 }
15325
15326
15327 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15328 selected_window is redisplayed.
15329
15330 We can return without actually redisplaying the window if
15331 fonts_changed_p is nonzero. In that case, redisplay_internal will
15332 retry. */
15333
15334 static void
15335 redisplay_window (Lisp_Object window, int just_this_one_p)
15336 {
15337 struct window *w = XWINDOW (window);
15338 struct frame *f = XFRAME (w->frame);
15339 struct buffer *buffer = XBUFFER (w->buffer);
15340 struct buffer *old = current_buffer;
15341 struct text_pos lpoint, opoint, startp;
15342 int update_mode_line;
15343 int tem;
15344 struct it it;
15345 /* Record it now because it's overwritten. */
15346 int current_matrix_up_to_date_p = 0;
15347 int used_current_matrix_p = 0;
15348 /* This is less strict than current_matrix_up_to_date_p.
15349 It indicates that the buffer contents and narrowing are unchanged. */
15350 int buffer_unchanged_p = 0;
15351 int temp_scroll_step = 0;
15352 ptrdiff_t count = SPECPDL_INDEX ();
15353 int rc;
15354 int centering_position = -1;
15355 int last_line_misfit = 0;
15356 ptrdiff_t beg_unchanged, end_unchanged;
15357
15358 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15359 opoint = lpoint;
15360
15361 /* W must be a leaf window here. */
15362 eassert (!NILP (w->buffer));
15363 #ifdef GLYPH_DEBUG
15364 *w->desired_matrix->method = 0;
15365 #endif
15366
15367 restart:
15368 reconsider_clip_changes (w, buffer);
15369
15370 /* Has the mode line to be updated? */
15371 update_mode_line = (w->update_mode_line
15372 || update_mode_lines
15373 || buffer->clip_changed
15374 || buffer->prevent_redisplay_optimizations_p);
15375
15376 if (MINI_WINDOW_P (w))
15377 {
15378 if (w == XWINDOW (echo_area_window)
15379 && !NILP (echo_area_buffer[0]))
15380 {
15381 if (update_mode_line)
15382 /* We may have to update a tty frame's menu bar or a
15383 tool-bar. Example `M-x C-h C-h C-g'. */
15384 goto finish_menu_bars;
15385 else
15386 /* We've already displayed the echo area glyphs in this window. */
15387 goto finish_scroll_bars;
15388 }
15389 else if ((w != XWINDOW (minibuf_window)
15390 || minibuf_level == 0)
15391 /* When buffer is nonempty, redisplay window normally. */
15392 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15393 /* Quail displays non-mini buffers in minibuffer window.
15394 In that case, redisplay the window normally. */
15395 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15396 {
15397 /* W is a mini-buffer window, but it's not active, so clear
15398 it. */
15399 int yb = window_text_bottom_y (w);
15400 struct glyph_row *row;
15401 int y;
15402
15403 for (y = 0, row = w->desired_matrix->rows;
15404 y < yb;
15405 y += row->height, ++row)
15406 blank_row (w, row, y);
15407 goto finish_scroll_bars;
15408 }
15409
15410 clear_glyph_matrix (w->desired_matrix);
15411 }
15412
15413 /* Otherwise set up data on this window; select its buffer and point
15414 value. */
15415 /* Really select the buffer, for the sake of buffer-local
15416 variables. */
15417 set_buffer_internal_1 (XBUFFER (w->buffer));
15418
15419 current_matrix_up_to_date_p
15420 = (!NILP (w->window_end_valid)
15421 && !current_buffer->clip_changed
15422 && !current_buffer->prevent_redisplay_optimizations_p
15423 && w->last_modified >= MODIFF
15424 && w->last_overlay_modified >= OVERLAY_MODIFF);
15425
15426 /* Run the window-bottom-change-functions
15427 if it is possible that the text on the screen has changed
15428 (either due to modification of the text, or any other reason). */
15429 if (!current_matrix_up_to_date_p
15430 && !NILP (Vwindow_text_change_functions))
15431 {
15432 safe_run_hooks (Qwindow_text_change_functions);
15433 goto restart;
15434 }
15435
15436 beg_unchanged = BEG_UNCHANGED;
15437 end_unchanged = END_UNCHANGED;
15438
15439 SET_TEXT_POS (opoint, PT, PT_BYTE);
15440
15441 specbind (Qinhibit_point_motion_hooks, Qt);
15442
15443 buffer_unchanged_p
15444 = (!NILP (w->window_end_valid)
15445 && !current_buffer->clip_changed
15446 && w->last_modified >= MODIFF
15447 && w->last_overlay_modified >= OVERLAY_MODIFF);
15448
15449 /* When windows_or_buffers_changed is non-zero, we can't rely on
15450 the window end being valid, so set it to nil there. */
15451 if (windows_or_buffers_changed)
15452 {
15453 /* If window starts on a continuation line, maybe adjust the
15454 window start in case the window's width changed. */
15455 if (XMARKER (w->start)->buffer == current_buffer)
15456 compute_window_start_on_continuation_line (w);
15457
15458 w->window_end_valid = Qnil;
15459 }
15460
15461 /* Some sanity checks. */
15462 CHECK_WINDOW_END (w);
15463 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15464 abort ();
15465 if (BYTEPOS (opoint) < CHARPOS (opoint))
15466 abort ();
15467
15468 /* If %c is in mode line, update it if needed. */
15469 if (!NILP (w->column_number_displayed)
15470 /* This alternative quickly identifies a common case
15471 where no change is needed. */
15472 && !(PT == w->last_point
15473 && w->last_modified >= MODIFF
15474 && w->last_overlay_modified >= OVERLAY_MODIFF)
15475 && (XFASTINT (w->column_number_displayed) != current_column ()))
15476 update_mode_line = 1;
15477
15478 /* Count number of windows showing the selected buffer. An indirect
15479 buffer counts as its base buffer. */
15480 if (!just_this_one_p)
15481 {
15482 struct buffer *current_base, *window_base;
15483 current_base = current_buffer;
15484 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15485 if (current_base->base_buffer)
15486 current_base = current_base->base_buffer;
15487 if (window_base->base_buffer)
15488 window_base = window_base->base_buffer;
15489 if (current_base == window_base)
15490 buffer_shared++;
15491 }
15492
15493 /* Point refers normally to the selected window. For any other
15494 window, set up appropriate value. */
15495 if (!EQ (window, selected_window))
15496 {
15497 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15498 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15499 if (new_pt < BEGV)
15500 {
15501 new_pt = BEGV;
15502 new_pt_byte = BEGV_BYTE;
15503 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15504 }
15505 else if (new_pt > (ZV - 1))
15506 {
15507 new_pt = ZV;
15508 new_pt_byte = ZV_BYTE;
15509 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15510 }
15511
15512 /* We don't use SET_PT so that the point-motion hooks don't run. */
15513 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15514 }
15515
15516 /* If any of the character widths specified in the display table
15517 have changed, invalidate the width run cache. It's true that
15518 this may be a bit late to catch such changes, but the rest of
15519 redisplay goes (non-fatally) haywire when the display table is
15520 changed, so why should we worry about doing any better? */
15521 if (current_buffer->width_run_cache)
15522 {
15523 struct Lisp_Char_Table *disptab = buffer_display_table ();
15524
15525 if (! disptab_matches_widthtab (disptab,
15526 XVECTOR (BVAR (current_buffer, width_table))))
15527 {
15528 invalidate_region_cache (current_buffer,
15529 current_buffer->width_run_cache,
15530 BEG, Z);
15531 recompute_width_table (current_buffer, disptab);
15532 }
15533 }
15534
15535 /* If window-start is screwed up, choose a new one. */
15536 if (XMARKER (w->start)->buffer != current_buffer)
15537 goto recenter;
15538
15539 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15540
15541 /* If someone specified a new starting point but did not insist,
15542 check whether it can be used. */
15543 if (w->optional_new_start
15544 && CHARPOS (startp) >= BEGV
15545 && CHARPOS (startp) <= ZV)
15546 {
15547 w->optional_new_start = 0;
15548 start_display (&it, w, startp);
15549 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15550 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15551 if (IT_CHARPOS (it) == PT)
15552 w->force_start = 1;
15553 /* IT may overshoot PT if text at PT is invisible. */
15554 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15555 w->force_start = 1;
15556 }
15557
15558 force_start:
15559
15560 /* Handle case where place to start displaying has been specified,
15561 unless the specified location is outside the accessible range. */
15562 if (w->force_start || w->frozen_window_start_p)
15563 {
15564 /* We set this later on if we have to adjust point. */
15565 int new_vpos = -1;
15566
15567 w->force_start = 0;
15568 w->vscroll = 0;
15569 w->window_end_valid = Qnil;
15570
15571 /* Forget any recorded base line for line number display. */
15572 if (!buffer_unchanged_p)
15573 w->base_line_number = Qnil;
15574
15575 /* Redisplay the mode line. Select the buffer properly for that.
15576 Also, run the hook window-scroll-functions
15577 because we have scrolled. */
15578 /* Note, we do this after clearing force_start because
15579 if there's an error, it is better to forget about force_start
15580 than to get into an infinite loop calling the hook functions
15581 and having them get more errors. */
15582 if (!update_mode_line
15583 || ! NILP (Vwindow_scroll_functions))
15584 {
15585 update_mode_line = 1;
15586 w->update_mode_line = 1;
15587 startp = run_window_scroll_functions (window, startp);
15588 }
15589
15590 w->last_modified = 0;
15591 w->last_overlay_modified = 0;
15592 if (CHARPOS (startp) < BEGV)
15593 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15594 else if (CHARPOS (startp) > ZV)
15595 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15596
15597 /* Redisplay, then check if cursor has been set during the
15598 redisplay. Give up if new fonts were loaded. */
15599 /* We used to issue a CHECK_MARGINS argument to try_window here,
15600 but this causes scrolling to fail when point begins inside
15601 the scroll margin (bug#148) -- cyd */
15602 if (!try_window (window, startp, 0))
15603 {
15604 w->force_start = 1;
15605 clear_glyph_matrix (w->desired_matrix);
15606 goto need_larger_matrices;
15607 }
15608
15609 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15610 {
15611 /* If point does not appear, try to move point so it does
15612 appear. The desired matrix has been built above, so we
15613 can use it here. */
15614 new_vpos = window_box_height (w) / 2;
15615 }
15616
15617 if (!cursor_row_fully_visible_p (w, 0, 0))
15618 {
15619 /* Point does appear, but on a line partly visible at end of window.
15620 Move it back to a fully-visible line. */
15621 new_vpos = window_box_height (w);
15622 }
15623
15624 /* If we need to move point for either of the above reasons,
15625 now actually do it. */
15626 if (new_vpos >= 0)
15627 {
15628 struct glyph_row *row;
15629
15630 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15631 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15632 ++row;
15633
15634 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15635 MATRIX_ROW_START_BYTEPOS (row));
15636
15637 if (w != XWINDOW (selected_window))
15638 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15639 else if (current_buffer == old)
15640 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15641
15642 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15643
15644 /* If we are highlighting the region, then we just changed
15645 the region, so redisplay to show it. */
15646 if (!NILP (Vtransient_mark_mode)
15647 && !NILP (BVAR (current_buffer, mark_active)))
15648 {
15649 clear_glyph_matrix (w->desired_matrix);
15650 if (!try_window (window, startp, 0))
15651 goto need_larger_matrices;
15652 }
15653 }
15654
15655 #ifdef GLYPH_DEBUG
15656 debug_method_add (w, "forced window start");
15657 #endif
15658 goto done;
15659 }
15660
15661 /* Handle case where text has not changed, only point, and it has
15662 not moved off the frame, and we are not retrying after hscroll.
15663 (current_matrix_up_to_date_p is nonzero when retrying.) */
15664 if (current_matrix_up_to_date_p
15665 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15666 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15667 {
15668 switch (rc)
15669 {
15670 case CURSOR_MOVEMENT_SUCCESS:
15671 used_current_matrix_p = 1;
15672 goto done;
15673
15674 case CURSOR_MOVEMENT_MUST_SCROLL:
15675 goto try_to_scroll;
15676
15677 default:
15678 abort ();
15679 }
15680 }
15681 /* If current starting point was originally the beginning of a line
15682 but no longer is, find a new starting point. */
15683 else if (w->start_at_line_beg
15684 && !(CHARPOS (startp) <= BEGV
15685 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15686 {
15687 #ifdef GLYPH_DEBUG
15688 debug_method_add (w, "recenter 1");
15689 #endif
15690 goto recenter;
15691 }
15692
15693 /* Try scrolling with try_window_id. Value is > 0 if update has
15694 been done, it is -1 if we know that the same window start will
15695 not work. It is 0 if unsuccessful for some other reason. */
15696 else if ((tem = try_window_id (w)) != 0)
15697 {
15698 #ifdef GLYPH_DEBUG
15699 debug_method_add (w, "try_window_id %d", tem);
15700 #endif
15701
15702 if (fonts_changed_p)
15703 goto need_larger_matrices;
15704 if (tem > 0)
15705 goto done;
15706
15707 /* Otherwise try_window_id has returned -1 which means that we
15708 don't want the alternative below this comment to execute. */
15709 }
15710 else if (CHARPOS (startp) >= BEGV
15711 && CHARPOS (startp) <= ZV
15712 && PT >= CHARPOS (startp)
15713 && (CHARPOS (startp) < ZV
15714 /* Avoid starting at end of buffer. */
15715 || CHARPOS (startp) == BEGV
15716 || (w->last_modified >= MODIFF
15717 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15718 {
15719 int d1, d2, d3, d4, d5, d6;
15720
15721 /* If first window line is a continuation line, and window start
15722 is inside the modified region, but the first change is before
15723 current window start, we must select a new window start.
15724
15725 However, if this is the result of a down-mouse event (e.g. by
15726 extending the mouse-drag-overlay), we don't want to select a
15727 new window start, since that would change the position under
15728 the mouse, resulting in an unwanted mouse-movement rather
15729 than a simple mouse-click. */
15730 if (!w->start_at_line_beg
15731 && NILP (do_mouse_tracking)
15732 && CHARPOS (startp) > BEGV
15733 && CHARPOS (startp) > BEG + beg_unchanged
15734 && CHARPOS (startp) <= Z - end_unchanged
15735 /* Even if w->start_at_line_beg is nil, a new window may
15736 start at a line_beg, since that's how set_buffer_window
15737 sets it. So, we need to check the return value of
15738 compute_window_start_on_continuation_line. (See also
15739 bug#197). */
15740 && XMARKER (w->start)->buffer == current_buffer
15741 && compute_window_start_on_continuation_line (w)
15742 /* It doesn't make sense to force the window start like we
15743 do at label force_start if it is already known that point
15744 will not be visible in the resulting window, because
15745 doing so will move point from its correct position
15746 instead of scrolling the window to bring point into view.
15747 See bug#9324. */
15748 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15749 {
15750 w->force_start = 1;
15751 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15752 goto force_start;
15753 }
15754
15755 #ifdef GLYPH_DEBUG
15756 debug_method_add (w, "same window start");
15757 #endif
15758
15759 /* Try to redisplay starting at same place as before.
15760 If point has not moved off frame, accept the results. */
15761 if (!current_matrix_up_to_date_p
15762 /* Don't use try_window_reusing_current_matrix in this case
15763 because a window scroll function can have changed the
15764 buffer. */
15765 || !NILP (Vwindow_scroll_functions)
15766 || MINI_WINDOW_P (w)
15767 || !(used_current_matrix_p
15768 = try_window_reusing_current_matrix (w)))
15769 {
15770 IF_DEBUG (debug_method_add (w, "1"));
15771 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15772 /* -1 means we need to scroll.
15773 0 means we need new matrices, but fonts_changed_p
15774 is set in that case, so we will detect it below. */
15775 goto try_to_scroll;
15776 }
15777
15778 if (fonts_changed_p)
15779 goto need_larger_matrices;
15780
15781 if (w->cursor.vpos >= 0)
15782 {
15783 if (!just_this_one_p
15784 || current_buffer->clip_changed
15785 || BEG_UNCHANGED < CHARPOS (startp))
15786 /* Forget any recorded base line for line number display. */
15787 w->base_line_number = Qnil;
15788
15789 if (!cursor_row_fully_visible_p (w, 1, 0))
15790 {
15791 clear_glyph_matrix (w->desired_matrix);
15792 last_line_misfit = 1;
15793 }
15794 /* Drop through and scroll. */
15795 else
15796 goto done;
15797 }
15798 else
15799 clear_glyph_matrix (w->desired_matrix);
15800 }
15801
15802 try_to_scroll:
15803
15804 w->last_modified = 0;
15805 w->last_overlay_modified = 0;
15806
15807 /* Redisplay the mode line. Select the buffer properly for that. */
15808 if (!update_mode_line)
15809 {
15810 update_mode_line = 1;
15811 w->update_mode_line = 1;
15812 }
15813
15814 /* Try to scroll by specified few lines. */
15815 if ((scroll_conservatively
15816 || emacs_scroll_step
15817 || temp_scroll_step
15818 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15819 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15820 && CHARPOS (startp) >= BEGV
15821 && CHARPOS (startp) <= ZV)
15822 {
15823 /* The function returns -1 if new fonts were loaded, 1 if
15824 successful, 0 if not successful. */
15825 int ss = try_scrolling (window, just_this_one_p,
15826 scroll_conservatively,
15827 emacs_scroll_step,
15828 temp_scroll_step, last_line_misfit);
15829 switch (ss)
15830 {
15831 case SCROLLING_SUCCESS:
15832 goto done;
15833
15834 case SCROLLING_NEED_LARGER_MATRICES:
15835 goto need_larger_matrices;
15836
15837 case SCROLLING_FAILED:
15838 break;
15839
15840 default:
15841 abort ();
15842 }
15843 }
15844
15845 /* Finally, just choose a place to start which positions point
15846 according to user preferences. */
15847
15848 recenter:
15849
15850 #ifdef GLYPH_DEBUG
15851 debug_method_add (w, "recenter");
15852 #endif
15853
15854 /* w->vscroll = 0; */
15855
15856 /* Forget any previously recorded base line for line number display. */
15857 if (!buffer_unchanged_p)
15858 w->base_line_number = Qnil;
15859
15860 /* Determine the window start relative to point. */
15861 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15862 it.current_y = it.last_visible_y;
15863 if (centering_position < 0)
15864 {
15865 int margin =
15866 scroll_margin > 0
15867 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15868 : 0;
15869 ptrdiff_t margin_pos = CHARPOS (startp);
15870 Lisp_Object aggressive;
15871 int scrolling_up;
15872
15873 /* If there is a scroll margin at the top of the window, find
15874 its character position. */
15875 if (margin
15876 /* Cannot call start_display if startp is not in the
15877 accessible region of the buffer. This can happen when we
15878 have just switched to a different buffer and/or changed
15879 its restriction. In that case, startp is initialized to
15880 the character position 1 (BEGV) because we did not yet
15881 have chance to display the buffer even once. */
15882 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15883 {
15884 struct it it1;
15885 void *it1data = NULL;
15886
15887 SAVE_IT (it1, it, it1data);
15888 start_display (&it1, w, startp);
15889 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15890 margin_pos = IT_CHARPOS (it1);
15891 RESTORE_IT (&it, &it, it1data);
15892 }
15893 scrolling_up = PT > margin_pos;
15894 aggressive =
15895 scrolling_up
15896 ? BVAR (current_buffer, scroll_up_aggressively)
15897 : BVAR (current_buffer, scroll_down_aggressively);
15898
15899 if (!MINI_WINDOW_P (w)
15900 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15901 {
15902 int pt_offset = 0;
15903
15904 /* Setting scroll-conservatively overrides
15905 scroll-*-aggressively. */
15906 if (!scroll_conservatively && NUMBERP (aggressive))
15907 {
15908 double float_amount = XFLOATINT (aggressive);
15909
15910 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15911 if (pt_offset == 0 && float_amount > 0)
15912 pt_offset = 1;
15913 if (pt_offset && margin > 0)
15914 margin -= 1;
15915 }
15916 /* Compute how much to move the window start backward from
15917 point so that point will be displayed where the user
15918 wants it. */
15919 if (scrolling_up)
15920 {
15921 centering_position = it.last_visible_y;
15922 if (pt_offset)
15923 centering_position -= pt_offset;
15924 centering_position -=
15925 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15926 + WINDOW_HEADER_LINE_HEIGHT (w);
15927 /* Don't let point enter the scroll margin near top of
15928 the window. */
15929 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15930 centering_position = margin * FRAME_LINE_HEIGHT (f);
15931 }
15932 else
15933 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15934 }
15935 else
15936 /* Set the window start half the height of the window backward
15937 from point. */
15938 centering_position = window_box_height (w) / 2;
15939 }
15940 move_it_vertically_backward (&it, centering_position);
15941
15942 eassert (IT_CHARPOS (it) >= BEGV);
15943
15944 /* The function move_it_vertically_backward may move over more
15945 than the specified y-distance. If it->w is small, e.g. a
15946 mini-buffer window, we may end up in front of the window's
15947 display area. Start displaying at the start of the line
15948 containing PT in this case. */
15949 if (it.current_y <= 0)
15950 {
15951 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15952 move_it_vertically_backward (&it, 0);
15953 it.current_y = 0;
15954 }
15955
15956 it.current_x = it.hpos = 0;
15957
15958 /* Set the window start position here explicitly, to avoid an
15959 infinite loop in case the functions in window-scroll-functions
15960 get errors. */
15961 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15962
15963 /* Run scroll hooks. */
15964 startp = run_window_scroll_functions (window, it.current.pos);
15965
15966 /* Redisplay the window. */
15967 if (!current_matrix_up_to_date_p
15968 || windows_or_buffers_changed
15969 || cursor_type_changed
15970 /* Don't use try_window_reusing_current_matrix in this case
15971 because it can have changed the buffer. */
15972 || !NILP (Vwindow_scroll_functions)
15973 || !just_this_one_p
15974 || MINI_WINDOW_P (w)
15975 || !(used_current_matrix_p
15976 = try_window_reusing_current_matrix (w)))
15977 try_window (window, startp, 0);
15978
15979 /* If new fonts have been loaded (due to fontsets), give up. We
15980 have to start a new redisplay since we need to re-adjust glyph
15981 matrices. */
15982 if (fonts_changed_p)
15983 goto need_larger_matrices;
15984
15985 /* If cursor did not appear assume that the middle of the window is
15986 in the first line of the window. Do it again with the next line.
15987 (Imagine a window of height 100, displaying two lines of height
15988 60. Moving back 50 from it->last_visible_y will end in the first
15989 line.) */
15990 if (w->cursor.vpos < 0)
15991 {
15992 if (!NILP (w->window_end_valid)
15993 && PT >= Z - XFASTINT (w->window_end_pos))
15994 {
15995 clear_glyph_matrix (w->desired_matrix);
15996 move_it_by_lines (&it, 1);
15997 try_window (window, it.current.pos, 0);
15998 }
15999 else if (PT < IT_CHARPOS (it))
16000 {
16001 clear_glyph_matrix (w->desired_matrix);
16002 move_it_by_lines (&it, -1);
16003 try_window (window, it.current.pos, 0);
16004 }
16005 else
16006 {
16007 /* Not much we can do about it. */
16008 }
16009 }
16010
16011 /* Consider the following case: Window starts at BEGV, there is
16012 invisible, intangible text at BEGV, so that display starts at
16013 some point START > BEGV. It can happen that we are called with
16014 PT somewhere between BEGV and START. Try to handle that case. */
16015 if (w->cursor.vpos < 0)
16016 {
16017 struct glyph_row *row = w->current_matrix->rows;
16018 if (row->mode_line_p)
16019 ++row;
16020 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16021 }
16022
16023 if (!cursor_row_fully_visible_p (w, 0, 0))
16024 {
16025 /* If vscroll is enabled, disable it and try again. */
16026 if (w->vscroll)
16027 {
16028 w->vscroll = 0;
16029 clear_glyph_matrix (w->desired_matrix);
16030 goto recenter;
16031 }
16032
16033 /* Users who set scroll-conservatively to a large number want
16034 point just above/below the scroll margin. If we ended up
16035 with point's row partially visible, move the window start to
16036 make that row fully visible and out of the margin. */
16037 if (scroll_conservatively > SCROLL_LIMIT)
16038 {
16039 int margin =
16040 scroll_margin > 0
16041 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16042 : 0;
16043 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16044
16045 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16046 clear_glyph_matrix (w->desired_matrix);
16047 if (1 == try_window (window, it.current.pos,
16048 TRY_WINDOW_CHECK_MARGINS))
16049 goto done;
16050 }
16051
16052 /* If centering point failed to make the whole line visible,
16053 put point at the top instead. That has to make the whole line
16054 visible, if it can be done. */
16055 if (centering_position == 0)
16056 goto done;
16057
16058 clear_glyph_matrix (w->desired_matrix);
16059 centering_position = 0;
16060 goto recenter;
16061 }
16062
16063 done:
16064
16065 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16066 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16067 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16068
16069 /* Display the mode line, if we must. */
16070 if ((update_mode_line
16071 /* If window not full width, must redo its mode line
16072 if (a) the window to its side is being redone and
16073 (b) we do a frame-based redisplay. This is a consequence
16074 of how inverted lines are drawn in frame-based redisplay. */
16075 || (!just_this_one_p
16076 && !FRAME_WINDOW_P (f)
16077 && !WINDOW_FULL_WIDTH_P (w))
16078 /* Line number to display. */
16079 || INTEGERP (w->base_line_pos)
16080 /* Column number is displayed and different from the one displayed. */
16081 || (!NILP (w->column_number_displayed)
16082 && (XFASTINT (w->column_number_displayed) != current_column ())))
16083 /* This means that the window has a mode line. */
16084 && (WINDOW_WANTS_MODELINE_P (w)
16085 || WINDOW_WANTS_HEADER_LINE_P (w)))
16086 {
16087 display_mode_lines (w);
16088
16089 /* If mode line height has changed, arrange for a thorough
16090 immediate redisplay using the correct mode line height. */
16091 if (WINDOW_WANTS_MODELINE_P (w)
16092 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16093 {
16094 fonts_changed_p = 1;
16095 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16096 = DESIRED_MODE_LINE_HEIGHT (w);
16097 }
16098
16099 /* If header line height has changed, arrange for a thorough
16100 immediate redisplay using the correct header line height. */
16101 if (WINDOW_WANTS_HEADER_LINE_P (w)
16102 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16103 {
16104 fonts_changed_p = 1;
16105 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16106 = DESIRED_HEADER_LINE_HEIGHT (w);
16107 }
16108
16109 if (fonts_changed_p)
16110 goto need_larger_matrices;
16111 }
16112
16113 if (!line_number_displayed
16114 && !BUFFERP (w->base_line_pos))
16115 {
16116 w->base_line_pos = Qnil;
16117 w->base_line_number = Qnil;
16118 }
16119
16120 finish_menu_bars:
16121
16122 /* When we reach a frame's selected window, redo the frame's menu bar. */
16123 if (update_mode_line
16124 && EQ (FRAME_SELECTED_WINDOW (f), window))
16125 {
16126 int redisplay_menu_p = 0;
16127
16128 if (FRAME_WINDOW_P (f))
16129 {
16130 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16131 || defined (HAVE_NS) || defined (USE_GTK)
16132 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16133 #else
16134 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16135 #endif
16136 }
16137 else
16138 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16139
16140 if (redisplay_menu_p)
16141 display_menu_bar (w);
16142
16143 #ifdef HAVE_WINDOW_SYSTEM
16144 if (FRAME_WINDOW_P (f))
16145 {
16146 #if defined (USE_GTK) || defined (HAVE_NS)
16147 if (FRAME_EXTERNAL_TOOL_BAR (f))
16148 redisplay_tool_bar (f);
16149 #else
16150 if (WINDOWP (f->tool_bar_window)
16151 && (FRAME_TOOL_BAR_LINES (f) > 0
16152 || !NILP (Vauto_resize_tool_bars))
16153 && redisplay_tool_bar (f))
16154 ignore_mouse_drag_p = 1;
16155 #endif
16156 }
16157 #endif
16158 }
16159
16160 #ifdef HAVE_WINDOW_SYSTEM
16161 if (FRAME_WINDOW_P (f)
16162 && update_window_fringes (w, (just_this_one_p
16163 || (!used_current_matrix_p && !overlay_arrow_seen)
16164 || w->pseudo_window_p)))
16165 {
16166 update_begin (f);
16167 BLOCK_INPUT;
16168 if (draw_window_fringes (w, 1))
16169 x_draw_vertical_border (w);
16170 UNBLOCK_INPUT;
16171 update_end (f);
16172 }
16173 #endif /* HAVE_WINDOW_SYSTEM */
16174
16175 /* We go to this label, with fonts_changed_p nonzero,
16176 if it is necessary to try again using larger glyph matrices.
16177 We have to redeem the scroll bar even in this case,
16178 because the loop in redisplay_internal expects that. */
16179 need_larger_matrices:
16180 ;
16181 finish_scroll_bars:
16182
16183 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16184 {
16185 /* Set the thumb's position and size. */
16186 set_vertical_scroll_bar (w);
16187
16188 /* Note that we actually used the scroll bar attached to this
16189 window, so it shouldn't be deleted at the end of redisplay. */
16190 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16191 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16192 }
16193
16194 /* Restore current_buffer and value of point in it. The window
16195 update may have changed the buffer, so first make sure `opoint'
16196 is still valid (Bug#6177). */
16197 if (CHARPOS (opoint) < BEGV)
16198 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16199 else if (CHARPOS (opoint) > ZV)
16200 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16201 else
16202 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16203
16204 set_buffer_internal_1 (old);
16205 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16206 shorter. This can be caused by log truncation in *Messages*. */
16207 if (CHARPOS (lpoint) <= ZV)
16208 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16209
16210 unbind_to (count, Qnil);
16211 }
16212
16213
16214 /* Build the complete desired matrix of WINDOW with a window start
16215 buffer position POS.
16216
16217 Value is 1 if successful. It is zero if fonts were loaded during
16218 redisplay which makes re-adjusting glyph matrices necessary, and -1
16219 if point would appear in the scroll margins.
16220 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16221 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16222 set in FLAGS.) */
16223
16224 int
16225 try_window (Lisp_Object window, struct text_pos pos, int flags)
16226 {
16227 struct window *w = XWINDOW (window);
16228 struct it it;
16229 struct glyph_row *last_text_row = NULL;
16230 struct frame *f = XFRAME (w->frame);
16231
16232 /* Make POS the new window start. */
16233 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16234
16235 /* Mark cursor position as unknown. No overlay arrow seen. */
16236 w->cursor.vpos = -1;
16237 overlay_arrow_seen = 0;
16238
16239 /* Initialize iterator and info to start at POS. */
16240 start_display (&it, w, pos);
16241
16242 /* Display all lines of W. */
16243 while (it.current_y < it.last_visible_y)
16244 {
16245 if (display_line (&it))
16246 last_text_row = it.glyph_row - 1;
16247 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16248 return 0;
16249 }
16250
16251 /* Don't let the cursor end in the scroll margins. */
16252 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16253 && !MINI_WINDOW_P (w))
16254 {
16255 int this_scroll_margin;
16256
16257 if (scroll_margin > 0)
16258 {
16259 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16260 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16261 }
16262 else
16263 this_scroll_margin = 0;
16264
16265 if ((w->cursor.y >= 0 /* not vscrolled */
16266 && w->cursor.y < this_scroll_margin
16267 && CHARPOS (pos) > BEGV
16268 && IT_CHARPOS (it) < ZV)
16269 /* rms: considering make_cursor_line_fully_visible_p here
16270 seems to give wrong results. We don't want to recenter
16271 when the last line is partly visible, we want to allow
16272 that case to be handled in the usual way. */
16273 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16274 {
16275 w->cursor.vpos = -1;
16276 clear_glyph_matrix (w->desired_matrix);
16277 return -1;
16278 }
16279 }
16280
16281 /* If bottom moved off end of frame, change mode line percentage. */
16282 if (XFASTINT (w->window_end_pos) <= 0
16283 && Z != IT_CHARPOS (it))
16284 w->update_mode_line = 1;
16285
16286 /* Set window_end_pos to the offset of the last character displayed
16287 on the window from the end of current_buffer. Set
16288 window_end_vpos to its row number. */
16289 if (last_text_row)
16290 {
16291 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16292 w->window_end_bytepos
16293 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16294 w->window_end_pos
16295 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16296 w->window_end_vpos
16297 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16298 eassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16299 ->displays_text_p);
16300 }
16301 else
16302 {
16303 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16304 w->window_end_pos = make_number (Z - ZV);
16305 w->window_end_vpos = make_number (0);
16306 }
16307
16308 /* But that is not valid info until redisplay finishes. */
16309 w->window_end_valid = Qnil;
16310 return 1;
16311 }
16312
16313
16314 \f
16315 /************************************************************************
16316 Window redisplay reusing current matrix when buffer has not changed
16317 ************************************************************************/
16318
16319 /* Try redisplay of window W showing an unchanged buffer with a
16320 different window start than the last time it was displayed by
16321 reusing its current matrix. Value is non-zero if successful.
16322 W->start is the new window start. */
16323
16324 static int
16325 try_window_reusing_current_matrix (struct window *w)
16326 {
16327 struct frame *f = XFRAME (w->frame);
16328 struct glyph_row *bottom_row;
16329 struct it it;
16330 struct run run;
16331 struct text_pos start, new_start;
16332 int nrows_scrolled, i;
16333 struct glyph_row *last_text_row;
16334 struct glyph_row *last_reused_text_row;
16335 struct glyph_row *start_row;
16336 int start_vpos, min_y, max_y;
16337
16338 #ifdef GLYPH_DEBUG
16339 if (inhibit_try_window_reusing)
16340 return 0;
16341 #endif
16342
16343 if (/* This function doesn't handle terminal frames. */
16344 !FRAME_WINDOW_P (f)
16345 /* Don't try to reuse the display if windows have been split
16346 or such. */
16347 || windows_or_buffers_changed
16348 || cursor_type_changed)
16349 return 0;
16350
16351 /* Can't do this if region may have changed. */
16352 if ((!NILP (Vtransient_mark_mode)
16353 && !NILP (BVAR (current_buffer, mark_active)))
16354 || !NILP (w->region_showing)
16355 || !NILP (Vshow_trailing_whitespace))
16356 return 0;
16357
16358 /* If top-line visibility has changed, give up. */
16359 if (WINDOW_WANTS_HEADER_LINE_P (w)
16360 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16361 return 0;
16362
16363 /* Give up if old or new display is scrolled vertically. We could
16364 make this function handle this, but right now it doesn't. */
16365 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16366 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16367 return 0;
16368
16369 /* The variable new_start now holds the new window start. The old
16370 start `start' can be determined from the current matrix. */
16371 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16372 start = start_row->minpos;
16373 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16374
16375 /* Clear the desired matrix for the display below. */
16376 clear_glyph_matrix (w->desired_matrix);
16377
16378 if (CHARPOS (new_start) <= CHARPOS (start))
16379 {
16380 /* Don't use this method if the display starts with an ellipsis
16381 displayed for invisible text. It's not easy to handle that case
16382 below, and it's certainly not worth the effort since this is
16383 not a frequent case. */
16384 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16385 return 0;
16386
16387 IF_DEBUG (debug_method_add (w, "twu1"));
16388
16389 /* Display up to a row that can be reused. The variable
16390 last_text_row is set to the last row displayed that displays
16391 text. Note that it.vpos == 0 if or if not there is a
16392 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16393 start_display (&it, w, new_start);
16394 w->cursor.vpos = -1;
16395 last_text_row = last_reused_text_row = NULL;
16396
16397 while (it.current_y < it.last_visible_y
16398 && !fonts_changed_p)
16399 {
16400 /* If we have reached into the characters in the START row,
16401 that means the line boundaries have changed. So we
16402 can't start copying with the row START. Maybe it will
16403 work to start copying with the following row. */
16404 while (IT_CHARPOS (it) > CHARPOS (start))
16405 {
16406 /* Advance to the next row as the "start". */
16407 start_row++;
16408 start = start_row->minpos;
16409 /* If there are no more rows to try, or just one, give up. */
16410 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16411 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16412 || CHARPOS (start) == ZV)
16413 {
16414 clear_glyph_matrix (w->desired_matrix);
16415 return 0;
16416 }
16417
16418 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16419 }
16420 /* If we have reached alignment, we can copy the rest of the
16421 rows. */
16422 if (IT_CHARPOS (it) == CHARPOS (start)
16423 /* Don't accept "alignment" inside a display vector,
16424 since start_row could have started in the middle of
16425 that same display vector (thus their character
16426 positions match), and we have no way of telling if
16427 that is the case. */
16428 && it.current.dpvec_index < 0)
16429 break;
16430
16431 if (display_line (&it))
16432 last_text_row = it.glyph_row - 1;
16433
16434 }
16435
16436 /* A value of current_y < last_visible_y means that we stopped
16437 at the previous window start, which in turn means that we
16438 have at least one reusable row. */
16439 if (it.current_y < it.last_visible_y)
16440 {
16441 struct glyph_row *row;
16442
16443 /* IT.vpos always starts from 0; it counts text lines. */
16444 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16445
16446 /* Find PT if not already found in the lines displayed. */
16447 if (w->cursor.vpos < 0)
16448 {
16449 int dy = it.current_y - start_row->y;
16450
16451 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16452 row = row_containing_pos (w, PT, row, NULL, dy);
16453 if (row)
16454 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16455 dy, nrows_scrolled);
16456 else
16457 {
16458 clear_glyph_matrix (w->desired_matrix);
16459 return 0;
16460 }
16461 }
16462
16463 /* Scroll the display. Do it before the current matrix is
16464 changed. The problem here is that update has not yet
16465 run, i.e. part of the current matrix is not up to date.
16466 scroll_run_hook will clear the cursor, and use the
16467 current matrix to get the height of the row the cursor is
16468 in. */
16469 run.current_y = start_row->y;
16470 run.desired_y = it.current_y;
16471 run.height = it.last_visible_y - it.current_y;
16472
16473 if (run.height > 0 && run.current_y != run.desired_y)
16474 {
16475 update_begin (f);
16476 FRAME_RIF (f)->update_window_begin_hook (w);
16477 FRAME_RIF (f)->clear_window_mouse_face (w);
16478 FRAME_RIF (f)->scroll_run_hook (w, &run);
16479 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16480 update_end (f);
16481 }
16482
16483 /* Shift current matrix down by nrows_scrolled lines. */
16484 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16485 rotate_matrix (w->current_matrix,
16486 start_vpos,
16487 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16488 nrows_scrolled);
16489
16490 /* Disable lines that must be updated. */
16491 for (i = 0; i < nrows_scrolled; ++i)
16492 (start_row + i)->enabled_p = 0;
16493
16494 /* Re-compute Y positions. */
16495 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16496 max_y = it.last_visible_y;
16497 for (row = start_row + nrows_scrolled;
16498 row < bottom_row;
16499 ++row)
16500 {
16501 row->y = it.current_y;
16502 row->visible_height = row->height;
16503
16504 if (row->y < min_y)
16505 row->visible_height -= min_y - row->y;
16506 if (row->y + row->height > max_y)
16507 row->visible_height -= row->y + row->height - max_y;
16508 if (row->fringe_bitmap_periodic_p)
16509 row->redraw_fringe_bitmaps_p = 1;
16510
16511 it.current_y += row->height;
16512
16513 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16514 last_reused_text_row = row;
16515 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16516 break;
16517 }
16518
16519 /* Disable lines in the current matrix which are now
16520 below the window. */
16521 for (++row; row < bottom_row; ++row)
16522 row->enabled_p = row->mode_line_p = 0;
16523 }
16524
16525 /* Update window_end_pos etc.; last_reused_text_row is the last
16526 reused row from the current matrix containing text, if any.
16527 The value of last_text_row is the last displayed line
16528 containing text. */
16529 if (last_reused_text_row)
16530 {
16531 w->window_end_bytepos
16532 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16533 w->window_end_pos
16534 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16535 w->window_end_vpos
16536 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16537 w->current_matrix));
16538 }
16539 else if (last_text_row)
16540 {
16541 w->window_end_bytepos
16542 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16543 w->window_end_pos
16544 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16545 w->window_end_vpos
16546 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16547 }
16548 else
16549 {
16550 /* This window must be completely empty. */
16551 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16552 w->window_end_pos = make_number (Z - ZV);
16553 w->window_end_vpos = make_number (0);
16554 }
16555 w->window_end_valid = Qnil;
16556
16557 /* Update hint: don't try scrolling again in update_window. */
16558 w->desired_matrix->no_scrolling_p = 1;
16559
16560 #ifdef GLYPH_DEBUG
16561 debug_method_add (w, "try_window_reusing_current_matrix 1");
16562 #endif
16563 return 1;
16564 }
16565 else if (CHARPOS (new_start) > CHARPOS (start))
16566 {
16567 struct glyph_row *pt_row, *row;
16568 struct glyph_row *first_reusable_row;
16569 struct glyph_row *first_row_to_display;
16570 int dy;
16571 int yb = window_text_bottom_y (w);
16572
16573 /* Find the row starting at new_start, if there is one. Don't
16574 reuse a partially visible line at the end. */
16575 first_reusable_row = start_row;
16576 while (first_reusable_row->enabled_p
16577 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16578 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16579 < CHARPOS (new_start)))
16580 ++first_reusable_row;
16581
16582 /* Give up if there is no row to reuse. */
16583 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16584 || !first_reusable_row->enabled_p
16585 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16586 != CHARPOS (new_start)))
16587 return 0;
16588
16589 /* We can reuse fully visible rows beginning with
16590 first_reusable_row to the end of the window. Set
16591 first_row_to_display to the first row that cannot be reused.
16592 Set pt_row to the row containing point, if there is any. */
16593 pt_row = NULL;
16594 for (first_row_to_display = first_reusable_row;
16595 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16596 ++first_row_to_display)
16597 {
16598 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16599 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16600 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16601 && first_row_to_display->ends_at_zv_p
16602 && pt_row == NULL)))
16603 pt_row = first_row_to_display;
16604 }
16605
16606 /* Start displaying at the start of first_row_to_display. */
16607 eassert (first_row_to_display->y < yb);
16608 init_to_row_start (&it, w, first_row_to_display);
16609
16610 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16611 - start_vpos);
16612 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16613 - nrows_scrolled);
16614 it.current_y = (first_row_to_display->y - first_reusable_row->y
16615 + WINDOW_HEADER_LINE_HEIGHT (w));
16616
16617 /* Display lines beginning with first_row_to_display in the
16618 desired matrix. Set last_text_row to the last row displayed
16619 that displays text. */
16620 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16621 if (pt_row == NULL)
16622 w->cursor.vpos = -1;
16623 last_text_row = NULL;
16624 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16625 if (display_line (&it))
16626 last_text_row = it.glyph_row - 1;
16627
16628 /* If point is in a reused row, adjust y and vpos of the cursor
16629 position. */
16630 if (pt_row)
16631 {
16632 w->cursor.vpos -= nrows_scrolled;
16633 w->cursor.y -= first_reusable_row->y - start_row->y;
16634 }
16635
16636 /* Give up if point isn't in a row displayed or reused. (This
16637 also handles the case where w->cursor.vpos < nrows_scrolled
16638 after the calls to display_line, which can happen with scroll
16639 margins. See bug#1295.) */
16640 if (w->cursor.vpos < 0)
16641 {
16642 clear_glyph_matrix (w->desired_matrix);
16643 return 0;
16644 }
16645
16646 /* Scroll the display. */
16647 run.current_y = first_reusable_row->y;
16648 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16649 run.height = it.last_visible_y - run.current_y;
16650 dy = run.current_y - run.desired_y;
16651
16652 if (run.height)
16653 {
16654 update_begin (f);
16655 FRAME_RIF (f)->update_window_begin_hook (w);
16656 FRAME_RIF (f)->clear_window_mouse_face (w);
16657 FRAME_RIF (f)->scroll_run_hook (w, &run);
16658 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16659 update_end (f);
16660 }
16661
16662 /* Adjust Y positions of reused rows. */
16663 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16664 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16665 max_y = it.last_visible_y;
16666 for (row = first_reusable_row; row < first_row_to_display; ++row)
16667 {
16668 row->y -= dy;
16669 row->visible_height = row->height;
16670 if (row->y < min_y)
16671 row->visible_height -= min_y - row->y;
16672 if (row->y + row->height > max_y)
16673 row->visible_height -= row->y + row->height - max_y;
16674 if (row->fringe_bitmap_periodic_p)
16675 row->redraw_fringe_bitmaps_p = 1;
16676 }
16677
16678 /* Scroll the current matrix. */
16679 eassert (nrows_scrolled > 0);
16680 rotate_matrix (w->current_matrix,
16681 start_vpos,
16682 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16683 -nrows_scrolled);
16684
16685 /* Disable rows not reused. */
16686 for (row -= nrows_scrolled; row < bottom_row; ++row)
16687 row->enabled_p = 0;
16688
16689 /* Point may have moved to a different line, so we cannot assume that
16690 the previous cursor position is valid; locate the correct row. */
16691 if (pt_row)
16692 {
16693 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16694 row < bottom_row
16695 && PT >= MATRIX_ROW_END_CHARPOS (row)
16696 && !row->ends_at_zv_p;
16697 row++)
16698 {
16699 w->cursor.vpos++;
16700 w->cursor.y = row->y;
16701 }
16702 if (row < bottom_row)
16703 {
16704 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16705 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16706
16707 /* Can't use this optimization with bidi-reordered glyph
16708 rows, unless cursor is already at point. */
16709 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16710 {
16711 if (!(w->cursor.hpos >= 0
16712 && w->cursor.hpos < row->used[TEXT_AREA]
16713 && BUFFERP (glyph->object)
16714 && glyph->charpos == PT))
16715 return 0;
16716 }
16717 else
16718 for (; glyph < end
16719 && (!BUFFERP (glyph->object)
16720 || glyph->charpos < PT);
16721 glyph++)
16722 {
16723 w->cursor.hpos++;
16724 w->cursor.x += glyph->pixel_width;
16725 }
16726 }
16727 }
16728
16729 /* Adjust window end. A null value of last_text_row means that
16730 the window end is in reused rows which in turn means that
16731 only its vpos can have changed. */
16732 if (last_text_row)
16733 {
16734 w->window_end_bytepos
16735 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16736 w->window_end_pos
16737 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16738 w->window_end_vpos
16739 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16740 }
16741 else
16742 {
16743 w->window_end_vpos
16744 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16745 }
16746
16747 w->window_end_valid = Qnil;
16748 w->desired_matrix->no_scrolling_p = 1;
16749
16750 #ifdef GLYPH_DEBUG
16751 debug_method_add (w, "try_window_reusing_current_matrix 2");
16752 #endif
16753 return 1;
16754 }
16755
16756 return 0;
16757 }
16758
16759
16760 \f
16761 /************************************************************************
16762 Window redisplay reusing current matrix when buffer has changed
16763 ************************************************************************/
16764
16765 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16766 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16767 ptrdiff_t *, ptrdiff_t *);
16768 static struct glyph_row *
16769 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16770 struct glyph_row *);
16771
16772
16773 /* Return the last row in MATRIX displaying text. If row START is
16774 non-null, start searching with that row. IT gives the dimensions
16775 of the display. Value is null if matrix is empty; otherwise it is
16776 a pointer to the row found. */
16777
16778 static struct glyph_row *
16779 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16780 struct glyph_row *start)
16781 {
16782 struct glyph_row *row, *row_found;
16783
16784 /* Set row_found to the last row in IT->w's current matrix
16785 displaying text. The loop looks funny but think of partially
16786 visible lines. */
16787 row_found = NULL;
16788 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16789 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16790 {
16791 eassert (row->enabled_p);
16792 row_found = row;
16793 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16794 break;
16795 ++row;
16796 }
16797
16798 return row_found;
16799 }
16800
16801
16802 /* Return the last row in the current matrix of W that is not affected
16803 by changes at the start of current_buffer that occurred since W's
16804 current matrix was built. Value is null if no such row exists.
16805
16806 BEG_UNCHANGED us the number of characters unchanged at the start of
16807 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16808 first changed character in current_buffer. Characters at positions <
16809 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16810 when the current matrix was built. */
16811
16812 static struct glyph_row *
16813 find_last_unchanged_at_beg_row (struct window *w)
16814 {
16815 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16816 struct glyph_row *row;
16817 struct glyph_row *row_found = NULL;
16818 int yb = window_text_bottom_y (w);
16819
16820 /* Find the last row displaying unchanged text. */
16821 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16822 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16823 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16824 ++row)
16825 {
16826 if (/* If row ends before first_changed_pos, it is unchanged,
16827 except in some case. */
16828 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16829 /* When row ends in ZV and we write at ZV it is not
16830 unchanged. */
16831 && !row->ends_at_zv_p
16832 /* When first_changed_pos is the end of a continued line,
16833 row is not unchanged because it may be no longer
16834 continued. */
16835 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16836 && (row->continued_p
16837 || row->exact_window_width_line_p))
16838 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16839 needs to be recomputed, so don't consider this row as
16840 unchanged. This happens when the last line was
16841 bidi-reordered and was killed immediately before this
16842 redisplay cycle. In that case, ROW->end stores the
16843 buffer position of the first visual-order character of
16844 the killed text, which is now beyond ZV. */
16845 && CHARPOS (row->end.pos) <= ZV)
16846 row_found = row;
16847
16848 /* Stop if last visible row. */
16849 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16850 break;
16851 }
16852
16853 return row_found;
16854 }
16855
16856
16857 /* Find the first glyph row in the current matrix of W that is not
16858 affected by changes at the end of current_buffer since the
16859 time W's current matrix was built.
16860
16861 Return in *DELTA the number of chars by which buffer positions in
16862 unchanged text at the end of current_buffer must be adjusted.
16863
16864 Return in *DELTA_BYTES the corresponding number of bytes.
16865
16866 Value is null if no such row exists, i.e. all rows are affected by
16867 changes. */
16868
16869 static struct glyph_row *
16870 find_first_unchanged_at_end_row (struct window *w,
16871 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16872 {
16873 struct glyph_row *row;
16874 struct glyph_row *row_found = NULL;
16875
16876 *delta = *delta_bytes = 0;
16877
16878 /* Display must not have been paused, otherwise the current matrix
16879 is not up to date. */
16880 eassert (!NILP (w->window_end_valid));
16881
16882 /* A value of window_end_pos >= END_UNCHANGED means that the window
16883 end is in the range of changed text. If so, there is no
16884 unchanged row at the end of W's current matrix. */
16885 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16886 return NULL;
16887
16888 /* Set row to the last row in W's current matrix displaying text. */
16889 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16890
16891 /* If matrix is entirely empty, no unchanged row exists. */
16892 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16893 {
16894 /* The value of row is the last glyph row in the matrix having a
16895 meaningful buffer position in it. The end position of row
16896 corresponds to window_end_pos. This allows us to translate
16897 buffer positions in the current matrix to current buffer
16898 positions for characters not in changed text. */
16899 ptrdiff_t Z_old =
16900 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16901 ptrdiff_t Z_BYTE_old =
16902 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16903 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16904 struct glyph_row *first_text_row
16905 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16906
16907 *delta = Z - Z_old;
16908 *delta_bytes = Z_BYTE - Z_BYTE_old;
16909
16910 /* Set last_unchanged_pos to the buffer position of the last
16911 character in the buffer that has not been changed. Z is the
16912 index + 1 of the last character in current_buffer, i.e. by
16913 subtracting END_UNCHANGED we get the index of the last
16914 unchanged character, and we have to add BEG to get its buffer
16915 position. */
16916 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16917 last_unchanged_pos_old = last_unchanged_pos - *delta;
16918
16919 /* Search backward from ROW for a row displaying a line that
16920 starts at a minimum position >= last_unchanged_pos_old. */
16921 for (; row > first_text_row; --row)
16922 {
16923 /* This used to abort, but it can happen.
16924 It is ok to just stop the search instead here. KFS. */
16925 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16926 break;
16927
16928 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16929 row_found = row;
16930 }
16931 }
16932
16933 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16934
16935 return row_found;
16936 }
16937
16938
16939 /* Make sure that glyph rows in the current matrix of window W
16940 reference the same glyph memory as corresponding rows in the
16941 frame's frame matrix. This function is called after scrolling W's
16942 current matrix on a terminal frame in try_window_id and
16943 try_window_reusing_current_matrix. */
16944
16945 static void
16946 sync_frame_with_window_matrix_rows (struct window *w)
16947 {
16948 struct frame *f = XFRAME (w->frame);
16949 struct glyph_row *window_row, *window_row_end, *frame_row;
16950
16951 /* Preconditions: W must be a leaf window and full-width. Its frame
16952 must have a frame matrix. */
16953 eassert (NILP (w->hchild) && NILP (w->vchild));
16954 eassert (WINDOW_FULL_WIDTH_P (w));
16955 eassert (!FRAME_WINDOW_P (f));
16956
16957 /* If W is a full-width window, glyph pointers in W's current matrix
16958 have, by definition, to be the same as glyph pointers in the
16959 corresponding frame matrix. Note that frame matrices have no
16960 marginal areas (see build_frame_matrix). */
16961 window_row = w->current_matrix->rows;
16962 window_row_end = window_row + w->current_matrix->nrows;
16963 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16964 while (window_row < window_row_end)
16965 {
16966 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16967 struct glyph *end = window_row->glyphs[LAST_AREA];
16968
16969 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16970 frame_row->glyphs[TEXT_AREA] = start;
16971 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16972 frame_row->glyphs[LAST_AREA] = end;
16973
16974 /* Disable frame rows whose corresponding window rows have
16975 been disabled in try_window_id. */
16976 if (!window_row->enabled_p)
16977 frame_row->enabled_p = 0;
16978
16979 ++window_row, ++frame_row;
16980 }
16981 }
16982
16983
16984 /* Find the glyph row in window W containing CHARPOS. Consider all
16985 rows between START and END (not inclusive). END null means search
16986 all rows to the end of the display area of W. Value is the row
16987 containing CHARPOS or null. */
16988
16989 struct glyph_row *
16990 row_containing_pos (struct window *w, ptrdiff_t charpos,
16991 struct glyph_row *start, struct glyph_row *end, int dy)
16992 {
16993 struct glyph_row *row = start;
16994 struct glyph_row *best_row = NULL;
16995 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16996 int last_y;
16997
16998 /* If we happen to start on a header-line, skip that. */
16999 if (row->mode_line_p)
17000 ++row;
17001
17002 if ((end && row >= end) || !row->enabled_p)
17003 return NULL;
17004
17005 last_y = window_text_bottom_y (w) - dy;
17006
17007 while (1)
17008 {
17009 /* Give up if we have gone too far. */
17010 if (end && row >= end)
17011 return NULL;
17012 /* This formerly returned if they were equal.
17013 I think that both quantities are of a "last plus one" type;
17014 if so, when they are equal, the row is within the screen. -- rms. */
17015 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17016 return NULL;
17017
17018 /* If it is in this row, return this row. */
17019 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17020 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17021 /* The end position of a row equals the start
17022 position of the next row. If CHARPOS is there, we
17023 would rather display it in the next line, except
17024 when this line ends in ZV. */
17025 && !row->ends_at_zv_p
17026 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17027 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17028 {
17029 struct glyph *g;
17030
17031 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17032 || (!best_row && !row->continued_p))
17033 return row;
17034 /* In bidi-reordered rows, there could be several rows
17035 occluding point, all of them belonging to the same
17036 continued line. We need to find the row which fits
17037 CHARPOS the best. */
17038 for (g = row->glyphs[TEXT_AREA];
17039 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17040 g++)
17041 {
17042 if (!STRINGP (g->object))
17043 {
17044 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17045 {
17046 mindif = eabs (g->charpos - charpos);
17047 best_row = row;
17048 /* Exact match always wins. */
17049 if (mindif == 0)
17050 return best_row;
17051 }
17052 }
17053 }
17054 }
17055 else if (best_row && !row->continued_p)
17056 return best_row;
17057 ++row;
17058 }
17059 }
17060
17061
17062 /* Try to redisplay window W by reusing its existing display. W's
17063 current matrix must be up to date when this function is called,
17064 i.e. window_end_valid must not be nil.
17065
17066 Value is
17067
17068 1 if display has been updated
17069 0 if otherwise unsuccessful
17070 -1 if redisplay with same window start is known not to succeed
17071
17072 The following steps are performed:
17073
17074 1. Find the last row in the current matrix of W that is not
17075 affected by changes at the start of current_buffer. If no such row
17076 is found, give up.
17077
17078 2. Find the first row in W's current matrix that is not affected by
17079 changes at the end of current_buffer. Maybe there is no such row.
17080
17081 3. Display lines beginning with the row + 1 found in step 1 to the
17082 row found in step 2 or, if step 2 didn't find a row, to the end of
17083 the window.
17084
17085 4. If cursor is not known to appear on the window, give up.
17086
17087 5. If display stopped at the row found in step 2, scroll the
17088 display and current matrix as needed.
17089
17090 6. Maybe display some lines at the end of W, if we must. This can
17091 happen under various circumstances, like a partially visible line
17092 becoming fully visible, or because newly displayed lines are displayed
17093 in smaller font sizes.
17094
17095 7. Update W's window end information. */
17096
17097 static int
17098 try_window_id (struct window *w)
17099 {
17100 struct frame *f = XFRAME (w->frame);
17101 struct glyph_matrix *current_matrix = w->current_matrix;
17102 struct glyph_matrix *desired_matrix = w->desired_matrix;
17103 struct glyph_row *last_unchanged_at_beg_row;
17104 struct glyph_row *first_unchanged_at_end_row;
17105 struct glyph_row *row;
17106 struct glyph_row *bottom_row;
17107 int bottom_vpos;
17108 struct it it;
17109 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17110 int dvpos, dy;
17111 struct text_pos start_pos;
17112 struct run run;
17113 int first_unchanged_at_end_vpos = 0;
17114 struct glyph_row *last_text_row, *last_text_row_at_end;
17115 struct text_pos start;
17116 ptrdiff_t first_changed_charpos, last_changed_charpos;
17117
17118 #ifdef GLYPH_DEBUG
17119 if (inhibit_try_window_id)
17120 return 0;
17121 #endif
17122
17123 /* This is handy for debugging. */
17124 #if 0
17125 #define GIVE_UP(X) \
17126 do { \
17127 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17128 return 0; \
17129 } while (0)
17130 #else
17131 #define GIVE_UP(X) return 0
17132 #endif
17133
17134 SET_TEXT_POS_FROM_MARKER (start, w->start);
17135
17136 /* Don't use this for mini-windows because these can show
17137 messages and mini-buffers, and we don't handle that here. */
17138 if (MINI_WINDOW_P (w))
17139 GIVE_UP (1);
17140
17141 /* This flag is used to prevent redisplay optimizations. */
17142 if (windows_or_buffers_changed || cursor_type_changed)
17143 GIVE_UP (2);
17144
17145 /* Verify that narrowing has not changed.
17146 Also verify that we were not told to prevent redisplay optimizations.
17147 It would be nice to further
17148 reduce the number of cases where this prevents try_window_id. */
17149 if (current_buffer->clip_changed
17150 || current_buffer->prevent_redisplay_optimizations_p)
17151 GIVE_UP (3);
17152
17153 /* Window must either use window-based redisplay or be full width. */
17154 if (!FRAME_WINDOW_P (f)
17155 && (!FRAME_LINE_INS_DEL_OK (f)
17156 || !WINDOW_FULL_WIDTH_P (w)))
17157 GIVE_UP (4);
17158
17159 /* Give up if point is known NOT to appear in W. */
17160 if (PT < CHARPOS (start))
17161 GIVE_UP (5);
17162
17163 /* Another way to prevent redisplay optimizations. */
17164 if (w->last_modified == 0)
17165 GIVE_UP (6);
17166
17167 /* Verify that window is not hscrolled. */
17168 if (w->hscroll != 0)
17169 GIVE_UP (7);
17170
17171 /* Verify that display wasn't paused. */
17172 if (NILP (w->window_end_valid))
17173 GIVE_UP (8);
17174
17175 /* Can't use this if highlighting a region because a cursor movement
17176 will do more than just set the cursor. */
17177 if (!NILP (Vtransient_mark_mode)
17178 && !NILP (BVAR (current_buffer, mark_active)))
17179 GIVE_UP (9);
17180
17181 /* Likewise if highlighting trailing whitespace. */
17182 if (!NILP (Vshow_trailing_whitespace))
17183 GIVE_UP (11);
17184
17185 /* Likewise if showing a region. */
17186 if (!NILP (w->region_showing))
17187 GIVE_UP (10);
17188
17189 /* Can't use this if overlay arrow position and/or string have
17190 changed. */
17191 if (overlay_arrows_changed_p ())
17192 GIVE_UP (12);
17193
17194 /* When word-wrap is on, adding a space to the first word of a
17195 wrapped line can change the wrap position, altering the line
17196 above it. It might be worthwhile to handle this more
17197 intelligently, but for now just redisplay from scratch. */
17198 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17199 GIVE_UP (21);
17200
17201 /* Under bidi reordering, adding or deleting a character in the
17202 beginning of a paragraph, before the first strong directional
17203 character, can change the base direction of the paragraph (unless
17204 the buffer specifies a fixed paragraph direction), which will
17205 require to redisplay the whole paragraph. It might be worthwhile
17206 to find the paragraph limits and widen the range of redisplayed
17207 lines to that, but for now just give up this optimization and
17208 redisplay from scratch. */
17209 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17210 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17211 GIVE_UP (22);
17212
17213 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17214 only if buffer has really changed. The reason is that the gap is
17215 initially at Z for freshly visited files. The code below would
17216 set end_unchanged to 0 in that case. */
17217 if (MODIFF > SAVE_MODIFF
17218 /* This seems to happen sometimes after saving a buffer. */
17219 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17220 {
17221 if (GPT - BEG < BEG_UNCHANGED)
17222 BEG_UNCHANGED = GPT - BEG;
17223 if (Z - GPT < END_UNCHANGED)
17224 END_UNCHANGED = Z - GPT;
17225 }
17226
17227 /* The position of the first and last character that has been changed. */
17228 first_changed_charpos = BEG + BEG_UNCHANGED;
17229 last_changed_charpos = Z - END_UNCHANGED;
17230
17231 /* If window starts after a line end, and the last change is in
17232 front of that newline, then changes don't affect the display.
17233 This case happens with stealth-fontification. Note that although
17234 the display is unchanged, glyph positions in the matrix have to
17235 be adjusted, of course. */
17236 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17237 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17238 && ((last_changed_charpos < CHARPOS (start)
17239 && CHARPOS (start) == BEGV)
17240 || (last_changed_charpos < CHARPOS (start) - 1
17241 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17242 {
17243 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17244 struct glyph_row *r0;
17245
17246 /* Compute how many chars/bytes have been added to or removed
17247 from the buffer. */
17248 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17249 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17250 Z_delta = Z - Z_old;
17251 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17252
17253 /* Give up if PT is not in the window. Note that it already has
17254 been checked at the start of try_window_id that PT is not in
17255 front of the window start. */
17256 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17257 GIVE_UP (13);
17258
17259 /* If window start is unchanged, we can reuse the whole matrix
17260 as is, after adjusting glyph positions. No need to compute
17261 the window end again, since its offset from Z hasn't changed. */
17262 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17263 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17264 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17265 /* PT must not be in a partially visible line. */
17266 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17267 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17268 {
17269 /* Adjust positions in the glyph matrix. */
17270 if (Z_delta || Z_delta_bytes)
17271 {
17272 struct glyph_row *r1
17273 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17274 increment_matrix_positions (w->current_matrix,
17275 MATRIX_ROW_VPOS (r0, current_matrix),
17276 MATRIX_ROW_VPOS (r1, current_matrix),
17277 Z_delta, Z_delta_bytes);
17278 }
17279
17280 /* Set the cursor. */
17281 row = row_containing_pos (w, PT, r0, NULL, 0);
17282 if (row)
17283 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17284 else
17285 abort ();
17286 return 1;
17287 }
17288 }
17289
17290 /* Handle the case that changes are all below what is displayed in
17291 the window, and that PT is in the window. This shortcut cannot
17292 be taken if ZV is visible in the window, and text has been added
17293 there that is visible in the window. */
17294 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17295 /* ZV is not visible in the window, or there are no
17296 changes at ZV, actually. */
17297 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17298 || first_changed_charpos == last_changed_charpos))
17299 {
17300 struct glyph_row *r0;
17301
17302 /* Give up if PT is not in the window. Note that it already has
17303 been checked at the start of try_window_id that PT is not in
17304 front of the window start. */
17305 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17306 GIVE_UP (14);
17307
17308 /* If window start is unchanged, we can reuse the whole matrix
17309 as is, without changing glyph positions since no text has
17310 been added/removed in front of the window end. */
17311 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17312 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17313 /* PT must not be in a partially visible line. */
17314 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17315 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17316 {
17317 /* We have to compute the window end anew since text
17318 could have been added/removed after it. */
17319 w->window_end_pos
17320 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17321 w->window_end_bytepos
17322 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17323
17324 /* Set the cursor. */
17325 row = row_containing_pos (w, PT, r0, NULL, 0);
17326 if (row)
17327 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17328 else
17329 abort ();
17330 return 2;
17331 }
17332 }
17333
17334 /* Give up if window start is in the changed area.
17335
17336 The condition used to read
17337
17338 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17339
17340 but why that was tested escapes me at the moment. */
17341 if (CHARPOS (start) >= first_changed_charpos
17342 && CHARPOS (start) <= last_changed_charpos)
17343 GIVE_UP (15);
17344
17345 /* Check that window start agrees with the start of the first glyph
17346 row in its current matrix. Check this after we know the window
17347 start is not in changed text, otherwise positions would not be
17348 comparable. */
17349 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17350 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17351 GIVE_UP (16);
17352
17353 /* Give up if the window ends in strings. Overlay strings
17354 at the end are difficult to handle, so don't try. */
17355 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17356 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17357 GIVE_UP (20);
17358
17359 /* Compute the position at which we have to start displaying new
17360 lines. Some of the lines at the top of the window might be
17361 reusable because they are not displaying changed text. Find the
17362 last row in W's current matrix not affected by changes at the
17363 start of current_buffer. Value is null if changes start in the
17364 first line of window. */
17365 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17366 if (last_unchanged_at_beg_row)
17367 {
17368 /* Avoid starting to display in the middle of a character, a TAB
17369 for instance. This is easier than to set up the iterator
17370 exactly, and it's not a frequent case, so the additional
17371 effort wouldn't really pay off. */
17372 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17373 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17374 && last_unchanged_at_beg_row > w->current_matrix->rows)
17375 --last_unchanged_at_beg_row;
17376
17377 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17378 GIVE_UP (17);
17379
17380 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17381 GIVE_UP (18);
17382 start_pos = it.current.pos;
17383
17384 /* Start displaying new lines in the desired matrix at the same
17385 vpos we would use in the current matrix, i.e. below
17386 last_unchanged_at_beg_row. */
17387 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17388 current_matrix);
17389 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17390 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17391
17392 eassert (it.hpos == 0 && it.current_x == 0);
17393 }
17394 else
17395 {
17396 /* There are no reusable lines at the start of the window.
17397 Start displaying in the first text line. */
17398 start_display (&it, w, start);
17399 it.vpos = it.first_vpos;
17400 start_pos = it.current.pos;
17401 }
17402
17403 /* Find the first row that is not affected by changes at the end of
17404 the buffer. Value will be null if there is no unchanged row, in
17405 which case we must redisplay to the end of the window. delta
17406 will be set to the value by which buffer positions beginning with
17407 first_unchanged_at_end_row have to be adjusted due to text
17408 changes. */
17409 first_unchanged_at_end_row
17410 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17411 IF_DEBUG (debug_delta = delta);
17412 IF_DEBUG (debug_delta_bytes = delta_bytes);
17413
17414 /* Set stop_pos to the buffer position up to which we will have to
17415 display new lines. If first_unchanged_at_end_row != NULL, this
17416 is the buffer position of the start of the line displayed in that
17417 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17418 that we don't stop at a buffer position. */
17419 stop_pos = 0;
17420 if (first_unchanged_at_end_row)
17421 {
17422 eassert (last_unchanged_at_beg_row == NULL
17423 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17424
17425 /* If this is a continuation line, move forward to the next one
17426 that isn't. Changes in lines above affect this line.
17427 Caution: this may move first_unchanged_at_end_row to a row
17428 not displaying text. */
17429 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17430 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17431 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17432 < it.last_visible_y))
17433 ++first_unchanged_at_end_row;
17434
17435 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17436 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17437 >= it.last_visible_y))
17438 first_unchanged_at_end_row = NULL;
17439 else
17440 {
17441 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17442 + delta);
17443 first_unchanged_at_end_vpos
17444 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17445 eassert (stop_pos >= Z - END_UNCHANGED);
17446 }
17447 }
17448 else if (last_unchanged_at_beg_row == NULL)
17449 GIVE_UP (19);
17450
17451
17452 #ifdef GLYPH_DEBUG
17453
17454 /* Either there is no unchanged row at the end, or the one we have
17455 now displays text. This is a necessary condition for the window
17456 end pos calculation at the end of this function. */
17457 eassert (first_unchanged_at_end_row == NULL
17458 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17459
17460 debug_last_unchanged_at_beg_vpos
17461 = (last_unchanged_at_beg_row
17462 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17463 : -1);
17464 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17465
17466 #endif /* GLYPH_DEBUG */
17467
17468
17469 /* Display new lines. Set last_text_row to the last new line
17470 displayed which has text on it, i.e. might end up as being the
17471 line where the window_end_vpos is. */
17472 w->cursor.vpos = -1;
17473 last_text_row = NULL;
17474 overlay_arrow_seen = 0;
17475 while (it.current_y < it.last_visible_y
17476 && !fonts_changed_p
17477 && (first_unchanged_at_end_row == NULL
17478 || IT_CHARPOS (it) < stop_pos))
17479 {
17480 if (display_line (&it))
17481 last_text_row = it.glyph_row - 1;
17482 }
17483
17484 if (fonts_changed_p)
17485 return -1;
17486
17487
17488 /* Compute differences in buffer positions, y-positions etc. for
17489 lines reused at the bottom of the window. Compute what we can
17490 scroll. */
17491 if (first_unchanged_at_end_row
17492 /* No lines reused because we displayed everything up to the
17493 bottom of the window. */
17494 && it.current_y < it.last_visible_y)
17495 {
17496 dvpos = (it.vpos
17497 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17498 current_matrix));
17499 dy = it.current_y - first_unchanged_at_end_row->y;
17500 run.current_y = first_unchanged_at_end_row->y;
17501 run.desired_y = run.current_y + dy;
17502 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17503 }
17504 else
17505 {
17506 delta = delta_bytes = dvpos = dy
17507 = run.current_y = run.desired_y = run.height = 0;
17508 first_unchanged_at_end_row = NULL;
17509 }
17510 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17511
17512
17513 /* Find the cursor if not already found. We have to decide whether
17514 PT will appear on this window (it sometimes doesn't, but this is
17515 not a very frequent case.) This decision has to be made before
17516 the current matrix is altered. A value of cursor.vpos < 0 means
17517 that PT is either in one of the lines beginning at
17518 first_unchanged_at_end_row or below the window. Don't care for
17519 lines that might be displayed later at the window end; as
17520 mentioned, this is not a frequent case. */
17521 if (w->cursor.vpos < 0)
17522 {
17523 /* Cursor in unchanged rows at the top? */
17524 if (PT < CHARPOS (start_pos)
17525 && last_unchanged_at_beg_row)
17526 {
17527 row = row_containing_pos (w, PT,
17528 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17529 last_unchanged_at_beg_row + 1, 0);
17530 if (row)
17531 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17532 }
17533
17534 /* Start from first_unchanged_at_end_row looking for PT. */
17535 else if (first_unchanged_at_end_row)
17536 {
17537 row = row_containing_pos (w, PT - delta,
17538 first_unchanged_at_end_row, NULL, 0);
17539 if (row)
17540 set_cursor_from_row (w, row, w->current_matrix, delta,
17541 delta_bytes, dy, dvpos);
17542 }
17543
17544 /* Give up if cursor was not found. */
17545 if (w->cursor.vpos < 0)
17546 {
17547 clear_glyph_matrix (w->desired_matrix);
17548 return -1;
17549 }
17550 }
17551
17552 /* Don't let the cursor end in the scroll margins. */
17553 {
17554 int this_scroll_margin, cursor_height;
17555
17556 this_scroll_margin =
17557 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17558 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17559 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17560
17561 if ((w->cursor.y < this_scroll_margin
17562 && CHARPOS (start) > BEGV)
17563 /* Old redisplay didn't take scroll margin into account at the bottom,
17564 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17565 || (w->cursor.y + (make_cursor_line_fully_visible_p
17566 ? cursor_height + this_scroll_margin
17567 : 1)) > it.last_visible_y)
17568 {
17569 w->cursor.vpos = -1;
17570 clear_glyph_matrix (w->desired_matrix);
17571 return -1;
17572 }
17573 }
17574
17575 /* Scroll the display. Do it before changing the current matrix so
17576 that xterm.c doesn't get confused about where the cursor glyph is
17577 found. */
17578 if (dy && run.height)
17579 {
17580 update_begin (f);
17581
17582 if (FRAME_WINDOW_P (f))
17583 {
17584 FRAME_RIF (f)->update_window_begin_hook (w);
17585 FRAME_RIF (f)->clear_window_mouse_face (w);
17586 FRAME_RIF (f)->scroll_run_hook (w, &run);
17587 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17588 }
17589 else
17590 {
17591 /* Terminal frame. In this case, dvpos gives the number of
17592 lines to scroll by; dvpos < 0 means scroll up. */
17593 int from_vpos
17594 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17595 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17596 int end = (WINDOW_TOP_EDGE_LINE (w)
17597 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17598 + window_internal_height (w));
17599
17600 #if defined (HAVE_GPM) || defined (MSDOS)
17601 x_clear_window_mouse_face (w);
17602 #endif
17603 /* Perform the operation on the screen. */
17604 if (dvpos > 0)
17605 {
17606 /* Scroll last_unchanged_at_beg_row to the end of the
17607 window down dvpos lines. */
17608 set_terminal_window (f, end);
17609
17610 /* On dumb terminals delete dvpos lines at the end
17611 before inserting dvpos empty lines. */
17612 if (!FRAME_SCROLL_REGION_OK (f))
17613 ins_del_lines (f, end - dvpos, -dvpos);
17614
17615 /* Insert dvpos empty lines in front of
17616 last_unchanged_at_beg_row. */
17617 ins_del_lines (f, from, dvpos);
17618 }
17619 else if (dvpos < 0)
17620 {
17621 /* Scroll up last_unchanged_at_beg_vpos to the end of
17622 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17623 set_terminal_window (f, end);
17624
17625 /* Delete dvpos lines in front of
17626 last_unchanged_at_beg_vpos. ins_del_lines will set
17627 the cursor to the given vpos and emit |dvpos| delete
17628 line sequences. */
17629 ins_del_lines (f, from + dvpos, dvpos);
17630
17631 /* On a dumb terminal insert dvpos empty lines at the
17632 end. */
17633 if (!FRAME_SCROLL_REGION_OK (f))
17634 ins_del_lines (f, end + dvpos, -dvpos);
17635 }
17636
17637 set_terminal_window (f, 0);
17638 }
17639
17640 update_end (f);
17641 }
17642
17643 /* Shift reused rows of the current matrix to the right position.
17644 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17645 text. */
17646 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17647 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17648 if (dvpos < 0)
17649 {
17650 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17651 bottom_vpos, dvpos);
17652 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17653 bottom_vpos, 0);
17654 }
17655 else if (dvpos > 0)
17656 {
17657 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17658 bottom_vpos, dvpos);
17659 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17660 first_unchanged_at_end_vpos + dvpos, 0);
17661 }
17662
17663 /* For frame-based redisplay, make sure that current frame and window
17664 matrix are in sync with respect to glyph memory. */
17665 if (!FRAME_WINDOW_P (f))
17666 sync_frame_with_window_matrix_rows (w);
17667
17668 /* Adjust buffer positions in reused rows. */
17669 if (delta || delta_bytes)
17670 increment_matrix_positions (current_matrix,
17671 first_unchanged_at_end_vpos + dvpos,
17672 bottom_vpos, delta, delta_bytes);
17673
17674 /* Adjust Y positions. */
17675 if (dy)
17676 shift_glyph_matrix (w, current_matrix,
17677 first_unchanged_at_end_vpos + dvpos,
17678 bottom_vpos, dy);
17679
17680 if (first_unchanged_at_end_row)
17681 {
17682 first_unchanged_at_end_row += dvpos;
17683 if (first_unchanged_at_end_row->y >= it.last_visible_y
17684 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17685 first_unchanged_at_end_row = NULL;
17686 }
17687
17688 /* If scrolling up, there may be some lines to display at the end of
17689 the window. */
17690 last_text_row_at_end = NULL;
17691 if (dy < 0)
17692 {
17693 /* Scrolling up can leave for example a partially visible line
17694 at the end of the window to be redisplayed. */
17695 /* Set last_row to the glyph row in the current matrix where the
17696 window end line is found. It has been moved up or down in
17697 the matrix by dvpos. */
17698 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17699 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17700
17701 /* If last_row is the window end line, it should display text. */
17702 eassert (last_row->displays_text_p);
17703
17704 /* If window end line was partially visible before, begin
17705 displaying at that line. Otherwise begin displaying with the
17706 line following it. */
17707 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17708 {
17709 init_to_row_start (&it, w, last_row);
17710 it.vpos = last_vpos;
17711 it.current_y = last_row->y;
17712 }
17713 else
17714 {
17715 init_to_row_end (&it, w, last_row);
17716 it.vpos = 1 + last_vpos;
17717 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17718 ++last_row;
17719 }
17720
17721 /* We may start in a continuation line. If so, we have to
17722 get the right continuation_lines_width and current_x. */
17723 it.continuation_lines_width = last_row->continuation_lines_width;
17724 it.hpos = it.current_x = 0;
17725
17726 /* Display the rest of the lines at the window end. */
17727 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17728 while (it.current_y < it.last_visible_y
17729 && !fonts_changed_p)
17730 {
17731 /* Is it always sure that the display agrees with lines in
17732 the current matrix? I don't think so, so we mark rows
17733 displayed invalid in the current matrix by setting their
17734 enabled_p flag to zero. */
17735 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17736 if (display_line (&it))
17737 last_text_row_at_end = it.glyph_row - 1;
17738 }
17739 }
17740
17741 /* Update window_end_pos and window_end_vpos. */
17742 if (first_unchanged_at_end_row
17743 && !last_text_row_at_end)
17744 {
17745 /* Window end line if one of the preserved rows from the current
17746 matrix. Set row to the last row displaying text in current
17747 matrix starting at first_unchanged_at_end_row, after
17748 scrolling. */
17749 eassert (first_unchanged_at_end_row->displays_text_p);
17750 row = find_last_row_displaying_text (w->current_matrix, &it,
17751 first_unchanged_at_end_row);
17752 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17753
17754 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17755 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17756 w->window_end_vpos
17757 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17758 eassert (w->window_end_bytepos >= 0);
17759 IF_DEBUG (debug_method_add (w, "A"));
17760 }
17761 else if (last_text_row_at_end)
17762 {
17763 w->window_end_pos
17764 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17765 w->window_end_bytepos
17766 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17767 w->window_end_vpos
17768 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17769 eassert (w->window_end_bytepos >= 0);
17770 IF_DEBUG (debug_method_add (w, "B"));
17771 }
17772 else if (last_text_row)
17773 {
17774 /* We have displayed either to the end of the window or at the
17775 end of the window, i.e. the last row with text is to be found
17776 in the desired matrix. */
17777 w->window_end_pos
17778 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17779 w->window_end_bytepos
17780 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17781 w->window_end_vpos
17782 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17783 eassert (w->window_end_bytepos >= 0);
17784 }
17785 else if (first_unchanged_at_end_row == NULL
17786 && last_text_row == NULL
17787 && last_text_row_at_end == NULL)
17788 {
17789 /* Displayed to end of window, but no line containing text was
17790 displayed. Lines were deleted at the end of the window. */
17791 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17792 int vpos = XFASTINT (w->window_end_vpos);
17793 struct glyph_row *current_row = current_matrix->rows + vpos;
17794 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17795
17796 for (row = NULL;
17797 row == NULL && vpos >= first_vpos;
17798 --vpos, --current_row, --desired_row)
17799 {
17800 if (desired_row->enabled_p)
17801 {
17802 if (desired_row->displays_text_p)
17803 row = desired_row;
17804 }
17805 else if (current_row->displays_text_p)
17806 row = current_row;
17807 }
17808
17809 eassert (row != NULL);
17810 w->window_end_vpos = make_number (vpos + 1);
17811 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17812 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17813 eassert (w->window_end_bytepos >= 0);
17814 IF_DEBUG (debug_method_add (w, "C"));
17815 }
17816 else
17817 abort ();
17818
17819 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17820 debug_end_vpos = XFASTINT (w->window_end_vpos));
17821
17822 /* Record that display has not been completed. */
17823 w->window_end_valid = Qnil;
17824 w->desired_matrix->no_scrolling_p = 1;
17825 return 3;
17826
17827 #undef GIVE_UP
17828 }
17829
17830
17831 \f
17832 /***********************************************************************
17833 More debugging support
17834 ***********************************************************************/
17835
17836 #ifdef GLYPH_DEBUG
17837
17838 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17839 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17840 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17841
17842
17843 /* Dump the contents of glyph matrix MATRIX on stderr.
17844
17845 GLYPHS 0 means don't show glyph contents.
17846 GLYPHS 1 means show glyphs in short form
17847 GLYPHS > 1 means show glyphs in long form. */
17848
17849 void
17850 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17851 {
17852 int i;
17853 for (i = 0; i < matrix->nrows; ++i)
17854 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17855 }
17856
17857
17858 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17859 the glyph row and area where the glyph comes from. */
17860
17861 void
17862 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17863 {
17864 if (glyph->type == CHAR_GLYPH)
17865 {
17866 fprintf (stderr,
17867 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17868 glyph - row->glyphs[TEXT_AREA],
17869 'C',
17870 glyph->charpos,
17871 (BUFFERP (glyph->object)
17872 ? 'B'
17873 : (STRINGP (glyph->object)
17874 ? 'S'
17875 : '-')),
17876 glyph->pixel_width,
17877 glyph->u.ch,
17878 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17879 ? glyph->u.ch
17880 : '.'),
17881 glyph->face_id,
17882 glyph->left_box_line_p,
17883 glyph->right_box_line_p);
17884 }
17885 else if (glyph->type == STRETCH_GLYPH)
17886 {
17887 fprintf (stderr,
17888 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17889 glyph - row->glyphs[TEXT_AREA],
17890 'S',
17891 glyph->charpos,
17892 (BUFFERP (glyph->object)
17893 ? 'B'
17894 : (STRINGP (glyph->object)
17895 ? 'S'
17896 : '-')),
17897 glyph->pixel_width,
17898 0,
17899 '.',
17900 glyph->face_id,
17901 glyph->left_box_line_p,
17902 glyph->right_box_line_p);
17903 }
17904 else if (glyph->type == IMAGE_GLYPH)
17905 {
17906 fprintf (stderr,
17907 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17908 glyph - row->glyphs[TEXT_AREA],
17909 'I',
17910 glyph->charpos,
17911 (BUFFERP (glyph->object)
17912 ? 'B'
17913 : (STRINGP (glyph->object)
17914 ? 'S'
17915 : '-')),
17916 glyph->pixel_width,
17917 glyph->u.img_id,
17918 '.',
17919 glyph->face_id,
17920 glyph->left_box_line_p,
17921 glyph->right_box_line_p);
17922 }
17923 else if (glyph->type == COMPOSITE_GLYPH)
17924 {
17925 fprintf (stderr,
17926 " %5td %4c %6"pI"d %c %3d 0x%05x",
17927 glyph - row->glyphs[TEXT_AREA],
17928 '+',
17929 glyph->charpos,
17930 (BUFFERP (glyph->object)
17931 ? 'B'
17932 : (STRINGP (glyph->object)
17933 ? 'S'
17934 : '-')),
17935 glyph->pixel_width,
17936 glyph->u.cmp.id);
17937 if (glyph->u.cmp.automatic)
17938 fprintf (stderr,
17939 "[%d-%d]",
17940 glyph->slice.cmp.from, glyph->slice.cmp.to);
17941 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17942 glyph->face_id,
17943 glyph->left_box_line_p,
17944 glyph->right_box_line_p);
17945 }
17946 }
17947
17948
17949 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17950 GLYPHS 0 means don't show glyph contents.
17951 GLYPHS 1 means show glyphs in short form
17952 GLYPHS > 1 means show glyphs in long form. */
17953
17954 void
17955 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17956 {
17957 if (glyphs != 1)
17958 {
17959 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17960 fprintf (stderr, "======================================================================\n");
17961
17962 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17963 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17964 vpos,
17965 MATRIX_ROW_START_CHARPOS (row),
17966 MATRIX_ROW_END_CHARPOS (row),
17967 row->used[TEXT_AREA],
17968 row->contains_overlapping_glyphs_p,
17969 row->enabled_p,
17970 row->truncated_on_left_p,
17971 row->truncated_on_right_p,
17972 row->continued_p,
17973 MATRIX_ROW_CONTINUATION_LINE_P (row),
17974 row->displays_text_p,
17975 row->ends_at_zv_p,
17976 row->fill_line_p,
17977 row->ends_in_middle_of_char_p,
17978 row->starts_in_middle_of_char_p,
17979 row->mouse_face_p,
17980 row->x,
17981 row->y,
17982 row->pixel_width,
17983 row->height,
17984 row->visible_height,
17985 row->ascent,
17986 row->phys_ascent);
17987 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17988 row->end.overlay_string_index,
17989 row->continuation_lines_width);
17990 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17991 CHARPOS (row->start.string_pos),
17992 CHARPOS (row->end.string_pos));
17993 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17994 row->end.dpvec_index);
17995 }
17996
17997 if (glyphs > 1)
17998 {
17999 int area;
18000
18001 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18002 {
18003 struct glyph *glyph = row->glyphs[area];
18004 struct glyph *glyph_end = glyph + row->used[area];
18005
18006 /* Glyph for a line end in text. */
18007 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18008 ++glyph_end;
18009
18010 if (glyph < glyph_end)
18011 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18012
18013 for (; glyph < glyph_end; ++glyph)
18014 dump_glyph (row, glyph, area);
18015 }
18016 }
18017 else if (glyphs == 1)
18018 {
18019 int area;
18020
18021 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18022 {
18023 char *s = alloca (row->used[area] + 1);
18024 int i;
18025
18026 for (i = 0; i < row->used[area]; ++i)
18027 {
18028 struct glyph *glyph = row->glyphs[area] + i;
18029 if (glyph->type == CHAR_GLYPH
18030 && glyph->u.ch < 0x80
18031 && glyph->u.ch >= ' ')
18032 s[i] = glyph->u.ch;
18033 else
18034 s[i] = '.';
18035 }
18036
18037 s[i] = '\0';
18038 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18039 }
18040 }
18041 }
18042
18043
18044 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18045 Sdump_glyph_matrix, 0, 1, "p",
18046 doc: /* Dump the current matrix of the selected window to stderr.
18047 Shows contents of glyph row structures. With non-nil
18048 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18049 glyphs in short form, otherwise show glyphs in long form. */)
18050 (Lisp_Object glyphs)
18051 {
18052 struct window *w = XWINDOW (selected_window);
18053 struct buffer *buffer = XBUFFER (w->buffer);
18054
18055 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18056 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18057 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18058 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18059 fprintf (stderr, "=============================================\n");
18060 dump_glyph_matrix (w->current_matrix,
18061 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18062 return Qnil;
18063 }
18064
18065
18066 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18067 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18068 (void)
18069 {
18070 struct frame *f = XFRAME (selected_frame);
18071 dump_glyph_matrix (f->current_matrix, 1);
18072 return Qnil;
18073 }
18074
18075
18076 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18077 doc: /* Dump glyph row ROW to stderr.
18078 GLYPH 0 means don't dump glyphs.
18079 GLYPH 1 means dump glyphs in short form.
18080 GLYPH > 1 or omitted means dump glyphs in long form. */)
18081 (Lisp_Object row, Lisp_Object glyphs)
18082 {
18083 struct glyph_matrix *matrix;
18084 EMACS_INT vpos;
18085
18086 CHECK_NUMBER (row);
18087 matrix = XWINDOW (selected_window)->current_matrix;
18088 vpos = XINT (row);
18089 if (vpos >= 0 && vpos < matrix->nrows)
18090 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18091 vpos,
18092 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18093 return Qnil;
18094 }
18095
18096
18097 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18098 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18099 GLYPH 0 means don't dump glyphs.
18100 GLYPH 1 means dump glyphs in short form.
18101 GLYPH > 1 or omitted means dump glyphs in long form. */)
18102 (Lisp_Object row, Lisp_Object glyphs)
18103 {
18104 struct frame *sf = SELECTED_FRAME ();
18105 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18106 EMACS_INT vpos;
18107
18108 CHECK_NUMBER (row);
18109 vpos = XINT (row);
18110 if (vpos >= 0 && vpos < m->nrows)
18111 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18112 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18113 return Qnil;
18114 }
18115
18116
18117 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18118 doc: /* Toggle tracing of redisplay.
18119 With ARG, turn tracing on if and only if ARG is positive. */)
18120 (Lisp_Object arg)
18121 {
18122 if (NILP (arg))
18123 trace_redisplay_p = !trace_redisplay_p;
18124 else
18125 {
18126 arg = Fprefix_numeric_value (arg);
18127 trace_redisplay_p = XINT (arg) > 0;
18128 }
18129
18130 return Qnil;
18131 }
18132
18133
18134 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18135 doc: /* Like `format', but print result to stderr.
18136 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18137 (ptrdiff_t nargs, Lisp_Object *args)
18138 {
18139 Lisp_Object s = Fformat (nargs, args);
18140 fprintf (stderr, "%s", SDATA (s));
18141 return Qnil;
18142 }
18143
18144 #endif /* GLYPH_DEBUG */
18145
18146
18147 \f
18148 /***********************************************************************
18149 Building Desired Matrix Rows
18150 ***********************************************************************/
18151
18152 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18153 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18154
18155 static struct glyph_row *
18156 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18157 {
18158 struct frame *f = XFRAME (WINDOW_FRAME (w));
18159 struct buffer *buffer = XBUFFER (w->buffer);
18160 struct buffer *old = current_buffer;
18161 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18162 int arrow_len = SCHARS (overlay_arrow_string);
18163 const unsigned char *arrow_end = arrow_string + arrow_len;
18164 const unsigned char *p;
18165 struct it it;
18166 int multibyte_p;
18167 int n_glyphs_before;
18168
18169 set_buffer_temp (buffer);
18170 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18171 it.glyph_row->used[TEXT_AREA] = 0;
18172 SET_TEXT_POS (it.position, 0, 0);
18173
18174 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18175 p = arrow_string;
18176 while (p < arrow_end)
18177 {
18178 Lisp_Object face, ilisp;
18179
18180 /* Get the next character. */
18181 if (multibyte_p)
18182 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18183 else
18184 {
18185 it.c = it.char_to_display = *p, it.len = 1;
18186 if (! ASCII_CHAR_P (it.c))
18187 it.char_to_display = BYTE8_TO_CHAR (it.c);
18188 }
18189 p += it.len;
18190
18191 /* Get its face. */
18192 ilisp = make_number (p - arrow_string);
18193 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18194 it.face_id = compute_char_face (f, it.char_to_display, face);
18195
18196 /* Compute its width, get its glyphs. */
18197 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18198 SET_TEXT_POS (it.position, -1, -1);
18199 PRODUCE_GLYPHS (&it);
18200
18201 /* If this character doesn't fit any more in the line, we have
18202 to remove some glyphs. */
18203 if (it.current_x > it.last_visible_x)
18204 {
18205 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18206 break;
18207 }
18208 }
18209
18210 set_buffer_temp (old);
18211 return it.glyph_row;
18212 }
18213
18214
18215 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18216 glyphs to insert is determined by produce_special_glyphs. */
18217
18218 static void
18219 insert_left_trunc_glyphs (struct it *it)
18220 {
18221 struct it truncate_it;
18222 struct glyph *from, *end, *to, *toend;
18223
18224 eassert (!FRAME_WINDOW_P (it->f)
18225 || (!it->glyph_row->reversed_p
18226 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18227 || (it->glyph_row->reversed_p
18228 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18229
18230 /* Get the truncation glyphs. */
18231 truncate_it = *it;
18232 truncate_it.current_x = 0;
18233 truncate_it.face_id = DEFAULT_FACE_ID;
18234 truncate_it.glyph_row = &scratch_glyph_row;
18235 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18236 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18237 truncate_it.object = make_number (0);
18238 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18239
18240 /* Overwrite glyphs from IT with truncation glyphs. */
18241 if (!it->glyph_row->reversed_p)
18242 {
18243 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18244
18245 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18246 end = from + tused;
18247 to = it->glyph_row->glyphs[TEXT_AREA];
18248 toend = to + it->glyph_row->used[TEXT_AREA];
18249 if (FRAME_WINDOW_P (it->f))
18250 {
18251 /* On GUI frames, when variable-size fonts are displayed,
18252 the truncation glyphs may need more pixels than the row's
18253 glyphs they overwrite. We overwrite more glyphs to free
18254 enough screen real estate, and enlarge the stretch glyph
18255 on the right (see display_line), if there is one, to
18256 preserve the screen position of the truncation glyphs on
18257 the right. */
18258 int w = 0;
18259 struct glyph *g = to;
18260 short used;
18261
18262 while (g < toend && it->glyph_row->x + w < 0)
18263 {
18264 w += g->pixel_width;
18265 ++g;
18266 }
18267 it->glyph_row->x = 0;
18268 w = 0;
18269 while (g < toend && w < it->truncation_pixel_width)
18270 {
18271 w += g->pixel_width;
18272 ++g;
18273 }
18274 if (g - to - tused > 0)
18275 {
18276 memmove (to + tused, g, toend - g);
18277 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18278 }
18279 used = it->glyph_row->used[TEXT_AREA];
18280 if (it->glyph_row->truncated_on_right_p
18281 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18282 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18283 == STRETCH_GLYPH)
18284 {
18285 int extra = w - it->truncation_pixel_width;
18286
18287 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18288 }
18289 }
18290
18291 while (from < end)
18292 *to++ = *from++;
18293
18294 /* There may be padding glyphs left over. Overwrite them too. */
18295 if (!FRAME_WINDOW_P (it->f))
18296 {
18297 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18298 {
18299 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18300 while (from < end)
18301 *to++ = *from++;
18302 }
18303 }
18304
18305 if (to > toend)
18306 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18307 }
18308 else
18309 {
18310 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18311
18312 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18313 that back to front. */
18314 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18315 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18316 toend = it->glyph_row->glyphs[TEXT_AREA];
18317 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18318 if (FRAME_WINDOW_P (it->f))
18319 {
18320 int w = 0;
18321 struct glyph *g = to;
18322
18323 while (g >= toend && w < it->truncation_pixel_width)
18324 {
18325 w += g->pixel_width;
18326 --g;
18327 }
18328 if (to - g - tused > 0)
18329 to = g + tused;
18330 if (it->glyph_row->truncated_on_right_p
18331 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18332 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18333 {
18334 int extra = w - it->truncation_pixel_width;
18335
18336 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18337 }
18338 }
18339
18340 while (from >= end && to >= toend)
18341 *to-- = *from--;
18342 if (!FRAME_WINDOW_P (it->f))
18343 {
18344 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18345 {
18346 from =
18347 truncate_it.glyph_row->glyphs[TEXT_AREA]
18348 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18349 while (from >= end && to >= toend)
18350 *to-- = *from--;
18351 }
18352 }
18353 if (from >= end)
18354 {
18355 /* Need to free some room before prepending additional
18356 glyphs. */
18357 int move_by = from - end + 1;
18358 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18359 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18360
18361 for ( ; g >= g0; g--)
18362 g[move_by] = *g;
18363 while (from >= end)
18364 *to-- = *from--;
18365 it->glyph_row->used[TEXT_AREA] += move_by;
18366 }
18367 }
18368 }
18369
18370 /* Compute the hash code for ROW. */
18371 unsigned
18372 row_hash (struct glyph_row *row)
18373 {
18374 int area, k;
18375 unsigned hashval = 0;
18376
18377 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18378 for (k = 0; k < row->used[area]; ++k)
18379 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18380 + row->glyphs[area][k].u.val
18381 + row->glyphs[area][k].face_id
18382 + row->glyphs[area][k].padding_p
18383 + (row->glyphs[area][k].type << 2));
18384
18385 return hashval;
18386 }
18387
18388 /* Compute the pixel height and width of IT->glyph_row.
18389
18390 Most of the time, ascent and height of a display line will be equal
18391 to the max_ascent and max_height values of the display iterator
18392 structure. This is not the case if
18393
18394 1. We hit ZV without displaying anything. In this case, max_ascent
18395 and max_height will be zero.
18396
18397 2. We have some glyphs that don't contribute to the line height.
18398 (The glyph row flag contributes_to_line_height_p is for future
18399 pixmap extensions).
18400
18401 The first case is easily covered by using default values because in
18402 these cases, the line height does not really matter, except that it
18403 must not be zero. */
18404
18405 static void
18406 compute_line_metrics (struct it *it)
18407 {
18408 struct glyph_row *row = it->glyph_row;
18409
18410 if (FRAME_WINDOW_P (it->f))
18411 {
18412 int i, min_y, max_y;
18413
18414 /* The line may consist of one space only, that was added to
18415 place the cursor on it. If so, the row's height hasn't been
18416 computed yet. */
18417 if (row->height == 0)
18418 {
18419 if (it->max_ascent + it->max_descent == 0)
18420 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18421 row->ascent = it->max_ascent;
18422 row->height = it->max_ascent + it->max_descent;
18423 row->phys_ascent = it->max_phys_ascent;
18424 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18425 row->extra_line_spacing = it->max_extra_line_spacing;
18426 }
18427
18428 /* Compute the width of this line. */
18429 row->pixel_width = row->x;
18430 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18431 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18432
18433 eassert (row->pixel_width >= 0);
18434 eassert (row->ascent >= 0 && row->height > 0);
18435
18436 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18437 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18438
18439 /* If first line's physical ascent is larger than its logical
18440 ascent, use the physical ascent, and make the row taller.
18441 This makes accented characters fully visible. */
18442 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18443 && row->phys_ascent > row->ascent)
18444 {
18445 row->height += row->phys_ascent - row->ascent;
18446 row->ascent = row->phys_ascent;
18447 }
18448
18449 /* Compute how much of the line is visible. */
18450 row->visible_height = row->height;
18451
18452 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18453 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18454
18455 if (row->y < min_y)
18456 row->visible_height -= min_y - row->y;
18457 if (row->y + row->height > max_y)
18458 row->visible_height -= row->y + row->height - max_y;
18459 }
18460 else
18461 {
18462 row->pixel_width = row->used[TEXT_AREA];
18463 if (row->continued_p)
18464 row->pixel_width -= it->continuation_pixel_width;
18465 else if (row->truncated_on_right_p)
18466 row->pixel_width -= it->truncation_pixel_width;
18467 row->ascent = row->phys_ascent = 0;
18468 row->height = row->phys_height = row->visible_height = 1;
18469 row->extra_line_spacing = 0;
18470 }
18471
18472 /* Compute a hash code for this row. */
18473 row->hash = row_hash (row);
18474
18475 it->max_ascent = it->max_descent = 0;
18476 it->max_phys_ascent = it->max_phys_descent = 0;
18477 }
18478
18479
18480 /* Append one space to the glyph row of iterator IT if doing a
18481 window-based redisplay. The space has the same face as
18482 IT->face_id. Value is non-zero if a space was added.
18483
18484 This function is called to make sure that there is always one glyph
18485 at the end of a glyph row that the cursor can be set on under
18486 window-systems. (If there weren't such a glyph we would not know
18487 how wide and tall a box cursor should be displayed).
18488
18489 At the same time this space let's a nicely handle clearing to the
18490 end of the line if the row ends in italic text. */
18491
18492 static int
18493 append_space_for_newline (struct it *it, int default_face_p)
18494 {
18495 if (FRAME_WINDOW_P (it->f))
18496 {
18497 int n = it->glyph_row->used[TEXT_AREA];
18498
18499 if (it->glyph_row->glyphs[TEXT_AREA] + n
18500 < it->glyph_row->glyphs[1 + TEXT_AREA])
18501 {
18502 /* Save some values that must not be changed.
18503 Must save IT->c and IT->len because otherwise
18504 ITERATOR_AT_END_P wouldn't work anymore after
18505 append_space_for_newline has been called. */
18506 enum display_element_type saved_what = it->what;
18507 int saved_c = it->c, saved_len = it->len;
18508 int saved_char_to_display = it->char_to_display;
18509 int saved_x = it->current_x;
18510 int saved_face_id = it->face_id;
18511 struct text_pos saved_pos;
18512 Lisp_Object saved_object;
18513 struct face *face;
18514
18515 saved_object = it->object;
18516 saved_pos = it->position;
18517
18518 it->what = IT_CHARACTER;
18519 memset (&it->position, 0, sizeof it->position);
18520 it->object = make_number (0);
18521 it->c = it->char_to_display = ' ';
18522 it->len = 1;
18523
18524 /* If the default face was remapped, be sure to use the
18525 remapped face for the appended newline. */
18526 if (default_face_p)
18527 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18528 else if (it->face_before_selective_p)
18529 it->face_id = it->saved_face_id;
18530 face = FACE_FROM_ID (it->f, it->face_id);
18531 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18532
18533 PRODUCE_GLYPHS (it);
18534
18535 it->override_ascent = -1;
18536 it->constrain_row_ascent_descent_p = 0;
18537 it->current_x = saved_x;
18538 it->object = saved_object;
18539 it->position = saved_pos;
18540 it->what = saved_what;
18541 it->face_id = saved_face_id;
18542 it->len = saved_len;
18543 it->c = saved_c;
18544 it->char_to_display = saved_char_to_display;
18545 return 1;
18546 }
18547 }
18548
18549 return 0;
18550 }
18551
18552
18553 /* Extend the face of the last glyph in the text area of IT->glyph_row
18554 to the end of the display line. Called from display_line. If the
18555 glyph row is empty, add a space glyph to it so that we know the
18556 face to draw. Set the glyph row flag fill_line_p. If the glyph
18557 row is R2L, prepend a stretch glyph to cover the empty space to the
18558 left of the leftmost glyph. */
18559
18560 static void
18561 extend_face_to_end_of_line (struct it *it)
18562 {
18563 struct face *face, *default_face;
18564 struct frame *f = it->f;
18565
18566 /* If line is already filled, do nothing. Non window-system frames
18567 get a grace of one more ``pixel'' because their characters are
18568 1-``pixel'' wide, so they hit the equality too early. This grace
18569 is needed only for R2L rows that are not continued, to produce
18570 one extra blank where we could display the cursor. */
18571 if (it->current_x >= it->last_visible_x
18572 + (!FRAME_WINDOW_P (f)
18573 && it->glyph_row->reversed_p
18574 && !it->glyph_row->continued_p))
18575 return;
18576
18577 /* The default face, possibly remapped. */
18578 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18579
18580 /* Face extension extends the background and box of IT->face_id
18581 to the end of the line. If the background equals the background
18582 of the frame, we don't have to do anything. */
18583 if (it->face_before_selective_p)
18584 face = FACE_FROM_ID (f, it->saved_face_id);
18585 else
18586 face = FACE_FROM_ID (f, it->face_id);
18587
18588 if (FRAME_WINDOW_P (f)
18589 && it->glyph_row->displays_text_p
18590 && face->box == FACE_NO_BOX
18591 && face->background == FRAME_BACKGROUND_PIXEL (f)
18592 && !face->stipple
18593 && !it->glyph_row->reversed_p)
18594 return;
18595
18596 /* Set the glyph row flag indicating that the face of the last glyph
18597 in the text area has to be drawn to the end of the text area. */
18598 it->glyph_row->fill_line_p = 1;
18599
18600 /* If current character of IT is not ASCII, make sure we have the
18601 ASCII face. This will be automatically undone the next time
18602 get_next_display_element returns a multibyte character. Note
18603 that the character will always be single byte in unibyte
18604 text. */
18605 if (!ASCII_CHAR_P (it->c))
18606 {
18607 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18608 }
18609
18610 if (FRAME_WINDOW_P (f))
18611 {
18612 /* If the row is empty, add a space with the current face of IT,
18613 so that we know which face to draw. */
18614 if (it->glyph_row->used[TEXT_AREA] == 0)
18615 {
18616 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18617 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18618 it->glyph_row->used[TEXT_AREA] = 1;
18619 }
18620 #ifdef HAVE_WINDOW_SYSTEM
18621 if (it->glyph_row->reversed_p)
18622 {
18623 /* Prepend a stretch glyph to the row, such that the
18624 rightmost glyph will be drawn flushed all the way to the
18625 right margin of the window. The stretch glyph that will
18626 occupy the empty space, if any, to the left of the
18627 glyphs. */
18628 struct font *font = face->font ? face->font : FRAME_FONT (f);
18629 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18630 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18631 struct glyph *g;
18632 int row_width, stretch_ascent, stretch_width;
18633 struct text_pos saved_pos;
18634 int saved_face_id, saved_avoid_cursor;
18635
18636 for (row_width = 0, g = row_start; g < row_end; g++)
18637 row_width += g->pixel_width;
18638 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18639 if (stretch_width > 0)
18640 {
18641 stretch_ascent =
18642 (((it->ascent + it->descent)
18643 * FONT_BASE (font)) / FONT_HEIGHT (font));
18644 saved_pos = it->position;
18645 memset (&it->position, 0, sizeof it->position);
18646 saved_avoid_cursor = it->avoid_cursor_p;
18647 it->avoid_cursor_p = 1;
18648 saved_face_id = it->face_id;
18649 /* The last row's stretch glyph should get the default
18650 face, to avoid painting the rest of the window with
18651 the region face, if the region ends at ZV. */
18652 if (it->glyph_row->ends_at_zv_p)
18653 it->face_id = default_face->id;
18654 else
18655 it->face_id = face->id;
18656 append_stretch_glyph (it, make_number (0), stretch_width,
18657 it->ascent + it->descent, stretch_ascent);
18658 it->position = saved_pos;
18659 it->avoid_cursor_p = saved_avoid_cursor;
18660 it->face_id = saved_face_id;
18661 }
18662 }
18663 #endif /* HAVE_WINDOW_SYSTEM */
18664 }
18665 else
18666 {
18667 /* Save some values that must not be changed. */
18668 int saved_x = it->current_x;
18669 struct text_pos saved_pos;
18670 Lisp_Object saved_object;
18671 enum display_element_type saved_what = it->what;
18672 int saved_face_id = it->face_id;
18673
18674 saved_object = it->object;
18675 saved_pos = it->position;
18676
18677 it->what = IT_CHARACTER;
18678 memset (&it->position, 0, sizeof it->position);
18679 it->object = make_number (0);
18680 it->c = it->char_to_display = ' ';
18681 it->len = 1;
18682 /* The last row's blank glyphs should get the default face, to
18683 avoid painting the rest of the window with the region face,
18684 if the region ends at ZV. */
18685 if (it->glyph_row->ends_at_zv_p)
18686 it->face_id = default_face->id;
18687 else
18688 it->face_id = face->id;
18689
18690 PRODUCE_GLYPHS (it);
18691
18692 while (it->current_x <= it->last_visible_x)
18693 PRODUCE_GLYPHS (it);
18694
18695 /* Don't count these blanks really. It would let us insert a left
18696 truncation glyph below and make us set the cursor on them, maybe. */
18697 it->current_x = saved_x;
18698 it->object = saved_object;
18699 it->position = saved_pos;
18700 it->what = saved_what;
18701 it->face_id = saved_face_id;
18702 }
18703 }
18704
18705
18706 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18707 trailing whitespace. */
18708
18709 static int
18710 trailing_whitespace_p (ptrdiff_t charpos)
18711 {
18712 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18713 int c = 0;
18714
18715 while (bytepos < ZV_BYTE
18716 && (c = FETCH_CHAR (bytepos),
18717 c == ' ' || c == '\t'))
18718 ++bytepos;
18719
18720 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18721 {
18722 if (bytepos != PT_BYTE)
18723 return 1;
18724 }
18725 return 0;
18726 }
18727
18728
18729 /* Highlight trailing whitespace, if any, in ROW. */
18730
18731 static void
18732 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18733 {
18734 int used = row->used[TEXT_AREA];
18735
18736 if (used)
18737 {
18738 struct glyph *start = row->glyphs[TEXT_AREA];
18739 struct glyph *glyph = start + used - 1;
18740
18741 if (row->reversed_p)
18742 {
18743 /* Right-to-left rows need to be processed in the opposite
18744 direction, so swap the edge pointers. */
18745 glyph = start;
18746 start = row->glyphs[TEXT_AREA] + used - 1;
18747 }
18748
18749 /* Skip over glyphs inserted to display the cursor at the
18750 end of a line, for extending the face of the last glyph
18751 to the end of the line on terminals, and for truncation
18752 and continuation glyphs. */
18753 if (!row->reversed_p)
18754 {
18755 while (glyph >= start
18756 && glyph->type == CHAR_GLYPH
18757 && INTEGERP (glyph->object))
18758 --glyph;
18759 }
18760 else
18761 {
18762 while (glyph <= start
18763 && glyph->type == CHAR_GLYPH
18764 && INTEGERP (glyph->object))
18765 ++glyph;
18766 }
18767
18768 /* If last glyph is a space or stretch, and it's trailing
18769 whitespace, set the face of all trailing whitespace glyphs in
18770 IT->glyph_row to `trailing-whitespace'. */
18771 if ((row->reversed_p ? glyph <= start : glyph >= start)
18772 && BUFFERP (glyph->object)
18773 && (glyph->type == STRETCH_GLYPH
18774 || (glyph->type == CHAR_GLYPH
18775 && glyph->u.ch == ' '))
18776 && trailing_whitespace_p (glyph->charpos))
18777 {
18778 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18779 if (face_id < 0)
18780 return;
18781
18782 if (!row->reversed_p)
18783 {
18784 while (glyph >= start
18785 && BUFFERP (glyph->object)
18786 && (glyph->type == STRETCH_GLYPH
18787 || (glyph->type == CHAR_GLYPH
18788 && glyph->u.ch == ' ')))
18789 (glyph--)->face_id = face_id;
18790 }
18791 else
18792 {
18793 while (glyph <= start
18794 && BUFFERP (glyph->object)
18795 && (glyph->type == STRETCH_GLYPH
18796 || (glyph->type == CHAR_GLYPH
18797 && glyph->u.ch == ' ')))
18798 (glyph++)->face_id = face_id;
18799 }
18800 }
18801 }
18802 }
18803
18804
18805 /* Value is non-zero if glyph row ROW should be
18806 used to hold the cursor. */
18807
18808 static int
18809 cursor_row_p (struct glyph_row *row)
18810 {
18811 int result = 1;
18812
18813 if (PT == CHARPOS (row->end.pos)
18814 || PT == MATRIX_ROW_END_CHARPOS (row))
18815 {
18816 /* Suppose the row ends on a string.
18817 Unless the row is continued, that means it ends on a newline
18818 in the string. If it's anything other than a display string
18819 (e.g., a before-string from an overlay), we don't want the
18820 cursor there. (This heuristic seems to give the optimal
18821 behavior for the various types of multi-line strings.)
18822 One exception: if the string has `cursor' property on one of
18823 its characters, we _do_ want the cursor there. */
18824 if (CHARPOS (row->end.string_pos) >= 0)
18825 {
18826 if (row->continued_p)
18827 result = 1;
18828 else
18829 {
18830 /* Check for `display' property. */
18831 struct glyph *beg = row->glyphs[TEXT_AREA];
18832 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18833 struct glyph *glyph;
18834
18835 result = 0;
18836 for (glyph = end; glyph >= beg; --glyph)
18837 if (STRINGP (glyph->object))
18838 {
18839 Lisp_Object prop
18840 = Fget_char_property (make_number (PT),
18841 Qdisplay, Qnil);
18842 result =
18843 (!NILP (prop)
18844 && display_prop_string_p (prop, glyph->object));
18845 /* If there's a `cursor' property on one of the
18846 string's characters, this row is a cursor row,
18847 even though this is not a display string. */
18848 if (!result)
18849 {
18850 Lisp_Object s = glyph->object;
18851
18852 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18853 {
18854 ptrdiff_t gpos = glyph->charpos;
18855
18856 if (!NILP (Fget_char_property (make_number (gpos),
18857 Qcursor, s)))
18858 {
18859 result = 1;
18860 break;
18861 }
18862 }
18863 }
18864 break;
18865 }
18866 }
18867 }
18868 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18869 {
18870 /* If the row ends in middle of a real character,
18871 and the line is continued, we want the cursor here.
18872 That's because CHARPOS (ROW->end.pos) would equal
18873 PT if PT is before the character. */
18874 if (!row->ends_in_ellipsis_p)
18875 result = row->continued_p;
18876 else
18877 /* If the row ends in an ellipsis, then
18878 CHARPOS (ROW->end.pos) will equal point after the
18879 invisible text. We want that position to be displayed
18880 after the ellipsis. */
18881 result = 0;
18882 }
18883 /* If the row ends at ZV, display the cursor at the end of that
18884 row instead of at the start of the row below. */
18885 else if (row->ends_at_zv_p)
18886 result = 1;
18887 else
18888 result = 0;
18889 }
18890
18891 return result;
18892 }
18893
18894 \f
18895
18896 /* Push the property PROP so that it will be rendered at the current
18897 position in IT. Return 1 if PROP was successfully pushed, 0
18898 otherwise. Called from handle_line_prefix to handle the
18899 `line-prefix' and `wrap-prefix' properties. */
18900
18901 static int
18902 push_prefix_prop (struct it *it, Lisp_Object prop)
18903 {
18904 struct text_pos pos =
18905 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18906
18907 eassert (it->method == GET_FROM_BUFFER
18908 || it->method == GET_FROM_DISPLAY_VECTOR
18909 || it->method == GET_FROM_STRING);
18910
18911 /* We need to save the current buffer/string position, so it will be
18912 restored by pop_it, because iterate_out_of_display_property
18913 depends on that being set correctly, but some situations leave
18914 it->position not yet set when this function is called. */
18915 push_it (it, &pos);
18916
18917 if (STRINGP (prop))
18918 {
18919 if (SCHARS (prop) == 0)
18920 {
18921 pop_it (it);
18922 return 0;
18923 }
18924
18925 it->string = prop;
18926 it->string_from_prefix_prop_p = 1;
18927 it->multibyte_p = STRING_MULTIBYTE (it->string);
18928 it->current.overlay_string_index = -1;
18929 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18930 it->end_charpos = it->string_nchars = SCHARS (it->string);
18931 it->method = GET_FROM_STRING;
18932 it->stop_charpos = 0;
18933 it->prev_stop = 0;
18934 it->base_level_stop = 0;
18935
18936 /* Force paragraph direction to be that of the parent
18937 buffer/string. */
18938 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18939 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18940 else
18941 it->paragraph_embedding = L2R;
18942
18943 /* Set up the bidi iterator for this display string. */
18944 if (it->bidi_p)
18945 {
18946 it->bidi_it.string.lstring = it->string;
18947 it->bidi_it.string.s = NULL;
18948 it->bidi_it.string.schars = it->end_charpos;
18949 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18950 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18951 it->bidi_it.string.unibyte = !it->multibyte_p;
18952 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18953 }
18954 }
18955 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18956 {
18957 it->method = GET_FROM_STRETCH;
18958 it->object = prop;
18959 }
18960 #ifdef HAVE_WINDOW_SYSTEM
18961 else if (IMAGEP (prop))
18962 {
18963 it->what = IT_IMAGE;
18964 it->image_id = lookup_image (it->f, prop);
18965 it->method = GET_FROM_IMAGE;
18966 }
18967 #endif /* HAVE_WINDOW_SYSTEM */
18968 else
18969 {
18970 pop_it (it); /* bogus display property, give up */
18971 return 0;
18972 }
18973
18974 return 1;
18975 }
18976
18977 /* Return the character-property PROP at the current position in IT. */
18978
18979 static Lisp_Object
18980 get_it_property (struct it *it, Lisp_Object prop)
18981 {
18982 Lisp_Object position;
18983
18984 if (STRINGP (it->object))
18985 position = make_number (IT_STRING_CHARPOS (*it));
18986 else if (BUFFERP (it->object))
18987 position = make_number (IT_CHARPOS (*it));
18988 else
18989 return Qnil;
18990
18991 return Fget_char_property (position, prop, it->object);
18992 }
18993
18994 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18995
18996 static void
18997 handle_line_prefix (struct it *it)
18998 {
18999 Lisp_Object prefix;
19000
19001 if (it->continuation_lines_width > 0)
19002 {
19003 prefix = get_it_property (it, Qwrap_prefix);
19004 if (NILP (prefix))
19005 prefix = Vwrap_prefix;
19006 }
19007 else
19008 {
19009 prefix = get_it_property (it, Qline_prefix);
19010 if (NILP (prefix))
19011 prefix = Vline_prefix;
19012 }
19013 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19014 {
19015 /* If the prefix is wider than the window, and we try to wrap
19016 it, it would acquire its own wrap prefix, and so on till the
19017 iterator stack overflows. So, don't wrap the prefix. */
19018 it->line_wrap = TRUNCATE;
19019 it->avoid_cursor_p = 1;
19020 }
19021 }
19022
19023 \f
19024
19025 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19026 only for R2L lines from display_line and display_string, when they
19027 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19028 the line/string needs to be continued on the next glyph row. */
19029 static void
19030 unproduce_glyphs (struct it *it, int n)
19031 {
19032 struct glyph *glyph, *end;
19033
19034 eassert (it->glyph_row);
19035 eassert (it->glyph_row->reversed_p);
19036 eassert (it->area == TEXT_AREA);
19037 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19038
19039 if (n > it->glyph_row->used[TEXT_AREA])
19040 n = it->glyph_row->used[TEXT_AREA];
19041 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19042 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19043 for ( ; glyph < end; glyph++)
19044 glyph[-n] = *glyph;
19045 }
19046
19047 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19048 and ROW->maxpos. */
19049 static void
19050 find_row_edges (struct it *it, struct glyph_row *row,
19051 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19052 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19053 {
19054 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19055 lines' rows is implemented for bidi-reordered rows. */
19056
19057 /* ROW->minpos is the value of min_pos, the minimal buffer position
19058 we have in ROW, or ROW->start.pos if that is smaller. */
19059 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19060 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19061 else
19062 /* We didn't find buffer positions smaller than ROW->start, or
19063 didn't find _any_ valid buffer positions in any of the glyphs,
19064 so we must trust the iterator's computed positions. */
19065 row->minpos = row->start.pos;
19066 if (max_pos <= 0)
19067 {
19068 max_pos = CHARPOS (it->current.pos);
19069 max_bpos = BYTEPOS (it->current.pos);
19070 }
19071
19072 /* Here are the various use-cases for ending the row, and the
19073 corresponding values for ROW->maxpos:
19074
19075 Line ends in a newline from buffer eol_pos + 1
19076 Line is continued from buffer max_pos + 1
19077 Line is truncated on right it->current.pos
19078 Line ends in a newline from string max_pos + 1(*)
19079 (*) + 1 only when line ends in a forward scan
19080 Line is continued from string max_pos
19081 Line is continued from display vector max_pos
19082 Line is entirely from a string min_pos == max_pos
19083 Line is entirely from a display vector min_pos == max_pos
19084 Line that ends at ZV ZV
19085
19086 If you discover other use-cases, please add them here as
19087 appropriate. */
19088 if (row->ends_at_zv_p)
19089 row->maxpos = it->current.pos;
19090 else if (row->used[TEXT_AREA])
19091 {
19092 int seen_this_string = 0;
19093 struct glyph_row *r1 = row - 1;
19094
19095 /* Did we see the same display string on the previous row? */
19096 if (STRINGP (it->object)
19097 /* this is not the first row */
19098 && row > it->w->desired_matrix->rows
19099 /* previous row is not the header line */
19100 && !r1->mode_line_p
19101 /* previous row also ends in a newline from a string */
19102 && r1->ends_in_newline_from_string_p)
19103 {
19104 struct glyph *start, *end;
19105
19106 /* Search for the last glyph of the previous row that came
19107 from buffer or string. Depending on whether the row is
19108 L2R or R2L, we need to process it front to back or the
19109 other way round. */
19110 if (!r1->reversed_p)
19111 {
19112 start = r1->glyphs[TEXT_AREA];
19113 end = start + r1->used[TEXT_AREA];
19114 /* Glyphs inserted by redisplay have an integer (zero)
19115 as their object. */
19116 while (end > start
19117 && INTEGERP ((end - 1)->object)
19118 && (end - 1)->charpos <= 0)
19119 --end;
19120 if (end > start)
19121 {
19122 if (EQ ((end - 1)->object, it->object))
19123 seen_this_string = 1;
19124 }
19125 else
19126 /* If all the glyphs of the previous row were inserted
19127 by redisplay, it means the previous row was
19128 produced from a single newline, which is only
19129 possible if that newline came from the same string
19130 as the one which produced this ROW. */
19131 seen_this_string = 1;
19132 }
19133 else
19134 {
19135 end = r1->glyphs[TEXT_AREA] - 1;
19136 start = end + r1->used[TEXT_AREA];
19137 while (end < start
19138 && INTEGERP ((end + 1)->object)
19139 && (end + 1)->charpos <= 0)
19140 ++end;
19141 if (end < start)
19142 {
19143 if (EQ ((end + 1)->object, it->object))
19144 seen_this_string = 1;
19145 }
19146 else
19147 seen_this_string = 1;
19148 }
19149 }
19150 /* Take note of each display string that covers a newline only
19151 once, the first time we see it. This is for when a display
19152 string includes more than one newline in it. */
19153 if (row->ends_in_newline_from_string_p && !seen_this_string)
19154 {
19155 /* If we were scanning the buffer forward when we displayed
19156 the string, we want to account for at least one buffer
19157 position that belongs to this row (position covered by
19158 the display string), so that cursor positioning will
19159 consider this row as a candidate when point is at the end
19160 of the visual line represented by this row. This is not
19161 required when scanning back, because max_pos will already
19162 have a much larger value. */
19163 if (CHARPOS (row->end.pos) > max_pos)
19164 INC_BOTH (max_pos, max_bpos);
19165 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19166 }
19167 else if (CHARPOS (it->eol_pos) > 0)
19168 SET_TEXT_POS (row->maxpos,
19169 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19170 else if (row->continued_p)
19171 {
19172 /* If max_pos is different from IT's current position, it
19173 means IT->method does not belong to the display element
19174 at max_pos. However, it also means that the display
19175 element at max_pos was displayed in its entirety on this
19176 line, which is equivalent to saying that the next line
19177 starts at the next buffer position. */
19178 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19179 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19180 else
19181 {
19182 INC_BOTH (max_pos, max_bpos);
19183 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19184 }
19185 }
19186 else if (row->truncated_on_right_p)
19187 /* display_line already called reseat_at_next_visible_line_start,
19188 which puts the iterator at the beginning of the next line, in
19189 the logical order. */
19190 row->maxpos = it->current.pos;
19191 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19192 /* A line that is entirely from a string/image/stretch... */
19193 row->maxpos = row->minpos;
19194 else
19195 abort ();
19196 }
19197 else
19198 row->maxpos = it->current.pos;
19199 }
19200
19201 /* Construct the glyph row IT->glyph_row in the desired matrix of
19202 IT->w from text at the current position of IT. See dispextern.h
19203 for an overview of struct it. Value is non-zero if
19204 IT->glyph_row displays text, as opposed to a line displaying ZV
19205 only. */
19206
19207 static int
19208 display_line (struct it *it)
19209 {
19210 struct glyph_row *row = it->glyph_row;
19211 Lisp_Object overlay_arrow_string;
19212 struct it wrap_it;
19213 void *wrap_data = NULL;
19214 int may_wrap = 0, wrap_x IF_LINT (= 0);
19215 int wrap_row_used = -1;
19216 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19217 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19218 int wrap_row_extra_line_spacing IF_LINT (= 0);
19219 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19220 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19221 int cvpos;
19222 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19223 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19224
19225 /* We always start displaying at hpos zero even if hscrolled. */
19226 eassert (it->hpos == 0 && it->current_x == 0);
19227
19228 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19229 >= it->w->desired_matrix->nrows)
19230 {
19231 it->w->nrows_scale_factor++;
19232 fonts_changed_p = 1;
19233 return 0;
19234 }
19235
19236 /* Is IT->w showing the region? */
19237 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19238
19239 /* Clear the result glyph row and enable it. */
19240 prepare_desired_row (row);
19241
19242 row->y = it->current_y;
19243 row->start = it->start;
19244 row->continuation_lines_width = it->continuation_lines_width;
19245 row->displays_text_p = 1;
19246 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19247 it->starts_in_middle_of_char_p = 0;
19248
19249 /* Arrange the overlays nicely for our purposes. Usually, we call
19250 display_line on only one line at a time, in which case this
19251 can't really hurt too much, or we call it on lines which appear
19252 one after another in the buffer, in which case all calls to
19253 recenter_overlay_lists but the first will be pretty cheap. */
19254 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19255
19256 /* Move over display elements that are not visible because we are
19257 hscrolled. This may stop at an x-position < IT->first_visible_x
19258 if the first glyph is partially visible or if we hit a line end. */
19259 if (it->current_x < it->first_visible_x)
19260 {
19261 enum move_it_result move_result;
19262
19263 this_line_min_pos = row->start.pos;
19264 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19265 MOVE_TO_POS | MOVE_TO_X);
19266 /* If we are under a large hscroll, move_it_in_display_line_to
19267 could hit the end of the line without reaching
19268 it->first_visible_x. Pretend that we did reach it. This is
19269 especially important on a TTY, where we will call
19270 extend_face_to_end_of_line, which needs to know how many
19271 blank glyphs to produce. */
19272 if (it->current_x < it->first_visible_x
19273 && (move_result == MOVE_NEWLINE_OR_CR
19274 || move_result == MOVE_POS_MATCH_OR_ZV))
19275 it->current_x = it->first_visible_x;
19276
19277 /* Record the smallest positions seen while we moved over
19278 display elements that are not visible. This is needed by
19279 redisplay_internal for optimizing the case where the cursor
19280 stays inside the same line. The rest of this function only
19281 considers positions that are actually displayed, so
19282 RECORD_MAX_MIN_POS will not otherwise record positions that
19283 are hscrolled to the left of the left edge of the window. */
19284 min_pos = CHARPOS (this_line_min_pos);
19285 min_bpos = BYTEPOS (this_line_min_pos);
19286 }
19287 else
19288 {
19289 /* We only do this when not calling `move_it_in_display_line_to'
19290 above, because move_it_in_display_line_to calls
19291 handle_line_prefix itself. */
19292 handle_line_prefix (it);
19293 }
19294
19295 /* Get the initial row height. This is either the height of the
19296 text hscrolled, if there is any, or zero. */
19297 row->ascent = it->max_ascent;
19298 row->height = it->max_ascent + it->max_descent;
19299 row->phys_ascent = it->max_phys_ascent;
19300 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19301 row->extra_line_spacing = it->max_extra_line_spacing;
19302
19303 /* Utility macro to record max and min buffer positions seen until now. */
19304 #define RECORD_MAX_MIN_POS(IT) \
19305 do \
19306 { \
19307 int composition_p = !STRINGP ((IT)->string) \
19308 && ((IT)->what == IT_COMPOSITION); \
19309 ptrdiff_t current_pos = \
19310 composition_p ? (IT)->cmp_it.charpos \
19311 : IT_CHARPOS (*(IT)); \
19312 ptrdiff_t current_bpos = \
19313 composition_p ? CHAR_TO_BYTE (current_pos) \
19314 : IT_BYTEPOS (*(IT)); \
19315 if (current_pos < min_pos) \
19316 { \
19317 min_pos = current_pos; \
19318 min_bpos = current_bpos; \
19319 } \
19320 if (IT_CHARPOS (*it) > max_pos) \
19321 { \
19322 max_pos = IT_CHARPOS (*it); \
19323 max_bpos = IT_BYTEPOS (*it); \
19324 } \
19325 } \
19326 while (0)
19327
19328 /* Loop generating characters. The loop is left with IT on the next
19329 character to display. */
19330 while (1)
19331 {
19332 int n_glyphs_before, hpos_before, x_before;
19333 int x, nglyphs;
19334 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19335
19336 /* Retrieve the next thing to display. Value is zero if end of
19337 buffer reached. */
19338 if (!get_next_display_element (it))
19339 {
19340 /* Maybe add a space at the end of this line that is used to
19341 display the cursor there under X. Set the charpos of the
19342 first glyph of blank lines not corresponding to any text
19343 to -1. */
19344 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19345 row->exact_window_width_line_p = 1;
19346 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19347 || row->used[TEXT_AREA] == 0)
19348 {
19349 row->glyphs[TEXT_AREA]->charpos = -1;
19350 row->displays_text_p = 0;
19351
19352 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19353 && (!MINI_WINDOW_P (it->w)
19354 || (minibuf_level && EQ (it->window, minibuf_window))))
19355 row->indicate_empty_line_p = 1;
19356 }
19357
19358 it->continuation_lines_width = 0;
19359 row->ends_at_zv_p = 1;
19360 /* A row that displays right-to-left text must always have
19361 its last face extended all the way to the end of line,
19362 even if this row ends in ZV, because we still write to
19363 the screen left to right. We also need to extend the
19364 last face if the default face is remapped to some
19365 different face, otherwise the functions that clear
19366 portions of the screen will clear with the default face's
19367 background color. */
19368 if (row->reversed_p
19369 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19370 extend_face_to_end_of_line (it);
19371 break;
19372 }
19373
19374 /* Now, get the metrics of what we want to display. This also
19375 generates glyphs in `row' (which is IT->glyph_row). */
19376 n_glyphs_before = row->used[TEXT_AREA];
19377 x = it->current_x;
19378
19379 /* Remember the line height so far in case the next element doesn't
19380 fit on the line. */
19381 if (it->line_wrap != TRUNCATE)
19382 {
19383 ascent = it->max_ascent;
19384 descent = it->max_descent;
19385 phys_ascent = it->max_phys_ascent;
19386 phys_descent = it->max_phys_descent;
19387
19388 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19389 {
19390 if (IT_DISPLAYING_WHITESPACE (it))
19391 may_wrap = 1;
19392 else if (may_wrap)
19393 {
19394 SAVE_IT (wrap_it, *it, wrap_data);
19395 wrap_x = x;
19396 wrap_row_used = row->used[TEXT_AREA];
19397 wrap_row_ascent = row->ascent;
19398 wrap_row_height = row->height;
19399 wrap_row_phys_ascent = row->phys_ascent;
19400 wrap_row_phys_height = row->phys_height;
19401 wrap_row_extra_line_spacing = row->extra_line_spacing;
19402 wrap_row_min_pos = min_pos;
19403 wrap_row_min_bpos = min_bpos;
19404 wrap_row_max_pos = max_pos;
19405 wrap_row_max_bpos = max_bpos;
19406 may_wrap = 0;
19407 }
19408 }
19409 }
19410
19411 PRODUCE_GLYPHS (it);
19412
19413 /* If this display element was in marginal areas, continue with
19414 the next one. */
19415 if (it->area != TEXT_AREA)
19416 {
19417 row->ascent = max (row->ascent, it->max_ascent);
19418 row->height = max (row->height, it->max_ascent + it->max_descent);
19419 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19420 row->phys_height = max (row->phys_height,
19421 it->max_phys_ascent + it->max_phys_descent);
19422 row->extra_line_spacing = max (row->extra_line_spacing,
19423 it->max_extra_line_spacing);
19424 set_iterator_to_next (it, 1);
19425 continue;
19426 }
19427
19428 /* Does the display element fit on the line? If we truncate
19429 lines, we should draw past the right edge of the window. If
19430 we don't truncate, we want to stop so that we can display the
19431 continuation glyph before the right margin. If lines are
19432 continued, there are two possible strategies for characters
19433 resulting in more than 1 glyph (e.g. tabs): Display as many
19434 glyphs as possible in this line and leave the rest for the
19435 continuation line, or display the whole element in the next
19436 line. Original redisplay did the former, so we do it also. */
19437 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19438 hpos_before = it->hpos;
19439 x_before = x;
19440
19441 if (/* Not a newline. */
19442 nglyphs > 0
19443 /* Glyphs produced fit entirely in the line. */
19444 && it->current_x < it->last_visible_x)
19445 {
19446 it->hpos += nglyphs;
19447 row->ascent = max (row->ascent, it->max_ascent);
19448 row->height = max (row->height, it->max_ascent + it->max_descent);
19449 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19450 row->phys_height = max (row->phys_height,
19451 it->max_phys_ascent + it->max_phys_descent);
19452 row->extra_line_spacing = max (row->extra_line_spacing,
19453 it->max_extra_line_spacing);
19454 if (it->current_x - it->pixel_width < it->first_visible_x)
19455 row->x = x - it->first_visible_x;
19456 /* Record the maximum and minimum buffer positions seen so
19457 far in glyphs that will be displayed by this row. */
19458 if (it->bidi_p)
19459 RECORD_MAX_MIN_POS (it);
19460 }
19461 else
19462 {
19463 int i, new_x;
19464 struct glyph *glyph;
19465
19466 for (i = 0; i < nglyphs; ++i, x = new_x)
19467 {
19468 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19469 new_x = x + glyph->pixel_width;
19470
19471 if (/* Lines are continued. */
19472 it->line_wrap != TRUNCATE
19473 && (/* Glyph doesn't fit on the line. */
19474 new_x > it->last_visible_x
19475 /* Or it fits exactly on a window system frame. */
19476 || (new_x == it->last_visible_x
19477 && FRAME_WINDOW_P (it->f)
19478 && (row->reversed_p
19479 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19480 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19481 {
19482 /* End of a continued line. */
19483
19484 if (it->hpos == 0
19485 || (new_x == it->last_visible_x
19486 && FRAME_WINDOW_P (it->f)
19487 && (row->reversed_p
19488 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19489 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19490 {
19491 /* Current glyph is the only one on the line or
19492 fits exactly on the line. We must continue
19493 the line because we can't draw the cursor
19494 after the glyph. */
19495 row->continued_p = 1;
19496 it->current_x = new_x;
19497 it->continuation_lines_width += new_x;
19498 ++it->hpos;
19499 if (i == nglyphs - 1)
19500 {
19501 /* If line-wrap is on, check if a previous
19502 wrap point was found. */
19503 if (wrap_row_used > 0
19504 /* Even if there is a previous wrap
19505 point, continue the line here as
19506 usual, if (i) the previous character
19507 was a space or tab AND (ii) the
19508 current character is not. */
19509 && (!may_wrap
19510 || IT_DISPLAYING_WHITESPACE (it)))
19511 goto back_to_wrap;
19512
19513 /* Record the maximum and minimum buffer
19514 positions seen so far in glyphs that will be
19515 displayed by this row. */
19516 if (it->bidi_p)
19517 RECORD_MAX_MIN_POS (it);
19518 set_iterator_to_next (it, 1);
19519 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19520 {
19521 if (!get_next_display_element (it))
19522 {
19523 row->exact_window_width_line_p = 1;
19524 it->continuation_lines_width = 0;
19525 row->continued_p = 0;
19526 row->ends_at_zv_p = 1;
19527 }
19528 else if (ITERATOR_AT_END_OF_LINE_P (it))
19529 {
19530 row->continued_p = 0;
19531 row->exact_window_width_line_p = 1;
19532 }
19533 }
19534 }
19535 else if (it->bidi_p)
19536 RECORD_MAX_MIN_POS (it);
19537 }
19538 else if (CHAR_GLYPH_PADDING_P (*glyph)
19539 && !FRAME_WINDOW_P (it->f))
19540 {
19541 /* A padding glyph that doesn't fit on this line.
19542 This means the whole character doesn't fit
19543 on the line. */
19544 if (row->reversed_p)
19545 unproduce_glyphs (it, row->used[TEXT_AREA]
19546 - n_glyphs_before);
19547 row->used[TEXT_AREA] = n_glyphs_before;
19548
19549 /* Fill the rest of the row with continuation
19550 glyphs like in 20.x. */
19551 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19552 < row->glyphs[1 + TEXT_AREA])
19553 produce_special_glyphs (it, IT_CONTINUATION);
19554
19555 row->continued_p = 1;
19556 it->current_x = x_before;
19557 it->continuation_lines_width += x_before;
19558
19559 /* Restore the height to what it was before the
19560 element not fitting on the line. */
19561 it->max_ascent = ascent;
19562 it->max_descent = descent;
19563 it->max_phys_ascent = phys_ascent;
19564 it->max_phys_descent = phys_descent;
19565 }
19566 else if (wrap_row_used > 0)
19567 {
19568 back_to_wrap:
19569 if (row->reversed_p)
19570 unproduce_glyphs (it,
19571 row->used[TEXT_AREA] - wrap_row_used);
19572 RESTORE_IT (it, &wrap_it, wrap_data);
19573 it->continuation_lines_width += wrap_x;
19574 row->used[TEXT_AREA] = wrap_row_used;
19575 row->ascent = wrap_row_ascent;
19576 row->height = wrap_row_height;
19577 row->phys_ascent = wrap_row_phys_ascent;
19578 row->phys_height = wrap_row_phys_height;
19579 row->extra_line_spacing = wrap_row_extra_line_spacing;
19580 min_pos = wrap_row_min_pos;
19581 min_bpos = wrap_row_min_bpos;
19582 max_pos = wrap_row_max_pos;
19583 max_bpos = wrap_row_max_bpos;
19584 row->continued_p = 1;
19585 row->ends_at_zv_p = 0;
19586 row->exact_window_width_line_p = 0;
19587 it->continuation_lines_width += x;
19588
19589 /* Make sure that a non-default face is extended
19590 up to the right margin of the window. */
19591 extend_face_to_end_of_line (it);
19592 }
19593 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19594 {
19595 /* A TAB that extends past the right edge of the
19596 window. This produces a single glyph on
19597 window system frames. We leave the glyph in
19598 this row and let it fill the row, but don't
19599 consume the TAB. */
19600 if ((row->reversed_p
19601 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19602 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19603 produce_special_glyphs (it, IT_CONTINUATION);
19604 it->continuation_lines_width += it->last_visible_x;
19605 row->ends_in_middle_of_char_p = 1;
19606 row->continued_p = 1;
19607 glyph->pixel_width = it->last_visible_x - x;
19608 it->starts_in_middle_of_char_p = 1;
19609 }
19610 else
19611 {
19612 /* Something other than a TAB that draws past
19613 the right edge of the window. Restore
19614 positions to values before the element. */
19615 if (row->reversed_p)
19616 unproduce_glyphs (it, row->used[TEXT_AREA]
19617 - (n_glyphs_before + i));
19618 row->used[TEXT_AREA] = n_glyphs_before + i;
19619
19620 /* Display continuation glyphs. */
19621 if (!FRAME_WINDOW_P (it->f)
19622 || (row->reversed_p
19623 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19624 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19625 produce_special_glyphs (it, IT_CONTINUATION);
19626 row->continued_p = 1;
19627
19628 it->current_x = x_before;
19629 it->continuation_lines_width += x;
19630 extend_face_to_end_of_line (it);
19631
19632 if (nglyphs > 1 && i > 0)
19633 {
19634 row->ends_in_middle_of_char_p = 1;
19635 it->starts_in_middle_of_char_p = 1;
19636 }
19637
19638 /* Restore the height to what it was before the
19639 element not fitting on the line. */
19640 it->max_ascent = ascent;
19641 it->max_descent = descent;
19642 it->max_phys_ascent = phys_ascent;
19643 it->max_phys_descent = phys_descent;
19644 }
19645
19646 break;
19647 }
19648 else if (new_x > it->first_visible_x)
19649 {
19650 /* Increment number of glyphs actually displayed. */
19651 ++it->hpos;
19652
19653 /* Record the maximum and minimum buffer positions
19654 seen so far in glyphs that will be displayed by
19655 this row. */
19656 if (it->bidi_p)
19657 RECORD_MAX_MIN_POS (it);
19658
19659 if (x < it->first_visible_x)
19660 /* Glyph is partially visible, i.e. row starts at
19661 negative X position. */
19662 row->x = x - it->first_visible_x;
19663 }
19664 else
19665 {
19666 /* Glyph is completely off the left margin of the
19667 window. This should not happen because of the
19668 move_it_in_display_line at the start of this
19669 function, unless the text display area of the
19670 window is empty. */
19671 eassert (it->first_visible_x <= it->last_visible_x);
19672 }
19673 }
19674 /* Even if this display element produced no glyphs at all,
19675 we want to record its position. */
19676 if (it->bidi_p && nglyphs == 0)
19677 RECORD_MAX_MIN_POS (it);
19678
19679 row->ascent = max (row->ascent, it->max_ascent);
19680 row->height = max (row->height, it->max_ascent + it->max_descent);
19681 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19682 row->phys_height = max (row->phys_height,
19683 it->max_phys_ascent + it->max_phys_descent);
19684 row->extra_line_spacing = max (row->extra_line_spacing,
19685 it->max_extra_line_spacing);
19686
19687 /* End of this display line if row is continued. */
19688 if (row->continued_p || row->ends_at_zv_p)
19689 break;
19690 }
19691
19692 at_end_of_line:
19693 /* Is this a line end? If yes, we're also done, after making
19694 sure that a non-default face is extended up to the right
19695 margin of the window. */
19696 if (ITERATOR_AT_END_OF_LINE_P (it))
19697 {
19698 int used_before = row->used[TEXT_AREA];
19699
19700 row->ends_in_newline_from_string_p = STRINGP (it->object);
19701
19702 /* Add a space at the end of the line that is used to
19703 display the cursor there. */
19704 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19705 append_space_for_newline (it, 0);
19706
19707 /* Extend the face to the end of the line. */
19708 extend_face_to_end_of_line (it);
19709
19710 /* Make sure we have the position. */
19711 if (used_before == 0)
19712 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19713
19714 /* Record the position of the newline, for use in
19715 find_row_edges. */
19716 it->eol_pos = it->current.pos;
19717
19718 /* Consume the line end. This skips over invisible lines. */
19719 set_iterator_to_next (it, 1);
19720 it->continuation_lines_width = 0;
19721 break;
19722 }
19723
19724 /* Proceed with next display element. Note that this skips
19725 over lines invisible because of selective display. */
19726 set_iterator_to_next (it, 1);
19727
19728 /* If we truncate lines, we are done when the last displayed
19729 glyphs reach past the right margin of the window. */
19730 if (it->line_wrap == TRUNCATE
19731 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19732 ? (it->current_x >= it->last_visible_x)
19733 : (it->current_x > it->last_visible_x)))
19734 {
19735 /* Maybe add truncation glyphs. */
19736 if (!FRAME_WINDOW_P (it->f)
19737 || (row->reversed_p
19738 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19739 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19740 {
19741 int i, n;
19742
19743 if (!row->reversed_p)
19744 {
19745 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19746 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19747 break;
19748 }
19749 else
19750 {
19751 for (i = 0; i < row->used[TEXT_AREA]; i++)
19752 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19753 break;
19754 /* Remove any padding glyphs at the front of ROW, to
19755 make room for the truncation glyphs we will be
19756 adding below. The loop below always inserts at
19757 least one truncation glyph, so also remove the
19758 last glyph added to ROW. */
19759 unproduce_glyphs (it, i + 1);
19760 /* Adjust i for the loop below. */
19761 i = row->used[TEXT_AREA] - (i + 1);
19762 }
19763
19764 it->current_x = x_before;
19765 if (!FRAME_WINDOW_P (it->f))
19766 {
19767 for (n = row->used[TEXT_AREA]; i < n; ++i)
19768 {
19769 row->used[TEXT_AREA] = i;
19770 produce_special_glyphs (it, IT_TRUNCATION);
19771 }
19772 }
19773 #ifdef HAVE_WINDOW_SYSTEM
19774 else
19775 {
19776 int stretch_width = it->last_visible_x - it->current_x;
19777
19778 row->used[TEXT_AREA] = i;
19779 if (stretch_width > 0)
19780 {
19781 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19782 struct font *font =
19783 face->font ? face->font : FRAME_FONT (it->f);
19784 int stretch_ascent =
19785 (((it->ascent + it->descent)
19786 * FONT_BASE (font)) / FONT_HEIGHT (font));
19787 struct text_pos saved_pos = it->position;
19788
19789 memset (&it->position, 0, sizeof it->position);
19790 append_stretch_glyph (it, make_number (0), stretch_width,
19791 it->ascent + it->descent,
19792 stretch_ascent);
19793 it->position = saved_pos;
19794 }
19795 produce_special_glyphs (it, IT_TRUNCATION);
19796 }
19797 #endif
19798 }
19799 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19800 {
19801 /* Don't truncate if we can overflow newline into fringe. */
19802 if (!get_next_display_element (it))
19803 {
19804 it->continuation_lines_width = 0;
19805 row->ends_at_zv_p = 1;
19806 row->exact_window_width_line_p = 1;
19807 break;
19808 }
19809 if (ITERATOR_AT_END_OF_LINE_P (it))
19810 {
19811 row->exact_window_width_line_p = 1;
19812 goto at_end_of_line;
19813 }
19814 it->current_x = x_before;
19815 }
19816
19817 row->truncated_on_right_p = 1;
19818 it->continuation_lines_width = 0;
19819 reseat_at_next_visible_line_start (it, 0);
19820 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19821 it->hpos = hpos_before;
19822 break;
19823 }
19824 }
19825
19826 if (wrap_data)
19827 bidi_unshelve_cache (wrap_data, 1);
19828
19829 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19830 at the left window margin. */
19831 if (it->first_visible_x
19832 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19833 {
19834 if (!FRAME_WINDOW_P (it->f)
19835 || (row->reversed_p
19836 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19837 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19838 insert_left_trunc_glyphs (it);
19839 row->truncated_on_left_p = 1;
19840 }
19841
19842 /* Remember the position at which this line ends.
19843
19844 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19845 cannot be before the call to find_row_edges below, since that is
19846 where these positions are determined. */
19847 row->end = it->current;
19848 if (!it->bidi_p)
19849 {
19850 row->minpos = row->start.pos;
19851 row->maxpos = row->end.pos;
19852 }
19853 else
19854 {
19855 /* ROW->minpos and ROW->maxpos must be the smallest and
19856 `1 + the largest' buffer positions in ROW. But if ROW was
19857 bidi-reordered, these two positions can be anywhere in the
19858 row, so we must determine them now. */
19859 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19860 }
19861
19862 /* If the start of this line is the overlay arrow-position, then
19863 mark this glyph row as the one containing the overlay arrow.
19864 This is clearly a mess with variable size fonts. It would be
19865 better to let it be displayed like cursors under X. */
19866 if ((row->displays_text_p || !overlay_arrow_seen)
19867 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19868 !NILP (overlay_arrow_string)))
19869 {
19870 /* Overlay arrow in window redisplay is a fringe bitmap. */
19871 if (STRINGP (overlay_arrow_string))
19872 {
19873 struct glyph_row *arrow_row
19874 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19875 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19876 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19877 struct glyph *p = row->glyphs[TEXT_AREA];
19878 struct glyph *p2, *end;
19879
19880 /* Copy the arrow glyphs. */
19881 while (glyph < arrow_end)
19882 *p++ = *glyph++;
19883
19884 /* Throw away padding glyphs. */
19885 p2 = p;
19886 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19887 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19888 ++p2;
19889 if (p2 > p)
19890 {
19891 while (p2 < end)
19892 *p++ = *p2++;
19893 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19894 }
19895 }
19896 else
19897 {
19898 eassert (INTEGERP (overlay_arrow_string));
19899 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19900 }
19901 overlay_arrow_seen = 1;
19902 }
19903
19904 /* Highlight trailing whitespace. */
19905 if (!NILP (Vshow_trailing_whitespace))
19906 highlight_trailing_whitespace (it->f, it->glyph_row);
19907
19908 /* Compute pixel dimensions of this line. */
19909 compute_line_metrics (it);
19910
19911 /* Implementation note: No changes in the glyphs of ROW or in their
19912 faces can be done past this point, because compute_line_metrics
19913 computes ROW's hash value and stores it within the glyph_row
19914 structure. */
19915
19916 /* Record whether this row ends inside an ellipsis. */
19917 row->ends_in_ellipsis_p
19918 = (it->method == GET_FROM_DISPLAY_VECTOR
19919 && it->ellipsis_p);
19920
19921 /* Save fringe bitmaps in this row. */
19922 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19923 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19924 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19925 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19926
19927 it->left_user_fringe_bitmap = 0;
19928 it->left_user_fringe_face_id = 0;
19929 it->right_user_fringe_bitmap = 0;
19930 it->right_user_fringe_face_id = 0;
19931
19932 /* Maybe set the cursor. */
19933 cvpos = it->w->cursor.vpos;
19934 if ((cvpos < 0
19935 /* In bidi-reordered rows, keep checking for proper cursor
19936 position even if one has been found already, because buffer
19937 positions in such rows change non-linearly with ROW->VPOS,
19938 when a line is continued. One exception: when we are at ZV,
19939 display cursor on the first suitable glyph row, since all
19940 the empty rows after that also have their position set to ZV. */
19941 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19942 lines' rows is implemented for bidi-reordered rows. */
19943 || (it->bidi_p
19944 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19945 && PT >= MATRIX_ROW_START_CHARPOS (row)
19946 && PT <= MATRIX_ROW_END_CHARPOS (row)
19947 && cursor_row_p (row))
19948 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19949
19950 /* Prepare for the next line. This line starts horizontally at (X
19951 HPOS) = (0 0). Vertical positions are incremented. As a
19952 convenience for the caller, IT->glyph_row is set to the next
19953 row to be used. */
19954 it->current_x = it->hpos = 0;
19955 it->current_y += row->height;
19956 SET_TEXT_POS (it->eol_pos, 0, 0);
19957 ++it->vpos;
19958 ++it->glyph_row;
19959 /* The next row should by default use the same value of the
19960 reversed_p flag as this one. set_iterator_to_next decides when
19961 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19962 the flag accordingly. */
19963 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19964 it->glyph_row->reversed_p = row->reversed_p;
19965 it->start = row->end;
19966 return row->displays_text_p;
19967
19968 #undef RECORD_MAX_MIN_POS
19969 }
19970
19971 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19972 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19973 doc: /* Return paragraph direction at point in BUFFER.
19974 Value is either `left-to-right' or `right-to-left'.
19975 If BUFFER is omitted or nil, it defaults to the current buffer.
19976
19977 Paragraph direction determines how the text in the paragraph is displayed.
19978 In left-to-right paragraphs, text begins at the left margin of the window
19979 and the reading direction is generally left to right. In right-to-left
19980 paragraphs, text begins at the right margin and is read from right to left.
19981
19982 See also `bidi-paragraph-direction'. */)
19983 (Lisp_Object buffer)
19984 {
19985 struct buffer *buf = current_buffer;
19986 struct buffer *old = buf;
19987
19988 if (! NILP (buffer))
19989 {
19990 CHECK_BUFFER (buffer);
19991 buf = XBUFFER (buffer);
19992 }
19993
19994 if (NILP (BVAR (buf, bidi_display_reordering))
19995 || NILP (BVAR (buf, enable_multibyte_characters))
19996 /* When we are loading loadup.el, the character property tables
19997 needed for bidi iteration are not yet available. */
19998 || !NILP (Vpurify_flag))
19999 return Qleft_to_right;
20000 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20001 return BVAR (buf, bidi_paragraph_direction);
20002 else
20003 {
20004 /* Determine the direction from buffer text. We could try to
20005 use current_matrix if it is up to date, but this seems fast
20006 enough as it is. */
20007 struct bidi_it itb;
20008 ptrdiff_t pos = BUF_PT (buf);
20009 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20010 int c;
20011 void *itb_data = bidi_shelve_cache ();
20012
20013 set_buffer_temp (buf);
20014 /* bidi_paragraph_init finds the base direction of the paragraph
20015 by searching forward from paragraph start. We need the base
20016 direction of the current or _previous_ paragraph, so we need
20017 to make sure we are within that paragraph. To that end, find
20018 the previous non-empty line. */
20019 if (pos >= ZV && pos > BEGV)
20020 {
20021 pos--;
20022 bytepos = CHAR_TO_BYTE (pos);
20023 }
20024 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20025 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20026 {
20027 while ((c = FETCH_BYTE (bytepos)) == '\n'
20028 || c == ' ' || c == '\t' || c == '\f')
20029 {
20030 if (bytepos <= BEGV_BYTE)
20031 break;
20032 bytepos--;
20033 pos--;
20034 }
20035 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20036 bytepos--;
20037 }
20038 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20039 itb.paragraph_dir = NEUTRAL_DIR;
20040 itb.string.s = NULL;
20041 itb.string.lstring = Qnil;
20042 itb.string.bufpos = 0;
20043 itb.string.unibyte = 0;
20044 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20045 bidi_unshelve_cache (itb_data, 0);
20046 set_buffer_temp (old);
20047 switch (itb.paragraph_dir)
20048 {
20049 case L2R:
20050 return Qleft_to_right;
20051 break;
20052 case R2L:
20053 return Qright_to_left;
20054 break;
20055 default:
20056 abort ();
20057 }
20058 }
20059 }
20060
20061
20062 \f
20063 /***********************************************************************
20064 Menu Bar
20065 ***********************************************************************/
20066
20067 /* Redisplay the menu bar in the frame for window W.
20068
20069 The menu bar of X frames that don't have X toolkit support is
20070 displayed in a special window W->frame->menu_bar_window.
20071
20072 The menu bar of terminal frames is treated specially as far as
20073 glyph matrices are concerned. Menu bar lines are not part of
20074 windows, so the update is done directly on the frame matrix rows
20075 for the menu bar. */
20076
20077 static void
20078 display_menu_bar (struct window *w)
20079 {
20080 struct frame *f = XFRAME (WINDOW_FRAME (w));
20081 struct it it;
20082 Lisp_Object items;
20083 int i;
20084
20085 /* Don't do all this for graphical frames. */
20086 #ifdef HAVE_NTGUI
20087 if (FRAME_W32_P (f))
20088 return;
20089 #endif
20090 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20091 if (FRAME_X_P (f))
20092 return;
20093 #endif
20094
20095 #ifdef HAVE_NS
20096 if (FRAME_NS_P (f))
20097 return;
20098 #endif /* HAVE_NS */
20099
20100 #ifdef USE_X_TOOLKIT
20101 eassert (!FRAME_WINDOW_P (f));
20102 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20103 it.first_visible_x = 0;
20104 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20105 #else /* not USE_X_TOOLKIT */
20106 if (FRAME_WINDOW_P (f))
20107 {
20108 /* Menu bar lines are displayed in the desired matrix of the
20109 dummy window menu_bar_window. */
20110 struct window *menu_w;
20111 eassert (WINDOWP (f->menu_bar_window));
20112 menu_w = XWINDOW (f->menu_bar_window);
20113 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20114 MENU_FACE_ID);
20115 it.first_visible_x = 0;
20116 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20117 }
20118 else
20119 {
20120 /* This is a TTY frame, i.e. character hpos/vpos are used as
20121 pixel x/y. */
20122 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20123 MENU_FACE_ID);
20124 it.first_visible_x = 0;
20125 it.last_visible_x = FRAME_COLS (f);
20126 }
20127 #endif /* not USE_X_TOOLKIT */
20128
20129 /* FIXME: This should be controlled by a user option. See the
20130 comments in redisplay_tool_bar and display_mode_line about
20131 this. */
20132 it.paragraph_embedding = L2R;
20133
20134 if (! mode_line_inverse_video)
20135 /* Force the menu-bar to be displayed in the default face. */
20136 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20137
20138 /* Clear all rows of the menu bar. */
20139 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20140 {
20141 struct glyph_row *row = it.glyph_row + i;
20142 clear_glyph_row (row);
20143 row->enabled_p = 1;
20144 row->full_width_p = 1;
20145 }
20146
20147 /* Display all items of the menu bar. */
20148 items = FRAME_MENU_BAR_ITEMS (it.f);
20149 for (i = 0; i < ASIZE (items); i += 4)
20150 {
20151 Lisp_Object string;
20152
20153 /* Stop at nil string. */
20154 string = AREF (items, i + 1);
20155 if (NILP (string))
20156 break;
20157
20158 /* Remember where item was displayed. */
20159 ASET (items, i + 3, make_number (it.hpos));
20160
20161 /* Display the item, pad with one space. */
20162 if (it.current_x < it.last_visible_x)
20163 display_string (NULL, string, Qnil, 0, 0, &it,
20164 SCHARS (string) + 1, 0, 0, -1);
20165 }
20166
20167 /* Fill out the line with spaces. */
20168 if (it.current_x < it.last_visible_x)
20169 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20170
20171 /* Compute the total height of the lines. */
20172 compute_line_metrics (&it);
20173 }
20174
20175
20176 \f
20177 /***********************************************************************
20178 Mode Line
20179 ***********************************************************************/
20180
20181 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20182 FORCE is non-zero, redisplay mode lines unconditionally.
20183 Otherwise, redisplay only mode lines that are garbaged. Value is
20184 the number of windows whose mode lines were redisplayed. */
20185
20186 static int
20187 redisplay_mode_lines (Lisp_Object window, int force)
20188 {
20189 int nwindows = 0;
20190
20191 while (!NILP (window))
20192 {
20193 struct window *w = XWINDOW (window);
20194
20195 if (WINDOWP (w->hchild))
20196 nwindows += redisplay_mode_lines (w->hchild, force);
20197 else if (WINDOWP (w->vchild))
20198 nwindows += redisplay_mode_lines (w->vchild, force);
20199 else if (force
20200 || FRAME_GARBAGED_P (XFRAME (w->frame))
20201 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20202 {
20203 struct text_pos lpoint;
20204 struct buffer *old = current_buffer;
20205
20206 /* Set the window's buffer for the mode line display. */
20207 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20208 set_buffer_internal_1 (XBUFFER (w->buffer));
20209
20210 /* Point refers normally to the selected window. For any
20211 other window, set up appropriate value. */
20212 if (!EQ (window, selected_window))
20213 {
20214 struct text_pos pt;
20215
20216 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20217 if (CHARPOS (pt) < BEGV)
20218 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20219 else if (CHARPOS (pt) > (ZV - 1))
20220 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20221 else
20222 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20223 }
20224
20225 /* Display mode lines. */
20226 clear_glyph_matrix (w->desired_matrix);
20227 if (display_mode_lines (w))
20228 {
20229 ++nwindows;
20230 w->must_be_updated_p = 1;
20231 }
20232
20233 /* Restore old settings. */
20234 set_buffer_internal_1 (old);
20235 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20236 }
20237
20238 window = w->next;
20239 }
20240
20241 return nwindows;
20242 }
20243
20244
20245 /* Display the mode and/or header line of window W. Value is the
20246 sum number of mode lines and header lines displayed. */
20247
20248 static int
20249 display_mode_lines (struct window *w)
20250 {
20251 Lisp_Object old_selected_window, old_selected_frame;
20252 int n = 0;
20253
20254 old_selected_frame = selected_frame;
20255 selected_frame = w->frame;
20256 old_selected_window = selected_window;
20257 XSETWINDOW (selected_window, w);
20258
20259 /* These will be set while the mode line specs are processed. */
20260 line_number_displayed = 0;
20261 w->column_number_displayed = Qnil;
20262
20263 if (WINDOW_WANTS_MODELINE_P (w))
20264 {
20265 struct window *sel_w = XWINDOW (old_selected_window);
20266
20267 /* Select mode line face based on the real selected window. */
20268 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20269 BVAR (current_buffer, mode_line_format));
20270 ++n;
20271 }
20272
20273 if (WINDOW_WANTS_HEADER_LINE_P (w))
20274 {
20275 display_mode_line (w, HEADER_LINE_FACE_ID,
20276 BVAR (current_buffer, header_line_format));
20277 ++n;
20278 }
20279
20280 selected_frame = old_selected_frame;
20281 selected_window = old_selected_window;
20282 return n;
20283 }
20284
20285
20286 /* Display mode or header line of window W. FACE_ID specifies which
20287 line to display; it is either MODE_LINE_FACE_ID or
20288 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20289 display. Value is the pixel height of the mode/header line
20290 displayed. */
20291
20292 static int
20293 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20294 {
20295 struct it it;
20296 struct face *face;
20297 ptrdiff_t count = SPECPDL_INDEX ();
20298
20299 init_iterator (&it, w, -1, -1, NULL, face_id);
20300 /* Don't extend on a previously drawn mode-line.
20301 This may happen if called from pos_visible_p. */
20302 it.glyph_row->enabled_p = 0;
20303 prepare_desired_row (it.glyph_row);
20304
20305 it.glyph_row->mode_line_p = 1;
20306
20307 if (! mode_line_inverse_video)
20308 /* Force the mode-line to be displayed in the default face. */
20309 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20310
20311 /* FIXME: This should be controlled by a user option. But
20312 supporting such an option is not trivial, since the mode line is
20313 made up of many separate strings. */
20314 it.paragraph_embedding = L2R;
20315
20316 record_unwind_protect (unwind_format_mode_line,
20317 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20318
20319 mode_line_target = MODE_LINE_DISPLAY;
20320
20321 /* Temporarily make frame's keyboard the current kboard so that
20322 kboard-local variables in the mode_line_format will get the right
20323 values. */
20324 push_kboard (FRAME_KBOARD (it.f));
20325 record_unwind_save_match_data ();
20326 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20327 pop_kboard ();
20328
20329 unbind_to (count, Qnil);
20330
20331 /* Fill up with spaces. */
20332 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20333
20334 compute_line_metrics (&it);
20335 it.glyph_row->full_width_p = 1;
20336 it.glyph_row->continued_p = 0;
20337 it.glyph_row->truncated_on_left_p = 0;
20338 it.glyph_row->truncated_on_right_p = 0;
20339
20340 /* Make a 3D mode-line have a shadow at its right end. */
20341 face = FACE_FROM_ID (it.f, face_id);
20342 extend_face_to_end_of_line (&it);
20343 if (face->box != FACE_NO_BOX)
20344 {
20345 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20346 + it.glyph_row->used[TEXT_AREA] - 1);
20347 last->right_box_line_p = 1;
20348 }
20349
20350 return it.glyph_row->height;
20351 }
20352
20353 /* Move element ELT in LIST to the front of LIST.
20354 Return the updated list. */
20355
20356 static Lisp_Object
20357 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20358 {
20359 register Lisp_Object tail, prev;
20360 register Lisp_Object tem;
20361
20362 tail = list;
20363 prev = Qnil;
20364 while (CONSP (tail))
20365 {
20366 tem = XCAR (tail);
20367
20368 if (EQ (elt, tem))
20369 {
20370 /* Splice out the link TAIL. */
20371 if (NILP (prev))
20372 list = XCDR (tail);
20373 else
20374 Fsetcdr (prev, XCDR (tail));
20375
20376 /* Now make it the first. */
20377 Fsetcdr (tail, list);
20378 return tail;
20379 }
20380 else
20381 prev = tail;
20382 tail = XCDR (tail);
20383 QUIT;
20384 }
20385
20386 /* Not found--return unchanged LIST. */
20387 return list;
20388 }
20389
20390 /* Contribute ELT to the mode line for window IT->w. How it
20391 translates into text depends on its data type.
20392
20393 IT describes the display environment in which we display, as usual.
20394
20395 DEPTH is the depth in recursion. It is used to prevent
20396 infinite recursion here.
20397
20398 FIELD_WIDTH is the number of characters the display of ELT should
20399 occupy in the mode line, and PRECISION is the maximum number of
20400 characters to display from ELT's representation. See
20401 display_string for details.
20402
20403 Returns the hpos of the end of the text generated by ELT.
20404
20405 PROPS is a property list to add to any string we encounter.
20406
20407 If RISKY is nonzero, remove (disregard) any properties in any string
20408 we encounter, and ignore :eval and :propertize.
20409
20410 The global variable `mode_line_target' determines whether the
20411 output is passed to `store_mode_line_noprop',
20412 `store_mode_line_string', or `display_string'. */
20413
20414 static int
20415 display_mode_element (struct it *it, int depth, int field_width, int precision,
20416 Lisp_Object elt, Lisp_Object props, int risky)
20417 {
20418 int n = 0, field, prec;
20419 int literal = 0;
20420
20421 tail_recurse:
20422 if (depth > 100)
20423 elt = build_string ("*too-deep*");
20424
20425 depth++;
20426
20427 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20428 {
20429 case Lisp_String:
20430 {
20431 /* A string: output it and check for %-constructs within it. */
20432 unsigned char c;
20433 ptrdiff_t offset = 0;
20434
20435 if (SCHARS (elt) > 0
20436 && (!NILP (props) || risky))
20437 {
20438 Lisp_Object oprops, aelt;
20439 oprops = Ftext_properties_at (make_number (0), elt);
20440
20441 /* If the starting string's properties are not what
20442 we want, translate the string. Also, if the string
20443 is risky, do that anyway. */
20444
20445 if (NILP (Fequal (props, oprops)) || risky)
20446 {
20447 /* If the starting string has properties,
20448 merge the specified ones onto the existing ones. */
20449 if (! NILP (oprops) && !risky)
20450 {
20451 Lisp_Object tem;
20452
20453 oprops = Fcopy_sequence (oprops);
20454 tem = props;
20455 while (CONSP (tem))
20456 {
20457 oprops = Fplist_put (oprops, XCAR (tem),
20458 XCAR (XCDR (tem)));
20459 tem = XCDR (XCDR (tem));
20460 }
20461 props = oprops;
20462 }
20463
20464 aelt = Fassoc (elt, mode_line_proptrans_alist);
20465 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20466 {
20467 /* AELT is what we want. Move it to the front
20468 without consing. */
20469 elt = XCAR (aelt);
20470 mode_line_proptrans_alist
20471 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20472 }
20473 else
20474 {
20475 Lisp_Object tem;
20476
20477 /* If AELT has the wrong props, it is useless.
20478 so get rid of it. */
20479 if (! NILP (aelt))
20480 mode_line_proptrans_alist
20481 = Fdelq (aelt, mode_line_proptrans_alist);
20482
20483 elt = Fcopy_sequence (elt);
20484 Fset_text_properties (make_number (0), Flength (elt),
20485 props, elt);
20486 /* Add this item to mode_line_proptrans_alist. */
20487 mode_line_proptrans_alist
20488 = Fcons (Fcons (elt, props),
20489 mode_line_proptrans_alist);
20490 /* Truncate mode_line_proptrans_alist
20491 to at most 50 elements. */
20492 tem = Fnthcdr (make_number (50),
20493 mode_line_proptrans_alist);
20494 if (! NILP (tem))
20495 XSETCDR (tem, Qnil);
20496 }
20497 }
20498 }
20499
20500 offset = 0;
20501
20502 if (literal)
20503 {
20504 prec = precision - n;
20505 switch (mode_line_target)
20506 {
20507 case MODE_LINE_NOPROP:
20508 case MODE_LINE_TITLE:
20509 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20510 break;
20511 case MODE_LINE_STRING:
20512 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20513 break;
20514 case MODE_LINE_DISPLAY:
20515 n += display_string (NULL, elt, Qnil, 0, 0, it,
20516 0, prec, 0, STRING_MULTIBYTE (elt));
20517 break;
20518 }
20519
20520 break;
20521 }
20522
20523 /* Handle the non-literal case. */
20524
20525 while ((precision <= 0 || n < precision)
20526 && SREF (elt, offset) != 0
20527 && (mode_line_target != MODE_LINE_DISPLAY
20528 || it->current_x < it->last_visible_x))
20529 {
20530 ptrdiff_t last_offset = offset;
20531
20532 /* Advance to end of string or next format specifier. */
20533 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20534 ;
20535
20536 if (offset - 1 != last_offset)
20537 {
20538 ptrdiff_t nchars, nbytes;
20539
20540 /* Output to end of string or up to '%'. Field width
20541 is length of string. Don't output more than
20542 PRECISION allows us. */
20543 offset--;
20544
20545 prec = c_string_width (SDATA (elt) + last_offset,
20546 offset - last_offset, precision - n,
20547 &nchars, &nbytes);
20548
20549 switch (mode_line_target)
20550 {
20551 case MODE_LINE_NOPROP:
20552 case MODE_LINE_TITLE:
20553 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20554 break;
20555 case MODE_LINE_STRING:
20556 {
20557 ptrdiff_t bytepos = last_offset;
20558 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20559 ptrdiff_t endpos = (precision <= 0
20560 ? string_byte_to_char (elt, offset)
20561 : charpos + nchars);
20562
20563 n += store_mode_line_string (NULL,
20564 Fsubstring (elt, make_number (charpos),
20565 make_number (endpos)),
20566 0, 0, 0, Qnil);
20567 }
20568 break;
20569 case MODE_LINE_DISPLAY:
20570 {
20571 ptrdiff_t bytepos = last_offset;
20572 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20573
20574 if (precision <= 0)
20575 nchars = string_byte_to_char (elt, offset) - charpos;
20576 n += display_string (NULL, elt, Qnil, 0, charpos,
20577 it, 0, nchars, 0,
20578 STRING_MULTIBYTE (elt));
20579 }
20580 break;
20581 }
20582 }
20583 else /* c == '%' */
20584 {
20585 ptrdiff_t percent_position = offset;
20586
20587 /* Get the specified minimum width. Zero means
20588 don't pad. */
20589 field = 0;
20590 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20591 field = field * 10 + c - '0';
20592
20593 /* Don't pad beyond the total padding allowed. */
20594 if (field_width - n > 0 && field > field_width - n)
20595 field = field_width - n;
20596
20597 /* Note that either PRECISION <= 0 or N < PRECISION. */
20598 prec = precision - n;
20599
20600 if (c == 'M')
20601 n += display_mode_element (it, depth, field, prec,
20602 Vglobal_mode_string, props,
20603 risky);
20604 else if (c != 0)
20605 {
20606 int multibyte;
20607 ptrdiff_t bytepos, charpos;
20608 const char *spec;
20609 Lisp_Object string;
20610
20611 bytepos = percent_position;
20612 charpos = (STRING_MULTIBYTE (elt)
20613 ? string_byte_to_char (elt, bytepos)
20614 : bytepos);
20615 spec = decode_mode_spec (it->w, c, field, &string);
20616 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20617
20618 switch (mode_line_target)
20619 {
20620 case MODE_LINE_NOPROP:
20621 case MODE_LINE_TITLE:
20622 n += store_mode_line_noprop (spec, field, prec);
20623 break;
20624 case MODE_LINE_STRING:
20625 {
20626 Lisp_Object tem = build_string (spec);
20627 props = Ftext_properties_at (make_number (charpos), elt);
20628 /* Should only keep face property in props */
20629 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20630 }
20631 break;
20632 case MODE_LINE_DISPLAY:
20633 {
20634 int nglyphs_before, nwritten;
20635
20636 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20637 nwritten = display_string (spec, string, elt,
20638 charpos, 0, it,
20639 field, prec, 0,
20640 multibyte);
20641
20642 /* Assign to the glyphs written above the
20643 string where the `%x' came from, position
20644 of the `%'. */
20645 if (nwritten > 0)
20646 {
20647 struct glyph *glyph
20648 = (it->glyph_row->glyphs[TEXT_AREA]
20649 + nglyphs_before);
20650 int i;
20651
20652 for (i = 0; i < nwritten; ++i)
20653 {
20654 glyph[i].object = elt;
20655 glyph[i].charpos = charpos;
20656 }
20657
20658 n += nwritten;
20659 }
20660 }
20661 break;
20662 }
20663 }
20664 else /* c == 0 */
20665 break;
20666 }
20667 }
20668 }
20669 break;
20670
20671 case Lisp_Symbol:
20672 /* A symbol: process the value of the symbol recursively
20673 as if it appeared here directly. Avoid error if symbol void.
20674 Special case: if value of symbol is a string, output the string
20675 literally. */
20676 {
20677 register Lisp_Object tem;
20678
20679 /* If the variable is not marked as risky to set
20680 then its contents are risky to use. */
20681 if (NILP (Fget (elt, Qrisky_local_variable)))
20682 risky = 1;
20683
20684 tem = Fboundp (elt);
20685 if (!NILP (tem))
20686 {
20687 tem = Fsymbol_value (elt);
20688 /* If value is a string, output that string literally:
20689 don't check for % within it. */
20690 if (STRINGP (tem))
20691 literal = 1;
20692
20693 if (!EQ (tem, elt))
20694 {
20695 /* Give up right away for nil or t. */
20696 elt = tem;
20697 goto tail_recurse;
20698 }
20699 }
20700 }
20701 break;
20702
20703 case Lisp_Cons:
20704 {
20705 register Lisp_Object car, tem;
20706
20707 /* A cons cell: five distinct cases.
20708 If first element is :eval or :propertize, do something special.
20709 If first element is a string or a cons, process all the elements
20710 and effectively concatenate them.
20711 If first element is a negative number, truncate displaying cdr to
20712 at most that many characters. If positive, pad (with spaces)
20713 to at least that many characters.
20714 If first element is a symbol, process the cadr or caddr recursively
20715 according to whether the symbol's value is non-nil or nil. */
20716 car = XCAR (elt);
20717 if (EQ (car, QCeval))
20718 {
20719 /* An element of the form (:eval FORM) means evaluate FORM
20720 and use the result as mode line elements. */
20721
20722 if (risky)
20723 break;
20724
20725 if (CONSP (XCDR (elt)))
20726 {
20727 Lisp_Object spec;
20728 spec = safe_eval (XCAR (XCDR (elt)));
20729 n += display_mode_element (it, depth, field_width - n,
20730 precision - n, spec, props,
20731 risky);
20732 }
20733 }
20734 else if (EQ (car, QCpropertize))
20735 {
20736 /* An element of the form (:propertize ELT PROPS...)
20737 means display ELT but applying properties PROPS. */
20738
20739 if (risky)
20740 break;
20741
20742 if (CONSP (XCDR (elt)))
20743 n += display_mode_element (it, depth, field_width - n,
20744 precision - n, XCAR (XCDR (elt)),
20745 XCDR (XCDR (elt)), risky);
20746 }
20747 else if (SYMBOLP (car))
20748 {
20749 tem = Fboundp (car);
20750 elt = XCDR (elt);
20751 if (!CONSP (elt))
20752 goto invalid;
20753 /* elt is now the cdr, and we know it is a cons cell.
20754 Use its car if CAR has a non-nil value. */
20755 if (!NILP (tem))
20756 {
20757 tem = Fsymbol_value (car);
20758 if (!NILP (tem))
20759 {
20760 elt = XCAR (elt);
20761 goto tail_recurse;
20762 }
20763 }
20764 /* Symbol's value is nil (or symbol is unbound)
20765 Get the cddr of the original list
20766 and if possible find the caddr and use that. */
20767 elt = XCDR (elt);
20768 if (NILP (elt))
20769 break;
20770 else if (!CONSP (elt))
20771 goto invalid;
20772 elt = XCAR (elt);
20773 goto tail_recurse;
20774 }
20775 else if (INTEGERP (car))
20776 {
20777 register int lim = XINT (car);
20778 elt = XCDR (elt);
20779 if (lim < 0)
20780 {
20781 /* Negative int means reduce maximum width. */
20782 if (precision <= 0)
20783 precision = -lim;
20784 else
20785 precision = min (precision, -lim);
20786 }
20787 else if (lim > 0)
20788 {
20789 /* Padding specified. Don't let it be more than
20790 current maximum. */
20791 if (precision > 0)
20792 lim = min (precision, lim);
20793
20794 /* If that's more padding than already wanted, queue it.
20795 But don't reduce padding already specified even if
20796 that is beyond the current truncation point. */
20797 field_width = max (lim, field_width);
20798 }
20799 goto tail_recurse;
20800 }
20801 else if (STRINGP (car) || CONSP (car))
20802 {
20803 Lisp_Object halftail = elt;
20804 int len = 0;
20805
20806 while (CONSP (elt)
20807 && (precision <= 0 || n < precision))
20808 {
20809 n += display_mode_element (it, depth,
20810 /* Do padding only after the last
20811 element in the list. */
20812 (! CONSP (XCDR (elt))
20813 ? field_width - n
20814 : 0),
20815 precision - n, XCAR (elt),
20816 props, risky);
20817 elt = XCDR (elt);
20818 len++;
20819 if ((len & 1) == 0)
20820 halftail = XCDR (halftail);
20821 /* Check for cycle. */
20822 if (EQ (halftail, elt))
20823 break;
20824 }
20825 }
20826 }
20827 break;
20828
20829 default:
20830 invalid:
20831 elt = build_string ("*invalid*");
20832 goto tail_recurse;
20833 }
20834
20835 /* Pad to FIELD_WIDTH. */
20836 if (field_width > 0 && n < field_width)
20837 {
20838 switch (mode_line_target)
20839 {
20840 case MODE_LINE_NOPROP:
20841 case MODE_LINE_TITLE:
20842 n += store_mode_line_noprop ("", field_width - n, 0);
20843 break;
20844 case MODE_LINE_STRING:
20845 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20846 break;
20847 case MODE_LINE_DISPLAY:
20848 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20849 0, 0, 0);
20850 break;
20851 }
20852 }
20853
20854 return n;
20855 }
20856
20857 /* Store a mode-line string element in mode_line_string_list.
20858
20859 If STRING is non-null, display that C string. Otherwise, the Lisp
20860 string LISP_STRING is displayed.
20861
20862 FIELD_WIDTH is the minimum number of output glyphs to produce.
20863 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20864 with spaces. FIELD_WIDTH <= 0 means don't pad.
20865
20866 PRECISION is the maximum number of characters to output from
20867 STRING. PRECISION <= 0 means don't truncate the string.
20868
20869 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20870 properties to the string.
20871
20872 PROPS are the properties to add to the string.
20873 The mode_line_string_face face property is always added to the string.
20874 */
20875
20876 static int
20877 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20878 int field_width, int precision, Lisp_Object props)
20879 {
20880 ptrdiff_t len;
20881 int n = 0;
20882
20883 if (string != NULL)
20884 {
20885 len = strlen (string);
20886 if (precision > 0 && len > precision)
20887 len = precision;
20888 lisp_string = make_string (string, len);
20889 if (NILP (props))
20890 props = mode_line_string_face_prop;
20891 else if (!NILP (mode_line_string_face))
20892 {
20893 Lisp_Object face = Fplist_get (props, Qface);
20894 props = Fcopy_sequence (props);
20895 if (NILP (face))
20896 face = mode_line_string_face;
20897 else
20898 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20899 props = Fplist_put (props, Qface, face);
20900 }
20901 Fadd_text_properties (make_number (0), make_number (len),
20902 props, lisp_string);
20903 }
20904 else
20905 {
20906 len = XFASTINT (Flength (lisp_string));
20907 if (precision > 0 && len > precision)
20908 {
20909 len = precision;
20910 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20911 precision = -1;
20912 }
20913 if (!NILP (mode_line_string_face))
20914 {
20915 Lisp_Object face;
20916 if (NILP (props))
20917 props = Ftext_properties_at (make_number (0), lisp_string);
20918 face = Fplist_get (props, Qface);
20919 if (NILP (face))
20920 face = mode_line_string_face;
20921 else
20922 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20923 props = Fcons (Qface, Fcons (face, Qnil));
20924 if (copy_string)
20925 lisp_string = Fcopy_sequence (lisp_string);
20926 }
20927 if (!NILP (props))
20928 Fadd_text_properties (make_number (0), make_number (len),
20929 props, lisp_string);
20930 }
20931
20932 if (len > 0)
20933 {
20934 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20935 n += len;
20936 }
20937
20938 if (field_width > len)
20939 {
20940 field_width -= len;
20941 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20942 if (!NILP (props))
20943 Fadd_text_properties (make_number (0), make_number (field_width),
20944 props, lisp_string);
20945 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20946 n += field_width;
20947 }
20948
20949 return n;
20950 }
20951
20952
20953 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20954 1, 4, 0,
20955 doc: /* Format a string out of a mode line format specification.
20956 First arg FORMAT specifies the mode line format (see `mode-line-format'
20957 for details) to use.
20958
20959 By default, the format is evaluated for the currently selected window.
20960
20961 Optional second arg FACE specifies the face property to put on all
20962 characters for which no face is specified. The value nil means the
20963 default face. The value t means whatever face the window's mode line
20964 currently uses (either `mode-line' or `mode-line-inactive',
20965 depending on whether the window is the selected window or not).
20966 An integer value means the value string has no text
20967 properties.
20968
20969 Optional third and fourth args WINDOW and BUFFER specify the window
20970 and buffer to use as the context for the formatting (defaults
20971 are the selected window and the WINDOW's buffer). */)
20972 (Lisp_Object format, Lisp_Object face,
20973 Lisp_Object window, Lisp_Object buffer)
20974 {
20975 struct it it;
20976 int len;
20977 struct window *w;
20978 struct buffer *old_buffer = NULL;
20979 int face_id;
20980 int no_props = INTEGERP (face);
20981 ptrdiff_t count = SPECPDL_INDEX ();
20982 Lisp_Object str;
20983 int string_start = 0;
20984
20985 if (NILP (window))
20986 window = selected_window;
20987 CHECK_WINDOW (window);
20988 w = XWINDOW (window);
20989
20990 if (NILP (buffer))
20991 buffer = w->buffer;
20992 CHECK_BUFFER (buffer);
20993
20994 /* Make formatting the modeline a non-op when noninteractive, otherwise
20995 there will be problems later caused by a partially initialized frame. */
20996 if (NILP (format) || noninteractive)
20997 return empty_unibyte_string;
20998
20999 if (no_props)
21000 face = Qnil;
21001
21002 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21003 : EQ (face, Qt) ? (EQ (window, selected_window)
21004 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21005 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21006 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21007 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21008 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21009 : DEFAULT_FACE_ID;
21010
21011 if (XBUFFER (buffer) != current_buffer)
21012 old_buffer = current_buffer;
21013
21014 /* Save things including mode_line_proptrans_alist,
21015 and set that to nil so that we don't alter the outer value. */
21016 record_unwind_protect (unwind_format_mode_line,
21017 format_mode_line_unwind_data
21018 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21019 old_buffer, selected_window, 1));
21020 mode_line_proptrans_alist = Qnil;
21021
21022 Fselect_window (window, Qt);
21023 if (old_buffer)
21024 set_buffer_internal_1 (XBUFFER (buffer));
21025
21026 init_iterator (&it, w, -1, -1, NULL, face_id);
21027
21028 if (no_props)
21029 {
21030 mode_line_target = MODE_LINE_NOPROP;
21031 mode_line_string_face_prop = Qnil;
21032 mode_line_string_list = Qnil;
21033 string_start = MODE_LINE_NOPROP_LEN (0);
21034 }
21035 else
21036 {
21037 mode_line_target = MODE_LINE_STRING;
21038 mode_line_string_list = Qnil;
21039 mode_line_string_face = face;
21040 mode_line_string_face_prop
21041 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21042 }
21043
21044 push_kboard (FRAME_KBOARD (it.f));
21045 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21046 pop_kboard ();
21047
21048 if (no_props)
21049 {
21050 len = MODE_LINE_NOPROP_LEN (string_start);
21051 str = make_string (mode_line_noprop_buf + string_start, len);
21052 }
21053 else
21054 {
21055 mode_line_string_list = Fnreverse (mode_line_string_list);
21056 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21057 empty_unibyte_string);
21058 }
21059
21060 unbind_to (count, Qnil);
21061 return str;
21062 }
21063
21064 /* Write a null-terminated, right justified decimal representation of
21065 the positive integer D to BUF using a minimal field width WIDTH. */
21066
21067 static void
21068 pint2str (register char *buf, register int width, register ptrdiff_t d)
21069 {
21070 register char *p = buf;
21071
21072 if (d <= 0)
21073 *p++ = '0';
21074 else
21075 {
21076 while (d > 0)
21077 {
21078 *p++ = d % 10 + '0';
21079 d /= 10;
21080 }
21081 }
21082
21083 for (width -= (int) (p - buf); width > 0; --width)
21084 *p++ = ' ';
21085 *p-- = '\0';
21086 while (p > buf)
21087 {
21088 d = *buf;
21089 *buf++ = *p;
21090 *p-- = d;
21091 }
21092 }
21093
21094 /* Write a null-terminated, right justified decimal and "human
21095 readable" representation of the nonnegative integer D to BUF using
21096 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21097
21098 static const char power_letter[] =
21099 {
21100 0, /* no letter */
21101 'k', /* kilo */
21102 'M', /* mega */
21103 'G', /* giga */
21104 'T', /* tera */
21105 'P', /* peta */
21106 'E', /* exa */
21107 'Z', /* zetta */
21108 'Y' /* yotta */
21109 };
21110
21111 static void
21112 pint2hrstr (char *buf, int width, ptrdiff_t d)
21113 {
21114 /* We aim to represent the nonnegative integer D as
21115 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21116 ptrdiff_t quotient = d;
21117 int remainder = 0;
21118 /* -1 means: do not use TENTHS. */
21119 int tenths = -1;
21120 int exponent = 0;
21121
21122 /* Length of QUOTIENT.TENTHS as a string. */
21123 int length;
21124
21125 char * psuffix;
21126 char * p;
21127
21128 if (1000 <= quotient)
21129 {
21130 /* Scale to the appropriate EXPONENT. */
21131 do
21132 {
21133 remainder = quotient % 1000;
21134 quotient /= 1000;
21135 exponent++;
21136 }
21137 while (1000 <= quotient);
21138
21139 /* Round to nearest and decide whether to use TENTHS or not. */
21140 if (quotient <= 9)
21141 {
21142 tenths = remainder / 100;
21143 if (50 <= remainder % 100)
21144 {
21145 if (tenths < 9)
21146 tenths++;
21147 else
21148 {
21149 quotient++;
21150 if (quotient == 10)
21151 tenths = -1;
21152 else
21153 tenths = 0;
21154 }
21155 }
21156 }
21157 else
21158 if (500 <= remainder)
21159 {
21160 if (quotient < 999)
21161 quotient++;
21162 else
21163 {
21164 quotient = 1;
21165 exponent++;
21166 tenths = 0;
21167 }
21168 }
21169 }
21170
21171 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21172 if (tenths == -1 && quotient <= 99)
21173 if (quotient <= 9)
21174 length = 1;
21175 else
21176 length = 2;
21177 else
21178 length = 3;
21179 p = psuffix = buf + max (width, length);
21180
21181 /* Print EXPONENT. */
21182 *psuffix++ = power_letter[exponent];
21183 *psuffix = '\0';
21184
21185 /* Print TENTHS. */
21186 if (tenths >= 0)
21187 {
21188 *--p = '0' + tenths;
21189 *--p = '.';
21190 }
21191
21192 /* Print QUOTIENT. */
21193 do
21194 {
21195 int digit = quotient % 10;
21196 *--p = '0' + digit;
21197 }
21198 while ((quotient /= 10) != 0);
21199
21200 /* Print leading spaces. */
21201 while (buf < p)
21202 *--p = ' ';
21203 }
21204
21205 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21206 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21207 type of CODING_SYSTEM. Return updated pointer into BUF. */
21208
21209 static unsigned char invalid_eol_type[] = "(*invalid*)";
21210
21211 static char *
21212 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21213 {
21214 Lisp_Object val;
21215 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21216 const unsigned char *eol_str;
21217 int eol_str_len;
21218 /* The EOL conversion we are using. */
21219 Lisp_Object eoltype;
21220
21221 val = CODING_SYSTEM_SPEC (coding_system);
21222 eoltype = Qnil;
21223
21224 if (!VECTORP (val)) /* Not yet decided. */
21225 {
21226 *buf++ = multibyte ? '-' : ' ';
21227 if (eol_flag)
21228 eoltype = eol_mnemonic_undecided;
21229 /* Don't mention EOL conversion if it isn't decided. */
21230 }
21231 else
21232 {
21233 Lisp_Object attrs;
21234 Lisp_Object eolvalue;
21235
21236 attrs = AREF (val, 0);
21237 eolvalue = AREF (val, 2);
21238
21239 *buf++ = multibyte
21240 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21241 : ' ';
21242
21243 if (eol_flag)
21244 {
21245 /* The EOL conversion that is normal on this system. */
21246
21247 if (NILP (eolvalue)) /* Not yet decided. */
21248 eoltype = eol_mnemonic_undecided;
21249 else if (VECTORP (eolvalue)) /* Not yet decided. */
21250 eoltype = eol_mnemonic_undecided;
21251 else /* eolvalue is Qunix, Qdos, or Qmac. */
21252 eoltype = (EQ (eolvalue, Qunix)
21253 ? eol_mnemonic_unix
21254 : (EQ (eolvalue, Qdos) == 1
21255 ? eol_mnemonic_dos : eol_mnemonic_mac));
21256 }
21257 }
21258
21259 if (eol_flag)
21260 {
21261 /* Mention the EOL conversion if it is not the usual one. */
21262 if (STRINGP (eoltype))
21263 {
21264 eol_str = SDATA (eoltype);
21265 eol_str_len = SBYTES (eoltype);
21266 }
21267 else if (CHARACTERP (eoltype))
21268 {
21269 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21270 int c = XFASTINT (eoltype);
21271 eol_str_len = CHAR_STRING (c, tmp);
21272 eol_str = tmp;
21273 }
21274 else
21275 {
21276 eol_str = invalid_eol_type;
21277 eol_str_len = sizeof (invalid_eol_type) - 1;
21278 }
21279 memcpy (buf, eol_str, eol_str_len);
21280 buf += eol_str_len;
21281 }
21282
21283 return buf;
21284 }
21285
21286 /* Return a string for the output of a mode line %-spec for window W,
21287 generated by character C. FIELD_WIDTH > 0 means pad the string
21288 returned with spaces to that value. Return a Lisp string in
21289 *STRING if the resulting string is taken from that Lisp string.
21290
21291 Note we operate on the current buffer for most purposes,
21292 the exception being w->base_line_pos. */
21293
21294 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21295
21296 static const char *
21297 decode_mode_spec (struct window *w, register int c, int field_width,
21298 Lisp_Object *string)
21299 {
21300 Lisp_Object obj;
21301 struct frame *f = XFRAME (WINDOW_FRAME (w));
21302 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21303 struct buffer *b = current_buffer;
21304
21305 obj = Qnil;
21306 *string = Qnil;
21307
21308 switch (c)
21309 {
21310 case '*':
21311 if (!NILP (BVAR (b, read_only)))
21312 return "%";
21313 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21314 return "*";
21315 return "-";
21316
21317 case '+':
21318 /* This differs from %* only for a modified read-only buffer. */
21319 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21320 return "*";
21321 if (!NILP (BVAR (b, read_only)))
21322 return "%";
21323 return "-";
21324
21325 case '&':
21326 /* This differs from %* in ignoring read-only-ness. */
21327 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21328 return "*";
21329 return "-";
21330
21331 case '%':
21332 return "%";
21333
21334 case '[':
21335 {
21336 int i;
21337 char *p;
21338
21339 if (command_loop_level > 5)
21340 return "[[[... ";
21341 p = decode_mode_spec_buf;
21342 for (i = 0; i < command_loop_level; i++)
21343 *p++ = '[';
21344 *p = 0;
21345 return decode_mode_spec_buf;
21346 }
21347
21348 case ']':
21349 {
21350 int i;
21351 char *p;
21352
21353 if (command_loop_level > 5)
21354 return " ...]]]";
21355 p = decode_mode_spec_buf;
21356 for (i = 0; i < command_loop_level; i++)
21357 *p++ = ']';
21358 *p = 0;
21359 return decode_mode_spec_buf;
21360 }
21361
21362 case '-':
21363 {
21364 register int i;
21365
21366 /* Let lots_of_dashes be a string of infinite length. */
21367 if (mode_line_target == MODE_LINE_NOPROP ||
21368 mode_line_target == MODE_LINE_STRING)
21369 return "--";
21370 if (field_width <= 0
21371 || field_width > sizeof (lots_of_dashes))
21372 {
21373 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21374 decode_mode_spec_buf[i] = '-';
21375 decode_mode_spec_buf[i] = '\0';
21376 return decode_mode_spec_buf;
21377 }
21378 else
21379 return lots_of_dashes;
21380 }
21381
21382 case 'b':
21383 obj = BVAR (b, name);
21384 break;
21385
21386 case 'c':
21387 /* %c and %l are ignored in `frame-title-format'.
21388 (In redisplay_internal, the frame title is drawn _before_ the
21389 windows are updated, so the stuff which depends on actual
21390 window contents (such as %l) may fail to render properly, or
21391 even crash emacs.) */
21392 if (mode_line_target == MODE_LINE_TITLE)
21393 return "";
21394 else
21395 {
21396 ptrdiff_t col = current_column ();
21397 w->column_number_displayed = make_number (col);
21398 pint2str (decode_mode_spec_buf, field_width, col);
21399 return decode_mode_spec_buf;
21400 }
21401
21402 case 'e':
21403 #ifndef SYSTEM_MALLOC
21404 {
21405 if (NILP (Vmemory_full))
21406 return "";
21407 else
21408 return "!MEM FULL! ";
21409 }
21410 #else
21411 return "";
21412 #endif
21413
21414 case 'F':
21415 /* %F displays the frame name. */
21416 if (!NILP (f->title))
21417 return SSDATA (f->title);
21418 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21419 return SSDATA (f->name);
21420 return "Emacs";
21421
21422 case 'f':
21423 obj = BVAR (b, filename);
21424 break;
21425
21426 case 'i':
21427 {
21428 ptrdiff_t size = ZV - BEGV;
21429 pint2str (decode_mode_spec_buf, field_width, size);
21430 return decode_mode_spec_buf;
21431 }
21432
21433 case 'I':
21434 {
21435 ptrdiff_t size = ZV - BEGV;
21436 pint2hrstr (decode_mode_spec_buf, field_width, size);
21437 return decode_mode_spec_buf;
21438 }
21439
21440 case 'l':
21441 {
21442 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21443 ptrdiff_t topline, nlines, height;
21444 ptrdiff_t junk;
21445
21446 /* %c and %l are ignored in `frame-title-format'. */
21447 if (mode_line_target == MODE_LINE_TITLE)
21448 return "";
21449
21450 startpos = XMARKER (w->start)->charpos;
21451 startpos_byte = marker_byte_position (w->start);
21452 height = WINDOW_TOTAL_LINES (w);
21453
21454 /* If we decided that this buffer isn't suitable for line numbers,
21455 don't forget that too fast. */
21456 if (EQ (w->base_line_pos, w->buffer))
21457 goto no_value;
21458 /* But do forget it, if the window shows a different buffer now. */
21459 else if (BUFFERP (w->base_line_pos))
21460 w->base_line_pos = Qnil;
21461
21462 /* If the buffer is very big, don't waste time. */
21463 if (INTEGERP (Vline_number_display_limit)
21464 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21465 {
21466 w->base_line_pos = Qnil;
21467 w->base_line_number = Qnil;
21468 goto no_value;
21469 }
21470
21471 if (INTEGERP (w->base_line_number)
21472 && INTEGERP (w->base_line_pos)
21473 && XFASTINT (w->base_line_pos) <= startpos)
21474 {
21475 line = XFASTINT (w->base_line_number);
21476 linepos = XFASTINT (w->base_line_pos);
21477 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21478 }
21479 else
21480 {
21481 line = 1;
21482 linepos = BUF_BEGV (b);
21483 linepos_byte = BUF_BEGV_BYTE (b);
21484 }
21485
21486 /* Count lines from base line to window start position. */
21487 nlines = display_count_lines (linepos_byte,
21488 startpos_byte,
21489 startpos, &junk);
21490
21491 topline = nlines + line;
21492
21493 /* Determine a new base line, if the old one is too close
21494 or too far away, or if we did not have one.
21495 "Too close" means it's plausible a scroll-down would
21496 go back past it. */
21497 if (startpos == BUF_BEGV (b))
21498 {
21499 w->base_line_number = make_number (topline);
21500 w->base_line_pos = make_number (BUF_BEGV (b));
21501 }
21502 else if (nlines < height + 25 || nlines > height * 3 + 50
21503 || linepos == BUF_BEGV (b))
21504 {
21505 ptrdiff_t limit = BUF_BEGV (b);
21506 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21507 ptrdiff_t position;
21508 ptrdiff_t distance =
21509 (height * 2 + 30) * line_number_display_limit_width;
21510
21511 if (startpos - distance > limit)
21512 {
21513 limit = startpos - distance;
21514 limit_byte = CHAR_TO_BYTE (limit);
21515 }
21516
21517 nlines = display_count_lines (startpos_byte,
21518 limit_byte,
21519 - (height * 2 + 30),
21520 &position);
21521 /* If we couldn't find the lines we wanted within
21522 line_number_display_limit_width chars per line,
21523 give up on line numbers for this window. */
21524 if (position == limit_byte && limit == startpos - distance)
21525 {
21526 w->base_line_pos = w->buffer;
21527 w->base_line_number = Qnil;
21528 goto no_value;
21529 }
21530
21531 w->base_line_number = make_number (topline - nlines);
21532 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21533 }
21534
21535 /* Now count lines from the start pos to point. */
21536 nlines = display_count_lines (startpos_byte,
21537 PT_BYTE, PT, &junk);
21538
21539 /* Record that we did display the line number. */
21540 line_number_displayed = 1;
21541
21542 /* Make the string to show. */
21543 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21544 return decode_mode_spec_buf;
21545 no_value:
21546 {
21547 char* p = decode_mode_spec_buf;
21548 int pad = field_width - 2;
21549 while (pad-- > 0)
21550 *p++ = ' ';
21551 *p++ = '?';
21552 *p++ = '?';
21553 *p = '\0';
21554 return decode_mode_spec_buf;
21555 }
21556 }
21557 break;
21558
21559 case 'm':
21560 obj = BVAR (b, mode_name);
21561 break;
21562
21563 case 'n':
21564 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21565 return " Narrow";
21566 break;
21567
21568 case 'p':
21569 {
21570 ptrdiff_t pos = marker_position (w->start);
21571 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21572
21573 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21574 {
21575 if (pos <= BUF_BEGV (b))
21576 return "All";
21577 else
21578 return "Bottom";
21579 }
21580 else if (pos <= BUF_BEGV (b))
21581 return "Top";
21582 else
21583 {
21584 if (total > 1000000)
21585 /* Do it differently for a large value, to avoid overflow. */
21586 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21587 else
21588 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21589 /* We can't normally display a 3-digit number,
21590 so get us a 2-digit number that is close. */
21591 if (total == 100)
21592 total = 99;
21593 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21594 return decode_mode_spec_buf;
21595 }
21596 }
21597
21598 /* Display percentage of size above the bottom of the screen. */
21599 case 'P':
21600 {
21601 ptrdiff_t toppos = marker_position (w->start);
21602 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21603 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21604
21605 if (botpos >= BUF_ZV (b))
21606 {
21607 if (toppos <= BUF_BEGV (b))
21608 return "All";
21609 else
21610 return "Bottom";
21611 }
21612 else
21613 {
21614 if (total > 1000000)
21615 /* Do it differently for a large value, to avoid overflow. */
21616 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21617 else
21618 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21619 /* We can't normally display a 3-digit number,
21620 so get us a 2-digit number that is close. */
21621 if (total == 100)
21622 total = 99;
21623 if (toppos <= BUF_BEGV (b))
21624 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21625 else
21626 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21627 return decode_mode_spec_buf;
21628 }
21629 }
21630
21631 case 's':
21632 /* status of process */
21633 obj = Fget_buffer_process (Fcurrent_buffer ());
21634 if (NILP (obj))
21635 return "no process";
21636 #ifndef MSDOS
21637 obj = Fsymbol_name (Fprocess_status (obj));
21638 #endif
21639 break;
21640
21641 case '@':
21642 {
21643 ptrdiff_t count = inhibit_garbage_collection ();
21644 Lisp_Object val = call1 (intern ("file-remote-p"),
21645 BVAR (current_buffer, directory));
21646 unbind_to (count, Qnil);
21647
21648 if (NILP (val))
21649 return "-";
21650 else
21651 return "@";
21652 }
21653
21654 case 't': /* indicate TEXT or BINARY */
21655 return "T";
21656
21657 case 'z':
21658 /* coding-system (not including end-of-line format) */
21659 case 'Z':
21660 /* coding-system (including end-of-line type) */
21661 {
21662 int eol_flag = (c == 'Z');
21663 char *p = decode_mode_spec_buf;
21664
21665 if (! FRAME_WINDOW_P (f))
21666 {
21667 /* No need to mention EOL here--the terminal never needs
21668 to do EOL conversion. */
21669 p = decode_mode_spec_coding (CODING_ID_NAME
21670 (FRAME_KEYBOARD_CODING (f)->id),
21671 p, 0);
21672 p = decode_mode_spec_coding (CODING_ID_NAME
21673 (FRAME_TERMINAL_CODING (f)->id),
21674 p, 0);
21675 }
21676 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21677 p, eol_flag);
21678
21679 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21680 #ifdef subprocesses
21681 obj = Fget_buffer_process (Fcurrent_buffer ());
21682 if (PROCESSP (obj))
21683 {
21684 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21685 p, eol_flag);
21686 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21687 p, eol_flag);
21688 }
21689 #endif /* subprocesses */
21690 #endif /* 0 */
21691 *p = 0;
21692 return decode_mode_spec_buf;
21693 }
21694 }
21695
21696 if (STRINGP (obj))
21697 {
21698 *string = obj;
21699 return SSDATA (obj);
21700 }
21701 else
21702 return "";
21703 }
21704
21705
21706 /* Count up to COUNT lines starting from START_BYTE.
21707 But don't go beyond LIMIT_BYTE.
21708 Return the number of lines thus found (always nonnegative).
21709
21710 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21711
21712 static ptrdiff_t
21713 display_count_lines (ptrdiff_t start_byte,
21714 ptrdiff_t limit_byte, ptrdiff_t count,
21715 ptrdiff_t *byte_pos_ptr)
21716 {
21717 register unsigned char *cursor;
21718 unsigned char *base;
21719
21720 register ptrdiff_t ceiling;
21721 register unsigned char *ceiling_addr;
21722 ptrdiff_t orig_count = count;
21723
21724 /* If we are not in selective display mode,
21725 check only for newlines. */
21726 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21727 && !INTEGERP (BVAR (current_buffer, selective_display)));
21728
21729 if (count > 0)
21730 {
21731 while (start_byte < limit_byte)
21732 {
21733 ceiling = BUFFER_CEILING_OF (start_byte);
21734 ceiling = min (limit_byte - 1, ceiling);
21735 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21736 base = (cursor = BYTE_POS_ADDR (start_byte));
21737 while (1)
21738 {
21739 if (selective_display)
21740 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21741 ;
21742 else
21743 while (*cursor != '\n' && ++cursor != ceiling_addr)
21744 ;
21745
21746 if (cursor != ceiling_addr)
21747 {
21748 if (--count == 0)
21749 {
21750 start_byte += cursor - base + 1;
21751 *byte_pos_ptr = start_byte;
21752 return orig_count;
21753 }
21754 else
21755 if (++cursor == ceiling_addr)
21756 break;
21757 }
21758 else
21759 break;
21760 }
21761 start_byte += cursor - base;
21762 }
21763 }
21764 else
21765 {
21766 while (start_byte > limit_byte)
21767 {
21768 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21769 ceiling = max (limit_byte, ceiling);
21770 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21771 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21772 while (1)
21773 {
21774 if (selective_display)
21775 while (--cursor != ceiling_addr
21776 && *cursor != '\n' && *cursor != 015)
21777 ;
21778 else
21779 while (--cursor != ceiling_addr && *cursor != '\n')
21780 ;
21781
21782 if (cursor != ceiling_addr)
21783 {
21784 if (++count == 0)
21785 {
21786 start_byte += cursor - base + 1;
21787 *byte_pos_ptr = start_byte;
21788 /* When scanning backwards, we should
21789 not count the newline posterior to which we stop. */
21790 return - orig_count - 1;
21791 }
21792 }
21793 else
21794 break;
21795 }
21796 /* Here we add 1 to compensate for the last decrement
21797 of CURSOR, which took it past the valid range. */
21798 start_byte += cursor - base + 1;
21799 }
21800 }
21801
21802 *byte_pos_ptr = limit_byte;
21803
21804 if (count < 0)
21805 return - orig_count + count;
21806 return orig_count - count;
21807
21808 }
21809
21810
21811 \f
21812 /***********************************************************************
21813 Displaying strings
21814 ***********************************************************************/
21815
21816 /* Display a NUL-terminated string, starting with index START.
21817
21818 If STRING is non-null, display that C string. Otherwise, the Lisp
21819 string LISP_STRING is displayed. There's a case that STRING is
21820 non-null and LISP_STRING is not nil. It means STRING is a string
21821 data of LISP_STRING. In that case, we display LISP_STRING while
21822 ignoring its text properties.
21823
21824 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21825 FACE_STRING. Display STRING or LISP_STRING with the face at
21826 FACE_STRING_POS in FACE_STRING:
21827
21828 Display the string in the environment given by IT, but use the
21829 standard display table, temporarily.
21830
21831 FIELD_WIDTH is the minimum number of output glyphs to produce.
21832 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21833 with spaces. If STRING has more characters, more than FIELD_WIDTH
21834 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21835
21836 PRECISION is the maximum number of characters to output from
21837 STRING. PRECISION < 0 means don't truncate the string.
21838
21839 This is roughly equivalent to printf format specifiers:
21840
21841 FIELD_WIDTH PRECISION PRINTF
21842 ----------------------------------------
21843 -1 -1 %s
21844 -1 10 %.10s
21845 10 -1 %10s
21846 20 10 %20.10s
21847
21848 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21849 display them, and < 0 means obey the current buffer's value of
21850 enable_multibyte_characters.
21851
21852 Value is the number of columns displayed. */
21853
21854 static int
21855 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21856 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21857 int field_width, int precision, int max_x, int multibyte)
21858 {
21859 int hpos_at_start = it->hpos;
21860 int saved_face_id = it->face_id;
21861 struct glyph_row *row = it->glyph_row;
21862 ptrdiff_t it_charpos;
21863
21864 /* Initialize the iterator IT for iteration over STRING beginning
21865 with index START. */
21866 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21867 precision, field_width, multibyte);
21868 if (string && STRINGP (lisp_string))
21869 /* LISP_STRING is the one returned by decode_mode_spec. We should
21870 ignore its text properties. */
21871 it->stop_charpos = it->end_charpos;
21872
21873 /* If displaying STRING, set up the face of the iterator from
21874 FACE_STRING, if that's given. */
21875 if (STRINGP (face_string))
21876 {
21877 ptrdiff_t endptr;
21878 struct face *face;
21879
21880 it->face_id
21881 = face_at_string_position (it->w, face_string, face_string_pos,
21882 0, it->region_beg_charpos,
21883 it->region_end_charpos,
21884 &endptr, it->base_face_id, 0);
21885 face = FACE_FROM_ID (it->f, it->face_id);
21886 it->face_box_p = face->box != FACE_NO_BOX;
21887 }
21888
21889 /* Set max_x to the maximum allowed X position. Don't let it go
21890 beyond the right edge of the window. */
21891 if (max_x <= 0)
21892 max_x = it->last_visible_x;
21893 else
21894 max_x = min (max_x, it->last_visible_x);
21895
21896 /* Skip over display elements that are not visible. because IT->w is
21897 hscrolled. */
21898 if (it->current_x < it->first_visible_x)
21899 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21900 MOVE_TO_POS | MOVE_TO_X);
21901
21902 row->ascent = it->max_ascent;
21903 row->height = it->max_ascent + it->max_descent;
21904 row->phys_ascent = it->max_phys_ascent;
21905 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21906 row->extra_line_spacing = it->max_extra_line_spacing;
21907
21908 if (STRINGP (it->string))
21909 it_charpos = IT_STRING_CHARPOS (*it);
21910 else
21911 it_charpos = IT_CHARPOS (*it);
21912
21913 /* This condition is for the case that we are called with current_x
21914 past last_visible_x. */
21915 while (it->current_x < max_x)
21916 {
21917 int x_before, x, n_glyphs_before, i, nglyphs;
21918
21919 /* Get the next display element. */
21920 if (!get_next_display_element (it))
21921 break;
21922
21923 /* Produce glyphs. */
21924 x_before = it->current_x;
21925 n_glyphs_before = row->used[TEXT_AREA];
21926 PRODUCE_GLYPHS (it);
21927
21928 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21929 i = 0;
21930 x = x_before;
21931 while (i < nglyphs)
21932 {
21933 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21934
21935 if (it->line_wrap != TRUNCATE
21936 && x + glyph->pixel_width > max_x)
21937 {
21938 /* End of continued line or max_x reached. */
21939 if (CHAR_GLYPH_PADDING_P (*glyph))
21940 {
21941 /* A wide character is unbreakable. */
21942 if (row->reversed_p)
21943 unproduce_glyphs (it, row->used[TEXT_AREA]
21944 - n_glyphs_before);
21945 row->used[TEXT_AREA] = n_glyphs_before;
21946 it->current_x = x_before;
21947 }
21948 else
21949 {
21950 if (row->reversed_p)
21951 unproduce_glyphs (it, row->used[TEXT_AREA]
21952 - (n_glyphs_before + i));
21953 row->used[TEXT_AREA] = n_glyphs_before + i;
21954 it->current_x = x;
21955 }
21956 break;
21957 }
21958 else if (x + glyph->pixel_width >= it->first_visible_x)
21959 {
21960 /* Glyph is at least partially visible. */
21961 ++it->hpos;
21962 if (x < it->first_visible_x)
21963 row->x = x - it->first_visible_x;
21964 }
21965 else
21966 {
21967 /* Glyph is off the left margin of the display area.
21968 Should not happen. */
21969 abort ();
21970 }
21971
21972 row->ascent = max (row->ascent, it->max_ascent);
21973 row->height = max (row->height, it->max_ascent + it->max_descent);
21974 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21975 row->phys_height = max (row->phys_height,
21976 it->max_phys_ascent + it->max_phys_descent);
21977 row->extra_line_spacing = max (row->extra_line_spacing,
21978 it->max_extra_line_spacing);
21979 x += glyph->pixel_width;
21980 ++i;
21981 }
21982
21983 /* Stop if max_x reached. */
21984 if (i < nglyphs)
21985 break;
21986
21987 /* Stop at line ends. */
21988 if (ITERATOR_AT_END_OF_LINE_P (it))
21989 {
21990 it->continuation_lines_width = 0;
21991 break;
21992 }
21993
21994 set_iterator_to_next (it, 1);
21995 if (STRINGP (it->string))
21996 it_charpos = IT_STRING_CHARPOS (*it);
21997 else
21998 it_charpos = IT_CHARPOS (*it);
21999
22000 /* Stop if truncating at the right edge. */
22001 if (it->line_wrap == TRUNCATE
22002 && it->current_x >= it->last_visible_x)
22003 {
22004 /* Add truncation mark, but don't do it if the line is
22005 truncated at a padding space. */
22006 if (it_charpos < it->string_nchars)
22007 {
22008 if (!FRAME_WINDOW_P (it->f))
22009 {
22010 int ii, n;
22011
22012 if (it->current_x > it->last_visible_x)
22013 {
22014 if (!row->reversed_p)
22015 {
22016 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22017 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22018 break;
22019 }
22020 else
22021 {
22022 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22023 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22024 break;
22025 unproduce_glyphs (it, ii + 1);
22026 ii = row->used[TEXT_AREA] - (ii + 1);
22027 }
22028 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22029 {
22030 row->used[TEXT_AREA] = ii;
22031 produce_special_glyphs (it, IT_TRUNCATION);
22032 }
22033 }
22034 produce_special_glyphs (it, IT_TRUNCATION);
22035 }
22036 row->truncated_on_right_p = 1;
22037 }
22038 break;
22039 }
22040 }
22041
22042 /* Maybe insert a truncation at the left. */
22043 if (it->first_visible_x
22044 && it_charpos > 0)
22045 {
22046 if (!FRAME_WINDOW_P (it->f)
22047 || (row->reversed_p
22048 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22049 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22050 insert_left_trunc_glyphs (it);
22051 row->truncated_on_left_p = 1;
22052 }
22053
22054 it->face_id = saved_face_id;
22055
22056 /* Value is number of columns displayed. */
22057 return it->hpos - hpos_at_start;
22058 }
22059
22060
22061 \f
22062 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22063 appears as an element of LIST or as the car of an element of LIST.
22064 If PROPVAL is a list, compare each element against LIST in that
22065 way, and return 1/2 if any element of PROPVAL is found in LIST.
22066 Otherwise return 0. This function cannot quit.
22067 The return value is 2 if the text is invisible but with an ellipsis
22068 and 1 if it's invisible and without an ellipsis. */
22069
22070 int
22071 invisible_p (register Lisp_Object propval, Lisp_Object list)
22072 {
22073 register Lisp_Object tail, proptail;
22074
22075 for (tail = list; CONSP (tail); tail = XCDR (tail))
22076 {
22077 register Lisp_Object tem;
22078 tem = XCAR (tail);
22079 if (EQ (propval, tem))
22080 return 1;
22081 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22082 return NILP (XCDR (tem)) ? 1 : 2;
22083 }
22084
22085 if (CONSP (propval))
22086 {
22087 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22088 {
22089 Lisp_Object propelt;
22090 propelt = XCAR (proptail);
22091 for (tail = list; CONSP (tail); tail = XCDR (tail))
22092 {
22093 register Lisp_Object tem;
22094 tem = XCAR (tail);
22095 if (EQ (propelt, tem))
22096 return 1;
22097 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22098 return NILP (XCDR (tem)) ? 1 : 2;
22099 }
22100 }
22101 }
22102
22103 return 0;
22104 }
22105
22106 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22107 doc: /* Non-nil if the property makes the text invisible.
22108 POS-OR-PROP can be a marker or number, in which case it is taken to be
22109 a position in the current buffer and the value of the `invisible' property
22110 is checked; or it can be some other value, which is then presumed to be the
22111 value of the `invisible' property of the text of interest.
22112 The non-nil value returned can be t for truly invisible text or something
22113 else if the text is replaced by an ellipsis. */)
22114 (Lisp_Object pos_or_prop)
22115 {
22116 Lisp_Object prop
22117 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22118 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22119 : pos_or_prop);
22120 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22121 return (invis == 0 ? Qnil
22122 : invis == 1 ? Qt
22123 : make_number (invis));
22124 }
22125
22126 /* Calculate a width or height in pixels from a specification using
22127 the following elements:
22128
22129 SPEC ::=
22130 NUM - a (fractional) multiple of the default font width/height
22131 (NUM) - specifies exactly NUM pixels
22132 UNIT - a fixed number of pixels, see below.
22133 ELEMENT - size of a display element in pixels, see below.
22134 (NUM . SPEC) - equals NUM * SPEC
22135 (+ SPEC SPEC ...) - add pixel values
22136 (- SPEC SPEC ...) - subtract pixel values
22137 (- SPEC) - negate pixel value
22138
22139 NUM ::=
22140 INT or FLOAT - a number constant
22141 SYMBOL - use symbol's (buffer local) variable binding.
22142
22143 UNIT ::=
22144 in - pixels per inch *)
22145 mm - pixels per 1/1000 meter *)
22146 cm - pixels per 1/100 meter *)
22147 width - width of current font in pixels.
22148 height - height of current font in pixels.
22149
22150 *) using the ratio(s) defined in display-pixels-per-inch.
22151
22152 ELEMENT ::=
22153
22154 left-fringe - left fringe width in pixels
22155 right-fringe - right fringe width in pixels
22156
22157 left-margin - left margin width in pixels
22158 right-margin - right margin width in pixels
22159
22160 scroll-bar - scroll-bar area width in pixels
22161
22162 Examples:
22163
22164 Pixels corresponding to 5 inches:
22165 (5 . in)
22166
22167 Total width of non-text areas on left side of window (if scroll-bar is on left):
22168 '(space :width (+ left-fringe left-margin scroll-bar))
22169
22170 Align to first text column (in header line):
22171 '(space :align-to 0)
22172
22173 Align to middle of text area minus half the width of variable `my-image'
22174 containing a loaded image:
22175 '(space :align-to (0.5 . (- text my-image)))
22176
22177 Width of left margin minus width of 1 character in the default font:
22178 '(space :width (- left-margin 1))
22179
22180 Width of left margin minus width of 2 characters in the current font:
22181 '(space :width (- left-margin (2 . width)))
22182
22183 Center 1 character over left-margin (in header line):
22184 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22185
22186 Different ways to express width of left fringe plus left margin minus one pixel:
22187 '(space :width (- (+ left-fringe left-margin) (1)))
22188 '(space :width (+ left-fringe left-margin (- (1))))
22189 '(space :width (+ left-fringe left-margin (-1)))
22190
22191 */
22192
22193 #define NUMVAL(X) \
22194 ((INTEGERP (X) || FLOATP (X)) \
22195 ? XFLOATINT (X) \
22196 : - 1)
22197
22198 static int
22199 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22200 struct font *font, int width_p, int *align_to)
22201 {
22202 double pixels;
22203
22204 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22205 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22206
22207 if (NILP (prop))
22208 return OK_PIXELS (0);
22209
22210 eassert (FRAME_LIVE_P (it->f));
22211
22212 if (SYMBOLP (prop))
22213 {
22214 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22215 {
22216 char *unit = SSDATA (SYMBOL_NAME (prop));
22217
22218 if (unit[0] == 'i' && unit[1] == 'n')
22219 pixels = 1.0;
22220 else if (unit[0] == 'm' && unit[1] == 'm')
22221 pixels = 25.4;
22222 else if (unit[0] == 'c' && unit[1] == 'm')
22223 pixels = 2.54;
22224 else
22225 pixels = 0;
22226 if (pixels > 0)
22227 {
22228 double ppi;
22229 #ifdef HAVE_WINDOW_SYSTEM
22230 if (FRAME_WINDOW_P (it->f)
22231 && (ppi = (width_p
22232 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22233 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22234 ppi > 0))
22235 return OK_PIXELS (ppi / pixels);
22236 #endif
22237
22238 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22239 || (CONSP (Vdisplay_pixels_per_inch)
22240 && (ppi = (width_p
22241 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22242 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22243 ppi > 0)))
22244 return OK_PIXELS (ppi / pixels);
22245
22246 return 0;
22247 }
22248 }
22249
22250 #ifdef HAVE_WINDOW_SYSTEM
22251 if (EQ (prop, Qheight))
22252 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22253 if (EQ (prop, Qwidth))
22254 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22255 #else
22256 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22257 return OK_PIXELS (1);
22258 #endif
22259
22260 if (EQ (prop, Qtext))
22261 return OK_PIXELS (width_p
22262 ? window_box_width (it->w, TEXT_AREA)
22263 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22264
22265 if (align_to && *align_to < 0)
22266 {
22267 *res = 0;
22268 if (EQ (prop, Qleft))
22269 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22270 if (EQ (prop, Qright))
22271 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22272 if (EQ (prop, Qcenter))
22273 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22274 + window_box_width (it->w, TEXT_AREA) / 2);
22275 if (EQ (prop, Qleft_fringe))
22276 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22277 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22278 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22279 if (EQ (prop, Qright_fringe))
22280 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22281 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22282 : window_box_right_offset (it->w, TEXT_AREA));
22283 if (EQ (prop, Qleft_margin))
22284 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22285 if (EQ (prop, Qright_margin))
22286 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22287 if (EQ (prop, Qscroll_bar))
22288 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22289 ? 0
22290 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22291 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22292 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22293 : 0)));
22294 }
22295 else
22296 {
22297 if (EQ (prop, Qleft_fringe))
22298 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22299 if (EQ (prop, Qright_fringe))
22300 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22301 if (EQ (prop, Qleft_margin))
22302 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22303 if (EQ (prop, Qright_margin))
22304 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22305 if (EQ (prop, Qscroll_bar))
22306 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22307 }
22308
22309 prop = buffer_local_value_1 (prop, it->w->buffer);
22310 if (EQ (prop, Qunbound))
22311 prop = Qnil;
22312 }
22313
22314 if (INTEGERP (prop) || FLOATP (prop))
22315 {
22316 int base_unit = (width_p
22317 ? FRAME_COLUMN_WIDTH (it->f)
22318 : FRAME_LINE_HEIGHT (it->f));
22319 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22320 }
22321
22322 if (CONSP (prop))
22323 {
22324 Lisp_Object car = XCAR (prop);
22325 Lisp_Object cdr = XCDR (prop);
22326
22327 if (SYMBOLP (car))
22328 {
22329 #ifdef HAVE_WINDOW_SYSTEM
22330 if (FRAME_WINDOW_P (it->f)
22331 && valid_image_p (prop))
22332 {
22333 ptrdiff_t id = lookup_image (it->f, prop);
22334 struct image *img = IMAGE_FROM_ID (it->f, id);
22335
22336 return OK_PIXELS (width_p ? img->width : img->height);
22337 }
22338 #endif
22339 if (EQ (car, Qplus) || EQ (car, Qminus))
22340 {
22341 int first = 1;
22342 double px;
22343
22344 pixels = 0;
22345 while (CONSP (cdr))
22346 {
22347 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22348 font, width_p, align_to))
22349 return 0;
22350 if (first)
22351 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22352 else
22353 pixels += px;
22354 cdr = XCDR (cdr);
22355 }
22356 if (EQ (car, Qminus))
22357 pixels = -pixels;
22358 return OK_PIXELS (pixels);
22359 }
22360
22361 car = buffer_local_value_1 (car, it->w->buffer);
22362 if (EQ (car, Qunbound))
22363 car = Qnil;
22364 }
22365
22366 if (INTEGERP (car) || FLOATP (car))
22367 {
22368 double fact;
22369 pixels = XFLOATINT (car);
22370 if (NILP (cdr))
22371 return OK_PIXELS (pixels);
22372 if (calc_pixel_width_or_height (&fact, it, cdr,
22373 font, width_p, align_to))
22374 return OK_PIXELS (pixels * fact);
22375 return 0;
22376 }
22377
22378 return 0;
22379 }
22380
22381 return 0;
22382 }
22383
22384 \f
22385 /***********************************************************************
22386 Glyph Display
22387 ***********************************************************************/
22388
22389 #ifdef HAVE_WINDOW_SYSTEM
22390
22391 #ifdef GLYPH_DEBUG
22392
22393 void
22394 dump_glyph_string (struct glyph_string *s)
22395 {
22396 fprintf (stderr, "glyph string\n");
22397 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22398 s->x, s->y, s->width, s->height);
22399 fprintf (stderr, " ybase = %d\n", s->ybase);
22400 fprintf (stderr, " hl = %d\n", s->hl);
22401 fprintf (stderr, " left overhang = %d, right = %d\n",
22402 s->left_overhang, s->right_overhang);
22403 fprintf (stderr, " nchars = %d\n", s->nchars);
22404 fprintf (stderr, " extends to end of line = %d\n",
22405 s->extends_to_end_of_line_p);
22406 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22407 fprintf (stderr, " bg width = %d\n", s->background_width);
22408 }
22409
22410 #endif /* GLYPH_DEBUG */
22411
22412 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22413 of XChar2b structures for S; it can't be allocated in
22414 init_glyph_string because it must be allocated via `alloca'. W
22415 is the window on which S is drawn. ROW and AREA are the glyph row
22416 and area within the row from which S is constructed. START is the
22417 index of the first glyph structure covered by S. HL is a
22418 face-override for drawing S. */
22419
22420 #ifdef HAVE_NTGUI
22421 #define OPTIONAL_HDC(hdc) HDC hdc,
22422 #define DECLARE_HDC(hdc) HDC hdc;
22423 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22424 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22425 #endif
22426
22427 #ifndef OPTIONAL_HDC
22428 #define OPTIONAL_HDC(hdc)
22429 #define DECLARE_HDC(hdc)
22430 #define ALLOCATE_HDC(hdc, f)
22431 #define RELEASE_HDC(hdc, f)
22432 #endif
22433
22434 static void
22435 init_glyph_string (struct glyph_string *s,
22436 OPTIONAL_HDC (hdc)
22437 XChar2b *char2b, struct window *w, struct glyph_row *row,
22438 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22439 {
22440 memset (s, 0, sizeof *s);
22441 s->w = w;
22442 s->f = XFRAME (w->frame);
22443 #ifdef HAVE_NTGUI
22444 s->hdc = hdc;
22445 #endif
22446 s->display = FRAME_X_DISPLAY (s->f);
22447 s->window = FRAME_X_WINDOW (s->f);
22448 s->char2b = char2b;
22449 s->hl = hl;
22450 s->row = row;
22451 s->area = area;
22452 s->first_glyph = row->glyphs[area] + start;
22453 s->height = row->height;
22454 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22455 s->ybase = s->y + row->ascent;
22456 }
22457
22458
22459 /* Append the list of glyph strings with head H and tail T to the list
22460 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22461
22462 static inline void
22463 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22464 struct glyph_string *h, struct glyph_string *t)
22465 {
22466 if (h)
22467 {
22468 if (*head)
22469 (*tail)->next = h;
22470 else
22471 *head = h;
22472 h->prev = *tail;
22473 *tail = t;
22474 }
22475 }
22476
22477
22478 /* Prepend the list of glyph strings with head H and tail T to the
22479 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22480 result. */
22481
22482 static inline void
22483 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22484 struct glyph_string *h, struct glyph_string *t)
22485 {
22486 if (h)
22487 {
22488 if (*head)
22489 (*head)->prev = t;
22490 else
22491 *tail = t;
22492 t->next = *head;
22493 *head = h;
22494 }
22495 }
22496
22497
22498 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22499 Set *HEAD and *TAIL to the resulting list. */
22500
22501 static inline void
22502 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22503 struct glyph_string *s)
22504 {
22505 s->next = s->prev = NULL;
22506 append_glyph_string_lists (head, tail, s, s);
22507 }
22508
22509
22510 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22511 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22512 make sure that X resources for the face returned are allocated.
22513 Value is a pointer to a realized face that is ready for display if
22514 DISPLAY_P is non-zero. */
22515
22516 static inline struct face *
22517 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22518 XChar2b *char2b, int display_p)
22519 {
22520 struct face *face = FACE_FROM_ID (f, face_id);
22521
22522 if (face->font)
22523 {
22524 unsigned code = face->font->driver->encode_char (face->font, c);
22525
22526 if (code != FONT_INVALID_CODE)
22527 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22528 else
22529 STORE_XCHAR2B (char2b, 0, 0);
22530 }
22531
22532 /* Make sure X resources of the face are allocated. */
22533 #ifdef HAVE_X_WINDOWS
22534 if (display_p)
22535 #endif
22536 {
22537 eassert (face != NULL);
22538 PREPARE_FACE_FOR_DISPLAY (f, face);
22539 }
22540
22541 return face;
22542 }
22543
22544
22545 /* Get face and two-byte form of character glyph GLYPH on frame F.
22546 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22547 a pointer to a realized face that is ready for display. */
22548
22549 static inline struct face *
22550 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22551 XChar2b *char2b, int *two_byte_p)
22552 {
22553 struct face *face;
22554
22555 eassert (glyph->type == CHAR_GLYPH);
22556 face = FACE_FROM_ID (f, glyph->face_id);
22557
22558 if (two_byte_p)
22559 *two_byte_p = 0;
22560
22561 if (face->font)
22562 {
22563 unsigned code;
22564
22565 if (CHAR_BYTE8_P (glyph->u.ch))
22566 code = CHAR_TO_BYTE8 (glyph->u.ch);
22567 else
22568 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22569
22570 if (code != FONT_INVALID_CODE)
22571 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22572 else
22573 STORE_XCHAR2B (char2b, 0, 0);
22574 }
22575
22576 /* Make sure X resources of the face are allocated. */
22577 eassert (face != NULL);
22578 PREPARE_FACE_FOR_DISPLAY (f, face);
22579 return face;
22580 }
22581
22582
22583 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22584 Return 1 if FONT has a glyph for C, otherwise return 0. */
22585
22586 static inline int
22587 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22588 {
22589 unsigned code;
22590
22591 if (CHAR_BYTE8_P (c))
22592 code = CHAR_TO_BYTE8 (c);
22593 else
22594 code = font->driver->encode_char (font, c);
22595
22596 if (code == FONT_INVALID_CODE)
22597 return 0;
22598 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22599 return 1;
22600 }
22601
22602
22603 /* Fill glyph string S with composition components specified by S->cmp.
22604
22605 BASE_FACE is the base face of the composition.
22606 S->cmp_from is the index of the first component for S.
22607
22608 OVERLAPS non-zero means S should draw the foreground only, and use
22609 its physical height for clipping. See also draw_glyphs.
22610
22611 Value is the index of a component not in S. */
22612
22613 static int
22614 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22615 int overlaps)
22616 {
22617 int i;
22618 /* For all glyphs of this composition, starting at the offset
22619 S->cmp_from, until we reach the end of the definition or encounter a
22620 glyph that requires the different face, add it to S. */
22621 struct face *face;
22622
22623 eassert (s);
22624
22625 s->for_overlaps = overlaps;
22626 s->face = NULL;
22627 s->font = NULL;
22628 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22629 {
22630 int c = COMPOSITION_GLYPH (s->cmp, i);
22631
22632 /* TAB in a composition means display glyphs with padding space
22633 on the left or right. */
22634 if (c != '\t')
22635 {
22636 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22637 -1, Qnil);
22638
22639 face = get_char_face_and_encoding (s->f, c, face_id,
22640 s->char2b + i, 1);
22641 if (face)
22642 {
22643 if (! s->face)
22644 {
22645 s->face = face;
22646 s->font = s->face->font;
22647 }
22648 else if (s->face != face)
22649 break;
22650 }
22651 }
22652 ++s->nchars;
22653 }
22654 s->cmp_to = i;
22655
22656 if (s->face == NULL)
22657 {
22658 s->face = base_face->ascii_face;
22659 s->font = s->face->font;
22660 }
22661
22662 /* All glyph strings for the same composition has the same width,
22663 i.e. the width set for the first component of the composition. */
22664 s->width = s->first_glyph->pixel_width;
22665
22666 /* If the specified font could not be loaded, use the frame's
22667 default font, but record the fact that we couldn't load it in
22668 the glyph string so that we can draw rectangles for the
22669 characters of the glyph string. */
22670 if (s->font == NULL)
22671 {
22672 s->font_not_found_p = 1;
22673 s->font = FRAME_FONT (s->f);
22674 }
22675
22676 /* Adjust base line for subscript/superscript text. */
22677 s->ybase += s->first_glyph->voffset;
22678
22679 /* This glyph string must always be drawn with 16-bit functions. */
22680 s->two_byte_p = 1;
22681
22682 return s->cmp_to;
22683 }
22684
22685 static int
22686 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22687 int start, int end, int overlaps)
22688 {
22689 struct glyph *glyph, *last;
22690 Lisp_Object lgstring;
22691 int i;
22692
22693 s->for_overlaps = overlaps;
22694 glyph = s->row->glyphs[s->area] + start;
22695 last = s->row->glyphs[s->area] + end;
22696 s->cmp_id = glyph->u.cmp.id;
22697 s->cmp_from = glyph->slice.cmp.from;
22698 s->cmp_to = glyph->slice.cmp.to + 1;
22699 s->face = FACE_FROM_ID (s->f, face_id);
22700 lgstring = composition_gstring_from_id (s->cmp_id);
22701 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22702 glyph++;
22703 while (glyph < last
22704 && glyph->u.cmp.automatic
22705 && glyph->u.cmp.id == s->cmp_id
22706 && s->cmp_to == glyph->slice.cmp.from)
22707 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22708
22709 for (i = s->cmp_from; i < s->cmp_to; i++)
22710 {
22711 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22712 unsigned code = LGLYPH_CODE (lglyph);
22713
22714 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22715 }
22716 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22717 return glyph - s->row->glyphs[s->area];
22718 }
22719
22720
22721 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22722 See the comment of fill_glyph_string for arguments.
22723 Value is the index of the first glyph not in S. */
22724
22725
22726 static int
22727 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22728 int start, int end, int overlaps)
22729 {
22730 struct glyph *glyph, *last;
22731 int voffset;
22732
22733 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22734 s->for_overlaps = overlaps;
22735 glyph = s->row->glyphs[s->area] + start;
22736 last = s->row->glyphs[s->area] + end;
22737 voffset = glyph->voffset;
22738 s->face = FACE_FROM_ID (s->f, face_id);
22739 s->font = s->face->font;
22740 s->nchars = 1;
22741 s->width = glyph->pixel_width;
22742 glyph++;
22743 while (glyph < last
22744 && glyph->type == GLYPHLESS_GLYPH
22745 && glyph->voffset == voffset
22746 && glyph->face_id == face_id)
22747 {
22748 s->nchars++;
22749 s->width += glyph->pixel_width;
22750 glyph++;
22751 }
22752 s->ybase += voffset;
22753 return glyph - s->row->glyphs[s->area];
22754 }
22755
22756
22757 /* Fill glyph string S from a sequence of character glyphs.
22758
22759 FACE_ID is the face id of the string. START is the index of the
22760 first glyph to consider, END is the index of the last + 1.
22761 OVERLAPS non-zero means S should draw the foreground only, and use
22762 its physical height for clipping. See also draw_glyphs.
22763
22764 Value is the index of the first glyph not in S. */
22765
22766 static int
22767 fill_glyph_string (struct glyph_string *s, int face_id,
22768 int start, int end, int overlaps)
22769 {
22770 struct glyph *glyph, *last;
22771 int voffset;
22772 int glyph_not_available_p;
22773
22774 eassert (s->f == XFRAME (s->w->frame));
22775 eassert (s->nchars == 0);
22776 eassert (start >= 0 && end > start);
22777
22778 s->for_overlaps = overlaps;
22779 glyph = s->row->glyphs[s->area] + start;
22780 last = s->row->glyphs[s->area] + end;
22781 voffset = glyph->voffset;
22782 s->padding_p = glyph->padding_p;
22783 glyph_not_available_p = glyph->glyph_not_available_p;
22784
22785 while (glyph < last
22786 && glyph->type == CHAR_GLYPH
22787 && glyph->voffset == voffset
22788 /* Same face id implies same font, nowadays. */
22789 && glyph->face_id == face_id
22790 && glyph->glyph_not_available_p == glyph_not_available_p)
22791 {
22792 int two_byte_p;
22793
22794 s->face = get_glyph_face_and_encoding (s->f, glyph,
22795 s->char2b + s->nchars,
22796 &two_byte_p);
22797 s->two_byte_p = two_byte_p;
22798 ++s->nchars;
22799 eassert (s->nchars <= end - start);
22800 s->width += glyph->pixel_width;
22801 if (glyph++->padding_p != s->padding_p)
22802 break;
22803 }
22804
22805 s->font = s->face->font;
22806
22807 /* If the specified font could not be loaded, use the frame's font,
22808 but record the fact that we couldn't load it in
22809 S->font_not_found_p so that we can draw rectangles for the
22810 characters of the glyph string. */
22811 if (s->font == NULL || glyph_not_available_p)
22812 {
22813 s->font_not_found_p = 1;
22814 s->font = FRAME_FONT (s->f);
22815 }
22816
22817 /* Adjust base line for subscript/superscript text. */
22818 s->ybase += voffset;
22819
22820 eassert (s->face && s->face->gc);
22821 return glyph - s->row->glyphs[s->area];
22822 }
22823
22824
22825 /* Fill glyph string S from image glyph S->first_glyph. */
22826
22827 static void
22828 fill_image_glyph_string (struct glyph_string *s)
22829 {
22830 eassert (s->first_glyph->type == IMAGE_GLYPH);
22831 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22832 eassert (s->img);
22833 s->slice = s->first_glyph->slice.img;
22834 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22835 s->font = s->face->font;
22836 s->width = s->first_glyph->pixel_width;
22837
22838 /* Adjust base line for subscript/superscript text. */
22839 s->ybase += s->first_glyph->voffset;
22840 }
22841
22842
22843 /* Fill glyph string S from a sequence of stretch glyphs.
22844
22845 START is the index of the first glyph to consider,
22846 END is the index of the last + 1.
22847
22848 Value is the index of the first glyph not in S. */
22849
22850 static int
22851 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22852 {
22853 struct glyph *glyph, *last;
22854 int voffset, face_id;
22855
22856 eassert (s->first_glyph->type == STRETCH_GLYPH);
22857
22858 glyph = s->row->glyphs[s->area] + start;
22859 last = s->row->glyphs[s->area] + end;
22860 face_id = glyph->face_id;
22861 s->face = FACE_FROM_ID (s->f, face_id);
22862 s->font = s->face->font;
22863 s->width = glyph->pixel_width;
22864 s->nchars = 1;
22865 voffset = glyph->voffset;
22866
22867 for (++glyph;
22868 (glyph < last
22869 && glyph->type == STRETCH_GLYPH
22870 && glyph->voffset == voffset
22871 && glyph->face_id == face_id);
22872 ++glyph)
22873 s->width += glyph->pixel_width;
22874
22875 /* Adjust base line for subscript/superscript text. */
22876 s->ybase += voffset;
22877
22878 /* The case that face->gc == 0 is handled when drawing the glyph
22879 string by calling PREPARE_FACE_FOR_DISPLAY. */
22880 eassert (s->face);
22881 return glyph - s->row->glyphs[s->area];
22882 }
22883
22884 static struct font_metrics *
22885 get_per_char_metric (struct font *font, XChar2b *char2b)
22886 {
22887 static struct font_metrics metrics;
22888 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22889
22890 if (! font || code == FONT_INVALID_CODE)
22891 return NULL;
22892 font->driver->text_extents (font, &code, 1, &metrics);
22893 return &metrics;
22894 }
22895
22896 /* EXPORT for RIF:
22897 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22898 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22899 assumed to be zero. */
22900
22901 void
22902 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22903 {
22904 *left = *right = 0;
22905
22906 if (glyph->type == CHAR_GLYPH)
22907 {
22908 struct face *face;
22909 XChar2b char2b;
22910 struct font_metrics *pcm;
22911
22912 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22913 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22914 {
22915 if (pcm->rbearing > pcm->width)
22916 *right = pcm->rbearing - pcm->width;
22917 if (pcm->lbearing < 0)
22918 *left = -pcm->lbearing;
22919 }
22920 }
22921 else if (glyph->type == COMPOSITE_GLYPH)
22922 {
22923 if (! glyph->u.cmp.automatic)
22924 {
22925 struct composition *cmp = composition_table[glyph->u.cmp.id];
22926
22927 if (cmp->rbearing > cmp->pixel_width)
22928 *right = cmp->rbearing - cmp->pixel_width;
22929 if (cmp->lbearing < 0)
22930 *left = - cmp->lbearing;
22931 }
22932 else
22933 {
22934 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22935 struct font_metrics metrics;
22936
22937 composition_gstring_width (gstring, glyph->slice.cmp.from,
22938 glyph->slice.cmp.to + 1, &metrics);
22939 if (metrics.rbearing > metrics.width)
22940 *right = metrics.rbearing - metrics.width;
22941 if (metrics.lbearing < 0)
22942 *left = - metrics.lbearing;
22943 }
22944 }
22945 }
22946
22947
22948 /* Return the index of the first glyph preceding glyph string S that
22949 is overwritten by S because of S's left overhang. Value is -1
22950 if no glyphs are overwritten. */
22951
22952 static int
22953 left_overwritten (struct glyph_string *s)
22954 {
22955 int k;
22956
22957 if (s->left_overhang)
22958 {
22959 int x = 0, i;
22960 struct glyph *glyphs = s->row->glyphs[s->area];
22961 int first = s->first_glyph - glyphs;
22962
22963 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22964 x -= glyphs[i].pixel_width;
22965
22966 k = i + 1;
22967 }
22968 else
22969 k = -1;
22970
22971 return k;
22972 }
22973
22974
22975 /* Return the index of the first glyph preceding glyph string S that
22976 is overwriting S because of its right overhang. Value is -1 if no
22977 glyph in front of S overwrites S. */
22978
22979 static int
22980 left_overwriting (struct glyph_string *s)
22981 {
22982 int i, k, x;
22983 struct glyph *glyphs = s->row->glyphs[s->area];
22984 int first = s->first_glyph - glyphs;
22985
22986 k = -1;
22987 x = 0;
22988 for (i = first - 1; i >= 0; --i)
22989 {
22990 int left, right;
22991 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22992 if (x + right > 0)
22993 k = i;
22994 x -= glyphs[i].pixel_width;
22995 }
22996
22997 return k;
22998 }
22999
23000
23001 /* Return the index of the last glyph following glyph string S that is
23002 overwritten by S because of S's right overhang. Value is -1 if
23003 no such glyph is found. */
23004
23005 static int
23006 right_overwritten (struct glyph_string *s)
23007 {
23008 int k = -1;
23009
23010 if (s->right_overhang)
23011 {
23012 int x = 0, i;
23013 struct glyph *glyphs = s->row->glyphs[s->area];
23014 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23015 int end = s->row->used[s->area];
23016
23017 for (i = first; i < end && s->right_overhang > x; ++i)
23018 x += glyphs[i].pixel_width;
23019
23020 k = i;
23021 }
23022
23023 return k;
23024 }
23025
23026
23027 /* Return the index of the last glyph following glyph string S that
23028 overwrites S because of its left overhang. Value is negative
23029 if no such glyph is found. */
23030
23031 static int
23032 right_overwriting (struct glyph_string *s)
23033 {
23034 int i, k, x;
23035 int end = s->row->used[s->area];
23036 struct glyph *glyphs = s->row->glyphs[s->area];
23037 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23038
23039 k = -1;
23040 x = 0;
23041 for (i = first; i < end; ++i)
23042 {
23043 int left, right;
23044 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23045 if (x - left < 0)
23046 k = i;
23047 x += glyphs[i].pixel_width;
23048 }
23049
23050 return k;
23051 }
23052
23053
23054 /* Set background width of glyph string S. START is the index of the
23055 first glyph following S. LAST_X is the right-most x-position + 1
23056 in the drawing area. */
23057
23058 static inline void
23059 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23060 {
23061 /* If the face of this glyph string has to be drawn to the end of
23062 the drawing area, set S->extends_to_end_of_line_p. */
23063
23064 if (start == s->row->used[s->area]
23065 && s->area == TEXT_AREA
23066 && ((s->row->fill_line_p
23067 && (s->hl == DRAW_NORMAL_TEXT
23068 || s->hl == DRAW_IMAGE_RAISED
23069 || s->hl == DRAW_IMAGE_SUNKEN))
23070 || s->hl == DRAW_MOUSE_FACE))
23071 s->extends_to_end_of_line_p = 1;
23072
23073 /* If S extends its face to the end of the line, set its
23074 background_width to the distance to the right edge of the drawing
23075 area. */
23076 if (s->extends_to_end_of_line_p)
23077 s->background_width = last_x - s->x + 1;
23078 else
23079 s->background_width = s->width;
23080 }
23081
23082
23083 /* Compute overhangs and x-positions for glyph string S and its
23084 predecessors, or successors. X is the starting x-position for S.
23085 BACKWARD_P non-zero means process predecessors. */
23086
23087 static void
23088 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23089 {
23090 if (backward_p)
23091 {
23092 while (s)
23093 {
23094 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23095 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23096 x -= s->width;
23097 s->x = x;
23098 s = s->prev;
23099 }
23100 }
23101 else
23102 {
23103 while (s)
23104 {
23105 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23106 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23107 s->x = x;
23108 x += s->width;
23109 s = s->next;
23110 }
23111 }
23112 }
23113
23114
23115
23116 /* The following macros are only called from draw_glyphs below.
23117 They reference the following parameters of that function directly:
23118 `w', `row', `area', and `overlap_p'
23119 as well as the following local variables:
23120 `s', `f', and `hdc' (in W32) */
23121
23122 #ifdef HAVE_NTGUI
23123 /* On W32, silently add local `hdc' variable to argument list of
23124 init_glyph_string. */
23125 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23126 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23127 #else
23128 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23129 init_glyph_string (s, char2b, w, row, area, start, hl)
23130 #endif
23131
23132 /* Add a glyph string for a stretch glyph to the list of strings
23133 between HEAD and TAIL. START is the index of the stretch glyph in
23134 row area AREA of glyph row ROW. END is the index of the last glyph
23135 in that glyph row area. X is the current output position assigned
23136 to the new glyph string constructed. HL overrides that face of the
23137 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23138 is the right-most x-position of the drawing area. */
23139
23140 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23141 and below -- keep them on one line. */
23142 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23143 do \
23144 { \
23145 s = alloca (sizeof *s); \
23146 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23147 START = fill_stretch_glyph_string (s, START, END); \
23148 append_glyph_string (&HEAD, &TAIL, s); \
23149 s->x = (X); \
23150 } \
23151 while (0)
23152
23153
23154 /* Add a glyph string for an image glyph to the list of strings
23155 between HEAD and TAIL. START is the index of the image glyph in
23156 row area AREA of glyph row ROW. END is the index of the last glyph
23157 in that glyph row area. X is the current output position assigned
23158 to the new glyph string constructed. HL overrides that face of the
23159 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23160 is the right-most x-position of the drawing area. */
23161
23162 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23163 do \
23164 { \
23165 s = alloca (sizeof *s); \
23166 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23167 fill_image_glyph_string (s); \
23168 append_glyph_string (&HEAD, &TAIL, s); \
23169 ++START; \
23170 s->x = (X); \
23171 } \
23172 while (0)
23173
23174
23175 /* Add a glyph string for a sequence of character glyphs to the list
23176 of strings between HEAD and TAIL. START is the index of the first
23177 glyph in row area AREA of glyph row ROW that is part of the new
23178 glyph string. END is the index of the last glyph in that glyph row
23179 area. X is the current output position assigned to the new glyph
23180 string constructed. HL overrides that face of the glyph; e.g. it
23181 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23182 right-most x-position of the drawing area. */
23183
23184 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23185 do \
23186 { \
23187 int face_id; \
23188 XChar2b *char2b; \
23189 \
23190 face_id = (row)->glyphs[area][START].face_id; \
23191 \
23192 s = alloca (sizeof *s); \
23193 char2b = alloca ((END - START) * sizeof *char2b); \
23194 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23195 append_glyph_string (&HEAD, &TAIL, s); \
23196 s->x = (X); \
23197 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23198 } \
23199 while (0)
23200
23201
23202 /* Add a glyph string for a composite sequence to the list of strings
23203 between HEAD and TAIL. START is the index of the first glyph in
23204 row area AREA of glyph row ROW that is part of the new glyph
23205 string. END is the index of the last glyph in that glyph row area.
23206 X is the current output position assigned to the new glyph string
23207 constructed. HL overrides that face of the glyph; e.g. it is
23208 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23209 x-position of the drawing area. */
23210
23211 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23212 do { \
23213 int face_id = (row)->glyphs[area][START].face_id; \
23214 struct face *base_face = FACE_FROM_ID (f, face_id); \
23215 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23216 struct composition *cmp = composition_table[cmp_id]; \
23217 XChar2b *char2b; \
23218 struct glyph_string *first_s = NULL; \
23219 int n; \
23220 \
23221 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23222 \
23223 /* Make glyph_strings for each glyph sequence that is drawable by \
23224 the same face, and append them to HEAD/TAIL. */ \
23225 for (n = 0; n < cmp->glyph_len;) \
23226 { \
23227 s = alloca (sizeof *s); \
23228 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23229 append_glyph_string (&(HEAD), &(TAIL), s); \
23230 s->cmp = cmp; \
23231 s->cmp_from = n; \
23232 s->x = (X); \
23233 if (n == 0) \
23234 first_s = s; \
23235 n = fill_composite_glyph_string (s, base_face, overlaps); \
23236 } \
23237 \
23238 ++START; \
23239 s = first_s; \
23240 } while (0)
23241
23242
23243 /* Add a glyph string for a glyph-string sequence to the list of strings
23244 between HEAD and TAIL. */
23245
23246 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23247 do { \
23248 int face_id; \
23249 XChar2b *char2b; \
23250 Lisp_Object gstring; \
23251 \
23252 face_id = (row)->glyphs[area][START].face_id; \
23253 gstring = (composition_gstring_from_id \
23254 ((row)->glyphs[area][START].u.cmp.id)); \
23255 s = alloca (sizeof *s); \
23256 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23257 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23258 append_glyph_string (&(HEAD), &(TAIL), s); \
23259 s->x = (X); \
23260 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23261 } while (0)
23262
23263
23264 /* Add a glyph string for a sequence of glyphless character's glyphs
23265 to the list of strings between HEAD and TAIL. The meanings of
23266 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23267
23268 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23269 do \
23270 { \
23271 int face_id; \
23272 \
23273 face_id = (row)->glyphs[area][START].face_id; \
23274 \
23275 s = alloca (sizeof *s); \
23276 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23277 append_glyph_string (&HEAD, &TAIL, s); \
23278 s->x = (X); \
23279 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23280 overlaps); \
23281 } \
23282 while (0)
23283
23284
23285 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23286 of AREA of glyph row ROW on window W between indices START and END.
23287 HL overrides the face for drawing glyph strings, e.g. it is
23288 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23289 x-positions of the drawing area.
23290
23291 This is an ugly monster macro construct because we must use alloca
23292 to allocate glyph strings (because draw_glyphs can be called
23293 asynchronously). */
23294
23295 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23296 do \
23297 { \
23298 HEAD = TAIL = NULL; \
23299 while (START < END) \
23300 { \
23301 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23302 switch (first_glyph->type) \
23303 { \
23304 case CHAR_GLYPH: \
23305 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23306 HL, X, LAST_X); \
23307 break; \
23308 \
23309 case COMPOSITE_GLYPH: \
23310 if (first_glyph->u.cmp.automatic) \
23311 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23312 HL, X, LAST_X); \
23313 else \
23314 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23315 HL, X, LAST_X); \
23316 break; \
23317 \
23318 case STRETCH_GLYPH: \
23319 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23320 HL, X, LAST_X); \
23321 break; \
23322 \
23323 case IMAGE_GLYPH: \
23324 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23325 HL, X, LAST_X); \
23326 break; \
23327 \
23328 case GLYPHLESS_GLYPH: \
23329 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23330 HL, X, LAST_X); \
23331 break; \
23332 \
23333 default: \
23334 abort (); \
23335 } \
23336 \
23337 if (s) \
23338 { \
23339 set_glyph_string_background_width (s, START, LAST_X); \
23340 (X) += s->width; \
23341 } \
23342 } \
23343 } while (0)
23344
23345
23346 /* Draw glyphs between START and END in AREA of ROW on window W,
23347 starting at x-position X. X is relative to AREA in W. HL is a
23348 face-override with the following meaning:
23349
23350 DRAW_NORMAL_TEXT draw normally
23351 DRAW_CURSOR draw in cursor face
23352 DRAW_MOUSE_FACE draw in mouse face.
23353 DRAW_INVERSE_VIDEO draw in mode line face
23354 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23355 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23356
23357 If OVERLAPS is non-zero, draw only the foreground of characters and
23358 clip to the physical height of ROW. Non-zero value also defines
23359 the overlapping part to be drawn:
23360
23361 OVERLAPS_PRED overlap with preceding rows
23362 OVERLAPS_SUCC overlap with succeeding rows
23363 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23364 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23365
23366 Value is the x-position reached, relative to AREA of W. */
23367
23368 static int
23369 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23370 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23371 enum draw_glyphs_face hl, int overlaps)
23372 {
23373 struct glyph_string *head, *tail;
23374 struct glyph_string *s;
23375 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23376 int i, j, x_reached, last_x, area_left = 0;
23377 struct frame *f = XFRAME (WINDOW_FRAME (w));
23378 DECLARE_HDC (hdc);
23379
23380 ALLOCATE_HDC (hdc, f);
23381
23382 /* Let's rather be paranoid than getting a SEGV. */
23383 end = min (end, row->used[area]);
23384 start = max (0, start);
23385 start = min (end, start);
23386
23387 /* Translate X to frame coordinates. Set last_x to the right
23388 end of the drawing area. */
23389 if (row->full_width_p)
23390 {
23391 /* X is relative to the left edge of W, without scroll bars
23392 or fringes. */
23393 area_left = WINDOW_LEFT_EDGE_X (w);
23394 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23395 }
23396 else
23397 {
23398 area_left = window_box_left (w, area);
23399 last_x = area_left + window_box_width (w, area);
23400 }
23401 x += area_left;
23402
23403 /* Build a doubly-linked list of glyph_string structures between
23404 head and tail from what we have to draw. Note that the macro
23405 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23406 the reason we use a separate variable `i'. */
23407 i = start;
23408 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23409 if (tail)
23410 x_reached = tail->x + tail->background_width;
23411 else
23412 x_reached = x;
23413
23414 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23415 the row, redraw some glyphs in front or following the glyph
23416 strings built above. */
23417 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23418 {
23419 struct glyph_string *h, *t;
23420 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23421 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23422 int check_mouse_face = 0;
23423 int dummy_x = 0;
23424
23425 /* If mouse highlighting is on, we may need to draw adjacent
23426 glyphs using mouse-face highlighting. */
23427 if (area == TEXT_AREA && row->mouse_face_p)
23428 {
23429 struct glyph_row *mouse_beg_row, *mouse_end_row;
23430
23431 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23432 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23433
23434 if (row >= mouse_beg_row && row <= mouse_end_row)
23435 {
23436 check_mouse_face = 1;
23437 mouse_beg_col = (row == mouse_beg_row)
23438 ? hlinfo->mouse_face_beg_col : 0;
23439 mouse_end_col = (row == mouse_end_row)
23440 ? hlinfo->mouse_face_end_col
23441 : row->used[TEXT_AREA];
23442 }
23443 }
23444
23445 /* Compute overhangs for all glyph strings. */
23446 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23447 for (s = head; s; s = s->next)
23448 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23449
23450 /* Prepend glyph strings for glyphs in front of the first glyph
23451 string that are overwritten because of the first glyph
23452 string's left overhang. The background of all strings
23453 prepended must be drawn because the first glyph string
23454 draws over it. */
23455 i = left_overwritten (head);
23456 if (i >= 0)
23457 {
23458 enum draw_glyphs_face overlap_hl;
23459
23460 /* If this row contains mouse highlighting, attempt to draw
23461 the overlapped glyphs with the correct highlight. This
23462 code fails if the overlap encompasses more than one glyph
23463 and mouse-highlight spans only some of these glyphs.
23464 However, making it work perfectly involves a lot more
23465 code, and I don't know if the pathological case occurs in
23466 practice, so we'll stick to this for now. --- cyd */
23467 if (check_mouse_face
23468 && mouse_beg_col < start && mouse_end_col > i)
23469 overlap_hl = DRAW_MOUSE_FACE;
23470 else
23471 overlap_hl = DRAW_NORMAL_TEXT;
23472
23473 j = i;
23474 BUILD_GLYPH_STRINGS (j, start, h, t,
23475 overlap_hl, dummy_x, last_x);
23476 start = i;
23477 compute_overhangs_and_x (t, head->x, 1);
23478 prepend_glyph_string_lists (&head, &tail, h, t);
23479 clip_head = head;
23480 }
23481
23482 /* Prepend glyph strings for glyphs in front of the first glyph
23483 string that overwrite that glyph string because of their
23484 right overhang. For these strings, only the foreground must
23485 be drawn, because it draws over the glyph string at `head'.
23486 The background must not be drawn because this would overwrite
23487 right overhangs of preceding glyphs for which no glyph
23488 strings exist. */
23489 i = left_overwriting (head);
23490 if (i >= 0)
23491 {
23492 enum draw_glyphs_face overlap_hl;
23493
23494 if (check_mouse_face
23495 && mouse_beg_col < start && mouse_end_col > i)
23496 overlap_hl = DRAW_MOUSE_FACE;
23497 else
23498 overlap_hl = DRAW_NORMAL_TEXT;
23499
23500 clip_head = head;
23501 BUILD_GLYPH_STRINGS (i, start, h, t,
23502 overlap_hl, dummy_x, last_x);
23503 for (s = h; s; s = s->next)
23504 s->background_filled_p = 1;
23505 compute_overhangs_and_x (t, head->x, 1);
23506 prepend_glyph_string_lists (&head, &tail, h, t);
23507 }
23508
23509 /* Append glyphs strings for glyphs following the last glyph
23510 string tail that are overwritten by tail. The background of
23511 these strings has to be drawn because tail's foreground draws
23512 over it. */
23513 i = right_overwritten (tail);
23514 if (i >= 0)
23515 {
23516 enum draw_glyphs_face overlap_hl;
23517
23518 if (check_mouse_face
23519 && mouse_beg_col < i && mouse_end_col > end)
23520 overlap_hl = DRAW_MOUSE_FACE;
23521 else
23522 overlap_hl = DRAW_NORMAL_TEXT;
23523
23524 BUILD_GLYPH_STRINGS (end, i, h, t,
23525 overlap_hl, x, last_x);
23526 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23527 we don't have `end = i;' here. */
23528 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23529 append_glyph_string_lists (&head, &tail, h, t);
23530 clip_tail = tail;
23531 }
23532
23533 /* Append glyph strings for glyphs following the last glyph
23534 string tail that overwrite tail. The foreground of such
23535 glyphs has to be drawn because it writes into the background
23536 of tail. The background must not be drawn because it could
23537 paint over the foreground of following glyphs. */
23538 i = right_overwriting (tail);
23539 if (i >= 0)
23540 {
23541 enum draw_glyphs_face overlap_hl;
23542 if (check_mouse_face
23543 && mouse_beg_col < i && mouse_end_col > end)
23544 overlap_hl = DRAW_MOUSE_FACE;
23545 else
23546 overlap_hl = DRAW_NORMAL_TEXT;
23547
23548 clip_tail = tail;
23549 i++; /* We must include the Ith glyph. */
23550 BUILD_GLYPH_STRINGS (end, i, h, t,
23551 overlap_hl, x, last_x);
23552 for (s = h; s; s = s->next)
23553 s->background_filled_p = 1;
23554 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23555 append_glyph_string_lists (&head, &tail, h, t);
23556 }
23557 if (clip_head || clip_tail)
23558 for (s = head; s; s = s->next)
23559 {
23560 s->clip_head = clip_head;
23561 s->clip_tail = clip_tail;
23562 }
23563 }
23564
23565 /* Draw all strings. */
23566 for (s = head; s; s = s->next)
23567 FRAME_RIF (f)->draw_glyph_string (s);
23568
23569 #ifndef HAVE_NS
23570 /* When focus a sole frame and move horizontally, this sets on_p to 0
23571 causing a failure to erase prev cursor position. */
23572 if (area == TEXT_AREA
23573 && !row->full_width_p
23574 /* When drawing overlapping rows, only the glyph strings'
23575 foreground is drawn, which doesn't erase a cursor
23576 completely. */
23577 && !overlaps)
23578 {
23579 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23580 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23581 : (tail ? tail->x + tail->background_width : x));
23582 x0 -= area_left;
23583 x1 -= area_left;
23584
23585 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23586 row->y, MATRIX_ROW_BOTTOM_Y (row));
23587 }
23588 #endif
23589
23590 /* Value is the x-position up to which drawn, relative to AREA of W.
23591 This doesn't include parts drawn because of overhangs. */
23592 if (row->full_width_p)
23593 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23594 else
23595 x_reached -= area_left;
23596
23597 RELEASE_HDC (hdc, f);
23598
23599 return x_reached;
23600 }
23601
23602 /* Expand row matrix if too narrow. Don't expand if area
23603 is not present. */
23604
23605 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23606 { \
23607 if (!fonts_changed_p \
23608 && (it->glyph_row->glyphs[area] \
23609 < it->glyph_row->glyphs[area + 1])) \
23610 { \
23611 it->w->ncols_scale_factor++; \
23612 fonts_changed_p = 1; \
23613 } \
23614 }
23615
23616 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23617 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23618
23619 static inline void
23620 append_glyph (struct it *it)
23621 {
23622 struct glyph *glyph;
23623 enum glyph_row_area area = it->area;
23624
23625 eassert (it->glyph_row);
23626 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23627
23628 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23629 if (glyph < it->glyph_row->glyphs[area + 1])
23630 {
23631 /* If the glyph row is reversed, we need to prepend the glyph
23632 rather than append it. */
23633 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23634 {
23635 struct glyph *g;
23636
23637 /* Make room for the additional glyph. */
23638 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23639 g[1] = *g;
23640 glyph = it->glyph_row->glyphs[area];
23641 }
23642 glyph->charpos = CHARPOS (it->position);
23643 glyph->object = it->object;
23644 if (it->pixel_width > 0)
23645 {
23646 glyph->pixel_width = it->pixel_width;
23647 glyph->padding_p = 0;
23648 }
23649 else
23650 {
23651 /* Assure at least 1-pixel width. Otherwise, cursor can't
23652 be displayed correctly. */
23653 glyph->pixel_width = 1;
23654 glyph->padding_p = 1;
23655 }
23656 glyph->ascent = it->ascent;
23657 glyph->descent = it->descent;
23658 glyph->voffset = it->voffset;
23659 glyph->type = CHAR_GLYPH;
23660 glyph->avoid_cursor_p = it->avoid_cursor_p;
23661 glyph->multibyte_p = it->multibyte_p;
23662 glyph->left_box_line_p = it->start_of_box_run_p;
23663 glyph->right_box_line_p = it->end_of_box_run_p;
23664 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23665 || it->phys_descent > it->descent);
23666 glyph->glyph_not_available_p = it->glyph_not_available_p;
23667 glyph->face_id = it->face_id;
23668 glyph->u.ch = it->char_to_display;
23669 glyph->slice.img = null_glyph_slice;
23670 glyph->font_type = FONT_TYPE_UNKNOWN;
23671 if (it->bidi_p)
23672 {
23673 glyph->resolved_level = it->bidi_it.resolved_level;
23674 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23675 abort ();
23676 glyph->bidi_type = it->bidi_it.type;
23677 }
23678 else
23679 {
23680 glyph->resolved_level = 0;
23681 glyph->bidi_type = UNKNOWN_BT;
23682 }
23683 ++it->glyph_row->used[area];
23684 }
23685 else
23686 IT_EXPAND_MATRIX_WIDTH (it, area);
23687 }
23688
23689 /* Store one glyph for the composition IT->cmp_it.id in
23690 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23691 non-null. */
23692
23693 static inline void
23694 append_composite_glyph (struct it *it)
23695 {
23696 struct glyph *glyph;
23697 enum glyph_row_area area = it->area;
23698
23699 eassert (it->glyph_row);
23700
23701 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23702 if (glyph < it->glyph_row->glyphs[area + 1])
23703 {
23704 /* If the glyph row is reversed, we need to prepend the glyph
23705 rather than append it. */
23706 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23707 {
23708 struct glyph *g;
23709
23710 /* Make room for the new glyph. */
23711 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23712 g[1] = *g;
23713 glyph = it->glyph_row->glyphs[it->area];
23714 }
23715 glyph->charpos = it->cmp_it.charpos;
23716 glyph->object = it->object;
23717 glyph->pixel_width = it->pixel_width;
23718 glyph->ascent = it->ascent;
23719 glyph->descent = it->descent;
23720 glyph->voffset = it->voffset;
23721 glyph->type = COMPOSITE_GLYPH;
23722 if (it->cmp_it.ch < 0)
23723 {
23724 glyph->u.cmp.automatic = 0;
23725 glyph->u.cmp.id = it->cmp_it.id;
23726 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23727 }
23728 else
23729 {
23730 glyph->u.cmp.automatic = 1;
23731 glyph->u.cmp.id = it->cmp_it.id;
23732 glyph->slice.cmp.from = it->cmp_it.from;
23733 glyph->slice.cmp.to = it->cmp_it.to - 1;
23734 }
23735 glyph->avoid_cursor_p = it->avoid_cursor_p;
23736 glyph->multibyte_p = it->multibyte_p;
23737 glyph->left_box_line_p = it->start_of_box_run_p;
23738 glyph->right_box_line_p = it->end_of_box_run_p;
23739 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23740 || it->phys_descent > it->descent);
23741 glyph->padding_p = 0;
23742 glyph->glyph_not_available_p = 0;
23743 glyph->face_id = it->face_id;
23744 glyph->font_type = FONT_TYPE_UNKNOWN;
23745 if (it->bidi_p)
23746 {
23747 glyph->resolved_level = it->bidi_it.resolved_level;
23748 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23749 abort ();
23750 glyph->bidi_type = it->bidi_it.type;
23751 }
23752 ++it->glyph_row->used[area];
23753 }
23754 else
23755 IT_EXPAND_MATRIX_WIDTH (it, area);
23756 }
23757
23758
23759 /* Change IT->ascent and IT->height according to the setting of
23760 IT->voffset. */
23761
23762 static inline void
23763 take_vertical_position_into_account (struct it *it)
23764 {
23765 if (it->voffset)
23766 {
23767 if (it->voffset < 0)
23768 /* Increase the ascent so that we can display the text higher
23769 in the line. */
23770 it->ascent -= it->voffset;
23771 else
23772 /* Increase the descent so that we can display the text lower
23773 in the line. */
23774 it->descent += it->voffset;
23775 }
23776 }
23777
23778
23779 /* Produce glyphs/get display metrics for the image IT is loaded with.
23780 See the description of struct display_iterator in dispextern.h for
23781 an overview of struct display_iterator. */
23782
23783 static void
23784 produce_image_glyph (struct it *it)
23785 {
23786 struct image *img;
23787 struct face *face;
23788 int glyph_ascent, crop;
23789 struct glyph_slice slice;
23790
23791 eassert (it->what == IT_IMAGE);
23792
23793 face = FACE_FROM_ID (it->f, it->face_id);
23794 eassert (face);
23795 /* Make sure X resources of the face is loaded. */
23796 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23797
23798 if (it->image_id < 0)
23799 {
23800 /* Fringe bitmap. */
23801 it->ascent = it->phys_ascent = 0;
23802 it->descent = it->phys_descent = 0;
23803 it->pixel_width = 0;
23804 it->nglyphs = 0;
23805 return;
23806 }
23807
23808 img = IMAGE_FROM_ID (it->f, it->image_id);
23809 eassert (img);
23810 /* Make sure X resources of the image is loaded. */
23811 prepare_image_for_display (it->f, img);
23812
23813 slice.x = slice.y = 0;
23814 slice.width = img->width;
23815 slice.height = img->height;
23816
23817 if (INTEGERP (it->slice.x))
23818 slice.x = XINT (it->slice.x);
23819 else if (FLOATP (it->slice.x))
23820 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23821
23822 if (INTEGERP (it->slice.y))
23823 slice.y = XINT (it->slice.y);
23824 else if (FLOATP (it->slice.y))
23825 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23826
23827 if (INTEGERP (it->slice.width))
23828 slice.width = XINT (it->slice.width);
23829 else if (FLOATP (it->slice.width))
23830 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23831
23832 if (INTEGERP (it->slice.height))
23833 slice.height = XINT (it->slice.height);
23834 else if (FLOATP (it->slice.height))
23835 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23836
23837 if (slice.x >= img->width)
23838 slice.x = img->width;
23839 if (slice.y >= img->height)
23840 slice.y = img->height;
23841 if (slice.x + slice.width >= img->width)
23842 slice.width = img->width - slice.x;
23843 if (slice.y + slice.height > img->height)
23844 slice.height = img->height - slice.y;
23845
23846 if (slice.width == 0 || slice.height == 0)
23847 return;
23848
23849 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23850
23851 it->descent = slice.height - glyph_ascent;
23852 if (slice.y == 0)
23853 it->descent += img->vmargin;
23854 if (slice.y + slice.height == img->height)
23855 it->descent += img->vmargin;
23856 it->phys_descent = it->descent;
23857
23858 it->pixel_width = slice.width;
23859 if (slice.x == 0)
23860 it->pixel_width += img->hmargin;
23861 if (slice.x + slice.width == img->width)
23862 it->pixel_width += img->hmargin;
23863
23864 /* It's quite possible for images to have an ascent greater than
23865 their height, so don't get confused in that case. */
23866 if (it->descent < 0)
23867 it->descent = 0;
23868
23869 it->nglyphs = 1;
23870
23871 if (face->box != FACE_NO_BOX)
23872 {
23873 if (face->box_line_width > 0)
23874 {
23875 if (slice.y == 0)
23876 it->ascent += face->box_line_width;
23877 if (slice.y + slice.height == img->height)
23878 it->descent += face->box_line_width;
23879 }
23880
23881 if (it->start_of_box_run_p && slice.x == 0)
23882 it->pixel_width += eabs (face->box_line_width);
23883 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23884 it->pixel_width += eabs (face->box_line_width);
23885 }
23886
23887 take_vertical_position_into_account (it);
23888
23889 /* Automatically crop wide image glyphs at right edge so we can
23890 draw the cursor on same display row. */
23891 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23892 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23893 {
23894 it->pixel_width -= crop;
23895 slice.width -= crop;
23896 }
23897
23898 if (it->glyph_row)
23899 {
23900 struct glyph *glyph;
23901 enum glyph_row_area area = it->area;
23902
23903 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23904 if (glyph < it->glyph_row->glyphs[area + 1])
23905 {
23906 glyph->charpos = CHARPOS (it->position);
23907 glyph->object = it->object;
23908 glyph->pixel_width = it->pixel_width;
23909 glyph->ascent = glyph_ascent;
23910 glyph->descent = it->descent;
23911 glyph->voffset = it->voffset;
23912 glyph->type = IMAGE_GLYPH;
23913 glyph->avoid_cursor_p = it->avoid_cursor_p;
23914 glyph->multibyte_p = it->multibyte_p;
23915 glyph->left_box_line_p = it->start_of_box_run_p;
23916 glyph->right_box_line_p = it->end_of_box_run_p;
23917 glyph->overlaps_vertically_p = 0;
23918 glyph->padding_p = 0;
23919 glyph->glyph_not_available_p = 0;
23920 glyph->face_id = it->face_id;
23921 glyph->u.img_id = img->id;
23922 glyph->slice.img = slice;
23923 glyph->font_type = FONT_TYPE_UNKNOWN;
23924 if (it->bidi_p)
23925 {
23926 glyph->resolved_level = it->bidi_it.resolved_level;
23927 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23928 abort ();
23929 glyph->bidi_type = it->bidi_it.type;
23930 }
23931 ++it->glyph_row->used[area];
23932 }
23933 else
23934 IT_EXPAND_MATRIX_WIDTH (it, area);
23935 }
23936 }
23937
23938
23939 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23940 of the glyph, WIDTH and HEIGHT are the width and height of the
23941 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23942
23943 static void
23944 append_stretch_glyph (struct it *it, Lisp_Object object,
23945 int width, int height, int ascent)
23946 {
23947 struct glyph *glyph;
23948 enum glyph_row_area area = it->area;
23949
23950 eassert (ascent >= 0 && ascent <= height);
23951
23952 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23953 if (glyph < it->glyph_row->glyphs[area + 1])
23954 {
23955 /* If the glyph row is reversed, we need to prepend the glyph
23956 rather than append it. */
23957 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23958 {
23959 struct glyph *g;
23960
23961 /* Make room for the additional glyph. */
23962 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23963 g[1] = *g;
23964 glyph = it->glyph_row->glyphs[area];
23965 }
23966 glyph->charpos = CHARPOS (it->position);
23967 glyph->object = object;
23968 glyph->pixel_width = width;
23969 glyph->ascent = ascent;
23970 glyph->descent = height - ascent;
23971 glyph->voffset = it->voffset;
23972 glyph->type = STRETCH_GLYPH;
23973 glyph->avoid_cursor_p = it->avoid_cursor_p;
23974 glyph->multibyte_p = it->multibyte_p;
23975 glyph->left_box_line_p = it->start_of_box_run_p;
23976 glyph->right_box_line_p = it->end_of_box_run_p;
23977 glyph->overlaps_vertically_p = 0;
23978 glyph->padding_p = 0;
23979 glyph->glyph_not_available_p = 0;
23980 glyph->face_id = it->face_id;
23981 glyph->u.stretch.ascent = ascent;
23982 glyph->u.stretch.height = height;
23983 glyph->slice.img = null_glyph_slice;
23984 glyph->font_type = FONT_TYPE_UNKNOWN;
23985 if (it->bidi_p)
23986 {
23987 glyph->resolved_level = it->bidi_it.resolved_level;
23988 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23989 abort ();
23990 glyph->bidi_type = it->bidi_it.type;
23991 }
23992 else
23993 {
23994 glyph->resolved_level = 0;
23995 glyph->bidi_type = UNKNOWN_BT;
23996 }
23997 ++it->glyph_row->used[area];
23998 }
23999 else
24000 IT_EXPAND_MATRIX_WIDTH (it, area);
24001 }
24002
24003 #endif /* HAVE_WINDOW_SYSTEM */
24004
24005 /* Produce a stretch glyph for iterator IT. IT->object is the value
24006 of the glyph property displayed. The value must be a list
24007 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24008 being recognized:
24009
24010 1. `:width WIDTH' specifies that the space should be WIDTH *
24011 canonical char width wide. WIDTH may be an integer or floating
24012 point number.
24013
24014 2. `:relative-width FACTOR' specifies that the width of the stretch
24015 should be computed from the width of the first character having the
24016 `glyph' property, and should be FACTOR times that width.
24017
24018 3. `:align-to HPOS' specifies that the space should be wide enough
24019 to reach HPOS, a value in canonical character units.
24020
24021 Exactly one of the above pairs must be present.
24022
24023 4. `:height HEIGHT' specifies that the height of the stretch produced
24024 should be HEIGHT, measured in canonical character units.
24025
24026 5. `:relative-height FACTOR' specifies that the height of the
24027 stretch should be FACTOR times the height of the characters having
24028 the glyph property.
24029
24030 Either none or exactly one of 4 or 5 must be present.
24031
24032 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24033 of the stretch should be used for the ascent of the stretch.
24034 ASCENT must be in the range 0 <= ASCENT <= 100. */
24035
24036 void
24037 produce_stretch_glyph (struct it *it)
24038 {
24039 /* (space :width WIDTH :height HEIGHT ...) */
24040 Lisp_Object prop, plist;
24041 int width = 0, height = 0, align_to = -1;
24042 int zero_width_ok_p = 0;
24043 int ascent = 0;
24044 double tem;
24045 struct face *face = NULL;
24046 struct font *font = NULL;
24047
24048 #ifdef HAVE_WINDOW_SYSTEM
24049 int zero_height_ok_p = 0;
24050
24051 if (FRAME_WINDOW_P (it->f))
24052 {
24053 face = FACE_FROM_ID (it->f, it->face_id);
24054 font = face->font ? face->font : FRAME_FONT (it->f);
24055 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24056 }
24057 #endif
24058
24059 /* List should start with `space'. */
24060 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24061 plist = XCDR (it->object);
24062
24063 /* Compute the width of the stretch. */
24064 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24065 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24066 {
24067 /* Absolute width `:width WIDTH' specified and valid. */
24068 zero_width_ok_p = 1;
24069 width = (int)tem;
24070 }
24071 #ifdef HAVE_WINDOW_SYSTEM
24072 else if (FRAME_WINDOW_P (it->f)
24073 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24074 {
24075 /* Relative width `:relative-width FACTOR' specified and valid.
24076 Compute the width of the characters having the `glyph'
24077 property. */
24078 struct it it2;
24079 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24080
24081 it2 = *it;
24082 if (it->multibyte_p)
24083 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24084 else
24085 {
24086 it2.c = it2.char_to_display = *p, it2.len = 1;
24087 if (! ASCII_CHAR_P (it2.c))
24088 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24089 }
24090
24091 it2.glyph_row = NULL;
24092 it2.what = IT_CHARACTER;
24093 x_produce_glyphs (&it2);
24094 width = NUMVAL (prop) * it2.pixel_width;
24095 }
24096 #endif /* HAVE_WINDOW_SYSTEM */
24097 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24098 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24099 {
24100 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24101 align_to = (align_to < 0
24102 ? 0
24103 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24104 else if (align_to < 0)
24105 align_to = window_box_left_offset (it->w, TEXT_AREA);
24106 width = max (0, (int)tem + align_to - it->current_x);
24107 zero_width_ok_p = 1;
24108 }
24109 else
24110 /* Nothing specified -> width defaults to canonical char width. */
24111 width = FRAME_COLUMN_WIDTH (it->f);
24112
24113 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24114 width = 1;
24115
24116 #ifdef HAVE_WINDOW_SYSTEM
24117 /* Compute height. */
24118 if (FRAME_WINDOW_P (it->f))
24119 {
24120 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24121 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24122 {
24123 height = (int)tem;
24124 zero_height_ok_p = 1;
24125 }
24126 else if (prop = Fplist_get (plist, QCrelative_height),
24127 NUMVAL (prop) > 0)
24128 height = FONT_HEIGHT (font) * NUMVAL (prop);
24129 else
24130 height = FONT_HEIGHT (font);
24131
24132 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24133 height = 1;
24134
24135 /* Compute percentage of height used for ascent. If
24136 `:ascent ASCENT' is present and valid, use that. Otherwise,
24137 derive the ascent from the font in use. */
24138 if (prop = Fplist_get (plist, QCascent),
24139 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24140 ascent = height * NUMVAL (prop) / 100.0;
24141 else if (!NILP (prop)
24142 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24143 ascent = min (max (0, (int)tem), height);
24144 else
24145 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24146 }
24147 else
24148 #endif /* HAVE_WINDOW_SYSTEM */
24149 height = 1;
24150
24151 if (width > 0 && it->line_wrap != TRUNCATE
24152 && it->current_x + width > it->last_visible_x)
24153 {
24154 width = it->last_visible_x - it->current_x;
24155 #ifdef HAVE_WINDOW_SYSTEM
24156 /* Subtract one more pixel from the stretch width, but only on
24157 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24158 width -= FRAME_WINDOW_P (it->f);
24159 #endif
24160 }
24161
24162 if (width > 0 && height > 0 && it->glyph_row)
24163 {
24164 Lisp_Object o_object = it->object;
24165 Lisp_Object object = it->stack[it->sp - 1].string;
24166 int n = width;
24167
24168 if (!STRINGP (object))
24169 object = it->w->buffer;
24170 #ifdef HAVE_WINDOW_SYSTEM
24171 if (FRAME_WINDOW_P (it->f))
24172 append_stretch_glyph (it, object, width, height, ascent);
24173 else
24174 #endif
24175 {
24176 it->object = object;
24177 it->char_to_display = ' ';
24178 it->pixel_width = it->len = 1;
24179 while (n--)
24180 tty_append_glyph (it);
24181 it->object = o_object;
24182 }
24183 }
24184
24185 it->pixel_width = width;
24186 #ifdef HAVE_WINDOW_SYSTEM
24187 if (FRAME_WINDOW_P (it->f))
24188 {
24189 it->ascent = it->phys_ascent = ascent;
24190 it->descent = it->phys_descent = height - it->ascent;
24191 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24192 take_vertical_position_into_account (it);
24193 }
24194 else
24195 #endif
24196 it->nglyphs = width;
24197 }
24198
24199 #ifdef HAVE_WINDOW_SYSTEM
24200
24201 /* Calculate line-height and line-spacing properties.
24202 An integer value specifies explicit pixel value.
24203 A float value specifies relative value to current face height.
24204 A cons (float . face-name) specifies relative value to
24205 height of specified face font.
24206
24207 Returns height in pixels, or nil. */
24208
24209
24210 static Lisp_Object
24211 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24212 int boff, int override)
24213 {
24214 Lisp_Object face_name = Qnil;
24215 int ascent, descent, height;
24216
24217 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24218 return val;
24219
24220 if (CONSP (val))
24221 {
24222 face_name = XCAR (val);
24223 val = XCDR (val);
24224 if (!NUMBERP (val))
24225 val = make_number (1);
24226 if (NILP (face_name))
24227 {
24228 height = it->ascent + it->descent;
24229 goto scale;
24230 }
24231 }
24232
24233 if (NILP (face_name))
24234 {
24235 font = FRAME_FONT (it->f);
24236 boff = FRAME_BASELINE_OFFSET (it->f);
24237 }
24238 else if (EQ (face_name, Qt))
24239 {
24240 override = 0;
24241 }
24242 else
24243 {
24244 int face_id;
24245 struct face *face;
24246
24247 face_id = lookup_named_face (it->f, face_name, 0);
24248 if (face_id < 0)
24249 return make_number (-1);
24250
24251 face = FACE_FROM_ID (it->f, face_id);
24252 font = face->font;
24253 if (font == NULL)
24254 return make_number (-1);
24255 boff = font->baseline_offset;
24256 if (font->vertical_centering)
24257 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24258 }
24259
24260 ascent = FONT_BASE (font) + boff;
24261 descent = FONT_DESCENT (font) - boff;
24262
24263 if (override)
24264 {
24265 it->override_ascent = ascent;
24266 it->override_descent = descent;
24267 it->override_boff = boff;
24268 }
24269
24270 height = ascent + descent;
24271
24272 scale:
24273 if (FLOATP (val))
24274 height = (int)(XFLOAT_DATA (val) * height);
24275 else if (INTEGERP (val))
24276 height *= XINT (val);
24277
24278 return make_number (height);
24279 }
24280
24281
24282 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24283 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24284 and only if this is for a character for which no font was found.
24285
24286 If the display method (it->glyphless_method) is
24287 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24288 length of the acronym or the hexadecimal string, UPPER_XOFF and
24289 UPPER_YOFF are pixel offsets for the upper part of the string,
24290 LOWER_XOFF and LOWER_YOFF are for the lower part.
24291
24292 For the other display methods, LEN through LOWER_YOFF are zero. */
24293
24294 static void
24295 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24296 short upper_xoff, short upper_yoff,
24297 short lower_xoff, short lower_yoff)
24298 {
24299 struct glyph *glyph;
24300 enum glyph_row_area area = it->area;
24301
24302 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24303 if (glyph < it->glyph_row->glyphs[area + 1])
24304 {
24305 /* If the glyph row is reversed, we need to prepend the glyph
24306 rather than append it. */
24307 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24308 {
24309 struct glyph *g;
24310
24311 /* Make room for the additional glyph. */
24312 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24313 g[1] = *g;
24314 glyph = it->glyph_row->glyphs[area];
24315 }
24316 glyph->charpos = CHARPOS (it->position);
24317 glyph->object = it->object;
24318 glyph->pixel_width = it->pixel_width;
24319 glyph->ascent = it->ascent;
24320 glyph->descent = it->descent;
24321 glyph->voffset = it->voffset;
24322 glyph->type = GLYPHLESS_GLYPH;
24323 glyph->u.glyphless.method = it->glyphless_method;
24324 glyph->u.glyphless.for_no_font = for_no_font;
24325 glyph->u.glyphless.len = len;
24326 glyph->u.glyphless.ch = it->c;
24327 glyph->slice.glyphless.upper_xoff = upper_xoff;
24328 glyph->slice.glyphless.upper_yoff = upper_yoff;
24329 glyph->slice.glyphless.lower_xoff = lower_xoff;
24330 glyph->slice.glyphless.lower_yoff = lower_yoff;
24331 glyph->avoid_cursor_p = it->avoid_cursor_p;
24332 glyph->multibyte_p = it->multibyte_p;
24333 glyph->left_box_line_p = it->start_of_box_run_p;
24334 glyph->right_box_line_p = it->end_of_box_run_p;
24335 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24336 || it->phys_descent > it->descent);
24337 glyph->padding_p = 0;
24338 glyph->glyph_not_available_p = 0;
24339 glyph->face_id = face_id;
24340 glyph->font_type = FONT_TYPE_UNKNOWN;
24341 if (it->bidi_p)
24342 {
24343 glyph->resolved_level = it->bidi_it.resolved_level;
24344 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24345 abort ();
24346 glyph->bidi_type = it->bidi_it.type;
24347 }
24348 ++it->glyph_row->used[area];
24349 }
24350 else
24351 IT_EXPAND_MATRIX_WIDTH (it, area);
24352 }
24353
24354
24355 /* Produce a glyph for a glyphless character for iterator IT.
24356 IT->glyphless_method specifies which method to use for displaying
24357 the character. See the description of enum
24358 glyphless_display_method in dispextern.h for the detail.
24359
24360 FOR_NO_FONT is nonzero if and only if this is for a character for
24361 which no font was found. ACRONYM, if non-nil, is an acronym string
24362 for the character. */
24363
24364 static void
24365 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24366 {
24367 int face_id;
24368 struct face *face;
24369 struct font *font;
24370 int base_width, base_height, width, height;
24371 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24372 int len;
24373
24374 /* Get the metrics of the base font. We always refer to the current
24375 ASCII face. */
24376 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24377 font = face->font ? face->font : FRAME_FONT (it->f);
24378 it->ascent = FONT_BASE (font) + font->baseline_offset;
24379 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24380 base_height = it->ascent + it->descent;
24381 base_width = font->average_width;
24382
24383 /* Get a face ID for the glyph by utilizing a cache (the same way as
24384 done for `escape-glyph' in get_next_display_element). */
24385 if (it->f == last_glyphless_glyph_frame
24386 && it->face_id == last_glyphless_glyph_face_id)
24387 {
24388 face_id = last_glyphless_glyph_merged_face_id;
24389 }
24390 else
24391 {
24392 /* Merge the `glyphless-char' face into the current face. */
24393 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24394 last_glyphless_glyph_frame = it->f;
24395 last_glyphless_glyph_face_id = it->face_id;
24396 last_glyphless_glyph_merged_face_id = face_id;
24397 }
24398
24399 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24400 {
24401 it->pixel_width = THIN_SPACE_WIDTH;
24402 len = 0;
24403 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24404 }
24405 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24406 {
24407 width = CHAR_WIDTH (it->c);
24408 if (width == 0)
24409 width = 1;
24410 else if (width > 4)
24411 width = 4;
24412 it->pixel_width = base_width * width;
24413 len = 0;
24414 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24415 }
24416 else
24417 {
24418 char buf[7];
24419 const char *str;
24420 unsigned int code[6];
24421 int upper_len;
24422 int ascent, descent;
24423 struct font_metrics metrics_upper, metrics_lower;
24424
24425 face = FACE_FROM_ID (it->f, face_id);
24426 font = face->font ? face->font : FRAME_FONT (it->f);
24427 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24428
24429 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24430 {
24431 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24432 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24433 if (CONSP (acronym))
24434 acronym = XCAR (acronym);
24435 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24436 }
24437 else
24438 {
24439 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24440 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24441 str = buf;
24442 }
24443 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24444 code[len] = font->driver->encode_char (font, str[len]);
24445 upper_len = (len + 1) / 2;
24446 font->driver->text_extents (font, code, upper_len,
24447 &metrics_upper);
24448 font->driver->text_extents (font, code + upper_len, len - upper_len,
24449 &metrics_lower);
24450
24451
24452
24453 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24454 width = max (metrics_upper.width, metrics_lower.width) + 4;
24455 upper_xoff = upper_yoff = 2; /* the typical case */
24456 if (base_width >= width)
24457 {
24458 /* Align the upper to the left, the lower to the right. */
24459 it->pixel_width = base_width;
24460 lower_xoff = base_width - 2 - metrics_lower.width;
24461 }
24462 else
24463 {
24464 /* Center the shorter one. */
24465 it->pixel_width = width;
24466 if (metrics_upper.width >= metrics_lower.width)
24467 lower_xoff = (width - metrics_lower.width) / 2;
24468 else
24469 {
24470 /* FIXME: This code doesn't look right. It formerly was
24471 missing the "lower_xoff = 0;", which couldn't have
24472 been right since it left lower_xoff uninitialized. */
24473 lower_xoff = 0;
24474 upper_xoff = (width - metrics_upper.width) / 2;
24475 }
24476 }
24477
24478 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24479 top, bottom, and between upper and lower strings. */
24480 height = (metrics_upper.ascent + metrics_upper.descent
24481 + metrics_lower.ascent + metrics_lower.descent) + 5;
24482 /* Center vertically.
24483 H:base_height, D:base_descent
24484 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24485
24486 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24487 descent = D - H/2 + h/2;
24488 lower_yoff = descent - 2 - ld;
24489 upper_yoff = lower_yoff - la - 1 - ud; */
24490 ascent = - (it->descent - (base_height + height + 1) / 2);
24491 descent = it->descent - (base_height - height) / 2;
24492 lower_yoff = descent - 2 - metrics_lower.descent;
24493 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24494 - metrics_upper.descent);
24495 /* Don't make the height shorter than the base height. */
24496 if (height > base_height)
24497 {
24498 it->ascent = ascent;
24499 it->descent = descent;
24500 }
24501 }
24502
24503 it->phys_ascent = it->ascent;
24504 it->phys_descent = it->descent;
24505 if (it->glyph_row)
24506 append_glyphless_glyph (it, face_id, for_no_font, len,
24507 upper_xoff, upper_yoff,
24508 lower_xoff, lower_yoff);
24509 it->nglyphs = 1;
24510 take_vertical_position_into_account (it);
24511 }
24512
24513
24514 /* RIF:
24515 Produce glyphs/get display metrics for the display element IT is
24516 loaded with. See the description of struct it in dispextern.h
24517 for an overview of struct it. */
24518
24519 void
24520 x_produce_glyphs (struct it *it)
24521 {
24522 int extra_line_spacing = it->extra_line_spacing;
24523
24524 it->glyph_not_available_p = 0;
24525
24526 if (it->what == IT_CHARACTER)
24527 {
24528 XChar2b char2b;
24529 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24530 struct font *font = face->font;
24531 struct font_metrics *pcm = NULL;
24532 int boff; /* baseline offset */
24533
24534 if (font == NULL)
24535 {
24536 /* When no suitable font is found, display this character by
24537 the method specified in the first extra slot of
24538 Vglyphless_char_display. */
24539 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24540
24541 eassert (it->what == IT_GLYPHLESS);
24542 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24543 goto done;
24544 }
24545
24546 boff = font->baseline_offset;
24547 if (font->vertical_centering)
24548 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24549
24550 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24551 {
24552 int stretched_p;
24553
24554 it->nglyphs = 1;
24555
24556 if (it->override_ascent >= 0)
24557 {
24558 it->ascent = it->override_ascent;
24559 it->descent = it->override_descent;
24560 boff = it->override_boff;
24561 }
24562 else
24563 {
24564 it->ascent = FONT_BASE (font) + boff;
24565 it->descent = FONT_DESCENT (font) - boff;
24566 }
24567
24568 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24569 {
24570 pcm = get_per_char_metric (font, &char2b);
24571 if (pcm->width == 0
24572 && pcm->rbearing == 0 && pcm->lbearing == 0)
24573 pcm = NULL;
24574 }
24575
24576 if (pcm)
24577 {
24578 it->phys_ascent = pcm->ascent + boff;
24579 it->phys_descent = pcm->descent - boff;
24580 it->pixel_width = pcm->width;
24581 }
24582 else
24583 {
24584 it->glyph_not_available_p = 1;
24585 it->phys_ascent = it->ascent;
24586 it->phys_descent = it->descent;
24587 it->pixel_width = font->space_width;
24588 }
24589
24590 if (it->constrain_row_ascent_descent_p)
24591 {
24592 if (it->descent > it->max_descent)
24593 {
24594 it->ascent += it->descent - it->max_descent;
24595 it->descent = it->max_descent;
24596 }
24597 if (it->ascent > it->max_ascent)
24598 {
24599 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24600 it->ascent = it->max_ascent;
24601 }
24602 it->phys_ascent = min (it->phys_ascent, it->ascent);
24603 it->phys_descent = min (it->phys_descent, it->descent);
24604 extra_line_spacing = 0;
24605 }
24606
24607 /* If this is a space inside a region of text with
24608 `space-width' property, change its width. */
24609 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24610 if (stretched_p)
24611 it->pixel_width *= XFLOATINT (it->space_width);
24612
24613 /* If face has a box, add the box thickness to the character
24614 height. If character has a box line to the left and/or
24615 right, add the box line width to the character's width. */
24616 if (face->box != FACE_NO_BOX)
24617 {
24618 int thick = face->box_line_width;
24619
24620 if (thick > 0)
24621 {
24622 it->ascent += thick;
24623 it->descent += thick;
24624 }
24625 else
24626 thick = -thick;
24627
24628 if (it->start_of_box_run_p)
24629 it->pixel_width += thick;
24630 if (it->end_of_box_run_p)
24631 it->pixel_width += thick;
24632 }
24633
24634 /* If face has an overline, add the height of the overline
24635 (1 pixel) and a 1 pixel margin to the character height. */
24636 if (face->overline_p)
24637 it->ascent += overline_margin;
24638
24639 if (it->constrain_row_ascent_descent_p)
24640 {
24641 if (it->ascent > it->max_ascent)
24642 it->ascent = it->max_ascent;
24643 if (it->descent > it->max_descent)
24644 it->descent = it->max_descent;
24645 }
24646
24647 take_vertical_position_into_account (it);
24648
24649 /* If we have to actually produce glyphs, do it. */
24650 if (it->glyph_row)
24651 {
24652 if (stretched_p)
24653 {
24654 /* Translate a space with a `space-width' property
24655 into a stretch glyph. */
24656 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24657 / FONT_HEIGHT (font));
24658 append_stretch_glyph (it, it->object, it->pixel_width,
24659 it->ascent + it->descent, ascent);
24660 }
24661 else
24662 append_glyph (it);
24663
24664 /* If characters with lbearing or rbearing are displayed
24665 in this line, record that fact in a flag of the
24666 glyph row. This is used to optimize X output code. */
24667 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24668 it->glyph_row->contains_overlapping_glyphs_p = 1;
24669 }
24670 if (! stretched_p && it->pixel_width == 0)
24671 /* We assure that all visible glyphs have at least 1-pixel
24672 width. */
24673 it->pixel_width = 1;
24674 }
24675 else if (it->char_to_display == '\n')
24676 {
24677 /* A newline has no width, but we need the height of the
24678 line. But if previous part of the line sets a height,
24679 don't increase that height */
24680
24681 Lisp_Object height;
24682 Lisp_Object total_height = Qnil;
24683
24684 it->override_ascent = -1;
24685 it->pixel_width = 0;
24686 it->nglyphs = 0;
24687
24688 height = get_it_property (it, Qline_height);
24689 /* Split (line-height total-height) list */
24690 if (CONSP (height)
24691 && CONSP (XCDR (height))
24692 && NILP (XCDR (XCDR (height))))
24693 {
24694 total_height = XCAR (XCDR (height));
24695 height = XCAR (height);
24696 }
24697 height = calc_line_height_property (it, height, font, boff, 1);
24698
24699 if (it->override_ascent >= 0)
24700 {
24701 it->ascent = it->override_ascent;
24702 it->descent = it->override_descent;
24703 boff = it->override_boff;
24704 }
24705 else
24706 {
24707 it->ascent = FONT_BASE (font) + boff;
24708 it->descent = FONT_DESCENT (font) - boff;
24709 }
24710
24711 if (EQ (height, Qt))
24712 {
24713 if (it->descent > it->max_descent)
24714 {
24715 it->ascent += it->descent - it->max_descent;
24716 it->descent = it->max_descent;
24717 }
24718 if (it->ascent > it->max_ascent)
24719 {
24720 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24721 it->ascent = it->max_ascent;
24722 }
24723 it->phys_ascent = min (it->phys_ascent, it->ascent);
24724 it->phys_descent = min (it->phys_descent, it->descent);
24725 it->constrain_row_ascent_descent_p = 1;
24726 extra_line_spacing = 0;
24727 }
24728 else
24729 {
24730 Lisp_Object spacing;
24731
24732 it->phys_ascent = it->ascent;
24733 it->phys_descent = it->descent;
24734
24735 if ((it->max_ascent > 0 || it->max_descent > 0)
24736 && face->box != FACE_NO_BOX
24737 && face->box_line_width > 0)
24738 {
24739 it->ascent += face->box_line_width;
24740 it->descent += face->box_line_width;
24741 }
24742 if (!NILP (height)
24743 && XINT (height) > it->ascent + it->descent)
24744 it->ascent = XINT (height) - it->descent;
24745
24746 if (!NILP (total_height))
24747 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24748 else
24749 {
24750 spacing = get_it_property (it, Qline_spacing);
24751 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24752 }
24753 if (INTEGERP (spacing))
24754 {
24755 extra_line_spacing = XINT (spacing);
24756 if (!NILP (total_height))
24757 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24758 }
24759 }
24760 }
24761 else /* i.e. (it->char_to_display == '\t') */
24762 {
24763 if (font->space_width > 0)
24764 {
24765 int tab_width = it->tab_width * font->space_width;
24766 int x = it->current_x + it->continuation_lines_width;
24767 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24768
24769 /* If the distance from the current position to the next tab
24770 stop is less than a space character width, use the
24771 tab stop after that. */
24772 if (next_tab_x - x < font->space_width)
24773 next_tab_x += tab_width;
24774
24775 it->pixel_width = next_tab_x - x;
24776 it->nglyphs = 1;
24777 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24778 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24779
24780 if (it->glyph_row)
24781 {
24782 append_stretch_glyph (it, it->object, it->pixel_width,
24783 it->ascent + it->descent, it->ascent);
24784 }
24785 }
24786 else
24787 {
24788 it->pixel_width = 0;
24789 it->nglyphs = 1;
24790 }
24791 }
24792 }
24793 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24794 {
24795 /* A static composition.
24796
24797 Note: A composition is represented as one glyph in the
24798 glyph matrix. There are no padding glyphs.
24799
24800 Important note: pixel_width, ascent, and descent are the
24801 values of what is drawn by draw_glyphs (i.e. the values of
24802 the overall glyphs composed). */
24803 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24804 int boff; /* baseline offset */
24805 struct composition *cmp = composition_table[it->cmp_it.id];
24806 int glyph_len = cmp->glyph_len;
24807 struct font *font = face->font;
24808
24809 it->nglyphs = 1;
24810
24811 /* If we have not yet calculated pixel size data of glyphs of
24812 the composition for the current face font, calculate them
24813 now. Theoretically, we have to check all fonts for the
24814 glyphs, but that requires much time and memory space. So,
24815 here we check only the font of the first glyph. This may
24816 lead to incorrect display, but it's very rare, and C-l
24817 (recenter-top-bottom) can correct the display anyway. */
24818 if (! cmp->font || cmp->font != font)
24819 {
24820 /* Ascent and descent of the font of the first character
24821 of this composition (adjusted by baseline offset).
24822 Ascent and descent of overall glyphs should not be less
24823 than these, respectively. */
24824 int font_ascent, font_descent, font_height;
24825 /* Bounding box of the overall glyphs. */
24826 int leftmost, rightmost, lowest, highest;
24827 int lbearing, rbearing;
24828 int i, width, ascent, descent;
24829 int left_padded = 0, right_padded = 0;
24830 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24831 XChar2b char2b;
24832 struct font_metrics *pcm;
24833 int font_not_found_p;
24834 ptrdiff_t pos;
24835
24836 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24837 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24838 break;
24839 if (glyph_len < cmp->glyph_len)
24840 right_padded = 1;
24841 for (i = 0; i < glyph_len; i++)
24842 {
24843 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24844 break;
24845 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24846 }
24847 if (i > 0)
24848 left_padded = 1;
24849
24850 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24851 : IT_CHARPOS (*it));
24852 /* If no suitable font is found, use the default font. */
24853 font_not_found_p = font == NULL;
24854 if (font_not_found_p)
24855 {
24856 face = face->ascii_face;
24857 font = face->font;
24858 }
24859 boff = font->baseline_offset;
24860 if (font->vertical_centering)
24861 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24862 font_ascent = FONT_BASE (font) + boff;
24863 font_descent = FONT_DESCENT (font) - boff;
24864 font_height = FONT_HEIGHT (font);
24865
24866 cmp->font = (void *) font;
24867
24868 pcm = NULL;
24869 if (! font_not_found_p)
24870 {
24871 get_char_face_and_encoding (it->f, c, it->face_id,
24872 &char2b, 0);
24873 pcm = get_per_char_metric (font, &char2b);
24874 }
24875
24876 /* Initialize the bounding box. */
24877 if (pcm)
24878 {
24879 width = cmp->glyph_len > 0 ? pcm->width : 0;
24880 ascent = pcm->ascent;
24881 descent = pcm->descent;
24882 lbearing = pcm->lbearing;
24883 rbearing = pcm->rbearing;
24884 }
24885 else
24886 {
24887 width = cmp->glyph_len > 0 ? font->space_width : 0;
24888 ascent = FONT_BASE (font);
24889 descent = FONT_DESCENT (font);
24890 lbearing = 0;
24891 rbearing = width;
24892 }
24893
24894 rightmost = width;
24895 leftmost = 0;
24896 lowest = - descent + boff;
24897 highest = ascent + boff;
24898
24899 if (! font_not_found_p
24900 && font->default_ascent
24901 && CHAR_TABLE_P (Vuse_default_ascent)
24902 && !NILP (Faref (Vuse_default_ascent,
24903 make_number (it->char_to_display))))
24904 highest = font->default_ascent + boff;
24905
24906 /* Draw the first glyph at the normal position. It may be
24907 shifted to right later if some other glyphs are drawn
24908 at the left. */
24909 cmp->offsets[i * 2] = 0;
24910 cmp->offsets[i * 2 + 1] = boff;
24911 cmp->lbearing = lbearing;
24912 cmp->rbearing = rbearing;
24913
24914 /* Set cmp->offsets for the remaining glyphs. */
24915 for (i++; i < glyph_len; i++)
24916 {
24917 int left, right, btm, top;
24918 int ch = COMPOSITION_GLYPH (cmp, i);
24919 int face_id;
24920 struct face *this_face;
24921
24922 if (ch == '\t')
24923 ch = ' ';
24924 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24925 this_face = FACE_FROM_ID (it->f, face_id);
24926 font = this_face->font;
24927
24928 if (font == NULL)
24929 pcm = NULL;
24930 else
24931 {
24932 get_char_face_and_encoding (it->f, ch, face_id,
24933 &char2b, 0);
24934 pcm = get_per_char_metric (font, &char2b);
24935 }
24936 if (! pcm)
24937 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24938 else
24939 {
24940 width = pcm->width;
24941 ascent = pcm->ascent;
24942 descent = pcm->descent;
24943 lbearing = pcm->lbearing;
24944 rbearing = pcm->rbearing;
24945 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24946 {
24947 /* Relative composition with or without
24948 alternate chars. */
24949 left = (leftmost + rightmost - width) / 2;
24950 btm = - descent + boff;
24951 if (font->relative_compose
24952 && (! CHAR_TABLE_P (Vignore_relative_composition)
24953 || NILP (Faref (Vignore_relative_composition,
24954 make_number (ch)))))
24955 {
24956
24957 if (- descent >= font->relative_compose)
24958 /* One extra pixel between two glyphs. */
24959 btm = highest + 1;
24960 else if (ascent <= 0)
24961 /* One extra pixel between two glyphs. */
24962 btm = lowest - 1 - ascent - descent;
24963 }
24964 }
24965 else
24966 {
24967 /* A composition rule is specified by an integer
24968 value that encodes global and new reference
24969 points (GREF and NREF). GREF and NREF are
24970 specified by numbers as below:
24971
24972 0---1---2 -- ascent
24973 | |
24974 | |
24975 | |
24976 9--10--11 -- center
24977 | |
24978 ---3---4---5--- baseline
24979 | |
24980 6---7---8 -- descent
24981 */
24982 int rule = COMPOSITION_RULE (cmp, i);
24983 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24984
24985 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24986 grefx = gref % 3, nrefx = nref % 3;
24987 grefy = gref / 3, nrefy = nref / 3;
24988 if (xoff)
24989 xoff = font_height * (xoff - 128) / 256;
24990 if (yoff)
24991 yoff = font_height * (yoff - 128) / 256;
24992
24993 left = (leftmost
24994 + grefx * (rightmost - leftmost) / 2
24995 - nrefx * width / 2
24996 + xoff);
24997
24998 btm = ((grefy == 0 ? highest
24999 : grefy == 1 ? 0
25000 : grefy == 2 ? lowest
25001 : (highest + lowest) / 2)
25002 - (nrefy == 0 ? ascent + descent
25003 : nrefy == 1 ? descent - boff
25004 : nrefy == 2 ? 0
25005 : (ascent + descent) / 2)
25006 + yoff);
25007 }
25008
25009 cmp->offsets[i * 2] = left;
25010 cmp->offsets[i * 2 + 1] = btm + descent;
25011
25012 /* Update the bounding box of the overall glyphs. */
25013 if (width > 0)
25014 {
25015 right = left + width;
25016 if (left < leftmost)
25017 leftmost = left;
25018 if (right > rightmost)
25019 rightmost = right;
25020 }
25021 top = btm + descent + ascent;
25022 if (top > highest)
25023 highest = top;
25024 if (btm < lowest)
25025 lowest = btm;
25026
25027 if (cmp->lbearing > left + lbearing)
25028 cmp->lbearing = left + lbearing;
25029 if (cmp->rbearing < left + rbearing)
25030 cmp->rbearing = left + rbearing;
25031 }
25032 }
25033
25034 /* If there are glyphs whose x-offsets are negative,
25035 shift all glyphs to the right and make all x-offsets
25036 non-negative. */
25037 if (leftmost < 0)
25038 {
25039 for (i = 0; i < cmp->glyph_len; i++)
25040 cmp->offsets[i * 2] -= leftmost;
25041 rightmost -= leftmost;
25042 cmp->lbearing -= leftmost;
25043 cmp->rbearing -= leftmost;
25044 }
25045
25046 if (left_padded && cmp->lbearing < 0)
25047 {
25048 for (i = 0; i < cmp->glyph_len; i++)
25049 cmp->offsets[i * 2] -= cmp->lbearing;
25050 rightmost -= cmp->lbearing;
25051 cmp->rbearing -= cmp->lbearing;
25052 cmp->lbearing = 0;
25053 }
25054 if (right_padded && rightmost < cmp->rbearing)
25055 {
25056 rightmost = cmp->rbearing;
25057 }
25058
25059 cmp->pixel_width = rightmost;
25060 cmp->ascent = highest;
25061 cmp->descent = - lowest;
25062 if (cmp->ascent < font_ascent)
25063 cmp->ascent = font_ascent;
25064 if (cmp->descent < font_descent)
25065 cmp->descent = font_descent;
25066 }
25067
25068 if (it->glyph_row
25069 && (cmp->lbearing < 0
25070 || cmp->rbearing > cmp->pixel_width))
25071 it->glyph_row->contains_overlapping_glyphs_p = 1;
25072
25073 it->pixel_width = cmp->pixel_width;
25074 it->ascent = it->phys_ascent = cmp->ascent;
25075 it->descent = it->phys_descent = cmp->descent;
25076 if (face->box != FACE_NO_BOX)
25077 {
25078 int thick = face->box_line_width;
25079
25080 if (thick > 0)
25081 {
25082 it->ascent += thick;
25083 it->descent += thick;
25084 }
25085 else
25086 thick = - thick;
25087
25088 if (it->start_of_box_run_p)
25089 it->pixel_width += thick;
25090 if (it->end_of_box_run_p)
25091 it->pixel_width += thick;
25092 }
25093
25094 /* If face has an overline, add the height of the overline
25095 (1 pixel) and a 1 pixel margin to the character height. */
25096 if (face->overline_p)
25097 it->ascent += overline_margin;
25098
25099 take_vertical_position_into_account (it);
25100 if (it->ascent < 0)
25101 it->ascent = 0;
25102 if (it->descent < 0)
25103 it->descent = 0;
25104
25105 if (it->glyph_row && cmp->glyph_len > 0)
25106 append_composite_glyph (it);
25107 }
25108 else if (it->what == IT_COMPOSITION)
25109 {
25110 /* A dynamic (automatic) composition. */
25111 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25112 Lisp_Object gstring;
25113 struct font_metrics metrics;
25114
25115 it->nglyphs = 1;
25116
25117 gstring = composition_gstring_from_id (it->cmp_it.id);
25118 it->pixel_width
25119 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25120 &metrics);
25121 if (it->glyph_row
25122 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25123 it->glyph_row->contains_overlapping_glyphs_p = 1;
25124 it->ascent = it->phys_ascent = metrics.ascent;
25125 it->descent = it->phys_descent = metrics.descent;
25126 if (face->box != FACE_NO_BOX)
25127 {
25128 int thick = face->box_line_width;
25129
25130 if (thick > 0)
25131 {
25132 it->ascent += thick;
25133 it->descent += thick;
25134 }
25135 else
25136 thick = - thick;
25137
25138 if (it->start_of_box_run_p)
25139 it->pixel_width += thick;
25140 if (it->end_of_box_run_p)
25141 it->pixel_width += thick;
25142 }
25143 /* If face has an overline, add the height of the overline
25144 (1 pixel) and a 1 pixel margin to the character height. */
25145 if (face->overline_p)
25146 it->ascent += overline_margin;
25147 take_vertical_position_into_account (it);
25148 if (it->ascent < 0)
25149 it->ascent = 0;
25150 if (it->descent < 0)
25151 it->descent = 0;
25152
25153 if (it->glyph_row)
25154 append_composite_glyph (it);
25155 }
25156 else if (it->what == IT_GLYPHLESS)
25157 produce_glyphless_glyph (it, 0, Qnil);
25158 else if (it->what == IT_IMAGE)
25159 produce_image_glyph (it);
25160 else if (it->what == IT_STRETCH)
25161 produce_stretch_glyph (it);
25162
25163 done:
25164 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25165 because this isn't true for images with `:ascent 100'. */
25166 eassert (it->ascent >= 0 && it->descent >= 0);
25167 if (it->area == TEXT_AREA)
25168 it->current_x += it->pixel_width;
25169
25170 if (extra_line_spacing > 0)
25171 {
25172 it->descent += extra_line_spacing;
25173 if (extra_line_spacing > it->max_extra_line_spacing)
25174 it->max_extra_line_spacing = extra_line_spacing;
25175 }
25176
25177 it->max_ascent = max (it->max_ascent, it->ascent);
25178 it->max_descent = max (it->max_descent, it->descent);
25179 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25180 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25181 }
25182
25183 /* EXPORT for RIF:
25184 Output LEN glyphs starting at START at the nominal cursor position.
25185 Advance the nominal cursor over the text. The global variable
25186 updated_window contains the window being updated, updated_row is
25187 the glyph row being updated, and updated_area is the area of that
25188 row being updated. */
25189
25190 void
25191 x_write_glyphs (struct glyph *start, int len)
25192 {
25193 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25194
25195 eassert (updated_window && updated_row);
25196 /* When the window is hscrolled, cursor hpos can legitimately be out
25197 of bounds, but we draw the cursor at the corresponding window
25198 margin in that case. */
25199 if (!updated_row->reversed_p && chpos < 0)
25200 chpos = 0;
25201 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25202 chpos = updated_row->used[TEXT_AREA] - 1;
25203
25204 BLOCK_INPUT;
25205
25206 /* Write glyphs. */
25207
25208 hpos = start - updated_row->glyphs[updated_area];
25209 x = draw_glyphs (updated_window, output_cursor.x,
25210 updated_row, updated_area,
25211 hpos, hpos + len,
25212 DRAW_NORMAL_TEXT, 0);
25213
25214 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25215 if (updated_area == TEXT_AREA
25216 && updated_window->phys_cursor_on_p
25217 && updated_window->phys_cursor.vpos == output_cursor.vpos
25218 && chpos >= hpos
25219 && chpos < hpos + len)
25220 updated_window->phys_cursor_on_p = 0;
25221
25222 UNBLOCK_INPUT;
25223
25224 /* Advance the output cursor. */
25225 output_cursor.hpos += len;
25226 output_cursor.x = x;
25227 }
25228
25229
25230 /* EXPORT for RIF:
25231 Insert LEN glyphs from START at the nominal cursor position. */
25232
25233 void
25234 x_insert_glyphs (struct glyph *start, int len)
25235 {
25236 struct frame *f;
25237 struct window *w;
25238 int line_height, shift_by_width, shifted_region_width;
25239 struct glyph_row *row;
25240 struct glyph *glyph;
25241 int frame_x, frame_y;
25242 ptrdiff_t hpos;
25243
25244 eassert (updated_window && updated_row);
25245 BLOCK_INPUT;
25246 w = updated_window;
25247 f = XFRAME (WINDOW_FRAME (w));
25248
25249 /* Get the height of the line we are in. */
25250 row = updated_row;
25251 line_height = row->height;
25252
25253 /* Get the width of the glyphs to insert. */
25254 shift_by_width = 0;
25255 for (glyph = start; glyph < start + len; ++glyph)
25256 shift_by_width += glyph->pixel_width;
25257
25258 /* Get the width of the region to shift right. */
25259 shifted_region_width = (window_box_width (w, updated_area)
25260 - output_cursor.x
25261 - shift_by_width);
25262
25263 /* Shift right. */
25264 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25265 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25266
25267 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25268 line_height, shift_by_width);
25269
25270 /* Write the glyphs. */
25271 hpos = start - row->glyphs[updated_area];
25272 draw_glyphs (w, output_cursor.x, row, updated_area,
25273 hpos, hpos + len,
25274 DRAW_NORMAL_TEXT, 0);
25275
25276 /* Advance the output cursor. */
25277 output_cursor.hpos += len;
25278 output_cursor.x += shift_by_width;
25279 UNBLOCK_INPUT;
25280 }
25281
25282
25283 /* EXPORT for RIF:
25284 Erase the current text line from the nominal cursor position
25285 (inclusive) to pixel column TO_X (exclusive). The idea is that
25286 everything from TO_X onward is already erased.
25287
25288 TO_X is a pixel position relative to updated_area of
25289 updated_window. TO_X == -1 means clear to the end of this area. */
25290
25291 void
25292 x_clear_end_of_line (int to_x)
25293 {
25294 struct frame *f;
25295 struct window *w = updated_window;
25296 int max_x, min_y, max_y;
25297 int from_x, from_y, to_y;
25298
25299 eassert (updated_window && updated_row);
25300 f = XFRAME (w->frame);
25301
25302 if (updated_row->full_width_p)
25303 max_x = WINDOW_TOTAL_WIDTH (w);
25304 else
25305 max_x = window_box_width (w, updated_area);
25306 max_y = window_text_bottom_y (w);
25307
25308 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25309 of window. For TO_X > 0, truncate to end of drawing area. */
25310 if (to_x == 0)
25311 return;
25312 else if (to_x < 0)
25313 to_x = max_x;
25314 else
25315 to_x = min (to_x, max_x);
25316
25317 to_y = min (max_y, output_cursor.y + updated_row->height);
25318
25319 /* Notice if the cursor will be cleared by this operation. */
25320 if (!updated_row->full_width_p)
25321 notice_overwritten_cursor (w, updated_area,
25322 output_cursor.x, -1,
25323 updated_row->y,
25324 MATRIX_ROW_BOTTOM_Y (updated_row));
25325
25326 from_x = output_cursor.x;
25327
25328 /* Translate to frame coordinates. */
25329 if (updated_row->full_width_p)
25330 {
25331 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25332 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25333 }
25334 else
25335 {
25336 int area_left = window_box_left (w, updated_area);
25337 from_x += area_left;
25338 to_x += area_left;
25339 }
25340
25341 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25342 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25343 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25344
25345 /* Prevent inadvertently clearing to end of the X window. */
25346 if (to_x > from_x && to_y > from_y)
25347 {
25348 BLOCK_INPUT;
25349 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25350 to_x - from_x, to_y - from_y);
25351 UNBLOCK_INPUT;
25352 }
25353 }
25354
25355 #endif /* HAVE_WINDOW_SYSTEM */
25356
25357
25358 \f
25359 /***********************************************************************
25360 Cursor types
25361 ***********************************************************************/
25362
25363 /* Value is the internal representation of the specified cursor type
25364 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25365 of the bar cursor. */
25366
25367 static enum text_cursor_kinds
25368 get_specified_cursor_type (Lisp_Object arg, int *width)
25369 {
25370 enum text_cursor_kinds type;
25371
25372 if (NILP (arg))
25373 return NO_CURSOR;
25374
25375 if (EQ (arg, Qbox))
25376 return FILLED_BOX_CURSOR;
25377
25378 if (EQ (arg, Qhollow))
25379 return HOLLOW_BOX_CURSOR;
25380
25381 if (EQ (arg, Qbar))
25382 {
25383 *width = 2;
25384 return BAR_CURSOR;
25385 }
25386
25387 if (CONSP (arg)
25388 && EQ (XCAR (arg), Qbar)
25389 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25390 {
25391 *width = XINT (XCDR (arg));
25392 return BAR_CURSOR;
25393 }
25394
25395 if (EQ (arg, Qhbar))
25396 {
25397 *width = 2;
25398 return HBAR_CURSOR;
25399 }
25400
25401 if (CONSP (arg)
25402 && EQ (XCAR (arg), Qhbar)
25403 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25404 {
25405 *width = XINT (XCDR (arg));
25406 return HBAR_CURSOR;
25407 }
25408
25409 /* Treat anything unknown as "hollow box cursor".
25410 It was bad to signal an error; people have trouble fixing
25411 .Xdefaults with Emacs, when it has something bad in it. */
25412 type = HOLLOW_BOX_CURSOR;
25413
25414 return type;
25415 }
25416
25417 /* Set the default cursor types for specified frame. */
25418 void
25419 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25420 {
25421 int width = 1;
25422 Lisp_Object tem;
25423
25424 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25425 FRAME_CURSOR_WIDTH (f) = width;
25426
25427 /* By default, set up the blink-off state depending on the on-state. */
25428
25429 tem = Fassoc (arg, Vblink_cursor_alist);
25430 if (!NILP (tem))
25431 {
25432 FRAME_BLINK_OFF_CURSOR (f)
25433 = get_specified_cursor_type (XCDR (tem), &width);
25434 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25435 }
25436 else
25437 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25438 }
25439
25440
25441 #ifdef HAVE_WINDOW_SYSTEM
25442
25443 /* Return the cursor we want to be displayed in window W. Return
25444 width of bar/hbar cursor through WIDTH arg. Return with
25445 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25446 (i.e. if the `system caret' should track this cursor).
25447
25448 In a mini-buffer window, we want the cursor only to appear if we
25449 are reading input from this window. For the selected window, we
25450 want the cursor type given by the frame parameter or buffer local
25451 setting of cursor-type. If explicitly marked off, draw no cursor.
25452 In all other cases, we want a hollow box cursor. */
25453
25454 static enum text_cursor_kinds
25455 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25456 int *active_cursor)
25457 {
25458 struct frame *f = XFRAME (w->frame);
25459 struct buffer *b = XBUFFER (w->buffer);
25460 int cursor_type = DEFAULT_CURSOR;
25461 Lisp_Object alt_cursor;
25462 int non_selected = 0;
25463
25464 *active_cursor = 1;
25465
25466 /* Echo area */
25467 if (cursor_in_echo_area
25468 && FRAME_HAS_MINIBUF_P (f)
25469 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25470 {
25471 if (w == XWINDOW (echo_area_window))
25472 {
25473 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25474 {
25475 *width = FRAME_CURSOR_WIDTH (f);
25476 return FRAME_DESIRED_CURSOR (f);
25477 }
25478 else
25479 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25480 }
25481
25482 *active_cursor = 0;
25483 non_selected = 1;
25484 }
25485
25486 /* Detect a nonselected window or nonselected frame. */
25487 else if (w != XWINDOW (f->selected_window)
25488 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25489 {
25490 *active_cursor = 0;
25491
25492 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25493 return NO_CURSOR;
25494
25495 non_selected = 1;
25496 }
25497
25498 /* Never display a cursor in a window in which cursor-type is nil. */
25499 if (NILP (BVAR (b, cursor_type)))
25500 return NO_CURSOR;
25501
25502 /* Get the normal cursor type for this window. */
25503 if (EQ (BVAR (b, cursor_type), Qt))
25504 {
25505 cursor_type = FRAME_DESIRED_CURSOR (f);
25506 *width = FRAME_CURSOR_WIDTH (f);
25507 }
25508 else
25509 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25510
25511 /* Use cursor-in-non-selected-windows instead
25512 for non-selected window or frame. */
25513 if (non_selected)
25514 {
25515 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25516 if (!EQ (Qt, alt_cursor))
25517 return get_specified_cursor_type (alt_cursor, width);
25518 /* t means modify the normal cursor type. */
25519 if (cursor_type == FILLED_BOX_CURSOR)
25520 cursor_type = HOLLOW_BOX_CURSOR;
25521 else if (cursor_type == BAR_CURSOR && *width > 1)
25522 --*width;
25523 return cursor_type;
25524 }
25525
25526 /* Use normal cursor if not blinked off. */
25527 if (!w->cursor_off_p)
25528 {
25529 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25530 {
25531 if (cursor_type == FILLED_BOX_CURSOR)
25532 {
25533 /* Using a block cursor on large images can be very annoying.
25534 So use a hollow cursor for "large" images.
25535 If image is not transparent (no mask), also use hollow cursor. */
25536 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25537 if (img != NULL && IMAGEP (img->spec))
25538 {
25539 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25540 where N = size of default frame font size.
25541 This should cover most of the "tiny" icons people may use. */
25542 if (!img->mask
25543 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25544 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25545 cursor_type = HOLLOW_BOX_CURSOR;
25546 }
25547 }
25548 else if (cursor_type != NO_CURSOR)
25549 {
25550 /* Display current only supports BOX and HOLLOW cursors for images.
25551 So for now, unconditionally use a HOLLOW cursor when cursor is
25552 not a solid box cursor. */
25553 cursor_type = HOLLOW_BOX_CURSOR;
25554 }
25555 }
25556 return cursor_type;
25557 }
25558
25559 /* Cursor is blinked off, so determine how to "toggle" it. */
25560
25561 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25562 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25563 return get_specified_cursor_type (XCDR (alt_cursor), width);
25564
25565 /* Then see if frame has specified a specific blink off cursor type. */
25566 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25567 {
25568 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25569 return FRAME_BLINK_OFF_CURSOR (f);
25570 }
25571
25572 #if 0
25573 /* Some people liked having a permanently visible blinking cursor,
25574 while others had very strong opinions against it. So it was
25575 decided to remove it. KFS 2003-09-03 */
25576
25577 /* Finally perform built-in cursor blinking:
25578 filled box <-> hollow box
25579 wide [h]bar <-> narrow [h]bar
25580 narrow [h]bar <-> no cursor
25581 other type <-> no cursor */
25582
25583 if (cursor_type == FILLED_BOX_CURSOR)
25584 return HOLLOW_BOX_CURSOR;
25585
25586 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25587 {
25588 *width = 1;
25589 return cursor_type;
25590 }
25591 #endif
25592
25593 return NO_CURSOR;
25594 }
25595
25596
25597 /* Notice when the text cursor of window W has been completely
25598 overwritten by a drawing operation that outputs glyphs in AREA
25599 starting at X0 and ending at X1 in the line starting at Y0 and
25600 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25601 the rest of the line after X0 has been written. Y coordinates
25602 are window-relative. */
25603
25604 static void
25605 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25606 int x0, int x1, int y0, int y1)
25607 {
25608 int cx0, cx1, cy0, cy1;
25609 struct glyph_row *row;
25610
25611 if (!w->phys_cursor_on_p)
25612 return;
25613 if (area != TEXT_AREA)
25614 return;
25615
25616 if (w->phys_cursor.vpos < 0
25617 || w->phys_cursor.vpos >= w->current_matrix->nrows
25618 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25619 !(row->enabled_p && row->displays_text_p)))
25620 return;
25621
25622 if (row->cursor_in_fringe_p)
25623 {
25624 row->cursor_in_fringe_p = 0;
25625 draw_fringe_bitmap (w, row, row->reversed_p);
25626 w->phys_cursor_on_p = 0;
25627 return;
25628 }
25629
25630 cx0 = w->phys_cursor.x;
25631 cx1 = cx0 + w->phys_cursor_width;
25632 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25633 return;
25634
25635 /* The cursor image will be completely removed from the
25636 screen if the output area intersects the cursor area in
25637 y-direction. When we draw in [y0 y1[, and some part of
25638 the cursor is at y < y0, that part must have been drawn
25639 before. When scrolling, the cursor is erased before
25640 actually scrolling, so we don't come here. When not
25641 scrolling, the rows above the old cursor row must have
25642 changed, and in this case these rows must have written
25643 over the cursor image.
25644
25645 Likewise if part of the cursor is below y1, with the
25646 exception of the cursor being in the first blank row at
25647 the buffer and window end because update_text_area
25648 doesn't draw that row. (Except when it does, but
25649 that's handled in update_text_area.) */
25650
25651 cy0 = w->phys_cursor.y;
25652 cy1 = cy0 + w->phys_cursor_height;
25653 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25654 return;
25655
25656 w->phys_cursor_on_p = 0;
25657 }
25658
25659 #endif /* HAVE_WINDOW_SYSTEM */
25660
25661 \f
25662 /************************************************************************
25663 Mouse Face
25664 ************************************************************************/
25665
25666 #ifdef HAVE_WINDOW_SYSTEM
25667
25668 /* EXPORT for RIF:
25669 Fix the display of area AREA of overlapping row ROW in window W
25670 with respect to the overlapping part OVERLAPS. */
25671
25672 void
25673 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25674 enum glyph_row_area area, int overlaps)
25675 {
25676 int i, x;
25677
25678 BLOCK_INPUT;
25679
25680 x = 0;
25681 for (i = 0; i < row->used[area];)
25682 {
25683 if (row->glyphs[area][i].overlaps_vertically_p)
25684 {
25685 int start = i, start_x = x;
25686
25687 do
25688 {
25689 x += row->glyphs[area][i].pixel_width;
25690 ++i;
25691 }
25692 while (i < row->used[area]
25693 && row->glyphs[area][i].overlaps_vertically_p);
25694
25695 draw_glyphs (w, start_x, row, area,
25696 start, i,
25697 DRAW_NORMAL_TEXT, overlaps);
25698 }
25699 else
25700 {
25701 x += row->glyphs[area][i].pixel_width;
25702 ++i;
25703 }
25704 }
25705
25706 UNBLOCK_INPUT;
25707 }
25708
25709
25710 /* EXPORT:
25711 Draw the cursor glyph of window W in glyph row ROW. See the
25712 comment of draw_glyphs for the meaning of HL. */
25713
25714 void
25715 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25716 enum draw_glyphs_face hl)
25717 {
25718 /* If cursor hpos is out of bounds, don't draw garbage. This can
25719 happen in mini-buffer windows when switching between echo area
25720 glyphs and mini-buffer. */
25721 if ((row->reversed_p
25722 ? (w->phys_cursor.hpos >= 0)
25723 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25724 {
25725 int on_p = w->phys_cursor_on_p;
25726 int x1;
25727 int hpos = w->phys_cursor.hpos;
25728
25729 /* When the window is hscrolled, cursor hpos can legitimately be
25730 out of bounds, but we draw the cursor at the corresponding
25731 window margin in that case. */
25732 if (!row->reversed_p && hpos < 0)
25733 hpos = 0;
25734 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25735 hpos = row->used[TEXT_AREA] - 1;
25736
25737 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25738 hl, 0);
25739 w->phys_cursor_on_p = on_p;
25740
25741 if (hl == DRAW_CURSOR)
25742 w->phys_cursor_width = x1 - w->phys_cursor.x;
25743 /* When we erase the cursor, and ROW is overlapped by other
25744 rows, make sure that these overlapping parts of other rows
25745 are redrawn. */
25746 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25747 {
25748 w->phys_cursor_width = x1 - w->phys_cursor.x;
25749
25750 if (row > w->current_matrix->rows
25751 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25752 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25753 OVERLAPS_ERASED_CURSOR);
25754
25755 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25756 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25757 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25758 OVERLAPS_ERASED_CURSOR);
25759 }
25760 }
25761 }
25762
25763
25764 /* EXPORT:
25765 Erase the image of a cursor of window W from the screen. */
25766
25767 void
25768 erase_phys_cursor (struct window *w)
25769 {
25770 struct frame *f = XFRAME (w->frame);
25771 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25772 int hpos = w->phys_cursor.hpos;
25773 int vpos = w->phys_cursor.vpos;
25774 int mouse_face_here_p = 0;
25775 struct glyph_matrix *active_glyphs = w->current_matrix;
25776 struct glyph_row *cursor_row;
25777 struct glyph *cursor_glyph;
25778 enum draw_glyphs_face hl;
25779
25780 /* No cursor displayed or row invalidated => nothing to do on the
25781 screen. */
25782 if (w->phys_cursor_type == NO_CURSOR)
25783 goto mark_cursor_off;
25784
25785 /* VPOS >= active_glyphs->nrows means that window has been resized.
25786 Don't bother to erase the cursor. */
25787 if (vpos >= active_glyphs->nrows)
25788 goto mark_cursor_off;
25789
25790 /* If row containing cursor is marked invalid, there is nothing we
25791 can do. */
25792 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25793 if (!cursor_row->enabled_p)
25794 goto mark_cursor_off;
25795
25796 /* If line spacing is > 0, old cursor may only be partially visible in
25797 window after split-window. So adjust visible height. */
25798 cursor_row->visible_height = min (cursor_row->visible_height,
25799 window_text_bottom_y (w) - cursor_row->y);
25800
25801 /* If row is completely invisible, don't attempt to delete a cursor which
25802 isn't there. This can happen if cursor is at top of a window, and
25803 we switch to a buffer with a header line in that window. */
25804 if (cursor_row->visible_height <= 0)
25805 goto mark_cursor_off;
25806
25807 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25808 if (cursor_row->cursor_in_fringe_p)
25809 {
25810 cursor_row->cursor_in_fringe_p = 0;
25811 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25812 goto mark_cursor_off;
25813 }
25814
25815 /* This can happen when the new row is shorter than the old one.
25816 In this case, either draw_glyphs or clear_end_of_line
25817 should have cleared the cursor. Note that we wouldn't be
25818 able to erase the cursor in this case because we don't have a
25819 cursor glyph at hand. */
25820 if ((cursor_row->reversed_p
25821 ? (w->phys_cursor.hpos < 0)
25822 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25823 goto mark_cursor_off;
25824
25825 /* When the window is hscrolled, cursor hpos can legitimately be out
25826 of bounds, but we draw the cursor at the corresponding window
25827 margin in that case. */
25828 if (!cursor_row->reversed_p && hpos < 0)
25829 hpos = 0;
25830 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25831 hpos = cursor_row->used[TEXT_AREA] - 1;
25832
25833 /* If the cursor is in the mouse face area, redisplay that when
25834 we clear the cursor. */
25835 if (! NILP (hlinfo->mouse_face_window)
25836 && coords_in_mouse_face_p (w, hpos, vpos)
25837 /* Don't redraw the cursor's spot in mouse face if it is at the
25838 end of a line (on a newline). The cursor appears there, but
25839 mouse highlighting does not. */
25840 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25841 mouse_face_here_p = 1;
25842
25843 /* Maybe clear the display under the cursor. */
25844 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25845 {
25846 int x, y, left_x;
25847 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25848 int width;
25849
25850 cursor_glyph = get_phys_cursor_glyph (w);
25851 if (cursor_glyph == NULL)
25852 goto mark_cursor_off;
25853
25854 width = cursor_glyph->pixel_width;
25855 left_x = window_box_left_offset (w, TEXT_AREA);
25856 x = w->phys_cursor.x;
25857 if (x < left_x)
25858 width -= left_x - x;
25859 width = min (width, window_box_width (w, TEXT_AREA) - x);
25860 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25861 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25862
25863 if (width > 0)
25864 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25865 }
25866
25867 /* Erase the cursor by redrawing the character underneath it. */
25868 if (mouse_face_here_p)
25869 hl = DRAW_MOUSE_FACE;
25870 else
25871 hl = DRAW_NORMAL_TEXT;
25872 draw_phys_cursor_glyph (w, cursor_row, hl);
25873
25874 mark_cursor_off:
25875 w->phys_cursor_on_p = 0;
25876 w->phys_cursor_type = NO_CURSOR;
25877 }
25878
25879
25880 /* EXPORT:
25881 Display or clear cursor of window W. If ON is zero, clear the
25882 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25883 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25884
25885 void
25886 display_and_set_cursor (struct window *w, int on,
25887 int hpos, int vpos, int x, int y)
25888 {
25889 struct frame *f = XFRAME (w->frame);
25890 int new_cursor_type;
25891 int new_cursor_width;
25892 int active_cursor;
25893 struct glyph_row *glyph_row;
25894 struct glyph *glyph;
25895
25896 /* This is pointless on invisible frames, and dangerous on garbaged
25897 windows and frames; in the latter case, the frame or window may
25898 be in the midst of changing its size, and x and y may be off the
25899 window. */
25900 if (! FRAME_VISIBLE_P (f)
25901 || FRAME_GARBAGED_P (f)
25902 || vpos >= w->current_matrix->nrows
25903 || hpos >= w->current_matrix->matrix_w)
25904 return;
25905
25906 /* If cursor is off and we want it off, return quickly. */
25907 if (!on && !w->phys_cursor_on_p)
25908 return;
25909
25910 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25911 /* If cursor row is not enabled, we don't really know where to
25912 display the cursor. */
25913 if (!glyph_row->enabled_p)
25914 {
25915 w->phys_cursor_on_p = 0;
25916 return;
25917 }
25918
25919 glyph = NULL;
25920 if (!glyph_row->exact_window_width_line_p
25921 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25922 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25923
25924 eassert (interrupt_input_blocked);
25925
25926 /* Set new_cursor_type to the cursor we want to be displayed. */
25927 new_cursor_type = get_window_cursor_type (w, glyph,
25928 &new_cursor_width, &active_cursor);
25929
25930 /* If cursor is currently being shown and we don't want it to be or
25931 it is in the wrong place, or the cursor type is not what we want,
25932 erase it. */
25933 if (w->phys_cursor_on_p
25934 && (!on
25935 || w->phys_cursor.x != x
25936 || w->phys_cursor.y != y
25937 || new_cursor_type != w->phys_cursor_type
25938 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25939 && new_cursor_width != w->phys_cursor_width)))
25940 erase_phys_cursor (w);
25941
25942 /* Don't check phys_cursor_on_p here because that flag is only set
25943 to zero in some cases where we know that the cursor has been
25944 completely erased, to avoid the extra work of erasing the cursor
25945 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25946 still not be visible, or it has only been partly erased. */
25947 if (on)
25948 {
25949 w->phys_cursor_ascent = glyph_row->ascent;
25950 w->phys_cursor_height = glyph_row->height;
25951
25952 /* Set phys_cursor_.* before x_draw_.* is called because some
25953 of them may need the information. */
25954 w->phys_cursor.x = x;
25955 w->phys_cursor.y = glyph_row->y;
25956 w->phys_cursor.hpos = hpos;
25957 w->phys_cursor.vpos = vpos;
25958 }
25959
25960 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25961 new_cursor_type, new_cursor_width,
25962 on, active_cursor);
25963 }
25964
25965
25966 /* Switch the display of W's cursor on or off, according to the value
25967 of ON. */
25968
25969 static void
25970 update_window_cursor (struct window *w, int on)
25971 {
25972 /* Don't update cursor in windows whose frame is in the process
25973 of being deleted. */
25974 if (w->current_matrix)
25975 {
25976 int hpos = w->phys_cursor.hpos;
25977 int vpos = w->phys_cursor.vpos;
25978 struct glyph_row *row;
25979
25980 if (vpos >= w->current_matrix->nrows
25981 || hpos >= w->current_matrix->matrix_w)
25982 return;
25983
25984 row = MATRIX_ROW (w->current_matrix, vpos);
25985
25986 /* When the window is hscrolled, cursor hpos can legitimately be
25987 out of bounds, but we draw the cursor at the corresponding
25988 window margin in that case. */
25989 if (!row->reversed_p && hpos < 0)
25990 hpos = 0;
25991 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25992 hpos = row->used[TEXT_AREA] - 1;
25993
25994 BLOCK_INPUT;
25995 display_and_set_cursor (w, on, hpos, vpos,
25996 w->phys_cursor.x, w->phys_cursor.y);
25997 UNBLOCK_INPUT;
25998 }
25999 }
26000
26001
26002 /* Call update_window_cursor with parameter ON_P on all leaf windows
26003 in the window tree rooted at W. */
26004
26005 static void
26006 update_cursor_in_window_tree (struct window *w, int on_p)
26007 {
26008 while (w)
26009 {
26010 if (!NILP (w->hchild))
26011 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26012 else if (!NILP (w->vchild))
26013 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26014 else
26015 update_window_cursor (w, on_p);
26016
26017 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26018 }
26019 }
26020
26021
26022 /* EXPORT:
26023 Display the cursor on window W, or clear it, according to ON_P.
26024 Don't change the cursor's position. */
26025
26026 void
26027 x_update_cursor (struct frame *f, int on_p)
26028 {
26029 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26030 }
26031
26032
26033 /* EXPORT:
26034 Clear the cursor of window W to background color, and mark the
26035 cursor as not shown. This is used when the text where the cursor
26036 is about to be rewritten. */
26037
26038 void
26039 x_clear_cursor (struct window *w)
26040 {
26041 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26042 update_window_cursor (w, 0);
26043 }
26044
26045 #endif /* HAVE_WINDOW_SYSTEM */
26046
26047 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26048 and MSDOS. */
26049 static void
26050 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26051 int start_hpos, int end_hpos,
26052 enum draw_glyphs_face draw)
26053 {
26054 #ifdef HAVE_WINDOW_SYSTEM
26055 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26056 {
26057 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26058 return;
26059 }
26060 #endif
26061 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26062 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26063 #endif
26064 }
26065
26066 /* Display the active region described by mouse_face_* according to DRAW. */
26067
26068 static void
26069 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26070 {
26071 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26072 struct frame *f = XFRAME (WINDOW_FRAME (w));
26073
26074 if (/* If window is in the process of being destroyed, don't bother
26075 to do anything. */
26076 w->current_matrix != NULL
26077 /* Don't update mouse highlight if hidden */
26078 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26079 /* Recognize when we are called to operate on rows that don't exist
26080 anymore. This can happen when a window is split. */
26081 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26082 {
26083 int phys_cursor_on_p = w->phys_cursor_on_p;
26084 struct glyph_row *row, *first, *last;
26085
26086 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26087 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26088
26089 for (row = first; row <= last && row->enabled_p; ++row)
26090 {
26091 int start_hpos, end_hpos, start_x;
26092
26093 /* For all but the first row, the highlight starts at column 0. */
26094 if (row == first)
26095 {
26096 /* R2L rows have BEG and END in reversed order, but the
26097 screen drawing geometry is always left to right. So
26098 we need to mirror the beginning and end of the
26099 highlighted area in R2L rows. */
26100 if (!row->reversed_p)
26101 {
26102 start_hpos = hlinfo->mouse_face_beg_col;
26103 start_x = hlinfo->mouse_face_beg_x;
26104 }
26105 else if (row == last)
26106 {
26107 start_hpos = hlinfo->mouse_face_end_col;
26108 start_x = hlinfo->mouse_face_end_x;
26109 }
26110 else
26111 {
26112 start_hpos = 0;
26113 start_x = 0;
26114 }
26115 }
26116 else if (row->reversed_p && row == last)
26117 {
26118 start_hpos = hlinfo->mouse_face_end_col;
26119 start_x = hlinfo->mouse_face_end_x;
26120 }
26121 else
26122 {
26123 start_hpos = 0;
26124 start_x = 0;
26125 }
26126
26127 if (row == last)
26128 {
26129 if (!row->reversed_p)
26130 end_hpos = hlinfo->mouse_face_end_col;
26131 else if (row == first)
26132 end_hpos = hlinfo->mouse_face_beg_col;
26133 else
26134 {
26135 end_hpos = row->used[TEXT_AREA];
26136 if (draw == DRAW_NORMAL_TEXT)
26137 row->fill_line_p = 1; /* Clear to end of line */
26138 }
26139 }
26140 else if (row->reversed_p && row == first)
26141 end_hpos = hlinfo->mouse_face_beg_col;
26142 else
26143 {
26144 end_hpos = row->used[TEXT_AREA];
26145 if (draw == DRAW_NORMAL_TEXT)
26146 row->fill_line_p = 1; /* Clear to end of line */
26147 }
26148
26149 if (end_hpos > start_hpos)
26150 {
26151 draw_row_with_mouse_face (w, start_x, row,
26152 start_hpos, end_hpos, draw);
26153
26154 row->mouse_face_p
26155 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26156 }
26157 }
26158
26159 #ifdef HAVE_WINDOW_SYSTEM
26160 /* When we've written over the cursor, arrange for it to
26161 be displayed again. */
26162 if (FRAME_WINDOW_P (f)
26163 && phys_cursor_on_p && !w->phys_cursor_on_p)
26164 {
26165 int hpos = w->phys_cursor.hpos;
26166
26167 /* When the window is hscrolled, cursor hpos can legitimately be
26168 out of bounds, but we draw the cursor at the corresponding
26169 window margin in that case. */
26170 if (!row->reversed_p && hpos < 0)
26171 hpos = 0;
26172 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26173 hpos = row->used[TEXT_AREA] - 1;
26174
26175 BLOCK_INPUT;
26176 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26177 w->phys_cursor.x, w->phys_cursor.y);
26178 UNBLOCK_INPUT;
26179 }
26180 #endif /* HAVE_WINDOW_SYSTEM */
26181 }
26182
26183 #ifdef HAVE_WINDOW_SYSTEM
26184 /* Change the mouse cursor. */
26185 if (FRAME_WINDOW_P (f))
26186 {
26187 if (draw == DRAW_NORMAL_TEXT
26188 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26189 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26190 else if (draw == DRAW_MOUSE_FACE)
26191 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26192 else
26193 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26194 }
26195 #endif /* HAVE_WINDOW_SYSTEM */
26196 }
26197
26198 /* EXPORT:
26199 Clear out the mouse-highlighted active region.
26200 Redraw it un-highlighted first. Value is non-zero if mouse
26201 face was actually drawn unhighlighted. */
26202
26203 int
26204 clear_mouse_face (Mouse_HLInfo *hlinfo)
26205 {
26206 int cleared = 0;
26207
26208 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26209 {
26210 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26211 cleared = 1;
26212 }
26213
26214 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26215 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26216 hlinfo->mouse_face_window = Qnil;
26217 hlinfo->mouse_face_overlay = Qnil;
26218 return cleared;
26219 }
26220
26221 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26222 within the mouse face on that window. */
26223 static int
26224 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26225 {
26226 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26227
26228 /* Quickly resolve the easy cases. */
26229 if (!(WINDOWP (hlinfo->mouse_face_window)
26230 && XWINDOW (hlinfo->mouse_face_window) == w))
26231 return 0;
26232 if (vpos < hlinfo->mouse_face_beg_row
26233 || vpos > hlinfo->mouse_face_end_row)
26234 return 0;
26235 if (vpos > hlinfo->mouse_face_beg_row
26236 && vpos < hlinfo->mouse_face_end_row)
26237 return 1;
26238
26239 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26240 {
26241 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26242 {
26243 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26244 return 1;
26245 }
26246 else if ((vpos == hlinfo->mouse_face_beg_row
26247 && hpos >= hlinfo->mouse_face_beg_col)
26248 || (vpos == hlinfo->mouse_face_end_row
26249 && hpos < hlinfo->mouse_face_end_col))
26250 return 1;
26251 }
26252 else
26253 {
26254 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26255 {
26256 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26257 return 1;
26258 }
26259 else if ((vpos == hlinfo->mouse_face_beg_row
26260 && hpos <= hlinfo->mouse_face_beg_col)
26261 || (vpos == hlinfo->mouse_face_end_row
26262 && hpos > hlinfo->mouse_face_end_col))
26263 return 1;
26264 }
26265 return 0;
26266 }
26267
26268
26269 /* EXPORT:
26270 Non-zero if physical cursor of window W is within mouse face. */
26271
26272 int
26273 cursor_in_mouse_face_p (struct window *w)
26274 {
26275 int hpos = w->phys_cursor.hpos;
26276 int vpos = w->phys_cursor.vpos;
26277 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26278
26279 /* When the window is hscrolled, cursor hpos can legitimately be out
26280 of bounds, but we draw the cursor at the corresponding window
26281 margin in that case. */
26282 if (!row->reversed_p && hpos < 0)
26283 hpos = 0;
26284 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26285 hpos = row->used[TEXT_AREA] - 1;
26286
26287 return coords_in_mouse_face_p (w, hpos, vpos);
26288 }
26289
26290
26291 \f
26292 /* Find the glyph rows START_ROW and END_ROW of window W that display
26293 characters between buffer positions START_CHARPOS and END_CHARPOS
26294 (excluding END_CHARPOS). DISP_STRING is a display string that
26295 covers these buffer positions. This is similar to
26296 row_containing_pos, but is more accurate when bidi reordering makes
26297 buffer positions change non-linearly with glyph rows. */
26298 static void
26299 rows_from_pos_range (struct window *w,
26300 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26301 Lisp_Object disp_string,
26302 struct glyph_row **start, struct glyph_row **end)
26303 {
26304 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26305 int last_y = window_text_bottom_y (w);
26306 struct glyph_row *row;
26307
26308 *start = NULL;
26309 *end = NULL;
26310
26311 while (!first->enabled_p
26312 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26313 first++;
26314
26315 /* Find the START row. */
26316 for (row = first;
26317 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26318 row++)
26319 {
26320 /* A row can potentially be the START row if the range of the
26321 characters it displays intersects the range
26322 [START_CHARPOS..END_CHARPOS). */
26323 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26324 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26325 /* See the commentary in row_containing_pos, for the
26326 explanation of the complicated way to check whether
26327 some position is beyond the end of the characters
26328 displayed by a row. */
26329 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26330 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26331 && !row->ends_at_zv_p
26332 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26333 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26334 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26335 && !row->ends_at_zv_p
26336 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26337 {
26338 /* Found a candidate row. Now make sure at least one of the
26339 glyphs it displays has a charpos from the range
26340 [START_CHARPOS..END_CHARPOS).
26341
26342 This is not obvious because bidi reordering could make
26343 buffer positions of a row be 1,2,3,102,101,100, and if we
26344 want to highlight characters in [50..60), we don't want
26345 this row, even though [50..60) does intersect [1..103),
26346 the range of character positions given by the row's start
26347 and end positions. */
26348 struct glyph *g = row->glyphs[TEXT_AREA];
26349 struct glyph *e = g + row->used[TEXT_AREA];
26350
26351 while (g < e)
26352 {
26353 if (((BUFFERP (g->object) || INTEGERP (g->object))
26354 && start_charpos <= g->charpos && g->charpos < end_charpos)
26355 /* A glyph that comes from DISP_STRING is by
26356 definition to be highlighted. */
26357 || EQ (g->object, disp_string))
26358 *start = row;
26359 g++;
26360 }
26361 if (*start)
26362 break;
26363 }
26364 }
26365
26366 /* Find the END row. */
26367 if (!*start
26368 /* If the last row is partially visible, start looking for END
26369 from that row, instead of starting from FIRST. */
26370 && !(row->enabled_p
26371 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26372 row = first;
26373 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26374 {
26375 struct glyph_row *next = row + 1;
26376 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26377
26378 if (!next->enabled_p
26379 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26380 /* The first row >= START whose range of displayed characters
26381 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26382 is the row END + 1. */
26383 || (start_charpos < next_start
26384 && end_charpos < next_start)
26385 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26386 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26387 && !next->ends_at_zv_p
26388 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26389 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26390 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26391 && !next->ends_at_zv_p
26392 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26393 {
26394 *end = row;
26395 break;
26396 }
26397 else
26398 {
26399 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26400 but none of the characters it displays are in the range, it is
26401 also END + 1. */
26402 struct glyph *g = next->glyphs[TEXT_AREA];
26403 struct glyph *s = g;
26404 struct glyph *e = g + next->used[TEXT_AREA];
26405
26406 while (g < e)
26407 {
26408 if (((BUFFERP (g->object) || INTEGERP (g->object))
26409 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26410 /* If the buffer position of the first glyph in
26411 the row is equal to END_CHARPOS, it means
26412 the last character to be highlighted is the
26413 newline of ROW, and we must consider NEXT as
26414 END, not END+1. */
26415 || (((!next->reversed_p && g == s)
26416 || (next->reversed_p && g == e - 1))
26417 && (g->charpos == end_charpos
26418 /* Special case for when NEXT is an
26419 empty line at ZV. */
26420 || (g->charpos == -1
26421 && !row->ends_at_zv_p
26422 && next_start == end_charpos)))))
26423 /* A glyph that comes from DISP_STRING is by
26424 definition to be highlighted. */
26425 || EQ (g->object, disp_string))
26426 break;
26427 g++;
26428 }
26429 if (g == e)
26430 {
26431 *end = row;
26432 break;
26433 }
26434 /* The first row that ends at ZV must be the last to be
26435 highlighted. */
26436 else if (next->ends_at_zv_p)
26437 {
26438 *end = next;
26439 break;
26440 }
26441 }
26442 }
26443 }
26444
26445 /* This function sets the mouse_face_* elements of HLINFO, assuming
26446 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26447 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26448 for the overlay or run of text properties specifying the mouse
26449 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26450 before-string and after-string that must also be highlighted.
26451 DISP_STRING, if non-nil, is a display string that may cover some
26452 or all of the highlighted text. */
26453
26454 static void
26455 mouse_face_from_buffer_pos (Lisp_Object window,
26456 Mouse_HLInfo *hlinfo,
26457 ptrdiff_t mouse_charpos,
26458 ptrdiff_t start_charpos,
26459 ptrdiff_t end_charpos,
26460 Lisp_Object before_string,
26461 Lisp_Object after_string,
26462 Lisp_Object disp_string)
26463 {
26464 struct window *w = XWINDOW (window);
26465 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26466 struct glyph_row *r1, *r2;
26467 struct glyph *glyph, *end;
26468 ptrdiff_t ignore, pos;
26469 int x;
26470
26471 eassert (NILP (disp_string) || STRINGP (disp_string));
26472 eassert (NILP (before_string) || STRINGP (before_string));
26473 eassert (NILP (after_string) || STRINGP (after_string));
26474
26475 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26476 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26477 if (r1 == NULL)
26478 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26479 /* If the before-string or display-string contains newlines,
26480 rows_from_pos_range skips to its last row. Move back. */
26481 if (!NILP (before_string) || !NILP (disp_string))
26482 {
26483 struct glyph_row *prev;
26484 while ((prev = r1 - 1, prev >= first)
26485 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26486 && prev->used[TEXT_AREA] > 0)
26487 {
26488 struct glyph *beg = prev->glyphs[TEXT_AREA];
26489 glyph = beg + prev->used[TEXT_AREA];
26490 while (--glyph >= beg && INTEGERP (glyph->object));
26491 if (glyph < beg
26492 || !(EQ (glyph->object, before_string)
26493 || EQ (glyph->object, disp_string)))
26494 break;
26495 r1 = prev;
26496 }
26497 }
26498 if (r2 == NULL)
26499 {
26500 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26501 hlinfo->mouse_face_past_end = 1;
26502 }
26503 else if (!NILP (after_string))
26504 {
26505 /* If the after-string has newlines, advance to its last row. */
26506 struct glyph_row *next;
26507 struct glyph_row *last
26508 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26509
26510 for (next = r2 + 1;
26511 next <= last
26512 && next->used[TEXT_AREA] > 0
26513 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26514 ++next)
26515 r2 = next;
26516 }
26517 /* The rest of the display engine assumes that mouse_face_beg_row is
26518 either above mouse_face_end_row or identical to it. But with
26519 bidi-reordered continued lines, the row for START_CHARPOS could
26520 be below the row for END_CHARPOS. If so, swap the rows and store
26521 them in correct order. */
26522 if (r1->y > r2->y)
26523 {
26524 struct glyph_row *tem = r2;
26525
26526 r2 = r1;
26527 r1 = tem;
26528 }
26529
26530 hlinfo->mouse_face_beg_y = r1->y;
26531 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26532 hlinfo->mouse_face_end_y = r2->y;
26533 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26534
26535 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26536 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26537 could be anywhere in the row and in any order. The strategy
26538 below is to find the leftmost and the rightmost glyph that
26539 belongs to either of these 3 strings, or whose position is
26540 between START_CHARPOS and END_CHARPOS, and highlight all the
26541 glyphs between those two. This may cover more than just the text
26542 between START_CHARPOS and END_CHARPOS if the range of characters
26543 strides the bidi level boundary, e.g. if the beginning is in R2L
26544 text while the end is in L2R text or vice versa. */
26545 if (!r1->reversed_p)
26546 {
26547 /* This row is in a left to right paragraph. Scan it left to
26548 right. */
26549 glyph = r1->glyphs[TEXT_AREA];
26550 end = glyph + r1->used[TEXT_AREA];
26551 x = r1->x;
26552
26553 /* Skip truncation glyphs at the start of the glyph row. */
26554 if (r1->displays_text_p)
26555 for (; glyph < end
26556 && INTEGERP (glyph->object)
26557 && glyph->charpos < 0;
26558 ++glyph)
26559 x += glyph->pixel_width;
26560
26561 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26562 or DISP_STRING, and the first glyph from buffer whose
26563 position is between START_CHARPOS and END_CHARPOS. */
26564 for (; glyph < end
26565 && !INTEGERP (glyph->object)
26566 && !EQ (glyph->object, disp_string)
26567 && !(BUFFERP (glyph->object)
26568 && (glyph->charpos >= start_charpos
26569 && glyph->charpos < end_charpos));
26570 ++glyph)
26571 {
26572 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26573 are present at buffer positions between START_CHARPOS and
26574 END_CHARPOS, or if they come from an overlay. */
26575 if (EQ (glyph->object, before_string))
26576 {
26577 pos = string_buffer_position (before_string,
26578 start_charpos);
26579 /* If pos == 0, it means before_string came from an
26580 overlay, not from a buffer position. */
26581 if (!pos || (pos >= start_charpos && pos < end_charpos))
26582 break;
26583 }
26584 else if (EQ (glyph->object, after_string))
26585 {
26586 pos = string_buffer_position (after_string, end_charpos);
26587 if (!pos || (pos >= start_charpos && pos < end_charpos))
26588 break;
26589 }
26590 x += glyph->pixel_width;
26591 }
26592 hlinfo->mouse_face_beg_x = x;
26593 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26594 }
26595 else
26596 {
26597 /* This row is in a right to left paragraph. Scan it right to
26598 left. */
26599 struct glyph *g;
26600
26601 end = r1->glyphs[TEXT_AREA] - 1;
26602 glyph = end + r1->used[TEXT_AREA];
26603
26604 /* Skip truncation glyphs at the start of the glyph row. */
26605 if (r1->displays_text_p)
26606 for (; glyph > end
26607 && INTEGERP (glyph->object)
26608 && glyph->charpos < 0;
26609 --glyph)
26610 ;
26611
26612 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26613 or DISP_STRING, and the first glyph from buffer whose
26614 position is between START_CHARPOS and END_CHARPOS. */
26615 for (; glyph > end
26616 && !INTEGERP (glyph->object)
26617 && !EQ (glyph->object, disp_string)
26618 && !(BUFFERP (glyph->object)
26619 && (glyph->charpos >= start_charpos
26620 && glyph->charpos < end_charpos));
26621 --glyph)
26622 {
26623 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26624 are present at buffer positions between START_CHARPOS and
26625 END_CHARPOS, or if they come from an overlay. */
26626 if (EQ (glyph->object, before_string))
26627 {
26628 pos = string_buffer_position (before_string, start_charpos);
26629 /* If pos == 0, it means before_string came from an
26630 overlay, not from a buffer position. */
26631 if (!pos || (pos >= start_charpos && pos < end_charpos))
26632 break;
26633 }
26634 else if (EQ (glyph->object, after_string))
26635 {
26636 pos = string_buffer_position (after_string, end_charpos);
26637 if (!pos || (pos >= start_charpos && pos < end_charpos))
26638 break;
26639 }
26640 }
26641
26642 glyph++; /* first glyph to the right of the highlighted area */
26643 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26644 x += g->pixel_width;
26645 hlinfo->mouse_face_beg_x = x;
26646 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26647 }
26648
26649 /* If the highlight ends in a different row, compute GLYPH and END
26650 for the end row. Otherwise, reuse the values computed above for
26651 the row where the highlight begins. */
26652 if (r2 != r1)
26653 {
26654 if (!r2->reversed_p)
26655 {
26656 glyph = r2->glyphs[TEXT_AREA];
26657 end = glyph + r2->used[TEXT_AREA];
26658 x = r2->x;
26659 }
26660 else
26661 {
26662 end = r2->glyphs[TEXT_AREA] - 1;
26663 glyph = end + r2->used[TEXT_AREA];
26664 }
26665 }
26666
26667 if (!r2->reversed_p)
26668 {
26669 /* Skip truncation and continuation glyphs near the end of the
26670 row, and also blanks and stretch glyphs inserted by
26671 extend_face_to_end_of_line. */
26672 while (end > glyph
26673 && INTEGERP ((end - 1)->object))
26674 --end;
26675 /* Scan the rest of the glyph row from the end, looking for the
26676 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26677 DISP_STRING, or whose position is between START_CHARPOS
26678 and END_CHARPOS */
26679 for (--end;
26680 end > glyph
26681 && !INTEGERP (end->object)
26682 && !EQ (end->object, disp_string)
26683 && !(BUFFERP (end->object)
26684 && (end->charpos >= start_charpos
26685 && end->charpos < end_charpos));
26686 --end)
26687 {
26688 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26689 are present at buffer positions between START_CHARPOS and
26690 END_CHARPOS, or if they come from an overlay. */
26691 if (EQ (end->object, before_string))
26692 {
26693 pos = string_buffer_position (before_string, start_charpos);
26694 if (!pos || (pos >= start_charpos && pos < end_charpos))
26695 break;
26696 }
26697 else if (EQ (end->object, after_string))
26698 {
26699 pos = string_buffer_position (after_string, end_charpos);
26700 if (!pos || (pos >= start_charpos && pos < end_charpos))
26701 break;
26702 }
26703 }
26704 /* Find the X coordinate of the last glyph to be highlighted. */
26705 for (; glyph <= end; ++glyph)
26706 x += glyph->pixel_width;
26707
26708 hlinfo->mouse_face_end_x = x;
26709 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26710 }
26711 else
26712 {
26713 /* Skip truncation and continuation glyphs near the end of the
26714 row, and also blanks and stretch glyphs inserted by
26715 extend_face_to_end_of_line. */
26716 x = r2->x;
26717 end++;
26718 while (end < glyph
26719 && INTEGERP (end->object))
26720 {
26721 x += end->pixel_width;
26722 ++end;
26723 }
26724 /* Scan the rest of the glyph row from the end, looking for the
26725 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26726 DISP_STRING, or whose position is between START_CHARPOS
26727 and END_CHARPOS */
26728 for ( ;
26729 end < glyph
26730 && !INTEGERP (end->object)
26731 && !EQ (end->object, disp_string)
26732 && !(BUFFERP (end->object)
26733 && (end->charpos >= start_charpos
26734 && end->charpos < end_charpos));
26735 ++end)
26736 {
26737 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26738 are present at buffer positions between START_CHARPOS and
26739 END_CHARPOS, or if they come from an overlay. */
26740 if (EQ (end->object, before_string))
26741 {
26742 pos = string_buffer_position (before_string, start_charpos);
26743 if (!pos || (pos >= start_charpos && pos < end_charpos))
26744 break;
26745 }
26746 else if (EQ (end->object, after_string))
26747 {
26748 pos = string_buffer_position (after_string, end_charpos);
26749 if (!pos || (pos >= start_charpos && pos < end_charpos))
26750 break;
26751 }
26752 x += end->pixel_width;
26753 }
26754 /* If we exited the above loop because we arrived at the last
26755 glyph of the row, and its buffer position is still not in
26756 range, it means the last character in range is the preceding
26757 newline. Bump the end column and x values to get past the
26758 last glyph. */
26759 if (end == glyph
26760 && BUFFERP (end->object)
26761 && (end->charpos < start_charpos
26762 || end->charpos >= end_charpos))
26763 {
26764 x += end->pixel_width;
26765 ++end;
26766 }
26767 hlinfo->mouse_face_end_x = x;
26768 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26769 }
26770
26771 hlinfo->mouse_face_window = window;
26772 hlinfo->mouse_face_face_id
26773 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26774 mouse_charpos + 1,
26775 !hlinfo->mouse_face_hidden, -1);
26776 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26777 }
26778
26779 /* The following function is not used anymore (replaced with
26780 mouse_face_from_string_pos), but I leave it here for the time
26781 being, in case someone would. */
26782
26783 #if 0 /* not used */
26784
26785 /* Find the position of the glyph for position POS in OBJECT in
26786 window W's current matrix, and return in *X, *Y the pixel
26787 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26788
26789 RIGHT_P non-zero means return the position of the right edge of the
26790 glyph, RIGHT_P zero means return the left edge position.
26791
26792 If no glyph for POS exists in the matrix, return the position of
26793 the glyph with the next smaller position that is in the matrix, if
26794 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26795 exists in the matrix, return the position of the glyph with the
26796 next larger position in OBJECT.
26797
26798 Value is non-zero if a glyph was found. */
26799
26800 static int
26801 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26802 int *hpos, int *vpos, int *x, int *y, int right_p)
26803 {
26804 int yb = window_text_bottom_y (w);
26805 struct glyph_row *r;
26806 struct glyph *best_glyph = NULL;
26807 struct glyph_row *best_row = NULL;
26808 int best_x = 0;
26809
26810 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26811 r->enabled_p && r->y < yb;
26812 ++r)
26813 {
26814 struct glyph *g = r->glyphs[TEXT_AREA];
26815 struct glyph *e = g + r->used[TEXT_AREA];
26816 int gx;
26817
26818 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26819 if (EQ (g->object, object))
26820 {
26821 if (g->charpos == pos)
26822 {
26823 best_glyph = g;
26824 best_x = gx;
26825 best_row = r;
26826 goto found;
26827 }
26828 else if (best_glyph == NULL
26829 || ((eabs (g->charpos - pos)
26830 < eabs (best_glyph->charpos - pos))
26831 && (right_p
26832 ? g->charpos < pos
26833 : g->charpos > pos)))
26834 {
26835 best_glyph = g;
26836 best_x = gx;
26837 best_row = r;
26838 }
26839 }
26840 }
26841
26842 found:
26843
26844 if (best_glyph)
26845 {
26846 *x = best_x;
26847 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26848
26849 if (right_p)
26850 {
26851 *x += best_glyph->pixel_width;
26852 ++*hpos;
26853 }
26854
26855 *y = best_row->y;
26856 *vpos = best_row - w->current_matrix->rows;
26857 }
26858
26859 return best_glyph != NULL;
26860 }
26861 #endif /* not used */
26862
26863 /* Find the positions of the first and the last glyphs in window W's
26864 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26865 (assumed to be a string), and return in HLINFO's mouse_face_*
26866 members the pixel and column/row coordinates of those glyphs. */
26867
26868 static void
26869 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26870 Lisp_Object object,
26871 ptrdiff_t startpos, ptrdiff_t endpos)
26872 {
26873 int yb = window_text_bottom_y (w);
26874 struct glyph_row *r;
26875 struct glyph *g, *e;
26876 int gx;
26877 int found = 0;
26878
26879 /* Find the glyph row with at least one position in the range
26880 [STARTPOS..ENDPOS], and the first glyph in that row whose
26881 position belongs to that range. */
26882 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26883 r->enabled_p && r->y < yb;
26884 ++r)
26885 {
26886 if (!r->reversed_p)
26887 {
26888 g = r->glyphs[TEXT_AREA];
26889 e = g + r->used[TEXT_AREA];
26890 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26891 if (EQ (g->object, object)
26892 && startpos <= g->charpos && g->charpos <= endpos)
26893 {
26894 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26895 hlinfo->mouse_face_beg_y = r->y;
26896 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26897 hlinfo->mouse_face_beg_x = gx;
26898 found = 1;
26899 break;
26900 }
26901 }
26902 else
26903 {
26904 struct glyph *g1;
26905
26906 e = r->glyphs[TEXT_AREA];
26907 g = e + r->used[TEXT_AREA];
26908 for ( ; g > e; --g)
26909 if (EQ ((g-1)->object, object)
26910 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26911 {
26912 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26913 hlinfo->mouse_face_beg_y = r->y;
26914 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26915 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26916 gx += g1->pixel_width;
26917 hlinfo->mouse_face_beg_x = gx;
26918 found = 1;
26919 break;
26920 }
26921 }
26922 if (found)
26923 break;
26924 }
26925
26926 if (!found)
26927 return;
26928
26929 /* Starting with the next row, look for the first row which does NOT
26930 include any glyphs whose positions are in the range. */
26931 for (++r; r->enabled_p && r->y < yb; ++r)
26932 {
26933 g = r->glyphs[TEXT_AREA];
26934 e = g + r->used[TEXT_AREA];
26935 found = 0;
26936 for ( ; g < e; ++g)
26937 if (EQ (g->object, object)
26938 && startpos <= g->charpos && g->charpos <= endpos)
26939 {
26940 found = 1;
26941 break;
26942 }
26943 if (!found)
26944 break;
26945 }
26946
26947 /* The highlighted region ends on the previous row. */
26948 r--;
26949
26950 /* Set the end row and its vertical pixel coordinate. */
26951 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26952 hlinfo->mouse_face_end_y = r->y;
26953
26954 /* Compute and set the end column and the end column's horizontal
26955 pixel coordinate. */
26956 if (!r->reversed_p)
26957 {
26958 g = r->glyphs[TEXT_AREA];
26959 e = g + r->used[TEXT_AREA];
26960 for ( ; e > g; --e)
26961 if (EQ ((e-1)->object, object)
26962 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26963 break;
26964 hlinfo->mouse_face_end_col = e - g;
26965
26966 for (gx = r->x; g < e; ++g)
26967 gx += g->pixel_width;
26968 hlinfo->mouse_face_end_x = gx;
26969 }
26970 else
26971 {
26972 e = r->glyphs[TEXT_AREA];
26973 g = e + r->used[TEXT_AREA];
26974 for (gx = r->x ; e < g; ++e)
26975 {
26976 if (EQ (e->object, object)
26977 && startpos <= e->charpos && e->charpos <= endpos)
26978 break;
26979 gx += e->pixel_width;
26980 }
26981 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26982 hlinfo->mouse_face_end_x = gx;
26983 }
26984 }
26985
26986 #ifdef HAVE_WINDOW_SYSTEM
26987
26988 /* See if position X, Y is within a hot-spot of an image. */
26989
26990 static int
26991 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26992 {
26993 if (!CONSP (hot_spot))
26994 return 0;
26995
26996 if (EQ (XCAR (hot_spot), Qrect))
26997 {
26998 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26999 Lisp_Object rect = XCDR (hot_spot);
27000 Lisp_Object tem;
27001 if (!CONSP (rect))
27002 return 0;
27003 if (!CONSP (XCAR (rect)))
27004 return 0;
27005 if (!CONSP (XCDR (rect)))
27006 return 0;
27007 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27008 return 0;
27009 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27010 return 0;
27011 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27012 return 0;
27013 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27014 return 0;
27015 return 1;
27016 }
27017 else if (EQ (XCAR (hot_spot), Qcircle))
27018 {
27019 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27020 Lisp_Object circ = XCDR (hot_spot);
27021 Lisp_Object lr, lx0, ly0;
27022 if (CONSP (circ)
27023 && CONSP (XCAR (circ))
27024 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27025 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27026 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27027 {
27028 double r = XFLOATINT (lr);
27029 double dx = XINT (lx0) - x;
27030 double dy = XINT (ly0) - y;
27031 return (dx * dx + dy * dy <= r * r);
27032 }
27033 }
27034 else if (EQ (XCAR (hot_spot), Qpoly))
27035 {
27036 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27037 if (VECTORP (XCDR (hot_spot)))
27038 {
27039 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27040 Lisp_Object *poly = v->contents;
27041 ptrdiff_t n = v->header.size;
27042 ptrdiff_t i;
27043 int inside = 0;
27044 Lisp_Object lx, ly;
27045 int x0, y0;
27046
27047 /* Need an even number of coordinates, and at least 3 edges. */
27048 if (n < 6 || n & 1)
27049 return 0;
27050
27051 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27052 If count is odd, we are inside polygon. Pixels on edges
27053 may or may not be included depending on actual geometry of the
27054 polygon. */
27055 if ((lx = poly[n-2], !INTEGERP (lx))
27056 || (ly = poly[n-1], !INTEGERP (lx)))
27057 return 0;
27058 x0 = XINT (lx), y0 = XINT (ly);
27059 for (i = 0; i < n; i += 2)
27060 {
27061 int x1 = x0, y1 = y0;
27062 if ((lx = poly[i], !INTEGERP (lx))
27063 || (ly = poly[i+1], !INTEGERP (ly)))
27064 return 0;
27065 x0 = XINT (lx), y0 = XINT (ly);
27066
27067 /* Does this segment cross the X line? */
27068 if (x0 >= x)
27069 {
27070 if (x1 >= x)
27071 continue;
27072 }
27073 else if (x1 < x)
27074 continue;
27075 if (y > y0 && y > y1)
27076 continue;
27077 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27078 inside = !inside;
27079 }
27080 return inside;
27081 }
27082 }
27083 return 0;
27084 }
27085
27086 Lisp_Object
27087 find_hot_spot (Lisp_Object map, int x, int y)
27088 {
27089 while (CONSP (map))
27090 {
27091 if (CONSP (XCAR (map))
27092 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27093 return XCAR (map);
27094 map = XCDR (map);
27095 }
27096
27097 return Qnil;
27098 }
27099
27100 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27101 3, 3, 0,
27102 doc: /* Lookup in image map MAP coordinates X and Y.
27103 An image map is an alist where each element has the format (AREA ID PLIST).
27104 An AREA is specified as either a rectangle, a circle, or a polygon:
27105 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27106 pixel coordinates of the upper left and bottom right corners.
27107 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27108 and the radius of the circle; r may be a float or integer.
27109 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27110 vector describes one corner in the polygon.
27111 Returns the alist element for the first matching AREA in MAP. */)
27112 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27113 {
27114 if (NILP (map))
27115 return Qnil;
27116
27117 CHECK_NUMBER (x);
27118 CHECK_NUMBER (y);
27119
27120 return find_hot_spot (map,
27121 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27122 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27123 }
27124
27125
27126 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27127 static void
27128 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27129 {
27130 /* Do not change cursor shape while dragging mouse. */
27131 if (!NILP (do_mouse_tracking))
27132 return;
27133
27134 if (!NILP (pointer))
27135 {
27136 if (EQ (pointer, Qarrow))
27137 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27138 else if (EQ (pointer, Qhand))
27139 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27140 else if (EQ (pointer, Qtext))
27141 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27142 else if (EQ (pointer, intern ("hdrag")))
27143 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27144 #ifdef HAVE_X_WINDOWS
27145 else if (EQ (pointer, intern ("vdrag")))
27146 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27147 #endif
27148 else if (EQ (pointer, intern ("hourglass")))
27149 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27150 else if (EQ (pointer, Qmodeline))
27151 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27152 else
27153 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27154 }
27155
27156 if (cursor != No_Cursor)
27157 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27158 }
27159
27160 #endif /* HAVE_WINDOW_SYSTEM */
27161
27162 /* Take proper action when mouse has moved to the mode or header line
27163 or marginal area AREA of window W, x-position X and y-position Y.
27164 X is relative to the start of the text display area of W, so the
27165 width of bitmap areas and scroll bars must be subtracted to get a
27166 position relative to the start of the mode line. */
27167
27168 static void
27169 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27170 enum window_part area)
27171 {
27172 struct window *w = XWINDOW (window);
27173 struct frame *f = XFRAME (w->frame);
27174 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27175 #ifdef HAVE_WINDOW_SYSTEM
27176 Display_Info *dpyinfo;
27177 #endif
27178 Cursor cursor = No_Cursor;
27179 Lisp_Object pointer = Qnil;
27180 int dx, dy, width, height;
27181 ptrdiff_t charpos;
27182 Lisp_Object string, object = Qnil;
27183 Lisp_Object pos IF_LINT (= Qnil), help;
27184
27185 Lisp_Object mouse_face;
27186 int original_x_pixel = x;
27187 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27188 struct glyph_row *row IF_LINT (= 0);
27189
27190 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27191 {
27192 int x0;
27193 struct glyph *end;
27194
27195 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27196 returns them in row/column units! */
27197 string = mode_line_string (w, area, &x, &y, &charpos,
27198 &object, &dx, &dy, &width, &height);
27199
27200 row = (area == ON_MODE_LINE
27201 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27202 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27203
27204 /* Find the glyph under the mouse pointer. */
27205 if (row->mode_line_p && row->enabled_p)
27206 {
27207 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27208 end = glyph + row->used[TEXT_AREA];
27209
27210 for (x0 = original_x_pixel;
27211 glyph < end && x0 >= glyph->pixel_width;
27212 ++glyph)
27213 x0 -= glyph->pixel_width;
27214
27215 if (glyph >= end)
27216 glyph = NULL;
27217 }
27218 }
27219 else
27220 {
27221 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27222 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27223 returns them in row/column units! */
27224 string = marginal_area_string (w, area, &x, &y, &charpos,
27225 &object, &dx, &dy, &width, &height);
27226 }
27227
27228 help = Qnil;
27229
27230 #ifdef HAVE_WINDOW_SYSTEM
27231 if (IMAGEP (object))
27232 {
27233 Lisp_Object image_map, hotspot;
27234 if ((image_map = Fplist_get (XCDR (object), QCmap),
27235 !NILP (image_map))
27236 && (hotspot = find_hot_spot (image_map, dx, dy),
27237 CONSP (hotspot))
27238 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27239 {
27240 Lisp_Object plist;
27241
27242 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27243 If so, we could look for mouse-enter, mouse-leave
27244 properties in PLIST (and do something...). */
27245 hotspot = XCDR (hotspot);
27246 if (CONSP (hotspot)
27247 && (plist = XCAR (hotspot), CONSP (plist)))
27248 {
27249 pointer = Fplist_get (plist, Qpointer);
27250 if (NILP (pointer))
27251 pointer = Qhand;
27252 help = Fplist_get (plist, Qhelp_echo);
27253 if (!NILP (help))
27254 {
27255 help_echo_string = help;
27256 XSETWINDOW (help_echo_window, w);
27257 help_echo_object = w->buffer;
27258 help_echo_pos = charpos;
27259 }
27260 }
27261 }
27262 if (NILP (pointer))
27263 pointer = Fplist_get (XCDR (object), QCpointer);
27264 }
27265 #endif /* HAVE_WINDOW_SYSTEM */
27266
27267 if (STRINGP (string))
27268 pos = make_number (charpos);
27269
27270 /* Set the help text and mouse pointer. If the mouse is on a part
27271 of the mode line without any text (e.g. past the right edge of
27272 the mode line text), use the default help text and pointer. */
27273 if (STRINGP (string) || area == ON_MODE_LINE)
27274 {
27275 /* Arrange to display the help by setting the global variables
27276 help_echo_string, help_echo_object, and help_echo_pos. */
27277 if (NILP (help))
27278 {
27279 if (STRINGP (string))
27280 help = Fget_text_property (pos, Qhelp_echo, string);
27281
27282 if (!NILP (help))
27283 {
27284 help_echo_string = help;
27285 XSETWINDOW (help_echo_window, w);
27286 help_echo_object = string;
27287 help_echo_pos = charpos;
27288 }
27289 else if (area == ON_MODE_LINE)
27290 {
27291 Lisp_Object default_help
27292 = buffer_local_value_1 (Qmode_line_default_help_echo,
27293 w->buffer);
27294
27295 if (STRINGP (default_help))
27296 {
27297 help_echo_string = default_help;
27298 XSETWINDOW (help_echo_window, w);
27299 help_echo_object = Qnil;
27300 help_echo_pos = -1;
27301 }
27302 }
27303 }
27304
27305 #ifdef HAVE_WINDOW_SYSTEM
27306 /* Change the mouse pointer according to what is under it. */
27307 if (FRAME_WINDOW_P (f))
27308 {
27309 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27310 if (STRINGP (string))
27311 {
27312 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27313
27314 if (NILP (pointer))
27315 pointer = Fget_text_property (pos, Qpointer, string);
27316
27317 /* Change the mouse pointer according to what is under X/Y. */
27318 if (NILP (pointer)
27319 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27320 {
27321 Lisp_Object map;
27322 map = Fget_text_property (pos, Qlocal_map, string);
27323 if (!KEYMAPP (map))
27324 map = Fget_text_property (pos, Qkeymap, string);
27325 if (!KEYMAPP (map))
27326 cursor = dpyinfo->vertical_scroll_bar_cursor;
27327 }
27328 }
27329 else
27330 /* Default mode-line pointer. */
27331 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27332 }
27333 #endif
27334 }
27335
27336 /* Change the mouse face according to what is under X/Y. */
27337 if (STRINGP (string))
27338 {
27339 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27340 if (!NILP (mouse_face)
27341 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27342 && glyph)
27343 {
27344 Lisp_Object b, e;
27345
27346 struct glyph * tmp_glyph;
27347
27348 int gpos;
27349 int gseq_length;
27350 int total_pixel_width;
27351 ptrdiff_t begpos, endpos, ignore;
27352
27353 int vpos, hpos;
27354
27355 b = Fprevious_single_property_change (make_number (charpos + 1),
27356 Qmouse_face, string, Qnil);
27357 if (NILP (b))
27358 begpos = 0;
27359 else
27360 begpos = XINT (b);
27361
27362 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27363 if (NILP (e))
27364 endpos = SCHARS (string);
27365 else
27366 endpos = XINT (e);
27367
27368 /* Calculate the glyph position GPOS of GLYPH in the
27369 displayed string, relative to the beginning of the
27370 highlighted part of the string.
27371
27372 Note: GPOS is different from CHARPOS. CHARPOS is the
27373 position of GLYPH in the internal string object. A mode
27374 line string format has structures which are converted to
27375 a flattened string by the Emacs Lisp interpreter. The
27376 internal string is an element of those structures. The
27377 displayed string is the flattened string. */
27378 tmp_glyph = row_start_glyph;
27379 while (tmp_glyph < glyph
27380 && (!(EQ (tmp_glyph->object, glyph->object)
27381 && begpos <= tmp_glyph->charpos
27382 && tmp_glyph->charpos < endpos)))
27383 tmp_glyph++;
27384 gpos = glyph - tmp_glyph;
27385
27386 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27387 the highlighted part of the displayed string to which
27388 GLYPH belongs. Note: GSEQ_LENGTH is different from
27389 SCHARS (STRING), because the latter returns the length of
27390 the internal string. */
27391 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27392 tmp_glyph > glyph
27393 && (!(EQ (tmp_glyph->object, glyph->object)
27394 && begpos <= tmp_glyph->charpos
27395 && tmp_glyph->charpos < endpos));
27396 tmp_glyph--)
27397 ;
27398 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27399
27400 /* Calculate the total pixel width of all the glyphs between
27401 the beginning of the highlighted area and GLYPH. */
27402 total_pixel_width = 0;
27403 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27404 total_pixel_width += tmp_glyph->pixel_width;
27405
27406 /* Pre calculation of re-rendering position. Note: X is in
27407 column units here, after the call to mode_line_string or
27408 marginal_area_string. */
27409 hpos = x - gpos;
27410 vpos = (area == ON_MODE_LINE
27411 ? (w->current_matrix)->nrows - 1
27412 : 0);
27413
27414 /* If GLYPH's position is included in the region that is
27415 already drawn in mouse face, we have nothing to do. */
27416 if ( EQ (window, hlinfo->mouse_face_window)
27417 && (!row->reversed_p
27418 ? (hlinfo->mouse_face_beg_col <= hpos
27419 && hpos < hlinfo->mouse_face_end_col)
27420 /* In R2L rows we swap BEG and END, see below. */
27421 : (hlinfo->mouse_face_end_col <= hpos
27422 && hpos < hlinfo->mouse_face_beg_col))
27423 && hlinfo->mouse_face_beg_row == vpos )
27424 return;
27425
27426 if (clear_mouse_face (hlinfo))
27427 cursor = No_Cursor;
27428
27429 if (!row->reversed_p)
27430 {
27431 hlinfo->mouse_face_beg_col = hpos;
27432 hlinfo->mouse_face_beg_x = original_x_pixel
27433 - (total_pixel_width + dx);
27434 hlinfo->mouse_face_end_col = hpos + gseq_length;
27435 hlinfo->mouse_face_end_x = 0;
27436 }
27437 else
27438 {
27439 /* In R2L rows, show_mouse_face expects BEG and END
27440 coordinates to be swapped. */
27441 hlinfo->mouse_face_end_col = hpos;
27442 hlinfo->mouse_face_end_x = original_x_pixel
27443 - (total_pixel_width + dx);
27444 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27445 hlinfo->mouse_face_beg_x = 0;
27446 }
27447
27448 hlinfo->mouse_face_beg_row = vpos;
27449 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27450 hlinfo->mouse_face_beg_y = 0;
27451 hlinfo->mouse_face_end_y = 0;
27452 hlinfo->mouse_face_past_end = 0;
27453 hlinfo->mouse_face_window = window;
27454
27455 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27456 charpos,
27457 0, 0, 0,
27458 &ignore,
27459 glyph->face_id,
27460 1);
27461 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27462
27463 if (NILP (pointer))
27464 pointer = Qhand;
27465 }
27466 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27467 clear_mouse_face (hlinfo);
27468 }
27469 #ifdef HAVE_WINDOW_SYSTEM
27470 if (FRAME_WINDOW_P (f))
27471 define_frame_cursor1 (f, cursor, pointer);
27472 #endif
27473 }
27474
27475
27476 /* EXPORT:
27477 Take proper action when the mouse has moved to position X, Y on
27478 frame F as regards highlighting characters that have mouse-face
27479 properties. Also de-highlighting chars where the mouse was before.
27480 X and Y can be negative or out of range. */
27481
27482 void
27483 note_mouse_highlight (struct frame *f, int x, int y)
27484 {
27485 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27486 enum window_part part = ON_NOTHING;
27487 Lisp_Object window;
27488 struct window *w;
27489 Cursor cursor = No_Cursor;
27490 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27491 struct buffer *b;
27492
27493 /* When a menu is active, don't highlight because this looks odd. */
27494 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27495 if (popup_activated ())
27496 return;
27497 #endif
27498
27499 if (NILP (Vmouse_highlight)
27500 || !f->glyphs_initialized_p
27501 || f->pointer_invisible)
27502 return;
27503
27504 hlinfo->mouse_face_mouse_x = x;
27505 hlinfo->mouse_face_mouse_y = y;
27506 hlinfo->mouse_face_mouse_frame = f;
27507
27508 if (hlinfo->mouse_face_defer)
27509 return;
27510
27511 if (gc_in_progress)
27512 {
27513 hlinfo->mouse_face_deferred_gc = 1;
27514 return;
27515 }
27516
27517 /* Which window is that in? */
27518 window = window_from_coordinates (f, x, y, &part, 1);
27519
27520 /* If displaying active text in another window, clear that. */
27521 if (! EQ (window, hlinfo->mouse_face_window)
27522 /* Also clear if we move out of text area in same window. */
27523 || (!NILP (hlinfo->mouse_face_window)
27524 && !NILP (window)
27525 && part != ON_TEXT
27526 && part != ON_MODE_LINE
27527 && part != ON_HEADER_LINE))
27528 clear_mouse_face (hlinfo);
27529
27530 /* Not on a window -> return. */
27531 if (!WINDOWP (window))
27532 return;
27533
27534 /* Reset help_echo_string. It will get recomputed below. */
27535 help_echo_string = Qnil;
27536
27537 /* Convert to window-relative pixel coordinates. */
27538 w = XWINDOW (window);
27539 frame_to_window_pixel_xy (w, &x, &y);
27540
27541 #ifdef HAVE_WINDOW_SYSTEM
27542 /* Handle tool-bar window differently since it doesn't display a
27543 buffer. */
27544 if (EQ (window, f->tool_bar_window))
27545 {
27546 note_tool_bar_highlight (f, x, y);
27547 return;
27548 }
27549 #endif
27550
27551 /* Mouse is on the mode, header line or margin? */
27552 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27553 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27554 {
27555 note_mode_line_or_margin_highlight (window, x, y, part);
27556 return;
27557 }
27558
27559 #ifdef HAVE_WINDOW_SYSTEM
27560 if (part == ON_VERTICAL_BORDER)
27561 {
27562 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27563 help_echo_string = build_string ("drag-mouse-1: resize");
27564 }
27565 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27566 || part == ON_SCROLL_BAR)
27567 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27568 else
27569 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27570 #endif
27571
27572 /* Are we in a window whose display is up to date?
27573 And verify the buffer's text has not changed. */
27574 b = XBUFFER (w->buffer);
27575 if (part == ON_TEXT
27576 && EQ (w->window_end_valid, w->buffer)
27577 && w->last_modified == BUF_MODIFF (b)
27578 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27579 {
27580 int hpos, vpos, dx, dy, area = LAST_AREA;
27581 ptrdiff_t pos;
27582 struct glyph *glyph;
27583 Lisp_Object object;
27584 Lisp_Object mouse_face = Qnil, position;
27585 Lisp_Object *overlay_vec = NULL;
27586 ptrdiff_t i, noverlays;
27587 struct buffer *obuf;
27588 ptrdiff_t obegv, ozv;
27589 int same_region;
27590
27591 /* Find the glyph under X/Y. */
27592 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27593
27594 #ifdef HAVE_WINDOW_SYSTEM
27595 /* Look for :pointer property on image. */
27596 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27597 {
27598 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27599 if (img != NULL && IMAGEP (img->spec))
27600 {
27601 Lisp_Object image_map, hotspot;
27602 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27603 !NILP (image_map))
27604 && (hotspot = find_hot_spot (image_map,
27605 glyph->slice.img.x + dx,
27606 glyph->slice.img.y + dy),
27607 CONSP (hotspot))
27608 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27609 {
27610 Lisp_Object plist;
27611
27612 /* Could check XCAR (hotspot) to see if we enter/leave
27613 this hot-spot.
27614 If so, we could look for mouse-enter, mouse-leave
27615 properties in PLIST (and do something...). */
27616 hotspot = XCDR (hotspot);
27617 if (CONSP (hotspot)
27618 && (plist = XCAR (hotspot), CONSP (plist)))
27619 {
27620 pointer = Fplist_get (plist, Qpointer);
27621 if (NILP (pointer))
27622 pointer = Qhand;
27623 help_echo_string = Fplist_get (plist, Qhelp_echo);
27624 if (!NILP (help_echo_string))
27625 {
27626 help_echo_window = window;
27627 help_echo_object = glyph->object;
27628 help_echo_pos = glyph->charpos;
27629 }
27630 }
27631 }
27632 if (NILP (pointer))
27633 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27634 }
27635 }
27636 #endif /* HAVE_WINDOW_SYSTEM */
27637
27638 /* Clear mouse face if X/Y not over text. */
27639 if (glyph == NULL
27640 || area != TEXT_AREA
27641 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27642 /* Glyph's OBJECT is an integer for glyphs inserted by the
27643 display engine for its internal purposes, like truncation
27644 and continuation glyphs and blanks beyond the end of
27645 line's text on text terminals. If we are over such a
27646 glyph, we are not over any text. */
27647 || INTEGERP (glyph->object)
27648 /* R2L rows have a stretch glyph at their front, which
27649 stands for no text, whereas L2R rows have no glyphs at
27650 all beyond the end of text. Treat such stretch glyphs
27651 like we do with NULL glyphs in L2R rows. */
27652 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27653 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27654 && glyph->type == STRETCH_GLYPH
27655 && glyph->avoid_cursor_p))
27656 {
27657 if (clear_mouse_face (hlinfo))
27658 cursor = No_Cursor;
27659 #ifdef HAVE_WINDOW_SYSTEM
27660 if (FRAME_WINDOW_P (f) && NILP (pointer))
27661 {
27662 if (area != TEXT_AREA)
27663 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27664 else
27665 pointer = Vvoid_text_area_pointer;
27666 }
27667 #endif
27668 goto set_cursor;
27669 }
27670
27671 pos = glyph->charpos;
27672 object = glyph->object;
27673 if (!STRINGP (object) && !BUFFERP (object))
27674 goto set_cursor;
27675
27676 /* If we get an out-of-range value, return now; avoid an error. */
27677 if (BUFFERP (object) && pos > BUF_Z (b))
27678 goto set_cursor;
27679
27680 /* Make the window's buffer temporarily current for
27681 overlays_at and compute_char_face. */
27682 obuf = current_buffer;
27683 current_buffer = b;
27684 obegv = BEGV;
27685 ozv = ZV;
27686 BEGV = BEG;
27687 ZV = Z;
27688
27689 /* Is this char mouse-active or does it have help-echo? */
27690 position = make_number (pos);
27691
27692 if (BUFFERP (object))
27693 {
27694 /* Put all the overlays we want in a vector in overlay_vec. */
27695 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27696 /* Sort overlays into increasing priority order. */
27697 noverlays = sort_overlays (overlay_vec, noverlays, w);
27698 }
27699 else
27700 noverlays = 0;
27701
27702 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27703
27704 if (same_region)
27705 cursor = No_Cursor;
27706
27707 /* Check mouse-face highlighting. */
27708 if (! same_region
27709 /* If there exists an overlay with mouse-face overlapping
27710 the one we are currently highlighting, we have to
27711 check if we enter the overlapping overlay, and then
27712 highlight only that. */
27713 || (OVERLAYP (hlinfo->mouse_face_overlay)
27714 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27715 {
27716 /* Find the highest priority overlay with a mouse-face. */
27717 Lisp_Object overlay = Qnil;
27718 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27719 {
27720 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27721 if (!NILP (mouse_face))
27722 overlay = overlay_vec[i];
27723 }
27724
27725 /* If we're highlighting the same overlay as before, there's
27726 no need to do that again. */
27727 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27728 goto check_help_echo;
27729 hlinfo->mouse_face_overlay = overlay;
27730
27731 /* Clear the display of the old active region, if any. */
27732 if (clear_mouse_face (hlinfo))
27733 cursor = No_Cursor;
27734
27735 /* If no overlay applies, get a text property. */
27736 if (NILP (overlay))
27737 mouse_face = Fget_text_property (position, Qmouse_face, object);
27738
27739 /* Next, compute the bounds of the mouse highlighting and
27740 display it. */
27741 if (!NILP (mouse_face) && STRINGP (object))
27742 {
27743 /* The mouse-highlighting comes from a display string
27744 with a mouse-face. */
27745 Lisp_Object s, e;
27746 ptrdiff_t ignore;
27747
27748 s = Fprevious_single_property_change
27749 (make_number (pos + 1), Qmouse_face, object, Qnil);
27750 e = Fnext_single_property_change
27751 (position, Qmouse_face, object, Qnil);
27752 if (NILP (s))
27753 s = make_number (0);
27754 if (NILP (e))
27755 e = make_number (SCHARS (object) - 1);
27756 mouse_face_from_string_pos (w, hlinfo, object,
27757 XINT (s), XINT (e));
27758 hlinfo->mouse_face_past_end = 0;
27759 hlinfo->mouse_face_window = window;
27760 hlinfo->mouse_face_face_id
27761 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27762 glyph->face_id, 1);
27763 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27764 cursor = No_Cursor;
27765 }
27766 else
27767 {
27768 /* The mouse-highlighting, if any, comes from an overlay
27769 or text property in the buffer. */
27770 Lisp_Object buffer IF_LINT (= Qnil);
27771 Lisp_Object disp_string IF_LINT (= Qnil);
27772
27773 if (STRINGP (object))
27774 {
27775 /* If we are on a display string with no mouse-face,
27776 check if the text under it has one. */
27777 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27778 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27779 pos = string_buffer_position (object, start);
27780 if (pos > 0)
27781 {
27782 mouse_face = get_char_property_and_overlay
27783 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27784 buffer = w->buffer;
27785 disp_string = object;
27786 }
27787 }
27788 else
27789 {
27790 buffer = object;
27791 disp_string = Qnil;
27792 }
27793
27794 if (!NILP (mouse_face))
27795 {
27796 Lisp_Object before, after;
27797 Lisp_Object before_string, after_string;
27798 /* To correctly find the limits of mouse highlight
27799 in a bidi-reordered buffer, we must not use the
27800 optimization of limiting the search in
27801 previous-single-property-change and
27802 next-single-property-change, because
27803 rows_from_pos_range needs the real start and end
27804 positions to DTRT in this case. That's because
27805 the first row visible in a window does not
27806 necessarily display the character whose position
27807 is the smallest. */
27808 Lisp_Object lim1 =
27809 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27810 ? Fmarker_position (w->start)
27811 : Qnil;
27812 Lisp_Object lim2 =
27813 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27814 ? make_number (BUF_Z (XBUFFER (buffer))
27815 - XFASTINT (w->window_end_pos))
27816 : Qnil;
27817
27818 if (NILP (overlay))
27819 {
27820 /* Handle the text property case. */
27821 before = Fprevious_single_property_change
27822 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27823 after = Fnext_single_property_change
27824 (make_number (pos), Qmouse_face, buffer, lim2);
27825 before_string = after_string = Qnil;
27826 }
27827 else
27828 {
27829 /* Handle the overlay case. */
27830 before = Foverlay_start (overlay);
27831 after = Foverlay_end (overlay);
27832 before_string = Foverlay_get (overlay, Qbefore_string);
27833 after_string = Foverlay_get (overlay, Qafter_string);
27834
27835 if (!STRINGP (before_string)) before_string = Qnil;
27836 if (!STRINGP (after_string)) after_string = Qnil;
27837 }
27838
27839 mouse_face_from_buffer_pos (window, hlinfo, pos,
27840 NILP (before)
27841 ? 1
27842 : XFASTINT (before),
27843 NILP (after)
27844 ? BUF_Z (XBUFFER (buffer))
27845 : XFASTINT (after),
27846 before_string, after_string,
27847 disp_string);
27848 cursor = No_Cursor;
27849 }
27850 }
27851 }
27852
27853 check_help_echo:
27854
27855 /* Look for a `help-echo' property. */
27856 if (NILP (help_echo_string)) {
27857 Lisp_Object help, overlay;
27858
27859 /* Check overlays first. */
27860 help = overlay = Qnil;
27861 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27862 {
27863 overlay = overlay_vec[i];
27864 help = Foverlay_get (overlay, Qhelp_echo);
27865 }
27866
27867 if (!NILP (help))
27868 {
27869 help_echo_string = help;
27870 help_echo_window = window;
27871 help_echo_object = overlay;
27872 help_echo_pos = pos;
27873 }
27874 else
27875 {
27876 Lisp_Object obj = glyph->object;
27877 ptrdiff_t charpos = glyph->charpos;
27878
27879 /* Try text properties. */
27880 if (STRINGP (obj)
27881 && charpos >= 0
27882 && charpos < SCHARS (obj))
27883 {
27884 help = Fget_text_property (make_number (charpos),
27885 Qhelp_echo, obj);
27886 if (NILP (help))
27887 {
27888 /* If the string itself doesn't specify a help-echo,
27889 see if the buffer text ``under'' it does. */
27890 struct glyph_row *r
27891 = MATRIX_ROW (w->current_matrix, vpos);
27892 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27893 ptrdiff_t p = string_buffer_position (obj, start);
27894 if (p > 0)
27895 {
27896 help = Fget_char_property (make_number (p),
27897 Qhelp_echo, w->buffer);
27898 if (!NILP (help))
27899 {
27900 charpos = p;
27901 obj = w->buffer;
27902 }
27903 }
27904 }
27905 }
27906 else if (BUFFERP (obj)
27907 && charpos >= BEGV
27908 && charpos < ZV)
27909 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27910 obj);
27911
27912 if (!NILP (help))
27913 {
27914 help_echo_string = help;
27915 help_echo_window = window;
27916 help_echo_object = obj;
27917 help_echo_pos = charpos;
27918 }
27919 }
27920 }
27921
27922 #ifdef HAVE_WINDOW_SYSTEM
27923 /* Look for a `pointer' property. */
27924 if (FRAME_WINDOW_P (f) && NILP (pointer))
27925 {
27926 /* Check overlays first. */
27927 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27928 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27929
27930 if (NILP (pointer))
27931 {
27932 Lisp_Object obj = glyph->object;
27933 ptrdiff_t charpos = glyph->charpos;
27934
27935 /* Try text properties. */
27936 if (STRINGP (obj)
27937 && charpos >= 0
27938 && charpos < SCHARS (obj))
27939 {
27940 pointer = Fget_text_property (make_number (charpos),
27941 Qpointer, obj);
27942 if (NILP (pointer))
27943 {
27944 /* If the string itself doesn't specify a pointer,
27945 see if the buffer text ``under'' it does. */
27946 struct glyph_row *r
27947 = MATRIX_ROW (w->current_matrix, vpos);
27948 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27949 ptrdiff_t p = string_buffer_position (obj, start);
27950 if (p > 0)
27951 pointer = Fget_char_property (make_number (p),
27952 Qpointer, w->buffer);
27953 }
27954 }
27955 else if (BUFFERP (obj)
27956 && charpos >= BEGV
27957 && charpos < ZV)
27958 pointer = Fget_text_property (make_number (charpos),
27959 Qpointer, obj);
27960 }
27961 }
27962 #endif /* HAVE_WINDOW_SYSTEM */
27963
27964 BEGV = obegv;
27965 ZV = ozv;
27966 current_buffer = obuf;
27967 }
27968
27969 set_cursor:
27970
27971 #ifdef HAVE_WINDOW_SYSTEM
27972 if (FRAME_WINDOW_P (f))
27973 define_frame_cursor1 (f, cursor, pointer);
27974 #else
27975 /* This is here to prevent a compiler error, about "label at end of
27976 compound statement". */
27977 return;
27978 #endif
27979 }
27980
27981
27982 /* EXPORT for RIF:
27983 Clear any mouse-face on window W. This function is part of the
27984 redisplay interface, and is called from try_window_id and similar
27985 functions to ensure the mouse-highlight is off. */
27986
27987 void
27988 x_clear_window_mouse_face (struct window *w)
27989 {
27990 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27991 Lisp_Object window;
27992
27993 BLOCK_INPUT;
27994 XSETWINDOW (window, w);
27995 if (EQ (window, hlinfo->mouse_face_window))
27996 clear_mouse_face (hlinfo);
27997 UNBLOCK_INPUT;
27998 }
27999
28000
28001 /* EXPORT:
28002 Just discard the mouse face information for frame F, if any.
28003 This is used when the size of F is changed. */
28004
28005 void
28006 cancel_mouse_face (struct frame *f)
28007 {
28008 Lisp_Object window;
28009 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28010
28011 window = hlinfo->mouse_face_window;
28012 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28013 {
28014 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28015 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28016 hlinfo->mouse_face_window = Qnil;
28017 }
28018 }
28019
28020
28021 \f
28022 /***********************************************************************
28023 Exposure Events
28024 ***********************************************************************/
28025
28026 #ifdef HAVE_WINDOW_SYSTEM
28027
28028 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28029 which intersects rectangle R. R is in window-relative coordinates. */
28030
28031 static void
28032 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28033 enum glyph_row_area area)
28034 {
28035 struct glyph *first = row->glyphs[area];
28036 struct glyph *end = row->glyphs[area] + row->used[area];
28037 struct glyph *last;
28038 int first_x, start_x, x;
28039
28040 if (area == TEXT_AREA && row->fill_line_p)
28041 /* If row extends face to end of line write the whole line. */
28042 draw_glyphs (w, 0, row, area,
28043 0, row->used[area],
28044 DRAW_NORMAL_TEXT, 0);
28045 else
28046 {
28047 /* Set START_X to the window-relative start position for drawing glyphs of
28048 AREA. The first glyph of the text area can be partially visible.
28049 The first glyphs of other areas cannot. */
28050 start_x = window_box_left_offset (w, area);
28051 x = start_x;
28052 if (area == TEXT_AREA)
28053 x += row->x;
28054
28055 /* Find the first glyph that must be redrawn. */
28056 while (first < end
28057 && x + first->pixel_width < r->x)
28058 {
28059 x += first->pixel_width;
28060 ++first;
28061 }
28062
28063 /* Find the last one. */
28064 last = first;
28065 first_x = x;
28066 while (last < end
28067 && x < r->x + r->width)
28068 {
28069 x += last->pixel_width;
28070 ++last;
28071 }
28072
28073 /* Repaint. */
28074 if (last > first)
28075 draw_glyphs (w, first_x - start_x, row, area,
28076 first - row->glyphs[area], last - row->glyphs[area],
28077 DRAW_NORMAL_TEXT, 0);
28078 }
28079 }
28080
28081
28082 /* Redraw the parts of the glyph row ROW on window W intersecting
28083 rectangle R. R is in window-relative coordinates. Value is
28084 non-zero if mouse-face was overwritten. */
28085
28086 static int
28087 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28088 {
28089 eassert (row->enabled_p);
28090
28091 if (row->mode_line_p || w->pseudo_window_p)
28092 draw_glyphs (w, 0, row, TEXT_AREA,
28093 0, row->used[TEXT_AREA],
28094 DRAW_NORMAL_TEXT, 0);
28095 else
28096 {
28097 if (row->used[LEFT_MARGIN_AREA])
28098 expose_area (w, row, r, LEFT_MARGIN_AREA);
28099 if (row->used[TEXT_AREA])
28100 expose_area (w, row, r, TEXT_AREA);
28101 if (row->used[RIGHT_MARGIN_AREA])
28102 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28103 draw_row_fringe_bitmaps (w, row);
28104 }
28105
28106 return row->mouse_face_p;
28107 }
28108
28109
28110 /* Redraw those parts of glyphs rows during expose event handling that
28111 overlap other rows. Redrawing of an exposed line writes over parts
28112 of lines overlapping that exposed line; this function fixes that.
28113
28114 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28115 row in W's current matrix that is exposed and overlaps other rows.
28116 LAST_OVERLAPPING_ROW is the last such row. */
28117
28118 static void
28119 expose_overlaps (struct window *w,
28120 struct glyph_row *first_overlapping_row,
28121 struct glyph_row *last_overlapping_row,
28122 XRectangle *r)
28123 {
28124 struct glyph_row *row;
28125
28126 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28127 if (row->overlapping_p)
28128 {
28129 eassert (row->enabled_p && !row->mode_line_p);
28130
28131 row->clip = r;
28132 if (row->used[LEFT_MARGIN_AREA])
28133 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28134
28135 if (row->used[TEXT_AREA])
28136 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28137
28138 if (row->used[RIGHT_MARGIN_AREA])
28139 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28140 row->clip = NULL;
28141 }
28142 }
28143
28144
28145 /* Return non-zero if W's cursor intersects rectangle R. */
28146
28147 static int
28148 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28149 {
28150 XRectangle cr, result;
28151 struct glyph *cursor_glyph;
28152 struct glyph_row *row;
28153
28154 if (w->phys_cursor.vpos >= 0
28155 && w->phys_cursor.vpos < w->current_matrix->nrows
28156 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28157 row->enabled_p)
28158 && row->cursor_in_fringe_p)
28159 {
28160 /* Cursor is in the fringe. */
28161 cr.x = window_box_right_offset (w,
28162 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28163 ? RIGHT_MARGIN_AREA
28164 : TEXT_AREA));
28165 cr.y = row->y;
28166 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28167 cr.height = row->height;
28168 return x_intersect_rectangles (&cr, r, &result);
28169 }
28170
28171 cursor_glyph = get_phys_cursor_glyph (w);
28172 if (cursor_glyph)
28173 {
28174 /* r is relative to W's box, but w->phys_cursor.x is relative
28175 to left edge of W's TEXT area. Adjust it. */
28176 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28177 cr.y = w->phys_cursor.y;
28178 cr.width = cursor_glyph->pixel_width;
28179 cr.height = w->phys_cursor_height;
28180 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28181 I assume the effect is the same -- and this is portable. */
28182 return x_intersect_rectangles (&cr, r, &result);
28183 }
28184 /* If we don't understand the format, pretend we're not in the hot-spot. */
28185 return 0;
28186 }
28187
28188
28189 /* EXPORT:
28190 Draw a vertical window border to the right of window W if W doesn't
28191 have vertical scroll bars. */
28192
28193 void
28194 x_draw_vertical_border (struct window *w)
28195 {
28196 struct frame *f = XFRAME (WINDOW_FRAME (w));
28197
28198 /* We could do better, if we knew what type of scroll-bar the adjacent
28199 windows (on either side) have... But we don't :-(
28200 However, I think this works ok. ++KFS 2003-04-25 */
28201
28202 /* Redraw borders between horizontally adjacent windows. Don't
28203 do it for frames with vertical scroll bars because either the
28204 right scroll bar of a window, or the left scroll bar of its
28205 neighbor will suffice as a border. */
28206 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28207 return;
28208
28209 if (!WINDOW_RIGHTMOST_P (w)
28210 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28211 {
28212 int x0, x1, y0, y1;
28213
28214 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28215 y1 -= 1;
28216
28217 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28218 x1 -= 1;
28219
28220 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28221 }
28222 else if (!WINDOW_LEFTMOST_P (w)
28223 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28224 {
28225 int x0, x1, y0, y1;
28226
28227 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28228 y1 -= 1;
28229
28230 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28231 x0 -= 1;
28232
28233 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28234 }
28235 }
28236
28237
28238 /* Redraw the part of window W intersection rectangle FR. Pixel
28239 coordinates in FR are frame-relative. Call this function with
28240 input blocked. Value is non-zero if the exposure overwrites
28241 mouse-face. */
28242
28243 static int
28244 expose_window (struct window *w, XRectangle *fr)
28245 {
28246 struct frame *f = XFRAME (w->frame);
28247 XRectangle wr, r;
28248 int mouse_face_overwritten_p = 0;
28249
28250 /* If window is not yet fully initialized, do nothing. This can
28251 happen when toolkit scroll bars are used and a window is split.
28252 Reconfiguring the scroll bar will generate an expose for a newly
28253 created window. */
28254 if (w->current_matrix == NULL)
28255 return 0;
28256
28257 /* When we're currently updating the window, display and current
28258 matrix usually don't agree. Arrange for a thorough display
28259 later. */
28260 if (w == updated_window)
28261 {
28262 SET_FRAME_GARBAGED (f);
28263 return 0;
28264 }
28265
28266 /* Frame-relative pixel rectangle of W. */
28267 wr.x = WINDOW_LEFT_EDGE_X (w);
28268 wr.y = WINDOW_TOP_EDGE_Y (w);
28269 wr.width = WINDOW_TOTAL_WIDTH (w);
28270 wr.height = WINDOW_TOTAL_HEIGHT (w);
28271
28272 if (x_intersect_rectangles (fr, &wr, &r))
28273 {
28274 int yb = window_text_bottom_y (w);
28275 struct glyph_row *row;
28276 int cursor_cleared_p, phys_cursor_on_p;
28277 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28278
28279 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28280 r.x, r.y, r.width, r.height));
28281
28282 /* Convert to window coordinates. */
28283 r.x -= WINDOW_LEFT_EDGE_X (w);
28284 r.y -= WINDOW_TOP_EDGE_Y (w);
28285
28286 /* Turn off the cursor. */
28287 if (!w->pseudo_window_p
28288 && phys_cursor_in_rect_p (w, &r))
28289 {
28290 x_clear_cursor (w);
28291 cursor_cleared_p = 1;
28292 }
28293 else
28294 cursor_cleared_p = 0;
28295
28296 /* If the row containing the cursor extends face to end of line,
28297 then expose_area might overwrite the cursor outside the
28298 rectangle and thus notice_overwritten_cursor might clear
28299 w->phys_cursor_on_p. We remember the original value and
28300 check later if it is changed. */
28301 phys_cursor_on_p = w->phys_cursor_on_p;
28302
28303 /* Update lines intersecting rectangle R. */
28304 first_overlapping_row = last_overlapping_row = NULL;
28305 for (row = w->current_matrix->rows;
28306 row->enabled_p;
28307 ++row)
28308 {
28309 int y0 = row->y;
28310 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28311
28312 if ((y0 >= r.y && y0 < r.y + r.height)
28313 || (y1 > r.y && y1 < r.y + r.height)
28314 || (r.y >= y0 && r.y < y1)
28315 || (r.y + r.height > y0 && r.y + r.height < y1))
28316 {
28317 /* A header line may be overlapping, but there is no need
28318 to fix overlapping areas for them. KFS 2005-02-12 */
28319 if (row->overlapping_p && !row->mode_line_p)
28320 {
28321 if (first_overlapping_row == NULL)
28322 first_overlapping_row = row;
28323 last_overlapping_row = row;
28324 }
28325
28326 row->clip = fr;
28327 if (expose_line (w, row, &r))
28328 mouse_face_overwritten_p = 1;
28329 row->clip = NULL;
28330 }
28331 else if (row->overlapping_p)
28332 {
28333 /* We must redraw a row overlapping the exposed area. */
28334 if (y0 < r.y
28335 ? y0 + row->phys_height > r.y
28336 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28337 {
28338 if (first_overlapping_row == NULL)
28339 first_overlapping_row = row;
28340 last_overlapping_row = row;
28341 }
28342 }
28343
28344 if (y1 >= yb)
28345 break;
28346 }
28347
28348 /* Display the mode line if there is one. */
28349 if (WINDOW_WANTS_MODELINE_P (w)
28350 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28351 row->enabled_p)
28352 && row->y < r.y + r.height)
28353 {
28354 if (expose_line (w, row, &r))
28355 mouse_face_overwritten_p = 1;
28356 }
28357
28358 if (!w->pseudo_window_p)
28359 {
28360 /* Fix the display of overlapping rows. */
28361 if (first_overlapping_row)
28362 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28363 fr);
28364
28365 /* Draw border between windows. */
28366 x_draw_vertical_border (w);
28367
28368 /* Turn the cursor on again. */
28369 if (cursor_cleared_p
28370 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28371 update_window_cursor (w, 1);
28372 }
28373 }
28374
28375 return mouse_face_overwritten_p;
28376 }
28377
28378
28379
28380 /* Redraw (parts) of all windows in the window tree rooted at W that
28381 intersect R. R contains frame pixel coordinates. Value is
28382 non-zero if the exposure overwrites mouse-face. */
28383
28384 static int
28385 expose_window_tree (struct window *w, XRectangle *r)
28386 {
28387 struct frame *f = XFRAME (w->frame);
28388 int mouse_face_overwritten_p = 0;
28389
28390 while (w && !FRAME_GARBAGED_P (f))
28391 {
28392 if (!NILP (w->hchild))
28393 mouse_face_overwritten_p
28394 |= expose_window_tree (XWINDOW (w->hchild), r);
28395 else if (!NILP (w->vchild))
28396 mouse_face_overwritten_p
28397 |= expose_window_tree (XWINDOW (w->vchild), r);
28398 else
28399 mouse_face_overwritten_p |= expose_window (w, r);
28400
28401 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28402 }
28403
28404 return mouse_face_overwritten_p;
28405 }
28406
28407
28408 /* EXPORT:
28409 Redisplay an exposed area of frame F. X and Y are the upper-left
28410 corner of the exposed rectangle. W and H are width and height of
28411 the exposed area. All are pixel values. W or H zero means redraw
28412 the entire frame. */
28413
28414 void
28415 expose_frame (struct frame *f, int x, int y, int w, int h)
28416 {
28417 XRectangle r;
28418 int mouse_face_overwritten_p = 0;
28419
28420 TRACE ((stderr, "expose_frame "));
28421
28422 /* No need to redraw if frame will be redrawn soon. */
28423 if (FRAME_GARBAGED_P (f))
28424 {
28425 TRACE ((stderr, " garbaged\n"));
28426 return;
28427 }
28428
28429 /* If basic faces haven't been realized yet, there is no point in
28430 trying to redraw anything. This can happen when we get an expose
28431 event while Emacs is starting, e.g. by moving another window. */
28432 if (FRAME_FACE_CACHE (f) == NULL
28433 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28434 {
28435 TRACE ((stderr, " no faces\n"));
28436 return;
28437 }
28438
28439 if (w == 0 || h == 0)
28440 {
28441 r.x = r.y = 0;
28442 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28443 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28444 }
28445 else
28446 {
28447 r.x = x;
28448 r.y = y;
28449 r.width = w;
28450 r.height = h;
28451 }
28452
28453 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28454 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28455
28456 if (WINDOWP (f->tool_bar_window))
28457 mouse_face_overwritten_p
28458 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28459
28460 #ifdef HAVE_X_WINDOWS
28461 #ifndef MSDOS
28462 #ifndef USE_X_TOOLKIT
28463 if (WINDOWP (f->menu_bar_window))
28464 mouse_face_overwritten_p
28465 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28466 #endif /* not USE_X_TOOLKIT */
28467 #endif
28468 #endif
28469
28470 /* Some window managers support a focus-follows-mouse style with
28471 delayed raising of frames. Imagine a partially obscured frame,
28472 and moving the mouse into partially obscured mouse-face on that
28473 frame. The visible part of the mouse-face will be highlighted,
28474 then the WM raises the obscured frame. With at least one WM, KDE
28475 2.1, Emacs is not getting any event for the raising of the frame
28476 (even tried with SubstructureRedirectMask), only Expose events.
28477 These expose events will draw text normally, i.e. not
28478 highlighted. Which means we must redo the highlight here.
28479 Subsume it under ``we love X''. --gerd 2001-08-15 */
28480 /* Included in Windows version because Windows most likely does not
28481 do the right thing if any third party tool offers
28482 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28483 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28484 {
28485 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28486 if (f == hlinfo->mouse_face_mouse_frame)
28487 {
28488 int mouse_x = hlinfo->mouse_face_mouse_x;
28489 int mouse_y = hlinfo->mouse_face_mouse_y;
28490 clear_mouse_face (hlinfo);
28491 note_mouse_highlight (f, mouse_x, mouse_y);
28492 }
28493 }
28494 }
28495
28496
28497 /* EXPORT:
28498 Determine the intersection of two rectangles R1 and R2. Return
28499 the intersection in *RESULT. Value is non-zero if RESULT is not
28500 empty. */
28501
28502 int
28503 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28504 {
28505 XRectangle *left, *right;
28506 XRectangle *upper, *lower;
28507 int intersection_p = 0;
28508
28509 /* Rearrange so that R1 is the left-most rectangle. */
28510 if (r1->x < r2->x)
28511 left = r1, right = r2;
28512 else
28513 left = r2, right = r1;
28514
28515 /* X0 of the intersection is right.x0, if this is inside R1,
28516 otherwise there is no intersection. */
28517 if (right->x <= left->x + left->width)
28518 {
28519 result->x = right->x;
28520
28521 /* The right end of the intersection is the minimum of
28522 the right ends of left and right. */
28523 result->width = (min (left->x + left->width, right->x + right->width)
28524 - result->x);
28525
28526 /* Same game for Y. */
28527 if (r1->y < r2->y)
28528 upper = r1, lower = r2;
28529 else
28530 upper = r2, lower = r1;
28531
28532 /* The upper end of the intersection is lower.y0, if this is inside
28533 of upper. Otherwise, there is no intersection. */
28534 if (lower->y <= upper->y + upper->height)
28535 {
28536 result->y = lower->y;
28537
28538 /* The lower end of the intersection is the minimum of the lower
28539 ends of upper and lower. */
28540 result->height = (min (lower->y + lower->height,
28541 upper->y + upper->height)
28542 - result->y);
28543 intersection_p = 1;
28544 }
28545 }
28546
28547 return intersection_p;
28548 }
28549
28550 #endif /* HAVE_WINDOW_SYSTEM */
28551
28552 \f
28553 /***********************************************************************
28554 Initialization
28555 ***********************************************************************/
28556
28557 void
28558 syms_of_xdisp (void)
28559 {
28560 Vwith_echo_area_save_vector = Qnil;
28561 staticpro (&Vwith_echo_area_save_vector);
28562
28563 Vmessage_stack = Qnil;
28564 staticpro (&Vmessage_stack);
28565
28566 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28567
28568 message_dolog_marker1 = Fmake_marker ();
28569 staticpro (&message_dolog_marker1);
28570 message_dolog_marker2 = Fmake_marker ();
28571 staticpro (&message_dolog_marker2);
28572 message_dolog_marker3 = Fmake_marker ();
28573 staticpro (&message_dolog_marker3);
28574
28575 #ifdef GLYPH_DEBUG
28576 defsubr (&Sdump_frame_glyph_matrix);
28577 defsubr (&Sdump_glyph_matrix);
28578 defsubr (&Sdump_glyph_row);
28579 defsubr (&Sdump_tool_bar_row);
28580 defsubr (&Strace_redisplay);
28581 defsubr (&Strace_to_stderr);
28582 #endif
28583 #ifdef HAVE_WINDOW_SYSTEM
28584 defsubr (&Stool_bar_lines_needed);
28585 defsubr (&Slookup_image_map);
28586 #endif
28587 defsubr (&Sformat_mode_line);
28588 defsubr (&Sinvisible_p);
28589 defsubr (&Scurrent_bidi_paragraph_direction);
28590
28591 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28592 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28593 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28594 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28595 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28596 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28597 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28598 DEFSYM (Qeval, "eval");
28599 DEFSYM (QCdata, ":data");
28600 DEFSYM (Qdisplay, "display");
28601 DEFSYM (Qspace_width, "space-width");
28602 DEFSYM (Qraise, "raise");
28603 DEFSYM (Qslice, "slice");
28604 DEFSYM (Qspace, "space");
28605 DEFSYM (Qmargin, "margin");
28606 DEFSYM (Qpointer, "pointer");
28607 DEFSYM (Qleft_margin, "left-margin");
28608 DEFSYM (Qright_margin, "right-margin");
28609 DEFSYM (Qcenter, "center");
28610 DEFSYM (Qline_height, "line-height");
28611 DEFSYM (QCalign_to, ":align-to");
28612 DEFSYM (QCrelative_width, ":relative-width");
28613 DEFSYM (QCrelative_height, ":relative-height");
28614 DEFSYM (QCeval, ":eval");
28615 DEFSYM (QCpropertize, ":propertize");
28616 DEFSYM (QCfile, ":file");
28617 DEFSYM (Qfontified, "fontified");
28618 DEFSYM (Qfontification_functions, "fontification-functions");
28619 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28620 DEFSYM (Qescape_glyph, "escape-glyph");
28621 DEFSYM (Qnobreak_space, "nobreak-space");
28622 DEFSYM (Qimage, "image");
28623 DEFSYM (Qtext, "text");
28624 DEFSYM (Qboth, "both");
28625 DEFSYM (Qboth_horiz, "both-horiz");
28626 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28627 DEFSYM (QCmap, ":map");
28628 DEFSYM (QCpointer, ":pointer");
28629 DEFSYM (Qrect, "rect");
28630 DEFSYM (Qcircle, "circle");
28631 DEFSYM (Qpoly, "poly");
28632 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28633 DEFSYM (Qgrow_only, "grow-only");
28634 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28635 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28636 DEFSYM (Qposition, "position");
28637 DEFSYM (Qbuffer_position, "buffer-position");
28638 DEFSYM (Qobject, "object");
28639 DEFSYM (Qbar, "bar");
28640 DEFSYM (Qhbar, "hbar");
28641 DEFSYM (Qbox, "box");
28642 DEFSYM (Qhollow, "hollow");
28643 DEFSYM (Qhand, "hand");
28644 DEFSYM (Qarrow, "arrow");
28645 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28646
28647 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28648 Fcons (intern_c_string ("void-variable"), Qnil)),
28649 Qnil);
28650 staticpro (&list_of_error);
28651
28652 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28653 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28654 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28655 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28656
28657 echo_buffer[0] = echo_buffer[1] = Qnil;
28658 staticpro (&echo_buffer[0]);
28659 staticpro (&echo_buffer[1]);
28660
28661 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28662 staticpro (&echo_area_buffer[0]);
28663 staticpro (&echo_area_buffer[1]);
28664
28665 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28666 staticpro (&Vmessages_buffer_name);
28667
28668 mode_line_proptrans_alist = Qnil;
28669 staticpro (&mode_line_proptrans_alist);
28670 mode_line_string_list = Qnil;
28671 staticpro (&mode_line_string_list);
28672 mode_line_string_face = Qnil;
28673 staticpro (&mode_line_string_face);
28674 mode_line_string_face_prop = Qnil;
28675 staticpro (&mode_line_string_face_prop);
28676 Vmode_line_unwind_vector = Qnil;
28677 staticpro (&Vmode_line_unwind_vector);
28678
28679 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28680
28681 help_echo_string = Qnil;
28682 staticpro (&help_echo_string);
28683 help_echo_object = Qnil;
28684 staticpro (&help_echo_object);
28685 help_echo_window = Qnil;
28686 staticpro (&help_echo_window);
28687 previous_help_echo_string = Qnil;
28688 staticpro (&previous_help_echo_string);
28689 help_echo_pos = -1;
28690
28691 DEFSYM (Qright_to_left, "right-to-left");
28692 DEFSYM (Qleft_to_right, "left-to-right");
28693
28694 #ifdef HAVE_WINDOW_SYSTEM
28695 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28696 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28697 For example, if a block cursor is over a tab, it will be drawn as
28698 wide as that tab on the display. */);
28699 x_stretch_cursor_p = 0;
28700 #endif
28701
28702 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28703 doc: /* Non-nil means highlight trailing whitespace.
28704 The face used for trailing whitespace is `trailing-whitespace'. */);
28705 Vshow_trailing_whitespace = Qnil;
28706
28707 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28708 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28709 If the value is t, Emacs highlights non-ASCII chars which have the
28710 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28711 or `escape-glyph' face respectively.
28712
28713 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28714 U+2011 (non-breaking hyphen) are affected.
28715
28716 Any other non-nil value means to display these characters as a escape
28717 glyph followed by an ordinary space or hyphen.
28718
28719 A value of nil means no special handling of these characters. */);
28720 Vnobreak_char_display = Qt;
28721
28722 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28723 doc: /* The pointer shape to show in void text areas.
28724 A value of nil means to show the text pointer. Other options are `arrow',
28725 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28726 Vvoid_text_area_pointer = Qarrow;
28727
28728 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28729 doc: /* Non-nil means don't actually do any redisplay.
28730 This is used for internal purposes. */);
28731 Vinhibit_redisplay = Qnil;
28732
28733 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28734 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28735 Vglobal_mode_string = Qnil;
28736
28737 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28738 doc: /* Marker for where to display an arrow on top of the buffer text.
28739 This must be the beginning of a line in order to work.
28740 See also `overlay-arrow-string'. */);
28741 Voverlay_arrow_position = Qnil;
28742
28743 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28744 doc: /* String to display as an arrow in non-window frames.
28745 See also `overlay-arrow-position'. */);
28746 Voverlay_arrow_string = make_pure_c_string ("=>");
28747
28748 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28749 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28750 The symbols on this list are examined during redisplay to determine
28751 where to display overlay arrows. */);
28752 Voverlay_arrow_variable_list
28753 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28754
28755 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28756 doc: /* The number of lines to try scrolling a window by when point moves out.
28757 If that fails to bring point back on frame, point is centered instead.
28758 If this is zero, point is always centered after it moves off frame.
28759 If you want scrolling to always be a line at a time, you should set
28760 `scroll-conservatively' to a large value rather than set this to 1. */);
28761
28762 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28763 doc: /* Scroll up to this many lines, to bring point back on screen.
28764 If point moves off-screen, redisplay will scroll by up to
28765 `scroll-conservatively' lines in order to bring point just barely
28766 onto the screen again. If that cannot be done, then redisplay
28767 recenters point as usual.
28768
28769 If the value is greater than 100, redisplay will never recenter point,
28770 but will always scroll just enough text to bring point into view, even
28771 if you move far away.
28772
28773 A value of zero means always recenter point if it moves off screen. */);
28774 scroll_conservatively = 0;
28775
28776 DEFVAR_INT ("scroll-margin", scroll_margin,
28777 doc: /* Number of lines of margin at the top and bottom of a window.
28778 Recenter the window whenever point gets within this many lines
28779 of the top or bottom of the window. */);
28780 scroll_margin = 0;
28781
28782 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28783 doc: /* Pixels per inch value for non-window system displays.
28784 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28785 Vdisplay_pixels_per_inch = make_float (72.0);
28786
28787 #ifdef GLYPH_DEBUG
28788 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28789 #endif
28790
28791 DEFVAR_LISP ("truncate-partial-width-windows",
28792 Vtruncate_partial_width_windows,
28793 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28794 For an integer value, truncate lines in each window narrower than the
28795 full frame width, provided the window width is less than that integer;
28796 otherwise, respect the value of `truncate-lines'.
28797
28798 For any other non-nil value, truncate lines in all windows that do
28799 not span the full frame width.
28800
28801 A value of nil means to respect the value of `truncate-lines'.
28802
28803 If `word-wrap' is enabled, you might want to reduce this. */);
28804 Vtruncate_partial_width_windows = make_number (50);
28805
28806 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28807 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28808 Any other value means to use the appropriate face, `mode-line',
28809 `header-line', or `menu' respectively. */);
28810 mode_line_inverse_video = 1;
28811
28812 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28813 doc: /* Maximum buffer size for which line number should be displayed.
28814 If the buffer is bigger than this, the line number does not appear
28815 in the mode line. A value of nil means no limit. */);
28816 Vline_number_display_limit = Qnil;
28817
28818 DEFVAR_INT ("line-number-display-limit-width",
28819 line_number_display_limit_width,
28820 doc: /* Maximum line width (in characters) for line number display.
28821 If the average length of the lines near point is bigger than this, then the
28822 line number may be omitted from the mode line. */);
28823 line_number_display_limit_width = 200;
28824
28825 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28826 doc: /* Non-nil means highlight region even in nonselected windows. */);
28827 highlight_nonselected_windows = 0;
28828
28829 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28830 doc: /* Non-nil if more than one frame is visible on this display.
28831 Minibuffer-only frames don't count, but iconified frames do.
28832 This variable is not guaranteed to be accurate except while processing
28833 `frame-title-format' and `icon-title-format'. */);
28834
28835 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28836 doc: /* Template for displaying the title bar of visible frames.
28837 \(Assuming the window manager supports this feature.)
28838
28839 This variable has the same structure as `mode-line-format', except that
28840 the %c and %l constructs are ignored. It is used only on frames for
28841 which no explicit name has been set \(see `modify-frame-parameters'). */);
28842
28843 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28844 doc: /* Template for displaying the title bar of an iconified frame.
28845 \(Assuming the window manager supports this feature.)
28846 This variable has the same structure as `mode-line-format' (which see),
28847 and is used only on frames for which no explicit name has been set
28848 \(see `modify-frame-parameters'). */);
28849 Vicon_title_format
28850 = Vframe_title_format
28851 = pure_cons (intern_c_string ("multiple-frames"),
28852 pure_cons (make_pure_c_string ("%b"),
28853 pure_cons (pure_cons (empty_unibyte_string,
28854 pure_cons (intern_c_string ("invocation-name"),
28855 pure_cons (make_pure_c_string ("@"),
28856 pure_cons (intern_c_string ("system-name"),
28857 Qnil)))),
28858 Qnil)));
28859
28860 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28861 doc: /* Maximum number of lines to keep in the message log buffer.
28862 If nil, disable message logging. If t, log messages but don't truncate
28863 the buffer when it becomes large. */);
28864 Vmessage_log_max = make_number (100);
28865
28866 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28867 doc: /* Functions called before redisplay, if window sizes have changed.
28868 The value should be a list of functions that take one argument.
28869 Just before redisplay, for each frame, if any of its windows have changed
28870 size since the last redisplay, or have been split or deleted,
28871 all the functions in the list are called, with the frame as argument. */);
28872 Vwindow_size_change_functions = Qnil;
28873
28874 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28875 doc: /* List of functions to call before redisplaying a window with scrolling.
28876 Each function is called with two arguments, the window and its new
28877 display-start position. Note that these functions are also called by
28878 `set-window-buffer'. Also note that the value of `window-end' is not
28879 valid when these functions are called.
28880
28881 Warning: Do not use this feature to alter the way the window
28882 is scrolled. It is not designed for that, and such use probably won't
28883 work. */);
28884 Vwindow_scroll_functions = Qnil;
28885
28886 DEFVAR_LISP ("window-text-change-functions",
28887 Vwindow_text_change_functions,
28888 doc: /* Functions to call in redisplay when text in the window might change. */);
28889 Vwindow_text_change_functions = Qnil;
28890
28891 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28892 doc: /* Functions called when redisplay of a window reaches the end trigger.
28893 Each function is called with two arguments, the window and the end trigger value.
28894 See `set-window-redisplay-end-trigger'. */);
28895 Vredisplay_end_trigger_functions = Qnil;
28896
28897 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28898 doc: /* Non-nil means autoselect window with mouse pointer.
28899 If nil, do not autoselect windows.
28900 A positive number means delay autoselection by that many seconds: a
28901 window is autoselected only after the mouse has remained in that
28902 window for the duration of the delay.
28903 A negative number has a similar effect, but causes windows to be
28904 autoselected only after the mouse has stopped moving. \(Because of
28905 the way Emacs compares mouse events, you will occasionally wait twice
28906 that time before the window gets selected.\)
28907 Any other value means to autoselect window instantaneously when the
28908 mouse pointer enters it.
28909
28910 Autoselection selects the minibuffer only if it is active, and never
28911 unselects the minibuffer if it is active.
28912
28913 When customizing this variable make sure that the actual value of
28914 `focus-follows-mouse' matches the behavior of your window manager. */);
28915 Vmouse_autoselect_window = Qnil;
28916
28917 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28918 doc: /* Non-nil means automatically resize tool-bars.
28919 This dynamically changes the tool-bar's height to the minimum height
28920 that is needed to make all tool-bar items visible.
28921 If value is `grow-only', the tool-bar's height is only increased
28922 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28923 Vauto_resize_tool_bars = Qt;
28924
28925 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28926 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28927 auto_raise_tool_bar_buttons_p = 1;
28928
28929 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28930 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28931 make_cursor_line_fully_visible_p = 1;
28932
28933 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28934 doc: /* Border below tool-bar in pixels.
28935 If an integer, use it as the height of the border.
28936 If it is one of `internal-border-width' or `border-width', use the
28937 value of the corresponding frame parameter.
28938 Otherwise, no border is added below the tool-bar. */);
28939 Vtool_bar_border = Qinternal_border_width;
28940
28941 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28942 doc: /* Margin around tool-bar buttons in pixels.
28943 If an integer, use that for both horizontal and vertical margins.
28944 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28945 HORZ specifying the horizontal margin, and VERT specifying the
28946 vertical margin. */);
28947 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28948
28949 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28950 doc: /* Relief thickness of tool-bar buttons. */);
28951 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28952
28953 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28954 doc: /* Tool bar style to use.
28955 It can be one of
28956 image - show images only
28957 text - show text only
28958 both - show both, text below image
28959 both-horiz - show text to the right of the image
28960 text-image-horiz - show text to the left of the image
28961 any other - use system default or image if no system default.
28962
28963 This variable only affects the GTK+ toolkit version of Emacs. */);
28964 Vtool_bar_style = Qnil;
28965
28966 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28967 doc: /* Maximum number of characters a label can have to be shown.
28968 The tool bar style must also show labels for this to have any effect, see
28969 `tool-bar-style'. */);
28970 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28971
28972 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28973 doc: /* List of functions to call to fontify regions of text.
28974 Each function is called with one argument POS. Functions must
28975 fontify a region starting at POS in the current buffer, and give
28976 fontified regions the property `fontified'. */);
28977 Vfontification_functions = Qnil;
28978 Fmake_variable_buffer_local (Qfontification_functions);
28979
28980 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28981 unibyte_display_via_language_environment,
28982 doc: /* Non-nil means display unibyte text according to language environment.
28983 Specifically, this means that raw bytes in the range 160-255 decimal
28984 are displayed by converting them to the equivalent multibyte characters
28985 according to the current language environment. As a result, they are
28986 displayed according to the current fontset.
28987
28988 Note that this variable affects only how these bytes are displayed,
28989 but does not change the fact they are interpreted as raw bytes. */);
28990 unibyte_display_via_language_environment = 0;
28991
28992 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28993 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
28994 If a float, it specifies a fraction of the mini-window frame's height.
28995 If an integer, it specifies a number of lines. */);
28996 Vmax_mini_window_height = make_float (0.25);
28997
28998 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28999 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29000 A value of nil means don't automatically resize mini-windows.
29001 A value of t means resize them to fit the text displayed in them.
29002 A value of `grow-only', the default, means let mini-windows grow only;
29003 they return to their normal size when the minibuffer is closed, or the
29004 echo area becomes empty. */);
29005 Vresize_mini_windows = Qgrow_only;
29006
29007 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29008 doc: /* Alist specifying how to blink the cursor off.
29009 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29010 `cursor-type' frame-parameter or variable equals ON-STATE,
29011 comparing using `equal', Emacs uses OFF-STATE to specify
29012 how to blink it off. ON-STATE and OFF-STATE are values for
29013 the `cursor-type' frame parameter.
29014
29015 If a frame's ON-STATE has no entry in this list,
29016 the frame's other specifications determine how to blink the cursor off. */);
29017 Vblink_cursor_alist = Qnil;
29018
29019 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29020 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29021 If non-nil, windows are automatically scrolled horizontally to make
29022 point visible. */);
29023 automatic_hscrolling_p = 1;
29024 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29025
29026 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29027 doc: /* How many columns away from the window edge point is allowed to get
29028 before automatic hscrolling will horizontally scroll the window. */);
29029 hscroll_margin = 5;
29030
29031 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29032 doc: /* How many columns to scroll the window when point gets too close to the edge.
29033 When point is less than `hscroll-margin' columns from the window
29034 edge, automatic hscrolling will scroll the window by the amount of columns
29035 determined by this variable. If its value is a positive integer, scroll that
29036 many columns. If it's a positive floating-point number, it specifies the
29037 fraction of the window's width to scroll. If it's nil or zero, point will be
29038 centered horizontally after the scroll. Any other value, including negative
29039 numbers, are treated as if the value were zero.
29040
29041 Automatic hscrolling always moves point outside the scroll margin, so if
29042 point was more than scroll step columns inside the margin, the window will
29043 scroll more than the value given by the scroll step.
29044
29045 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29046 and `scroll-right' overrides this variable's effect. */);
29047 Vhscroll_step = make_number (0);
29048
29049 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29050 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29051 Bind this around calls to `message' to let it take effect. */);
29052 message_truncate_lines = 0;
29053
29054 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29055 doc: /* Normal hook run to update the menu bar definitions.
29056 Redisplay runs this hook before it redisplays the menu bar.
29057 This is used to update submenus such as Buffers,
29058 whose contents depend on various data. */);
29059 Vmenu_bar_update_hook = Qnil;
29060
29061 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29062 doc: /* Frame for which we are updating a menu.
29063 The enable predicate for a menu binding should check this variable. */);
29064 Vmenu_updating_frame = Qnil;
29065
29066 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29067 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29068 inhibit_menubar_update = 0;
29069
29070 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29071 doc: /* Prefix prepended to all continuation lines at display time.
29072 The value may be a string, an image, or a stretch-glyph; it is
29073 interpreted in the same way as the value of a `display' text property.
29074
29075 This variable is overridden by any `wrap-prefix' text or overlay
29076 property.
29077
29078 To add a prefix to non-continuation lines, use `line-prefix'. */);
29079 Vwrap_prefix = Qnil;
29080 DEFSYM (Qwrap_prefix, "wrap-prefix");
29081 Fmake_variable_buffer_local (Qwrap_prefix);
29082
29083 DEFVAR_LISP ("line-prefix", Vline_prefix,
29084 doc: /* Prefix prepended to all non-continuation lines at display time.
29085 The value may be a string, an image, or a stretch-glyph; it is
29086 interpreted in the same way as the value of a `display' text property.
29087
29088 This variable is overridden by any `line-prefix' text or overlay
29089 property.
29090
29091 To add a prefix to continuation lines, use `wrap-prefix'. */);
29092 Vline_prefix = Qnil;
29093 DEFSYM (Qline_prefix, "line-prefix");
29094 Fmake_variable_buffer_local (Qline_prefix);
29095
29096 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29097 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29098 inhibit_eval_during_redisplay = 0;
29099
29100 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29101 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29102 inhibit_free_realized_faces = 0;
29103
29104 #ifdef GLYPH_DEBUG
29105 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29106 doc: /* Inhibit try_window_id display optimization. */);
29107 inhibit_try_window_id = 0;
29108
29109 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29110 doc: /* Inhibit try_window_reusing display optimization. */);
29111 inhibit_try_window_reusing = 0;
29112
29113 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29114 doc: /* Inhibit try_cursor_movement display optimization. */);
29115 inhibit_try_cursor_movement = 0;
29116 #endif /* GLYPH_DEBUG */
29117
29118 DEFVAR_INT ("overline-margin", overline_margin,
29119 doc: /* Space between overline and text, in pixels.
29120 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29121 margin to the character height. */);
29122 overline_margin = 2;
29123
29124 DEFVAR_INT ("underline-minimum-offset",
29125 underline_minimum_offset,
29126 doc: /* Minimum distance between baseline and underline.
29127 This can improve legibility of underlined text at small font sizes,
29128 particularly when using variable `x-use-underline-position-properties'
29129 with fonts that specify an UNDERLINE_POSITION relatively close to the
29130 baseline. The default value is 1. */);
29131 underline_minimum_offset = 1;
29132
29133 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29134 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29135 This feature only works when on a window system that can change
29136 cursor shapes. */);
29137 display_hourglass_p = 1;
29138
29139 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29140 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29141 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29142
29143 hourglass_atimer = NULL;
29144 hourglass_shown_p = 0;
29145
29146 DEFSYM (Qglyphless_char, "glyphless-char");
29147 DEFSYM (Qhex_code, "hex-code");
29148 DEFSYM (Qempty_box, "empty-box");
29149 DEFSYM (Qthin_space, "thin-space");
29150 DEFSYM (Qzero_width, "zero-width");
29151
29152 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29153 /* Intern this now in case it isn't already done.
29154 Setting this variable twice is harmless.
29155 But don't staticpro it here--that is done in alloc.c. */
29156 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29157 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29158
29159 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29160 doc: /* Char-table defining glyphless characters.
29161 Each element, if non-nil, should be one of the following:
29162 an ASCII acronym string: display this string in a box
29163 `hex-code': display the hexadecimal code of a character in a box
29164 `empty-box': display as an empty box
29165 `thin-space': display as 1-pixel width space
29166 `zero-width': don't display
29167 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29168 display method for graphical terminals and text terminals respectively.
29169 GRAPHICAL and TEXT should each have one of the values listed above.
29170
29171 The char-table has one extra slot to control the display of a character for
29172 which no font is found. This slot only takes effect on graphical terminals.
29173 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29174 `thin-space'. The default is `empty-box'. */);
29175 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29176 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29177 Qempty_box);
29178 }
29179
29180
29181 /* Initialize this module when Emacs starts. */
29182
29183 void
29184 init_xdisp (void)
29185 {
29186 current_header_line_height = current_mode_line_height = -1;
29187
29188 CHARPOS (this_line_start_pos) = 0;
29189
29190 if (!noninteractive)
29191 {
29192 struct window *m = XWINDOW (minibuf_window);
29193 Lisp_Object frame = m->frame;
29194 struct frame *f = XFRAME (frame);
29195 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29196 struct window *r = XWINDOW (root);
29197 int i;
29198
29199 echo_area_window = minibuf_window;
29200
29201 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
29202 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29203 XSETFASTINT (r->total_cols, FRAME_COLS (f));
29204 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
29205 XSETFASTINT (m->total_lines, 1);
29206 XSETFASTINT (m->total_cols, FRAME_COLS (f));
29207
29208 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29209 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29210 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29211
29212 /* The default ellipsis glyphs `...'. */
29213 for (i = 0; i < 3; ++i)
29214 default_invis_vector[i] = make_number ('.');
29215 }
29216
29217 {
29218 /* Allocate the buffer for frame titles.
29219 Also used for `format-mode-line'. */
29220 int size = 100;
29221 mode_line_noprop_buf = xmalloc (size);
29222 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29223 mode_line_noprop_ptr = mode_line_noprop_buf;
29224 mode_line_target = MODE_LINE_DISPLAY;
29225 }
29226
29227 help_echo_showing_p = 0;
29228 }
29229
29230 /* Since w32 does not support atimers, it defines its own implementation of
29231 the following three functions in w32fns.c. */
29232 #ifndef WINDOWSNT
29233
29234 /* Platform-independent portion of hourglass implementation. */
29235
29236 /* Cancel a currently active hourglass timer, and start a new one. */
29237 void
29238 start_hourglass (void)
29239 {
29240 #if defined (HAVE_WINDOW_SYSTEM)
29241 EMACS_TIME delay;
29242
29243 cancel_hourglass ();
29244
29245 if (INTEGERP (Vhourglass_delay)
29246 && XINT (Vhourglass_delay) > 0)
29247 EMACS_SET_SECS_NSECS (delay,
29248 min (XINT (Vhourglass_delay), TYPE_MAXIMUM (time_t)),
29249 0);
29250 else if (FLOATP (Vhourglass_delay)
29251 && XFLOAT_DATA (Vhourglass_delay) > 0)
29252 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29253 else
29254 EMACS_SET_SECS_NSECS (delay, DEFAULT_HOURGLASS_DELAY, 0);
29255
29256 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29257 show_hourglass, NULL);
29258 #endif
29259 }
29260
29261
29262 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29263 shown. */
29264 void
29265 cancel_hourglass (void)
29266 {
29267 #if defined (HAVE_WINDOW_SYSTEM)
29268 if (hourglass_atimer)
29269 {
29270 cancel_atimer (hourglass_atimer);
29271 hourglass_atimer = NULL;
29272 }
29273
29274 if (hourglass_shown_p)
29275 hide_hourglass ();
29276 #endif
29277 }
29278 #endif /* ! WINDOWSNT */