EMACS_TIME simplification (Bug#11875).
[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 echo_buffer[i] = Fget_buffer_create
9870 (make_formatted_string (name, " *Echo Area %d*", i));
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 *method = w->desired_matrix->method;
12496 int len = strlen (method);
12497 int size = sizeof w->desired_matrix->method;
12498 int remaining = size - len - 1;
12499 va_list ap;
12500
12501 if (len && remaining)
12502 {
12503 method[len] = '|';
12504 --remaining, ++len;
12505 }
12506
12507 va_start (ap, fmt);
12508 vsnprintf (method + len, remaining + 1, fmt, ap);
12509 va_end (ap);
12510
12511 if (trace_redisplay_p)
12512 fprintf (stderr, "%p (%s): %s\n",
12513 w,
12514 ((BUFFERP (w->buffer)
12515 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12516 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12517 : "no buffer"),
12518 method + len);
12519 }
12520
12521 #endif /* GLYPH_DEBUG */
12522
12523
12524 /* Value is non-zero if all changes in window W, which displays
12525 current_buffer, are in the text between START and END. START is a
12526 buffer position, END is given as a distance from Z. Used in
12527 redisplay_internal for display optimization. */
12528
12529 static inline int
12530 text_outside_line_unchanged_p (struct window *w,
12531 ptrdiff_t start, ptrdiff_t end)
12532 {
12533 int unchanged_p = 1;
12534
12535 /* If text or overlays have changed, see where. */
12536 if (w->last_modified < MODIFF
12537 || w->last_overlay_modified < OVERLAY_MODIFF)
12538 {
12539 /* Gap in the line? */
12540 if (GPT < start || Z - GPT < end)
12541 unchanged_p = 0;
12542
12543 /* Changes start in front of the line, or end after it? */
12544 if (unchanged_p
12545 && (BEG_UNCHANGED < start - 1
12546 || END_UNCHANGED < end))
12547 unchanged_p = 0;
12548
12549 /* If selective display, can't optimize if changes start at the
12550 beginning of the line. */
12551 if (unchanged_p
12552 && INTEGERP (BVAR (current_buffer, selective_display))
12553 && XINT (BVAR (current_buffer, selective_display)) > 0
12554 && (BEG_UNCHANGED < start || GPT <= start))
12555 unchanged_p = 0;
12556
12557 /* If there are overlays at the start or end of the line, these
12558 may have overlay strings with newlines in them. A change at
12559 START, for instance, may actually concern the display of such
12560 overlay strings as well, and they are displayed on different
12561 lines. So, quickly rule out this case. (For the future, it
12562 might be desirable to implement something more telling than
12563 just BEG/END_UNCHANGED.) */
12564 if (unchanged_p)
12565 {
12566 if (BEG + BEG_UNCHANGED == start
12567 && overlay_touches_p (start))
12568 unchanged_p = 0;
12569 if (END_UNCHANGED == end
12570 && overlay_touches_p (Z - end))
12571 unchanged_p = 0;
12572 }
12573
12574 /* Under bidi reordering, adding or deleting a character in the
12575 beginning of a paragraph, before the first strong directional
12576 character, can change the base direction of the paragraph (unless
12577 the buffer specifies a fixed paragraph direction), which will
12578 require to redisplay the whole paragraph. It might be worthwhile
12579 to find the paragraph limits and widen the range of redisplayed
12580 lines to that, but for now just give up this optimization. */
12581 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12582 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12583 unchanged_p = 0;
12584 }
12585
12586 return unchanged_p;
12587 }
12588
12589
12590 /* Do a frame update, taking possible shortcuts into account. This is
12591 the main external entry point for redisplay.
12592
12593 If the last redisplay displayed an echo area message and that message
12594 is no longer requested, we clear the echo area or bring back the
12595 mini-buffer if that is in use. */
12596
12597 void
12598 redisplay (void)
12599 {
12600 redisplay_internal ();
12601 }
12602
12603
12604 static Lisp_Object
12605 overlay_arrow_string_or_property (Lisp_Object var)
12606 {
12607 Lisp_Object val;
12608
12609 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12610 return val;
12611
12612 return Voverlay_arrow_string;
12613 }
12614
12615 /* Return 1 if there are any overlay-arrows in current_buffer. */
12616 static int
12617 overlay_arrow_in_current_buffer_p (void)
12618 {
12619 Lisp_Object vlist;
12620
12621 for (vlist = Voverlay_arrow_variable_list;
12622 CONSP (vlist);
12623 vlist = XCDR (vlist))
12624 {
12625 Lisp_Object var = XCAR (vlist);
12626 Lisp_Object val;
12627
12628 if (!SYMBOLP (var))
12629 continue;
12630 val = find_symbol_value (var);
12631 if (MARKERP (val)
12632 && current_buffer == XMARKER (val)->buffer)
12633 return 1;
12634 }
12635 return 0;
12636 }
12637
12638
12639 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12640 has changed. */
12641
12642 static int
12643 overlay_arrows_changed_p (void)
12644 {
12645 Lisp_Object vlist;
12646
12647 for (vlist = Voverlay_arrow_variable_list;
12648 CONSP (vlist);
12649 vlist = XCDR (vlist))
12650 {
12651 Lisp_Object var = XCAR (vlist);
12652 Lisp_Object val, pstr;
12653
12654 if (!SYMBOLP (var))
12655 continue;
12656 val = find_symbol_value (var);
12657 if (!MARKERP (val))
12658 continue;
12659 if (! EQ (COERCE_MARKER (val),
12660 Fget (var, Qlast_arrow_position))
12661 || ! (pstr = overlay_arrow_string_or_property (var),
12662 EQ (pstr, Fget (var, Qlast_arrow_string))))
12663 return 1;
12664 }
12665 return 0;
12666 }
12667
12668 /* Mark overlay arrows to be updated on next redisplay. */
12669
12670 static void
12671 update_overlay_arrows (int up_to_date)
12672 {
12673 Lisp_Object vlist;
12674
12675 for (vlist = Voverlay_arrow_variable_list;
12676 CONSP (vlist);
12677 vlist = XCDR (vlist))
12678 {
12679 Lisp_Object var = XCAR (vlist);
12680
12681 if (!SYMBOLP (var))
12682 continue;
12683
12684 if (up_to_date > 0)
12685 {
12686 Lisp_Object val = find_symbol_value (var);
12687 Fput (var, Qlast_arrow_position,
12688 COERCE_MARKER (val));
12689 Fput (var, Qlast_arrow_string,
12690 overlay_arrow_string_or_property (var));
12691 }
12692 else if (up_to_date < 0
12693 || !NILP (Fget (var, Qlast_arrow_position)))
12694 {
12695 Fput (var, Qlast_arrow_position, Qt);
12696 Fput (var, Qlast_arrow_string, Qt);
12697 }
12698 }
12699 }
12700
12701
12702 /* Return overlay arrow string to display at row.
12703 Return integer (bitmap number) for arrow bitmap in left fringe.
12704 Return nil if no overlay arrow. */
12705
12706 static Lisp_Object
12707 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12708 {
12709 Lisp_Object vlist;
12710
12711 for (vlist = Voverlay_arrow_variable_list;
12712 CONSP (vlist);
12713 vlist = XCDR (vlist))
12714 {
12715 Lisp_Object var = XCAR (vlist);
12716 Lisp_Object val;
12717
12718 if (!SYMBOLP (var))
12719 continue;
12720
12721 val = find_symbol_value (var);
12722
12723 if (MARKERP (val)
12724 && current_buffer == XMARKER (val)->buffer
12725 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12726 {
12727 if (FRAME_WINDOW_P (it->f)
12728 /* FIXME: if ROW->reversed_p is set, this should test
12729 the right fringe, not the left one. */
12730 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12731 {
12732 #ifdef HAVE_WINDOW_SYSTEM
12733 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12734 {
12735 int fringe_bitmap;
12736 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12737 return make_number (fringe_bitmap);
12738 }
12739 #endif
12740 return make_number (-1); /* Use default arrow bitmap */
12741 }
12742 return overlay_arrow_string_or_property (var);
12743 }
12744 }
12745
12746 return Qnil;
12747 }
12748
12749 /* Return 1 if point moved out of or into a composition. Otherwise
12750 return 0. PREV_BUF and PREV_PT are the last point buffer and
12751 position. BUF and PT are the current point buffer and position. */
12752
12753 static int
12754 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12755 struct buffer *buf, ptrdiff_t pt)
12756 {
12757 ptrdiff_t start, end;
12758 Lisp_Object prop;
12759 Lisp_Object buffer;
12760
12761 XSETBUFFER (buffer, buf);
12762 /* Check a composition at the last point if point moved within the
12763 same buffer. */
12764 if (prev_buf == buf)
12765 {
12766 if (prev_pt == pt)
12767 /* Point didn't move. */
12768 return 0;
12769
12770 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12771 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12772 && COMPOSITION_VALID_P (start, end, prop)
12773 && start < prev_pt && end > prev_pt)
12774 /* The last point was within the composition. Return 1 iff
12775 point moved out of the composition. */
12776 return (pt <= start || pt >= end);
12777 }
12778
12779 /* Check a composition at the current point. */
12780 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12781 && find_composition (pt, -1, &start, &end, &prop, buffer)
12782 && COMPOSITION_VALID_P (start, end, prop)
12783 && start < pt && end > pt);
12784 }
12785
12786
12787 /* Reconsider the setting of B->clip_changed which is displayed
12788 in window W. */
12789
12790 static inline void
12791 reconsider_clip_changes (struct window *w, struct buffer *b)
12792 {
12793 if (b->clip_changed
12794 && !NILP (w->window_end_valid)
12795 && w->current_matrix->buffer == b
12796 && w->current_matrix->zv == BUF_ZV (b)
12797 && w->current_matrix->begv == BUF_BEGV (b))
12798 b->clip_changed = 0;
12799
12800 /* If display wasn't paused, and W is not a tool bar window, see if
12801 point has been moved into or out of a composition. In that case,
12802 we set b->clip_changed to 1 to force updating the screen. If
12803 b->clip_changed has already been set to 1, we can skip this
12804 check. */
12805 if (!b->clip_changed
12806 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12807 {
12808 ptrdiff_t pt;
12809
12810 if (w == XWINDOW (selected_window))
12811 pt = PT;
12812 else
12813 pt = marker_position (w->pointm);
12814
12815 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12816 || pt != w->last_point)
12817 && check_point_in_composition (w->current_matrix->buffer,
12818 w->last_point,
12819 XBUFFER (w->buffer), pt))
12820 b->clip_changed = 1;
12821 }
12822 }
12823 \f
12824
12825 /* Select FRAME to forward the values of frame-local variables into C
12826 variables so that the redisplay routines can access those values
12827 directly. */
12828
12829 static void
12830 select_frame_for_redisplay (Lisp_Object frame)
12831 {
12832 Lisp_Object tail, tem;
12833 Lisp_Object old = selected_frame;
12834 struct Lisp_Symbol *sym;
12835
12836 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12837
12838 selected_frame = frame;
12839
12840 do {
12841 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12842 if (CONSP (XCAR (tail))
12843 && (tem = XCAR (XCAR (tail)),
12844 SYMBOLP (tem))
12845 && (sym = indirect_variable (XSYMBOL (tem)),
12846 sym->redirect == SYMBOL_LOCALIZED)
12847 && sym->val.blv->frame_local)
12848 /* Use find_symbol_value rather than Fsymbol_value
12849 to avoid an error if it is void. */
12850 find_symbol_value (tem);
12851 } while (!EQ (frame, old) && (frame = old, 1));
12852 }
12853
12854
12855 #define STOP_POLLING \
12856 do { if (! polling_stopped_here) stop_polling (); \
12857 polling_stopped_here = 1; } while (0)
12858
12859 #define RESUME_POLLING \
12860 do { if (polling_stopped_here) start_polling (); \
12861 polling_stopped_here = 0; } while (0)
12862
12863
12864 /* Perhaps in the future avoid recentering windows if it
12865 is not necessary; currently that causes some problems. */
12866
12867 static void
12868 redisplay_internal (void)
12869 {
12870 struct window *w = XWINDOW (selected_window);
12871 struct window *sw;
12872 struct frame *fr;
12873 int pending;
12874 int must_finish = 0;
12875 struct text_pos tlbufpos, tlendpos;
12876 int number_of_visible_frames;
12877 ptrdiff_t count, count1;
12878 struct frame *sf;
12879 int polling_stopped_here = 0;
12880 Lisp_Object old_frame = selected_frame;
12881
12882 /* Non-zero means redisplay has to consider all windows on all
12883 frames. Zero means, only selected_window is considered. */
12884 int consider_all_windows_p;
12885
12886 /* Non-zero means redisplay has to redisplay the miniwindow */
12887 int update_miniwindow_p = 0;
12888
12889 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12890
12891 /* No redisplay if running in batch mode or frame is not yet fully
12892 initialized, or redisplay is explicitly turned off by setting
12893 Vinhibit_redisplay. */
12894 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12895 || !NILP (Vinhibit_redisplay))
12896 return;
12897
12898 /* Don't examine these until after testing Vinhibit_redisplay.
12899 When Emacs is shutting down, perhaps because its connection to
12900 X has dropped, we should not look at them at all. */
12901 fr = XFRAME (w->frame);
12902 sf = SELECTED_FRAME ();
12903
12904 if (!fr->glyphs_initialized_p)
12905 return;
12906
12907 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12908 if (popup_activated ())
12909 return;
12910 #endif
12911
12912 /* I don't think this happens but let's be paranoid. */
12913 if (redisplaying_p)
12914 return;
12915
12916 /* Record a function that resets redisplaying_p to its old value
12917 when we leave this function. */
12918 count = SPECPDL_INDEX ();
12919 record_unwind_protect (unwind_redisplay,
12920 Fcons (make_number (redisplaying_p), selected_frame));
12921 ++redisplaying_p;
12922 specbind (Qinhibit_free_realized_faces, Qnil);
12923
12924 {
12925 Lisp_Object tail, frame;
12926
12927 FOR_EACH_FRAME (tail, frame)
12928 {
12929 struct frame *f = XFRAME (frame);
12930 f->already_hscrolled_p = 0;
12931 }
12932 }
12933
12934 retry:
12935 /* Remember the currently selected window. */
12936 sw = w;
12937
12938 if (!EQ (old_frame, selected_frame)
12939 && FRAME_LIVE_P (XFRAME (old_frame)))
12940 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12941 selected_frame and selected_window to be temporarily out-of-sync so
12942 when we come back here via `goto retry', we need to resync because we
12943 may need to run Elisp code (via prepare_menu_bars). */
12944 select_frame_for_redisplay (old_frame);
12945
12946 pending = 0;
12947 reconsider_clip_changes (w, current_buffer);
12948 last_escape_glyph_frame = NULL;
12949 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12950 last_glyphless_glyph_frame = NULL;
12951 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12952
12953 /* If new fonts have been loaded that make a glyph matrix adjustment
12954 necessary, do it. */
12955 if (fonts_changed_p)
12956 {
12957 adjust_glyphs (NULL);
12958 ++windows_or_buffers_changed;
12959 fonts_changed_p = 0;
12960 }
12961
12962 /* If face_change_count is non-zero, init_iterator will free all
12963 realized faces, which includes the faces referenced from current
12964 matrices. So, we can't reuse current matrices in this case. */
12965 if (face_change_count)
12966 ++windows_or_buffers_changed;
12967
12968 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12969 && FRAME_TTY (sf)->previous_frame != sf)
12970 {
12971 /* Since frames on a single ASCII terminal share the same
12972 display area, displaying a different frame means redisplay
12973 the whole thing. */
12974 windows_or_buffers_changed++;
12975 SET_FRAME_GARBAGED (sf);
12976 #ifndef DOS_NT
12977 set_tty_color_mode (FRAME_TTY (sf), sf);
12978 #endif
12979 FRAME_TTY (sf)->previous_frame = sf;
12980 }
12981
12982 /* Set the visible flags for all frames. Do this before checking
12983 for resized or garbaged frames; they want to know if their frames
12984 are visible. See the comment in frame.h for
12985 FRAME_SAMPLE_VISIBILITY. */
12986 {
12987 Lisp_Object tail, frame;
12988
12989 number_of_visible_frames = 0;
12990
12991 FOR_EACH_FRAME (tail, frame)
12992 {
12993 struct frame *f = XFRAME (frame);
12994
12995 FRAME_SAMPLE_VISIBILITY (f);
12996 if (FRAME_VISIBLE_P (f))
12997 ++number_of_visible_frames;
12998 clear_desired_matrices (f);
12999 }
13000 }
13001
13002 /* Notice any pending interrupt request to change frame size. */
13003 do_pending_window_change (1);
13004
13005 /* do_pending_window_change could change the selected_window due to
13006 frame resizing which makes the selected window too small. */
13007 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13008 {
13009 sw = w;
13010 reconsider_clip_changes (w, current_buffer);
13011 }
13012
13013 /* Clear frames marked as garbaged. */
13014 if (frame_garbaged)
13015 clear_garbaged_frames ();
13016
13017 /* Build menubar and tool-bar items. */
13018 if (NILP (Vmemory_full))
13019 prepare_menu_bars ();
13020
13021 if (windows_or_buffers_changed)
13022 update_mode_lines++;
13023
13024 /* Detect case that we need to write or remove a star in the mode line. */
13025 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13026 {
13027 w->update_mode_line = 1;
13028 if (buffer_shared > 1)
13029 update_mode_lines++;
13030 }
13031
13032 /* Avoid invocation of point motion hooks by `current_column' below. */
13033 count1 = SPECPDL_INDEX ();
13034 specbind (Qinhibit_point_motion_hooks, Qt);
13035
13036 /* If %c is in the mode line, update it if needed. */
13037 if (!NILP (w->column_number_displayed)
13038 /* This alternative quickly identifies a common case
13039 where no change is needed. */
13040 && !(PT == w->last_point
13041 && w->last_modified >= MODIFF
13042 && w->last_overlay_modified >= OVERLAY_MODIFF)
13043 && (XFASTINT (w->column_number_displayed) != current_column ()))
13044 w->update_mode_line = 1;
13045
13046 unbind_to (count1, Qnil);
13047
13048 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13049
13050 /* The variable buffer_shared is set in redisplay_window and
13051 indicates that we redisplay a buffer in different windows. See
13052 there. */
13053 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13054 || cursor_type_changed);
13055
13056 /* If specs for an arrow have changed, do thorough redisplay
13057 to ensure we remove any arrow that should no longer exist. */
13058 if (overlay_arrows_changed_p ())
13059 consider_all_windows_p = windows_or_buffers_changed = 1;
13060
13061 /* Normally the message* functions will have already displayed and
13062 updated the echo area, but the frame may have been trashed, or
13063 the update may have been preempted, so display the echo area
13064 again here. Checking message_cleared_p captures the case that
13065 the echo area should be cleared. */
13066 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13067 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13068 || (message_cleared_p
13069 && minibuf_level == 0
13070 /* If the mini-window is currently selected, this means the
13071 echo-area doesn't show through. */
13072 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13073 {
13074 int window_height_changed_p = echo_area_display (0);
13075
13076 if (message_cleared_p)
13077 update_miniwindow_p = 1;
13078
13079 must_finish = 1;
13080
13081 /* If we don't display the current message, don't clear the
13082 message_cleared_p flag, because, if we did, we wouldn't clear
13083 the echo area in the next redisplay which doesn't preserve
13084 the echo area. */
13085 if (!display_last_displayed_message_p)
13086 message_cleared_p = 0;
13087
13088 if (fonts_changed_p)
13089 goto retry;
13090 else if (window_height_changed_p)
13091 {
13092 consider_all_windows_p = 1;
13093 ++update_mode_lines;
13094 ++windows_or_buffers_changed;
13095
13096 /* If window configuration was changed, frames may have been
13097 marked garbaged. Clear them or we will experience
13098 surprises wrt scrolling. */
13099 if (frame_garbaged)
13100 clear_garbaged_frames ();
13101 }
13102 }
13103 else if (EQ (selected_window, minibuf_window)
13104 && (current_buffer->clip_changed
13105 || w->last_modified < MODIFF
13106 || w->last_overlay_modified < OVERLAY_MODIFF)
13107 && resize_mini_window (w, 0))
13108 {
13109 /* Resized active mini-window to fit the size of what it is
13110 showing if its contents might have changed. */
13111 must_finish = 1;
13112 /* FIXME: this causes all frames to be updated, which seems unnecessary
13113 since only the current frame needs to be considered. This function needs
13114 to be rewritten with two variables, consider_all_windows and
13115 consider_all_frames. */
13116 consider_all_windows_p = 1;
13117 ++windows_or_buffers_changed;
13118 ++update_mode_lines;
13119
13120 /* If window configuration was changed, frames may have been
13121 marked garbaged. Clear them or we will experience
13122 surprises wrt scrolling. */
13123 if (frame_garbaged)
13124 clear_garbaged_frames ();
13125 }
13126
13127
13128 /* If showing the region, and mark has changed, we must redisplay
13129 the whole window. The assignment to this_line_start_pos prevents
13130 the optimization directly below this if-statement. */
13131 if (((!NILP (Vtransient_mark_mode)
13132 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13133 != !NILP (w->region_showing))
13134 || (!NILP (w->region_showing)
13135 && !EQ (w->region_showing,
13136 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13137 CHARPOS (this_line_start_pos) = 0;
13138
13139 /* Optimize the case that only the line containing the cursor in the
13140 selected window has changed. Variables starting with this_ are
13141 set in display_line and record information about the line
13142 containing the cursor. */
13143 tlbufpos = this_line_start_pos;
13144 tlendpos = this_line_end_pos;
13145 if (!consider_all_windows_p
13146 && CHARPOS (tlbufpos) > 0
13147 && !w->update_mode_line
13148 && !current_buffer->clip_changed
13149 && !current_buffer->prevent_redisplay_optimizations_p
13150 && FRAME_VISIBLE_P (XFRAME (w->frame))
13151 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13152 /* Make sure recorded data applies to current buffer, etc. */
13153 && this_line_buffer == current_buffer
13154 && current_buffer == XBUFFER (w->buffer)
13155 && !w->force_start
13156 && !w->optional_new_start
13157 /* Point must be on the line that we have info recorded about. */
13158 && PT >= CHARPOS (tlbufpos)
13159 && PT <= Z - CHARPOS (tlendpos)
13160 /* All text outside that line, including its final newline,
13161 must be unchanged. */
13162 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13163 CHARPOS (tlendpos)))
13164 {
13165 if (CHARPOS (tlbufpos) > BEGV
13166 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13167 && (CHARPOS (tlbufpos) == ZV
13168 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13169 /* Former continuation line has disappeared by becoming empty. */
13170 goto cancel;
13171 else if (w->last_modified < MODIFF
13172 || w->last_overlay_modified < OVERLAY_MODIFF
13173 || MINI_WINDOW_P (w))
13174 {
13175 /* We have to handle the case of continuation around a
13176 wide-column character (see the comment in indent.c around
13177 line 1340).
13178
13179 For instance, in the following case:
13180
13181 -------- Insert --------
13182 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13183 J_I_ ==> J_I_ `^^' are cursors.
13184 ^^ ^^
13185 -------- --------
13186
13187 As we have to redraw the line above, we cannot use this
13188 optimization. */
13189
13190 struct it it;
13191 int line_height_before = this_line_pixel_height;
13192
13193 /* Note that start_display will handle the case that the
13194 line starting at tlbufpos is a continuation line. */
13195 start_display (&it, w, tlbufpos);
13196
13197 /* Implementation note: It this still necessary? */
13198 if (it.current_x != this_line_start_x)
13199 goto cancel;
13200
13201 TRACE ((stderr, "trying display optimization 1\n"));
13202 w->cursor.vpos = -1;
13203 overlay_arrow_seen = 0;
13204 it.vpos = this_line_vpos;
13205 it.current_y = this_line_y;
13206 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13207 display_line (&it);
13208
13209 /* If line contains point, is not continued,
13210 and ends at same distance from eob as before, we win. */
13211 if (w->cursor.vpos >= 0
13212 /* Line is not continued, otherwise this_line_start_pos
13213 would have been set to 0 in display_line. */
13214 && CHARPOS (this_line_start_pos)
13215 /* Line ends as before. */
13216 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13217 /* Line has same height as before. Otherwise other lines
13218 would have to be shifted up or down. */
13219 && this_line_pixel_height == line_height_before)
13220 {
13221 /* If this is not the window's last line, we must adjust
13222 the charstarts of the lines below. */
13223 if (it.current_y < it.last_visible_y)
13224 {
13225 struct glyph_row *row
13226 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13227 ptrdiff_t delta, delta_bytes;
13228
13229 /* We used to distinguish between two cases here,
13230 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13231 when the line ends in a newline or the end of the
13232 buffer's accessible portion. But both cases did
13233 the same, so they were collapsed. */
13234 delta = (Z
13235 - CHARPOS (tlendpos)
13236 - MATRIX_ROW_START_CHARPOS (row));
13237 delta_bytes = (Z_BYTE
13238 - BYTEPOS (tlendpos)
13239 - MATRIX_ROW_START_BYTEPOS (row));
13240
13241 increment_matrix_positions (w->current_matrix,
13242 this_line_vpos + 1,
13243 w->current_matrix->nrows,
13244 delta, delta_bytes);
13245 }
13246
13247 /* If this row displays text now but previously didn't,
13248 or vice versa, w->window_end_vpos may have to be
13249 adjusted. */
13250 if ((it.glyph_row - 1)->displays_text_p)
13251 {
13252 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13253 XSETINT (w->window_end_vpos, this_line_vpos);
13254 }
13255 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13256 && this_line_vpos > 0)
13257 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13258 w->window_end_valid = Qnil;
13259
13260 /* Update hint: No need to try to scroll in update_window. */
13261 w->desired_matrix->no_scrolling_p = 1;
13262
13263 #ifdef GLYPH_DEBUG
13264 *w->desired_matrix->method = 0;
13265 debug_method_add (w, "optimization 1");
13266 #endif
13267 #ifdef HAVE_WINDOW_SYSTEM
13268 update_window_fringes (w, 0);
13269 #endif
13270 goto update;
13271 }
13272 else
13273 goto cancel;
13274 }
13275 else if (/* Cursor position hasn't changed. */
13276 PT == w->last_point
13277 /* Make sure the cursor was last displayed
13278 in this window. Otherwise we have to reposition it. */
13279 && 0 <= w->cursor.vpos
13280 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13281 {
13282 if (!must_finish)
13283 {
13284 do_pending_window_change (1);
13285 /* If selected_window changed, redisplay again. */
13286 if (WINDOWP (selected_window)
13287 && (w = XWINDOW (selected_window)) != sw)
13288 goto retry;
13289
13290 /* We used to always goto end_of_redisplay here, but this
13291 isn't enough if we have a blinking cursor. */
13292 if (w->cursor_off_p == w->last_cursor_off_p)
13293 goto end_of_redisplay;
13294 }
13295 goto update;
13296 }
13297 /* If highlighting the region, or if the cursor is in the echo area,
13298 then we can't just move the cursor. */
13299 else if (! (!NILP (Vtransient_mark_mode)
13300 && !NILP (BVAR (current_buffer, mark_active)))
13301 && (EQ (selected_window,
13302 BVAR (current_buffer, last_selected_window))
13303 || highlight_nonselected_windows)
13304 && NILP (w->region_showing)
13305 && NILP (Vshow_trailing_whitespace)
13306 && !cursor_in_echo_area)
13307 {
13308 struct it it;
13309 struct glyph_row *row;
13310
13311 /* Skip from tlbufpos to PT and see where it is. Note that
13312 PT may be in invisible text. If so, we will end at the
13313 next visible position. */
13314 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13315 NULL, DEFAULT_FACE_ID);
13316 it.current_x = this_line_start_x;
13317 it.current_y = this_line_y;
13318 it.vpos = this_line_vpos;
13319
13320 /* The call to move_it_to stops in front of PT, but
13321 moves over before-strings. */
13322 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13323
13324 if (it.vpos == this_line_vpos
13325 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13326 row->enabled_p))
13327 {
13328 eassert (this_line_vpos == it.vpos);
13329 eassert (this_line_y == it.current_y);
13330 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13331 #ifdef GLYPH_DEBUG
13332 *w->desired_matrix->method = 0;
13333 debug_method_add (w, "optimization 3");
13334 #endif
13335 goto update;
13336 }
13337 else
13338 goto cancel;
13339 }
13340
13341 cancel:
13342 /* Text changed drastically or point moved off of line. */
13343 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13344 }
13345
13346 CHARPOS (this_line_start_pos) = 0;
13347 consider_all_windows_p |= buffer_shared > 1;
13348 ++clear_face_cache_count;
13349 #ifdef HAVE_WINDOW_SYSTEM
13350 ++clear_image_cache_count;
13351 #endif
13352
13353 /* Build desired matrices, and update the display. If
13354 consider_all_windows_p is non-zero, do it for all windows on all
13355 frames. Otherwise do it for selected_window, only. */
13356
13357 if (consider_all_windows_p)
13358 {
13359 Lisp_Object tail, frame;
13360
13361 FOR_EACH_FRAME (tail, frame)
13362 XFRAME (frame)->updated_p = 0;
13363
13364 /* Recompute # windows showing selected buffer. This will be
13365 incremented each time such a window is displayed. */
13366 buffer_shared = 0;
13367
13368 FOR_EACH_FRAME (tail, frame)
13369 {
13370 struct frame *f = XFRAME (frame);
13371
13372 /* We don't have to do anything for unselected terminal
13373 frames. */
13374 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13375 && !EQ (FRAME_TTY (f)->top_frame, frame))
13376 continue;
13377
13378 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13379 {
13380 if (! EQ (frame, selected_frame))
13381 /* Select the frame, for the sake of frame-local
13382 variables. */
13383 select_frame_for_redisplay (frame);
13384
13385 /* Mark all the scroll bars to be removed; we'll redeem
13386 the ones we want when we redisplay their windows. */
13387 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13388 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13389
13390 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13391 redisplay_windows (FRAME_ROOT_WINDOW (f));
13392
13393 /* The X error handler may have deleted that frame. */
13394 if (!FRAME_LIVE_P (f))
13395 continue;
13396
13397 /* Any scroll bars which redisplay_windows should have
13398 nuked should now go away. */
13399 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13400 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13401
13402 /* If fonts changed, display again. */
13403 /* ??? rms: I suspect it is a mistake to jump all the way
13404 back to retry here. It should just retry this frame. */
13405 if (fonts_changed_p)
13406 goto retry;
13407
13408 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13409 {
13410 /* See if we have to hscroll. */
13411 if (!f->already_hscrolled_p)
13412 {
13413 f->already_hscrolled_p = 1;
13414 if (hscroll_windows (f->root_window))
13415 goto retry;
13416 }
13417
13418 /* Prevent various kinds of signals during display
13419 update. stdio is not robust about handling
13420 signals, which can cause an apparent I/O
13421 error. */
13422 if (interrupt_input)
13423 unrequest_sigio ();
13424 STOP_POLLING;
13425
13426 /* Update the display. */
13427 set_window_update_flags (XWINDOW (f->root_window), 1);
13428 pending |= update_frame (f, 0, 0);
13429 f->updated_p = 1;
13430 }
13431 }
13432 }
13433
13434 if (!EQ (old_frame, selected_frame)
13435 && FRAME_LIVE_P (XFRAME (old_frame)))
13436 /* We played a bit fast-and-loose above and allowed selected_frame
13437 and selected_window to be temporarily out-of-sync but let's make
13438 sure this stays contained. */
13439 select_frame_for_redisplay (old_frame);
13440 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13441
13442 if (!pending)
13443 {
13444 /* Do the mark_window_display_accurate after all windows have
13445 been redisplayed because this call resets flags in buffers
13446 which are needed for proper redisplay. */
13447 FOR_EACH_FRAME (tail, frame)
13448 {
13449 struct frame *f = XFRAME (frame);
13450 if (f->updated_p)
13451 {
13452 mark_window_display_accurate (f->root_window, 1);
13453 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13454 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13455 }
13456 }
13457 }
13458 }
13459 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13460 {
13461 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13462 struct frame *mini_frame;
13463
13464 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13465 /* Use list_of_error, not Qerror, so that
13466 we catch only errors and don't run the debugger. */
13467 internal_condition_case_1 (redisplay_window_1, selected_window,
13468 list_of_error,
13469 redisplay_window_error);
13470 if (update_miniwindow_p)
13471 internal_condition_case_1 (redisplay_window_1, mini_window,
13472 list_of_error,
13473 redisplay_window_error);
13474
13475 /* Compare desired and current matrices, perform output. */
13476
13477 update:
13478 /* If fonts changed, display again. */
13479 if (fonts_changed_p)
13480 goto retry;
13481
13482 /* Prevent various kinds of signals during display update.
13483 stdio is not robust about handling signals,
13484 which can cause an apparent I/O error. */
13485 if (interrupt_input)
13486 unrequest_sigio ();
13487 STOP_POLLING;
13488
13489 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13490 {
13491 if (hscroll_windows (selected_window))
13492 goto retry;
13493
13494 XWINDOW (selected_window)->must_be_updated_p = 1;
13495 pending = update_frame (sf, 0, 0);
13496 }
13497
13498 /* We may have called echo_area_display at the top of this
13499 function. If the echo area is on another frame, that may
13500 have put text on a frame other than the selected one, so the
13501 above call to update_frame would not have caught it. Catch
13502 it here. */
13503 mini_window = FRAME_MINIBUF_WINDOW (sf);
13504 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13505
13506 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13507 {
13508 XWINDOW (mini_window)->must_be_updated_p = 1;
13509 pending |= update_frame (mini_frame, 0, 0);
13510 if (!pending && hscroll_windows (mini_window))
13511 goto retry;
13512 }
13513 }
13514
13515 /* If display was paused because of pending input, make sure we do a
13516 thorough update the next time. */
13517 if (pending)
13518 {
13519 /* Prevent the optimization at the beginning of
13520 redisplay_internal that tries a single-line update of the
13521 line containing the cursor in the selected window. */
13522 CHARPOS (this_line_start_pos) = 0;
13523
13524 /* Let the overlay arrow be updated the next time. */
13525 update_overlay_arrows (0);
13526
13527 /* If we pause after scrolling, some rows in the current
13528 matrices of some windows are not valid. */
13529 if (!WINDOW_FULL_WIDTH_P (w)
13530 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13531 update_mode_lines = 1;
13532 }
13533 else
13534 {
13535 if (!consider_all_windows_p)
13536 {
13537 /* This has already been done above if
13538 consider_all_windows_p is set. */
13539 mark_window_display_accurate_1 (w, 1);
13540
13541 /* Say overlay arrows are up to date. */
13542 update_overlay_arrows (1);
13543
13544 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13545 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13546 }
13547
13548 update_mode_lines = 0;
13549 windows_or_buffers_changed = 0;
13550 cursor_type_changed = 0;
13551 }
13552
13553 /* Start SIGIO interrupts coming again. Having them off during the
13554 code above makes it less likely one will discard output, but not
13555 impossible, since there might be stuff in the system buffer here.
13556 But it is much hairier to try to do anything about that. */
13557 if (interrupt_input)
13558 request_sigio ();
13559 RESUME_POLLING;
13560
13561 /* If a frame has become visible which was not before, redisplay
13562 again, so that we display it. Expose events for such a frame
13563 (which it gets when becoming visible) don't call the parts of
13564 redisplay constructing glyphs, so simply exposing a frame won't
13565 display anything in this case. So, we have to display these
13566 frames here explicitly. */
13567 if (!pending)
13568 {
13569 Lisp_Object tail, frame;
13570 int new_count = 0;
13571
13572 FOR_EACH_FRAME (tail, frame)
13573 {
13574 int this_is_visible = 0;
13575
13576 if (XFRAME (frame)->visible)
13577 this_is_visible = 1;
13578 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13579 if (XFRAME (frame)->visible)
13580 this_is_visible = 1;
13581
13582 if (this_is_visible)
13583 new_count++;
13584 }
13585
13586 if (new_count != number_of_visible_frames)
13587 windows_or_buffers_changed++;
13588 }
13589
13590 /* Change frame size now if a change is pending. */
13591 do_pending_window_change (1);
13592
13593 /* If we just did a pending size change, or have additional
13594 visible frames, or selected_window changed, redisplay again. */
13595 if ((windows_or_buffers_changed && !pending)
13596 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13597 goto retry;
13598
13599 /* Clear the face and image caches.
13600
13601 We used to do this only if consider_all_windows_p. But the cache
13602 needs to be cleared if a timer creates images in the current
13603 buffer (e.g. the test case in Bug#6230). */
13604
13605 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13606 {
13607 clear_face_cache (0);
13608 clear_face_cache_count = 0;
13609 }
13610
13611 #ifdef HAVE_WINDOW_SYSTEM
13612 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13613 {
13614 clear_image_caches (Qnil);
13615 clear_image_cache_count = 0;
13616 }
13617 #endif /* HAVE_WINDOW_SYSTEM */
13618
13619 end_of_redisplay:
13620 unbind_to (count, Qnil);
13621 RESUME_POLLING;
13622 }
13623
13624
13625 /* Redisplay, but leave alone any recent echo area message unless
13626 another message has been requested in its place.
13627
13628 This is useful in situations where you need to redisplay but no
13629 user action has occurred, making it inappropriate for the message
13630 area to be cleared. See tracking_off and
13631 wait_reading_process_output for examples of these situations.
13632
13633 FROM_WHERE is an integer saying from where this function was
13634 called. This is useful for debugging. */
13635
13636 void
13637 redisplay_preserve_echo_area (int from_where)
13638 {
13639 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13640
13641 if (!NILP (echo_area_buffer[1]))
13642 {
13643 /* We have a previously displayed message, but no current
13644 message. Redisplay the previous message. */
13645 display_last_displayed_message_p = 1;
13646 redisplay_internal ();
13647 display_last_displayed_message_p = 0;
13648 }
13649 else
13650 redisplay_internal ();
13651
13652 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13653 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13654 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13655 }
13656
13657
13658 /* Function registered with record_unwind_protect in
13659 redisplay_internal. Reset redisplaying_p to the value it had
13660 before redisplay_internal was called, and clear
13661 prevent_freeing_realized_faces_p. It also selects the previously
13662 selected frame, unless it has been deleted (by an X connection
13663 failure during redisplay, for example). */
13664
13665 static Lisp_Object
13666 unwind_redisplay (Lisp_Object val)
13667 {
13668 Lisp_Object old_redisplaying_p, old_frame;
13669
13670 old_redisplaying_p = XCAR (val);
13671 redisplaying_p = XFASTINT (old_redisplaying_p);
13672 old_frame = XCDR (val);
13673 if (! EQ (old_frame, selected_frame)
13674 && FRAME_LIVE_P (XFRAME (old_frame)))
13675 select_frame_for_redisplay (old_frame);
13676 return Qnil;
13677 }
13678
13679
13680 /* Mark the display of window W as accurate or inaccurate. If
13681 ACCURATE_P is non-zero mark display of W as accurate. If
13682 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13683 redisplay_internal is called. */
13684
13685 static void
13686 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13687 {
13688 if (BUFFERP (w->buffer))
13689 {
13690 struct buffer *b = XBUFFER (w->buffer);
13691
13692 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13693 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13694 w->last_had_star
13695 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13696
13697 if (accurate_p)
13698 {
13699 b->clip_changed = 0;
13700 b->prevent_redisplay_optimizations_p = 0;
13701
13702 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13703 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13704 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13705 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13706
13707 w->current_matrix->buffer = b;
13708 w->current_matrix->begv = BUF_BEGV (b);
13709 w->current_matrix->zv = BUF_ZV (b);
13710
13711 w->last_cursor = w->cursor;
13712 w->last_cursor_off_p = w->cursor_off_p;
13713
13714 if (w == XWINDOW (selected_window))
13715 w->last_point = BUF_PT (b);
13716 else
13717 w->last_point = XMARKER (w->pointm)->charpos;
13718 }
13719 }
13720
13721 if (accurate_p)
13722 {
13723 w->window_end_valid = w->buffer;
13724 w->update_mode_line = 0;
13725 }
13726 }
13727
13728
13729 /* Mark the display of windows in the window tree rooted at WINDOW as
13730 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13731 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13732 be redisplayed the next time redisplay_internal is called. */
13733
13734 void
13735 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13736 {
13737 struct window *w;
13738
13739 for (; !NILP (window); window = w->next)
13740 {
13741 w = XWINDOW (window);
13742 mark_window_display_accurate_1 (w, accurate_p);
13743
13744 if (!NILP (w->vchild))
13745 mark_window_display_accurate (w->vchild, accurate_p);
13746 if (!NILP (w->hchild))
13747 mark_window_display_accurate (w->hchild, accurate_p);
13748 }
13749
13750 if (accurate_p)
13751 {
13752 update_overlay_arrows (1);
13753 }
13754 else
13755 {
13756 /* Force a thorough redisplay the next time by setting
13757 last_arrow_position and last_arrow_string to t, which is
13758 unequal to any useful value of Voverlay_arrow_... */
13759 update_overlay_arrows (-1);
13760 }
13761 }
13762
13763
13764 /* Return value in display table DP (Lisp_Char_Table *) for character
13765 C. Since a display table doesn't have any parent, we don't have to
13766 follow parent. Do not call this function directly but use the
13767 macro DISP_CHAR_VECTOR. */
13768
13769 Lisp_Object
13770 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13771 {
13772 Lisp_Object val;
13773
13774 if (ASCII_CHAR_P (c))
13775 {
13776 val = dp->ascii;
13777 if (SUB_CHAR_TABLE_P (val))
13778 val = XSUB_CHAR_TABLE (val)->contents[c];
13779 }
13780 else
13781 {
13782 Lisp_Object table;
13783
13784 XSETCHAR_TABLE (table, dp);
13785 val = char_table_ref (table, c);
13786 }
13787 if (NILP (val))
13788 val = dp->defalt;
13789 return val;
13790 }
13791
13792
13793 \f
13794 /***********************************************************************
13795 Window Redisplay
13796 ***********************************************************************/
13797
13798 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13799
13800 static void
13801 redisplay_windows (Lisp_Object window)
13802 {
13803 while (!NILP (window))
13804 {
13805 struct window *w = XWINDOW (window);
13806
13807 if (!NILP (w->hchild))
13808 redisplay_windows (w->hchild);
13809 else if (!NILP (w->vchild))
13810 redisplay_windows (w->vchild);
13811 else if (!NILP (w->buffer))
13812 {
13813 displayed_buffer = XBUFFER (w->buffer);
13814 /* Use list_of_error, not Qerror, so that
13815 we catch only errors and don't run the debugger. */
13816 internal_condition_case_1 (redisplay_window_0, window,
13817 list_of_error,
13818 redisplay_window_error);
13819 }
13820
13821 window = w->next;
13822 }
13823 }
13824
13825 static Lisp_Object
13826 redisplay_window_error (Lisp_Object ignore)
13827 {
13828 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13829 return Qnil;
13830 }
13831
13832 static Lisp_Object
13833 redisplay_window_0 (Lisp_Object window)
13834 {
13835 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13836 redisplay_window (window, 0);
13837 return Qnil;
13838 }
13839
13840 static Lisp_Object
13841 redisplay_window_1 (Lisp_Object window)
13842 {
13843 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13844 redisplay_window (window, 1);
13845 return Qnil;
13846 }
13847 \f
13848
13849 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13850 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13851 which positions recorded in ROW differ from current buffer
13852 positions.
13853
13854 Return 0 if cursor is not on this row, 1 otherwise. */
13855
13856 static int
13857 set_cursor_from_row (struct window *w, struct glyph_row *row,
13858 struct glyph_matrix *matrix,
13859 ptrdiff_t delta, ptrdiff_t delta_bytes,
13860 int dy, int dvpos)
13861 {
13862 struct glyph *glyph = row->glyphs[TEXT_AREA];
13863 struct glyph *end = glyph + row->used[TEXT_AREA];
13864 struct glyph *cursor = NULL;
13865 /* The last known character position in row. */
13866 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13867 int x = row->x;
13868 ptrdiff_t pt_old = PT - delta;
13869 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13870 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13871 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13872 /* A glyph beyond the edge of TEXT_AREA which we should never
13873 touch. */
13874 struct glyph *glyphs_end = end;
13875 /* Non-zero means we've found a match for cursor position, but that
13876 glyph has the avoid_cursor_p flag set. */
13877 int match_with_avoid_cursor = 0;
13878 /* Non-zero means we've seen at least one glyph that came from a
13879 display string. */
13880 int string_seen = 0;
13881 /* Largest and smallest buffer positions seen so far during scan of
13882 glyph row. */
13883 ptrdiff_t bpos_max = pos_before;
13884 ptrdiff_t bpos_min = pos_after;
13885 /* Last buffer position covered by an overlay string with an integer
13886 `cursor' property. */
13887 ptrdiff_t bpos_covered = 0;
13888 /* Non-zero means the display string on which to display the cursor
13889 comes from a text property, not from an overlay. */
13890 int string_from_text_prop = 0;
13891
13892 /* Don't even try doing anything if called for a mode-line or
13893 header-line row, since the rest of the code isn't prepared to
13894 deal with such calamities. */
13895 eassert (!row->mode_line_p);
13896 if (row->mode_line_p)
13897 return 0;
13898
13899 /* Skip over glyphs not having an object at the start and the end of
13900 the row. These are special glyphs like truncation marks on
13901 terminal frames. */
13902 if (row->displays_text_p)
13903 {
13904 if (!row->reversed_p)
13905 {
13906 while (glyph < end
13907 && INTEGERP (glyph->object)
13908 && glyph->charpos < 0)
13909 {
13910 x += glyph->pixel_width;
13911 ++glyph;
13912 }
13913 while (end > glyph
13914 && INTEGERP ((end - 1)->object)
13915 /* CHARPOS is zero for blanks and stretch glyphs
13916 inserted by extend_face_to_end_of_line. */
13917 && (end - 1)->charpos <= 0)
13918 --end;
13919 glyph_before = glyph - 1;
13920 glyph_after = end;
13921 }
13922 else
13923 {
13924 struct glyph *g;
13925
13926 /* If the glyph row is reversed, we need to process it from back
13927 to front, so swap the edge pointers. */
13928 glyphs_end = end = glyph - 1;
13929 glyph += row->used[TEXT_AREA] - 1;
13930
13931 while (glyph > end + 1
13932 && INTEGERP (glyph->object)
13933 && glyph->charpos < 0)
13934 {
13935 --glyph;
13936 x -= glyph->pixel_width;
13937 }
13938 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13939 --glyph;
13940 /* By default, in reversed rows we put the cursor on the
13941 rightmost (first in the reading order) glyph. */
13942 for (g = end + 1; g < glyph; g++)
13943 x += g->pixel_width;
13944 while (end < glyph
13945 && INTEGERP ((end + 1)->object)
13946 && (end + 1)->charpos <= 0)
13947 ++end;
13948 glyph_before = glyph + 1;
13949 glyph_after = end;
13950 }
13951 }
13952 else if (row->reversed_p)
13953 {
13954 /* In R2L rows that don't display text, put the cursor on the
13955 rightmost glyph. Case in point: an empty last line that is
13956 part of an R2L paragraph. */
13957 cursor = end - 1;
13958 /* Avoid placing the cursor on the last glyph of the row, where
13959 on terminal frames we hold the vertical border between
13960 adjacent windows. */
13961 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13962 && !WINDOW_RIGHTMOST_P (w)
13963 && cursor == row->glyphs[LAST_AREA] - 1)
13964 cursor--;
13965 x = -1; /* will be computed below, at label compute_x */
13966 }
13967
13968 /* Step 1: Try to find the glyph whose character position
13969 corresponds to point. If that's not possible, find 2 glyphs
13970 whose character positions are the closest to point, one before
13971 point, the other after it. */
13972 if (!row->reversed_p)
13973 while (/* not marched to end of glyph row */
13974 glyph < end
13975 /* glyph was not inserted by redisplay for internal purposes */
13976 && !INTEGERP (glyph->object))
13977 {
13978 if (BUFFERP (glyph->object))
13979 {
13980 ptrdiff_t dpos = glyph->charpos - pt_old;
13981
13982 if (glyph->charpos > bpos_max)
13983 bpos_max = glyph->charpos;
13984 if (glyph->charpos < bpos_min)
13985 bpos_min = glyph->charpos;
13986 if (!glyph->avoid_cursor_p)
13987 {
13988 /* If we hit point, we've found the glyph on which to
13989 display the cursor. */
13990 if (dpos == 0)
13991 {
13992 match_with_avoid_cursor = 0;
13993 break;
13994 }
13995 /* See if we've found a better approximation to
13996 POS_BEFORE or to POS_AFTER. */
13997 if (0 > dpos && dpos > pos_before - pt_old)
13998 {
13999 pos_before = glyph->charpos;
14000 glyph_before = glyph;
14001 }
14002 else if (0 < dpos && dpos < pos_after - pt_old)
14003 {
14004 pos_after = glyph->charpos;
14005 glyph_after = glyph;
14006 }
14007 }
14008 else if (dpos == 0)
14009 match_with_avoid_cursor = 1;
14010 }
14011 else if (STRINGP (glyph->object))
14012 {
14013 Lisp_Object chprop;
14014 ptrdiff_t glyph_pos = glyph->charpos;
14015
14016 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14017 glyph->object);
14018 if (!NILP (chprop))
14019 {
14020 /* If the string came from a `display' text property,
14021 look up the buffer position of that property and
14022 use that position to update bpos_max, as if we
14023 actually saw such a position in one of the row's
14024 glyphs. This helps with supporting integer values
14025 of `cursor' property on the display string in
14026 situations where most or all of the row's buffer
14027 text is completely covered by display properties,
14028 so that no glyph with valid buffer positions is
14029 ever seen in the row. */
14030 ptrdiff_t prop_pos =
14031 string_buffer_position_lim (glyph->object, pos_before,
14032 pos_after, 0);
14033
14034 if (prop_pos >= pos_before)
14035 bpos_max = prop_pos - 1;
14036 }
14037 if (INTEGERP (chprop))
14038 {
14039 bpos_covered = bpos_max + XINT (chprop);
14040 /* If the `cursor' property covers buffer positions up
14041 to and including point, we should display cursor on
14042 this glyph. Note that, if a `cursor' property on one
14043 of the string's characters has an integer value, we
14044 will break out of the loop below _before_ we get to
14045 the position match above. IOW, integer values of
14046 the `cursor' property override the "exact match for
14047 point" strategy of positioning the cursor. */
14048 /* Implementation note: bpos_max == pt_old when, e.g.,
14049 we are in an empty line, where bpos_max is set to
14050 MATRIX_ROW_START_CHARPOS, see above. */
14051 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14052 {
14053 cursor = glyph;
14054 break;
14055 }
14056 }
14057
14058 string_seen = 1;
14059 }
14060 x += glyph->pixel_width;
14061 ++glyph;
14062 }
14063 else if (glyph > end) /* row is reversed */
14064 while (!INTEGERP (glyph->object))
14065 {
14066 if (BUFFERP (glyph->object))
14067 {
14068 ptrdiff_t dpos = glyph->charpos - pt_old;
14069
14070 if (glyph->charpos > bpos_max)
14071 bpos_max = glyph->charpos;
14072 if (glyph->charpos < bpos_min)
14073 bpos_min = glyph->charpos;
14074 if (!glyph->avoid_cursor_p)
14075 {
14076 if (dpos == 0)
14077 {
14078 match_with_avoid_cursor = 0;
14079 break;
14080 }
14081 if (0 > dpos && dpos > pos_before - pt_old)
14082 {
14083 pos_before = glyph->charpos;
14084 glyph_before = glyph;
14085 }
14086 else if (0 < dpos && dpos < pos_after - pt_old)
14087 {
14088 pos_after = glyph->charpos;
14089 glyph_after = glyph;
14090 }
14091 }
14092 else if (dpos == 0)
14093 match_with_avoid_cursor = 1;
14094 }
14095 else if (STRINGP (glyph->object))
14096 {
14097 Lisp_Object chprop;
14098 ptrdiff_t glyph_pos = glyph->charpos;
14099
14100 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14101 glyph->object);
14102 if (!NILP (chprop))
14103 {
14104 ptrdiff_t prop_pos =
14105 string_buffer_position_lim (glyph->object, pos_before,
14106 pos_after, 0);
14107
14108 if (prop_pos >= pos_before)
14109 bpos_max = prop_pos - 1;
14110 }
14111 if (INTEGERP (chprop))
14112 {
14113 bpos_covered = bpos_max + XINT (chprop);
14114 /* If the `cursor' property covers buffer positions up
14115 to and including point, we should display cursor on
14116 this glyph. */
14117 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14118 {
14119 cursor = glyph;
14120 break;
14121 }
14122 }
14123 string_seen = 1;
14124 }
14125 --glyph;
14126 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14127 {
14128 x--; /* can't use any pixel_width */
14129 break;
14130 }
14131 x -= glyph->pixel_width;
14132 }
14133
14134 /* Step 2: If we didn't find an exact match for point, we need to
14135 look for a proper place to put the cursor among glyphs between
14136 GLYPH_BEFORE and GLYPH_AFTER. */
14137 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14138 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14139 && bpos_covered < pt_old)
14140 {
14141 /* An empty line has a single glyph whose OBJECT is zero and
14142 whose CHARPOS is the position of a newline on that line.
14143 Note that on a TTY, there are more glyphs after that, which
14144 were produced by extend_face_to_end_of_line, but their
14145 CHARPOS is zero or negative. */
14146 int empty_line_p =
14147 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14148 && INTEGERP (glyph->object) && glyph->charpos > 0;
14149
14150 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14151 {
14152 ptrdiff_t ellipsis_pos;
14153
14154 /* Scan back over the ellipsis glyphs. */
14155 if (!row->reversed_p)
14156 {
14157 ellipsis_pos = (glyph - 1)->charpos;
14158 while (glyph > row->glyphs[TEXT_AREA]
14159 && (glyph - 1)->charpos == ellipsis_pos)
14160 glyph--, x -= glyph->pixel_width;
14161 /* That loop always goes one position too far, including
14162 the glyph before the ellipsis. So scan forward over
14163 that one. */
14164 x += glyph->pixel_width;
14165 glyph++;
14166 }
14167 else /* row is reversed */
14168 {
14169 ellipsis_pos = (glyph + 1)->charpos;
14170 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14171 && (glyph + 1)->charpos == ellipsis_pos)
14172 glyph++, x += glyph->pixel_width;
14173 x -= glyph->pixel_width;
14174 glyph--;
14175 }
14176 }
14177 else if (match_with_avoid_cursor)
14178 {
14179 cursor = glyph_after;
14180 x = -1;
14181 }
14182 else if (string_seen)
14183 {
14184 int incr = row->reversed_p ? -1 : +1;
14185
14186 /* Need to find the glyph that came out of a string which is
14187 present at point. That glyph is somewhere between
14188 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14189 positioned between POS_BEFORE and POS_AFTER in the
14190 buffer. */
14191 struct glyph *start, *stop;
14192 ptrdiff_t pos = pos_before;
14193
14194 x = -1;
14195
14196 /* If the row ends in a newline from a display string,
14197 reordering could have moved the glyphs belonging to the
14198 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14199 in this case we extend the search to the last glyph in
14200 the row that was not inserted by redisplay. */
14201 if (row->ends_in_newline_from_string_p)
14202 {
14203 glyph_after = end;
14204 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14205 }
14206
14207 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14208 correspond to POS_BEFORE and POS_AFTER, respectively. We
14209 need START and STOP in the order that corresponds to the
14210 row's direction as given by its reversed_p flag. If the
14211 directionality of characters between POS_BEFORE and
14212 POS_AFTER is the opposite of the row's base direction,
14213 these characters will have been reordered for display,
14214 and we need to reverse START and STOP. */
14215 if (!row->reversed_p)
14216 {
14217 start = min (glyph_before, glyph_after);
14218 stop = max (glyph_before, glyph_after);
14219 }
14220 else
14221 {
14222 start = max (glyph_before, glyph_after);
14223 stop = min (glyph_before, glyph_after);
14224 }
14225 for (glyph = start + incr;
14226 row->reversed_p ? glyph > stop : glyph < stop; )
14227 {
14228
14229 /* Any glyphs that come from the buffer are here because
14230 of bidi reordering. Skip them, and only pay
14231 attention to glyphs that came from some string. */
14232 if (STRINGP (glyph->object))
14233 {
14234 Lisp_Object str;
14235 ptrdiff_t tem;
14236 /* If the display property covers the newline, we
14237 need to search for it one position farther. */
14238 ptrdiff_t lim = pos_after
14239 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14240
14241 string_from_text_prop = 0;
14242 str = glyph->object;
14243 tem = string_buffer_position_lim (str, pos, lim, 0);
14244 if (tem == 0 /* from overlay */
14245 || pos <= tem)
14246 {
14247 /* If the string from which this glyph came is
14248 found in the buffer at point, or at position
14249 that is closer to point than pos_after, then
14250 we've found the glyph we've been looking for.
14251 If it comes from an overlay (tem == 0), and
14252 it has the `cursor' property on one of its
14253 glyphs, record that glyph as a candidate for
14254 displaying the cursor. (As in the
14255 unidirectional version, we will display the
14256 cursor on the last candidate we find.) */
14257 if (tem == 0
14258 || tem == pt_old
14259 || (tem - pt_old > 0 && tem < pos_after))
14260 {
14261 /* The glyphs from this string could have
14262 been reordered. Find the one with the
14263 smallest string position. Or there could
14264 be a character in the string with the
14265 `cursor' property, which means display
14266 cursor on that character's glyph. */
14267 ptrdiff_t strpos = glyph->charpos;
14268
14269 if (tem)
14270 {
14271 cursor = glyph;
14272 string_from_text_prop = 1;
14273 }
14274 for ( ;
14275 (row->reversed_p ? glyph > stop : glyph < stop)
14276 && EQ (glyph->object, str);
14277 glyph += incr)
14278 {
14279 Lisp_Object cprop;
14280 ptrdiff_t gpos = glyph->charpos;
14281
14282 cprop = Fget_char_property (make_number (gpos),
14283 Qcursor,
14284 glyph->object);
14285 if (!NILP (cprop))
14286 {
14287 cursor = glyph;
14288 break;
14289 }
14290 if (tem && glyph->charpos < strpos)
14291 {
14292 strpos = glyph->charpos;
14293 cursor = glyph;
14294 }
14295 }
14296
14297 if (tem == pt_old
14298 || (tem - pt_old > 0 && tem < pos_after))
14299 goto compute_x;
14300 }
14301 if (tem)
14302 pos = tem + 1; /* don't find previous instances */
14303 }
14304 /* This string is not what we want; skip all of the
14305 glyphs that came from it. */
14306 while ((row->reversed_p ? glyph > stop : glyph < stop)
14307 && EQ (glyph->object, str))
14308 glyph += incr;
14309 }
14310 else
14311 glyph += incr;
14312 }
14313
14314 /* If we reached the end of the line, and END was from a string,
14315 the cursor is not on this line. */
14316 if (cursor == NULL
14317 && (row->reversed_p ? glyph <= end : glyph >= end)
14318 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14319 && STRINGP (end->object)
14320 && row->continued_p)
14321 return 0;
14322 }
14323 /* A truncated row may not include PT among its character positions.
14324 Setting the cursor inside the scroll margin will trigger
14325 recalculation of hscroll in hscroll_window_tree. But if a
14326 display string covers point, defer to the string-handling
14327 code below to figure this out. */
14328 else if (row->truncated_on_left_p && pt_old < bpos_min)
14329 {
14330 cursor = glyph_before;
14331 x = -1;
14332 }
14333 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14334 /* Zero-width characters produce no glyphs. */
14335 || (!empty_line_p
14336 && (row->reversed_p
14337 ? glyph_after > glyphs_end
14338 : glyph_after < glyphs_end)))
14339 {
14340 cursor = glyph_after;
14341 x = -1;
14342 }
14343 }
14344
14345 compute_x:
14346 if (cursor != NULL)
14347 glyph = cursor;
14348 else if (glyph == glyphs_end
14349 && pos_before == pos_after
14350 && STRINGP ((row->reversed_p
14351 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14352 : row->glyphs[TEXT_AREA])->object))
14353 {
14354 /* If all the glyphs of this row came from strings, put the
14355 cursor on the first glyph of the row. This avoids having the
14356 cursor outside of the text area in this very rare and hard
14357 use case. */
14358 glyph =
14359 row->reversed_p
14360 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14361 : row->glyphs[TEXT_AREA];
14362 }
14363 if (x < 0)
14364 {
14365 struct glyph *g;
14366
14367 /* Need to compute x that corresponds to GLYPH. */
14368 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14369 {
14370 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14371 abort ();
14372 x += g->pixel_width;
14373 }
14374 }
14375
14376 /* ROW could be part of a continued line, which, under bidi
14377 reordering, might have other rows whose start and end charpos
14378 occlude point. Only set w->cursor if we found a better
14379 approximation to the cursor position than we have from previously
14380 examined candidate rows belonging to the same continued line. */
14381 if (/* we already have a candidate row */
14382 w->cursor.vpos >= 0
14383 /* that candidate is not the row we are processing */
14384 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14385 /* Make sure cursor.vpos specifies a row whose start and end
14386 charpos occlude point, and it is valid candidate for being a
14387 cursor-row. This is because some callers of this function
14388 leave cursor.vpos at the row where the cursor was displayed
14389 during the last redisplay cycle. */
14390 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14391 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14392 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14393 {
14394 struct glyph *g1 =
14395 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14396
14397 /* Don't consider glyphs that are outside TEXT_AREA. */
14398 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14399 return 0;
14400 /* Keep the candidate whose buffer position is the closest to
14401 point or has the `cursor' property. */
14402 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14403 w->cursor.hpos >= 0
14404 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14405 && ((BUFFERP (g1->object)
14406 && (g1->charpos == pt_old /* an exact match always wins */
14407 || (BUFFERP (glyph->object)
14408 && eabs (g1->charpos - pt_old)
14409 < eabs (glyph->charpos - pt_old))))
14410 /* previous candidate is a glyph from a string that has
14411 a non-nil `cursor' property */
14412 || (STRINGP (g1->object)
14413 && (!NILP (Fget_char_property (make_number (g1->charpos),
14414 Qcursor, g1->object))
14415 /* previous candidate is from the same display
14416 string as this one, and the display string
14417 came from a text property */
14418 || (EQ (g1->object, glyph->object)
14419 && string_from_text_prop)
14420 /* this candidate is from newline and its
14421 position is not an exact match */
14422 || (INTEGERP (glyph->object)
14423 && glyph->charpos != pt_old)))))
14424 return 0;
14425 /* If this candidate gives an exact match, use that. */
14426 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14427 /* If this candidate is a glyph created for the
14428 terminating newline of a line, and point is on that
14429 newline, it wins because it's an exact match. */
14430 || (!row->continued_p
14431 && INTEGERP (glyph->object)
14432 && glyph->charpos == 0
14433 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14434 /* Otherwise, keep the candidate that comes from a row
14435 spanning less buffer positions. This may win when one or
14436 both candidate positions are on glyphs that came from
14437 display strings, for which we cannot compare buffer
14438 positions. */
14439 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14440 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14441 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14442 return 0;
14443 }
14444 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14445 w->cursor.x = x;
14446 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14447 w->cursor.y = row->y + dy;
14448
14449 if (w == XWINDOW (selected_window))
14450 {
14451 if (!row->continued_p
14452 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14453 && row->x == 0)
14454 {
14455 this_line_buffer = XBUFFER (w->buffer);
14456
14457 CHARPOS (this_line_start_pos)
14458 = MATRIX_ROW_START_CHARPOS (row) + delta;
14459 BYTEPOS (this_line_start_pos)
14460 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14461
14462 CHARPOS (this_line_end_pos)
14463 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14464 BYTEPOS (this_line_end_pos)
14465 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14466
14467 this_line_y = w->cursor.y;
14468 this_line_pixel_height = row->height;
14469 this_line_vpos = w->cursor.vpos;
14470 this_line_start_x = row->x;
14471 }
14472 else
14473 CHARPOS (this_line_start_pos) = 0;
14474 }
14475
14476 return 1;
14477 }
14478
14479
14480 /* Run window scroll functions, if any, for WINDOW with new window
14481 start STARTP. Sets the window start of WINDOW to that position.
14482
14483 We assume that the window's buffer is really current. */
14484
14485 static inline struct text_pos
14486 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14487 {
14488 struct window *w = XWINDOW (window);
14489 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14490
14491 if (current_buffer != XBUFFER (w->buffer))
14492 abort ();
14493
14494 if (!NILP (Vwindow_scroll_functions))
14495 {
14496 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14497 make_number (CHARPOS (startp)));
14498 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14499 /* In case the hook functions switch buffers. */
14500 if (current_buffer != XBUFFER (w->buffer))
14501 set_buffer_internal_1 (XBUFFER (w->buffer));
14502 }
14503
14504 return startp;
14505 }
14506
14507
14508 /* Make sure the line containing the cursor is fully visible.
14509 A value of 1 means there is nothing to be done.
14510 (Either the line is fully visible, or it cannot be made so,
14511 or we cannot tell.)
14512
14513 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14514 is higher than window.
14515
14516 A value of 0 means the caller should do scrolling
14517 as if point had gone off the screen. */
14518
14519 static int
14520 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14521 {
14522 struct glyph_matrix *matrix;
14523 struct glyph_row *row;
14524 int window_height;
14525
14526 if (!make_cursor_line_fully_visible_p)
14527 return 1;
14528
14529 /* It's not always possible to find the cursor, e.g, when a window
14530 is full of overlay strings. Don't do anything in that case. */
14531 if (w->cursor.vpos < 0)
14532 return 1;
14533
14534 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14535 row = MATRIX_ROW (matrix, w->cursor.vpos);
14536
14537 /* If the cursor row is not partially visible, there's nothing to do. */
14538 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14539 return 1;
14540
14541 /* If the row the cursor is in is taller than the window's height,
14542 it's not clear what to do, so do nothing. */
14543 window_height = window_box_height (w);
14544 if (row->height >= window_height)
14545 {
14546 if (!force_p || MINI_WINDOW_P (w)
14547 || w->vscroll || w->cursor.vpos == 0)
14548 return 1;
14549 }
14550 return 0;
14551 }
14552
14553
14554 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14555 non-zero means only WINDOW is redisplayed in redisplay_internal.
14556 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14557 in redisplay_window to bring a partially visible line into view in
14558 the case that only the cursor has moved.
14559
14560 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14561 last screen line's vertical height extends past the end of the screen.
14562
14563 Value is
14564
14565 1 if scrolling succeeded
14566
14567 0 if scrolling didn't find point.
14568
14569 -1 if new fonts have been loaded so that we must interrupt
14570 redisplay, adjust glyph matrices, and try again. */
14571
14572 enum
14573 {
14574 SCROLLING_SUCCESS,
14575 SCROLLING_FAILED,
14576 SCROLLING_NEED_LARGER_MATRICES
14577 };
14578
14579 /* If scroll-conservatively is more than this, never recenter.
14580
14581 If you change this, don't forget to update the doc string of
14582 `scroll-conservatively' and the Emacs manual. */
14583 #define SCROLL_LIMIT 100
14584
14585 static int
14586 try_scrolling (Lisp_Object window, int just_this_one_p,
14587 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14588 int temp_scroll_step, int last_line_misfit)
14589 {
14590 struct window *w = XWINDOW (window);
14591 struct frame *f = XFRAME (w->frame);
14592 struct text_pos pos, startp;
14593 struct it it;
14594 int this_scroll_margin, scroll_max, rc, height;
14595 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14596 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14597 Lisp_Object aggressive;
14598 /* We will never try scrolling more than this number of lines. */
14599 int scroll_limit = SCROLL_LIMIT;
14600
14601 #ifdef GLYPH_DEBUG
14602 debug_method_add (w, "try_scrolling");
14603 #endif
14604
14605 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14606
14607 /* Compute scroll margin height in pixels. We scroll when point is
14608 within this distance from the top or bottom of the window. */
14609 if (scroll_margin > 0)
14610 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14611 * FRAME_LINE_HEIGHT (f);
14612 else
14613 this_scroll_margin = 0;
14614
14615 /* Force arg_scroll_conservatively to have a reasonable value, to
14616 avoid scrolling too far away with slow move_it_* functions. Note
14617 that the user can supply scroll-conservatively equal to
14618 `most-positive-fixnum', which can be larger than INT_MAX. */
14619 if (arg_scroll_conservatively > scroll_limit)
14620 {
14621 arg_scroll_conservatively = scroll_limit + 1;
14622 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14623 }
14624 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14625 /* Compute how much we should try to scroll maximally to bring
14626 point into view. */
14627 scroll_max = (max (scroll_step,
14628 max (arg_scroll_conservatively, temp_scroll_step))
14629 * FRAME_LINE_HEIGHT (f));
14630 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14631 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14632 /* We're trying to scroll because of aggressive scrolling but no
14633 scroll_step is set. Choose an arbitrary one. */
14634 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14635 else
14636 scroll_max = 0;
14637
14638 too_near_end:
14639
14640 /* Decide whether to scroll down. */
14641 if (PT > CHARPOS (startp))
14642 {
14643 int scroll_margin_y;
14644
14645 /* Compute the pixel ypos of the scroll margin, then move IT to
14646 either that ypos or PT, whichever comes first. */
14647 start_display (&it, w, startp);
14648 scroll_margin_y = it.last_visible_y - this_scroll_margin
14649 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14650 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14651 (MOVE_TO_POS | MOVE_TO_Y));
14652
14653 if (PT > CHARPOS (it.current.pos))
14654 {
14655 int y0 = line_bottom_y (&it);
14656 /* Compute how many pixels below window bottom to stop searching
14657 for PT. This avoids costly search for PT that is far away if
14658 the user limited scrolling by a small number of lines, but
14659 always finds PT if scroll_conservatively is set to a large
14660 number, such as most-positive-fixnum. */
14661 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14662 int y_to_move = it.last_visible_y + slack;
14663
14664 /* Compute the distance from the scroll margin to PT or to
14665 the scroll limit, whichever comes first. This should
14666 include the height of the cursor line, to make that line
14667 fully visible. */
14668 move_it_to (&it, PT, -1, y_to_move,
14669 -1, MOVE_TO_POS | MOVE_TO_Y);
14670 dy = line_bottom_y (&it) - y0;
14671
14672 if (dy > scroll_max)
14673 return SCROLLING_FAILED;
14674
14675 if (dy > 0)
14676 scroll_down_p = 1;
14677 }
14678 }
14679
14680 if (scroll_down_p)
14681 {
14682 /* Point is in or below the bottom scroll margin, so move the
14683 window start down. If scrolling conservatively, move it just
14684 enough down to make point visible. If scroll_step is set,
14685 move it down by scroll_step. */
14686 if (arg_scroll_conservatively)
14687 amount_to_scroll
14688 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14689 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14690 else if (scroll_step || temp_scroll_step)
14691 amount_to_scroll = scroll_max;
14692 else
14693 {
14694 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14695 height = WINDOW_BOX_TEXT_HEIGHT (w);
14696 if (NUMBERP (aggressive))
14697 {
14698 double float_amount = XFLOATINT (aggressive) * height;
14699 amount_to_scroll = float_amount;
14700 if (amount_to_scroll == 0 && float_amount > 0)
14701 amount_to_scroll = 1;
14702 /* Don't let point enter the scroll margin near top of
14703 the window. */
14704 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14705 amount_to_scroll = height - 2*this_scroll_margin + dy;
14706 }
14707 }
14708
14709 if (amount_to_scroll <= 0)
14710 return SCROLLING_FAILED;
14711
14712 start_display (&it, w, startp);
14713 if (arg_scroll_conservatively <= scroll_limit)
14714 move_it_vertically (&it, amount_to_scroll);
14715 else
14716 {
14717 /* Extra precision for users who set scroll-conservatively
14718 to a large number: make sure the amount we scroll
14719 the window start is never less than amount_to_scroll,
14720 which was computed as distance from window bottom to
14721 point. This matters when lines at window top and lines
14722 below window bottom have different height. */
14723 struct it it1;
14724 void *it1data = NULL;
14725 /* We use a temporary it1 because line_bottom_y can modify
14726 its argument, if it moves one line down; see there. */
14727 int start_y;
14728
14729 SAVE_IT (it1, it, it1data);
14730 start_y = line_bottom_y (&it1);
14731 do {
14732 RESTORE_IT (&it, &it, it1data);
14733 move_it_by_lines (&it, 1);
14734 SAVE_IT (it1, it, it1data);
14735 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14736 }
14737
14738 /* If STARTP is unchanged, move it down another screen line. */
14739 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14740 move_it_by_lines (&it, 1);
14741 startp = it.current.pos;
14742 }
14743 else
14744 {
14745 struct text_pos scroll_margin_pos = startp;
14746
14747 /* See if point is inside the scroll margin at the top of the
14748 window. */
14749 if (this_scroll_margin)
14750 {
14751 start_display (&it, w, startp);
14752 move_it_vertically (&it, this_scroll_margin);
14753 scroll_margin_pos = it.current.pos;
14754 }
14755
14756 if (PT < CHARPOS (scroll_margin_pos))
14757 {
14758 /* Point is in the scroll margin at the top of the window or
14759 above what is displayed in the window. */
14760 int y0, y_to_move;
14761
14762 /* Compute the vertical distance from PT to the scroll
14763 margin position. Move as far as scroll_max allows, or
14764 one screenful, or 10 screen lines, whichever is largest.
14765 Give up if distance is greater than scroll_max. */
14766 SET_TEXT_POS (pos, PT, PT_BYTE);
14767 start_display (&it, w, pos);
14768 y0 = it.current_y;
14769 y_to_move = max (it.last_visible_y,
14770 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14771 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14772 y_to_move, -1,
14773 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14774 dy = it.current_y - y0;
14775 if (dy > scroll_max)
14776 return SCROLLING_FAILED;
14777
14778 /* Compute new window start. */
14779 start_display (&it, w, startp);
14780
14781 if (arg_scroll_conservatively)
14782 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14783 max (scroll_step, temp_scroll_step));
14784 else if (scroll_step || temp_scroll_step)
14785 amount_to_scroll = scroll_max;
14786 else
14787 {
14788 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14789 height = WINDOW_BOX_TEXT_HEIGHT (w);
14790 if (NUMBERP (aggressive))
14791 {
14792 double float_amount = XFLOATINT (aggressive) * height;
14793 amount_to_scroll = float_amount;
14794 if (amount_to_scroll == 0 && float_amount > 0)
14795 amount_to_scroll = 1;
14796 amount_to_scroll -=
14797 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14798 /* Don't let point enter the scroll margin near
14799 bottom of the window. */
14800 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14801 amount_to_scroll = height - 2*this_scroll_margin + dy;
14802 }
14803 }
14804
14805 if (amount_to_scroll <= 0)
14806 return SCROLLING_FAILED;
14807
14808 move_it_vertically_backward (&it, amount_to_scroll);
14809 startp = it.current.pos;
14810 }
14811 }
14812
14813 /* Run window scroll functions. */
14814 startp = run_window_scroll_functions (window, startp);
14815
14816 /* Display the window. Give up if new fonts are loaded, or if point
14817 doesn't appear. */
14818 if (!try_window (window, startp, 0))
14819 rc = SCROLLING_NEED_LARGER_MATRICES;
14820 else if (w->cursor.vpos < 0)
14821 {
14822 clear_glyph_matrix (w->desired_matrix);
14823 rc = SCROLLING_FAILED;
14824 }
14825 else
14826 {
14827 /* Maybe forget recorded base line for line number display. */
14828 if (!just_this_one_p
14829 || current_buffer->clip_changed
14830 || BEG_UNCHANGED < CHARPOS (startp))
14831 w->base_line_number = Qnil;
14832
14833 /* If cursor ends up on a partially visible line,
14834 treat that as being off the bottom of the screen. */
14835 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14836 /* It's possible that the cursor is on the first line of the
14837 buffer, which is partially obscured due to a vscroll
14838 (Bug#7537). In that case, avoid looping forever . */
14839 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14840 {
14841 clear_glyph_matrix (w->desired_matrix);
14842 ++extra_scroll_margin_lines;
14843 goto too_near_end;
14844 }
14845 rc = SCROLLING_SUCCESS;
14846 }
14847
14848 return rc;
14849 }
14850
14851
14852 /* Compute a suitable window start for window W if display of W starts
14853 on a continuation line. Value is non-zero if a new window start
14854 was computed.
14855
14856 The new window start will be computed, based on W's width, starting
14857 from the start of the continued line. It is the start of the
14858 screen line with the minimum distance from the old start W->start. */
14859
14860 static int
14861 compute_window_start_on_continuation_line (struct window *w)
14862 {
14863 struct text_pos pos, start_pos;
14864 int window_start_changed_p = 0;
14865
14866 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14867
14868 /* If window start is on a continuation line... Window start may be
14869 < BEGV in case there's invisible text at the start of the
14870 buffer (M-x rmail, for example). */
14871 if (CHARPOS (start_pos) > BEGV
14872 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14873 {
14874 struct it it;
14875 struct glyph_row *row;
14876
14877 /* Handle the case that the window start is out of range. */
14878 if (CHARPOS (start_pos) < BEGV)
14879 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14880 else if (CHARPOS (start_pos) > ZV)
14881 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14882
14883 /* Find the start of the continued line. This should be fast
14884 because scan_buffer is fast (newline cache). */
14885 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14886 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14887 row, DEFAULT_FACE_ID);
14888 reseat_at_previous_visible_line_start (&it);
14889
14890 /* If the line start is "too far" away from the window start,
14891 say it takes too much time to compute a new window start. */
14892 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14893 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14894 {
14895 int min_distance, distance;
14896
14897 /* Move forward by display lines to find the new window
14898 start. If window width was enlarged, the new start can
14899 be expected to be > the old start. If window width was
14900 decreased, the new window start will be < the old start.
14901 So, we're looking for the display line start with the
14902 minimum distance from the old window start. */
14903 pos = it.current.pos;
14904 min_distance = INFINITY;
14905 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14906 distance < min_distance)
14907 {
14908 min_distance = distance;
14909 pos = it.current.pos;
14910 move_it_by_lines (&it, 1);
14911 }
14912
14913 /* Set the window start there. */
14914 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14915 window_start_changed_p = 1;
14916 }
14917 }
14918
14919 return window_start_changed_p;
14920 }
14921
14922
14923 /* Try cursor movement in case text has not changed in window WINDOW,
14924 with window start STARTP. Value is
14925
14926 CURSOR_MOVEMENT_SUCCESS if successful
14927
14928 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14929
14930 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14931 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14932 we want to scroll as if scroll-step were set to 1. See the code.
14933
14934 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14935 which case we have to abort this redisplay, and adjust matrices
14936 first. */
14937
14938 enum
14939 {
14940 CURSOR_MOVEMENT_SUCCESS,
14941 CURSOR_MOVEMENT_CANNOT_BE_USED,
14942 CURSOR_MOVEMENT_MUST_SCROLL,
14943 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14944 };
14945
14946 static int
14947 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14948 {
14949 struct window *w = XWINDOW (window);
14950 struct frame *f = XFRAME (w->frame);
14951 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14952
14953 #ifdef GLYPH_DEBUG
14954 if (inhibit_try_cursor_movement)
14955 return rc;
14956 #endif
14957
14958 /* Previously, there was a check for Lisp integer in the
14959 if-statement below. Now, this field is converted to
14960 ptrdiff_t, thus zero means invalid position in a buffer. */
14961 eassert (w->last_point > 0);
14962
14963 /* Handle case where text has not changed, only point, and it has
14964 not moved off the frame. */
14965 if (/* Point may be in this window. */
14966 PT >= CHARPOS (startp)
14967 /* Selective display hasn't changed. */
14968 && !current_buffer->clip_changed
14969 /* Function force-mode-line-update is used to force a thorough
14970 redisplay. It sets either windows_or_buffers_changed or
14971 update_mode_lines. So don't take a shortcut here for these
14972 cases. */
14973 && !update_mode_lines
14974 && !windows_or_buffers_changed
14975 && !cursor_type_changed
14976 /* Can't use this case if highlighting a region. When a
14977 region exists, cursor movement has to do more than just
14978 set the cursor. */
14979 && !(!NILP (Vtransient_mark_mode)
14980 && !NILP (BVAR (current_buffer, mark_active)))
14981 && NILP (w->region_showing)
14982 && NILP (Vshow_trailing_whitespace)
14983 /* This code is not used for mini-buffer for the sake of the case
14984 of redisplaying to replace an echo area message; since in
14985 that case the mini-buffer contents per se are usually
14986 unchanged. This code is of no real use in the mini-buffer
14987 since the handling of this_line_start_pos, etc., in redisplay
14988 handles the same cases. */
14989 && !EQ (window, minibuf_window)
14990 /* When splitting windows or for new windows, it happens that
14991 redisplay is called with a nil window_end_vpos or one being
14992 larger than the window. This should really be fixed in
14993 window.c. I don't have this on my list, now, so we do
14994 approximately the same as the old redisplay code. --gerd. */
14995 && INTEGERP (w->window_end_vpos)
14996 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14997 && (FRAME_WINDOW_P (f)
14998 || !overlay_arrow_in_current_buffer_p ()))
14999 {
15000 int this_scroll_margin, top_scroll_margin;
15001 struct glyph_row *row = NULL;
15002
15003 #ifdef GLYPH_DEBUG
15004 debug_method_add (w, "cursor movement");
15005 #endif
15006
15007 /* Scroll if point within this distance from the top or bottom
15008 of the window. This is a pixel value. */
15009 if (scroll_margin > 0)
15010 {
15011 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15012 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15013 }
15014 else
15015 this_scroll_margin = 0;
15016
15017 top_scroll_margin = this_scroll_margin;
15018 if (WINDOW_WANTS_HEADER_LINE_P (w))
15019 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15020
15021 /* Start with the row the cursor was displayed during the last
15022 not paused redisplay. Give up if that row is not valid. */
15023 if (w->last_cursor.vpos < 0
15024 || w->last_cursor.vpos >= w->current_matrix->nrows)
15025 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15026 else
15027 {
15028 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15029 if (row->mode_line_p)
15030 ++row;
15031 if (!row->enabled_p)
15032 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15033 }
15034
15035 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15036 {
15037 int scroll_p = 0, must_scroll = 0;
15038 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15039
15040 if (PT > w->last_point)
15041 {
15042 /* Point has moved forward. */
15043 while (MATRIX_ROW_END_CHARPOS (row) < PT
15044 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15045 {
15046 eassert (row->enabled_p);
15047 ++row;
15048 }
15049
15050 /* If the end position of a row equals the start
15051 position of the next row, and PT is at that position,
15052 we would rather display cursor in the next line. */
15053 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15054 && MATRIX_ROW_END_CHARPOS (row) == PT
15055 && row < w->current_matrix->rows
15056 + w->current_matrix->nrows - 1
15057 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15058 && !cursor_row_p (row))
15059 ++row;
15060
15061 /* If within the scroll margin, scroll. Note that
15062 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15063 the next line would be drawn, and that
15064 this_scroll_margin can be zero. */
15065 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15066 || PT > MATRIX_ROW_END_CHARPOS (row)
15067 /* Line is completely visible last line in window
15068 and PT is to be set in the next line. */
15069 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15070 && PT == MATRIX_ROW_END_CHARPOS (row)
15071 && !row->ends_at_zv_p
15072 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15073 scroll_p = 1;
15074 }
15075 else if (PT < w->last_point)
15076 {
15077 /* Cursor has to be moved backward. Note that PT >=
15078 CHARPOS (startp) because of the outer if-statement. */
15079 while (!row->mode_line_p
15080 && (MATRIX_ROW_START_CHARPOS (row) > PT
15081 || (MATRIX_ROW_START_CHARPOS (row) == PT
15082 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15083 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15084 row > w->current_matrix->rows
15085 && (row-1)->ends_in_newline_from_string_p))))
15086 && (row->y > top_scroll_margin
15087 || CHARPOS (startp) == BEGV))
15088 {
15089 eassert (row->enabled_p);
15090 --row;
15091 }
15092
15093 /* Consider the following case: Window starts at BEGV,
15094 there is invisible, intangible text at BEGV, so that
15095 display starts at some point START > BEGV. It can
15096 happen that we are called with PT somewhere between
15097 BEGV and START. Try to handle that case. */
15098 if (row < w->current_matrix->rows
15099 || row->mode_line_p)
15100 {
15101 row = w->current_matrix->rows;
15102 if (row->mode_line_p)
15103 ++row;
15104 }
15105
15106 /* Due to newlines in overlay strings, we may have to
15107 skip forward over overlay strings. */
15108 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15109 && MATRIX_ROW_END_CHARPOS (row) == PT
15110 && !cursor_row_p (row))
15111 ++row;
15112
15113 /* If within the scroll margin, scroll. */
15114 if (row->y < top_scroll_margin
15115 && CHARPOS (startp) != BEGV)
15116 scroll_p = 1;
15117 }
15118 else
15119 {
15120 /* Cursor did not move. So don't scroll even if cursor line
15121 is partially visible, as it was so before. */
15122 rc = CURSOR_MOVEMENT_SUCCESS;
15123 }
15124
15125 if (PT < MATRIX_ROW_START_CHARPOS (row)
15126 || PT > MATRIX_ROW_END_CHARPOS (row))
15127 {
15128 /* if PT is not in the glyph row, give up. */
15129 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15130 must_scroll = 1;
15131 }
15132 else if (rc != CURSOR_MOVEMENT_SUCCESS
15133 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15134 {
15135 struct glyph_row *row1;
15136
15137 /* If rows are bidi-reordered and point moved, back up
15138 until we find a row that does not belong to a
15139 continuation line. This is because we must consider
15140 all rows of a continued line as candidates for the
15141 new cursor positioning, since row start and end
15142 positions change non-linearly with vertical position
15143 in such rows. */
15144 /* FIXME: Revisit this when glyph ``spilling'' in
15145 continuation lines' rows is implemented for
15146 bidi-reordered rows. */
15147 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15148 MATRIX_ROW_CONTINUATION_LINE_P (row);
15149 --row)
15150 {
15151 /* If we hit the beginning of the displayed portion
15152 without finding the first row of a continued
15153 line, give up. */
15154 if (row <= row1)
15155 {
15156 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15157 break;
15158 }
15159 eassert (row->enabled_p);
15160 }
15161 }
15162 if (must_scroll)
15163 ;
15164 else if (rc != CURSOR_MOVEMENT_SUCCESS
15165 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15166 /* Make sure this isn't a header line by any chance, since
15167 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15168 && !row->mode_line_p
15169 && make_cursor_line_fully_visible_p)
15170 {
15171 if (PT == MATRIX_ROW_END_CHARPOS (row)
15172 && !row->ends_at_zv_p
15173 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15174 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15175 else if (row->height > window_box_height (w))
15176 {
15177 /* If we end up in a partially visible line, let's
15178 make it fully visible, except when it's taller
15179 than the window, in which case we can't do much
15180 about it. */
15181 *scroll_step = 1;
15182 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15183 }
15184 else
15185 {
15186 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15187 if (!cursor_row_fully_visible_p (w, 0, 1))
15188 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15189 else
15190 rc = CURSOR_MOVEMENT_SUCCESS;
15191 }
15192 }
15193 else if (scroll_p)
15194 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15195 else if (rc != CURSOR_MOVEMENT_SUCCESS
15196 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15197 {
15198 /* With bidi-reordered rows, there could be more than
15199 one candidate row whose start and end positions
15200 occlude point. We need to let set_cursor_from_row
15201 find the best candidate. */
15202 /* FIXME: Revisit this when glyph ``spilling'' in
15203 continuation lines' rows is implemented for
15204 bidi-reordered rows. */
15205 int rv = 0;
15206
15207 do
15208 {
15209 int at_zv_p = 0, exact_match_p = 0;
15210
15211 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15212 && PT <= MATRIX_ROW_END_CHARPOS (row)
15213 && cursor_row_p (row))
15214 rv |= set_cursor_from_row (w, row, w->current_matrix,
15215 0, 0, 0, 0);
15216 /* As soon as we've found the exact match for point,
15217 or the first suitable row whose ends_at_zv_p flag
15218 is set, we are done. */
15219 at_zv_p =
15220 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15221 if (rv && !at_zv_p
15222 && w->cursor.hpos >= 0
15223 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15224 w->cursor.vpos))
15225 {
15226 struct glyph_row *candidate =
15227 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15228 struct glyph *g =
15229 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15230 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15231
15232 exact_match_p =
15233 (BUFFERP (g->object) && g->charpos == PT)
15234 || (INTEGERP (g->object)
15235 && (g->charpos == PT
15236 || (g->charpos == 0 && endpos - 1 == PT)));
15237 }
15238 if (rv && (at_zv_p || exact_match_p))
15239 {
15240 rc = CURSOR_MOVEMENT_SUCCESS;
15241 break;
15242 }
15243 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15244 break;
15245 ++row;
15246 }
15247 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15248 || row->continued_p)
15249 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15250 || (MATRIX_ROW_START_CHARPOS (row) == PT
15251 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15252 /* If we didn't find any candidate rows, or exited the
15253 loop before all the candidates were examined, signal
15254 to the caller that this method failed. */
15255 if (rc != CURSOR_MOVEMENT_SUCCESS
15256 && !(rv
15257 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15258 && !row->continued_p))
15259 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15260 else if (rv)
15261 rc = CURSOR_MOVEMENT_SUCCESS;
15262 }
15263 else
15264 {
15265 do
15266 {
15267 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15268 {
15269 rc = CURSOR_MOVEMENT_SUCCESS;
15270 break;
15271 }
15272 ++row;
15273 }
15274 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15275 && MATRIX_ROW_START_CHARPOS (row) == PT
15276 && cursor_row_p (row));
15277 }
15278 }
15279 }
15280
15281 return rc;
15282 }
15283
15284 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15285 static
15286 #endif
15287 void
15288 set_vertical_scroll_bar (struct window *w)
15289 {
15290 ptrdiff_t start, end, whole;
15291
15292 /* Calculate the start and end positions for the current window.
15293 At some point, it would be nice to choose between scrollbars
15294 which reflect the whole buffer size, with special markers
15295 indicating narrowing, and scrollbars which reflect only the
15296 visible region.
15297
15298 Note that mini-buffers sometimes aren't displaying any text. */
15299 if (!MINI_WINDOW_P (w)
15300 || (w == XWINDOW (minibuf_window)
15301 && NILP (echo_area_buffer[0])))
15302 {
15303 struct buffer *buf = XBUFFER (w->buffer);
15304 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15305 start = marker_position (w->start) - BUF_BEGV (buf);
15306 /* I don't think this is guaranteed to be right. For the
15307 moment, we'll pretend it is. */
15308 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15309
15310 if (end < start)
15311 end = start;
15312 if (whole < (end - start))
15313 whole = end - start;
15314 }
15315 else
15316 start = end = whole = 0;
15317
15318 /* Indicate what this scroll bar ought to be displaying now. */
15319 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15320 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15321 (w, end - start, whole, start);
15322 }
15323
15324
15325 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15326 selected_window is redisplayed.
15327
15328 We can return without actually redisplaying the window if
15329 fonts_changed_p is nonzero. In that case, redisplay_internal will
15330 retry. */
15331
15332 static void
15333 redisplay_window (Lisp_Object window, int just_this_one_p)
15334 {
15335 struct window *w = XWINDOW (window);
15336 struct frame *f = XFRAME (w->frame);
15337 struct buffer *buffer = XBUFFER (w->buffer);
15338 struct buffer *old = current_buffer;
15339 struct text_pos lpoint, opoint, startp;
15340 int update_mode_line;
15341 int tem;
15342 struct it it;
15343 /* Record it now because it's overwritten. */
15344 int current_matrix_up_to_date_p = 0;
15345 int used_current_matrix_p = 0;
15346 /* This is less strict than current_matrix_up_to_date_p.
15347 It indicates that the buffer contents and narrowing are unchanged. */
15348 int buffer_unchanged_p = 0;
15349 int temp_scroll_step = 0;
15350 ptrdiff_t count = SPECPDL_INDEX ();
15351 int rc;
15352 int centering_position = -1;
15353 int last_line_misfit = 0;
15354 ptrdiff_t beg_unchanged, end_unchanged;
15355
15356 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15357 opoint = lpoint;
15358
15359 /* W must be a leaf window here. */
15360 eassert (!NILP (w->buffer));
15361 #ifdef GLYPH_DEBUG
15362 *w->desired_matrix->method = 0;
15363 #endif
15364
15365 restart:
15366 reconsider_clip_changes (w, buffer);
15367
15368 /* Has the mode line to be updated? */
15369 update_mode_line = (w->update_mode_line
15370 || update_mode_lines
15371 || buffer->clip_changed
15372 || buffer->prevent_redisplay_optimizations_p);
15373
15374 if (MINI_WINDOW_P (w))
15375 {
15376 if (w == XWINDOW (echo_area_window)
15377 && !NILP (echo_area_buffer[0]))
15378 {
15379 if (update_mode_line)
15380 /* We may have to update a tty frame's menu bar or a
15381 tool-bar. Example `M-x C-h C-h C-g'. */
15382 goto finish_menu_bars;
15383 else
15384 /* We've already displayed the echo area glyphs in this window. */
15385 goto finish_scroll_bars;
15386 }
15387 else if ((w != XWINDOW (minibuf_window)
15388 || minibuf_level == 0)
15389 /* When buffer is nonempty, redisplay window normally. */
15390 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15391 /* Quail displays non-mini buffers in minibuffer window.
15392 In that case, redisplay the window normally. */
15393 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15394 {
15395 /* W is a mini-buffer window, but it's not active, so clear
15396 it. */
15397 int yb = window_text_bottom_y (w);
15398 struct glyph_row *row;
15399 int y;
15400
15401 for (y = 0, row = w->desired_matrix->rows;
15402 y < yb;
15403 y += row->height, ++row)
15404 blank_row (w, row, y);
15405 goto finish_scroll_bars;
15406 }
15407
15408 clear_glyph_matrix (w->desired_matrix);
15409 }
15410
15411 /* Otherwise set up data on this window; select its buffer and point
15412 value. */
15413 /* Really select the buffer, for the sake of buffer-local
15414 variables. */
15415 set_buffer_internal_1 (XBUFFER (w->buffer));
15416
15417 current_matrix_up_to_date_p
15418 = (!NILP (w->window_end_valid)
15419 && !current_buffer->clip_changed
15420 && !current_buffer->prevent_redisplay_optimizations_p
15421 && w->last_modified >= MODIFF
15422 && w->last_overlay_modified >= OVERLAY_MODIFF);
15423
15424 /* Run the window-bottom-change-functions
15425 if it is possible that the text on the screen has changed
15426 (either due to modification of the text, or any other reason). */
15427 if (!current_matrix_up_to_date_p
15428 && !NILP (Vwindow_text_change_functions))
15429 {
15430 safe_run_hooks (Qwindow_text_change_functions);
15431 goto restart;
15432 }
15433
15434 beg_unchanged = BEG_UNCHANGED;
15435 end_unchanged = END_UNCHANGED;
15436
15437 SET_TEXT_POS (opoint, PT, PT_BYTE);
15438
15439 specbind (Qinhibit_point_motion_hooks, Qt);
15440
15441 buffer_unchanged_p
15442 = (!NILP (w->window_end_valid)
15443 && !current_buffer->clip_changed
15444 && w->last_modified >= MODIFF
15445 && w->last_overlay_modified >= OVERLAY_MODIFF);
15446
15447 /* When windows_or_buffers_changed is non-zero, we can't rely on
15448 the window end being valid, so set it to nil there. */
15449 if (windows_or_buffers_changed)
15450 {
15451 /* If window starts on a continuation line, maybe adjust the
15452 window start in case the window's width changed. */
15453 if (XMARKER (w->start)->buffer == current_buffer)
15454 compute_window_start_on_continuation_line (w);
15455
15456 w->window_end_valid = Qnil;
15457 }
15458
15459 /* Some sanity checks. */
15460 CHECK_WINDOW_END (w);
15461 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15462 abort ();
15463 if (BYTEPOS (opoint) < CHARPOS (opoint))
15464 abort ();
15465
15466 /* If %c is in mode line, update it if needed. */
15467 if (!NILP (w->column_number_displayed)
15468 /* This alternative quickly identifies a common case
15469 where no change is needed. */
15470 && !(PT == w->last_point
15471 && w->last_modified >= MODIFF
15472 && w->last_overlay_modified >= OVERLAY_MODIFF)
15473 && (XFASTINT (w->column_number_displayed) != current_column ()))
15474 update_mode_line = 1;
15475
15476 /* Count number of windows showing the selected buffer. An indirect
15477 buffer counts as its base buffer. */
15478 if (!just_this_one_p)
15479 {
15480 struct buffer *current_base, *window_base;
15481 current_base = current_buffer;
15482 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15483 if (current_base->base_buffer)
15484 current_base = current_base->base_buffer;
15485 if (window_base->base_buffer)
15486 window_base = window_base->base_buffer;
15487 if (current_base == window_base)
15488 buffer_shared++;
15489 }
15490
15491 /* Point refers normally to the selected window. For any other
15492 window, set up appropriate value. */
15493 if (!EQ (window, selected_window))
15494 {
15495 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15496 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15497 if (new_pt < BEGV)
15498 {
15499 new_pt = BEGV;
15500 new_pt_byte = BEGV_BYTE;
15501 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15502 }
15503 else if (new_pt > (ZV - 1))
15504 {
15505 new_pt = ZV;
15506 new_pt_byte = ZV_BYTE;
15507 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15508 }
15509
15510 /* We don't use SET_PT so that the point-motion hooks don't run. */
15511 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15512 }
15513
15514 /* If any of the character widths specified in the display table
15515 have changed, invalidate the width run cache. It's true that
15516 this may be a bit late to catch such changes, but the rest of
15517 redisplay goes (non-fatally) haywire when the display table is
15518 changed, so why should we worry about doing any better? */
15519 if (current_buffer->width_run_cache)
15520 {
15521 struct Lisp_Char_Table *disptab = buffer_display_table ();
15522
15523 if (! disptab_matches_widthtab (disptab,
15524 XVECTOR (BVAR (current_buffer, width_table))))
15525 {
15526 invalidate_region_cache (current_buffer,
15527 current_buffer->width_run_cache,
15528 BEG, Z);
15529 recompute_width_table (current_buffer, disptab);
15530 }
15531 }
15532
15533 /* If window-start is screwed up, choose a new one. */
15534 if (XMARKER (w->start)->buffer != current_buffer)
15535 goto recenter;
15536
15537 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15538
15539 /* If someone specified a new starting point but did not insist,
15540 check whether it can be used. */
15541 if (w->optional_new_start
15542 && CHARPOS (startp) >= BEGV
15543 && CHARPOS (startp) <= ZV)
15544 {
15545 w->optional_new_start = 0;
15546 start_display (&it, w, startp);
15547 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15548 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15549 if (IT_CHARPOS (it) == PT)
15550 w->force_start = 1;
15551 /* IT may overshoot PT if text at PT is invisible. */
15552 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15553 w->force_start = 1;
15554 }
15555
15556 force_start:
15557
15558 /* Handle case where place to start displaying has been specified,
15559 unless the specified location is outside the accessible range. */
15560 if (w->force_start || w->frozen_window_start_p)
15561 {
15562 /* We set this later on if we have to adjust point. */
15563 int new_vpos = -1;
15564
15565 w->force_start = 0;
15566 w->vscroll = 0;
15567 w->window_end_valid = Qnil;
15568
15569 /* Forget any recorded base line for line number display. */
15570 if (!buffer_unchanged_p)
15571 w->base_line_number = Qnil;
15572
15573 /* Redisplay the mode line. Select the buffer properly for that.
15574 Also, run the hook window-scroll-functions
15575 because we have scrolled. */
15576 /* Note, we do this after clearing force_start because
15577 if there's an error, it is better to forget about force_start
15578 than to get into an infinite loop calling the hook functions
15579 and having them get more errors. */
15580 if (!update_mode_line
15581 || ! NILP (Vwindow_scroll_functions))
15582 {
15583 update_mode_line = 1;
15584 w->update_mode_line = 1;
15585 startp = run_window_scroll_functions (window, startp);
15586 }
15587
15588 w->last_modified = 0;
15589 w->last_overlay_modified = 0;
15590 if (CHARPOS (startp) < BEGV)
15591 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15592 else if (CHARPOS (startp) > ZV)
15593 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15594
15595 /* Redisplay, then check if cursor has been set during the
15596 redisplay. Give up if new fonts were loaded. */
15597 /* We used to issue a CHECK_MARGINS argument to try_window here,
15598 but this causes scrolling to fail when point begins inside
15599 the scroll margin (bug#148) -- cyd */
15600 if (!try_window (window, startp, 0))
15601 {
15602 w->force_start = 1;
15603 clear_glyph_matrix (w->desired_matrix);
15604 goto need_larger_matrices;
15605 }
15606
15607 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15608 {
15609 /* If point does not appear, try to move point so it does
15610 appear. The desired matrix has been built above, so we
15611 can use it here. */
15612 new_vpos = window_box_height (w) / 2;
15613 }
15614
15615 if (!cursor_row_fully_visible_p (w, 0, 0))
15616 {
15617 /* Point does appear, but on a line partly visible at end of window.
15618 Move it back to a fully-visible line. */
15619 new_vpos = window_box_height (w);
15620 }
15621
15622 /* If we need to move point for either of the above reasons,
15623 now actually do it. */
15624 if (new_vpos >= 0)
15625 {
15626 struct glyph_row *row;
15627
15628 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15629 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15630 ++row;
15631
15632 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15633 MATRIX_ROW_START_BYTEPOS (row));
15634
15635 if (w != XWINDOW (selected_window))
15636 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15637 else if (current_buffer == old)
15638 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15639
15640 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15641
15642 /* If we are highlighting the region, then we just changed
15643 the region, so redisplay to show it. */
15644 if (!NILP (Vtransient_mark_mode)
15645 && !NILP (BVAR (current_buffer, mark_active)))
15646 {
15647 clear_glyph_matrix (w->desired_matrix);
15648 if (!try_window (window, startp, 0))
15649 goto need_larger_matrices;
15650 }
15651 }
15652
15653 #ifdef GLYPH_DEBUG
15654 debug_method_add (w, "forced window start");
15655 #endif
15656 goto done;
15657 }
15658
15659 /* Handle case where text has not changed, only point, and it has
15660 not moved off the frame, and we are not retrying after hscroll.
15661 (current_matrix_up_to_date_p is nonzero when retrying.) */
15662 if (current_matrix_up_to_date_p
15663 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15664 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15665 {
15666 switch (rc)
15667 {
15668 case CURSOR_MOVEMENT_SUCCESS:
15669 used_current_matrix_p = 1;
15670 goto done;
15671
15672 case CURSOR_MOVEMENT_MUST_SCROLL:
15673 goto try_to_scroll;
15674
15675 default:
15676 abort ();
15677 }
15678 }
15679 /* If current starting point was originally the beginning of a line
15680 but no longer is, find a new starting point. */
15681 else if (w->start_at_line_beg
15682 && !(CHARPOS (startp) <= BEGV
15683 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15684 {
15685 #ifdef GLYPH_DEBUG
15686 debug_method_add (w, "recenter 1");
15687 #endif
15688 goto recenter;
15689 }
15690
15691 /* Try scrolling with try_window_id. Value is > 0 if update has
15692 been done, it is -1 if we know that the same window start will
15693 not work. It is 0 if unsuccessful for some other reason. */
15694 else if ((tem = try_window_id (w)) != 0)
15695 {
15696 #ifdef GLYPH_DEBUG
15697 debug_method_add (w, "try_window_id %d", tem);
15698 #endif
15699
15700 if (fonts_changed_p)
15701 goto need_larger_matrices;
15702 if (tem > 0)
15703 goto done;
15704
15705 /* Otherwise try_window_id has returned -1 which means that we
15706 don't want the alternative below this comment to execute. */
15707 }
15708 else if (CHARPOS (startp) >= BEGV
15709 && CHARPOS (startp) <= ZV
15710 && PT >= CHARPOS (startp)
15711 && (CHARPOS (startp) < ZV
15712 /* Avoid starting at end of buffer. */
15713 || CHARPOS (startp) == BEGV
15714 || (w->last_modified >= MODIFF
15715 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15716 {
15717 int d1, d2, d3, d4, d5, d6;
15718
15719 /* If first window line is a continuation line, and window start
15720 is inside the modified region, but the first change is before
15721 current window start, we must select a new window start.
15722
15723 However, if this is the result of a down-mouse event (e.g. by
15724 extending the mouse-drag-overlay), we don't want to select a
15725 new window start, since that would change the position under
15726 the mouse, resulting in an unwanted mouse-movement rather
15727 than a simple mouse-click. */
15728 if (!w->start_at_line_beg
15729 && NILP (do_mouse_tracking)
15730 && CHARPOS (startp) > BEGV
15731 && CHARPOS (startp) > BEG + beg_unchanged
15732 && CHARPOS (startp) <= Z - end_unchanged
15733 /* Even if w->start_at_line_beg is nil, a new window may
15734 start at a line_beg, since that's how set_buffer_window
15735 sets it. So, we need to check the return value of
15736 compute_window_start_on_continuation_line. (See also
15737 bug#197). */
15738 && XMARKER (w->start)->buffer == current_buffer
15739 && compute_window_start_on_continuation_line (w)
15740 /* It doesn't make sense to force the window start like we
15741 do at label force_start if it is already known that point
15742 will not be visible in the resulting window, because
15743 doing so will move point from its correct position
15744 instead of scrolling the window to bring point into view.
15745 See bug#9324. */
15746 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15747 {
15748 w->force_start = 1;
15749 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15750 goto force_start;
15751 }
15752
15753 #ifdef GLYPH_DEBUG
15754 debug_method_add (w, "same window start");
15755 #endif
15756
15757 /* Try to redisplay starting at same place as before.
15758 If point has not moved off frame, accept the results. */
15759 if (!current_matrix_up_to_date_p
15760 /* Don't use try_window_reusing_current_matrix in this case
15761 because a window scroll function can have changed the
15762 buffer. */
15763 || !NILP (Vwindow_scroll_functions)
15764 || MINI_WINDOW_P (w)
15765 || !(used_current_matrix_p
15766 = try_window_reusing_current_matrix (w)))
15767 {
15768 IF_DEBUG (debug_method_add (w, "1"));
15769 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15770 /* -1 means we need to scroll.
15771 0 means we need new matrices, but fonts_changed_p
15772 is set in that case, so we will detect it below. */
15773 goto try_to_scroll;
15774 }
15775
15776 if (fonts_changed_p)
15777 goto need_larger_matrices;
15778
15779 if (w->cursor.vpos >= 0)
15780 {
15781 if (!just_this_one_p
15782 || current_buffer->clip_changed
15783 || BEG_UNCHANGED < CHARPOS (startp))
15784 /* Forget any recorded base line for line number display. */
15785 w->base_line_number = Qnil;
15786
15787 if (!cursor_row_fully_visible_p (w, 1, 0))
15788 {
15789 clear_glyph_matrix (w->desired_matrix);
15790 last_line_misfit = 1;
15791 }
15792 /* Drop through and scroll. */
15793 else
15794 goto done;
15795 }
15796 else
15797 clear_glyph_matrix (w->desired_matrix);
15798 }
15799
15800 try_to_scroll:
15801
15802 w->last_modified = 0;
15803 w->last_overlay_modified = 0;
15804
15805 /* Redisplay the mode line. Select the buffer properly for that. */
15806 if (!update_mode_line)
15807 {
15808 update_mode_line = 1;
15809 w->update_mode_line = 1;
15810 }
15811
15812 /* Try to scroll by specified few lines. */
15813 if ((scroll_conservatively
15814 || emacs_scroll_step
15815 || temp_scroll_step
15816 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15817 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15818 && CHARPOS (startp) >= BEGV
15819 && CHARPOS (startp) <= ZV)
15820 {
15821 /* The function returns -1 if new fonts were loaded, 1 if
15822 successful, 0 if not successful. */
15823 int ss = try_scrolling (window, just_this_one_p,
15824 scroll_conservatively,
15825 emacs_scroll_step,
15826 temp_scroll_step, last_line_misfit);
15827 switch (ss)
15828 {
15829 case SCROLLING_SUCCESS:
15830 goto done;
15831
15832 case SCROLLING_NEED_LARGER_MATRICES:
15833 goto need_larger_matrices;
15834
15835 case SCROLLING_FAILED:
15836 break;
15837
15838 default:
15839 abort ();
15840 }
15841 }
15842
15843 /* Finally, just choose a place to start which positions point
15844 according to user preferences. */
15845
15846 recenter:
15847
15848 #ifdef GLYPH_DEBUG
15849 debug_method_add (w, "recenter");
15850 #endif
15851
15852 /* w->vscroll = 0; */
15853
15854 /* Forget any previously recorded base line for line number display. */
15855 if (!buffer_unchanged_p)
15856 w->base_line_number = Qnil;
15857
15858 /* Determine the window start relative to point. */
15859 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15860 it.current_y = it.last_visible_y;
15861 if (centering_position < 0)
15862 {
15863 int margin =
15864 scroll_margin > 0
15865 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15866 : 0;
15867 ptrdiff_t margin_pos = CHARPOS (startp);
15868 Lisp_Object aggressive;
15869 int scrolling_up;
15870
15871 /* If there is a scroll margin at the top of the window, find
15872 its character position. */
15873 if (margin
15874 /* Cannot call start_display if startp is not in the
15875 accessible region of the buffer. This can happen when we
15876 have just switched to a different buffer and/or changed
15877 its restriction. In that case, startp is initialized to
15878 the character position 1 (BEGV) because we did not yet
15879 have chance to display the buffer even once. */
15880 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15881 {
15882 struct it it1;
15883 void *it1data = NULL;
15884
15885 SAVE_IT (it1, it, it1data);
15886 start_display (&it1, w, startp);
15887 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15888 margin_pos = IT_CHARPOS (it1);
15889 RESTORE_IT (&it, &it, it1data);
15890 }
15891 scrolling_up = PT > margin_pos;
15892 aggressive =
15893 scrolling_up
15894 ? BVAR (current_buffer, scroll_up_aggressively)
15895 : BVAR (current_buffer, scroll_down_aggressively);
15896
15897 if (!MINI_WINDOW_P (w)
15898 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15899 {
15900 int pt_offset = 0;
15901
15902 /* Setting scroll-conservatively overrides
15903 scroll-*-aggressively. */
15904 if (!scroll_conservatively && NUMBERP (aggressive))
15905 {
15906 double float_amount = XFLOATINT (aggressive);
15907
15908 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15909 if (pt_offset == 0 && float_amount > 0)
15910 pt_offset = 1;
15911 if (pt_offset && margin > 0)
15912 margin -= 1;
15913 }
15914 /* Compute how much to move the window start backward from
15915 point so that point will be displayed where the user
15916 wants it. */
15917 if (scrolling_up)
15918 {
15919 centering_position = it.last_visible_y;
15920 if (pt_offset)
15921 centering_position -= pt_offset;
15922 centering_position -=
15923 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15924 + WINDOW_HEADER_LINE_HEIGHT (w);
15925 /* Don't let point enter the scroll margin near top of
15926 the window. */
15927 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15928 centering_position = margin * FRAME_LINE_HEIGHT (f);
15929 }
15930 else
15931 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15932 }
15933 else
15934 /* Set the window start half the height of the window backward
15935 from point. */
15936 centering_position = window_box_height (w) / 2;
15937 }
15938 move_it_vertically_backward (&it, centering_position);
15939
15940 eassert (IT_CHARPOS (it) >= BEGV);
15941
15942 /* The function move_it_vertically_backward may move over more
15943 than the specified y-distance. If it->w is small, e.g. a
15944 mini-buffer window, we may end up in front of the window's
15945 display area. Start displaying at the start of the line
15946 containing PT in this case. */
15947 if (it.current_y <= 0)
15948 {
15949 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15950 move_it_vertically_backward (&it, 0);
15951 it.current_y = 0;
15952 }
15953
15954 it.current_x = it.hpos = 0;
15955
15956 /* Set the window start position here explicitly, to avoid an
15957 infinite loop in case the functions in window-scroll-functions
15958 get errors. */
15959 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15960
15961 /* Run scroll hooks. */
15962 startp = run_window_scroll_functions (window, it.current.pos);
15963
15964 /* Redisplay the window. */
15965 if (!current_matrix_up_to_date_p
15966 || windows_or_buffers_changed
15967 || cursor_type_changed
15968 /* Don't use try_window_reusing_current_matrix in this case
15969 because it can have changed the buffer. */
15970 || !NILP (Vwindow_scroll_functions)
15971 || !just_this_one_p
15972 || MINI_WINDOW_P (w)
15973 || !(used_current_matrix_p
15974 = try_window_reusing_current_matrix (w)))
15975 try_window (window, startp, 0);
15976
15977 /* If new fonts have been loaded (due to fontsets), give up. We
15978 have to start a new redisplay since we need to re-adjust glyph
15979 matrices. */
15980 if (fonts_changed_p)
15981 goto need_larger_matrices;
15982
15983 /* If cursor did not appear assume that the middle of the window is
15984 in the first line of the window. Do it again with the next line.
15985 (Imagine a window of height 100, displaying two lines of height
15986 60. Moving back 50 from it->last_visible_y will end in the first
15987 line.) */
15988 if (w->cursor.vpos < 0)
15989 {
15990 if (!NILP (w->window_end_valid)
15991 && PT >= Z - XFASTINT (w->window_end_pos))
15992 {
15993 clear_glyph_matrix (w->desired_matrix);
15994 move_it_by_lines (&it, 1);
15995 try_window (window, it.current.pos, 0);
15996 }
15997 else if (PT < IT_CHARPOS (it))
15998 {
15999 clear_glyph_matrix (w->desired_matrix);
16000 move_it_by_lines (&it, -1);
16001 try_window (window, it.current.pos, 0);
16002 }
16003 else
16004 {
16005 /* Not much we can do about it. */
16006 }
16007 }
16008
16009 /* Consider the following case: Window starts at BEGV, there is
16010 invisible, intangible text at BEGV, so that display starts at
16011 some point START > BEGV. It can happen that we are called with
16012 PT somewhere between BEGV and START. Try to handle that case. */
16013 if (w->cursor.vpos < 0)
16014 {
16015 struct glyph_row *row = w->current_matrix->rows;
16016 if (row->mode_line_p)
16017 ++row;
16018 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16019 }
16020
16021 if (!cursor_row_fully_visible_p (w, 0, 0))
16022 {
16023 /* If vscroll is enabled, disable it and try again. */
16024 if (w->vscroll)
16025 {
16026 w->vscroll = 0;
16027 clear_glyph_matrix (w->desired_matrix);
16028 goto recenter;
16029 }
16030
16031 /* Users who set scroll-conservatively to a large number want
16032 point just above/below the scroll margin. If we ended up
16033 with point's row partially visible, move the window start to
16034 make that row fully visible and out of the margin. */
16035 if (scroll_conservatively > SCROLL_LIMIT)
16036 {
16037 int margin =
16038 scroll_margin > 0
16039 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16040 : 0;
16041 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16042
16043 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16044 clear_glyph_matrix (w->desired_matrix);
16045 if (1 == try_window (window, it.current.pos,
16046 TRY_WINDOW_CHECK_MARGINS))
16047 goto done;
16048 }
16049
16050 /* If centering point failed to make the whole line visible,
16051 put point at the top instead. That has to make the whole line
16052 visible, if it can be done. */
16053 if (centering_position == 0)
16054 goto done;
16055
16056 clear_glyph_matrix (w->desired_matrix);
16057 centering_position = 0;
16058 goto recenter;
16059 }
16060
16061 done:
16062
16063 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16064 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16065 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16066
16067 /* Display the mode line, if we must. */
16068 if ((update_mode_line
16069 /* If window not full width, must redo its mode line
16070 if (a) the window to its side is being redone and
16071 (b) we do a frame-based redisplay. This is a consequence
16072 of how inverted lines are drawn in frame-based redisplay. */
16073 || (!just_this_one_p
16074 && !FRAME_WINDOW_P (f)
16075 && !WINDOW_FULL_WIDTH_P (w))
16076 /* Line number to display. */
16077 || INTEGERP (w->base_line_pos)
16078 /* Column number is displayed and different from the one displayed. */
16079 || (!NILP (w->column_number_displayed)
16080 && (XFASTINT (w->column_number_displayed) != current_column ())))
16081 /* This means that the window has a mode line. */
16082 && (WINDOW_WANTS_MODELINE_P (w)
16083 || WINDOW_WANTS_HEADER_LINE_P (w)))
16084 {
16085 display_mode_lines (w);
16086
16087 /* If mode line height has changed, arrange for a thorough
16088 immediate redisplay using the correct mode line height. */
16089 if (WINDOW_WANTS_MODELINE_P (w)
16090 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16091 {
16092 fonts_changed_p = 1;
16093 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16094 = DESIRED_MODE_LINE_HEIGHT (w);
16095 }
16096
16097 /* If header line height has changed, arrange for a thorough
16098 immediate redisplay using the correct header line height. */
16099 if (WINDOW_WANTS_HEADER_LINE_P (w)
16100 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16101 {
16102 fonts_changed_p = 1;
16103 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16104 = DESIRED_HEADER_LINE_HEIGHT (w);
16105 }
16106
16107 if (fonts_changed_p)
16108 goto need_larger_matrices;
16109 }
16110
16111 if (!line_number_displayed
16112 && !BUFFERP (w->base_line_pos))
16113 {
16114 w->base_line_pos = Qnil;
16115 w->base_line_number = Qnil;
16116 }
16117
16118 finish_menu_bars:
16119
16120 /* When we reach a frame's selected window, redo the frame's menu bar. */
16121 if (update_mode_line
16122 && EQ (FRAME_SELECTED_WINDOW (f), window))
16123 {
16124 int redisplay_menu_p = 0;
16125
16126 if (FRAME_WINDOW_P (f))
16127 {
16128 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16129 || defined (HAVE_NS) || defined (USE_GTK)
16130 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16131 #else
16132 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16133 #endif
16134 }
16135 else
16136 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16137
16138 if (redisplay_menu_p)
16139 display_menu_bar (w);
16140
16141 #ifdef HAVE_WINDOW_SYSTEM
16142 if (FRAME_WINDOW_P (f))
16143 {
16144 #if defined (USE_GTK) || defined (HAVE_NS)
16145 if (FRAME_EXTERNAL_TOOL_BAR (f))
16146 redisplay_tool_bar (f);
16147 #else
16148 if (WINDOWP (f->tool_bar_window)
16149 && (FRAME_TOOL_BAR_LINES (f) > 0
16150 || !NILP (Vauto_resize_tool_bars))
16151 && redisplay_tool_bar (f))
16152 ignore_mouse_drag_p = 1;
16153 #endif
16154 }
16155 #endif
16156 }
16157
16158 #ifdef HAVE_WINDOW_SYSTEM
16159 if (FRAME_WINDOW_P (f)
16160 && update_window_fringes (w, (just_this_one_p
16161 || (!used_current_matrix_p && !overlay_arrow_seen)
16162 || w->pseudo_window_p)))
16163 {
16164 update_begin (f);
16165 BLOCK_INPUT;
16166 if (draw_window_fringes (w, 1))
16167 x_draw_vertical_border (w);
16168 UNBLOCK_INPUT;
16169 update_end (f);
16170 }
16171 #endif /* HAVE_WINDOW_SYSTEM */
16172
16173 /* We go to this label, with fonts_changed_p nonzero,
16174 if it is necessary to try again using larger glyph matrices.
16175 We have to redeem the scroll bar even in this case,
16176 because the loop in redisplay_internal expects that. */
16177 need_larger_matrices:
16178 ;
16179 finish_scroll_bars:
16180
16181 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16182 {
16183 /* Set the thumb's position and size. */
16184 set_vertical_scroll_bar (w);
16185
16186 /* Note that we actually used the scroll bar attached to this
16187 window, so it shouldn't be deleted at the end of redisplay. */
16188 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16189 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16190 }
16191
16192 /* Restore current_buffer and value of point in it. The window
16193 update may have changed the buffer, so first make sure `opoint'
16194 is still valid (Bug#6177). */
16195 if (CHARPOS (opoint) < BEGV)
16196 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16197 else if (CHARPOS (opoint) > ZV)
16198 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16199 else
16200 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16201
16202 set_buffer_internal_1 (old);
16203 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16204 shorter. This can be caused by log truncation in *Messages*. */
16205 if (CHARPOS (lpoint) <= ZV)
16206 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16207
16208 unbind_to (count, Qnil);
16209 }
16210
16211
16212 /* Build the complete desired matrix of WINDOW with a window start
16213 buffer position POS.
16214
16215 Value is 1 if successful. It is zero if fonts were loaded during
16216 redisplay which makes re-adjusting glyph matrices necessary, and -1
16217 if point would appear in the scroll margins.
16218 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16219 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16220 set in FLAGS.) */
16221
16222 int
16223 try_window (Lisp_Object window, struct text_pos pos, int flags)
16224 {
16225 struct window *w = XWINDOW (window);
16226 struct it it;
16227 struct glyph_row *last_text_row = NULL;
16228 struct frame *f = XFRAME (w->frame);
16229
16230 /* Make POS the new window start. */
16231 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16232
16233 /* Mark cursor position as unknown. No overlay arrow seen. */
16234 w->cursor.vpos = -1;
16235 overlay_arrow_seen = 0;
16236
16237 /* Initialize iterator and info to start at POS. */
16238 start_display (&it, w, pos);
16239
16240 /* Display all lines of W. */
16241 while (it.current_y < it.last_visible_y)
16242 {
16243 if (display_line (&it))
16244 last_text_row = it.glyph_row - 1;
16245 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16246 return 0;
16247 }
16248
16249 /* Don't let the cursor end in the scroll margins. */
16250 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16251 && !MINI_WINDOW_P (w))
16252 {
16253 int this_scroll_margin;
16254
16255 if (scroll_margin > 0)
16256 {
16257 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16258 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16259 }
16260 else
16261 this_scroll_margin = 0;
16262
16263 if ((w->cursor.y >= 0 /* not vscrolled */
16264 && w->cursor.y < this_scroll_margin
16265 && CHARPOS (pos) > BEGV
16266 && IT_CHARPOS (it) < ZV)
16267 /* rms: considering make_cursor_line_fully_visible_p here
16268 seems to give wrong results. We don't want to recenter
16269 when the last line is partly visible, we want to allow
16270 that case to be handled in the usual way. */
16271 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16272 {
16273 w->cursor.vpos = -1;
16274 clear_glyph_matrix (w->desired_matrix);
16275 return -1;
16276 }
16277 }
16278
16279 /* If bottom moved off end of frame, change mode line percentage. */
16280 if (XFASTINT (w->window_end_pos) <= 0
16281 && Z != IT_CHARPOS (it))
16282 w->update_mode_line = 1;
16283
16284 /* Set window_end_pos to the offset of the last character displayed
16285 on the window from the end of current_buffer. Set
16286 window_end_vpos to its row number. */
16287 if (last_text_row)
16288 {
16289 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16290 w->window_end_bytepos
16291 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16292 w->window_end_pos
16293 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16294 w->window_end_vpos
16295 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16296 eassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16297 ->displays_text_p);
16298 }
16299 else
16300 {
16301 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16302 w->window_end_pos = make_number (Z - ZV);
16303 w->window_end_vpos = make_number (0);
16304 }
16305
16306 /* But that is not valid info until redisplay finishes. */
16307 w->window_end_valid = Qnil;
16308 return 1;
16309 }
16310
16311
16312 \f
16313 /************************************************************************
16314 Window redisplay reusing current matrix when buffer has not changed
16315 ************************************************************************/
16316
16317 /* Try redisplay of window W showing an unchanged buffer with a
16318 different window start than the last time it was displayed by
16319 reusing its current matrix. Value is non-zero if successful.
16320 W->start is the new window start. */
16321
16322 static int
16323 try_window_reusing_current_matrix (struct window *w)
16324 {
16325 struct frame *f = XFRAME (w->frame);
16326 struct glyph_row *bottom_row;
16327 struct it it;
16328 struct run run;
16329 struct text_pos start, new_start;
16330 int nrows_scrolled, i;
16331 struct glyph_row *last_text_row;
16332 struct glyph_row *last_reused_text_row;
16333 struct glyph_row *start_row;
16334 int start_vpos, min_y, max_y;
16335
16336 #ifdef GLYPH_DEBUG
16337 if (inhibit_try_window_reusing)
16338 return 0;
16339 #endif
16340
16341 if (/* This function doesn't handle terminal frames. */
16342 !FRAME_WINDOW_P (f)
16343 /* Don't try to reuse the display if windows have been split
16344 or such. */
16345 || windows_or_buffers_changed
16346 || cursor_type_changed)
16347 return 0;
16348
16349 /* Can't do this if region may have changed. */
16350 if ((!NILP (Vtransient_mark_mode)
16351 && !NILP (BVAR (current_buffer, mark_active)))
16352 || !NILP (w->region_showing)
16353 || !NILP (Vshow_trailing_whitespace))
16354 return 0;
16355
16356 /* If top-line visibility has changed, give up. */
16357 if (WINDOW_WANTS_HEADER_LINE_P (w)
16358 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16359 return 0;
16360
16361 /* Give up if old or new display is scrolled vertically. We could
16362 make this function handle this, but right now it doesn't. */
16363 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16364 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16365 return 0;
16366
16367 /* The variable new_start now holds the new window start. The old
16368 start `start' can be determined from the current matrix. */
16369 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16370 start = start_row->minpos;
16371 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16372
16373 /* Clear the desired matrix for the display below. */
16374 clear_glyph_matrix (w->desired_matrix);
16375
16376 if (CHARPOS (new_start) <= CHARPOS (start))
16377 {
16378 /* Don't use this method if the display starts with an ellipsis
16379 displayed for invisible text. It's not easy to handle that case
16380 below, and it's certainly not worth the effort since this is
16381 not a frequent case. */
16382 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16383 return 0;
16384
16385 IF_DEBUG (debug_method_add (w, "twu1"));
16386
16387 /* Display up to a row that can be reused. The variable
16388 last_text_row is set to the last row displayed that displays
16389 text. Note that it.vpos == 0 if or if not there is a
16390 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16391 start_display (&it, w, new_start);
16392 w->cursor.vpos = -1;
16393 last_text_row = last_reused_text_row = NULL;
16394
16395 while (it.current_y < it.last_visible_y
16396 && !fonts_changed_p)
16397 {
16398 /* If we have reached into the characters in the START row,
16399 that means the line boundaries have changed. So we
16400 can't start copying with the row START. Maybe it will
16401 work to start copying with the following row. */
16402 while (IT_CHARPOS (it) > CHARPOS (start))
16403 {
16404 /* Advance to the next row as the "start". */
16405 start_row++;
16406 start = start_row->minpos;
16407 /* If there are no more rows to try, or just one, give up. */
16408 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16409 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16410 || CHARPOS (start) == ZV)
16411 {
16412 clear_glyph_matrix (w->desired_matrix);
16413 return 0;
16414 }
16415
16416 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16417 }
16418 /* If we have reached alignment, we can copy the rest of the
16419 rows. */
16420 if (IT_CHARPOS (it) == CHARPOS (start)
16421 /* Don't accept "alignment" inside a display vector,
16422 since start_row could have started in the middle of
16423 that same display vector (thus their character
16424 positions match), and we have no way of telling if
16425 that is the case. */
16426 && it.current.dpvec_index < 0)
16427 break;
16428
16429 if (display_line (&it))
16430 last_text_row = it.glyph_row - 1;
16431
16432 }
16433
16434 /* A value of current_y < last_visible_y means that we stopped
16435 at the previous window start, which in turn means that we
16436 have at least one reusable row. */
16437 if (it.current_y < it.last_visible_y)
16438 {
16439 struct glyph_row *row;
16440
16441 /* IT.vpos always starts from 0; it counts text lines. */
16442 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16443
16444 /* Find PT if not already found in the lines displayed. */
16445 if (w->cursor.vpos < 0)
16446 {
16447 int dy = it.current_y - start_row->y;
16448
16449 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16450 row = row_containing_pos (w, PT, row, NULL, dy);
16451 if (row)
16452 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16453 dy, nrows_scrolled);
16454 else
16455 {
16456 clear_glyph_matrix (w->desired_matrix);
16457 return 0;
16458 }
16459 }
16460
16461 /* Scroll the display. Do it before the current matrix is
16462 changed. The problem here is that update has not yet
16463 run, i.e. part of the current matrix is not up to date.
16464 scroll_run_hook will clear the cursor, and use the
16465 current matrix to get the height of the row the cursor is
16466 in. */
16467 run.current_y = start_row->y;
16468 run.desired_y = it.current_y;
16469 run.height = it.last_visible_y - it.current_y;
16470
16471 if (run.height > 0 && run.current_y != run.desired_y)
16472 {
16473 update_begin (f);
16474 FRAME_RIF (f)->update_window_begin_hook (w);
16475 FRAME_RIF (f)->clear_window_mouse_face (w);
16476 FRAME_RIF (f)->scroll_run_hook (w, &run);
16477 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16478 update_end (f);
16479 }
16480
16481 /* Shift current matrix down by nrows_scrolled lines. */
16482 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16483 rotate_matrix (w->current_matrix,
16484 start_vpos,
16485 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16486 nrows_scrolled);
16487
16488 /* Disable lines that must be updated. */
16489 for (i = 0; i < nrows_scrolled; ++i)
16490 (start_row + i)->enabled_p = 0;
16491
16492 /* Re-compute Y positions. */
16493 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16494 max_y = it.last_visible_y;
16495 for (row = start_row + nrows_scrolled;
16496 row < bottom_row;
16497 ++row)
16498 {
16499 row->y = it.current_y;
16500 row->visible_height = row->height;
16501
16502 if (row->y < min_y)
16503 row->visible_height -= min_y - row->y;
16504 if (row->y + row->height > max_y)
16505 row->visible_height -= row->y + row->height - max_y;
16506 if (row->fringe_bitmap_periodic_p)
16507 row->redraw_fringe_bitmaps_p = 1;
16508
16509 it.current_y += row->height;
16510
16511 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16512 last_reused_text_row = row;
16513 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16514 break;
16515 }
16516
16517 /* Disable lines in the current matrix which are now
16518 below the window. */
16519 for (++row; row < bottom_row; ++row)
16520 row->enabled_p = row->mode_line_p = 0;
16521 }
16522
16523 /* Update window_end_pos etc.; last_reused_text_row is the last
16524 reused row from the current matrix containing text, if any.
16525 The value of last_text_row is the last displayed line
16526 containing text. */
16527 if (last_reused_text_row)
16528 {
16529 w->window_end_bytepos
16530 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16531 w->window_end_pos
16532 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16533 w->window_end_vpos
16534 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16535 w->current_matrix));
16536 }
16537 else if (last_text_row)
16538 {
16539 w->window_end_bytepos
16540 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16541 w->window_end_pos
16542 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16543 w->window_end_vpos
16544 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16545 }
16546 else
16547 {
16548 /* This window must be completely empty. */
16549 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16550 w->window_end_pos = make_number (Z - ZV);
16551 w->window_end_vpos = make_number (0);
16552 }
16553 w->window_end_valid = Qnil;
16554
16555 /* Update hint: don't try scrolling again in update_window. */
16556 w->desired_matrix->no_scrolling_p = 1;
16557
16558 #ifdef GLYPH_DEBUG
16559 debug_method_add (w, "try_window_reusing_current_matrix 1");
16560 #endif
16561 return 1;
16562 }
16563 else if (CHARPOS (new_start) > CHARPOS (start))
16564 {
16565 struct glyph_row *pt_row, *row;
16566 struct glyph_row *first_reusable_row;
16567 struct glyph_row *first_row_to_display;
16568 int dy;
16569 int yb = window_text_bottom_y (w);
16570
16571 /* Find the row starting at new_start, if there is one. Don't
16572 reuse a partially visible line at the end. */
16573 first_reusable_row = start_row;
16574 while (first_reusable_row->enabled_p
16575 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16576 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16577 < CHARPOS (new_start)))
16578 ++first_reusable_row;
16579
16580 /* Give up if there is no row to reuse. */
16581 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16582 || !first_reusable_row->enabled_p
16583 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16584 != CHARPOS (new_start)))
16585 return 0;
16586
16587 /* We can reuse fully visible rows beginning with
16588 first_reusable_row to the end of the window. Set
16589 first_row_to_display to the first row that cannot be reused.
16590 Set pt_row to the row containing point, if there is any. */
16591 pt_row = NULL;
16592 for (first_row_to_display = first_reusable_row;
16593 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16594 ++first_row_to_display)
16595 {
16596 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16597 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16598 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16599 && first_row_to_display->ends_at_zv_p
16600 && pt_row == NULL)))
16601 pt_row = first_row_to_display;
16602 }
16603
16604 /* Start displaying at the start of first_row_to_display. */
16605 eassert (first_row_to_display->y < yb);
16606 init_to_row_start (&it, w, first_row_to_display);
16607
16608 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16609 - start_vpos);
16610 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16611 - nrows_scrolled);
16612 it.current_y = (first_row_to_display->y - first_reusable_row->y
16613 + WINDOW_HEADER_LINE_HEIGHT (w));
16614
16615 /* Display lines beginning with first_row_to_display in the
16616 desired matrix. Set last_text_row to the last row displayed
16617 that displays text. */
16618 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16619 if (pt_row == NULL)
16620 w->cursor.vpos = -1;
16621 last_text_row = NULL;
16622 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16623 if (display_line (&it))
16624 last_text_row = it.glyph_row - 1;
16625
16626 /* If point is in a reused row, adjust y and vpos of the cursor
16627 position. */
16628 if (pt_row)
16629 {
16630 w->cursor.vpos -= nrows_scrolled;
16631 w->cursor.y -= first_reusable_row->y - start_row->y;
16632 }
16633
16634 /* Give up if point isn't in a row displayed or reused. (This
16635 also handles the case where w->cursor.vpos < nrows_scrolled
16636 after the calls to display_line, which can happen with scroll
16637 margins. See bug#1295.) */
16638 if (w->cursor.vpos < 0)
16639 {
16640 clear_glyph_matrix (w->desired_matrix);
16641 return 0;
16642 }
16643
16644 /* Scroll the display. */
16645 run.current_y = first_reusable_row->y;
16646 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16647 run.height = it.last_visible_y - run.current_y;
16648 dy = run.current_y - run.desired_y;
16649
16650 if (run.height)
16651 {
16652 update_begin (f);
16653 FRAME_RIF (f)->update_window_begin_hook (w);
16654 FRAME_RIF (f)->clear_window_mouse_face (w);
16655 FRAME_RIF (f)->scroll_run_hook (w, &run);
16656 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16657 update_end (f);
16658 }
16659
16660 /* Adjust Y positions of reused rows. */
16661 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16662 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16663 max_y = it.last_visible_y;
16664 for (row = first_reusable_row; row < first_row_to_display; ++row)
16665 {
16666 row->y -= dy;
16667 row->visible_height = row->height;
16668 if (row->y < min_y)
16669 row->visible_height -= min_y - row->y;
16670 if (row->y + row->height > max_y)
16671 row->visible_height -= row->y + row->height - max_y;
16672 if (row->fringe_bitmap_periodic_p)
16673 row->redraw_fringe_bitmaps_p = 1;
16674 }
16675
16676 /* Scroll the current matrix. */
16677 eassert (nrows_scrolled > 0);
16678 rotate_matrix (w->current_matrix,
16679 start_vpos,
16680 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16681 -nrows_scrolled);
16682
16683 /* Disable rows not reused. */
16684 for (row -= nrows_scrolled; row < bottom_row; ++row)
16685 row->enabled_p = 0;
16686
16687 /* Point may have moved to a different line, so we cannot assume that
16688 the previous cursor position is valid; locate the correct row. */
16689 if (pt_row)
16690 {
16691 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16692 row < bottom_row
16693 && PT >= MATRIX_ROW_END_CHARPOS (row)
16694 && !row->ends_at_zv_p;
16695 row++)
16696 {
16697 w->cursor.vpos++;
16698 w->cursor.y = row->y;
16699 }
16700 if (row < bottom_row)
16701 {
16702 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16703 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16704
16705 /* Can't use this optimization with bidi-reordered glyph
16706 rows, unless cursor is already at point. */
16707 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16708 {
16709 if (!(w->cursor.hpos >= 0
16710 && w->cursor.hpos < row->used[TEXT_AREA]
16711 && BUFFERP (glyph->object)
16712 && glyph->charpos == PT))
16713 return 0;
16714 }
16715 else
16716 for (; glyph < end
16717 && (!BUFFERP (glyph->object)
16718 || glyph->charpos < PT);
16719 glyph++)
16720 {
16721 w->cursor.hpos++;
16722 w->cursor.x += glyph->pixel_width;
16723 }
16724 }
16725 }
16726
16727 /* Adjust window end. A null value of last_text_row means that
16728 the window end is in reused rows which in turn means that
16729 only its vpos can have changed. */
16730 if (last_text_row)
16731 {
16732 w->window_end_bytepos
16733 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16734 w->window_end_pos
16735 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16736 w->window_end_vpos
16737 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16738 }
16739 else
16740 {
16741 w->window_end_vpos
16742 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16743 }
16744
16745 w->window_end_valid = Qnil;
16746 w->desired_matrix->no_scrolling_p = 1;
16747
16748 #ifdef GLYPH_DEBUG
16749 debug_method_add (w, "try_window_reusing_current_matrix 2");
16750 #endif
16751 return 1;
16752 }
16753
16754 return 0;
16755 }
16756
16757
16758 \f
16759 /************************************************************************
16760 Window redisplay reusing current matrix when buffer has changed
16761 ************************************************************************/
16762
16763 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16764 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16765 ptrdiff_t *, ptrdiff_t *);
16766 static struct glyph_row *
16767 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16768 struct glyph_row *);
16769
16770
16771 /* Return the last row in MATRIX displaying text. If row START is
16772 non-null, start searching with that row. IT gives the dimensions
16773 of the display. Value is null if matrix is empty; otherwise it is
16774 a pointer to the row found. */
16775
16776 static struct glyph_row *
16777 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16778 struct glyph_row *start)
16779 {
16780 struct glyph_row *row, *row_found;
16781
16782 /* Set row_found to the last row in IT->w's current matrix
16783 displaying text. The loop looks funny but think of partially
16784 visible lines. */
16785 row_found = NULL;
16786 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16787 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16788 {
16789 eassert (row->enabled_p);
16790 row_found = row;
16791 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16792 break;
16793 ++row;
16794 }
16795
16796 return row_found;
16797 }
16798
16799
16800 /* Return the last row in the current matrix of W that is not affected
16801 by changes at the start of current_buffer that occurred since W's
16802 current matrix was built. Value is null if no such row exists.
16803
16804 BEG_UNCHANGED us the number of characters unchanged at the start of
16805 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16806 first changed character in current_buffer. Characters at positions <
16807 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16808 when the current matrix was built. */
16809
16810 static struct glyph_row *
16811 find_last_unchanged_at_beg_row (struct window *w)
16812 {
16813 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16814 struct glyph_row *row;
16815 struct glyph_row *row_found = NULL;
16816 int yb = window_text_bottom_y (w);
16817
16818 /* Find the last row displaying unchanged text. */
16819 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16820 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16821 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16822 ++row)
16823 {
16824 if (/* If row ends before first_changed_pos, it is unchanged,
16825 except in some case. */
16826 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16827 /* When row ends in ZV and we write at ZV it is not
16828 unchanged. */
16829 && !row->ends_at_zv_p
16830 /* When first_changed_pos is the end of a continued line,
16831 row is not unchanged because it may be no longer
16832 continued. */
16833 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16834 && (row->continued_p
16835 || row->exact_window_width_line_p))
16836 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16837 needs to be recomputed, so don't consider this row as
16838 unchanged. This happens when the last line was
16839 bidi-reordered and was killed immediately before this
16840 redisplay cycle. In that case, ROW->end stores the
16841 buffer position of the first visual-order character of
16842 the killed text, which is now beyond ZV. */
16843 && CHARPOS (row->end.pos) <= ZV)
16844 row_found = row;
16845
16846 /* Stop if last visible row. */
16847 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16848 break;
16849 }
16850
16851 return row_found;
16852 }
16853
16854
16855 /* Find the first glyph row in the current matrix of W that is not
16856 affected by changes at the end of current_buffer since the
16857 time W's current matrix was built.
16858
16859 Return in *DELTA the number of chars by which buffer positions in
16860 unchanged text at the end of current_buffer must be adjusted.
16861
16862 Return in *DELTA_BYTES the corresponding number of bytes.
16863
16864 Value is null if no such row exists, i.e. all rows are affected by
16865 changes. */
16866
16867 static struct glyph_row *
16868 find_first_unchanged_at_end_row (struct window *w,
16869 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16870 {
16871 struct glyph_row *row;
16872 struct glyph_row *row_found = NULL;
16873
16874 *delta = *delta_bytes = 0;
16875
16876 /* Display must not have been paused, otherwise the current matrix
16877 is not up to date. */
16878 eassert (!NILP (w->window_end_valid));
16879
16880 /* A value of window_end_pos >= END_UNCHANGED means that the window
16881 end is in the range of changed text. If so, there is no
16882 unchanged row at the end of W's current matrix. */
16883 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16884 return NULL;
16885
16886 /* Set row to the last row in W's current matrix displaying text. */
16887 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16888
16889 /* If matrix is entirely empty, no unchanged row exists. */
16890 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16891 {
16892 /* The value of row is the last glyph row in the matrix having a
16893 meaningful buffer position in it. The end position of row
16894 corresponds to window_end_pos. This allows us to translate
16895 buffer positions in the current matrix to current buffer
16896 positions for characters not in changed text. */
16897 ptrdiff_t Z_old =
16898 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16899 ptrdiff_t Z_BYTE_old =
16900 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16901 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16902 struct glyph_row *first_text_row
16903 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16904
16905 *delta = Z - Z_old;
16906 *delta_bytes = Z_BYTE - Z_BYTE_old;
16907
16908 /* Set last_unchanged_pos to the buffer position of the last
16909 character in the buffer that has not been changed. Z is the
16910 index + 1 of the last character in current_buffer, i.e. by
16911 subtracting END_UNCHANGED we get the index of the last
16912 unchanged character, and we have to add BEG to get its buffer
16913 position. */
16914 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16915 last_unchanged_pos_old = last_unchanged_pos - *delta;
16916
16917 /* Search backward from ROW for a row displaying a line that
16918 starts at a minimum position >= last_unchanged_pos_old. */
16919 for (; row > first_text_row; --row)
16920 {
16921 /* This used to abort, but it can happen.
16922 It is ok to just stop the search instead here. KFS. */
16923 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16924 break;
16925
16926 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16927 row_found = row;
16928 }
16929 }
16930
16931 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16932
16933 return row_found;
16934 }
16935
16936
16937 /* Make sure that glyph rows in the current matrix of window W
16938 reference the same glyph memory as corresponding rows in the
16939 frame's frame matrix. This function is called after scrolling W's
16940 current matrix on a terminal frame in try_window_id and
16941 try_window_reusing_current_matrix. */
16942
16943 static void
16944 sync_frame_with_window_matrix_rows (struct window *w)
16945 {
16946 struct frame *f = XFRAME (w->frame);
16947 struct glyph_row *window_row, *window_row_end, *frame_row;
16948
16949 /* Preconditions: W must be a leaf window and full-width. Its frame
16950 must have a frame matrix. */
16951 eassert (NILP (w->hchild) && NILP (w->vchild));
16952 eassert (WINDOW_FULL_WIDTH_P (w));
16953 eassert (!FRAME_WINDOW_P (f));
16954
16955 /* If W is a full-width window, glyph pointers in W's current matrix
16956 have, by definition, to be the same as glyph pointers in the
16957 corresponding frame matrix. Note that frame matrices have no
16958 marginal areas (see build_frame_matrix). */
16959 window_row = w->current_matrix->rows;
16960 window_row_end = window_row + w->current_matrix->nrows;
16961 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16962 while (window_row < window_row_end)
16963 {
16964 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16965 struct glyph *end = window_row->glyphs[LAST_AREA];
16966
16967 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16968 frame_row->glyphs[TEXT_AREA] = start;
16969 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16970 frame_row->glyphs[LAST_AREA] = end;
16971
16972 /* Disable frame rows whose corresponding window rows have
16973 been disabled in try_window_id. */
16974 if (!window_row->enabled_p)
16975 frame_row->enabled_p = 0;
16976
16977 ++window_row, ++frame_row;
16978 }
16979 }
16980
16981
16982 /* Find the glyph row in window W containing CHARPOS. Consider all
16983 rows between START and END (not inclusive). END null means search
16984 all rows to the end of the display area of W. Value is the row
16985 containing CHARPOS or null. */
16986
16987 struct glyph_row *
16988 row_containing_pos (struct window *w, ptrdiff_t charpos,
16989 struct glyph_row *start, struct glyph_row *end, int dy)
16990 {
16991 struct glyph_row *row = start;
16992 struct glyph_row *best_row = NULL;
16993 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16994 int last_y;
16995
16996 /* If we happen to start on a header-line, skip that. */
16997 if (row->mode_line_p)
16998 ++row;
16999
17000 if ((end && row >= end) || !row->enabled_p)
17001 return NULL;
17002
17003 last_y = window_text_bottom_y (w) - dy;
17004
17005 while (1)
17006 {
17007 /* Give up if we have gone too far. */
17008 if (end && row >= end)
17009 return NULL;
17010 /* This formerly returned if they were equal.
17011 I think that both quantities are of a "last plus one" type;
17012 if so, when they are equal, the row is within the screen. -- rms. */
17013 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17014 return NULL;
17015
17016 /* If it is in this row, return this row. */
17017 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17018 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17019 /* The end position of a row equals the start
17020 position of the next row. If CHARPOS is there, we
17021 would rather display it in the next line, except
17022 when this line ends in ZV. */
17023 && !row->ends_at_zv_p
17024 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17025 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17026 {
17027 struct glyph *g;
17028
17029 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17030 || (!best_row && !row->continued_p))
17031 return row;
17032 /* In bidi-reordered rows, there could be several rows
17033 occluding point, all of them belonging to the same
17034 continued line. We need to find the row which fits
17035 CHARPOS the best. */
17036 for (g = row->glyphs[TEXT_AREA];
17037 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17038 g++)
17039 {
17040 if (!STRINGP (g->object))
17041 {
17042 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17043 {
17044 mindif = eabs (g->charpos - charpos);
17045 best_row = row;
17046 /* Exact match always wins. */
17047 if (mindif == 0)
17048 return best_row;
17049 }
17050 }
17051 }
17052 }
17053 else if (best_row && !row->continued_p)
17054 return best_row;
17055 ++row;
17056 }
17057 }
17058
17059
17060 /* Try to redisplay window W by reusing its existing display. W's
17061 current matrix must be up to date when this function is called,
17062 i.e. window_end_valid must not be nil.
17063
17064 Value is
17065
17066 1 if display has been updated
17067 0 if otherwise unsuccessful
17068 -1 if redisplay with same window start is known not to succeed
17069
17070 The following steps are performed:
17071
17072 1. Find the last row in the current matrix of W that is not
17073 affected by changes at the start of current_buffer. If no such row
17074 is found, give up.
17075
17076 2. Find the first row in W's current matrix that is not affected by
17077 changes at the end of current_buffer. Maybe there is no such row.
17078
17079 3. Display lines beginning with the row + 1 found in step 1 to the
17080 row found in step 2 or, if step 2 didn't find a row, to the end of
17081 the window.
17082
17083 4. If cursor is not known to appear on the window, give up.
17084
17085 5. If display stopped at the row found in step 2, scroll the
17086 display and current matrix as needed.
17087
17088 6. Maybe display some lines at the end of W, if we must. This can
17089 happen under various circumstances, like a partially visible line
17090 becoming fully visible, or because newly displayed lines are displayed
17091 in smaller font sizes.
17092
17093 7. Update W's window end information. */
17094
17095 static int
17096 try_window_id (struct window *w)
17097 {
17098 struct frame *f = XFRAME (w->frame);
17099 struct glyph_matrix *current_matrix = w->current_matrix;
17100 struct glyph_matrix *desired_matrix = w->desired_matrix;
17101 struct glyph_row *last_unchanged_at_beg_row;
17102 struct glyph_row *first_unchanged_at_end_row;
17103 struct glyph_row *row;
17104 struct glyph_row *bottom_row;
17105 int bottom_vpos;
17106 struct it it;
17107 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17108 int dvpos, dy;
17109 struct text_pos start_pos;
17110 struct run run;
17111 int first_unchanged_at_end_vpos = 0;
17112 struct glyph_row *last_text_row, *last_text_row_at_end;
17113 struct text_pos start;
17114 ptrdiff_t first_changed_charpos, last_changed_charpos;
17115
17116 #ifdef GLYPH_DEBUG
17117 if (inhibit_try_window_id)
17118 return 0;
17119 #endif
17120
17121 /* This is handy for debugging. */
17122 #if 0
17123 #define GIVE_UP(X) \
17124 do { \
17125 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17126 return 0; \
17127 } while (0)
17128 #else
17129 #define GIVE_UP(X) return 0
17130 #endif
17131
17132 SET_TEXT_POS_FROM_MARKER (start, w->start);
17133
17134 /* Don't use this for mini-windows because these can show
17135 messages and mini-buffers, and we don't handle that here. */
17136 if (MINI_WINDOW_P (w))
17137 GIVE_UP (1);
17138
17139 /* This flag is used to prevent redisplay optimizations. */
17140 if (windows_or_buffers_changed || cursor_type_changed)
17141 GIVE_UP (2);
17142
17143 /* Verify that narrowing has not changed.
17144 Also verify that we were not told to prevent redisplay optimizations.
17145 It would be nice to further
17146 reduce the number of cases where this prevents try_window_id. */
17147 if (current_buffer->clip_changed
17148 || current_buffer->prevent_redisplay_optimizations_p)
17149 GIVE_UP (3);
17150
17151 /* Window must either use window-based redisplay or be full width. */
17152 if (!FRAME_WINDOW_P (f)
17153 && (!FRAME_LINE_INS_DEL_OK (f)
17154 || !WINDOW_FULL_WIDTH_P (w)))
17155 GIVE_UP (4);
17156
17157 /* Give up if point is known NOT to appear in W. */
17158 if (PT < CHARPOS (start))
17159 GIVE_UP (5);
17160
17161 /* Another way to prevent redisplay optimizations. */
17162 if (w->last_modified == 0)
17163 GIVE_UP (6);
17164
17165 /* Verify that window is not hscrolled. */
17166 if (w->hscroll != 0)
17167 GIVE_UP (7);
17168
17169 /* Verify that display wasn't paused. */
17170 if (NILP (w->window_end_valid))
17171 GIVE_UP (8);
17172
17173 /* Can't use this if highlighting a region because a cursor movement
17174 will do more than just set the cursor. */
17175 if (!NILP (Vtransient_mark_mode)
17176 && !NILP (BVAR (current_buffer, mark_active)))
17177 GIVE_UP (9);
17178
17179 /* Likewise if highlighting trailing whitespace. */
17180 if (!NILP (Vshow_trailing_whitespace))
17181 GIVE_UP (11);
17182
17183 /* Likewise if showing a region. */
17184 if (!NILP (w->region_showing))
17185 GIVE_UP (10);
17186
17187 /* Can't use this if overlay arrow position and/or string have
17188 changed. */
17189 if (overlay_arrows_changed_p ())
17190 GIVE_UP (12);
17191
17192 /* When word-wrap is on, adding a space to the first word of a
17193 wrapped line can change the wrap position, altering the line
17194 above it. It might be worthwhile to handle this more
17195 intelligently, but for now just redisplay from scratch. */
17196 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17197 GIVE_UP (21);
17198
17199 /* Under bidi reordering, adding or deleting a character in the
17200 beginning of a paragraph, before the first strong directional
17201 character, can change the base direction of the paragraph (unless
17202 the buffer specifies a fixed paragraph direction), which will
17203 require to redisplay the whole paragraph. It might be worthwhile
17204 to find the paragraph limits and widen the range of redisplayed
17205 lines to that, but for now just give up this optimization and
17206 redisplay from scratch. */
17207 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17208 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17209 GIVE_UP (22);
17210
17211 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17212 only if buffer has really changed. The reason is that the gap is
17213 initially at Z for freshly visited files. The code below would
17214 set end_unchanged to 0 in that case. */
17215 if (MODIFF > SAVE_MODIFF
17216 /* This seems to happen sometimes after saving a buffer. */
17217 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17218 {
17219 if (GPT - BEG < BEG_UNCHANGED)
17220 BEG_UNCHANGED = GPT - BEG;
17221 if (Z - GPT < END_UNCHANGED)
17222 END_UNCHANGED = Z - GPT;
17223 }
17224
17225 /* The position of the first and last character that has been changed. */
17226 first_changed_charpos = BEG + BEG_UNCHANGED;
17227 last_changed_charpos = Z - END_UNCHANGED;
17228
17229 /* If window starts after a line end, and the last change is in
17230 front of that newline, then changes don't affect the display.
17231 This case happens with stealth-fontification. Note that although
17232 the display is unchanged, glyph positions in the matrix have to
17233 be adjusted, of course. */
17234 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17235 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17236 && ((last_changed_charpos < CHARPOS (start)
17237 && CHARPOS (start) == BEGV)
17238 || (last_changed_charpos < CHARPOS (start) - 1
17239 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17240 {
17241 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17242 struct glyph_row *r0;
17243
17244 /* Compute how many chars/bytes have been added to or removed
17245 from the buffer. */
17246 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17247 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17248 Z_delta = Z - Z_old;
17249 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17250
17251 /* Give up if PT is not in the window. Note that it already has
17252 been checked at the start of try_window_id that PT is not in
17253 front of the window start. */
17254 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17255 GIVE_UP (13);
17256
17257 /* If window start is unchanged, we can reuse the whole matrix
17258 as is, after adjusting glyph positions. No need to compute
17259 the window end again, since its offset from Z hasn't changed. */
17260 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17261 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17262 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17263 /* PT must not be in a partially visible line. */
17264 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17265 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17266 {
17267 /* Adjust positions in the glyph matrix. */
17268 if (Z_delta || Z_delta_bytes)
17269 {
17270 struct glyph_row *r1
17271 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17272 increment_matrix_positions (w->current_matrix,
17273 MATRIX_ROW_VPOS (r0, current_matrix),
17274 MATRIX_ROW_VPOS (r1, current_matrix),
17275 Z_delta, Z_delta_bytes);
17276 }
17277
17278 /* Set the cursor. */
17279 row = row_containing_pos (w, PT, r0, NULL, 0);
17280 if (row)
17281 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17282 else
17283 abort ();
17284 return 1;
17285 }
17286 }
17287
17288 /* Handle the case that changes are all below what is displayed in
17289 the window, and that PT is in the window. This shortcut cannot
17290 be taken if ZV is visible in the window, and text has been added
17291 there that is visible in the window. */
17292 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17293 /* ZV is not visible in the window, or there are no
17294 changes at ZV, actually. */
17295 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17296 || first_changed_charpos == last_changed_charpos))
17297 {
17298 struct glyph_row *r0;
17299
17300 /* Give up if PT is not in the window. Note that it already has
17301 been checked at the start of try_window_id that PT is not in
17302 front of the window start. */
17303 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17304 GIVE_UP (14);
17305
17306 /* If window start is unchanged, we can reuse the whole matrix
17307 as is, without changing glyph positions since no text has
17308 been added/removed in front of the window end. */
17309 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17310 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17311 /* PT must not be in a partially visible line. */
17312 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17313 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17314 {
17315 /* We have to compute the window end anew since text
17316 could have been added/removed after it. */
17317 w->window_end_pos
17318 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17319 w->window_end_bytepos
17320 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17321
17322 /* Set the cursor. */
17323 row = row_containing_pos (w, PT, r0, NULL, 0);
17324 if (row)
17325 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17326 else
17327 abort ();
17328 return 2;
17329 }
17330 }
17331
17332 /* Give up if window start is in the changed area.
17333
17334 The condition used to read
17335
17336 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17337
17338 but why that was tested escapes me at the moment. */
17339 if (CHARPOS (start) >= first_changed_charpos
17340 && CHARPOS (start) <= last_changed_charpos)
17341 GIVE_UP (15);
17342
17343 /* Check that window start agrees with the start of the first glyph
17344 row in its current matrix. Check this after we know the window
17345 start is not in changed text, otherwise positions would not be
17346 comparable. */
17347 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17348 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17349 GIVE_UP (16);
17350
17351 /* Give up if the window ends in strings. Overlay strings
17352 at the end are difficult to handle, so don't try. */
17353 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17354 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17355 GIVE_UP (20);
17356
17357 /* Compute the position at which we have to start displaying new
17358 lines. Some of the lines at the top of the window might be
17359 reusable because they are not displaying changed text. Find the
17360 last row in W's current matrix not affected by changes at the
17361 start of current_buffer. Value is null if changes start in the
17362 first line of window. */
17363 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17364 if (last_unchanged_at_beg_row)
17365 {
17366 /* Avoid starting to display in the middle of a character, a TAB
17367 for instance. This is easier than to set up the iterator
17368 exactly, and it's not a frequent case, so the additional
17369 effort wouldn't really pay off. */
17370 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17371 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17372 && last_unchanged_at_beg_row > w->current_matrix->rows)
17373 --last_unchanged_at_beg_row;
17374
17375 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17376 GIVE_UP (17);
17377
17378 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17379 GIVE_UP (18);
17380 start_pos = it.current.pos;
17381
17382 /* Start displaying new lines in the desired matrix at the same
17383 vpos we would use in the current matrix, i.e. below
17384 last_unchanged_at_beg_row. */
17385 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17386 current_matrix);
17387 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17388 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17389
17390 eassert (it.hpos == 0 && it.current_x == 0);
17391 }
17392 else
17393 {
17394 /* There are no reusable lines at the start of the window.
17395 Start displaying in the first text line. */
17396 start_display (&it, w, start);
17397 it.vpos = it.first_vpos;
17398 start_pos = it.current.pos;
17399 }
17400
17401 /* Find the first row that is not affected by changes at the end of
17402 the buffer. Value will be null if there is no unchanged row, in
17403 which case we must redisplay to the end of the window. delta
17404 will be set to the value by which buffer positions beginning with
17405 first_unchanged_at_end_row have to be adjusted due to text
17406 changes. */
17407 first_unchanged_at_end_row
17408 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17409 IF_DEBUG (debug_delta = delta);
17410 IF_DEBUG (debug_delta_bytes = delta_bytes);
17411
17412 /* Set stop_pos to the buffer position up to which we will have to
17413 display new lines. If first_unchanged_at_end_row != NULL, this
17414 is the buffer position of the start of the line displayed in that
17415 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17416 that we don't stop at a buffer position. */
17417 stop_pos = 0;
17418 if (first_unchanged_at_end_row)
17419 {
17420 eassert (last_unchanged_at_beg_row == NULL
17421 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17422
17423 /* If this is a continuation line, move forward to the next one
17424 that isn't. Changes in lines above affect this line.
17425 Caution: this may move first_unchanged_at_end_row to a row
17426 not displaying text. */
17427 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17428 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17429 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17430 < it.last_visible_y))
17431 ++first_unchanged_at_end_row;
17432
17433 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17434 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17435 >= it.last_visible_y))
17436 first_unchanged_at_end_row = NULL;
17437 else
17438 {
17439 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17440 + delta);
17441 first_unchanged_at_end_vpos
17442 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17443 eassert (stop_pos >= Z - END_UNCHANGED);
17444 }
17445 }
17446 else if (last_unchanged_at_beg_row == NULL)
17447 GIVE_UP (19);
17448
17449
17450 #ifdef GLYPH_DEBUG
17451
17452 /* Either there is no unchanged row at the end, or the one we have
17453 now displays text. This is a necessary condition for the window
17454 end pos calculation at the end of this function. */
17455 eassert (first_unchanged_at_end_row == NULL
17456 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17457
17458 debug_last_unchanged_at_beg_vpos
17459 = (last_unchanged_at_beg_row
17460 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17461 : -1);
17462 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17463
17464 #endif /* GLYPH_DEBUG */
17465
17466
17467 /* Display new lines. Set last_text_row to the last new line
17468 displayed which has text on it, i.e. might end up as being the
17469 line where the window_end_vpos is. */
17470 w->cursor.vpos = -1;
17471 last_text_row = NULL;
17472 overlay_arrow_seen = 0;
17473 while (it.current_y < it.last_visible_y
17474 && !fonts_changed_p
17475 && (first_unchanged_at_end_row == NULL
17476 || IT_CHARPOS (it) < stop_pos))
17477 {
17478 if (display_line (&it))
17479 last_text_row = it.glyph_row - 1;
17480 }
17481
17482 if (fonts_changed_p)
17483 return -1;
17484
17485
17486 /* Compute differences in buffer positions, y-positions etc. for
17487 lines reused at the bottom of the window. Compute what we can
17488 scroll. */
17489 if (first_unchanged_at_end_row
17490 /* No lines reused because we displayed everything up to the
17491 bottom of the window. */
17492 && it.current_y < it.last_visible_y)
17493 {
17494 dvpos = (it.vpos
17495 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17496 current_matrix));
17497 dy = it.current_y - first_unchanged_at_end_row->y;
17498 run.current_y = first_unchanged_at_end_row->y;
17499 run.desired_y = run.current_y + dy;
17500 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17501 }
17502 else
17503 {
17504 delta = delta_bytes = dvpos = dy
17505 = run.current_y = run.desired_y = run.height = 0;
17506 first_unchanged_at_end_row = NULL;
17507 }
17508 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17509
17510
17511 /* Find the cursor if not already found. We have to decide whether
17512 PT will appear on this window (it sometimes doesn't, but this is
17513 not a very frequent case.) This decision has to be made before
17514 the current matrix is altered. A value of cursor.vpos < 0 means
17515 that PT is either in one of the lines beginning at
17516 first_unchanged_at_end_row or below the window. Don't care for
17517 lines that might be displayed later at the window end; as
17518 mentioned, this is not a frequent case. */
17519 if (w->cursor.vpos < 0)
17520 {
17521 /* Cursor in unchanged rows at the top? */
17522 if (PT < CHARPOS (start_pos)
17523 && last_unchanged_at_beg_row)
17524 {
17525 row = row_containing_pos (w, PT,
17526 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17527 last_unchanged_at_beg_row + 1, 0);
17528 if (row)
17529 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17530 }
17531
17532 /* Start from first_unchanged_at_end_row looking for PT. */
17533 else if (first_unchanged_at_end_row)
17534 {
17535 row = row_containing_pos (w, PT - delta,
17536 first_unchanged_at_end_row, NULL, 0);
17537 if (row)
17538 set_cursor_from_row (w, row, w->current_matrix, delta,
17539 delta_bytes, dy, dvpos);
17540 }
17541
17542 /* Give up if cursor was not found. */
17543 if (w->cursor.vpos < 0)
17544 {
17545 clear_glyph_matrix (w->desired_matrix);
17546 return -1;
17547 }
17548 }
17549
17550 /* Don't let the cursor end in the scroll margins. */
17551 {
17552 int this_scroll_margin, cursor_height;
17553
17554 this_scroll_margin =
17555 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17556 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17557 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17558
17559 if ((w->cursor.y < this_scroll_margin
17560 && CHARPOS (start) > BEGV)
17561 /* Old redisplay didn't take scroll margin into account at the bottom,
17562 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17563 || (w->cursor.y + (make_cursor_line_fully_visible_p
17564 ? cursor_height + this_scroll_margin
17565 : 1)) > it.last_visible_y)
17566 {
17567 w->cursor.vpos = -1;
17568 clear_glyph_matrix (w->desired_matrix);
17569 return -1;
17570 }
17571 }
17572
17573 /* Scroll the display. Do it before changing the current matrix so
17574 that xterm.c doesn't get confused about where the cursor glyph is
17575 found. */
17576 if (dy && run.height)
17577 {
17578 update_begin (f);
17579
17580 if (FRAME_WINDOW_P (f))
17581 {
17582 FRAME_RIF (f)->update_window_begin_hook (w);
17583 FRAME_RIF (f)->clear_window_mouse_face (w);
17584 FRAME_RIF (f)->scroll_run_hook (w, &run);
17585 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17586 }
17587 else
17588 {
17589 /* Terminal frame. In this case, dvpos gives the number of
17590 lines to scroll by; dvpos < 0 means scroll up. */
17591 int from_vpos
17592 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17593 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17594 int end = (WINDOW_TOP_EDGE_LINE (w)
17595 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17596 + window_internal_height (w));
17597
17598 #if defined (HAVE_GPM) || defined (MSDOS)
17599 x_clear_window_mouse_face (w);
17600 #endif
17601 /* Perform the operation on the screen. */
17602 if (dvpos > 0)
17603 {
17604 /* Scroll last_unchanged_at_beg_row to the end of the
17605 window down dvpos lines. */
17606 set_terminal_window (f, end);
17607
17608 /* On dumb terminals delete dvpos lines at the end
17609 before inserting dvpos empty lines. */
17610 if (!FRAME_SCROLL_REGION_OK (f))
17611 ins_del_lines (f, end - dvpos, -dvpos);
17612
17613 /* Insert dvpos empty lines in front of
17614 last_unchanged_at_beg_row. */
17615 ins_del_lines (f, from, dvpos);
17616 }
17617 else if (dvpos < 0)
17618 {
17619 /* Scroll up last_unchanged_at_beg_vpos to the end of
17620 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17621 set_terminal_window (f, end);
17622
17623 /* Delete dvpos lines in front of
17624 last_unchanged_at_beg_vpos. ins_del_lines will set
17625 the cursor to the given vpos and emit |dvpos| delete
17626 line sequences. */
17627 ins_del_lines (f, from + dvpos, dvpos);
17628
17629 /* On a dumb terminal insert dvpos empty lines at the
17630 end. */
17631 if (!FRAME_SCROLL_REGION_OK (f))
17632 ins_del_lines (f, end + dvpos, -dvpos);
17633 }
17634
17635 set_terminal_window (f, 0);
17636 }
17637
17638 update_end (f);
17639 }
17640
17641 /* Shift reused rows of the current matrix to the right position.
17642 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17643 text. */
17644 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17645 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17646 if (dvpos < 0)
17647 {
17648 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17649 bottom_vpos, dvpos);
17650 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17651 bottom_vpos, 0);
17652 }
17653 else if (dvpos > 0)
17654 {
17655 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17656 bottom_vpos, dvpos);
17657 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17658 first_unchanged_at_end_vpos + dvpos, 0);
17659 }
17660
17661 /* For frame-based redisplay, make sure that current frame and window
17662 matrix are in sync with respect to glyph memory. */
17663 if (!FRAME_WINDOW_P (f))
17664 sync_frame_with_window_matrix_rows (w);
17665
17666 /* Adjust buffer positions in reused rows. */
17667 if (delta || delta_bytes)
17668 increment_matrix_positions (current_matrix,
17669 first_unchanged_at_end_vpos + dvpos,
17670 bottom_vpos, delta, delta_bytes);
17671
17672 /* Adjust Y positions. */
17673 if (dy)
17674 shift_glyph_matrix (w, current_matrix,
17675 first_unchanged_at_end_vpos + dvpos,
17676 bottom_vpos, dy);
17677
17678 if (first_unchanged_at_end_row)
17679 {
17680 first_unchanged_at_end_row += dvpos;
17681 if (first_unchanged_at_end_row->y >= it.last_visible_y
17682 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17683 first_unchanged_at_end_row = NULL;
17684 }
17685
17686 /* If scrolling up, there may be some lines to display at the end of
17687 the window. */
17688 last_text_row_at_end = NULL;
17689 if (dy < 0)
17690 {
17691 /* Scrolling up can leave for example a partially visible line
17692 at the end of the window to be redisplayed. */
17693 /* Set last_row to the glyph row in the current matrix where the
17694 window end line is found. It has been moved up or down in
17695 the matrix by dvpos. */
17696 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17697 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17698
17699 /* If last_row is the window end line, it should display text. */
17700 eassert (last_row->displays_text_p);
17701
17702 /* If window end line was partially visible before, begin
17703 displaying at that line. Otherwise begin displaying with the
17704 line following it. */
17705 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17706 {
17707 init_to_row_start (&it, w, last_row);
17708 it.vpos = last_vpos;
17709 it.current_y = last_row->y;
17710 }
17711 else
17712 {
17713 init_to_row_end (&it, w, last_row);
17714 it.vpos = 1 + last_vpos;
17715 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17716 ++last_row;
17717 }
17718
17719 /* We may start in a continuation line. If so, we have to
17720 get the right continuation_lines_width and current_x. */
17721 it.continuation_lines_width = last_row->continuation_lines_width;
17722 it.hpos = it.current_x = 0;
17723
17724 /* Display the rest of the lines at the window end. */
17725 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17726 while (it.current_y < it.last_visible_y
17727 && !fonts_changed_p)
17728 {
17729 /* Is it always sure that the display agrees with lines in
17730 the current matrix? I don't think so, so we mark rows
17731 displayed invalid in the current matrix by setting their
17732 enabled_p flag to zero. */
17733 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17734 if (display_line (&it))
17735 last_text_row_at_end = it.glyph_row - 1;
17736 }
17737 }
17738
17739 /* Update window_end_pos and window_end_vpos. */
17740 if (first_unchanged_at_end_row
17741 && !last_text_row_at_end)
17742 {
17743 /* Window end line if one of the preserved rows from the current
17744 matrix. Set row to the last row displaying text in current
17745 matrix starting at first_unchanged_at_end_row, after
17746 scrolling. */
17747 eassert (first_unchanged_at_end_row->displays_text_p);
17748 row = find_last_row_displaying_text (w->current_matrix, &it,
17749 first_unchanged_at_end_row);
17750 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17751
17752 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17753 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17754 w->window_end_vpos
17755 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17756 eassert (w->window_end_bytepos >= 0);
17757 IF_DEBUG (debug_method_add (w, "A"));
17758 }
17759 else if (last_text_row_at_end)
17760 {
17761 w->window_end_pos
17762 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17763 w->window_end_bytepos
17764 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17765 w->window_end_vpos
17766 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17767 eassert (w->window_end_bytepos >= 0);
17768 IF_DEBUG (debug_method_add (w, "B"));
17769 }
17770 else if (last_text_row)
17771 {
17772 /* We have displayed either to the end of the window or at the
17773 end of the window, i.e. the last row with text is to be found
17774 in the desired matrix. */
17775 w->window_end_pos
17776 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17777 w->window_end_bytepos
17778 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17779 w->window_end_vpos
17780 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17781 eassert (w->window_end_bytepos >= 0);
17782 }
17783 else if (first_unchanged_at_end_row == NULL
17784 && last_text_row == NULL
17785 && last_text_row_at_end == NULL)
17786 {
17787 /* Displayed to end of window, but no line containing text was
17788 displayed. Lines were deleted at the end of the window. */
17789 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17790 int vpos = XFASTINT (w->window_end_vpos);
17791 struct glyph_row *current_row = current_matrix->rows + vpos;
17792 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17793
17794 for (row = NULL;
17795 row == NULL && vpos >= first_vpos;
17796 --vpos, --current_row, --desired_row)
17797 {
17798 if (desired_row->enabled_p)
17799 {
17800 if (desired_row->displays_text_p)
17801 row = desired_row;
17802 }
17803 else if (current_row->displays_text_p)
17804 row = current_row;
17805 }
17806
17807 eassert (row != NULL);
17808 w->window_end_vpos = make_number (vpos + 1);
17809 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17810 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17811 eassert (w->window_end_bytepos >= 0);
17812 IF_DEBUG (debug_method_add (w, "C"));
17813 }
17814 else
17815 abort ();
17816
17817 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17818 debug_end_vpos = XFASTINT (w->window_end_vpos));
17819
17820 /* Record that display has not been completed. */
17821 w->window_end_valid = Qnil;
17822 w->desired_matrix->no_scrolling_p = 1;
17823 return 3;
17824
17825 #undef GIVE_UP
17826 }
17827
17828
17829 \f
17830 /***********************************************************************
17831 More debugging support
17832 ***********************************************************************/
17833
17834 #ifdef GLYPH_DEBUG
17835
17836 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17837 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17838 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17839
17840
17841 /* Dump the contents of glyph matrix MATRIX on stderr.
17842
17843 GLYPHS 0 means don't show glyph contents.
17844 GLYPHS 1 means show glyphs in short form
17845 GLYPHS > 1 means show glyphs in long form. */
17846
17847 void
17848 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17849 {
17850 int i;
17851 for (i = 0; i < matrix->nrows; ++i)
17852 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17853 }
17854
17855
17856 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17857 the glyph row and area where the glyph comes from. */
17858
17859 void
17860 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17861 {
17862 if (glyph->type == CHAR_GLYPH)
17863 {
17864 fprintf (stderr,
17865 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17866 glyph - row->glyphs[TEXT_AREA],
17867 'C',
17868 glyph->charpos,
17869 (BUFFERP (glyph->object)
17870 ? 'B'
17871 : (STRINGP (glyph->object)
17872 ? 'S'
17873 : '-')),
17874 glyph->pixel_width,
17875 glyph->u.ch,
17876 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17877 ? glyph->u.ch
17878 : '.'),
17879 glyph->face_id,
17880 glyph->left_box_line_p,
17881 glyph->right_box_line_p);
17882 }
17883 else if (glyph->type == STRETCH_GLYPH)
17884 {
17885 fprintf (stderr,
17886 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17887 glyph - row->glyphs[TEXT_AREA],
17888 'S',
17889 glyph->charpos,
17890 (BUFFERP (glyph->object)
17891 ? 'B'
17892 : (STRINGP (glyph->object)
17893 ? 'S'
17894 : '-')),
17895 glyph->pixel_width,
17896 0,
17897 '.',
17898 glyph->face_id,
17899 glyph->left_box_line_p,
17900 glyph->right_box_line_p);
17901 }
17902 else if (glyph->type == IMAGE_GLYPH)
17903 {
17904 fprintf (stderr,
17905 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17906 glyph - row->glyphs[TEXT_AREA],
17907 'I',
17908 glyph->charpos,
17909 (BUFFERP (glyph->object)
17910 ? 'B'
17911 : (STRINGP (glyph->object)
17912 ? 'S'
17913 : '-')),
17914 glyph->pixel_width,
17915 glyph->u.img_id,
17916 '.',
17917 glyph->face_id,
17918 glyph->left_box_line_p,
17919 glyph->right_box_line_p);
17920 }
17921 else if (glyph->type == COMPOSITE_GLYPH)
17922 {
17923 fprintf (stderr,
17924 " %5td %4c %6"pI"d %c %3d 0x%05x",
17925 glyph - row->glyphs[TEXT_AREA],
17926 '+',
17927 glyph->charpos,
17928 (BUFFERP (glyph->object)
17929 ? 'B'
17930 : (STRINGP (glyph->object)
17931 ? 'S'
17932 : '-')),
17933 glyph->pixel_width,
17934 glyph->u.cmp.id);
17935 if (glyph->u.cmp.automatic)
17936 fprintf (stderr,
17937 "[%d-%d]",
17938 glyph->slice.cmp.from, glyph->slice.cmp.to);
17939 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17940 glyph->face_id,
17941 glyph->left_box_line_p,
17942 glyph->right_box_line_p);
17943 }
17944 }
17945
17946
17947 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17948 GLYPHS 0 means don't show glyph contents.
17949 GLYPHS 1 means show glyphs in short form
17950 GLYPHS > 1 means show glyphs in long form. */
17951
17952 void
17953 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17954 {
17955 if (glyphs != 1)
17956 {
17957 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17958 fprintf (stderr, "======================================================================\n");
17959
17960 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17961 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17962 vpos,
17963 MATRIX_ROW_START_CHARPOS (row),
17964 MATRIX_ROW_END_CHARPOS (row),
17965 row->used[TEXT_AREA],
17966 row->contains_overlapping_glyphs_p,
17967 row->enabled_p,
17968 row->truncated_on_left_p,
17969 row->truncated_on_right_p,
17970 row->continued_p,
17971 MATRIX_ROW_CONTINUATION_LINE_P (row),
17972 row->displays_text_p,
17973 row->ends_at_zv_p,
17974 row->fill_line_p,
17975 row->ends_in_middle_of_char_p,
17976 row->starts_in_middle_of_char_p,
17977 row->mouse_face_p,
17978 row->x,
17979 row->y,
17980 row->pixel_width,
17981 row->height,
17982 row->visible_height,
17983 row->ascent,
17984 row->phys_ascent);
17985 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17986 row->end.overlay_string_index,
17987 row->continuation_lines_width);
17988 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17989 CHARPOS (row->start.string_pos),
17990 CHARPOS (row->end.string_pos));
17991 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17992 row->end.dpvec_index);
17993 }
17994
17995 if (glyphs > 1)
17996 {
17997 int area;
17998
17999 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18000 {
18001 struct glyph *glyph = row->glyphs[area];
18002 struct glyph *glyph_end = glyph + row->used[area];
18003
18004 /* Glyph for a line end in text. */
18005 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18006 ++glyph_end;
18007
18008 if (glyph < glyph_end)
18009 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18010
18011 for (; glyph < glyph_end; ++glyph)
18012 dump_glyph (row, glyph, area);
18013 }
18014 }
18015 else if (glyphs == 1)
18016 {
18017 int area;
18018
18019 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18020 {
18021 char *s = alloca (row->used[area] + 1);
18022 int i;
18023
18024 for (i = 0; i < row->used[area]; ++i)
18025 {
18026 struct glyph *glyph = row->glyphs[area] + i;
18027 if (glyph->type == CHAR_GLYPH
18028 && glyph->u.ch < 0x80
18029 && glyph->u.ch >= ' ')
18030 s[i] = glyph->u.ch;
18031 else
18032 s[i] = '.';
18033 }
18034
18035 s[i] = '\0';
18036 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18037 }
18038 }
18039 }
18040
18041
18042 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18043 Sdump_glyph_matrix, 0, 1, "p",
18044 doc: /* Dump the current matrix of the selected window to stderr.
18045 Shows contents of glyph row structures. With non-nil
18046 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18047 glyphs in short form, otherwise show glyphs in long form. */)
18048 (Lisp_Object glyphs)
18049 {
18050 struct window *w = XWINDOW (selected_window);
18051 struct buffer *buffer = XBUFFER (w->buffer);
18052
18053 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18054 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18055 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18056 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18057 fprintf (stderr, "=============================================\n");
18058 dump_glyph_matrix (w->current_matrix,
18059 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18060 return Qnil;
18061 }
18062
18063
18064 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18065 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18066 (void)
18067 {
18068 struct frame *f = XFRAME (selected_frame);
18069 dump_glyph_matrix (f->current_matrix, 1);
18070 return Qnil;
18071 }
18072
18073
18074 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18075 doc: /* Dump glyph row ROW to stderr.
18076 GLYPH 0 means don't dump glyphs.
18077 GLYPH 1 means dump glyphs in short form.
18078 GLYPH > 1 or omitted means dump glyphs in long form. */)
18079 (Lisp_Object row, Lisp_Object glyphs)
18080 {
18081 struct glyph_matrix *matrix;
18082 EMACS_INT vpos;
18083
18084 CHECK_NUMBER (row);
18085 matrix = XWINDOW (selected_window)->current_matrix;
18086 vpos = XINT (row);
18087 if (vpos >= 0 && vpos < matrix->nrows)
18088 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18089 vpos,
18090 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18091 return Qnil;
18092 }
18093
18094
18095 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18096 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18097 GLYPH 0 means don't dump glyphs.
18098 GLYPH 1 means dump glyphs in short form.
18099 GLYPH > 1 or omitted means dump glyphs in long form. */)
18100 (Lisp_Object row, Lisp_Object glyphs)
18101 {
18102 struct frame *sf = SELECTED_FRAME ();
18103 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18104 EMACS_INT vpos;
18105
18106 CHECK_NUMBER (row);
18107 vpos = XINT (row);
18108 if (vpos >= 0 && vpos < m->nrows)
18109 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18110 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18111 return Qnil;
18112 }
18113
18114
18115 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18116 doc: /* Toggle tracing of redisplay.
18117 With ARG, turn tracing on if and only if ARG is positive. */)
18118 (Lisp_Object arg)
18119 {
18120 if (NILP (arg))
18121 trace_redisplay_p = !trace_redisplay_p;
18122 else
18123 {
18124 arg = Fprefix_numeric_value (arg);
18125 trace_redisplay_p = XINT (arg) > 0;
18126 }
18127
18128 return Qnil;
18129 }
18130
18131
18132 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18133 doc: /* Like `format', but print result to stderr.
18134 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18135 (ptrdiff_t nargs, Lisp_Object *args)
18136 {
18137 Lisp_Object s = Fformat (nargs, args);
18138 fprintf (stderr, "%s", SDATA (s));
18139 return Qnil;
18140 }
18141
18142 #endif /* GLYPH_DEBUG */
18143
18144
18145 \f
18146 /***********************************************************************
18147 Building Desired Matrix Rows
18148 ***********************************************************************/
18149
18150 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18151 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18152
18153 static struct glyph_row *
18154 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18155 {
18156 struct frame *f = XFRAME (WINDOW_FRAME (w));
18157 struct buffer *buffer = XBUFFER (w->buffer);
18158 struct buffer *old = current_buffer;
18159 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18160 int arrow_len = SCHARS (overlay_arrow_string);
18161 const unsigned char *arrow_end = arrow_string + arrow_len;
18162 const unsigned char *p;
18163 struct it it;
18164 int multibyte_p;
18165 int n_glyphs_before;
18166
18167 set_buffer_temp (buffer);
18168 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18169 it.glyph_row->used[TEXT_AREA] = 0;
18170 SET_TEXT_POS (it.position, 0, 0);
18171
18172 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18173 p = arrow_string;
18174 while (p < arrow_end)
18175 {
18176 Lisp_Object face, ilisp;
18177
18178 /* Get the next character. */
18179 if (multibyte_p)
18180 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18181 else
18182 {
18183 it.c = it.char_to_display = *p, it.len = 1;
18184 if (! ASCII_CHAR_P (it.c))
18185 it.char_to_display = BYTE8_TO_CHAR (it.c);
18186 }
18187 p += it.len;
18188
18189 /* Get its face. */
18190 ilisp = make_number (p - arrow_string);
18191 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18192 it.face_id = compute_char_face (f, it.char_to_display, face);
18193
18194 /* Compute its width, get its glyphs. */
18195 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18196 SET_TEXT_POS (it.position, -1, -1);
18197 PRODUCE_GLYPHS (&it);
18198
18199 /* If this character doesn't fit any more in the line, we have
18200 to remove some glyphs. */
18201 if (it.current_x > it.last_visible_x)
18202 {
18203 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18204 break;
18205 }
18206 }
18207
18208 set_buffer_temp (old);
18209 return it.glyph_row;
18210 }
18211
18212
18213 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18214 glyphs to insert is determined by produce_special_glyphs. */
18215
18216 static void
18217 insert_left_trunc_glyphs (struct it *it)
18218 {
18219 struct it truncate_it;
18220 struct glyph *from, *end, *to, *toend;
18221
18222 eassert (!FRAME_WINDOW_P (it->f)
18223 || (!it->glyph_row->reversed_p
18224 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18225 || (it->glyph_row->reversed_p
18226 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18227
18228 /* Get the truncation glyphs. */
18229 truncate_it = *it;
18230 truncate_it.current_x = 0;
18231 truncate_it.face_id = DEFAULT_FACE_ID;
18232 truncate_it.glyph_row = &scratch_glyph_row;
18233 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18234 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18235 truncate_it.object = make_number (0);
18236 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18237
18238 /* Overwrite glyphs from IT with truncation glyphs. */
18239 if (!it->glyph_row->reversed_p)
18240 {
18241 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18242
18243 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18244 end = from + tused;
18245 to = it->glyph_row->glyphs[TEXT_AREA];
18246 toend = to + it->glyph_row->used[TEXT_AREA];
18247 if (FRAME_WINDOW_P (it->f))
18248 {
18249 /* On GUI frames, when variable-size fonts are displayed,
18250 the truncation glyphs may need more pixels than the row's
18251 glyphs they overwrite. We overwrite more glyphs to free
18252 enough screen real estate, and enlarge the stretch glyph
18253 on the right (see display_line), if there is one, to
18254 preserve the screen position of the truncation glyphs on
18255 the right. */
18256 int w = 0;
18257 struct glyph *g = to;
18258 short used;
18259
18260 while (g < toend && it->glyph_row->x + w < 0)
18261 {
18262 w += g->pixel_width;
18263 ++g;
18264 }
18265 it->glyph_row->x = 0;
18266 w = 0;
18267 while (g < toend && w < it->truncation_pixel_width)
18268 {
18269 w += g->pixel_width;
18270 ++g;
18271 }
18272 if (g - to - tused > 0)
18273 {
18274 memmove (to + tused, g, toend - g);
18275 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18276 }
18277 used = it->glyph_row->used[TEXT_AREA];
18278 if (it->glyph_row->truncated_on_right_p
18279 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18280 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18281 == STRETCH_GLYPH)
18282 {
18283 int extra = w - it->truncation_pixel_width;
18284
18285 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18286 }
18287 }
18288
18289 while (from < end)
18290 *to++ = *from++;
18291
18292 /* There may be padding glyphs left over. Overwrite them too. */
18293 if (!FRAME_WINDOW_P (it->f))
18294 {
18295 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18296 {
18297 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18298 while (from < end)
18299 *to++ = *from++;
18300 }
18301 }
18302
18303 if (to > toend)
18304 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18305 }
18306 else
18307 {
18308 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18309
18310 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18311 that back to front. */
18312 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18313 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18314 toend = it->glyph_row->glyphs[TEXT_AREA];
18315 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18316 if (FRAME_WINDOW_P (it->f))
18317 {
18318 int w = 0;
18319 struct glyph *g = to;
18320
18321 while (g >= toend && w < it->truncation_pixel_width)
18322 {
18323 w += g->pixel_width;
18324 --g;
18325 }
18326 if (to - g - tused > 0)
18327 to = g + tused;
18328 if (it->glyph_row->truncated_on_right_p
18329 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18330 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18331 {
18332 int extra = w - it->truncation_pixel_width;
18333
18334 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18335 }
18336 }
18337
18338 while (from >= end && to >= toend)
18339 *to-- = *from--;
18340 if (!FRAME_WINDOW_P (it->f))
18341 {
18342 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18343 {
18344 from =
18345 truncate_it.glyph_row->glyphs[TEXT_AREA]
18346 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18347 while (from >= end && to >= toend)
18348 *to-- = *from--;
18349 }
18350 }
18351 if (from >= end)
18352 {
18353 /* Need to free some room before prepending additional
18354 glyphs. */
18355 int move_by = from - end + 1;
18356 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18357 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18358
18359 for ( ; g >= g0; g--)
18360 g[move_by] = *g;
18361 while (from >= end)
18362 *to-- = *from--;
18363 it->glyph_row->used[TEXT_AREA] += move_by;
18364 }
18365 }
18366 }
18367
18368 /* Compute the hash code for ROW. */
18369 unsigned
18370 row_hash (struct glyph_row *row)
18371 {
18372 int area, k;
18373 unsigned hashval = 0;
18374
18375 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18376 for (k = 0; k < row->used[area]; ++k)
18377 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18378 + row->glyphs[area][k].u.val
18379 + row->glyphs[area][k].face_id
18380 + row->glyphs[area][k].padding_p
18381 + (row->glyphs[area][k].type << 2));
18382
18383 return hashval;
18384 }
18385
18386 /* Compute the pixel height and width of IT->glyph_row.
18387
18388 Most of the time, ascent and height of a display line will be equal
18389 to the max_ascent and max_height values of the display iterator
18390 structure. This is not the case if
18391
18392 1. We hit ZV without displaying anything. In this case, max_ascent
18393 and max_height will be zero.
18394
18395 2. We have some glyphs that don't contribute to the line height.
18396 (The glyph row flag contributes_to_line_height_p is for future
18397 pixmap extensions).
18398
18399 The first case is easily covered by using default values because in
18400 these cases, the line height does not really matter, except that it
18401 must not be zero. */
18402
18403 static void
18404 compute_line_metrics (struct it *it)
18405 {
18406 struct glyph_row *row = it->glyph_row;
18407
18408 if (FRAME_WINDOW_P (it->f))
18409 {
18410 int i, min_y, max_y;
18411
18412 /* The line may consist of one space only, that was added to
18413 place the cursor on it. If so, the row's height hasn't been
18414 computed yet. */
18415 if (row->height == 0)
18416 {
18417 if (it->max_ascent + it->max_descent == 0)
18418 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18419 row->ascent = it->max_ascent;
18420 row->height = it->max_ascent + it->max_descent;
18421 row->phys_ascent = it->max_phys_ascent;
18422 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18423 row->extra_line_spacing = it->max_extra_line_spacing;
18424 }
18425
18426 /* Compute the width of this line. */
18427 row->pixel_width = row->x;
18428 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18429 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18430
18431 eassert (row->pixel_width >= 0);
18432 eassert (row->ascent >= 0 && row->height > 0);
18433
18434 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18435 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18436
18437 /* If first line's physical ascent is larger than its logical
18438 ascent, use the physical ascent, and make the row taller.
18439 This makes accented characters fully visible. */
18440 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18441 && row->phys_ascent > row->ascent)
18442 {
18443 row->height += row->phys_ascent - row->ascent;
18444 row->ascent = row->phys_ascent;
18445 }
18446
18447 /* Compute how much of the line is visible. */
18448 row->visible_height = row->height;
18449
18450 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18451 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18452
18453 if (row->y < min_y)
18454 row->visible_height -= min_y - row->y;
18455 if (row->y + row->height > max_y)
18456 row->visible_height -= row->y + row->height - max_y;
18457 }
18458 else
18459 {
18460 row->pixel_width = row->used[TEXT_AREA];
18461 if (row->continued_p)
18462 row->pixel_width -= it->continuation_pixel_width;
18463 else if (row->truncated_on_right_p)
18464 row->pixel_width -= it->truncation_pixel_width;
18465 row->ascent = row->phys_ascent = 0;
18466 row->height = row->phys_height = row->visible_height = 1;
18467 row->extra_line_spacing = 0;
18468 }
18469
18470 /* Compute a hash code for this row. */
18471 row->hash = row_hash (row);
18472
18473 it->max_ascent = it->max_descent = 0;
18474 it->max_phys_ascent = it->max_phys_descent = 0;
18475 }
18476
18477
18478 /* Append one space to the glyph row of iterator IT if doing a
18479 window-based redisplay. The space has the same face as
18480 IT->face_id. Value is non-zero if a space was added.
18481
18482 This function is called to make sure that there is always one glyph
18483 at the end of a glyph row that the cursor can be set on under
18484 window-systems. (If there weren't such a glyph we would not know
18485 how wide and tall a box cursor should be displayed).
18486
18487 At the same time this space let's a nicely handle clearing to the
18488 end of the line if the row ends in italic text. */
18489
18490 static int
18491 append_space_for_newline (struct it *it, int default_face_p)
18492 {
18493 if (FRAME_WINDOW_P (it->f))
18494 {
18495 int n = it->glyph_row->used[TEXT_AREA];
18496
18497 if (it->glyph_row->glyphs[TEXT_AREA] + n
18498 < it->glyph_row->glyphs[1 + TEXT_AREA])
18499 {
18500 /* Save some values that must not be changed.
18501 Must save IT->c and IT->len because otherwise
18502 ITERATOR_AT_END_P wouldn't work anymore after
18503 append_space_for_newline has been called. */
18504 enum display_element_type saved_what = it->what;
18505 int saved_c = it->c, saved_len = it->len;
18506 int saved_char_to_display = it->char_to_display;
18507 int saved_x = it->current_x;
18508 int saved_face_id = it->face_id;
18509 struct text_pos saved_pos;
18510 Lisp_Object saved_object;
18511 struct face *face;
18512
18513 saved_object = it->object;
18514 saved_pos = it->position;
18515
18516 it->what = IT_CHARACTER;
18517 memset (&it->position, 0, sizeof it->position);
18518 it->object = make_number (0);
18519 it->c = it->char_to_display = ' ';
18520 it->len = 1;
18521
18522 /* If the default face was remapped, be sure to use the
18523 remapped face for the appended newline. */
18524 if (default_face_p)
18525 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18526 else if (it->face_before_selective_p)
18527 it->face_id = it->saved_face_id;
18528 face = FACE_FROM_ID (it->f, it->face_id);
18529 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18530
18531 PRODUCE_GLYPHS (it);
18532
18533 it->override_ascent = -1;
18534 it->constrain_row_ascent_descent_p = 0;
18535 it->current_x = saved_x;
18536 it->object = saved_object;
18537 it->position = saved_pos;
18538 it->what = saved_what;
18539 it->face_id = saved_face_id;
18540 it->len = saved_len;
18541 it->c = saved_c;
18542 it->char_to_display = saved_char_to_display;
18543 return 1;
18544 }
18545 }
18546
18547 return 0;
18548 }
18549
18550
18551 /* Extend the face of the last glyph in the text area of IT->glyph_row
18552 to the end of the display line. Called from display_line. If the
18553 glyph row is empty, add a space glyph to it so that we know the
18554 face to draw. Set the glyph row flag fill_line_p. If the glyph
18555 row is R2L, prepend a stretch glyph to cover the empty space to the
18556 left of the leftmost glyph. */
18557
18558 static void
18559 extend_face_to_end_of_line (struct it *it)
18560 {
18561 struct face *face, *default_face;
18562 struct frame *f = it->f;
18563
18564 /* If line is already filled, do nothing. Non window-system frames
18565 get a grace of one more ``pixel'' because their characters are
18566 1-``pixel'' wide, so they hit the equality too early. This grace
18567 is needed only for R2L rows that are not continued, to produce
18568 one extra blank where we could display the cursor. */
18569 if (it->current_x >= it->last_visible_x
18570 + (!FRAME_WINDOW_P (f)
18571 && it->glyph_row->reversed_p
18572 && !it->glyph_row->continued_p))
18573 return;
18574
18575 /* The default face, possibly remapped. */
18576 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18577
18578 /* Face extension extends the background and box of IT->face_id
18579 to the end of the line. If the background equals the background
18580 of the frame, we don't have to do anything. */
18581 if (it->face_before_selective_p)
18582 face = FACE_FROM_ID (f, it->saved_face_id);
18583 else
18584 face = FACE_FROM_ID (f, it->face_id);
18585
18586 if (FRAME_WINDOW_P (f)
18587 && it->glyph_row->displays_text_p
18588 && face->box == FACE_NO_BOX
18589 && face->background == FRAME_BACKGROUND_PIXEL (f)
18590 && !face->stipple
18591 && !it->glyph_row->reversed_p)
18592 return;
18593
18594 /* Set the glyph row flag indicating that the face of the last glyph
18595 in the text area has to be drawn to the end of the text area. */
18596 it->glyph_row->fill_line_p = 1;
18597
18598 /* If current character of IT is not ASCII, make sure we have the
18599 ASCII face. This will be automatically undone the next time
18600 get_next_display_element returns a multibyte character. Note
18601 that the character will always be single byte in unibyte
18602 text. */
18603 if (!ASCII_CHAR_P (it->c))
18604 {
18605 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18606 }
18607
18608 if (FRAME_WINDOW_P (f))
18609 {
18610 /* If the row is empty, add a space with the current face of IT,
18611 so that we know which face to draw. */
18612 if (it->glyph_row->used[TEXT_AREA] == 0)
18613 {
18614 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18615 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18616 it->glyph_row->used[TEXT_AREA] = 1;
18617 }
18618 #ifdef HAVE_WINDOW_SYSTEM
18619 if (it->glyph_row->reversed_p)
18620 {
18621 /* Prepend a stretch glyph to the row, such that the
18622 rightmost glyph will be drawn flushed all the way to the
18623 right margin of the window. The stretch glyph that will
18624 occupy the empty space, if any, to the left of the
18625 glyphs. */
18626 struct font *font = face->font ? face->font : FRAME_FONT (f);
18627 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18628 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18629 struct glyph *g;
18630 int row_width, stretch_ascent, stretch_width;
18631 struct text_pos saved_pos;
18632 int saved_face_id, saved_avoid_cursor;
18633
18634 for (row_width = 0, g = row_start; g < row_end; g++)
18635 row_width += g->pixel_width;
18636 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18637 if (stretch_width > 0)
18638 {
18639 stretch_ascent =
18640 (((it->ascent + it->descent)
18641 * FONT_BASE (font)) / FONT_HEIGHT (font));
18642 saved_pos = it->position;
18643 memset (&it->position, 0, sizeof it->position);
18644 saved_avoid_cursor = it->avoid_cursor_p;
18645 it->avoid_cursor_p = 1;
18646 saved_face_id = it->face_id;
18647 /* The last row's stretch glyph should get the default
18648 face, to avoid painting the rest of the window with
18649 the region face, if the region ends at ZV. */
18650 if (it->glyph_row->ends_at_zv_p)
18651 it->face_id = default_face->id;
18652 else
18653 it->face_id = face->id;
18654 append_stretch_glyph (it, make_number (0), stretch_width,
18655 it->ascent + it->descent, stretch_ascent);
18656 it->position = saved_pos;
18657 it->avoid_cursor_p = saved_avoid_cursor;
18658 it->face_id = saved_face_id;
18659 }
18660 }
18661 #endif /* HAVE_WINDOW_SYSTEM */
18662 }
18663 else
18664 {
18665 /* Save some values that must not be changed. */
18666 int saved_x = it->current_x;
18667 struct text_pos saved_pos;
18668 Lisp_Object saved_object;
18669 enum display_element_type saved_what = it->what;
18670 int saved_face_id = it->face_id;
18671
18672 saved_object = it->object;
18673 saved_pos = it->position;
18674
18675 it->what = IT_CHARACTER;
18676 memset (&it->position, 0, sizeof it->position);
18677 it->object = make_number (0);
18678 it->c = it->char_to_display = ' ';
18679 it->len = 1;
18680 /* The last row's blank glyphs should get the default face, to
18681 avoid painting the rest of the window with the region face,
18682 if the region ends at ZV. */
18683 if (it->glyph_row->ends_at_zv_p)
18684 it->face_id = default_face->id;
18685 else
18686 it->face_id = face->id;
18687
18688 PRODUCE_GLYPHS (it);
18689
18690 while (it->current_x <= it->last_visible_x)
18691 PRODUCE_GLYPHS (it);
18692
18693 /* Don't count these blanks really. It would let us insert a left
18694 truncation glyph below and make us set the cursor on them, maybe. */
18695 it->current_x = saved_x;
18696 it->object = saved_object;
18697 it->position = saved_pos;
18698 it->what = saved_what;
18699 it->face_id = saved_face_id;
18700 }
18701 }
18702
18703
18704 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18705 trailing whitespace. */
18706
18707 static int
18708 trailing_whitespace_p (ptrdiff_t charpos)
18709 {
18710 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18711 int c = 0;
18712
18713 while (bytepos < ZV_BYTE
18714 && (c = FETCH_CHAR (bytepos),
18715 c == ' ' || c == '\t'))
18716 ++bytepos;
18717
18718 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18719 {
18720 if (bytepos != PT_BYTE)
18721 return 1;
18722 }
18723 return 0;
18724 }
18725
18726
18727 /* Highlight trailing whitespace, if any, in ROW. */
18728
18729 static void
18730 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18731 {
18732 int used = row->used[TEXT_AREA];
18733
18734 if (used)
18735 {
18736 struct glyph *start = row->glyphs[TEXT_AREA];
18737 struct glyph *glyph = start + used - 1;
18738
18739 if (row->reversed_p)
18740 {
18741 /* Right-to-left rows need to be processed in the opposite
18742 direction, so swap the edge pointers. */
18743 glyph = start;
18744 start = row->glyphs[TEXT_AREA] + used - 1;
18745 }
18746
18747 /* Skip over glyphs inserted to display the cursor at the
18748 end of a line, for extending the face of the last glyph
18749 to the end of the line on terminals, and for truncation
18750 and continuation glyphs. */
18751 if (!row->reversed_p)
18752 {
18753 while (glyph >= start
18754 && glyph->type == CHAR_GLYPH
18755 && INTEGERP (glyph->object))
18756 --glyph;
18757 }
18758 else
18759 {
18760 while (glyph <= start
18761 && glyph->type == CHAR_GLYPH
18762 && INTEGERP (glyph->object))
18763 ++glyph;
18764 }
18765
18766 /* If last glyph is a space or stretch, and it's trailing
18767 whitespace, set the face of all trailing whitespace glyphs in
18768 IT->glyph_row to `trailing-whitespace'. */
18769 if ((row->reversed_p ? glyph <= start : glyph >= start)
18770 && BUFFERP (glyph->object)
18771 && (glyph->type == STRETCH_GLYPH
18772 || (glyph->type == CHAR_GLYPH
18773 && glyph->u.ch == ' '))
18774 && trailing_whitespace_p (glyph->charpos))
18775 {
18776 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18777 if (face_id < 0)
18778 return;
18779
18780 if (!row->reversed_p)
18781 {
18782 while (glyph >= start
18783 && BUFFERP (glyph->object)
18784 && (glyph->type == STRETCH_GLYPH
18785 || (glyph->type == CHAR_GLYPH
18786 && glyph->u.ch == ' ')))
18787 (glyph--)->face_id = face_id;
18788 }
18789 else
18790 {
18791 while (glyph <= start
18792 && BUFFERP (glyph->object)
18793 && (glyph->type == STRETCH_GLYPH
18794 || (glyph->type == CHAR_GLYPH
18795 && glyph->u.ch == ' ')))
18796 (glyph++)->face_id = face_id;
18797 }
18798 }
18799 }
18800 }
18801
18802
18803 /* Value is non-zero if glyph row ROW should be
18804 used to hold the cursor. */
18805
18806 static int
18807 cursor_row_p (struct glyph_row *row)
18808 {
18809 int result = 1;
18810
18811 if (PT == CHARPOS (row->end.pos)
18812 || PT == MATRIX_ROW_END_CHARPOS (row))
18813 {
18814 /* Suppose the row ends on a string.
18815 Unless the row is continued, that means it ends on a newline
18816 in the string. If it's anything other than a display string
18817 (e.g., a before-string from an overlay), we don't want the
18818 cursor there. (This heuristic seems to give the optimal
18819 behavior for the various types of multi-line strings.)
18820 One exception: if the string has `cursor' property on one of
18821 its characters, we _do_ want the cursor there. */
18822 if (CHARPOS (row->end.string_pos) >= 0)
18823 {
18824 if (row->continued_p)
18825 result = 1;
18826 else
18827 {
18828 /* Check for `display' property. */
18829 struct glyph *beg = row->glyphs[TEXT_AREA];
18830 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18831 struct glyph *glyph;
18832
18833 result = 0;
18834 for (glyph = end; glyph >= beg; --glyph)
18835 if (STRINGP (glyph->object))
18836 {
18837 Lisp_Object prop
18838 = Fget_char_property (make_number (PT),
18839 Qdisplay, Qnil);
18840 result =
18841 (!NILP (prop)
18842 && display_prop_string_p (prop, glyph->object));
18843 /* If there's a `cursor' property on one of the
18844 string's characters, this row is a cursor row,
18845 even though this is not a display string. */
18846 if (!result)
18847 {
18848 Lisp_Object s = glyph->object;
18849
18850 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18851 {
18852 ptrdiff_t gpos = glyph->charpos;
18853
18854 if (!NILP (Fget_char_property (make_number (gpos),
18855 Qcursor, s)))
18856 {
18857 result = 1;
18858 break;
18859 }
18860 }
18861 }
18862 break;
18863 }
18864 }
18865 }
18866 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18867 {
18868 /* If the row ends in middle of a real character,
18869 and the line is continued, we want the cursor here.
18870 That's because CHARPOS (ROW->end.pos) would equal
18871 PT if PT is before the character. */
18872 if (!row->ends_in_ellipsis_p)
18873 result = row->continued_p;
18874 else
18875 /* If the row ends in an ellipsis, then
18876 CHARPOS (ROW->end.pos) will equal point after the
18877 invisible text. We want that position to be displayed
18878 after the ellipsis. */
18879 result = 0;
18880 }
18881 /* If the row ends at ZV, display the cursor at the end of that
18882 row instead of at the start of the row below. */
18883 else if (row->ends_at_zv_p)
18884 result = 1;
18885 else
18886 result = 0;
18887 }
18888
18889 return result;
18890 }
18891
18892 \f
18893
18894 /* Push the property PROP so that it will be rendered at the current
18895 position in IT. Return 1 if PROP was successfully pushed, 0
18896 otherwise. Called from handle_line_prefix to handle the
18897 `line-prefix' and `wrap-prefix' properties. */
18898
18899 static int
18900 push_prefix_prop (struct it *it, Lisp_Object prop)
18901 {
18902 struct text_pos pos =
18903 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18904
18905 eassert (it->method == GET_FROM_BUFFER
18906 || it->method == GET_FROM_DISPLAY_VECTOR
18907 || it->method == GET_FROM_STRING);
18908
18909 /* We need to save the current buffer/string position, so it will be
18910 restored by pop_it, because iterate_out_of_display_property
18911 depends on that being set correctly, but some situations leave
18912 it->position not yet set when this function is called. */
18913 push_it (it, &pos);
18914
18915 if (STRINGP (prop))
18916 {
18917 if (SCHARS (prop) == 0)
18918 {
18919 pop_it (it);
18920 return 0;
18921 }
18922
18923 it->string = prop;
18924 it->string_from_prefix_prop_p = 1;
18925 it->multibyte_p = STRING_MULTIBYTE (it->string);
18926 it->current.overlay_string_index = -1;
18927 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18928 it->end_charpos = it->string_nchars = SCHARS (it->string);
18929 it->method = GET_FROM_STRING;
18930 it->stop_charpos = 0;
18931 it->prev_stop = 0;
18932 it->base_level_stop = 0;
18933
18934 /* Force paragraph direction to be that of the parent
18935 buffer/string. */
18936 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18937 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18938 else
18939 it->paragraph_embedding = L2R;
18940
18941 /* Set up the bidi iterator for this display string. */
18942 if (it->bidi_p)
18943 {
18944 it->bidi_it.string.lstring = it->string;
18945 it->bidi_it.string.s = NULL;
18946 it->bidi_it.string.schars = it->end_charpos;
18947 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18948 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18949 it->bidi_it.string.unibyte = !it->multibyte_p;
18950 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18951 }
18952 }
18953 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18954 {
18955 it->method = GET_FROM_STRETCH;
18956 it->object = prop;
18957 }
18958 #ifdef HAVE_WINDOW_SYSTEM
18959 else if (IMAGEP (prop))
18960 {
18961 it->what = IT_IMAGE;
18962 it->image_id = lookup_image (it->f, prop);
18963 it->method = GET_FROM_IMAGE;
18964 }
18965 #endif /* HAVE_WINDOW_SYSTEM */
18966 else
18967 {
18968 pop_it (it); /* bogus display property, give up */
18969 return 0;
18970 }
18971
18972 return 1;
18973 }
18974
18975 /* Return the character-property PROP at the current position in IT. */
18976
18977 static Lisp_Object
18978 get_it_property (struct it *it, Lisp_Object prop)
18979 {
18980 Lisp_Object position;
18981
18982 if (STRINGP (it->object))
18983 position = make_number (IT_STRING_CHARPOS (*it));
18984 else if (BUFFERP (it->object))
18985 position = make_number (IT_CHARPOS (*it));
18986 else
18987 return Qnil;
18988
18989 return Fget_char_property (position, prop, it->object);
18990 }
18991
18992 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18993
18994 static void
18995 handle_line_prefix (struct it *it)
18996 {
18997 Lisp_Object prefix;
18998
18999 if (it->continuation_lines_width > 0)
19000 {
19001 prefix = get_it_property (it, Qwrap_prefix);
19002 if (NILP (prefix))
19003 prefix = Vwrap_prefix;
19004 }
19005 else
19006 {
19007 prefix = get_it_property (it, Qline_prefix);
19008 if (NILP (prefix))
19009 prefix = Vline_prefix;
19010 }
19011 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19012 {
19013 /* If the prefix is wider than the window, and we try to wrap
19014 it, it would acquire its own wrap prefix, and so on till the
19015 iterator stack overflows. So, don't wrap the prefix. */
19016 it->line_wrap = TRUNCATE;
19017 it->avoid_cursor_p = 1;
19018 }
19019 }
19020
19021 \f
19022
19023 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19024 only for R2L lines from display_line and display_string, when they
19025 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19026 the line/string needs to be continued on the next glyph row. */
19027 static void
19028 unproduce_glyphs (struct it *it, int n)
19029 {
19030 struct glyph *glyph, *end;
19031
19032 eassert (it->glyph_row);
19033 eassert (it->glyph_row->reversed_p);
19034 eassert (it->area == TEXT_AREA);
19035 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19036
19037 if (n > it->glyph_row->used[TEXT_AREA])
19038 n = it->glyph_row->used[TEXT_AREA];
19039 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19040 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19041 for ( ; glyph < end; glyph++)
19042 glyph[-n] = *glyph;
19043 }
19044
19045 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19046 and ROW->maxpos. */
19047 static void
19048 find_row_edges (struct it *it, struct glyph_row *row,
19049 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19050 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19051 {
19052 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19053 lines' rows is implemented for bidi-reordered rows. */
19054
19055 /* ROW->minpos is the value of min_pos, the minimal buffer position
19056 we have in ROW, or ROW->start.pos if that is smaller. */
19057 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19058 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19059 else
19060 /* We didn't find buffer positions smaller than ROW->start, or
19061 didn't find _any_ valid buffer positions in any of the glyphs,
19062 so we must trust the iterator's computed positions. */
19063 row->minpos = row->start.pos;
19064 if (max_pos <= 0)
19065 {
19066 max_pos = CHARPOS (it->current.pos);
19067 max_bpos = BYTEPOS (it->current.pos);
19068 }
19069
19070 /* Here are the various use-cases for ending the row, and the
19071 corresponding values for ROW->maxpos:
19072
19073 Line ends in a newline from buffer eol_pos + 1
19074 Line is continued from buffer max_pos + 1
19075 Line is truncated on right it->current.pos
19076 Line ends in a newline from string max_pos + 1(*)
19077 (*) + 1 only when line ends in a forward scan
19078 Line is continued from string max_pos
19079 Line is continued from display vector max_pos
19080 Line is entirely from a string min_pos == max_pos
19081 Line is entirely from a display vector min_pos == max_pos
19082 Line that ends at ZV ZV
19083
19084 If you discover other use-cases, please add them here as
19085 appropriate. */
19086 if (row->ends_at_zv_p)
19087 row->maxpos = it->current.pos;
19088 else if (row->used[TEXT_AREA])
19089 {
19090 int seen_this_string = 0;
19091 struct glyph_row *r1 = row - 1;
19092
19093 /* Did we see the same display string on the previous row? */
19094 if (STRINGP (it->object)
19095 /* this is not the first row */
19096 && row > it->w->desired_matrix->rows
19097 /* previous row is not the header line */
19098 && !r1->mode_line_p
19099 /* previous row also ends in a newline from a string */
19100 && r1->ends_in_newline_from_string_p)
19101 {
19102 struct glyph *start, *end;
19103
19104 /* Search for the last glyph of the previous row that came
19105 from buffer or string. Depending on whether the row is
19106 L2R or R2L, we need to process it front to back or the
19107 other way round. */
19108 if (!r1->reversed_p)
19109 {
19110 start = r1->glyphs[TEXT_AREA];
19111 end = start + r1->used[TEXT_AREA];
19112 /* Glyphs inserted by redisplay have an integer (zero)
19113 as their object. */
19114 while (end > start
19115 && INTEGERP ((end - 1)->object)
19116 && (end - 1)->charpos <= 0)
19117 --end;
19118 if (end > start)
19119 {
19120 if (EQ ((end - 1)->object, it->object))
19121 seen_this_string = 1;
19122 }
19123 else
19124 /* If all the glyphs of the previous row were inserted
19125 by redisplay, it means the previous row was
19126 produced from a single newline, which is only
19127 possible if that newline came from the same string
19128 as the one which produced this ROW. */
19129 seen_this_string = 1;
19130 }
19131 else
19132 {
19133 end = r1->glyphs[TEXT_AREA] - 1;
19134 start = end + r1->used[TEXT_AREA];
19135 while (end < start
19136 && INTEGERP ((end + 1)->object)
19137 && (end + 1)->charpos <= 0)
19138 ++end;
19139 if (end < start)
19140 {
19141 if (EQ ((end + 1)->object, it->object))
19142 seen_this_string = 1;
19143 }
19144 else
19145 seen_this_string = 1;
19146 }
19147 }
19148 /* Take note of each display string that covers a newline only
19149 once, the first time we see it. This is for when a display
19150 string includes more than one newline in it. */
19151 if (row->ends_in_newline_from_string_p && !seen_this_string)
19152 {
19153 /* If we were scanning the buffer forward when we displayed
19154 the string, we want to account for at least one buffer
19155 position that belongs to this row (position covered by
19156 the display string), so that cursor positioning will
19157 consider this row as a candidate when point is at the end
19158 of the visual line represented by this row. This is not
19159 required when scanning back, because max_pos will already
19160 have a much larger value. */
19161 if (CHARPOS (row->end.pos) > max_pos)
19162 INC_BOTH (max_pos, max_bpos);
19163 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19164 }
19165 else if (CHARPOS (it->eol_pos) > 0)
19166 SET_TEXT_POS (row->maxpos,
19167 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19168 else if (row->continued_p)
19169 {
19170 /* If max_pos is different from IT's current position, it
19171 means IT->method does not belong to the display element
19172 at max_pos. However, it also means that the display
19173 element at max_pos was displayed in its entirety on this
19174 line, which is equivalent to saying that the next line
19175 starts at the next buffer position. */
19176 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19177 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19178 else
19179 {
19180 INC_BOTH (max_pos, max_bpos);
19181 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19182 }
19183 }
19184 else if (row->truncated_on_right_p)
19185 /* display_line already called reseat_at_next_visible_line_start,
19186 which puts the iterator at the beginning of the next line, in
19187 the logical order. */
19188 row->maxpos = it->current.pos;
19189 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19190 /* A line that is entirely from a string/image/stretch... */
19191 row->maxpos = row->minpos;
19192 else
19193 abort ();
19194 }
19195 else
19196 row->maxpos = it->current.pos;
19197 }
19198
19199 /* Construct the glyph row IT->glyph_row in the desired matrix of
19200 IT->w from text at the current position of IT. See dispextern.h
19201 for an overview of struct it. Value is non-zero if
19202 IT->glyph_row displays text, as opposed to a line displaying ZV
19203 only. */
19204
19205 static int
19206 display_line (struct it *it)
19207 {
19208 struct glyph_row *row = it->glyph_row;
19209 Lisp_Object overlay_arrow_string;
19210 struct it wrap_it;
19211 void *wrap_data = NULL;
19212 int may_wrap = 0, wrap_x IF_LINT (= 0);
19213 int wrap_row_used = -1;
19214 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19215 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19216 int wrap_row_extra_line_spacing IF_LINT (= 0);
19217 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19218 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19219 int cvpos;
19220 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19221 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19222
19223 /* We always start displaying at hpos zero even if hscrolled. */
19224 eassert (it->hpos == 0 && it->current_x == 0);
19225
19226 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19227 >= it->w->desired_matrix->nrows)
19228 {
19229 it->w->nrows_scale_factor++;
19230 fonts_changed_p = 1;
19231 return 0;
19232 }
19233
19234 /* Is IT->w showing the region? */
19235 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19236
19237 /* Clear the result glyph row and enable it. */
19238 prepare_desired_row (row);
19239
19240 row->y = it->current_y;
19241 row->start = it->start;
19242 row->continuation_lines_width = it->continuation_lines_width;
19243 row->displays_text_p = 1;
19244 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19245 it->starts_in_middle_of_char_p = 0;
19246
19247 /* Arrange the overlays nicely for our purposes. Usually, we call
19248 display_line on only one line at a time, in which case this
19249 can't really hurt too much, or we call it on lines which appear
19250 one after another in the buffer, in which case all calls to
19251 recenter_overlay_lists but the first will be pretty cheap. */
19252 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19253
19254 /* Move over display elements that are not visible because we are
19255 hscrolled. This may stop at an x-position < IT->first_visible_x
19256 if the first glyph is partially visible or if we hit a line end. */
19257 if (it->current_x < it->first_visible_x)
19258 {
19259 enum move_it_result move_result;
19260
19261 this_line_min_pos = row->start.pos;
19262 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19263 MOVE_TO_POS | MOVE_TO_X);
19264 /* If we are under a large hscroll, move_it_in_display_line_to
19265 could hit the end of the line without reaching
19266 it->first_visible_x. Pretend that we did reach it. This is
19267 especially important on a TTY, where we will call
19268 extend_face_to_end_of_line, which needs to know how many
19269 blank glyphs to produce. */
19270 if (it->current_x < it->first_visible_x
19271 && (move_result == MOVE_NEWLINE_OR_CR
19272 || move_result == MOVE_POS_MATCH_OR_ZV))
19273 it->current_x = it->first_visible_x;
19274
19275 /* Record the smallest positions seen while we moved over
19276 display elements that are not visible. This is needed by
19277 redisplay_internal for optimizing the case where the cursor
19278 stays inside the same line. The rest of this function only
19279 considers positions that are actually displayed, so
19280 RECORD_MAX_MIN_POS will not otherwise record positions that
19281 are hscrolled to the left of the left edge of the window. */
19282 min_pos = CHARPOS (this_line_min_pos);
19283 min_bpos = BYTEPOS (this_line_min_pos);
19284 }
19285 else
19286 {
19287 /* We only do this when not calling `move_it_in_display_line_to'
19288 above, because move_it_in_display_line_to calls
19289 handle_line_prefix itself. */
19290 handle_line_prefix (it);
19291 }
19292
19293 /* Get the initial row height. This is either the height of the
19294 text hscrolled, if there is any, or zero. */
19295 row->ascent = it->max_ascent;
19296 row->height = it->max_ascent + it->max_descent;
19297 row->phys_ascent = it->max_phys_ascent;
19298 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19299 row->extra_line_spacing = it->max_extra_line_spacing;
19300
19301 /* Utility macro to record max and min buffer positions seen until now. */
19302 #define RECORD_MAX_MIN_POS(IT) \
19303 do \
19304 { \
19305 int composition_p = !STRINGP ((IT)->string) \
19306 && ((IT)->what == IT_COMPOSITION); \
19307 ptrdiff_t current_pos = \
19308 composition_p ? (IT)->cmp_it.charpos \
19309 : IT_CHARPOS (*(IT)); \
19310 ptrdiff_t current_bpos = \
19311 composition_p ? CHAR_TO_BYTE (current_pos) \
19312 : IT_BYTEPOS (*(IT)); \
19313 if (current_pos < min_pos) \
19314 { \
19315 min_pos = current_pos; \
19316 min_bpos = current_bpos; \
19317 } \
19318 if (IT_CHARPOS (*it) > max_pos) \
19319 { \
19320 max_pos = IT_CHARPOS (*it); \
19321 max_bpos = IT_BYTEPOS (*it); \
19322 } \
19323 } \
19324 while (0)
19325
19326 /* Loop generating characters. The loop is left with IT on the next
19327 character to display. */
19328 while (1)
19329 {
19330 int n_glyphs_before, hpos_before, x_before;
19331 int x, nglyphs;
19332 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19333
19334 /* Retrieve the next thing to display. Value is zero if end of
19335 buffer reached. */
19336 if (!get_next_display_element (it))
19337 {
19338 /* Maybe add a space at the end of this line that is used to
19339 display the cursor there under X. Set the charpos of the
19340 first glyph of blank lines not corresponding to any text
19341 to -1. */
19342 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19343 row->exact_window_width_line_p = 1;
19344 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19345 || row->used[TEXT_AREA] == 0)
19346 {
19347 row->glyphs[TEXT_AREA]->charpos = -1;
19348 row->displays_text_p = 0;
19349
19350 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19351 && (!MINI_WINDOW_P (it->w)
19352 || (minibuf_level && EQ (it->window, minibuf_window))))
19353 row->indicate_empty_line_p = 1;
19354 }
19355
19356 it->continuation_lines_width = 0;
19357 row->ends_at_zv_p = 1;
19358 /* A row that displays right-to-left text must always have
19359 its last face extended all the way to the end of line,
19360 even if this row ends in ZV, because we still write to
19361 the screen left to right. We also need to extend the
19362 last face if the default face is remapped to some
19363 different face, otherwise the functions that clear
19364 portions of the screen will clear with the default face's
19365 background color. */
19366 if (row->reversed_p
19367 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19368 extend_face_to_end_of_line (it);
19369 break;
19370 }
19371
19372 /* Now, get the metrics of what we want to display. This also
19373 generates glyphs in `row' (which is IT->glyph_row). */
19374 n_glyphs_before = row->used[TEXT_AREA];
19375 x = it->current_x;
19376
19377 /* Remember the line height so far in case the next element doesn't
19378 fit on the line. */
19379 if (it->line_wrap != TRUNCATE)
19380 {
19381 ascent = it->max_ascent;
19382 descent = it->max_descent;
19383 phys_ascent = it->max_phys_ascent;
19384 phys_descent = it->max_phys_descent;
19385
19386 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19387 {
19388 if (IT_DISPLAYING_WHITESPACE (it))
19389 may_wrap = 1;
19390 else if (may_wrap)
19391 {
19392 SAVE_IT (wrap_it, *it, wrap_data);
19393 wrap_x = x;
19394 wrap_row_used = row->used[TEXT_AREA];
19395 wrap_row_ascent = row->ascent;
19396 wrap_row_height = row->height;
19397 wrap_row_phys_ascent = row->phys_ascent;
19398 wrap_row_phys_height = row->phys_height;
19399 wrap_row_extra_line_spacing = row->extra_line_spacing;
19400 wrap_row_min_pos = min_pos;
19401 wrap_row_min_bpos = min_bpos;
19402 wrap_row_max_pos = max_pos;
19403 wrap_row_max_bpos = max_bpos;
19404 may_wrap = 0;
19405 }
19406 }
19407 }
19408
19409 PRODUCE_GLYPHS (it);
19410
19411 /* If this display element was in marginal areas, continue with
19412 the next one. */
19413 if (it->area != TEXT_AREA)
19414 {
19415 row->ascent = max (row->ascent, it->max_ascent);
19416 row->height = max (row->height, it->max_ascent + it->max_descent);
19417 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19418 row->phys_height = max (row->phys_height,
19419 it->max_phys_ascent + it->max_phys_descent);
19420 row->extra_line_spacing = max (row->extra_line_spacing,
19421 it->max_extra_line_spacing);
19422 set_iterator_to_next (it, 1);
19423 continue;
19424 }
19425
19426 /* Does the display element fit on the line? If we truncate
19427 lines, we should draw past the right edge of the window. If
19428 we don't truncate, we want to stop so that we can display the
19429 continuation glyph before the right margin. If lines are
19430 continued, there are two possible strategies for characters
19431 resulting in more than 1 glyph (e.g. tabs): Display as many
19432 glyphs as possible in this line and leave the rest for the
19433 continuation line, or display the whole element in the next
19434 line. Original redisplay did the former, so we do it also. */
19435 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19436 hpos_before = it->hpos;
19437 x_before = x;
19438
19439 if (/* Not a newline. */
19440 nglyphs > 0
19441 /* Glyphs produced fit entirely in the line. */
19442 && it->current_x < it->last_visible_x)
19443 {
19444 it->hpos += nglyphs;
19445 row->ascent = max (row->ascent, it->max_ascent);
19446 row->height = max (row->height, it->max_ascent + it->max_descent);
19447 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19448 row->phys_height = max (row->phys_height,
19449 it->max_phys_ascent + it->max_phys_descent);
19450 row->extra_line_spacing = max (row->extra_line_spacing,
19451 it->max_extra_line_spacing);
19452 if (it->current_x - it->pixel_width < it->first_visible_x)
19453 row->x = x - it->first_visible_x;
19454 /* Record the maximum and minimum buffer positions seen so
19455 far in glyphs that will be displayed by this row. */
19456 if (it->bidi_p)
19457 RECORD_MAX_MIN_POS (it);
19458 }
19459 else
19460 {
19461 int i, new_x;
19462 struct glyph *glyph;
19463
19464 for (i = 0; i < nglyphs; ++i, x = new_x)
19465 {
19466 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19467 new_x = x + glyph->pixel_width;
19468
19469 if (/* Lines are continued. */
19470 it->line_wrap != TRUNCATE
19471 && (/* Glyph doesn't fit on the line. */
19472 new_x > it->last_visible_x
19473 /* Or it fits exactly on a window system frame. */
19474 || (new_x == it->last_visible_x
19475 && FRAME_WINDOW_P (it->f)
19476 && (row->reversed_p
19477 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19478 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19479 {
19480 /* End of a continued line. */
19481
19482 if (it->hpos == 0
19483 || (new_x == it->last_visible_x
19484 && FRAME_WINDOW_P (it->f)
19485 && (row->reversed_p
19486 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19487 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19488 {
19489 /* Current glyph is the only one on the line or
19490 fits exactly on the line. We must continue
19491 the line because we can't draw the cursor
19492 after the glyph. */
19493 row->continued_p = 1;
19494 it->current_x = new_x;
19495 it->continuation_lines_width += new_x;
19496 ++it->hpos;
19497 if (i == nglyphs - 1)
19498 {
19499 /* If line-wrap is on, check if a previous
19500 wrap point was found. */
19501 if (wrap_row_used > 0
19502 /* Even if there is a previous wrap
19503 point, continue the line here as
19504 usual, if (i) the previous character
19505 was a space or tab AND (ii) the
19506 current character is not. */
19507 && (!may_wrap
19508 || IT_DISPLAYING_WHITESPACE (it)))
19509 goto back_to_wrap;
19510
19511 /* Record the maximum and minimum buffer
19512 positions seen so far in glyphs that will be
19513 displayed by this row. */
19514 if (it->bidi_p)
19515 RECORD_MAX_MIN_POS (it);
19516 set_iterator_to_next (it, 1);
19517 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19518 {
19519 if (!get_next_display_element (it))
19520 {
19521 row->exact_window_width_line_p = 1;
19522 it->continuation_lines_width = 0;
19523 row->continued_p = 0;
19524 row->ends_at_zv_p = 1;
19525 }
19526 else if (ITERATOR_AT_END_OF_LINE_P (it))
19527 {
19528 row->continued_p = 0;
19529 row->exact_window_width_line_p = 1;
19530 }
19531 }
19532 }
19533 else if (it->bidi_p)
19534 RECORD_MAX_MIN_POS (it);
19535 }
19536 else if (CHAR_GLYPH_PADDING_P (*glyph)
19537 && !FRAME_WINDOW_P (it->f))
19538 {
19539 /* A padding glyph that doesn't fit on this line.
19540 This means the whole character doesn't fit
19541 on the line. */
19542 if (row->reversed_p)
19543 unproduce_glyphs (it, row->used[TEXT_AREA]
19544 - n_glyphs_before);
19545 row->used[TEXT_AREA] = n_glyphs_before;
19546
19547 /* Fill the rest of the row with continuation
19548 glyphs like in 20.x. */
19549 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19550 < row->glyphs[1 + TEXT_AREA])
19551 produce_special_glyphs (it, IT_CONTINUATION);
19552
19553 row->continued_p = 1;
19554 it->current_x = x_before;
19555 it->continuation_lines_width += x_before;
19556
19557 /* Restore the height to what it was before the
19558 element not fitting on the line. */
19559 it->max_ascent = ascent;
19560 it->max_descent = descent;
19561 it->max_phys_ascent = phys_ascent;
19562 it->max_phys_descent = phys_descent;
19563 }
19564 else if (wrap_row_used > 0)
19565 {
19566 back_to_wrap:
19567 if (row->reversed_p)
19568 unproduce_glyphs (it,
19569 row->used[TEXT_AREA] - wrap_row_used);
19570 RESTORE_IT (it, &wrap_it, wrap_data);
19571 it->continuation_lines_width += wrap_x;
19572 row->used[TEXT_AREA] = wrap_row_used;
19573 row->ascent = wrap_row_ascent;
19574 row->height = wrap_row_height;
19575 row->phys_ascent = wrap_row_phys_ascent;
19576 row->phys_height = wrap_row_phys_height;
19577 row->extra_line_spacing = wrap_row_extra_line_spacing;
19578 min_pos = wrap_row_min_pos;
19579 min_bpos = wrap_row_min_bpos;
19580 max_pos = wrap_row_max_pos;
19581 max_bpos = wrap_row_max_bpos;
19582 row->continued_p = 1;
19583 row->ends_at_zv_p = 0;
19584 row->exact_window_width_line_p = 0;
19585 it->continuation_lines_width += x;
19586
19587 /* Make sure that a non-default face is extended
19588 up to the right margin of the window. */
19589 extend_face_to_end_of_line (it);
19590 }
19591 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19592 {
19593 /* A TAB that extends past the right edge of the
19594 window. This produces a single glyph on
19595 window system frames. We leave the glyph in
19596 this row and let it fill the row, but don't
19597 consume the TAB. */
19598 if ((row->reversed_p
19599 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19600 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19601 produce_special_glyphs (it, IT_CONTINUATION);
19602 it->continuation_lines_width += it->last_visible_x;
19603 row->ends_in_middle_of_char_p = 1;
19604 row->continued_p = 1;
19605 glyph->pixel_width = it->last_visible_x - x;
19606 it->starts_in_middle_of_char_p = 1;
19607 }
19608 else
19609 {
19610 /* Something other than a TAB that draws past
19611 the right edge of the window. Restore
19612 positions to values before the element. */
19613 if (row->reversed_p)
19614 unproduce_glyphs (it, row->used[TEXT_AREA]
19615 - (n_glyphs_before + i));
19616 row->used[TEXT_AREA] = n_glyphs_before + i;
19617
19618 /* Display continuation glyphs. */
19619 if (!FRAME_WINDOW_P (it->f)
19620 || (row->reversed_p
19621 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19622 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19623 produce_special_glyphs (it, IT_CONTINUATION);
19624 row->continued_p = 1;
19625
19626 it->current_x = x_before;
19627 it->continuation_lines_width += x;
19628 extend_face_to_end_of_line (it);
19629
19630 if (nglyphs > 1 && i > 0)
19631 {
19632 row->ends_in_middle_of_char_p = 1;
19633 it->starts_in_middle_of_char_p = 1;
19634 }
19635
19636 /* Restore the height to what it was before the
19637 element not fitting on the line. */
19638 it->max_ascent = ascent;
19639 it->max_descent = descent;
19640 it->max_phys_ascent = phys_ascent;
19641 it->max_phys_descent = phys_descent;
19642 }
19643
19644 break;
19645 }
19646 else if (new_x > it->first_visible_x)
19647 {
19648 /* Increment number of glyphs actually displayed. */
19649 ++it->hpos;
19650
19651 /* Record the maximum and minimum buffer positions
19652 seen so far in glyphs that will be displayed by
19653 this row. */
19654 if (it->bidi_p)
19655 RECORD_MAX_MIN_POS (it);
19656
19657 if (x < it->first_visible_x)
19658 /* Glyph is partially visible, i.e. row starts at
19659 negative X position. */
19660 row->x = x - it->first_visible_x;
19661 }
19662 else
19663 {
19664 /* Glyph is completely off the left margin of the
19665 window. This should not happen because of the
19666 move_it_in_display_line at the start of this
19667 function, unless the text display area of the
19668 window is empty. */
19669 eassert (it->first_visible_x <= it->last_visible_x);
19670 }
19671 }
19672 /* Even if this display element produced no glyphs at all,
19673 we want to record its position. */
19674 if (it->bidi_p && nglyphs == 0)
19675 RECORD_MAX_MIN_POS (it);
19676
19677 row->ascent = max (row->ascent, it->max_ascent);
19678 row->height = max (row->height, it->max_ascent + it->max_descent);
19679 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19680 row->phys_height = max (row->phys_height,
19681 it->max_phys_ascent + it->max_phys_descent);
19682 row->extra_line_spacing = max (row->extra_line_spacing,
19683 it->max_extra_line_spacing);
19684
19685 /* End of this display line if row is continued. */
19686 if (row->continued_p || row->ends_at_zv_p)
19687 break;
19688 }
19689
19690 at_end_of_line:
19691 /* Is this a line end? If yes, we're also done, after making
19692 sure that a non-default face is extended up to the right
19693 margin of the window. */
19694 if (ITERATOR_AT_END_OF_LINE_P (it))
19695 {
19696 int used_before = row->used[TEXT_AREA];
19697
19698 row->ends_in_newline_from_string_p = STRINGP (it->object);
19699
19700 /* Add a space at the end of the line that is used to
19701 display the cursor there. */
19702 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19703 append_space_for_newline (it, 0);
19704
19705 /* Extend the face to the end of the line. */
19706 extend_face_to_end_of_line (it);
19707
19708 /* Make sure we have the position. */
19709 if (used_before == 0)
19710 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19711
19712 /* Record the position of the newline, for use in
19713 find_row_edges. */
19714 it->eol_pos = it->current.pos;
19715
19716 /* Consume the line end. This skips over invisible lines. */
19717 set_iterator_to_next (it, 1);
19718 it->continuation_lines_width = 0;
19719 break;
19720 }
19721
19722 /* Proceed with next display element. Note that this skips
19723 over lines invisible because of selective display. */
19724 set_iterator_to_next (it, 1);
19725
19726 /* If we truncate lines, we are done when the last displayed
19727 glyphs reach past the right margin of the window. */
19728 if (it->line_wrap == TRUNCATE
19729 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19730 ? (it->current_x >= it->last_visible_x)
19731 : (it->current_x > it->last_visible_x)))
19732 {
19733 /* Maybe add truncation glyphs. */
19734 if (!FRAME_WINDOW_P (it->f)
19735 || (row->reversed_p
19736 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19737 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19738 {
19739 int i, n;
19740
19741 if (!row->reversed_p)
19742 {
19743 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19744 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19745 break;
19746 }
19747 else
19748 {
19749 for (i = 0; i < row->used[TEXT_AREA]; i++)
19750 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19751 break;
19752 /* Remove any padding glyphs at the front of ROW, to
19753 make room for the truncation glyphs we will be
19754 adding below. The loop below always inserts at
19755 least one truncation glyph, so also remove the
19756 last glyph added to ROW. */
19757 unproduce_glyphs (it, i + 1);
19758 /* Adjust i for the loop below. */
19759 i = row->used[TEXT_AREA] - (i + 1);
19760 }
19761
19762 it->current_x = x_before;
19763 if (!FRAME_WINDOW_P (it->f))
19764 {
19765 for (n = row->used[TEXT_AREA]; i < n; ++i)
19766 {
19767 row->used[TEXT_AREA] = i;
19768 produce_special_glyphs (it, IT_TRUNCATION);
19769 }
19770 }
19771 #ifdef HAVE_WINDOW_SYSTEM
19772 else
19773 {
19774 /* On a GUI frame, when the right fringe (left
19775 fringe for R2L rows) is turned off, we produce
19776 truncation glyphs preceded by a stretch glyph
19777 whose width is computed such that the truncation
19778 glyphs are aligned at the window margin, even
19779 when very different fonts are used in different
19780 glyph rows. */
19781 int stretch_width = it->last_visible_x - it->current_x;
19782
19783 row->used[TEXT_AREA] = i;
19784 if (stretch_width > 0)
19785 {
19786 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19787 struct font *font =
19788 face->font ? face->font : FRAME_FONT (it->f);
19789 int stretch_ascent =
19790 (((it->ascent + it->descent)
19791 * FONT_BASE (font)) / FONT_HEIGHT (font));
19792 struct text_pos saved_pos = it->position;
19793
19794 memset (&it->position, 0, sizeof it->position);
19795 append_stretch_glyph (it, make_number (0), stretch_width,
19796 it->ascent + it->descent,
19797 stretch_ascent);
19798 it->position = saved_pos;
19799 }
19800 produce_special_glyphs (it, IT_TRUNCATION);
19801 }
19802 #endif
19803 }
19804 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19805 {
19806 /* Don't truncate if we can overflow newline into fringe. */
19807 if (!get_next_display_element (it))
19808 {
19809 it->continuation_lines_width = 0;
19810 row->ends_at_zv_p = 1;
19811 row->exact_window_width_line_p = 1;
19812 break;
19813 }
19814 if (ITERATOR_AT_END_OF_LINE_P (it))
19815 {
19816 row->exact_window_width_line_p = 1;
19817 goto at_end_of_line;
19818 }
19819 it->current_x = x_before;
19820 }
19821
19822 row->truncated_on_right_p = 1;
19823 it->continuation_lines_width = 0;
19824 reseat_at_next_visible_line_start (it, 0);
19825 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19826 it->hpos = hpos_before;
19827 break;
19828 }
19829 }
19830
19831 if (wrap_data)
19832 bidi_unshelve_cache (wrap_data, 1);
19833
19834 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19835 at the left window margin. */
19836 if (it->first_visible_x
19837 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19838 {
19839 if (!FRAME_WINDOW_P (it->f)
19840 || (row->reversed_p
19841 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19842 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19843 insert_left_trunc_glyphs (it);
19844 row->truncated_on_left_p = 1;
19845 }
19846
19847 /* Remember the position at which this line ends.
19848
19849 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19850 cannot be before the call to find_row_edges below, since that is
19851 where these positions are determined. */
19852 row->end = it->current;
19853 if (!it->bidi_p)
19854 {
19855 row->minpos = row->start.pos;
19856 row->maxpos = row->end.pos;
19857 }
19858 else
19859 {
19860 /* ROW->minpos and ROW->maxpos must be the smallest and
19861 `1 + the largest' buffer positions in ROW. But if ROW was
19862 bidi-reordered, these two positions can be anywhere in the
19863 row, so we must determine them now. */
19864 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19865 }
19866
19867 /* If the start of this line is the overlay arrow-position, then
19868 mark this glyph row as the one containing the overlay arrow.
19869 This is clearly a mess with variable size fonts. It would be
19870 better to let it be displayed like cursors under X. */
19871 if ((row->displays_text_p || !overlay_arrow_seen)
19872 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19873 !NILP (overlay_arrow_string)))
19874 {
19875 /* Overlay arrow in window redisplay is a fringe bitmap. */
19876 if (STRINGP (overlay_arrow_string))
19877 {
19878 struct glyph_row *arrow_row
19879 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19880 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19881 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19882 struct glyph *p = row->glyphs[TEXT_AREA];
19883 struct glyph *p2, *end;
19884
19885 /* Copy the arrow glyphs. */
19886 while (glyph < arrow_end)
19887 *p++ = *glyph++;
19888
19889 /* Throw away padding glyphs. */
19890 p2 = p;
19891 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19892 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19893 ++p2;
19894 if (p2 > p)
19895 {
19896 while (p2 < end)
19897 *p++ = *p2++;
19898 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19899 }
19900 }
19901 else
19902 {
19903 eassert (INTEGERP (overlay_arrow_string));
19904 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19905 }
19906 overlay_arrow_seen = 1;
19907 }
19908
19909 /* Highlight trailing whitespace. */
19910 if (!NILP (Vshow_trailing_whitespace))
19911 highlight_trailing_whitespace (it->f, it->glyph_row);
19912
19913 /* Compute pixel dimensions of this line. */
19914 compute_line_metrics (it);
19915
19916 /* Implementation note: No changes in the glyphs of ROW or in their
19917 faces can be done past this point, because compute_line_metrics
19918 computes ROW's hash value and stores it within the glyph_row
19919 structure. */
19920
19921 /* Record whether this row ends inside an ellipsis. */
19922 row->ends_in_ellipsis_p
19923 = (it->method == GET_FROM_DISPLAY_VECTOR
19924 && it->ellipsis_p);
19925
19926 /* Save fringe bitmaps in this row. */
19927 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19928 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19929 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19930 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19931
19932 it->left_user_fringe_bitmap = 0;
19933 it->left_user_fringe_face_id = 0;
19934 it->right_user_fringe_bitmap = 0;
19935 it->right_user_fringe_face_id = 0;
19936
19937 /* Maybe set the cursor. */
19938 cvpos = it->w->cursor.vpos;
19939 if ((cvpos < 0
19940 /* In bidi-reordered rows, keep checking for proper cursor
19941 position even if one has been found already, because buffer
19942 positions in such rows change non-linearly with ROW->VPOS,
19943 when a line is continued. One exception: when we are at ZV,
19944 display cursor on the first suitable glyph row, since all
19945 the empty rows after that also have their position set to ZV. */
19946 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19947 lines' rows is implemented for bidi-reordered rows. */
19948 || (it->bidi_p
19949 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19950 && PT >= MATRIX_ROW_START_CHARPOS (row)
19951 && PT <= MATRIX_ROW_END_CHARPOS (row)
19952 && cursor_row_p (row))
19953 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19954
19955 /* Prepare for the next line. This line starts horizontally at (X
19956 HPOS) = (0 0). Vertical positions are incremented. As a
19957 convenience for the caller, IT->glyph_row is set to the next
19958 row to be used. */
19959 it->current_x = it->hpos = 0;
19960 it->current_y += row->height;
19961 SET_TEXT_POS (it->eol_pos, 0, 0);
19962 ++it->vpos;
19963 ++it->glyph_row;
19964 /* The next row should by default use the same value of the
19965 reversed_p flag as this one. set_iterator_to_next decides when
19966 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19967 the flag accordingly. */
19968 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19969 it->glyph_row->reversed_p = row->reversed_p;
19970 it->start = row->end;
19971 return row->displays_text_p;
19972
19973 #undef RECORD_MAX_MIN_POS
19974 }
19975
19976 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19977 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19978 doc: /* Return paragraph direction at point in BUFFER.
19979 Value is either `left-to-right' or `right-to-left'.
19980 If BUFFER is omitted or nil, it defaults to the current buffer.
19981
19982 Paragraph direction determines how the text in the paragraph is displayed.
19983 In left-to-right paragraphs, text begins at the left margin of the window
19984 and the reading direction is generally left to right. In right-to-left
19985 paragraphs, text begins at the right margin and is read from right to left.
19986
19987 See also `bidi-paragraph-direction'. */)
19988 (Lisp_Object buffer)
19989 {
19990 struct buffer *buf = current_buffer;
19991 struct buffer *old = buf;
19992
19993 if (! NILP (buffer))
19994 {
19995 CHECK_BUFFER (buffer);
19996 buf = XBUFFER (buffer);
19997 }
19998
19999 if (NILP (BVAR (buf, bidi_display_reordering))
20000 || NILP (BVAR (buf, enable_multibyte_characters))
20001 /* When we are loading loadup.el, the character property tables
20002 needed for bidi iteration are not yet available. */
20003 || !NILP (Vpurify_flag))
20004 return Qleft_to_right;
20005 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20006 return BVAR (buf, bidi_paragraph_direction);
20007 else
20008 {
20009 /* Determine the direction from buffer text. We could try to
20010 use current_matrix if it is up to date, but this seems fast
20011 enough as it is. */
20012 struct bidi_it itb;
20013 ptrdiff_t pos = BUF_PT (buf);
20014 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20015 int c;
20016 void *itb_data = bidi_shelve_cache ();
20017
20018 set_buffer_temp (buf);
20019 /* bidi_paragraph_init finds the base direction of the paragraph
20020 by searching forward from paragraph start. We need the base
20021 direction of the current or _previous_ paragraph, so we need
20022 to make sure we are within that paragraph. To that end, find
20023 the previous non-empty line. */
20024 if (pos >= ZV && pos > BEGV)
20025 {
20026 pos--;
20027 bytepos = CHAR_TO_BYTE (pos);
20028 }
20029 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20030 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20031 {
20032 while ((c = FETCH_BYTE (bytepos)) == '\n'
20033 || c == ' ' || c == '\t' || c == '\f')
20034 {
20035 if (bytepos <= BEGV_BYTE)
20036 break;
20037 bytepos--;
20038 pos--;
20039 }
20040 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20041 bytepos--;
20042 }
20043 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20044 itb.paragraph_dir = NEUTRAL_DIR;
20045 itb.string.s = NULL;
20046 itb.string.lstring = Qnil;
20047 itb.string.bufpos = 0;
20048 itb.string.unibyte = 0;
20049 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20050 bidi_unshelve_cache (itb_data, 0);
20051 set_buffer_temp (old);
20052 switch (itb.paragraph_dir)
20053 {
20054 case L2R:
20055 return Qleft_to_right;
20056 break;
20057 case R2L:
20058 return Qright_to_left;
20059 break;
20060 default:
20061 abort ();
20062 }
20063 }
20064 }
20065
20066
20067 \f
20068 /***********************************************************************
20069 Menu Bar
20070 ***********************************************************************/
20071
20072 /* Redisplay the menu bar in the frame for window W.
20073
20074 The menu bar of X frames that don't have X toolkit support is
20075 displayed in a special window W->frame->menu_bar_window.
20076
20077 The menu bar of terminal frames is treated specially as far as
20078 glyph matrices are concerned. Menu bar lines are not part of
20079 windows, so the update is done directly on the frame matrix rows
20080 for the menu bar. */
20081
20082 static void
20083 display_menu_bar (struct window *w)
20084 {
20085 struct frame *f = XFRAME (WINDOW_FRAME (w));
20086 struct it it;
20087 Lisp_Object items;
20088 int i;
20089
20090 /* Don't do all this for graphical frames. */
20091 #ifdef HAVE_NTGUI
20092 if (FRAME_W32_P (f))
20093 return;
20094 #endif
20095 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20096 if (FRAME_X_P (f))
20097 return;
20098 #endif
20099
20100 #ifdef HAVE_NS
20101 if (FRAME_NS_P (f))
20102 return;
20103 #endif /* HAVE_NS */
20104
20105 #ifdef USE_X_TOOLKIT
20106 eassert (!FRAME_WINDOW_P (f));
20107 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20108 it.first_visible_x = 0;
20109 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20110 #else /* not USE_X_TOOLKIT */
20111 if (FRAME_WINDOW_P (f))
20112 {
20113 /* Menu bar lines are displayed in the desired matrix of the
20114 dummy window menu_bar_window. */
20115 struct window *menu_w;
20116 eassert (WINDOWP (f->menu_bar_window));
20117 menu_w = XWINDOW (f->menu_bar_window);
20118 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20119 MENU_FACE_ID);
20120 it.first_visible_x = 0;
20121 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20122 }
20123 else
20124 {
20125 /* This is a TTY frame, i.e. character hpos/vpos are used as
20126 pixel x/y. */
20127 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20128 MENU_FACE_ID);
20129 it.first_visible_x = 0;
20130 it.last_visible_x = FRAME_COLS (f);
20131 }
20132 #endif /* not USE_X_TOOLKIT */
20133
20134 /* FIXME: This should be controlled by a user option. See the
20135 comments in redisplay_tool_bar and display_mode_line about
20136 this. */
20137 it.paragraph_embedding = L2R;
20138
20139 if (! mode_line_inverse_video)
20140 /* Force the menu-bar to be displayed in the default face. */
20141 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20142
20143 /* Clear all rows of the menu bar. */
20144 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20145 {
20146 struct glyph_row *row = it.glyph_row + i;
20147 clear_glyph_row (row);
20148 row->enabled_p = 1;
20149 row->full_width_p = 1;
20150 }
20151
20152 /* Display all items of the menu bar. */
20153 items = FRAME_MENU_BAR_ITEMS (it.f);
20154 for (i = 0; i < ASIZE (items); i += 4)
20155 {
20156 Lisp_Object string;
20157
20158 /* Stop at nil string. */
20159 string = AREF (items, i + 1);
20160 if (NILP (string))
20161 break;
20162
20163 /* Remember where item was displayed. */
20164 ASET (items, i + 3, make_number (it.hpos));
20165
20166 /* Display the item, pad with one space. */
20167 if (it.current_x < it.last_visible_x)
20168 display_string (NULL, string, Qnil, 0, 0, &it,
20169 SCHARS (string) + 1, 0, 0, -1);
20170 }
20171
20172 /* Fill out the line with spaces. */
20173 if (it.current_x < it.last_visible_x)
20174 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20175
20176 /* Compute the total height of the lines. */
20177 compute_line_metrics (&it);
20178 }
20179
20180
20181 \f
20182 /***********************************************************************
20183 Mode Line
20184 ***********************************************************************/
20185
20186 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20187 FORCE is non-zero, redisplay mode lines unconditionally.
20188 Otherwise, redisplay only mode lines that are garbaged. Value is
20189 the number of windows whose mode lines were redisplayed. */
20190
20191 static int
20192 redisplay_mode_lines (Lisp_Object window, int force)
20193 {
20194 int nwindows = 0;
20195
20196 while (!NILP (window))
20197 {
20198 struct window *w = XWINDOW (window);
20199
20200 if (WINDOWP (w->hchild))
20201 nwindows += redisplay_mode_lines (w->hchild, force);
20202 else if (WINDOWP (w->vchild))
20203 nwindows += redisplay_mode_lines (w->vchild, force);
20204 else if (force
20205 || FRAME_GARBAGED_P (XFRAME (w->frame))
20206 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20207 {
20208 struct text_pos lpoint;
20209 struct buffer *old = current_buffer;
20210
20211 /* Set the window's buffer for the mode line display. */
20212 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20213 set_buffer_internal_1 (XBUFFER (w->buffer));
20214
20215 /* Point refers normally to the selected window. For any
20216 other window, set up appropriate value. */
20217 if (!EQ (window, selected_window))
20218 {
20219 struct text_pos pt;
20220
20221 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20222 if (CHARPOS (pt) < BEGV)
20223 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20224 else if (CHARPOS (pt) > (ZV - 1))
20225 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20226 else
20227 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20228 }
20229
20230 /* Display mode lines. */
20231 clear_glyph_matrix (w->desired_matrix);
20232 if (display_mode_lines (w))
20233 {
20234 ++nwindows;
20235 w->must_be_updated_p = 1;
20236 }
20237
20238 /* Restore old settings. */
20239 set_buffer_internal_1 (old);
20240 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20241 }
20242
20243 window = w->next;
20244 }
20245
20246 return nwindows;
20247 }
20248
20249
20250 /* Display the mode and/or header line of window W. Value is the
20251 sum number of mode lines and header lines displayed. */
20252
20253 static int
20254 display_mode_lines (struct window *w)
20255 {
20256 Lisp_Object old_selected_window, old_selected_frame;
20257 int n = 0;
20258
20259 old_selected_frame = selected_frame;
20260 selected_frame = w->frame;
20261 old_selected_window = selected_window;
20262 XSETWINDOW (selected_window, w);
20263
20264 /* These will be set while the mode line specs are processed. */
20265 line_number_displayed = 0;
20266 w->column_number_displayed = Qnil;
20267
20268 if (WINDOW_WANTS_MODELINE_P (w))
20269 {
20270 struct window *sel_w = XWINDOW (old_selected_window);
20271
20272 /* Select mode line face based on the real selected window. */
20273 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20274 BVAR (current_buffer, mode_line_format));
20275 ++n;
20276 }
20277
20278 if (WINDOW_WANTS_HEADER_LINE_P (w))
20279 {
20280 display_mode_line (w, HEADER_LINE_FACE_ID,
20281 BVAR (current_buffer, header_line_format));
20282 ++n;
20283 }
20284
20285 selected_frame = old_selected_frame;
20286 selected_window = old_selected_window;
20287 return n;
20288 }
20289
20290
20291 /* Display mode or header line of window W. FACE_ID specifies which
20292 line to display; it is either MODE_LINE_FACE_ID or
20293 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20294 display. Value is the pixel height of the mode/header line
20295 displayed. */
20296
20297 static int
20298 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20299 {
20300 struct it it;
20301 struct face *face;
20302 ptrdiff_t count = SPECPDL_INDEX ();
20303
20304 init_iterator (&it, w, -1, -1, NULL, face_id);
20305 /* Don't extend on a previously drawn mode-line.
20306 This may happen if called from pos_visible_p. */
20307 it.glyph_row->enabled_p = 0;
20308 prepare_desired_row (it.glyph_row);
20309
20310 it.glyph_row->mode_line_p = 1;
20311
20312 if (! mode_line_inverse_video)
20313 /* Force the mode-line to be displayed in the default face. */
20314 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20315
20316 /* FIXME: This should be controlled by a user option. But
20317 supporting such an option is not trivial, since the mode line is
20318 made up of many separate strings. */
20319 it.paragraph_embedding = L2R;
20320
20321 record_unwind_protect (unwind_format_mode_line,
20322 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20323
20324 mode_line_target = MODE_LINE_DISPLAY;
20325
20326 /* Temporarily make frame's keyboard the current kboard so that
20327 kboard-local variables in the mode_line_format will get the right
20328 values. */
20329 push_kboard (FRAME_KBOARD (it.f));
20330 record_unwind_save_match_data ();
20331 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20332 pop_kboard ();
20333
20334 unbind_to (count, Qnil);
20335
20336 /* Fill up with spaces. */
20337 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20338
20339 compute_line_metrics (&it);
20340 it.glyph_row->full_width_p = 1;
20341 it.glyph_row->continued_p = 0;
20342 it.glyph_row->truncated_on_left_p = 0;
20343 it.glyph_row->truncated_on_right_p = 0;
20344
20345 /* Make a 3D mode-line have a shadow at its right end. */
20346 face = FACE_FROM_ID (it.f, face_id);
20347 extend_face_to_end_of_line (&it);
20348 if (face->box != FACE_NO_BOX)
20349 {
20350 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20351 + it.glyph_row->used[TEXT_AREA] - 1);
20352 last->right_box_line_p = 1;
20353 }
20354
20355 return it.glyph_row->height;
20356 }
20357
20358 /* Move element ELT in LIST to the front of LIST.
20359 Return the updated list. */
20360
20361 static Lisp_Object
20362 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20363 {
20364 register Lisp_Object tail, prev;
20365 register Lisp_Object tem;
20366
20367 tail = list;
20368 prev = Qnil;
20369 while (CONSP (tail))
20370 {
20371 tem = XCAR (tail);
20372
20373 if (EQ (elt, tem))
20374 {
20375 /* Splice out the link TAIL. */
20376 if (NILP (prev))
20377 list = XCDR (tail);
20378 else
20379 Fsetcdr (prev, XCDR (tail));
20380
20381 /* Now make it the first. */
20382 Fsetcdr (tail, list);
20383 return tail;
20384 }
20385 else
20386 prev = tail;
20387 tail = XCDR (tail);
20388 QUIT;
20389 }
20390
20391 /* Not found--return unchanged LIST. */
20392 return list;
20393 }
20394
20395 /* Contribute ELT to the mode line for window IT->w. How it
20396 translates into text depends on its data type.
20397
20398 IT describes the display environment in which we display, as usual.
20399
20400 DEPTH is the depth in recursion. It is used to prevent
20401 infinite recursion here.
20402
20403 FIELD_WIDTH is the number of characters the display of ELT should
20404 occupy in the mode line, and PRECISION is the maximum number of
20405 characters to display from ELT's representation. See
20406 display_string for details.
20407
20408 Returns the hpos of the end of the text generated by ELT.
20409
20410 PROPS is a property list to add to any string we encounter.
20411
20412 If RISKY is nonzero, remove (disregard) any properties in any string
20413 we encounter, and ignore :eval and :propertize.
20414
20415 The global variable `mode_line_target' determines whether the
20416 output is passed to `store_mode_line_noprop',
20417 `store_mode_line_string', or `display_string'. */
20418
20419 static int
20420 display_mode_element (struct it *it, int depth, int field_width, int precision,
20421 Lisp_Object elt, Lisp_Object props, int risky)
20422 {
20423 int n = 0, field, prec;
20424 int literal = 0;
20425
20426 tail_recurse:
20427 if (depth > 100)
20428 elt = build_string ("*too-deep*");
20429
20430 depth++;
20431
20432 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20433 {
20434 case Lisp_String:
20435 {
20436 /* A string: output it and check for %-constructs within it. */
20437 unsigned char c;
20438 ptrdiff_t offset = 0;
20439
20440 if (SCHARS (elt) > 0
20441 && (!NILP (props) || risky))
20442 {
20443 Lisp_Object oprops, aelt;
20444 oprops = Ftext_properties_at (make_number (0), elt);
20445
20446 /* If the starting string's properties are not what
20447 we want, translate the string. Also, if the string
20448 is risky, do that anyway. */
20449
20450 if (NILP (Fequal (props, oprops)) || risky)
20451 {
20452 /* If the starting string has properties,
20453 merge the specified ones onto the existing ones. */
20454 if (! NILP (oprops) && !risky)
20455 {
20456 Lisp_Object tem;
20457
20458 oprops = Fcopy_sequence (oprops);
20459 tem = props;
20460 while (CONSP (tem))
20461 {
20462 oprops = Fplist_put (oprops, XCAR (tem),
20463 XCAR (XCDR (tem)));
20464 tem = XCDR (XCDR (tem));
20465 }
20466 props = oprops;
20467 }
20468
20469 aelt = Fassoc (elt, mode_line_proptrans_alist);
20470 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20471 {
20472 /* AELT is what we want. Move it to the front
20473 without consing. */
20474 elt = XCAR (aelt);
20475 mode_line_proptrans_alist
20476 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20477 }
20478 else
20479 {
20480 Lisp_Object tem;
20481
20482 /* If AELT has the wrong props, it is useless.
20483 so get rid of it. */
20484 if (! NILP (aelt))
20485 mode_line_proptrans_alist
20486 = Fdelq (aelt, mode_line_proptrans_alist);
20487
20488 elt = Fcopy_sequence (elt);
20489 Fset_text_properties (make_number (0), Flength (elt),
20490 props, elt);
20491 /* Add this item to mode_line_proptrans_alist. */
20492 mode_line_proptrans_alist
20493 = Fcons (Fcons (elt, props),
20494 mode_line_proptrans_alist);
20495 /* Truncate mode_line_proptrans_alist
20496 to at most 50 elements. */
20497 tem = Fnthcdr (make_number (50),
20498 mode_line_proptrans_alist);
20499 if (! NILP (tem))
20500 XSETCDR (tem, Qnil);
20501 }
20502 }
20503 }
20504
20505 offset = 0;
20506
20507 if (literal)
20508 {
20509 prec = precision - n;
20510 switch (mode_line_target)
20511 {
20512 case MODE_LINE_NOPROP:
20513 case MODE_LINE_TITLE:
20514 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20515 break;
20516 case MODE_LINE_STRING:
20517 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20518 break;
20519 case MODE_LINE_DISPLAY:
20520 n += display_string (NULL, elt, Qnil, 0, 0, it,
20521 0, prec, 0, STRING_MULTIBYTE (elt));
20522 break;
20523 }
20524
20525 break;
20526 }
20527
20528 /* Handle the non-literal case. */
20529
20530 while ((precision <= 0 || n < precision)
20531 && SREF (elt, offset) != 0
20532 && (mode_line_target != MODE_LINE_DISPLAY
20533 || it->current_x < it->last_visible_x))
20534 {
20535 ptrdiff_t last_offset = offset;
20536
20537 /* Advance to end of string or next format specifier. */
20538 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20539 ;
20540
20541 if (offset - 1 != last_offset)
20542 {
20543 ptrdiff_t nchars, nbytes;
20544
20545 /* Output to end of string or up to '%'. Field width
20546 is length of string. Don't output more than
20547 PRECISION allows us. */
20548 offset--;
20549
20550 prec = c_string_width (SDATA (elt) + last_offset,
20551 offset - last_offset, precision - n,
20552 &nchars, &nbytes);
20553
20554 switch (mode_line_target)
20555 {
20556 case MODE_LINE_NOPROP:
20557 case MODE_LINE_TITLE:
20558 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20559 break;
20560 case MODE_LINE_STRING:
20561 {
20562 ptrdiff_t bytepos = last_offset;
20563 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20564 ptrdiff_t endpos = (precision <= 0
20565 ? string_byte_to_char (elt, offset)
20566 : charpos + nchars);
20567
20568 n += store_mode_line_string (NULL,
20569 Fsubstring (elt, make_number (charpos),
20570 make_number (endpos)),
20571 0, 0, 0, Qnil);
20572 }
20573 break;
20574 case MODE_LINE_DISPLAY:
20575 {
20576 ptrdiff_t bytepos = last_offset;
20577 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20578
20579 if (precision <= 0)
20580 nchars = string_byte_to_char (elt, offset) - charpos;
20581 n += display_string (NULL, elt, Qnil, 0, charpos,
20582 it, 0, nchars, 0,
20583 STRING_MULTIBYTE (elt));
20584 }
20585 break;
20586 }
20587 }
20588 else /* c == '%' */
20589 {
20590 ptrdiff_t percent_position = offset;
20591
20592 /* Get the specified minimum width. Zero means
20593 don't pad. */
20594 field = 0;
20595 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20596 field = field * 10 + c - '0';
20597
20598 /* Don't pad beyond the total padding allowed. */
20599 if (field_width - n > 0 && field > field_width - n)
20600 field = field_width - n;
20601
20602 /* Note that either PRECISION <= 0 or N < PRECISION. */
20603 prec = precision - n;
20604
20605 if (c == 'M')
20606 n += display_mode_element (it, depth, field, prec,
20607 Vglobal_mode_string, props,
20608 risky);
20609 else if (c != 0)
20610 {
20611 int multibyte;
20612 ptrdiff_t bytepos, charpos;
20613 const char *spec;
20614 Lisp_Object string;
20615
20616 bytepos = percent_position;
20617 charpos = (STRING_MULTIBYTE (elt)
20618 ? string_byte_to_char (elt, bytepos)
20619 : bytepos);
20620 spec = decode_mode_spec (it->w, c, field, &string);
20621 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20622
20623 switch (mode_line_target)
20624 {
20625 case MODE_LINE_NOPROP:
20626 case MODE_LINE_TITLE:
20627 n += store_mode_line_noprop (spec, field, prec);
20628 break;
20629 case MODE_LINE_STRING:
20630 {
20631 Lisp_Object tem = build_string (spec);
20632 props = Ftext_properties_at (make_number (charpos), elt);
20633 /* Should only keep face property in props */
20634 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20635 }
20636 break;
20637 case MODE_LINE_DISPLAY:
20638 {
20639 int nglyphs_before, nwritten;
20640
20641 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20642 nwritten = display_string (spec, string, elt,
20643 charpos, 0, it,
20644 field, prec, 0,
20645 multibyte);
20646
20647 /* Assign to the glyphs written above the
20648 string where the `%x' came from, position
20649 of the `%'. */
20650 if (nwritten > 0)
20651 {
20652 struct glyph *glyph
20653 = (it->glyph_row->glyphs[TEXT_AREA]
20654 + nglyphs_before);
20655 int i;
20656
20657 for (i = 0; i < nwritten; ++i)
20658 {
20659 glyph[i].object = elt;
20660 glyph[i].charpos = charpos;
20661 }
20662
20663 n += nwritten;
20664 }
20665 }
20666 break;
20667 }
20668 }
20669 else /* c == 0 */
20670 break;
20671 }
20672 }
20673 }
20674 break;
20675
20676 case Lisp_Symbol:
20677 /* A symbol: process the value of the symbol recursively
20678 as if it appeared here directly. Avoid error if symbol void.
20679 Special case: if value of symbol is a string, output the string
20680 literally. */
20681 {
20682 register Lisp_Object tem;
20683
20684 /* If the variable is not marked as risky to set
20685 then its contents are risky to use. */
20686 if (NILP (Fget (elt, Qrisky_local_variable)))
20687 risky = 1;
20688
20689 tem = Fboundp (elt);
20690 if (!NILP (tem))
20691 {
20692 tem = Fsymbol_value (elt);
20693 /* If value is a string, output that string literally:
20694 don't check for % within it. */
20695 if (STRINGP (tem))
20696 literal = 1;
20697
20698 if (!EQ (tem, elt))
20699 {
20700 /* Give up right away for nil or t. */
20701 elt = tem;
20702 goto tail_recurse;
20703 }
20704 }
20705 }
20706 break;
20707
20708 case Lisp_Cons:
20709 {
20710 register Lisp_Object car, tem;
20711
20712 /* A cons cell: five distinct cases.
20713 If first element is :eval or :propertize, do something special.
20714 If first element is a string or a cons, process all the elements
20715 and effectively concatenate them.
20716 If first element is a negative number, truncate displaying cdr to
20717 at most that many characters. If positive, pad (with spaces)
20718 to at least that many characters.
20719 If first element is a symbol, process the cadr or caddr recursively
20720 according to whether the symbol's value is non-nil or nil. */
20721 car = XCAR (elt);
20722 if (EQ (car, QCeval))
20723 {
20724 /* An element of the form (:eval FORM) means evaluate FORM
20725 and use the result as mode line elements. */
20726
20727 if (risky)
20728 break;
20729
20730 if (CONSP (XCDR (elt)))
20731 {
20732 Lisp_Object spec;
20733 spec = safe_eval (XCAR (XCDR (elt)));
20734 n += display_mode_element (it, depth, field_width - n,
20735 precision - n, spec, props,
20736 risky);
20737 }
20738 }
20739 else if (EQ (car, QCpropertize))
20740 {
20741 /* An element of the form (:propertize ELT PROPS...)
20742 means display ELT but applying properties PROPS. */
20743
20744 if (risky)
20745 break;
20746
20747 if (CONSP (XCDR (elt)))
20748 n += display_mode_element (it, depth, field_width - n,
20749 precision - n, XCAR (XCDR (elt)),
20750 XCDR (XCDR (elt)), risky);
20751 }
20752 else if (SYMBOLP (car))
20753 {
20754 tem = Fboundp (car);
20755 elt = XCDR (elt);
20756 if (!CONSP (elt))
20757 goto invalid;
20758 /* elt is now the cdr, and we know it is a cons cell.
20759 Use its car if CAR has a non-nil value. */
20760 if (!NILP (tem))
20761 {
20762 tem = Fsymbol_value (car);
20763 if (!NILP (tem))
20764 {
20765 elt = XCAR (elt);
20766 goto tail_recurse;
20767 }
20768 }
20769 /* Symbol's value is nil (or symbol is unbound)
20770 Get the cddr of the original list
20771 and if possible find the caddr and use that. */
20772 elt = XCDR (elt);
20773 if (NILP (elt))
20774 break;
20775 else if (!CONSP (elt))
20776 goto invalid;
20777 elt = XCAR (elt);
20778 goto tail_recurse;
20779 }
20780 else if (INTEGERP (car))
20781 {
20782 register int lim = XINT (car);
20783 elt = XCDR (elt);
20784 if (lim < 0)
20785 {
20786 /* Negative int means reduce maximum width. */
20787 if (precision <= 0)
20788 precision = -lim;
20789 else
20790 precision = min (precision, -lim);
20791 }
20792 else if (lim > 0)
20793 {
20794 /* Padding specified. Don't let it be more than
20795 current maximum. */
20796 if (precision > 0)
20797 lim = min (precision, lim);
20798
20799 /* If that's more padding than already wanted, queue it.
20800 But don't reduce padding already specified even if
20801 that is beyond the current truncation point. */
20802 field_width = max (lim, field_width);
20803 }
20804 goto tail_recurse;
20805 }
20806 else if (STRINGP (car) || CONSP (car))
20807 {
20808 Lisp_Object halftail = elt;
20809 int len = 0;
20810
20811 while (CONSP (elt)
20812 && (precision <= 0 || n < precision))
20813 {
20814 n += display_mode_element (it, depth,
20815 /* Do padding only after the last
20816 element in the list. */
20817 (! CONSP (XCDR (elt))
20818 ? field_width - n
20819 : 0),
20820 precision - n, XCAR (elt),
20821 props, risky);
20822 elt = XCDR (elt);
20823 len++;
20824 if ((len & 1) == 0)
20825 halftail = XCDR (halftail);
20826 /* Check for cycle. */
20827 if (EQ (halftail, elt))
20828 break;
20829 }
20830 }
20831 }
20832 break;
20833
20834 default:
20835 invalid:
20836 elt = build_string ("*invalid*");
20837 goto tail_recurse;
20838 }
20839
20840 /* Pad to FIELD_WIDTH. */
20841 if (field_width > 0 && n < field_width)
20842 {
20843 switch (mode_line_target)
20844 {
20845 case MODE_LINE_NOPROP:
20846 case MODE_LINE_TITLE:
20847 n += store_mode_line_noprop ("", field_width - n, 0);
20848 break;
20849 case MODE_LINE_STRING:
20850 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20851 break;
20852 case MODE_LINE_DISPLAY:
20853 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20854 0, 0, 0);
20855 break;
20856 }
20857 }
20858
20859 return n;
20860 }
20861
20862 /* Store a mode-line string element in mode_line_string_list.
20863
20864 If STRING is non-null, display that C string. Otherwise, the Lisp
20865 string LISP_STRING is displayed.
20866
20867 FIELD_WIDTH is the minimum number of output glyphs to produce.
20868 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20869 with spaces. FIELD_WIDTH <= 0 means don't pad.
20870
20871 PRECISION is the maximum number of characters to output from
20872 STRING. PRECISION <= 0 means don't truncate the string.
20873
20874 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20875 properties to the string.
20876
20877 PROPS are the properties to add to the string.
20878 The mode_line_string_face face property is always added to the string.
20879 */
20880
20881 static int
20882 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20883 int field_width, int precision, Lisp_Object props)
20884 {
20885 ptrdiff_t len;
20886 int n = 0;
20887
20888 if (string != NULL)
20889 {
20890 len = strlen (string);
20891 if (precision > 0 && len > precision)
20892 len = precision;
20893 lisp_string = make_string (string, len);
20894 if (NILP (props))
20895 props = mode_line_string_face_prop;
20896 else if (!NILP (mode_line_string_face))
20897 {
20898 Lisp_Object face = Fplist_get (props, Qface);
20899 props = Fcopy_sequence (props);
20900 if (NILP (face))
20901 face = mode_line_string_face;
20902 else
20903 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20904 props = Fplist_put (props, Qface, face);
20905 }
20906 Fadd_text_properties (make_number (0), make_number (len),
20907 props, lisp_string);
20908 }
20909 else
20910 {
20911 len = XFASTINT (Flength (lisp_string));
20912 if (precision > 0 && len > precision)
20913 {
20914 len = precision;
20915 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20916 precision = -1;
20917 }
20918 if (!NILP (mode_line_string_face))
20919 {
20920 Lisp_Object face;
20921 if (NILP (props))
20922 props = Ftext_properties_at (make_number (0), lisp_string);
20923 face = Fplist_get (props, Qface);
20924 if (NILP (face))
20925 face = mode_line_string_face;
20926 else
20927 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20928 props = Fcons (Qface, Fcons (face, Qnil));
20929 if (copy_string)
20930 lisp_string = Fcopy_sequence (lisp_string);
20931 }
20932 if (!NILP (props))
20933 Fadd_text_properties (make_number (0), make_number (len),
20934 props, lisp_string);
20935 }
20936
20937 if (len > 0)
20938 {
20939 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20940 n += len;
20941 }
20942
20943 if (field_width > len)
20944 {
20945 field_width -= len;
20946 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20947 if (!NILP (props))
20948 Fadd_text_properties (make_number (0), make_number (field_width),
20949 props, lisp_string);
20950 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20951 n += field_width;
20952 }
20953
20954 return n;
20955 }
20956
20957
20958 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20959 1, 4, 0,
20960 doc: /* Format a string out of a mode line format specification.
20961 First arg FORMAT specifies the mode line format (see `mode-line-format'
20962 for details) to use.
20963
20964 By default, the format is evaluated for the currently selected window.
20965
20966 Optional second arg FACE specifies the face property to put on all
20967 characters for which no face is specified. The value nil means the
20968 default face. The value t means whatever face the window's mode line
20969 currently uses (either `mode-line' or `mode-line-inactive',
20970 depending on whether the window is the selected window or not).
20971 An integer value means the value string has no text
20972 properties.
20973
20974 Optional third and fourth args WINDOW and BUFFER specify the window
20975 and buffer to use as the context for the formatting (defaults
20976 are the selected window and the WINDOW's buffer). */)
20977 (Lisp_Object format, Lisp_Object face,
20978 Lisp_Object window, Lisp_Object buffer)
20979 {
20980 struct it it;
20981 int len;
20982 struct window *w;
20983 struct buffer *old_buffer = NULL;
20984 int face_id;
20985 int no_props = INTEGERP (face);
20986 ptrdiff_t count = SPECPDL_INDEX ();
20987 Lisp_Object str;
20988 int string_start = 0;
20989
20990 if (NILP (window))
20991 window = selected_window;
20992 CHECK_WINDOW (window);
20993 w = XWINDOW (window);
20994
20995 if (NILP (buffer))
20996 buffer = w->buffer;
20997 CHECK_BUFFER (buffer);
20998
20999 /* Make formatting the modeline a non-op when noninteractive, otherwise
21000 there will be problems later caused by a partially initialized frame. */
21001 if (NILP (format) || noninteractive)
21002 return empty_unibyte_string;
21003
21004 if (no_props)
21005 face = Qnil;
21006
21007 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21008 : EQ (face, Qt) ? (EQ (window, selected_window)
21009 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21010 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21011 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21012 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21013 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21014 : DEFAULT_FACE_ID;
21015
21016 if (XBUFFER (buffer) != current_buffer)
21017 old_buffer = current_buffer;
21018
21019 /* Save things including mode_line_proptrans_alist,
21020 and set that to nil so that we don't alter the outer value. */
21021 record_unwind_protect (unwind_format_mode_line,
21022 format_mode_line_unwind_data
21023 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21024 old_buffer, selected_window, 1));
21025 mode_line_proptrans_alist = Qnil;
21026
21027 Fselect_window (window, Qt);
21028 if (old_buffer)
21029 set_buffer_internal_1 (XBUFFER (buffer));
21030
21031 init_iterator (&it, w, -1, -1, NULL, face_id);
21032
21033 if (no_props)
21034 {
21035 mode_line_target = MODE_LINE_NOPROP;
21036 mode_line_string_face_prop = Qnil;
21037 mode_line_string_list = Qnil;
21038 string_start = MODE_LINE_NOPROP_LEN (0);
21039 }
21040 else
21041 {
21042 mode_line_target = MODE_LINE_STRING;
21043 mode_line_string_list = Qnil;
21044 mode_line_string_face = face;
21045 mode_line_string_face_prop
21046 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21047 }
21048
21049 push_kboard (FRAME_KBOARD (it.f));
21050 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21051 pop_kboard ();
21052
21053 if (no_props)
21054 {
21055 len = MODE_LINE_NOPROP_LEN (string_start);
21056 str = make_string (mode_line_noprop_buf + string_start, len);
21057 }
21058 else
21059 {
21060 mode_line_string_list = Fnreverse (mode_line_string_list);
21061 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21062 empty_unibyte_string);
21063 }
21064
21065 unbind_to (count, Qnil);
21066 return str;
21067 }
21068
21069 /* Write a null-terminated, right justified decimal representation of
21070 the positive integer D to BUF using a minimal field width WIDTH. */
21071
21072 static void
21073 pint2str (register char *buf, register int width, register ptrdiff_t d)
21074 {
21075 register char *p = buf;
21076
21077 if (d <= 0)
21078 *p++ = '0';
21079 else
21080 {
21081 while (d > 0)
21082 {
21083 *p++ = d % 10 + '0';
21084 d /= 10;
21085 }
21086 }
21087
21088 for (width -= (int) (p - buf); width > 0; --width)
21089 *p++ = ' ';
21090 *p-- = '\0';
21091 while (p > buf)
21092 {
21093 d = *buf;
21094 *buf++ = *p;
21095 *p-- = d;
21096 }
21097 }
21098
21099 /* Write a null-terminated, right justified decimal and "human
21100 readable" representation of the nonnegative integer D to BUF using
21101 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21102
21103 static const char power_letter[] =
21104 {
21105 0, /* no letter */
21106 'k', /* kilo */
21107 'M', /* mega */
21108 'G', /* giga */
21109 'T', /* tera */
21110 'P', /* peta */
21111 'E', /* exa */
21112 'Z', /* zetta */
21113 'Y' /* yotta */
21114 };
21115
21116 static void
21117 pint2hrstr (char *buf, int width, ptrdiff_t d)
21118 {
21119 /* We aim to represent the nonnegative integer D as
21120 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21121 ptrdiff_t quotient = d;
21122 int remainder = 0;
21123 /* -1 means: do not use TENTHS. */
21124 int tenths = -1;
21125 int exponent = 0;
21126
21127 /* Length of QUOTIENT.TENTHS as a string. */
21128 int length;
21129
21130 char * psuffix;
21131 char * p;
21132
21133 if (1000 <= quotient)
21134 {
21135 /* Scale to the appropriate EXPONENT. */
21136 do
21137 {
21138 remainder = quotient % 1000;
21139 quotient /= 1000;
21140 exponent++;
21141 }
21142 while (1000 <= quotient);
21143
21144 /* Round to nearest and decide whether to use TENTHS or not. */
21145 if (quotient <= 9)
21146 {
21147 tenths = remainder / 100;
21148 if (50 <= remainder % 100)
21149 {
21150 if (tenths < 9)
21151 tenths++;
21152 else
21153 {
21154 quotient++;
21155 if (quotient == 10)
21156 tenths = -1;
21157 else
21158 tenths = 0;
21159 }
21160 }
21161 }
21162 else
21163 if (500 <= remainder)
21164 {
21165 if (quotient < 999)
21166 quotient++;
21167 else
21168 {
21169 quotient = 1;
21170 exponent++;
21171 tenths = 0;
21172 }
21173 }
21174 }
21175
21176 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21177 if (tenths == -1 && quotient <= 99)
21178 if (quotient <= 9)
21179 length = 1;
21180 else
21181 length = 2;
21182 else
21183 length = 3;
21184 p = psuffix = buf + max (width, length);
21185
21186 /* Print EXPONENT. */
21187 *psuffix++ = power_letter[exponent];
21188 *psuffix = '\0';
21189
21190 /* Print TENTHS. */
21191 if (tenths >= 0)
21192 {
21193 *--p = '0' + tenths;
21194 *--p = '.';
21195 }
21196
21197 /* Print QUOTIENT. */
21198 do
21199 {
21200 int digit = quotient % 10;
21201 *--p = '0' + digit;
21202 }
21203 while ((quotient /= 10) != 0);
21204
21205 /* Print leading spaces. */
21206 while (buf < p)
21207 *--p = ' ';
21208 }
21209
21210 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21211 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21212 type of CODING_SYSTEM. Return updated pointer into BUF. */
21213
21214 static unsigned char invalid_eol_type[] = "(*invalid*)";
21215
21216 static char *
21217 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21218 {
21219 Lisp_Object val;
21220 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21221 const unsigned char *eol_str;
21222 int eol_str_len;
21223 /* The EOL conversion we are using. */
21224 Lisp_Object eoltype;
21225
21226 val = CODING_SYSTEM_SPEC (coding_system);
21227 eoltype = Qnil;
21228
21229 if (!VECTORP (val)) /* Not yet decided. */
21230 {
21231 *buf++ = multibyte ? '-' : ' ';
21232 if (eol_flag)
21233 eoltype = eol_mnemonic_undecided;
21234 /* Don't mention EOL conversion if it isn't decided. */
21235 }
21236 else
21237 {
21238 Lisp_Object attrs;
21239 Lisp_Object eolvalue;
21240
21241 attrs = AREF (val, 0);
21242 eolvalue = AREF (val, 2);
21243
21244 *buf++ = multibyte
21245 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21246 : ' ';
21247
21248 if (eol_flag)
21249 {
21250 /* The EOL conversion that is normal on this system. */
21251
21252 if (NILP (eolvalue)) /* Not yet decided. */
21253 eoltype = eol_mnemonic_undecided;
21254 else if (VECTORP (eolvalue)) /* Not yet decided. */
21255 eoltype = eol_mnemonic_undecided;
21256 else /* eolvalue is Qunix, Qdos, or Qmac. */
21257 eoltype = (EQ (eolvalue, Qunix)
21258 ? eol_mnemonic_unix
21259 : (EQ (eolvalue, Qdos) == 1
21260 ? eol_mnemonic_dos : eol_mnemonic_mac));
21261 }
21262 }
21263
21264 if (eol_flag)
21265 {
21266 /* Mention the EOL conversion if it is not the usual one. */
21267 if (STRINGP (eoltype))
21268 {
21269 eol_str = SDATA (eoltype);
21270 eol_str_len = SBYTES (eoltype);
21271 }
21272 else if (CHARACTERP (eoltype))
21273 {
21274 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21275 int c = XFASTINT (eoltype);
21276 eol_str_len = CHAR_STRING (c, tmp);
21277 eol_str = tmp;
21278 }
21279 else
21280 {
21281 eol_str = invalid_eol_type;
21282 eol_str_len = sizeof (invalid_eol_type) - 1;
21283 }
21284 memcpy (buf, eol_str, eol_str_len);
21285 buf += eol_str_len;
21286 }
21287
21288 return buf;
21289 }
21290
21291 /* Return a string for the output of a mode line %-spec for window W,
21292 generated by character C. FIELD_WIDTH > 0 means pad the string
21293 returned with spaces to that value. Return a Lisp string in
21294 *STRING if the resulting string is taken from that Lisp string.
21295
21296 Note we operate on the current buffer for most purposes,
21297 the exception being w->base_line_pos. */
21298
21299 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21300
21301 static const char *
21302 decode_mode_spec (struct window *w, register int c, int field_width,
21303 Lisp_Object *string)
21304 {
21305 Lisp_Object obj;
21306 struct frame *f = XFRAME (WINDOW_FRAME (w));
21307 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21308 struct buffer *b = current_buffer;
21309
21310 obj = Qnil;
21311 *string = Qnil;
21312
21313 switch (c)
21314 {
21315 case '*':
21316 if (!NILP (BVAR (b, read_only)))
21317 return "%";
21318 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21319 return "*";
21320 return "-";
21321
21322 case '+':
21323 /* This differs from %* only for a modified read-only buffer. */
21324 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21325 return "*";
21326 if (!NILP (BVAR (b, read_only)))
21327 return "%";
21328 return "-";
21329
21330 case '&':
21331 /* This differs from %* in ignoring read-only-ness. */
21332 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21333 return "*";
21334 return "-";
21335
21336 case '%':
21337 return "%";
21338
21339 case '[':
21340 {
21341 int i;
21342 char *p;
21343
21344 if (command_loop_level > 5)
21345 return "[[[... ";
21346 p = decode_mode_spec_buf;
21347 for (i = 0; i < command_loop_level; i++)
21348 *p++ = '[';
21349 *p = 0;
21350 return decode_mode_spec_buf;
21351 }
21352
21353 case ']':
21354 {
21355 int i;
21356 char *p;
21357
21358 if (command_loop_level > 5)
21359 return " ...]]]";
21360 p = decode_mode_spec_buf;
21361 for (i = 0; i < command_loop_level; i++)
21362 *p++ = ']';
21363 *p = 0;
21364 return decode_mode_spec_buf;
21365 }
21366
21367 case '-':
21368 {
21369 register int i;
21370
21371 /* Let lots_of_dashes be a string of infinite length. */
21372 if (mode_line_target == MODE_LINE_NOPROP ||
21373 mode_line_target == MODE_LINE_STRING)
21374 return "--";
21375 if (field_width <= 0
21376 || field_width > sizeof (lots_of_dashes))
21377 {
21378 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21379 decode_mode_spec_buf[i] = '-';
21380 decode_mode_spec_buf[i] = '\0';
21381 return decode_mode_spec_buf;
21382 }
21383 else
21384 return lots_of_dashes;
21385 }
21386
21387 case 'b':
21388 obj = BVAR (b, name);
21389 break;
21390
21391 case 'c':
21392 /* %c and %l are ignored in `frame-title-format'.
21393 (In redisplay_internal, the frame title is drawn _before_ the
21394 windows are updated, so the stuff which depends on actual
21395 window contents (such as %l) may fail to render properly, or
21396 even crash emacs.) */
21397 if (mode_line_target == MODE_LINE_TITLE)
21398 return "";
21399 else
21400 {
21401 ptrdiff_t col = current_column ();
21402 w->column_number_displayed = make_number (col);
21403 pint2str (decode_mode_spec_buf, field_width, col);
21404 return decode_mode_spec_buf;
21405 }
21406
21407 case 'e':
21408 #ifndef SYSTEM_MALLOC
21409 {
21410 if (NILP (Vmemory_full))
21411 return "";
21412 else
21413 return "!MEM FULL! ";
21414 }
21415 #else
21416 return "";
21417 #endif
21418
21419 case 'F':
21420 /* %F displays the frame name. */
21421 if (!NILP (f->title))
21422 return SSDATA (f->title);
21423 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21424 return SSDATA (f->name);
21425 return "Emacs";
21426
21427 case 'f':
21428 obj = BVAR (b, filename);
21429 break;
21430
21431 case 'i':
21432 {
21433 ptrdiff_t size = ZV - BEGV;
21434 pint2str (decode_mode_spec_buf, field_width, size);
21435 return decode_mode_spec_buf;
21436 }
21437
21438 case 'I':
21439 {
21440 ptrdiff_t size = ZV - BEGV;
21441 pint2hrstr (decode_mode_spec_buf, field_width, size);
21442 return decode_mode_spec_buf;
21443 }
21444
21445 case 'l':
21446 {
21447 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21448 ptrdiff_t topline, nlines, height;
21449 ptrdiff_t junk;
21450
21451 /* %c and %l are ignored in `frame-title-format'. */
21452 if (mode_line_target == MODE_LINE_TITLE)
21453 return "";
21454
21455 startpos = XMARKER (w->start)->charpos;
21456 startpos_byte = marker_byte_position (w->start);
21457 height = WINDOW_TOTAL_LINES (w);
21458
21459 /* If we decided that this buffer isn't suitable for line numbers,
21460 don't forget that too fast. */
21461 if (EQ (w->base_line_pos, w->buffer))
21462 goto no_value;
21463 /* But do forget it, if the window shows a different buffer now. */
21464 else if (BUFFERP (w->base_line_pos))
21465 w->base_line_pos = Qnil;
21466
21467 /* If the buffer is very big, don't waste time. */
21468 if (INTEGERP (Vline_number_display_limit)
21469 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21470 {
21471 w->base_line_pos = Qnil;
21472 w->base_line_number = Qnil;
21473 goto no_value;
21474 }
21475
21476 if (INTEGERP (w->base_line_number)
21477 && INTEGERP (w->base_line_pos)
21478 && XFASTINT (w->base_line_pos) <= startpos)
21479 {
21480 line = XFASTINT (w->base_line_number);
21481 linepos = XFASTINT (w->base_line_pos);
21482 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21483 }
21484 else
21485 {
21486 line = 1;
21487 linepos = BUF_BEGV (b);
21488 linepos_byte = BUF_BEGV_BYTE (b);
21489 }
21490
21491 /* Count lines from base line to window start position. */
21492 nlines = display_count_lines (linepos_byte,
21493 startpos_byte,
21494 startpos, &junk);
21495
21496 topline = nlines + line;
21497
21498 /* Determine a new base line, if the old one is too close
21499 or too far away, or if we did not have one.
21500 "Too close" means it's plausible a scroll-down would
21501 go back past it. */
21502 if (startpos == BUF_BEGV (b))
21503 {
21504 w->base_line_number = make_number (topline);
21505 w->base_line_pos = make_number (BUF_BEGV (b));
21506 }
21507 else if (nlines < height + 25 || nlines > height * 3 + 50
21508 || linepos == BUF_BEGV (b))
21509 {
21510 ptrdiff_t limit = BUF_BEGV (b);
21511 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21512 ptrdiff_t position;
21513 ptrdiff_t distance =
21514 (height * 2 + 30) * line_number_display_limit_width;
21515
21516 if (startpos - distance > limit)
21517 {
21518 limit = startpos - distance;
21519 limit_byte = CHAR_TO_BYTE (limit);
21520 }
21521
21522 nlines = display_count_lines (startpos_byte,
21523 limit_byte,
21524 - (height * 2 + 30),
21525 &position);
21526 /* If we couldn't find the lines we wanted within
21527 line_number_display_limit_width chars per line,
21528 give up on line numbers for this window. */
21529 if (position == limit_byte && limit == startpos - distance)
21530 {
21531 w->base_line_pos = w->buffer;
21532 w->base_line_number = Qnil;
21533 goto no_value;
21534 }
21535
21536 w->base_line_number = make_number (topline - nlines);
21537 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21538 }
21539
21540 /* Now count lines from the start pos to point. */
21541 nlines = display_count_lines (startpos_byte,
21542 PT_BYTE, PT, &junk);
21543
21544 /* Record that we did display the line number. */
21545 line_number_displayed = 1;
21546
21547 /* Make the string to show. */
21548 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21549 return decode_mode_spec_buf;
21550 no_value:
21551 {
21552 char* p = decode_mode_spec_buf;
21553 int pad = field_width - 2;
21554 while (pad-- > 0)
21555 *p++ = ' ';
21556 *p++ = '?';
21557 *p++ = '?';
21558 *p = '\0';
21559 return decode_mode_spec_buf;
21560 }
21561 }
21562 break;
21563
21564 case 'm':
21565 obj = BVAR (b, mode_name);
21566 break;
21567
21568 case 'n':
21569 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21570 return " Narrow";
21571 break;
21572
21573 case 'p':
21574 {
21575 ptrdiff_t pos = marker_position (w->start);
21576 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21577
21578 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21579 {
21580 if (pos <= BUF_BEGV (b))
21581 return "All";
21582 else
21583 return "Bottom";
21584 }
21585 else if (pos <= BUF_BEGV (b))
21586 return "Top";
21587 else
21588 {
21589 if (total > 1000000)
21590 /* Do it differently for a large value, to avoid overflow. */
21591 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21592 else
21593 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21594 /* We can't normally display a 3-digit number,
21595 so get us a 2-digit number that is close. */
21596 if (total == 100)
21597 total = 99;
21598 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21599 return decode_mode_spec_buf;
21600 }
21601 }
21602
21603 /* Display percentage of size above the bottom of the screen. */
21604 case 'P':
21605 {
21606 ptrdiff_t toppos = marker_position (w->start);
21607 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21608 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21609
21610 if (botpos >= BUF_ZV (b))
21611 {
21612 if (toppos <= BUF_BEGV (b))
21613 return "All";
21614 else
21615 return "Bottom";
21616 }
21617 else
21618 {
21619 if (total > 1000000)
21620 /* Do it differently for a large value, to avoid overflow. */
21621 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21622 else
21623 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21624 /* We can't normally display a 3-digit number,
21625 so get us a 2-digit number that is close. */
21626 if (total == 100)
21627 total = 99;
21628 if (toppos <= BUF_BEGV (b))
21629 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21630 else
21631 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21632 return decode_mode_spec_buf;
21633 }
21634 }
21635
21636 case 's':
21637 /* status of process */
21638 obj = Fget_buffer_process (Fcurrent_buffer ());
21639 if (NILP (obj))
21640 return "no process";
21641 #ifndef MSDOS
21642 obj = Fsymbol_name (Fprocess_status (obj));
21643 #endif
21644 break;
21645
21646 case '@':
21647 {
21648 ptrdiff_t count = inhibit_garbage_collection ();
21649 Lisp_Object val = call1 (intern ("file-remote-p"),
21650 BVAR (current_buffer, directory));
21651 unbind_to (count, Qnil);
21652
21653 if (NILP (val))
21654 return "-";
21655 else
21656 return "@";
21657 }
21658
21659 case 't': /* indicate TEXT or BINARY */
21660 return "T";
21661
21662 case 'z':
21663 /* coding-system (not including end-of-line format) */
21664 case 'Z':
21665 /* coding-system (including end-of-line type) */
21666 {
21667 int eol_flag = (c == 'Z');
21668 char *p = decode_mode_spec_buf;
21669
21670 if (! FRAME_WINDOW_P (f))
21671 {
21672 /* No need to mention EOL here--the terminal never needs
21673 to do EOL conversion. */
21674 p = decode_mode_spec_coding (CODING_ID_NAME
21675 (FRAME_KEYBOARD_CODING (f)->id),
21676 p, 0);
21677 p = decode_mode_spec_coding (CODING_ID_NAME
21678 (FRAME_TERMINAL_CODING (f)->id),
21679 p, 0);
21680 }
21681 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21682 p, eol_flag);
21683
21684 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21685 #ifdef subprocesses
21686 obj = Fget_buffer_process (Fcurrent_buffer ());
21687 if (PROCESSP (obj))
21688 {
21689 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21690 p, eol_flag);
21691 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21692 p, eol_flag);
21693 }
21694 #endif /* subprocesses */
21695 #endif /* 0 */
21696 *p = 0;
21697 return decode_mode_spec_buf;
21698 }
21699 }
21700
21701 if (STRINGP (obj))
21702 {
21703 *string = obj;
21704 return SSDATA (obj);
21705 }
21706 else
21707 return "";
21708 }
21709
21710
21711 /* Count up to COUNT lines starting from START_BYTE.
21712 But don't go beyond LIMIT_BYTE.
21713 Return the number of lines thus found (always nonnegative).
21714
21715 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21716
21717 static ptrdiff_t
21718 display_count_lines (ptrdiff_t start_byte,
21719 ptrdiff_t limit_byte, ptrdiff_t count,
21720 ptrdiff_t *byte_pos_ptr)
21721 {
21722 register unsigned char *cursor;
21723 unsigned char *base;
21724
21725 register ptrdiff_t ceiling;
21726 register unsigned char *ceiling_addr;
21727 ptrdiff_t orig_count = count;
21728
21729 /* If we are not in selective display mode,
21730 check only for newlines. */
21731 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21732 && !INTEGERP (BVAR (current_buffer, selective_display)));
21733
21734 if (count > 0)
21735 {
21736 while (start_byte < limit_byte)
21737 {
21738 ceiling = BUFFER_CEILING_OF (start_byte);
21739 ceiling = min (limit_byte - 1, ceiling);
21740 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21741 base = (cursor = BYTE_POS_ADDR (start_byte));
21742 while (1)
21743 {
21744 if (selective_display)
21745 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21746 ;
21747 else
21748 while (*cursor != '\n' && ++cursor != ceiling_addr)
21749 ;
21750
21751 if (cursor != ceiling_addr)
21752 {
21753 if (--count == 0)
21754 {
21755 start_byte += cursor - base + 1;
21756 *byte_pos_ptr = start_byte;
21757 return orig_count;
21758 }
21759 else
21760 if (++cursor == ceiling_addr)
21761 break;
21762 }
21763 else
21764 break;
21765 }
21766 start_byte += cursor - base;
21767 }
21768 }
21769 else
21770 {
21771 while (start_byte > limit_byte)
21772 {
21773 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21774 ceiling = max (limit_byte, ceiling);
21775 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21776 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21777 while (1)
21778 {
21779 if (selective_display)
21780 while (--cursor != ceiling_addr
21781 && *cursor != '\n' && *cursor != 015)
21782 ;
21783 else
21784 while (--cursor != ceiling_addr && *cursor != '\n')
21785 ;
21786
21787 if (cursor != ceiling_addr)
21788 {
21789 if (++count == 0)
21790 {
21791 start_byte += cursor - base + 1;
21792 *byte_pos_ptr = start_byte;
21793 /* When scanning backwards, we should
21794 not count the newline posterior to which we stop. */
21795 return - orig_count - 1;
21796 }
21797 }
21798 else
21799 break;
21800 }
21801 /* Here we add 1 to compensate for the last decrement
21802 of CURSOR, which took it past the valid range. */
21803 start_byte += cursor - base + 1;
21804 }
21805 }
21806
21807 *byte_pos_ptr = limit_byte;
21808
21809 if (count < 0)
21810 return - orig_count + count;
21811 return orig_count - count;
21812
21813 }
21814
21815
21816 \f
21817 /***********************************************************************
21818 Displaying strings
21819 ***********************************************************************/
21820
21821 /* Display a NUL-terminated string, starting with index START.
21822
21823 If STRING is non-null, display that C string. Otherwise, the Lisp
21824 string LISP_STRING is displayed. There's a case that STRING is
21825 non-null and LISP_STRING is not nil. It means STRING is a string
21826 data of LISP_STRING. In that case, we display LISP_STRING while
21827 ignoring its text properties.
21828
21829 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21830 FACE_STRING. Display STRING or LISP_STRING with the face at
21831 FACE_STRING_POS in FACE_STRING:
21832
21833 Display the string in the environment given by IT, but use the
21834 standard display table, temporarily.
21835
21836 FIELD_WIDTH is the minimum number of output glyphs to produce.
21837 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21838 with spaces. If STRING has more characters, more than FIELD_WIDTH
21839 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21840
21841 PRECISION is the maximum number of characters to output from
21842 STRING. PRECISION < 0 means don't truncate the string.
21843
21844 This is roughly equivalent to printf format specifiers:
21845
21846 FIELD_WIDTH PRECISION PRINTF
21847 ----------------------------------------
21848 -1 -1 %s
21849 -1 10 %.10s
21850 10 -1 %10s
21851 20 10 %20.10s
21852
21853 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21854 display them, and < 0 means obey the current buffer's value of
21855 enable_multibyte_characters.
21856
21857 Value is the number of columns displayed. */
21858
21859 static int
21860 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21861 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21862 int field_width, int precision, int max_x, int multibyte)
21863 {
21864 int hpos_at_start = it->hpos;
21865 int saved_face_id = it->face_id;
21866 struct glyph_row *row = it->glyph_row;
21867 ptrdiff_t it_charpos;
21868
21869 /* Initialize the iterator IT for iteration over STRING beginning
21870 with index START. */
21871 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21872 precision, field_width, multibyte);
21873 if (string && STRINGP (lisp_string))
21874 /* LISP_STRING is the one returned by decode_mode_spec. We should
21875 ignore its text properties. */
21876 it->stop_charpos = it->end_charpos;
21877
21878 /* If displaying STRING, set up the face of the iterator from
21879 FACE_STRING, if that's given. */
21880 if (STRINGP (face_string))
21881 {
21882 ptrdiff_t endptr;
21883 struct face *face;
21884
21885 it->face_id
21886 = face_at_string_position (it->w, face_string, face_string_pos,
21887 0, it->region_beg_charpos,
21888 it->region_end_charpos,
21889 &endptr, it->base_face_id, 0);
21890 face = FACE_FROM_ID (it->f, it->face_id);
21891 it->face_box_p = face->box != FACE_NO_BOX;
21892 }
21893
21894 /* Set max_x to the maximum allowed X position. Don't let it go
21895 beyond the right edge of the window. */
21896 if (max_x <= 0)
21897 max_x = it->last_visible_x;
21898 else
21899 max_x = min (max_x, it->last_visible_x);
21900
21901 /* Skip over display elements that are not visible. because IT->w is
21902 hscrolled. */
21903 if (it->current_x < it->first_visible_x)
21904 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21905 MOVE_TO_POS | MOVE_TO_X);
21906
21907 row->ascent = it->max_ascent;
21908 row->height = it->max_ascent + it->max_descent;
21909 row->phys_ascent = it->max_phys_ascent;
21910 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21911 row->extra_line_spacing = it->max_extra_line_spacing;
21912
21913 if (STRINGP (it->string))
21914 it_charpos = IT_STRING_CHARPOS (*it);
21915 else
21916 it_charpos = IT_CHARPOS (*it);
21917
21918 /* This condition is for the case that we are called with current_x
21919 past last_visible_x. */
21920 while (it->current_x < max_x)
21921 {
21922 int x_before, x, n_glyphs_before, i, nglyphs;
21923
21924 /* Get the next display element. */
21925 if (!get_next_display_element (it))
21926 break;
21927
21928 /* Produce glyphs. */
21929 x_before = it->current_x;
21930 n_glyphs_before = row->used[TEXT_AREA];
21931 PRODUCE_GLYPHS (it);
21932
21933 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21934 i = 0;
21935 x = x_before;
21936 while (i < nglyphs)
21937 {
21938 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21939
21940 if (it->line_wrap != TRUNCATE
21941 && x + glyph->pixel_width > max_x)
21942 {
21943 /* End of continued line or max_x reached. */
21944 if (CHAR_GLYPH_PADDING_P (*glyph))
21945 {
21946 /* A wide character is unbreakable. */
21947 if (row->reversed_p)
21948 unproduce_glyphs (it, row->used[TEXT_AREA]
21949 - n_glyphs_before);
21950 row->used[TEXT_AREA] = n_glyphs_before;
21951 it->current_x = x_before;
21952 }
21953 else
21954 {
21955 if (row->reversed_p)
21956 unproduce_glyphs (it, row->used[TEXT_AREA]
21957 - (n_glyphs_before + i));
21958 row->used[TEXT_AREA] = n_glyphs_before + i;
21959 it->current_x = x;
21960 }
21961 break;
21962 }
21963 else if (x + glyph->pixel_width >= it->first_visible_x)
21964 {
21965 /* Glyph is at least partially visible. */
21966 ++it->hpos;
21967 if (x < it->first_visible_x)
21968 row->x = x - it->first_visible_x;
21969 }
21970 else
21971 {
21972 /* Glyph is off the left margin of the display area.
21973 Should not happen. */
21974 abort ();
21975 }
21976
21977 row->ascent = max (row->ascent, it->max_ascent);
21978 row->height = max (row->height, it->max_ascent + it->max_descent);
21979 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21980 row->phys_height = max (row->phys_height,
21981 it->max_phys_ascent + it->max_phys_descent);
21982 row->extra_line_spacing = max (row->extra_line_spacing,
21983 it->max_extra_line_spacing);
21984 x += glyph->pixel_width;
21985 ++i;
21986 }
21987
21988 /* Stop if max_x reached. */
21989 if (i < nglyphs)
21990 break;
21991
21992 /* Stop at line ends. */
21993 if (ITERATOR_AT_END_OF_LINE_P (it))
21994 {
21995 it->continuation_lines_width = 0;
21996 break;
21997 }
21998
21999 set_iterator_to_next (it, 1);
22000 if (STRINGP (it->string))
22001 it_charpos = IT_STRING_CHARPOS (*it);
22002 else
22003 it_charpos = IT_CHARPOS (*it);
22004
22005 /* Stop if truncating at the right edge. */
22006 if (it->line_wrap == TRUNCATE
22007 && it->current_x >= it->last_visible_x)
22008 {
22009 /* Add truncation mark, but don't do it if the line is
22010 truncated at a padding space. */
22011 if (it_charpos < it->string_nchars)
22012 {
22013 if (!FRAME_WINDOW_P (it->f))
22014 {
22015 int ii, n;
22016
22017 if (it->current_x > it->last_visible_x)
22018 {
22019 if (!row->reversed_p)
22020 {
22021 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22022 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22023 break;
22024 }
22025 else
22026 {
22027 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22028 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22029 break;
22030 unproduce_glyphs (it, ii + 1);
22031 ii = row->used[TEXT_AREA] - (ii + 1);
22032 }
22033 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22034 {
22035 row->used[TEXT_AREA] = ii;
22036 produce_special_glyphs (it, IT_TRUNCATION);
22037 }
22038 }
22039 produce_special_glyphs (it, IT_TRUNCATION);
22040 }
22041 row->truncated_on_right_p = 1;
22042 }
22043 break;
22044 }
22045 }
22046
22047 /* Maybe insert a truncation at the left. */
22048 if (it->first_visible_x
22049 && it_charpos > 0)
22050 {
22051 if (!FRAME_WINDOW_P (it->f)
22052 || (row->reversed_p
22053 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22054 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22055 insert_left_trunc_glyphs (it);
22056 row->truncated_on_left_p = 1;
22057 }
22058
22059 it->face_id = saved_face_id;
22060
22061 /* Value is number of columns displayed. */
22062 return it->hpos - hpos_at_start;
22063 }
22064
22065
22066 \f
22067 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22068 appears as an element of LIST or as the car of an element of LIST.
22069 If PROPVAL is a list, compare each element against LIST in that
22070 way, and return 1/2 if any element of PROPVAL is found in LIST.
22071 Otherwise return 0. This function cannot quit.
22072 The return value is 2 if the text is invisible but with an ellipsis
22073 and 1 if it's invisible and without an ellipsis. */
22074
22075 int
22076 invisible_p (register Lisp_Object propval, Lisp_Object list)
22077 {
22078 register Lisp_Object tail, proptail;
22079
22080 for (tail = list; CONSP (tail); tail = XCDR (tail))
22081 {
22082 register Lisp_Object tem;
22083 tem = XCAR (tail);
22084 if (EQ (propval, tem))
22085 return 1;
22086 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22087 return NILP (XCDR (tem)) ? 1 : 2;
22088 }
22089
22090 if (CONSP (propval))
22091 {
22092 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22093 {
22094 Lisp_Object propelt;
22095 propelt = XCAR (proptail);
22096 for (tail = list; CONSP (tail); tail = XCDR (tail))
22097 {
22098 register Lisp_Object tem;
22099 tem = XCAR (tail);
22100 if (EQ (propelt, tem))
22101 return 1;
22102 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22103 return NILP (XCDR (tem)) ? 1 : 2;
22104 }
22105 }
22106 }
22107
22108 return 0;
22109 }
22110
22111 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22112 doc: /* Non-nil if the property makes the text invisible.
22113 POS-OR-PROP can be a marker or number, in which case it is taken to be
22114 a position in the current buffer and the value of the `invisible' property
22115 is checked; or it can be some other value, which is then presumed to be the
22116 value of the `invisible' property of the text of interest.
22117 The non-nil value returned can be t for truly invisible text or something
22118 else if the text is replaced by an ellipsis. */)
22119 (Lisp_Object pos_or_prop)
22120 {
22121 Lisp_Object prop
22122 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22123 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22124 : pos_or_prop);
22125 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22126 return (invis == 0 ? Qnil
22127 : invis == 1 ? Qt
22128 : make_number (invis));
22129 }
22130
22131 /* Calculate a width or height in pixels from a specification using
22132 the following elements:
22133
22134 SPEC ::=
22135 NUM - a (fractional) multiple of the default font width/height
22136 (NUM) - specifies exactly NUM pixels
22137 UNIT - a fixed number of pixels, see below.
22138 ELEMENT - size of a display element in pixels, see below.
22139 (NUM . SPEC) - equals NUM * SPEC
22140 (+ SPEC SPEC ...) - add pixel values
22141 (- SPEC SPEC ...) - subtract pixel values
22142 (- SPEC) - negate pixel value
22143
22144 NUM ::=
22145 INT or FLOAT - a number constant
22146 SYMBOL - use symbol's (buffer local) variable binding.
22147
22148 UNIT ::=
22149 in - pixels per inch *)
22150 mm - pixels per 1/1000 meter *)
22151 cm - pixels per 1/100 meter *)
22152 width - width of current font in pixels.
22153 height - height of current font in pixels.
22154
22155 *) using the ratio(s) defined in display-pixels-per-inch.
22156
22157 ELEMENT ::=
22158
22159 left-fringe - left fringe width in pixels
22160 right-fringe - right fringe width in pixels
22161
22162 left-margin - left margin width in pixels
22163 right-margin - right margin width in pixels
22164
22165 scroll-bar - scroll-bar area width in pixels
22166
22167 Examples:
22168
22169 Pixels corresponding to 5 inches:
22170 (5 . in)
22171
22172 Total width of non-text areas on left side of window (if scroll-bar is on left):
22173 '(space :width (+ left-fringe left-margin scroll-bar))
22174
22175 Align to first text column (in header line):
22176 '(space :align-to 0)
22177
22178 Align to middle of text area minus half the width of variable `my-image'
22179 containing a loaded image:
22180 '(space :align-to (0.5 . (- text my-image)))
22181
22182 Width of left margin minus width of 1 character in the default font:
22183 '(space :width (- left-margin 1))
22184
22185 Width of left margin minus width of 2 characters in the current font:
22186 '(space :width (- left-margin (2 . width)))
22187
22188 Center 1 character over left-margin (in header line):
22189 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22190
22191 Different ways to express width of left fringe plus left margin minus one pixel:
22192 '(space :width (- (+ left-fringe left-margin) (1)))
22193 '(space :width (+ left-fringe left-margin (- (1))))
22194 '(space :width (+ left-fringe left-margin (-1)))
22195
22196 */
22197
22198 #define NUMVAL(X) \
22199 ((INTEGERP (X) || FLOATP (X)) \
22200 ? XFLOATINT (X) \
22201 : - 1)
22202
22203 static int
22204 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22205 struct font *font, int width_p, int *align_to)
22206 {
22207 double pixels;
22208
22209 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22210 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22211
22212 if (NILP (prop))
22213 return OK_PIXELS (0);
22214
22215 eassert (FRAME_LIVE_P (it->f));
22216
22217 if (SYMBOLP (prop))
22218 {
22219 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22220 {
22221 char *unit = SSDATA (SYMBOL_NAME (prop));
22222
22223 if (unit[0] == 'i' && unit[1] == 'n')
22224 pixels = 1.0;
22225 else if (unit[0] == 'm' && unit[1] == 'm')
22226 pixels = 25.4;
22227 else if (unit[0] == 'c' && unit[1] == 'm')
22228 pixels = 2.54;
22229 else
22230 pixels = 0;
22231 if (pixels > 0)
22232 {
22233 double ppi;
22234 #ifdef HAVE_WINDOW_SYSTEM
22235 if (FRAME_WINDOW_P (it->f)
22236 && (ppi = (width_p
22237 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22238 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22239 ppi > 0))
22240 return OK_PIXELS (ppi / pixels);
22241 #endif
22242
22243 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22244 || (CONSP (Vdisplay_pixels_per_inch)
22245 && (ppi = (width_p
22246 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22247 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22248 ppi > 0)))
22249 return OK_PIXELS (ppi / pixels);
22250
22251 return 0;
22252 }
22253 }
22254
22255 #ifdef HAVE_WINDOW_SYSTEM
22256 if (EQ (prop, Qheight))
22257 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22258 if (EQ (prop, Qwidth))
22259 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22260 #else
22261 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22262 return OK_PIXELS (1);
22263 #endif
22264
22265 if (EQ (prop, Qtext))
22266 return OK_PIXELS (width_p
22267 ? window_box_width (it->w, TEXT_AREA)
22268 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22269
22270 if (align_to && *align_to < 0)
22271 {
22272 *res = 0;
22273 if (EQ (prop, Qleft))
22274 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22275 if (EQ (prop, Qright))
22276 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22277 if (EQ (prop, Qcenter))
22278 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22279 + window_box_width (it->w, TEXT_AREA) / 2);
22280 if (EQ (prop, Qleft_fringe))
22281 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22282 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22283 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22284 if (EQ (prop, Qright_fringe))
22285 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22286 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22287 : window_box_right_offset (it->w, TEXT_AREA));
22288 if (EQ (prop, Qleft_margin))
22289 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22290 if (EQ (prop, Qright_margin))
22291 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22292 if (EQ (prop, Qscroll_bar))
22293 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22294 ? 0
22295 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22296 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22297 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22298 : 0)));
22299 }
22300 else
22301 {
22302 if (EQ (prop, Qleft_fringe))
22303 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22304 if (EQ (prop, Qright_fringe))
22305 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22306 if (EQ (prop, Qleft_margin))
22307 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22308 if (EQ (prop, Qright_margin))
22309 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22310 if (EQ (prop, Qscroll_bar))
22311 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22312 }
22313
22314 prop = buffer_local_value_1 (prop, it->w->buffer);
22315 if (EQ (prop, Qunbound))
22316 prop = Qnil;
22317 }
22318
22319 if (INTEGERP (prop) || FLOATP (prop))
22320 {
22321 int base_unit = (width_p
22322 ? FRAME_COLUMN_WIDTH (it->f)
22323 : FRAME_LINE_HEIGHT (it->f));
22324 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22325 }
22326
22327 if (CONSP (prop))
22328 {
22329 Lisp_Object car = XCAR (prop);
22330 Lisp_Object cdr = XCDR (prop);
22331
22332 if (SYMBOLP (car))
22333 {
22334 #ifdef HAVE_WINDOW_SYSTEM
22335 if (FRAME_WINDOW_P (it->f)
22336 && valid_image_p (prop))
22337 {
22338 ptrdiff_t id = lookup_image (it->f, prop);
22339 struct image *img = IMAGE_FROM_ID (it->f, id);
22340
22341 return OK_PIXELS (width_p ? img->width : img->height);
22342 }
22343 #endif
22344 if (EQ (car, Qplus) || EQ (car, Qminus))
22345 {
22346 int first = 1;
22347 double px;
22348
22349 pixels = 0;
22350 while (CONSP (cdr))
22351 {
22352 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22353 font, width_p, align_to))
22354 return 0;
22355 if (first)
22356 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22357 else
22358 pixels += px;
22359 cdr = XCDR (cdr);
22360 }
22361 if (EQ (car, Qminus))
22362 pixels = -pixels;
22363 return OK_PIXELS (pixels);
22364 }
22365
22366 car = buffer_local_value_1 (car, it->w->buffer);
22367 if (EQ (car, Qunbound))
22368 car = Qnil;
22369 }
22370
22371 if (INTEGERP (car) || FLOATP (car))
22372 {
22373 double fact;
22374 pixels = XFLOATINT (car);
22375 if (NILP (cdr))
22376 return OK_PIXELS (pixels);
22377 if (calc_pixel_width_or_height (&fact, it, cdr,
22378 font, width_p, align_to))
22379 return OK_PIXELS (pixels * fact);
22380 return 0;
22381 }
22382
22383 return 0;
22384 }
22385
22386 return 0;
22387 }
22388
22389 \f
22390 /***********************************************************************
22391 Glyph Display
22392 ***********************************************************************/
22393
22394 #ifdef HAVE_WINDOW_SYSTEM
22395
22396 #ifdef GLYPH_DEBUG
22397
22398 void
22399 dump_glyph_string (struct glyph_string *s)
22400 {
22401 fprintf (stderr, "glyph string\n");
22402 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22403 s->x, s->y, s->width, s->height);
22404 fprintf (stderr, " ybase = %d\n", s->ybase);
22405 fprintf (stderr, " hl = %d\n", s->hl);
22406 fprintf (stderr, " left overhang = %d, right = %d\n",
22407 s->left_overhang, s->right_overhang);
22408 fprintf (stderr, " nchars = %d\n", s->nchars);
22409 fprintf (stderr, " extends to end of line = %d\n",
22410 s->extends_to_end_of_line_p);
22411 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22412 fprintf (stderr, " bg width = %d\n", s->background_width);
22413 }
22414
22415 #endif /* GLYPH_DEBUG */
22416
22417 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22418 of XChar2b structures for S; it can't be allocated in
22419 init_glyph_string because it must be allocated via `alloca'. W
22420 is the window on which S is drawn. ROW and AREA are the glyph row
22421 and area within the row from which S is constructed. START is the
22422 index of the first glyph structure covered by S. HL is a
22423 face-override for drawing S. */
22424
22425 #ifdef HAVE_NTGUI
22426 #define OPTIONAL_HDC(hdc) HDC hdc,
22427 #define DECLARE_HDC(hdc) HDC hdc;
22428 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22429 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22430 #endif
22431
22432 #ifndef OPTIONAL_HDC
22433 #define OPTIONAL_HDC(hdc)
22434 #define DECLARE_HDC(hdc)
22435 #define ALLOCATE_HDC(hdc, f)
22436 #define RELEASE_HDC(hdc, f)
22437 #endif
22438
22439 static void
22440 init_glyph_string (struct glyph_string *s,
22441 OPTIONAL_HDC (hdc)
22442 XChar2b *char2b, struct window *w, struct glyph_row *row,
22443 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22444 {
22445 memset (s, 0, sizeof *s);
22446 s->w = w;
22447 s->f = XFRAME (w->frame);
22448 #ifdef HAVE_NTGUI
22449 s->hdc = hdc;
22450 #endif
22451 s->display = FRAME_X_DISPLAY (s->f);
22452 s->window = FRAME_X_WINDOW (s->f);
22453 s->char2b = char2b;
22454 s->hl = hl;
22455 s->row = row;
22456 s->area = area;
22457 s->first_glyph = row->glyphs[area] + start;
22458 s->height = row->height;
22459 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22460 s->ybase = s->y + row->ascent;
22461 }
22462
22463
22464 /* Append the list of glyph strings with head H and tail T to the list
22465 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22466
22467 static inline void
22468 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22469 struct glyph_string *h, struct glyph_string *t)
22470 {
22471 if (h)
22472 {
22473 if (*head)
22474 (*tail)->next = h;
22475 else
22476 *head = h;
22477 h->prev = *tail;
22478 *tail = t;
22479 }
22480 }
22481
22482
22483 /* Prepend the list of glyph strings with head H and tail T to the
22484 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22485 result. */
22486
22487 static inline void
22488 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22489 struct glyph_string *h, struct glyph_string *t)
22490 {
22491 if (h)
22492 {
22493 if (*head)
22494 (*head)->prev = t;
22495 else
22496 *tail = t;
22497 t->next = *head;
22498 *head = h;
22499 }
22500 }
22501
22502
22503 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22504 Set *HEAD and *TAIL to the resulting list. */
22505
22506 static inline void
22507 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22508 struct glyph_string *s)
22509 {
22510 s->next = s->prev = NULL;
22511 append_glyph_string_lists (head, tail, s, s);
22512 }
22513
22514
22515 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22516 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22517 make sure that X resources for the face returned are allocated.
22518 Value is a pointer to a realized face that is ready for display if
22519 DISPLAY_P is non-zero. */
22520
22521 static inline struct face *
22522 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22523 XChar2b *char2b, int display_p)
22524 {
22525 struct face *face = FACE_FROM_ID (f, face_id);
22526
22527 if (face->font)
22528 {
22529 unsigned code = face->font->driver->encode_char (face->font, c);
22530
22531 if (code != FONT_INVALID_CODE)
22532 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22533 else
22534 STORE_XCHAR2B (char2b, 0, 0);
22535 }
22536
22537 /* Make sure X resources of the face are allocated. */
22538 #ifdef HAVE_X_WINDOWS
22539 if (display_p)
22540 #endif
22541 {
22542 eassert (face != NULL);
22543 PREPARE_FACE_FOR_DISPLAY (f, face);
22544 }
22545
22546 return face;
22547 }
22548
22549
22550 /* Get face and two-byte form of character glyph GLYPH on frame F.
22551 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22552 a pointer to a realized face that is ready for display. */
22553
22554 static inline struct face *
22555 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22556 XChar2b *char2b, int *two_byte_p)
22557 {
22558 struct face *face;
22559
22560 eassert (glyph->type == CHAR_GLYPH);
22561 face = FACE_FROM_ID (f, glyph->face_id);
22562
22563 if (two_byte_p)
22564 *two_byte_p = 0;
22565
22566 if (face->font)
22567 {
22568 unsigned code;
22569
22570 if (CHAR_BYTE8_P (glyph->u.ch))
22571 code = CHAR_TO_BYTE8 (glyph->u.ch);
22572 else
22573 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22574
22575 if (code != FONT_INVALID_CODE)
22576 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22577 else
22578 STORE_XCHAR2B (char2b, 0, 0);
22579 }
22580
22581 /* Make sure X resources of the face are allocated. */
22582 eassert (face != NULL);
22583 PREPARE_FACE_FOR_DISPLAY (f, face);
22584 return face;
22585 }
22586
22587
22588 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22589 Return 1 if FONT has a glyph for C, otherwise return 0. */
22590
22591 static inline int
22592 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22593 {
22594 unsigned code;
22595
22596 if (CHAR_BYTE8_P (c))
22597 code = CHAR_TO_BYTE8 (c);
22598 else
22599 code = font->driver->encode_char (font, c);
22600
22601 if (code == FONT_INVALID_CODE)
22602 return 0;
22603 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22604 return 1;
22605 }
22606
22607
22608 /* Fill glyph string S with composition components specified by S->cmp.
22609
22610 BASE_FACE is the base face of the composition.
22611 S->cmp_from is the index of the first component for S.
22612
22613 OVERLAPS non-zero means S should draw the foreground only, and use
22614 its physical height for clipping. See also draw_glyphs.
22615
22616 Value is the index of a component not in S. */
22617
22618 static int
22619 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22620 int overlaps)
22621 {
22622 int i;
22623 /* For all glyphs of this composition, starting at the offset
22624 S->cmp_from, until we reach the end of the definition or encounter a
22625 glyph that requires the different face, add it to S. */
22626 struct face *face;
22627
22628 eassert (s);
22629
22630 s->for_overlaps = overlaps;
22631 s->face = NULL;
22632 s->font = NULL;
22633 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22634 {
22635 int c = COMPOSITION_GLYPH (s->cmp, i);
22636
22637 /* TAB in a composition means display glyphs with padding space
22638 on the left or right. */
22639 if (c != '\t')
22640 {
22641 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22642 -1, Qnil);
22643
22644 face = get_char_face_and_encoding (s->f, c, face_id,
22645 s->char2b + i, 1);
22646 if (face)
22647 {
22648 if (! s->face)
22649 {
22650 s->face = face;
22651 s->font = s->face->font;
22652 }
22653 else if (s->face != face)
22654 break;
22655 }
22656 }
22657 ++s->nchars;
22658 }
22659 s->cmp_to = i;
22660
22661 if (s->face == NULL)
22662 {
22663 s->face = base_face->ascii_face;
22664 s->font = s->face->font;
22665 }
22666
22667 /* All glyph strings for the same composition has the same width,
22668 i.e. the width set for the first component of the composition. */
22669 s->width = s->first_glyph->pixel_width;
22670
22671 /* If the specified font could not be loaded, use the frame's
22672 default font, but record the fact that we couldn't load it in
22673 the glyph string so that we can draw rectangles for the
22674 characters of the glyph string. */
22675 if (s->font == NULL)
22676 {
22677 s->font_not_found_p = 1;
22678 s->font = FRAME_FONT (s->f);
22679 }
22680
22681 /* Adjust base line for subscript/superscript text. */
22682 s->ybase += s->first_glyph->voffset;
22683
22684 /* This glyph string must always be drawn with 16-bit functions. */
22685 s->two_byte_p = 1;
22686
22687 return s->cmp_to;
22688 }
22689
22690 static int
22691 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22692 int start, int end, int overlaps)
22693 {
22694 struct glyph *glyph, *last;
22695 Lisp_Object lgstring;
22696 int i;
22697
22698 s->for_overlaps = overlaps;
22699 glyph = s->row->glyphs[s->area] + start;
22700 last = s->row->glyphs[s->area] + end;
22701 s->cmp_id = glyph->u.cmp.id;
22702 s->cmp_from = glyph->slice.cmp.from;
22703 s->cmp_to = glyph->slice.cmp.to + 1;
22704 s->face = FACE_FROM_ID (s->f, face_id);
22705 lgstring = composition_gstring_from_id (s->cmp_id);
22706 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22707 glyph++;
22708 while (glyph < last
22709 && glyph->u.cmp.automatic
22710 && glyph->u.cmp.id == s->cmp_id
22711 && s->cmp_to == glyph->slice.cmp.from)
22712 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22713
22714 for (i = s->cmp_from; i < s->cmp_to; i++)
22715 {
22716 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22717 unsigned code = LGLYPH_CODE (lglyph);
22718
22719 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22720 }
22721 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22722 return glyph - s->row->glyphs[s->area];
22723 }
22724
22725
22726 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22727 See the comment of fill_glyph_string for arguments.
22728 Value is the index of the first glyph not in S. */
22729
22730
22731 static int
22732 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22733 int start, int end, int overlaps)
22734 {
22735 struct glyph *glyph, *last;
22736 int voffset;
22737
22738 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22739 s->for_overlaps = overlaps;
22740 glyph = s->row->glyphs[s->area] + start;
22741 last = s->row->glyphs[s->area] + end;
22742 voffset = glyph->voffset;
22743 s->face = FACE_FROM_ID (s->f, face_id);
22744 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22745 s->nchars = 1;
22746 s->width = glyph->pixel_width;
22747 glyph++;
22748 while (glyph < last
22749 && glyph->type == GLYPHLESS_GLYPH
22750 && glyph->voffset == voffset
22751 && glyph->face_id == face_id)
22752 {
22753 s->nchars++;
22754 s->width += glyph->pixel_width;
22755 glyph++;
22756 }
22757 s->ybase += voffset;
22758 return glyph - s->row->glyphs[s->area];
22759 }
22760
22761
22762 /* Fill glyph string S from a sequence of character glyphs.
22763
22764 FACE_ID is the face id of the string. START is the index of the
22765 first glyph to consider, END is the index of the last + 1.
22766 OVERLAPS non-zero means S should draw the foreground only, and use
22767 its physical height for clipping. See also draw_glyphs.
22768
22769 Value is the index of the first glyph not in S. */
22770
22771 static int
22772 fill_glyph_string (struct glyph_string *s, int face_id,
22773 int start, int end, int overlaps)
22774 {
22775 struct glyph *glyph, *last;
22776 int voffset;
22777 int glyph_not_available_p;
22778
22779 eassert (s->f == XFRAME (s->w->frame));
22780 eassert (s->nchars == 0);
22781 eassert (start >= 0 && end > start);
22782
22783 s->for_overlaps = overlaps;
22784 glyph = s->row->glyphs[s->area] + start;
22785 last = s->row->glyphs[s->area] + end;
22786 voffset = glyph->voffset;
22787 s->padding_p = glyph->padding_p;
22788 glyph_not_available_p = glyph->glyph_not_available_p;
22789
22790 while (glyph < last
22791 && glyph->type == CHAR_GLYPH
22792 && glyph->voffset == voffset
22793 /* Same face id implies same font, nowadays. */
22794 && glyph->face_id == face_id
22795 && glyph->glyph_not_available_p == glyph_not_available_p)
22796 {
22797 int two_byte_p;
22798
22799 s->face = get_glyph_face_and_encoding (s->f, glyph,
22800 s->char2b + s->nchars,
22801 &two_byte_p);
22802 s->two_byte_p = two_byte_p;
22803 ++s->nchars;
22804 eassert (s->nchars <= end - start);
22805 s->width += glyph->pixel_width;
22806 if (glyph++->padding_p != s->padding_p)
22807 break;
22808 }
22809
22810 s->font = s->face->font;
22811
22812 /* If the specified font could not be loaded, use the frame's font,
22813 but record the fact that we couldn't load it in
22814 S->font_not_found_p so that we can draw rectangles for the
22815 characters of the glyph string. */
22816 if (s->font == NULL || glyph_not_available_p)
22817 {
22818 s->font_not_found_p = 1;
22819 s->font = FRAME_FONT (s->f);
22820 }
22821
22822 /* Adjust base line for subscript/superscript text. */
22823 s->ybase += voffset;
22824
22825 eassert (s->face && s->face->gc);
22826 return glyph - s->row->glyphs[s->area];
22827 }
22828
22829
22830 /* Fill glyph string S from image glyph S->first_glyph. */
22831
22832 static void
22833 fill_image_glyph_string (struct glyph_string *s)
22834 {
22835 eassert (s->first_glyph->type == IMAGE_GLYPH);
22836 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22837 eassert (s->img);
22838 s->slice = s->first_glyph->slice.img;
22839 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22840 s->font = s->face->font;
22841 s->width = s->first_glyph->pixel_width;
22842
22843 /* Adjust base line for subscript/superscript text. */
22844 s->ybase += s->first_glyph->voffset;
22845 }
22846
22847
22848 /* Fill glyph string S from a sequence of stretch glyphs.
22849
22850 START is the index of the first glyph to consider,
22851 END is the index of the last + 1.
22852
22853 Value is the index of the first glyph not in S. */
22854
22855 static int
22856 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22857 {
22858 struct glyph *glyph, *last;
22859 int voffset, face_id;
22860
22861 eassert (s->first_glyph->type == STRETCH_GLYPH);
22862
22863 glyph = s->row->glyphs[s->area] + start;
22864 last = s->row->glyphs[s->area] + end;
22865 face_id = glyph->face_id;
22866 s->face = FACE_FROM_ID (s->f, face_id);
22867 s->font = s->face->font;
22868 s->width = glyph->pixel_width;
22869 s->nchars = 1;
22870 voffset = glyph->voffset;
22871
22872 for (++glyph;
22873 (glyph < last
22874 && glyph->type == STRETCH_GLYPH
22875 && glyph->voffset == voffset
22876 && glyph->face_id == face_id);
22877 ++glyph)
22878 s->width += glyph->pixel_width;
22879
22880 /* Adjust base line for subscript/superscript text. */
22881 s->ybase += voffset;
22882
22883 /* The case that face->gc == 0 is handled when drawing the glyph
22884 string by calling PREPARE_FACE_FOR_DISPLAY. */
22885 eassert (s->face);
22886 return glyph - s->row->glyphs[s->area];
22887 }
22888
22889 static struct font_metrics *
22890 get_per_char_metric (struct font *font, XChar2b *char2b)
22891 {
22892 static struct font_metrics metrics;
22893 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22894
22895 if (! font || code == FONT_INVALID_CODE)
22896 return NULL;
22897 font->driver->text_extents (font, &code, 1, &metrics);
22898 return &metrics;
22899 }
22900
22901 /* EXPORT for RIF:
22902 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22903 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22904 assumed to be zero. */
22905
22906 void
22907 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22908 {
22909 *left = *right = 0;
22910
22911 if (glyph->type == CHAR_GLYPH)
22912 {
22913 struct face *face;
22914 XChar2b char2b;
22915 struct font_metrics *pcm;
22916
22917 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22918 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22919 {
22920 if (pcm->rbearing > pcm->width)
22921 *right = pcm->rbearing - pcm->width;
22922 if (pcm->lbearing < 0)
22923 *left = -pcm->lbearing;
22924 }
22925 }
22926 else if (glyph->type == COMPOSITE_GLYPH)
22927 {
22928 if (! glyph->u.cmp.automatic)
22929 {
22930 struct composition *cmp = composition_table[glyph->u.cmp.id];
22931
22932 if (cmp->rbearing > cmp->pixel_width)
22933 *right = cmp->rbearing - cmp->pixel_width;
22934 if (cmp->lbearing < 0)
22935 *left = - cmp->lbearing;
22936 }
22937 else
22938 {
22939 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22940 struct font_metrics metrics;
22941
22942 composition_gstring_width (gstring, glyph->slice.cmp.from,
22943 glyph->slice.cmp.to + 1, &metrics);
22944 if (metrics.rbearing > metrics.width)
22945 *right = metrics.rbearing - metrics.width;
22946 if (metrics.lbearing < 0)
22947 *left = - metrics.lbearing;
22948 }
22949 }
22950 }
22951
22952
22953 /* Return the index of the first glyph preceding glyph string S that
22954 is overwritten by S because of S's left overhang. Value is -1
22955 if no glyphs are overwritten. */
22956
22957 static int
22958 left_overwritten (struct glyph_string *s)
22959 {
22960 int k;
22961
22962 if (s->left_overhang)
22963 {
22964 int x = 0, i;
22965 struct glyph *glyphs = s->row->glyphs[s->area];
22966 int first = s->first_glyph - glyphs;
22967
22968 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22969 x -= glyphs[i].pixel_width;
22970
22971 k = i + 1;
22972 }
22973 else
22974 k = -1;
22975
22976 return k;
22977 }
22978
22979
22980 /* Return the index of the first glyph preceding glyph string S that
22981 is overwriting S because of its right overhang. Value is -1 if no
22982 glyph in front of S overwrites S. */
22983
22984 static int
22985 left_overwriting (struct glyph_string *s)
22986 {
22987 int i, k, x;
22988 struct glyph *glyphs = s->row->glyphs[s->area];
22989 int first = s->first_glyph - glyphs;
22990
22991 k = -1;
22992 x = 0;
22993 for (i = first - 1; i >= 0; --i)
22994 {
22995 int left, right;
22996 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22997 if (x + right > 0)
22998 k = i;
22999 x -= glyphs[i].pixel_width;
23000 }
23001
23002 return k;
23003 }
23004
23005
23006 /* Return the index of the last glyph following glyph string S that is
23007 overwritten by S because of S's right overhang. Value is -1 if
23008 no such glyph is found. */
23009
23010 static int
23011 right_overwritten (struct glyph_string *s)
23012 {
23013 int k = -1;
23014
23015 if (s->right_overhang)
23016 {
23017 int x = 0, i;
23018 struct glyph *glyphs = s->row->glyphs[s->area];
23019 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23020 int end = s->row->used[s->area];
23021
23022 for (i = first; i < end && s->right_overhang > x; ++i)
23023 x += glyphs[i].pixel_width;
23024
23025 k = i;
23026 }
23027
23028 return k;
23029 }
23030
23031
23032 /* Return the index of the last glyph following glyph string S that
23033 overwrites S because of its left overhang. Value is negative
23034 if no such glyph is found. */
23035
23036 static int
23037 right_overwriting (struct glyph_string *s)
23038 {
23039 int i, k, x;
23040 int end = s->row->used[s->area];
23041 struct glyph *glyphs = s->row->glyphs[s->area];
23042 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23043
23044 k = -1;
23045 x = 0;
23046 for (i = first; i < end; ++i)
23047 {
23048 int left, right;
23049 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23050 if (x - left < 0)
23051 k = i;
23052 x += glyphs[i].pixel_width;
23053 }
23054
23055 return k;
23056 }
23057
23058
23059 /* Set background width of glyph string S. START is the index of the
23060 first glyph following S. LAST_X is the right-most x-position + 1
23061 in the drawing area. */
23062
23063 static inline void
23064 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23065 {
23066 /* If the face of this glyph string has to be drawn to the end of
23067 the drawing area, set S->extends_to_end_of_line_p. */
23068
23069 if (start == s->row->used[s->area]
23070 && s->area == TEXT_AREA
23071 && ((s->row->fill_line_p
23072 && (s->hl == DRAW_NORMAL_TEXT
23073 || s->hl == DRAW_IMAGE_RAISED
23074 || s->hl == DRAW_IMAGE_SUNKEN))
23075 || s->hl == DRAW_MOUSE_FACE))
23076 s->extends_to_end_of_line_p = 1;
23077
23078 /* If S extends its face to the end of the line, set its
23079 background_width to the distance to the right edge of the drawing
23080 area. */
23081 if (s->extends_to_end_of_line_p)
23082 s->background_width = last_x - s->x + 1;
23083 else
23084 s->background_width = s->width;
23085 }
23086
23087
23088 /* Compute overhangs and x-positions for glyph string S and its
23089 predecessors, or successors. X is the starting x-position for S.
23090 BACKWARD_P non-zero means process predecessors. */
23091
23092 static void
23093 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23094 {
23095 if (backward_p)
23096 {
23097 while (s)
23098 {
23099 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23100 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23101 x -= s->width;
23102 s->x = x;
23103 s = s->prev;
23104 }
23105 }
23106 else
23107 {
23108 while (s)
23109 {
23110 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23111 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23112 s->x = x;
23113 x += s->width;
23114 s = s->next;
23115 }
23116 }
23117 }
23118
23119
23120
23121 /* The following macros are only called from draw_glyphs below.
23122 They reference the following parameters of that function directly:
23123 `w', `row', `area', and `overlap_p'
23124 as well as the following local variables:
23125 `s', `f', and `hdc' (in W32) */
23126
23127 #ifdef HAVE_NTGUI
23128 /* On W32, silently add local `hdc' variable to argument list of
23129 init_glyph_string. */
23130 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23131 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23132 #else
23133 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23134 init_glyph_string (s, char2b, w, row, area, start, hl)
23135 #endif
23136
23137 /* Add a glyph string for a stretch glyph to the list of strings
23138 between HEAD and TAIL. START is the index of the stretch glyph in
23139 row area AREA of glyph row ROW. END is the index of the last glyph
23140 in that glyph row area. X is the current output position assigned
23141 to the new glyph string constructed. HL overrides that face of the
23142 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23143 is the right-most x-position of the drawing area. */
23144
23145 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23146 and below -- keep them on one line. */
23147 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23148 do \
23149 { \
23150 s = alloca (sizeof *s); \
23151 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23152 START = fill_stretch_glyph_string (s, START, END); \
23153 append_glyph_string (&HEAD, &TAIL, s); \
23154 s->x = (X); \
23155 } \
23156 while (0)
23157
23158
23159 /* Add a glyph string for an image glyph to the list of strings
23160 between HEAD and TAIL. START is the index of the image glyph in
23161 row area AREA of glyph row ROW. END is the index of the last glyph
23162 in that glyph row area. X is the current output position assigned
23163 to the new glyph string constructed. HL overrides that face of the
23164 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23165 is the right-most x-position of the drawing area. */
23166
23167 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23168 do \
23169 { \
23170 s = alloca (sizeof *s); \
23171 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23172 fill_image_glyph_string (s); \
23173 append_glyph_string (&HEAD, &TAIL, s); \
23174 ++START; \
23175 s->x = (X); \
23176 } \
23177 while (0)
23178
23179
23180 /* Add a glyph string for a sequence of character glyphs to the list
23181 of strings between HEAD and TAIL. START is the index of the first
23182 glyph in row area AREA of glyph row ROW that is part of the new
23183 glyph string. END is the index of the last glyph in that glyph row
23184 area. X is the current output position assigned to the new glyph
23185 string constructed. HL overrides that face of the glyph; e.g. it
23186 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23187 right-most x-position of the drawing area. */
23188
23189 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23190 do \
23191 { \
23192 int face_id; \
23193 XChar2b *char2b; \
23194 \
23195 face_id = (row)->glyphs[area][START].face_id; \
23196 \
23197 s = alloca (sizeof *s); \
23198 char2b = alloca ((END - START) * sizeof *char2b); \
23199 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23200 append_glyph_string (&HEAD, &TAIL, s); \
23201 s->x = (X); \
23202 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23203 } \
23204 while (0)
23205
23206
23207 /* Add a glyph string for a composite sequence to the list of strings
23208 between HEAD and TAIL. START is the index of the first glyph in
23209 row area AREA of glyph row ROW that is part of the new glyph
23210 string. END is the index of the last glyph in that glyph row area.
23211 X is the current output position assigned to the new glyph string
23212 constructed. HL overrides that face of the glyph; e.g. it is
23213 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23214 x-position of the drawing area. */
23215
23216 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23217 do { \
23218 int face_id = (row)->glyphs[area][START].face_id; \
23219 struct face *base_face = FACE_FROM_ID (f, face_id); \
23220 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23221 struct composition *cmp = composition_table[cmp_id]; \
23222 XChar2b *char2b; \
23223 struct glyph_string *first_s = NULL; \
23224 int n; \
23225 \
23226 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23227 \
23228 /* Make glyph_strings for each glyph sequence that is drawable by \
23229 the same face, and append them to HEAD/TAIL. */ \
23230 for (n = 0; n < cmp->glyph_len;) \
23231 { \
23232 s = alloca (sizeof *s); \
23233 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23234 append_glyph_string (&(HEAD), &(TAIL), s); \
23235 s->cmp = cmp; \
23236 s->cmp_from = n; \
23237 s->x = (X); \
23238 if (n == 0) \
23239 first_s = s; \
23240 n = fill_composite_glyph_string (s, base_face, overlaps); \
23241 } \
23242 \
23243 ++START; \
23244 s = first_s; \
23245 } while (0)
23246
23247
23248 /* Add a glyph string for a glyph-string sequence to the list of strings
23249 between HEAD and TAIL. */
23250
23251 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23252 do { \
23253 int face_id; \
23254 XChar2b *char2b; \
23255 Lisp_Object gstring; \
23256 \
23257 face_id = (row)->glyphs[area][START].face_id; \
23258 gstring = (composition_gstring_from_id \
23259 ((row)->glyphs[area][START].u.cmp.id)); \
23260 s = alloca (sizeof *s); \
23261 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23262 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23263 append_glyph_string (&(HEAD), &(TAIL), s); \
23264 s->x = (X); \
23265 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23266 } while (0)
23267
23268
23269 /* Add a glyph string for a sequence of glyphless character's glyphs
23270 to the list of strings between HEAD and TAIL. The meanings of
23271 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23272
23273 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23274 do \
23275 { \
23276 int face_id; \
23277 \
23278 face_id = (row)->glyphs[area][START].face_id; \
23279 \
23280 s = alloca (sizeof *s); \
23281 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23282 append_glyph_string (&HEAD, &TAIL, s); \
23283 s->x = (X); \
23284 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23285 overlaps); \
23286 } \
23287 while (0)
23288
23289
23290 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23291 of AREA of glyph row ROW on window W between indices START and END.
23292 HL overrides the face for drawing glyph strings, e.g. it is
23293 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23294 x-positions of the drawing area.
23295
23296 This is an ugly monster macro construct because we must use alloca
23297 to allocate glyph strings (because draw_glyphs can be called
23298 asynchronously). */
23299
23300 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23301 do \
23302 { \
23303 HEAD = TAIL = NULL; \
23304 while (START < END) \
23305 { \
23306 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23307 switch (first_glyph->type) \
23308 { \
23309 case CHAR_GLYPH: \
23310 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23311 HL, X, LAST_X); \
23312 break; \
23313 \
23314 case COMPOSITE_GLYPH: \
23315 if (first_glyph->u.cmp.automatic) \
23316 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23317 HL, X, LAST_X); \
23318 else \
23319 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23320 HL, X, LAST_X); \
23321 break; \
23322 \
23323 case STRETCH_GLYPH: \
23324 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23325 HL, X, LAST_X); \
23326 break; \
23327 \
23328 case IMAGE_GLYPH: \
23329 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23330 HL, X, LAST_X); \
23331 break; \
23332 \
23333 case GLYPHLESS_GLYPH: \
23334 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23335 HL, X, LAST_X); \
23336 break; \
23337 \
23338 default: \
23339 abort (); \
23340 } \
23341 \
23342 if (s) \
23343 { \
23344 set_glyph_string_background_width (s, START, LAST_X); \
23345 (X) += s->width; \
23346 } \
23347 } \
23348 } while (0)
23349
23350
23351 /* Draw glyphs between START and END in AREA of ROW on window W,
23352 starting at x-position X. X is relative to AREA in W. HL is a
23353 face-override with the following meaning:
23354
23355 DRAW_NORMAL_TEXT draw normally
23356 DRAW_CURSOR draw in cursor face
23357 DRAW_MOUSE_FACE draw in mouse face.
23358 DRAW_INVERSE_VIDEO draw in mode line face
23359 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23360 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23361
23362 If OVERLAPS is non-zero, draw only the foreground of characters and
23363 clip to the physical height of ROW. Non-zero value also defines
23364 the overlapping part to be drawn:
23365
23366 OVERLAPS_PRED overlap with preceding rows
23367 OVERLAPS_SUCC overlap with succeeding rows
23368 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23369 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23370
23371 Value is the x-position reached, relative to AREA of W. */
23372
23373 static int
23374 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23375 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23376 enum draw_glyphs_face hl, int overlaps)
23377 {
23378 struct glyph_string *head, *tail;
23379 struct glyph_string *s;
23380 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23381 int i, j, x_reached, last_x, area_left = 0;
23382 struct frame *f = XFRAME (WINDOW_FRAME (w));
23383 DECLARE_HDC (hdc);
23384
23385 ALLOCATE_HDC (hdc, f);
23386
23387 /* Let's rather be paranoid than getting a SEGV. */
23388 end = min (end, row->used[area]);
23389 start = max (0, start);
23390 start = min (end, start);
23391
23392 /* Translate X to frame coordinates. Set last_x to the right
23393 end of the drawing area. */
23394 if (row->full_width_p)
23395 {
23396 /* X is relative to the left edge of W, without scroll bars
23397 or fringes. */
23398 area_left = WINDOW_LEFT_EDGE_X (w);
23399 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23400 }
23401 else
23402 {
23403 area_left = window_box_left (w, area);
23404 last_x = area_left + window_box_width (w, area);
23405 }
23406 x += area_left;
23407
23408 /* Build a doubly-linked list of glyph_string structures between
23409 head and tail from what we have to draw. Note that the macro
23410 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23411 the reason we use a separate variable `i'. */
23412 i = start;
23413 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23414 if (tail)
23415 x_reached = tail->x + tail->background_width;
23416 else
23417 x_reached = x;
23418
23419 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23420 the row, redraw some glyphs in front or following the glyph
23421 strings built above. */
23422 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23423 {
23424 struct glyph_string *h, *t;
23425 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23426 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23427 int check_mouse_face = 0;
23428 int dummy_x = 0;
23429
23430 /* If mouse highlighting is on, we may need to draw adjacent
23431 glyphs using mouse-face highlighting. */
23432 if (area == TEXT_AREA && row->mouse_face_p)
23433 {
23434 struct glyph_row *mouse_beg_row, *mouse_end_row;
23435
23436 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23437 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23438
23439 if (row >= mouse_beg_row && row <= mouse_end_row)
23440 {
23441 check_mouse_face = 1;
23442 mouse_beg_col = (row == mouse_beg_row)
23443 ? hlinfo->mouse_face_beg_col : 0;
23444 mouse_end_col = (row == mouse_end_row)
23445 ? hlinfo->mouse_face_end_col
23446 : row->used[TEXT_AREA];
23447 }
23448 }
23449
23450 /* Compute overhangs for all glyph strings. */
23451 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23452 for (s = head; s; s = s->next)
23453 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23454
23455 /* Prepend glyph strings for glyphs in front of the first glyph
23456 string that are overwritten because of the first glyph
23457 string's left overhang. The background of all strings
23458 prepended must be drawn because the first glyph string
23459 draws over it. */
23460 i = left_overwritten (head);
23461 if (i >= 0)
23462 {
23463 enum draw_glyphs_face overlap_hl;
23464
23465 /* If this row contains mouse highlighting, attempt to draw
23466 the overlapped glyphs with the correct highlight. This
23467 code fails if the overlap encompasses more than one glyph
23468 and mouse-highlight spans only some of these glyphs.
23469 However, making it work perfectly involves a lot more
23470 code, and I don't know if the pathological case occurs in
23471 practice, so we'll stick to this for now. --- cyd */
23472 if (check_mouse_face
23473 && mouse_beg_col < start && mouse_end_col > i)
23474 overlap_hl = DRAW_MOUSE_FACE;
23475 else
23476 overlap_hl = DRAW_NORMAL_TEXT;
23477
23478 j = i;
23479 BUILD_GLYPH_STRINGS (j, start, h, t,
23480 overlap_hl, dummy_x, last_x);
23481 start = i;
23482 compute_overhangs_and_x (t, head->x, 1);
23483 prepend_glyph_string_lists (&head, &tail, h, t);
23484 clip_head = head;
23485 }
23486
23487 /* Prepend glyph strings for glyphs in front of the first glyph
23488 string that overwrite that glyph string because of their
23489 right overhang. For these strings, only the foreground must
23490 be drawn, because it draws over the glyph string at `head'.
23491 The background must not be drawn because this would overwrite
23492 right overhangs of preceding glyphs for which no glyph
23493 strings exist. */
23494 i = left_overwriting (head);
23495 if (i >= 0)
23496 {
23497 enum draw_glyphs_face overlap_hl;
23498
23499 if (check_mouse_face
23500 && mouse_beg_col < start && mouse_end_col > i)
23501 overlap_hl = DRAW_MOUSE_FACE;
23502 else
23503 overlap_hl = DRAW_NORMAL_TEXT;
23504
23505 clip_head = head;
23506 BUILD_GLYPH_STRINGS (i, start, h, t,
23507 overlap_hl, dummy_x, last_x);
23508 for (s = h; s; s = s->next)
23509 s->background_filled_p = 1;
23510 compute_overhangs_and_x (t, head->x, 1);
23511 prepend_glyph_string_lists (&head, &tail, h, t);
23512 }
23513
23514 /* Append glyphs strings for glyphs following the last glyph
23515 string tail that are overwritten by tail. The background of
23516 these strings has to be drawn because tail's foreground draws
23517 over it. */
23518 i = right_overwritten (tail);
23519 if (i >= 0)
23520 {
23521 enum draw_glyphs_face overlap_hl;
23522
23523 if (check_mouse_face
23524 && mouse_beg_col < i && mouse_end_col > end)
23525 overlap_hl = DRAW_MOUSE_FACE;
23526 else
23527 overlap_hl = DRAW_NORMAL_TEXT;
23528
23529 BUILD_GLYPH_STRINGS (end, i, h, t,
23530 overlap_hl, x, last_x);
23531 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23532 we don't have `end = i;' here. */
23533 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23534 append_glyph_string_lists (&head, &tail, h, t);
23535 clip_tail = tail;
23536 }
23537
23538 /* Append glyph strings for glyphs following the last glyph
23539 string tail that overwrite tail. The foreground of such
23540 glyphs has to be drawn because it writes into the background
23541 of tail. The background must not be drawn because it could
23542 paint over the foreground of following glyphs. */
23543 i = right_overwriting (tail);
23544 if (i >= 0)
23545 {
23546 enum draw_glyphs_face overlap_hl;
23547 if (check_mouse_face
23548 && mouse_beg_col < i && mouse_end_col > end)
23549 overlap_hl = DRAW_MOUSE_FACE;
23550 else
23551 overlap_hl = DRAW_NORMAL_TEXT;
23552
23553 clip_tail = tail;
23554 i++; /* We must include the Ith glyph. */
23555 BUILD_GLYPH_STRINGS (end, i, h, t,
23556 overlap_hl, x, last_x);
23557 for (s = h; s; s = s->next)
23558 s->background_filled_p = 1;
23559 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23560 append_glyph_string_lists (&head, &tail, h, t);
23561 }
23562 if (clip_head || clip_tail)
23563 for (s = head; s; s = s->next)
23564 {
23565 s->clip_head = clip_head;
23566 s->clip_tail = clip_tail;
23567 }
23568 }
23569
23570 /* Draw all strings. */
23571 for (s = head; s; s = s->next)
23572 FRAME_RIF (f)->draw_glyph_string (s);
23573
23574 #ifndef HAVE_NS
23575 /* When focus a sole frame and move horizontally, this sets on_p to 0
23576 causing a failure to erase prev cursor position. */
23577 if (area == TEXT_AREA
23578 && !row->full_width_p
23579 /* When drawing overlapping rows, only the glyph strings'
23580 foreground is drawn, which doesn't erase a cursor
23581 completely. */
23582 && !overlaps)
23583 {
23584 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23585 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23586 : (tail ? tail->x + tail->background_width : x));
23587 x0 -= area_left;
23588 x1 -= area_left;
23589
23590 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23591 row->y, MATRIX_ROW_BOTTOM_Y (row));
23592 }
23593 #endif
23594
23595 /* Value is the x-position up to which drawn, relative to AREA of W.
23596 This doesn't include parts drawn because of overhangs. */
23597 if (row->full_width_p)
23598 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23599 else
23600 x_reached -= area_left;
23601
23602 RELEASE_HDC (hdc, f);
23603
23604 return x_reached;
23605 }
23606
23607 /* Expand row matrix if too narrow. Don't expand if area
23608 is not present. */
23609
23610 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23611 { \
23612 if (!fonts_changed_p \
23613 && (it->glyph_row->glyphs[area] \
23614 < it->glyph_row->glyphs[area + 1])) \
23615 { \
23616 it->w->ncols_scale_factor++; \
23617 fonts_changed_p = 1; \
23618 } \
23619 }
23620
23621 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23622 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23623
23624 static inline void
23625 append_glyph (struct it *it)
23626 {
23627 struct glyph *glyph;
23628 enum glyph_row_area area = it->area;
23629
23630 eassert (it->glyph_row);
23631 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23632
23633 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23634 if (glyph < it->glyph_row->glyphs[area + 1])
23635 {
23636 /* If the glyph row is reversed, we need to prepend the glyph
23637 rather than append it. */
23638 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23639 {
23640 struct glyph *g;
23641
23642 /* Make room for the additional glyph. */
23643 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23644 g[1] = *g;
23645 glyph = it->glyph_row->glyphs[area];
23646 }
23647 glyph->charpos = CHARPOS (it->position);
23648 glyph->object = it->object;
23649 if (it->pixel_width > 0)
23650 {
23651 glyph->pixel_width = it->pixel_width;
23652 glyph->padding_p = 0;
23653 }
23654 else
23655 {
23656 /* Assure at least 1-pixel width. Otherwise, cursor can't
23657 be displayed correctly. */
23658 glyph->pixel_width = 1;
23659 glyph->padding_p = 1;
23660 }
23661 glyph->ascent = it->ascent;
23662 glyph->descent = it->descent;
23663 glyph->voffset = it->voffset;
23664 glyph->type = CHAR_GLYPH;
23665 glyph->avoid_cursor_p = it->avoid_cursor_p;
23666 glyph->multibyte_p = it->multibyte_p;
23667 glyph->left_box_line_p = it->start_of_box_run_p;
23668 glyph->right_box_line_p = it->end_of_box_run_p;
23669 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23670 || it->phys_descent > it->descent);
23671 glyph->glyph_not_available_p = it->glyph_not_available_p;
23672 glyph->face_id = it->face_id;
23673 glyph->u.ch = it->char_to_display;
23674 glyph->slice.img = null_glyph_slice;
23675 glyph->font_type = FONT_TYPE_UNKNOWN;
23676 if (it->bidi_p)
23677 {
23678 glyph->resolved_level = it->bidi_it.resolved_level;
23679 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23680 abort ();
23681 glyph->bidi_type = it->bidi_it.type;
23682 }
23683 else
23684 {
23685 glyph->resolved_level = 0;
23686 glyph->bidi_type = UNKNOWN_BT;
23687 }
23688 ++it->glyph_row->used[area];
23689 }
23690 else
23691 IT_EXPAND_MATRIX_WIDTH (it, area);
23692 }
23693
23694 /* Store one glyph for the composition IT->cmp_it.id in
23695 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23696 non-null. */
23697
23698 static inline void
23699 append_composite_glyph (struct it *it)
23700 {
23701 struct glyph *glyph;
23702 enum glyph_row_area area = it->area;
23703
23704 eassert (it->glyph_row);
23705
23706 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23707 if (glyph < it->glyph_row->glyphs[area + 1])
23708 {
23709 /* If the glyph row is reversed, we need to prepend the glyph
23710 rather than append it. */
23711 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23712 {
23713 struct glyph *g;
23714
23715 /* Make room for the new glyph. */
23716 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23717 g[1] = *g;
23718 glyph = it->glyph_row->glyphs[it->area];
23719 }
23720 glyph->charpos = it->cmp_it.charpos;
23721 glyph->object = it->object;
23722 glyph->pixel_width = it->pixel_width;
23723 glyph->ascent = it->ascent;
23724 glyph->descent = it->descent;
23725 glyph->voffset = it->voffset;
23726 glyph->type = COMPOSITE_GLYPH;
23727 if (it->cmp_it.ch < 0)
23728 {
23729 glyph->u.cmp.automatic = 0;
23730 glyph->u.cmp.id = it->cmp_it.id;
23731 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23732 }
23733 else
23734 {
23735 glyph->u.cmp.automatic = 1;
23736 glyph->u.cmp.id = it->cmp_it.id;
23737 glyph->slice.cmp.from = it->cmp_it.from;
23738 glyph->slice.cmp.to = it->cmp_it.to - 1;
23739 }
23740 glyph->avoid_cursor_p = it->avoid_cursor_p;
23741 glyph->multibyte_p = it->multibyte_p;
23742 glyph->left_box_line_p = it->start_of_box_run_p;
23743 glyph->right_box_line_p = it->end_of_box_run_p;
23744 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23745 || it->phys_descent > it->descent);
23746 glyph->padding_p = 0;
23747 glyph->glyph_not_available_p = 0;
23748 glyph->face_id = it->face_id;
23749 glyph->font_type = FONT_TYPE_UNKNOWN;
23750 if (it->bidi_p)
23751 {
23752 glyph->resolved_level = it->bidi_it.resolved_level;
23753 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23754 abort ();
23755 glyph->bidi_type = it->bidi_it.type;
23756 }
23757 ++it->glyph_row->used[area];
23758 }
23759 else
23760 IT_EXPAND_MATRIX_WIDTH (it, area);
23761 }
23762
23763
23764 /* Change IT->ascent and IT->height according to the setting of
23765 IT->voffset. */
23766
23767 static inline void
23768 take_vertical_position_into_account (struct it *it)
23769 {
23770 if (it->voffset)
23771 {
23772 if (it->voffset < 0)
23773 /* Increase the ascent so that we can display the text higher
23774 in the line. */
23775 it->ascent -= it->voffset;
23776 else
23777 /* Increase the descent so that we can display the text lower
23778 in the line. */
23779 it->descent += it->voffset;
23780 }
23781 }
23782
23783
23784 /* Produce glyphs/get display metrics for the image IT is loaded with.
23785 See the description of struct display_iterator in dispextern.h for
23786 an overview of struct display_iterator. */
23787
23788 static void
23789 produce_image_glyph (struct it *it)
23790 {
23791 struct image *img;
23792 struct face *face;
23793 int glyph_ascent, crop;
23794 struct glyph_slice slice;
23795
23796 eassert (it->what == IT_IMAGE);
23797
23798 face = FACE_FROM_ID (it->f, it->face_id);
23799 eassert (face);
23800 /* Make sure X resources of the face is loaded. */
23801 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23802
23803 if (it->image_id < 0)
23804 {
23805 /* Fringe bitmap. */
23806 it->ascent = it->phys_ascent = 0;
23807 it->descent = it->phys_descent = 0;
23808 it->pixel_width = 0;
23809 it->nglyphs = 0;
23810 return;
23811 }
23812
23813 img = IMAGE_FROM_ID (it->f, it->image_id);
23814 eassert (img);
23815 /* Make sure X resources of the image is loaded. */
23816 prepare_image_for_display (it->f, img);
23817
23818 slice.x = slice.y = 0;
23819 slice.width = img->width;
23820 slice.height = img->height;
23821
23822 if (INTEGERP (it->slice.x))
23823 slice.x = XINT (it->slice.x);
23824 else if (FLOATP (it->slice.x))
23825 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23826
23827 if (INTEGERP (it->slice.y))
23828 slice.y = XINT (it->slice.y);
23829 else if (FLOATP (it->slice.y))
23830 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23831
23832 if (INTEGERP (it->slice.width))
23833 slice.width = XINT (it->slice.width);
23834 else if (FLOATP (it->slice.width))
23835 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23836
23837 if (INTEGERP (it->slice.height))
23838 slice.height = XINT (it->slice.height);
23839 else if (FLOATP (it->slice.height))
23840 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23841
23842 if (slice.x >= img->width)
23843 slice.x = img->width;
23844 if (slice.y >= img->height)
23845 slice.y = img->height;
23846 if (slice.x + slice.width >= img->width)
23847 slice.width = img->width - slice.x;
23848 if (slice.y + slice.height > img->height)
23849 slice.height = img->height - slice.y;
23850
23851 if (slice.width == 0 || slice.height == 0)
23852 return;
23853
23854 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23855
23856 it->descent = slice.height - glyph_ascent;
23857 if (slice.y == 0)
23858 it->descent += img->vmargin;
23859 if (slice.y + slice.height == img->height)
23860 it->descent += img->vmargin;
23861 it->phys_descent = it->descent;
23862
23863 it->pixel_width = slice.width;
23864 if (slice.x == 0)
23865 it->pixel_width += img->hmargin;
23866 if (slice.x + slice.width == img->width)
23867 it->pixel_width += img->hmargin;
23868
23869 /* It's quite possible for images to have an ascent greater than
23870 their height, so don't get confused in that case. */
23871 if (it->descent < 0)
23872 it->descent = 0;
23873
23874 it->nglyphs = 1;
23875
23876 if (face->box != FACE_NO_BOX)
23877 {
23878 if (face->box_line_width > 0)
23879 {
23880 if (slice.y == 0)
23881 it->ascent += face->box_line_width;
23882 if (slice.y + slice.height == img->height)
23883 it->descent += face->box_line_width;
23884 }
23885
23886 if (it->start_of_box_run_p && slice.x == 0)
23887 it->pixel_width += eabs (face->box_line_width);
23888 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23889 it->pixel_width += eabs (face->box_line_width);
23890 }
23891
23892 take_vertical_position_into_account (it);
23893
23894 /* Automatically crop wide image glyphs at right edge so we can
23895 draw the cursor on same display row. */
23896 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23897 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23898 {
23899 it->pixel_width -= crop;
23900 slice.width -= crop;
23901 }
23902
23903 if (it->glyph_row)
23904 {
23905 struct glyph *glyph;
23906 enum glyph_row_area area = it->area;
23907
23908 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23909 if (glyph < it->glyph_row->glyphs[area + 1])
23910 {
23911 glyph->charpos = CHARPOS (it->position);
23912 glyph->object = it->object;
23913 glyph->pixel_width = it->pixel_width;
23914 glyph->ascent = glyph_ascent;
23915 glyph->descent = it->descent;
23916 glyph->voffset = it->voffset;
23917 glyph->type = IMAGE_GLYPH;
23918 glyph->avoid_cursor_p = it->avoid_cursor_p;
23919 glyph->multibyte_p = it->multibyte_p;
23920 glyph->left_box_line_p = it->start_of_box_run_p;
23921 glyph->right_box_line_p = it->end_of_box_run_p;
23922 glyph->overlaps_vertically_p = 0;
23923 glyph->padding_p = 0;
23924 glyph->glyph_not_available_p = 0;
23925 glyph->face_id = it->face_id;
23926 glyph->u.img_id = img->id;
23927 glyph->slice.img = slice;
23928 glyph->font_type = FONT_TYPE_UNKNOWN;
23929 if (it->bidi_p)
23930 {
23931 glyph->resolved_level = it->bidi_it.resolved_level;
23932 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23933 abort ();
23934 glyph->bidi_type = it->bidi_it.type;
23935 }
23936 ++it->glyph_row->used[area];
23937 }
23938 else
23939 IT_EXPAND_MATRIX_WIDTH (it, area);
23940 }
23941 }
23942
23943
23944 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23945 of the glyph, WIDTH and HEIGHT are the width and height of the
23946 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23947
23948 static void
23949 append_stretch_glyph (struct it *it, Lisp_Object object,
23950 int width, int height, int ascent)
23951 {
23952 struct glyph *glyph;
23953 enum glyph_row_area area = it->area;
23954
23955 eassert (ascent >= 0 && ascent <= height);
23956
23957 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23958 if (glyph < it->glyph_row->glyphs[area + 1])
23959 {
23960 /* If the glyph row is reversed, we need to prepend the glyph
23961 rather than append it. */
23962 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23963 {
23964 struct glyph *g;
23965
23966 /* Make room for the additional glyph. */
23967 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23968 g[1] = *g;
23969 glyph = it->glyph_row->glyphs[area];
23970 }
23971 glyph->charpos = CHARPOS (it->position);
23972 glyph->object = object;
23973 glyph->pixel_width = width;
23974 glyph->ascent = ascent;
23975 glyph->descent = height - ascent;
23976 glyph->voffset = it->voffset;
23977 glyph->type = STRETCH_GLYPH;
23978 glyph->avoid_cursor_p = it->avoid_cursor_p;
23979 glyph->multibyte_p = it->multibyte_p;
23980 glyph->left_box_line_p = it->start_of_box_run_p;
23981 glyph->right_box_line_p = it->end_of_box_run_p;
23982 glyph->overlaps_vertically_p = 0;
23983 glyph->padding_p = 0;
23984 glyph->glyph_not_available_p = 0;
23985 glyph->face_id = it->face_id;
23986 glyph->u.stretch.ascent = ascent;
23987 glyph->u.stretch.height = height;
23988 glyph->slice.img = null_glyph_slice;
23989 glyph->font_type = FONT_TYPE_UNKNOWN;
23990 if (it->bidi_p)
23991 {
23992 glyph->resolved_level = it->bidi_it.resolved_level;
23993 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23994 abort ();
23995 glyph->bidi_type = it->bidi_it.type;
23996 }
23997 else
23998 {
23999 glyph->resolved_level = 0;
24000 glyph->bidi_type = UNKNOWN_BT;
24001 }
24002 ++it->glyph_row->used[area];
24003 }
24004 else
24005 IT_EXPAND_MATRIX_WIDTH (it, area);
24006 }
24007
24008 #endif /* HAVE_WINDOW_SYSTEM */
24009
24010 /* Produce a stretch glyph for iterator IT. IT->object is the value
24011 of the glyph property displayed. The value must be a list
24012 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24013 being recognized:
24014
24015 1. `:width WIDTH' specifies that the space should be WIDTH *
24016 canonical char width wide. WIDTH may be an integer or floating
24017 point number.
24018
24019 2. `:relative-width FACTOR' specifies that the width of the stretch
24020 should be computed from the width of the first character having the
24021 `glyph' property, and should be FACTOR times that width.
24022
24023 3. `:align-to HPOS' specifies that the space should be wide enough
24024 to reach HPOS, a value in canonical character units.
24025
24026 Exactly one of the above pairs must be present.
24027
24028 4. `:height HEIGHT' specifies that the height of the stretch produced
24029 should be HEIGHT, measured in canonical character units.
24030
24031 5. `:relative-height FACTOR' specifies that the height of the
24032 stretch should be FACTOR times the height of the characters having
24033 the glyph property.
24034
24035 Either none or exactly one of 4 or 5 must be present.
24036
24037 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24038 of the stretch should be used for the ascent of the stretch.
24039 ASCENT must be in the range 0 <= ASCENT <= 100. */
24040
24041 void
24042 produce_stretch_glyph (struct it *it)
24043 {
24044 /* (space :width WIDTH :height HEIGHT ...) */
24045 Lisp_Object prop, plist;
24046 int width = 0, height = 0, align_to = -1;
24047 int zero_width_ok_p = 0;
24048 int ascent = 0;
24049 double tem;
24050 struct face *face = NULL;
24051 struct font *font = NULL;
24052
24053 #ifdef HAVE_WINDOW_SYSTEM
24054 int zero_height_ok_p = 0;
24055
24056 if (FRAME_WINDOW_P (it->f))
24057 {
24058 face = FACE_FROM_ID (it->f, it->face_id);
24059 font = face->font ? face->font : FRAME_FONT (it->f);
24060 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24061 }
24062 #endif
24063
24064 /* List should start with `space'. */
24065 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24066 plist = XCDR (it->object);
24067
24068 /* Compute the width of the stretch. */
24069 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24070 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24071 {
24072 /* Absolute width `:width WIDTH' specified and valid. */
24073 zero_width_ok_p = 1;
24074 width = (int)tem;
24075 }
24076 #ifdef HAVE_WINDOW_SYSTEM
24077 else if (FRAME_WINDOW_P (it->f)
24078 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24079 {
24080 /* Relative width `:relative-width FACTOR' specified and valid.
24081 Compute the width of the characters having the `glyph'
24082 property. */
24083 struct it it2;
24084 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24085
24086 it2 = *it;
24087 if (it->multibyte_p)
24088 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24089 else
24090 {
24091 it2.c = it2.char_to_display = *p, it2.len = 1;
24092 if (! ASCII_CHAR_P (it2.c))
24093 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24094 }
24095
24096 it2.glyph_row = NULL;
24097 it2.what = IT_CHARACTER;
24098 x_produce_glyphs (&it2);
24099 width = NUMVAL (prop) * it2.pixel_width;
24100 }
24101 #endif /* HAVE_WINDOW_SYSTEM */
24102 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24103 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24104 {
24105 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24106 align_to = (align_to < 0
24107 ? 0
24108 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24109 else if (align_to < 0)
24110 align_to = window_box_left_offset (it->w, TEXT_AREA);
24111 width = max (0, (int)tem + align_to - it->current_x);
24112 zero_width_ok_p = 1;
24113 }
24114 else
24115 /* Nothing specified -> width defaults to canonical char width. */
24116 width = FRAME_COLUMN_WIDTH (it->f);
24117
24118 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24119 width = 1;
24120
24121 #ifdef HAVE_WINDOW_SYSTEM
24122 /* Compute height. */
24123 if (FRAME_WINDOW_P (it->f))
24124 {
24125 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24126 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24127 {
24128 height = (int)tem;
24129 zero_height_ok_p = 1;
24130 }
24131 else if (prop = Fplist_get (plist, QCrelative_height),
24132 NUMVAL (prop) > 0)
24133 height = FONT_HEIGHT (font) * NUMVAL (prop);
24134 else
24135 height = FONT_HEIGHT (font);
24136
24137 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24138 height = 1;
24139
24140 /* Compute percentage of height used for ascent. If
24141 `:ascent ASCENT' is present and valid, use that. Otherwise,
24142 derive the ascent from the font in use. */
24143 if (prop = Fplist_get (plist, QCascent),
24144 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24145 ascent = height * NUMVAL (prop) / 100.0;
24146 else if (!NILP (prop)
24147 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24148 ascent = min (max (0, (int)tem), height);
24149 else
24150 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24151 }
24152 else
24153 #endif /* HAVE_WINDOW_SYSTEM */
24154 height = 1;
24155
24156 if (width > 0 && it->line_wrap != TRUNCATE
24157 && it->current_x + width > it->last_visible_x)
24158 {
24159 width = it->last_visible_x - it->current_x;
24160 #ifdef HAVE_WINDOW_SYSTEM
24161 /* Subtract one more pixel from the stretch width, but only on
24162 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24163 width -= FRAME_WINDOW_P (it->f);
24164 #endif
24165 }
24166
24167 if (width > 0 && height > 0 && it->glyph_row)
24168 {
24169 Lisp_Object o_object = it->object;
24170 Lisp_Object object = it->stack[it->sp - 1].string;
24171 int n = width;
24172
24173 if (!STRINGP (object))
24174 object = it->w->buffer;
24175 #ifdef HAVE_WINDOW_SYSTEM
24176 if (FRAME_WINDOW_P (it->f))
24177 append_stretch_glyph (it, object, width, height, ascent);
24178 else
24179 #endif
24180 {
24181 it->object = object;
24182 it->char_to_display = ' ';
24183 it->pixel_width = it->len = 1;
24184 while (n--)
24185 tty_append_glyph (it);
24186 it->object = o_object;
24187 }
24188 }
24189
24190 it->pixel_width = width;
24191 #ifdef HAVE_WINDOW_SYSTEM
24192 if (FRAME_WINDOW_P (it->f))
24193 {
24194 it->ascent = it->phys_ascent = ascent;
24195 it->descent = it->phys_descent = height - it->ascent;
24196 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24197 take_vertical_position_into_account (it);
24198 }
24199 else
24200 #endif
24201 it->nglyphs = width;
24202 }
24203
24204 /* Get information about special display element WHAT in an
24205 environment described by IT. WHAT is one of IT_TRUNCATION or
24206 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24207 non-null glyph_row member. This function ensures that fields like
24208 face_id, c, len of IT are left untouched. */
24209
24210 void
24211 produce_special_glyphs (struct it *it, enum display_element_type what)
24212 {
24213 struct it temp_it;
24214 Lisp_Object gc;
24215 GLYPH glyph;
24216
24217 temp_it = *it;
24218 temp_it.dp = NULL;
24219 temp_it.what = IT_CHARACTER;
24220 temp_it.len = 1;
24221 temp_it.object = make_number (0);
24222 memset (&temp_it.current, 0, sizeof temp_it.current);
24223
24224 if (what == IT_CONTINUATION)
24225 {
24226 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24227 if (it->bidi_it.paragraph_dir == R2L)
24228 SET_GLYPH_FROM_CHAR (glyph, '/');
24229 else
24230 SET_GLYPH_FROM_CHAR (glyph, '\\');
24231 if (it->dp
24232 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24233 {
24234 /* FIXME: Should we mirror GC for R2L lines? */
24235 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24236 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24237 }
24238 }
24239 else if (what == IT_TRUNCATION)
24240 {
24241 /* Truncation glyph. */
24242 SET_GLYPH_FROM_CHAR (glyph, '$');
24243 if (it->dp
24244 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24245 {
24246 /* FIXME: Should we mirror GC for R2L lines? */
24247 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24248 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24249 }
24250 }
24251 else
24252 abort ();
24253
24254 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24255 temp_it.face_id = GLYPH_FACE (glyph);
24256 temp_it.len = CHAR_BYTES (temp_it.c);
24257
24258 PRODUCE_GLYPHS (&temp_it);
24259 it->pixel_width = temp_it.pixel_width;
24260 it->nglyphs = temp_it.pixel_width;
24261 }
24262
24263 #ifdef HAVE_WINDOW_SYSTEM
24264
24265 /* Calculate line-height and line-spacing properties.
24266 An integer value specifies explicit pixel value.
24267 A float value specifies relative value to current face height.
24268 A cons (float . face-name) specifies relative value to
24269 height of specified face font.
24270
24271 Returns height in pixels, or nil. */
24272
24273
24274 static Lisp_Object
24275 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24276 int boff, int override)
24277 {
24278 Lisp_Object face_name = Qnil;
24279 int ascent, descent, height;
24280
24281 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24282 return val;
24283
24284 if (CONSP (val))
24285 {
24286 face_name = XCAR (val);
24287 val = XCDR (val);
24288 if (!NUMBERP (val))
24289 val = make_number (1);
24290 if (NILP (face_name))
24291 {
24292 height = it->ascent + it->descent;
24293 goto scale;
24294 }
24295 }
24296
24297 if (NILP (face_name))
24298 {
24299 font = FRAME_FONT (it->f);
24300 boff = FRAME_BASELINE_OFFSET (it->f);
24301 }
24302 else if (EQ (face_name, Qt))
24303 {
24304 override = 0;
24305 }
24306 else
24307 {
24308 int face_id;
24309 struct face *face;
24310
24311 face_id = lookup_named_face (it->f, face_name, 0);
24312 if (face_id < 0)
24313 return make_number (-1);
24314
24315 face = FACE_FROM_ID (it->f, face_id);
24316 font = face->font;
24317 if (font == NULL)
24318 return make_number (-1);
24319 boff = font->baseline_offset;
24320 if (font->vertical_centering)
24321 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24322 }
24323
24324 ascent = FONT_BASE (font) + boff;
24325 descent = FONT_DESCENT (font) - boff;
24326
24327 if (override)
24328 {
24329 it->override_ascent = ascent;
24330 it->override_descent = descent;
24331 it->override_boff = boff;
24332 }
24333
24334 height = ascent + descent;
24335
24336 scale:
24337 if (FLOATP (val))
24338 height = (int)(XFLOAT_DATA (val) * height);
24339 else if (INTEGERP (val))
24340 height *= XINT (val);
24341
24342 return make_number (height);
24343 }
24344
24345
24346 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24347 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24348 and only if this is for a character for which no font was found.
24349
24350 If the display method (it->glyphless_method) is
24351 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24352 length of the acronym or the hexadecimal string, UPPER_XOFF and
24353 UPPER_YOFF are pixel offsets for the upper part of the string,
24354 LOWER_XOFF and LOWER_YOFF are for the lower part.
24355
24356 For the other display methods, LEN through LOWER_YOFF are zero. */
24357
24358 static void
24359 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24360 short upper_xoff, short upper_yoff,
24361 short lower_xoff, short lower_yoff)
24362 {
24363 struct glyph *glyph;
24364 enum glyph_row_area area = it->area;
24365
24366 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24367 if (glyph < it->glyph_row->glyphs[area + 1])
24368 {
24369 /* If the glyph row is reversed, we need to prepend the glyph
24370 rather than append it. */
24371 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24372 {
24373 struct glyph *g;
24374
24375 /* Make room for the additional glyph. */
24376 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24377 g[1] = *g;
24378 glyph = it->glyph_row->glyphs[area];
24379 }
24380 glyph->charpos = CHARPOS (it->position);
24381 glyph->object = it->object;
24382 glyph->pixel_width = it->pixel_width;
24383 glyph->ascent = it->ascent;
24384 glyph->descent = it->descent;
24385 glyph->voffset = it->voffset;
24386 glyph->type = GLYPHLESS_GLYPH;
24387 glyph->u.glyphless.method = it->glyphless_method;
24388 glyph->u.glyphless.for_no_font = for_no_font;
24389 glyph->u.glyphless.len = len;
24390 glyph->u.glyphless.ch = it->c;
24391 glyph->slice.glyphless.upper_xoff = upper_xoff;
24392 glyph->slice.glyphless.upper_yoff = upper_yoff;
24393 glyph->slice.glyphless.lower_xoff = lower_xoff;
24394 glyph->slice.glyphless.lower_yoff = lower_yoff;
24395 glyph->avoid_cursor_p = it->avoid_cursor_p;
24396 glyph->multibyte_p = it->multibyte_p;
24397 glyph->left_box_line_p = it->start_of_box_run_p;
24398 glyph->right_box_line_p = it->end_of_box_run_p;
24399 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24400 || it->phys_descent > it->descent);
24401 glyph->padding_p = 0;
24402 glyph->glyph_not_available_p = 0;
24403 glyph->face_id = face_id;
24404 glyph->font_type = FONT_TYPE_UNKNOWN;
24405 if (it->bidi_p)
24406 {
24407 glyph->resolved_level = it->bidi_it.resolved_level;
24408 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24409 abort ();
24410 glyph->bidi_type = it->bidi_it.type;
24411 }
24412 ++it->glyph_row->used[area];
24413 }
24414 else
24415 IT_EXPAND_MATRIX_WIDTH (it, area);
24416 }
24417
24418
24419 /* Produce a glyph for a glyphless character for iterator IT.
24420 IT->glyphless_method specifies which method to use for displaying
24421 the character. See the description of enum
24422 glyphless_display_method in dispextern.h for the detail.
24423
24424 FOR_NO_FONT is nonzero if and only if this is for a character for
24425 which no font was found. ACRONYM, if non-nil, is an acronym string
24426 for the character. */
24427
24428 static void
24429 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24430 {
24431 int face_id;
24432 struct face *face;
24433 struct font *font;
24434 int base_width, base_height, width, height;
24435 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24436 int len;
24437
24438 /* Get the metrics of the base font. We always refer to the current
24439 ASCII face. */
24440 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24441 font = face->font ? face->font : FRAME_FONT (it->f);
24442 it->ascent = FONT_BASE (font) + font->baseline_offset;
24443 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24444 base_height = it->ascent + it->descent;
24445 base_width = font->average_width;
24446
24447 /* Get a face ID for the glyph by utilizing a cache (the same way as
24448 done for `escape-glyph' in get_next_display_element). */
24449 if (it->f == last_glyphless_glyph_frame
24450 && it->face_id == last_glyphless_glyph_face_id)
24451 {
24452 face_id = last_glyphless_glyph_merged_face_id;
24453 }
24454 else
24455 {
24456 /* Merge the `glyphless-char' face into the current face. */
24457 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24458 last_glyphless_glyph_frame = it->f;
24459 last_glyphless_glyph_face_id = it->face_id;
24460 last_glyphless_glyph_merged_face_id = face_id;
24461 }
24462
24463 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24464 {
24465 it->pixel_width = THIN_SPACE_WIDTH;
24466 len = 0;
24467 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24468 }
24469 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24470 {
24471 width = CHAR_WIDTH (it->c);
24472 if (width == 0)
24473 width = 1;
24474 else if (width > 4)
24475 width = 4;
24476 it->pixel_width = base_width * width;
24477 len = 0;
24478 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24479 }
24480 else
24481 {
24482 char buf[7];
24483 const char *str;
24484 unsigned int code[6];
24485 int upper_len;
24486 int ascent, descent;
24487 struct font_metrics metrics_upper, metrics_lower;
24488
24489 face = FACE_FROM_ID (it->f, face_id);
24490 font = face->font ? face->font : FRAME_FONT (it->f);
24491 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24492
24493 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24494 {
24495 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24496 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24497 if (CONSP (acronym))
24498 acronym = XCAR (acronym);
24499 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24500 }
24501 else
24502 {
24503 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24504 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24505 str = buf;
24506 }
24507 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24508 code[len] = font->driver->encode_char (font, str[len]);
24509 upper_len = (len + 1) / 2;
24510 font->driver->text_extents (font, code, upper_len,
24511 &metrics_upper);
24512 font->driver->text_extents (font, code + upper_len, len - upper_len,
24513 &metrics_lower);
24514
24515
24516
24517 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24518 width = max (metrics_upper.width, metrics_lower.width) + 4;
24519 upper_xoff = upper_yoff = 2; /* the typical case */
24520 if (base_width >= width)
24521 {
24522 /* Align the upper to the left, the lower to the right. */
24523 it->pixel_width = base_width;
24524 lower_xoff = base_width - 2 - metrics_lower.width;
24525 }
24526 else
24527 {
24528 /* Center the shorter one. */
24529 it->pixel_width = width;
24530 if (metrics_upper.width >= metrics_lower.width)
24531 lower_xoff = (width - metrics_lower.width) / 2;
24532 else
24533 {
24534 /* FIXME: This code doesn't look right. It formerly was
24535 missing the "lower_xoff = 0;", which couldn't have
24536 been right since it left lower_xoff uninitialized. */
24537 lower_xoff = 0;
24538 upper_xoff = (width - metrics_upper.width) / 2;
24539 }
24540 }
24541
24542 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24543 top, bottom, and between upper and lower strings. */
24544 height = (metrics_upper.ascent + metrics_upper.descent
24545 + metrics_lower.ascent + metrics_lower.descent) + 5;
24546 /* Center vertically.
24547 H:base_height, D:base_descent
24548 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24549
24550 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24551 descent = D - H/2 + h/2;
24552 lower_yoff = descent - 2 - ld;
24553 upper_yoff = lower_yoff - la - 1 - ud; */
24554 ascent = - (it->descent - (base_height + height + 1) / 2);
24555 descent = it->descent - (base_height - height) / 2;
24556 lower_yoff = descent - 2 - metrics_lower.descent;
24557 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24558 - metrics_upper.descent);
24559 /* Don't make the height shorter than the base height. */
24560 if (height > base_height)
24561 {
24562 it->ascent = ascent;
24563 it->descent = descent;
24564 }
24565 }
24566
24567 it->phys_ascent = it->ascent;
24568 it->phys_descent = it->descent;
24569 if (it->glyph_row)
24570 append_glyphless_glyph (it, face_id, for_no_font, len,
24571 upper_xoff, upper_yoff,
24572 lower_xoff, lower_yoff);
24573 it->nglyphs = 1;
24574 take_vertical_position_into_account (it);
24575 }
24576
24577
24578 /* RIF:
24579 Produce glyphs/get display metrics for the display element IT is
24580 loaded with. See the description of struct it in dispextern.h
24581 for an overview of struct it. */
24582
24583 void
24584 x_produce_glyphs (struct it *it)
24585 {
24586 int extra_line_spacing = it->extra_line_spacing;
24587
24588 it->glyph_not_available_p = 0;
24589
24590 if (it->what == IT_CHARACTER)
24591 {
24592 XChar2b char2b;
24593 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24594 struct font *font = face->font;
24595 struct font_metrics *pcm = NULL;
24596 int boff; /* baseline offset */
24597
24598 if (font == NULL)
24599 {
24600 /* When no suitable font is found, display this character by
24601 the method specified in the first extra slot of
24602 Vglyphless_char_display. */
24603 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24604
24605 eassert (it->what == IT_GLYPHLESS);
24606 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24607 goto done;
24608 }
24609
24610 boff = font->baseline_offset;
24611 if (font->vertical_centering)
24612 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24613
24614 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24615 {
24616 int stretched_p;
24617
24618 it->nglyphs = 1;
24619
24620 if (it->override_ascent >= 0)
24621 {
24622 it->ascent = it->override_ascent;
24623 it->descent = it->override_descent;
24624 boff = it->override_boff;
24625 }
24626 else
24627 {
24628 it->ascent = FONT_BASE (font) + boff;
24629 it->descent = FONT_DESCENT (font) - boff;
24630 }
24631
24632 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24633 {
24634 pcm = get_per_char_metric (font, &char2b);
24635 if (pcm->width == 0
24636 && pcm->rbearing == 0 && pcm->lbearing == 0)
24637 pcm = NULL;
24638 }
24639
24640 if (pcm)
24641 {
24642 it->phys_ascent = pcm->ascent + boff;
24643 it->phys_descent = pcm->descent - boff;
24644 it->pixel_width = pcm->width;
24645 }
24646 else
24647 {
24648 it->glyph_not_available_p = 1;
24649 it->phys_ascent = it->ascent;
24650 it->phys_descent = it->descent;
24651 it->pixel_width = font->space_width;
24652 }
24653
24654 if (it->constrain_row_ascent_descent_p)
24655 {
24656 if (it->descent > it->max_descent)
24657 {
24658 it->ascent += it->descent - it->max_descent;
24659 it->descent = it->max_descent;
24660 }
24661 if (it->ascent > it->max_ascent)
24662 {
24663 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24664 it->ascent = it->max_ascent;
24665 }
24666 it->phys_ascent = min (it->phys_ascent, it->ascent);
24667 it->phys_descent = min (it->phys_descent, it->descent);
24668 extra_line_spacing = 0;
24669 }
24670
24671 /* If this is a space inside a region of text with
24672 `space-width' property, change its width. */
24673 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24674 if (stretched_p)
24675 it->pixel_width *= XFLOATINT (it->space_width);
24676
24677 /* If face has a box, add the box thickness to the character
24678 height. If character has a box line to the left and/or
24679 right, add the box line width to the character's width. */
24680 if (face->box != FACE_NO_BOX)
24681 {
24682 int thick = face->box_line_width;
24683
24684 if (thick > 0)
24685 {
24686 it->ascent += thick;
24687 it->descent += thick;
24688 }
24689 else
24690 thick = -thick;
24691
24692 if (it->start_of_box_run_p)
24693 it->pixel_width += thick;
24694 if (it->end_of_box_run_p)
24695 it->pixel_width += thick;
24696 }
24697
24698 /* If face has an overline, add the height of the overline
24699 (1 pixel) and a 1 pixel margin to the character height. */
24700 if (face->overline_p)
24701 it->ascent += overline_margin;
24702
24703 if (it->constrain_row_ascent_descent_p)
24704 {
24705 if (it->ascent > it->max_ascent)
24706 it->ascent = it->max_ascent;
24707 if (it->descent > it->max_descent)
24708 it->descent = it->max_descent;
24709 }
24710
24711 take_vertical_position_into_account (it);
24712
24713 /* If we have to actually produce glyphs, do it. */
24714 if (it->glyph_row)
24715 {
24716 if (stretched_p)
24717 {
24718 /* Translate a space with a `space-width' property
24719 into a stretch glyph. */
24720 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24721 / FONT_HEIGHT (font));
24722 append_stretch_glyph (it, it->object, it->pixel_width,
24723 it->ascent + it->descent, ascent);
24724 }
24725 else
24726 append_glyph (it);
24727
24728 /* If characters with lbearing or rbearing are displayed
24729 in this line, record that fact in a flag of the
24730 glyph row. This is used to optimize X output code. */
24731 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24732 it->glyph_row->contains_overlapping_glyphs_p = 1;
24733 }
24734 if (! stretched_p && it->pixel_width == 0)
24735 /* We assure that all visible glyphs have at least 1-pixel
24736 width. */
24737 it->pixel_width = 1;
24738 }
24739 else if (it->char_to_display == '\n')
24740 {
24741 /* A newline has no width, but we need the height of the
24742 line. But if previous part of the line sets a height,
24743 don't increase that height */
24744
24745 Lisp_Object height;
24746 Lisp_Object total_height = Qnil;
24747
24748 it->override_ascent = -1;
24749 it->pixel_width = 0;
24750 it->nglyphs = 0;
24751
24752 height = get_it_property (it, Qline_height);
24753 /* Split (line-height total-height) list */
24754 if (CONSP (height)
24755 && CONSP (XCDR (height))
24756 && NILP (XCDR (XCDR (height))))
24757 {
24758 total_height = XCAR (XCDR (height));
24759 height = XCAR (height);
24760 }
24761 height = calc_line_height_property (it, height, font, boff, 1);
24762
24763 if (it->override_ascent >= 0)
24764 {
24765 it->ascent = it->override_ascent;
24766 it->descent = it->override_descent;
24767 boff = it->override_boff;
24768 }
24769 else
24770 {
24771 it->ascent = FONT_BASE (font) + boff;
24772 it->descent = FONT_DESCENT (font) - boff;
24773 }
24774
24775 if (EQ (height, Qt))
24776 {
24777 if (it->descent > it->max_descent)
24778 {
24779 it->ascent += it->descent - it->max_descent;
24780 it->descent = it->max_descent;
24781 }
24782 if (it->ascent > it->max_ascent)
24783 {
24784 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24785 it->ascent = it->max_ascent;
24786 }
24787 it->phys_ascent = min (it->phys_ascent, it->ascent);
24788 it->phys_descent = min (it->phys_descent, it->descent);
24789 it->constrain_row_ascent_descent_p = 1;
24790 extra_line_spacing = 0;
24791 }
24792 else
24793 {
24794 Lisp_Object spacing;
24795
24796 it->phys_ascent = it->ascent;
24797 it->phys_descent = it->descent;
24798
24799 if ((it->max_ascent > 0 || it->max_descent > 0)
24800 && face->box != FACE_NO_BOX
24801 && face->box_line_width > 0)
24802 {
24803 it->ascent += face->box_line_width;
24804 it->descent += face->box_line_width;
24805 }
24806 if (!NILP (height)
24807 && XINT (height) > it->ascent + it->descent)
24808 it->ascent = XINT (height) - it->descent;
24809
24810 if (!NILP (total_height))
24811 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24812 else
24813 {
24814 spacing = get_it_property (it, Qline_spacing);
24815 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24816 }
24817 if (INTEGERP (spacing))
24818 {
24819 extra_line_spacing = XINT (spacing);
24820 if (!NILP (total_height))
24821 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24822 }
24823 }
24824 }
24825 else /* i.e. (it->char_to_display == '\t') */
24826 {
24827 if (font->space_width > 0)
24828 {
24829 int tab_width = it->tab_width * font->space_width;
24830 int x = it->current_x + it->continuation_lines_width;
24831 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24832
24833 /* If the distance from the current position to the next tab
24834 stop is less than a space character width, use the
24835 tab stop after that. */
24836 if (next_tab_x - x < font->space_width)
24837 next_tab_x += tab_width;
24838
24839 it->pixel_width = next_tab_x - x;
24840 it->nglyphs = 1;
24841 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24842 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24843
24844 if (it->glyph_row)
24845 {
24846 append_stretch_glyph (it, it->object, it->pixel_width,
24847 it->ascent + it->descent, it->ascent);
24848 }
24849 }
24850 else
24851 {
24852 it->pixel_width = 0;
24853 it->nglyphs = 1;
24854 }
24855 }
24856 }
24857 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24858 {
24859 /* A static composition.
24860
24861 Note: A composition is represented as one glyph in the
24862 glyph matrix. There are no padding glyphs.
24863
24864 Important note: pixel_width, ascent, and descent are the
24865 values of what is drawn by draw_glyphs (i.e. the values of
24866 the overall glyphs composed). */
24867 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24868 int boff; /* baseline offset */
24869 struct composition *cmp = composition_table[it->cmp_it.id];
24870 int glyph_len = cmp->glyph_len;
24871 struct font *font = face->font;
24872
24873 it->nglyphs = 1;
24874
24875 /* If we have not yet calculated pixel size data of glyphs of
24876 the composition for the current face font, calculate them
24877 now. Theoretically, we have to check all fonts for the
24878 glyphs, but that requires much time and memory space. So,
24879 here we check only the font of the first glyph. This may
24880 lead to incorrect display, but it's very rare, and C-l
24881 (recenter-top-bottom) can correct the display anyway. */
24882 if (! cmp->font || cmp->font != font)
24883 {
24884 /* Ascent and descent of the font of the first character
24885 of this composition (adjusted by baseline offset).
24886 Ascent and descent of overall glyphs should not be less
24887 than these, respectively. */
24888 int font_ascent, font_descent, font_height;
24889 /* Bounding box of the overall glyphs. */
24890 int leftmost, rightmost, lowest, highest;
24891 int lbearing, rbearing;
24892 int i, width, ascent, descent;
24893 int left_padded = 0, right_padded = 0;
24894 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24895 XChar2b char2b;
24896 struct font_metrics *pcm;
24897 int font_not_found_p;
24898 ptrdiff_t pos;
24899
24900 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24901 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24902 break;
24903 if (glyph_len < cmp->glyph_len)
24904 right_padded = 1;
24905 for (i = 0; i < glyph_len; i++)
24906 {
24907 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24908 break;
24909 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24910 }
24911 if (i > 0)
24912 left_padded = 1;
24913
24914 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24915 : IT_CHARPOS (*it));
24916 /* If no suitable font is found, use the default font. */
24917 font_not_found_p = font == NULL;
24918 if (font_not_found_p)
24919 {
24920 face = face->ascii_face;
24921 font = face->font;
24922 }
24923 boff = font->baseline_offset;
24924 if (font->vertical_centering)
24925 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24926 font_ascent = FONT_BASE (font) + boff;
24927 font_descent = FONT_DESCENT (font) - boff;
24928 font_height = FONT_HEIGHT (font);
24929
24930 cmp->font = (void *) font;
24931
24932 pcm = NULL;
24933 if (! font_not_found_p)
24934 {
24935 get_char_face_and_encoding (it->f, c, it->face_id,
24936 &char2b, 0);
24937 pcm = get_per_char_metric (font, &char2b);
24938 }
24939
24940 /* Initialize the bounding box. */
24941 if (pcm)
24942 {
24943 width = cmp->glyph_len > 0 ? pcm->width : 0;
24944 ascent = pcm->ascent;
24945 descent = pcm->descent;
24946 lbearing = pcm->lbearing;
24947 rbearing = pcm->rbearing;
24948 }
24949 else
24950 {
24951 width = cmp->glyph_len > 0 ? font->space_width : 0;
24952 ascent = FONT_BASE (font);
24953 descent = FONT_DESCENT (font);
24954 lbearing = 0;
24955 rbearing = width;
24956 }
24957
24958 rightmost = width;
24959 leftmost = 0;
24960 lowest = - descent + boff;
24961 highest = ascent + boff;
24962
24963 if (! font_not_found_p
24964 && font->default_ascent
24965 && CHAR_TABLE_P (Vuse_default_ascent)
24966 && !NILP (Faref (Vuse_default_ascent,
24967 make_number (it->char_to_display))))
24968 highest = font->default_ascent + boff;
24969
24970 /* Draw the first glyph at the normal position. It may be
24971 shifted to right later if some other glyphs are drawn
24972 at the left. */
24973 cmp->offsets[i * 2] = 0;
24974 cmp->offsets[i * 2 + 1] = boff;
24975 cmp->lbearing = lbearing;
24976 cmp->rbearing = rbearing;
24977
24978 /* Set cmp->offsets for the remaining glyphs. */
24979 for (i++; i < glyph_len; i++)
24980 {
24981 int left, right, btm, top;
24982 int ch = COMPOSITION_GLYPH (cmp, i);
24983 int face_id;
24984 struct face *this_face;
24985
24986 if (ch == '\t')
24987 ch = ' ';
24988 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24989 this_face = FACE_FROM_ID (it->f, face_id);
24990 font = this_face->font;
24991
24992 if (font == NULL)
24993 pcm = NULL;
24994 else
24995 {
24996 get_char_face_and_encoding (it->f, ch, face_id,
24997 &char2b, 0);
24998 pcm = get_per_char_metric (font, &char2b);
24999 }
25000 if (! pcm)
25001 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25002 else
25003 {
25004 width = pcm->width;
25005 ascent = pcm->ascent;
25006 descent = pcm->descent;
25007 lbearing = pcm->lbearing;
25008 rbearing = pcm->rbearing;
25009 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25010 {
25011 /* Relative composition with or without
25012 alternate chars. */
25013 left = (leftmost + rightmost - width) / 2;
25014 btm = - descent + boff;
25015 if (font->relative_compose
25016 && (! CHAR_TABLE_P (Vignore_relative_composition)
25017 || NILP (Faref (Vignore_relative_composition,
25018 make_number (ch)))))
25019 {
25020
25021 if (- descent >= font->relative_compose)
25022 /* One extra pixel between two glyphs. */
25023 btm = highest + 1;
25024 else if (ascent <= 0)
25025 /* One extra pixel between two glyphs. */
25026 btm = lowest - 1 - ascent - descent;
25027 }
25028 }
25029 else
25030 {
25031 /* A composition rule is specified by an integer
25032 value that encodes global and new reference
25033 points (GREF and NREF). GREF and NREF are
25034 specified by numbers as below:
25035
25036 0---1---2 -- ascent
25037 | |
25038 | |
25039 | |
25040 9--10--11 -- center
25041 | |
25042 ---3---4---5--- baseline
25043 | |
25044 6---7---8 -- descent
25045 */
25046 int rule = COMPOSITION_RULE (cmp, i);
25047 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25048
25049 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25050 grefx = gref % 3, nrefx = nref % 3;
25051 grefy = gref / 3, nrefy = nref / 3;
25052 if (xoff)
25053 xoff = font_height * (xoff - 128) / 256;
25054 if (yoff)
25055 yoff = font_height * (yoff - 128) / 256;
25056
25057 left = (leftmost
25058 + grefx * (rightmost - leftmost) / 2
25059 - nrefx * width / 2
25060 + xoff);
25061
25062 btm = ((grefy == 0 ? highest
25063 : grefy == 1 ? 0
25064 : grefy == 2 ? lowest
25065 : (highest + lowest) / 2)
25066 - (nrefy == 0 ? ascent + descent
25067 : nrefy == 1 ? descent - boff
25068 : nrefy == 2 ? 0
25069 : (ascent + descent) / 2)
25070 + yoff);
25071 }
25072
25073 cmp->offsets[i * 2] = left;
25074 cmp->offsets[i * 2 + 1] = btm + descent;
25075
25076 /* Update the bounding box of the overall glyphs. */
25077 if (width > 0)
25078 {
25079 right = left + width;
25080 if (left < leftmost)
25081 leftmost = left;
25082 if (right > rightmost)
25083 rightmost = right;
25084 }
25085 top = btm + descent + ascent;
25086 if (top > highest)
25087 highest = top;
25088 if (btm < lowest)
25089 lowest = btm;
25090
25091 if (cmp->lbearing > left + lbearing)
25092 cmp->lbearing = left + lbearing;
25093 if (cmp->rbearing < left + rbearing)
25094 cmp->rbearing = left + rbearing;
25095 }
25096 }
25097
25098 /* If there are glyphs whose x-offsets are negative,
25099 shift all glyphs to the right and make all x-offsets
25100 non-negative. */
25101 if (leftmost < 0)
25102 {
25103 for (i = 0; i < cmp->glyph_len; i++)
25104 cmp->offsets[i * 2] -= leftmost;
25105 rightmost -= leftmost;
25106 cmp->lbearing -= leftmost;
25107 cmp->rbearing -= leftmost;
25108 }
25109
25110 if (left_padded && cmp->lbearing < 0)
25111 {
25112 for (i = 0; i < cmp->glyph_len; i++)
25113 cmp->offsets[i * 2] -= cmp->lbearing;
25114 rightmost -= cmp->lbearing;
25115 cmp->rbearing -= cmp->lbearing;
25116 cmp->lbearing = 0;
25117 }
25118 if (right_padded && rightmost < cmp->rbearing)
25119 {
25120 rightmost = cmp->rbearing;
25121 }
25122
25123 cmp->pixel_width = rightmost;
25124 cmp->ascent = highest;
25125 cmp->descent = - lowest;
25126 if (cmp->ascent < font_ascent)
25127 cmp->ascent = font_ascent;
25128 if (cmp->descent < font_descent)
25129 cmp->descent = font_descent;
25130 }
25131
25132 if (it->glyph_row
25133 && (cmp->lbearing < 0
25134 || cmp->rbearing > cmp->pixel_width))
25135 it->glyph_row->contains_overlapping_glyphs_p = 1;
25136
25137 it->pixel_width = cmp->pixel_width;
25138 it->ascent = it->phys_ascent = cmp->ascent;
25139 it->descent = it->phys_descent = cmp->descent;
25140 if (face->box != FACE_NO_BOX)
25141 {
25142 int thick = face->box_line_width;
25143
25144 if (thick > 0)
25145 {
25146 it->ascent += thick;
25147 it->descent += thick;
25148 }
25149 else
25150 thick = - thick;
25151
25152 if (it->start_of_box_run_p)
25153 it->pixel_width += thick;
25154 if (it->end_of_box_run_p)
25155 it->pixel_width += thick;
25156 }
25157
25158 /* If face has an overline, add the height of the overline
25159 (1 pixel) and a 1 pixel margin to the character height. */
25160 if (face->overline_p)
25161 it->ascent += overline_margin;
25162
25163 take_vertical_position_into_account (it);
25164 if (it->ascent < 0)
25165 it->ascent = 0;
25166 if (it->descent < 0)
25167 it->descent = 0;
25168
25169 if (it->glyph_row && cmp->glyph_len > 0)
25170 append_composite_glyph (it);
25171 }
25172 else if (it->what == IT_COMPOSITION)
25173 {
25174 /* A dynamic (automatic) composition. */
25175 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25176 Lisp_Object gstring;
25177 struct font_metrics metrics;
25178
25179 it->nglyphs = 1;
25180
25181 gstring = composition_gstring_from_id (it->cmp_it.id);
25182 it->pixel_width
25183 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25184 &metrics);
25185 if (it->glyph_row
25186 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25187 it->glyph_row->contains_overlapping_glyphs_p = 1;
25188 it->ascent = it->phys_ascent = metrics.ascent;
25189 it->descent = it->phys_descent = metrics.descent;
25190 if (face->box != FACE_NO_BOX)
25191 {
25192 int thick = face->box_line_width;
25193
25194 if (thick > 0)
25195 {
25196 it->ascent += thick;
25197 it->descent += thick;
25198 }
25199 else
25200 thick = - thick;
25201
25202 if (it->start_of_box_run_p)
25203 it->pixel_width += thick;
25204 if (it->end_of_box_run_p)
25205 it->pixel_width += thick;
25206 }
25207 /* If face has an overline, add the height of the overline
25208 (1 pixel) and a 1 pixel margin to the character height. */
25209 if (face->overline_p)
25210 it->ascent += overline_margin;
25211 take_vertical_position_into_account (it);
25212 if (it->ascent < 0)
25213 it->ascent = 0;
25214 if (it->descent < 0)
25215 it->descent = 0;
25216
25217 if (it->glyph_row)
25218 append_composite_glyph (it);
25219 }
25220 else if (it->what == IT_GLYPHLESS)
25221 produce_glyphless_glyph (it, 0, Qnil);
25222 else if (it->what == IT_IMAGE)
25223 produce_image_glyph (it);
25224 else if (it->what == IT_STRETCH)
25225 produce_stretch_glyph (it);
25226
25227 done:
25228 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25229 because this isn't true for images with `:ascent 100'. */
25230 eassert (it->ascent >= 0 && it->descent >= 0);
25231 if (it->area == TEXT_AREA)
25232 it->current_x += it->pixel_width;
25233
25234 if (extra_line_spacing > 0)
25235 {
25236 it->descent += extra_line_spacing;
25237 if (extra_line_spacing > it->max_extra_line_spacing)
25238 it->max_extra_line_spacing = extra_line_spacing;
25239 }
25240
25241 it->max_ascent = max (it->max_ascent, it->ascent);
25242 it->max_descent = max (it->max_descent, it->descent);
25243 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25244 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25245 }
25246
25247 /* EXPORT for RIF:
25248 Output LEN glyphs starting at START at the nominal cursor position.
25249 Advance the nominal cursor over the text. The global variable
25250 updated_window contains the window being updated, updated_row is
25251 the glyph row being updated, and updated_area is the area of that
25252 row being updated. */
25253
25254 void
25255 x_write_glyphs (struct glyph *start, int len)
25256 {
25257 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25258
25259 eassert (updated_window && updated_row);
25260 /* When the window is hscrolled, cursor hpos can legitimately be out
25261 of bounds, but we draw the cursor at the corresponding window
25262 margin in that case. */
25263 if (!updated_row->reversed_p && chpos < 0)
25264 chpos = 0;
25265 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25266 chpos = updated_row->used[TEXT_AREA] - 1;
25267
25268 BLOCK_INPUT;
25269
25270 /* Write glyphs. */
25271
25272 hpos = start - updated_row->glyphs[updated_area];
25273 x = draw_glyphs (updated_window, output_cursor.x,
25274 updated_row, updated_area,
25275 hpos, hpos + len,
25276 DRAW_NORMAL_TEXT, 0);
25277
25278 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25279 if (updated_area == TEXT_AREA
25280 && updated_window->phys_cursor_on_p
25281 && updated_window->phys_cursor.vpos == output_cursor.vpos
25282 && chpos >= hpos
25283 && chpos < hpos + len)
25284 updated_window->phys_cursor_on_p = 0;
25285
25286 UNBLOCK_INPUT;
25287
25288 /* Advance the output cursor. */
25289 output_cursor.hpos += len;
25290 output_cursor.x = x;
25291 }
25292
25293
25294 /* EXPORT for RIF:
25295 Insert LEN glyphs from START at the nominal cursor position. */
25296
25297 void
25298 x_insert_glyphs (struct glyph *start, int len)
25299 {
25300 struct frame *f;
25301 struct window *w;
25302 int line_height, shift_by_width, shifted_region_width;
25303 struct glyph_row *row;
25304 struct glyph *glyph;
25305 int frame_x, frame_y;
25306 ptrdiff_t hpos;
25307
25308 eassert (updated_window && updated_row);
25309 BLOCK_INPUT;
25310 w = updated_window;
25311 f = XFRAME (WINDOW_FRAME (w));
25312
25313 /* Get the height of the line we are in. */
25314 row = updated_row;
25315 line_height = row->height;
25316
25317 /* Get the width of the glyphs to insert. */
25318 shift_by_width = 0;
25319 for (glyph = start; glyph < start + len; ++glyph)
25320 shift_by_width += glyph->pixel_width;
25321
25322 /* Get the width of the region to shift right. */
25323 shifted_region_width = (window_box_width (w, updated_area)
25324 - output_cursor.x
25325 - shift_by_width);
25326
25327 /* Shift right. */
25328 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25329 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25330
25331 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25332 line_height, shift_by_width);
25333
25334 /* Write the glyphs. */
25335 hpos = start - row->glyphs[updated_area];
25336 draw_glyphs (w, output_cursor.x, row, updated_area,
25337 hpos, hpos + len,
25338 DRAW_NORMAL_TEXT, 0);
25339
25340 /* Advance the output cursor. */
25341 output_cursor.hpos += len;
25342 output_cursor.x += shift_by_width;
25343 UNBLOCK_INPUT;
25344 }
25345
25346
25347 /* EXPORT for RIF:
25348 Erase the current text line from the nominal cursor position
25349 (inclusive) to pixel column TO_X (exclusive). The idea is that
25350 everything from TO_X onward is already erased.
25351
25352 TO_X is a pixel position relative to updated_area of
25353 updated_window. TO_X == -1 means clear to the end of this area. */
25354
25355 void
25356 x_clear_end_of_line (int to_x)
25357 {
25358 struct frame *f;
25359 struct window *w = updated_window;
25360 int max_x, min_y, max_y;
25361 int from_x, from_y, to_y;
25362
25363 eassert (updated_window && updated_row);
25364 f = XFRAME (w->frame);
25365
25366 if (updated_row->full_width_p)
25367 max_x = WINDOW_TOTAL_WIDTH (w);
25368 else
25369 max_x = window_box_width (w, updated_area);
25370 max_y = window_text_bottom_y (w);
25371
25372 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25373 of window. For TO_X > 0, truncate to end of drawing area. */
25374 if (to_x == 0)
25375 return;
25376 else if (to_x < 0)
25377 to_x = max_x;
25378 else
25379 to_x = min (to_x, max_x);
25380
25381 to_y = min (max_y, output_cursor.y + updated_row->height);
25382
25383 /* Notice if the cursor will be cleared by this operation. */
25384 if (!updated_row->full_width_p)
25385 notice_overwritten_cursor (w, updated_area,
25386 output_cursor.x, -1,
25387 updated_row->y,
25388 MATRIX_ROW_BOTTOM_Y (updated_row));
25389
25390 from_x = output_cursor.x;
25391
25392 /* Translate to frame coordinates. */
25393 if (updated_row->full_width_p)
25394 {
25395 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25396 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25397 }
25398 else
25399 {
25400 int area_left = window_box_left (w, updated_area);
25401 from_x += area_left;
25402 to_x += area_left;
25403 }
25404
25405 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25406 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25407 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25408
25409 /* Prevent inadvertently clearing to end of the X window. */
25410 if (to_x > from_x && to_y > from_y)
25411 {
25412 BLOCK_INPUT;
25413 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25414 to_x - from_x, to_y - from_y);
25415 UNBLOCK_INPUT;
25416 }
25417 }
25418
25419 #endif /* HAVE_WINDOW_SYSTEM */
25420
25421
25422 \f
25423 /***********************************************************************
25424 Cursor types
25425 ***********************************************************************/
25426
25427 /* Value is the internal representation of the specified cursor type
25428 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25429 of the bar cursor. */
25430
25431 static enum text_cursor_kinds
25432 get_specified_cursor_type (Lisp_Object arg, int *width)
25433 {
25434 enum text_cursor_kinds type;
25435
25436 if (NILP (arg))
25437 return NO_CURSOR;
25438
25439 if (EQ (arg, Qbox))
25440 return FILLED_BOX_CURSOR;
25441
25442 if (EQ (arg, Qhollow))
25443 return HOLLOW_BOX_CURSOR;
25444
25445 if (EQ (arg, Qbar))
25446 {
25447 *width = 2;
25448 return BAR_CURSOR;
25449 }
25450
25451 if (CONSP (arg)
25452 && EQ (XCAR (arg), Qbar)
25453 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25454 {
25455 *width = XINT (XCDR (arg));
25456 return BAR_CURSOR;
25457 }
25458
25459 if (EQ (arg, Qhbar))
25460 {
25461 *width = 2;
25462 return HBAR_CURSOR;
25463 }
25464
25465 if (CONSP (arg)
25466 && EQ (XCAR (arg), Qhbar)
25467 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25468 {
25469 *width = XINT (XCDR (arg));
25470 return HBAR_CURSOR;
25471 }
25472
25473 /* Treat anything unknown as "hollow box cursor".
25474 It was bad to signal an error; people have trouble fixing
25475 .Xdefaults with Emacs, when it has something bad in it. */
25476 type = HOLLOW_BOX_CURSOR;
25477
25478 return type;
25479 }
25480
25481 /* Set the default cursor types for specified frame. */
25482 void
25483 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25484 {
25485 int width = 1;
25486 Lisp_Object tem;
25487
25488 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25489 FRAME_CURSOR_WIDTH (f) = width;
25490
25491 /* By default, set up the blink-off state depending on the on-state. */
25492
25493 tem = Fassoc (arg, Vblink_cursor_alist);
25494 if (!NILP (tem))
25495 {
25496 FRAME_BLINK_OFF_CURSOR (f)
25497 = get_specified_cursor_type (XCDR (tem), &width);
25498 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25499 }
25500 else
25501 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25502 }
25503
25504
25505 #ifdef HAVE_WINDOW_SYSTEM
25506
25507 /* Return the cursor we want to be displayed in window W. Return
25508 width of bar/hbar cursor through WIDTH arg. Return with
25509 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25510 (i.e. if the `system caret' should track this cursor).
25511
25512 In a mini-buffer window, we want the cursor only to appear if we
25513 are reading input from this window. For the selected window, we
25514 want the cursor type given by the frame parameter or buffer local
25515 setting of cursor-type. If explicitly marked off, draw no cursor.
25516 In all other cases, we want a hollow box cursor. */
25517
25518 static enum text_cursor_kinds
25519 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25520 int *active_cursor)
25521 {
25522 struct frame *f = XFRAME (w->frame);
25523 struct buffer *b = XBUFFER (w->buffer);
25524 int cursor_type = DEFAULT_CURSOR;
25525 Lisp_Object alt_cursor;
25526 int non_selected = 0;
25527
25528 *active_cursor = 1;
25529
25530 /* Echo area */
25531 if (cursor_in_echo_area
25532 && FRAME_HAS_MINIBUF_P (f)
25533 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25534 {
25535 if (w == XWINDOW (echo_area_window))
25536 {
25537 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25538 {
25539 *width = FRAME_CURSOR_WIDTH (f);
25540 return FRAME_DESIRED_CURSOR (f);
25541 }
25542 else
25543 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25544 }
25545
25546 *active_cursor = 0;
25547 non_selected = 1;
25548 }
25549
25550 /* Detect a nonselected window or nonselected frame. */
25551 else if (w != XWINDOW (f->selected_window)
25552 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25553 {
25554 *active_cursor = 0;
25555
25556 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25557 return NO_CURSOR;
25558
25559 non_selected = 1;
25560 }
25561
25562 /* Never display a cursor in a window in which cursor-type is nil. */
25563 if (NILP (BVAR (b, cursor_type)))
25564 return NO_CURSOR;
25565
25566 /* Get the normal cursor type for this window. */
25567 if (EQ (BVAR (b, cursor_type), Qt))
25568 {
25569 cursor_type = FRAME_DESIRED_CURSOR (f);
25570 *width = FRAME_CURSOR_WIDTH (f);
25571 }
25572 else
25573 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25574
25575 /* Use cursor-in-non-selected-windows instead
25576 for non-selected window or frame. */
25577 if (non_selected)
25578 {
25579 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25580 if (!EQ (Qt, alt_cursor))
25581 return get_specified_cursor_type (alt_cursor, width);
25582 /* t means modify the normal cursor type. */
25583 if (cursor_type == FILLED_BOX_CURSOR)
25584 cursor_type = HOLLOW_BOX_CURSOR;
25585 else if (cursor_type == BAR_CURSOR && *width > 1)
25586 --*width;
25587 return cursor_type;
25588 }
25589
25590 /* Use normal cursor if not blinked off. */
25591 if (!w->cursor_off_p)
25592 {
25593 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25594 {
25595 if (cursor_type == FILLED_BOX_CURSOR)
25596 {
25597 /* Using a block cursor on large images can be very annoying.
25598 So use a hollow cursor for "large" images.
25599 If image is not transparent (no mask), also use hollow cursor. */
25600 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25601 if (img != NULL && IMAGEP (img->spec))
25602 {
25603 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25604 where N = size of default frame font size.
25605 This should cover most of the "tiny" icons people may use. */
25606 if (!img->mask
25607 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25608 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25609 cursor_type = HOLLOW_BOX_CURSOR;
25610 }
25611 }
25612 else if (cursor_type != NO_CURSOR)
25613 {
25614 /* Display current only supports BOX and HOLLOW cursors for images.
25615 So for now, unconditionally use a HOLLOW cursor when cursor is
25616 not a solid box cursor. */
25617 cursor_type = HOLLOW_BOX_CURSOR;
25618 }
25619 }
25620 return cursor_type;
25621 }
25622
25623 /* Cursor is blinked off, so determine how to "toggle" it. */
25624
25625 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25626 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25627 return get_specified_cursor_type (XCDR (alt_cursor), width);
25628
25629 /* Then see if frame has specified a specific blink off cursor type. */
25630 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25631 {
25632 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25633 return FRAME_BLINK_OFF_CURSOR (f);
25634 }
25635
25636 #if 0
25637 /* Some people liked having a permanently visible blinking cursor,
25638 while others had very strong opinions against it. So it was
25639 decided to remove it. KFS 2003-09-03 */
25640
25641 /* Finally perform built-in cursor blinking:
25642 filled box <-> hollow box
25643 wide [h]bar <-> narrow [h]bar
25644 narrow [h]bar <-> no cursor
25645 other type <-> no cursor */
25646
25647 if (cursor_type == FILLED_BOX_CURSOR)
25648 return HOLLOW_BOX_CURSOR;
25649
25650 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25651 {
25652 *width = 1;
25653 return cursor_type;
25654 }
25655 #endif
25656
25657 return NO_CURSOR;
25658 }
25659
25660
25661 /* Notice when the text cursor of window W has been completely
25662 overwritten by a drawing operation that outputs glyphs in AREA
25663 starting at X0 and ending at X1 in the line starting at Y0 and
25664 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25665 the rest of the line after X0 has been written. Y coordinates
25666 are window-relative. */
25667
25668 static void
25669 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25670 int x0, int x1, int y0, int y1)
25671 {
25672 int cx0, cx1, cy0, cy1;
25673 struct glyph_row *row;
25674
25675 if (!w->phys_cursor_on_p)
25676 return;
25677 if (area != TEXT_AREA)
25678 return;
25679
25680 if (w->phys_cursor.vpos < 0
25681 || w->phys_cursor.vpos >= w->current_matrix->nrows
25682 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25683 !(row->enabled_p && row->displays_text_p)))
25684 return;
25685
25686 if (row->cursor_in_fringe_p)
25687 {
25688 row->cursor_in_fringe_p = 0;
25689 draw_fringe_bitmap (w, row, row->reversed_p);
25690 w->phys_cursor_on_p = 0;
25691 return;
25692 }
25693
25694 cx0 = w->phys_cursor.x;
25695 cx1 = cx0 + w->phys_cursor_width;
25696 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25697 return;
25698
25699 /* The cursor image will be completely removed from the
25700 screen if the output area intersects the cursor area in
25701 y-direction. When we draw in [y0 y1[, and some part of
25702 the cursor is at y < y0, that part must have been drawn
25703 before. When scrolling, the cursor is erased before
25704 actually scrolling, so we don't come here. When not
25705 scrolling, the rows above the old cursor row must have
25706 changed, and in this case these rows must have written
25707 over the cursor image.
25708
25709 Likewise if part of the cursor is below y1, with the
25710 exception of the cursor being in the first blank row at
25711 the buffer and window end because update_text_area
25712 doesn't draw that row. (Except when it does, but
25713 that's handled in update_text_area.) */
25714
25715 cy0 = w->phys_cursor.y;
25716 cy1 = cy0 + w->phys_cursor_height;
25717 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25718 return;
25719
25720 w->phys_cursor_on_p = 0;
25721 }
25722
25723 #endif /* HAVE_WINDOW_SYSTEM */
25724
25725 \f
25726 /************************************************************************
25727 Mouse Face
25728 ************************************************************************/
25729
25730 #ifdef HAVE_WINDOW_SYSTEM
25731
25732 /* EXPORT for RIF:
25733 Fix the display of area AREA of overlapping row ROW in window W
25734 with respect to the overlapping part OVERLAPS. */
25735
25736 void
25737 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25738 enum glyph_row_area area, int overlaps)
25739 {
25740 int i, x;
25741
25742 BLOCK_INPUT;
25743
25744 x = 0;
25745 for (i = 0; i < row->used[area];)
25746 {
25747 if (row->glyphs[area][i].overlaps_vertically_p)
25748 {
25749 int start = i, start_x = x;
25750
25751 do
25752 {
25753 x += row->glyphs[area][i].pixel_width;
25754 ++i;
25755 }
25756 while (i < row->used[area]
25757 && row->glyphs[area][i].overlaps_vertically_p);
25758
25759 draw_glyphs (w, start_x, row, area,
25760 start, i,
25761 DRAW_NORMAL_TEXT, overlaps);
25762 }
25763 else
25764 {
25765 x += row->glyphs[area][i].pixel_width;
25766 ++i;
25767 }
25768 }
25769
25770 UNBLOCK_INPUT;
25771 }
25772
25773
25774 /* EXPORT:
25775 Draw the cursor glyph of window W in glyph row ROW. See the
25776 comment of draw_glyphs for the meaning of HL. */
25777
25778 void
25779 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25780 enum draw_glyphs_face hl)
25781 {
25782 /* If cursor hpos is out of bounds, don't draw garbage. This can
25783 happen in mini-buffer windows when switching between echo area
25784 glyphs and mini-buffer. */
25785 if ((row->reversed_p
25786 ? (w->phys_cursor.hpos >= 0)
25787 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25788 {
25789 int on_p = w->phys_cursor_on_p;
25790 int x1;
25791 int hpos = w->phys_cursor.hpos;
25792
25793 /* When the window is hscrolled, cursor hpos can legitimately be
25794 out of bounds, but we draw the cursor at the corresponding
25795 window margin in that case. */
25796 if (!row->reversed_p && hpos < 0)
25797 hpos = 0;
25798 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25799 hpos = row->used[TEXT_AREA] - 1;
25800
25801 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25802 hl, 0);
25803 w->phys_cursor_on_p = on_p;
25804
25805 if (hl == DRAW_CURSOR)
25806 w->phys_cursor_width = x1 - w->phys_cursor.x;
25807 /* When we erase the cursor, and ROW is overlapped by other
25808 rows, make sure that these overlapping parts of other rows
25809 are redrawn. */
25810 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25811 {
25812 w->phys_cursor_width = x1 - w->phys_cursor.x;
25813
25814 if (row > w->current_matrix->rows
25815 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25816 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25817 OVERLAPS_ERASED_CURSOR);
25818
25819 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25820 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25821 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25822 OVERLAPS_ERASED_CURSOR);
25823 }
25824 }
25825 }
25826
25827
25828 /* EXPORT:
25829 Erase the image of a cursor of window W from the screen. */
25830
25831 void
25832 erase_phys_cursor (struct window *w)
25833 {
25834 struct frame *f = XFRAME (w->frame);
25835 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25836 int hpos = w->phys_cursor.hpos;
25837 int vpos = w->phys_cursor.vpos;
25838 int mouse_face_here_p = 0;
25839 struct glyph_matrix *active_glyphs = w->current_matrix;
25840 struct glyph_row *cursor_row;
25841 struct glyph *cursor_glyph;
25842 enum draw_glyphs_face hl;
25843
25844 /* No cursor displayed or row invalidated => nothing to do on the
25845 screen. */
25846 if (w->phys_cursor_type == NO_CURSOR)
25847 goto mark_cursor_off;
25848
25849 /* VPOS >= active_glyphs->nrows means that window has been resized.
25850 Don't bother to erase the cursor. */
25851 if (vpos >= active_glyphs->nrows)
25852 goto mark_cursor_off;
25853
25854 /* If row containing cursor is marked invalid, there is nothing we
25855 can do. */
25856 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25857 if (!cursor_row->enabled_p)
25858 goto mark_cursor_off;
25859
25860 /* If line spacing is > 0, old cursor may only be partially visible in
25861 window after split-window. So adjust visible height. */
25862 cursor_row->visible_height = min (cursor_row->visible_height,
25863 window_text_bottom_y (w) - cursor_row->y);
25864
25865 /* If row is completely invisible, don't attempt to delete a cursor which
25866 isn't there. This can happen if cursor is at top of a window, and
25867 we switch to a buffer with a header line in that window. */
25868 if (cursor_row->visible_height <= 0)
25869 goto mark_cursor_off;
25870
25871 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25872 if (cursor_row->cursor_in_fringe_p)
25873 {
25874 cursor_row->cursor_in_fringe_p = 0;
25875 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25876 goto mark_cursor_off;
25877 }
25878
25879 /* This can happen when the new row is shorter than the old one.
25880 In this case, either draw_glyphs or clear_end_of_line
25881 should have cleared the cursor. Note that we wouldn't be
25882 able to erase the cursor in this case because we don't have a
25883 cursor glyph at hand. */
25884 if ((cursor_row->reversed_p
25885 ? (w->phys_cursor.hpos < 0)
25886 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25887 goto mark_cursor_off;
25888
25889 /* When the window is hscrolled, cursor hpos can legitimately be out
25890 of bounds, but we draw the cursor at the corresponding window
25891 margin in that case. */
25892 if (!cursor_row->reversed_p && hpos < 0)
25893 hpos = 0;
25894 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25895 hpos = cursor_row->used[TEXT_AREA] - 1;
25896
25897 /* If the cursor is in the mouse face area, redisplay that when
25898 we clear the cursor. */
25899 if (! NILP (hlinfo->mouse_face_window)
25900 && coords_in_mouse_face_p (w, hpos, vpos)
25901 /* Don't redraw the cursor's spot in mouse face if it is at the
25902 end of a line (on a newline). The cursor appears there, but
25903 mouse highlighting does not. */
25904 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25905 mouse_face_here_p = 1;
25906
25907 /* Maybe clear the display under the cursor. */
25908 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25909 {
25910 int x, y, left_x;
25911 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25912 int width;
25913
25914 cursor_glyph = get_phys_cursor_glyph (w);
25915 if (cursor_glyph == NULL)
25916 goto mark_cursor_off;
25917
25918 width = cursor_glyph->pixel_width;
25919 left_x = window_box_left_offset (w, TEXT_AREA);
25920 x = w->phys_cursor.x;
25921 if (x < left_x)
25922 width -= left_x - x;
25923 width = min (width, window_box_width (w, TEXT_AREA) - x);
25924 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25925 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25926
25927 if (width > 0)
25928 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25929 }
25930
25931 /* Erase the cursor by redrawing the character underneath it. */
25932 if (mouse_face_here_p)
25933 hl = DRAW_MOUSE_FACE;
25934 else
25935 hl = DRAW_NORMAL_TEXT;
25936 draw_phys_cursor_glyph (w, cursor_row, hl);
25937
25938 mark_cursor_off:
25939 w->phys_cursor_on_p = 0;
25940 w->phys_cursor_type = NO_CURSOR;
25941 }
25942
25943
25944 /* EXPORT:
25945 Display or clear cursor of window W. If ON is zero, clear the
25946 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25947 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25948
25949 void
25950 display_and_set_cursor (struct window *w, int on,
25951 int hpos, int vpos, int x, int y)
25952 {
25953 struct frame *f = XFRAME (w->frame);
25954 int new_cursor_type;
25955 int new_cursor_width;
25956 int active_cursor;
25957 struct glyph_row *glyph_row;
25958 struct glyph *glyph;
25959
25960 /* This is pointless on invisible frames, and dangerous on garbaged
25961 windows and frames; in the latter case, the frame or window may
25962 be in the midst of changing its size, and x and y may be off the
25963 window. */
25964 if (! FRAME_VISIBLE_P (f)
25965 || FRAME_GARBAGED_P (f)
25966 || vpos >= w->current_matrix->nrows
25967 || hpos >= w->current_matrix->matrix_w)
25968 return;
25969
25970 /* If cursor is off and we want it off, return quickly. */
25971 if (!on && !w->phys_cursor_on_p)
25972 return;
25973
25974 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25975 /* If cursor row is not enabled, we don't really know where to
25976 display the cursor. */
25977 if (!glyph_row->enabled_p)
25978 {
25979 w->phys_cursor_on_p = 0;
25980 return;
25981 }
25982
25983 glyph = NULL;
25984 if (!glyph_row->exact_window_width_line_p
25985 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25986 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25987
25988 eassert (interrupt_input_blocked);
25989
25990 /* Set new_cursor_type to the cursor we want to be displayed. */
25991 new_cursor_type = get_window_cursor_type (w, glyph,
25992 &new_cursor_width, &active_cursor);
25993
25994 /* If cursor is currently being shown and we don't want it to be or
25995 it is in the wrong place, or the cursor type is not what we want,
25996 erase it. */
25997 if (w->phys_cursor_on_p
25998 && (!on
25999 || w->phys_cursor.x != x
26000 || w->phys_cursor.y != y
26001 || new_cursor_type != w->phys_cursor_type
26002 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26003 && new_cursor_width != w->phys_cursor_width)))
26004 erase_phys_cursor (w);
26005
26006 /* Don't check phys_cursor_on_p here because that flag is only set
26007 to zero in some cases where we know that the cursor has been
26008 completely erased, to avoid the extra work of erasing the cursor
26009 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26010 still not be visible, or it has only been partly erased. */
26011 if (on)
26012 {
26013 w->phys_cursor_ascent = glyph_row->ascent;
26014 w->phys_cursor_height = glyph_row->height;
26015
26016 /* Set phys_cursor_.* before x_draw_.* is called because some
26017 of them may need the information. */
26018 w->phys_cursor.x = x;
26019 w->phys_cursor.y = glyph_row->y;
26020 w->phys_cursor.hpos = hpos;
26021 w->phys_cursor.vpos = vpos;
26022 }
26023
26024 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26025 new_cursor_type, new_cursor_width,
26026 on, active_cursor);
26027 }
26028
26029
26030 /* Switch the display of W's cursor on or off, according to the value
26031 of ON. */
26032
26033 static void
26034 update_window_cursor (struct window *w, int on)
26035 {
26036 /* Don't update cursor in windows whose frame is in the process
26037 of being deleted. */
26038 if (w->current_matrix)
26039 {
26040 int hpos = w->phys_cursor.hpos;
26041 int vpos = w->phys_cursor.vpos;
26042 struct glyph_row *row;
26043
26044 if (vpos >= w->current_matrix->nrows
26045 || hpos >= w->current_matrix->matrix_w)
26046 return;
26047
26048 row = MATRIX_ROW (w->current_matrix, vpos);
26049
26050 /* When the window is hscrolled, cursor hpos can legitimately be
26051 out of bounds, but we draw the cursor at the corresponding
26052 window margin in that case. */
26053 if (!row->reversed_p && hpos < 0)
26054 hpos = 0;
26055 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26056 hpos = row->used[TEXT_AREA] - 1;
26057
26058 BLOCK_INPUT;
26059 display_and_set_cursor (w, on, hpos, vpos,
26060 w->phys_cursor.x, w->phys_cursor.y);
26061 UNBLOCK_INPUT;
26062 }
26063 }
26064
26065
26066 /* Call update_window_cursor with parameter ON_P on all leaf windows
26067 in the window tree rooted at W. */
26068
26069 static void
26070 update_cursor_in_window_tree (struct window *w, int on_p)
26071 {
26072 while (w)
26073 {
26074 if (!NILP (w->hchild))
26075 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26076 else if (!NILP (w->vchild))
26077 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26078 else
26079 update_window_cursor (w, on_p);
26080
26081 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26082 }
26083 }
26084
26085
26086 /* EXPORT:
26087 Display the cursor on window W, or clear it, according to ON_P.
26088 Don't change the cursor's position. */
26089
26090 void
26091 x_update_cursor (struct frame *f, int on_p)
26092 {
26093 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26094 }
26095
26096
26097 /* EXPORT:
26098 Clear the cursor of window W to background color, and mark the
26099 cursor as not shown. This is used when the text where the cursor
26100 is about to be rewritten. */
26101
26102 void
26103 x_clear_cursor (struct window *w)
26104 {
26105 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26106 update_window_cursor (w, 0);
26107 }
26108
26109 #endif /* HAVE_WINDOW_SYSTEM */
26110
26111 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26112 and MSDOS. */
26113 static void
26114 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26115 int start_hpos, int end_hpos,
26116 enum draw_glyphs_face draw)
26117 {
26118 #ifdef HAVE_WINDOW_SYSTEM
26119 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26120 {
26121 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26122 return;
26123 }
26124 #endif
26125 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26126 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26127 #endif
26128 }
26129
26130 /* Display the active region described by mouse_face_* according to DRAW. */
26131
26132 static void
26133 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26134 {
26135 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26136 struct frame *f = XFRAME (WINDOW_FRAME (w));
26137
26138 if (/* If window is in the process of being destroyed, don't bother
26139 to do anything. */
26140 w->current_matrix != NULL
26141 /* Don't update mouse highlight if hidden */
26142 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26143 /* Recognize when we are called to operate on rows that don't exist
26144 anymore. This can happen when a window is split. */
26145 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26146 {
26147 int phys_cursor_on_p = w->phys_cursor_on_p;
26148 struct glyph_row *row, *first, *last;
26149
26150 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26151 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26152
26153 for (row = first; row <= last && row->enabled_p; ++row)
26154 {
26155 int start_hpos, end_hpos, start_x;
26156
26157 /* For all but the first row, the highlight starts at column 0. */
26158 if (row == first)
26159 {
26160 /* R2L rows have BEG and END in reversed order, but the
26161 screen drawing geometry is always left to right. So
26162 we need to mirror the beginning and end of the
26163 highlighted area in R2L rows. */
26164 if (!row->reversed_p)
26165 {
26166 start_hpos = hlinfo->mouse_face_beg_col;
26167 start_x = hlinfo->mouse_face_beg_x;
26168 }
26169 else if (row == last)
26170 {
26171 start_hpos = hlinfo->mouse_face_end_col;
26172 start_x = hlinfo->mouse_face_end_x;
26173 }
26174 else
26175 {
26176 start_hpos = 0;
26177 start_x = 0;
26178 }
26179 }
26180 else if (row->reversed_p && row == last)
26181 {
26182 start_hpos = hlinfo->mouse_face_end_col;
26183 start_x = hlinfo->mouse_face_end_x;
26184 }
26185 else
26186 {
26187 start_hpos = 0;
26188 start_x = 0;
26189 }
26190
26191 if (row == last)
26192 {
26193 if (!row->reversed_p)
26194 end_hpos = hlinfo->mouse_face_end_col;
26195 else if (row == first)
26196 end_hpos = hlinfo->mouse_face_beg_col;
26197 else
26198 {
26199 end_hpos = row->used[TEXT_AREA];
26200 if (draw == DRAW_NORMAL_TEXT)
26201 row->fill_line_p = 1; /* Clear to end of line */
26202 }
26203 }
26204 else if (row->reversed_p && row == first)
26205 end_hpos = hlinfo->mouse_face_beg_col;
26206 else
26207 {
26208 end_hpos = row->used[TEXT_AREA];
26209 if (draw == DRAW_NORMAL_TEXT)
26210 row->fill_line_p = 1; /* Clear to end of line */
26211 }
26212
26213 if (end_hpos > start_hpos)
26214 {
26215 draw_row_with_mouse_face (w, start_x, row,
26216 start_hpos, end_hpos, draw);
26217
26218 row->mouse_face_p
26219 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26220 }
26221 }
26222
26223 #ifdef HAVE_WINDOW_SYSTEM
26224 /* When we've written over the cursor, arrange for it to
26225 be displayed again. */
26226 if (FRAME_WINDOW_P (f)
26227 && phys_cursor_on_p && !w->phys_cursor_on_p)
26228 {
26229 int hpos = w->phys_cursor.hpos;
26230
26231 /* When the window is hscrolled, cursor hpos can legitimately be
26232 out of bounds, but we draw the cursor at the corresponding
26233 window margin in that case. */
26234 if (!row->reversed_p && hpos < 0)
26235 hpos = 0;
26236 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26237 hpos = row->used[TEXT_AREA] - 1;
26238
26239 BLOCK_INPUT;
26240 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26241 w->phys_cursor.x, w->phys_cursor.y);
26242 UNBLOCK_INPUT;
26243 }
26244 #endif /* HAVE_WINDOW_SYSTEM */
26245 }
26246
26247 #ifdef HAVE_WINDOW_SYSTEM
26248 /* Change the mouse cursor. */
26249 if (FRAME_WINDOW_P (f))
26250 {
26251 if (draw == DRAW_NORMAL_TEXT
26252 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26253 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26254 else if (draw == DRAW_MOUSE_FACE)
26255 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26256 else
26257 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26258 }
26259 #endif /* HAVE_WINDOW_SYSTEM */
26260 }
26261
26262 /* EXPORT:
26263 Clear out the mouse-highlighted active region.
26264 Redraw it un-highlighted first. Value is non-zero if mouse
26265 face was actually drawn unhighlighted. */
26266
26267 int
26268 clear_mouse_face (Mouse_HLInfo *hlinfo)
26269 {
26270 int cleared = 0;
26271
26272 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26273 {
26274 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26275 cleared = 1;
26276 }
26277
26278 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26279 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26280 hlinfo->mouse_face_window = Qnil;
26281 hlinfo->mouse_face_overlay = Qnil;
26282 return cleared;
26283 }
26284
26285 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26286 within the mouse face on that window. */
26287 static int
26288 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26289 {
26290 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26291
26292 /* Quickly resolve the easy cases. */
26293 if (!(WINDOWP (hlinfo->mouse_face_window)
26294 && XWINDOW (hlinfo->mouse_face_window) == w))
26295 return 0;
26296 if (vpos < hlinfo->mouse_face_beg_row
26297 || vpos > hlinfo->mouse_face_end_row)
26298 return 0;
26299 if (vpos > hlinfo->mouse_face_beg_row
26300 && vpos < hlinfo->mouse_face_end_row)
26301 return 1;
26302
26303 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26304 {
26305 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26306 {
26307 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26308 return 1;
26309 }
26310 else if ((vpos == hlinfo->mouse_face_beg_row
26311 && hpos >= hlinfo->mouse_face_beg_col)
26312 || (vpos == hlinfo->mouse_face_end_row
26313 && hpos < hlinfo->mouse_face_end_col))
26314 return 1;
26315 }
26316 else
26317 {
26318 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26319 {
26320 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26321 return 1;
26322 }
26323 else if ((vpos == hlinfo->mouse_face_beg_row
26324 && hpos <= hlinfo->mouse_face_beg_col)
26325 || (vpos == hlinfo->mouse_face_end_row
26326 && hpos > hlinfo->mouse_face_end_col))
26327 return 1;
26328 }
26329 return 0;
26330 }
26331
26332
26333 /* EXPORT:
26334 Non-zero if physical cursor of window W is within mouse face. */
26335
26336 int
26337 cursor_in_mouse_face_p (struct window *w)
26338 {
26339 int hpos = w->phys_cursor.hpos;
26340 int vpos = w->phys_cursor.vpos;
26341 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26342
26343 /* When the window is hscrolled, cursor hpos can legitimately be out
26344 of bounds, but we draw the cursor at the corresponding window
26345 margin in that case. */
26346 if (!row->reversed_p && hpos < 0)
26347 hpos = 0;
26348 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26349 hpos = row->used[TEXT_AREA] - 1;
26350
26351 return coords_in_mouse_face_p (w, hpos, vpos);
26352 }
26353
26354
26355 \f
26356 /* Find the glyph rows START_ROW and END_ROW of window W that display
26357 characters between buffer positions START_CHARPOS and END_CHARPOS
26358 (excluding END_CHARPOS). DISP_STRING is a display string that
26359 covers these buffer positions. This is similar to
26360 row_containing_pos, but is more accurate when bidi reordering makes
26361 buffer positions change non-linearly with glyph rows. */
26362 static void
26363 rows_from_pos_range (struct window *w,
26364 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26365 Lisp_Object disp_string,
26366 struct glyph_row **start, struct glyph_row **end)
26367 {
26368 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26369 int last_y = window_text_bottom_y (w);
26370 struct glyph_row *row;
26371
26372 *start = NULL;
26373 *end = NULL;
26374
26375 while (!first->enabled_p
26376 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26377 first++;
26378
26379 /* Find the START row. */
26380 for (row = first;
26381 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26382 row++)
26383 {
26384 /* A row can potentially be the START row if the range of the
26385 characters it displays intersects the range
26386 [START_CHARPOS..END_CHARPOS). */
26387 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26388 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26389 /* See the commentary in row_containing_pos, for the
26390 explanation of the complicated way to check whether
26391 some position is beyond the end of the characters
26392 displayed by a row. */
26393 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26394 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26395 && !row->ends_at_zv_p
26396 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26397 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26398 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26399 && !row->ends_at_zv_p
26400 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26401 {
26402 /* Found a candidate row. Now make sure at least one of the
26403 glyphs it displays has a charpos from the range
26404 [START_CHARPOS..END_CHARPOS).
26405
26406 This is not obvious because bidi reordering could make
26407 buffer positions of a row be 1,2,3,102,101,100, and if we
26408 want to highlight characters in [50..60), we don't want
26409 this row, even though [50..60) does intersect [1..103),
26410 the range of character positions given by the row's start
26411 and end positions. */
26412 struct glyph *g = row->glyphs[TEXT_AREA];
26413 struct glyph *e = g + row->used[TEXT_AREA];
26414
26415 while (g < e)
26416 {
26417 if (((BUFFERP (g->object) || INTEGERP (g->object))
26418 && start_charpos <= g->charpos && g->charpos < end_charpos)
26419 /* A glyph that comes from DISP_STRING is by
26420 definition to be highlighted. */
26421 || EQ (g->object, disp_string))
26422 *start = row;
26423 g++;
26424 }
26425 if (*start)
26426 break;
26427 }
26428 }
26429
26430 /* Find the END row. */
26431 if (!*start
26432 /* If the last row is partially visible, start looking for END
26433 from that row, instead of starting from FIRST. */
26434 && !(row->enabled_p
26435 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26436 row = first;
26437 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26438 {
26439 struct glyph_row *next = row + 1;
26440 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26441
26442 if (!next->enabled_p
26443 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26444 /* The first row >= START whose range of displayed characters
26445 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26446 is the row END + 1. */
26447 || (start_charpos < next_start
26448 && end_charpos < next_start)
26449 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26450 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26451 && !next->ends_at_zv_p
26452 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26453 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26454 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26455 && !next->ends_at_zv_p
26456 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26457 {
26458 *end = row;
26459 break;
26460 }
26461 else
26462 {
26463 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26464 but none of the characters it displays are in the range, it is
26465 also END + 1. */
26466 struct glyph *g = next->glyphs[TEXT_AREA];
26467 struct glyph *s = g;
26468 struct glyph *e = g + next->used[TEXT_AREA];
26469
26470 while (g < e)
26471 {
26472 if (((BUFFERP (g->object) || INTEGERP (g->object))
26473 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26474 /* If the buffer position of the first glyph in
26475 the row is equal to END_CHARPOS, it means
26476 the last character to be highlighted is the
26477 newline of ROW, and we must consider NEXT as
26478 END, not END+1. */
26479 || (((!next->reversed_p && g == s)
26480 || (next->reversed_p && g == e - 1))
26481 && (g->charpos == end_charpos
26482 /* Special case for when NEXT is an
26483 empty line at ZV. */
26484 || (g->charpos == -1
26485 && !row->ends_at_zv_p
26486 && next_start == end_charpos)))))
26487 /* A glyph that comes from DISP_STRING is by
26488 definition to be highlighted. */
26489 || EQ (g->object, disp_string))
26490 break;
26491 g++;
26492 }
26493 if (g == e)
26494 {
26495 *end = row;
26496 break;
26497 }
26498 /* The first row that ends at ZV must be the last to be
26499 highlighted. */
26500 else if (next->ends_at_zv_p)
26501 {
26502 *end = next;
26503 break;
26504 }
26505 }
26506 }
26507 }
26508
26509 /* This function sets the mouse_face_* elements of HLINFO, assuming
26510 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26511 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26512 for the overlay or run of text properties specifying the mouse
26513 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26514 before-string and after-string that must also be highlighted.
26515 DISP_STRING, if non-nil, is a display string that may cover some
26516 or all of the highlighted text. */
26517
26518 static void
26519 mouse_face_from_buffer_pos (Lisp_Object window,
26520 Mouse_HLInfo *hlinfo,
26521 ptrdiff_t mouse_charpos,
26522 ptrdiff_t start_charpos,
26523 ptrdiff_t end_charpos,
26524 Lisp_Object before_string,
26525 Lisp_Object after_string,
26526 Lisp_Object disp_string)
26527 {
26528 struct window *w = XWINDOW (window);
26529 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26530 struct glyph_row *r1, *r2;
26531 struct glyph *glyph, *end;
26532 ptrdiff_t ignore, pos;
26533 int x;
26534
26535 eassert (NILP (disp_string) || STRINGP (disp_string));
26536 eassert (NILP (before_string) || STRINGP (before_string));
26537 eassert (NILP (after_string) || STRINGP (after_string));
26538
26539 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26540 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26541 if (r1 == NULL)
26542 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26543 /* If the before-string or display-string contains newlines,
26544 rows_from_pos_range skips to its last row. Move back. */
26545 if (!NILP (before_string) || !NILP (disp_string))
26546 {
26547 struct glyph_row *prev;
26548 while ((prev = r1 - 1, prev >= first)
26549 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26550 && prev->used[TEXT_AREA] > 0)
26551 {
26552 struct glyph *beg = prev->glyphs[TEXT_AREA];
26553 glyph = beg + prev->used[TEXT_AREA];
26554 while (--glyph >= beg && INTEGERP (glyph->object));
26555 if (glyph < beg
26556 || !(EQ (glyph->object, before_string)
26557 || EQ (glyph->object, disp_string)))
26558 break;
26559 r1 = prev;
26560 }
26561 }
26562 if (r2 == NULL)
26563 {
26564 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26565 hlinfo->mouse_face_past_end = 1;
26566 }
26567 else if (!NILP (after_string))
26568 {
26569 /* If the after-string has newlines, advance to its last row. */
26570 struct glyph_row *next;
26571 struct glyph_row *last
26572 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26573
26574 for (next = r2 + 1;
26575 next <= last
26576 && next->used[TEXT_AREA] > 0
26577 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26578 ++next)
26579 r2 = next;
26580 }
26581 /* The rest of the display engine assumes that mouse_face_beg_row is
26582 either above mouse_face_end_row or identical to it. But with
26583 bidi-reordered continued lines, the row for START_CHARPOS could
26584 be below the row for END_CHARPOS. If so, swap the rows and store
26585 them in correct order. */
26586 if (r1->y > r2->y)
26587 {
26588 struct glyph_row *tem = r2;
26589
26590 r2 = r1;
26591 r1 = tem;
26592 }
26593
26594 hlinfo->mouse_face_beg_y = r1->y;
26595 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26596 hlinfo->mouse_face_end_y = r2->y;
26597 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26598
26599 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26600 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26601 could be anywhere in the row and in any order. The strategy
26602 below is to find the leftmost and the rightmost glyph that
26603 belongs to either of these 3 strings, or whose position is
26604 between START_CHARPOS and END_CHARPOS, and highlight all the
26605 glyphs between those two. This may cover more than just the text
26606 between START_CHARPOS and END_CHARPOS if the range of characters
26607 strides the bidi level boundary, e.g. if the beginning is in R2L
26608 text while the end is in L2R text or vice versa. */
26609 if (!r1->reversed_p)
26610 {
26611 /* This row is in a left to right paragraph. Scan it left to
26612 right. */
26613 glyph = r1->glyphs[TEXT_AREA];
26614 end = glyph + r1->used[TEXT_AREA];
26615 x = r1->x;
26616
26617 /* Skip truncation glyphs at the start of the glyph row. */
26618 if (r1->displays_text_p)
26619 for (; glyph < end
26620 && INTEGERP (glyph->object)
26621 && glyph->charpos < 0;
26622 ++glyph)
26623 x += glyph->pixel_width;
26624
26625 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26626 or DISP_STRING, and the first glyph from buffer whose
26627 position is between START_CHARPOS and END_CHARPOS. */
26628 for (; glyph < end
26629 && !INTEGERP (glyph->object)
26630 && !EQ (glyph->object, disp_string)
26631 && !(BUFFERP (glyph->object)
26632 && (glyph->charpos >= start_charpos
26633 && glyph->charpos < end_charpos));
26634 ++glyph)
26635 {
26636 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26637 are present at buffer positions between START_CHARPOS and
26638 END_CHARPOS, or if they come from an overlay. */
26639 if (EQ (glyph->object, before_string))
26640 {
26641 pos = string_buffer_position (before_string,
26642 start_charpos);
26643 /* If pos == 0, it means before_string came from an
26644 overlay, not from a buffer position. */
26645 if (!pos || (pos >= start_charpos && pos < end_charpos))
26646 break;
26647 }
26648 else if (EQ (glyph->object, after_string))
26649 {
26650 pos = string_buffer_position (after_string, end_charpos);
26651 if (!pos || (pos >= start_charpos && pos < end_charpos))
26652 break;
26653 }
26654 x += glyph->pixel_width;
26655 }
26656 hlinfo->mouse_face_beg_x = x;
26657 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26658 }
26659 else
26660 {
26661 /* This row is in a right to left paragraph. Scan it right to
26662 left. */
26663 struct glyph *g;
26664
26665 end = r1->glyphs[TEXT_AREA] - 1;
26666 glyph = end + r1->used[TEXT_AREA];
26667
26668 /* Skip truncation glyphs at the start of the glyph row. */
26669 if (r1->displays_text_p)
26670 for (; glyph > end
26671 && INTEGERP (glyph->object)
26672 && glyph->charpos < 0;
26673 --glyph)
26674 ;
26675
26676 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26677 or DISP_STRING, and the first glyph from buffer whose
26678 position is between START_CHARPOS and END_CHARPOS. */
26679 for (; glyph > end
26680 && !INTEGERP (glyph->object)
26681 && !EQ (glyph->object, disp_string)
26682 && !(BUFFERP (glyph->object)
26683 && (glyph->charpos >= start_charpos
26684 && glyph->charpos < end_charpos));
26685 --glyph)
26686 {
26687 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26688 are present at buffer positions between START_CHARPOS and
26689 END_CHARPOS, or if they come from an overlay. */
26690 if (EQ (glyph->object, before_string))
26691 {
26692 pos = string_buffer_position (before_string, start_charpos);
26693 /* If pos == 0, it means before_string came from an
26694 overlay, not from a buffer position. */
26695 if (!pos || (pos >= start_charpos && pos < end_charpos))
26696 break;
26697 }
26698 else if (EQ (glyph->object, after_string))
26699 {
26700 pos = string_buffer_position (after_string, end_charpos);
26701 if (!pos || (pos >= start_charpos && pos < end_charpos))
26702 break;
26703 }
26704 }
26705
26706 glyph++; /* first glyph to the right of the highlighted area */
26707 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26708 x += g->pixel_width;
26709 hlinfo->mouse_face_beg_x = x;
26710 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26711 }
26712
26713 /* If the highlight ends in a different row, compute GLYPH and END
26714 for the end row. Otherwise, reuse the values computed above for
26715 the row where the highlight begins. */
26716 if (r2 != r1)
26717 {
26718 if (!r2->reversed_p)
26719 {
26720 glyph = r2->glyphs[TEXT_AREA];
26721 end = glyph + r2->used[TEXT_AREA];
26722 x = r2->x;
26723 }
26724 else
26725 {
26726 end = r2->glyphs[TEXT_AREA] - 1;
26727 glyph = end + r2->used[TEXT_AREA];
26728 }
26729 }
26730
26731 if (!r2->reversed_p)
26732 {
26733 /* Skip truncation and continuation glyphs near the end of the
26734 row, and also blanks and stretch glyphs inserted by
26735 extend_face_to_end_of_line. */
26736 while (end > glyph
26737 && INTEGERP ((end - 1)->object))
26738 --end;
26739 /* Scan the rest of the glyph row from the end, looking for the
26740 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26741 DISP_STRING, or whose position is between START_CHARPOS
26742 and END_CHARPOS */
26743 for (--end;
26744 end > glyph
26745 && !INTEGERP (end->object)
26746 && !EQ (end->object, disp_string)
26747 && !(BUFFERP (end->object)
26748 && (end->charpos >= start_charpos
26749 && end->charpos < end_charpos));
26750 --end)
26751 {
26752 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26753 are present at buffer positions between START_CHARPOS and
26754 END_CHARPOS, or if they come from an overlay. */
26755 if (EQ (end->object, before_string))
26756 {
26757 pos = string_buffer_position (before_string, start_charpos);
26758 if (!pos || (pos >= start_charpos && pos < end_charpos))
26759 break;
26760 }
26761 else if (EQ (end->object, after_string))
26762 {
26763 pos = string_buffer_position (after_string, end_charpos);
26764 if (!pos || (pos >= start_charpos && pos < end_charpos))
26765 break;
26766 }
26767 }
26768 /* Find the X coordinate of the last glyph to be highlighted. */
26769 for (; glyph <= end; ++glyph)
26770 x += glyph->pixel_width;
26771
26772 hlinfo->mouse_face_end_x = x;
26773 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26774 }
26775 else
26776 {
26777 /* Skip truncation and continuation glyphs near the end of the
26778 row, and also blanks and stretch glyphs inserted by
26779 extend_face_to_end_of_line. */
26780 x = r2->x;
26781 end++;
26782 while (end < glyph
26783 && INTEGERP (end->object))
26784 {
26785 x += end->pixel_width;
26786 ++end;
26787 }
26788 /* Scan the rest of the glyph row from the end, looking for the
26789 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26790 DISP_STRING, or whose position is between START_CHARPOS
26791 and END_CHARPOS */
26792 for ( ;
26793 end < glyph
26794 && !INTEGERP (end->object)
26795 && !EQ (end->object, disp_string)
26796 && !(BUFFERP (end->object)
26797 && (end->charpos >= start_charpos
26798 && end->charpos < end_charpos));
26799 ++end)
26800 {
26801 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26802 are present at buffer positions between START_CHARPOS and
26803 END_CHARPOS, or if they come from an overlay. */
26804 if (EQ (end->object, before_string))
26805 {
26806 pos = string_buffer_position (before_string, start_charpos);
26807 if (!pos || (pos >= start_charpos && pos < end_charpos))
26808 break;
26809 }
26810 else if (EQ (end->object, after_string))
26811 {
26812 pos = string_buffer_position (after_string, end_charpos);
26813 if (!pos || (pos >= start_charpos && pos < end_charpos))
26814 break;
26815 }
26816 x += end->pixel_width;
26817 }
26818 /* If we exited the above loop because we arrived at the last
26819 glyph of the row, and its buffer position is still not in
26820 range, it means the last character in range is the preceding
26821 newline. Bump the end column and x values to get past the
26822 last glyph. */
26823 if (end == glyph
26824 && BUFFERP (end->object)
26825 && (end->charpos < start_charpos
26826 || end->charpos >= end_charpos))
26827 {
26828 x += end->pixel_width;
26829 ++end;
26830 }
26831 hlinfo->mouse_face_end_x = x;
26832 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26833 }
26834
26835 hlinfo->mouse_face_window = window;
26836 hlinfo->mouse_face_face_id
26837 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26838 mouse_charpos + 1,
26839 !hlinfo->mouse_face_hidden, -1);
26840 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26841 }
26842
26843 /* The following function is not used anymore (replaced with
26844 mouse_face_from_string_pos), but I leave it here for the time
26845 being, in case someone would. */
26846
26847 #if 0 /* not used */
26848
26849 /* Find the position of the glyph for position POS in OBJECT in
26850 window W's current matrix, and return in *X, *Y the pixel
26851 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26852
26853 RIGHT_P non-zero means return the position of the right edge of the
26854 glyph, RIGHT_P zero means return the left edge position.
26855
26856 If no glyph for POS exists in the matrix, return the position of
26857 the glyph with the next smaller position that is in the matrix, if
26858 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26859 exists in the matrix, return the position of the glyph with the
26860 next larger position in OBJECT.
26861
26862 Value is non-zero if a glyph was found. */
26863
26864 static int
26865 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26866 int *hpos, int *vpos, int *x, int *y, int right_p)
26867 {
26868 int yb = window_text_bottom_y (w);
26869 struct glyph_row *r;
26870 struct glyph *best_glyph = NULL;
26871 struct glyph_row *best_row = NULL;
26872 int best_x = 0;
26873
26874 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26875 r->enabled_p && r->y < yb;
26876 ++r)
26877 {
26878 struct glyph *g = r->glyphs[TEXT_AREA];
26879 struct glyph *e = g + r->used[TEXT_AREA];
26880 int gx;
26881
26882 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26883 if (EQ (g->object, object))
26884 {
26885 if (g->charpos == pos)
26886 {
26887 best_glyph = g;
26888 best_x = gx;
26889 best_row = r;
26890 goto found;
26891 }
26892 else if (best_glyph == NULL
26893 || ((eabs (g->charpos - pos)
26894 < eabs (best_glyph->charpos - pos))
26895 && (right_p
26896 ? g->charpos < pos
26897 : g->charpos > pos)))
26898 {
26899 best_glyph = g;
26900 best_x = gx;
26901 best_row = r;
26902 }
26903 }
26904 }
26905
26906 found:
26907
26908 if (best_glyph)
26909 {
26910 *x = best_x;
26911 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26912
26913 if (right_p)
26914 {
26915 *x += best_glyph->pixel_width;
26916 ++*hpos;
26917 }
26918
26919 *y = best_row->y;
26920 *vpos = best_row - w->current_matrix->rows;
26921 }
26922
26923 return best_glyph != NULL;
26924 }
26925 #endif /* not used */
26926
26927 /* Find the positions of the first and the last glyphs in window W's
26928 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26929 (assumed to be a string), and return in HLINFO's mouse_face_*
26930 members the pixel and column/row coordinates of those glyphs. */
26931
26932 static void
26933 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26934 Lisp_Object object,
26935 ptrdiff_t startpos, ptrdiff_t endpos)
26936 {
26937 int yb = window_text_bottom_y (w);
26938 struct glyph_row *r;
26939 struct glyph *g, *e;
26940 int gx;
26941 int found = 0;
26942
26943 /* Find the glyph row with at least one position in the range
26944 [STARTPOS..ENDPOS], and the first glyph in that row whose
26945 position belongs to that range. */
26946 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26947 r->enabled_p && r->y < yb;
26948 ++r)
26949 {
26950 if (!r->reversed_p)
26951 {
26952 g = r->glyphs[TEXT_AREA];
26953 e = g + r->used[TEXT_AREA];
26954 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26955 if (EQ (g->object, object)
26956 && startpos <= g->charpos && g->charpos <= endpos)
26957 {
26958 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26959 hlinfo->mouse_face_beg_y = r->y;
26960 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26961 hlinfo->mouse_face_beg_x = gx;
26962 found = 1;
26963 break;
26964 }
26965 }
26966 else
26967 {
26968 struct glyph *g1;
26969
26970 e = r->glyphs[TEXT_AREA];
26971 g = e + r->used[TEXT_AREA];
26972 for ( ; g > e; --g)
26973 if (EQ ((g-1)->object, object)
26974 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26975 {
26976 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26977 hlinfo->mouse_face_beg_y = r->y;
26978 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26979 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26980 gx += g1->pixel_width;
26981 hlinfo->mouse_face_beg_x = gx;
26982 found = 1;
26983 break;
26984 }
26985 }
26986 if (found)
26987 break;
26988 }
26989
26990 if (!found)
26991 return;
26992
26993 /* Starting with the next row, look for the first row which does NOT
26994 include any glyphs whose positions are in the range. */
26995 for (++r; r->enabled_p && r->y < yb; ++r)
26996 {
26997 g = r->glyphs[TEXT_AREA];
26998 e = g + r->used[TEXT_AREA];
26999 found = 0;
27000 for ( ; g < e; ++g)
27001 if (EQ (g->object, object)
27002 && startpos <= g->charpos && g->charpos <= endpos)
27003 {
27004 found = 1;
27005 break;
27006 }
27007 if (!found)
27008 break;
27009 }
27010
27011 /* The highlighted region ends on the previous row. */
27012 r--;
27013
27014 /* Set the end row and its vertical pixel coordinate. */
27015 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27016 hlinfo->mouse_face_end_y = r->y;
27017
27018 /* Compute and set the end column and the end column's horizontal
27019 pixel coordinate. */
27020 if (!r->reversed_p)
27021 {
27022 g = r->glyphs[TEXT_AREA];
27023 e = g + r->used[TEXT_AREA];
27024 for ( ; e > g; --e)
27025 if (EQ ((e-1)->object, object)
27026 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27027 break;
27028 hlinfo->mouse_face_end_col = e - g;
27029
27030 for (gx = r->x; g < e; ++g)
27031 gx += g->pixel_width;
27032 hlinfo->mouse_face_end_x = gx;
27033 }
27034 else
27035 {
27036 e = r->glyphs[TEXT_AREA];
27037 g = e + r->used[TEXT_AREA];
27038 for (gx = r->x ; e < g; ++e)
27039 {
27040 if (EQ (e->object, object)
27041 && startpos <= e->charpos && e->charpos <= endpos)
27042 break;
27043 gx += e->pixel_width;
27044 }
27045 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27046 hlinfo->mouse_face_end_x = gx;
27047 }
27048 }
27049
27050 #ifdef HAVE_WINDOW_SYSTEM
27051
27052 /* See if position X, Y is within a hot-spot of an image. */
27053
27054 static int
27055 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27056 {
27057 if (!CONSP (hot_spot))
27058 return 0;
27059
27060 if (EQ (XCAR (hot_spot), Qrect))
27061 {
27062 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27063 Lisp_Object rect = XCDR (hot_spot);
27064 Lisp_Object tem;
27065 if (!CONSP (rect))
27066 return 0;
27067 if (!CONSP (XCAR (rect)))
27068 return 0;
27069 if (!CONSP (XCDR (rect)))
27070 return 0;
27071 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27072 return 0;
27073 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27074 return 0;
27075 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27076 return 0;
27077 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27078 return 0;
27079 return 1;
27080 }
27081 else if (EQ (XCAR (hot_spot), Qcircle))
27082 {
27083 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27084 Lisp_Object circ = XCDR (hot_spot);
27085 Lisp_Object lr, lx0, ly0;
27086 if (CONSP (circ)
27087 && CONSP (XCAR (circ))
27088 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27089 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27090 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27091 {
27092 double r = XFLOATINT (lr);
27093 double dx = XINT (lx0) - x;
27094 double dy = XINT (ly0) - y;
27095 return (dx * dx + dy * dy <= r * r);
27096 }
27097 }
27098 else if (EQ (XCAR (hot_spot), Qpoly))
27099 {
27100 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27101 if (VECTORP (XCDR (hot_spot)))
27102 {
27103 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27104 Lisp_Object *poly = v->contents;
27105 ptrdiff_t n = v->header.size;
27106 ptrdiff_t i;
27107 int inside = 0;
27108 Lisp_Object lx, ly;
27109 int x0, y0;
27110
27111 /* Need an even number of coordinates, and at least 3 edges. */
27112 if (n < 6 || n & 1)
27113 return 0;
27114
27115 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27116 If count is odd, we are inside polygon. Pixels on edges
27117 may or may not be included depending on actual geometry of the
27118 polygon. */
27119 if ((lx = poly[n-2], !INTEGERP (lx))
27120 || (ly = poly[n-1], !INTEGERP (lx)))
27121 return 0;
27122 x0 = XINT (lx), y0 = XINT (ly);
27123 for (i = 0; i < n; i += 2)
27124 {
27125 int x1 = x0, y1 = y0;
27126 if ((lx = poly[i], !INTEGERP (lx))
27127 || (ly = poly[i+1], !INTEGERP (ly)))
27128 return 0;
27129 x0 = XINT (lx), y0 = XINT (ly);
27130
27131 /* Does this segment cross the X line? */
27132 if (x0 >= x)
27133 {
27134 if (x1 >= x)
27135 continue;
27136 }
27137 else if (x1 < x)
27138 continue;
27139 if (y > y0 && y > y1)
27140 continue;
27141 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27142 inside = !inside;
27143 }
27144 return inside;
27145 }
27146 }
27147 return 0;
27148 }
27149
27150 Lisp_Object
27151 find_hot_spot (Lisp_Object map, int x, int y)
27152 {
27153 while (CONSP (map))
27154 {
27155 if (CONSP (XCAR (map))
27156 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27157 return XCAR (map);
27158 map = XCDR (map);
27159 }
27160
27161 return Qnil;
27162 }
27163
27164 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27165 3, 3, 0,
27166 doc: /* Lookup in image map MAP coordinates X and Y.
27167 An image map is an alist where each element has the format (AREA ID PLIST).
27168 An AREA is specified as either a rectangle, a circle, or a polygon:
27169 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27170 pixel coordinates of the upper left and bottom right corners.
27171 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27172 and the radius of the circle; r may be a float or integer.
27173 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27174 vector describes one corner in the polygon.
27175 Returns the alist element for the first matching AREA in MAP. */)
27176 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27177 {
27178 if (NILP (map))
27179 return Qnil;
27180
27181 CHECK_NUMBER (x);
27182 CHECK_NUMBER (y);
27183
27184 return find_hot_spot (map,
27185 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27186 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27187 }
27188
27189
27190 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27191 static void
27192 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27193 {
27194 /* Do not change cursor shape while dragging mouse. */
27195 if (!NILP (do_mouse_tracking))
27196 return;
27197
27198 if (!NILP (pointer))
27199 {
27200 if (EQ (pointer, Qarrow))
27201 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27202 else if (EQ (pointer, Qhand))
27203 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27204 else if (EQ (pointer, Qtext))
27205 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27206 else if (EQ (pointer, intern ("hdrag")))
27207 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27208 #ifdef HAVE_X_WINDOWS
27209 else if (EQ (pointer, intern ("vdrag")))
27210 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27211 #endif
27212 else if (EQ (pointer, intern ("hourglass")))
27213 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27214 else if (EQ (pointer, Qmodeline))
27215 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27216 else
27217 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27218 }
27219
27220 if (cursor != No_Cursor)
27221 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27222 }
27223
27224 #endif /* HAVE_WINDOW_SYSTEM */
27225
27226 /* Take proper action when mouse has moved to the mode or header line
27227 or marginal area AREA of window W, x-position X and y-position Y.
27228 X is relative to the start of the text display area of W, so the
27229 width of bitmap areas and scroll bars must be subtracted to get a
27230 position relative to the start of the mode line. */
27231
27232 static void
27233 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27234 enum window_part area)
27235 {
27236 struct window *w = XWINDOW (window);
27237 struct frame *f = XFRAME (w->frame);
27238 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27239 #ifdef HAVE_WINDOW_SYSTEM
27240 Display_Info *dpyinfo;
27241 #endif
27242 Cursor cursor = No_Cursor;
27243 Lisp_Object pointer = Qnil;
27244 int dx, dy, width, height;
27245 ptrdiff_t charpos;
27246 Lisp_Object string, object = Qnil;
27247 Lisp_Object pos IF_LINT (= Qnil), help;
27248
27249 Lisp_Object mouse_face;
27250 int original_x_pixel = x;
27251 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27252 struct glyph_row *row IF_LINT (= 0);
27253
27254 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27255 {
27256 int x0;
27257 struct glyph *end;
27258
27259 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27260 returns them in row/column units! */
27261 string = mode_line_string (w, area, &x, &y, &charpos,
27262 &object, &dx, &dy, &width, &height);
27263
27264 row = (area == ON_MODE_LINE
27265 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27266 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27267
27268 /* Find the glyph under the mouse pointer. */
27269 if (row->mode_line_p && row->enabled_p)
27270 {
27271 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27272 end = glyph + row->used[TEXT_AREA];
27273
27274 for (x0 = original_x_pixel;
27275 glyph < end && x0 >= glyph->pixel_width;
27276 ++glyph)
27277 x0 -= glyph->pixel_width;
27278
27279 if (glyph >= end)
27280 glyph = NULL;
27281 }
27282 }
27283 else
27284 {
27285 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27286 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27287 returns them in row/column units! */
27288 string = marginal_area_string (w, area, &x, &y, &charpos,
27289 &object, &dx, &dy, &width, &height);
27290 }
27291
27292 help = Qnil;
27293
27294 #ifdef HAVE_WINDOW_SYSTEM
27295 if (IMAGEP (object))
27296 {
27297 Lisp_Object image_map, hotspot;
27298 if ((image_map = Fplist_get (XCDR (object), QCmap),
27299 !NILP (image_map))
27300 && (hotspot = find_hot_spot (image_map, dx, dy),
27301 CONSP (hotspot))
27302 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27303 {
27304 Lisp_Object plist;
27305
27306 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27307 If so, we could look for mouse-enter, mouse-leave
27308 properties in PLIST (and do something...). */
27309 hotspot = XCDR (hotspot);
27310 if (CONSP (hotspot)
27311 && (plist = XCAR (hotspot), CONSP (plist)))
27312 {
27313 pointer = Fplist_get (plist, Qpointer);
27314 if (NILP (pointer))
27315 pointer = Qhand;
27316 help = Fplist_get (plist, Qhelp_echo);
27317 if (!NILP (help))
27318 {
27319 help_echo_string = help;
27320 XSETWINDOW (help_echo_window, w);
27321 help_echo_object = w->buffer;
27322 help_echo_pos = charpos;
27323 }
27324 }
27325 }
27326 if (NILP (pointer))
27327 pointer = Fplist_get (XCDR (object), QCpointer);
27328 }
27329 #endif /* HAVE_WINDOW_SYSTEM */
27330
27331 if (STRINGP (string))
27332 pos = make_number (charpos);
27333
27334 /* Set the help text and mouse pointer. If the mouse is on a part
27335 of the mode line without any text (e.g. past the right edge of
27336 the mode line text), use the default help text and pointer. */
27337 if (STRINGP (string) || area == ON_MODE_LINE)
27338 {
27339 /* Arrange to display the help by setting the global variables
27340 help_echo_string, help_echo_object, and help_echo_pos. */
27341 if (NILP (help))
27342 {
27343 if (STRINGP (string))
27344 help = Fget_text_property (pos, Qhelp_echo, string);
27345
27346 if (!NILP (help))
27347 {
27348 help_echo_string = help;
27349 XSETWINDOW (help_echo_window, w);
27350 help_echo_object = string;
27351 help_echo_pos = charpos;
27352 }
27353 else if (area == ON_MODE_LINE)
27354 {
27355 Lisp_Object default_help
27356 = buffer_local_value_1 (Qmode_line_default_help_echo,
27357 w->buffer);
27358
27359 if (STRINGP (default_help))
27360 {
27361 help_echo_string = default_help;
27362 XSETWINDOW (help_echo_window, w);
27363 help_echo_object = Qnil;
27364 help_echo_pos = -1;
27365 }
27366 }
27367 }
27368
27369 #ifdef HAVE_WINDOW_SYSTEM
27370 /* Change the mouse pointer according to what is under it. */
27371 if (FRAME_WINDOW_P (f))
27372 {
27373 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27374 if (STRINGP (string))
27375 {
27376 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27377
27378 if (NILP (pointer))
27379 pointer = Fget_text_property (pos, Qpointer, string);
27380
27381 /* Change the mouse pointer according to what is under X/Y. */
27382 if (NILP (pointer)
27383 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27384 {
27385 Lisp_Object map;
27386 map = Fget_text_property (pos, Qlocal_map, string);
27387 if (!KEYMAPP (map))
27388 map = Fget_text_property (pos, Qkeymap, string);
27389 if (!KEYMAPP (map))
27390 cursor = dpyinfo->vertical_scroll_bar_cursor;
27391 }
27392 }
27393 else
27394 /* Default mode-line pointer. */
27395 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27396 }
27397 #endif
27398 }
27399
27400 /* Change the mouse face according to what is under X/Y. */
27401 if (STRINGP (string))
27402 {
27403 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27404 if (!NILP (mouse_face)
27405 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27406 && glyph)
27407 {
27408 Lisp_Object b, e;
27409
27410 struct glyph * tmp_glyph;
27411
27412 int gpos;
27413 int gseq_length;
27414 int total_pixel_width;
27415 ptrdiff_t begpos, endpos, ignore;
27416
27417 int vpos, hpos;
27418
27419 b = Fprevious_single_property_change (make_number (charpos + 1),
27420 Qmouse_face, string, Qnil);
27421 if (NILP (b))
27422 begpos = 0;
27423 else
27424 begpos = XINT (b);
27425
27426 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27427 if (NILP (e))
27428 endpos = SCHARS (string);
27429 else
27430 endpos = XINT (e);
27431
27432 /* Calculate the glyph position GPOS of GLYPH in the
27433 displayed string, relative to the beginning of the
27434 highlighted part of the string.
27435
27436 Note: GPOS is different from CHARPOS. CHARPOS is the
27437 position of GLYPH in the internal string object. A mode
27438 line string format has structures which are converted to
27439 a flattened string by the Emacs Lisp interpreter. The
27440 internal string is an element of those structures. The
27441 displayed string is the flattened string. */
27442 tmp_glyph = row_start_glyph;
27443 while (tmp_glyph < glyph
27444 && (!(EQ (tmp_glyph->object, glyph->object)
27445 && begpos <= tmp_glyph->charpos
27446 && tmp_glyph->charpos < endpos)))
27447 tmp_glyph++;
27448 gpos = glyph - tmp_glyph;
27449
27450 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27451 the highlighted part of the displayed string to which
27452 GLYPH belongs. Note: GSEQ_LENGTH is different from
27453 SCHARS (STRING), because the latter returns the length of
27454 the internal string. */
27455 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27456 tmp_glyph > glyph
27457 && (!(EQ (tmp_glyph->object, glyph->object)
27458 && begpos <= tmp_glyph->charpos
27459 && tmp_glyph->charpos < endpos));
27460 tmp_glyph--)
27461 ;
27462 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27463
27464 /* Calculate the total pixel width of all the glyphs between
27465 the beginning of the highlighted area and GLYPH. */
27466 total_pixel_width = 0;
27467 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27468 total_pixel_width += tmp_glyph->pixel_width;
27469
27470 /* Pre calculation of re-rendering position. Note: X is in
27471 column units here, after the call to mode_line_string or
27472 marginal_area_string. */
27473 hpos = x - gpos;
27474 vpos = (area == ON_MODE_LINE
27475 ? (w->current_matrix)->nrows - 1
27476 : 0);
27477
27478 /* If GLYPH's position is included in the region that is
27479 already drawn in mouse face, we have nothing to do. */
27480 if ( EQ (window, hlinfo->mouse_face_window)
27481 && (!row->reversed_p
27482 ? (hlinfo->mouse_face_beg_col <= hpos
27483 && hpos < hlinfo->mouse_face_end_col)
27484 /* In R2L rows we swap BEG and END, see below. */
27485 : (hlinfo->mouse_face_end_col <= hpos
27486 && hpos < hlinfo->mouse_face_beg_col))
27487 && hlinfo->mouse_face_beg_row == vpos )
27488 return;
27489
27490 if (clear_mouse_face (hlinfo))
27491 cursor = No_Cursor;
27492
27493 if (!row->reversed_p)
27494 {
27495 hlinfo->mouse_face_beg_col = hpos;
27496 hlinfo->mouse_face_beg_x = original_x_pixel
27497 - (total_pixel_width + dx);
27498 hlinfo->mouse_face_end_col = hpos + gseq_length;
27499 hlinfo->mouse_face_end_x = 0;
27500 }
27501 else
27502 {
27503 /* In R2L rows, show_mouse_face expects BEG and END
27504 coordinates to be swapped. */
27505 hlinfo->mouse_face_end_col = hpos;
27506 hlinfo->mouse_face_end_x = original_x_pixel
27507 - (total_pixel_width + dx);
27508 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27509 hlinfo->mouse_face_beg_x = 0;
27510 }
27511
27512 hlinfo->mouse_face_beg_row = vpos;
27513 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27514 hlinfo->mouse_face_beg_y = 0;
27515 hlinfo->mouse_face_end_y = 0;
27516 hlinfo->mouse_face_past_end = 0;
27517 hlinfo->mouse_face_window = window;
27518
27519 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27520 charpos,
27521 0, 0, 0,
27522 &ignore,
27523 glyph->face_id,
27524 1);
27525 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27526
27527 if (NILP (pointer))
27528 pointer = Qhand;
27529 }
27530 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27531 clear_mouse_face (hlinfo);
27532 }
27533 #ifdef HAVE_WINDOW_SYSTEM
27534 if (FRAME_WINDOW_P (f))
27535 define_frame_cursor1 (f, cursor, pointer);
27536 #endif
27537 }
27538
27539
27540 /* EXPORT:
27541 Take proper action when the mouse has moved to position X, Y on
27542 frame F as regards highlighting characters that have mouse-face
27543 properties. Also de-highlighting chars where the mouse was before.
27544 X and Y can be negative or out of range. */
27545
27546 void
27547 note_mouse_highlight (struct frame *f, int x, int y)
27548 {
27549 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27550 enum window_part part = ON_NOTHING;
27551 Lisp_Object window;
27552 struct window *w;
27553 Cursor cursor = No_Cursor;
27554 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27555 struct buffer *b;
27556
27557 /* When a menu is active, don't highlight because this looks odd. */
27558 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27559 if (popup_activated ())
27560 return;
27561 #endif
27562
27563 if (NILP (Vmouse_highlight)
27564 || !f->glyphs_initialized_p
27565 || f->pointer_invisible)
27566 return;
27567
27568 hlinfo->mouse_face_mouse_x = x;
27569 hlinfo->mouse_face_mouse_y = y;
27570 hlinfo->mouse_face_mouse_frame = f;
27571
27572 if (hlinfo->mouse_face_defer)
27573 return;
27574
27575 if (gc_in_progress)
27576 {
27577 hlinfo->mouse_face_deferred_gc = 1;
27578 return;
27579 }
27580
27581 /* Which window is that in? */
27582 window = window_from_coordinates (f, x, y, &part, 1);
27583
27584 /* If displaying active text in another window, clear that. */
27585 if (! EQ (window, hlinfo->mouse_face_window)
27586 /* Also clear if we move out of text area in same window. */
27587 || (!NILP (hlinfo->mouse_face_window)
27588 && !NILP (window)
27589 && part != ON_TEXT
27590 && part != ON_MODE_LINE
27591 && part != ON_HEADER_LINE))
27592 clear_mouse_face (hlinfo);
27593
27594 /* Not on a window -> return. */
27595 if (!WINDOWP (window))
27596 return;
27597
27598 /* Reset help_echo_string. It will get recomputed below. */
27599 help_echo_string = Qnil;
27600
27601 /* Convert to window-relative pixel coordinates. */
27602 w = XWINDOW (window);
27603 frame_to_window_pixel_xy (w, &x, &y);
27604
27605 #ifdef HAVE_WINDOW_SYSTEM
27606 /* Handle tool-bar window differently since it doesn't display a
27607 buffer. */
27608 if (EQ (window, f->tool_bar_window))
27609 {
27610 note_tool_bar_highlight (f, x, y);
27611 return;
27612 }
27613 #endif
27614
27615 /* Mouse is on the mode, header line or margin? */
27616 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27617 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27618 {
27619 note_mode_line_or_margin_highlight (window, x, y, part);
27620 return;
27621 }
27622
27623 #ifdef HAVE_WINDOW_SYSTEM
27624 if (part == ON_VERTICAL_BORDER)
27625 {
27626 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27627 help_echo_string = build_string ("drag-mouse-1: resize");
27628 }
27629 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27630 || part == ON_SCROLL_BAR)
27631 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27632 else
27633 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27634 #endif
27635
27636 /* Are we in a window whose display is up to date?
27637 And verify the buffer's text has not changed. */
27638 b = XBUFFER (w->buffer);
27639 if (part == ON_TEXT
27640 && EQ (w->window_end_valid, w->buffer)
27641 && w->last_modified == BUF_MODIFF (b)
27642 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27643 {
27644 int hpos, vpos, dx, dy, area = LAST_AREA;
27645 ptrdiff_t pos;
27646 struct glyph *glyph;
27647 Lisp_Object object;
27648 Lisp_Object mouse_face = Qnil, position;
27649 Lisp_Object *overlay_vec = NULL;
27650 ptrdiff_t i, noverlays;
27651 struct buffer *obuf;
27652 ptrdiff_t obegv, ozv;
27653 int same_region;
27654
27655 /* Find the glyph under X/Y. */
27656 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27657
27658 #ifdef HAVE_WINDOW_SYSTEM
27659 /* Look for :pointer property on image. */
27660 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27661 {
27662 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27663 if (img != NULL && IMAGEP (img->spec))
27664 {
27665 Lisp_Object image_map, hotspot;
27666 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27667 !NILP (image_map))
27668 && (hotspot = find_hot_spot (image_map,
27669 glyph->slice.img.x + dx,
27670 glyph->slice.img.y + dy),
27671 CONSP (hotspot))
27672 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27673 {
27674 Lisp_Object plist;
27675
27676 /* Could check XCAR (hotspot) to see if we enter/leave
27677 this hot-spot.
27678 If so, we could look for mouse-enter, mouse-leave
27679 properties in PLIST (and do something...). */
27680 hotspot = XCDR (hotspot);
27681 if (CONSP (hotspot)
27682 && (plist = XCAR (hotspot), CONSP (plist)))
27683 {
27684 pointer = Fplist_get (plist, Qpointer);
27685 if (NILP (pointer))
27686 pointer = Qhand;
27687 help_echo_string = Fplist_get (plist, Qhelp_echo);
27688 if (!NILP (help_echo_string))
27689 {
27690 help_echo_window = window;
27691 help_echo_object = glyph->object;
27692 help_echo_pos = glyph->charpos;
27693 }
27694 }
27695 }
27696 if (NILP (pointer))
27697 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27698 }
27699 }
27700 #endif /* HAVE_WINDOW_SYSTEM */
27701
27702 /* Clear mouse face if X/Y not over text. */
27703 if (glyph == NULL
27704 || area != TEXT_AREA
27705 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27706 /* Glyph's OBJECT is an integer for glyphs inserted by the
27707 display engine for its internal purposes, like truncation
27708 and continuation glyphs and blanks beyond the end of
27709 line's text on text terminals. If we are over such a
27710 glyph, we are not over any text. */
27711 || INTEGERP (glyph->object)
27712 /* R2L rows have a stretch glyph at their front, which
27713 stands for no text, whereas L2R rows have no glyphs at
27714 all beyond the end of text. Treat such stretch glyphs
27715 like we do with NULL glyphs in L2R rows. */
27716 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27717 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27718 && glyph->type == STRETCH_GLYPH
27719 && glyph->avoid_cursor_p))
27720 {
27721 if (clear_mouse_face (hlinfo))
27722 cursor = No_Cursor;
27723 #ifdef HAVE_WINDOW_SYSTEM
27724 if (FRAME_WINDOW_P (f) && NILP (pointer))
27725 {
27726 if (area != TEXT_AREA)
27727 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27728 else
27729 pointer = Vvoid_text_area_pointer;
27730 }
27731 #endif
27732 goto set_cursor;
27733 }
27734
27735 pos = glyph->charpos;
27736 object = glyph->object;
27737 if (!STRINGP (object) && !BUFFERP (object))
27738 goto set_cursor;
27739
27740 /* If we get an out-of-range value, return now; avoid an error. */
27741 if (BUFFERP (object) && pos > BUF_Z (b))
27742 goto set_cursor;
27743
27744 /* Make the window's buffer temporarily current for
27745 overlays_at and compute_char_face. */
27746 obuf = current_buffer;
27747 current_buffer = b;
27748 obegv = BEGV;
27749 ozv = ZV;
27750 BEGV = BEG;
27751 ZV = Z;
27752
27753 /* Is this char mouse-active or does it have help-echo? */
27754 position = make_number (pos);
27755
27756 if (BUFFERP (object))
27757 {
27758 /* Put all the overlays we want in a vector in overlay_vec. */
27759 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27760 /* Sort overlays into increasing priority order. */
27761 noverlays = sort_overlays (overlay_vec, noverlays, w);
27762 }
27763 else
27764 noverlays = 0;
27765
27766 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27767
27768 if (same_region)
27769 cursor = No_Cursor;
27770
27771 /* Check mouse-face highlighting. */
27772 if (! same_region
27773 /* If there exists an overlay with mouse-face overlapping
27774 the one we are currently highlighting, we have to
27775 check if we enter the overlapping overlay, and then
27776 highlight only that. */
27777 || (OVERLAYP (hlinfo->mouse_face_overlay)
27778 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27779 {
27780 /* Find the highest priority overlay with a mouse-face. */
27781 Lisp_Object overlay = Qnil;
27782 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27783 {
27784 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27785 if (!NILP (mouse_face))
27786 overlay = overlay_vec[i];
27787 }
27788
27789 /* If we're highlighting the same overlay as before, there's
27790 no need to do that again. */
27791 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27792 goto check_help_echo;
27793 hlinfo->mouse_face_overlay = overlay;
27794
27795 /* Clear the display of the old active region, if any. */
27796 if (clear_mouse_face (hlinfo))
27797 cursor = No_Cursor;
27798
27799 /* If no overlay applies, get a text property. */
27800 if (NILP (overlay))
27801 mouse_face = Fget_text_property (position, Qmouse_face, object);
27802
27803 /* Next, compute the bounds of the mouse highlighting and
27804 display it. */
27805 if (!NILP (mouse_face) && STRINGP (object))
27806 {
27807 /* The mouse-highlighting comes from a display string
27808 with a mouse-face. */
27809 Lisp_Object s, e;
27810 ptrdiff_t ignore;
27811
27812 s = Fprevious_single_property_change
27813 (make_number (pos + 1), Qmouse_face, object, Qnil);
27814 e = Fnext_single_property_change
27815 (position, Qmouse_face, object, Qnil);
27816 if (NILP (s))
27817 s = make_number (0);
27818 if (NILP (e))
27819 e = make_number (SCHARS (object) - 1);
27820 mouse_face_from_string_pos (w, hlinfo, object,
27821 XINT (s), XINT (e));
27822 hlinfo->mouse_face_past_end = 0;
27823 hlinfo->mouse_face_window = window;
27824 hlinfo->mouse_face_face_id
27825 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27826 glyph->face_id, 1);
27827 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27828 cursor = No_Cursor;
27829 }
27830 else
27831 {
27832 /* The mouse-highlighting, if any, comes from an overlay
27833 or text property in the buffer. */
27834 Lisp_Object buffer IF_LINT (= Qnil);
27835 Lisp_Object disp_string IF_LINT (= Qnil);
27836
27837 if (STRINGP (object))
27838 {
27839 /* If we are on a display string with no mouse-face,
27840 check if the text under it has one. */
27841 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27842 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27843 pos = string_buffer_position (object, start);
27844 if (pos > 0)
27845 {
27846 mouse_face = get_char_property_and_overlay
27847 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27848 buffer = w->buffer;
27849 disp_string = object;
27850 }
27851 }
27852 else
27853 {
27854 buffer = object;
27855 disp_string = Qnil;
27856 }
27857
27858 if (!NILP (mouse_face))
27859 {
27860 Lisp_Object before, after;
27861 Lisp_Object before_string, after_string;
27862 /* To correctly find the limits of mouse highlight
27863 in a bidi-reordered buffer, we must not use the
27864 optimization of limiting the search in
27865 previous-single-property-change and
27866 next-single-property-change, because
27867 rows_from_pos_range needs the real start and end
27868 positions to DTRT in this case. That's because
27869 the first row visible in a window does not
27870 necessarily display the character whose position
27871 is the smallest. */
27872 Lisp_Object lim1 =
27873 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27874 ? Fmarker_position (w->start)
27875 : Qnil;
27876 Lisp_Object lim2 =
27877 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27878 ? make_number (BUF_Z (XBUFFER (buffer))
27879 - XFASTINT (w->window_end_pos))
27880 : Qnil;
27881
27882 if (NILP (overlay))
27883 {
27884 /* Handle the text property case. */
27885 before = Fprevious_single_property_change
27886 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27887 after = Fnext_single_property_change
27888 (make_number (pos), Qmouse_face, buffer, lim2);
27889 before_string = after_string = Qnil;
27890 }
27891 else
27892 {
27893 /* Handle the overlay case. */
27894 before = Foverlay_start (overlay);
27895 after = Foverlay_end (overlay);
27896 before_string = Foverlay_get (overlay, Qbefore_string);
27897 after_string = Foverlay_get (overlay, Qafter_string);
27898
27899 if (!STRINGP (before_string)) before_string = Qnil;
27900 if (!STRINGP (after_string)) after_string = Qnil;
27901 }
27902
27903 mouse_face_from_buffer_pos (window, hlinfo, pos,
27904 NILP (before)
27905 ? 1
27906 : XFASTINT (before),
27907 NILP (after)
27908 ? BUF_Z (XBUFFER (buffer))
27909 : XFASTINT (after),
27910 before_string, after_string,
27911 disp_string);
27912 cursor = No_Cursor;
27913 }
27914 }
27915 }
27916
27917 check_help_echo:
27918
27919 /* Look for a `help-echo' property. */
27920 if (NILP (help_echo_string)) {
27921 Lisp_Object help, overlay;
27922
27923 /* Check overlays first. */
27924 help = overlay = Qnil;
27925 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27926 {
27927 overlay = overlay_vec[i];
27928 help = Foverlay_get (overlay, Qhelp_echo);
27929 }
27930
27931 if (!NILP (help))
27932 {
27933 help_echo_string = help;
27934 help_echo_window = window;
27935 help_echo_object = overlay;
27936 help_echo_pos = pos;
27937 }
27938 else
27939 {
27940 Lisp_Object obj = glyph->object;
27941 ptrdiff_t charpos = glyph->charpos;
27942
27943 /* Try text properties. */
27944 if (STRINGP (obj)
27945 && charpos >= 0
27946 && charpos < SCHARS (obj))
27947 {
27948 help = Fget_text_property (make_number (charpos),
27949 Qhelp_echo, obj);
27950 if (NILP (help))
27951 {
27952 /* If the string itself doesn't specify a help-echo,
27953 see if the buffer text ``under'' it does. */
27954 struct glyph_row *r
27955 = MATRIX_ROW (w->current_matrix, vpos);
27956 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27957 ptrdiff_t p = string_buffer_position (obj, start);
27958 if (p > 0)
27959 {
27960 help = Fget_char_property (make_number (p),
27961 Qhelp_echo, w->buffer);
27962 if (!NILP (help))
27963 {
27964 charpos = p;
27965 obj = w->buffer;
27966 }
27967 }
27968 }
27969 }
27970 else if (BUFFERP (obj)
27971 && charpos >= BEGV
27972 && charpos < ZV)
27973 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27974 obj);
27975
27976 if (!NILP (help))
27977 {
27978 help_echo_string = help;
27979 help_echo_window = window;
27980 help_echo_object = obj;
27981 help_echo_pos = charpos;
27982 }
27983 }
27984 }
27985
27986 #ifdef HAVE_WINDOW_SYSTEM
27987 /* Look for a `pointer' property. */
27988 if (FRAME_WINDOW_P (f) && NILP (pointer))
27989 {
27990 /* Check overlays first. */
27991 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27992 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27993
27994 if (NILP (pointer))
27995 {
27996 Lisp_Object obj = glyph->object;
27997 ptrdiff_t charpos = glyph->charpos;
27998
27999 /* Try text properties. */
28000 if (STRINGP (obj)
28001 && charpos >= 0
28002 && charpos < SCHARS (obj))
28003 {
28004 pointer = Fget_text_property (make_number (charpos),
28005 Qpointer, obj);
28006 if (NILP (pointer))
28007 {
28008 /* If the string itself doesn't specify a pointer,
28009 see if the buffer text ``under'' it does. */
28010 struct glyph_row *r
28011 = MATRIX_ROW (w->current_matrix, vpos);
28012 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28013 ptrdiff_t p = string_buffer_position (obj, start);
28014 if (p > 0)
28015 pointer = Fget_char_property (make_number (p),
28016 Qpointer, w->buffer);
28017 }
28018 }
28019 else if (BUFFERP (obj)
28020 && charpos >= BEGV
28021 && charpos < ZV)
28022 pointer = Fget_text_property (make_number (charpos),
28023 Qpointer, obj);
28024 }
28025 }
28026 #endif /* HAVE_WINDOW_SYSTEM */
28027
28028 BEGV = obegv;
28029 ZV = ozv;
28030 current_buffer = obuf;
28031 }
28032
28033 set_cursor:
28034
28035 #ifdef HAVE_WINDOW_SYSTEM
28036 if (FRAME_WINDOW_P (f))
28037 define_frame_cursor1 (f, cursor, pointer);
28038 #else
28039 /* This is here to prevent a compiler error, about "label at end of
28040 compound statement". */
28041 return;
28042 #endif
28043 }
28044
28045
28046 /* EXPORT for RIF:
28047 Clear any mouse-face on window W. This function is part of the
28048 redisplay interface, and is called from try_window_id and similar
28049 functions to ensure the mouse-highlight is off. */
28050
28051 void
28052 x_clear_window_mouse_face (struct window *w)
28053 {
28054 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28055 Lisp_Object window;
28056
28057 BLOCK_INPUT;
28058 XSETWINDOW (window, w);
28059 if (EQ (window, hlinfo->mouse_face_window))
28060 clear_mouse_face (hlinfo);
28061 UNBLOCK_INPUT;
28062 }
28063
28064
28065 /* EXPORT:
28066 Just discard the mouse face information for frame F, if any.
28067 This is used when the size of F is changed. */
28068
28069 void
28070 cancel_mouse_face (struct frame *f)
28071 {
28072 Lisp_Object window;
28073 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28074
28075 window = hlinfo->mouse_face_window;
28076 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28077 {
28078 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28079 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28080 hlinfo->mouse_face_window = Qnil;
28081 }
28082 }
28083
28084
28085 \f
28086 /***********************************************************************
28087 Exposure Events
28088 ***********************************************************************/
28089
28090 #ifdef HAVE_WINDOW_SYSTEM
28091
28092 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28093 which intersects rectangle R. R is in window-relative coordinates. */
28094
28095 static void
28096 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28097 enum glyph_row_area area)
28098 {
28099 struct glyph *first = row->glyphs[area];
28100 struct glyph *end = row->glyphs[area] + row->used[area];
28101 struct glyph *last;
28102 int first_x, start_x, x;
28103
28104 if (area == TEXT_AREA && row->fill_line_p)
28105 /* If row extends face to end of line write the whole line. */
28106 draw_glyphs (w, 0, row, area,
28107 0, row->used[area],
28108 DRAW_NORMAL_TEXT, 0);
28109 else
28110 {
28111 /* Set START_X to the window-relative start position for drawing glyphs of
28112 AREA. The first glyph of the text area can be partially visible.
28113 The first glyphs of other areas cannot. */
28114 start_x = window_box_left_offset (w, area);
28115 x = start_x;
28116 if (area == TEXT_AREA)
28117 x += row->x;
28118
28119 /* Find the first glyph that must be redrawn. */
28120 while (first < end
28121 && x + first->pixel_width < r->x)
28122 {
28123 x += first->pixel_width;
28124 ++first;
28125 }
28126
28127 /* Find the last one. */
28128 last = first;
28129 first_x = x;
28130 while (last < end
28131 && x < r->x + r->width)
28132 {
28133 x += last->pixel_width;
28134 ++last;
28135 }
28136
28137 /* Repaint. */
28138 if (last > first)
28139 draw_glyphs (w, first_x - start_x, row, area,
28140 first - row->glyphs[area], last - row->glyphs[area],
28141 DRAW_NORMAL_TEXT, 0);
28142 }
28143 }
28144
28145
28146 /* Redraw the parts of the glyph row ROW on window W intersecting
28147 rectangle R. R is in window-relative coordinates. Value is
28148 non-zero if mouse-face was overwritten. */
28149
28150 static int
28151 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28152 {
28153 eassert (row->enabled_p);
28154
28155 if (row->mode_line_p || w->pseudo_window_p)
28156 draw_glyphs (w, 0, row, TEXT_AREA,
28157 0, row->used[TEXT_AREA],
28158 DRAW_NORMAL_TEXT, 0);
28159 else
28160 {
28161 if (row->used[LEFT_MARGIN_AREA])
28162 expose_area (w, row, r, LEFT_MARGIN_AREA);
28163 if (row->used[TEXT_AREA])
28164 expose_area (w, row, r, TEXT_AREA);
28165 if (row->used[RIGHT_MARGIN_AREA])
28166 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28167 draw_row_fringe_bitmaps (w, row);
28168 }
28169
28170 return row->mouse_face_p;
28171 }
28172
28173
28174 /* Redraw those parts of glyphs rows during expose event handling that
28175 overlap other rows. Redrawing of an exposed line writes over parts
28176 of lines overlapping that exposed line; this function fixes that.
28177
28178 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28179 row in W's current matrix that is exposed and overlaps other rows.
28180 LAST_OVERLAPPING_ROW is the last such row. */
28181
28182 static void
28183 expose_overlaps (struct window *w,
28184 struct glyph_row *first_overlapping_row,
28185 struct glyph_row *last_overlapping_row,
28186 XRectangle *r)
28187 {
28188 struct glyph_row *row;
28189
28190 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28191 if (row->overlapping_p)
28192 {
28193 eassert (row->enabled_p && !row->mode_line_p);
28194
28195 row->clip = r;
28196 if (row->used[LEFT_MARGIN_AREA])
28197 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28198
28199 if (row->used[TEXT_AREA])
28200 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28201
28202 if (row->used[RIGHT_MARGIN_AREA])
28203 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28204 row->clip = NULL;
28205 }
28206 }
28207
28208
28209 /* Return non-zero if W's cursor intersects rectangle R. */
28210
28211 static int
28212 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28213 {
28214 XRectangle cr, result;
28215 struct glyph *cursor_glyph;
28216 struct glyph_row *row;
28217
28218 if (w->phys_cursor.vpos >= 0
28219 && w->phys_cursor.vpos < w->current_matrix->nrows
28220 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28221 row->enabled_p)
28222 && row->cursor_in_fringe_p)
28223 {
28224 /* Cursor is in the fringe. */
28225 cr.x = window_box_right_offset (w,
28226 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28227 ? RIGHT_MARGIN_AREA
28228 : TEXT_AREA));
28229 cr.y = row->y;
28230 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28231 cr.height = row->height;
28232 return x_intersect_rectangles (&cr, r, &result);
28233 }
28234
28235 cursor_glyph = get_phys_cursor_glyph (w);
28236 if (cursor_glyph)
28237 {
28238 /* r is relative to W's box, but w->phys_cursor.x is relative
28239 to left edge of W's TEXT area. Adjust it. */
28240 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28241 cr.y = w->phys_cursor.y;
28242 cr.width = cursor_glyph->pixel_width;
28243 cr.height = w->phys_cursor_height;
28244 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28245 I assume the effect is the same -- and this is portable. */
28246 return x_intersect_rectangles (&cr, r, &result);
28247 }
28248 /* If we don't understand the format, pretend we're not in the hot-spot. */
28249 return 0;
28250 }
28251
28252
28253 /* EXPORT:
28254 Draw a vertical window border to the right of window W if W doesn't
28255 have vertical scroll bars. */
28256
28257 void
28258 x_draw_vertical_border (struct window *w)
28259 {
28260 struct frame *f = XFRAME (WINDOW_FRAME (w));
28261
28262 /* We could do better, if we knew what type of scroll-bar the adjacent
28263 windows (on either side) have... But we don't :-(
28264 However, I think this works ok. ++KFS 2003-04-25 */
28265
28266 /* Redraw borders between horizontally adjacent windows. Don't
28267 do it for frames with vertical scroll bars because either the
28268 right scroll bar of a window, or the left scroll bar of its
28269 neighbor will suffice as a border. */
28270 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28271 return;
28272
28273 if (!WINDOW_RIGHTMOST_P (w)
28274 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28275 {
28276 int x0, x1, y0, y1;
28277
28278 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28279 y1 -= 1;
28280
28281 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28282 x1 -= 1;
28283
28284 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28285 }
28286 else if (!WINDOW_LEFTMOST_P (w)
28287 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28288 {
28289 int x0, x1, y0, y1;
28290
28291 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28292 y1 -= 1;
28293
28294 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28295 x0 -= 1;
28296
28297 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28298 }
28299 }
28300
28301
28302 /* Redraw the part of window W intersection rectangle FR. Pixel
28303 coordinates in FR are frame-relative. Call this function with
28304 input blocked. Value is non-zero if the exposure overwrites
28305 mouse-face. */
28306
28307 static int
28308 expose_window (struct window *w, XRectangle *fr)
28309 {
28310 struct frame *f = XFRAME (w->frame);
28311 XRectangle wr, r;
28312 int mouse_face_overwritten_p = 0;
28313
28314 /* If window is not yet fully initialized, do nothing. This can
28315 happen when toolkit scroll bars are used and a window is split.
28316 Reconfiguring the scroll bar will generate an expose for a newly
28317 created window. */
28318 if (w->current_matrix == NULL)
28319 return 0;
28320
28321 /* When we're currently updating the window, display and current
28322 matrix usually don't agree. Arrange for a thorough display
28323 later. */
28324 if (w == updated_window)
28325 {
28326 SET_FRAME_GARBAGED (f);
28327 return 0;
28328 }
28329
28330 /* Frame-relative pixel rectangle of W. */
28331 wr.x = WINDOW_LEFT_EDGE_X (w);
28332 wr.y = WINDOW_TOP_EDGE_Y (w);
28333 wr.width = WINDOW_TOTAL_WIDTH (w);
28334 wr.height = WINDOW_TOTAL_HEIGHT (w);
28335
28336 if (x_intersect_rectangles (fr, &wr, &r))
28337 {
28338 int yb = window_text_bottom_y (w);
28339 struct glyph_row *row;
28340 int cursor_cleared_p, phys_cursor_on_p;
28341 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28342
28343 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28344 r.x, r.y, r.width, r.height));
28345
28346 /* Convert to window coordinates. */
28347 r.x -= WINDOW_LEFT_EDGE_X (w);
28348 r.y -= WINDOW_TOP_EDGE_Y (w);
28349
28350 /* Turn off the cursor. */
28351 if (!w->pseudo_window_p
28352 && phys_cursor_in_rect_p (w, &r))
28353 {
28354 x_clear_cursor (w);
28355 cursor_cleared_p = 1;
28356 }
28357 else
28358 cursor_cleared_p = 0;
28359
28360 /* If the row containing the cursor extends face to end of line,
28361 then expose_area might overwrite the cursor outside the
28362 rectangle and thus notice_overwritten_cursor might clear
28363 w->phys_cursor_on_p. We remember the original value and
28364 check later if it is changed. */
28365 phys_cursor_on_p = w->phys_cursor_on_p;
28366
28367 /* Update lines intersecting rectangle R. */
28368 first_overlapping_row = last_overlapping_row = NULL;
28369 for (row = w->current_matrix->rows;
28370 row->enabled_p;
28371 ++row)
28372 {
28373 int y0 = row->y;
28374 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28375
28376 if ((y0 >= r.y && y0 < r.y + r.height)
28377 || (y1 > r.y && y1 < r.y + r.height)
28378 || (r.y >= y0 && r.y < y1)
28379 || (r.y + r.height > y0 && r.y + r.height < y1))
28380 {
28381 /* A header line may be overlapping, but there is no need
28382 to fix overlapping areas for them. KFS 2005-02-12 */
28383 if (row->overlapping_p && !row->mode_line_p)
28384 {
28385 if (first_overlapping_row == NULL)
28386 first_overlapping_row = row;
28387 last_overlapping_row = row;
28388 }
28389
28390 row->clip = fr;
28391 if (expose_line (w, row, &r))
28392 mouse_face_overwritten_p = 1;
28393 row->clip = NULL;
28394 }
28395 else if (row->overlapping_p)
28396 {
28397 /* We must redraw a row overlapping the exposed area. */
28398 if (y0 < r.y
28399 ? y0 + row->phys_height > r.y
28400 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28401 {
28402 if (first_overlapping_row == NULL)
28403 first_overlapping_row = row;
28404 last_overlapping_row = row;
28405 }
28406 }
28407
28408 if (y1 >= yb)
28409 break;
28410 }
28411
28412 /* Display the mode line if there is one. */
28413 if (WINDOW_WANTS_MODELINE_P (w)
28414 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28415 row->enabled_p)
28416 && row->y < r.y + r.height)
28417 {
28418 if (expose_line (w, row, &r))
28419 mouse_face_overwritten_p = 1;
28420 }
28421
28422 if (!w->pseudo_window_p)
28423 {
28424 /* Fix the display of overlapping rows. */
28425 if (first_overlapping_row)
28426 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28427 fr);
28428
28429 /* Draw border between windows. */
28430 x_draw_vertical_border (w);
28431
28432 /* Turn the cursor on again. */
28433 if (cursor_cleared_p
28434 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28435 update_window_cursor (w, 1);
28436 }
28437 }
28438
28439 return mouse_face_overwritten_p;
28440 }
28441
28442
28443
28444 /* Redraw (parts) of all windows in the window tree rooted at W that
28445 intersect R. R contains frame pixel coordinates. Value is
28446 non-zero if the exposure overwrites mouse-face. */
28447
28448 static int
28449 expose_window_tree (struct window *w, XRectangle *r)
28450 {
28451 struct frame *f = XFRAME (w->frame);
28452 int mouse_face_overwritten_p = 0;
28453
28454 while (w && !FRAME_GARBAGED_P (f))
28455 {
28456 if (!NILP (w->hchild))
28457 mouse_face_overwritten_p
28458 |= expose_window_tree (XWINDOW (w->hchild), r);
28459 else if (!NILP (w->vchild))
28460 mouse_face_overwritten_p
28461 |= expose_window_tree (XWINDOW (w->vchild), r);
28462 else
28463 mouse_face_overwritten_p |= expose_window (w, r);
28464
28465 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28466 }
28467
28468 return mouse_face_overwritten_p;
28469 }
28470
28471
28472 /* EXPORT:
28473 Redisplay an exposed area of frame F. X and Y are the upper-left
28474 corner of the exposed rectangle. W and H are width and height of
28475 the exposed area. All are pixel values. W or H zero means redraw
28476 the entire frame. */
28477
28478 void
28479 expose_frame (struct frame *f, int x, int y, int w, int h)
28480 {
28481 XRectangle r;
28482 int mouse_face_overwritten_p = 0;
28483
28484 TRACE ((stderr, "expose_frame "));
28485
28486 /* No need to redraw if frame will be redrawn soon. */
28487 if (FRAME_GARBAGED_P (f))
28488 {
28489 TRACE ((stderr, " garbaged\n"));
28490 return;
28491 }
28492
28493 /* If basic faces haven't been realized yet, there is no point in
28494 trying to redraw anything. This can happen when we get an expose
28495 event while Emacs is starting, e.g. by moving another window. */
28496 if (FRAME_FACE_CACHE (f) == NULL
28497 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28498 {
28499 TRACE ((stderr, " no faces\n"));
28500 return;
28501 }
28502
28503 if (w == 0 || h == 0)
28504 {
28505 r.x = r.y = 0;
28506 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28507 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28508 }
28509 else
28510 {
28511 r.x = x;
28512 r.y = y;
28513 r.width = w;
28514 r.height = h;
28515 }
28516
28517 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28518 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28519
28520 if (WINDOWP (f->tool_bar_window))
28521 mouse_face_overwritten_p
28522 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28523
28524 #ifdef HAVE_X_WINDOWS
28525 #ifndef MSDOS
28526 #ifndef USE_X_TOOLKIT
28527 if (WINDOWP (f->menu_bar_window))
28528 mouse_face_overwritten_p
28529 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28530 #endif /* not USE_X_TOOLKIT */
28531 #endif
28532 #endif
28533
28534 /* Some window managers support a focus-follows-mouse style with
28535 delayed raising of frames. Imagine a partially obscured frame,
28536 and moving the mouse into partially obscured mouse-face on that
28537 frame. The visible part of the mouse-face will be highlighted,
28538 then the WM raises the obscured frame. With at least one WM, KDE
28539 2.1, Emacs is not getting any event for the raising of the frame
28540 (even tried with SubstructureRedirectMask), only Expose events.
28541 These expose events will draw text normally, i.e. not
28542 highlighted. Which means we must redo the highlight here.
28543 Subsume it under ``we love X''. --gerd 2001-08-15 */
28544 /* Included in Windows version because Windows most likely does not
28545 do the right thing if any third party tool offers
28546 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28547 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28548 {
28549 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28550 if (f == hlinfo->mouse_face_mouse_frame)
28551 {
28552 int mouse_x = hlinfo->mouse_face_mouse_x;
28553 int mouse_y = hlinfo->mouse_face_mouse_y;
28554 clear_mouse_face (hlinfo);
28555 note_mouse_highlight (f, mouse_x, mouse_y);
28556 }
28557 }
28558 }
28559
28560
28561 /* EXPORT:
28562 Determine the intersection of two rectangles R1 and R2. Return
28563 the intersection in *RESULT. Value is non-zero if RESULT is not
28564 empty. */
28565
28566 int
28567 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28568 {
28569 XRectangle *left, *right;
28570 XRectangle *upper, *lower;
28571 int intersection_p = 0;
28572
28573 /* Rearrange so that R1 is the left-most rectangle. */
28574 if (r1->x < r2->x)
28575 left = r1, right = r2;
28576 else
28577 left = r2, right = r1;
28578
28579 /* X0 of the intersection is right.x0, if this is inside R1,
28580 otherwise there is no intersection. */
28581 if (right->x <= left->x + left->width)
28582 {
28583 result->x = right->x;
28584
28585 /* The right end of the intersection is the minimum of
28586 the right ends of left and right. */
28587 result->width = (min (left->x + left->width, right->x + right->width)
28588 - result->x);
28589
28590 /* Same game for Y. */
28591 if (r1->y < r2->y)
28592 upper = r1, lower = r2;
28593 else
28594 upper = r2, lower = r1;
28595
28596 /* The upper end of the intersection is lower.y0, if this is inside
28597 of upper. Otherwise, there is no intersection. */
28598 if (lower->y <= upper->y + upper->height)
28599 {
28600 result->y = lower->y;
28601
28602 /* The lower end of the intersection is the minimum of the lower
28603 ends of upper and lower. */
28604 result->height = (min (lower->y + lower->height,
28605 upper->y + upper->height)
28606 - result->y);
28607 intersection_p = 1;
28608 }
28609 }
28610
28611 return intersection_p;
28612 }
28613
28614 #endif /* HAVE_WINDOW_SYSTEM */
28615
28616 \f
28617 /***********************************************************************
28618 Initialization
28619 ***********************************************************************/
28620
28621 void
28622 syms_of_xdisp (void)
28623 {
28624 Vwith_echo_area_save_vector = Qnil;
28625 staticpro (&Vwith_echo_area_save_vector);
28626
28627 Vmessage_stack = Qnil;
28628 staticpro (&Vmessage_stack);
28629
28630 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28631
28632 message_dolog_marker1 = Fmake_marker ();
28633 staticpro (&message_dolog_marker1);
28634 message_dolog_marker2 = Fmake_marker ();
28635 staticpro (&message_dolog_marker2);
28636 message_dolog_marker3 = Fmake_marker ();
28637 staticpro (&message_dolog_marker3);
28638
28639 #ifdef GLYPH_DEBUG
28640 defsubr (&Sdump_frame_glyph_matrix);
28641 defsubr (&Sdump_glyph_matrix);
28642 defsubr (&Sdump_glyph_row);
28643 defsubr (&Sdump_tool_bar_row);
28644 defsubr (&Strace_redisplay);
28645 defsubr (&Strace_to_stderr);
28646 #endif
28647 #ifdef HAVE_WINDOW_SYSTEM
28648 defsubr (&Stool_bar_lines_needed);
28649 defsubr (&Slookup_image_map);
28650 #endif
28651 defsubr (&Sformat_mode_line);
28652 defsubr (&Sinvisible_p);
28653 defsubr (&Scurrent_bidi_paragraph_direction);
28654
28655 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28656 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28657 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28658 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28659 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28660 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28661 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28662 DEFSYM (Qeval, "eval");
28663 DEFSYM (QCdata, ":data");
28664 DEFSYM (Qdisplay, "display");
28665 DEFSYM (Qspace_width, "space-width");
28666 DEFSYM (Qraise, "raise");
28667 DEFSYM (Qslice, "slice");
28668 DEFSYM (Qspace, "space");
28669 DEFSYM (Qmargin, "margin");
28670 DEFSYM (Qpointer, "pointer");
28671 DEFSYM (Qleft_margin, "left-margin");
28672 DEFSYM (Qright_margin, "right-margin");
28673 DEFSYM (Qcenter, "center");
28674 DEFSYM (Qline_height, "line-height");
28675 DEFSYM (QCalign_to, ":align-to");
28676 DEFSYM (QCrelative_width, ":relative-width");
28677 DEFSYM (QCrelative_height, ":relative-height");
28678 DEFSYM (QCeval, ":eval");
28679 DEFSYM (QCpropertize, ":propertize");
28680 DEFSYM (QCfile, ":file");
28681 DEFSYM (Qfontified, "fontified");
28682 DEFSYM (Qfontification_functions, "fontification-functions");
28683 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28684 DEFSYM (Qescape_glyph, "escape-glyph");
28685 DEFSYM (Qnobreak_space, "nobreak-space");
28686 DEFSYM (Qimage, "image");
28687 DEFSYM (Qtext, "text");
28688 DEFSYM (Qboth, "both");
28689 DEFSYM (Qboth_horiz, "both-horiz");
28690 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28691 DEFSYM (QCmap, ":map");
28692 DEFSYM (QCpointer, ":pointer");
28693 DEFSYM (Qrect, "rect");
28694 DEFSYM (Qcircle, "circle");
28695 DEFSYM (Qpoly, "poly");
28696 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28697 DEFSYM (Qgrow_only, "grow-only");
28698 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28699 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28700 DEFSYM (Qposition, "position");
28701 DEFSYM (Qbuffer_position, "buffer-position");
28702 DEFSYM (Qobject, "object");
28703 DEFSYM (Qbar, "bar");
28704 DEFSYM (Qhbar, "hbar");
28705 DEFSYM (Qbox, "box");
28706 DEFSYM (Qhollow, "hollow");
28707 DEFSYM (Qhand, "hand");
28708 DEFSYM (Qarrow, "arrow");
28709 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28710
28711 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28712 Fcons (intern_c_string ("void-variable"), Qnil)),
28713 Qnil);
28714 staticpro (&list_of_error);
28715
28716 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28717 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28718 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28719 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28720
28721 echo_buffer[0] = echo_buffer[1] = Qnil;
28722 staticpro (&echo_buffer[0]);
28723 staticpro (&echo_buffer[1]);
28724
28725 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28726 staticpro (&echo_area_buffer[0]);
28727 staticpro (&echo_area_buffer[1]);
28728
28729 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28730 staticpro (&Vmessages_buffer_name);
28731
28732 mode_line_proptrans_alist = Qnil;
28733 staticpro (&mode_line_proptrans_alist);
28734 mode_line_string_list = Qnil;
28735 staticpro (&mode_line_string_list);
28736 mode_line_string_face = Qnil;
28737 staticpro (&mode_line_string_face);
28738 mode_line_string_face_prop = Qnil;
28739 staticpro (&mode_line_string_face_prop);
28740 Vmode_line_unwind_vector = Qnil;
28741 staticpro (&Vmode_line_unwind_vector);
28742
28743 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28744
28745 help_echo_string = Qnil;
28746 staticpro (&help_echo_string);
28747 help_echo_object = Qnil;
28748 staticpro (&help_echo_object);
28749 help_echo_window = Qnil;
28750 staticpro (&help_echo_window);
28751 previous_help_echo_string = Qnil;
28752 staticpro (&previous_help_echo_string);
28753 help_echo_pos = -1;
28754
28755 DEFSYM (Qright_to_left, "right-to-left");
28756 DEFSYM (Qleft_to_right, "left-to-right");
28757
28758 #ifdef HAVE_WINDOW_SYSTEM
28759 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28760 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28761 For example, if a block cursor is over a tab, it will be drawn as
28762 wide as that tab on the display. */);
28763 x_stretch_cursor_p = 0;
28764 #endif
28765
28766 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28767 doc: /* Non-nil means highlight trailing whitespace.
28768 The face used for trailing whitespace is `trailing-whitespace'. */);
28769 Vshow_trailing_whitespace = Qnil;
28770
28771 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28772 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28773 If the value is t, Emacs highlights non-ASCII chars which have the
28774 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28775 or `escape-glyph' face respectively.
28776
28777 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28778 U+2011 (non-breaking hyphen) are affected.
28779
28780 Any other non-nil value means to display these characters as a escape
28781 glyph followed by an ordinary space or hyphen.
28782
28783 A value of nil means no special handling of these characters. */);
28784 Vnobreak_char_display = Qt;
28785
28786 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28787 doc: /* The pointer shape to show in void text areas.
28788 A value of nil means to show the text pointer. Other options are `arrow',
28789 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28790 Vvoid_text_area_pointer = Qarrow;
28791
28792 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28793 doc: /* Non-nil means don't actually do any redisplay.
28794 This is used for internal purposes. */);
28795 Vinhibit_redisplay = Qnil;
28796
28797 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28798 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28799 Vglobal_mode_string = Qnil;
28800
28801 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28802 doc: /* Marker for where to display an arrow on top of the buffer text.
28803 This must be the beginning of a line in order to work.
28804 See also `overlay-arrow-string'. */);
28805 Voverlay_arrow_position = Qnil;
28806
28807 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28808 doc: /* String to display as an arrow in non-window frames.
28809 See also `overlay-arrow-position'. */);
28810 Voverlay_arrow_string = build_pure_c_string ("=>");
28811
28812 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28813 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28814 The symbols on this list are examined during redisplay to determine
28815 where to display overlay arrows. */);
28816 Voverlay_arrow_variable_list
28817 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28818
28819 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28820 doc: /* The number of lines to try scrolling a window by when point moves out.
28821 If that fails to bring point back on frame, point is centered instead.
28822 If this is zero, point is always centered after it moves off frame.
28823 If you want scrolling to always be a line at a time, you should set
28824 `scroll-conservatively' to a large value rather than set this to 1. */);
28825
28826 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28827 doc: /* Scroll up to this many lines, to bring point back on screen.
28828 If point moves off-screen, redisplay will scroll by up to
28829 `scroll-conservatively' lines in order to bring point just barely
28830 onto the screen again. If that cannot be done, then redisplay
28831 recenters point as usual.
28832
28833 If the value is greater than 100, redisplay will never recenter point,
28834 but will always scroll just enough text to bring point into view, even
28835 if you move far away.
28836
28837 A value of zero means always recenter point if it moves off screen. */);
28838 scroll_conservatively = 0;
28839
28840 DEFVAR_INT ("scroll-margin", scroll_margin,
28841 doc: /* Number of lines of margin at the top and bottom of a window.
28842 Recenter the window whenever point gets within this many lines
28843 of the top or bottom of the window. */);
28844 scroll_margin = 0;
28845
28846 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28847 doc: /* Pixels per inch value for non-window system displays.
28848 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28849 Vdisplay_pixels_per_inch = make_float (72.0);
28850
28851 #ifdef GLYPH_DEBUG
28852 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28853 #endif
28854
28855 DEFVAR_LISP ("truncate-partial-width-windows",
28856 Vtruncate_partial_width_windows,
28857 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28858 For an integer value, truncate lines in each window narrower than the
28859 full frame width, provided the window width is less than that integer;
28860 otherwise, respect the value of `truncate-lines'.
28861
28862 For any other non-nil value, truncate lines in all windows that do
28863 not span the full frame width.
28864
28865 A value of nil means to respect the value of `truncate-lines'.
28866
28867 If `word-wrap' is enabled, you might want to reduce this. */);
28868 Vtruncate_partial_width_windows = make_number (50);
28869
28870 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28871 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28872 Any other value means to use the appropriate face, `mode-line',
28873 `header-line', or `menu' respectively. */);
28874 mode_line_inverse_video = 1;
28875
28876 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28877 doc: /* Maximum buffer size for which line number should be displayed.
28878 If the buffer is bigger than this, the line number does not appear
28879 in the mode line. A value of nil means no limit. */);
28880 Vline_number_display_limit = Qnil;
28881
28882 DEFVAR_INT ("line-number-display-limit-width",
28883 line_number_display_limit_width,
28884 doc: /* Maximum line width (in characters) for line number display.
28885 If the average length of the lines near point is bigger than this, then the
28886 line number may be omitted from the mode line. */);
28887 line_number_display_limit_width = 200;
28888
28889 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28890 doc: /* Non-nil means highlight region even in nonselected windows. */);
28891 highlight_nonselected_windows = 0;
28892
28893 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28894 doc: /* Non-nil if more than one frame is visible on this display.
28895 Minibuffer-only frames don't count, but iconified frames do.
28896 This variable is not guaranteed to be accurate except while processing
28897 `frame-title-format' and `icon-title-format'. */);
28898
28899 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28900 doc: /* Template for displaying the title bar of visible frames.
28901 \(Assuming the window manager supports this feature.)
28902
28903 This variable has the same structure as `mode-line-format', except that
28904 the %c and %l constructs are ignored. It is used only on frames for
28905 which no explicit name has been set \(see `modify-frame-parameters'). */);
28906
28907 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28908 doc: /* Template for displaying the title bar of an iconified frame.
28909 \(Assuming the window manager supports this feature.)
28910 This variable has the same structure as `mode-line-format' (which see),
28911 and is used only on frames for which no explicit name has been set
28912 \(see `modify-frame-parameters'). */);
28913 Vicon_title_format
28914 = Vframe_title_format
28915 = pure_cons (intern_c_string ("multiple-frames"),
28916 pure_cons (build_pure_c_string ("%b"),
28917 pure_cons (pure_cons (empty_unibyte_string,
28918 pure_cons (intern_c_string ("invocation-name"),
28919 pure_cons (build_pure_c_string ("@"),
28920 pure_cons (intern_c_string ("system-name"),
28921 Qnil)))),
28922 Qnil)));
28923
28924 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28925 doc: /* Maximum number of lines to keep in the message log buffer.
28926 If nil, disable message logging. If t, log messages but don't truncate
28927 the buffer when it becomes large. */);
28928 Vmessage_log_max = make_number (100);
28929
28930 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28931 doc: /* Functions called before redisplay, if window sizes have changed.
28932 The value should be a list of functions that take one argument.
28933 Just before redisplay, for each frame, if any of its windows have changed
28934 size since the last redisplay, or have been split or deleted,
28935 all the functions in the list are called, with the frame as argument. */);
28936 Vwindow_size_change_functions = Qnil;
28937
28938 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28939 doc: /* List of functions to call before redisplaying a window with scrolling.
28940 Each function is called with two arguments, the window and its new
28941 display-start position. Note that these functions are also called by
28942 `set-window-buffer'. Also note that the value of `window-end' is not
28943 valid when these functions are called.
28944
28945 Warning: Do not use this feature to alter the way the window
28946 is scrolled. It is not designed for that, and such use probably won't
28947 work. */);
28948 Vwindow_scroll_functions = Qnil;
28949
28950 DEFVAR_LISP ("window-text-change-functions",
28951 Vwindow_text_change_functions,
28952 doc: /* Functions to call in redisplay when text in the window might change. */);
28953 Vwindow_text_change_functions = Qnil;
28954
28955 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28956 doc: /* Functions called when redisplay of a window reaches the end trigger.
28957 Each function is called with two arguments, the window and the end trigger value.
28958 See `set-window-redisplay-end-trigger'. */);
28959 Vredisplay_end_trigger_functions = Qnil;
28960
28961 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28962 doc: /* Non-nil means autoselect window with mouse pointer.
28963 If nil, do not autoselect windows.
28964 A positive number means delay autoselection by that many seconds: a
28965 window is autoselected only after the mouse has remained in that
28966 window for the duration of the delay.
28967 A negative number has a similar effect, but causes windows to be
28968 autoselected only after the mouse has stopped moving. \(Because of
28969 the way Emacs compares mouse events, you will occasionally wait twice
28970 that time before the window gets selected.\)
28971 Any other value means to autoselect window instantaneously when the
28972 mouse pointer enters it.
28973
28974 Autoselection selects the minibuffer only if it is active, and never
28975 unselects the minibuffer if it is active.
28976
28977 When customizing this variable make sure that the actual value of
28978 `focus-follows-mouse' matches the behavior of your window manager. */);
28979 Vmouse_autoselect_window = Qnil;
28980
28981 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28982 doc: /* Non-nil means automatically resize tool-bars.
28983 This dynamically changes the tool-bar's height to the minimum height
28984 that is needed to make all tool-bar items visible.
28985 If value is `grow-only', the tool-bar's height is only increased
28986 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28987 Vauto_resize_tool_bars = Qt;
28988
28989 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28990 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28991 auto_raise_tool_bar_buttons_p = 1;
28992
28993 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28994 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28995 make_cursor_line_fully_visible_p = 1;
28996
28997 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28998 doc: /* Border below tool-bar in pixels.
28999 If an integer, use it as the height of the border.
29000 If it is one of `internal-border-width' or `border-width', use the
29001 value of the corresponding frame parameter.
29002 Otherwise, no border is added below the tool-bar. */);
29003 Vtool_bar_border = Qinternal_border_width;
29004
29005 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29006 doc: /* Margin around tool-bar buttons in pixels.
29007 If an integer, use that for both horizontal and vertical margins.
29008 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29009 HORZ specifying the horizontal margin, and VERT specifying the
29010 vertical margin. */);
29011 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29012
29013 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29014 doc: /* Relief thickness of tool-bar buttons. */);
29015 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29016
29017 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29018 doc: /* Tool bar style to use.
29019 It can be one of
29020 image - show images only
29021 text - show text only
29022 both - show both, text below image
29023 both-horiz - show text to the right of the image
29024 text-image-horiz - show text to the left of the image
29025 any other - use system default or image if no system default.
29026
29027 This variable only affects the GTK+ toolkit version of Emacs. */);
29028 Vtool_bar_style = Qnil;
29029
29030 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29031 doc: /* Maximum number of characters a label can have to be shown.
29032 The tool bar style must also show labels for this to have any effect, see
29033 `tool-bar-style'. */);
29034 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29035
29036 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29037 doc: /* List of functions to call to fontify regions of text.
29038 Each function is called with one argument POS. Functions must
29039 fontify a region starting at POS in the current buffer, and give
29040 fontified regions the property `fontified'. */);
29041 Vfontification_functions = Qnil;
29042 Fmake_variable_buffer_local (Qfontification_functions);
29043
29044 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29045 unibyte_display_via_language_environment,
29046 doc: /* Non-nil means display unibyte text according to language environment.
29047 Specifically, this means that raw bytes in the range 160-255 decimal
29048 are displayed by converting them to the equivalent multibyte characters
29049 according to the current language environment. As a result, they are
29050 displayed according to the current fontset.
29051
29052 Note that this variable affects only how these bytes are displayed,
29053 but does not change the fact they are interpreted as raw bytes. */);
29054 unibyte_display_via_language_environment = 0;
29055
29056 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29057 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29058 If a float, it specifies a fraction of the mini-window frame's height.
29059 If an integer, it specifies a number of lines. */);
29060 Vmax_mini_window_height = make_float (0.25);
29061
29062 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29063 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29064 A value of nil means don't automatically resize mini-windows.
29065 A value of t means resize them to fit the text displayed in them.
29066 A value of `grow-only', the default, means let mini-windows grow only;
29067 they return to their normal size when the minibuffer is closed, or the
29068 echo area becomes empty. */);
29069 Vresize_mini_windows = Qgrow_only;
29070
29071 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29072 doc: /* Alist specifying how to blink the cursor off.
29073 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29074 `cursor-type' frame-parameter or variable equals ON-STATE,
29075 comparing using `equal', Emacs uses OFF-STATE to specify
29076 how to blink it off. ON-STATE and OFF-STATE are values for
29077 the `cursor-type' frame parameter.
29078
29079 If a frame's ON-STATE has no entry in this list,
29080 the frame's other specifications determine how to blink the cursor off. */);
29081 Vblink_cursor_alist = Qnil;
29082
29083 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29084 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29085 If non-nil, windows are automatically scrolled horizontally to make
29086 point visible. */);
29087 automatic_hscrolling_p = 1;
29088 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29089
29090 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29091 doc: /* How many columns away from the window edge point is allowed to get
29092 before automatic hscrolling will horizontally scroll the window. */);
29093 hscroll_margin = 5;
29094
29095 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29096 doc: /* How many columns to scroll the window when point gets too close to the edge.
29097 When point is less than `hscroll-margin' columns from the window
29098 edge, automatic hscrolling will scroll the window by the amount of columns
29099 determined by this variable. If its value is a positive integer, scroll that
29100 many columns. If it's a positive floating-point number, it specifies the
29101 fraction of the window's width to scroll. If it's nil or zero, point will be
29102 centered horizontally after the scroll. Any other value, including negative
29103 numbers, are treated as if the value were zero.
29104
29105 Automatic hscrolling always moves point outside the scroll margin, so if
29106 point was more than scroll step columns inside the margin, the window will
29107 scroll more than the value given by the scroll step.
29108
29109 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29110 and `scroll-right' overrides this variable's effect. */);
29111 Vhscroll_step = make_number (0);
29112
29113 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29114 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29115 Bind this around calls to `message' to let it take effect. */);
29116 message_truncate_lines = 0;
29117
29118 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29119 doc: /* Normal hook run to update the menu bar definitions.
29120 Redisplay runs this hook before it redisplays the menu bar.
29121 This is used to update submenus such as Buffers,
29122 whose contents depend on various data. */);
29123 Vmenu_bar_update_hook = Qnil;
29124
29125 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29126 doc: /* Frame for which we are updating a menu.
29127 The enable predicate for a menu binding should check this variable. */);
29128 Vmenu_updating_frame = Qnil;
29129
29130 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29131 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29132 inhibit_menubar_update = 0;
29133
29134 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29135 doc: /* Prefix prepended to all continuation lines at display time.
29136 The value may be a string, an image, or a stretch-glyph; it is
29137 interpreted in the same way as the value of a `display' text property.
29138
29139 This variable is overridden by any `wrap-prefix' text or overlay
29140 property.
29141
29142 To add a prefix to non-continuation lines, use `line-prefix'. */);
29143 Vwrap_prefix = Qnil;
29144 DEFSYM (Qwrap_prefix, "wrap-prefix");
29145 Fmake_variable_buffer_local (Qwrap_prefix);
29146
29147 DEFVAR_LISP ("line-prefix", Vline_prefix,
29148 doc: /* Prefix prepended to all non-continuation lines at display time.
29149 The value may be a string, an image, or a stretch-glyph; it is
29150 interpreted in the same way as the value of a `display' text property.
29151
29152 This variable is overridden by any `line-prefix' text or overlay
29153 property.
29154
29155 To add a prefix to continuation lines, use `wrap-prefix'. */);
29156 Vline_prefix = Qnil;
29157 DEFSYM (Qline_prefix, "line-prefix");
29158 Fmake_variable_buffer_local (Qline_prefix);
29159
29160 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29161 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29162 inhibit_eval_during_redisplay = 0;
29163
29164 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29165 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29166 inhibit_free_realized_faces = 0;
29167
29168 #ifdef GLYPH_DEBUG
29169 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29170 doc: /* Inhibit try_window_id display optimization. */);
29171 inhibit_try_window_id = 0;
29172
29173 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29174 doc: /* Inhibit try_window_reusing display optimization. */);
29175 inhibit_try_window_reusing = 0;
29176
29177 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29178 doc: /* Inhibit try_cursor_movement display optimization. */);
29179 inhibit_try_cursor_movement = 0;
29180 #endif /* GLYPH_DEBUG */
29181
29182 DEFVAR_INT ("overline-margin", overline_margin,
29183 doc: /* Space between overline and text, in pixels.
29184 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29185 margin to the character height. */);
29186 overline_margin = 2;
29187
29188 DEFVAR_INT ("underline-minimum-offset",
29189 underline_minimum_offset,
29190 doc: /* Minimum distance between baseline and underline.
29191 This can improve legibility of underlined text at small font sizes,
29192 particularly when using variable `x-use-underline-position-properties'
29193 with fonts that specify an UNDERLINE_POSITION relatively close to the
29194 baseline. The default value is 1. */);
29195 underline_minimum_offset = 1;
29196
29197 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29198 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29199 This feature only works when on a window system that can change
29200 cursor shapes. */);
29201 display_hourglass_p = 1;
29202
29203 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29204 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29205 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29206
29207 hourglass_atimer = NULL;
29208 hourglass_shown_p = 0;
29209
29210 DEFSYM (Qglyphless_char, "glyphless-char");
29211 DEFSYM (Qhex_code, "hex-code");
29212 DEFSYM (Qempty_box, "empty-box");
29213 DEFSYM (Qthin_space, "thin-space");
29214 DEFSYM (Qzero_width, "zero-width");
29215
29216 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29217 /* Intern this now in case it isn't already done.
29218 Setting this variable twice is harmless.
29219 But don't staticpro it here--that is done in alloc.c. */
29220 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29221 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29222
29223 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29224 doc: /* Char-table defining glyphless characters.
29225 Each element, if non-nil, should be one of the following:
29226 an ASCII acronym string: display this string in a box
29227 `hex-code': display the hexadecimal code of a character in a box
29228 `empty-box': display as an empty box
29229 `thin-space': display as 1-pixel width space
29230 `zero-width': don't display
29231 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29232 display method for graphical terminals and text terminals respectively.
29233 GRAPHICAL and TEXT should each have one of the values listed above.
29234
29235 The char-table has one extra slot to control the display of a character for
29236 which no font is found. This slot only takes effect on graphical terminals.
29237 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29238 `thin-space'. The default is `empty-box'. */);
29239 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29240 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29241 Qempty_box);
29242 }
29243
29244
29245 /* Initialize this module when Emacs starts. */
29246
29247 void
29248 init_xdisp (void)
29249 {
29250 current_header_line_height = current_mode_line_height = -1;
29251
29252 CHARPOS (this_line_start_pos) = 0;
29253
29254 if (!noninteractive)
29255 {
29256 struct window *m = XWINDOW (minibuf_window);
29257 Lisp_Object frame = m->frame;
29258 struct frame *f = XFRAME (frame);
29259 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29260 struct window *r = XWINDOW (root);
29261 int i;
29262
29263 echo_area_window = minibuf_window;
29264
29265 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
29266 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29267 XSETFASTINT (r->total_cols, FRAME_COLS (f));
29268 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
29269 XSETFASTINT (m->total_lines, 1);
29270 XSETFASTINT (m->total_cols, FRAME_COLS (f));
29271
29272 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29273 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29274 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29275
29276 /* The default ellipsis glyphs `...'. */
29277 for (i = 0; i < 3; ++i)
29278 default_invis_vector[i] = make_number ('.');
29279 }
29280
29281 {
29282 /* Allocate the buffer for frame titles.
29283 Also used for `format-mode-line'. */
29284 int size = 100;
29285 mode_line_noprop_buf = xmalloc (size);
29286 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29287 mode_line_noprop_ptr = mode_line_noprop_buf;
29288 mode_line_target = MODE_LINE_DISPLAY;
29289 }
29290
29291 help_echo_showing_p = 0;
29292 }
29293
29294 /* Since w32 does not support atimers, it defines its own implementation of
29295 the following three functions in w32fns.c. */
29296 #ifndef WINDOWSNT
29297
29298 /* Platform-independent portion of hourglass implementation. */
29299
29300 /* Cancel a currently active hourglass timer, and start a new one. */
29301 void
29302 start_hourglass (void)
29303 {
29304 #if defined (HAVE_WINDOW_SYSTEM)
29305 EMACS_TIME delay;
29306
29307 cancel_hourglass ();
29308
29309 if (INTEGERP (Vhourglass_delay)
29310 && XINT (Vhourglass_delay) > 0)
29311 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29312 TYPE_MAXIMUM (time_t)),
29313 0);
29314 else if (FLOATP (Vhourglass_delay)
29315 && XFLOAT_DATA (Vhourglass_delay) > 0)
29316 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29317 else
29318 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29319
29320 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29321 show_hourglass, NULL);
29322 #endif
29323 }
29324
29325
29326 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29327 shown. */
29328 void
29329 cancel_hourglass (void)
29330 {
29331 #if defined (HAVE_WINDOW_SYSTEM)
29332 if (hourglass_atimer)
29333 {
29334 cancel_atimer (hourglass_atimer);
29335 hourglass_atimer = NULL;
29336 }
29337
29338 if (hourglass_shown_p)
29339 hide_hourglass ();
29340 #endif
29341 }
29342 #endif /* ! WINDOWSNT */