Use INTERNAL_FIELD for processes.
[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 produce_special_glyphs (struct it *, enum display_element_type);
955 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
956 static int coords_in_mouse_face_p (struct window *, int, int);
957
958
959 \f
960 /***********************************************************************
961 Window display dimensions
962 ***********************************************************************/
963
964 /* Return the bottom boundary y-position for text lines in window W.
965 This is the first y position at which a line cannot start.
966 It is relative to the top of the window.
967
968 This is the height of W minus the height of a mode line, if any. */
969
970 int
971 window_text_bottom_y (struct window *w)
972 {
973 int height = WINDOW_TOTAL_HEIGHT (w);
974
975 if (WINDOW_WANTS_MODELINE_P (w))
976 height -= CURRENT_MODE_LINE_HEIGHT (w);
977 return height;
978 }
979
980 /* Return the pixel width of display area AREA of window W. AREA < 0
981 means return the total width of W, not including fringes to
982 the left and right of the window. */
983
984 int
985 window_box_width (struct window *w, int area)
986 {
987 int cols = XFASTINT (WVAR (w, total_cols));
988 int pixels = 0;
989
990 if (!w->pseudo_window_p)
991 {
992 cols -= WINDOW_SCROLL_BAR_COLS (w);
993
994 if (area == TEXT_AREA)
995 {
996 if (INTEGERP (WVAR (w, left_margin_cols)))
997 cols -= XFASTINT (WVAR (w, left_margin_cols));
998 if (INTEGERP (WVAR (w, right_margin_cols)))
999 cols -= XFASTINT (WVAR (w, right_margin_cols));
1000 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1001 }
1002 else if (area == LEFT_MARGIN_AREA)
1003 {
1004 cols = (INTEGERP (WVAR (w, left_margin_cols))
1005 ? XFASTINT (WVAR (w, left_margin_cols)) : 0);
1006 pixels = 0;
1007 }
1008 else if (area == RIGHT_MARGIN_AREA)
1009 {
1010 cols = (INTEGERP (WVAR (w, right_margin_cols))
1011 ? XFASTINT (WVAR (w, right_margin_cols)) : 0);
1012 pixels = 0;
1013 }
1014 }
1015
1016 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1017 }
1018
1019
1020 /* Return the pixel height of the display area of window W, not
1021 including mode lines of W, if any. */
1022
1023 int
1024 window_box_height (struct window *w)
1025 {
1026 struct frame *f = XFRAME (WVAR (w, frame));
1027 int height = WINDOW_TOTAL_HEIGHT (w);
1028
1029 eassert (height >= 0);
1030
1031 /* Note: the code below that determines the mode-line/header-line
1032 height is essentially the same as that contained in the macro
1033 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1034 the appropriate glyph row has its `mode_line_p' flag set,
1035 and if it doesn't, uses estimate_mode_line_height instead. */
1036
1037 if (WINDOW_WANTS_MODELINE_P (w))
1038 {
1039 struct glyph_row *ml_row
1040 = (w->current_matrix && w->current_matrix->rows
1041 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1042 : 0);
1043 if (ml_row && ml_row->mode_line_p)
1044 height -= ml_row->height;
1045 else
1046 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1047 }
1048
1049 if (WINDOW_WANTS_HEADER_LINE_P (w))
1050 {
1051 struct glyph_row *hl_row
1052 = (w->current_matrix && w->current_matrix->rows
1053 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1054 : 0);
1055 if (hl_row && hl_row->mode_line_p)
1056 height -= hl_row->height;
1057 else
1058 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1059 }
1060
1061 /* With a very small font and a mode-line that's taller than
1062 default, we might end up with a negative height. */
1063 return max (0, height);
1064 }
1065
1066 /* Return the window-relative coordinate of the left edge of display
1067 area AREA of window W. AREA < 0 means return the left edge of the
1068 whole window, to the right of the left fringe of W. */
1069
1070 int
1071 window_box_left_offset (struct window *w, int area)
1072 {
1073 int x;
1074
1075 if (w->pseudo_window_p)
1076 return 0;
1077
1078 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1079
1080 if (area == TEXT_AREA)
1081 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1082 + window_box_width (w, LEFT_MARGIN_AREA));
1083 else if (area == RIGHT_MARGIN_AREA)
1084 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1085 + window_box_width (w, LEFT_MARGIN_AREA)
1086 + window_box_width (w, TEXT_AREA)
1087 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1088 ? 0
1089 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1090 else if (area == LEFT_MARGIN_AREA
1091 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1092 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1093
1094 return x;
1095 }
1096
1097
1098 /* Return the window-relative coordinate of the right edge of display
1099 area AREA of window W. AREA < 0 means return the right edge of the
1100 whole window, to the left of the right fringe of W. */
1101
1102 int
1103 window_box_right_offset (struct window *w, int area)
1104 {
1105 return window_box_left_offset (w, area) + window_box_width (w, area);
1106 }
1107
1108 /* Return the frame-relative coordinate of the left edge of display
1109 area AREA of window W. AREA < 0 means return the left edge of the
1110 whole window, to the right of the left fringe of W. */
1111
1112 int
1113 window_box_left (struct window *w, int area)
1114 {
1115 struct frame *f = XFRAME (WVAR (w, frame));
1116 int x;
1117
1118 if (w->pseudo_window_p)
1119 return FRAME_INTERNAL_BORDER_WIDTH (f);
1120
1121 x = (WINDOW_LEFT_EDGE_X (w)
1122 + window_box_left_offset (w, area));
1123
1124 return x;
1125 }
1126
1127
1128 /* Return the frame-relative coordinate of the right edge of display
1129 area AREA of window W. AREA < 0 means return the right edge of the
1130 whole window, to the left of the right fringe of W. */
1131
1132 int
1133 window_box_right (struct window *w, int area)
1134 {
1135 return window_box_left (w, area) + window_box_width (w, area);
1136 }
1137
1138 /* Get the bounding box of the display area AREA of window W, without
1139 mode lines, in frame-relative coordinates. AREA < 0 means the
1140 whole window, not including the left and right fringes of
1141 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1142 coordinates of the upper-left corner of the box. Return in
1143 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1144
1145 void
1146 window_box (struct window *w, int area, int *box_x, int *box_y,
1147 int *box_width, int *box_height)
1148 {
1149 if (box_width)
1150 *box_width = window_box_width (w, area);
1151 if (box_height)
1152 *box_height = window_box_height (w);
1153 if (box_x)
1154 *box_x = window_box_left (w, area);
1155 if (box_y)
1156 {
1157 *box_y = WINDOW_TOP_EDGE_Y (w);
1158 if (WINDOW_WANTS_HEADER_LINE_P (w))
1159 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1160 }
1161 }
1162
1163
1164 /* Get the bounding box of the display area AREA of window W, without
1165 mode lines. AREA < 0 means the whole window, not including the
1166 left and right fringe of the window. Return in *TOP_LEFT_X
1167 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1168 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1169 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1170 box. */
1171
1172 static inline void
1173 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1174 int *bottom_right_x, int *bottom_right_y)
1175 {
1176 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1177 bottom_right_y);
1178 *bottom_right_x += *top_left_x;
1179 *bottom_right_y += *top_left_y;
1180 }
1181
1182
1183 \f
1184 /***********************************************************************
1185 Utilities
1186 ***********************************************************************/
1187
1188 /* Return the bottom y-position of the line the iterator IT is in.
1189 This can modify IT's settings. */
1190
1191 int
1192 line_bottom_y (struct it *it)
1193 {
1194 int line_height = it->max_ascent + it->max_descent;
1195 int line_top_y = it->current_y;
1196
1197 if (line_height == 0)
1198 {
1199 if (last_height)
1200 line_height = last_height;
1201 else if (IT_CHARPOS (*it) < ZV)
1202 {
1203 move_it_by_lines (it, 1);
1204 line_height = (it->max_ascent || it->max_descent
1205 ? it->max_ascent + it->max_descent
1206 : last_height);
1207 }
1208 else
1209 {
1210 struct glyph_row *row = it->glyph_row;
1211
1212 /* Use the default character height. */
1213 it->glyph_row = NULL;
1214 it->what = IT_CHARACTER;
1215 it->c = ' ';
1216 it->len = 1;
1217 PRODUCE_GLYPHS (it);
1218 line_height = it->ascent + it->descent;
1219 it->glyph_row = row;
1220 }
1221 }
1222
1223 return line_top_y + line_height;
1224 }
1225
1226 /* Subroutine of pos_visible_p below. Extracts a display string, if
1227 any, from the display spec given as its argument. */
1228 static Lisp_Object
1229 string_from_display_spec (Lisp_Object spec)
1230 {
1231 if (CONSP (spec))
1232 {
1233 while (CONSP (spec))
1234 {
1235 if (STRINGP (XCAR (spec)))
1236 return XCAR (spec);
1237 spec = XCDR (spec);
1238 }
1239 }
1240 else if (VECTORP (spec))
1241 {
1242 ptrdiff_t i;
1243
1244 for (i = 0; i < ASIZE (spec); i++)
1245 {
1246 if (STRINGP (AREF (spec, i)))
1247 return AREF (spec, i);
1248 }
1249 return Qnil;
1250 }
1251
1252 return spec;
1253 }
1254
1255
1256 /* Limit insanely large values of W->hscroll on frame F to the largest
1257 value that will still prevent first_visible_x and last_visible_x of
1258 'struct it' from overflowing an int. */
1259 static inline int
1260 window_hscroll_limited (struct window *w, struct frame *f)
1261 {
1262 ptrdiff_t window_hscroll = w->hscroll;
1263 int window_text_width = window_box_width (w, TEXT_AREA);
1264 int colwidth = FRAME_COLUMN_WIDTH (f);
1265
1266 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1267 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1268
1269 return window_hscroll;
1270 }
1271
1272 /* Return 1 if position CHARPOS is visible in window W.
1273 CHARPOS < 0 means return info about WINDOW_END position.
1274 If visible, set *X and *Y to pixel coordinates of top left corner.
1275 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1276 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1277
1278 int
1279 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1280 int *rtop, int *rbot, int *rowh, int *vpos)
1281 {
1282 struct it it;
1283 void *itdata = bidi_shelve_cache ();
1284 struct text_pos top;
1285 int visible_p = 0;
1286 struct buffer *old_buffer = NULL;
1287
1288 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1289 return visible_p;
1290
1291 if (XBUFFER (WVAR (w, buffer)) != current_buffer)
1292 {
1293 old_buffer = current_buffer;
1294 set_buffer_internal_1 (XBUFFER (WVAR (w, buffer)));
1295 }
1296
1297 SET_TEXT_POS_FROM_MARKER (top, WVAR (w, start));
1298 /* Scrolling a minibuffer window via scroll bar when the echo area
1299 shows long text sometimes resets the minibuffer contents behind
1300 our backs. */
1301 if (CHARPOS (top) > ZV)
1302 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1303
1304 /* Compute exact mode line heights. */
1305 if (WINDOW_WANTS_MODELINE_P (w))
1306 current_mode_line_height
1307 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1308 BVAR (current_buffer, mode_line_format));
1309
1310 if (WINDOW_WANTS_HEADER_LINE_P (w))
1311 current_header_line_height
1312 = display_mode_line (w, HEADER_LINE_FACE_ID,
1313 BVAR (current_buffer, header_line_format));
1314
1315 start_display (&it, w, top);
1316 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1317 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1318
1319 if (charpos >= 0
1320 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1321 && IT_CHARPOS (it) >= charpos)
1322 /* When scanning backwards under bidi iteration, move_it_to
1323 stops at or _before_ CHARPOS, because it stops at or to
1324 the _right_ of the character at CHARPOS. */
1325 || (it.bidi_p && it.bidi_it.scan_dir == -1
1326 && IT_CHARPOS (it) <= charpos)))
1327 {
1328 /* We have reached CHARPOS, or passed it. How the call to
1329 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1330 or covered by a display property, move_it_to stops at the end
1331 of the invisible text, to the right of CHARPOS. (ii) If
1332 CHARPOS is in a display vector, move_it_to stops on its last
1333 glyph. */
1334 int top_x = it.current_x;
1335 int top_y = it.current_y;
1336 /* Calling line_bottom_y may change it.method, it.position, etc. */
1337 enum it_method it_method = it.method;
1338 int bottom_y = (last_height = 0, line_bottom_y (&it));
1339 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1340
1341 if (top_y < window_top_y)
1342 visible_p = bottom_y > window_top_y;
1343 else if (top_y < it.last_visible_y)
1344 visible_p = 1;
1345 if (bottom_y >= it.last_visible_y
1346 && it.bidi_p && it.bidi_it.scan_dir == -1
1347 && IT_CHARPOS (it) < charpos)
1348 {
1349 /* When the last line of the window is scanned backwards
1350 under bidi iteration, we could be duped into thinking
1351 that we have passed CHARPOS, when in fact move_it_to
1352 simply stopped short of CHARPOS because it reached
1353 last_visible_y. To see if that's what happened, we call
1354 move_it_to again with a slightly larger vertical limit,
1355 and see if it actually moved vertically; if it did, we
1356 didn't really reach CHARPOS, which is beyond window end. */
1357 struct it save_it = it;
1358 /* Why 10? because we don't know how many canonical lines
1359 will the height of the next line(s) be. So we guess. */
1360 int ten_more_lines =
1361 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1362
1363 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1364 MOVE_TO_POS | MOVE_TO_Y);
1365 if (it.current_y > top_y)
1366 visible_p = 0;
1367
1368 it = save_it;
1369 }
1370 if (visible_p)
1371 {
1372 if (it_method == GET_FROM_DISPLAY_VECTOR)
1373 {
1374 /* We stopped on the last glyph of a display vector.
1375 Try and recompute. Hack alert! */
1376 if (charpos < 2 || top.charpos >= charpos)
1377 top_x = it.glyph_row->x;
1378 else
1379 {
1380 struct it it2;
1381 start_display (&it2, w, top);
1382 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1383 get_next_display_element (&it2);
1384 PRODUCE_GLYPHS (&it2);
1385 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1386 || it2.current_x > it2.last_visible_x)
1387 top_x = it.glyph_row->x;
1388 else
1389 {
1390 top_x = it2.current_x;
1391 top_y = it2.current_y;
1392 }
1393 }
1394 }
1395 else if (IT_CHARPOS (it) != charpos)
1396 {
1397 Lisp_Object cpos = make_number (charpos);
1398 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1399 Lisp_Object string = string_from_display_spec (spec);
1400 int newline_in_string = 0;
1401
1402 if (STRINGP (string))
1403 {
1404 const char *s = SSDATA (string);
1405 const char *e = s + SBYTES (string);
1406 while (s < e)
1407 {
1408 if (*s++ == '\n')
1409 {
1410 newline_in_string = 1;
1411 break;
1412 }
1413 }
1414 }
1415 /* The tricky code below is needed because there's a
1416 discrepancy between move_it_to and how we set cursor
1417 when the display line ends in a newline from a
1418 display string. move_it_to will stop _after_ such
1419 display strings, whereas set_cursor_from_row
1420 conspires with cursor_row_p to place the cursor on
1421 the first glyph produced from the display string. */
1422
1423 /* We have overshoot PT because it is covered by a
1424 display property whose value is a string. If the
1425 string includes embedded newlines, we are also in the
1426 wrong display line. Backtrack to the correct line,
1427 where the display string begins. */
1428 if (newline_in_string)
1429 {
1430 Lisp_Object startpos, endpos;
1431 EMACS_INT start, end;
1432 struct it it3;
1433 int it3_moved;
1434
1435 /* Find the first and the last buffer positions
1436 covered by the display string. */
1437 endpos =
1438 Fnext_single_char_property_change (cpos, Qdisplay,
1439 Qnil, Qnil);
1440 startpos =
1441 Fprevious_single_char_property_change (endpos, Qdisplay,
1442 Qnil, Qnil);
1443 start = XFASTINT (startpos);
1444 end = XFASTINT (endpos);
1445 /* Move to the last buffer position before the
1446 display property. */
1447 start_display (&it3, w, top);
1448 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1449 /* Move forward one more line if the position before
1450 the display string is a newline or if it is the
1451 rightmost character on a line that is
1452 continued or word-wrapped. */
1453 if (it3.method == GET_FROM_BUFFER
1454 && it3.c == '\n')
1455 move_it_by_lines (&it3, 1);
1456 else if (move_it_in_display_line_to (&it3, -1,
1457 it3.current_x
1458 + it3.pixel_width,
1459 MOVE_TO_X)
1460 == MOVE_LINE_CONTINUED)
1461 {
1462 move_it_by_lines (&it3, 1);
1463 /* When we are under word-wrap, the #$@%!
1464 move_it_by_lines moves 2 lines, so we need to
1465 fix that up. */
1466 if (it3.line_wrap == WORD_WRAP)
1467 move_it_by_lines (&it3, -1);
1468 }
1469
1470 /* Record the vertical coordinate of the display
1471 line where we wound up. */
1472 top_y = it3.current_y;
1473 if (it3.bidi_p)
1474 {
1475 /* When characters are reordered for display,
1476 the character displayed to the left of the
1477 display string could be _after_ the display
1478 property in the logical order. Use the
1479 smallest vertical position of these two. */
1480 start_display (&it3, w, top);
1481 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1482 if (it3.current_y < top_y)
1483 top_y = it3.current_y;
1484 }
1485 /* Move from the top of the window to the beginning
1486 of the display line where the display string
1487 begins. */
1488 start_display (&it3, w, top);
1489 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1490 /* If it3_moved stays zero after the 'while' loop
1491 below, that means we already were at a newline
1492 before the loop (e.g., the display string begins
1493 with a newline), so we don't need to (and cannot)
1494 inspect the glyphs of it3.glyph_row, because
1495 PRODUCE_GLYPHS will not produce anything for a
1496 newline, and thus it3.glyph_row stays at its
1497 stale content it got at top of the window. */
1498 it3_moved = 0;
1499 /* Finally, advance the iterator until we hit the
1500 first display element whose character position is
1501 CHARPOS, or until the first newline from the
1502 display string, which signals the end of the
1503 display line. */
1504 while (get_next_display_element (&it3))
1505 {
1506 PRODUCE_GLYPHS (&it3);
1507 if (IT_CHARPOS (it3) == charpos
1508 || ITERATOR_AT_END_OF_LINE_P (&it3))
1509 break;
1510 it3_moved = 1;
1511 set_iterator_to_next (&it3, 0);
1512 }
1513 top_x = it3.current_x - it3.pixel_width;
1514 /* Normally, we would exit the above loop because we
1515 found the display element whose character
1516 position is CHARPOS. For the contingency that we
1517 didn't, and stopped at the first newline from the
1518 display string, move back over the glyphs
1519 produced from the string, until we find the
1520 rightmost glyph not from the string. */
1521 if (it3_moved
1522 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1523 {
1524 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1525 + it3.glyph_row->used[TEXT_AREA];
1526
1527 while (EQ ((g - 1)->object, string))
1528 {
1529 --g;
1530 top_x -= g->pixel_width;
1531 }
1532 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1533 + it3.glyph_row->used[TEXT_AREA]);
1534 }
1535 }
1536 }
1537
1538 *x = top_x;
1539 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1540 *rtop = max (0, window_top_y - top_y);
1541 *rbot = max (0, bottom_y - it.last_visible_y);
1542 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1543 - max (top_y, window_top_y)));
1544 *vpos = it.vpos;
1545 }
1546 }
1547 else
1548 {
1549 /* We were asked to provide info about WINDOW_END. */
1550 struct it it2;
1551 void *it2data = NULL;
1552
1553 SAVE_IT (it2, it, it2data);
1554 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1555 move_it_by_lines (&it, 1);
1556 if (charpos < IT_CHARPOS (it)
1557 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1558 {
1559 visible_p = 1;
1560 RESTORE_IT (&it2, &it2, it2data);
1561 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1562 *x = it2.current_x;
1563 *y = it2.current_y + it2.max_ascent - it2.ascent;
1564 *rtop = max (0, -it2.current_y);
1565 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1566 - it.last_visible_y));
1567 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1568 it.last_visible_y)
1569 - max (it2.current_y,
1570 WINDOW_HEADER_LINE_HEIGHT (w))));
1571 *vpos = it2.vpos;
1572 }
1573 else
1574 bidi_unshelve_cache (it2data, 1);
1575 }
1576 bidi_unshelve_cache (itdata, 0);
1577
1578 if (old_buffer)
1579 set_buffer_internal_1 (old_buffer);
1580
1581 current_header_line_height = current_mode_line_height = -1;
1582
1583 if (visible_p && w->hscroll > 0)
1584 *x -=
1585 window_hscroll_limited (w, WINDOW_XFRAME (w))
1586 * WINDOW_FRAME_COLUMN_WIDTH (w);
1587
1588 #if 0
1589 /* Debugging code. */
1590 if (visible_p)
1591 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1592 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1593 else
1594 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1595 #endif
1596
1597 return visible_p;
1598 }
1599
1600
1601 /* Return the next character from STR. Return in *LEN the length of
1602 the character. This is like STRING_CHAR_AND_LENGTH but never
1603 returns an invalid character. If we find one, we return a `?', but
1604 with the length of the invalid character. */
1605
1606 static inline int
1607 string_char_and_length (const unsigned char *str, int *len)
1608 {
1609 int c;
1610
1611 c = STRING_CHAR_AND_LENGTH (str, *len);
1612 if (!CHAR_VALID_P (c))
1613 /* We may not change the length here because other places in Emacs
1614 don't use this function, i.e. they silently accept invalid
1615 characters. */
1616 c = '?';
1617
1618 return c;
1619 }
1620
1621
1622
1623 /* Given a position POS containing a valid character and byte position
1624 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1625
1626 static struct text_pos
1627 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1628 {
1629 eassert (STRINGP (string) && nchars >= 0);
1630
1631 if (STRING_MULTIBYTE (string))
1632 {
1633 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1634 int len;
1635
1636 while (nchars--)
1637 {
1638 string_char_and_length (p, &len);
1639 p += len;
1640 CHARPOS (pos) += 1;
1641 BYTEPOS (pos) += len;
1642 }
1643 }
1644 else
1645 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1646
1647 return pos;
1648 }
1649
1650
1651 /* Value is the text position, i.e. character and byte position,
1652 for character position CHARPOS in STRING. */
1653
1654 static inline struct text_pos
1655 string_pos (ptrdiff_t charpos, Lisp_Object string)
1656 {
1657 struct text_pos pos;
1658 eassert (STRINGP (string));
1659 eassert (charpos >= 0);
1660 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1661 return pos;
1662 }
1663
1664
1665 /* Value is a text position, i.e. character and byte position, for
1666 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1667 means recognize multibyte characters. */
1668
1669 static struct text_pos
1670 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1671 {
1672 struct text_pos pos;
1673
1674 eassert (s != NULL);
1675 eassert (charpos >= 0);
1676
1677 if (multibyte_p)
1678 {
1679 int len;
1680
1681 SET_TEXT_POS (pos, 0, 0);
1682 while (charpos--)
1683 {
1684 string_char_and_length ((const unsigned char *) s, &len);
1685 s += len;
1686 CHARPOS (pos) += 1;
1687 BYTEPOS (pos) += len;
1688 }
1689 }
1690 else
1691 SET_TEXT_POS (pos, charpos, charpos);
1692
1693 return pos;
1694 }
1695
1696
1697 /* Value is the number of characters in C string S. MULTIBYTE_P
1698 non-zero means recognize multibyte characters. */
1699
1700 static ptrdiff_t
1701 number_of_chars (const char *s, int multibyte_p)
1702 {
1703 ptrdiff_t nchars;
1704
1705 if (multibyte_p)
1706 {
1707 ptrdiff_t rest = strlen (s);
1708 int len;
1709 const unsigned char *p = (const unsigned char *) s;
1710
1711 for (nchars = 0; rest > 0; ++nchars)
1712 {
1713 string_char_and_length (p, &len);
1714 rest -= len, p += len;
1715 }
1716 }
1717 else
1718 nchars = strlen (s);
1719
1720 return nchars;
1721 }
1722
1723
1724 /* Compute byte position NEWPOS->bytepos corresponding to
1725 NEWPOS->charpos. POS is a known position in string STRING.
1726 NEWPOS->charpos must be >= POS.charpos. */
1727
1728 static void
1729 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1730 {
1731 eassert (STRINGP (string));
1732 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1733
1734 if (STRING_MULTIBYTE (string))
1735 *newpos = string_pos_nchars_ahead (pos, string,
1736 CHARPOS (*newpos) - CHARPOS (pos));
1737 else
1738 BYTEPOS (*newpos) = CHARPOS (*newpos);
1739 }
1740
1741 /* EXPORT:
1742 Return an estimation of the pixel height of mode or header lines on
1743 frame F. FACE_ID specifies what line's height to estimate. */
1744
1745 int
1746 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1747 {
1748 #ifdef HAVE_WINDOW_SYSTEM
1749 if (FRAME_WINDOW_P (f))
1750 {
1751 int height = FONT_HEIGHT (FRAME_FONT (f));
1752
1753 /* This function is called so early when Emacs starts that the face
1754 cache and mode line face are not yet initialized. */
1755 if (FRAME_FACE_CACHE (f))
1756 {
1757 struct face *face = FACE_FROM_ID (f, face_id);
1758 if (face)
1759 {
1760 if (face->font)
1761 height = FONT_HEIGHT (face->font);
1762 if (face->box_line_width > 0)
1763 height += 2 * face->box_line_width;
1764 }
1765 }
1766
1767 return height;
1768 }
1769 #endif
1770
1771 return 1;
1772 }
1773
1774 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1775 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1776 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1777 not force the value into range. */
1778
1779 void
1780 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1781 int *x, int *y, NativeRectangle *bounds, int noclip)
1782 {
1783
1784 #ifdef HAVE_WINDOW_SYSTEM
1785 if (FRAME_WINDOW_P (f))
1786 {
1787 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1788 even for negative values. */
1789 if (pix_x < 0)
1790 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1791 if (pix_y < 0)
1792 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1793
1794 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1795 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1796
1797 if (bounds)
1798 STORE_NATIVE_RECT (*bounds,
1799 FRAME_COL_TO_PIXEL_X (f, pix_x),
1800 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1801 FRAME_COLUMN_WIDTH (f) - 1,
1802 FRAME_LINE_HEIGHT (f) - 1);
1803
1804 if (!noclip)
1805 {
1806 if (pix_x < 0)
1807 pix_x = 0;
1808 else if (pix_x > FRAME_TOTAL_COLS (f))
1809 pix_x = FRAME_TOTAL_COLS (f);
1810
1811 if (pix_y < 0)
1812 pix_y = 0;
1813 else if (pix_y > FRAME_LINES (f))
1814 pix_y = FRAME_LINES (f);
1815 }
1816 }
1817 #endif
1818
1819 *x = pix_x;
1820 *y = pix_y;
1821 }
1822
1823
1824 /* Find the glyph under window-relative coordinates X/Y in window W.
1825 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1826 strings. Return in *HPOS and *VPOS the row and column number of
1827 the glyph found. Return in *AREA the glyph area containing X.
1828 Value is a pointer to the glyph found or null if X/Y is not on
1829 text, or we can't tell because W's current matrix is not up to
1830 date. */
1831
1832 static
1833 struct glyph *
1834 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1835 int *dx, int *dy, int *area)
1836 {
1837 struct glyph *glyph, *end;
1838 struct glyph_row *row = NULL;
1839 int x0, i;
1840
1841 /* Find row containing Y. Give up if some row is not enabled. */
1842 for (i = 0; i < w->current_matrix->nrows; ++i)
1843 {
1844 row = MATRIX_ROW (w->current_matrix, i);
1845 if (!row->enabled_p)
1846 return NULL;
1847 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1848 break;
1849 }
1850
1851 *vpos = i;
1852 *hpos = 0;
1853
1854 /* Give up if Y is not in the window. */
1855 if (i == w->current_matrix->nrows)
1856 return NULL;
1857
1858 /* Get the glyph area containing X. */
1859 if (w->pseudo_window_p)
1860 {
1861 *area = TEXT_AREA;
1862 x0 = 0;
1863 }
1864 else
1865 {
1866 if (x < window_box_left_offset (w, TEXT_AREA))
1867 {
1868 *area = LEFT_MARGIN_AREA;
1869 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1870 }
1871 else if (x < window_box_right_offset (w, TEXT_AREA))
1872 {
1873 *area = TEXT_AREA;
1874 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1875 }
1876 else
1877 {
1878 *area = RIGHT_MARGIN_AREA;
1879 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1880 }
1881 }
1882
1883 /* Find glyph containing X. */
1884 glyph = row->glyphs[*area];
1885 end = glyph + row->used[*area];
1886 x -= x0;
1887 while (glyph < end && x >= glyph->pixel_width)
1888 {
1889 x -= glyph->pixel_width;
1890 ++glyph;
1891 }
1892
1893 if (glyph == end)
1894 return NULL;
1895
1896 if (dx)
1897 {
1898 *dx = x;
1899 *dy = y - (row->y + row->ascent - glyph->ascent);
1900 }
1901
1902 *hpos = glyph - row->glyphs[*area];
1903 return glyph;
1904 }
1905
1906 /* Convert frame-relative x/y to coordinates relative to window W.
1907 Takes pseudo-windows into account. */
1908
1909 static void
1910 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1911 {
1912 if (w->pseudo_window_p)
1913 {
1914 /* A pseudo-window is always full-width, and starts at the
1915 left edge of the frame, plus a frame border. */
1916 struct frame *f = XFRAME (WVAR (w, frame));
1917 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1918 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1919 }
1920 else
1921 {
1922 *x -= WINDOW_LEFT_EDGE_X (w);
1923 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1924 }
1925 }
1926
1927 #ifdef HAVE_WINDOW_SYSTEM
1928
1929 /* EXPORT:
1930 Return in RECTS[] at most N clipping rectangles for glyph string S.
1931 Return the number of stored rectangles. */
1932
1933 int
1934 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1935 {
1936 XRectangle r;
1937
1938 if (n <= 0)
1939 return 0;
1940
1941 if (s->row->full_width_p)
1942 {
1943 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1944 r.x = WINDOW_LEFT_EDGE_X (s->w);
1945 r.width = WINDOW_TOTAL_WIDTH (s->w);
1946
1947 /* Unless displaying a mode or menu bar line, which are always
1948 fully visible, clip to the visible part of the row. */
1949 if (s->w->pseudo_window_p)
1950 r.height = s->row->visible_height;
1951 else
1952 r.height = s->height;
1953 }
1954 else
1955 {
1956 /* This is a text line that may be partially visible. */
1957 r.x = window_box_left (s->w, s->area);
1958 r.width = window_box_width (s->w, s->area);
1959 r.height = s->row->visible_height;
1960 }
1961
1962 if (s->clip_head)
1963 if (r.x < s->clip_head->x)
1964 {
1965 if (r.width >= s->clip_head->x - r.x)
1966 r.width -= s->clip_head->x - r.x;
1967 else
1968 r.width = 0;
1969 r.x = s->clip_head->x;
1970 }
1971 if (s->clip_tail)
1972 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1973 {
1974 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1975 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1976 else
1977 r.width = 0;
1978 }
1979
1980 /* If S draws overlapping rows, it's sufficient to use the top and
1981 bottom of the window for clipping because this glyph string
1982 intentionally draws over other lines. */
1983 if (s->for_overlaps)
1984 {
1985 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1986 r.height = window_text_bottom_y (s->w) - r.y;
1987
1988 /* Alas, the above simple strategy does not work for the
1989 environments with anti-aliased text: if the same text is
1990 drawn onto the same place multiple times, it gets thicker.
1991 If the overlap we are processing is for the erased cursor, we
1992 take the intersection with the rectangle of the cursor. */
1993 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1994 {
1995 XRectangle rc, r_save = r;
1996
1997 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1998 rc.y = s->w->phys_cursor.y;
1999 rc.width = s->w->phys_cursor_width;
2000 rc.height = s->w->phys_cursor_height;
2001
2002 x_intersect_rectangles (&r_save, &rc, &r);
2003 }
2004 }
2005 else
2006 {
2007 /* Don't use S->y for clipping because it doesn't take partially
2008 visible lines into account. For example, it can be negative for
2009 partially visible lines at the top of a window. */
2010 if (!s->row->full_width_p
2011 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2012 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2013 else
2014 r.y = max (0, s->row->y);
2015 }
2016
2017 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2018
2019 /* If drawing the cursor, don't let glyph draw outside its
2020 advertised boundaries. Cleartype does this under some circumstances. */
2021 if (s->hl == DRAW_CURSOR)
2022 {
2023 struct glyph *glyph = s->first_glyph;
2024 int height, max_y;
2025
2026 if (s->x > r.x)
2027 {
2028 r.width -= s->x - r.x;
2029 r.x = s->x;
2030 }
2031 r.width = min (r.width, glyph->pixel_width);
2032
2033 /* If r.y is below window bottom, ensure that we still see a cursor. */
2034 height = min (glyph->ascent + glyph->descent,
2035 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2036 max_y = window_text_bottom_y (s->w) - height;
2037 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2038 if (s->ybase - glyph->ascent > max_y)
2039 {
2040 r.y = max_y;
2041 r.height = height;
2042 }
2043 else
2044 {
2045 /* Don't draw cursor glyph taller than our actual glyph. */
2046 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2047 if (height < r.height)
2048 {
2049 max_y = r.y + r.height;
2050 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2051 r.height = min (max_y - r.y, height);
2052 }
2053 }
2054 }
2055
2056 if (s->row->clip)
2057 {
2058 XRectangle r_save = r;
2059
2060 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2061 r.width = 0;
2062 }
2063
2064 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2065 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2066 {
2067 #ifdef CONVERT_FROM_XRECT
2068 CONVERT_FROM_XRECT (r, *rects);
2069 #else
2070 *rects = r;
2071 #endif
2072 return 1;
2073 }
2074 else
2075 {
2076 /* If we are processing overlapping and allowed to return
2077 multiple clipping rectangles, we exclude the row of the glyph
2078 string from the clipping rectangle. This is to avoid drawing
2079 the same text on the environment with anti-aliasing. */
2080 #ifdef CONVERT_FROM_XRECT
2081 XRectangle rs[2];
2082 #else
2083 XRectangle *rs = rects;
2084 #endif
2085 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2086
2087 if (s->for_overlaps & OVERLAPS_PRED)
2088 {
2089 rs[i] = r;
2090 if (r.y + r.height > row_y)
2091 {
2092 if (r.y < row_y)
2093 rs[i].height = row_y - r.y;
2094 else
2095 rs[i].height = 0;
2096 }
2097 i++;
2098 }
2099 if (s->for_overlaps & OVERLAPS_SUCC)
2100 {
2101 rs[i] = r;
2102 if (r.y < row_y + s->row->visible_height)
2103 {
2104 if (r.y + r.height > row_y + s->row->visible_height)
2105 {
2106 rs[i].y = row_y + s->row->visible_height;
2107 rs[i].height = r.y + r.height - rs[i].y;
2108 }
2109 else
2110 rs[i].height = 0;
2111 }
2112 i++;
2113 }
2114
2115 n = i;
2116 #ifdef CONVERT_FROM_XRECT
2117 for (i = 0; i < n; i++)
2118 CONVERT_FROM_XRECT (rs[i], rects[i]);
2119 #endif
2120 return n;
2121 }
2122 }
2123
2124 /* EXPORT:
2125 Return in *NR the clipping rectangle for glyph string S. */
2126
2127 void
2128 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2129 {
2130 get_glyph_string_clip_rects (s, nr, 1);
2131 }
2132
2133
2134 /* EXPORT:
2135 Return the position and height of the phys cursor in window W.
2136 Set w->phys_cursor_width to width of phys cursor.
2137 */
2138
2139 void
2140 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2141 struct glyph *glyph, int *xp, int *yp, int *heightp)
2142 {
2143 struct frame *f = XFRAME (WINDOW_FRAME (w));
2144 int x, y, wd, h, h0, y0;
2145
2146 /* Compute the width of the rectangle to draw. If on a stretch
2147 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2148 rectangle as wide as the glyph, but use a canonical character
2149 width instead. */
2150 wd = glyph->pixel_width - 1;
2151 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2152 wd++; /* Why? */
2153 #endif
2154
2155 x = w->phys_cursor.x;
2156 if (x < 0)
2157 {
2158 wd += x;
2159 x = 0;
2160 }
2161
2162 if (glyph->type == STRETCH_GLYPH
2163 && !x_stretch_cursor_p)
2164 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2165 w->phys_cursor_width = wd;
2166
2167 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2168
2169 /* If y is below window bottom, ensure that we still see a cursor. */
2170 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2171
2172 h = max (h0, glyph->ascent + glyph->descent);
2173 h0 = min (h0, glyph->ascent + glyph->descent);
2174
2175 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2176 if (y < y0)
2177 {
2178 h = max (h - (y0 - y) + 1, h0);
2179 y = y0 - 1;
2180 }
2181 else
2182 {
2183 y0 = window_text_bottom_y (w) - h0;
2184 if (y > y0)
2185 {
2186 h += y - y0;
2187 y = y0;
2188 }
2189 }
2190
2191 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2192 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2193 *heightp = h;
2194 }
2195
2196 /*
2197 * Remember which glyph the mouse is over.
2198 */
2199
2200 void
2201 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2202 {
2203 Lisp_Object window;
2204 struct window *w;
2205 struct glyph_row *r, *gr, *end_row;
2206 enum window_part part;
2207 enum glyph_row_area area;
2208 int x, y, width, height;
2209
2210 /* Try to determine frame pixel position and size of the glyph under
2211 frame pixel coordinates X/Y on frame F. */
2212
2213 if (!f->glyphs_initialized_p
2214 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2215 NILP (window)))
2216 {
2217 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2218 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2219 goto virtual_glyph;
2220 }
2221
2222 w = XWINDOW (window);
2223 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2224 height = WINDOW_FRAME_LINE_HEIGHT (w);
2225
2226 x = window_relative_x_coord (w, part, gx);
2227 y = gy - WINDOW_TOP_EDGE_Y (w);
2228
2229 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2230 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2231
2232 if (w->pseudo_window_p)
2233 {
2234 area = TEXT_AREA;
2235 part = ON_MODE_LINE; /* Don't adjust margin. */
2236 goto text_glyph;
2237 }
2238
2239 switch (part)
2240 {
2241 case ON_LEFT_MARGIN:
2242 area = LEFT_MARGIN_AREA;
2243 goto text_glyph;
2244
2245 case ON_RIGHT_MARGIN:
2246 area = RIGHT_MARGIN_AREA;
2247 goto text_glyph;
2248
2249 case ON_HEADER_LINE:
2250 case ON_MODE_LINE:
2251 gr = (part == ON_HEADER_LINE
2252 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2253 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2254 gy = gr->y;
2255 area = TEXT_AREA;
2256 goto text_glyph_row_found;
2257
2258 case ON_TEXT:
2259 area = TEXT_AREA;
2260
2261 text_glyph:
2262 gr = 0; gy = 0;
2263 for (; r <= end_row && r->enabled_p; ++r)
2264 if (r->y + r->height > y)
2265 {
2266 gr = r; gy = r->y;
2267 break;
2268 }
2269
2270 text_glyph_row_found:
2271 if (gr && gy <= y)
2272 {
2273 struct glyph *g = gr->glyphs[area];
2274 struct glyph *end = g + gr->used[area];
2275
2276 height = gr->height;
2277 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2278 if (gx + g->pixel_width > x)
2279 break;
2280
2281 if (g < end)
2282 {
2283 if (g->type == IMAGE_GLYPH)
2284 {
2285 /* Don't remember when mouse is over image, as
2286 image may have hot-spots. */
2287 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2288 return;
2289 }
2290 width = g->pixel_width;
2291 }
2292 else
2293 {
2294 /* Use nominal char spacing at end of line. */
2295 x -= gx;
2296 gx += (x / width) * width;
2297 }
2298
2299 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2300 gx += window_box_left_offset (w, area);
2301 }
2302 else
2303 {
2304 /* Use nominal line height at end of window. */
2305 gx = (x / width) * width;
2306 y -= gy;
2307 gy += (y / height) * height;
2308 }
2309 break;
2310
2311 case ON_LEFT_FRINGE:
2312 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2313 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2314 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2315 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2316 goto row_glyph;
2317
2318 case ON_RIGHT_FRINGE:
2319 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2320 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2321 : window_box_right_offset (w, TEXT_AREA));
2322 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2323 goto row_glyph;
2324
2325 case ON_SCROLL_BAR:
2326 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2327 ? 0
2328 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2329 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2330 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2331 : 0)));
2332 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2333
2334 row_glyph:
2335 gr = 0, gy = 0;
2336 for (; r <= end_row && r->enabled_p; ++r)
2337 if (r->y + r->height > y)
2338 {
2339 gr = r; gy = r->y;
2340 break;
2341 }
2342
2343 if (gr && gy <= y)
2344 height = gr->height;
2345 else
2346 {
2347 /* Use nominal line height at end of window. */
2348 y -= gy;
2349 gy += (y / height) * height;
2350 }
2351 break;
2352
2353 default:
2354 ;
2355 virtual_glyph:
2356 /* If there is no glyph under the mouse, then we divide the screen
2357 into a grid of the smallest glyph in the frame, and use that
2358 as our "glyph". */
2359
2360 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2361 round down even for negative values. */
2362 if (gx < 0)
2363 gx -= width - 1;
2364 if (gy < 0)
2365 gy -= height - 1;
2366
2367 gx = (gx / width) * width;
2368 gy = (gy / height) * height;
2369
2370 goto store_rect;
2371 }
2372
2373 gx += WINDOW_LEFT_EDGE_X (w);
2374 gy += WINDOW_TOP_EDGE_Y (w);
2375
2376 store_rect:
2377 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2378
2379 /* Visible feedback for debugging. */
2380 #if 0
2381 #if HAVE_X_WINDOWS
2382 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2383 f->output_data.x->normal_gc,
2384 gx, gy, width, height);
2385 #endif
2386 #endif
2387 }
2388
2389
2390 #endif /* HAVE_WINDOW_SYSTEM */
2391
2392 \f
2393 /***********************************************************************
2394 Lisp form evaluation
2395 ***********************************************************************/
2396
2397 /* Error handler for safe_eval and safe_call. */
2398
2399 static Lisp_Object
2400 safe_eval_handler (Lisp_Object arg)
2401 {
2402 add_to_log ("Error during redisplay: %S", arg, Qnil);
2403 return Qnil;
2404 }
2405
2406 /* Call function FUNC with the rest of NARGS - 1 arguments
2407 following. Return the result, or nil if something went
2408 wrong. Prevent redisplay during the evaluation. */
2409
2410 Lisp_Object
2411 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2412 {
2413 Lisp_Object val;
2414
2415 if (inhibit_eval_during_redisplay)
2416 val = Qnil;
2417 else
2418 {
2419 va_list ap;
2420 ptrdiff_t i;
2421 ptrdiff_t count = SPECPDL_INDEX ();
2422 struct gcpro gcpro1;
2423 Lisp_Object *args = alloca (nargs * sizeof (Lisp_Object));
2424
2425 args[0] = func;
2426 va_start (ap, func);
2427 for (i = 1; i < nargs; i++)
2428 args[i] = va_arg (ap, Lisp_Object);
2429 va_end (ap);
2430
2431 GCPRO1 (args[0]);
2432 gcpro1.nvars = nargs;
2433 specbind (Qinhibit_redisplay, Qt);
2434 /* Use Qt to ensure debugger does not run,
2435 so there is no possibility of wanting to redisplay. */
2436 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2437 safe_eval_handler);
2438 UNGCPRO;
2439 val = unbind_to (count, val);
2440 }
2441
2442 return val;
2443 }
2444
2445
2446 /* Call function FN with one argument ARG.
2447 Return the result, or nil if something went wrong. */
2448
2449 Lisp_Object
2450 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2451 {
2452 return safe_call (2, fn, arg);
2453 }
2454
2455 static Lisp_Object Qeval;
2456
2457 Lisp_Object
2458 safe_eval (Lisp_Object sexpr)
2459 {
2460 return safe_call1 (Qeval, sexpr);
2461 }
2462
2463 /* Call function FN with two arguments ARG1 and ARG2.
2464 Return the result, or nil if something went wrong. */
2465
2466 Lisp_Object
2467 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2468 {
2469 return safe_call (3, fn, arg1, arg2);
2470 }
2471
2472
2473 \f
2474 /***********************************************************************
2475 Debugging
2476 ***********************************************************************/
2477
2478 #if 0
2479
2480 /* Define CHECK_IT to perform sanity checks on iterators.
2481 This is for debugging. It is too slow to do unconditionally. */
2482
2483 static void
2484 check_it (struct it *it)
2485 {
2486 if (it->method == GET_FROM_STRING)
2487 {
2488 eassert (STRINGP (it->string));
2489 eassert (IT_STRING_CHARPOS (*it) >= 0);
2490 }
2491 else
2492 {
2493 eassert (IT_STRING_CHARPOS (*it) < 0);
2494 if (it->method == GET_FROM_BUFFER)
2495 {
2496 /* Check that character and byte positions agree. */
2497 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2498 }
2499 }
2500
2501 if (it->dpvec)
2502 eassert (it->current.dpvec_index >= 0);
2503 else
2504 eassert (it->current.dpvec_index < 0);
2505 }
2506
2507 #define CHECK_IT(IT) check_it ((IT))
2508
2509 #else /* not 0 */
2510
2511 #define CHECK_IT(IT) (void) 0
2512
2513 #endif /* not 0 */
2514
2515
2516 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2517
2518 /* Check that the window end of window W is what we expect it
2519 to be---the last row in the current matrix displaying text. */
2520
2521 static void
2522 check_window_end (struct window *w)
2523 {
2524 if (!MINI_WINDOW_P (w)
2525 && !NILP (WVAR (w, window_end_valid)))
2526 {
2527 struct glyph_row *row;
2528 eassert ((row = MATRIX_ROW (w->current_matrix,
2529 XFASTINT (WVAR (w, window_end_vpos))),
2530 !row->enabled_p
2531 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2532 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2533 }
2534 }
2535
2536 #define CHECK_WINDOW_END(W) check_window_end ((W))
2537
2538 #else
2539
2540 #define CHECK_WINDOW_END(W) (void) 0
2541
2542 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2543
2544
2545 \f
2546 /***********************************************************************
2547 Iterator initialization
2548 ***********************************************************************/
2549
2550 /* Initialize IT for displaying current_buffer in window W, starting
2551 at character position CHARPOS. CHARPOS < 0 means that no buffer
2552 position is specified which is useful when the iterator is assigned
2553 a position later. BYTEPOS is the byte position corresponding to
2554 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2555
2556 If ROW is not null, calls to produce_glyphs with IT as parameter
2557 will produce glyphs in that row.
2558
2559 BASE_FACE_ID is the id of a base face to use. It must be one of
2560 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2561 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2562 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2563
2564 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2565 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2566 will be initialized to use the corresponding mode line glyph row of
2567 the desired matrix of W. */
2568
2569 void
2570 init_iterator (struct it *it, struct window *w,
2571 ptrdiff_t charpos, ptrdiff_t bytepos,
2572 struct glyph_row *row, enum face_id base_face_id)
2573 {
2574 int highlight_region_p;
2575 enum face_id remapped_base_face_id = base_face_id;
2576
2577 /* Some precondition checks. */
2578 eassert (w != NULL && it != NULL);
2579 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2580 && charpos <= ZV));
2581
2582 /* If face attributes have been changed since the last redisplay,
2583 free realized faces now because they depend on face definitions
2584 that might have changed. Don't free faces while there might be
2585 desired matrices pending which reference these faces. */
2586 if (face_change_count && !inhibit_free_realized_faces)
2587 {
2588 face_change_count = 0;
2589 free_all_realized_faces (Qnil);
2590 }
2591
2592 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2593 if (! NILP (Vface_remapping_alist))
2594 remapped_base_face_id
2595 = lookup_basic_face (XFRAME (WVAR (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 (WVAR (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
2664 (-1, XINT (BVAR (current_buffer, 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 (WVAR (w, redisplay_end_trigger))
2708 && XMARKER (WVAR (w, redisplay_end_trigger))->buffer != 0)
2709 it->redisplay_end_trigger_charpos
2710 = marker_position (WVAR (w, redisplay_end_trigger));
2711 else if (INTEGERP (WVAR (w, redisplay_end_trigger)))
2712 it->redisplay_end_trigger_charpos =
2713 clip_to_bounds (PTRDIFF_MIN, XINT (WVAR (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. But leave the dimensions
2736 zero for tooltip frames, as these glyphs look ugly there and also
2737 sabotage calculations of tooltip dimensions in x-show-tip. */
2738 #ifdef HAVE_WINDOW_SYSTEM
2739 if (!(FRAME_WINDOW_P (it->f)
2740 && FRAMEP (tip_frame)
2741 && it->f == XFRAME (tip_frame)))
2742 #endif
2743 {
2744 if (it->line_wrap == TRUNCATE)
2745 {
2746 /* We will need the truncation glyph. */
2747 eassert (it->glyph_row == NULL);
2748 produce_special_glyphs (it, IT_TRUNCATION);
2749 it->truncation_pixel_width = it->pixel_width;
2750 }
2751 else
2752 {
2753 /* We will need the continuation glyph. */
2754 eassert (it->glyph_row == NULL);
2755 produce_special_glyphs (it, IT_CONTINUATION);
2756 it->continuation_pixel_width = it->pixel_width;
2757 }
2758 }
2759
2760 /* Reset these values to zero because the produce_special_glyphs
2761 above has changed them. */
2762 it->pixel_width = it->ascent = it->descent = 0;
2763 it->phys_ascent = it->phys_descent = 0;
2764
2765 /* Set this after getting the dimensions of truncation and
2766 continuation glyphs, so that we don't produce glyphs when calling
2767 produce_special_glyphs, above. */
2768 it->glyph_row = row;
2769 it->area = TEXT_AREA;
2770
2771 /* Forget any previous info about this row being reversed. */
2772 if (it->glyph_row)
2773 it->glyph_row->reversed_p = 0;
2774
2775 /* Get the dimensions of the display area. The display area
2776 consists of the visible window area plus a horizontally scrolled
2777 part to the left of the window. All x-values are relative to the
2778 start of this total display area. */
2779 if (base_face_id != DEFAULT_FACE_ID)
2780 {
2781 /* Mode lines, menu bar in terminal frames. */
2782 it->first_visible_x = 0;
2783 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2784 }
2785 else
2786 {
2787 it->first_visible_x =
2788 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2789 it->last_visible_x = (it->first_visible_x
2790 + window_box_width (w, TEXT_AREA));
2791
2792 /* If we truncate lines, leave room for the truncation glyph(s) at
2793 the right margin. Otherwise, leave room for the continuation
2794 glyph(s). Done only if the window has no fringes. Since we
2795 don't know at this point whether there will be any R2L lines in
2796 the window, we reserve space for truncation/continuation glyphs
2797 even if only one of the fringes is absent. */
2798 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2799 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2800 {
2801 if (it->line_wrap == TRUNCATE)
2802 it->last_visible_x -= it->truncation_pixel_width;
2803 else
2804 it->last_visible_x -= it->continuation_pixel_width;
2805 }
2806
2807 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2808 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2809 }
2810
2811 /* Leave room for a border glyph. */
2812 if (!FRAME_WINDOW_P (it->f)
2813 && !WINDOW_RIGHTMOST_P (it->w))
2814 it->last_visible_x -= 1;
2815
2816 it->last_visible_y = window_text_bottom_y (w);
2817
2818 /* For mode lines and alike, arrange for the first glyph having a
2819 left box line if the face specifies a box. */
2820 if (base_face_id != DEFAULT_FACE_ID)
2821 {
2822 struct face *face;
2823
2824 it->face_id = remapped_base_face_id;
2825
2826 /* If we have a boxed mode line, make the first character appear
2827 with a left box line. */
2828 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2829 if (face->box != FACE_NO_BOX)
2830 it->start_of_box_run_p = 1;
2831 }
2832
2833 /* If a buffer position was specified, set the iterator there,
2834 getting overlays and face properties from that position. */
2835 if (charpos >= BUF_BEG (current_buffer))
2836 {
2837 it->end_charpos = ZV;
2838 IT_CHARPOS (*it) = charpos;
2839
2840 /* We will rely on `reseat' to set this up properly, via
2841 handle_face_prop. */
2842 it->face_id = it->base_face_id;
2843
2844 /* Compute byte position if not specified. */
2845 if (bytepos < charpos)
2846 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2847 else
2848 IT_BYTEPOS (*it) = bytepos;
2849
2850 it->start = it->current;
2851 /* Do we need to reorder bidirectional text? Not if this is a
2852 unibyte buffer: by definition, none of the single-byte
2853 characters are strong R2L, so no reordering is needed. And
2854 bidi.c doesn't support unibyte buffers anyway. Also, don't
2855 reorder while we are loading loadup.el, since the tables of
2856 character properties needed for reordering are not yet
2857 available. */
2858 it->bidi_p =
2859 NILP (Vpurify_flag)
2860 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2861 && it->multibyte_p;
2862
2863 /* If we are to reorder bidirectional text, init the bidi
2864 iterator. */
2865 if (it->bidi_p)
2866 {
2867 /* Note the paragraph direction that this buffer wants to
2868 use. */
2869 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2870 Qleft_to_right))
2871 it->paragraph_embedding = L2R;
2872 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2873 Qright_to_left))
2874 it->paragraph_embedding = R2L;
2875 else
2876 it->paragraph_embedding = NEUTRAL_DIR;
2877 bidi_unshelve_cache (NULL, 0);
2878 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2879 &it->bidi_it);
2880 }
2881
2882 /* Compute faces etc. */
2883 reseat (it, it->current.pos, 1);
2884 }
2885
2886 CHECK_IT (it);
2887 }
2888
2889
2890 /* Initialize IT for the display of window W with window start POS. */
2891
2892 void
2893 start_display (struct it *it, struct window *w, struct text_pos pos)
2894 {
2895 struct glyph_row *row;
2896 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2897
2898 row = w->desired_matrix->rows + first_vpos;
2899 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2900 it->first_vpos = first_vpos;
2901
2902 /* Don't reseat to previous visible line start if current start
2903 position is in a string or image. */
2904 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2905 {
2906 int start_at_line_beg_p;
2907 int first_y = it->current_y;
2908
2909 /* If window start is not at a line start, skip forward to POS to
2910 get the correct continuation lines width. */
2911 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2912 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2913 if (!start_at_line_beg_p)
2914 {
2915 int new_x;
2916
2917 reseat_at_previous_visible_line_start (it);
2918 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2919
2920 new_x = it->current_x + it->pixel_width;
2921
2922 /* If lines are continued, this line may end in the middle
2923 of a multi-glyph character (e.g. a control character
2924 displayed as \003, or in the middle of an overlay
2925 string). In this case move_it_to above will not have
2926 taken us to the start of the continuation line but to the
2927 end of the continued line. */
2928 if (it->current_x > 0
2929 && it->line_wrap != TRUNCATE /* Lines are continued. */
2930 && (/* And glyph doesn't fit on the line. */
2931 new_x > it->last_visible_x
2932 /* Or it fits exactly and we're on a window
2933 system frame. */
2934 || (new_x == it->last_visible_x
2935 && FRAME_WINDOW_P (it->f)
2936 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2937 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2938 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2939 {
2940 if ((it->current.dpvec_index >= 0
2941 || it->current.overlay_string_index >= 0)
2942 /* If we are on a newline from a display vector or
2943 overlay string, then we are already at the end of
2944 a screen line; no need to go to the next line in
2945 that case, as this line is not really continued.
2946 (If we do go to the next line, C-e will not DTRT.) */
2947 && it->c != '\n')
2948 {
2949 set_iterator_to_next (it, 1);
2950 move_it_in_display_line_to (it, -1, -1, 0);
2951 }
2952
2953 it->continuation_lines_width += it->current_x;
2954 }
2955 /* If the character at POS is displayed via a display
2956 vector, move_it_to above stops at the final glyph of
2957 IT->dpvec. To make the caller redisplay that character
2958 again (a.k.a. start at POS), we need to reset the
2959 dpvec_index to the beginning of IT->dpvec. */
2960 else if (it->current.dpvec_index >= 0)
2961 it->current.dpvec_index = 0;
2962
2963 /* We're starting a new display line, not affected by the
2964 height of the continued line, so clear the appropriate
2965 fields in the iterator structure. */
2966 it->max_ascent = it->max_descent = 0;
2967 it->max_phys_ascent = it->max_phys_descent = 0;
2968
2969 it->current_y = first_y;
2970 it->vpos = 0;
2971 it->current_x = it->hpos = 0;
2972 }
2973 }
2974 }
2975
2976
2977 /* Return 1 if POS is a position in ellipses displayed for invisible
2978 text. W is the window we display, for text property lookup. */
2979
2980 static int
2981 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2982 {
2983 Lisp_Object prop, window;
2984 int ellipses_p = 0;
2985 ptrdiff_t charpos = CHARPOS (pos->pos);
2986
2987 /* If POS specifies a position in a display vector, this might
2988 be for an ellipsis displayed for invisible text. We won't
2989 get the iterator set up for delivering that ellipsis unless
2990 we make sure that it gets aware of the invisible text. */
2991 if (pos->dpvec_index >= 0
2992 && pos->overlay_string_index < 0
2993 && CHARPOS (pos->string_pos) < 0
2994 && charpos > BEGV
2995 && (XSETWINDOW (window, w),
2996 prop = Fget_char_property (make_number (charpos),
2997 Qinvisible, window),
2998 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2999 {
3000 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3001 window);
3002 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3003 }
3004
3005 return ellipses_p;
3006 }
3007
3008
3009 /* Initialize IT for stepping through current_buffer in window W,
3010 starting at position POS that includes overlay string and display
3011 vector/ control character translation position information. Value
3012 is zero if there are overlay strings with newlines at POS. */
3013
3014 static int
3015 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3016 {
3017 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3018 int i, overlay_strings_with_newlines = 0;
3019
3020 /* If POS specifies a position in a display vector, this might
3021 be for an ellipsis displayed for invisible text. We won't
3022 get the iterator set up for delivering that ellipsis unless
3023 we make sure that it gets aware of the invisible text. */
3024 if (in_ellipses_for_invisible_text_p (pos, w))
3025 {
3026 --charpos;
3027 bytepos = 0;
3028 }
3029
3030 /* Keep in mind: the call to reseat in init_iterator skips invisible
3031 text, so we might end up at a position different from POS. This
3032 is only a problem when POS is a row start after a newline and an
3033 overlay starts there with an after-string, and the overlay has an
3034 invisible property. Since we don't skip invisible text in
3035 display_line and elsewhere immediately after consuming the
3036 newline before the row start, such a POS will not be in a string,
3037 but the call to init_iterator below will move us to the
3038 after-string. */
3039 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3040
3041 /* This only scans the current chunk -- it should scan all chunks.
3042 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3043 to 16 in 22.1 to make this a lesser problem. */
3044 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3045 {
3046 const char *s = SSDATA (it->overlay_strings[i]);
3047 const char *e = s + SBYTES (it->overlay_strings[i]);
3048
3049 while (s < e && *s != '\n')
3050 ++s;
3051
3052 if (s < e)
3053 {
3054 overlay_strings_with_newlines = 1;
3055 break;
3056 }
3057 }
3058
3059 /* If position is within an overlay string, set up IT to the right
3060 overlay string. */
3061 if (pos->overlay_string_index >= 0)
3062 {
3063 int relative_index;
3064
3065 /* If the first overlay string happens to have a `display'
3066 property for an image, the iterator will be set up for that
3067 image, and we have to undo that setup first before we can
3068 correct the overlay string index. */
3069 if (it->method == GET_FROM_IMAGE)
3070 pop_it (it);
3071
3072 /* We already have the first chunk of overlay strings in
3073 IT->overlay_strings. Load more until the one for
3074 pos->overlay_string_index is in IT->overlay_strings. */
3075 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3076 {
3077 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3078 it->current.overlay_string_index = 0;
3079 while (n--)
3080 {
3081 load_overlay_strings (it, 0);
3082 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3083 }
3084 }
3085
3086 it->current.overlay_string_index = pos->overlay_string_index;
3087 relative_index = (it->current.overlay_string_index
3088 % OVERLAY_STRING_CHUNK_SIZE);
3089 it->string = it->overlay_strings[relative_index];
3090 eassert (STRINGP (it->string));
3091 it->current.string_pos = pos->string_pos;
3092 it->method = GET_FROM_STRING;
3093 }
3094
3095 if (CHARPOS (pos->string_pos) >= 0)
3096 {
3097 /* Recorded position is not in an overlay string, but in another
3098 string. This can only be a string from a `display' property.
3099 IT should already be filled with that string. */
3100 it->current.string_pos = pos->string_pos;
3101 eassert (STRINGP (it->string));
3102 }
3103
3104 /* Restore position in display vector translations, control
3105 character translations or ellipses. */
3106 if (pos->dpvec_index >= 0)
3107 {
3108 if (it->dpvec == NULL)
3109 get_next_display_element (it);
3110 eassert (it->dpvec && it->current.dpvec_index == 0);
3111 it->current.dpvec_index = pos->dpvec_index;
3112 }
3113
3114 CHECK_IT (it);
3115 return !overlay_strings_with_newlines;
3116 }
3117
3118
3119 /* Initialize IT for stepping through current_buffer in window W
3120 starting at ROW->start. */
3121
3122 static void
3123 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3124 {
3125 init_from_display_pos (it, w, &row->start);
3126 it->start = row->start;
3127 it->continuation_lines_width = row->continuation_lines_width;
3128 CHECK_IT (it);
3129 }
3130
3131
3132 /* Initialize IT for stepping through current_buffer in window W
3133 starting in the line following ROW, i.e. starting at ROW->end.
3134 Value is zero if there are overlay strings with newlines at ROW's
3135 end position. */
3136
3137 static int
3138 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3139 {
3140 int success = 0;
3141
3142 if (init_from_display_pos (it, w, &row->end))
3143 {
3144 if (row->continued_p)
3145 it->continuation_lines_width
3146 = row->continuation_lines_width + row->pixel_width;
3147 CHECK_IT (it);
3148 success = 1;
3149 }
3150
3151 return success;
3152 }
3153
3154
3155
3156 \f
3157 /***********************************************************************
3158 Text properties
3159 ***********************************************************************/
3160
3161 /* Called when IT reaches IT->stop_charpos. Handle text property and
3162 overlay changes. Set IT->stop_charpos to the next position where
3163 to stop. */
3164
3165 static void
3166 handle_stop (struct it *it)
3167 {
3168 enum prop_handled handled;
3169 int handle_overlay_change_p;
3170 struct props *p;
3171
3172 it->dpvec = NULL;
3173 it->current.dpvec_index = -1;
3174 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3175 it->ignore_overlay_strings_at_pos_p = 0;
3176 it->ellipsis_p = 0;
3177
3178 /* Use face of preceding text for ellipsis (if invisible) */
3179 if (it->selective_display_ellipsis_p)
3180 it->saved_face_id = it->face_id;
3181
3182 do
3183 {
3184 handled = HANDLED_NORMALLY;
3185
3186 /* Call text property handlers. */
3187 for (p = it_props; p->handler; ++p)
3188 {
3189 handled = p->handler (it);
3190
3191 if (handled == HANDLED_RECOMPUTE_PROPS)
3192 break;
3193 else if (handled == HANDLED_RETURN)
3194 {
3195 /* We still want to show before and after strings from
3196 overlays even if the actual buffer text is replaced. */
3197 if (!handle_overlay_change_p
3198 || it->sp > 1
3199 /* Don't call get_overlay_strings_1 if we already
3200 have overlay strings loaded, because doing so
3201 will load them again and push the iterator state
3202 onto the stack one more time, which is not
3203 expected by the rest of the code that processes
3204 overlay strings. */
3205 || (it->current.overlay_string_index < 0
3206 ? !get_overlay_strings_1 (it, 0, 0)
3207 : 0))
3208 {
3209 if (it->ellipsis_p)
3210 setup_for_ellipsis (it, 0);
3211 /* When handling a display spec, we might load an
3212 empty string. In that case, discard it here. We
3213 used to discard it in handle_single_display_spec,
3214 but that causes get_overlay_strings_1, above, to
3215 ignore overlay strings that we must check. */
3216 if (STRINGP (it->string) && !SCHARS (it->string))
3217 pop_it (it);
3218 return;
3219 }
3220 else if (STRINGP (it->string) && !SCHARS (it->string))
3221 pop_it (it);
3222 else
3223 {
3224 it->ignore_overlay_strings_at_pos_p = 1;
3225 it->string_from_display_prop_p = 0;
3226 it->from_disp_prop_p = 0;
3227 handle_overlay_change_p = 0;
3228 }
3229 handled = HANDLED_RECOMPUTE_PROPS;
3230 break;
3231 }
3232 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3233 handle_overlay_change_p = 0;
3234 }
3235
3236 if (handled != HANDLED_RECOMPUTE_PROPS)
3237 {
3238 /* Don't check for overlay strings below when set to deliver
3239 characters from a display vector. */
3240 if (it->method == GET_FROM_DISPLAY_VECTOR)
3241 handle_overlay_change_p = 0;
3242
3243 /* Handle overlay changes.
3244 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3245 if it finds overlays. */
3246 if (handle_overlay_change_p)
3247 handled = handle_overlay_change (it);
3248 }
3249
3250 if (it->ellipsis_p)
3251 {
3252 setup_for_ellipsis (it, 0);
3253 break;
3254 }
3255 }
3256 while (handled == HANDLED_RECOMPUTE_PROPS);
3257
3258 /* Determine where to stop next. */
3259 if (handled == HANDLED_NORMALLY)
3260 compute_stop_pos (it);
3261 }
3262
3263
3264 /* Compute IT->stop_charpos from text property and overlay change
3265 information for IT's current position. */
3266
3267 static void
3268 compute_stop_pos (struct it *it)
3269 {
3270 register INTERVAL iv, next_iv;
3271 Lisp_Object object, limit, position;
3272 ptrdiff_t charpos, bytepos;
3273
3274 if (STRINGP (it->string))
3275 {
3276 /* Strings are usually short, so don't limit the search for
3277 properties. */
3278 it->stop_charpos = it->end_charpos;
3279 object = it->string;
3280 limit = Qnil;
3281 charpos = IT_STRING_CHARPOS (*it);
3282 bytepos = IT_STRING_BYTEPOS (*it);
3283 }
3284 else
3285 {
3286 ptrdiff_t pos;
3287
3288 /* If end_charpos is out of range for some reason, such as a
3289 misbehaving display function, rationalize it (Bug#5984). */
3290 if (it->end_charpos > ZV)
3291 it->end_charpos = ZV;
3292 it->stop_charpos = it->end_charpos;
3293
3294 /* If next overlay change is in front of the current stop pos
3295 (which is IT->end_charpos), stop there. Note: value of
3296 next_overlay_change is point-max if no overlay change
3297 follows. */
3298 charpos = IT_CHARPOS (*it);
3299 bytepos = IT_BYTEPOS (*it);
3300 pos = next_overlay_change (charpos);
3301 if (pos < it->stop_charpos)
3302 it->stop_charpos = pos;
3303
3304 /* If showing the region, we have to stop at the region
3305 start or end because the face might change there. */
3306 if (it->region_beg_charpos > 0)
3307 {
3308 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3309 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3310 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3311 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3312 }
3313
3314 /* Set up variables for computing the stop position from text
3315 property changes. */
3316 XSETBUFFER (object, current_buffer);
3317 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3318 }
3319
3320 /* Get the interval containing IT's position. Value is a null
3321 interval if there isn't such an interval. */
3322 position = make_number (charpos);
3323 iv = validate_interval_range (object, &position, &position, 0);
3324 if (!NULL_INTERVAL_P (iv))
3325 {
3326 Lisp_Object values_here[LAST_PROP_IDX];
3327 struct props *p;
3328
3329 /* Get properties here. */
3330 for (p = it_props; p->handler; ++p)
3331 values_here[p->idx] = textget (iv->plist, *p->name);
3332
3333 /* Look for an interval following iv that has different
3334 properties. */
3335 for (next_iv = next_interval (iv);
3336 (!NULL_INTERVAL_P (next_iv)
3337 && (NILP (limit)
3338 || XFASTINT (limit) > next_iv->position));
3339 next_iv = next_interval (next_iv))
3340 {
3341 for (p = it_props; p->handler; ++p)
3342 {
3343 Lisp_Object new_value;
3344
3345 new_value = textget (next_iv->plist, *p->name);
3346 if (!EQ (values_here[p->idx], new_value))
3347 break;
3348 }
3349
3350 if (p->handler)
3351 break;
3352 }
3353
3354 if (!NULL_INTERVAL_P (next_iv))
3355 {
3356 if (INTEGERP (limit)
3357 && next_iv->position >= XFASTINT (limit))
3358 /* No text property change up to limit. */
3359 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3360 else
3361 /* Text properties change in next_iv. */
3362 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3363 }
3364 }
3365
3366 if (it->cmp_it.id < 0)
3367 {
3368 ptrdiff_t stoppos = it->end_charpos;
3369
3370 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3371 stoppos = -1;
3372 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3373 stoppos, it->string);
3374 }
3375
3376 eassert (STRINGP (it->string)
3377 || (it->stop_charpos >= BEGV
3378 && it->stop_charpos >= IT_CHARPOS (*it)));
3379 }
3380
3381
3382 /* Return the position of the next overlay change after POS in
3383 current_buffer. Value is point-max if no overlay change
3384 follows. This is like `next-overlay-change' but doesn't use
3385 xmalloc. */
3386
3387 static ptrdiff_t
3388 next_overlay_change (ptrdiff_t pos)
3389 {
3390 ptrdiff_t i, noverlays;
3391 ptrdiff_t endpos;
3392 Lisp_Object *overlays;
3393
3394 /* Get all overlays at the given position. */
3395 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3396
3397 /* If any of these overlays ends before endpos,
3398 use its ending point instead. */
3399 for (i = 0; i < noverlays; ++i)
3400 {
3401 Lisp_Object oend;
3402 ptrdiff_t oendpos;
3403
3404 oend = OVERLAY_END (overlays[i]);
3405 oendpos = OVERLAY_POSITION (oend);
3406 endpos = min (endpos, oendpos);
3407 }
3408
3409 return endpos;
3410 }
3411
3412 /* How many characters forward to search for a display property or
3413 display string. Searching too far forward makes the bidi display
3414 sluggish, especially in small windows. */
3415 #define MAX_DISP_SCAN 250
3416
3417 /* Return the character position of a display string at or after
3418 position specified by POSITION. If no display string exists at or
3419 after POSITION, return ZV. A display string is either an overlay
3420 with `display' property whose value is a string, or a `display'
3421 text property whose value is a string. STRING is data about the
3422 string to iterate; if STRING->lstring is nil, we are iterating a
3423 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3424 on a GUI frame. DISP_PROP is set to zero if we searched
3425 MAX_DISP_SCAN characters forward without finding any display
3426 strings, non-zero otherwise. It is set to 2 if the display string
3427 uses any kind of `(space ...)' spec that will produce a stretch of
3428 white space in the text area. */
3429 ptrdiff_t
3430 compute_display_string_pos (struct text_pos *position,
3431 struct bidi_string_data *string,
3432 int frame_window_p, int *disp_prop)
3433 {
3434 /* OBJECT = nil means current buffer. */
3435 Lisp_Object object =
3436 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3437 Lisp_Object pos, spec, limpos;
3438 int string_p = (string && (STRINGP (string->lstring) || string->s));
3439 ptrdiff_t eob = string_p ? string->schars : ZV;
3440 ptrdiff_t begb = string_p ? 0 : BEGV;
3441 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3442 ptrdiff_t lim =
3443 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3444 struct text_pos tpos;
3445 int rv = 0;
3446
3447 *disp_prop = 1;
3448
3449 if (charpos >= eob
3450 /* We don't support display properties whose values are strings
3451 that have display string properties. */
3452 || string->from_disp_str
3453 /* C strings cannot have display properties. */
3454 || (string->s && !STRINGP (object)))
3455 {
3456 *disp_prop = 0;
3457 return eob;
3458 }
3459
3460 /* If the character at CHARPOS is where the display string begins,
3461 return CHARPOS. */
3462 pos = make_number (charpos);
3463 if (STRINGP (object))
3464 bufpos = string->bufpos;
3465 else
3466 bufpos = charpos;
3467 tpos = *position;
3468 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3469 && (charpos <= begb
3470 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3471 object),
3472 spec))
3473 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3474 frame_window_p)))
3475 {
3476 if (rv == 2)
3477 *disp_prop = 2;
3478 return charpos;
3479 }
3480
3481 /* Look forward for the first character with a `display' property
3482 that will replace the underlying text when displayed. */
3483 limpos = make_number (lim);
3484 do {
3485 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3486 CHARPOS (tpos) = XFASTINT (pos);
3487 if (CHARPOS (tpos) >= lim)
3488 {
3489 *disp_prop = 0;
3490 break;
3491 }
3492 if (STRINGP (object))
3493 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3494 else
3495 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3496 spec = Fget_char_property (pos, Qdisplay, object);
3497 if (!STRINGP (object))
3498 bufpos = CHARPOS (tpos);
3499 } while (NILP (spec)
3500 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3501 bufpos, frame_window_p)));
3502 if (rv == 2)
3503 *disp_prop = 2;
3504
3505 return CHARPOS (tpos);
3506 }
3507
3508 /* Return the character position of the end of the display string that
3509 started at CHARPOS. If there's no display string at CHARPOS,
3510 return -1. A display string is either an overlay with `display'
3511 property whose value is a string or a `display' text property whose
3512 value is a string. */
3513 ptrdiff_t
3514 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3515 {
3516 /* OBJECT = nil means current buffer. */
3517 Lisp_Object object =
3518 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3519 Lisp_Object pos = make_number (charpos);
3520 ptrdiff_t eob =
3521 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3522
3523 if (charpos >= eob || (string->s && !STRINGP (object)))
3524 return eob;
3525
3526 /* It could happen that the display property or overlay was removed
3527 since we found it in compute_display_string_pos above. One way
3528 this can happen is if JIT font-lock was called (through
3529 handle_fontified_prop), and jit-lock-functions remove text
3530 properties or overlays from the portion of buffer that includes
3531 CHARPOS. Muse mode is known to do that, for example. In this
3532 case, we return -1 to the caller, to signal that no display
3533 string is actually present at CHARPOS. See bidi_fetch_char for
3534 how this is handled.
3535
3536 An alternative would be to never look for display properties past
3537 it->stop_charpos. But neither compute_display_string_pos nor
3538 bidi_fetch_char that calls it know or care where the next
3539 stop_charpos is. */
3540 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3541 return -1;
3542
3543 /* Look forward for the first character where the `display' property
3544 changes. */
3545 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3546
3547 return XFASTINT (pos);
3548 }
3549
3550
3551 \f
3552 /***********************************************************************
3553 Fontification
3554 ***********************************************************************/
3555
3556 /* Handle changes in the `fontified' property of the current buffer by
3557 calling hook functions from Qfontification_functions to fontify
3558 regions of text. */
3559
3560 static enum prop_handled
3561 handle_fontified_prop (struct it *it)
3562 {
3563 Lisp_Object prop, pos;
3564 enum prop_handled handled = HANDLED_NORMALLY;
3565
3566 if (!NILP (Vmemory_full))
3567 return handled;
3568
3569 /* Get the value of the `fontified' property at IT's current buffer
3570 position. (The `fontified' property doesn't have a special
3571 meaning in strings.) If the value is nil, call functions from
3572 Qfontification_functions. */
3573 if (!STRINGP (it->string)
3574 && it->s == NULL
3575 && !NILP (Vfontification_functions)
3576 && !NILP (Vrun_hooks)
3577 && (pos = make_number (IT_CHARPOS (*it)),
3578 prop = Fget_char_property (pos, Qfontified, Qnil),
3579 /* Ignore the special cased nil value always present at EOB since
3580 no amount of fontifying will be able to change it. */
3581 NILP (prop) && IT_CHARPOS (*it) < Z))
3582 {
3583 ptrdiff_t count = SPECPDL_INDEX ();
3584 Lisp_Object val;
3585 struct buffer *obuf = current_buffer;
3586 int begv = BEGV, zv = ZV;
3587 int old_clip_changed = current_buffer->clip_changed;
3588
3589 val = Vfontification_functions;
3590 specbind (Qfontification_functions, Qnil);
3591
3592 eassert (it->end_charpos == ZV);
3593
3594 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3595 safe_call1 (val, pos);
3596 else
3597 {
3598 Lisp_Object fns, fn;
3599 struct gcpro gcpro1, gcpro2;
3600
3601 fns = Qnil;
3602 GCPRO2 (val, fns);
3603
3604 for (; CONSP (val); val = XCDR (val))
3605 {
3606 fn = XCAR (val);
3607
3608 if (EQ (fn, Qt))
3609 {
3610 /* A value of t indicates this hook has a local
3611 binding; it means to run the global binding too.
3612 In a global value, t should not occur. If it
3613 does, we must ignore it to avoid an endless
3614 loop. */
3615 for (fns = Fdefault_value (Qfontification_functions);
3616 CONSP (fns);
3617 fns = XCDR (fns))
3618 {
3619 fn = XCAR (fns);
3620 if (!EQ (fn, Qt))
3621 safe_call1 (fn, pos);
3622 }
3623 }
3624 else
3625 safe_call1 (fn, pos);
3626 }
3627
3628 UNGCPRO;
3629 }
3630
3631 unbind_to (count, Qnil);
3632
3633 /* Fontification functions routinely call `save-restriction'.
3634 Normally, this tags clip_changed, which can confuse redisplay
3635 (see discussion in Bug#6671). Since we don't perform any
3636 special handling of fontification changes in the case where
3637 `save-restriction' isn't called, there's no point doing so in
3638 this case either. So, if the buffer's restrictions are
3639 actually left unchanged, reset clip_changed. */
3640 if (obuf == current_buffer)
3641 {
3642 if (begv == BEGV && zv == ZV)
3643 current_buffer->clip_changed = old_clip_changed;
3644 }
3645 /* There isn't much we can reasonably do to protect against
3646 misbehaving fontification, but here's a fig leaf. */
3647 else if (!NILP (BVAR (obuf, name)))
3648 set_buffer_internal_1 (obuf);
3649
3650 /* The fontification code may have added/removed text.
3651 It could do even a lot worse, but let's at least protect against
3652 the most obvious case where only the text past `pos' gets changed',
3653 as is/was done in grep.el where some escapes sequences are turned
3654 into face properties (bug#7876). */
3655 it->end_charpos = ZV;
3656
3657 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3658 something. This avoids an endless loop if they failed to
3659 fontify the text for which reason ever. */
3660 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3661 handled = HANDLED_RECOMPUTE_PROPS;
3662 }
3663
3664 return handled;
3665 }
3666
3667
3668 \f
3669 /***********************************************************************
3670 Faces
3671 ***********************************************************************/
3672
3673 /* Set up iterator IT from face properties at its current position.
3674 Called from handle_stop. */
3675
3676 static enum prop_handled
3677 handle_face_prop (struct it *it)
3678 {
3679 int new_face_id;
3680 ptrdiff_t next_stop;
3681
3682 if (!STRINGP (it->string))
3683 {
3684 new_face_id
3685 = face_at_buffer_position (it->w,
3686 IT_CHARPOS (*it),
3687 it->region_beg_charpos,
3688 it->region_end_charpos,
3689 &next_stop,
3690 (IT_CHARPOS (*it)
3691 + TEXT_PROP_DISTANCE_LIMIT),
3692 0, it->base_face_id);
3693
3694 /* Is this a start of a run of characters with box face?
3695 Caveat: this can be called for a freshly initialized
3696 iterator; face_id is -1 in this case. We know that the new
3697 face will not change until limit, i.e. if the new face has a
3698 box, all characters up to limit will have one. But, as
3699 usual, we don't know whether limit is really the end. */
3700 if (new_face_id != it->face_id)
3701 {
3702 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3703
3704 /* If new face has a box but old face has not, this is
3705 the start of a run of characters with box, i.e. it has
3706 a shadow on the left side. The value of face_id of the
3707 iterator will be -1 if this is the initial call that gets
3708 the face. In this case, we have to look in front of IT's
3709 position and see whether there is a face != new_face_id. */
3710 it->start_of_box_run_p
3711 = (new_face->box != FACE_NO_BOX
3712 && (it->face_id >= 0
3713 || IT_CHARPOS (*it) == BEG
3714 || new_face_id != face_before_it_pos (it)));
3715 it->face_box_p = new_face->box != FACE_NO_BOX;
3716 }
3717 }
3718 else
3719 {
3720 int base_face_id;
3721 ptrdiff_t bufpos;
3722 int i;
3723 Lisp_Object from_overlay
3724 = (it->current.overlay_string_index >= 0
3725 ? it->string_overlays[it->current.overlay_string_index
3726 % OVERLAY_STRING_CHUNK_SIZE]
3727 : Qnil);
3728
3729 /* See if we got to this string directly or indirectly from
3730 an overlay property. That includes the before-string or
3731 after-string of an overlay, strings in display properties
3732 provided by an overlay, their text properties, etc.
3733
3734 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3735 if (! NILP (from_overlay))
3736 for (i = it->sp - 1; i >= 0; i--)
3737 {
3738 if (it->stack[i].current.overlay_string_index >= 0)
3739 from_overlay
3740 = it->string_overlays[it->stack[i].current.overlay_string_index
3741 % OVERLAY_STRING_CHUNK_SIZE];
3742 else if (! NILP (it->stack[i].from_overlay))
3743 from_overlay = it->stack[i].from_overlay;
3744
3745 if (!NILP (from_overlay))
3746 break;
3747 }
3748
3749 if (! NILP (from_overlay))
3750 {
3751 bufpos = IT_CHARPOS (*it);
3752 /* For a string from an overlay, the base face depends
3753 only on text properties and ignores overlays. */
3754 base_face_id
3755 = face_for_overlay_string (it->w,
3756 IT_CHARPOS (*it),
3757 it->region_beg_charpos,
3758 it->region_end_charpos,
3759 &next_stop,
3760 (IT_CHARPOS (*it)
3761 + TEXT_PROP_DISTANCE_LIMIT),
3762 0,
3763 from_overlay);
3764 }
3765 else
3766 {
3767 bufpos = 0;
3768
3769 /* For strings from a `display' property, use the face at
3770 IT's current buffer position as the base face to merge
3771 with, so that overlay strings appear in the same face as
3772 surrounding text, unless they specify their own
3773 faces. */
3774 base_face_id = it->string_from_prefix_prop_p
3775 ? DEFAULT_FACE_ID
3776 : underlying_face_id (it);
3777 }
3778
3779 new_face_id = face_at_string_position (it->w,
3780 it->string,
3781 IT_STRING_CHARPOS (*it),
3782 bufpos,
3783 it->region_beg_charpos,
3784 it->region_end_charpos,
3785 &next_stop,
3786 base_face_id, 0);
3787
3788 /* Is this a start of a run of characters with box? Caveat:
3789 this can be called for a freshly allocated iterator; face_id
3790 is -1 is this case. We know that the new face will not
3791 change until the next check pos, i.e. if the new face has a
3792 box, all characters up to that position will have a
3793 box. But, as usual, we don't know whether that position
3794 is really the end. */
3795 if (new_face_id != it->face_id)
3796 {
3797 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3798 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3799
3800 /* If new face has a box but old face hasn't, this is the
3801 start of a run of characters with box, i.e. it has a
3802 shadow on the left side. */
3803 it->start_of_box_run_p
3804 = new_face->box && (old_face == NULL || !old_face->box);
3805 it->face_box_p = new_face->box != FACE_NO_BOX;
3806 }
3807 }
3808
3809 it->face_id = new_face_id;
3810 return HANDLED_NORMALLY;
3811 }
3812
3813
3814 /* Return the ID of the face ``underlying'' IT's current position,
3815 which is in a string. If the iterator is associated with a
3816 buffer, return the face at IT's current buffer position.
3817 Otherwise, use the iterator's base_face_id. */
3818
3819 static int
3820 underlying_face_id (struct it *it)
3821 {
3822 int face_id = it->base_face_id, i;
3823
3824 eassert (STRINGP (it->string));
3825
3826 for (i = it->sp - 1; i >= 0; --i)
3827 if (NILP (it->stack[i].string))
3828 face_id = it->stack[i].face_id;
3829
3830 return face_id;
3831 }
3832
3833
3834 /* Compute the face one character before or after the current position
3835 of IT, in the visual order. BEFORE_P non-zero means get the face
3836 in front (to the left in L2R paragraphs, to the right in R2L
3837 paragraphs) of IT's screen position. Value is the ID of the face. */
3838
3839 static int
3840 face_before_or_after_it_pos (struct it *it, int before_p)
3841 {
3842 int face_id, limit;
3843 ptrdiff_t next_check_charpos;
3844 struct it it_copy;
3845 void *it_copy_data = NULL;
3846
3847 eassert (it->s == NULL);
3848
3849 if (STRINGP (it->string))
3850 {
3851 ptrdiff_t bufpos, charpos;
3852 int base_face_id;
3853
3854 /* No face change past the end of the string (for the case
3855 we are padding with spaces). No face change before the
3856 string start. */
3857 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3858 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3859 return it->face_id;
3860
3861 if (!it->bidi_p)
3862 {
3863 /* Set charpos to the position before or after IT's current
3864 position, in the logical order, which in the non-bidi
3865 case is the same as the visual order. */
3866 if (before_p)
3867 charpos = IT_STRING_CHARPOS (*it) - 1;
3868 else if (it->what == IT_COMPOSITION)
3869 /* For composition, we must check the character after the
3870 composition. */
3871 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3872 else
3873 charpos = IT_STRING_CHARPOS (*it) + 1;
3874 }
3875 else
3876 {
3877 if (before_p)
3878 {
3879 /* With bidi iteration, the character before the current
3880 in the visual order cannot be found by simple
3881 iteration, because "reverse" reordering is not
3882 supported. Instead, we need to use the move_it_*
3883 family of functions. */
3884 /* Ignore face changes before the first visible
3885 character on this display line. */
3886 if (it->current_x <= it->first_visible_x)
3887 return it->face_id;
3888 SAVE_IT (it_copy, *it, it_copy_data);
3889 /* Implementation note: Since move_it_in_display_line
3890 works in the iterator geometry, and thinks the first
3891 character is always the leftmost, even in R2L lines,
3892 we don't need to distinguish between the R2L and L2R
3893 cases here. */
3894 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3895 it_copy.current_x - 1, MOVE_TO_X);
3896 charpos = IT_STRING_CHARPOS (it_copy);
3897 RESTORE_IT (it, it, it_copy_data);
3898 }
3899 else
3900 {
3901 /* Set charpos to the string position of the character
3902 that comes after IT's current position in the visual
3903 order. */
3904 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3905
3906 it_copy = *it;
3907 while (n--)
3908 bidi_move_to_visually_next (&it_copy.bidi_it);
3909
3910 charpos = it_copy.bidi_it.charpos;
3911 }
3912 }
3913 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3914
3915 if (it->current.overlay_string_index >= 0)
3916 bufpos = IT_CHARPOS (*it);
3917 else
3918 bufpos = 0;
3919
3920 base_face_id = underlying_face_id (it);
3921
3922 /* Get the face for ASCII, or unibyte. */
3923 face_id = face_at_string_position (it->w,
3924 it->string,
3925 charpos,
3926 bufpos,
3927 it->region_beg_charpos,
3928 it->region_end_charpos,
3929 &next_check_charpos,
3930 base_face_id, 0);
3931
3932 /* Correct the face for charsets different from ASCII. Do it
3933 for the multibyte case only. The face returned above is
3934 suitable for unibyte text if IT->string is unibyte. */
3935 if (STRING_MULTIBYTE (it->string))
3936 {
3937 struct text_pos pos1 = string_pos (charpos, it->string);
3938 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3939 int c, len;
3940 struct face *face = FACE_FROM_ID (it->f, face_id);
3941
3942 c = string_char_and_length (p, &len);
3943 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3944 }
3945 }
3946 else
3947 {
3948 struct text_pos pos;
3949
3950 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3951 || (IT_CHARPOS (*it) <= BEGV && before_p))
3952 return it->face_id;
3953
3954 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3955 pos = it->current.pos;
3956
3957 if (!it->bidi_p)
3958 {
3959 if (before_p)
3960 DEC_TEXT_POS (pos, it->multibyte_p);
3961 else
3962 {
3963 if (it->what == IT_COMPOSITION)
3964 {
3965 /* For composition, we must check the position after
3966 the composition. */
3967 pos.charpos += it->cmp_it.nchars;
3968 pos.bytepos += it->len;
3969 }
3970 else
3971 INC_TEXT_POS (pos, it->multibyte_p);
3972 }
3973 }
3974 else
3975 {
3976 if (before_p)
3977 {
3978 /* With bidi iteration, the character before the current
3979 in the visual order cannot be found by simple
3980 iteration, because "reverse" reordering is not
3981 supported. Instead, we need to use the move_it_*
3982 family of functions. */
3983 /* Ignore face changes before the first visible
3984 character on this display line. */
3985 if (it->current_x <= it->first_visible_x)
3986 return it->face_id;
3987 SAVE_IT (it_copy, *it, it_copy_data);
3988 /* Implementation note: Since move_it_in_display_line
3989 works in the iterator geometry, and thinks the first
3990 character is always the leftmost, even in R2L lines,
3991 we don't need to distinguish between the R2L and L2R
3992 cases here. */
3993 move_it_in_display_line (&it_copy, ZV,
3994 it_copy.current_x - 1, MOVE_TO_X);
3995 pos = it_copy.current.pos;
3996 RESTORE_IT (it, it, it_copy_data);
3997 }
3998 else
3999 {
4000 /* Set charpos to the buffer position of the character
4001 that comes after IT's current position in the visual
4002 order. */
4003 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4004
4005 it_copy = *it;
4006 while (n--)
4007 bidi_move_to_visually_next (&it_copy.bidi_it);
4008
4009 SET_TEXT_POS (pos,
4010 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4011 }
4012 }
4013 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4014
4015 /* Determine face for CHARSET_ASCII, or unibyte. */
4016 face_id = face_at_buffer_position (it->w,
4017 CHARPOS (pos),
4018 it->region_beg_charpos,
4019 it->region_end_charpos,
4020 &next_check_charpos,
4021 limit, 0, -1);
4022
4023 /* Correct the face for charsets different from ASCII. Do it
4024 for the multibyte case only. The face returned above is
4025 suitable for unibyte text if current_buffer is unibyte. */
4026 if (it->multibyte_p)
4027 {
4028 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4029 struct face *face = FACE_FROM_ID (it->f, face_id);
4030 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4031 }
4032 }
4033
4034 return face_id;
4035 }
4036
4037
4038 \f
4039 /***********************************************************************
4040 Invisible text
4041 ***********************************************************************/
4042
4043 /* Set up iterator IT from invisible properties at its current
4044 position. Called from handle_stop. */
4045
4046 static enum prop_handled
4047 handle_invisible_prop (struct it *it)
4048 {
4049 enum prop_handled handled = HANDLED_NORMALLY;
4050
4051 if (STRINGP (it->string))
4052 {
4053 Lisp_Object prop, end_charpos, limit, charpos;
4054
4055 /* Get the value of the invisible text property at the
4056 current position. Value will be nil if there is no such
4057 property. */
4058 charpos = make_number (IT_STRING_CHARPOS (*it));
4059 prop = Fget_text_property (charpos, Qinvisible, it->string);
4060
4061 if (!NILP (prop)
4062 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4063 {
4064 ptrdiff_t endpos;
4065
4066 handled = HANDLED_RECOMPUTE_PROPS;
4067
4068 /* Get the position at which the next change of the
4069 invisible text property can be found in IT->string.
4070 Value will be nil if the property value is the same for
4071 all the rest of IT->string. */
4072 XSETINT (limit, SCHARS (it->string));
4073 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4074 it->string, limit);
4075
4076 /* Text at current position is invisible. The next
4077 change in the property is at position end_charpos.
4078 Move IT's current position to that position. */
4079 if (INTEGERP (end_charpos)
4080 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4081 {
4082 struct text_pos old;
4083 ptrdiff_t oldpos;
4084
4085 old = it->current.string_pos;
4086 oldpos = CHARPOS (old);
4087 if (it->bidi_p)
4088 {
4089 if (it->bidi_it.first_elt
4090 && it->bidi_it.charpos < SCHARS (it->string))
4091 bidi_paragraph_init (it->paragraph_embedding,
4092 &it->bidi_it, 1);
4093 /* Bidi-iterate out of the invisible text. */
4094 do
4095 {
4096 bidi_move_to_visually_next (&it->bidi_it);
4097 }
4098 while (oldpos <= it->bidi_it.charpos
4099 && it->bidi_it.charpos < endpos);
4100
4101 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4102 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4103 if (IT_CHARPOS (*it) >= endpos)
4104 it->prev_stop = endpos;
4105 }
4106 else
4107 {
4108 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4109 compute_string_pos (&it->current.string_pos, old, it->string);
4110 }
4111 }
4112 else
4113 {
4114 /* The rest of the string is invisible. If this is an
4115 overlay string, proceed with the next overlay string
4116 or whatever comes and return a character from there. */
4117 if (it->current.overlay_string_index >= 0)
4118 {
4119 next_overlay_string (it);
4120 /* Don't check for overlay strings when we just
4121 finished processing them. */
4122 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4123 }
4124 else
4125 {
4126 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4127 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4128 }
4129 }
4130 }
4131 }
4132 else
4133 {
4134 int invis_p;
4135 ptrdiff_t newpos, next_stop, start_charpos, tem;
4136 Lisp_Object pos, prop, overlay;
4137
4138 /* First of all, is there invisible text at this position? */
4139 tem = start_charpos = IT_CHARPOS (*it);
4140 pos = make_number (tem);
4141 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4142 &overlay);
4143 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4144
4145 /* If we are on invisible text, skip over it. */
4146 if (invis_p && start_charpos < it->end_charpos)
4147 {
4148 /* Record whether we have to display an ellipsis for the
4149 invisible text. */
4150 int display_ellipsis_p = invis_p == 2;
4151
4152 handled = HANDLED_RECOMPUTE_PROPS;
4153
4154 /* Loop skipping over invisible text. The loop is left at
4155 ZV or with IT on the first char being visible again. */
4156 do
4157 {
4158 /* Try to skip some invisible text. Return value is the
4159 position reached which can be equal to where we start
4160 if there is nothing invisible there. This skips both
4161 over invisible text properties and overlays with
4162 invisible property. */
4163 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4164
4165 /* If we skipped nothing at all we weren't at invisible
4166 text in the first place. If everything to the end of
4167 the buffer was skipped, end the loop. */
4168 if (newpos == tem || newpos >= ZV)
4169 invis_p = 0;
4170 else
4171 {
4172 /* We skipped some characters but not necessarily
4173 all there are. Check if we ended up on visible
4174 text. Fget_char_property returns the property of
4175 the char before the given position, i.e. if we
4176 get invis_p = 0, this means that the char at
4177 newpos is visible. */
4178 pos = make_number (newpos);
4179 prop = Fget_char_property (pos, Qinvisible, it->window);
4180 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4181 }
4182
4183 /* If we ended up on invisible text, proceed to
4184 skip starting with next_stop. */
4185 if (invis_p)
4186 tem = next_stop;
4187
4188 /* If there are adjacent invisible texts, don't lose the
4189 second one's ellipsis. */
4190 if (invis_p == 2)
4191 display_ellipsis_p = 1;
4192 }
4193 while (invis_p);
4194
4195 /* The position newpos is now either ZV or on visible text. */
4196 if (it->bidi_p)
4197 {
4198 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4199 int on_newline =
4200 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4201 int after_newline =
4202 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4203
4204 /* If the invisible text ends on a newline or on a
4205 character after a newline, we can avoid the costly,
4206 character by character, bidi iteration to NEWPOS, and
4207 instead simply reseat the iterator there. That's
4208 because all bidi reordering information is tossed at
4209 the newline. This is a big win for modes that hide
4210 complete lines, like Outline, Org, etc. */
4211 if (on_newline || after_newline)
4212 {
4213 struct text_pos tpos;
4214 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4215
4216 SET_TEXT_POS (tpos, newpos, bpos);
4217 reseat_1 (it, tpos, 0);
4218 /* If we reseat on a newline/ZV, we need to prep the
4219 bidi iterator for advancing to the next character
4220 after the newline/EOB, keeping the current paragraph
4221 direction (so that PRODUCE_GLYPHS does TRT wrt
4222 prepending/appending glyphs to a glyph row). */
4223 if (on_newline)
4224 {
4225 it->bidi_it.first_elt = 0;
4226 it->bidi_it.paragraph_dir = pdir;
4227 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4228 it->bidi_it.nchars = 1;
4229 it->bidi_it.ch_len = 1;
4230 }
4231 }
4232 else /* Must use the slow method. */
4233 {
4234 /* With bidi iteration, the region of invisible text
4235 could start and/or end in the middle of a
4236 non-base embedding level. Therefore, we need to
4237 skip invisible text using the bidi iterator,
4238 starting at IT's current position, until we find
4239 ourselves outside of the invisible text.
4240 Skipping invisible text _after_ bidi iteration
4241 avoids affecting the visual order of the
4242 displayed text when invisible properties are
4243 added or removed. */
4244 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4245 {
4246 /* If we were `reseat'ed to a new paragraph,
4247 determine the paragraph base direction. We
4248 need to do it now because
4249 next_element_from_buffer may not have a
4250 chance to do it, if we are going to skip any
4251 text at the beginning, which resets the
4252 FIRST_ELT flag. */
4253 bidi_paragraph_init (it->paragraph_embedding,
4254 &it->bidi_it, 1);
4255 }
4256 do
4257 {
4258 bidi_move_to_visually_next (&it->bidi_it);
4259 }
4260 while (it->stop_charpos <= it->bidi_it.charpos
4261 && it->bidi_it.charpos < newpos);
4262 IT_CHARPOS (*it) = it->bidi_it.charpos;
4263 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4264 /* If we overstepped NEWPOS, record its position in
4265 the iterator, so that we skip invisible text if
4266 later the bidi iteration lands us in the
4267 invisible region again. */
4268 if (IT_CHARPOS (*it) >= newpos)
4269 it->prev_stop = newpos;
4270 }
4271 }
4272 else
4273 {
4274 IT_CHARPOS (*it) = newpos;
4275 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4276 }
4277
4278 /* If there are before-strings at the start of invisible
4279 text, and the text is invisible because of a text
4280 property, arrange to show before-strings because 20.x did
4281 it that way. (If the text is invisible because of an
4282 overlay property instead of a text property, this is
4283 already handled in the overlay code.) */
4284 if (NILP (overlay)
4285 && get_overlay_strings (it, it->stop_charpos))
4286 {
4287 handled = HANDLED_RECOMPUTE_PROPS;
4288 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4289 }
4290 else if (display_ellipsis_p)
4291 {
4292 /* Make sure that the glyphs of the ellipsis will get
4293 correct `charpos' values. If we would not update
4294 it->position here, the glyphs would belong to the
4295 last visible character _before_ the invisible
4296 text, which confuses `set_cursor_from_row'.
4297
4298 We use the last invisible position instead of the
4299 first because this way the cursor is always drawn on
4300 the first "." of the ellipsis, whenever PT is inside
4301 the invisible text. Otherwise the cursor would be
4302 placed _after_ the ellipsis when the point is after the
4303 first invisible character. */
4304 if (!STRINGP (it->object))
4305 {
4306 it->position.charpos = newpos - 1;
4307 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4308 }
4309 it->ellipsis_p = 1;
4310 /* Let the ellipsis display before
4311 considering any properties of the following char.
4312 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4313 handled = HANDLED_RETURN;
4314 }
4315 }
4316 }
4317
4318 return handled;
4319 }
4320
4321
4322 /* Make iterator IT return `...' next.
4323 Replaces LEN characters from buffer. */
4324
4325 static void
4326 setup_for_ellipsis (struct it *it, int len)
4327 {
4328 /* Use the display table definition for `...'. Invalid glyphs
4329 will be handled by the method returning elements from dpvec. */
4330 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4331 {
4332 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4333 it->dpvec = v->contents;
4334 it->dpend = v->contents + v->header.size;
4335 }
4336 else
4337 {
4338 /* Default `...'. */
4339 it->dpvec = default_invis_vector;
4340 it->dpend = default_invis_vector + 3;
4341 }
4342
4343 it->dpvec_char_len = len;
4344 it->current.dpvec_index = 0;
4345 it->dpvec_face_id = -1;
4346
4347 /* Remember the current face id in case glyphs specify faces.
4348 IT's face is restored in set_iterator_to_next.
4349 saved_face_id was set to preceding char's face in handle_stop. */
4350 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4351 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4352
4353 it->method = GET_FROM_DISPLAY_VECTOR;
4354 it->ellipsis_p = 1;
4355 }
4356
4357
4358 \f
4359 /***********************************************************************
4360 'display' property
4361 ***********************************************************************/
4362
4363 /* Set up iterator IT from `display' property at its current position.
4364 Called from handle_stop.
4365 We return HANDLED_RETURN if some part of the display property
4366 overrides the display of the buffer text itself.
4367 Otherwise we return HANDLED_NORMALLY. */
4368
4369 static enum prop_handled
4370 handle_display_prop (struct it *it)
4371 {
4372 Lisp_Object propval, object, overlay;
4373 struct text_pos *position;
4374 ptrdiff_t bufpos;
4375 /* Nonzero if some property replaces the display of the text itself. */
4376 int display_replaced_p = 0;
4377
4378 if (STRINGP (it->string))
4379 {
4380 object = it->string;
4381 position = &it->current.string_pos;
4382 bufpos = CHARPOS (it->current.pos);
4383 }
4384 else
4385 {
4386 XSETWINDOW (object, it->w);
4387 position = &it->current.pos;
4388 bufpos = CHARPOS (*position);
4389 }
4390
4391 /* Reset those iterator values set from display property values. */
4392 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4393 it->space_width = Qnil;
4394 it->font_height = Qnil;
4395 it->voffset = 0;
4396
4397 /* We don't support recursive `display' properties, i.e. string
4398 values that have a string `display' property, that have a string
4399 `display' property etc. */
4400 if (!it->string_from_display_prop_p)
4401 it->area = TEXT_AREA;
4402
4403 propval = get_char_property_and_overlay (make_number (position->charpos),
4404 Qdisplay, object, &overlay);
4405 if (NILP (propval))
4406 return HANDLED_NORMALLY;
4407 /* Now OVERLAY is the overlay that gave us this property, or nil
4408 if it was a text property. */
4409
4410 if (!STRINGP (it->string))
4411 object = WVAR (it->w, buffer);
4412
4413 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4414 position, bufpos,
4415 FRAME_WINDOW_P (it->f));
4416
4417 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4418 }
4419
4420 /* Subroutine of handle_display_prop. Returns non-zero if the display
4421 specification in SPEC is a replacing specification, i.e. it would
4422 replace the text covered by `display' property with something else,
4423 such as an image or a display string. If SPEC includes any kind or
4424 `(space ...) specification, the value is 2; this is used by
4425 compute_display_string_pos, which see.
4426
4427 See handle_single_display_spec for documentation of arguments.
4428 frame_window_p is non-zero if the window being redisplayed is on a
4429 GUI frame; this argument is used only if IT is NULL, see below.
4430
4431 IT can be NULL, if this is called by the bidi reordering code
4432 through compute_display_string_pos, which see. In that case, this
4433 function only examines SPEC, but does not otherwise "handle" it, in
4434 the sense that it doesn't set up members of IT from the display
4435 spec. */
4436 static int
4437 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4438 Lisp_Object overlay, struct text_pos *position,
4439 ptrdiff_t bufpos, int frame_window_p)
4440 {
4441 int replacing_p = 0;
4442 int rv;
4443
4444 if (CONSP (spec)
4445 /* Simple specifications. */
4446 && !EQ (XCAR (spec), Qimage)
4447 && !EQ (XCAR (spec), Qspace)
4448 && !EQ (XCAR (spec), Qwhen)
4449 && !EQ (XCAR (spec), Qslice)
4450 && !EQ (XCAR (spec), Qspace_width)
4451 && !EQ (XCAR (spec), Qheight)
4452 && !EQ (XCAR (spec), Qraise)
4453 /* Marginal area specifications. */
4454 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4455 && !EQ (XCAR (spec), Qleft_fringe)
4456 && !EQ (XCAR (spec), Qright_fringe)
4457 && !NILP (XCAR (spec)))
4458 {
4459 for (; CONSP (spec); spec = XCDR (spec))
4460 {
4461 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4462 overlay, position, bufpos,
4463 replacing_p, frame_window_p)))
4464 {
4465 replacing_p = rv;
4466 /* If some text in a string is replaced, `position' no
4467 longer points to the position of `object'. */
4468 if (!it || STRINGP (object))
4469 break;
4470 }
4471 }
4472 }
4473 else if (VECTORP (spec))
4474 {
4475 ptrdiff_t i;
4476 for (i = 0; i < ASIZE (spec); ++i)
4477 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4478 overlay, position, bufpos,
4479 replacing_p, frame_window_p)))
4480 {
4481 replacing_p = rv;
4482 /* If some text in a string is replaced, `position' no
4483 longer points to the position of `object'. */
4484 if (!it || STRINGP (object))
4485 break;
4486 }
4487 }
4488 else
4489 {
4490 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4491 position, bufpos, 0,
4492 frame_window_p)))
4493 replacing_p = rv;
4494 }
4495
4496 return replacing_p;
4497 }
4498
4499 /* Value is the position of the end of the `display' property starting
4500 at START_POS in OBJECT. */
4501
4502 static struct text_pos
4503 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4504 {
4505 Lisp_Object end;
4506 struct text_pos end_pos;
4507
4508 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4509 Qdisplay, object, Qnil);
4510 CHARPOS (end_pos) = XFASTINT (end);
4511 if (STRINGP (object))
4512 compute_string_pos (&end_pos, start_pos, it->string);
4513 else
4514 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4515
4516 return end_pos;
4517 }
4518
4519
4520 /* Set up IT from a single `display' property specification SPEC. OBJECT
4521 is the object in which the `display' property was found. *POSITION
4522 is the position in OBJECT at which the `display' property was found.
4523 BUFPOS is the buffer position of OBJECT (different from POSITION if
4524 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4525 previously saw a display specification which already replaced text
4526 display with something else, for example an image; we ignore such
4527 properties after the first one has been processed.
4528
4529 OVERLAY is the overlay this `display' property came from,
4530 or nil if it was a text property.
4531
4532 If SPEC is a `space' or `image' specification, and in some other
4533 cases too, set *POSITION to the position where the `display'
4534 property ends.
4535
4536 If IT is NULL, only examine the property specification in SPEC, but
4537 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4538 is intended to be displayed in a window on a GUI frame.
4539
4540 Value is non-zero if something was found which replaces the display
4541 of buffer or string text. */
4542
4543 static int
4544 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4545 Lisp_Object overlay, struct text_pos *position,
4546 ptrdiff_t bufpos, int display_replaced_p,
4547 int frame_window_p)
4548 {
4549 Lisp_Object form;
4550 Lisp_Object location, value;
4551 struct text_pos start_pos = *position;
4552 int valid_p;
4553
4554 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4555 If the result is non-nil, use VALUE instead of SPEC. */
4556 form = Qt;
4557 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4558 {
4559 spec = XCDR (spec);
4560 if (!CONSP (spec))
4561 return 0;
4562 form = XCAR (spec);
4563 spec = XCDR (spec);
4564 }
4565
4566 if (!NILP (form) && !EQ (form, Qt))
4567 {
4568 ptrdiff_t count = SPECPDL_INDEX ();
4569 struct gcpro gcpro1;
4570
4571 /* Bind `object' to the object having the `display' property, a
4572 buffer or string. Bind `position' to the position in the
4573 object where the property was found, and `buffer-position'
4574 to the current position in the buffer. */
4575
4576 if (NILP (object))
4577 XSETBUFFER (object, current_buffer);
4578 specbind (Qobject, object);
4579 specbind (Qposition, make_number (CHARPOS (*position)));
4580 specbind (Qbuffer_position, make_number (bufpos));
4581 GCPRO1 (form);
4582 form = safe_eval (form);
4583 UNGCPRO;
4584 unbind_to (count, Qnil);
4585 }
4586
4587 if (NILP (form))
4588 return 0;
4589
4590 /* Handle `(height HEIGHT)' specifications. */
4591 if (CONSP (spec)
4592 && EQ (XCAR (spec), Qheight)
4593 && CONSP (XCDR (spec)))
4594 {
4595 if (it)
4596 {
4597 if (!FRAME_WINDOW_P (it->f))
4598 return 0;
4599
4600 it->font_height = XCAR (XCDR (spec));
4601 if (!NILP (it->font_height))
4602 {
4603 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4604 int new_height = -1;
4605
4606 if (CONSP (it->font_height)
4607 && (EQ (XCAR (it->font_height), Qplus)
4608 || EQ (XCAR (it->font_height), Qminus))
4609 && CONSP (XCDR (it->font_height))
4610 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4611 {
4612 /* `(+ N)' or `(- N)' where N is an integer. */
4613 int steps = XINT (XCAR (XCDR (it->font_height)));
4614 if (EQ (XCAR (it->font_height), Qplus))
4615 steps = - steps;
4616 it->face_id = smaller_face (it->f, it->face_id, steps);
4617 }
4618 else if (FUNCTIONP (it->font_height))
4619 {
4620 /* Call function with current height as argument.
4621 Value is the new height. */
4622 Lisp_Object height;
4623 height = safe_call1 (it->font_height,
4624 face->lface[LFACE_HEIGHT_INDEX]);
4625 if (NUMBERP (height))
4626 new_height = XFLOATINT (height);
4627 }
4628 else if (NUMBERP (it->font_height))
4629 {
4630 /* Value is a multiple of the canonical char height. */
4631 struct face *f;
4632
4633 f = FACE_FROM_ID (it->f,
4634 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4635 new_height = (XFLOATINT (it->font_height)
4636 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4637 }
4638 else
4639 {
4640 /* Evaluate IT->font_height with `height' bound to the
4641 current specified height to get the new height. */
4642 ptrdiff_t count = SPECPDL_INDEX ();
4643
4644 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4645 value = safe_eval (it->font_height);
4646 unbind_to (count, Qnil);
4647
4648 if (NUMBERP (value))
4649 new_height = XFLOATINT (value);
4650 }
4651
4652 if (new_height > 0)
4653 it->face_id = face_with_height (it->f, it->face_id, new_height);
4654 }
4655 }
4656
4657 return 0;
4658 }
4659
4660 /* Handle `(space-width WIDTH)'. */
4661 if (CONSP (spec)
4662 && EQ (XCAR (spec), Qspace_width)
4663 && CONSP (XCDR (spec)))
4664 {
4665 if (it)
4666 {
4667 if (!FRAME_WINDOW_P (it->f))
4668 return 0;
4669
4670 value = XCAR (XCDR (spec));
4671 if (NUMBERP (value) && XFLOATINT (value) > 0)
4672 it->space_width = value;
4673 }
4674
4675 return 0;
4676 }
4677
4678 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4679 if (CONSP (spec)
4680 && EQ (XCAR (spec), Qslice))
4681 {
4682 Lisp_Object tem;
4683
4684 if (it)
4685 {
4686 if (!FRAME_WINDOW_P (it->f))
4687 return 0;
4688
4689 if (tem = XCDR (spec), CONSP (tem))
4690 {
4691 it->slice.x = XCAR (tem);
4692 if (tem = XCDR (tem), CONSP (tem))
4693 {
4694 it->slice.y = XCAR (tem);
4695 if (tem = XCDR (tem), CONSP (tem))
4696 {
4697 it->slice.width = XCAR (tem);
4698 if (tem = XCDR (tem), CONSP (tem))
4699 it->slice.height = XCAR (tem);
4700 }
4701 }
4702 }
4703 }
4704
4705 return 0;
4706 }
4707
4708 /* Handle `(raise FACTOR)'. */
4709 if (CONSP (spec)
4710 && EQ (XCAR (spec), Qraise)
4711 && CONSP (XCDR (spec)))
4712 {
4713 if (it)
4714 {
4715 if (!FRAME_WINDOW_P (it->f))
4716 return 0;
4717
4718 #ifdef HAVE_WINDOW_SYSTEM
4719 value = XCAR (XCDR (spec));
4720 if (NUMBERP (value))
4721 {
4722 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4723 it->voffset = - (XFLOATINT (value)
4724 * (FONT_HEIGHT (face->font)));
4725 }
4726 #endif /* HAVE_WINDOW_SYSTEM */
4727 }
4728
4729 return 0;
4730 }
4731
4732 /* Don't handle the other kinds of display specifications
4733 inside a string that we got from a `display' property. */
4734 if (it && it->string_from_display_prop_p)
4735 return 0;
4736
4737 /* Characters having this form of property are not displayed, so
4738 we have to find the end of the property. */
4739 if (it)
4740 {
4741 start_pos = *position;
4742 *position = display_prop_end (it, object, start_pos);
4743 }
4744 value = Qnil;
4745
4746 /* Stop the scan at that end position--we assume that all
4747 text properties change there. */
4748 if (it)
4749 it->stop_charpos = position->charpos;
4750
4751 /* Handle `(left-fringe BITMAP [FACE])'
4752 and `(right-fringe BITMAP [FACE])'. */
4753 if (CONSP (spec)
4754 && (EQ (XCAR (spec), Qleft_fringe)
4755 || EQ (XCAR (spec), Qright_fringe))
4756 && CONSP (XCDR (spec)))
4757 {
4758 int fringe_bitmap;
4759
4760 if (it)
4761 {
4762 if (!FRAME_WINDOW_P (it->f))
4763 /* If we return here, POSITION has been advanced
4764 across the text with this property. */
4765 {
4766 /* Synchronize the bidi iterator with POSITION. This is
4767 needed because we are not going to push the iterator
4768 on behalf of this display property, so there will be
4769 no pop_it call to do this synchronization for us. */
4770 if (it->bidi_p)
4771 {
4772 it->position = *position;
4773 iterate_out_of_display_property (it);
4774 *position = it->position;
4775 }
4776 return 1;
4777 }
4778 }
4779 else if (!frame_window_p)
4780 return 1;
4781
4782 #ifdef HAVE_WINDOW_SYSTEM
4783 value = XCAR (XCDR (spec));
4784 if (!SYMBOLP (value)
4785 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4786 /* If we return here, POSITION has been advanced
4787 across the text with this property. */
4788 {
4789 if (it && it->bidi_p)
4790 {
4791 it->position = *position;
4792 iterate_out_of_display_property (it);
4793 *position = it->position;
4794 }
4795 return 1;
4796 }
4797
4798 if (it)
4799 {
4800 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4801
4802 if (CONSP (XCDR (XCDR (spec))))
4803 {
4804 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4805 int face_id2 = lookup_derived_face (it->f, face_name,
4806 FRINGE_FACE_ID, 0);
4807 if (face_id2 >= 0)
4808 face_id = face_id2;
4809 }
4810
4811 /* Save current settings of IT so that we can restore them
4812 when we are finished with the glyph property value. */
4813 push_it (it, position);
4814
4815 it->area = TEXT_AREA;
4816 it->what = IT_IMAGE;
4817 it->image_id = -1; /* no image */
4818 it->position = start_pos;
4819 it->object = NILP (object) ? WVAR (it->w, buffer) : object;
4820 it->method = GET_FROM_IMAGE;
4821 it->from_overlay = Qnil;
4822 it->face_id = face_id;
4823 it->from_disp_prop_p = 1;
4824
4825 /* Say that we haven't consumed the characters with
4826 `display' property yet. The call to pop_it in
4827 set_iterator_to_next will clean this up. */
4828 *position = start_pos;
4829
4830 if (EQ (XCAR (spec), Qleft_fringe))
4831 {
4832 it->left_user_fringe_bitmap = fringe_bitmap;
4833 it->left_user_fringe_face_id = face_id;
4834 }
4835 else
4836 {
4837 it->right_user_fringe_bitmap = fringe_bitmap;
4838 it->right_user_fringe_face_id = face_id;
4839 }
4840 }
4841 #endif /* HAVE_WINDOW_SYSTEM */
4842 return 1;
4843 }
4844
4845 /* Prepare to handle `((margin left-margin) ...)',
4846 `((margin right-margin) ...)' and `((margin nil) ...)'
4847 prefixes for display specifications. */
4848 location = Qunbound;
4849 if (CONSP (spec) && CONSP (XCAR (spec)))
4850 {
4851 Lisp_Object tem;
4852
4853 value = XCDR (spec);
4854 if (CONSP (value))
4855 value = XCAR (value);
4856
4857 tem = XCAR (spec);
4858 if (EQ (XCAR (tem), Qmargin)
4859 && (tem = XCDR (tem),
4860 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4861 (NILP (tem)
4862 || EQ (tem, Qleft_margin)
4863 || EQ (tem, Qright_margin))))
4864 location = tem;
4865 }
4866
4867 if (EQ (location, Qunbound))
4868 {
4869 location = Qnil;
4870 value = spec;
4871 }
4872
4873 /* After this point, VALUE is the property after any
4874 margin prefix has been stripped. It must be a string,
4875 an image specification, or `(space ...)'.
4876
4877 LOCATION specifies where to display: `left-margin',
4878 `right-margin' or nil. */
4879
4880 valid_p = (STRINGP (value)
4881 #ifdef HAVE_WINDOW_SYSTEM
4882 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4883 && valid_image_p (value))
4884 #endif /* not HAVE_WINDOW_SYSTEM */
4885 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4886
4887 if (valid_p && !display_replaced_p)
4888 {
4889 int retval = 1;
4890
4891 if (!it)
4892 {
4893 /* Callers need to know whether the display spec is any kind
4894 of `(space ...)' spec that is about to affect text-area
4895 display. */
4896 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4897 retval = 2;
4898 return retval;
4899 }
4900
4901 /* Save current settings of IT so that we can restore them
4902 when we are finished with the glyph property value. */
4903 push_it (it, position);
4904 it->from_overlay = overlay;
4905 it->from_disp_prop_p = 1;
4906
4907 if (NILP (location))
4908 it->area = TEXT_AREA;
4909 else if (EQ (location, Qleft_margin))
4910 it->area = LEFT_MARGIN_AREA;
4911 else
4912 it->area = RIGHT_MARGIN_AREA;
4913
4914 if (STRINGP (value))
4915 {
4916 it->string = value;
4917 it->multibyte_p = STRING_MULTIBYTE (it->string);
4918 it->current.overlay_string_index = -1;
4919 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4920 it->end_charpos = it->string_nchars = SCHARS (it->string);
4921 it->method = GET_FROM_STRING;
4922 it->stop_charpos = 0;
4923 it->prev_stop = 0;
4924 it->base_level_stop = 0;
4925 it->string_from_display_prop_p = 1;
4926 /* Say that we haven't consumed the characters with
4927 `display' property yet. The call to pop_it in
4928 set_iterator_to_next will clean this up. */
4929 if (BUFFERP (object))
4930 *position = start_pos;
4931
4932 /* Force paragraph direction to be that of the parent
4933 object. If the parent object's paragraph direction is
4934 not yet determined, default to L2R. */
4935 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4936 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4937 else
4938 it->paragraph_embedding = L2R;
4939
4940 /* Set up the bidi iterator for this display string. */
4941 if (it->bidi_p)
4942 {
4943 it->bidi_it.string.lstring = it->string;
4944 it->bidi_it.string.s = NULL;
4945 it->bidi_it.string.schars = it->end_charpos;
4946 it->bidi_it.string.bufpos = bufpos;
4947 it->bidi_it.string.from_disp_str = 1;
4948 it->bidi_it.string.unibyte = !it->multibyte_p;
4949 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4950 }
4951 }
4952 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4953 {
4954 it->method = GET_FROM_STRETCH;
4955 it->object = value;
4956 *position = it->position = start_pos;
4957 retval = 1 + (it->area == TEXT_AREA);
4958 }
4959 #ifdef HAVE_WINDOW_SYSTEM
4960 else
4961 {
4962 it->what = IT_IMAGE;
4963 it->image_id = lookup_image (it->f, value);
4964 it->position = start_pos;
4965 it->object = NILP (object) ? WVAR (it->w, buffer) : object;
4966 it->method = GET_FROM_IMAGE;
4967
4968 /* Say that we haven't consumed the characters with
4969 `display' property yet. The call to pop_it in
4970 set_iterator_to_next will clean this up. */
4971 *position = start_pos;
4972 }
4973 #endif /* HAVE_WINDOW_SYSTEM */
4974
4975 return retval;
4976 }
4977
4978 /* Invalid property or property not supported. Restore
4979 POSITION to what it was before. */
4980 *position = start_pos;
4981 return 0;
4982 }
4983
4984 /* Check if PROP is a display property value whose text should be
4985 treated as intangible. OVERLAY is the overlay from which PROP
4986 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4987 specify the buffer position covered by PROP. */
4988
4989 int
4990 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4991 ptrdiff_t charpos, ptrdiff_t bytepos)
4992 {
4993 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4994 struct text_pos position;
4995
4996 SET_TEXT_POS (position, charpos, bytepos);
4997 return handle_display_spec (NULL, prop, Qnil, overlay,
4998 &position, charpos, frame_window_p);
4999 }
5000
5001
5002 /* Return 1 if PROP is a display sub-property value containing STRING.
5003
5004 Implementation note: this and the following function are really
5005 special cases of handle_display_spec and
5006 handle_single_display_spec, and should ideally use the same code.
5007 Until they do, these two pairs must be consistent and must be
5008 modified in sync. */
5009
5010 static int
5011 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5012 {
5013 if (EQ (string, prop))
5014 return 1;
5015
5016 /* Skip over `when FORM'. */
5017 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5018 {
5019 prop = XCDR (prop);
5020 if (!CONSP (prop))
5021 return 0;
5022 /* Actually, the condition following `when' should be eval'ed,
5023 like handle_single_display_spec does, and we should return
5024 zero if it evaluates to nil. However, this function is
5025 called only when the buffer was already displayed and some
5026 glyph in the glyph matrix was found to come from a display
5027 string. Therefore, the condition was already evaluated, and
5028 the result was non-nil, otherwise the display string wouldn't
5029 have been displayed and we would have never been called for
5030 this property. Thus, we can skip the evaluation and assume
5031 its result is non-nil. */
5032 prop = XCDR (prop);
5033 }
5034
5035 if (CONSP (prop))
5036 /* Skip over `margin LOCATION'. */
5037 if (EQ (XCAR (prop), Qmargin))
5038 {
5039 prop = XCDR (prop);
5040 if (!CONSP (prop))
5041 return 0;
5042
5043 prop = XCDR (prop);
5044 if (!CONSP (prop))
5045 return 0;
5046 }
5047
5048 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5049 }
5050
5051
5052 /* Return 1 if STRING appears in the `display' property PROP. */
5053
5054 static int
5055 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5056 {
5057 if (CONSP (prop)
5058 && !EQ (XCAR (prop), Qwhen)
5059 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5060 {
5061 /* A list of sub-properties. */
5062 while (CONSP (prop))
5063 {
5064 if (single_display_spec_string_p (XCAR (prop), string))
5065 return 1;
5066 prop = XCDR (prop);
5067 }
5068 }
5069 else if (VECTORP (prop))
5070 {
5071 /* A vector of sub-properties. */
5072 ptrdiff_t i;
5073 for (i = 0; i < ASIZE (prop); ++i)
5074 if (single_display_spec_string_p (AREF (prop, i), string))
5075 return 1;
5076 }
5077 else
5078 return single_display_spec_string_p (prop, string);
5079
5080 return 0;
5081 }
5082
5083 /* Look for STRING in overlays and text properties in the current
5084 buffer, between character positions FROM and TO (excluding TO).
5085 BACK_P non-zero means look back (in this case, TO is supposed to be
5086 less than FROM).
5087 Value is the first character position where STRING was found, or
5088 zero if it wasn't found before hitting TO.
5089
5090 This function may only use code that doesn't eval because it is
5091 called asynchronously from note_mouse_highlight. */
5092
5093 static ptrdiff_t
5094 string_buffer_position_lim (Lisp_Object string,
5095 ptrdiff_t from, ptrdiff_t to, int back_p)
5096 {
5097 Lisp_Object limit, prop, pos;
5098 int found = 0;
5099
5100 pos = make_number (max (from, BEGV));
5101
5102 if (!back_p) /* looking forward */
5103 {
5104 limit = make_number (min (to, ZV));
5105 while (!found && !EQ (pos, limit))
5106 {
5107 prop = Fget_char_property (pos, Qdisplay, Qnil);
5108 if (!NILP (prop) && display_prop_string_p (prop, string))
5109 found = 1;
5110 else
5111 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5112 limit);
5113 }
5114 }
5115 else /* looking back */
5116 {
5117 limit = make_number (max (to, BEGV));
5118 while (!found && !EQ (pos, limit))
5119 {
5120 prop = Fget_char_property (pos, Qdisplay, Qnil);
5121 if (!NILP (prop) && display_prop_string_p (prop, string))
5122 found = 1;
5123 else
5124 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5125 limit);
5126 }
5127 }
5128
5129 return found ? XINT (pos) : 0;
5130 }
5131
5132 /* Determine which buffer position in current buffer STRING comes from.
5133 AROUND_CHARPOS is an approximate position where it could come from.
5134 Value is the buffer position or 0 if it couldn't be determined.
5135
5136 This function is necessary because we don't record buffer positions
5137 in glyphs generated from strings (to keep struct glyph small).
5138 This function may only use code that doesn't eval because it is
5139 called asynchronously from note_mouse_highlight. */
5140
5141 static ptrdiff_t
5142 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5143 {
5144 const int MAX_DISTANCE = 1000;
5145 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5146 around_charpos + MAX_DISTANCE,
5147 0);
5148
5149 if (!found)
5150 found = string_buffer_position_lim (string, around_charpos,
5151 around_charpos - MAX_DISTANCE, 1);
5152 return found;
5153 }
5154
5155
5156 \f
5157 /***********************************************************************
5158 `composition' property
5159 ***********************************************************************/
5160
5161 /* Set up iterator IT from `composition' property at its current
5162 position. Called from handle_stop. */
5163
5164 static enum prop_handled
5165 handle_composition_prop (struct it *it)
5166 {
5167 Lisp_Object prop, string;
5168 ptrdiff_t pos, pos_byte, start, end;
5169
5170 if (STRINGP (it->string))
5171 {
5172 unsigned char *s;
5173
5174 pos = IT_STRING_CHARPOS (*it);
5175 pos_byte = IT_STRING_BYTEPOS (*it);
5176 string = it->string;
5177 s = SDATA (string) + pos_byte;
5178 it->c = STRING_CHAR (s);
5179 }
5180 else
5181 {
5182 pos = IT_CHARPOS (*it);
5183 pos_byte = IT_BYTEPOS (*it);
5184 string = Qnil;
5185 it->c = FETCH_CHAR (pos_byte);
5186 }
5187
5188 /* If there's a valid composition and point is not inside of the
5189 composition (in the case that the composition is from the current
5190 buffer), draw a glyph composed from the composition components. */
5191 if (find_composition (pos, -1, &start, &end, &prop, string)
5192 && COMPOSITION_VALID_P (start, end, prop)
5193 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5194 {
5195 if (start < pos)
5196 /* As we can't handle this situation (perhaps font-lock added
5197 a new composition), we just return here hoping that next
5198 redisplay will detect this composition much earlier. */
5199 return HANDLED_NORMALLY;
5200 if (start != pos)
5201 {
5202 if (STRINGP (it->string))
5203 pos_byte = string_char_to_byte (it->string, start);
5204 else
5205 pos_byte = CHAR_TO_BYTE (start);
5206 }
5207 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5208 prop, string);
5209
5210 if (it->cmp_it.id >= 0)
5211 {
5212 it->cmp_it.ch = -1;
5213 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5214 it->cmp_it.nglyphs = -1;
5215 }
5216 }
5217
5218 return HANDLED_NORMALLY;
5219 }
5220
5221
5222 \f
5223 /***********************************************************************
5224 Overlay strings
5225 ***********************************************************************/
5226
5227 /* The following structure is used to record overlay strings for
5228 later sorting in load_overlay_strings. */
5229
5230 struct overlay_entry
5231 {
5232 Lisp_Object overlay;
5233 Lisp_Object string;
5234 EMACS_INT priority;
5235 int after_string_p;
5236 };
5237
5238
5239 /* Set up iterator IT from overlay strings at its current position.
5240 Called from handle_stop. */
5241
5242 static enum prop_handled
5243 handle_overlay_change (struct it *it)
5244 {
5245 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5246 return HANDLED_RECOMPUTE_PROPS;
5247 else
5248 return HANDLED_NORMALLY;
5249 }
5250
5251
5252 /* Set up the next overlay string for delivery by IT, if there is an
5253 overlay string to deliver. Called by set_iterator_to_next when the
5254 end of the current overlay string is reached. If there are more
5255 overlay strings to display, IT->string and
5256 IT->current.overlay_string_index are set appropriately here.
5257 Otherwise IT->string is set to nil. */
5258
5259 static void
5260 next_overlay_string (struct it *it)
5261 {
5262 ++it->current.overlay_string_index;
5263 if (it->current.overlay_string_index == it->n_overlay_strings)
5264 {
5265 /* No more overlay strings. Restore IT's settings to what
5266 they were before overlay strings were processed, and
5267 continue to deliver from current_buffer. */
5268
5269 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5270 pop_it (it);
5271 eassert (it->sp > 0
5272 || (NILP (it->string)
5273 && it->method == GET_FROM_BUFFER
5274 && it->stop_charpos >= BEGV
5275 && it->stop_charpos <= it->end_charpos));
5276 it->current.overlay_string_index = -1;
5277 it->n_overlay_strings = 0;
5278 it->overlay_strings_charpos = -1;
5279 /* If there's an empty display string on the stack, pop the
5280 stack, to resync the bidi iterator with IT's position. Such
5281 empty strings are pushed onto the stack in
5282 get_overlay_strings_1. */
5283 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5284 pop_it (it);
5285
5286 /* If we're at the end of the buffer, record that we have
5287 processed the overlay strings there already, so that
5288 next_element_from_buffer doesn't try it again. */
5289 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5290 it->overlay_strings_at_end_processed_p = 1;
5291 }
5292 else
5293 {
5294 /* There are more overlay strings to process. If
5295 IT->current.overlay_string_index has advanced to a position
5296 where we must load IT->overlay_strings with more strings, do
5297 it. We must load at the IT->overlay_strings_charpos where
5298 IT->n_overlay_strings was originally computed; when invisible
5299 text is present, this might not be IT_CHARPOS (Bug#7016). */
5300 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5301
5302 if (it->current.overlay_string_index && i == 0)
5303 load_overlay_strings (it, it->overlay_strings_charpos);
5304
5305 /* Initialize IT to deliver display elements from the overlay
5306 string. */
5307 it->string = it->overlay_strings[i];
5308 it->multibyte_p = STRING_MULTIBYTE (it->string);
5309 SET_TEXT_POS (it->current.string_pos, 0, 0);
5310 it->method = GET_FROM_STRING;
5311 it->stop_charpos = 0;
5312 if (it->cmp_it.stop_pos >= 0)
5313 it->cmp_it.stop_pos = 0;
5314 it->prev_stop = 0;
5315 it->base_level_stop = 0;
5316
5317 /* Set up the bidi iterator for this overlay string. */
5318 if (it->bidi_p)
5319 {
5320 it->bidi_it.string.lstring = it->string;
5321 it->bidi_it.string.s = NULL;
5322 it->bidi_it.string.schars = SCHARS (it->string);
5323 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5324 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5325 it->bidi_it.string.unibyte = !it->multibyte_p;
5326 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5327 }
5328 }
5329
5330 CHECK_IT (it);
5331 }
5332
5333
5334 /* Compare two overlay_entry structures E1 and E2. Used as a
5335 comparison function for qsort in load_overlay_strings. Overlay
5336 strings for the same position are sorted so that
5337
5338 1. All after-strings come in front of before-strings, except
5339 when they come from the same overlay.
5340
5341 2. Within after-strings, strings are sorted so that overlay strings
5342 from overlays with higher priorities come first.
5343
5344 2. Within before-strings, strings are sorted so that overlay
5345 strings from overlays with higher priorities come last.
5346
5347 Value is analogous to strcmp. */
5348
5349
5350 static int
5351 compare_overlay_entries (const void *e1, const void *e2)
5352 {
5353 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5354 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5355 int result;
5356
5357 if (entry1->after_string_p != entry2->after_string_p)
5358 {
5359 /* Let after-strings appear in front of before-strings if
5360 they come from different overlays. */
5361 if (EQ (entry1->overlay, entry2->overlay))
5362 result = entry1->after_string_p ? 1 : -1;
5363 else
5364 result = entry1->after_string_p ? -1 : 1;
5365 }
5366 else if (entry1->priority != entry2->priority)
5367 {
5368 if (entry1->after_string_p)
5369 /* After-strings sorted in order of decreasing priority. */
5370 result = entry2->priority < entry1->priority ? -1 : 1;
5371 else
5372 /* Before-strings sorted in order of increasing priority. */
5373 result = entry1->priority < entry2->priority ? -1 : 1;
5374 }
5375 else
5376 result = 0;
5377
5378 return result;
5379 }
5380
5381
5382 /* Load the vector IT->overlay_strings with overlay strings from IT's
5383 current buffer position, or from CHARPOS if that is > 0. Set
5384 IT->n_overlays to the total number of overlay strings found.
5385
5386 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5387 a time. On entry into load_overlay_strings,
5388 IT->current.overlay_string_index gives the number of overlay
5389 strings that have already been loaded by previous calls to this
5390 function.
5391
5392 IT->add_overlay_start contains an additional overlay start
5393 position to consider for taking overlay strings from, if non-zero.
5394 This position comes into play when the overlay has an `invisible'
5395 property, and both before and after-strings. When we've skipped to
5396 the end of the overlay, because of its `invisible' property, we
5397 nevertheless want its before-string to appear.
5398 IT->add_overlay_start will contain the overlay start position
5399 in this case.
5400
5401 Overlay strings are sorted so that after-string strings come in
5402 front of before-string strings. Within before and after-strings,
5403 strings are sorted by overlay priority. See also function
5404 compare_overlay_entries. */
5405
5406 static void
5407 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5408 {
5409 Lisp_Object overlay, window, str, invisible;
5410 struct Lisp_Overlay *ov;
5411 ptrdiff_t start, end;
5412 ptrdiff_t size = 20;
5413 ptrdiff_t n = 0, i, j;
5414 int invis_p;
5415 struct overlay_entry *entries = alloca (size * sizeof *entries);
5416 USE_SAFE_ALLOCA;
5417
5418 if (charpos <= 0)
5419 charpos = IT_CHARPOS (*it);
5420
5421 /* Append the overlay string STRING of overlay OVERLAY to vector
5422 `entries' which has size `size' and currently contains `n'
5423 elements. AFTER_P non-zero means STRING is an after-string of
5424 OVERLAY. */
5425 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5426 do \
5427 { \
5428 Lisp_Object priority; \
5429 \
5430 if (n == size) \
5431 { \
5432 struct overlay_entry *old = entries; \
5433 SAFE_NALLOCA (entries, 2, size); \
5434 memcpy (entries, old, size * sizeof *entries); \
5435 size *= 2; \
5436 } \
5437 \
5438 entries[n].string = (STRING); \
5439 entries[n].overlay = (OVERLAY); \
5440 priority = Foverlay_get ((OVERLAY), Qpriority); \
5441 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5442 entries[n].after_string_p = (AFTER_P); \
5443 ++n; \
5444 } \
5445 while (0)
5446
5447 /* Process overlay before the overlay center. */
5448 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5449 {
5450 XSETMISC (overlay, ov);
5451 eassert (OVERLAYP (overlay));
5452 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5453 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5454
5455 if (end < charpos)
5456 break;
5457
5458 /* Skip this overlay if it doesn't start or end at IT's current
5459 position. */
5460 if (end != charpos && start != charpos)
5461 continue;
5462
5463 /* Skip this overlay if it doesn't apply to IT->w. */
5464 window = Foverlay_get (overlay, Qwindow);
5465 if (WINDOWP (window) && XWINDOW (window) != it->w)
5466 continue;
5467
5468 /* If the text ``under'' the overlay is invisible, both before-
5469 and after-strings from this overlay are visible; start and
5470 end position are indistinguishable. */
5471 invisible = Foverlay_get (overlay, Qinvisible);
5472 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5473
5474 /* If overlay has a non-empty before-string, record it. */
5475 if ((start == charpos || (end == charpos && invis_p))
5476 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5477 && SCHARS (str))
5478 RECORD_OVERLAY_STRING (overlay, str, 0);
5479
5480 /* If overlay has a non-empty after-string, record it. */
5481 if ((end == charpos || (start == charpos && invis_p))
5482 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5483 && SCHARS (str))
5484 RECORD_OVERLAY_STRING (overlay, str, 1);
5485 }
5486
5487 /* Process overlays after the overlay center. */
5488 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5489 {
5490 XSETMISC (overlay, ov);
5491 eassert (OVERLAYP (overlay));
5492 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5493 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5494
5495 if (start > charpos)
5496 break;
5497
5498 /* Skip this overlay if it doesn't start or end at IT's current
5499 position. */
5500 if (end != charpos && start != charpos)
5501 continue;
5502
5503 /* Skip this overlay if it doesn't apply to IT->w. */
5504 window = Foverlay_get (overlay, Qwindow);
5505 if (WINDOWP (window) && XWINDOW (window) != it->w)
5506 continue;
5507
5508 /* If the text ``under'' the overlay is invisible, it has a zero
5509 dimension, and both before- and after-strings apply. */
5510 invisible = Foverlay_get (overlay, Qinvisible);
5511 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5512
5513 /* If overlay has a non-empty before-string, record it. */
5514 if ((start == charpos || (end == charpos && invis_p))
5515 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5516 && SCHARS (str))
5517 RECORD_OVERLAY_STRING (overlay, str, 0);
5518
5519 /* If overlay has a non-empty after-string, record it. */
5520 if ((end == charpos || (start == charpos && invis_p))
5521 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5522 && SCHARS (str))
5523 RECORD_OVERLAY_STRING (overlay, str, 1);
5524 }
5525
5526 #undef RECORD_OVERLAY_STRING
5527
5528 /* Sort entries. */
5529 if (n > 1)
5530 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5531
5532 /* Record number of overlay strings, and where we computed it. */
5533 it->n_overlay_strings = n;
5534 it->overlay_strings_charpos = charpos;
5535
5536 /* IT->current.overlay_string_index is the number of overlay strings
5537 that have already been consumed by IT. Copy some of the
5538 remaining overlay strings to IT->overlay_strings. */
5539 i = 0;
5540 j = it->current.overlay_string_index;
5541 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5542 {
5543 it->overlay_strings[i] = entries[j].string;
5544 it->string_overlays[i++] = entries[j++].overlay;
5545 }
5546
5547 CHECK_IT (it);
5548 SAFE_FREE ();
5549 }
5550
5551
5552 /* Get the first chunk of overlay strings at IT's current buffer
5553 position, or at CHARPOS if that is > 0. Value is non-zero if at
5554 least one overlay string was found. */
5555
5556 static int
5557 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5558 {
5559 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5560 process. This fills IT->overlay_strings with strings, and sets
5561 IT->n_overlay_strings to the total number of strings to process.
5562 IT->pos.overlay_string_index has to be set temporarily to zero
5563 because load_overlay_strings needs this; it must be set to -1
5564 when no overlay strings are found because a zero value would
5565 indicate a position in the first overlay string. */
5566 it->current.overlay_string_index = 0;
5567 load_overlay_strings (it, charpos);
5568
5569 /* If we found overlay strings, set up IT to deliver display
5570 elements from the first one. Otherwise set up IT to deliver
5571 from current_buffer. */
5572 if (it->n_overlay_strings)
5573 {
5574 /* Make sure we know settings in current_buffer, so that we can
5575 restore meaningful values when we're done with the overlay
5576 strings. */
5577 if (compute_stop_p)
5578 compute_stop_pos (it);
5579 eassert (it->face_id >= 0);
5580
5581 /* Save IT's settings. They are restored after all overlay
5582 strings have been processed. */
5583 eassert (!compute_stop_p || it->sp == 0);
5584
5585 /* When called from handle_stop, there might be an empty display
5586 string loaded. In that case, don't bother saving it. But
5587 don't use this optimization with the bidi iterator, since we
5588 need the corresponding pop_it call to resync the bidi
5589 iterator's position with IT's position, after we are done
5590 with the overlay strings. (The corresponding call to pop_it
5591 in case of an empty display string is in
5592 next_overlay_string.) */
5593 if (!(!it->bidi_p
5594 && STRINGP (it->string) && !SCHARS (it->string)))
5595 push_it (it, NULL);
5596
5597 /* Set up IT to deliver display elements from the first overlay
5598 string. */
5599 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5600 it->string = it->overlay_strings[0];
5601 it->from_overlay = Qnil;
5602 it->stop_charpos = 0;
5603 eassert (STRINGP (it->string));
5604 it->end_charpos = SCHARS (it->string);
5605 it->prev_stop = 0;
5606 it->base_level_stop = 0;
5607 it->multibyte_p = STRING_MULTIBYTE (it->string);
5608 it->method = GET_FROM_STRING;
5609 it->from_disp_prop_p = 0;
5610
5611 /* Force paragraph direction to be that of the parent
5612 buffer. */
5613 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5614 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5615 else
5616 it->paragraph_embedding = L2R;
5617
5618 /* Set up the bidi iterator for this overlay string. */
5619 if (it->bidi_p)
5620 {
5621 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5622
5623 it->bidi_it.string.lstring = it->string;
5624 it->bidi_it.string.s = NULL;
5625 it->bidi_it.string.schars = SCHARS (it->string);
5626 it->bidi_it.string.bufpos = pos;
5627 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5628 it->bidi_it.string.unibyte = !it->multibyte_p;
5629 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5630 }
5631 return 1;
5632 }
5633
5634 it->current.overlay_string_index = -1;
5635 return 0;
5636 }
5637
5638 static int
5639 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5640 {
5641 it->string = Qnil;
5642 it->method = GET_FROM_BUFFER;
5643
5644 (void) get_overlay_strings_1 (it, charpos, 1);
5645
5646 CHECK_IT (it);
5647
5648 /* Value is non-zero if we found at least one overlay string. */
5649 return STRINGP (it->string);
5650 }
5651
5652
5653 \f
5654 /***********************************************************************
5655 Saving and restoring state
5656 ***********************************************************************/
5657
5658 /* Save current settings of IT on IT->stack. Called, for example,
5659 before setting up IT for an overlay string, to be able to restore
5660 IT's settings to what they were after the overlay string has been
5661 processed. If POSITION is non-NULL, it is the position to save on
5662 the stack instead of IT->position. */
5663
5664 static void
5665 push_it (struct it *it, struct text_pos *position)
5666 {
5667 struct iterator_stack_entry *p;
5668
5669 eassert (it->sp < IT_STACK_SIZE);
5670 p = it->stack + it->sp;
5671
5672 p->stop_charpos = it->stop_charpos;
5673 p->prev_stop = it->prev_stop;
5674 p->base_level_stop = it->base_level_stop;
5675 p->cmp_it = it->cmp_it;
5676 eassert (it->face_id >= 0);
5677 p->face_id = it->face_id;
5678 p->string = it->string;
5679 p->method = it->method;
5680 p->from_overlay = it->from_overlay;
5681 switch (p->method)
5682 {
5683 case GET_FROM_IMAGE:
5684 p->u.image.object = it->object;
5685 p->u.image.image_id = it->image_id;
5686 p->u.image.slice = it->slice;
5687 break;
5688 case GET_FROM_STRETCH:
5689 p->u.stretch.object = it->object;
5690 break;
5691 }
5692 p->position = position ? *position : it->position;
5693 p->current = it->current;
5694 p->end_charpos = it->end_charpos;
5695 p->string_nchars = it->string_nchars;
5696 p->area = it->area;
5697 p->multibyte_p = it->multibyte_p;
5698 p->avoid_cursor_p = it->avoid_cursor_p;
5699 p->space_width = it->space_width;
5700 p->font_height = it->font_height;
5701 p->voffset = it->voffset;
5702 p->string_from_display_prop_p = it->string_from_display_prop_p;
5703 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5704 p->display_ellipsis_p = 0;
5705 p->line_wrap = it->line_wrap;
5706 p->bidi_p = it->bidi_p;
5707 p->paragraph_embedding = it->paragraph_embedding;
5708 p->from_disp_prop_p = it->from_disp_prop_p;
5709 ++it->sp;
5710
5711 /* Save the state of the bidi iterator as well. */
5712 if (it->bidi_p)
5713 bidi_push_it (&it->bidi_it);
5714 }
5715
5716 static void
5717 iterate_out_of_display_property (struct it *it)
5718 {
5719 int buffer_p = !STRINGP (it->string);
5720 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5721 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5722
5723 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5724
5725 /* Maybe initialize paragraph direction. If we are at the beginning
5726 of a new paragraph, next_element_from_buffer may not have a
5727 chance to do that. */
5728 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5729 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5730 /* prev_stop can be zero, so check against BEGV as well. */
5731 while (it->bidi_it.charpos >= bob
5732 && it->prev_stop <= it->bidi_it.charpos
5733 && it->bidi_it.charpos < CHARPOS (it->position)
5734 && it->bidi_it.charpos < eob)
5735 bidi_move_to_visually_next (&it->bidi_it);
5736 /* Record the stop_pos we just crossed, for when we cross it
5737 back, maybe. */
5738 if (it->bidi_it.charpos > CHARPOS (it->position))
5739 it->prev_stop = CHARPOS (it->position);
5740 /* If we ended up not where pop_it put us, resync IT's
5741 positional members with the bidi iterator. */
5742 if (it->bidi_it.charpos != CHARPOS (it->position))
5743 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5744 if (buffer_p)
5745 it->current.pos = it->position;
5746 else
5747 it->current.string_pos = it->position;
5748 }
5749
5750 /* Restore IT's settings from IT->stack. Called, for example, when no
5751 more overlay strings must be processed, and we return to delivering
5752 display elements from a buffer, or when the end of a string from a
5753 `display' property is reached and we return to delivering display
5754 elements from an overlay string, or from a buffer. */
5755
5756 static void
5757 pop_it (struct it *it)
5758 {
5759 struct iterator_stack_entry *p;
5760 int from_display_prop = it->from_disp_prop_p;
5761
5762 eassert (it->sp > 0);
5763 --it->sp;
5764 p = it->stack + it->sp;
5765 it->stop_charpos = p->stop_charpos;
5766 it->prev_stop = p->prev_stop;
5767 it->base_level_stop = p->base_level_stop;
5768 it->cmp_it = p->cmp_it;
5769 it->face_id = p->face_id;
5770 it->current = p->current;
5771 it->position = p->position;
5772 it->string = p->string;
5773 it->from_overlay = p->from_overlay;
5774 if (NILP (it->string))
5775 SET_TEXT_POS (it->current.string_pos, -1, -1);
5776 it->method = p->method;
5777 switch (it->method)
5778 {
5779 case GET_FROM_IMAGE:
5780 it->image_id = p->u.image.image_id;
5781 it->object = p->u.image.object;
5782 it->slice = p->u.image.slice;
5783 break;
5784 case GET_FROM_STRETCH:
5785 it->object = p->u.stretch.object;
5786 break;
5787 case GET_FROM_BUFFER:
5788 it->object = WVAR (it->w, buffer);
5789 break;
5790 case GET_FROM_STRING:
5791 it->object = it->string;
5792 break;
5793 case GET_FROM_DISPLAY_VECTOR:
5794 if (it->s)
5795 it->method = GET_FROM_C_STRING;
5796 else if (STRINGP (it->string))
5797 it->method = GET_FROM_STRING;
5798 else
5799 {
5800 it->method = GET_FROM_BUFFER;
5801 it->object = WVAR (it->w, buffer);
5802 }
5803 }
5804 it->end_charpos = p->end_charpos;
5805 it->string_nchars = p->string_nchars;
5806 it->area = p->area;
5807 it->multibyte_p = p->multibyte_p;
5808 it->avoid_cursor_p = p->avoid_cursor_p;
5809 it->space_width = p->space_width;
5810 it->font_height = p->font_height;
5811 it->voffset = p->voffset;
5812 it->string_from_display_prop_p = p->string_from_display_prop_p;
5813 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5814 it->line_wrap = p->line_wrap;
5815 it->bidi_p = p->bidi_p;
5816 it->paragraph_embedding = p->paragraph_embedding;
5817 it->from_disp_prop_p = p->from_disp_prop_p;
5818 if (it->bidi_p)
5819 {
5820 bidi_pop_it (&it->bidi_it);
5821 /* Bidi-iterate until we get out of the portion of text, if any,
5822 covered by a `display' text property or by an overlay with
5823 `display' property. (We cannot just jump there, because the
5824 internal coherency of the bidi iterator state can not be
5825 preserved across such jumps.) We also must determine the
5826 paragraph base direction if the overlay we just processed is
5827 at the beginning of a new paragraph. */
5828 if (from_display_prop
5829 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5830 iterate_out_of_display_property (it);
5831
5832 eassert ((BUFFERP (it->object)
5833 && IT_CHARPOS (*it) == it->bidi_it.charpos
5834 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5835 || (STRINGP (it->object)
5836 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5837 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5838 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5839 }
5840 }
5841
5842
5843 \f
5844 /***********************************************************************
5845 Moving over lines
5846 ***********************************************************************/
5847
5848 /* Set IT's current position to the previous line start. */
5849
5850 static void
5851 back_to_previous_line_start (struct it *it)
5852 {
5853 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5854 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5855 }
5856
5857
5858 /* Move IT to the next line start.
5859
5860 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5861 we skipped over part of the text (as opposed to moving the iterator
5862 continuously over the text). Otherwise, don't change the value
5863 of *SKIPPED_P.
5864
5865 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5866 iterator on the newline, if it was found.
5867
5868 Newlines may come from buffer text, overlay strings, or strings
5869 displayed via the `display' property. That's the reason we can't
5870 simply use find_next_newline_no_quit.
5871
5872 Note that this function may not skip over invisible text that is so
5873 because of text properties and immediately follows a newline. If
5874 it would, function reseat_at_next_visible_line_start, when called
5875 from set_iterator_to_next, would effectively make invisible
5876 characters following a newline part of the wrong glyph row, which
5877 leads to wrong cursor motion. */
5878
5879 static int
5880 forward_to_next_line_start (struct it *it, int *skipped_p,
5881 struct bidi_it *bidi_it_prev)
5882 {
5883 ptrdiff_t old_selective;
5884 int newline_found_p, n;
5885 const int MAX_NEWLINE_DISTANCE = 500;
5886
5887 /* If already on a newline, just consume it to avoid unintended
5888 skipping over invisible text below. */
5889 if (it->what == IT_CHARACTER
5890 && it->c == '\n'
5891 && CHARPOS (it->position) == IT_CHARPOS (*it))
5892 {
5893 if (it->bidi_p && bidi_it_prev)
5894 *bidi_it_prev = it->bidi_it;
5895 set_iterator_to_next (it, 0);
5896 it->c = 0;
5897 return 1;
5898 }
5899
5900 /* Don't handle selective display in the following. It's (a)
5901 unnecessary because it's done by the caller, and (b) leads to an
5902 infinite recursion because next_element_from_ellipsis indirectly
5903 calls this function. */
5904 old_selective = it->selective;
5905 it->selective = 0;
5906
5907 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5908 from buffer text. */
5909 for (n = newline_found_p = 0;
5910 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5911 n += STRINGP (it->string) ? 0 : 1)
5912 {
5913 if (!get_next_display_element (it))
5914 return 0;
5915 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5916 if (newline_found_p && it->bidi_p && bidi_it_prev)
5917 *bidi_it_prev = it->bidi_it;
5918 set_iterator_to_next (it, 0);
5919 }
5920
5921 /* If we didn't find a newline near enough, see if we can use a
5922 short-cut. */
5923 if (!newline_found_p)
5924 {
5925 ptrdiff_t start = IT_CHARPOS (*it);
5926 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5927 Lisp_Object pos;
5928
5929 eassert (!STRINGP (it->string));
5930
5931 /* If there isn't any `display' property in sight, and no
5932 overlays, we can just use the position of the newline in
5933 buffer text. */
5934 if (it->stop_charpos >= limit
5935 || ((pos = Fnext_single_property_change (make_number (start),
5936 Qdisplay, Qnil,
5937 make_number (limit)),
5938 NILP (pos))
5939 && next_overlay_change (start) == ZV))
5940 {
5941 if (!it->bidi_p)
5942 {
5943 IT_CHARPOS (*it) = limit;
5944 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5945 }
5946 else
5947 {
5948 struct bidi_it bprev;
5949
5950 /* Help bidi.c avoid expensive searches for display
5951 properties and overlays, by telling it that there are
5952 none up to `limit'. */
5953 if (it->bidi_it.disp_pos < limit)
5954 {
5955 it->bidi_it.disp_pos = limit;
5956 it->bidi_it.disp_prop = 0;
5957 }
5958 do {
5959 bprev = it->bidi_it;
5960 bidi_move_to_visually_next (&it->bidi_it);
5961 } while (it->bidi_it.charpos != limit);
5962 IT_CHARPOS (*it) = limit;
5963 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5964 if (bidi_it_prev)
5965 *bidi_it_prev = bprev;
5966 }
5967 *skipped_p = newline_found_p = 1;
5968 }
5969 else
5970 {
5971 while (get_next_display_element (it)
5972 && !newline_found_p)
5973 {
5974 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5975 if (newline_found_p && it->bidi_p && bidi_it_prev)
5976 *bidi_it_prev = it->bidi_it;
5977 set_iterator_to_next (it, 0);
5978 }
5979 }
5980 }
5981
5982 it->selective = old_selective;
5983 return newline_found_p;
5984 }
5985
5986
5987 /* Set IT's current position to the previous visible line start. Skip
5988 invisible text that is so either due to text properties or due to
5989 selective display. Caution: this does not change IT->current_x and
5990 IT->hpos. */
5991
5992 static void
5993 back_to_previous_visible_line_start (struct it *it)
5994 {
5995 while (IT_CHARPOS (*it) > BEGV)
5996 {
5997 back_to_previous_line_start (it);
5998
5999 if (IT_CHARPOS (*it) <= BEGV)
6000 break;
6001
6002 /* If selective > 0, then lines indented more than its value are
6003 invisible. */
6004 if (it->selective > 0
6005 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6006 it->selective))
6007 continue;
6008
6009 /* Check the newline before point for invisibility. */
6010 {
6011 Lisp_Object prop;
6012 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6013 Qinvisible, it->window);
6014 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6015 continue;
6016 }
6017
6018 if (IT_CHARPOS (*it) <= BEGV)
6019 break;
6020
6021 {
6022 struct it it2;
6023 void *it2data = NULL;
6024 ptrdiff_t pos;
6025 ptrdiff_t beg, end;
6026 Lisp_Object val, overlay;
6027
6028 SAVE_IT (it2, *it, it2data);
6029
6030 /* If newline is part of a composition, continue from start of composition */
6031 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6032 && beg < IT_CHARPOS (*it))
6033 goto replaced;
6034
6035 /* If newline is replaced by a display property, find start of overlay
6036 or interval and continue search from that point. */
6037 pos = --IT_CHARPOS (it2);
6038 --IT_BYTEPOS (it2);
6039 it2.sp = 0;
6040 bidi_unshelve_cache (NULL, 0);
6041 it2.string_from_display_prop_p = 0;
6042 it2.from_disp_prop_p = 0;
6043 if (handle_display_prop (&it2) == HANDLED_RETURN
6044 && !NILP (val = get_char_property_and_overlay
6045 (make_number (pos), Qdisplay, Qnil, &overlay))
6046 && (OVERLAYP (overlay)
6047 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6048 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6049 {
6050 RESTORE_IT (it, it, it2data);
6051 goto replaced;
6052 }
6053
6054 /* Newline is not replaced by anything -- so we are done. */
6055 RESTORE_IT (it, it, it2data);
6056 break;
6057
6058 replaced:
6059 if (beg < BEGV)
6060 beg = BEGV;
6061 IT_CHARPOS (*it) = beg;
6062 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6063 }
6064 }
6065
6066 it->continuation_lines_width = 0;
6067
6068 eassert (IT_CHARPOS (*it) >= BEGV);
6069 eassert (IT_CHARPOS (*it) == BEGV
6070 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6071 CHECK_IT (it);
6072 }
6073
6074
6075 /* Reseat iterator IT at the previous visible line start. Skip
6076 invisible text that is so either due to text properties or due to
6077 selective display. At the end, update IT's overlay information,
6078 face information etc. */
6079
6080 void
6081 reseat_at_previous_visible_line_start (struct it *it)
6082 {
6083 back_to_previous_visible_line_start (it);
6084 reseat (it, it->current.pos, 1);
6085 CHECK_IT (it);
6086 }
6087
6088
6089 /* Reseat iterator IT on the next visible line start in the current
6090 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6091 preceding the line start. Skip over invisible text that is so
6092 because of selective display. Compute faces, overlays etc at the
6093 new position. Note that this function does not skip over text that
6094 is invisible because of text properties. */
6095
6096 static void
6097 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6098 {
6099 int newline_found_p, skipped_p = 0;
6100 struct bidi_it bidi_it_prev;
6101
6102 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6103
6104 /* Skip over lines that are invisible because they are indented
6105 more than the value of IT->selective. */
6106 if (it->selective > 0)
6107 while (IT_CHARPOS (*it) < ZV
6108 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6109 it->selective))
6110 {
6111 eassert (IT_BYTEPOS (*it) == BEGV
6112 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6113 newline_found_p =
6114 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6115 }
6116
6117 /* Position on the newline if that's what's requested. */
6118 if (on_newline_p && newline_found_p)
6119 {
6120 if (STRINGP (it->string))
6121 {
6122 if (IT_STRING_CHARPOS (*it) > 0)
6123 {
6124 if (!it->bidi_p)
6125 {
6126 --IT_STRING_CHARPOS (*it);
6127 --IT_STRING_BYTEPOS (*it);
6128 }
6129 else
6130 {
6131 /* We need to restore the bidi iterator to the state
6132 it had on the newline, and resync the IT's
6133 position with that. */
6134 it->bidi_it = bidi_it_prev;
6135 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6136 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6137 }
6138 }
6139 }
6140 else if (IT_CHARPOS (*it) > BEGV)
6141 {
6142 if (!it->bidi_p)
6143 {
6144 --IT_CHARPOS (*it);
6145 --IT_BYTEPOS (*it);
6146 }
6147 else
6148 {
6149 /* We need to restore the bidi iterator to the state it
6150 had on the newline and resync IT with that. */
6151 it->bidi_it = bidi_it_prev;
6152 IT_CHARPOS (*it) = it->bidi_it.charpos;
6153 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6154 }
6155 reseat (it, it->current.pos, 0);
6156 }
6157 }
6158 else if (skipped_p)
6159 reseat (it, it->current.pos, 0);
6160
6161 CHECK_IT (it);
6162 }
6163
6164
6165 \f
6166 /***********************************************************************
6167 Changing an iterator's position
6168 ***********************************************************************/
6169
6170 /* Change IT's current position to POS in current_buffer. If FORCE_P
6171 is non-zero, always check for text properties at the new position.
6172 Otherwise, text properties are only looked up if POS >=
6173 IT->check_charpos of a property. */
6174
6175 static void
6176 reseat (struct it *it, struct text_pos pos, int force_p)
6177 {
6178 ptrdiff_t original_pos = IT_CHARPOS (*it);
6179
6180 reseat_1 (it, pos, 0);
6181
6182 /* Determine where to check text properties. Avoid doing it
6183 where possible because text property lookup is very expensive. */
6184 if (force_p
6185 || CHARPOS (pos) > it->stop_charpos
6186 || CHARPOS (pos) < original_pos)
6187 {
6188 if (it->bidi_p)
6189 {
6190 /* For bidi iteration, we need to prime prev_stop and
6191 base_level_stop with our best estimations. */
6192 /* Implementation note: Of course, POS is not necessarily a
6193 stop position, so assigning prev_pos to it is a lie; we
6194 should have called compute_stop_backwards. However, if
6195 the current buffer does not include any R2L characters,
6196 that call would be a waste of cycles, because the
6197 iterator will never move back, and thus never cross this
6198 "fake" stop position. So we delay that backward search
6199 until the time we really need it, in next_element_from_buffer. */
6200 if (CHARPOS (pos) != it->prev_stop)
6201 it->prev_stop = CHARPOS (pos);
6202 if (CHARPOS (pos) < it->base_level_stop)
6203 it->base_level_stop = 0; /* meaning it's unknown */
6204 handle_stop (it);
6205 }
6206 else
6207 {
6208 handle_stop (it);
6209 it->prev_stop = it->base_level_stop = 0;
6210 }
6211
6212 }
6213
6214 CHECK_IT (it);
6215 }
6216
6217
6218 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6219 IT->stop_pos to POS, also. */
6220
6221 static void
6222 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6223 {
6224 /* Don't call this function when scanning a C string. */
6225 eassert (it->s == NULL);
6226
6227 /* POS must be a reasonable value. */
6228 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6229
6230 it->current.pos = it->position = pos;
6231 it->end_charpos = ZV;
6232 it->dpvec = NULL;
6233 it->current.dpvec_index = -1;
6234 it->current.overlay_string_index = -1;
6235 IT_STRING_CHARPOS (*it) = -1;
6236 IT_STRING_BYTEPOS (*it) = -1;
6237 it->string = Qnil;
6238 it->method = GET_FROM_BUFFER;
6239 it->object = WVAR (it->w, buffer);
6240 it->area = TEXT_AREA;
6241 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6242 it->sp = 0;
6243 it->string_from_display_prop_p = 0;
6244 it->string_from_prefix_prop_p = 0;
6245
6246 it->from_disp_prop_p = 0;
6247 it->face_before_selective_p = 0;
6248 if (it->bidi_p)
6249 {
6250 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6251 &it->bidi_it);
6252 bidi_unshelve_cache (NULL, 0);
6253 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6254 it->bidi_it.string.s = NULL;
6255 it->bidi_it.string.lstring = Qnil;
6256 it->bidi_it.string.bufpos = 0;
6257 it->bidi_it.string.unibyte = 0;
6258 }
6259
6260 if (set_stop_p)
6261 {
6262 it->stop_charpos = CHARPOS (pos);
6263 it->base_level_stop = CHARPOS (pos);
6264 }
6265 }
6266
6267
6268 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6269 If S is non-null, it is a C string to iterate over. Otherwise,
6270 STRING gives a Lisp string to iterate over.
6271
6272 If PRECISION > 0, don't return more then PRECISION number of
6273 characters from the string.
6274
6275 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6276 characters have been returned. FIELD_WIDTH < 0 means an infinite
6277 field width.
6278
6279 MULTIBYTE = 0 means disable processing of multibyte characters,
6280 MULTIBYTE > 0 means enable it,
6281 MULTIBYTE < 0 means use IT->multibyte_p.
6282
6283 IT must be initialized via a prior call to init_iterator before
6284 calling this function. */
6285
6286 static void
6287 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6288 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6289 int multibyte)
6290 {
6291 /* No region in strings. */
6292 it->region_beg_charpos = it->region_end_charpos = -1;
6293
6294 /* No text property checks performed by default, but see below. */
6295 it->stop_charpos = -1;
6296
6297 /* Set iterator position and end position. */
6298 memset (&it->current, 0, sizeof it->current);
6299 it->current.overlay_string_index = -1;
6300 it->current.dpvec_index = -1;
6301 eassert (charpos >= 0);
6302
6303 /* If STRING is specified, use its multibyteness, otherwise use the
6304 setting of MULTIBYTE, if specified. */
6305 if (multibyte >= 0)
6306 it->multibyte_p = multibyte > 0;
6307
6308 /* Bidirectional reordering of strings is controlled by the default
6309 value of bidi-display-reordering. Don't try to reorder while
6310 loading loadup.el, as the necessary character property tables are
6311 not yet available. */
6312 it->bidi_p =
6313 NILP (Vpurify_flag)
6314 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6315
6316 if (s == NULL)
6317 {
6318 eassert (STRINGP (string));
6319 it->string = string;
6320 it->s = NULL;
6321 it->end_charpos = it->string_nchars = SCHARS (string);
6322 it->method = GET_FROM_STRING;
6323 it->current.string_pos = string_pos (charpos, string);
6324
6325 if (it->bidi_p)
6326 {
6327 it->bidi_it.string.lstring = string;
6328 it->bidi_it.string.s = NULL;
6329 it->bidi_it.string.schars = it->end_charpos;
6330 it->bidi_it.string.bufpos = 0;
6331 it->bidi_it.string.from_disp_str = 0;
6332 it->bidi_it.string.unibyte = !it->multibyte_p;
6333 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6334 FRAME_WINDOW_P (it->f), &it->bidi_it);
6335 }
6336 }
6337 else
6338 {
6339 it->s = (const unsigned char *) s;
6340 it->string = Qnil;
6341
6342 /* Note that we use IT->current.pos, not it->current.string_pos,
6343 for displaying C strings. */
6344 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6345 if (it->multibyte_p)
6346 {
6347 it->current.pos = c_string_pos (charpos, s, 1);
6348 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6349 }
6350 else
6351 {
6352 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6353 it->end_charpos = it->string_nchars = strlen (s);
6354 }
6355
6356 if (it->bidi_p)
6357 {
6358 it->bidi_it.string.lstring = Qnil;
6359 it->bidi_it.string.s = (const unsigned char *) s;
6360 it->bidi_it.string.schars = it->end_charpos;
6361 it->bidi_it.string.bufpos = 0;
6362 it->bidi_it.string.from_disp_str = 0;
6363 it->bidi_it.string.unibyte = !it->multibyte_p;
6364 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6365 &it->bidi_it);
6366 }
6367 it->method = GET_FROM_C_STRING;
6368 }
6369
6370 /* PRECISION > 0 means don't return more than PRECISION characters
6371 from the string. */
6372 if (precision > 0 && it->end_charpos - charpos > precision)
6373 {
6374 it->end_charpos = it->string_nchars = charpos + precision;
6375 if (it->bidi_p)
6376 it->bidi_it.string.schars = it->end_charpos;
6377 }
6378
6379 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6380 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6381 FIELD_WIDTH < 0 means infinite field width. This is useful for
6382 padding with `-' at the end of a mode line. */
6383 if (field_width < 0)
6384 field_width = INFINITY;
6385 /* Implementation note: We deliberately don't enlarge
6386 it->bidi_it.string.schars here to fit it->end_charpos, because
6387 the bidi iterator cannot produce characters out of thin air. */
6388 if (field_width > it->end_charpos - charpos)
6389 it->end_charpos = charpos + field_width;
6390
6391 /* Use the standard display table for displaying strings. */
6392 if (DISP_TABLE_P (Vstandard_display_table))
6393 it->dp = XCHAR_TABLE (Vstandard_display_table);
6394
6395 it->stop_charpos = charpos;
6396 it->prev_stop = charpos;
6397 it->base_level_stop = 0;
6398 if (it->bidi_p)
6399 {
6400 it->bidi_it.first_elt = 1;
6401 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6402 it->bidi_it.disp_pos = -1;
6403 }
6404 if (s == NULL && it->multibyte_p)
6405 {
6406 ptrdiff_t endpos = SCHARS (it->string);
6407 if (endpos > it->end_charpos)
6408 endpos = it->end_charpos;
6409 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6410 it->string);
6411 }
6412 CHECK_IT (it);
6413 }
6414
6415
6416 \f
6417 /***********************************************************************
6418 Iteration
6419 ***********************************************************************/
6420
6421 /* Map enum it_method value to corresponding next_element_from_* function. */
6422
6423 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6424 {
6425 next_element_from_buffer,
6426 next_element_from_display_vector,
6427 next_element_from_string,
6428 next_element_from_c_string,
6429 next_element_from_image,
6430 next_element_from_stretch
6431 };
6432
6433 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6434
6435
6436 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6437 (possibly with the following characters). */
6438
6439 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6440 ((IT)->cmp_it.id >= 0 \
6441 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6442 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6443 END_CHARPOS, (IT)->w, \
6444 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6445 (IT)->string)))
6446
6447
6448 /* Lookup the char-table Vglyphless_char_display for character C (-1
6449 if we want information for no-font case), and return the display
6450 method symbol. By side-effect, update it->what and
6451 it->glyphless_method. This function is called from
6452 get_next_display_element for each character element, and from
6453 x_produce_glyphs when no suitable font was found. */
6454
6455 Lisp_Object
6456 lookup_glyphless_char_display (int c, struct it *it)
6457 {
6458 Lisp_Object glyphless_method = Qnil;
6459
6460 if (CHAR_TABLE_P (Vglyphless_char_display)
6461 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6462 {
6463 if (c >= 0)
6464 {
6465 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6466 if (CONSP (glyphless_method))
6467 glyphless_method = FRAME_WINDOW_P (it->f)
6468 ? XCAR (glyphless_method)
6469 : XCDR (glyphless_method);
6470 }
6471 else
6472 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6473 }
6474
6475 retry:
6476 if (NILP (glyphless_method))
6477 {
6478 if (c >= 0)
6479 /* The default is to display the character by a proper font. */
6480 return Qnil;
6481 /* The default for the no-font case is to display an empty box. */
6482 glyphless_method = Qempty_box;
6483 }
6484 if (EQ (glyphless_method, Qzero_width))
6485 {
6486 if (c >= 0)
6487 return glyphless_method;
6488 /* This method can't be used for the no-font case. */
6489 glyphless_method = Qempty_box;
6490 }
6491 if (EQ (glyphless_method, Qthin_space))
6492 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6493 else if (EQ (glyphless_method, Qempty_box))
6494 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6495 else if (EQ (glyphless_method, Qhex_code))
6496 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6497 else if (STRINGP (glyphless_method))
6498 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6499 else
6500 {
6501 /* Invalid value. We use the default method. */
6502 glyphless_method = Qnil;
6503 goto retry;
6504 }
6505 it->what = IT_GLYPHLESS;
6506 return glyphless_method;
6507 }
6508
6509 /* Load IT's display element fields with information about the next
6510 display element from the current position of IT. Value is zero if
6511 end of buffer (or C string) is reached. */
6512
6513 static struct frame *last_escape_glyph_frame = NULL;
6514 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6515 static int last_escape_glyph_merged_face_id = 0;
6516
6517 struct frame *last_glyphless_glyph_frame = NULL;
6518 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6519 int last_glyphless_glyph_merged_face_id = 0;
6520
6521 static int
6522 get_next_display_element (struct it *it)
6523 {
6524 /* Non-zero means that we found a display element. Zero means that
6525 we hit the end of what we iterate over. Performance note: the
6526 function pointer `method' used here turns out to be faster than
6527 using a sequence of if-statements. */
6528 int success_p;
6529
6530 get_next:
6531 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6532
6533 if (it->what == IT_CHARACTER)
6534 {
6535 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6536 and only if (a) the resolved directionality of that character
6537 is R..." */
6538 /* FIXME: Do we need an exception for characters from display
6539 tables? */
6540 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6541 it->c = bidi_mirror_char (it->c);
6542 /* Map via display table or translate control characters.
6543 IT->c, IT->len etc. have been set to the next character by
6544 the function call above. If we have a display table, and it
6545 contains an entry for IT->c, translate it. Don't do this if
6546 IT->c itself comes from a display table, otherwise we could
6547 end up in an infinite recursion. (An alternative could be to
6548 count the recursion depth of this function and signal an
6549 error when a certain maximum depth is reached.) Is it worth
6550 it? */
6551 if (success_p && it->dpvec == NULL)
6552 {
6553 Lisp_Object dv;
6554 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6555 int nonascii_space_p = 0;
6556 int nonascii_hyphen_p = 0;
6557 int c = it->c; /* This is the character to display. */
6558
6559 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6560 {
6561 eassert (SINGLE_BYTE_CHAR_P (c));
6562 if (unibyte_display_via_language_environment)
6563 {
6564 c = DECODE_CHAR (unibyte, c);
6565 if (c < 0)
6566 c = BYTE8_TO_CHAR (it->c);
6567 }
6568 else
6569 c = BYTE8_TO_CHAR (it->c);
6570 }
6571
6572 if (it->dp
6573 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6574 VECTORP (dv)))
6575 {
6576 struct Lisp_Vector *v = XVECTOR (dv);
6577
6578 /* Return the first character from the display table
6579 entry, if not empty. If empty, don't display the
6580 current character. */
6581 if (v->header.size)
6582 {
6583 it->dpvec_char_len = it->len;
6584 it->dpvec = v->contents;
6585 it->dpend = v->contents + v->header.size;
6586 it->current.dpvec_index = 0;
6587 it->dpvec_face_id = -1;
6588 it->saved_face_id = it->face_id;
6589 it->method = GET_FROM_DISPLAY_VECTOR;
6590 it->ellipsis_p = 0;
6591 }
6592 else
6593 {
6594 set_iterator_to_next (it, 0);
6595 }
6596 goto get_next;
6597 }
6598
6599 if (! NILP (lookup_glyphless_char_display (c, it)))
6600 {
6601 if (it->what == IT_GLYPHLESS)
6602 goto done;
6603 /* Don't display this character. */
6604 set_iterator_to_next (it, 0);
6605 goto get_next;
6606 }
6607
6608 /* If `nobreak-char-display' is non-nil, we display
6609 non-ASCII spaces and hyphens specially. */
6610 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6611 {
6612 if (c == 0xA0)
6613 nonascii_space_p = 1;
6614 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6615 nonascii_hyphen_p = 1;
6616 }
6617
6618 /* Translate control characters into `\003' or `^C' form.
6619 Control characters coming from a display table entry are
6620 currently not translated because we use IT->dpvec to hold
6621 the translation. This could easily be changed but I
6622 don't believe that it is worth doing.
6623
6624 The characters handled by `nobreak-char-display' must be
6625 translated too.
6626
6627 Non-printable characters and raw-byte characters are also
6628 translated to octal form. */
6629 if (((c < ' ' || c == 127) /* ASCII control chars */
6630 ? (it->area != TEXT_AREA
6631 /* In mode line, treat \n, \t like other crl chars. */
6632 || (c != '\t'
6633 && it->glyph_row
6634 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6635 || (c != '\n' && c != '\t'))
6636 : (nonascii_space_p
6637 || nonascii_hyphen_p
6638 || CHAR_BYTE8_P (c)
6639 || ! CHAR_PRINTABLE_P (c))))
6640 {
6641 /* C is a control character, non-ASCII space/hyphen,
6642 raw-byte, or a non-printable character which must be
6643 displayed either as '\003' or as `^C' where the '\\'
6644 and '^' can be defined in the display table. Fill
6645 IT->ctl_chars with glyphs for what we have to
6646 display. Then, set IT->dpvec to these glyphs. */
6647 Lisp_Object gc;
6648 int ctl_len;
6649 int face_id;
6650 int lface_id = 0;
6651 int escape_glyph;
6652
6653 /* Handle control characters with ^. */
6654
6655 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6656 {
6657 int g;
6658
6659 g = '^'; /* default glyph for Control */
6660 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6661 if (it->dp
6662 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6663 {
6664 g = GLYPH_CODE_CHAR (gc);
6665 lface_id = GLYPH_CODE_FACE (gc);
6666 }
6667 if (lface_id)
6668 {
6669 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6670 }
6671 else if (it->f == last_escape_glyph_frame
6672 && it->face_id == last_escape_glyph_face_id)
6673 {
6674 face_id = last_escape_glyph_merged_face_id;
6675 }
6676 else
6677 {
6678 /* Merge the escape-glyph face into the current face. */
6679 face_id = merge_faces (it->f, Qescape_glyph, 0,
6680 it->face_id);
6681 last_escape_glyph_frame = it->f;
6682 last_escape_glyph_face_id = it->face_id;
6683 last_escape_glyph_merged_face_id = face_id;
6684 }
6685
6686 XSETINT (it->ctl_chars[0], g);
6687 XSETINT (it->ctl_chars[1], c ^ 0100);
6688 ctl_len = 2;
6689 goto display_control;
6690 }
6691
6692 /* Handle non-ascii space in the mode where it only gets
6693 highlighting. */
6694
6695 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6696 {
6697 /* Merge `nobreak-space' into the current face. */
6698 face_id = merge_faces (it->f, Qnobreak_space, 0,
6699 it->face_id);
6700 XSETINT (it->ctl_chars[0], ' ');
6701 ctl_len = 1;
6702 goto display_control;
6703 }
6704
6705 /* Handle sequences that start with the "escape glyph". */
6706
6707 /* the default escape glyph is \. */
6708 escape_glyph = '\\';
6709
6710 if (it->dp
6711 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6712 {
6713 escape_glyph = GLYPH_CODE_CHAR (gc);
6714 lface_id = GLYPH_CODE_FACE (gc);
6715 }
6716 if (lface_id)
6717 {
6718 /* The display table specified a face.
6719 Merge it into face_id and also into escape_glyph. */
6720 face_id = merge_faces (it->f, Qt, lface_id,
6721 it->face_id);
6722 }
6723 else if (it->f == last_escape_glyph_frame
6724 && it->face_id == last_escape_glyph_face_id)
6725 {
6726 face_id = last_escape_glyph_merged_face_id;
6727 }
6728 else
6729 {
6730 /* Merge the escape-glyph face into the current face. */
6731 face_id = merge_faces (it->f, Qescape_glyph, 0,
6732 it->face_id);
6733 last_escape_glyph_frame = it->f;
6734 last_escape_glyph_face_id = it->face_id;
6735 last_escape_glyph_merged_face_id = face_id;
6736 }
6737
6738 /* Draw non-ASCII hyphen with just highlighting: */
6739
6740 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6741 {
6742 XSETINT (it->ctl_chars[0], '-');
6743 ctl_len = 1;
6744 goto display_control;
6745 }
6746
6747 /* Draw non-ASCII space/hyphen with escape glyph: */
6748
6749 if (nonascii_space_p || nonascii_hyphen_p)
6750 {
6751 XSETINT (it->ctl_chars[0], escape_glyph);
6752 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6753 ctl_len = 2;
6754 goto display_control;
6755 }
6756
6757 {
6758 char str[10];
6759 int len, i;
6760
6761 if (CHAR_BYTE8_P (c))
6762 /* Display \200 instead of \17777600. */
6763 c = CHAR_TO_BYTE8 (c);
6764 len = sprintf (str, "%03o", c);
6765
6766 XSETINT (it->ctl_chars[0], escape_glyph);
6767 for (i = 0; i < len; i++)
6768 XSETINT (it->ctl_chars[i + 1], str[i]);
6769 ctl_len = len + 1;
6770 }
6771
6772 display_control:
6773 /* Set up IT->dpvec and return first character from it. */
6774 it->dpvec_char_len = it->len;
6775 it->dpvec = it->ctl_chars;
6776 it->dpend = it->dpvec + ctl_len;
6777 it->current.dpvec_index = 0;
6778 it->dpvec_face_id = face_id;
6779 it->saved_face_id = it->face_id;
6780 it->method = GET_FROM_DISPLAY_VECTOR;
6781 it->ellipsis_p = 0;
6782 goto get_next;
6783 }
6784 it->char_to_display = c;
6785 }
6786 else if (success_p)
6787 {
6788 it->char_to_display = it->c;
6789 }
6790 }
6791
6792 /* Adjust face id for a multibyte character. There are no multibyte
6793 character in unibyte text. */
6794 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6795 && it->multibyte_p
6796 && success_p
6797 && FRAME_WINDOW_P (it->f))
6798 {
6799 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6800
6801 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6802 {
6803 /* Automatic composition with glyph-string. */
6804 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6805
6806 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6807 }
6808 else
6809 {
6810 ptrdiff_t pos = (it->s ? -1
6811 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6812 : IT_CHARPOS (*it));
6813 int c;
6814
6815 if (it->what == IT_CHARACTER)
6816 c = it->char_to_display;
6817 else
6818 {
6819 struct composition *cmp = composition_table[it->cmp_it.id];
6820 int i;
6821
6822 c = ' ';
6823 for (i = 0; i < cmp->glyph_len; i++)
6824 /* TAB in a composition means display glyphs with
6825 padding space on the left or right. */
6826 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6827 break;
6828 }
6829 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6830 }
6831 }
6832
6833 done:
6834 /* Is this character the last one of a run of characters with
6835 box? If yes, set IT->end_of_box_run_p to 1. */
6836 if (it->face_box_p
6837 && it->s == NULL)
6838 {
6839 if (it->method == GET_FROM_STRING && it->sp)
6840 {
6841 int face_id = underlying_face_id (it);
6842 struct face *face = FACE_FROM_ID (it->f, face_id);
6843
6844 if (face)
6845 {
6846 if (face->box == FACE_NO_BOX)
6847 {
6848 /* If the box comes from face properties in a
6849 display string, check faces in that string. */
6850 int string_face_id = face_after_it_pos (it);
6851 it->end_of_box_run_p
6852 = (FACE_FROM_ID (it->f, string_face_id)->box
6853 == FACE_NO_BOX);
6854 }
6855 /* Otherwise, the box comes from the underlying face.
6856 If this is the last string character displayed, check
6857 the next buffer location. */
6858 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6859 && (it->current.overlay_string_index
6860 == it->n_overlay_strings - 1))
6861 {
6862 ptrdiff_t ignore;
6863 int next_face_id;
6864 struct text_pos pos = it->current.pos;
6865 INC_TEXT_POS (pos, it->multibyte_p);
6866
6867 next_face_id = face_at_buffer_position
6868 (it->w, CHARPOS (pos), it->region_beg_charpos,
6869 it->region_end_charpos, &ignore,
6870 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6871 -1);
6872 it->end_of_box_run_p
6873 = (FACE_FROM_ID (it->f, next_face_id)->box
6874 == FACE_NO_BOX);
6875 }
6876 }
6877 }
6878 else
6879 {
6880 int face_id = face_after_it_pos (it);
6881 it->end_of_box_run_p
6882 = (face_id != it->face_id
6883 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6884 }
6885 }
6886 /* If we reached the end of the object we've been iterating (e.g., a
6887 display string or an overlay string), and there's something on
6888 IT->stack, proceed with what's on the stack. It doesn't make
6889 sense to return zero if there's unprocessed stuff on the stack,
6890 because otherwise that stuff will never be displayed. */
6891 if (!success_p && it->sp > 0)
6892 {
6893 set_iterator_to_next (it, 0);
6894 success_p = get_next_display_element (it);
6895 }
6896
6897 /* Value is 0 if end of buffer or string reached. */
6898 return success_p;
6899 }
6900
6901
6902 /* Move IT to the next display element.
6903
6904 RESEAT_P non-zero means if called on a newline in buffer text,
6905 skip to the next visible line start.
6906
6907 Functions get_next_display_element and set_iterator_to_next are
6908 separate because I find this arrangement easier to handle than a
6909 get_next_display_element function that also increments IT's
6910 position. The way it is we can first look at an iterator's current
6911 display element, decide whether it fits on a line, and if it does,
6912 increment the iterator position. The other way around we probably
6913 would either need a flag indicating whether the iterator has to be
6914 incremented the next time, or we would have to implement a
6915 decrement position function which would not be easy to write. */
6916
6917 void
6918 set_iterator_to_next (struct it *it, int reseat_p)
6919 {
6920 /* Reset flags indicating start and end of a sequence of characters
6921 with box. Reset them at the start of this function because
6922 moving the iterator to a new position might set them. */
6923 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6924
6925 switch (it->method)
6926 {
6927 case GET_FROM_BUFFER:
6928 /* The current display element of IT is a character from
6929 current_buffer. Advance in the buffer, and maybe skip over
6930 invisible lines that are so because of selective display. */
6931 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6932 reseat_at_next_visible_line_start (it, 0);
6933 else if (it->cmp_it.id >= 0)
6934 {
6935 /* We are currently getting glyphs from a composition. */
6936 int i;
6937
6938 if (! it->bidi_p)
6939 {
6940 IT_CHARPOS (*it) += it->cmp_it.nchars;
6941 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6942 if (it->cmp_it.to < it->cmp_it.nglyphs)
6943 {
6944 it->cmp_it.from = it->cmp_it.to;
6945 }
6946 else
6947 {
6948 it->cmp_it.id = -1;
6949 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6950 IT_BYTEPOS (*it),
6951 it->end_charpos, Qnil);
6952 }
6953 }
6954 else if (! it->cmp_it.reversed_p)
6955 {
6956 /* Composition created while scanning forward. */
6957 /* Update IT's char/byte positions to point to the first
6958 character of the next grapheme cluster, or to the
6959 character visually after the current composition. */
6960 for (i = 0; i < it->cmp_it.nchars; i++)
6961 bidi_move_to_visually_next (&it->bidi_it);
6962 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6963 IT_CHARPOS (*it) = it->bidi_it.charpos;
6964
6965 if (it->cmp_it.to < it->cmp_it.nglyphs)
6966 {
6967 /* Proceed to the next grapheme cluster. */
6968 it->cmp_it.from = it->cmp_it.to;
6969 }
6970 else
6971 {
6972 /* No more grapheme clusters in this composition.
6973 Find the next stop position. */
6974 ptrdiff_t stop = it->end_charpos;
6975 if (it->bidi_it.scan_dir < 0)
6976 /* Now we are scanning backward and don't know
6977 where to stop. */
6978 stop = -1;
6979 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6980 IT_BYTEPOS (*it), stop, Qnil);
6981 }
6982 }
6983 else
6984 {
6985 /* Composition created while scanning backward. */
6986 /* Update IT's char/byte positions to point to the last
6987 character of the previous grapheme cluster, or the
6988 character visually after the current composition. */
6989 for (i = 0; i < it->cmp_it.nchars; i++)
6990 bidi_move_to_visually_next (&it->bidi_it);
6991 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6992 IT_CHARPOS (*it) = it->bidi_it.charpos;
6993 if (it->cmp_it.from > 0)
6994 {
6995 /* Proceed to the previous grapheme cluster. */
6996 it->cmp_it.to = it->cmp_it.from;
6997 }
6998 else
6999 {
7000 /* No more grapheme clusters in this composition.
7001 Find the next stop position. */
7002 ptrdiff_t stop = it->end_charpos;
7003 if (it->bidi_it.scan_dir < 0)
7004 /* Now we are scanning backward and don't know
7005 where to stop. */
7006 stop = -1;
7007 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7008 IT_BYTEPOS (*it), stop, Qnil);
7009 }
7010 }
7011 }
7012 else
7013 {
7014 eassert (it->len != 0);
7015
7016 if (!it->bidi_p)
7017 {
7018 IT_BYTEPOS (*it) += it->len;
7019 IT_CHARPOS (*it) += 1;
7020 }
7021 else
7022 {
7023 int prev_scan_dir = it->bidi_it.scan_dir;
7024 /* If this is a new paragraph, determine its base
7025 direction (a.k.a. its base embedding level). */
7026 if (it->bidi_it.new_paragraph)
7027 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7028 bidi_move_to_visually_next (&it->bidi_it);
7029 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7030 IT_CHARPOS (*it) = it->bidi_it.charpos;
7031 if (prev_scan_dir != it->bidi_it.scan_dir)
7032 {
7033 /* As the scan direction was changed, we must
7034 re-compute the stop position for composition. */
7035 ptrdiff_t stop = it->end_charpos;
7036 if (it->bidi_it.scan_dir < 0)
7037 stop = -1;
7038 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7039 IT_BYTEPOS (*it), stop, Qnil);
7040 }
7041 }
7042 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7043 }
7044 break;
7045
7046 case GET_FROM_C_STRING:
7047 /* Current display element of IT is from a C string. */
7048 if (!it->bidi_p
7049 /* If the string position is beyond string's end, it means
7050 next_element_from_c_string is padding the string with
7051 blanks, in which case we bypass the bidi iterator,
7052 because it cannot deal with such virtual characters. */
7053 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7054 {
7055 IT_BYTEPOS (*it) += it->len;
7056 IT_CHARPOS (*it) += 1;
7057 }
7058 else
7059 {
7060 bidi_move_to_visually_next (&it->bidi_it);
7061 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7062 IT_CHARPOS (*it) = it->bidi_it.charpos;
7063 }
7064 break;
7065
7066 case GET_FROM_DISPLAY_VECTOR:
7067 /* Current display element of IT is from a display table entry.
7068 Advance in the display table definition. Reset it to null if
7069 end reached, and continue with characters from buffers/
7070 strings. */
7071 ++it->current.dpvec_index;
7072
7073 /* Restore face of the iterator to what they were before the
7074 display vector entry (these entries may contain faces). */
7075 it->face_id = it->saved_face_id;
7076
7077 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7078 {
7079 int recheck_faces = it->ellipsis_p;
7080
7081 if (it->s)
7082 it->method = GET_FROM_C_STRING;
7083 else if (STRINGP (it->string))
7084 it->method = GET_FROM_STRING;
7085 else
7086 {
7087 it->method = GET_FROM_BUFFER;
7088 it->object = WVAR (it->w, buffer);
7089 }
7090
7091 it->dpvec = NULL;
7092 it->current.dpvec_index = -1;
7093
7094 /* Skip over characters which were displayed via IT->dpvec. */
7095 if (it->dpvec_char_len < 0)
7096 reseat_at_next_visible_line_start (it, 1);
7097 else if (it->dpvec_char_len > 0)
7098 {
7099 if (it->method == GET_FROM_STRING
7100 && it->n_overlay_strings > 0)
7101 it->ignore_overlay_strings_at_pos_p = 1;
7102 it->len = it->dpvec_char_len;
7103 set_iterator_to_next (it, reseat_p);
7104 }
7105
7106 /* Maybe recheck faces after display vector */
7107 if (recheck_faces)
7108 it->stop_charpos = IT_CHARPOS (*it);
7109 }
7110 break;
7111
7112 case GET_FROM_STRING:
7113 /* Current display element is a character from a Lisp string. */
7114 eassert (it->s == NULL && STRINGP (it->string));
7115 /* Don't advance past string end. These conditions are true
7116 when set_iterator_to_next is called at the end of
7117 get_next_display_element, in which case the Lisp string is
7118 already exhausted, and all we want is pop the iterator
7119 stack. */
7120 if (it->current.overlay_string_index >= 0)
7121 {
7122 /* This is an overlay string, so there's no padding with
7123 spaces, and the number of characters in the string is
7124 where the string ends. */
7125 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7126 goto consider_string_end;
7127 }
7128 else
7129 {
7130 /* Not an overlay string. There could be padding, so test
7131 against it->end_charpos . */
7132 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7133 goto consider_string_end;
7134 }
7135 if (it->cmp_it.id >= 0)
7136 {
7137 int i;
7138
7139 if (! it->bidi_p)
7140 {
7141 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7142 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7143 if (it->cmp_it.to < it->cmp_it.nglyphs)
7144 it->cmp_it.from = it->cmp_it.to;
7145 else
7146 {
7147 it->cmp_it.id = -1;
7148 composition_compute_stop_pos (&it->cmp_it,
7149 IT_STRING_CHARPOS (*it),
7150 IT_STRING_BYTEPOS (*it),
7151 it->end_charpos, it->string);
7152 }
7153 }
7154 else if (! it->cmp_it.reversed_p)
7155 {
7156 for (i = 0; i < it->cmp_it.nchars; i++)
7157 bidi_move_to_visually_next (&it->bidi_it);
7158 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7159 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7160
7161 if (it->cmp_it.to < it->cmp_it.nglyphs)
7162 it->cmp_it.from = it->cmp_it.to;
7163 else
7164 {
7165 ptrdiff_t stop = it->end_charpos;
7166 if (it->bidi_it.scan_dir < 0)
7167 stop = -1;
7168 composition_compute_stop_pos (&it->cmp_it,
7169 IT_STRING_CHARPOS (*it),
7170 IT_STRING_BYTEPOS (*it), stop,
7171 it->string);
7172 }
7173 }
7174 else
7175 {
7176 for (i = 0; i < it->cmp_it.nchars; i++)
7177 bidi_move_to_visually_next (&it->bidi_it);
7178 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7179 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7180 if (it->cmp_it.from > 0)
7181 it->cmp_it.to = it->cmp_it.from;
7182 else
7183 {
7184 ptrdiff_t stop = it->end_charpos;
7185 if (it->bidi_it.scan_dir < 0)
7186 stop = -1;
7187 composition_compute_stop_pos (&it->cmp_it,
7188 IT_STRING_CHARPOS (*it),
7189 IT_STRING_BYTEPOS (*it), stop,
7190 it->string);
7191 }
7192 }
7193 }
7194 else
7195 {
7196 if (!it->bidi_p
7197 /* If the string position is beyond string's end, it
7198 means next_element_from_string is padding the string
7199 with blanks, in which case we bypass the bidi
7200 iterator, because it cannot deal with such virtual
7201 characters. */
7202 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7203 {
7204 IT_STRING_BYTEPOS (*it) += it->len;
7205 IT_STRING_CHARPOS (*it) += 1;
7206 }
7207 else
7208 {
7209 int prev_scan_dir = it->bidi_it.scan_dir;
7210
7211 bidi_move_to_visually_next (&it->bidi_it);
7212 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7213 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7214 if (prev_scan_dir != it->bidi_it.scan_dir)
7215 {
7216 ptrdiff_t stop = it->end_charpos;
7217
7218 if (it->bidi_it.scan_dir < 0)
7219 stop = -1;
7220 composition_compute_stop_pos (&it->cmp_it,
7221 IT_STRING_CHARPOS (*it),
7222 IT_STRING_BYTEPOS (*it), stop,
7223 it->string);
7224 }
7225 }
7226 }
7227
7228 consider_string_end:
7229
7230 if (it->current.overlay_string_index >= 0)
7231 {
7232 /* IT->string is an overlay string. Advance to the
7233 next, if there is one. */
7234 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7235 {
7236 it->ellipsis_p = 0;
7237 next_overlay_string (it);
7238 if (it->ellipsis_p)
7239 setup_for_ellipsis (it, 0);
7240 }
7241 }
7242 else
7243 {
7244 /* IT->string is not an overlay string. If we reached
7245 its end, and there is something on IT->stack, proceed
7246 with what is on the stack. This can be either another
7247 string, this time an overlay string, or a buffer. */
7248 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7249 && it->sp > 0)
7250 {
7251 pop_it (it);
7252 if (it->method == GET_FROM_STRING)
7253 goto consider_string_end;
7254 }
7255 }
7256 break;
7257
7258 case GET_FROM_IMAGE:
7259 case GET_FROM_STRETCH:
7260 /* The position etc with which we have to proceed are on
7261 the stack. The position may be at the end of a string,
7262 if the `display' property takes up the whole string. */
7263 eassert (it->sp > 0);
7264 pop_it (it);
7265 if (it->method == GET_FROM_STRING)
7266 goto consider_string_end;
7267 break;
7268
7269 default:
7270 /* There are no other methods defined, so this should be a bug. */
7271 abort ();
7272 }
7273
7274 eassert (it->method != GET_FROM_STRING
7275 || (STRINGP (it->string)
7276 && IT_STRING_CHARPOS (*it) >= 0));
7277 }
7278
7279 /* Load IT's display element fields with information about the next
7280 display element which comes from a display table entry or from the
7281 result of translating a control character to one of the forms `^C'
7282 or `\003'.
7283
7284 IT->dpvec holds the glyphs to return as characters.
7285 IT->saved_face_id holds the face id before the display vector--it
7286 is restored into IT->face_id in set_iterator_to_next. */
7287
7288 static int
7289 next_element_from_display_vector (struct it *it)
7290 {
7291 Lisp_Object gc;
7292
7293 /* Precondition. */
7294 eassert (it->dpvec && it->current.dpvec_index >= 0);
7295
7296 it->face_id = it->saved_face_id;
7297
7298 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7299 That seemed totally bogus - so I changed it... */
7300 gc = it->dpvec[it->current.dpvec_index];
7301
7302 if (GLYPH_CODE_P (gc))
7303 {
7304 it->c = GLYPH_CODE_CHAR (gc);
7305 it->len = CHAR_BYTES (it->c);
7306
7307 /* The entry may contain a face id to use. Such a face id is
7308 the id of a Lisp face, not a realized face. A face id of
7309 zero means no face is specified. */
7310 if (it->dpvec_face_id >= 0)
7311 it->face_id = it->dpvec_face_id;
7312 else
7313 {
7314 int lface_id = GLYPH_CODE_FACE (gc);
7315 if (lface_id > 0)
7316 it->face_id = merge_faces (it->f, Qt, lface_id,
7317 it->saved_face_id);
7318 }
7319 }
7320 else
7321 /* Display table entry is invalid. Return a space. */
7322 it->c = ' ', it->len = 1;
7323
7324 /* Don't change position and object of the iterator here. They are
7325 still the values of the character that had this display table
7326 entry or was translated, and that's what we want. */
7327 it->what = IT_CHARACTER;
7328 return 1;
7329 }
7330
7331 /* Get the first element of string/buffer in the visual order, after
7332 being reseated to a new position in a string or a buffer. */
7333 static void
7334 get_visually_first_element (struct it *it)
7335 {
7336 int string_p = STRINGP (it->string) || it->s;
7337 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7338 ptrdiff_t bob = (string_p ? 0 : BEGV);
7339
7340 if (STRINGP (it->string))
7341 {
7342 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7343 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7344 }
7345 else
7346 {
7347 it->bidi_it.charpos = IT_CHARPOS (*it);
7348 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7349 }
7350
7351 if (it->bidi_it.charpos == eob)
7352 {
7353 /* Nothing to do, but reset the FIRST_ELT flag, like
7354 bidi_paragraph_init does, because we are not going to
7355 call it. */
7356 it->bidi_it.first_elt = 0;
7357 }
7358 else if (it->bidi_it.charpos == bob
7359 || (!string_p
7360 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7361 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7362 {
7363 /* If we are at the beginning of a line/string, we can produce
7364 the next element right away. */
7365 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7366 bidi_move_to_visually_next (&it->bidi_it);
7367 }
7368 else
7369 {
7370 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7371
7372 /* We need to prime the bidi iterator starting at the line's or
7373 string's beginning, before we will be able to produce the
7374 next element. */
7375 if (string_p)
7376 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7377 else
7378 {
7379 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7380 -1);
7381 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7382 }
7383 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7384 do
7385 {
7386 /* Now return to buffer/string position where we were asked
7387 to get the next display element, and produce that. */
7388 bidi_move_to_visually_next (&it->bidi_it);
7389 }
7390 while (it->bidi_it.bytepos != orig_bytepos
7391 && it->bidi_it.charpos < eob);
7392 }
7393
7394 /* Adjust IT's position information to where we ended up. */
7395 if (STRINGP (it->string))
7396 {
7397 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7398 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7399 }
7400 else
7401 {
7402 IT_CHARPOS (*it) = it->bidi_it.charpos;
7403 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7404 }
7405
7406 if (STRINGP (it->string) || !it->s)
7407 {
7408 ptrdiff_t stop, charpos, bytepos;
7409
7410 if (STRINGP (it->string))
7411 {
7412 eassert (!it->s);
7413 stop = SCHARS (it->string);
7414 if (stop > it->end_charpos)
7415 stop = it->end_charpos;
7416 charpos = IT_STRING_CHARPOS (*it);
7417 bytepos = IT_STRING_BYTEPOS (*it);
7418 }
7419 else
7420 {
7421 stop = it->end_charpos;
7422 charpos = IT_CHARPOS (*it);
7423 bytepos = IT_BYTEPOS (*it);
7424 }
7425 if (it->bidi_it.scan_dir < 0)
7426 stop = -1;
7427 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7428 it->string);
7429 }
7430 }
7431
7432 /* Load IT with the next display element from Lisp string IT->string.
7433 IT->current.string_pos is the current position within the string.
7434 If IT->current.overlay_string_index >= 0, the Lisp string is an
7435 overlay string. */
7436
7437 static int
7438 next_element_from_string (struct it *it)
7439 {
7440 struct text_pos position;
7441
7442 eassert (STRINGP (it->string));
7443 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7444 eassert (IT_STRING_CHARPOS (*it) >= 0);
7445 position = it->current.string_pos;
7446
7447 /* With bidi reordering, the character to display might not be the
7448 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7449 that we were reseat()ed to a new string, whose paragraph
7450 direction is not known. */
7451 if (it->bidi_p && it->bidi_it.first_elt)
7452 {
7453 get_visually_first_element (it);
7454 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7455 }
7456
7457 /* Time to check for invisible text? */
7458 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7459 {
7460 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7461 {
7462 if (!(!it->bidi_p
7463 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7464 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7465 {
7466 /* With bidi non-linear iteration, we could find
7467 ourselves far beyond the last computed stop_charpos,
7468 with several other stop positions in between that we
7469 missed. Scan them all now, in buffer's logical
7470 order, until we find and handle the last stop_charpos
7471 that precedes our current position. */
7472 handle_stop_backwards (it, it->stop_charpos);
7473 return GET_NEXT_DISPLAY_ELEMENT (it);
7474 }
7475 else
7476 {
7477 if (it->bidi_p)
7478 {
7479 /* Take note of the stop position we just moved
7480 across, for when we will move back across it. */
7481 it->prev_stop = it->stop_charpos;
7482 /* If we are at base paragraph embedding level, take
7483 note of the last stop position seen at this
7484 level. */
7485 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7486 it->base_level_stop = it->stop_charpos;
7487 }
7488 handle_stop (it);
7489
7490 /* Since a handler may have changed IT->method, we must
7491 recurse here. */
7492 return GET_NEXT_DISPLAY_ELEMENT (it);
7493 }
7494 }
7495 else if (it->bidi_p
7496 /* If we are before prev_stop, we may have overstepped
7497 on our way backwards a stop_pos, and if so, we need
7498 to handle that stop_pos. */
7499 && IT_STRING_CHARPOS (*it) < it->prev_stop
7500 /* We can sometimes back up for reasons that have nothing
7501 to do with bidi reordering. E.g., compositions. The
7502 code below is only needed when we are above the base
7503 embedding level, so test for that explicitly. */
7504 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7505 {
7506 /* If we lost track of base_level_stop, we have no better
7507 place for handle_stop_backwards to start from than string
7508 beginning. This happens, e.g., when we were reseated to
7509 the previous screenful of text by vertical-motion. */
7510 if (it->base_level_stop <= 0
7511 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7512 it->base_level_stop = 0;
7513 handle_stop_backwards (it, it->base_level_stop);
7514 return GET_NEXT_DISPLAY_ELEMENT (it);
7515 }
7516 }
7517
7518 if (it->current.overlay_string_index >= 0)
7519 {
7520 /* Get the next character from an overlay string. In overlay
7521 strings, there is no field width or padding with spaces to
7522 do. */
7523 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7524 {
7525 it->what = IT_EOB;
7526 return 0;
7527 }
7528 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7529 IT_STRING_BYTEPOS (*it),
7530 it->bidi_it.scan_dir < 0
7531 ? -1
7532 : SCHARS (it->string))
7533 && next_element_from_composition (it))
7534 {
7535 return 1;
7536 }
7537 else if (STRING_MULTIBYTE (it->string))
7538 {
7539 const unsigned char *s = (SDATA (it->string)
7540 + IT_STRING_BYTEPOS (*it));
7541 it->c = string_char_and_length (s, &it->len);
7542 }
7543 else
7544 {
7545 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7546 it->len = 1;
7547 }
7548 }
7549 else
7550 {
7551 /* Get the next character from a Lisp string that is not an
7552 overlay string. Such strings come from the mode line, for
7553 example. We may have to pad with spaces, or truncate the
7554 string. See also next_element_from_c_string. */
7555 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7556 {
7557 it->what = IT_EOB;
7558 return 0;
7559 }
7560 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7561 {
7562 /* Pad with spaces. */
7563 it->c = ' ', it->len = 1;
7564 CHARPOS (position) = BYTEPOS (position) = -1;
7565 }
7566 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7567 IT_STRING_BYTEPOS (*it),
7568 it->bidi_it.scan_dir < 0
7569 ? -1
7570 : it->string_nchars)
7571 && next_element_from_composition (it))
7572 {
7573 return 1;
7574 }
7575 else if (STRING_MULTIBYTE (it->string))
7576 {
7577 const unsigned char *s = (SDATA (it->string)
7578 + IT_STRING_BYTEPOS (*it));
7579 it->c = string_char_and_length (s, &it->len);
7580 }
7581 else
7582 {
7583 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7584 it->len = 1;
7585 }
7586 }
7587
7588 /* Record what we have and where it came from. */
7589 it->what = IT_CHARACTER;
7590 it->object = it->string;
7591 it->position = position;
7592 return 1;
7593 }
7594
7595
7596 /* Load IT with next display element from C string IT->s.
7597 IT->string_nchars is the maximum number of characters to return
7598 from the string. IT->end_charpos may be greater than
7599 IT->string_nchars when this function is called, in which case we
7600 may have to return padding spaces. Value is zero if end of string
7601 reached, including padding spaces. */
7602
7603 static int
7604 next_element_from_c_string (struct it *it)
7605 {
7606 int success_p = 1;
7607
7608 eassert (it->s);
7609 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7610 it->what = IT_CHARACTER;
7611 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7612 it->object = Qnil;
7613
7614 /* With bidi reordering, the character to display might not be the
7615 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7616 we were reseated to a new string, whose paragraph direction is
7617 not known. */
7618 if (it->bidi_p && it->bidi_it.first_elt)
7619 get_visually_first_element (it);
7620
7621 /* IT's position can be greater than IT->string_nchars in case a
7622 field width or precision has been specified when the iterator was
7623 initialized. */
7624 if (IT_CHARPOS (*it) >= it->end_charpos)
7625 {
7626 /* End of the game. */
7627 it->what = IT_EOB;
7628 success_p = 0;
7629 }
7630 else if (IT_CHARPOS (*it) >= it->string_nchars)
7631 {
7632 /* Pad with spaces. */
7633 it->c = ' ', it->len = 1;
7634 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7635 }
7636 else if (it->multibyte_p)
7637 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7638 else
7639 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7640
7641 return success_p;
7642 }
7643
7644
7645 /* Set up IT to return characters from an ellipsis, if appropriate.
7646 The definition of the ellipsis glyphs may come from a display table
7647 entry. This function fills IT with the first glyph from the
7648 ellipsis if an ellipsis is to be displayed. */
7649
7650 static int
7651 next_element_from_ellipsis (struct it *it)
7652 {
7653 if (it->selective_display_ellipsis_p)
7654 setup_for_ellipsis (it, it->len);
7655 else
7656 {
7657 /* The face at the current position may be different from the
7658 face we find after the invisible text. Remember what it
7659 was in IT->saved_face_id, and signal that it's there by
7660 setting face_before_selective_p. */
7661 it->saved_face_id = it->face_id;
7662 it->method = GET_FROM_BUFFER;
7663 it->object = WVAR (it->w, buffer);
7664 reseat_at_next_visible_line_start (it, 1);
7665 it->face_before_selective_p = 1;
7666 }
7667
7668 return GET_NEXT_DISPLAY_ELEMENT (it);
7669 }
7670
7671
7672 /* Deliver an image display element. The iterator IT is already
7673 filled with image information (done in handle_display_prop). Value
7674 is always 1. */
7675
7676
7677 static int
7678 next_element_from_image (struct it *it)
7679 {
7680 it->what = IT_IMAGE;
7681 it->ignore_overlay_strings_at_pos_p = 0;
7682 return 1;
7683 }
7684
7685
7686 /* Fill iterator IT with next display element from a stretch glyph
7687 property. IT->object is the value of the text property. Value is
7688 always 1. */
7689
7690 static int
7691 next_element_from_stretch (struct it *it)
7692 {
7693 it->what = IT_STRETCH;
7694 return 1;
7695 }
7696
7697 /* Scan backwards from IT's current position until we find a stop
7698 position, or until BEGV. This is called when we find ourself
7699 before both the last known prev_stop and base_level_stop while
7700 reordering bidirectional text. */
7701
7702 static void
7703 compute_stop_pos_backwards (struct it *it)
7704 {
7705 const int SCAN_BACK_LIMIT = 1000;
7706 struct text_pos pos;
7707 struct display_pos save_current = it->current;
7708 struct text_pos save_position = it->position;
7709 ptrdiff_t charpos = IT_CHARPOS (*it);
7710 ptrdiff_t where_we_are = charpos;
7711 ptrdiff_t save_stop_pos = it->stop_charpos;
7712 ptrdiff_t save_end_pos = it->end_charpos;
7713
7714 eassert (NILP (it->string) && !it->s);
7715 eassert (it->bidi_p);
7716 it->bidi_p = 0;
7717 do
7718 {
7719 it->end_charpos = min (charpos + 1, ZV);
7720 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7721 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7722 reseat_1 (it, pos, 0);
7723 compute_stop_pos (it);
7724 /* We must advance forward, right? */
7725 if (it->stop_charpos <= charpos)
7726 abort ();
7727 }
7728 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7729
7730 if (it->stop_charpos <= where_we_are)
7731 it->prev_stop = it->stop_charpos;
7732 else
7733 it->prev_stop = BEGV;
7734 it->bidi_p = 1;
7735 it->current = save_current;
7736 it->position = save_position;
7737 it->stop_charpos = save_stop_pos;
7738 it->end_charpos = save_end_pos;
7739 }
7740
7741 /* Scan forward from CHARPOS in the current buffer/string, until we
7742 find a stop position > current IT's position. Then handle the stop
7743 position before that. This is called when we bump into a stop
7744 position while reordering bidirectional text. CHARPOS should be
7745 the last previously processed stop_pos (or BEGV/0, if none were
7746 processed yet) whose position is less that IT's current
7747 position. */
7748
7749 static void
7750 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7751 {
7752 int bufp = !STRINGP (it->string);
7753 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7754 struct display_pos save_current = it->current;
7755 struct text_pos save_position = it->position;
7756 struct text_pos pos1;
7757 ptrdiff_t next_stop;
7758
7759 /* Scan in strict logical order. */
7760 eassert (it->bidi_p);
7761 it->bidi_p = 0;
7762 do
7763 {
7764 it->prev_stop = charpos;
7765 if (bufp)
7766 {
7767 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7768 reseat_1 (it, pos1, 0);
7769 }
7770 else
7771 it->current.string_pos = string_pos (charpos, it->string);
7772 compute_stop_pos (it);
7773 /* We must advance forward, right? */
7774 if (it->stop_charpos <= it->prev_stop)
7775 abort ();
7776 charpos = it->stop_charpos;
7777 }
7778 while (charpos <= where_we_are);
7779
7780 it->bidi_p = 1;
7781 it->current = save_current;
7782 it->position = save_position;
7783 next_stop = it->stop_charpos;
7784 it->stop_charpos = it->prev_stop;
7785 handle_stop (it);
7786 it->stop_charpos = next_stop;
7787 }
7788
7789 /* Load IT with the next display element from current_buffer. Value
7790 is zero if end of buffer reached. IT->stop_charpos is the next
7791 position at which to stop and check for text properties or buffer
7792 end. */
7793
7794 static int
7795 next_element_from_buffer (struct it *it)
7796 {
7797 int success_p = 1;
7798
7799 eassert (IT_CHARPOS (*it) >= BEGV);
7800 eassert (NILP (it->string) && !it->s);
7801 eassert (!it->bidi_p
7802 || (EQ (it->bidi_it.string.lstring, Qnil)
7803 && it->bidi_it.string.s == NULL));
7804
7805 /* With bidi reordering, the character to display might not be the
7806 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7807 we were reseat()ed to a new buffer position, which is potentially
7808 a different paragraph. */
7809 if (it->bidi_p && it->bidi_it.first_elt)
7810 {
7811 get_visually_first_element (it);
7812 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7813 }
7814
7815 if (IT_CHARPOS (*it) >= it->stop_charpos)
7816 {
7817 if (IT_CHARPOS (*it) >= it->end_charpos)
7818 {
7819 int overlay_strings_follow_p;
7820
7821 /* End of the game, except when overlay strings follow that
7822 haven't been returned yet. */
7823 if (it->overlay_strings_at_end_processed_p)
7824 overlay_strings_follow_p = 0;
7825 else
7826 {
7827 it->overlay_strings_at_end_processed_p = 1;
7828 overlay_strings_follow_p = get_overlay_strings (it, 0);
7829 }
7830
7831 if (overlay_strings_follow_p)
7832 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7833 else
7834 {
7835 it->what = IT_EOB;
7836 it->position = it->current.pos;
7837 success_p = 0;
7838 }
7839 }
7840 else if (!(!it->bidi_p
7841 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7842 || IT_CHARPOS (*it) == it->stop_charpos))
7843 {
7844 /* With bidi non-linear iteration, we could find ourselves
7845 far beyond the last computed stop_charpos, with several
7846 other stop positions in between that we missed. Scan
7847 them all now, in buffer's logical order, until we find
7848 and handle the last stop_charpos that precedes our
7849 current position. */
7850 handle_stop_backwards (it, it->stop_charpos);
7851 return GET_NEXT_DISPLAY_ELEMENT (it);
7852 }
7853 else
7854 {
7855 if (it->bidi_p)
7856 {
7857 /* Take note of the stop position we just moved across,
7858 for when we will move back across it. */
7859 it->prev_stop = it->stop_charpos;
7860 /* If we are at base paragraph embedding level, take
7861 note of the last stop position seen at this
7862 level. */
7863 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7864 it->base_level_stop = it->stop_charpos;
7865 }
7866 handle_stop (it);
7867 return GET_NEXT_DISPLAY_ELEMENT (it);
7868 }
7869 }
7870 else if (it->bidi_p
7871 /* If we are before prev_stop, we may have overstepped on
7872 our way backwards a stop_pos, and if so, we need to
7873 handle that stop_pos. */
7874 && IT_CHARPOS (*it) < it->prev_stop
7875 /* We can sometimes back up for reasons that have nothing
7876 to do with bidi reordering. E.g., compositions. The
7877 code below is only needed when we are above the base
7878 embedding level, so test for that explicitly. */
7879 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7880 {
7881 if (it->base_level_stop <= 0
7882 || IT_CHARPOS (*it) < it->base_level_stop)
7883 {
7884 /* If we lost track of base_level_stop, we need to find
7885 prev_stop by looking backwards. This happens, e.g., when
7886 we were reseated to the previous screenful of text by
7887 vertical-motion. */
7888 it->base_level_stop = BEGV;
7889 compute_stop_pos_backwards (it);
7890 handle_stop_backwards (it, it->prev_stop);
7891 }
7892 else
7893 handle_stop_backwards (it, it->base_level_stop);
7894 return GET_NEXT_DISPLAY_ELEMENT (it);
7895 }
7896 else
7897 {
7898 /* No face changes, overlays etc. in sight, so just return a
7899 character from current_buffer. */
7900 unsigned char *p;
7901 ptrdiff_t stop;
7902
7903 /* Maybe run the redisplay end trigger hook. Performance note:
7904 This doesn't seem to cost measurable time. */
7905 if (it->redisplay_end_trigger_charpos
7906 && it->glyph_row
7907 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7908 run_redisplay_end_trigger_hook (it);
7909
7910 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7911 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7912 stop)
7913 && next_element_from_composition (it))
7914 {
7915 return 1;
7916 }
7917
7918 /* Get the next character, maybe multibyte. */
7919 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7920 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7921 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7922 else
7923 it->c = *p, it->len = 1;
7924
7925 /* Record what we have and where it came from. */
7926 it->what = IT_CHARACTER;
7927 it->object = WVAR (it->w, buffer);
7928 it->position = it->current.pos;
7929
7930 /* Normally we return the character found above, except when we
7931 really want to return an ellipsis for selective display. */
7932 if (it->selective)
7933 {
7934 if (it->c == '\n')
7935 {
7936 /* A value of selective > 0 means hide lines indented more
7937 than that number of columns. */
7938 if (it->selective > 0
7939 && IT_CHARPOS (*it) + 1 < ZV
7940 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7941 IT_BYTEPOS (*it) + 1,
7942 it->selective))
7943 {
7944 success_p = next_element_from_ellipsis (it);
7945 it->dpvec_char_len = -1;
7946 }
7947 }
7948 else if (it->c == '\r' && it->selective == -1)
7949 {
7950 /* A value of selective == -1 means that everything from the
7951 CR to the end of the line is invisible, with maybe an
7952 ellipsis displayed for it. */
7953 success_p = next_element_from_ellipsis (it);
7954 it->dpvec_char_len = -1;
7955 }
7956 }
7957 }
7958
7959 /* Value is zero if end of buffer reached. */
7960 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7961 return success_p;
7962 }
7963
7964
7965 /* Run the redisplay end trigger hook for IT. */
7966
7967 static void
7968 run_redisplay_end_trigger_hook (struct it *it)
7969 {
7970 Lisp_Object args[3];
7971
7972 /* IT->glyph_row should be non-null, i.e. we should be actually
7973 displaying something, or otherwise we should not run the hook. */
7974 eassert (it->glyph_row);
7975
7976 /* Set up hook arguments. */
7977 args[0] = Qredisplay_end_trigger_functions;
7978 args[1] = it->window;
7979 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7980 it->redisplay_end_trigger_charpos = 0;
7981
7982 /* Since we are *trying* to run these functions, don't try to run
7983 them again, even if they get an error. */
7984 WVAR (it->w, redisplay_end_trigger) = Qnil;
7985 Frun_hook_with_args (3, args);
7986
7987 /* Notice if it changed the face of the character we are on. */
7988 handle_face_prop (it);
7989 }
7990
7991
7992 /* Deliver a composition display element. Unlike the other
7993 next_element_from_XXX, this function is not registered in the array
7994 get_next_element[]. It is called from next_element_from_buffer and
7995 next_element_from_string when necessary. */
7996
7997 static int
7998 next_element_from_composition (struct it *it)
7999 {
8000 it->what = IT_COMPOSITION;
8001 it->len = it->cmp_it.nbytes;
8002 if (STRINGP (it->string))
8003 {
8004 if (it->c < 0)
8005 {
8006 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8007 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8008 return 0;
8009 }
8010 it->position = it->current.string_pos;
8011 it->object = it->string;
8012 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8013 IT_STRING_BYTEPOS (*it), it->string);
8014 }
8015 else
8016 {
8017 if (it->c < 0)
8018 {
8019 IT_CHARPOS (*it) += it->cmp_it.nchars;
8020 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8021 if (it->bidi_p)
8022 {
8023 if (it->bidi_it.new_paragraph)
8024 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8025 /* Resync the bidi iterator with IT's new position.
8026 FIXME: this doesn't support bidirectional text. */
8027 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8028 bidi_move_to_visually_next (&it->bidi_it);
8029 }
8030 return 0;
8031 }
8032 it->position = it->current.pos;
8033 it->object = WVAR (it->w, buffer);
8034 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8035 IT_BYTEPOS (*it), Qnil);
8036 }
8037 return 1;
8038 }
8039
8040
8041 \f
8042 /***********************************************************************
8043 Moving an iterator without producing glyphs
8044 ***********************************************************************/
8045
8046 /* Check if iterator is at a position corresponding to a valid buffer
8047 position after some move_it_ call. */
8048
8049 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8050 ((it)->method == GET_FROM_STRING \
8051 ? IT_STRING_CHARPOS (*it) == 0 \
8052 : 1)
8053
8054
8055 /* Move iterator IT to a specified buffer or X position within one
8056 line on the display without producing glyphs.
8057
8058 OP should be a bit mask including some or all of these bits:
8059 MOVE_TO_X: Stop upon reaching x-position TO_X.
8060 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8061 Regardless of OP's value, stop upon reaching the end of the display line.
8062
8063 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8064 This means, in particular, that TO_X includes window's horizontal
8065 scroll amount.
8066
8067 The return value has several possible values that
8068 say what condition caused the scan to stop:
8069
8070 MOVE_POS_MATCH_OR_ZV
8071 - when TO_POS or ZV was reached.
8072
8073 MOVE_X_REACHED
8074 -when TO_X was reached before TO_POS or ZV were reached.
8075
8076 MOVE_LINE_CONTINUED
8077 - when we reached the end of the display area and the line must
8078 be continued.
8079
8080 MOVE_LINE_TRUNCATED
8081 - when we reached the end of the display area and the line is
8082 truncated.
8083
8084 MOVE_NEWLINE_OR_CR
8085 - when we stopped at a line end, i.e. a newline or a CR and selective
8086 display is on. */
8087
8088 static enum move_it_result
8089 move_it_in_display_line_to (struct it *it,
8090 ptrdiff_t to_charpos, int to_x,
8091 enum move_operation_enum op)
8092 {
8093 enum move_it_result result = MOVE_UNDEFINED;
8094 struct glyph_row *saved_glyph_row;
8095 struct it wrap_it, atpos_it, atx_it, ppos_it;
8096 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8097 void *ppos_data = NULL;
8098 int may_wrap = 0;
8099 enum it_method prev_method = it->method;
8100 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8101 int saw_smaller_pos = prev_pos < to_charpos;
8102
8103 /* Don't produce glyphs in produce_glyphs. */
8104 saved_glyph_row = it->glyph_row;
8105 it->glyph_row = NULL;
8106
8107 /* Use wrap_it to save a copy of IT wherever a word wrap could
8108 occur. Use atpos_it to save a copy of IT at the desired buffer
8109 position, if found, so that we can scan ahead and check if the
8110 word later overshoots the window edge. Use atx_it similarly, for
8111 pixel positions. */
8112 wrap_it.sp = -1;
8113 atpos_it.sp = -1;
8114 atx_it.sp = -1;
8115
8116 /* Use ppos_it under bidi reordering to save a copy of IT for the
8117 position > CHARPOS that is the closest to CHARPOS. We restore
8118 that position in IT when we have scanned the entire display line
8119 without finding a match for CHARPOS and all the character
8120 positions are greater than CHARPOS. */
8121 if (it->bidi_p)
8122 {
8123 SAVE_IT (ppos_it, *it, ppos_data);
8124 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8125 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8126 SAVE_IT (ppos_it, *it, ppos_data);
8127 }
8128
8129 #define BUFFER_POS_REACHED_P() \
8130 ((op & MOVE_TO_POS) != 0 \
8131 && BUFFERP (it->object) \
8132 && (IT_CHARPOS (*it) == to_charpos \
8133 || ((!it->bidi_p \
8134 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8135 && IT_CHARPOS (*it) > to_charpos) \
8136 || (it->what == IT_COMPOSITION \
8137 && ((IT_CHARPOS (*it) > to_charpos \
8138 && to_charpos >= it->cmp_it.charpos) \
8139 || (IT_CHARPOS (*it) < to_charpos \
8140 && to_charpos <= it->cmp_it.charpos)))) \
8141 && (it->method == GET_FROM_BUFFER \
8142 || (it->method == GET_FROM_DISPLAY_VECTOR \
8143 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8144
8145 /* If there's a line-/wrap-prefix, handle it. */
8146 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8147 && it->current_y < it->last_visible_y)
8148 handle_line_prefix (it);
8149
8150 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8151 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8152
8153 while (1)
8154 {
8155 int x, i, ascent = 0, descent = 0;
8156
8157 /* Utility macro to reset an iterator with x, ascent, and descent. */
8158 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8159 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8160 (IT)->max_descent = descent)
8161
8162 /* Stop if we move beyond TO_CHARPOS (after an image or a
8163 display string or stretch glyph). */
8164 if ((op & MOVE_TO_POS) != 0
8165 && BUFFERP (it->object)
8166 && it->method == GET_FROM_BUFFER
8167 && (((!it->bidi_p
8168 /* When the iterator is at base embedding level, we
8169 are guaranteed that characters are delivered for
8170 display in strictly increasing order of their
8171 buffer positions. */
8172 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8173 && IT_CHARPOS (*it) > to_charpos)
8174 || (it->bidi_p
8175 && (prev_method == GET_FROM_IMAGE
8176 || prev_method == GET_FROM_STRETCH
8177 || prev_method == GET_FROM_STRING)
8178 /* Passed TO_CHARPOS from left to right. */
8179 && ((prev_pos < to_charpos
8180 && IT_CHARPOS (*it) > to_charpos)
8181 /* Passed TO_CHARPOS from right to left. */
8182 || (prev_pos > to_charpos
8183 && IT_CHARPOS (*it) < to_charpos)))))
8184 {
8185 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8186 {
8187 result = MOVE_POS_MATCH_OR_ZV;
8188 break;
8189 }
8190 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8191 /* If wrap_it is valid, the current position might be in a
8192 word that is wrapped. So, save the iterator in
8193 atpos_it and continue to see if wrapping happens. */
8194 SAVE_IT (atpos_it, *it, atpos_data);
8195 }
8196
8197 /* Stop when ZV reached.
8198 We used to stop here when TO_CHARPOS reached as well, but that is
8199 too soon if this glyph does not fit on this line. So we handle it
8200 explicitly below. */
8201 if (!get_next_display_element (it))
8202 {
8203 result = MOVE_POS_MATCH_OR_ZV;
8204 break;
8205 }
8206
8207 if (it->line_wrap == TRUNCATE)
8208 {
8209 if (BUFFER_POS_REACHED_P ())
8210 {
8211 result = MOVE_POS_MATCH_OR_ZV;
8212 break;
8213 }
8214 }
8215 else
8216 {
8217 if (it->line_wrap == WORD_WRAP)
8218 {
8219 if (IT_DISPLAYING_WHITESPACE (it))
8220 may_wrap = 1;
8221 else if (may_wrap)
8222 {
8223 /* We have reached a glyph that follows one or more
8224 whitespace characters. If the position is
8225 already found, we are done. */
8226 if (atpos_it.sp >= 0)
8227 {
8228 RESTORE_IT (it, &atpos_it, atpos_data);
8229 result = MOVE_POS_MATCH_OR_ZV;
8230 goto done;
8231 }
8232 if (atx_it.sp >= 0)
8233 {
8234 RESTORE_IT (it, &atx_it, atx_data);
8235 result = MOVE_X_REACHED;
8236 goto done;
8237 }
8238 /* Otherwise, we can wrap here. */
8239 SAVE_IT (wrap_it, *it, wrap_data);
8240 may_wrap = 0;
8241 }
8242 }
8243 }
8244
8245 /* Remember the line height for the current line, in case
8246 the next element doesn't fit on the line. */
8247 ascent = it->max_ascent;
8248 descent = it->max_descent;
8249
8250 /* The call to produce_glyphs will get the metrics of the
8251 display element IT is loaded with. Record the x-position
8252 before this display element, in case it doesn't fit on the
8253 line. */
8254 x = it->current_x;
8255
8256 PRODUCE_GLYPHS (it);
8257
8258 if (it->area != TEXT_AREA)
8259 {
8260 prev_method = it->method;
8261 if (it->method == GET_FROM_BUFFER)
8262 prev_pos = IT_CHARPOS (*it);
8263 set_iterator_to_next (it, 1);
8264 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8265 SET_TEXT_POS (this_line_min_pos,
8266 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8267 if (it->bidi_p
8268 && (op & MOVE_TO_POS)
8269 && IT_CHARPOS (*it) > to_charpos
8270 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8271 SAVE_IT (ppos_it, *it, ppos_data);
8272 continue;
8273 }
8274
8275 /* The number of glyphs we get back in IT->nglyphs will normally
8276 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8277 character on a terminal frame, or (iii) a line end. For the
8278 second case, IT->nglyphs - 1 padding glyphs will be present.
8279 (On X frames, there is only one glyph produced for a
8280 composite character.)
8281
8282 The behavior implemented below means, for continuation lines,
8283 that as many spaces of a TAB as fit on the current line are
8284 displayed there. For terminal frames, as many glyphs of a
8285 multi-glyph character are displayed in the current line, too.
8286 This is what the old redisplay code did, and we keep it that
8287 way. Under X, the whole shape of a complex character must
8288 fit on the line or it will be completely displayed in the
8289 next line.
8290
8291 Note that both for tabs and padding glyphs, all glyphs have
8292 the same width. */
8293 if (it->nglyphs)
8294 {
8295 /* More than one glyph or glyph doesn't fit on line. All
8296 glyphs have the same width. */
8297 int single_glyph_width = it->pixel_width / it->nglyphs;
8298 int new_x;
8299 int x_before_this_char = x;
8300 int hpos_before_this_char = it->hpos;
8301
8302 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8303 {
8304 new_x = x + single_glyph_width;
8305
8306 /* We want to leave anything reaching TO_X to the caller. */
8307 if ((op & MOVE_TO_X) && new_x > to_x)
8308 {
8309 if (BUFFER_POS_REACHED_P ())
8310 {
8311 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8312 goto buffer_pos_reached;
8313 if (atpos_it.sp < 0)
8314 {
8315 SAVE_IT (atpos_it, *it, atpos_data);
8316 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8317 }
8318 }
8319 else
8320 {
8321 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8322 {
8323 it->current_x = x;
8324 result = MOVE_X_REACHED;
8325 break;
8326 }
8327 if (atx_it.sp < 0)
8328 {
8329 SAVE_IT (atx_it, *it, atx_data);
8330 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8331 }
8332 }
8333 }
8334
8335 if (/* Lines are continued. */
8336 it->line_wrap != TRUNCATE
8337 && (/* And glyph doesn't fit on the line. */
8338 new_x > it->last_visible_x
8339 /* Or it fits exactly and we're on a window
8340 system frame. */
8341 || (new_x == it->last_visible_x
8342 && FRAME_WINDOW_P (it->f)
8343 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8344 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8345 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8346 {
8347 if (/* IT->hpos == 0 means the very first glyph
8348 doesn't fit on the line, e.g. a wide image. */
8349 it->hpos == 0
8350 || (new_x == it->last_visible_x
8351 && FRAME_WINDOW_P (it->f)))
8352 {
8353 ++it->hpos;
8354 it->current_x = new_x;
8355
8356 /* The character's last glyph just barely fits
8357 in this row. */
8358 if (i == it->nglyphs - 1)
8359 {
8360 /* If this is the destination position,
8361 return a position *before* it in this row,
8362 now that we know it fits in this row. */
8363 if (BUFFER_POS_REACHED_P ())
8364 {
8365 if (it->line_wrap != WORD_WRAP
8366 || wrap_it.sp < 0)
8367 {
8368 it->hpos = hpos_before_this_char;
8369 it->current_x = x_before_this_char;
8370 result = MOVE_POS_MATCH_OR_ZV;
8371 break;
8372 }
8373 if (it->line_wrap == WORD_WRAP
8374 && atpos_it.sp < 0)
8375 {
8376 SAVE_IT (atpos_it, *it, atpos_data);
8377 atpos_it.current_x = x_before_this_char;
8378 atpos_it.hpos = hpos_before_this_char;
8379 }
8380 }
8381
8382 prev_method = it->method;
8383 if (it->method == GET_FROM_BUFFER)
8384 prev_pos = IT_CHARPOS (*it);
8385 set_iterator_to_next (it, 1);
8386 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8387 SET_TEXT_POS (this_line_min_pos,
8388 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8389 /* On graphical terminals, newlines may
8390 "overflow" into the fringe if
8391 overflow-newline-into-fringe is non-nil.
8392 On text terminals, and on graphical
8393 terminals with no right margin, newlines
8394 may overflow into the last glyph on the
8395 display line.*/
8396 if (!FRAME_WINDOW_P (it->f)
8397 || ((it->bidi_p
8398 && it->bidi_it.paragraph_dir == R2L)
8399 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8400 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8401 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8402 {
8403 if (!get_next_display_element (it))
8404 {
8405 result = MOVE_POS_MATCH_OR_ZV;
8406 break;
8407 }
8408 if (BUFFER_POS_REACHED_P ())
8409 {
8410 if (ITERATOR_AT_END_OF_LINE_P (it))
8411 result = MOVE_POS_MATCH_OR_ZV;
8412 else
8413 result = MOVE_LINE_CONTINUED;
8414 break;
8415 }
8416 if (ITERATOR_AT_END_OF_LINE_P (it))
8417 {
8418 result = MOVE_NEWLINE_OR_CR;
8419 break;
8420 }
8421 }
8422 }
8423 }
8424 else
8425 IT_RESET_X_ASCENT_DESCENT (it);
8426
8427 if (wrap_it.sp >= 0)
8428 {
8429 RESTORE_IT (it, &wrap_it, wrap_data);
8430 atpos_it.sp = -1;
8431 atx_it.sp = -1;
8432 }
8433
8434 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8435 IT_CHARPOS (*it)));
8436 result = MOVE_LINE_CONTINUED;
8437 break;
8438 }
8439
8440 if (BUFFER_POS_REACHED_P ())
8441 {
8442 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8443 goto buffer_pos_reached;
8444 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8445 {
8446 SAVE_IT (atpos_it, *it, atpos_data);
8447 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8448 }
8449 }
8450
8451 if (new_x > it->first_visible_x)
8452 {
8453 /* Glyph is visible. Increment number of glyphs that
8454 would be displayed. */
8455 ++it->hpos;
8456 }
8457 }
8458
8459 if (result != MOVE_UNDEFINED)
8460 break;
8461 }
8462 else if (BUFFER_POS_REACHED_P ())
8463 {
8464 buffer_pos_reached:
8465 IT_RESET_X_ASCENT_DESCENT (it);
8466 result = MOVE_POS_MATCH_OR_ZV;
8467 break;
8468 }
8469 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8470 {
8471 /* Stop when TO_X specified and reached. This check is
8472 necessary here because of lines consisting of a line end,
8473 only. The line end will not produce any glyphs and we
8474 would never get MOVE_X_REACHED. */
8475 eassert (it->nglyphs == 0);
8476 result = MOVE_X_REACHED;
8477 break;
8478 }
8479
8480 /* Is this a line end? If yes, we're done. */
8481 if (ITERATOR_AT_END_OF_LINE_P (it))
8482 {
8483 /* If we are past TO_CHARPOS, but never saw any character
8484 positions smaller than TO_CHARPOS, return
8485 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8486 did. */
8487 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8488 {
8489 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8490 {
8491 if (IT_CHARPOS (ppos_it) < ZV)
8492 {
8493 RESTORE_IT (it, &ppos_it, ppos_data);
8494 result = MOVE_POS_MATCH_OR_ZV;
8495 }
8496 else
8497 goto buffer_pos_reached;
8498 }
8499 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8500 && IT_CHARPOS (*it) > to_charpos)
8501 goto buffer_pos_reached;
8502 else
8503 result = MOVE_NEWLINE_OR_CR;
8504 }
8505 else
8506 result = MOVE_NEWLINE_OR_CR;
8507 break;
8508 }
8509
8510 prev_method = it->method;
8511 if (it->method == GET_FROM_BUFFER)
8512 prev_pos = IT_CHARPOS (*it);
8513 /* The current display element has been consumed. Advance
8514 to the next. */
8515 set_iterator_to_next (it, 1);
8516 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8517 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8518 if (IT_CHARPOS (*it) < to_charpos)
8519 saw_smaller_pos = 1;
8520 if (it->bidi_p
8521 && (op & MOVE_TO_POS)
8522 && IT_CHARPOS (*it) >= to_charpos
8523 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8524 SAVE_IT (ppos_it, *it, ppos_data);
8525
8526 /* Stop if lines are truncated and IT's current x-position is
8527 past the right edge of the window now. */
8528 if (it->line_wrap == TRUNCATE
8529 && it->current_x >= it->last_visible_x)
8530 {
8531 if (!FRAME_WINDOW_P (it->f)
8532 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8533 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8534 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8535 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8536 {
8537 int at_eob_p = 0;
8538
8539 if ((at_eob_p = !get_next_display_element (it))
8540 || BUFFER_POS_REACHED_P ()
8541 /* If we are past TO_CHARPOS, but never saw any
8542 character positions smaller than TO_CHARPOS,
8543 return MOVE_POS_MATCH_OR_ZV, like the
8544 unidirectional display did. */
8545 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8546 && !saw_smaller_pos
8547 && IT_CHARPOS (*it) > to_charpos))
8548 {
8549 if (it->bidi_p
8550 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8551 RESTORE_IT (it, &ppos_it, ppos_data);
8552 result = MOVE_POS_MATCH_OR_ZV;
8553 break;
8554 }
8555 if (ITERATOR_AT_END_OF_LINE_P (it))
8556 {
8557 result = MOVE_NEWLINE_OR_CR;
8558 break;
8559 }
8560 }
8561 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8562 && !saw_smaller_pos
8563 && IT_CHARPOS (*it) > to_charpos)
8564 {
8565 if (IT_CHARPOS (ppos_it) < ZV)
8566 RESTORE_IT (it, &ppos_it, ppos_data);
8567 result = MOVE_POS_MATCH_OR_ZV;
8568 break;
8569 }
8570 result = MOVE_LINE_TRUNCATED;
8571 break;
8572 }
8573 #undef IT_RESET_X_ASCENT_DESCENT
8574 }
8575
8576 #undef BUFFER_POS_REACHED_P
8577
8578 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8579 restore the saved iterator. */
8580 if (atpos_it.sp >= 0)
8581 RESTORE_IT (it, &atpos_it, atpos_data);
8582 else if (atx_it.sp >= 0)
8583 RESTORE_IT (it, &atx_it, atx_data);
8584
8585 done:
8586
8587 if (atpos_data)
8588 bidi_unshelve_cache (atpos_data, 1);
8589 if (atx_data)
8590 bidi_unshelve_cache (atx_data, 1);
8591 if (wrap_data)
8592 bidi_unshelve_cache (wrap_data, 1);
8593 if (ppos_data)
8594 bidi_unshelve_cache (ppos_data, 1);
8595
8596 /* Restore the iterator settings altered at the beginning of this
8597 function. */
8598 it->glyph_row = saved_glyph_row;
8599 return result;
8600 }
8601
8602 /* For external use. */
8603 void
8604 move_it_in_display_line (struct it *it,
8605 ptrdiff_t to_charpos, int to_x,
8606 enum move_operation_enum op)
8607 {
8608 if (it->line_wrap == WORD_WRAP
8609 && (op & MOVE_TO_X))
8610 {
8611 struct it save_it;
8612 void *save_data = NULL;
8613 int skip;
8614
8615 SAVE_IT (save_it, *it, save_data);
8616 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8617 /* When word-wrap is on, TO_X may lie past the end
8618 of a wrapped line. Then it->current is the
8619 character on the next line, so backtrack to the
8620 space before the wrap point. */
8621 if (skip == MOVE_LINE_CONTINUED)
8622 {
8623 int prev_x = max (it->current_x - 1, 0);
8624 RESTORE_IT (it, &save_it, save_data);
8625 move_it_in_display_line_to
8626 (it, -1, prev_x, MOVE_TO_X);
8627 }
8628 else
8629 bidi_unshelve_cache (save_data, 1);
8630 }
8631 else
8632 move_it_in_display_line_to (it, to_charpos, to_x, op);
8633 }
8634
8635
8636 /* Move IT forward until it satisfies one or more of the criteria in
8637 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8638
8639 OP is a bit-mask that specifies where to stop, and in particular,
8640 which of those four position arguments makes a difference. See the
8641 description of enum move_operation_enum.
8642
8643 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8644 screen line, this function will set IT to the next position that is
8645 displayed to the right of TO_CHARPOS on the screen. */
8646
8647 void
8648 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8649 {
8650 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8651 int line_height, line_start_x = 0, reached = 0;
8652 void *backup_data = NULL;
8653
8654 for (;;)
8655 {
8656 if (op & MOVE_TO_VPOS)
8657 {
8658 /* If no TO_CHARPOS and no TO_X specified, stop at the
8659 start of the line TO_VPOS. */
8660 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8661 {
8662 if (it->vpos == to_vpos)
8663 {
8664 reached = 1;
8665 break;
8666 }
8667 else
8668 skip = move_it_in_display_line_to (it, -1, -1, 0);
8669 }
8670 else
8671 {
8672 /* TO_VPOS >= 0 means stop at TO_X in the line at
8673 TO_VPOS, or at TO_POS, whichever comes first. */
8674 if (it->vpos == to_vpos)
8675 {
8676 reached = 2;
8677 break;
8678 }
8679
8680 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8681
8682 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8683 {
8684 reached = 3;
8685 break;
8686 }
8687 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8688 {
8689 /* We have reached TO_X but not in the line we want. */
8690 skip = move_it_in_display_line_to (it, to_charpos,
8691 -1, MOVE_TO_POS);
8692 if (skip == MOVE_POS_MATCH_OR_ZV)
8693 {
8694 reached = 4;
8695 break;
8696 }
8697 }
8698 }
8699 }
8700 else if (op & MOVE_TO_Y)
8701 {
8702 struct it it_backup;
8703
8704 if (it->line_wrap == WORD_WRAP)
8705 SAVE_IT (it_backup, *it, backup_data);
8706
8707 /* TO_Y specified means stop at TO_X in the line containing
8708 TO_Y---or at TO_CHARPOS if this is reached first. The
8709 problem is that we can't really tell whether the line
8710 contains TO_Y before we have completely scanned it, and
8711 this may skip past TO_X. What we do is to first scan to
8712 TO_X.
8713
8714 If TO_X is not specified, use a TO_X of zero. The reason
8715 is to make the outcome of this function more predictable.
8716 If we didn't use TO_X == 0, we would stop at the end of
8717 the line which is probably not what a caller would expect
8718 to happen. */
8719 skip = move_it_in_display_line_to
8720 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8721 (MOVE_TO_X | (op & MOVE_TO_POS)));
8722
8723 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8724 if (skip == MOVE_POS_MATCH_OR_ZV)
8725 reached = 5;
8726 else if (skip == MOVE_X_REACHED)
8727 {
8728 /* If TO_X was reached, we want to know whether TO_Y is
8729 in the line. We know this is the case if the already
8730 scanned glyphs make the line tall enough. Otherwise,
8731 we must check by scanning the rest of the line. */
8732 line_height = it->max_ascent + it->max_descent;
8733 if (to_y >= it->current_y
8734 && to_y < it->current_y + line_height)
8735 {
8736 reached = 6;
8737 break;
8738 }
8739 SAVE_IT (it_backup, *it, backup_data);
8740 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8741 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8742 op & MOVE_TO_POS);
8743 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8744 line_height = it->max_ascent + it->max_descent;
8745 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8746
8747 if (to_y >= it->current_y
8748 && to_y < it->current_y + line_height)
8749 {
8750 /* If TO_Y is in this line and TO_X was reached
8751 above, we scanned too far. We have to restore
8752 IT's settings to the ones before skipping. But
8753 keep the more accurate values of max_ascent and
8754 max_descent we've found while skipping the rest
8755 of the line, for the sake of callers, such as
8756 pos_visible_p, that need to know the line
8757 height. */
8758 int max_ascent = it->max_ascent;
8759 int max_descent = it->max_descent;
8760
8761 RESTORE_IT (it, &it_backup, backup_data);
8762 it->max_ascent = max_ascent;
8763 it->max_descent = max_descent;
8764 reached = 6;
8765 }
8766 else
8767 {
8768 skip = skip2;
8769 if (skip == MOVE_POS_MATCH_OR_ZV)
8770 reached = 7;
8771 }
8772 }
8773 else
8774 {
8775 /* Check whether TO_Y is in this line. */
8776 line_height = it->max_ascent + it->max_descent;
8777 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8778
8779 if (to_y >= it->current_y
8780 && to_y < it->current_y + line_height)
8781 {
8782 /* When word-wrap is on, TO_X may lie past the end
8783 of a wrapped line. Then it->current is the
8784 character on the next line, so backtrack to the
8785 space before the wrap point. */
8786 if (skip == MOVE_LINE_CONTINUED
8787 && it->line_wrap == WORD_WRAP)
8788 {
8789 int prev_x = max (it->current_x - 1, 0);
8790 RESTORE_IT (it, &it_backup, backup_data);
8791 skip = move_it_in_display_line_to
8792 (it, -1, prev_x, MOVE_TO_X);
8793 }
8794 reached = 6;
8795 }
8796 }
8797
8798 if (reached)
8799 break;
8800 }
8801 else if (BUFFERP (it->object)
8802 && (it->method == GET_FROM_BUFFER
8803 || it->method == GET_FROM_STRETCH)
8804 && IT_CHARPOS (*it) >= to_charpos
8805 /* Under bidi iteration, a call to set_iterator_to_next
8806 can scan far beyond to_charpos if the initial
8807 portion of the next line needs to be reordered. In
8808 that case, give move_it_in_display_line_to another
8809 chance below. */
8810 && !(it->bidi_p
8811 && it->bidi_it.scan_dir == -1))
8812 skip = MOVE_POS_MATCH_OR_ZV;
8813 else
8814 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8815
8816 switch (skip)
8817 {
8818 case MOVE_POS_MATCH_OR_ZV:
8819 reached = 8;
8820 goto out;
8821
8822 case MOVE_NEWLINE_OR_CR:
8823 set_iterator_to_next (it, 1);
8824 it->continuation_lines_width = 0;
8825 break;
8826
8827 case MOVE_LINE_TRUNCATED:
8828 it->continuation_lines_width = 0;
8829 reseat_at_next_visible_line_start (it, 0);
8830 if ((op & MOVE_TO_POS) != 0
8831 && IT_CHARPOS (*it) > to_charpos)
8832 {
8833 reached = 9;
8834 goto out;
8835 }
8836 break;
8837
8838 case MOVE_LINE_CONTINUED:
8839 /* For continued lines ending in a tab, some of the glyphs
8840 associated with the tab are displayed on the current
8841 line. Since it->current_x does not include these glyphs,
8842 we use it->last_visible_x instead. */
8843 if (it->c == '\t')
8844 {
8845 it->continuation_lines_width += it->last_visible_x;
8846 /* When moving by vpos, ensure that the iterator really
8847 advances to the next line (bug#847, bug#969). Fixme:
8848 do we need to do this in other circumstances? */
8849 if (it->current_x != it->last_visible_x
8850 && (op & MOVE_TO_VPOS)
8851 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8852 {
8853 line_start_x = it->current_x + it->pixel_width
8854 - it->last_visible_x;
8855 set_iterator_to_next (it, 0);
8856 }
8857 }
8858 else
8859 it->continuation_lines_width += it->current_x;
8860 break;
8861
8862 default:
8863 abort ();
8864 }
8865
8866 /* Reset/increment for the next run. */
8867 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8868 it->current_x = line_start_x;
8869 line_start_x = 0;
8870 it->hpos = 0;
8871 it->current_y += it->max_ascent + it->max_descent;
8872 ++it->vpos;
8873 last_height = it->max_ascent + it->max_descent;
8874 last_max_ascent = it->max_ascent;
8875 it->max_ascent = it->max_descent = 0;
8876 }
8877
8878 out:
8879
8880 /* On text terminals, we may stop at the end of a line in the middle
8881 of a multi-character glyph. If the glyph itself is continued,
8882 i.e. it is actually displayed on the next line, don't treat this
8883 stopping point as valid; move to the next line instead (unless
8884 that brings us offscreen). */
8885 if (!FRAME_WINDOW_P (it->f)
8886 && op & MOVE_TO_POS
8887 && IT_CHARPOS (*it) == to_charpos
8888 && it->what == IT_CHARACTER
8889 && it->nglyphs > 1
8890 && it->line_wrap == WINDOW_WRAP
8891 && it->current_x == it->last_visible_x - 1
8892 && it->c != '\n'
8893 && it->c != '\t'
8894 && it->vpos < XFASTINT (WVAR (it->w, window_end_vpos)))
8895 {
8896 it->continuation_lines_width += it->current_x;
8897 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8898 it->current_y += it->max_ascent + it->max_descent;
8899 ++it->vpos;
8900 last_height = it->max_ascent + it->max_descent;
8901 last_max_ascent = it->max_ascent;
8902 }
8903
8904 if (backup_data)
8905 bidi_unshelve_cache (backup_data, 1);
8906
8907 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8908 }
8909
8910
8911 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8912
8913 If DY > 0, move IT backward at least that many pixels. DY = 0
8914 means move IT backward to the preceding line start or BEGV. This
8915 function may move over more than DY pixels if IT->current_y - DY
8916 ends up in the middle of a line; in this case IT->current_y will be
8917 set to the top of the line moved to. */
8918
8919 void
8920 move_it_vertically_backward (struct it *it, int dy)
8921 {
8922 int nlines, h;
8923 struct it it2, it3;
8924 void *it2data = NULL, *it3data = NULL;
8925 ptrdiff_t start_pos;
8926
8927 move_further_back:
8928 eassert (dy >= 0);
8929
8930 start_pos = IT_CHARPOS (*it);
8931
8932 /* Estimate how many newlines we must move back. */
8933 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8934
8935 /* Set the iterator's position that many lines back. */
8936 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8937 back_to_previous_visible_line_start (it);
8938
8939 /* Reseat the iterator here. When moving backward, we don't want
8940 reseat to skip forward over invisible text, set up the iterator
8941 to deliver from overlay strings at the new position etc. So,
8942 use reseat_1 here. */
8943 reseat_1 (it, it->current.pos, 1);
8944
8945 /* We are now surely at a line start. */
8946 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8947 reordering is in effect. */
8948 it->continuation_lines_width = 0;
8949
8950 /* Move forward and see what y-distance we moved. First move to the
8951 start of the next line so that we get its height. We need this
8952 height to be able to tell whether we reached the specified
8953 y-distance. */
8954 SAVE_IT (it2, *it, it2data);
8955 it2.max_ascent = it2.max_descent = 0;
8956 do
8957 {
8958 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8959 MOVE_TO_POS | MOVE_TO_VPOS);
8960 }
8961 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8962 /* If we are in a display string which starts at START_POS,
8963 and that display string includes a newline, and we are
8964 right after that newline (i.e. at the beginning of a
8965 display line), exit the loop, because otherwise we will
8966 infloop, since move_it_to will see that it is already at
8967 START_POS and will not move. */
8968 || (it2.method == GET_FROM_STRING
8969 && IT_CHARPOS (it2) == start_pos
8970 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8971 eassert (IT_CHARPOS (*it) >= BEGV);
8972 SAVE_IT (it3, it2, it3data);
8973
8974 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8975 eassert (IT_CHARPOS (*it) >= BEGV);
8976 /* H is the actual vertical distance from the position in *IT
8977 and the starting position. */
8978 h = it2.current_y - it->current_y;
8979 /* NLINES is the distance in number of lines. */
8980 nlines = it2.vpos - it->vpos;
8981
8982 /* Correct IT's y and vpos position
8983 so that they are relative to the starting point. */
8984 it->vpos -= nlines;
8985 it->current_y -= h;
8986
8987 if (dy == 0)
8988 {
8989 /* DY == 0 means move to the start of the screen line. The
8990 value of nlines is > 0 if continuation lines were involved,
8991 or if the original IT position was at start of a line. */
8992 RESTORE_IT (it, it, it2data);
8993 if (nlines > 0)
8994 move_it_by_lines (it, nlines);
8995 /* The above code moves us to some position NLINES down,
8996 usually to its first glyph (leftmost in an L2R line), but
8997 that's not necessarily the start of the line, under bidi
8998 reordering. We want to get to the character position
8999 that is immediately after the newline of the previous
9000 line. */
9001 if (it->bidi_p
9002 && !it->continuation_lines_width
9003 && !STRINGP (it->string)
9004 && IT_CHARPOS (*it) > BEGV
9005 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9006 {
9007 ptrdiff_t nl_pos =
9008 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9009
9010 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9011 }
9012 bidi_unshelve_cache (it3data, 1);
9013 }
9014 else
9015 {
9016 /* The y-position we try to reach, relative to *IT.
9017 Note that H has been subtracted in front of the if-statement. */
9018 int target_y = it->current_y + h - dy;
9019 int y0 = it3.current_y;
9020 int y1;
9021 int line_height;
9022
9023 RESTORE_IT (&it3, &it3, it3data);
9024 y1 = line_bottom_y (&it3);
9025 line_height = y1 - y0;
9026 RESTORE_IT (it, it, it2data);
9027 /* If we did not reach target_y, try to move further backward if
9028 we can. If we moved too far backward, try to move forward. */
9029 if (target_y < it->current_y
9030 /* This is heuristic. In a window that's 3 lines high, with
9031 a line height of 13 pixels each, recentering with point
9032 on the bottom line will try to move -39/2 = 19 pixels
9033 backward. Try to avoid moving into the first line. */
9034 && (it->current_y - target_y
9035 > min (window_box_height (it->w), line_height * 2 / 3))
9036 && IT_CHARPOS (*it) > BEGV)
9037 {
9038 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9039 target_y - it->current_y));
9040 dy = it->current_y - target_y;
9041 goto move_further_back;
9042 }
9043 else if (target_y >= it->current_y + line_height
9044 && IT_CHARPOS (*it) < ZV)
9045 {
9046 /* Should move forward by at least one line, maybe more.
9047
9048 Note: Calling move_it_by_lines can be expensive on
9049 terminal frames, where compute_motion is used (via
9050 vmotion) to do the job, when there are very long lines
9051 and truncate-lines is nil. That's the reason for
9052 treating terminal frames specially here. */
9053
9054 if (!FRAME_WINDOW_P (it->f))
9055 move_it_vertically (it, target_y - (it->current_y + line_height));
9056 else
9057 {
9058 do
9059 {
9060 move_it_by_lines (it, 1);
9061 }
9062 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9063 }
9064 }
9065 }
9066 }
9067
9068
9069 /* Move IT by a specified amount of pixel lines DY. DY negative means
9070 move backwards. DY = 0 means move to start of screen line. At the
9071 end, IT will be on the start of a screen line. */
9072
9073 void
9074 move_it_vertically (struct it *it, int dy)
9075 {
9076 if (dy <= 0)
9077 move_it_vertically_backward (it, -dy);
9078 else
9079 {
9080 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9081 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9082 MOVE_TO_POS | MOVE_TO_Y);
9083 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9084
9085 /* If buffer ends in ZV without a newline, move to the start of
9086 the line to satisfy the post-condition. */
9087 if (IT_CHARPOS (*it) == ZV
9088 && ZV > BEGV
9089 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9090 move_it_by_lines (it, 0);
9091 }
9092 }
9093
9094
9095 /* Move iterator IT past the end of the text line it is in. */
9096
9097 void
9098 move_it_past_eol (struct it *it)
9099 {
9100 enum move_it_result rc;
9101
9102 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9103 if (rc == MOVE_NEWLINE_OR_CR)
9104 set_iterator_to_next (it, 0);
9105 }
9106
9107
9108 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9109 negative means move up. DVPOS == 0 means move to the start of the
9110 screen line.
9111
9112 Optimization idea: If we would know that IT->f doesn't use
9113 a face with proportional font, we could be faster for
9114 truncate-lines nil. */
9115
9116 void
9117 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9118 {
9119
9120 /* The commented-out optimization uses vmotion on terminals. This
9121 gives bad results, because elements like it->what, on which
9122 callers such as pos_visible_p rely, aren't updated. */
9123 /* struct position pos;
9124 if (!FRAME_WINDOW_P (it->f))
9125 {
9126 struct text_pos textpos;
9127
9128 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9129 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9130 reseat (it, textpos, 1);
9131 it->vpos += pos.vpos;
9132 it->current_y += pos.vpos;
9133 }
9134 else */
9135
9136 if (dvpos == 0)
9137 {
9138 /* DVPOS == 0 means move to the start of the screen line. */
9139 move_it_vertically_backward (it, 0);
9140 /* Let next call to line_bottom_y calculate real line height */
9141 last_height = 0;
9142 }
9143 else if (dvpos > 0)
9144 {
9145 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9146 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9147 {
9148 /* Only move to the next buffer position if we ended up in a
9149 string from display property, not in an overlay string
9150 (before-string or after-string). That is because the
9151 latter don't conceal the underlying buffer position, so
9152 we can ask to move the iterator to the exact position we
9153 are interested in. Note that, even if we are already at
9154 IT_CHARPOS (*it), the call below is not a no-op, as it
9155 will detect that we are at the end of the string, pop the
9156 iterator, and compute it->current_x and it->hpos
9157 correctly. */
9158 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9159 -1, -1, -1, MOVE_TO_POS);
9160 }
9161 }
9162 else
9163 {
9164 struct it it2;
9165 void *it2data = NULL;
9166 ptrdiff_t start_charpos, i;
9167
9168 /* Start at the beginning of the screen line containing IT's
9169 position. This may actually move vertically backwards,
9170 in case of overlays, so adjust dvpos accordingly. */
9171 dvpos += it->vpos;
9172 move_it_vertically_backward (it, 0);
9173 dvpos -= it->vpos;
9174
9175 /* Go back -DVPOS visible lines and reseat the iterator there. */
9176 start_charpos = IT_CHARPOS (*it);
9177 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9178 back_to_previous_visible_line_start (it);
9179 reseat (it, it->current.pos, 1);
9180
9181 /* Move further back if we end up in a string or an image. */
9182 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9183 {
9184 /* First try to move to start of display line. */
9185 dvpos += it->vpos;
9186 move_it_vertically_backward (it, 0);
9187 dvpos -= it->vpos;
9188 if (IT_POS_VALID_AFTER_MOVE_P (it))
9189 break;
9190 /* If start of line is still in string or image,
9191 move further back. */
9192 back_to_previous_visible_line_start (it);
9193 reseat (it, it->current.pos, 1);
9194 dvpos--;
9195 }
9196
9197 it->current_x = it->hpos = 0;
9198
9199 /* Above call may have moved too far if continuation lines
9200 are involved. Scan forward and see if it did. */
9201 SAVE_IT (it2, *it, it2data);
9202 it2.vpos = it2.current_y = 0;
9203 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9204 it->vpos -= it2.vpos;
9205 it->current_y -= it2.current_y;
9206 it->current_x = it->hpos = 0;
9207
9208 /* If we moved too far back, move IT some lines forward. */
9209 if (it2.vpos > -dvpos)
9210 {
9211 int delta = it2.vpos + dvpos;
9212
9213 RESTORE_IT (&it2, &it2, it2data);
9214 SAVE_IT (it2, *it, it2data);
9215 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9216 /* Move back again if we got too far ahead. */
9217 if (IT_CHARPOS (*it) >= start_charpos)
9218 RESTORE_IT (it, &it2, it2data);
9219 else
9220 bidi_unshelve_cache (it2data, 1);
9221 }
9222 else
9223 RESTORE_IT (it, it, it2data);
9224 }
9225 }
9226
9227 /* Return 1 if IT points into the middle of a display vector. */
9228
9229 int
9230 in_display_vector_p (struct it *it)
9231 {
9232 return (it->method == GET_FROM_DISPLAY_VECTOR
9233 && it->current.dpvec_index > 0
9234 && it->dpvec + it->current.dpvec_index != it->dpend);
9235 }
9236
9237 \f
9238 /***********************************************************************
9239 Messages
9240 ***********************************************************************/
9241
9242
9243 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9244 to *Messages*. */
9245
9246 void
9247 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9248 {
9249 Lisp_Object args[3];
9250 Lisp_Object msg, fmt;
9251 char *buffer;
9252 ptrdiff_t len;
9253 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9254 USE_SAFE_ALLOCA;
9255
9256 /* Do nothing if called asynchronously. Inserting text into
9257 a buffer may call after-change-functions and alike and
9258 that would means running Lisp asynchronously. */
9259 if (handling_signal)
9260 return;
9261
9262 fmt = msg = Qnil;
9263 GCPRO4 (fmt, msg, arg1, arg2);
9264
9265 args[0] = fmt = build_string (format);
9266 args[1] = arg1;
9267 args[2] = arg2;
9268 msg = Fformat (3, args);
9269
9270 len = SBYTES (msg) + 1;
9271 SAFE_ALLOCA (buffer, char *, len);
9272 memcpy (buffer, SDATA (msg), len);
9273
9274 message_dolog (buffer, len - 1, 1, 0);
9275 SAFE_FREE ();
9276
9277 UNGCPRO;
9278 }
9279
9280
9281 /* Output a newline in the *Messages* buffer if "needs" one. */
9282
9283 void
9284 message_log_maybe_newline (void)
9285 {
9286 if (message_log_need_newline)
9287 message_dolog ("", 0, 1, 0);
9288 }
9289
9290
9291 /* Add a string M of length NBYTES to the message log, optionally
9292 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9293 nonzero, means interpret the contents of M as multibyte. This
9294 function calls low-level routines in order to bypass text property
9295 hooks, etc. which might not be safe to run.
9296
9297 This may GC (insert may run before/after change hooks),
9298 so the buffer M must NOT point to a Lisp string. */
9299
9300 void
9301 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9302 {
9303 const unsigned char *msg = (const unsigned char *) m;
9304
9305 if (!NILP (Vmemory_full))
9306 return;
9307
9308 if (!NILP (Vmessage_log_max))
9309 {
9310 struct buffer *oldbuf;
9311 Lisp_Object oldpoint, oldbegv, oldzv;
9312 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9313 ptrdiff_t point_at_end = 0;
9314 ptrdiff_t zv_at_end = 0;
9315 Lisp_Object old_deactivate_mark, tem;
9316 struct gcpro gcpro1;
9317
9318 old_deactivate_mark = Vdeactivate_mark;
9319 oldbuf = current_buffer;
9320 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9321 BVAR (current_buffer, undo_list) = Qt;
9322
9323 oldpoint = message_dolog_marker1;
9324 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9325 oldbegv = message_dolog_marker2;
9326 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9327 oldzv = message_dolog_marker3;
9328 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9329 GCPRO1 (old_deactivate_mark);
9330
9331 if (PT == Z)
9332 point_at_end = 1;
9333 if (ZV == Z)
9334 zv_at_end = 1;
9335
9336 BEGV = BEG;
9337 BEGV_BYTE = BEG_BYTE;
9338 ZV = Z;
9339 ZV_BYTE = Z_BYTE;
9340 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9341
9342 /* Insert the string--maybe converting multibyte to single byte
9343 or vice versa, so that all the text fits the buffer. */
9344 if (multibyte
9345 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9346 {
9347 ptrdiff_t i;
9348 int c, char_bytes;
9349 char work[1];
9350
9351 /* Convert a multibyte string to single-byte
9352 for the *Message* buffer. */
9353 for (i = 0; i < nbytes; i += char_bytes)
9354 {
9355 c = string_char_and_length (msg + i, &char_bytes);
9356 work[0] = (ASCII_CHAR_P (c)
9357 ? c
9358 : multibyte_char_to_unibyte (c));
9359 insert_1_both (work, 1, 1, 1, 0, 0);
9360 }
9361 }
9362 else if (! multibyte
9363 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9364 {
9365 ptrdiff_t i;
9366 int c, char_bytes;
9367 unsigned char str[MAX_MULTIBYTE_LENGTH];
9368 /* Convert a single-byte string to multibyte
9369 for the *Message* buffer. */
9370 for (i = 0; i < nbytes; i++)
9371 {
9372 c = msg[i];
9373 MAKE_CHAR_MULTIBYTE (c);
9374 char_bytes = CHAR_STRING (c, str);
9375 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9376 }
9377 }
9378 else if (nbytes)
9379 insert_1 (m, nbytes, 1, 0, 0);
9380
9381 if (nlflag)
9382 {
9383 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9384 printmax_t dups;
9385 insert_1 ("\n", 1, 1, 0, 0);
9386
9387 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9388 this_bol = PT;
9389 this_bol_byte = PT_BYTE;
9390
9391 /* See if this line duplicates the previous one.
9392 If so, combine duplicates. */
9393 if (this_bol > BEG)
9394 {
9395 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9396 prev_bol = PT;
9397 prev_bol_byte = PT_BYTE;
9398
9399 dups = message_log_check_duplicate (prev_bol_byte,
9400 this_bol_byte);
9401 if (dups)
9402 {
9403 del_range_both (prev_bol, prev_bol_byte,
9404 this_bol, this_bol_byte, 0);
9405 if (dups > 1)
9406 {
9407 char dupstr[sizeof " [ times]"
9408 + INT_STRLEN_BOUND (printmax_t)];
9409
9410 /* If you change this format, don't forget to also
9411 change message_log_check_duplicate. */
9412 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9413 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9414 insert_1 (dupstr, duplen, 1, 0, 1);
9415 }
9416 }
9417 }
9418
9419 /* If we have more than the desired maximum number of lines
9420 in the *Messages* buffer now, delete the oldest ones.
9421 This is safe because we don't have undo in this buffer. */
9422
9423 if (NATNUMP (Vmessage_log_max))
9424 {
9425 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9426 -XFASTINT (Vmessage_log_max) - 1, 0);
9427 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9428 }
9429 }
9430 BEGV = XMARKER (oldbegv)->charpos;
9431 BEGV_BYTE = marker_byte_position (oldbegv);
9432
9433 if (zv_at_end)
9434 {
9435 ZV = Z;
9436 ZV_BYTE = Z_BYTE;
9437 }
9438 else
9439 {
9440 ZV = XMARKER (oldzv)->charpos;
9441 ZV_BYTE = marker_byte_position (oldzv);
9442 }
9443
9444 if (point_at_end)
9445 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9446 else
9447 /* We can't do Fgoto_char (oldpoint) because it will run some
9448 Lisp code. */
9449 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9450 XMARKER (oldpoint)->bytepos);
9451
9452 UNGCPRO;
9453 unchain_marker (XMARKER (oldpoint));
9454 unchain_marker (XMARKER (oldbegv));
9455 unchain_marker (XMARKER (oldzv));
9456
9457 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9458 set_buffer_internal (oldbuf);
9459 if (NILP (tem))
9460 windows_or_buffers_changed = old_windows_or_buffers_changed;
9461 message_log_need_newline = !nlflag;
9462 Vdeactivate_mark = old_deactivate_mark;
9463 }
9464 }
9465
9466
9467 /* We are at the end of the buffer after just having inserted a newline.
9468 (Note: We depend on the fact we won't be crossing the gap.)
9469 Check to see if the most recent message looks a lot like the previous one.
9470 Return 0 if different, 1 if the new one should just replace it, or a
9471 value N > 1 if we should also append " [N times]". */
9472
9473 static intmax_t
9474 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9475 {
9476 ptrdiff_t i;
9477 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9478 int seen_dots = 0;
9479 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9480 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9481
9482 for (i = 0; i < len; i++)
9483 {
9484 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9485 seen_dots = 1;
9486 if (p1[i] != p2[i])
9487 return seen_dots;
9488 }
9489 p1 += len;
9490 if (*p1 == '\n')
9491 return 2;
9492 if (*p1++ == ' ' && *p1++ == '[')
9493 {
9494 char *pend;
9495 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9496 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9497 return n+1;
9498 }
9499 return 0;
9500 }
9501 \f
9502
9503 /* Display an echo area message M with a specified length of NBYTES
9504 bytes. The string may include null characters. If M is 0, clear
9505 out any existing message, and let the mini-buffer text show
9506 through.
9507
9508 This may GC, so the buffer M must NOT point to a Lisp string. */
9509
9510 void
9511 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9512 {
9513 /* First flush out any partial line written with print. */
9514 message_log_maybe_newline ();
9515 if (m)
9516 message_dolog (m, nbytes, 1, multibyte);
9517 message2_nolog (m, nbytes, multibyte);
9518 }
9519
9520
9521 /* The non-logging counterpart of message2. */
9522
9523 void
9524 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9525 {
9526 struct frame *sf = SELECTED_FRAME ();
9527 message_enable_multibyte = multibyte;
9528
9529 if (FRAME_INITIAL_P (sf))
9530 {
9531 if (noninteractive_need_newline)
9532 putc ('\n', stderr);
9533 noninteractive_need_newline = 0;
9534 if (m)
9535 fwrite (m, nbytes, 1, stderr);
9536 if (cursor_in_echo_area == 0)
9537 fprintf (stderr, "\n");
9538 fflush (stderr);
9539 }
9540 /* A null message buffer means that the frame hasn't really been
9541 initialized yet. Error messages get reported properly by
9542 cmd_error, so this must be just an informative message; toss it. */
9543 else if (INTERACTIVE
9544 && sf->glyphs_initialized_p
9545 && FRAME_MESSAGE_BUF (sf))
9546 {
9547 Lisp_Object mini_window;
9548 struct frame *f;
9549
9550 /* Get the frame containing the mini-buffer
9551 that the selected frame is using. */
9552 mini_window = FRAME_MINIBUF_WINDOW (sf);
9553 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9554
9555 FRAME_SAMPLE_VISIBILITY (f);
9556 if (FRAME_VISIBLE_P (sf)
9557 && ! FRAME_VISIBLE_P (f))
9558 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9559
9560 if (m)
9561 {
9562 set_message (m, Qnil, nbytes, multibyte);
9563 if (minibuffer_auto_raise)
9564 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9565 }
9566 else
9567 clear_message (1, 1);
9568
9569 do_pending_window_change (0);
9570 echo_area_display (1);
9571 do_pending_window_change (0);
9572 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9573 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9574 }
9575 }
9576
9577
9578 /* Display an echo area message M with a specified length of NBYTES
9579 bytes. The string may include null characters. If M is not a
9580 string, clear out any existing message, and let the mini-buffer
9581 text show through.
9582
9583 This function cancels echoing. */
9584
9585 void
9586 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9587 {
9588 struct gcpro gcpro1;
9589
9590 GCPRO1 (m);
9591 clear_message (1,1);
9592 cancel_echoing ();
9593
9594 /* First flush out any partial line written with print. */
9595 message_log_maybe_newline ();
9596 if (STRINGP (m))
9597 {
9598 char *buffer;
9599 USE_SAFE_ALLOCA;
9600
9601 SAFE_ALLOCA (buffer, char *, nbytes);
9602 memcpy (buffer, SDATA (m), nbytes);
9603 message_dolog (buffer, nbytes, 1, multibyte);
9604 SAFE_FREE ();
9605 }
9606 message3_nolog (m, nbytes, multibyte);
9607
9608 UNGCPRO;
9609 }
9610
9611
9612 /* The non-logging version of message3.
9613 This does not cancel echoing, because it is used for echoing.
9614 Perhaps we need to make a separate function for echoing
9615 and make this cancel echoing. */
9616
9617 void
9618 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9619 {
9620 struct frame *sf = SELECTED_FRAME ();
9621 message_enable_multibyte = multibyte;
9622
9623 if (FRAME_INITIAL_P (sf))
9624 {
9625 if (noninteractive_need_newline)
9626 putc ('\n', stderr);
9627 noninteractive_need_newline = 0;
9628 if (STRINGP (m))
9629 fwrite (SDATA (m), nbytes, 1, stderr);
9630 if (cursor_in_echo_area == 0)
9631 fprintf (stderr, "\n");
9632 fflush (stderr);
9633 }
9634 /* A null message buffer means that the frame hasn't really been
9635 initialized yet. Error messages get reported properly by
9636 cmd_error, so this must be just an informative message; toss it. */
9637 else if (INTERACTIVE
9638 && sf->glyphs_initialized_p
9639 && FRAME_MESSAGE_BUF (sf))
9640 {
9641 Lisp_Object mini_window;
9642 Lisp_Object frame;
9643 struct frame *f;
9644
9645 /* Get the frame containing the mini-buffer
9646 that the selected frame is using. */
9647 mini_window = FRAME_MINIBUF_WINDOW (sf);
9648 frame = WVAR (XWINDOW (mini_window), frame);
9649 f = XFRAME (frame);
9650
9651 FRAME_SAMPLE_VISIBILITY (f);
9652 if (FRAME_VISIBLE_P (sf)
9653 && !FRAME_VISIBLE_P (f))
9654 Fmake_frame_visible (frame);
9655
9656 if (STRINGP (m) && SCHARS (m) > 0)
9657 {
9658 set_message (NULL, m, nbytes, multibyte);
9659 if (minibuffer_auto_raise)
9660 Fraise_frame (frame);
9661 /* Assume we are not echoing.
9662 (If we are, echo_now will override this.) */
9663 echo_message_buffer = Qnil;
9664 }
9665 else
9666 clear_message (1, 1);
9667
9668 do_pending_window_change (0);
9669 echo_area_display (1);
9670 do_pending_window_change (0);
9671 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9672 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9673 }
9674 }
9675
9676
9677 /* Display a null-terminated echo area message M. If M is 0, clear
9678 out any existing message, and let the mini-buffer text show through.
9679
9680 The buffer M must continue to exist until after the echo area gets
9681 cleared or some other message gets displayed there. Do not pass
9682 text that is stored in a Lisp string. Do not pass text in a buffer
9683 that was alloca'd. */
9684
9685 void
9686 message1 (const char *m)
9687 {
9688 message2 (m, (m ? strlen (m) : 0), 0);
9689 }
9690
9691
9692 /* The non-logging counterpart of message1. */
9693
9694 void
9695 message1_nolog (const char *m)
9696 {
9697 message2_nolog (m, (m ? strlen (m) : 0), 0);
9698 }
9699
9700 /* Display a message M which contains a single %s
9701 which gets replaced with STRING. */
9702
9703 void
9704 message_with_string (const char *m, Lisp_Object string, int log)
9705 {
9706 CHECK_STRING (string);
9707
9708 if (noninteractive)
9709 {
9710 if (m)
9711 {
9712 if (noninteractive_need_newline)
9713 putc ('\n', stderr);
9714 noninteractive_need_newline = 0;
9715 fprintf (stderr, m, SDATA (string));
9716 if (!cursor_in_echo_area)
9717 fprintf (stderr, "\n");
9718 fflush (stderr);
9719 }
9720 }
9721 else if (INTERACTIVE)
9722 {
9723 /* The frame whose minibuffer we're going to display the message on.
9724 It may be larger than the selected frame, so we need
9725 to use its buffer, not the selected frame's buffer. */
9726 Lisp_Object mini_window;
9727 struct frame *f, *sf = SELECTED_FRAME ();
9728
9729 /* Get the frame containing the minibuffer
9730 that the selected frame is using. */
9731 mini_window = FRAME_MINIBUF_WINDOW (sf);
9732 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9733
9734 /* A null message buffer means that the frame hasn't really been
9735 initialized yet. Error messages get reported properly by
9736 cmd_error, so this must be just an informative message; toss it. */
9737 if (FRAME_MESSAGE_BUF (f))
9738 {
9739 Lisp_Object args[2], msg;
9740 struct gcpro gcpro1, gcpro2;
9741
9742 args[0] = build_string (m);
9743 args[1] = msg = string;
9744 GCPRO2 (args[0], msg);
9745 gcpro1.nvars = 2;
9746
9747 msg = Fformat (2, args);
9748
9749 if (log)
9750 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9751 else
9752 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9753
9754 UNGCPRO;
9755
9756 /* Print should start at the beginning of the message
9757 buffer next time. */
9758 message_buf_print = 0;
9759 }
9760 }
9761 }
9762
9763
9764 /* Dump an informative message to the minibuf. If M is 0, clear out
9765 any existing message, and let the mini-buffer text show through. */
9766
9767 static void
9768 vmessage (const char *m, va_list ap)
9769 {
9770 if (noninteractive)
9771 {
9772 if (m)
9773 {
9774 if (noninteractive_need_newline)
9775 putc ('\n', stderr);
9776 noninteractive_need_newline = 0;
9777 vfprintf (stderr, m, ap);
9778 if (cursor_in_echo_area == 0)
9779 fprintf (stderr, "\n");
9780 fflush (stderr);
9781 }
9782 }
9783 else if (INTERACTIVE)
9784 {
9785 /* The frame whose mini-buffer we're going to display the message
9786 on. It may be larger than the selected frame, so we need to
9787 use its buffer, not the selected frame's buffer. */
9788 Lisp_Object mini_window;
9789 struct frame *f, *sf = SELECTED_FRAME ();
9790
9791 /* Get the frame containing the mini-buffer
9792 that the selected frame is using. */
9793 mini_window = FRAME_MINIBUF_WINDOW (sf);
9794 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9795
9796 /* A null message buffer means that the frame hasn't really been
9797 initialized yet. Error messages get reported properly by
9798 cmd_error, so this must be just an informative message; toss
9799 it. */
9800 if (FRAME_MESSAGE_BUF (f))
9801 {
9802 if (m)
9803 {
9804 ptrdiff_t len;
9805
9806 len = doprnt (FRAME_MESSAGE_BUF (f),
9807 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9808
9809 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9810 }
9811 else
9812 message1 (0);
9813
9814 /* Print should start at the beginning of the message
9815 buffer next time. */
9816 message_buf_print = 0;
9817 }
9818 }
9819 }
9820
9821 void
9822 message (const char *m, ...)
9823 {
9824 va_list ap;
9825 va_start (ap, m);
9826 vmessage (m, ap);
9827 va_end (ap);
9828 }
9829
9830
9831 #if 0
9832 /* The non-logging version of message. */
9833
9834 void
9835 message_nolog (const char *m, ...)
9836 {
9837 Lisp_Object old_log_max;
9838 va_list ap;
9839 va_start (ap, m);
9840 old_log_max = Vmessage_log_max;
9841 Vmessage_log_max = Qnil;
9842 vmessage (m, ap);
9843 Vmessage_log_max = old_log_max;
9844 va_end (ap);
9845 }
9846 #endif
9847
9848
9849 /* Display the current message in the current mini-buffer. This is
9850 only called from error handlers in process.c, and is not time
9851 critical. */
9852
9853 void
9854 update_echo_area (void)
9855 {
9856 if (!NILP (echo_area_buffer[0]))
9857 {
9858 Lisp_Object string;
9859 string = Fcurrent_message ();
9860 message3 (string, SBYTES (string),
9861 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9862 }
9863 }
9864
9865
9866 /* Make sure echo area buffers in `echo_buffers' are live.
9867 If they aren't, make new ones. */
9868
9869 static void
9870 ensure_echo_area_buffers (void)
9871 {
9872 int i;
9873
9874 for (i = 0; i < 2; ++i)
9875 if (!BUFFERP (echo_buffer[i])
9876 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9877 {
9878 char name[30];
9879 Lisp_Object old_buffer;
9880 int j;
9881
9882 old_buffer = echo_buffer[i];
9883 echo_buffer[i] = Fget_buffer_create
9884 (make_formatted_string (name, " *Echo Area %d*", i));
9885 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9886 /* to force word wrap in echo area -
9887 it was decided to postpone this*/
9888 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9889
9890 for (j = 0; j < 2; ++j)
9891 if (EQ (old_buffer, echo_area_buffer[j]))
9892 echo_area_buffer[j] = echo_buffer[i];
9893 }
9894 }
9895
9896
9897 /* Call FN with args A1..A4 with either the current or last displayed
9898 echo_area_buffer as current buffer.
9899
9900 WHICH zero means use the current message buffer
9901 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9902 from echo_buffer[] and clear it.
9903
9904 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9905 suitable buffer from echo_buffer[] and clear it.
9906
9907 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9908 that the current message becomes the last displayed one, make
9909 choose a suitable buffer for echo_area_buffer[0], and clear it.
9910
9911 Value is what FN returns. */
9912
9913 static int
9914 with_echo_area_buffer (struct window *w, int which,
9915 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9916 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9917 {
9918 Lisp_Object buffer;
9919 int this_one, the_other, clear_buffer_p, rc;
9920 ptrdiff_t count = SPECPDL_INDEX ();
9921
9922 /* If buffers aren't live, make new ones. */
9923 ensure_echo_area_buffers ();
9924
9925 clear_buffer_p = 0;
9926
9927 if (which == 0)
9928 this_one = 0, the_other = 1;
9929 else if (which > 0)
9930 this_one = 1, the_other = 0;
9931 else
9932 {
9933 this_one = 0, the_other = 1;
9934 clear_buffer_p = 1;
9935
9936 /* We need a fresh one in case the current echo buffer equals
9937 the one containing the last displayed echo area message. */
9938 if (!NILP (echo_area_buffer[this_one])
9939 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9940 echo_area_buffer[this_one] = Qnil;
9941 }
9942
9943 /* Choose a suitable buffer from echo_buffer[] is we don't
9944 have one. */
9945 if (NILP (echo_area_buffer[this_one]))
9946 {
9947 echo_area_buffer[this_one]
9948 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9949 ? echo_buffer[the_other]
9950 : echo_buffer[this_one]);
9951 clear_buffer_p = 1;
9952 }
9953
9954 buffer = echo_area_buffer[this_one];
9955
9956 /* Don't get confused by reusing the buffer used for echoing
9957 for a different purpose. */
9958 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9959 cancel_echoing ();
9960
9961 record_unwind_protect (unwind_with_echo_area_buffer,
9962 with_echo_area_buffer_unwind_data (w));
9963
9964 /* Make the echo area buffer current. Note that for display
9965 purposes, it is not necessary that the displayed window's buffer
9966 == current_buffer, except for text property lookup. So, let's
9967 only set that buffer temporarily here without doing a full
9968 Fset_window_buffer. We must also change w->pointm, though,
9969 because otherwise an assertions in unshow_buffer fails, and Emacs
9970 aborts. */
9971 set_buffer_internal_1 (XBUFFER (buffer));
9972 if (w)
9973 {
9974 WVAR (w, buffer) = buffer;
9975 set_marker_both (WVAR (w, pointm), buffer, BEG, BEG_BYTE);
9976 }
9977
9978 BVAR (current_buffer, undo_list) = Qt;
9979 BVAR (current_buffer, read_only) = Qnil;
9980 specbind (Qinhibit_read_only, Qt);
9981 specbind (Qinhibit_modification_hooks, Qt);
9982
9983 if (clear_buffer_p && Z > BEG)
9984 del_range (BEG, Z);
9985
9986 eassert (BEGV >= BEG);
9987 eassert (ZV <= Z && ZV >= BEGV);
9988
9989 rc = fn (a1, a2, a3, a4);
9990
9991 eassert (BEGV >= BEG);
9992 eassert (ZV <= Z && ZV >= BEGV);
9993
9994 unbind_to (count, Qnil);
9995 return rc;
9996 }
9997
9998
9999 /* Save state that should be preserved around the call to the function
10000 FN called in with_echo_area_buffer. */
10001
10002 static Lisp_Object
10003 with_echo_area_buffer_unwind_data (struct window *w)
10004 {
10005 int i = 0;
10006 Lisp_Object vector, tmp;
10007
10008 /* Reduce consing by keeping one vector in
10009 Vwith_echo_area_save_vector. */
10010 vector = Vwith_echo_area_save_vector;
10011 Vwith_echo_area_save_vector = Qnil;
10012
10013 if (NILP (vector))
10014 vector = Fmake_vector (make_number (7), Qnil);
10015
10016 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10017 ASET (vector, i, Vdeactivate_mark); ++i;
10018 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10019
10020 if (w)
10021 {
10022 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10023 ASET (vector, i, WVAR (w, buffer)); ++i;
10024 ASET (vector, i, make_number (XMARKER (WVAR (w, pointm))->charpos)); ++i;
10025 ASET (vector, i, make_number (XMARKER (WVAR (w, pointm))->bytepos)); ++i;
10026 }
10027 else
10028 {
10029 int end = i + 4;
10030 for (; i < end; ++i)
10031 ASET (vector, i, Qnil);
10032 }
10033
10034 eassert (i == ASIZE (vector));
10035 return vector;
10036 }
10037
10038
10039 /* Restore global state from VECTOR which was created by
10040 with_echo_area_buffer_unwind_data. */
10041
10042 static Lisp_Object
10043 unwind_with_echo_area_buffer (Lisp_Object vector)
10044 {
10045 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10046 Vdeactivate_mark = AREF (vector, 1);
10047 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10048
10049 if (WINDOWP (AREF (vector, 3)))
10050 {
10051 struct window *w;
10052 Lisp_Object buffer, charpos, bytepos;
10053
10054 w = XWINDOW (AREF (vector, 3));
10055 buffer = AREF (vector, 4);
10056 charpos = AREF (vector, 5);
10057 bytepos = AREF (vector, 6);
10058
10059 WVAR (w, buffer) = buffer;
10060 set_marker_both (WVAR (w, pointm), buffer,
10061 XFASTINT (charpos), XFASTINT (bytepos));
10062 }
10063
10064 Vwith_echo_area_save_vector = vector;
10065 return Qnil;
10066 }
10067
10068
10069 /* Set up the echo area for use by print functions. MULTIBYTE_P
10070 non-zero means we will print multibyte. */
10071
10072 void
10073 setup_echo_area_for_printing (int multibyte_p)
10074 {
10075 /* If we can't find an echo area any more, exit. */
10076 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10077 Fkill_emacs (Qnil);
10078
10079 ensure_echo_area_buffers ();
10080
10081 if (!message_buf_print)
10082 {
10083 /* A message has been output since the last time we printed.
10084 Choose a fresh echo area buffer. */
10085 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10086 echo_area_buffer[0] = echo_buffer[1];
10087 else
10088 echo_area_buffer[0] = echo_buffer[0];
10089
10090 /* Switch to that buffer and clear it. */
10091 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10092 BVAR (current_buffer, truncate_lines) = Qnil;
10093
10094 if (Z > BEG)
10095 {
10096 ptrdiff_t count = SPECPDL_INDEX ();
10097 specbind (Qinhibit_read_only, Qt);
10098 /* Note that undo recording is always disabled. */
10099 del_range (BEG, Z);
10100 unbind_to (count, Qnil);
10101 }
10102 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10103
10104 /* Set up the buffer for the multibyteness we need. */
10105 if (multibyte_p
10106 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10107 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10108
10109 /* Raise the frame containing the echo area. */
10110 if (minibuffer_auto_raise)
10111 {
10112 struct frame *sf = SELECTED_FRAME ();
10113 Lisp_Object mini_window;
10114 mini_window = FRAME_MINIBUF_WINDOW (sf);
10115 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10116 }
10117
10118 message_log_maybe_newline ();
10119 message_buf_print = 1;
10120 }
10121 else
10122 {
10123 if (NILP (echo_area_buffer[0]))
10124 {
10125 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10126 echo_area_buffer[0] = echo_buffer[1];
10127 else
10128 echo_area_buffer[0] = echo_buffer[0];
10129 }
10130
10131 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10132 {
10133 /* Someone switched buffers between print requests. */
10134 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10135 BVAR (current_buffer, truncate_lines) = Qnil;
10136 }
10137 }
10138 }
10139
10140
10141 /* Display an echo area message in window W. Value is non-zero if W's
10142 height is changed. If display_last_displayed_message_p is
10143 non-zero, display the message that was last displayed, otherwise
10144 display the current message. */
10145
10146 static int
10147 display_echo_area (struct window *w)
10148 {
10149 int i, no_message_p, window_height_changed_p;
10150
10151 /* Temporarily disable garbage collections while displaying the echo
10152 area. This is done because a GC can print a message itself.
10153 That message would modify the echo area buffer's contents while a
10154 redisplay of the buffer is going on, and seriously confuse
10155 redisplay. */
10156 ptrdiff_t count = inhibit_garbage_collection ();
10157
10158 /* If there is no message, we must call display_echo_area_1
10159 nevertheless because it resizes the window. But we will have to
10160 reset the echo_area_buffer in question to nil at the end because
10161 with_echo_area_buffer will sets it to an empty buffer. */
10162 i = display_last_displayed_message_p ? 1 : 0;
10163 no_message_p = NILP (echo_area_buffer[i]);
10164
10165 window_height_changed_p
10166 = with_echo_area_buffer (w, display_last_displayed_message_p,
10167 display_echo_area_1,
10168 (intptr_t) w, Qnil, 0, 0);
10169
10170 if (no_message_p)
10171 echo_area_buffer[i] = Qnil;
10172
10173 unbind_to (count, Qnil);
10174 return window_height_changed_p;
10175 }
10176
10177
10178 /* Helper for display_echo_area. Display the current buffer which
10179 contains the current echo area message in window W, a mini-window,
10180 a pointer to which is passed in A1. A2..A4 are currently not used.
10181 Change the height of W so that all of the message is displayed.
10182 Value is non-zero if height of W was changed. */
10183
10184 static int
10185 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10186 {
10187 intptr_t i1 = a1;
10188 struct window *w = (struct window *) i1;
10189 Lisp_Object window;
10190 struct text_pos start;
10191 int window_height_changed_p = 0;
10192
10193 /* Do this before displaying, so that we have a large enough glyph
10194 matrix for the display. If we can't get enough space for the
10195 whole text, display the last N lines. That works by setting w->start. */
10196 window_height_changed_p = resize_mini_window (w, 0);
10197
10198 /* Use the starting position chosen by resize_mini_window. */
10199 SET_TEXT_POS_FROM_MARKER (start, WVAR (w, start));
10200
10201 /* Display. */
10202 clear_glyph_matrix (w->desired_matrix);
10203 XSETWINDOW (window, w);
10204 try_window (window, start, 0);
10205
10206 return window_height_changed_p;
10207 }
10208
10209
10210 /* Resize the echo area window to exactly the size needed for the
10211 currently displayed message, if there is one. If a mini-buffer
10212 is active, don't shrink it. */
10213
10214 void
10215 resize_echo_area_exactly (void)
10216 {
10217 if (BUFFERP (echo_area_buffer[0])
10218 && WINDOWP (echo_area_window))
10219 {
10220 struct window *w = XWINDOW (echo_area_window);
10221 int resized_p;
10222 Lisp_Object resize_exactly;
10223
10224 if (minibuf_level == 0)
10225 resize_exactly = Qt;
10226 else
10227 resize_exactly = Qnil;
10228
10229 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10230 (intptr_t) w, resize_exactly,
10231 0, 0);
10232 if (resized_p)
10233 {
10234 ++windows_or_buffers_changed;
10235 ++update_mode_lines;
10236 redisplay_internal ();
10237 }
10238 }
10239 }
10240
10241
10242 /* Callback function for with_echo_area_buffer, when used from
10243 resize_echo_area_exactly. A1 contains a pointer to the window to
10244 resize, EXACTLY non-nil means resize the mini-window exactly to the
10245 size of the text displayed. A3 and A4 are not used. Value is what
10246 resize_mini_window returns. */
10247
10248 static int
10249 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10250 {
10251 intptr_t i1 = a1;
10252 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10253 }
10254
10255
10256 /* Resize mini-window W to fit the size of its contents. EXACT_P
10257 means size the window exactly to the size needed. Otherwise, it's
10258 only enlarged until W's buffer is empty.
10259
10260 Set W->start to the right place to begin display. If the whole
10261 contents fit, start at the beginning. Otherwise, start so as
10262 to make the end of the contents appear. This is particularly
10263 important for y-or-n-p, but seems desirable generally.
10264
10265 Value is non-zero if the window height has been changed. */
10266
10267 int
10268 resize_mini_window (struct window *w, int exact_p)
10269 {
10270 struct frame *f = XFRAME (WVAR (w, frame));
10271 int window_height_changed_p = 0;
10272
10273 eassert (MINI_WINDOW_P (w));
10274
10275 /* By default, start display at the beginning. */
10276 set_marker_both (WVAR (w, start), WVAR (w, buffer),
10277 BUF_BEGV (XBUFFER (WVAR (w, buffer))),
10278 BUF_BEGV_BYTE (XBUFFER (WVAR (w, buffer))));
10279
10280 /* Don't resize windows while redisplaying a window; it would
10281 confuse redisplay functions when the size of the window they are
10282 displaying changes from under them. Such a resizing can happen,
10283 for instance, when which-func prints a long message while
10284 we are running fontification-functions. We're running these
10285 functions with safe_call which binds inhibit-redisplay to t. */
10286 if (!NILP (Vinhibit_redisplay))
10287 return 0;
10288
10289 /* Nil means don't try to resize. */
10290 if (NILP (Vresize_mini_windows)
10291 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10292 return 0;
10293
10294 if (!FRAME_MINIBUF_ONLY_P (f))
10295 {
10296 struct it it;
10297 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10298 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10299 int height;
10300 EMACS_INT max_height;
10301 int unit = FRAME_LINE_HEIGHT (f);
10302 struct text_pos start;
10303 struct buffer *old_current_buffer = NULL;
10304
10305 if (current_buffer != XBUFFER (WVAR (w, buffer)))
10306 {
10307 old_current_buffer = current_buffer;
10308 set_buffer_internal (XBUFFER (WVAR (w, buffer)));
10309 }
10310
10311 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10312
10313 /* Compute the max. number of lines specified by the user. */
10314 if (FLOATP (Vmax_mini_window_height))
10315 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10316 else if (INTEGERP (Vmax_mini_window_height))
10317 max_height = XINT (Vmax_mini_window_height);
10318 else
10319 max_height = total_height / 4;
10320
10321 /* Correct that max. height if it's bogus. */
10322 max_height = max (1, max_height);
10323 max_height = min (total_height, max_height);
10324
10325 /* Find out the height of the text in the window. */
10326 if (it.line_wrap == TRUNCATE)
10327 height = 1;
10328 else
10329 {
10330 last_height = 0;
10331 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10332 if (it.max_ascent == 0 && it.max_descent == 0)
10333 height = it.current_y + last_height;
10334 else
10335 height = it.current_y + it.max_ascent + it.max_descent;
10336 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10337 height = (height + unit - 1) / unit;
10338 }
10339
10340 /* Compute a suitable window start. */
10341 if (height > max_height)
10342 {
10343 height = max_height;
10344 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10345 move_it_vertically_backward (&it, (height - 1) * unit);
10346 start = it.current.pos;
10347 }
10348 else
10349 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10350 SET_MARKER_FROM_TEXT_POS (WVAR (w, start), start);
10351
10352 if (EQ (Vresize_mini_windows, Qgrow_only))
10353 {
10354 /* Let it grow only, until we display an empty message, in which
10355 case the window shrinks again. */
10356 if (height > WINDOW_TOTAL_LINES (w))
10357 {
10358 int old_height = WINDOW_TOTAL_LINES (w);
10359 freeze_window_starts (f, 1);
10360 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10361 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10362 }
10363 else if (height < WINDOW_TOTAL_LINES (w)
10364 && (exact_p || BEGV == ZV))
10365 {
10366 int old_height = WINDOW_TOTAL_LINES (w);
10367 freeze_window_starts (f, 0);
10368 shrink_mini_window (w);
10369 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10370 }
10371 }
10372 else
10373 {
10374 /* Always resize to exact size needed. */
10375 if (height > WINDOW_TOTAL_LINES (w))
10376 {
10377 int old_height = WINDOW_TOTAL_LINES (w);
10378 freeze_window_starts (f, 1);
10379 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10380 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10381 }
10382 else if (height < WINDOW_TOTAL_LINES (w))
10383 {
10384 int old_height = WINDOW_TOTAL_LINES (w);
10385 freeze_window_starts (f, 0);
10386 shrink_mini_window (w);
10387
10388 if (height)
10389 {
10390 freeze_window_starts (f, 1);
10391 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10392 }
10393
10394 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10395 }
10396 }
10397
10398 if (old_current_buffer)
10399 set_buffer_internal (old_current_buffer);
10400 }
10401
10402 return window_height_changed_p;
10403 }
10404
10405
10406 /* Value is the current message, a string, or nil if there is no
10407 current message. */
10408
10409 Lisp_Object
10410 current_message (void)
10411 {
10412 Lisp_Object msg;
10413
10414 if (!BUFFERP (echo_area_buffer[0]))
10415 msg = Qnil;
10416 else
10417 {
10418 with_echo_area_buffer (0, 0, current_message_1,
10419 (intptr_t) &msg, Qnil, 0, 0);
10420 if (NILP (msg))
10421 echo_area_buffer[0] = Qnil;
10422 }
10423
10424 return msg;
10425 }
10426
10427
10428 static int
10429 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10430 {
10431 intptr_t i1 = a1;
10432 Lisp_Object *msg = (Lisp_Object *) i1;
10433
10434 if (Z > BEG)
10435 *msg = make_buffer_string (BEG, Z, 1);
10436 else
10437 *msg = Qnil;
10438 return 0;
10439 }
10440
10441
10442 /* Push the current message on Vmessage_stack for later restoration
10443 by restore_message. Value is non-zero if the current message isn't
10444 empty. This is a relatively infrequent operation, so it's not
10445 worth optimizing. */
10446
10447 int
10448 push_message (void)
10449 {
10450 Lisp_Object msg;
10451 msg = current_message ();
10452 Vmessage_stack = Fcons (msg, Vmessage_stack);
10453 return STRINGP (msg);
10454 }
10455
10456
10457 /* Restore message display from the top of Vmessage_stack. */
10458
10459 void
10460 restore_message (void)
10461 {
10462 Lisp_Object msg;
10463
10464 eassert (CONSP (Vmessage_stack));
10465 msg = XCAR (Vmessage_stack);
10466 if (STRINGP (msg))
10467 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10468 else
10469 message3_nolog (msg, 0, 0);
10470 }
10471
10472
10473 /* Handler for record_unwind_protect calling pop_message. */
10474
10475 Lisp_Object
10476 pop_message_unwind (Lisp_Object dummy)
10477 {
10478 pop_message ();
10479 return Qnil;
10480 }
10481
10482 /* Pop the top-most entry off Vmessage_stack. */
10483
10484 static void
10485 pop_message (void)
10486 {
10487 eassert (CONSP (Vmessage_stack));
10488 Vmessage_stack = XCDR (Vmessage_stack);
10489 }
10490
10491
10492 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10493 exits. If the stack is not empty, we have a missing pop_message
10494 somewhere. */
10495
10496 void
10497 check_message_stack (void)
10498 {
10499 if (!NILP (Vmessage_stack))
10500 abort ();
10501 }
10502
10503
10504 /* Truncate to NCHARS what will be displayed in the echo area the next
10505 time we display it---but don't redisplay it now. */
10506
10507 void
10508 truncate_echo_area (ptrdiff_t nchars)
10509 {
10510 if (nchars == 0)
10511 echo_area_buffer[0] = Qnil;
10512 /* A null message buffer means that the frame hasn't really been
10513 initialized yet. Error messages get reported properly by
10514 cmd_error, so this must be just an informative message; toss it. */
10515 else if (!noninteractive
10516 && INTERACTIVE
10517 && !NILP (echo_area_buffer[0]))
10518 {
10519 struct frame *sf = SELECTED_FRAME ();
10520 if (FRAME_MESSAGE_BUF (sf))
10521 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10522 }
10523 }
10524
10525
10526 /* Helper function for truncate_echo_area. Truncate the current
10527 message to at most NCHARS characters. */
10528
10529 static int
10530 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10531 {
10532 if (BEG + nchars < Z)
10533 del_range (BEG + nchars, Z);
10534 if (Z == BEG)
10535 echo_area_buffer[0] = Qnil;
10536 return 0;
10537 }
10538
10539
10540 /* Set the current message to a substring of S or STRING.
10541
10542 If STRING is a Lisp string, set the message to the first NBYTES
10543 bytes from STRING. NBYTES zero means use the whole string. If
10544 STRING is multibyte, the message will be displayed multibyte.
10545
10546 If S is not null, set the message to the first LEN bytes of S. LEN
10547 zero means use the whole string. MULTIBYTE_P non-zero means S is
10548 multibyte. Display the message multibyte in that case.
10549
10550 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10551 to t before calling set_message_1 (which calls insert).
10552 */
10553
10554 static void
10555 set_message (const char *s, Lisp_Object string,
10556 ptrdiff_t nbytes, int multibyte_p)
10557 {
10558 message_enable_multibyte
10559 = ((s && multibyte_p)
10560 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10561
10562 with_echo_area_buffer (0, -1, set_message_1,
10563 (intptr_t) s, string, nbytes, multibyte_p);
10564 message_buf_print = 0;
10565 help_echo_showing_p = 0;
10566 }
10567
10568
10569 /* Helper function for set_message. Arguments have the same meaning
10570 as there, with A1 corresponding to S and A2 corresponding to STRING
10571 This function is called with the echo area buffer being
10572 current. */
10573
10574 static int
10575 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10576 {
10577 intptr_t i1 = a1;
10578 const char *s = (const char *) i1;
10579 const unsigned char *msg = (const unsigned char *) s;
10580 Lisp_Object string = a2;
10581
10582 /* Change multibyteness of the echo buffer appropriately. */
10583 if (message_enable_multibyte
10584 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10585 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10586
10587 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10588 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10589 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10590
10591 /* Insert new message at BEG. */
10592 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10593
10594 if (STRINGP (string))
10595 {
10596 ptrdiff_t nchars;
10597
10598 if (nbytes == 0)
10599 nbytes = SBYTES (string);
10600 nchars = string_byte_to_char (string, nbytes);
10601
10602 /* This function takes care of single/multibyte conversion. We
10603 just have to ensure that the echo area buffer has the right
10604 setting of enable_multibyte_characters. */
10605 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10606 }
10607 else if (s)
10608 {
10609 if (nbytes == 0)
10610 nbytes = strlen (s);
10611
10612 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10613 {
10614 /* Convert from multi-byte to single-byte. */
10615 ptrdiff_t i;
10616 int c, n;
10617 char work[1];
10618
10619 /* Convert a multibyte string to single-byte. */
10620 for (i = 0; i < nbytes; i += n)
10621 {
10622 c = string_char_and_length (msg + i, &n);
10623 work[0] = (ASCII_CHAR_P (c)
10624 ? c
10625 : multibyte_char_to_unibyte (c));
10626 insert_1_both (work, 1, 1, 1, 0, 0);
10627 }
10628 }
10629 else if (!multibyte_p
10630 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10631 {
10632 /* Convert from single-byte to multi-byte. */
10633 ptrdiff_t i;
10634 int c, n;
10635 unsigned char str[MAX_MULTIBYTE_LENGTH];
10636
10637 /* Convert a single-byte string to multibyte. */
10638 for (i = 0; i < nbytes; i++)
10639 {
10640 c = msg[i];
10641 MAKE_CHAR_MULTIBYTE (c);
10642 n = CHAR_STRING (c, str);
10643 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10644 }
10645 }
10646 else
10647 insert_1 (s, nbytes, 1, 0, 0);
10648 }
10649
10650 return 0;
10651 }
10652
10653
10654 /* Clear messages. CURRENT_P non-zero means clear the current
10655 message. LAST_DISPLAYED_P non-zero means clear the message
10656 last displayed. */
10657
10658 void
10659 clear_message (int current_p, int last_displayed_p)
10660 {
10661 if (current_p)
10662 {
10663 echo_area_buffer[0] = Qnil;
10664 message_cleared_p = 1;
10665 }
10666
10667 if (last_displayed_p)
10668 echo_area_buffer[1] = Qnil;
10669
10670 message_buf_print = 0;
10671 }
10672
10673 /* Clear garbaged frames.
10674
10675 This function is used where the old redisplay called
10676 redraw_garbaged_frames which in turn called redraw_frame which in
10677 turn called clear_frame. The call to clear_frame was a source of
10678 flickering. I believe a clear_frame is not necessary. It should
10679 suffice in the new redisplay to invalidate all current matrices,
10680 and ensure a complete redisplay of all windows. */
10681
10682 static void
10683 clear_garbaged_frames (void)
10684 {
10685 if (frame_garbaged)
10686 {
10687 Lisp_Object tail, frame;
10688 int changed_count = 0;
10689
10690 FOR_EACH_FRAME (tail, frame)
10691 {
10692 struct frame *f = XFRAME (frame);
10693
10694 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10695 {
10696 if (f->resized_p)
10697 {
10698 Fredraw_frame (frame);
10699 f->force_flush_display_p = 1;
10700 }
10701 clear_current_matrices (f);
10702 changed_count++;
10703 f->garbaged = 0;
10704 f->resized_p = 0;
10705 }
10706 }
10707
10708 frame_garbaged = 0;
10709 if (changed_count)
10710 ++windows_or_buffers_changed;
10711 }
10712 }
10713
10714
10715 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10716 is non-zero update selected_frame. Value is non-zero if the
10717 mini-windows height has been changed. */
10718
10719 static int
10720 echo_area_display (int update_frame_p)
10721 {
10722 Lisp_Object mini_window;
10723 struct window *w;
10724 struct frame *f;
10725 int window_height_changed_p = 0;
10726 struct frame *sf = SELECTED_FRAME ();
10727
10728 mini_window = FRAME_MINIBUF_WINDOW (sf);
10729 w = XWINDOW (mini_window);
10730 f = XFRAME (WINDOW_FRAME (w));
10731
10732 /* Don't display if frame is invisible or not yet initialized. */
10733 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10734 return 0;
10735
10736 #ifdef HAVE_WINDOW_SYSTEM
10737 /* When Emacs starts, selected_frame may be the initial terminal
10738 frame. If we let this through, a message would be displayed on
10739 the terminal. */
10740 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10741 return 0;
10742 #endif /* HAVE_WINDOW_SYSTEM */
10743
10744 /* Redraw garbaged frames. */
10745 if (frame_garbaged)
10746 clear_garbaged_frames ();
10747
10748 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10749 {
10750 echo_area_window = mini_window;
10751 window_height_changed_p = display_echo_area (w);
10752 w->must_be_updated_p = 1;
10753
10754 /* Update the display, unless called from redisplay_internal.
10755 Also don't update the screen during redisplay itself. The
10756 update will happen at the end of redisplay, and an update
10757 here could cause confusion. */
10758 if (update_frame_p && !redisplaying_p)
10759 {
10760 int n = 0;
10761
10762 /* If the display update has been interrupted by pending
10763 input, update mode lines in the frame. Due to the
10764 pending input, it might have been that redisplay hasn't
10765 been called, so that mode lines above the echo area are
10766 garbaged. This looks odd, so we prevent it here. */
10767 if (!display_completed)
10768 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10769
10770 if (window_height_changed_p
10771 /* Don't do this if Emacs is shutting down. Redisplay
10772 needs to run hooks. */
10773 && !NILP (Vrun_hooks))
10774 {
10775 /* Must update other windows. Likewise as in other
10776 cases, don't let this update be interrupted by
10777 pending input. */
10778 ptrdiff_t count = SPECPDL_INDEX ();
10779 specbind (Qredisplay_dont_pause, Qt);
10780 windows_or_buffers_changed = 1;
10781 redisplay_internal ();
10782 unbind_to (count, Qnil);
10783 }
10784 else if (FRAME_WINDOW_P (f) && n == 0)
10785 {
10786 /* Window configuration is the same as before.
10787 Can do with a display update of the echo area,
10788 unless we displayed some mode lines. */
10789 update_single_window (w, 1);
10790 FRAME_RIF (f)->flush_display (f);
10791 }
10792 else
10793 update_frame (f, 1, 1);
10794
10795 /* If cursor is in the echo area, make sure that the next
10796 redisplay displays the minibuffer, so that the cursor will
10797 be replaced with what the minibuffer wants. */
10798 if (cursor_in_echo_area)
10799 ++windows_or_buffers_changed;
10800 }
10801 }
10802 else if (!EQ (mini_window, selected_window))
10803 windows_or_buffers_changed++;
10804
10805 /* Last displayed message is now the current message. */
10806 echo_area_buffer[1] = echo_area_buffer[0];
10807 /* Inform read_char that we're not echoing. */
10808 echo_message_buffer = Qnil;
10809
10810 /* Prevent redisplay optimization in redisplay_internal by resetting
10811 this_line_start_pos. This is done because the mini-buffer now
10812 displays the message instead of its buffer text. */
10813 if (EQ (mini_window, selected_window))
10814 CHARPOS (this_line_start_pos) = 0;
10815
10816 return window_height_changed_p;
10817 }
10818
10819
10820 \f
10821 /***********************************************************************
10822 Mode Lines and Frame Titles
10823 ***********************************************************************/
10824
10825 /* A buffer for constructing non-propertized mode-line strings and
10826 frame titles in it; allocated from the heap in init_xdisp and
10827 resized as needed in store_mode_line_noprop_char. */
10828
10829 static char *mode_line_noprop_buf;
10830
10831 /* The buffer's end, and a current output position in it. */
10832
10833 static char *mode_line_noprop_buf_end;
10834 static char *mode_line_noprop_ptr;
10835
10836 #define MODE_LINE_NOPROP_LEN(start) \
10837 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10838
10839 static enum {
10840 MODE_LINE_DISPLAY = 0,
10841 MODE_LINE_TITLE,
10842 MODE_LINE_NOPROP,
10843 MODE_LINE_STRING
10844 } mode_line_target;
10845
10846 /* Alist that caches the results of :propertize.
10847 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10848 static Lisp_Object mode_line_proptrans_alist;
10849
10850 /* List of strings making up the mode-line. */
10851 static Lisp_Object mode_line_string_list;
10852
10853 /* Base face property when building propertized mode line string. */
10854 static Lisp_Object mode_line_string_face;
10855 static Lisp_Object mode_line_string_face_prop;
10856
10857
10858 /* Unwind data for mode line strings */
10859
10860 static Lisp_Object Vmode_line_unwind_vector;
10861
10862 static Lisp_Object
10863 format_mode_line_unwind_data (struct frame *target_frame,
10864 struct buffer *obuf,
10865 Lisp_Object owin,
10866 int save_proptrans)
10867 {
10868 Lisp_Object vector, tmp;
10869
10870 /* Reduce consing by keeping one vector in
10871 Vwith_echo_area_save_vector. */
10872 vector = Vmode_line_unwind_vector;
10873 Vmode_line_unwind_vector = Qnil;
10874
10875 if (NILP (vector))
10876 vector = Fmake_vector (make_number (10), Qnil);
10877
10878 ASET (vector, 0, make_number (mode_line_target));
10879 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10880 ASET (vector, 2, mode_line_string_list);
10881 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10882 ASET (vector, 4, mode_line_string_face);
10883 ASET (vector, 5, mode_line_string_face_prop);
10884
10885 if (obuf)
10886 XSETBUFFER (tmp, obuf);
10887 else
10888 tmp = Qnil;
10889 ASET (vector, 6, tmp);
10890 ASET (vector, 7, owin);
10891 if (target_frame)
10892 {
10893 /* Similarly to `with-selected-window', if the operation selects
10894 a window on another frame, we must restore that frame's
10895 selected window, and (for a tty) the top-frame. */
10896 ASET (vector, 8, FVAR (target_frame, selected_window));
10897 if (FRAME_TERMCAP_P (target_frame))
10898 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10899 }
10900
10901 return vector;
10902 }
10903
10904 static Lisp_Object
10905 unwind_format_mode_line (Lisp_Object vector)
10906 {
10907 Lisp_Object old_window = AREF (vector, 7);
10908 Lisp_Object target_frame_window = AREF (vector, 8);
10909 Lisp_Object old_top_frame = AREF (vector, 9);
10910
10911 mode_line_target = XINT (AREF (vector, 0));
10912 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10913 mode_line_string_list = AREF (vector, 2);
10914 if (! EQ (AREF (vector, 3), Qt))
10915 mode_line_proptrans_alist = AREF (vector, 3);
10916 mode_line_string_face = AREF (vector, 4);
10917 mode_line_string_face_prop = AREF (vector, 5);
10918
10919 /* Select window before buffer, since it may change the buffer. */
10920 if (!NILP (old_window))
10921 {
10922 /* If the operation that we are unwinding had selected a window
10923 on a different frame, reset its frame-selected-window. For a
10924 text terminal, reset its top-frame if necessary. */
10925 if (!NILP (target_frame_window))
10926 {
10927 Lisp_Object frame
10928 = WINDOW_FRAME (XWINDOW (target_frame_window));
10929
10930 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10931 Fselect_window (target_frame_window, Qt);
10932
10933 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10934 Fselect_frame (old_top_frame, Qt);
10935 }
10936
10937 Fselect_window (old_window, Qt);
10938 }
10939
10940 if (!NILP (AREF (vector, 6)))
10941 {
10942 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10943 ASET (vector, 6, Qnil);
10944 }
10945
10946 Vmode_line_unwind_vector = vector;
10947 return Qnil;
10948 }
10949
10950
10951 /* Store a single character C for the frame title in mode_line_noprop_buf.
10952 Re-allocate mode_line_noprop_buf if necessary. */
10953
10954 static void
10955 store_mode_line_noprop_char (char c)
10956 {
10957 /* If output position has reached the end of the allocated buffer,
10958 increase the buffer's size. */
10959 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10960 {
10961 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10962 ptrdiff_t size = len;
10963 mode_line_noprop_buf =
10964 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10965 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10966 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10967 }
10968
10969 *mode_line_noprop_ptr++ = c;
10970 }
10971
10972
10973 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10974 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10975 characters that yield more columns than PRECISION; PRECISION <= 0
10976 means copy the whole string. Pad with spaces until FIELD_WIDTH
10977 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10978 pad. Called from display_mode_element when it is used to build a
10979 frame title. */
10980
10981 static int
10982 store_mode_line_noprop (const char *string, int field_width, int precision)
10983 {
10984 const unsigned char *str = (const unsigned char *) string;
10985 int n = 0;
10986 ptrdiff_t dummy, nbytes;
10987
10988 /* Copy at most PRECISION chars from STR. */
10989 nbytes = strlen (string);
10990 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10991 while (nbytes--)
10992 store_mode_line_noprop_char (*str++);
10993
10994 /* Fill up with spaces until FIELD_WIDTH reached. */
10995 while (field_width > 0
10996 && n < field_width)
10997 {
10998 store_mode_line_noprop_char (' ');
10999 ++n;
11000 }
11001
11002 return n;
11003 }
11004
11005 /***********************************************************************
11006 Frame Titles
11007 ***********************************************************************/
11008
11009 #ifdef HAVE_WINDOW_SYSTEM
11010
11011 /* Set the title of FRAME, if it has changed. The title format is
11012 Vicon_title_format if FRAME is iconified, otherwise it is
11013 frame_title_format. */
11014
11015 static void
11016 x_consider_frame_title (Lisp_Object frame)
11017 {
11018 struct frame *f = XFRAME (frame);
11019
11020 if (FRAME_WINDOW_P (f)
11021 || FRAME_MINIBUF_ONLY_P (f)
11022 || f->explicit_name)
11023 {
11024 /* Do we have more than one visible frame on this X display? */
11025 Lisp_Object tail;
11026 Lisp_Object fmt;
11027 ptrdiff_t title_start;
11028 char *title;
11029 ptrdiff_t len;
11030 struct it it;
11031 ptrdiff_t count = SPECPDL_INDEX ();
11032
11033 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11034 {
11035 Lisp_Object other_frame = XCAR (tail);
11036 struct frame *tf = XFRAME (other_frame);
11037
11038 if (tf != f
11039 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11040 && !FRAME_MINIBUF_ONLY_P (tf)
11041 && !EQ (other_frame, tip_frame)
11042 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11043 break;
11044 }
11045
11046 /* Set global variable indicating that multiple frames exist. */
11047 multiple_frames = CONSP (tail);
11048
11049 /* Switch to the buffer of selected window of the frame. Set up
11050 mode_line_target so that display_mode_element will output into
11051 mode_line_noprop_buf; then display the title. */
11052 record_unwind_protect (unwind_format_mode_line,
11053 format_mode_line_unwind_data
11054 (f, current_buffer, selected_window, 0));
11055
11056 Fselect_window (FVAR (f, selected_window), Qt);
11057 set_buffer_internal_1
11058 (XBUFFER (WVAR (XWINDOW (FVAR (f, selected_window)), buffer)));
11059 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11060
11061 mode_line_target = MODE_LINE_TITLE;
11062 title_start = MODE_LINE_NOPROP_LEN (0);
11063 init_iterator (&it, XWINDOW (FVAR (f, selected_window)), -1, -1,
11064 NULL, DEFAULT_FACE_ID);
11065 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11066 len = MODE_LINE_NOPROP_LEN (title_start);
11067 title = mode_line_noprop_buf + title_start;
11068 unbind_to (count, Qnil);
11069
11070 /* Set the title only if it's changed. This avoids consing in
11071 the common case where it hasn't. (If it turns out that we've
11072 already wasted too much time by walking through the list with
11073 display_mode_element, then we might need to optimize at a
11074 higher level than this.) */
11075 if (! STRINGP (FVAR (f, name))
11076 || SBYTES (FVAR (f, name)) != len
11077 || memcmp (title, SDATA (FVAR (f, name)), len) != 0)
11078 x_implicitly_set_name (f, make_string (title, len), Qnil);
11079 }
11080 }
11081
11082 #endif /* not HAVE_WINDOW_SYSTEM */
11083
11084 \f
11085 /***********************************************************************
11086 Menu Bars
11087 ***********************************************************************/
11088
11089
11090 /* Prepare for redisplay by updating menu-bar item lists when
11091 appropriate. This can call eval. */
11092
11093 void
11094 prepare_menu_bars (void)
11095 {
11096 int all_windows;
11097 struct gcpro gcpro1, gcpro2;
11098 struct frame *f;
11099 Lisp_Object tooltip_frame;
11100
11101 #ifdef HAVE_WINDOW_SYSTEM
11102 tooltip_frame = tip_frame;
11103 #else
11104 tooltip_frame = Qnil;
11105 #endif
11106
11107 /* Update all frame titles based on their buffer names, etc. We do
11108 this before the menu bars so that the buffer-menu will show the
11109 up-to-date frame titles. */
11110 #ifdef HAVE_WINDOW_SYSTEM
11111 if (windows_or_buffers_changed || update_mode_lines)
11112 {
11113 Lisp_Object tail, frame;
11114
11115 FOR_EACH_FRAME (tail, frame)
11116 {
11117 f = XFRAME (frame);
11118 if (!EQ (frame, tooltip_frame)
11119 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11120 x_consider_frame_title (frame);
11121 }
11122 }
11123 #endif /* HAVE_WINDOW_SYSTEM */
11124
11125 /* Update the menu bar item lists, if appropriate. This has to be
11126 done before any actual redisplay or generation of display lines. */
11127 all_windows = (update_mode_lines
11128 || buffer_shared > 1
11129 || windows_or_buffers_changed);
11130 if (all_windows)
11131 {
11132 Lisp_Object tail, frame;
11133 ptrdiff_t count = SPECPDL_INDEX ();
11134 /* 1 means that update_menu_bar has run its hooks
11135 so any further calls to update_menu_bar shouldn't do so again. */
11136 int menu_bar_hooks_run = 0;
11137
11138 record_unwind_save_match_data ();
11139
11140 FOR_EACH_FRAME (tail, frame)
11141 {
11142 f = XFRAME (frame);
11143
11144 /* Ignore tooltip frame. */
11145 if (EQ (frame, tooltip_frame))
11146 continue;
11147
11148 /* If a window on this frame changed size, report that to
11149 the user and clear the size-change flag. */
11150 if (FRAME_WINDOW_SIZES_CHANGED (f))
11151 {
11152 Lisp_Object functions;
11153
11154 /* Clear flag first in case we get an error below. */
11155 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11156 functions = Vwindow_size_change_functions;
11157 GCPRO2 (tail, functions);
11158
11159 while (CONSP (functions))
11160 {
11161 if (!EQ (XCAR (functions), Qt))
11162 call1 (XCAR (functions), frame);
11163 functions = XCDR (functions);
11164 }
11165 UNGCPRO;
11166 }
11167
11168 GCPRO1 (tail);
11169 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11170 #ifdef HAVE_WINDOW_SYSTEM
11171 update_tool_bar (f, 0);
11172 #endif
11173 #ifdef HAVE_NS
11174 if (windows_or_buffers_changed
11175 && FRAME_NS_P (f))
11176 ns_set_doc_edited
11177 (f, Fbuffer_modified_p
11178 (WVAR (XWINDOW (FVAR (f, selected_window)), buffer)));
11179 #endif
11180 UNGCPRO;
11181 }
11182
11183 unbind_to (count, Qnil);
11184 }
11185 else
11186 {
11187 struct frame *sf = SELECTED_FRAME ();
11188 update_menu_bar (sf, 1, 0);
11189 #ifdef HAVE_WINDOW_SYSTEM
11190 update_tool_bar (sf, 1);
11191 #endif
11192 }
11193 }
11194
11195
11196 /* Update the menu bar item list for frame F. This has to be done
11197 before we start to fill in any display lines, because it can call
11198 eval.
11199
11200 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11201
11202 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11203 already ran the menu bar hooks for this redisplay, so there
11204 is no need to run them again. The return value is the
11205 updated value of this flag, to pass to the next call. */
11206
11207 static int
11208 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11209 {
11210 Lisp_Object window;
11211 register struct window *w;
11212
11213 /* If called recursively during a menu update, do nothing. This can
11214 happen when, for instance, an activate-menubar-hook causes a
11215 redisplay. */
11216 if (inhibit_menubar_update)
11217 return hooks_run;
11218
11219 window = FRAME_SELECTED_WINDOW (f);
11220 w = XWINDOW (window);
11221
11222 if (FRAME_WINDOW_P (f)
11223 ?
11224 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11225 || defined (HAVE_NS) || defined (USE_GTK)
11226 FRAME_EXTERNAL_MENU_BAR (f)
11227 #else
11228 FRAME_MENU_BAR_LINES (f) > 0
11229 #endif
11230 : FRAME_MENU_BAR_LINES (f) > 0)
11231 {
11232 /* If the user has switched buffers or windows, we need to
11233 recompute to reflect the new bindings. But we'll
11234 recompute when update_mode_lines is set too; that means
11235 that people can use force-mode-line-update to request
11236 that the menu bar be recomputed. The adverse effect on
11237 the rest of the redisplay algorithm is about the same as
11238 windows_or_buffers_changed anyway. */
11239 if (windows_or_buffers_changed
11240 /* This used to test w->update_mode_line, but we believe
11241 there is no need to recompute the menu in that case. */
11242 || update_mode_lines
11243 || ((BUF_SAVE_MODIFF (XBUFFER (WVAR (w, buffer)))
11244 < BUF_MODIFF (XBUFFER (WVAR (w, buffer))))
11245 != w->last_had_star)
11246 || ((!NILP (Vtransient_mark_mode)
11247 && !NILP (BVAR (XBUFFER (WVAR (w, buffer)), mark_active)))
11248 != !NILP (WVAR (w, region_showing))))
11249 {
11250 struct buffer *prev = current_buffer;
11251 ptrdiff_t count = SPECPDL_INDEX ();
11252
11253 specbind (Qinhibit_menubar_update, Qt);
11254
11255 set_buffer_internal_1 (XBUFFER (WVAR (w, buffer)));
11256 if (save_match_data)
11257 record_unwind_save_match_data ();
11258 if (NILP (Voverriding_local_map_menu_flag))
11259 {
11260 specbind (Qoverriding_terminal_local_map, Qnil);
11261 specbind (Qoverriding_local_map, Qnil);
11262 }
11263
11264 if (!hooks_run)
11265 {
11266 /* Run the Lucid hook. */
11267 safe_run_hooks (Qactivate_menubar_hook);
11268
11269 /* If it has changed current-menubar from previous value,
11270 really recompute the menu-bar from the value. */
11271 if (! NILP (Vlucid_menu_bar_dirty_flag))
11272 call0 (Qrecompute_lucid_menubar);
11273
11274 safe_run_hooks (Qmenu_bar_update_hook);
11275
11276 hooks_run = 1;
11277 }
11278
11279 XSETFRAME (Vmenu_updating_frame, f);
11280 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11281
11282 /* Redisplay the menu bar in case we changed it. */
11283 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11284 || defined (HAVE_NS) || defined (USE_GTK)
11285 if (FRAME_WINDOW_P (f))
11286 {
11287 #if defined (HAVE_NS)
11288 /* All frames on Mac OS share the same menubar. So only
11289 the selected frame should be allowed to set it. */
11290 if (f == SELECTED_FRAME ())
11291 #endif
11292 set_frame_menubar (f, 0, 0);
11293 }
11294 else
11295 /* On a terminal screen, the menu bar is an ordinary screen
11296 line, and this makes it get updated. */
11297 w->update_mode_line = 1;
11298 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11299 /* In the non-toolkit version, the menu bar is an ordinary screen
11300 line, and this makes it get updated. */
11301 w->update_mode_line = 1;
11302 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11303
11304 unbind_to (count, Qnil);
11305 set_buffer_internal_1 (prev);
11306 }
11307 }
11308
11309 return hooks_run;
11310 }
11311
11312
11313 \f
11314 /***********************************************************************
11315 Output Cursor
11316 ***********************************************************************/
11317
11318 #ifdef HAVE_WINDOW_SYSTEM
11319
11320 /* EXPORT:
11321 Nominal cursor position -- where to draw output.
11322 HPOS and VPOS are window relative glyph matrix coordinates.
11323 X and Y are window relative pixel coordinates. */
11324
11325 struct cursor_pos output_cursor;
11326
11327
11328 /* EXPORT:
11329 Set the global variable output_cursor to CURSOR. All cursor
11330 positions are relative to updated_window. */
11331
11332 void
11333 set_output_cursor (struct cursor_pos *cursor)
11334 {
11335 output_cursor.hpos = cursor->hpos;
11336 output_cursor.vpos = cursor->vpos;
11337 output_cursor.x = cursor->x;
11338 output_cursor.y = cursor->y;
11339 }
11340
11341
11342 /* EXPORT for RIF:
11343 Set a nominal cursor position.
11344
11345 HPOS and VPOS are column/row positions in a window glyph matrix. X
11346 and Y are window text area relative pixel positions.
11347
11348 If this is done during an update, updated_window will contain the
11349 window that is being updated and the position is the future output
11350 cursor position for that window. If updated_window is null, use
11351 selected_window and display the cursor at the given position. */
11352
11353 void
11354 x_cursor_to (int vpos, int hpos, int y, int x)
11355 {
11356 struct window *w;
11357
11358 /* If updated_window is not set, work on selected_window. */
11359 if (updated_window)
11360 w = updated_window;
11361 else
11362 w = XWINDOW (selected_window);
11363
11364 /* Set the output cursor. */
11365 output_cursor.hpos = hpos;
11366 output_cursor.vpos = vpos;
11367 output_cursor.x = x;
11368 output_cursor.y = y;
11369
11370 /* If not called as part of an update, really display the cursor.
11371 This will also set the cursor position of W. */
11372 if (updated_window == NULL)
11373 {
11374 BLOCK_INPUT;
11375 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11376 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11377 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11378 UNBLOCK_INPUT;
11379 }
11380 }
11381
11382 #endif /* HAVE_WINDOW_SYSTEM */
11383
11384 \f
11385 /***********************************************************************
11386 Tool-bars
11387 ***********************************************************************/
11388
11389 #ifdef HAVE_WINDOW_SYSTEM
11390
11391 /* Where the mouse was last time we reported a mouse event. */
11392
11393 FRAME_PTR last_mouse_frame;
11394
11395 /* Tool-bar item index of the item on which a mouse button was pressed
11396 or -1. */
11397
11398 int last_tool_bar_item;
11399
11400
11401 static Lisp_Object
11402 update_tool_bar_unwind (Lisp_Object frame)
11403 {
11404 selected_frame = frame;
11405 return Qnil;
11406 }
11407
11408 /* Update the tool-bar item list for frame F. This has to be done
11409 before we start to fill in any display lines. Called from
11410 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11411 and restore it here. */
11412
11413 static void
11414 update_tool_bar (struct frame *f, int save_match_data)
11415 {
11416 #if defined (USE_GTK) || defined (HAVE_NS)
11417 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11418 #else
11419 int do_update = WINDOWP (FVAR (f, tool_bar_window))
11420 && WINDOW_TOTAL_LINES (XWINDOW (FVAR (f, tool_bar_window))) > 0;
11421 #endif
11422
11423 if (do_update)
11424 {
11425 Lisp_Object window;
11426 struct window *w;
11427
11428 window = FRAME_SELECTED_WINDOW (f);
11429 w = XWINDOW (window);
11430
11431 /* If the user has switched buffers or windows, we need to
11432 recompute to reflect the new bindings. But we'll
11433 recompute when update_mode_lines is set too; that means
11434 that people can use force-mode-line-update to request
11435 that the menu bar be recomputed. The adverse effect on
11436 the rest of the redisplay algorithm is about the same as
11437 windows_or_buffers_changed anyway. */
11438 if (windows_or_buffers_changed
11439 || w->update_mode_line
11440 || update_mode_lines
11441 || ((BUF_SAVE_MODIFF (XBUFFER (WVAR (w, buffer)))
11442 < BUF_MODIFF (XBUFFER (WVAR (w, buffer))))
11443 != w->last_had_star)
11444 || ((!NILP (Vtransient_mark_mode)
11445 && !NILP (BVAR (XBUFFER (WVAR (w, buffer)), mark_active)))
11446 != !NILP (WVAR (w, region_showing))))
11447 {
11448 struct buffer *prev = current_buffer;
11449 ptrdiff_t count = SPECPDL_INDEX ();
11450 Lisp_Object frame, new_tool_bar;
11451 int new_n_tool_bar;
11452 struct gcpro gcpro1;
11453
11454 /* Set current_buffer to the buffer of the selected
11455 window of the frame, so that we get the right local
11456 keymaps. */
11457 set_buffer_internal_1 (XBUFFER (WVAR (w, buffer)));
11458
11459 /* Save match data, if we must. */
11460 if (save_match_data)
11461 record_unwind_save_match_data ();
11462
11463 /* Make sure that we don't accidentally use bogus keymaps. */
11464 if (NILP (Voverriding_local_map_menu_flag))
11465 {
11466 specbind (Qoverriding_terminal_local_map, Qnil);
11467 specbind (Qoverriding_local_map, Qnil);
11468 }
11469
11470 GCPRO1 (new_tool_bar);
11471
11472 /* We must temporarily set the selected frame to this frame
11473 before calling tool_bar_items, because the calculation of
11474 the tool-bar keymap uses the selected frame (see
11475 `tool-bar-make-keymap' in tool-bar.el). */
11476 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11477 XSETFRAME (frame, f);
11478 selected_frame = frame;
11479
11480 /* Build desired tool-bar items from keymaps. */
11481 new_tool_bar = tool_bar_items
11482 (Fcopy_sequence (FVAR (f, tool_bar_items)), &new_n_tool_bar);
11483
11484 /* Redisplay the tool-bar if we changed it. */
11485 if (new_n_tool_bar != f->n_tool_bar_items
11486 || NILP (Fequal (new_tool_bar, FVAR (f, tool_bar_items))))
11487 {
11488 /* Redisplay that happens asynchronously due to an expose event
11489 may access f->tool_bar_items. Make sure we update both
11490 variables within BLOCK_INPUT so no such event interrupts. */
11491 BLOCK_INPUT;
11492 FVAR (f, tool_bar_items) = new_tool_bar;
11493 f->n_tool_bar_items = new_n_tool_bar;
11494 w->update_mode_line = 1;
11495 UNBLOCK_INPUT;
11496 }
11497
11498 UNGCPRO;
11499
11500 unbind_to (count, Qnil);
11501 set_buffer_internal_1 (prev);
11502 }
11503 }
11504 }
11505
11506
11507 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11508 F's desired tool-bar contents. F->tool_bar_items must have
11509 been set up previously by calling prepare_menu_bars. */
11510
11511 static void
11512 build_desired_tool_bar_string (struct frame *f)
11513 {
11514 int i, size, size_needed;
11515 struct gcpro gcpro1, gcpro2, gcpro3;
11516 Lisp_Object image, plist, props;
11517
11518 image = plist = props = Qnil;
11519 GCPRO3 (image, plist, props);
11520
11521 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11522 Otherwise, make a new string. */
11523
11524 /* The size of the string we might be able to reuse. */
11525 size = (STRINGP (FVAR (f, desired_tool_bar_string))
11526 ? SCHARS (FVAR (f, desired_tool_bar_string))
11527 : 0);
11528
11529 /* We need one space in the string for each image. */
11530 size_needed = f->n_tool_bar_items;
11531
11532 /* Reuse f->desired_tool_bar_string, if possible. */
11533 if (size < size_needed || NILP (FVAR (f, desired_tool_bar_string)))
11534 FVAR (f, desired_tool_bar_string)
11535 = Fmake_string (make_number (size_needed), make_number (' '));
11536 else
11537 {
11538 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11539 Fremove_text_properties (make_number (0), make_number (size),
11540 props, FVAR (f, desired_tool_bar_string));
11541 }
11542
11543 /* Put a `display' property on the string for the images to display,
11544 put a `menu_item' property on tool-bar items with a value that
11545 is the index of the item in F's tool-bar item vector. */
11546 for (i = 0; i < f->n_tool_bar_items; ++i)
11547 {
11548 #define PROP(IDX) \
11549 AREF (FVAR (f, tool_bar_items), i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11550
11551 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11552 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11553 int hmargin, vmargin, relief, idx, end;
11554
11555 /* If image is a vector, choose the image according to the
11556 button state. */
11557 image = PROP (TOOL_BAR_ITEM_IMAGES);
11558 if (VECTORP (image))
11559 {
11560 if (enabled_p)
11561 idx = (selected_p
11562 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11563 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11564 else
11565 idx = (selected_p
11566 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11567 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11568
11569 eassert (ASIZE (image) >= idx);
11570 image = AREF (image, idx);
11571 }
11572 else
11573 idx = -1;
11574
11575 /* Ignore invalid image specifications. */
11576 if (!valid_image_p (image))
11577 continue;
11578
11579 /* Display the tool-bar button pressed, or depressed. */
11580 plist = Fcopy_sequence (XCDR (image));
11581
11582 /* Compute margin and relief to draw. */
11583 relief = (tool_bar_button_relief >= 0
11584 ? tool_bar_button_relief
11585 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11586 hmargin = vmargin = relief;
11587
11588 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11589 INT_MAX - max (hmargin, vmargin)))
11590 {
11591 hmargin += XFASTINT (Vtool_bar_button_margin);
11592 vmargin += XFASTINT (Vtool_bar_button_margin);
11593 }
11594 else if (CONSP (Vtool_bar_button_margin))
11595 {
11596 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11597 INT_MAX - hmargin))
11598 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11599
11600 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11601 INT_MAX - vmargin))
11602 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11603 }
11604
11605 if (auto_raise_tool_bar_buttons_p)
11606 {
11607 /* Add a `:relief' property to the image spec if the item is
11608 selected. */
11609 if (selected_p)
11610 {
11611 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11612 hmargin -= relief;
11613 vmargin -= relief;
11614 }
11615 }
11616 else
11617 {
11618 /* If image is selected, display it pressed, i.e. with a
11619 negative relief. If it's not selected, display it with a
11620 raised relief. */
11621 plist = Fplist_put (plist, QCrelief,
11622 (selected_p
11623 ? make_number (-relief)
11624 : make_number (relief)));
11625 hmargin -= relief;
11626 vmargin -= relief;
11627 }
11628
11629 /* Put a margin around the image. */
11630 if (hmargin || vmargin)
11631 {
11632 if (hmargin == vmargin)
11633 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11634 else
11635 plist = Fplist_put (plist, QCmargin,
11636 Fcons (make_number (hmargin),
11637 make_number (vmargin)));
11638 }
11639
11640 /* If button is not enabled, and we don't have special images
11641 for the disabled state, make the image appear disabled by
11642 applying an appropriate algorithm to it. */
11643 if (!enabled_p && idx < 0)
11644 plist = Fplist_put (plist, QCconversion, Qdisabled);
11645
11646 /* Put a `display' text property on the string for the image to
11647 display. Put a `menu-item' property on the string that gives
11648 the start of this item's properties in the tool-bar items
11649 vector. */
11650 image = Fcons (Qimage, plist);
11651 props = list4 (Qdisplay, image,
11652 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11653
11654 /* Let the last image hide all remaining spaces in the tool bar
11655 string. The string can be longer than needed when we reuse a
11656 previous string. */
11657 if (i + 1 == f->n_tool_bar_items)
11658 end = SCHARS (FVAR (f, desired_tool_bar_string));
11659 else
11660 end = i + 1;
11661 Fadd_text_properties (make_number (i), make_number (end),
11662 props, FVAR (f, desired_tool_bar_string));
11663 #undef PROP
11664 }
11665
11666 UNGCPRO;
11667 }
11668
11669
11670 /* Display one line of the tool-bar of frame IT->f.
11671
11672 HEIGHT specifies the desired height of the tool-bar line.
11673 If the actual height of the glyph row is less than HEIGHT, the
11674 row's height is increased to HEIGHT, and the icons are centered
11675 vertically in the new height.
11676
11677 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11678 count a final empty row in case the tool-bar width exactly matches
11679 the window width.
11680 */
11681
11682 static void
11683 display_tool_bar_line (struct it *it, int height)
11684 {
11685 struct glyph_row *row = it->glyph_row;
11686 int max_x = it->last_visible_x;
11687 struct glyph *last;
11688
11689 prepare_desired_row (row);
11690 row->y = it->current_y;
11691
11692 /* Note that this isn't made use of if the face hasn't a box,
11693 so there's no need to check the face here. */
11694 it->start_of_box_run_p = 1;
11695
11696 while (it->current_x < max_x)
11697 {
11698 int x, n_glyphs_before, i, nglyphs;
11699 struct it it_before;
11700
11701 /* Get the next display element. */
11702 if (!get_next_display_element (it))
11703 {
11704 /* Don't count empty row if we are counting needed tool-bar lines. */
11705 if (height < 0 && !it->hpos)
11706 return;
11707 break;
11708 }
11709
11710 /* Produce glyphs. */
11711 n_glyphs_before = row->used[TEXT_AREA];
11712 it_before = *it;
11713
11714 PRODUCE_GLYPHS (it);
11715
11716 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11717 i = 0;
11718 x = it_before.current_x;
11719 while (i < nglyphs)
11720 {
11721 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11722
11723 if (x + glyph->pixel_width > max_x)
11724 {
11725 /* Glyph doesn't fit on line. Backtrack. */
11726 row->used[TEXT_AREA] = n_glyphs_before;
11727 *it = it_before;
11728 /* If this is the only glyph on this line, it will never fit on the
11729 tool-bar, so skip it. But ensure there is at least one glyph,
11730 so we don't accidentally disable the tool-bar. */
11731 if (n_glyphs_before == 0
11732 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11733 break;
11734 goto out;
11735 }
11736
11737 ++it->hpos;
11738 x += glyph->pixel_width;
11739 ++i;
11740 }
11741
11742 /* Stop at line end. */
11743 if (ITERATOR_AT_END_OF_LINE_P (it))
11744 break;
11745
11746 set_iterator_to_next (it, 1);
11747 }
11748
11749 out:;
11750
11751 row->displays_text_p = row->used[TEXT_AREA] != 0;
11752
11753 /* Use default face for the border below the tool bar.
11754
11755 FIXME: When auto-resize-tool-bars is grow-only, there is
11756 no additional border below the possibly empty tool-bar lines.
11757 So to make the extra empty lines look "normal", we have to
11758 use the tool-bar face for the border too. */
11759 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11760 it->face_id = DEFAULT_FACE_ID;
11761
11762 extend_face_to_end_of_line (it);
11763 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11764 last->right_box_line_p = 1;
11765 if (last == row->glyphs[TEXT_AREA])
11766 last->left_box_line_p = 1;
11767
11768 /* Make line the desired height and center it vertically. */
11769 if ((height -= it->max_ascent + it->max_descent) > 0)
11770 {
11771 /* Don't add more than one line height. */
11772 height %= FRAME_LINE_HEIGHT (it->f);
11773 it->max_ascent += height / 2;
11774 it->max_descent += (height + 1) / 2;
11775 }
11776
11777 compute_line_metrics (it);
11778
11779 /* If line is empty, make it occupy the rest of the tool-bar. */
11780 if (!row->displays_text_p)
11781 {
11782 row->height = row->phys_height = it->last_visible_y - row->y;
11783 row->visible_height = row->height;
11784 row->ascent = row->phys_ascent = 0;
11785 row->extra_line_spacing = 0;
11786 }
11787
11788 row->full_width_p = 1;
11789 row->continued_p = 0;
11790 row->truncated_on_left_p = 0;
11791 row->truncated_on_right_p = 0;
11792
11793 it->current_x = it->hpos = 0;
11794 it->current_y += row->height;
11795 ++it->vpos;
11796 ++it->glyph_row;
11797 }
11798
11799
11800 /* Max tool-bar height. */
11801
11802 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11803 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11804
11805 /* Value is the number of screen lines needed to make all tool-bar
11806 items of frame F visible. The number of actual rows needed is
11807 returned in *N_ROWS if non-NULL. */
11808
11809 static int
11810 tool_bar_lines_needed (struct frame *f, int *n_rows)
11811 {
11812 struct window *w = XWINDOW (FVAR (f, tool_bar_window));
11813 struct it it;
11814 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11815 the desired matrix, so use (unused) mode-line row as temporary row to
11816 avoid destroying the first tool-bar row. */
11817 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11818
11819 /* Initialize an iterator for iteration over
11820 F->desired_tool_bar_string in the tool-bar window of frame F. */
11821 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11822 it.first_visible_x = 0;
11823 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11824 reseat_to_string (&it, NULL, FVAR (f, desired_tool_bar_string), 0, 0, 0, -1);
11825 it.paragraph_embedding = L2R;
11826
11827 while (!ITERATOR_AT_END_P (&it))
11828 {
11829 clear_glyph_row (temp_row);
11830 it.glyph_row = temp_row;
11831 display_tool_bar_line (&it, -1);
11832 }
11833 clear_glyph_row (temp_row);
11834
11835 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11836 if (n_rows)
11837 *n_rows = it.vpos > 0 ? it.vpos : -1;
11838
11839 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11840 }
11841
11842
11843 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11844 0, 1, 0,
11845 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11846 (Lisp_Object frame)
11847 {
11848 struct frame *f;
11849 struct window *w;
11850 int nlines = 0;
11851
11852 if (NILP (frame))
11853 frame = selected_frame;
11854 else
11855 CHECK_FRAME (frame);
11856 f = XFRAME (frame);
11857
11858 if (WINDOWP (FVAR (f, tool_bar_window))
11859 && (w = XWINDOW (FVAR (f, tool_bar_window)),
11860 WINDOW_TOTAL_LINES (w) > 0))
11861 {
11862 update_tool_bar (f, 1);
11863 if (f->n_tool_bar_items)
11864 {
11865 build_desired_tool_bar_string (f);
11866 nlines = tool_bar_lines_needed (f, NULL);
11867 }
11868 }
11869
11870 return make_number (nlines);
11871 }
11872
11873
11874 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11875 height should be changed. */
11876
11877 static int
11878 redisplay_tool_bar (struct frame *f)
11879 {
11880 struct window *w;
11881 struct it it;
11882 struct glyph_row *row;
11883
11884 #if defined (USE_GTK) || defined (HAVE_NS)
11885 if (FRAME_EXTERNAL_TOOL_BAR (f))
11886 update_frame_tool_bar (f);
11887 return 0;
11888 #endif
11889
11890 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11891 do anything. This means you must start with tool-bar-lines
11892 non-zero to get the auto-sizing effect. Or in other words, you
11893 can turn off tool-bars by specifying tool-bar-lines zero. */
11894 if (!WINDOWP (FVAR (f, tool_bar_window))
11895 || (w = XWINDOW (FVAR (f, tool_bar_window)),
11896 WINDOW_TOTAL_LINES (w) == 0))
11897 return 0;
11898
11899 /* Set up an iterator for the tool-bar window. */
11900 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11901 it.first_visible_x = 0;
11902 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11903 row = it.glyph_row;
11904
11905 /* Build a string that represents the contents of the tool-bar. */
11906 build_desired_tool_bar_string (f);
11907 reseat_to_string (&it, NULL, FVAR (f, desired_tool_bar_string), 0, 0, 0, -1);
11908 /* FIXME: This should be controlled by a user option. But it
11909 doesn't make sense to have an R2L tool bar if the menu bar cannot
11910 be drawn also R2L, and making the menu bar R2L is tricky due
11911 toolkit-specific code that implements it. If an R2L tool bar is
11912 ever supported, display_tool_bar_line should also be augmented to
11913 call unproduce_glyphs like display_line and display_string
11914 do. */
11915 it.paragraph_embedding = L2R;
11916
11917 if (f->n_tool_bar_rows == 0)
11918 {
11919 int nlines;
11920
11921 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11922 nlines != WINDOW_TOTAL_LINES (w)))
11923 {
11924 Lisp_Object frame;
11925 int old_height = WINDOW_TOTAL_LINES (w);
11926
11927 XSETFRAME (frame, f);
11928 Fmodify_frame_parameters (frame,
11929 Fcons (Fcons (Qtool_bar_lines,
11930 make_number (nlines)),
11931 Qnil));
11932 if (WINDOW_TOTAL_LINES (w) != old_height)
11933 {
11934 clear_glyph_matrix (w->desired_matrix);
11935 fonts_changed_p = 1;
11936 return 1;
11937 }
11938 }
11939 }
11940
11941 /* Display as many lines as needed to display all tool-bar items. */
11942
11943 if (f->n_tool_bar_rows > 0)
11944 {
11945 int border, rows, height, extra;
11946
11947 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11948 border = XINT (Vtool_bar_border);
11949 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11950 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11951 else if (EQ (Vtool_bar_border, Qborder_width))
11952 border = f->border_width;
11953 else
11954 border = 0;
11955 if (border < 0)
11956 border = 0;
11957
11958 rows = f->n_tool_bar_rows;
11959 height = max (1, (it.last_visible_y - border) / rows);
11960 extra = it.last_visible_y - border - height * rows;
11961
11962 while (it.current_y < it.last_visible_y)
11963 {
11964 int h = 0;
11965 if (extra > 0 && rows-- > 0)
11966 {
11967 h = (extra + rows - 1) / rows;
11968 extra -= h;
11969 }
11970 display_tool_bar_line (&it, height + h);
11971 }
11972 }
11973 else
11974 {
11975 while (it.current_y < it.last_visible_y)
11976 display_tool_bar_line (&it, 0);
11977 }
11978
11979 /* It doesn't make much sense to try scrolling in the tool-bar
11980 window, so don't do it. */
11981 w->desired_matrix->no_scrolling_p = 1;
11982 w->must_be_updated_p = 1;
11983
11984 if (!NILP (Vauto_resize_tool_bars))
11985 {
11986 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11987 int change_height_p = 0;
11988
11989 /* If we couldn't display everything, change the tool-bar's
11990 height if there is room for more. */
11991 if (IT_STRING_CHARPOS (it) < it.end_charpos
11992 && it.current_y < max_tool_bar_height)
11993 change_height_p = 1;
11994
11995 row = it.glyph_row - 1;
11996
11997 /* If there are blank lines at the end, except for a partially
11998 visible blank line at the end that is smaller than
11999 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12000 if (!row->displays_text_p
12001 && row->height >= FRAME_LINE_HEIGHT (f))
12002 change_height_p = 1;
12003
12004 /* If row displays tool-bar items, but is partially visible,
12005 change the tool-bar's height. */
12006 if (row->displays_text_p
12007 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12008 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12009 change_height_p = 1;
12010
12011 /* Resize windows as needed by changing the `tool-bar-lines'
12012 frame parameter. */
12013 if (change_height_p)
12014 {
12015 Lisp_Object frame;
12016 int old_height = WINDOW_TOTAL_LINES (w);
12017 int nrows;
12018 int nlines = tool_bar_lines_needed (f, &nrows);
12019
12020 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12021 && !f->minimize_tool_bar_window_p)
12022 ? (nlines > old_height)
12023 : (nlines != old_height));
12024 f->minimize_tool_bar_window_p = 0;
12025
12026 if (change_height_p)
12027 {
12028 XSETFRAME (frame, f);
12029 Fmodify_frame_parameters (frame,
12030 Fcons (Fcons (Qtool_bar_lines,
12031 make_number (nlines)),
12032 Qnil));
12033 if (WINDOW_TOTAL_LINES (w) != old_height)
12034 {
12035 clear_glyph_matrix (w->desired_matrix);
12036 f->n_tool_bar_rows = nrows;
12037 fonts_changed_p = 1;
12038 return 1;
12039 }
12040 }
12041 }
12042 }
12043
12044 f->minimize_tool_bar_window_p = 0;
12045 return 0;
12046 }
12047
12048
12049 /* Get information about the tool-bar item which is displayed in GLYPH
12050 on frame F. Return in *PROP_IDX the index where tool-bar item
12051 properties start in F->tool_bar_items. Value is zero if
12052 GLYPH doesn't display a tool-bar item. */
12053
12054 static int
12055 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12056 {
12057 Lisp_Object prop;
12058 int success_p;
12059 int charpos;
12060
12061 /* This function can be called asynchronously, which means we must
12062 exclude any possibility that Fget_text_property signals an
12063 error. */
12064 charpos = min (SCHARS (FVAR (f, current_tool_bar_string)), glyph->charpos);
12065 charpos = max (0, charpos);
12066
12067 /* Get the text property `menu-item' at pos. The value of that
12068 property is the start index of this item's properties in
12069 F->tool_bar_items. */
12070 prop = Fget_text_property (make_number (charpos),
12071 Qmenu_item, FVAR (f, current_tool_bar_string));
12072 if (INTEGERP (prop))
12073 {
12074 *prop_idx = XINT (prop);
12075 success_p = 1;
12076 }
12077 else
12078 success_p = 0;
12079
12080 return success_p;
12081 }
12082
12083 \f
12084 /* Get information about the tool-bar item at position X/Y on frame F.
12085 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12086 the current matrix of the tool-bar window of F, or NULL if not
12087 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12088 item in F->tool_bar_items. Value is
12089
12090 -1 if X/Y is not on a tool-bar item
12091 0 if X/Y is on the same item that was highlighted before.
12092 1 otherwise. */
12093
12094 static int
12095 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12096 int *hpos, int *vpos, int *prop_idx)
12097 {
12098 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12099 struct window *w = XWINDOW (FVAR (f, tool_bar_window));
12100 int area;
12101
12102 /* Find the glyph under X/Y. */
12103 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12104 if (*glyph == NULL)
12105 return -1;
12106
12107 /* Get the start of this tool-bar item's properties in
12108 f->tool_bar_items. */
12109 if (!tool_bar_item_info (f, *glyph, prop_idx))
12110 return -1;
12111
12112 /* Is mouse on the highlighted item? */
12113 if (EQ (FVAR (f, tool_bar_window), hlinfo->mouse_face_window)
12114 && *vpos >= hlinfo->mouse_face_beg_row
12115 && *vpos <= hlinfo->mouse_face_end_row
12116 && (*vpos > hlinfo->mouse_face_beg_row
12117 || *hpos >= hlinfo->mouse_face_beg_col)
12118 && (*vpos < hlinfo->mouse_face_end_row
12119 || *hpos < hlinfo->mouse_face_end_col
12120 || hlinfo->mouse_face_past_end))
12121 return 0;
12122
12123 return 1;
12124 }
12125
12126
12127 /* EXPORT:
12128 Handle mouse button event on the tool-bar of frame F, at
12129 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12130 0 for button release. MODIFIERS is event modifiers for button
12131 release. */
12132
12133 void
12134 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12135 int modifiers)
12136 {
12137 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12138 struct window *w = XWINDOW (FVAR (f, tool_bar_window));
12139 int hpos, vpos, prop_idx;
12140 struct glyph *glyph;
12141 Lisp_Object enabled_p;
12142
12143 /* If not on the highlighted tool-bar item, return. */
12144 frame_to_window_pixel_xy (w, &x, &y);
12145 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12146 return;
12147
12148 /* If item is disabled, do nothing. */
12149 enabled_p = AREF (FVAR (f, tool_bar_items), prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12150 if (NILP (enabled_p))
12151 return;
12152
12153 if (down_p)
12154 {
12155 /* Show item in pressed state. */
12156 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12157 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12158 last_tool_bar_item = prop_idx;
12159 }
12160 else
12161 {
12162 Lisp_Object key, frame;
12163 struct input_event event;
12164 EVENT_INIT (event);
12165
12166 /* Show item in released state. */
12167 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12168 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12169
12170 key = AREF (FVAR (f, tool_bar_items), prop_idx + TOOL_BAR_ITEM_KEY);
12171
12172 XSETFRAME (frame, f);
12173 event.kind = TOOL_BAR_EVENT;
12174 event.frame_or_window = frame;
12175 event.arg = frame;
12176 kbd_buffer_store_event (&event);
12177
12178 event.kind = TOOL_BAR_EVENT;
12179 event.frame_or_window = frame;
12180 event.arg = key;
12181 event.modifiers = modifiers;
12182 kbd_buffer_store_event (&event);
12183 last_tool_bar_item = -1;
12184 }
12185 }
12186
12187
12188 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12189 tool-bar window-relative coordinates X/Y. Called from
12190 note_mouse_highlight. */
12191
12192 static void
12193 note_tool_bar_highlight (struct frame *f, int x, int y)
12194 {
12195 Lisp_Object window = FVAR (f, tool_bar_window);
12196 struct window *w = XWINDOW (window);
12197 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12198 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12199 int hpos, vpos;
12200 struct glyph *glyph;
12201 struct glyph_row *row;
12202 int i;
12203 Lisp_Object enabled_p;
12204 int prop_idx;
12205 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12206 int mouse_down_p, rc;
12207
12208 /* Function note_mouse_highlight is called with negative X/Y
12209 values when mouse moves outside of the frame. */
12210 if (x <= 0 || y <= 0)
12211 {
12212 clear_mouse_face (hlinfo);
12213 return;
12214 }
12215
12216 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12217 if (rc < 0)
12218 {
12219 /* Not on tool-bar item. */
12220 clear_mouse_face (hlinfo);
12221 return;
12222 }
12223 else if (rc == 0)
12224 /* On same tool-bar item as before. */
12225 goto set_help_echo;
12226
12227 clear_mouse_face (hlinfo);
12228
12229 /* Mouse is down, but on different tool-bar item? */
12230 mouse_down_p = (dpyinfo->grabbed
12231 && f == last_mouse_frame
12232 && FRAME_LIVE_P (f));
12233 if (mouse_down_p
12234 && last_tool_bar_item != prop_idx)
12235 return;
12236
12237 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12238 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12239
12240 /* If tool-bar item is not enabled, don't highlight it. */
12241 enabled_p = AREF (FVAR (f, tool_bar_items), prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12242 if (!NILP (enabled_p))
12243 {
12244 /* Compute the x-position of the glyph. In front and past the
12245 image is a space. We include this in the highlighted area. */
12246 row = MATRIX_ROW (w->current_matrix, vpos);
12247 for (i = x = 0; i < hpos; ++i)
12248 x += row->glyphs[TEXT_AREA][i].pixel_width;
12249
12250 /* Record this as the current active region. */
12251 hlinfo->mouse_face_beg_col = hpos;
12252 hlinfo->mouse_face_beg_row = vpos;
12253 hlinfo->mouse_face_beg_x = x;
12254 hlinfo->mouse_face_beg_y = row->y;
12255 hlinfo->mouse_face_past_end = 0;
12256
12257 hlinfo->mouse_face_end_col = hpos + 1;
12258 hlinfo->mouse_face_end_row = vpos;
12259 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12260 hlinfo->mouse_face_end_y = row->y;
12261 hlinfo->mouse_face_window = window;
12262 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12263
12264 /* Display it as active. */
12265 show_mouse_face (hlinfo, draw);
12266 hlinfo->mouse_face_image_state = draw;
12267 }
12268
12269 set_help_echo:
12270
12271 /* Set help_echo_string to a help string to display for this tool-bar item.
12272 XTread_socket does the rest. */
12273 help_echo_object = help_echo_window = Qnil;
12274 help_echo_pos = -1;
12275 help_echo_string = AREF (FVAR (f, tool_bar_items), prop_idx + TOOL_BAR_ITEM_HELP);
12276 if (NILP (help_echo_string))
12277 help_echo_string = AREF (FVAR (f, tool_bar_items), prop_idx + TOOL_BAR_ITEM_CAPTION);
12278 }
12279
12280 #endif /* HAVE_WINDOW_SYSTEM */
12281
12282
12283 \f
12284 /************************************************************************
12285 Horizontal scrolling
12286 ************************************************************************/
12287
12288 static int hscroll_window_tree (Lisp_Object);
12289 static int hscroll_windows (Lisp_Object);
12290
12291 /* For all leaf windows in the window tree rooted at WINDOW, set their
12292 hscroll value so that PT is (i) visible in the window, and (ii) so
12293 that it is not within a certain margin at the window's left and
12294 right border. Value is non-zero if any window's hscroll has been
12295 changed. */
12296
12297 static int
12298 hscroll_window_tree (Lisp_Object window)
12299 {
12300 int hscrolled_p = 0;
12301 int hscroll_relative_p = FLOATP (Vhscroll_step);
12302 int hscroll_step_abs = 0;
12303 double hscroll_step_rel = 0;
12304
12305 if (hscroll_relative_p)
12306 {
12307 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12308 if (hscroll_step_rel < 0)
12309 {
12310 hscroll_relative_p = 0;
12311 hscroll_step_abs = 0;
12312 }
12313 }
12314 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12315 {
12316 hscroll_step_abs = XINT (Vhscroll_step);
12317 if (hscroll_step_abs < 0)
12318 hscroll_step_abs = 0;
12319 }
12320 else
12321 hscroll_step_abs = 0;
12322
12323 while (WINDOWP (window))
12324 {
12325 struct window *w = XWINDOW (window);
12326
12327 if (WINDOWP (WVAR (w, hchild)))
12328 hscrolled_p |= hscroll_window_tree (WVAR (w, hchild));
12329 else if (WINDOWP (WVAR (w, vchild)))
12330 hscrolled_p |= hscroll_window_tree (WVAR (w, vchild));
12331 else if (w->cursor.vpos >= 0)
12332 {
12333 int h_margin;
12334 int text_area_width;
12335 struct glyph_row *current_cursor_row
12336 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12337 struct glyph_row *desired_cursor_row
12338 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12339 struct glyph_row *cursor_row
12340 = (desired_cursor_row->enabled_p
12341 ? desired_cursor_row
12342 : current_cursor_row);
12343 int row_r2l_p = cursor_row->reversed_p;
12344
12345 text_area_width = window_box_width (w, TEXT_AREA);
12346
12347 /* Scroll when cursor is inside this scroll margin. */
12348 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12349
12350 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, WVAR (w, buffer)))
12351 /* For left-to-right rows, hscroll when cursor is either
12352 (i) inside the right hscroll margin, or (ii) if it is
12353 inside the left margin and the window is already
12354 hscrolled. */
12355 && ((!row_r2l_p
12356 && ((w->hscroll
12357 && w->cursor.x <= h_margin)
12358 || (cursor_row->enabled_p
12359 && cursor_row->truncated_on_right_p
12360 && (w->cursor.x >= text_area_width - h_margin))))
12361 /* For right-to-left rows, the logic is similar,
12362 except that rules for scrolling to left and right
12363 are reversed. E.g., if cursor.x <= h_margin, we
12364 need to hscroll "to the right" unconditionally,
12365 and that will scroll the screen to the left so as
12366 to reveal the next portion of the row. */
12367 || (row_r2l_p
12368 && ((cursor_row->enabled_p
12369 /* FIXME: It is confusing to set the
12370 truncated_on_right_p flag when R2L rows
12371 are actually truncated on the left. */
12372 && cursor_row->truncated_on_right_p
12373 && w->cursor.x <= h_margin)
12374 || (w->hscroll
12375 && (w->cursor.x >= text_area_width - h_margin))))))
12376 {
12377 struct it it;
12378 ptrdiff_t hscroll;
12379 struct buffer *saved_current_buffer;
12380 ptrdiff_t pt;
12381 int wanted_x;
12382
12383 /* Find point in a display of infinite width. */
12384 saved_current_buffer = current_buffer;
12385 current_buffer = XBUFFER (WVAR (w, buffer));
12386
12387 if (w == XWINDOW (selected_window))
12388 pt = PT;
12389 else
12390 {
12391 pt = marker_position (WVAR (w, pointm));
12392 pt = max (BEGV, pt);
12393 pt = min (ZV, pt);
12394 }
12395
12396 /* Move iterator to pt starting at cursor_row->start in
12397 a line with infinite width. */
12398 init_to_row_start (&it, w, cursor_row);
12399 it.last_visible_x = INFINITY;
12400 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12401 current_buffer = saved_current_buffer;
12402
12403 /* Position cursor in window. */
12404 if (!hscroll_relative_p && hscroll_step_abs == 0)
12405 hscroll = max (0, (it.current_x
12406 - (ITERATOR_AT_END_OF_LINE_P (&it)
12407 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12408 : (text_area_width / 2))))
12409 / FRAME_COLUMN_WIDTH (it.f);
12410 else if ((!row_r2l_p
12411 && w->cursor.x >= text_area_width - h_margin)
12412 || (row_r2l_p && w->cursor.x <= h_margin))
12413 {
12414 if (hscroll_relative_p)
12415 wanted_x = text_area_width * (1 - hscroll_step_rel)
12416 - h_margin;
12417 else
12418 wanted_x = text_area_width
12419 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12420 - h_margin;
12421 hscroll
12422 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12423 }
12424 else
12425 {
12426 if (hscroll_relative_p)
12427 wanted_x = text_area_width * hscroll_step_rel
12428 + h_margin;
12429 else
12430 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12431 + h_margin;
12432 hscroll
12433 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12434 }
12435 hscroll = max (hscroll, w->min_hscroll);
12436
12437 /* Don't prevent redisplay optimizations if hscroll
12438 hasn't changed, as it will unnecessarily slow down
12439 redisplay. */
12440 if (w->hscroll != hscroll)
12441 {
12442 XBUFFER (WVAR (w, buffer))->prevent_redisplay_optimizations_p = 1;
12443 w->hscroll = hscroll;
12444 hscrolled_p = 1;
12445 }
12446 }
12447 }
12448
12449 window = WVAR (w, next);
12450 }
12451
12452 /* Value is non-zero if hscroll of any leaf window has been changed. */
12453 return hscrolled_p;
12454 }
12455
12456
12457 /* Set hscroll so that cursor is visible and not inside horizontal
12458 scroll margins for all windows in the tree rooted at WINDOW. See
12459 also hscroll_window_tree above. Value is non-zero if any window's
12460 hscroll has been changed. If it has, desired matrices on the frame
12461 of WINDOW are cleared. */
12462
12463 static int
12464 hscroll_windows (Lisp_Object window)
12465 {
12466 int hscrolled_p = hscroll_window_tree (window);
12467 if (hscrolled_p)
12468 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12469 return hscrolled_p;
12470 }
12471
12472
12473 \f
12474 /************************************************************************
12475 Redisplay
12476 ************************************************************************/
12477
12478 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12479 to a non-zero value. This is sometimes handy to have in a debugger
12480 session. */
12481
12482 #ifdef GLYPH_DEBUG
12483
12484 /* First and last unchanged row for try_window_id. */
12485
12486 static int debug_first_unchanged_at_end_vpos;
12487 static int debug_last_unchanged_at_beg_vpos;
12488
12489 /* Delta vpos and y. */
12490
12491 static int debug_dvpos, debug_dy;
12492
12493 /* Delta in characters and bytes for try_window_id. */
12494
12495 static ptrdiff_t debug_delta, debug_delta_bytes;
12496
12497 /* Values of window_end_pos and window_end_vpos at the end of
12498 try_window_id. */
12499
12500 static ptrdiff_t debug_end_vpos;
12501
12502 /* Append a string to W->desired_matrix->method. FMT is a printf
12503 format string. If trace_redisplay_p is non-zero also printf the
12504 resulting string to stderr. */
12505
12506 static void debug_method_add (struct window *, char const *, ...)
12507 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12508
12509 static void
12510 debug_method_add (struct window *w, char const *fmt, ...)
12511 {
12512 char *method = w->desired_matrix->method;
12513 int len = strlen (method);
12514 int size = sizeof w->desired_matrix->method;
12515 int remaining = size - len - 1;
12516 va_list ap;
12517
12518 if (len && remaining)
12519 {
12520 method[len] = '|';
12521 --remaining, ++len;
12522 }
12523
12524 va_start (ap, fmt);
12525 vsnprintf (method + len, remaining + 1, fmt, ap);
12526 va_end (ap);
12527
12528 if (trace_redisplay_p)
12529 fprintf (stderr, "%p (%s): %s\n",
12530 w,
12531 ((BUFFERP (WVAR (w, buffer))
12532 && STRINGP (BVAR (XBUFFER (WVAR (w, buffer)), name)))
12533 ? SSDATA (BVAR (XBUFFER (WVAR (w, buffer)), name))
12534 : "no buffer"),
12535 method + len);
12536 }
12537
12538 #endif /* GLYPH_DEBUG */
12539
12540
12541 /* Value is non-zero if all changes in window W, which displays
12542 current_buffer, are in the text between START and END. START is a
12543 buffer position, END is given as a distance from Z. Used in
12544 redisplay_internal for display optimization. */
12545
12546 static inline int
12547 text_outside_line_unchanged_p (struct window *w,
12548 ptrdiff_t start, ptrdiff_t end)
12549 {
12550 int unchanged_p = 1;
12551
12552 /* If text or overlays have changed, see where. */
12553 if (w->last_modified < MODIFF
12554 || w->last_overlay_modified < OVERLAY_MODIFF)
12555 {
12556 /* Gap in the line? */
12557 if (GPT < start || Z - GPT < end)
12558 unchanged_p = 0;
12559
12560 /* Changes start in front of the line, or end after it? */
12561 if (unchanged_p
12562 && (BEG_UNCHANGED < start - 1
12563 || END_UNCHANGED < end))
12564 unchanged_p = 0;
12565
12566 /* If selective display, can't optimize if changes start at the
12567 beginning of the line. */
12568 if (unchanged_p
12569 && INTEGERP (BVAR (current_buffer, selective_display))
12570 && XINT (BVAR (current_buffer, selective_display)) > 0
12571 && (BEG_UNCHANGED < start || GPT <= start))
12572 unchanged_p = 0;
12573
12574 /* If there are overlays at the start or end of the line, these
12575 may have overlay strings with newlines in them. A change at
12576 START, for instance, may actually concern the display of such
12577 overlay strings as well, and they are displayed on different
12578 lines. So, quickly rule out this case. (For the future, it
12579 might be desirable to implement something more telling than
12580 just BEG/END_UNCHANGED.) */
12581 if (unchanged_p)
12582 {
12583 if (BEG + BEG_UNCHANGED == start
12584 && overlay_touches_p (start))
12585 unchanged_p = 0;
12586 if (END_UNCHANGED == end
12587 && overlay_touches_p (Z - end))
12588 unchanged_p = 0;
12589 }
12590
12591 /* Under bidi reordering, adding or deleting a character in the
12592 beginning of a paragraph, before the first strong directional
12593 character, can change the base direction of the paragraph (unless
12594 the buffer specifies a fixed paragraph direction), which will
12595 require to redisplay the whole paragraph. It might be worthwhile
12596 to find the paragraph limits and widen the range of redisplayed
12597 lines to that, but for now just give up this optimization. */
12598 if (!NILP (BVAR (XBUFFER (WVAR (w, buffer)), bidi_display_reordering))
12599 && NILP (BVAR (XBUFFER (WVAR (w, buffer)), bidi_paragraph_direction)))
12600 unchanged_p = 0;
12601 }
12602
12603 return unchanged_p;
12604 }
12605
12606
12607 /* Do a frame update, taking possible shortcuts into account. This is
12608 the main external entry point for redisplay.
12609
12610 If the last redisplay displayed an echo area message and that message
12611 is no longer requested, we clear the echo area or bring back the
12612 mini-buffer if that is in use. */
12613
12614 void
12615 redisplay (void)
12616 {
12617 redisplay_internal ();
12618 }
12619
12620
12621 static Lisp_Object
12622 overlay_arrow_string_or_property (Lisp_Object var)
12623 {
12624 Lisp_Object val;
12625
12626 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12627 return val;
12628
12629 return Voverlay_arrow_string;
12630 }
12631
12632 /* Return 1 if there are any overlay-arrows in current_buffer. */
12633 static int
12634 overlay_arrow_in_current_buffer_p (void)
12635 {
12636 Lisp_Object vlist;
12637
12638 for (vlist = Voverlay_arrow_variable_list;
12639 CONSP (vlist);
12640 vlist = XCDR (vlist))
12641 {
12642 Lisp_Object var = XCAR (vlist);
12643 Lisp_Object val;
12644
12645 if (!SYMBOLP (var))
12646 continue;
12647 val = find_symbol_value (var);
12648 if (MARKERP (val)
12649 && current_buffer == XMARKER (val)->buffer)
12650 return 1;
12651 }
12652 return 0;
12653 }
12654
12655
12656 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12657 has changed. */
12658
12659 static int
12660 overlay_arrows_changed_p (void)
12661 {
12662 Lisp_Object vlist;
12663
12664 for (vlist = Voverlay_arrow_variable_list;
12665 CONSP (vlist);
12666 vlist = XCDR (vlist))
12667 {
12668 Lisp_Object var = XCAR (vlist);
12669 Lisp_Object val, pstr;
12670
12671 if (!SYMBOLP (var))
12672 continue;
12673 val = find_symbol_value (var);
12674 if (!MARKERP (val))
12675 continue;
12676 if (! EQ (COERCE_MARKER (val),
12677 Fget (var, Qlast_arrow_position))
12678 || ! (pstr = overlay_arrow_string_or_property (var),
12679 EQ (pstr, Fget (var, Qlast_arrow_string))))
12680 return 1;
12681 }
12682 return 0;
12683 }
12684
12685 /* Mark overlay arrows to be updated on next redisplay. */
12686
12687 static void
12688 update_overlay_arrows (int up_to_date)
12689 {
12690 Lisp_Object vlist;
12691
12692 for (vlist = Voverlay_arrow_variable_list;
12693 CONSP (vlist);
12694 vlist = XCDR (vlist))
12695 {
12696 Lisp_Object var = XCAR (vlist);
12697
12698 if (!SYMBOLP (var))
12699 continue;
12700
12701 if (up_to_date > 0)
12702 {
12703 Lisp_Object val = find_symbol_value (var);
12704 Fput (var, Qlast_arrow_position,
12705 COERCE_MARKER (val));
12706 Fput (var, Qlast_arrow_string,
12707 overlay_arrow_string_or_property (var));
12708 }
12709 else if (up_to_date < 0
12710 || !NILP (Fget (var, Qlast_arrow_position)))
12711 {
12712 Fput (var, Qlast_arrow_position, Qt);
12713 Fput (var, Qlast_arrow_string, Qt);
12714 }
12715 }
12716 }
12717
12718
12719 /* Return overlay arrow string to display at row.
12720 Return integer (bitmap number) for arrow bitmap in left fringe.
12721 Return nil if no overlay arrow. */
12722
12723 static Lisp_Object
12724 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12725 {
12726 Lisp_Object vlist;
12727
12728 for (vlist = Voverlay_arrow_variable_list;
12729 CONSP (vlist);
12730 vlist = XCDR (vlist))
12731 {
12732 Lisp_Object var = XCAR (vlist);
12733 Lisp_Object val;
12734
12735 if (!SYMBOLP (var))
12736 continue;
12737
12738 val = find_symbol_value (var);
12739
12740 if (MARKERP (val)
12741 && current_buffer == XMARKER (val)->buffer
12742 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12743 {
12744 if (FRAME_WINDOW_P (it->f)
12745 /* FIXME: if ROW->reversed_p is set, this should test
12746 the right fringe, not the left one. */
12747 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12748 {
12749 #ifdef HAVE_WINDOW_SYSTEM
12750 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12751 {
12752 int fringe_bitmap;
12753 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12754 return make_number (fringe_bitmap);
12755 }
12756 #endif
12757 return make_number (-1); /* Use default arrow bitmap */
12758 }
12759 return overlay_arrow_string_or_property (var);
12760 }
12761 }
12762
12763 return Qnil;
12764 }
12765
12766 /* Return 1 if point moved out of or into a composition. Otherwise
12767 return 0. PREV_BUF and PREV_PT are the last point buffer and
12768 position. BUF and PT are the current point buffer and position. */
12769
12770 static int
12771 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12772 struct buffer *buf, ptrdiff_t pt)
12773 {
12774 ptrdiff_t start, end;
12775 Lisp_Object prop;
12776 Lisp_Object buffer;
12777
12778 XSETBUFFER (buffer, buf);
12779 /* Check a composition at the last point if point moved within the
12780 same buffer. */
12781 if (prev_buf == buf)
12782 {
12783 if (prev_pt == pt)
12784 /* Point didn't move. */
12785 return 0;
12786
12787 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12788 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12789 && COMPOSITION_VALID_P (start, end, prop)
12790 && start < prev_pt && end > prev_pt)
12791 /* The last point was within the composition. Return 1 iff
12792 point moved out of the composition. */
12793 return (pt <= start || pt >= end);
12794 }
12795
12796 /* Check a composition at the current point. */
12797 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12798 && find_composition (pt, -1, &start, &end, &prop, buffer)
12799 && COMPOSITION_VALID_P (start, end, prop)
12800 && start < pt && end > pt);
12801 }
12802
12803
12804 /* Reconsider the setting of B->clip_changed which is displayed
12805 in window W. */
12806
12807 static inline void
12808 reconsider_clip_changes (struct window *w, struct buffer *b)
12809 {
12810 if (b->clip_changed
12811 && !NILP (WVAR (w, window_end_valid))
12812 && w->current_matrix->buffer == b
12813 && w->current_matrix->zv == BUF_ZV (b)
12814 && w->current_matrix->begv == BUF_BEGV (b))
12815 b->clip_changed = 0;
12816
12817 /* If display wasn't paused, and W is not a tool bar window, see if
12818 point has been moved into or out of a composition. In that case,
12819 we set b->clip_changed to 1 to force updating the screen. If
12820 b->clip_changed has already been set to 1, we can skip this
12821 check. */
12822 if (!b->clip_changed
12823 && BUFFERP (WVAR (w, buffer)) && !NILP (WVAR (w, window_end_valid)))
12824 {
12825 ptrdiff_t pt;
12826
12827 if (w == XWINDOW (selected_window))
12828 pt = PT;
12829 else
12830 pt = marker_position (WVAR (w, pointm));
12831
12832 if ((w->current_matrix->buffer != XBUFFER (WVAR (w, buffer))
12833 || pt != w->last_point)
12834 && check_point_in_composition (w->current_matrix->buffer,
12835 w->last_point,
12836 XBUFFER (WVAR (w, buffer)), pt))
12837 b->clip_changed = 1;
12838 }
12839 }
12840 \f
12841
12842 /* Select FRAME to forward the values of frame-local variables into C
12843 variables so that the redisplay routines can access those values
12844 directly. */
12845
12846 static void
12847 select_frame_for_redisplay (Lisp_Object frame)
12848 {
12849 Lisp_Object tail, tem;
12850 Lisp_Object old = selected_frame;
12851 struct Lisp_Symbol *sym;
12852
12853 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12854
12855 selected_frame = frame;
12856
12857 do {
12858 for (tail = FVAR (XFRAME (frame), param_alist);
12859 CONSP (tail); tail = XCDR (tail))
12860 if (CONSP (XCAR (tail))
12861 && (tem = XCAR (XCAR (tail)),
12862 SYMBOLP (tem))
12863 && (sym = indirect_variable (XSYMBOL (tem)),
12864 sym->redirect == SYMBOL_LOCALIZED)
12865 && sym->val.blv->frame_local)
12866 /* Use find_symbol_value rather than Fsymbol_value
12867 to avoid an error if it is void. */
12868 find_symbol_value (tem);
12869 } while (!EQ (frame, old) && (frame = old, 1));
12870 }
12871
12872
12873 #define STOP_POLLING \
12874 do { if (! polling_stopped_here) stop_polling (); \
12875 polling_stopped_here = 1; } while (0)
12876
12877 #define RESUME_POLLING \
12878 do { if (polling_stopped_here) start_polling (); \
12879 polling_stopped_here = 0; } while (0)
12880
12881
12882 /* Perhaps in the future avoid recentering windows if it
12883 is not necessary; currently that causes some problems. */
12884
12885 static void
12886 redisplay_internal (void)
12887 {
12888 struct window *w = XWINDOW (selected_window);
12889 struct window *sw;
12890 struct frame *fr;
12891 int pending;
12892 int must_finish = 0;
12893 struct text_pos tlbufpos, tlendpos;
12894 int number_of_visible_frames;
12895 ptrdiff_t count, count1;
12896 struct frame *sf;
12897 int polling_stopped_here = 0;
12898 Lisp_Object old_frame = selected_frame;
12899
12900 /* Non-zero means redisplay has to consider all windows on all
12901 frames. Zero means, only selected_window is considered. */
12902 int consider_all_windows_p;
12903
12904 /* Non-zero means redisplay has to redisplay the miniwindow */
12905 int update_miniwindow_p = 0;
12906
12907 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12908
12909 /* No redisplay if running in batch mode or frame is not yet fully
12910 initialized, or redisplay is explicitly turned off by setting
12911 Vinhibit_redisplay. */
12912 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12913 || !NILP (Vinhibit_redisplay))
12914 return;
12915
12916 /* Don't examine these until after testing Vinhibit_redisplay.
12917 When Emacs is shutting down, perhaps because its connection to
12918 X has dropped, we should not look at them at all. */
12919 fr = XFRAME (WVAR (w, frame));
12920 sf = SELECTED_FRAME ();
12921
12922 if (!fr->glyphs_initialized_p)
12923 return;
12924
12925 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12926 if (popup_activated ())
12927 return;
12928 #endif
12929
12930 /* I don't think this happens but let's be paranoid. */
12931 if (redisplaying_p)
12932 return;
12933
12934 /* Record a function that resets redisplaying_p to its old value
12935 when we leave this function. */
12936 count = SPECPDL_INDEX ();
12937 record_unwind_protect (unwind_redisplay,
12938 Fcons (make_number (redisplaying_p), selected_frame));
12939 ++redisplaying_p;
12940 specbind (Qinhibit_free_realized_faces, Qnil);
12941
12942 {
12943 Lisp_Object tail, frame;
12944
12945 FOR_EACH_FRAME (tail, frame)
12946 {
12947 struct frame *f = XFRAME (frame);
12948 f->already_hscrolled_p = 0;
12949 }
12950 }
12951
12952 retry:
12953 /* Remember the currently selected window. */
12954 sw = w;
12955
12956 if (!EQ (old_frame, selected_frame)
12957 && FRAME_LIVE_P (XFRAME (old_frame)))
12958 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12959 selected_frame and selected_window to be temporarily out-of-sync so
12960 when we come back here via `goto retry', we need to resync because we
12961 may need to run Elisp code (via prepare_menu_bars). */
12962 select_frame_for_redisplay (old_frame);
12963
12964 pending = 0;
12965 reconsider_clip_changes (w, current_buffer);
12966 last_escape_glyph_frame = NULL;
12967 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12968 last_glyphless_glyph_frame = NULL;
12969 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12970
12971 /* If new fonts have been loaded that make a glyph matrix adjustment
12972 necessary, do it. */
12973 if (fonts_changed_p)
12974 {
12975 adjust_glyphs (NULL);
12976 ++windows_or_buffers_changed;
12977 fonts_changed_p = 0;
12978 }
12979
12980 /* If face_change_count is non-zero, init_iterator will free all
12981 realized faces, which includes the faces referenced from current
12982 matrices. So, we can't reuse current matrices in this case. */
12983 if (face_change_count)
12984 ++windows_or_buffers_changed;
12985
12986 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12987 && FRAME_TTY (sf)->previous_frame != sf)
12988 {
12989 /* Since frames on a single ASCII terminal share the same
12990 display area, displaying a different frame means redisplay
12991 the whole thing. */
12992 windows_or_buffers_changed++;
12993 SET_FRAME_GARBAGED (sf);
12994 #ifndef DOS_NT
12995 set_tty_color_mode (FRAME_TTY (sf), sf);
12996 #endif
12997 FRAME_TTY (sf)->previous_frame = sf;
12998 }
12999
13000 /* Set the visible flags for all frames. Do this before checking
13001 for resized or garbaged frames; they want to know if their frames
13002 are visible. See the comment in frame.h for
13003 FRAME_SAMPLE_VISIBILITY. */
13004 {
13005 Lisp_Object tail, frame;
13006
13007 number_of_visible_frames = 0;
13008
13009 FOR_EACH_FRAME (tail, frame)
13010 {
13011 struct frame *f = XFRAME (frame);
13012
13013 FRAME_SAMPLE_VISIBILITY (f);
13014 if (FRAME_VISIBLE_P (f))
13015 ++number_of_visible_frames;
13016 clear_desired_matrices (f);
13017 }
13018 }
13019
13020 /* Notice any pending interrupt request to change frame size. */
13021 do_pending_window_change (1);
13022
13023 /* do_pending_window_change could change the selected_window due to
13024 frame resizing which makes the selected window too small. */
13025 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13026 {
13027 sw = w;
13028 reconsider_clip_changes (w, current_buffer);
13029 }
13030
13031 /* Clear frames marked as garbaged. */
13032 if (frame_garbaged)
13033 clear_garbaged_frames ();
13034
13035 /* Build menubar and tool-bar items. */
13036 if (NILP (Vmemory_full))
13037 prepare_menu_bars ();
13038
13039 if (windows_or_buffers_changed)
13040 update_mode_lines++;
13041
13042 /* Detect case that we need to write or remove a star in the mode line. */
13043 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13044 {
13045 w->update_mode_line = 1;
13046 if (buffer_shared > 1)
13047 update_mode_lines++;
13048 }
13049
13050 /* Avoid invocation of point motion hooks by `current_column' below. */
13051 count1 = SPECPDL_INDEX ();
13052 specbind (Qinhibit_point_motion_hooks, Qt);
13053
13054 /* If %c is in the mode line, update it if needed. */
13055 if (!NILP (WVAR (w, column_number_displayed))
13056 /* This alternative quickly identifies a common case
13057 where no change is needed. */
13058 && !(PT == w->last_point
13059 && w->last_modified >= MODIFF
13060 && w->last_overlay_modified >= OVERLAY_MODIFF)
13061 && (XFASTINT (WVAR (w, column_number_displayed)) != current_column ()))
13062 w->update_mode_line = 1;
13063
13064 unbind_to (count1, Qnil);
13065
13066 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (WVAR (w, frame))) = -1;
13067
13068 /* The variable buffer_shared is set in redisplay_window and
13069 indicates that we redisplay a buffer in different windows. See
13070 there. */
13071 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13072 || cursor_type_changed);
13073
13074 /* If specs for an arrow have changed, do thorough redisplay
13075 to ensure we remove any arrow that should no longer exist. */
13076 if (overlay_arrows_changed_p ())
13077 consider_all_windows_p = windows_or_buffers_changed = 1;
13078
13079 /* Normally the message* functions will have already displayed and
13080 updated the echo area, but the frame may have been trashed, or
13081 the update may have been preempted, so display the echo area
13082 again here. Checking message_cleared_p captures the case that
13083 the echo area should be cleared. */
13084 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13085 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13086 || (message_cleared_p
13087 && minibuf_level == 0
13088 /* If the mini-window is currently selected, this means the
13089 echo-area doesn't show through. */
13090 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13091 {
13092 int window_height_changed_p = echo_area_display (0);
13093
13094 if (message_cleared_p)
13095 update_miniwindow_p = 1;
13096
13097 must_finish = 1;
13098
13099 /* If we don't display the current message, don't clear the
13100 message_cleared_p flag, because, if we did, we wouldn't clear
13101 the echo area in the next redisplay which doesn't preserve
13102 the echo area. */
13103 if (!display_last_displayed_message_p)
13104 message_cleared_p = 0;
13105
13106 if (fonts_changed_p)
13107 goto retry;
13108 else if (window_height_changed_p)
13109 {
13110 consider_all_windows_p = 1;
13111 ++update_mode_lines;
13112 ++windows_or_buffers_changed;
13113
13114 /* If window configuration was changed, frames may have been
13115 marked garbaged. Clear them or we will experience
13116 surprises wrt scrolling. */
13117 if (frame_garbaged)
13118 clear_garbaged_frames ();
13119 }
13120 }
13121 else if (EQ (selected_window, minibuf_window)
13122 && (current_buffer->clip_changed
13123 || w->last_modified < MODIFF
13124 || w->last_overlay_modified < OVERLAY_MODIFF)
13125 && resize_mini_window (w, 0))
13126 {
13127 /* Resized active mini-window to fit the size of what it is
13128 showing if its contents might have changed. */
13129 must_finish = 1;
13130 /* FIXME: this causes all frames to be updated, which seems unnecessary
13131 since only the current frame needs to be considered. This function needs
13132 to be rewritten with two variables, consider_all_windows and
13133 consider_all_frames. */
13134 consider_all_windows_p = 1;
13135 ++windows_or_buffers_changed;
13136 ++update_mode_lines;
13137
13138 /* If window configuration was changed, frames may have been
13139 marked garbaged. Clear them or we will experience
13140 surprises wrt scrolling. */
13141 if (frame_garbaged)
13142 clear_garbaged_frames ();
13143 }
13144
13145
13146 /* If showing the region, and mark has changed, we must redisplay
13147 the whole window. The assignment to this_line_start_pos prevents
13148 the optimization directly below this if-statement. */
13149 if (((!NILP (Vtransient_mark_mode)
13150 && !NILP (BVAR (XBUFFER (WVAR (w, buffer)), mark_active)))
13151 != !NILP (WVAR (w, region_showing)))
13152 || (!NILP (WVAR (w, region_showing))
13153 && !EQ (WVAR (w, region_showing),
13154 Fmarker_position (BVAR (XBUFFER (WVAR (w, buffer)), mark)))))
13155 CHARPOS (this_line_start_pos) = 0;
13156
13157 /* Optimize the case that only the line containing the cursor in the
13158 selected window has changed. Variables starting with this_ are
13159 set in display_line and record information about the line
13160 containing the cursor. */
13161 tlbufpos = this_line_start_pos;
13162 tlendpos = this_line_end_pos;
13163 if (!consider_all_windows_p
13164 && CHARPOS (tlbufpos) > 0
13165 && !w->update_mode_line
13166 && !current_buffer->clip_changed
13167 && !current_buffer->prevent_redisplay_optimizations_p
13168 && FRAME_VISIBLE_P (XFRAME (WVAR (w, frame)))
13169 && !FRAME_OBSCURED_P (XFRAME (WVAR (w, frame)))
13170 /* Make sure recorded data applies to current buffer, etc. */
13171 && this_line_buffer == current_buffer
13172 && current_buffer == XBUFFER (WVAR (w, buffer))
13173 && !w->force_start
13174 && !w->optional_new_start
13175 /* Point must be on the line that we have info recorded about. */
13176 && PT >= CHARPOS (tlbufpos)
13177 && PT <= Z - CHARPOS (tlendpos)
13178 /* All text outside that line, including its final newline,
13179 must be unchanged. */
13180 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13181 CHARPOS (tlendpos)))
13182 {
13183 if (CHARPOS (tlbufpos) > BEGV
13184 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13185 && (CHARPOS (tlbufpos) == ZV
13186 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13187 /* Former continuation line has disappeared by becoming empty. */
13188 goto cancel;
13189 else if (w->last_modified < MODIFF
13190 || w->last_overlay_modified < OVERLAY_MODIFF
13191 || MINI_WINDOW_P (w))
13192 {
13193 /* We have to handle the case of continuation around a
13194 wide-column character (see the comment in indent.c around
13195 line 1340).
13196
13197 For instance, in the following case:
13198
13199 -------- Insert --------
13200 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13201 J_I_ ==> J_I_ `^^' are cursors.
13202 ^^ ^^
13203 -------- --------
13204
13205 As we have to redraw the line above, we cannot use this
13206 optimization. */
13207
13208 struct it it;
13209 int line_height_before = this_line_pixel_height;
13210
13211 /* Note that start_display will handle the case that the
13212 line starting at tlbufpos is a continuation line. */
13213 start_display (&it, w, tlbufpos);
13214
13215 /* Implementation note: It this still necessary? */
13216 if (it.current_x != this_line_start_x)
13217 goto cancel;
13218
13219 TRACE ((stderr, "trying display optimization 1\n"));
13220 w->cursor.vpos = -1;
13221 overlay_arrow_seen = 0;
13222 it.vpos = this_line_vpos;
13223 it.current_y = this_line_y;
13224 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13225 display_line (&it);
13226
13227 /* If line contains point, is not continued,
13228 and ends at same distance from eob as before, we win. */
13229 if (w->cursor.vpos >= 0
13230 /* Line is not continued, otherwise this_line_start_pos
13231 would have been set to 0 in display_line. */
13232 && CHARPOS (this_line_start_pos)
13233 /* Line ends as before. */
13234 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13235 /* Line has same height as before. Otherwise other lines
13236 would have to be shifted up or down. */
13237 && this_line_pixel_height == line_height_before)
13238 {
13239 /* If this is not the window's last line, we must adjust
13240 the charstarts of the lines below. */
13241 if (it.current_y < it.last_visible_y)
13242 {
13243 struct glyph_row *row
13244 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13245 ptrdiff_t delta, delta_bytes;
13246
13247 /* We used to distinguish between two cases here,
13248 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13249 when the line ends in a newline or the end of the
13250 buffer's accessible portion. But both cases did
13251 the same, so they were collapsed. */
13252 delta = (Z
13253 - CHARPOS (tlendpos)
13254 - MATRIX_ROW_START_CHARPOS (row));
13255 delta_bytes = (Z_BYTE
13256 - BYTEPOS (tlendpos)
13257 - MATRIX_ROW_START_BYTEPOS (row));
13258
13259 increment_matrix_positions (w->current_matrix,
13260 this_line_vpos + 1,
13261 w->current_matrix->nrows,
13262 delta, delta_bytes);
13263 }
13264
13265 /* If this row displays text now but previously didn't,
13266 or vice versa, w->window_end_vpos may have to be
13267 adjusted. */
13268 if ((it.glyph_row - 1)->displays_text_p)
13269 {
13270 if (XFASTINT (WVAR (w, window_end_vpos)) < this_line_vpos)
13271 XSETINT (WVAR (w, window_end_vpos), this_line_vpos);
13272 }
13273 else if (XFASTINT (WVAR (w, window_end_vpos)) == this_line_vpos
13274 && this_line_vpos > 0)
13275 XSETINT (WVAR (w, window_end_vpos), this_line_vpos - 1);
13276 WVAR (w, window_end_valid) = Qnil;
13277
13278 /* Update hint: No need to try to scroll in update_window. */
13279 w->desired_matrix->no_scrolling_p = 1;
13280
13281 #ifdef GLYPH_DEBUG
13282 *w->desired_matrix->method = 0;
13283 debug_method_add (w, "optimization 1");
13284 #endif
13285 #ifdef HAVE_WINDOW_SYSTEM
13286 update_window_fringes (w, 0);
13287 #endif
13288 goto update;
13289 }
13290 else
13291 goto cancel;
13292 }
13293 else if (/* Cursor position hasn't changed. */
13294 PT == w->last_point
13295 /* Make sure the cursor was last displayed
13296 in this window. Otherwise we have to reposition it. */
13297 && 0 <= w->cursor.vpos
13298 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13299 {
13300 if (!must_finish)
13301 {
13302 do_pending_window_change (1);
13303 /* If selected_window changed, redisplay again. */
13304 if (WINDOWP (selected_window)
13305 && (w = XWINDOW (selected_window)) != sw)
13306 goto retry;
13307
13308 /* We used to always goto end_of_redisplay here, but this
13309 isn't enough if we have a blinking cursor. */
13310 if (w->cursor_off_p == w->last_cursor_off_p)
13311 goto end_of_redisplay;
13312 }
13313 goto update;
13314 }
13315 /* If highlighting the region, or if the cursor is in the echo area,
13316 then we can't just move the cursor. */
13317 else if (! (!NILP (Vtransient_mark_mode)
13318 && !NILP (BVAR (current_buffer, mark_active)))
13319 && (EQ (selected_window,
13320 BVAR (current_buffer, last_selected_window))
13321 || highlight_nonselected_windows)
13322 && NILP (WVAR (w, region_showing))
13323 && NILP (Vshow_trailing_whitespace)
13324 && !cursor_in_echo_area)
13325 {
13326 struct it it;
13327 struct glyph_row *row;
13328
13329 /* Skip from tlbufpos to PT and see where it is. Note that
13330 PT may be in invisible text. If so, we will end at the
13331 next visible position. */
13332 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13333 NULL, DEFAULT_FACE_ID);
13334 it.current_x = this_line_start_x;
13335 it.current_y = this_line_y;
13336 it.vpos = this_line_vpos;
13337
13338 /* The call to move_it_to stops in front of PT, but
13339 moves over before-strings. */
13340 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13341
13342 if (it.vpos == this_line_vpos
13343 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13344 row->enabled_p))
13345 {
13346 eassert (this_line_vpos == it.vpos);
13347 eassert (this_line_y == it.current_y);
13348 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13349 #ifdef GLYPH_DEBUG
13350 *w->desired_matrix->method = 0;
13351 debug_method_add (w, "optimization 3");
13352 #endif
13353 goto update;
13354 }
13355 else
13356 goto cancel;
13357 }
13358
13359 cancel:
13360 /* Text changed drastically or point moved off of line. */
13361 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13362 }
13363
13364 CHARPOS (this_line_start_pos) = 0;
13365 consider_all_windows_p |= buffer_shared > 1;
13366 ++clear_face_cache_count;
13367 #ifdef HAVE_WINDOW_SYSTEM
13368 ++clear_image_cache_count;
13369 #endif
13370
13371 /* Build desired matrices, and update the display. If
13372 consider_all_windows_p is non-zero, do it for all windows on all
13373 frames. Otherwise do it for selected_window, only. */
13374
13375 if (consider_all_windows_p)
13376 {
13377 Lisp_Object tail, frame;
13378
13379 FOR_EACH_FRAME (tail, frame)
13380 XFRAME (frame)->updated_p = 0;
13381
13382 /* Recompute # windows showing selected buffer. This will be
13383 incremented each time such a window is displayed. */
13384 buffer_shared = 0;
13385
13386 FOR_EACH_FRAME (tail, frame)
13387 {
13388 struct frame *f = XFRAME (frame);
13389
13390 /* We don't have to do anything for unselected terminal
13391 frames. */
13392 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13393 && !EQ (FRAME_TTY (f)->top_frame, frame))
13394 continue;
13395
13396 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13397 {
13398 if (! EQ (frame, selected_frame))
13399 /* Select the frame, for the sake of frame-local
13400 variables. */
13401 select_frame_for_redisplay (frame);
13402
13403 /* Mark all the scroll bars to be removed; we'll redeem
13404 the ones we want when we redisplay their windows. */
13405 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13406 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13407
13408 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13409 redisplay_windows (FRAME_ROOT_WINDOW (f));
13410
13411 /* The X error handler may have deleted that frame. */
13412 if (!FRAME_LIVE_P (f))
13413 continue;
13414
13415 /* Any scroll bars which redisplay_windows should have
13416 nuked should now go away. */
13417 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13418 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13419
13420 /* If fonts changed, display again. */
13421 /* ??? rms: I suspect it is a mistake to jump all the way
13422 back to retry here. It should just retry this frame. */
13423 if (fonts_changed_p)
13424 goto retry;
13425
13426 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13427 {
13428 /* See if we have to hscroll. */
13429 if (!f->already_hscrolled_p)
13430 {
13431 f->already_hscrolled_p = 1;
13432 if (hscroll_windows (FVAR (f, root_window)))
13433 goto retry;
13434 }
13435
13436 /* Prevent various kinds of signals during display
13437 update. stdio is not robust about handling
13438 signals, which can cause an apparent I/O
13439 error. */
13440 if (interrupt_input)
13441 unrequest_sigio ();
13442 STOP_POLLING;
13443
13444 /* Update the display. */
13445 set_window_update_flags (XWINDOW (FVAR (f, root_window)), 1);
13446 pending |= update_frame (f, 0, 0);
13447 f->updated_p = 1;
13448 }
13449 }
13450 }
13451
13452 if (!EQ (old_frame, selected_frame)
13453 && FRAME_LIVE_P (XFRAME (old_frame)))
13454 /* We played a bit fast-and-loose above and allowed selected_frame
13455 and selected_window to be temporarily out-of-sync but let's make
13456 sure this stays contained. */
13457 select_frame_for_redisplay (old_frame);
13458 eassert (EQ (FVAR (XFRAME (selected_frame), selected_window),
13459 selected_window));
13460
13461 if (!pending)
13462 {
13463 /* Do the mark_window_display_accurate after all windows have
13464 been redisplayed because this call resets flags in buffers
13465 which are needed for proper redisplay. */
13466 FOR_EACH_FRAME (tail, frame)
13467 {
13468 struct frame *f = XFRAME (frame);
13469 if (f->updated_p)
13470 {
13471 mark_window_display_accurate (FVAR (f, root_window), 1);
13472 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13473 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13474 }
13475 }
13476 }
13477 }
13478 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13479 {
13480 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13481 struct frame *mini_frame;
13482
13483 displayed_buffer = XBUFFER (WVAR (XWINDOW (selected_window), buffer));
13484 /* Use list_of_error, not Qerror, so that
13485 we catch only errors and don't run the debugger. */
13486 internal_condition_case_1 (redisplay_window_1, selected_window,
13487 list_of_error,
13488 redisplay_window_error);
13489 if (update_miniwindow_p)
13490 internal_condition_case_1 (redisplay_window_1, mini_window,
13491 list_of_error,
13492 redisplay_window_error);
13493
13494 /* Compare desired and current matrices, perform output. */
13495
13496 update:
13497 /* If fonts changed, display again. */
13498 if (fonts_changed_p)
13499 goto retry;
13500
13501 /* Prevent various kinds of signals during display update.
13502 stdio is not robust about handling signals,
13503 which can cause an apparent I/O error. */
13504 if (interrupt_input)
13505 unrequest_sigio ();
13506 STOP_POLLING;
13507
13508 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13509 {
13510 if (hscroll_windows (selected_window))
13511 goto retry;
13512
13513 XWINDOW (selected_window)->must_be_updated_p = 1;
13514 pending = update_frame (sf, 0, 0);
13515 }
13516
13517 /* We may have called echo_area_display at the top of this
13518 function. If the echo area is on another frame, that may
13519 have put text on a frame other than the selected one, so the
13520 above call to update_frame would not have caught it. Catch
13521 it here. */
13522 mini_window = FRAME_MINIBUF_WINDOW (sf);
13523 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13524
13525 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13526 {
13527 XWINDOW (mini_window)->must_be_updated_p = 1;
13528 pending |= update_frame (mini_frame, 0, 0);
13529 if (!pending && hscroll_windows (mini_window))
13530 goto retry;
13531 }
13532 }
13533
13534 /* If display was paused because of pending input, make sure we do a
13535 thorough update the next time. */
13536 if (pending)
13537 {
13538 /* Prevent the optimization at the beginning of
13539 redisplay_internal that tries a single-line update of the
13540 line containing the cursor in the selected window. */
13541 CHARPOS (this_line_start_pos) = 0;
13542
13543 /* Let the overlay arrow be updated the next time. */
13544 update_overlay_arrows (0);
13545
13546 /* If we pause after scrolling, some rows in the current
13547 matrices of some windows are not valid. */
13548 if (!WINDOW_FULL_WIDTH_P (w)
13549 && !FRAME_WINDOW_P (XFRAME (WVAR (w, frame))))
13550 update_mode_lines = 1;
13551 }
13552 else
13553 {
13554 if (!consider_all_windows_p)
13555 {
13556 /* This has already been done above if
13557 consider_all_windows_p is set. */
13558 mark_window_display_accurate_1 (w, 1);
13559
13560 /* Say overlay arrows are up to date. */
13561 update_overlay_arrows (1);
13562
13563 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13564 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13565 }
13566
13567 update_mode_lines = 0;
13568 windows_or_buffers_changed = 0;
13569 cursor_type_changed = 0;
13570 }
13571
13572 /* Start SIGIO interrupts coming again. Having them off during the
13573 code above makes it less likely one will discard output, but not
13574 impossible, since there might be stuff in the system buffer here.
13575 But it is much hairier to try to do anything about that. */
13576 if (interrupt_input)
13577 request_sigio ();
13578 RESUME_POLLING;
13579
13580 /* If a frame has become visible which was not before, redisplay
13581 again, so that we display it. Expose events for such a frame
13582 (which it gets when becoming visible) don't call the parts of
13583 redisplay constructing glyphs, so simply exposing a frame won't
13584 display anything in this case. So, we have to display these
13585 frames here explicitly. */
13586 if (!pending)
13587 {
13588 Lisp_Object tail, frame;
13589 int new_count = 0;
13590
13591 FOR_EACH_FRAME (tail, frame)
13592 {
13593 int this_is_visible = 0;
13594
13595 if (XFRAME (frame)->visible)
13596 this_is_visible = 1;
13597 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13598 if (XFRAME (frame)->visible)
13599 this_is_visible = 1;
13600
13601 if (this_is_visible)
13602 new_count++;
13603 }
13604
13605 if (new_count != number_of_visible_frames)
13606 windows_or_buffers_changed++;
13607 }
13608
13609 /* Change frame size now if a change is pending. */
13610 do_pending_window_change (1);
13611
13612 /* If we just did a pending size change, or have additional
13613 visible frames, or selected_window changed, redisplay again. */
13614 if ((windows_or_buffers_changed && !pending)
13615 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13616 goto retry;
13617
13618 /* Clear the face and image caches.
13619
13620 We used to do this only if consider_all_windows_p. But the cache
13621 needs to be cleared if a timer creates images in the current
13622 buffer (e.g. the test case in Bug#6230). */
13623
13624 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13625 {
13626 clear_face_cache (0);
13627 clear_face_cache_count = 0;
13628 }
13629
13630 #ifdef HAVE_WINDOW_SYSTEM
13631 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13632 {
13633 clear_image_caches (Qnil);
13634 clear_image_cache_count = 0;
13635 }
13636 #endif /* HAVE_WINDOW_SYSTEM */
13637
13638 end_of_redisplay:
13639 unbind_to (count, Qnil);
13640 RESUME_POLLING;
13641 }
13642
13643
13644 /* Redisplay, but leave alone any recent echo area message unless
13645 another message has been requested in its place.
13646
13647 This is useful in situations where you need to redisplay but no
13648 user action has occurred, making it inappropriate for the message
13649 area to be cleared. See tracking_off and
13650 wait_reading_process_output for examples of these situations.
13651
13652 FROM_WHERE is an integer saying from where this function was
13653 called. This is useful for debugging. */
13654
13655 void
13656 redisplay_preserve_echo_area (int from_where)
13657 {
13658 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13659
13660 if (!NILP (echo_area_buffer[1]))
13661 {
13662 /* We have a previously displayed message, but no current
13663 message. Redisplay the previous message. */
13664 display_last_displayed_message_p = 1;
13665 redisplay_internal ();
13666 display_last_displayed_message_p = 0;
13667 }
13668 else
13669 redisplay_internal ();
13670
13671 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13672 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13673 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13674 }
13675
13676
13677 /* Function registered with record_unwind_protect in
13678 redisplay_internal. Reset redisplaying_p to the value it had
13679 before redisplay_internal was called, and clear
13680 prevent_freeing_realized_faces_p. It also selects the previously
13681 selected frame, unless it has been deleted (by an X connection
13682 failure during redisplay, for example). */
13683
13684 static Lisp_Object
13685 unwind_redisplay (Lisp_Object val)
13686 {
13687 Lisp_Object old_redisplaying_p, old_frame;
13688
13689 old_redisplaying_p = XCAR (val);
13690 redisplaying_p = XFASTINT (old_redisplaying_p);
13691 old_frame = XCDR (val);
13692 if (! EQ (old_frame, selected_frame)
13693 && FRAME_LIVE_P (XFRAME (old_frame)))
13694 select_frame_for_redisplay (old_frame);
13695 return Qnil;
13696 }
13697
13698
13699 /* Mark the display of window W as accurate or inaccurate. If
13700 ACCURATE_P is non-zero mark display of W as accurate. If
13701 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13702 redisplay_internal is called. */
13703
13704 static void
13705 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13706 {
13707 if (BUFFERP (WVAR (w, buffer)))
13708 {
13709 struct buffer *b = XBUFFER (WVAR (w, buffer));
13710
13711 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13712 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13713 w->last_had_star
13714 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13715
13716 if (accurate_p)
13717 {
13718 b->clip_changed = 0;
13719 b->prevent_redisplay_optimizations_p = 0;
13720
13721 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13722 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13723 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13724 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13725
13726 w->current_matrix->buffer = b;
13727 w->current_matrix->begv = BUF_BEGV (b);
13728 w->current_matrix->zv = BUF_ZV (b);
13729
13730 w->last_cursor = w->cursor;
13731 w->last_cursor_off_p = w->cursor_off_p;
13732
13733 if (w == XWINDOW (selected_window))
13734 w->last_point = BUF_PT (b);
13735 else
13736 w->last_point = XMARKER (WVAR (w, pointm))->charpos;
13737 }
13738 }
13739
13740 if (accurate_p)
13741 {
13742 WVAR (w, window_end_valid) = WVAR (w, buffer);
13743 w->update_mode_line = 0;
13744 }
13745 }
13746
13747
13748 /* Mark the display of windows in the window tree rooted at WINDOW as
13749 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13750 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13751 be redisplayed the next time redisplay_internal is called. */
13752
13753 void
13754 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13755 {
13756 struct window *w;
13757
13758 for (; !NILP (window); window = WVAR (w, next))
13759 {
13760 w = XWINDOW (window);
13761 mark_window_display_accurate_1 (w, accurate_p);
13762
13763 if (!NILP (WVAR (w, vchild)))
13764 mark_window_display_accurate (WVAR (w, vchild), accurate_p);
13765 if (!NILP (WVAR (w, hchild)))
13766 mark_window_display_accurate (WVAR (w, hchild), accurate_p);
13767 }
13768
13769 if (accurate_p)
13770 {
13771 update_overlay_arrows (1);
13772 }
13773 else
13774 {
13775 /* Force a thorough redisplay the next time by setting
13776 last_arrow_position and last_arrow_string to t, which is
13777 unequal to any useful value of Voverlay_arrow_... */
13778 update_overlay_arrows (-1);
13779 }
13780 }
13781
13782
13783 /* Return value in display table DP (Lisp_Char_Table *) for character
13784 C. Since a display table doesn't have any parent, we don't have to
13785 follow parent. Do not call this function directly but use the
13786 macro DISP_CHAR_VECTOR. */
13787
13788 Lisp_Object
13789 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13790 {
13791 Lisp_Object val;
13792
13793 if (ASCII_CHAR_P (c))
13794 {
13795 val = dp->ascii;
13796 if (SUB_CHAR_TABLE_P (val))
13797 val = XSUB_CHAR_TABLE (val)->contents[c];
13798 }
13799 else
13800 {
13801 Lisp_Object table;
13802
13803 XSETCHAR_TABLE (table, dp);
13804 val = char_table_ref (table, c);
13805 }
13806 if (NILP (val))
13807 val = dp->defalt;
13808 return val;
13809 }
13810
13811
13812 \f
13813 /***********************************************************************
13814 Window Redisplay
13815 ***********************************************************************/
13816
13817 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13818
13819 static void
13820 redisplay_windows (Lisp_Object window)
13821 {
13822 while (!NILP (window))
13823 {
13824 struct window *w = XWINDOW (window);
13825
13826 if (!NILP (WVAR (w, hchild)))
13827 redisplay_windows (WVAR (w, hchild));
13828 else if (!NILP (WVAR (w, vchild)))
13829 redisplay_windows (WVAR (w, vchild));
13830 else if (!NILP (WVAR (w, buffer)))
13831 {
13832 displayed_buffer = XBUFFER (WVAR (w, buffer));
13833 /* Use list_of_error, not Qerror, so that
13834 we catch only errors and don't run the debugger. */
13835 internal_condition_case_1 (redisplay_window_0, window,
13836 list_of_error,
13837 redisplay_window_error);
13838 }
13839
13840 window = WVAR (w, next);
13841 }
13842 }
13843
13844 static Lisp_Object
13845 redisplay_window_error (Lisp_Object ignore)
13846 {
13847 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13848 return Qnil;
13849 }
13850
13851 static Lisp_Object
13852 redisplay_window_0 (Lisp_Object window)
13853 {
13854 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13855 redisplay_window (window, 0);
13856 return Qnil;
13857 }
13858
13859 static Lisp_Object
13860 redisplay_window_1 (Lisp_Object window)
13861 {
13862 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13863 redisplay_window (window, 1);
13864 return Qnil;
13865 }
13866 \f
13867
13868 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13869 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13870 which positions recorded in ROW differ from current buffer
13871 positions.
13872
13873 Return 0 if cursor is not on this row, 1 otherwise. */
13874
13875 static int
13876 set_cursor_from_row (struct window *w, struct glyph_row *row,
13877 struct glyph_matrix *matrix,
13878 ptrdiff_t delta, ptrdiff_t delta_bytes,
13879 int dy, int dvpos)
13880 {
13881 struct glyph *glyph = row->glyphs[TEXT_AREA];
13882 struct glyph *end = glyph + row->used[TEXT_AREA];
13883 struct glyph *cursor = NULL;
13884 /* The last known character position in row. */
13885 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13886 int x = row->x;
13887 ptrdiff_t pt_old = PT - delta;
13888 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13889 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13890 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13891 /* A glyph beyond the edge of TEXT_AREA which we should never
13892 touch. */
13893 struct glyph *glyphs_end = end;
13894 /* Non-zero means we've found a match for cursor position, but that
13895 glyph has the avoid_cursor_p flag set. */
13896 int match_with_avoid_cursor = 0;
13897 /* Non-zero means we've seen at least one glyph that came from a
13898 display string. */
13899 int string_seen = 0;
13900 /* Largest and smallest buffer positions seen so far during scan of
13901 glyph row. */
13902 ptrdiff_t bpos_max = pos_before;
13903 ptrdiff_t bpos_min = pos_after;
13904 /* Last buffer position covered by an overlay string with an integer
13905 `cursor' property. */
13906 ptrdiff_t bpos_covered = 0;
13907 /* Non-zero means the display string on which to display the cursor
13908 comes from a text property, not from an overlay. */
13909 int string_from_text_prop = 0;
13910
13911 /* Don't even try doing anything if called for a mode-line or
13912 header-line row, since the rest of the code isn't prepared to
13913 deal with such calamities. */
13914 eassert (!row->mode_line_p);
13915 if (row->mode_line_p)
13916 return 0;
13917
13918 /* Skip over glyphs not having an object at the start and the end of
13919 the row. These are special glyphs like truncation marks on
13920 terminal frames. */
13921 if (row->displays_text_p)
13922 {
13923 if (!row->reversed_p)
13924 {
13925 while (glyph < end
13926 && INTEGERP (glyph->object)
13927 && glyph->charpos < 0)
13928 {
13929 x += glyph->pixel_width;
13930 ++glyph;
13931 }
13932 while (end > glyph
13933 && INTEGERP ((end - 1)->object)
13934 /* CHARPOS is zero for blanks and stretch glyphs
13935 inserted by extend_face_to_end_of_line. */
13936 && (end - 1)->charpos <= 0)
13937 --end;
13938 glyph_before = glyph - 1;
13939 glyph_after = end;
13940 }
13941 else
13942 {
13943 struct glyph *g;
13944
13945 /* If the glyph row is reversed, we need to process it from back
13946 to front, so swap the edge pointers. */
13947 glyphs_end = end = glyph - 1;
13948 glyph += row->used[TEXT_AREA] - 1;
13949
13950 while (glyph > end + 1
13951 && INTEGERP (glyph->object)
13952 && glyph->charpos < 0)
13953 {
13954 --glyph;
13955 x -= glyph->pixel_width;
13956 }
13957 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13958 --glyph;
13959 /* By default, in reversed rows we put the cursor on the
13960 rightmost (first in the reading order) glyph. */
13961 for (g = end + 1; g < glyph; g++)
13962 x += g->pixel_width;
13963 while (end < glyph
13964 && INTEGERP ((end + 1)->object)
13965 && (end + 1)->charpos <= 0)
13966 ++end;
13967 glyph_before = glyph + 1;
13968 glyph_after = end;
13969 }
13970 }
13971 else if (row->reversed_p)
13972 {
13973 /* In R2L rows that don't display text, put the cursor on the
13974 rightmost glyph. Case in point: an empty last line that is
13975 part of an R2L paragraph. */
13976 cursor = end - 1;
13977 /* Avoid placing the cursor on the last glyph of the row, where
13978 on terminal frames we hold the vertical border between
13979 adjacent windows. */
13980 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13981 && !WINDOW_RIGHTMOST_P (w)
13982 && cursor == row->glyphs[LAST_AREA] - 1)
13983 cursor--;
13984 x = -1; /* will be computed below, at label compute_x */
13985 }
13986
13987 /* Step 1: Try to find the glyph whose character position
13988 corresponds to point. If that's not possible, find 2 glyphs
13989 whose character positions are the closest to point, one before
13990 point, the other after it. */
13991 if (!row->reversed_p)
13992 while (/* not marched to end of glyph row */
13993 glyph < end
13994 /* glyph was not inserted by redisplay for internal purposes */
13995 && !INTEGERP (glyph->object))
13996 {
13997 if (BUFFERP (glyph->object))
13998 {
13999 ptrdiff_t dpos = glyph->charpos - pt_old;
14000
14001 if (glyph->charpos > bpos_max)
14002 bpos_max = glyph->charpos;
14003 if (glyph->charpos < bpos_min)
14004 bpos_min = glyph->charpos;
14005 if (!glyph->avoid_cursor_p)
14006 {
14007 /* If we hit point, we've found the glyph on which to
14008 display the cursor. */
14009 if (dpos == 0)
14010 {
14011 match_with_avoid_cursor = 0;
14012 break;
14013 }
14014 /* See if we've found a better approximation to
14015 POS_BEFORE or to POS_AFTER. */
14016 if (0 > dpos && dpos > pos_before - pt_old)
14017 {
14018 pos_before = glyph->charpos;
14019 glyph_before = glyph;
14020 }
14021 else if (0 < dpos && dpos < pos_after - pt_old)
14022 {
14023 pos_after = glyph->charpos;
14024 glyph_after = glyph;
14025 }
14026 }
14027 else if (dpos == 0)
14028 match_with_avoid_cursor = 1;
14029 }
14030 else if (STRINGP (glyph->object))
14031 {
14032 Lisp_Object chprop;
14033 ptrdiff_t glyph_pos = glyph->charpos;
14034
14035 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14036 glyph->object);
14037 if (!NILP (chprop))
14038 {
14039 /* If the string came from a `display' text property,
14040 look up the buffer position of that property and
14041 use that position to update bpos_max, as if we
14042 actually saw such a position in one of the row's
14043 glyphs. This helps with supporting integer values
14044 of `cursor' property on the display string in
14045 situations where most or all of the row's buffer
14046 text is completely covered by display properties,
14047 so that no glyph with valid buffer positions is
14048 ever seen in the row. */
14049 ptrdiff_t prop_pos =
14050 string_buffer_position_lim (glyph->object, pos_before,
14051 pos_after, 0);
14052
14053 if (prop_pos >= pos_before)
14054 bpos_max = prop_pos - 1;
14055 }
14056 if (INTEGERP (chprop))
14057 {
14058 bpos_covered = bpos_max + XINT (chprop);
14059 /* If the `cursor' property covers buffer positions up
14060 to and including point, we should display cursor on
14061 this glyph. Note that, if a `cursor' property on one
14062 of the string's characters has an integer value, we
14063 will break out of the loop below _before_ we get to
14064 the position match above. IOW, integer values of
14065 the `cursor' property override the "exact match for
14066 point" strategy of positioning the cursor. */
14067 /* Implementation note: bpos_max == pt_old when, e.g.,
14068 we are in an empty line, where bpos_max is set to
14069 MATRIX_ROW_START_CHARPOS, see above. */
14070 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14071 {
14072 cursor = glyph;
14073 break;
14074 }
14075 }
14076
14077 string_seen = 1;
14078 }
14079 x += glyph->pixel_width;
14080 ++glyph;
14081 }
14082 else if (glyph > end) /* row is reversed */
14083 while (!INTEGERP (glyph->object))
14084 {
14085 if (BUFFERP (glyph->object))
14086 {
14087 ptrdiff_t dpos = glyph->charpos - pt_old;
14088
14089 if (glyph->charpos > bpos_max)
14090 bpos_max = glyph->charpos;
14091 if (glyph->charpos < bpos_min)
14092 bpos_min = glyph->charpos;
14093 if (!glyph->avoid_cursor_p)
14094 {
14095 if (dpos == 0)
14096 {
14097 match_with_avoid_cursor = 0;
14098 break;
14099 }
14100 if (0 > dpos && dpos > pos_before - pt_old)
14101 {
14102 pos_before = glyph->charpos;
14103 glyph_before = glyph;
14104 }
14105 else if (0 < dpos && dpos < pos_after - pt_old)
14106 {
14107 pos_after = glyph->charpos;
14108 glyph_after = glyph;
14109 }
14110 }
14111 else if (dpos == 0)
14112 match_with_avoid_cursor = 1;
14113 }
14114 else if (STRINGP (glyph->object))
14115 {
14116 Lisp_Object chprop;
14117 ptrdiff_t glyph_pos = glyph->charpos;
14118
14119 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14120 glyph->object);
14121 if (!NILP (chprop))
14122 {
14123 ptrdiff_t prop_pos =
14124 string_buffer_position_lim (glyph->object, pos_before,
14125 pos_after, 0);
14126
14127 if (prop_pos >= pos_before)
14128 bpos_max = prop_pos - 1;
14129 }
14130 if (INTEGERP (chprop))
14131 {
14132 bpos_covered = bpos_max + XINT (chprop);
14133 /* If the `cursor' property covers buffer positions up
14134 to and including point, we should display cursor on
14135 this glyph. */
14136 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14137 {
14138 cursor = glyph;
14139 break;
14140 }
14141 }
14142 string_seen = 1;
14143 }
14144 --glyph;
14145 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14146 {
14147 x--; /* can't use any pixel_width */
14148 break;
14149 }
14150 x -= glyph->pixel_width;
14151 }
14152
14153 /* Step 2: If we didn't find an exact match for point, we need to
14154 look for a proper place to put the cursor among glyphs between
14155 GLYPH_BEFORE and GLYPH_AFTER. */
14156 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14157 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14158 && bpos_covered < pt_old)
14159 {
14160 /* An empty line has a single glyph whose OBJECT is zero and
14161 whose CHARPOS is the position of a newline on that line.
14162 Note that on a TTY, there are more glyphs after that, which
14163 were produced by extend_face_to_end_of_line, but their
14164 CHARPOS is zero or negative. */
14165 int empty_line_p =
14166 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14167 && INTEGERP (glyph->object) && glyph->charpos > 0;
14168
14169 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14170 {
14171 ptrdiff_t ellipsis_pos;
14172
14173 /* Scan back over the ellipsis glyphs. */
14174 if (!row->reversed_p)
14175 {
14176 ellipsis_pos = (glyph - 1)->charpos;
14177 while (glyph > row->glyphs[TEXT_AREA]
14178 && (glyph - 1)->charpos == ellipsis_pos)
14179 glyph--, x -= glyph->pixel_width;
14180 /* That loop always goes one position too far, including
14181 the glyph before the ellipsis. So scan forward over
14182 that one. */
14183 x += glyph->pixel_width;
14184 glyph++;
14185 }
14186 else /* row is reversed */
14187 {
14188 ellipsis_pos = (glyph + 1)->charpos;
14189 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14190 && (glyph + 1)->charpos == ellipsis_pos)
14191 glyph++, x += glyph->pixel_width;
14192 x -= glyph->pixel_width;
14193 glyph--;
14194 }
14195 }
14196 else if (match_with_avoid_cursor)
14197 {
14198 cursor = glyph_after;
14199 x = -1;
14200 }
14201 else if (string_seen)
14202 {
14203 int incr = row->reversed_p ? -1 : +1;
14204
14205 /* Need to find the glyph that came out of a string which is
14206 present at point. That glyph is somewhere between
14207 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14208 positioned between POS_BEFORE and POS_AFTER in the
14209 buffer. */
14210 struct glyph *start, *stop;
14211 ptrdiff_t pos = pos_before;
14212
14213 x = -1;
14214
14215 /* If the row ends in a newline from a display string,
14216 reordering could have moved the glyphs belonging to the
14217 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14218 in this case we extend the search to the last glyph in
14219 the row that was not inserted by redisplay. */
14220 if (row->ends_in_newline_from_string_p)
14221 {
14222 glyph_after = end;
14223 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14224 }
14225
14226 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14227 correspond to POS_BEFORE and POS_AFTER, respectively. We
14228 need START and STOP in the order that corresponds to the
14229 row's direction as given by its reversed_p flag. If the
14230 directionality of characters between POS_BEFORE and
14231 POS_AFTER is the opposite of the row's base direction,
14232 these characters will have been reordered for display,
14233 and we need to reverse START and STOP. */
14234 if (!row->reversed_p)
14235 {
14236 start = min (glyph_before, glyph_after);
14237 stop = max (glyph_before, glyph_after);
14238 }
14239 else
14240 {
14241 start = max (glyph_before, glyph_after);
14242 stop = min (glyph_before, glyph_after);
14243 }
14244 for (glyph = start + incr;
14245 row->reversed_p ? glyph > stop : glyph < stop; )
14246 {
14247
14248 /* Any glyphs that come from the buffer are here because
14249 of bidi reordering. Skip them, and only pay
14250 attention to glyphs that came from some string. */
14251 if (STRINGP (glyph->object))
14252 {
14253 Lisp_Object str;
14254 ptrdiff_t tem;
14255 /* If the display property covers the newline, we
14256 need to search for it one position farther. */
14257 ptrdiff_t lim = pos_after
14258 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14259
14260 string_from_text_prop = 0;
14261 str = glyph->object;
14262 tem = string_buffer_position_lim (str, pos, lim, 0);
14263 if (tem == 0 /* from overlay */
14264 || pos <= tem)
14265 {
14266 /* If the string from which this glyph came is
14267 found in the buffer at point, or at position
14268 that is closer to point than pos_after, then
14269 we've found the glyph we've been looking for.
14270 If it comes from an overlay (tem == 0), and
14271 it has the `cursor' property on one of its
14272 glyphs, record that glyph as a candidate for
14273 displaying the cursor. (As in the
14274 unidirectional version, we will display the
14275 cursor on the last candidate we find.) */
14276 if (tem == 0
14277 || tem == pt_old
14278 || (tem - pt_old > 0 && tem < pos_after))
14279 {
14280 /* The glyphs from this string could have
14281 been reordered. Find the one with the
14282 smallest string position. Or there could
14283 be a character in the string with the
14284 `cursor' property, which means display
14285 cursor on that character's glyph. */
14286 ptrdiff_t strpos = glyph->charpos;
14287
14288 if (tem)
14289 {
14290 cursor = glyph;
14291 string_from_text_prop = 1;
14292 }
14293 for ( ;
14294 (row->reversed_p ? glyph > stop : glyph < stop)
14295 && EQ (glyph->object, str);
14296 glyph += incr)
14297 {
14298 Lisp_Object cprop;
14299 ptrdiff_t gpos = glyph->charpos;
14300
14301 cprop = Fget_char_property (make_number (gpos),
14302 Qcursor,
14303 glyph->object);
14304 if (!NILP (cprop))
14305 {
14306 cursor = glyph;
14307 break;
14308 }
14309 if (tem && glyph->charpos < strpos)
14310 {
14311 strpos = glyph->charpos;
14312 cursor = glyph;
14313 }
14314 }
14315
14316 if (tem == pt_old
14317 || (tem - pt_old > 0 && tem < pos_after))
14318 goto compute_x;
14319 }
14320 if (tem)
14321 pos = tem + 1; /* don't find previous instances */
14322 }
14323 /* This string is not what we want; skip all of the
14324 glyphs that came from it. */
14325 while ((row->reversed_p ? glyph > stop : glyph < stop)
14326 && EQ (glyph->object, str))
14327 glyph += incr;
14328 }
14329 else
14330 glyph += incr;
14331 }
14332
14333 /* If we reached the end of the line, and END was from a string,
14334 the cursor is not on this line. */
14335 if (cursor == NULL
14336 && (row->reversed_p ? glyph <= end : glyph >= end)
14337 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14338 && STRINGP (end->object)
14339 && row->continued_p)
14340 return 0;
14341 }
14342 /* A truncated row may not include PT among its character positions.
14343 Setting the cursor inside the scroll margin will trigger
14344 recalculation of hscroll in hscroll_window_tree. But if a
14345 display string covers point, defer to the string-handling
14346 code below to figure this out. */
14347 else if (row->truncated_on_left_p && pt_old < bpos_min)
14348 {
14349 cursor = glyph_before;
14350 x = -1;
14351 }
14352 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14353 /* Zero-width characters produce no glyphs. */
14354 || (!empty_line_p
14355 && (row->reversed_p
14356 ? glyph_after > glyphs_end
14357 : glyph_after < glyphs_end)))
14358 {
14359 cursor = glyph_after;
14360 x = -1;
14361 }
14362 }
14363
14364 compute_x:
14365 if (cursor != NULL)
14366 glyph = cursor;
14367 else if (glyph == glyphs_end
14368 && pos_before == pos_after
14369 && STRINGP ((row->reversed_p
14370 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14371 : row->glyphs[TEXT_AREA])->object))
14372 {
14373 /* If all the glyphs of this row came from strings, put the
14374 cursor on the first glyph of the row. This avoids having the
14375 cursor outside of the text area in this very rare and hard
14376 use case. */
14377 glyph =
14378 row->reversed_p
14379 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14380 : row->glyphs[TEXT_AREA];
14381 }
14382 if (x < 0)
14383 {
14384 struct glyph *g;
14385
14386 /* Need to compute x that corresponds to GLYPH. */
14387 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14388 {
14389 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14390 abort ();
14391 x += g->pixel_width;
14392 }
14393 }
14394
14395 /* ROW could be part of a continued line, which, under bidi
14396 reordering, might have other rows whose start and end charpos
14397 occlude point. Only set w->cursor if we found a better
14398 approximation to the cursor position than we have from previously
14399 examined candidate rows belonging to the same continued line. */
14400 if (/* we already have a candidate row */
14401 w->cursor.vpos >= 0
14402 /* that candidate is not the row we are processing */
14403 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14404 /* Make sure cursor.vpos specifies a row whose start and end
14405 charpos occlude point, and it is valid candidate for being a
14406 cursor-row. This is because some callers of this function
14407 leave cursor.vpos at the row where the cursor was displayed
14408 during the last redisplay cycle. */
14409 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14410 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14411 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14412 {
14413 struct glyph *g1 =
14414 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14415
14416 /* Don't consider glyphs that are outside TEXT_AREA. */
14417 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14418 return 0;
14419 /* Keep the candidate whose buffer position is the closest to
14420 point or has the `cursor' property. */
14421 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14422 w->cursor.hpos >= 0
14423 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14424 && ((BUFFERP (g1->object)
14425 && (g1->charpos == pt_old /* an exact match always wins */
14426 || (BUFFERP (glyph->object)
14427 && eabs (g1->charpos - pt_old)
14428 < eabs (glyph->charpos - pt_old))))
14429 /* previous candidate is a glyph from a string that has
14430 a non-nil `cursor' property */
14431 || (STRINGP (g1->object)
14432 && (!NILP (Fget_char_property (make_number (g1->charpos),
14433 Qcursor, g1->object))
14434 /* previous candidate is from the same display
14435 string as this one, and the display string
14436 came from a text property */
14437 || (EQ (g1->object, glyph->object)
14438 && string_from_text_prop)
14439 /* this candidate is from newline and its
14440 position is not an exact match */
14441 || (INTEGERP (glyph->object)
14442 && glyph->charpos != pt_old)))))
14443 return 0;
14444 /* If this candidate gives an exact match, use that. */
14445 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14446 /* If this candidate is a glyph created for the
14447 terminating newline of a line, and point is on that
14448 newline, it wins because it's an exact match. */
14449 || (!row->continued_p
14450 && INTEGERP (glyph->object)
14451 && glyph->charpos == 0
14452 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14453 /* Otherwise, keep the candidate that comes from a row
14454 spanning less buffer positions. This may win when one or
14455 both candidate positions are on glyphs that came from
14456 display strings, for which we cannot compare buffer
14457 positions. */
14458 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14459 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14460 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14461 return 0;
14462 }
14463 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14464 w->cursor.x = x;
14465 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14466 w->cursor.y = row->y + dy;
14467
14468 if (w == XWINDOW (selected_window))
14469 {
14470 if (!row->continued_p
14471 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14472 && row->x == 0)
14473 {
14474 this_line_buffer = XBUFFER (WVAR (w, buffer));
14475
14476 CHARPOS (this_line_start_pos)
14477 = MATRIX_ROW_START_CHARPOS (row) + delta;
14478 BYTEPOS (this_line_start_pos)
14479 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14480
14481 CHARPOS (this_line_end_pos)
14482 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14483 BYTEPOS (this_line_end_pos)
14484 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14485
14486 this_line_y = w->cursor.y;
14487 this_line_pixel_height = row->height;
14488 this_line_vpos = w->cursor.vpos;
14489 this_line_start_x = row->x;
14490 }
14491 else
14492 CHARPOS (this_line_start_pos) = 0;
14493 }
14494
14495 return 1;
14496 }
14497
14498
14499 /* Run window scroll functions, if any, for WINDOW with new window
14500 start STARTP. Sets the window start of WINDOW to that position.
14501
14502 We assume that the window's buffer is really current. */
14503
14504 static inline struct text_pos
14505 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14506 {
14507 struct window *w = XWINDOW (window);
14508 SET_MARKER_FROM_TEXT_POS (WVAR (w, start), startp);
14509
14510 if (current_buffer != XBUFFER (WVAR (w, buffer)))
14511 abort ();
14512
14513 if (!NILP (Vwindow_scroll_functions))
14514 {
14515 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14516 make_number (CHARPOS (startp)));
14517 SET_TEXT_POS_FROM_MARKER (startp, WVAR (w, start));
14518 /* In case the hook functions switch buffers. */
14519 if (current_buffer != XBUFFER (WVAR (w, buffer)))
14520 set_buffer_internal_1 (XBUFFER (WVAR (w, buffer)));
14521 }
14522
14523 return startp;
14524 }
14525
14526
14527 /* Make sure the line containing the cursor is fully visible.
14528 A value of 1 means there is nothing to be done.
14529 (Either the line is fully visible, or it cannot be made so,
14530 or we cannot tell.)
14531
14532 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14533 is higher than window.
14534
14535 A value of 0 means the caller should do scrolling
14536 as if point had gone off the screen. */
14537
14538 static int
14539 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14540 {
14541 struct glyph_matrix *matrix;
14542 struct glyph_row *row;
14543 int window_height;
14544
14545 if (!make_cursor_line_fully_visible_p)
14546 return 1;
14547
14548 /* It's not always possible to find the cursor, e.g, when a window
14549 is full of overlay strings. Don't do anything in that case. */
14550 if (w->cursor.vpos < 0)
14551 return 1;
14552
14553 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14554 row = MATRIX_ROW (matrix, w->cursor.vpos);
14555
14556 /* If the cursor row is not partially visible, there's nothing to do. */
14557 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14558 return 1;
14559
14560 /* If the row the cursor is in is taller than the window's height,
14561 it's not clear what to do, so do nothing. */
14562 window_height = window_box_height (w);
14563 if (row->height >= window_height)
14564 {
14565 if (!force_p || MINI_WINDOW_P (w)
14566 || w->vscroll || w->cursor.vpos == 0)
14567 return 1;
14568 }
14569 return 0;
14570 }
14571
14572
14573 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14574 non-zero means only WINDOW is redisplayed in redisplay_internal.
14575 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14576 in redisplay_window to bring a partially visible line into view in
14577 the case that only the cursor has moved.
14578
14579 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14580 last screen line's vertical height extends past the end of the screen.
14581
14582 Value is
14583
14584 1 if scrolling succeeded
14585
14586 0 if scrolling didn't find point.
14587
14588 -1 if new fonts have been loaded so that we must interrupt
14589 redisplay, adjust glyph matrices, and try again. */
14590
14591 enum
14592 {
14593 SCROLLING_SUCCESS,
14594 SCROLLING_FAILED,
14595 SCROLLING_NEED_LARGER_MATRICES
14596 };
14597
14598 /* If scroll-conservatively is more than this, never recenter.
14599
14600 If you change this, don't forget to update the doc string of
14601 `scroll-conservatively' and the Emacs manual. */
14602 #define SCROLL_LIMIT 100
14603
14604 static int
14605 try_scrolling (Lisp_Object window, int just_this_one_p,
14606 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14607 int temp_scroll_step, int last_line_misfit)
14608 {
14609 struct window *w = XWINDOW (window);
14610 struct frame *f = XFRAME (WVAR (w, frame));
14611 struct text_pos pos, startp;
14612 struct it it;
14613 int this_scroll_margin, scroll_max, rc, height;
14614 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14615 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14616 Lisp_Object aggressive;
14617 /* We will never try scrolling more than this number of lines. */
14618 int scroll_limit = SCROLL_LIMIT;
14619
14620 #ifdef GLYPH_DEBUG
14621 debug_method_add (w, "try_scrolling");
14622 #endif
14623
14624 SET_TEXT_POS_FROM_MARKER (startp, WVAR (w, start));
14625
14626 /* Compute scroll margin height in pixels. We scroll when point is
14627 within this distance from the top or bottom of the window. */
14628 if (scroll_margin > 0)
14629 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14630 * FRAME_LINE_HEIGHT (f);
14631 else
14632 this_scroll_margin = 0;
14633
14634 /* Force arg_scroll_conservatively to have a reasonable value, to
14635 avoid scrolling too far away with slow move_it_* functions. Note
14636 that the user can supply scroll-conservatively equal to
14637 `most-positive-fixnum', which can be larger than INT_MAX. */
14638 if (arg_scroll_conservatively > scroll_limit)
14639 {
14640 arg_scroll_conservatively = scroll_limit + 1;
14641 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14642 }
14643 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14644 /* Compute how much we should try to scroll maximally to bring
14645 point into view. */
14646 scroll_max = (max (scroll_step,
14647 max (arg_scroll_conservatively, temp_scroll_step))
14648 * FRAME_LINE_HEIGHT (f));
14649 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14650 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14651 /* We're trying to scroll because of aggressive scrolling but no
14652 scroll_step is set. Choose an arbitrary one. */
14653 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14654 else
14655 scroll_max = 0;
14656
14657 too_near_end:
14658
14659 /* Decide whether to scroll down. */
14660 if (PT > CHARPOS (startp))
14661 {
14662 int scroll_margin_y;
14663
14664 /* Compute the pixel ypos of the scroll margin, then move IT to
14665 either that ypos or PT, whichever comes first. */
14666 start_display (&it, w, startp);
14667 scroll_margin_y = it.last_visible_y - this_scroll_margin
14668 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14669 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14670 (MOVE_TO_POS | MOVE_TO_Y));
14671
14672 if (PT > CHARPOS (it.current.pos))
14673 {
14674 int y0 = line_bottom_y (&it);
14675 /* Compute how many pixels below window bottom to stop searching
14676 for PT. This avoids costly search for PT that is far away if
14677 the user limited scrolling by a small number of lines, but
14678 always finds PT if scroll_conservatively is set to a large
14679 number, such as most-positive-fixnum. */
14680 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14681 int y_to_move = it.last_visible_y + slack;
14682
14683 /* Compute the distance from the scroll margin to PT or to
14684 the scroll limit, whichever comes first. This should
14685 include the height of the cursor line, to make that line
14686 fully visible. */
14687 move_it_to (&it, PT, -1, y_to_move,
14688 -1, MOVE_TO_POS | MOVE_TO_Y);
14689 dy = line_bottom_y (&it) - y0;
14690
14691 if (dy > scroll_max)
14692 return SCROLLING_FAILED;
14693
14694 if (dy > 0)
14695 scroll_down_p = 1;
14696 }
14697 }
14698
14699 if (scroll_down_p)
14700 {
14701 /* Point is in or below the bottom scroll margin, so move the
14702 window start down. If scrolling conservatively, move it just
14703 enough down to make point visible. If scroll_step is set,
14704 move it down by scroll_step. */
14705 if (arg_scroll_conservatively)
14706 amount_to_scroll
14707 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14708 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14709 else if (scroll_step || temp_scroll_step)
14710 amount_to_scroll = scroll_max;
14711 else
14712 {
14713 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14714 height = WINDOW_BOX_TEXT_HEIGHT (w);
14715 if (NUMBERP (aggressive))
14716 {
14717 double float_amount = XFLOATINT (aggressive) * height;
14718 amount_to_scroll = float_amount;
14719 if (amount_to_scroll == 0 && float_amount > 0)
14720 amount_to_scroll = 1;
14721 /* Don't let point enter the scroll margin near top of
14722 the window. */
14723 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14724 amount_to_scroll = height - 2*this_scroll_margin + dy;
14725 }
14726 }
14727
14728 if (amount_to_scroll <= 0)
14729 return SCROLLING_FAILED;
14730
14731 start_display (&it, w, startp);
14732 if (arg_scroll_conservatively <= scroll_limit)
14733 move_it_vertically (&it, amount_to_scroll);
14734 else
14735 {
14736 /* Extra precision for users who set scroll-conservatively
14737 to a large number: make sure the amount we scroll
14738 the window start is never less than amount_to_scroll,
14739 which was computed as distance from window bottom to
14740 point. This matters when lines at window top and lines
14741 below window bottom have different height. */
14742 struct it it1;
14743 void *it1data = NULL;
14744 /* We use a temporary it1 because line_bottom_y can modify
14745 its argument, if it moves one line down; see there. */
14746 int start_y;
14747
14748 SAVE_IT (it1, it, it1data);
14749 start_y = line_bottom_y (&it1);
14750 do {
14751 RESTORE_IT (&it, &it, it1data);
14752 move_it_by_lines (&it, 1);
14753 SAVE_IT (it1, it, it1data);
14754 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14755 }
14756
14757 /* If STARTP is unchanged, move it down another screen line. */
14758 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14759 move_it_by_lines (&it, 1);
14760 startp = it.current.pos;
14761 }
14762 else
14763 {
14764 struct text_pos scroll_margin_pos = startp;
14765
14766 /* See if point is inside the scroll margin at the top of the
14767 window. */
14768 if (this_scroll_margin)
14769 {
14770 start_display (&it, w, startp);
14771 move_it_vertically (&it, this_scroll_margin);
14772 scroll_margin_pos = it.current.pos;
14773 }
14774
14775 if (PT < CHARPOS (scroll_margin_pos))
14776 {
14777 /* Point is in the scroll margin at the top of the window or
14778 above what is displayed in the window. */
14779 int y0, y_to_move;
14780
14781 /* Compute the vertical distance from PT to the scroll
14782 margin position. Move as far as scroll_max allows, or
14783 one screenful, or 10 screen lines, whichever is largest.
14784 Give up if distance is greater than scroll_max. */
14785 SET_TEXT_POS (pos, PT, PT_BYTE);
14786 start_display (&it, w, pos);
14787 y0 = it.current_y;
14788 y_to_move = max (it.last_visible_y,
14789 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14790 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14791 y_to_move, -1,
14792 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14793 dy = it.current_y - y0;
14794 if (dy > scroll_max)
14795 return SCROLLING_FAILED;
14796
14797 /* Compute new window start. */
14798 start_display (&it, w, startp);
14799
14800 if (arg_scroll_conservatively)
14801 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14802 max (scroll_step, temp_scroll_step));
14803 else if (scroll_step || temp_scroll_step)
14804 amount_to_scroll = scroll_max;
14805 else
14806 {
14807 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14808 height = WINDOW_BOX_TEXT_HEIGHT (w);
14809 if (NUMBERP (aggressive))
14810 {
14811 double float_amount = XFLOATINT (aggressive) * height;
14812 amount_to_scroll = float_amount;
14813 if (amount_to_scroll == 0 && float_amount > 0)
14814 amount_to_scroll = 1;
14815 amount_to_scroll -=
14816 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14817 /* Don't let point enter the scroll margin near
14818 bottom of the window. */
14819 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14820 amount_to_scroll = height - 2*this_scroll_margin + dy;
14821 }
14822 }
14823
14824 if (amount_to_scroll <= 0)
14825 return SCROLLING_FAILED;
14826
14827 move_it_vertically_backward (&it, amount_to_scroll);
14828 startp = it.current.pos;
14829 }
14830 }
14831
14832 /* Run window scroll functions. */
14833 startp = run_window_scroll_functions (window, startp);
14834
14835 /* Display the window. Give up if new fonts are loaded, or if point
14836 doesn't appear. */
14837 if (!try_window (window, startp, 0))
14838 rc = SCROLLING_NEED_LARGER_MATRICES;
14839 else if (w->cursor.vpos < 0)
14840 {
14841 clear_glyph_matrix (w->desired_matrix);
14842 rc = SCROLLING_FAILED;
14843 }
14844 else
14845 {
14846 /* Maybe forget recorded base line for line number display. */
14847 if (!just_this_one_p
14848 || current_buffer->clip_changed
14849 || BEG_UNCHANGED < CHARPOS (startp))
14850 WVAR (w, base_line_number) = Qnil;
14851
14852 /* If cursor ends up on a partially visible line,
14853 treat that as being off the bottom of the screen. */
14854 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14855 /* It's possible that the cursor is on the first line of the
14856 buffer, which is partially obscured due to a vscroll
14857 (Bug#7537). In that case, avoid looping forever . */
14858 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14859 {
14860 clear_glyph_matrix (w->desired_matrix);
14861 ++extra_scroll_margin_lines;
14862 goto too_near_end;
14863 }
14864 rc = SCROLLING_SUCCESS;
14865 }
14866
14867 return rc;
14868 }
14869
14870
14871 /* Compute a suitable window start for window W if display of W starts
14872 on a continuation line. Value is non-zero if a new window start
14873 was computed.
14874
14875 The new window start will be computed, based on W's width, starting
14876 from the start of the continued line. It is the start of the
14877 screen line with the minimum distance from the old start W->start. */
14878
14879 static int
14880 compute_window_start_on_continuation_line (struct window *w)
14881 {
14882 struct text_pos pos, start_pos;
14883 int window_start_changed_p = 0;
14884
14885 SET_TEXT_POS_FROM_MARKER (start_pos, WVAR (w, start));
14886
14887 /* If window start is on a continuation line... Window start may be
14888 < BEGV in case there's invisible text at the start of the
14889 buffer (M-x rmail, for example). */
14890 if (CHARPOS (start_pos) > BEGV
14891 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14892 {
14893 struct it it;
14894 struct glyph_row *row;
14895
14896 /* Handle the case that the window start is out of range. */
14897 if (CHARPOS (start_pos) < BEGV)
14898 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14899 else if (CHARPOS (start_pos) > ZV)
14900 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14901
14902 /* Find the start of the continued line. This should be fast
14903 because scan_buffer is fast (newline cache). */
14904 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14905 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14906 row, DEFAULT_FACE_ID);
14907 reseat_at_previous_visible_line_start (&it);
14908
14909 /* If the line start is "too far" away from the window start,
14910 say it takes too much time to compute a new window start. */
14911 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14912 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14913 {
14914 int min_distance, distance;
14915
14916 /* Move forward by display lines to find the new window
14917 start. If window width was enlarged, the new start can
14918 be expected to be > the old start. If window width was
14919 decreased, the new window start will be < the old start.
14920 So, we're looking for the display line start with the
14921 minimum distance from the old window start. */
14922 pos = it.current.pos;
14923 min_distance = INFINITY;
14924 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14925 distance < min_distance)
14926 {
14927 min_distance = distance;
14928 pos = it.current.pos;
14929 move_it_by_lines (&it, 1);
14930 }
14931
14932 /* Set the window start there. */
14933 SET_MARKER_FROM_TEXT_POS (WVAR (w, start), pos);
14934 window_start_changed_p = 1;
14935 }
14936 }
14937
14938 return window_start_changed_p;
14939 }
14940
14941
14942 /* Try cursor movement in case text has not changed in window WINDOW,
14943 with window start STARTP. Value is
14944
14945 CURSOR_MOVEMENT_SUCCESS if successful
14946
14947 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14948
14949 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14950 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14951 we want to scroll as if scroll-step were set to 1. See the code.
14952
14953 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14954 which case we have to abort this redisplay, and adjust matrices
14955 first. */
14956
14957 enum
14958 {
14959 CURSOR_MOVEMENT_SUCCESS,
14960 CURSOR_MOVEMENT_CANNOT_BE_USED,
14961 CURSOR_MOVEMENT_MUST_SCROLL,
14962 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14963 };
14964
14965 static int
14966 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14967 {
14968 struct window *w = XWINDOW (window);
14969 struct frame *f = XFRAME (WVAR (w, frame));
14970 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14971
14972 #ifdef GLYPH_DEBUG
14973 if (inhibit_try_cursor_movement)
14974 return rc;
14975 #endif
14976
14977 /* Previously, there was a check for Lisp integer in the
14978 if-statement below. Now, this field is converted to
14979 ptrdiff_t, thus zero means invalid position in a buffer. */
14980 eassert (w->last_point > 0);
14981
14982 /* Handle case where text has not changed, only point, and it has
14983 not moved off the frame. */
14984 if (/* Point may be in this window. */
14985 PT >= CHARPOS (startp)
14986 /* Selective display hasn't changed. */
14987 && !current_buffer->clip_changed
14988 /* Function force-mode-line-update is used to force a thorough
14989 redisplay. It sets either windows_or_buffers_changed or
14990 update_mode_lines. So don't take a shortcut here for these
14991 cases. */
14992 && !update_mode_lines
14993 && !windows_or_buffers_changed
14994 && !cursor_type_changed
14995 /* Can't use this case if highlighting a region. When a
14996 region exists, cursor movement has to do more than just
14997 set the cursor. */
14998 && !(!NILP (Vtransient_mark_mode)
14999 && !NILP (BVAR (current_buffer, mark_active)))
15000 && NILP (WVAR (w, region_showing))
15001 && NILP (Vshow_trailing_whitespace)
15002 /* This code is not used for mini-buffer for the sake of the case
15003 of redisplaying to replace an echo area message; since in
15004 that case the mini-buffer contents per se are usually
15005 unchanged. This code is of no real use in the mini-buffer
15006 since the handling of this_line_start_pos, etc., in redisplay
15007 handles the same cases. */
15008 && !EQ (window, minibuf_window)
15009 /* When splitting windows or for new windows, it happens that
15010 redisplay is called with a nil window_end_vpos or one being
15011 larger than the window. This should really be fixed in
15012 window.c. I don't have this on my list, now, so we do
15013 approximately the same as the old redisplay code. --gerd. */
15014 && INTEGERP (WVAR (w, window_end_vpos))
15015 && XFASTINT (WVAR (w, window_end_vpos)) < w->current_matrix->nrows
15016 && (FRAME_WINDOW_P (f)
15017 || !overlay_arrow_in_current_buffer_p ()))
15018 {
15019 int this_scroll_margin, top_scroll_margin;
15020 struct glyph_row *row = NULL;
15021
15022 #ifdef GLYPH_DEBUG
15023 debug_method_add (w, "cursor movement");
15024 #endif
15025
15026 /* Scroll if point within this distance from the top or bottom
15027 of the window. This is a pixel value. */
15028 if (scroll_margin > 0)
15029 {
15030 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15031 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15032 }
15033 else
15034 this_scroll_margin = 0;
15035
15036 top_scroll_margin = this_scroll_margin;
15037 if (WINDOW_WANTS_HEADER_LINE_P (w))
15038 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15039
15040 /* Start with the row the cursor was displayed during the last
15041 not paused redisplay. Give up if that row is not valid. */
15042 if (w->last_cursor.vpos < 0
15043 || w->last_cursor.vpos >= w->current_matrix->nrows)
15044 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15045 else
15046 {
15047 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15048 if (row->mode_line_p)
15049 ++row;
15050 if (!row->enabled_p)
15051 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15052 }
15053
15054 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15055 {
15056 int scroll_p = 0, must_scroll = 0;
15057 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15058
15059 if (PT > w->last_point)
15060 {
15061 /* Point has moved forward. */
15062 while (MATRIX_ROW_END_CHARPOS (row) < PT
15063 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15064 {
15065 eassert (row->enabled_p);
15066 ++row;
15067 }
15068
15069 /* If the end position of a row equals the start
15070 position of the next row, and PT is at that position,
15071 we would rather display cursor in the next line. */
15072 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15073 && MATRIX_ROW_END_CHARPOS (row) == PT
15074 && row < w->current_matrix->rows
15075 + w->current_matrix->nrows - 1
15076 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15077 && !cursor_row_p (row))
15078 ++row;
15079
15080 /* If within the scroll margin, scroll. Note that
15081 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15082 the next line would be drawn, and that
15083 this_scroll_margin can be zero. */
15084 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15085 || PT > MATRIX_ROW_END_CHARPOS (row)
15086 /* Line is completely visible last line in window
15087 and PT is to be set in the next line. */
15088 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15089 && PT == MATRIX_ROW_END_CHARPOS (row)
15090 && !row->ends_at_zv_p
15091 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15092 scroll_p = 1;
15093 }
15094 else if (PT < w->last_point)
15095 {
15096 /* Cursor has to be moved backward. Note that PT >=
15097 CHARPOS (startp) because of the outer if-statement. */
15098 while (!row->mode_line_p
15099 && (MATRIX_ROW_START_CHARPOS (row) > PT
15100 || (MATRIX_ROW_START_CHARPOS (row) == PT
15101 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15102 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15103 row > w->current_matrix->rows
15104 && (row-1)->ends_in_newline_from_string_p))))
15105 && (row->y > top_scroll_margin
15106 || CHARPOS (startp) == BEGV))
15107 {
15108 eassert (row->enabled_p);
15109 --row;
15110 }
15111
15112 /* Consider the following case: Window starts at BEGV,
15113 there is invisible, intangible text at BEGV, so that
15114 display starts at some point START > BEGV. It can
15115 happen that we are called with PT somewhere between
15116 BEGV and START. Try to handle that case. */
15117 if (row < w->current_matrix->rows
15118 || row->mode_line_p)
15119 {
15120 row = w->current_matrix->rows;
15121 if (row->mode_line_p)
15122 ++row;
15123 }
15124
15125 /* Due to newlines in overlay strings, we may have to
15126 skip forward over overlay strings. */
15127 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15128 && MATRIX_ROW_END_CHARPOS (row) == PT
15129 && !cursor_row_p (row))
15130 ++row;
15131
15132 /* If within the scroll margin, scroll. */
15133 if (row->y < top_scroll_margin
15134 && CHARPOS (startp) != BEGV)
15135 scroll_p = 1;
15136 }
15137 else
15138 {
15139 /* Cursor did not move. So don't scroll even if cursor line
15140 is partially visible, as it was so before. */
15141 rc = CURSOR_MOVEMENT_SUCCESS;
15142 }
15143
15144 if (PT < MATRIX_ROW_START_CHARPOS (row)
15145 || PT > MATRIX_ROW_END_CHARPOS (row))
15146 {
15147 /* if PT is not in the glyph row, give up. */
15148 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15149 must_scroll = 1;
15150 }
15151 else if (rc != CURSOR_MOVEMENT_SUCCESS
15152 && !NILP (BVAR (XBUFFER (WVAR (w, buffer)), bidi_display_reordering)))
15153 {
15154 struct glyph_row *row1;
15155
15156 /* If rows are bidi-reordered and point moved, back up
15157 until we find a row that does not belong to a
15158 continuation line. This is because we must consider
15159 all rows of a continued line as candidates for the
15160 new cursor positioning, since row start and end
15161 positions change non-linearly with vertical position
15162 in such rows. */
15163 /* FIXME: Revisit this when glyph ``spilling'' in
15164 continuation lines' rows is implemented for
15165 bidi-reordered rows. */
15166 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15167 MATRIX_ROW_CONTINUATION_LINE_P (row);
15168 --row)
15169 {
15170 /* If we hit the beginning of the displayed portion
15171 without finding the first row of a continued
15172 line, give up. */
15173 if (row <= row1)
15174 {
15175 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15176 break;
15177 }
15178 eassert (row->enabled_p);
15179 }
15180 }
15181 if (must_scroll)
15182 ;
15183 else if (rc != CURSOR_MOVEMENT_SUCCESS
15184 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15185 /* Make sure this isn't a header line by any chance, since
15186 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15187 && !row->mode_line_p
15188 && make_cursor_line_fully_visible_p)
15189 {
15190 if (PT == MATRIX_ROW_END_CHARPOS (row)
15191 && !row->ends_at_zv_p
15192 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15193 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15194 else if (row->height > window_box_height (w))
15195 {
15196 /* If we end up in a partially visible line, let's
15197 make it fully visible, except when it's taller
15198 than the window, in which case we can't do much
15199 about it. */
15200 *scroll_step = 1;
15201 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15202 }
15203 else
15204 {
15205 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15206 if (!cursor_row_fully_visible_p (w, 0, 1))
15207 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15208 else
15209 rc = CURSOR_MOVEMENT_SUCCESS;
15210 }
15211 }
15212 else if (scroll_p)
15213 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15214 else if (rc != CURSOR_MOVEMENT_SUCCESS
15215 && !NILP (BVAR (XBUFFER (WVAR (w, buffer)), bidi_display_reordering)))
15216 {
15217 /* With bidi-reordered rows, there could be more than
15218 one candidate row whose start and end positions
15219 occlude point. We need to let set_cursor_from_row
15220 find the best candidate. */
15221 /* FIXME: Revisit this when glyph ``spilling'' in
15222 continuation lines' rows is implemented for
15223 bidi-reordered rows. */
15224 int rv = 0;
15225
15226 do
15227 {
15228 int at_zv_p = 0, exact_match_p = 0;
15229
15230 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15231 && PT <= MATRIX_ROW_END_CHARPOS (row)
15232 && cursor_row_p (row))
15233 rv |= set_cursor_from_row (w, row, w->current_matrix,
15234 0, 0, 0, 0);
15235 /* As soon as we've found the exact match for point,
15236 or the first suitable row whose ends_at_zv_p flag
15237 is set, we are done. */
15238 at_zv_p =
15239 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15240 if (rv && !at_zv_p
15241 && w->cursor.hpos >= 0
15242 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15243 w->cursor.vpos))
15244 {
15245 struct glyph_row *candidate =
15246 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15247 struct glyph *g =
15248 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15249 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15250
15251 exact_match_p =
15252 (BUFFERP (g->object) && g->charpos == PT)
15253 || (INTEGERP (g->object)
15254 && (g->charpos == PT
15255 || (g->charpos == 0 && endpos - 1 == PT)));
15256 }
15257 if (rv && (at_zv_p || exact_match_p))
15258 {
15259 rc = CURSOR_MOVEMENT_SUCCESS;
15260 break;
15261 }
15262 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15263 break;
15264 ++row;
15265 }
15266 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15267 || row->continued_p)
15268 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15269 || (MATRIX_ROW_START_CHARPOS (row) == PT
15270 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15271 /* If we didn't find any candidate rows, or exited the
15272 loop before all the candidates were examined, signal
15273 to the caller that this method failed. */
15274 if (rc != CURSOR_MOVEMENT_SUCCESS
15275 && !(rv
15276 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15277 && !row->continued_p))
15278 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15279 else if (rv)
15280 rc = CURSOR_MOVEMENT_SUCCESS;
15281 }
15282 else
15283 {
15284 do
15285 {
15286 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15287 {
15288 rc = CURSOR_MOVEMENT_SUCCESS;
15289 break;
15290 }
15291 ++row;
15292 }
15293 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15294 && MATRIX_ROW_START_CHARPOS (row) == PT
15295 && cursor_row_p (row));
15296 }
15297 }
15298 }
15299
15300 return rc;
15301 }
15302
15303 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15304 static
15305 #endif
15306 void
15307 set_vertical_scroll_bar (struct window *w)
15308 {
15309 ptrdiff_t start, end, whole;
15310
15311 /* Calculate the start and end positions for the current window.
15312 At some point, it would be nice to choose between scrollbars
15313 which reflect the whole buffer size, with special markers
15314 indicating narrowing, and scrollbars which reflect only the
15315 visible region.
15316
15317 Note that mini-buffers sometimes aren't displaying any text. */
15318 if (!MINI_WINDOW_P (w)
15319 || (w == XWINDOW (minibuf_window)
15320 && NILP (echo_area_buffer[0])))
15321 {
15322 struct buffer *buf = XBUFFER (WVAR (w, buffer));
15323 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15324 start = marker_position (WVAR (w, start)) - BUF_BEGV (buf);
15325 /* I don't think this is guaranteed to be right. For the
15326 moment, we'll pretend it is. */
15327 end = BUF_Z (buf) - XFASTINT (WVAR (w, window_end_pos)) - BUF_BEGV (buf);
15328
15329 if (end < start)
15330 end = start;
15331 if (whole < (end - start))
15332 whole = end - start;
15333 }
15334 else
15335 start = end = whole = 0;
15336
15337 /* Indicate what this scroll bar ought to be displaying now. */
15338 if (FRAME_TERMINAL (XFRAME (WVAR (w, frame)))->set_vertical_scroll_bar_hook)
15339 (*FRAME_TERMINAL (XFRAME (WVAR (w, frame)))->set_vertical_scroll_bar_hook)
15340 (w, end - start, whole, start);
15341 }
15342
15343
15344 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15345 selected_window is redisplayed.
15346
15347 We can return without actually redisplaying the window if
15348 fonts_changed_p is nonzero. In that case, redisplay_internal will
15349 retry. */
15350
15351 static void
15352 redisplay_window (Lisp_Object window, int just_this_one_p)
15353 {
15354 struct window *w = XWINDOW (window);
15355 struct frame *f = XFRAME (WVAR (w, frame));
15356 struct buffer *buffer = XBUFFER (WVAR (w, buffer));
15357 struct buffer *old = current_buffer;
15358 struct text_pos lpoint, opoint, startp;
15359 int update_mode_line;
15360 int tem;
15361 struct it it;
15362 /* Record it now because it's overwritten. */
15363 int current_matrix_up_to_date_p = 0;
15364 int used_current_matrix_p = 0;
15365 /* This is less strict than current_matrix_up_to_date_p.
15366 It indicates that the buffer contents and narrowing are unchanged. */
15367 int buffer_unchanged_p = 0;
15368 int temp_scroll_step = 0;
15369 ptrdiff_t count = SPECPDL_INDEX ();
15370 int rc;
15371 int centering_position = -1;
15372 int last_line_misfit = 0;
15373 ptrdiff_t beg_unchanged, end_unchanged;
15374
15375 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15376 opoint = lpoint;
15377
15378 /* W must be a leaf window here. */
15379 eassert (!NILP (WVAR (w, buffer)));
15380 #ifdef GLYPH_DEBUG
15381 *w->desired_matrix->method = 0;
15382 #endif
15383
15384 restart:
15385 reconsider_clip_changes (w, buffer);
15386
15387 /* Has the mode line to be updated? */
15388 update_mode_line = (w->update_mode_line
15389 || update_mode_lines
15390 || buffer->clip_changed
15391 || buffer->prevent_redisplay_optimizations_p);
15392
15393 if (MINI_WINDOW_P (w))
15394 {
15395 if (w == XWINDOW (echo_area_window)
15396 && !NILP (echo_area_buffer[0]))
15397 {
15398 if (update_mode_line)
15399 /* We may have to update a tty frame's menu bar or a
15400 tool-bar. Example `M-x C-h C-h C-g'. */
15401 goto finish_menu_bars;
15402 else
15403 /* We've already displayed the echo area glyphs in this window. */
15404 goto finish_scroll_bars;
15405 }
15406 else if ((w != XWINDOW (minibuf_window)
15407 || minibuf_level == 0)
15408 /* When buffer is nonempty, redisplay window normally. */
15409 && BUF_Z (XBUFFER (WVAR (w, buffer))) == BUF_BEG (XBUFFER (WVAR (w, buffer)))
15410 /* Quail displays non-mini buffers in minibuffer window.
15411 In that case, redisplay the window normally. */
15412 && !NILP (Fmemq (WVAR (w, buffer), Vminibuffer_list)))
15413 {
15414 /* W is a mini-buffer window, but it's not active, so clear
15415 it. */
15416 int yb = window_text_bottom_y (w);
15417 struct glyph_row *row;
15418 int y;
15419
15420 for (y = 0, row = w->desired_matrix->rows;
15421 y < yb;
15422 y += row->height, ++row)
15423 blank_row (w, row, y);
15424 goto finish_scroll_bars;
15425 }
15426
15427 clear_glyph_matrix (w->desired_matrix);
15428 }
15429
15430 /* Otherwise set up data on this window; select its buffer and point
15431 value. */
15432 /* Really select the buffer, for the sake of buffer-local
15433 variables. */
15434 set_buffer_internal_1 (XBUFFER (WVAR (w, buffer)));
15435
15436 current_matrix_up_to_date_p
15437 = (!NILP (WVAR (w, window_end_valid))
15438 && !current_buffer->clip_changed
15439 && !current_buffer->prevent_redisplay_optimizations_p
15440 && w->last_modified >= MODIFF
15441 && w->last_overlay_modified >= OVERLAY_MODIFF);
15442
15443 /* Run the window-bottom-change-functions
15444 if it is possible that the text on the screen has changed
15445 (either due to modification of the text, or any other reason). */
15446 if (!current_matrix_up_to_date_p
15447 && !NILP (Vwindow_text_change_functions))
15448 {
15449 safe_run_hooks (Qwindow_text_change_functions);
15450 goto restart;
15451 }
15452
15453 beg_unchanged = BEG_UNCHANGED;
15454 end_unchanged = END_UNCHANGED;
15455
15456 SET_TEXT_POS (opoint, PT, PT_BYTE);
15457
15458 specbind (Qinhibit_point_motion_hooks, Qt);
15459
15460 buffer_unchanged_p
15461 = (!NILP (WVAR (w, window_end_valid))
15462 && !current_buffer->clip_changed
15463 && w->last_modified >= MODIFF
15464 && w->last_overlay_modified >= OVERLAY_MODIFF);
15465
15466 /* When windows_or_buffers_changed is non-zero, we can't rely on
15467 the window end being valid, so set it to nil there. */
15468 if (windows_or_buffers_changed)
15469 {
15470 /* If window starts on a continuation line, maybe adjust the
15471 window start in case the window's width changed. */
15472 if (XMARKER (WVAR (w, start))->buffer == current_buffer)
15473 compute_window_start_on_continuation_line (w);
15474
15475 WVAR (w, window_end_valid) = Qnil;
15476 }
15477
15478 /* Some sanity checks. */
15479 CHECK_WINDOW_END (w);
15480 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15481 abort ();
15482 if (BYTEPOS (opoint) < CHARPOS (opoint))
15483 abort ();
15484
15485 /* If %c is in mode line, update it if needed. */
15486 if (!NILP (WVAR (w, column_number_displayed))
15487 /* This alternative quickly identifies a common case
15488 where no change is needed. */
15489 && !(PT == w->last_point
15490 && w->last_modified >= MODIFF
15491 && w->last_overlay_modified >= OVERLAY_MODIFF)
15492 && (XFASTINT (WVAR (w, column_number_displayed)) != current_column ()))
15493 update_mode_line = 1;
15494
15495 /* Count number of windows showing the selected buffer. An indirect
15496 buffer counts as its base buffer. */
15497 if (!just_this_one_p)
15498 {
15499 struct buffer *current_base, *window_base;
15500 current_base = current_buffer;
15501 window_base = XBUFFER (WVAR (XWINDOW (selected_window), buffer));
15502 if (current_base->base_buffer)
15503 current_base = current_base->base_buffer;
15504 if (window_base->base_buffer)
15505 window_base = window_base->base_buffer;
15506 if (current_base == window_base)
15507 buffer_shared++;
15508 }
15509
15510 /* Point refers normally to the selected window. For any other
15511 window, set up appropriate value. */
15512 if (!EQ (window, selected_window))
15513 {
15514 ptrdiff_t new_pt = XMARKER (WVAR (w, pointm))->charpos;
15515 ptrdiff_t new_pt_byte = marker_byte_position (WVAR (w, pointm));
15516 if (new_pt < BEGV)
15517 {
15518 new_pt = BEGV;
15519 new_pt_byte = BEGV_BYTE;
15520 set_marker_both (WVAR (w, pointm), Qnil, BEGV, BEGV_BYTE);
15521 }
15522 else if (new_pt > (ZV - 1))
15523 {
15524 new_pt = ZV;
15525 new_pt_byte = ZV_BYTE;
15526 set_marker_both (WVAR (w, pointm), Qnil, ZV, ZV_BYTE);
15527 }
15528
15529 /* We don't use SET_PT so that the point-motion hooks don't run. */
15530 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15531 }
15532
15533 /* If any of the character widths specified in the display table
15534 have changed, invalidate the width run cache. It's true that
15535 this may be a bit late to catch such changes, but the rest of
15536 redisplay goes (non-fatally) haywire when the display table is
15537 changed, so why should we worry about doing any better? */
15538 if (current_buffer->width_run_cache)
15539 {
15540 struct Lisp_Char_Table *disptab = buffer_display_table ();
15541
15542 if (! disptab_matches_widthtab
15543 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15544 {
15545 invalidate_region_cache (current_buffer,
15546 current_buffer->width_run_cache,
15547 BEG, Z);
15548 recompute_width_table (current_buffer, disptab);
15549 }
15550 }
15551
15552 /* If window-start is screwed up, choose a new one. */
15553 if (XMARKER (WVAR (w, start))->buffer != current_buffer)
15554 goto recenter;
15555
15556 SET_TEXT_POS_FROM_MARKER (startp, WVAR (w, start));
15557
15558 /* If someone specified a new starting point but did not insist,
15559 check whether it can be used. */
15560 if (w->optional_new_start
15561 && CHARPOS (startp) >= BEGV
15562 && CHARPOS (startp) <= ZV)
15563 {
15564 w->optional_new_start = 0;
15565 start_display (&it, w, startp);
15566 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15567 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15568 if (IT_CHARPOS (it) == PT)
15569 w->force_start = 1;
15570 /* IT may overshoot PT if text at PT is invisible. */
15571 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15572 w->force_start = 1;
15573 }
15574
15575 force_start:
15576
15577 /* Handle case where place to start displaying has been specified,
15578 unless the specified location is outside the accessible range. */
15579 if (w->force_start || w->frozen_window_start_p)
15580 {
15581 /* We set this later on if we have to adjust point. */
15582 int new_vpos = -1;
15583
15584 w->force_start = 0;
15585 w->vscroll = 0;
15586 WVAR (w, window_end_valid) = Qnil;
15587
15588 /* Forget any recorded base line for line number display. */
15589 if (!buffer_unchanged_p)
15590 WVAR (w, base_line_number) = Qnil;
15591
15592 /* Redisplay the mode line. Select the buffer properly for that.
15593 Also, run the hook window-scroll-functions
15594 because we have scrolled. */
15595 /* Note, we do this after clearing force_start because
15596 if there's an error, it is better to forget about force_start
15597 than to get into an infinite loop calling the hook functions
15598 and having them get more errors. */
15599 if (!update_mode_line
15600 || ! NILP (Vwindow_scroll_functions))
15601 {
15602 update_mode_line = 1;
15603 w->update_mode_line = 1;
15604 startp = run_window_scroll_functions (window, startp);
15605 }
15606
15607 w->last_modified = 0;
15608 w->last_overlay_modified = 0;
15609 if (CHARPOS (startp) < BEGV)
15610 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15611 else if (CHARPOS (startp) > ZV)
15612 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15613
15614 /* Redisplay, then check if cursor has been set during the
15615 redisplay. Give up if new fonts were loaded. */
15616 /* We used to issue a CHECK_MARGINS argument to try_window here,
15617 but this causes scrolling to fail when point begins inside
15618 the scroll margin (bug#148) -- cyd */
15619 if (!try_window (window, startp, 0))
15620 {
15621 w->force_start = 1;
15622 clear_glyph_matrix (w->desired_matrix);
15623 goto need_larger_matrices;
15624 }
15625
15626 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15627 {
15628 /* If point does not appear, try to move point so it does
15629 appear. The desired matrix has been built above, so we
15630 can use it here. */
15631 new_vpos = window_box_height (w) / 2;
15632 }
15633
15634 if (!cursor_row_fully_visible_p (w, 0, 0))
15635 {
15636 /* Point does appear, but on a line partly visible at end of window.
15637 Move it back to a fully-visible line. */
15638 new_vpos = window_box_height (w);
15639 }
15640
15641 /* If we need to move point for either of the above reasons,
15642 now actually do it. */
15643 if (new_vpos >= 0)
15644 {
15645 struct glyph_row *row;
15646
15647 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15648 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15649 ++row;
15650
15651 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15652 MATRIX_ROW_START_BYTEPOS (row));
15653
15654 if (w != XWINDOW (selected_window))
15655 set_marker_both (WVAR (w, pointm), Qnil, PT, PT_BYTE);
15656 else if (current_buffer == old)
15657 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15658
15659 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15660
15661 /* If we are highlighting the region, then we just changed
15662 the region, so redisplay to show it. */
15663 if (!NILP (Vtransient_mark_mode)
15664 && !NILP (BVAR (current_buffer, mark_active)))
15665 {
15666 clear_glyph_matrix (w->desired_matrix);
15667 if (!try_window (window, startp, 0))
15668 goto need_larger_matrices;
15669 }
15670 }
15671
15672 #ifdef GLYPH_DEBUG
15673 debug_method_add (w, "forced window start");
15674 #endif
15675 goto done;
15676 }
15677
15678 /* Handle case where text has not changed, only point, and it has
15679 not moved off the frame, and we are not retrying after hscroll.
15680 (current_matrix_up_to_date_p is nonzero when retrying.) */
15681 if (current_matrix_up_to_date_p
15682 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15683 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15684 {
15685 switch (rc)
15686 {
15687 case CURSOR_MOVEMENT_SUCCESS:
15688 used_current_matrix_p = 1;
15689 goto done;
15690
15691 case CURSOR_MOVEMENT_MUST_SCROLL:
15692 goto try_to_scroll;
15693
15694 default:
15695 abort ();
15696 }
15697 }
15698 /* If current starting point was originally the beginning of a line
15699 but no longer is, find a new starting point. */
15700 else if (w->start_at_line_beg
15701 && !(CHARPOS (startp) <= BEGV
15702 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15703 {
15704 #ifdef GLYPH_DEBUG
15705 debug_method_add (w, "recenter 1");
15706 #endif
15707 goto recenter;
15708 }
15709
15710 /* Try scrolling with try_window_id. Value is > 0 if update has
15711 been done, it is -1 if we know that the same window start will
15712 not work. It is 0 if unsuccessful for some other reason. */
15713 else if ((tem = try_window_id (w)) != 0)
15714 {
15715 #ifdef GLYPH_DEBUG
15716 debug_method_add (w, "try_window_id %d", tem);
15717 #endif
15718
15719 if (fonts_changed_p)
15720 goto need_larger_matrices;
15721 if (tem > 0)
15722 goto done;
15723
15724 /* Otherwise try_window_id has returned -1 which means that we
15725 don't want the alternative below this comment to execute. */
15726 }
15727 else if (CHARPOS (startp) >= BEGV
15728 && CHARPOS (startp) <= ZV
15729 && PT >= CHARPOS (startp)
15730 && (CHARPOS (startp) < ZV
15731 /* Avoid starting at end of buffer. */
15732 || CHARPOS (startp) == BEGV
15733 || (w->last_modified >= MODIFF
15734 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15735 {
15736 int d1, d2, d3, d4, d5, d6;
15737
15738 /* If first window line is a continuation line, and window start
15739 is inside the modified region, but the first change is before
15740 current window start, we must select a new window start.
15741
15742 However, if this is the result of a down-mouse event (e.g. by
15743 extending the mouse-drag-overlay), we don't want to select a
15744 new window start, since that would change the position under
15745 the mouse, resulting in an unwanted mouse-movement rather
15746 than a simple mouse-click. */
15747 if (!w->start_at_line_beg
15748 && NILP (do_mouse_tracking)
15749 && CHARPOS (startp) > BEGV
15750 && CHARPOS (startp) > BEG + beg_unchanged
15751 && CHARPOS (startp) <= Z - end_unchanged
15752 /* Even if w->start_at_line_beg is nil, a new window may
15753 start at a line_beg, since that's how set_buffer_window
15754 sets it. So, we need to check the return value of
15755 compute_window_start_on_continuation_line. (See also
15756 bug#197). */
15757 && XMARKER (WVAR (w, start))->buffer == current_buffer
15758 && compute_window_start_on_continuation_line (w)
15759 /* It doesn't make sense to force the window start like we
15760 do at label force_start if it is already known that point
15761 will not be visible in the resulting window, because
15762 doing so will move point from its correct position
15763 instead of scrolling the window to bring point into view.
15764 See bug#9324. */
15765 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15766 {
15767 w->force_start = 1;
15768 SET_TEXT_POS_FROM_MARKER (startp, WVAR (w, start));
15769 goto force_start;
15770 }
15771
15772 #ifdef GLYPH_DEBUG
15773 debug_method_add (w, "same window start");
15774 #endif
15775
15776 /* Try to redisplay starting at same place as before.
15777 If point has not moved off frame, accept the results. */
15778 if (!current_matrix_up_to_date_p
15779 /* Don't use try_window_reusing_current_matrix in this case
15780 because a window scroll function can have changed the
15781 buffer. */
15782 || !NILP (Vwindow_scroll_functions)
15783 || MINI_WINDOW_P (w)
15784 || !(used_current_matrix_p
15785 = try_window_reusing_current_matrix (w)))
15786 {
15787 IF_DEBUG (debug_method_add (w, "1"));
15788 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15789 /* -1 means we need to scroll.
15790 0 means we need new matrices, but fonts_changed_p
15791 is set in that case, so we will detect it below. */
15792 goto try_to_scroll;
15793 }
15794
15795 if (fonts_changed_p)
15796 goto need_larger_matrices;
15797
15798 if (w->cursor.vpos >= 0)
15799 {
15800 if (!just_this_one_p
15801 || current_buffer->clip_changed
15802 || BEG_UNCHANGED < CHARPOS (startp))
15803 /* Forget any recorded base line for line number display. */
15804 WVAR (w, base_line_number) = Qnil;
15805
15806 if (!cursor_row_fully_visible_p (w, 1, 0))
15807 {
15808 clear_glyph_matrix (w->desired_matrix);
15809 last_line_misfit = 1;
15810 }
15811 /* Drop through and scroll. */
15812 else
15813 goto done;
15814 }
15815 else
15816 clear_glyph_matrix (w->desired_matrix);
15817 }
15818
15819 try_to_scroll:
15820
15821 w->last_modified = 0;
15822 w->last_overlay_modified = 0;
15823
15824 /* Redisplay the mode line. Select the buffer properly for that. */
15825 if (!update_mode_line)
15826 {
15827 update_mode_line = 1;
15828 w->update_mode_line = 1;
15829 }
15830
15831 /* Try to scroll by specified few lines. */
15832 if ((scroll_conservatively
15833 || emacs_scroll_step
15834 || temp_scroll_step
15835 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15836 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15837 && CHARPOS (startp) >= BEGV
15838 && CHARPOS (startp) <= ZV)
15839 {
15840 /* The function returns -1 if new fonts were loaded, 1 if
15841 successful, 0 if not successful. */
15842 int ss = try_scrolling (window, just_this_one_p,
15843 scroll_conservatively,
15844 emacs_scroll_step,
15845 temp_scroll_step, last_line_misfit);
15846 switch (ss)
15847 {
15848 case SCROLLING_SUCCESS:
15849 goto done;
15850
15851 case SCROLLING_NEED_LARGER_MATRICES:
15852 goto need_larger_matrices;
15853
15854 case SCROLLING_FAILED:
15855 break;
15856
15857 default:
15858 abort ();
15859 }
15860 }
15861
15862 /* Finally, just choose a place to start which positions point
15863 according to user preferences. */
15864
15865 recenter:
15866
15867 #ifdef GLYPH_DEBUG
15868 debug_method_add (w, "recenter");
15869 #endif
15870
15871 /* w->vscroll = 0; */
15872
15873 /* Forget any previously recorded base line for line number display. */
15874 if (!buffer_unchanged_p)
15875 WVAR (w, base_line_number) = Qnil;
15876
15877 /* Determine the window start relative to point. */
15878 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15879 it.current_y = it.last_visible_y;
15880 if (centering_position < 0)
15881 {
15882 int margin =
15883 scroll_margin > 0
15884 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15885 : 0;
15886 ptrdiff_t margin_pos = CHARPOS (startp);
15887 Lisp_Object aggressive;
15888 int scrolling_up;
15889
15890 /* If there is a scroll margin at the top of the window, find
15891 its character position. */
15892 if (margin
15893 /* Cannot call start_display if startp is not in the
15894 accessible region of the buffer. This can happen when we
15895 have just switched to a different buffer and/or changed
15896 its restriction. In that case, startp is initialized to
15897 the character position 1 (BEGV) because we did not yet
15898 have chance to display the buffer even once. */
15899 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15900 {
15901 struct it it1;
15902 void *it1data = NULL;
15903
15904 SAVE_IT (it1, it, it1data);
15905 start_display (&it1, w, startp);
15906 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15907 margin_pos = IT_CHARPOS (it1);
15908 RESTORE_IT (&it, &it, it1data);
15909 }
15910 scrolling_up = PT > margin_pos;
15911 aggressive =
15912 scrolling_up
15913 ? BVAR (current_buffer, scroll_up_aggressively)
15914 : BVAR (current_buffer, scroll_down_aggressively);
15915
15916 if (!MINI_WINDOW_P (w)
15917 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15918 {
15919 int pt_offset = 0;
15920
15921 /* Setting scroll-conservatively overrides
15922 scroll-*-aggressively. */
15923 if (!scroll_conservatively && NUMBERP (aggressive))
15924 {
15925 double float_amount = XFLOATINT (aggressive);
15926
15927 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15928 if (pt_offset == 0 && float_amount > 0)
15929 pt_offset = 1;
15930 if (pt_offset && margin > 0)
15931 margin -= 1;
15932 }
15933 /* Compute how much to move the window start backward from
15934 point so that point will be displayed where the user
15935 wants it. */
15936 if (scrolling_up)
15937 {
15938 centering_position = it.last_visible_y;
15939 if (pt_offset)
15940 centering_position -= pt_offset;
15941 centering_position -=
15942 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15943 + WINDOW_HEADER_LINE_HEIGHT (w);
15944 /* Don't let point enter the scroll margin near top of
15945 the window. */
15946 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15947 centering_position = margin * FRAME_LINE_HEIGHT (f);
15948 }
15949 else
15950 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15951 }
15952 else
15953 /* Set the window start half the height of the window backward
15954 from point. */
15955 centering_position = window_box_height (w) / 2;
15956 }
15957 move_it_vertically_backward (&it, centering_position);
15958
15959 eassert (IT_CHARPOS (it) >= BEGV);
15960
15961 /* The function move_it_vertically_backward may move over more
15962 than the specified y-distance. If it->w is small, e.g. a
15963 mini-buffer window, we may end up in front of the window's
15964 display area. Start displaying at the start of the line
15965 containing PT in this case. */
15966 if (it.current_y <= 0)
15967 {
15968 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15969 move_it_vertically_backward (&it, 0);
15970 it.current_y = 0;
15971 }
15972
15973 it.current_x = it.hpos = 0;
15974
15975 /* Set the window start position here explicitly, to avoid an
15976 infinite loop in case the functions in window-scroll-functions
15977 get errors. */
15978 set_marker_both (WVAR (w, start), Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15979
15980 /* Run scroll hooks. */
15981 startp = run_window_scroll_functions (window, it.current.pos);
15982
15983 /* Redisplay the window. */
15984 if (!current_matrix_up_to_date_p
15985 || windows_or_buffers_changed
15986 || cursor_type_changed
15987 /* Don't use try_window_reusing_current_matrix in this case
15988 because it can have changed the buffer. */
15989 || !NILP (Vwindow_scroll_functions)
15990 || !just_this_one_p
15991 || MINI_WINDOW_P (w)
15992 || !(used_current_matrix_p
15993 = try_window_reusing_current_matrix (w)))
15994 try_window (window, startp, 0);
15995
15996 /* If new fonts have been loaded (due to fontsets), give up. We
15997 have to start a new redisplay since we need to re-adjust glyph
15998 matrices. */
15999 if (fonts_changed_p)
16000 goto need_larger_matrices;
16001
16002 /* If cursor did not appear assume that the middle of the window is
16003 in the first line of the window. Do it again with the next line.
16004 (Imagine a window of height 100, displaying two lines of height
16005 60. Moving back 50 from it->last_visible_y will end in the first
16006 line.) */
16007 if (w->cursor.vpos < 0)
16008 {
16009 if (!NILP (WVAR (w, window_end_valid))
16010 && PT >= Z - XFASTINT (WVAR (w, window_end_pos)))
16011 {
16012 clear_glyph_matrix (w->desired_matrix);
16013 move_it_by_lines (&it, 1);
16014 try_window (window, it.current.pos, 0);
16015 }
16016 else if (PT < IT_CHARPOS (it))
16017 {
16018 clear_glyph_matrix (w->desired_matrix);
16019 move_it_by_lines (&it, -1);
16020 try_window (window, it.current.pos, 0);
16021 }
16022 else
16023 {
16024 /* Not much we can do about it. */
16025 }
16026 }
16027
16028 /* Consider the following case: Window starts at BEGV, there is
16029 invisible, intangible text at BEGV, so that display starts at
16030 some point START > BEGV. It can happen that we are called with
16031 PT somewhere between BEGV and START. Try to handle that case. */
16032 if (w->cursor.vpos < 0)
16033 {
16034 struct glyph_row *row = w->current_matrix->rows;
16035 if (row->mode_line_p)
16036 ++row;
16037 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16038 }
16039
16040 if (!cursor_row_fully_visible_p (w, 0, 0))
16041 {
16042 /* If vscroll is enabled, disable it and try again. */
16043 if (w->vscroll)
16044 {
16045 w->vscroll = 0;
16046 clear_glyph_matrix (w->desired_matrix);
16047 goto recenter;
16048 }
16049
16050 /* Users who set scroll-conservatively to a large number want
16051 point just above/below the scroll margin. If we ended up
16052 with point's row partially visible, move the window start to
16053 make that row fully visible and out of the margin. */
16054 if (scroll_conservatively > SCROLL_LIMIT)
16055 {
16056 int margin =
16057 scroll_margin > 0
16058 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16059 : 0;
16060 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16061
16062 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16063 clear_glyph_matrix (w->desired_matrix);
16064 if (1 == try_window (window, it.current.pos,
16065 TRY_WINDOW_CHECK_MARGINS))
16066 goto done;
16067 }
16068
16069 /* If centering point failed to make the whole line visible,
16070 put point at the top instead. That has to make the whole line
16071 visible, if it can be done. */
16072 if (centering_position == 0)
16073 goto done;
16074
16075 clear_glyph_matrix (w->desired_matrix);
16076 centering_position = 0;
16077 goto recenter;
16078 }
16079
16080 done:
16081
16082 SET_TEXT_POS_FROM_MARKER (startp, WVAR (w, start));
16083 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16084 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16085
16086 /* Display the mode line, if we must. */
16087 if ((update_mode_line
16088 /* If window not full width, must redo its mode line
16089 if (a) the window to its side is being redone and
16090 (b) we do a frame-based redisplay. This is a consequence
16091 of how inverted lines are drawn in frame-based redisplay. */
16092 || (!just_this_one_p
16093 && !FRAME_WINDOW_P (f)
16094 && !WINDOW_FULL_WIDTH_P (w))
16095 /* Line number to display. */
16096 || INTEGERP (WVAR (w, base_line_pos))
16097 /* Column number is displayed and different from the one displayed. */
16098 || (!NILP (WVAR (w, column_number_displayed))
16099 && (XFASTINT (WVAR (w, column_number_displayed)) != current_column ())))
16100 /* This means that the window has a mode line. */
16101 && (WINDOW_WANTS_MODELINE_P (w)
16102 || WINDOW_WANTS_HEADER_LINE_P (w)))
16103 {
16104 display_mode_lines (w);
16105
16106 /* If mode line height has changed, arrange for a thorough
16107 immediate redisplay using the correct mode line height. */
16108 if (WINDOW_WANTS_MODELINE_P (w)
16109 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16110 {
16111 fonts_changed_p = 1;
16112 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16113 = DESIRED_MODE_LINE_HEIGHT (w);
16114 }
16115
16116 /* If header line height has changed, arrange for a thorough
16117 immediate redisplay using the correct header line height. */
16118 if (WINDOW_WANTS_HEADER_LINE_P (w)
16119 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16120 {
16121 fonts_changed_p = 1;
16122 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16123 = DESIRED_HEADER_LINE_HEIGHT (w);
16124 }
16125
16126 if (fonts_changed_p)
16127 goto need_larger_matrices;
16128 }
16129
16130 if (!line_number_displayed
16131 && !BUFFERP (WVAR (w, base_line_pos)))
16132 {
16133 WVAR (w, base_line_pos) = Qnil;
16134 WVAR (w, base_line_number) = Qnil;
16135 }
16136
16137 finish_menu_bars:
16138
16139 /* When we reach a frame's selected window, redo the frame's menu bar. */
16140 if (update_mode_line
16141 && EQ (FRAME_SELECTED_WINDOW (f), window))
16142 {
16143 int redisplay_menu_p = 0;
16144
16145 if (FRAME_WINDOW_P (f))
16146 {
16147 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16148 || defined (HAVE_NS) || defined (USE_GTK)
16149 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16150 #else
16151 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16152 #endif
16153 }
16154 else
16155 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16156
16157 if (redisplay_menu_p)
16158 display_menu_bar (w);
16159
16160 #ifdef HAVE_WINDOW_SYSTEM
16161 if (FRAME_WINDOW_P (f))
16162 {
16163 #if defined (USE_GTK) || defined (HAVE_NS)
16164 if (FRAME_EXTERNAL_TOOL_BAR (f))
16165 redisplay_tool_bar (f);
16166 #else
16167 if (WINDOWP (FVAR (f, tool_bar_window))
16168 && (FRAME_TOOL_BAR_LINES (f) > 0
16169 || !NILP (Vauto_resize_tool_bars))
16170 && redisplay_tool_bar (f))
16171 ignore_mouse_drag_p = 1;
16172 #endif
16173 }
16174 #endif
16175 }
16176
16177 #ifdef HAVE_WINDOW_SYSTEM
16178 if (FRAME_WINDOW_P (f)
16179 && update_window_fringes (w, (just_this_one_p
16180 || (!used_current_matrix_p && !overlay_arrow_seen)
16181 || w->pseudo_window_p)))
16182 {
16183 update_begin (f);
16184 BLOCK_INPUT;
16185 if (draw_window_fringes (w, 1))
16186 x_draw_vertical_border (w);
16187 UNBLOCK_INPUT;
16188 update_end (f);
16189 }
16190 #endif /* HAVE_WINDOW_SYSTEM */
16191
16192 /* We go to this label, with fonts_changed_p nonzero,
16193 if it is necessary to try again using larger glyph matrices.
16194 We have to redeem the scroll bar even in this case,
16195 because the loop in redisplay_internal expects that. */
16196 need_larger_matrices:
16197 ;
16198 finish_scroll_bars:
16199
16200 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16201 {
16202 /* Set the thumb's position and size. */
16203 set_vertical_scroll_bar (w);
16204
16205 /* Note that we actually used the scroll bar attached to this
16206 window, so it shouldn't be deleted at the end of redisplay. */
16207 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16208 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16209 }
16210
16211 /* Restore current_buffer and value of point in it. The window
16212 update may have changed the buffer, so first make sure `opoint'
16213 is still valid (Bug#6177). */
16214 if (CHARPOS (opoint) < BEGV)
16215 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16216 else if (CHARPOS (opoint) > ZV)
16217 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16218 else
16219 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16220
16221 set_buffer_internal_1 (old);
16222 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16223 shorter. This can be caused by log truncation in *Messages*. */
16224 if (CHARPOS (lpoint) <= ZV)
16225 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16226
16227 unbind_to (count, Qnil);
16228 }
16229
16230
16231 /* Build the complete desired matrix of WINDOW with a window start
16232 buffer position POS.
16233
16234 Value is 1 if successful. It is zero if fonts were loaded during
16235 redisplay which makes re-adjusting glyph matrices necessary, and -1
16236 if point would appear in the scroll margins.
16237 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16238 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16239 set in FLAGS.) */
16240
16241 int
16242 try_window (Lisp_Object window, struct text_pos pos, int flags)
16243 {
16244 struct window *w = XWINDOW (window);
16245 struct it it;
16246 struct glyph_row *last_text_row = NULL;
16247 struct frame *f = XFRAME (WVAR (w, frame));
16248
16249 /* Make POS the new window start. */
16250 set_marker_both (WVAR (w, start), Qnil, CHARPOS (pos), BYTEPOS (pos));
16251
16252 /* Mark cursor position as unknown. No overlay arrow seen. */
16253 w->cursor.vpos = -1;
16254 overlay_arrow_seen = 0;
16255
16256 /* Initialize iterator and info to start at POS. */
16257 start_display (&it, w, pos);
16258
16259 /* Display all lines of W. */
16260 while (it.current_y < it.last_visible_y)
16261 {
16262 if (display_line (&it))
16263 last_text_row = it.glyph_row - 1;
16264 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16265 return 0;
16266 }
16267
16268 /* Don't let the cursor end in the scroll margins. */
16269 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16270 && !MINI_WINDOW_P (w))
16271 {
16272 int this_scroll_margin;
16273
16274 if (scroll_margin > 0)
16275 {
16276 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16277 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16278 }
16279 else
16280 this_scroll_margin = 0;
16281
16282 if ((w->cursor.y >= 0 /* not vscrolled */
16283 && w->cursor.y < this_scroll_margin
16284 && CHARPOS (pos) > BEGV
16285 && IT_CHARPOS (it) < ZV)
16286 /* rms: considering make_cursor_line_fully_visible_p here
16287 seems to give wrong results. We don't want to recenter
16288 when the last line is partly visible, we want to allow
16289 that case to be handled in the usual way. */
16290 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16291 {
16292 w->cursor.vpos = -1;
16293 clear_glyph_matrix (w->desired_matrix);
16294 return -1;
16295 }
16296 }
16297
16298 /* If bottom moved off end of frame, change mode line percentage. */
16299 if (XFASTINT (WVAR (w, window_end_pos)) <= 0
16300 && Z != IT_CHARPOS (it))
16301 w->update_mode_line = 1;
16302
16303 /* Set window_end_pos to the offset of the last character displayed
16304 on the window from the end of current_buffer. Set
16305 window_end_vpos to its row number. */
16306 if (last_text_row)
16307 {
16308 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16309 w->window_end_bytepos
16310 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16311 WVAR (w, window_end_pos)
16312 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16313 WVAR (w, window_end_vpos)
16314 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16315 eassert
16316 (MATRIX_ROW (w->desired_matrix,
16317 XFASTINT (WVAR (w, window_end_vpos)))->displays_text_p);
16318 }
16319 else
16320 {
16321 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16322 WVAR (w, window_end_pos) = make_number (Z - ZV);
16323 WVAR (w, window_end_vpos) = make_number (0);
16324 }
16325
16326 /* But that is not valid info until redisplay finishes. */
16327 WVAR (w, window_end_valid) = Qnil;
16328 return 1;
16329 }
16330
16331
16332 \f
16333 /************************************************************************
16334 Window redisplay reusing current matrix when buffer has not changed
16335 ************************************************************************/
16336
16337 /* Try redisplay of window W showing an unchanged buffer with a
16338 different window start than the last time it was displayed by
16339 reusing its current matrix. Value is non-zero if successful.
16340 W->start is the new window start. */
16341
16342 static int
16343 try_window_reusing_current_matrix (struct window *w)
16344 {
16345 struct frame *f = XFRAME (WVAR (w, frame));
16346 struct glyph_row *bottom_row;
16347 struct it it;
16348 struct run run;
16349 struct text_pos start, new_start;
16350 int nrows_scrolled, i;
16351 struct glyph_row *last_text_row;
16352 struct glyph_row *last_reused_text_row;
16353 struct glyph_row *start_row;
16354 int start_vpos, min_y, max_y;
16355
16356 #ifdef GLYPH_DEBUG
16357 if (inhibit_try_window_reusing)
16358 return 0;
16359 #endif
16360
16361 if (/* This function doesn't handle terminal frames. */
16362 !FRAME_WINDOW_P (f)
16363 /* Don't try to reuse the display if windows have been split
16364 or such. */
16365 || windows_or_buffers_changed
16366 || cursor_type_changed)
16367 return 0;
16368
16369 /* Can't do this if region may have changed. */
16370 if ((!NILP (Vtransient_mark_mode)
16371 && !NILP (BVAR (current_buffer, mark_active)))
16372 || !NILP (WVAR (w, region_showing))
16373 || !NILP (Vshow_trailing_whitespace))
16374 return 0;
16375
16376 /* If top-line visibility has changed, give up. */
16377 if (WINDOW_WANTS_HEADER_LINE_P (w)
16378 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16379 return 0;
16380
16381 /* Give up if old or new display is scrolled vertically. We could
16382 make this function handle this, but right now it doesn't. */
16383 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16384 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16385 return 0;
16386
16387 /* The variable new_start now holds the new window start. The old
16388 start `start' can be determined from the current matrix. */
16389 SET_TEXT_POS_FROM_MARKER (new_start, WVAR (w, start));
16390 start = start_row->minpos;
16391 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16392
16393 /* Clear the desired matrix for the display below. */
16394 clear_glyph_matrix (w->desired_matrix);
16395
16396 if (CHARPOS (new_start) <= CHARPOS (start))
16397 {
16398 /* Don't use this method if the display starts with an ellipsis
16399 displayed for invisible text. It's not easy to handle that case
16400 below, and it's certainly not worth the effort since this is
16401 not a frequent case. */
16402 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16403 return 0;
16404
16405 IF_DEBUG (debug_method_add (w, "twu1"));
16406
16407 /* Display up to a row that can be reused. The variable
16408 last_text_row is set to the last row displayed that displays
16409 text. Note that it.vpos == 0 if or if not there is a
16410 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16411 start_display (&it, w, new_start);
16412 w->cursor.vpos = -1;
16413 last_text_row = last_reused_text_row = NULL;
16414
16415 while (it.current_y < it.last_visible_y
16416 && !fonts_changed_p)
16417 {
16418 /* If we have reached into the characters in the START row,
16419 that means the line boundaries have changed. So we
16420 can't start copying with the row START. Maybe it will
16421 work to start copying with the following row. */
16422 while (IT_CHARPOS (it) > CHARPOS (start))
16423 {
16424 /* Advance to the next row as the "start". */
16425 start_row++;
16426 start = start_row->minpos;
16427 /* If there are no more rows to try, or just one, give up. */
16428 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16429 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16430 || CHARPOS (start) == ZV)
16431 {
16432 clear_glyph_matrix (w->desired_matrix);
16433 return 0;
16434 }
16435
16436 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16437 }
16438 /* If we have reached alignment, we can copy the rest of the
16439 rows. */
16440 if (IT_CHARPOS (it) == CHARPOS (start)
16441 /* Don't accept "alignment" inside a display vector,
16442 since start_row could have started in the middle of
16443 that same display vector (thus their character
16444 positions match), and we have no way of telling if
16445 that is the case. */
16446 && it.current.dpvec_index < 0)
16447 break;
16448
16449 if (display_line (&it))
16450 last_text_row = it.glyph_row - 1;
16451
16452 }
16453
16454 /* A value of current_y < last_visible_y means that we stopped
16455 at the previous window start, which in turn means that we
16456 have at least one reusable row. */
16457 if (it.current_y < it.last_visible_y)
16458 {
16459 struct glyph_row *row;
16460
16461 /* IT.vpos always starts from 0; it counts text lines. */
16462 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16463
16464 /* Find PT if not already found in the lines displayed. */
16465 if (w->cursor.vpos < 0)
16466 {
16467 int dy = it.current_y - start_row->y;
16468
16469 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16470 row = row_containing_pos (w, PT, row, NULL, dy);
16471 if (row)
16472 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16473 dy, nrows_scrolled);
16474 else
16475 {
16476 clear_glyph_matrix (w->desired_matrix);
16477 return 0;
16478 }
16479 }
16480
16481 /* Scroll the display. Do it before the current matrix is
16482 changed. The problem here is that update has not yet
16483 run, i.e. part of the current matrix is not up to date.
16484 scroll_run_hook will clear the cursor, and use the
16485 current matrix to get the height of the row the cursor is
16486 in. */
16487 run.current_y = start_row->y;
16488 run.desired_y = it.current_y;
16489 run.height = it.last_visible_y - it.current_y;
16490
16491 if (run.height > 0 && run.current_y != run.desired_y)
16492 {
16493 update_begin (f);
16494 FRAME_RIF (f)->update_window_begin_hook (w);
16495 FRAME_RIF (f)->clear_window_mouse_face (w);
16496 FRAME_RIF (f)->scroll_run_hook (w, &run);
16497 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16498 update_end (f);
16499 }
16500
16501 /* Shift current matrix down by nrows_scrolled lines. */
16502 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16503 rotate_matrix (w->current_matrix,
16504 start_vpos,
16505 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16506 nrows_scrolled);
16507
16508 /* Disable lines that must be updated. */
16509 for (i = 0; i < nrows_scrolled; ++i)
16510 (start_row + i)->enabled_p = 0;
16511
16512 /* Re-compute Y positions. */
16513 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16514 max_y = it.last_visible_y;
16515 for (row = start_row + nrows_scrolled;
16516 row < bottom_row;
16517 ++row)
16518 {
16519 row->y = it.current_y;
16520 row->visible_height = row->height;
16521
16522 if (row->y < min_y)
16523 row->visible_height -= min_y - row->y;
16524 if (row->y + row->height > max_y)
16525 row->visible_height -= row->y + row->height - max_y;
16526 if (row->fringe_bitmap_periodic_p)
16527 row->redraw_fringe_bitmaps_p = 1;
16528
16529 it.current_y += row->height;
16530
16531 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16532 last_reused_text_row = row;
16533 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16534 break;
16535 }
16536
16537 /* Disable lines in the current matrix which are now
16538 below the window. */
16539 for (++row; row < bottom_row; ++row)
16540 row->enabled_p = row->mode_line_p = 0;
16541 }
16542
16543 /* Update window_end_pos etc.; last_reused_text_row is the last
16544 reused row from the current matrix containing text, if any.
16545 The value of last_text_row is the last displayed line
16546 containing text. */
16547 if (last_reused_text_row)
16548 {
16549 w->window_end_bytepos
16550 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16551 WVAR (w, window_end_pos)
16552 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16553 WVAR (w, window_end_vpos)
16554 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16555 w->current_matrix));
16556 }
16557 else if (last_text_row)
16558 {
16559 w->window_end_bytepos
16560 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16561 WVAR (w, window_end_pos)
16562 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16563 WVAR (w, window_end_vpos)
16564 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16565 }
16566 else
16567 {
16568 /* This window must be completely empty. */
16569 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16570 WVAR (w, window_end_pos) = make_number (Z - ZV);
16571 WVAR (w, window_end_vpos) = make_number (0);
16572 }
16573 WVAR (w, window_end_valid) = Qnil;
16574
16575 /* Update hint: don't try scrolling again in update_window. */
16576 w->desired_matrix->no_scrolling_p = 1;
16577
16578 #ifdef GLYPH_DEBUG
16579 debug_method_add (w, "try_window_reusing_current_matrix 1");
16580 #endif
16581 return 1;
16582 }
16583 else if (CHARPOS (new_start) > CHARPOS (start))
16584 {
16585 struct glyph_row *pt_row, *row;
16586 struct glyph_row *first_reusable_row;
16587 struct glyph_row *first_row_to_display;
16588 int dy;
16589 int yb = window_text_bottom_y (w);
16590
16591 /* Find the row starting at new_start, if there is one. Don't
16592 reuse a partially visible line at the end. */
16593 first_reusable_row = start_row;
16594 while (first_reusable_row->enabled_p
16595 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16596 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16597 < CHARPOS (new_start)))
16598 ++first_reusable_row;
16599
16600 /* Give up if there is no row to reuse. */
16601 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16602 || !first_reusable_row->enabled_p
16603 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16604 != CHARPOS (new_start)))
16605 return 0;
16606
16607 /* We can reuse fully visible rows beginning with
16608 first_reusable_row to the end of the window. Set
16609 first_row_to_display to the first row that cannot be reused.
16610 Set pt_row to the row containing point, if there is any. */
16611 pt_row = NULL;
16612 for (first_row_to_display = first_reusable_row;
16613 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16614 ++first_row_to_display)
16615 {
16616 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16617 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16618 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16619 && first_row_to_display->ends_at_zv_p
16620 && pt_row == NULL)))
16621 pt_row = first_row_to_display;
16622 }
16623
16624 /* Start displaying at the start of first_row_to_display. */
16625 eassert (first_row_to_display->y < yb);
16626 init_to_row_start (&it, w, first_row_to_display);
16627
16628 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16629 - start_vpos);
16630 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16631 - nrows_scrolled);
16632 it.current_y = (first_row_to_display->y - first_reusable_row->y
16633 + WINDOW_HEADER_LINE_HEIGHT (w));
16634
16635 /* Display lines beginning with first_row_to_display in the
16636 desired matrix. Set last_text_row to the last row displayed
16637 that displays text. */
16638 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16639 if (pt_row == NULL)
16640 w->cursor.vpos = -1;
16641 last_text_row = NULL;
16642 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16643 if (display_line (&it))
16644 last_text_row = it.glyph_row - 1;
16645
16646 /* If point is in a reused row, adjust y and vpos of the cursor
16647 position. */
16648 if (pt_row)
16649 {
16650 w->cursor.vpos -= nrows_scrolled;
16651 w->cursor.y -= first_reusable_row->y - start_row->y;
16652 }
16653
16654 /* Give up if point isn't in a row displayed or reused. (This
16655 also handles the case where w->cursor.vpos < nrows_scrolled
16656 after the calls to display_line, which can happen with scroll
16657 margins. See bug#1295.) */
16658 if (w->cursor.vpos < 0)
16659 {
16660 clear_glyph_matrix (w->desired_matrix);
16661 return 0;
16662 }
16663
16664 /* Scroll the display. */
16665 run.current_y = first_reusable_row->y;
16666 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16667 run.height = it.last_visible_y - run.current_y;
16668 dy = run.current_y - run.desired_y;
16669
16670 if (run.height)
16671 {
16672 update_begin (f);
16673 FRAME_RIF (f)->update_window_begin_hook (w);
16674 FRAME_RIF (f)->clear_window_mouse_face (w);
16675 FRAME_RIF (f)->scroll_run_hook (w, &run);
16676 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16677 update_end (f);
16678 }
16679
16680 /* Adjust Y positions of reused rows. */
16681 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16682 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16683 max_y = it.last_visible_y;
16684 for (row = first_reusable_row; row < first_row_to_display; ++row)
16685 {
16686 row->y -= dy;
16687 row->visible_height = row->height;
16688 if (row->y < min_y)
16689 row->visible_height -= min_y - row->y;
16690 if (row->y + row->height > max_y)
16691 row->visible_height -= row->y + row->height - max_y;
16692 if (row->fringe_bitmap_periodic_p)
16693 row->redraw_fringe_bitmaps_p = 1;
16694 }
16695
16696 /* Scroll the current matrix. */
16697 eassert (nrows_scrolled > 0);
16698 rotate_matrix (w->current_matrix,
16699 start_vpos,
16700 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16701 -nrows_scrolled);
16702
16703 /* Disable rows not reused. */
16704 for (row -= nrows_scrolled; row < bottom_row; ++row)
16705 row->enabled_p = 0;
16706
16707 /* Point may have moved to a different line, so we cannot assume that
16708 the previous cursor position is valid; locate the correct row. */
16709 if (pt_row)
16710 {
16711 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16712 row < bottom_row
16713 && PT >= MATRIX_ROW_END_CHARPOS (row)
16714 && !row->ends_at_zv_p;
16715 row++)
16716 {
16717 w->cursor.vpos++;
16718 w->cursor.y = row->y;
16719 }
16720 if (row < bottom_row)
16721 {
16722 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16723 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16724
16725 /* Can't use this optimization with bidi-reordered glyph
16726 rows, unless cursor is already at point. */
16727 if (!NILP (BVAR (XBUFFER (WVAR (w, buffer)), bidi_display_reordering)))
16728 {
16729 if (!(w->cursor.hpos >= 0
16730 && w->cursor.hpos < row->used[TEXT_AREA]
16731 && BUFFERP (glyph->object)
16732 && glyph->charpos == PT))
16733 return 0;
16734 }
16735 else
16736 for (; glyph < end
16737 && (!BUFFERP (glyph->object)
16738 || glyph->charpos < PT);
16739 glyph++)
16740 {
16741 w->cursor.hpos++;
16742 w->cursor.x += glyph->pixel_width;
16743 }
16744 }
16745 }
16746
16747 /* Adjust window end. A null value of last_text_row means that
16748 the window end is in reused rows which in turn means that
16749 only its vpos can have changed. */
16750 if (last_text_row)
16751 {
16752 w->window_end_bytepos
16753 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16754 WVAR (w, window_end_pos)
16755 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16756 WVAR (w, window_end_vpos)
16757 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16758 }
16759 else
16760 {
16761 WVAR (w, window_end_vpos)
16762 = make_number (XFASTINT (WVAR (w, window_end_vpos)) - nrows_scrolled);
16763 }
16764
16765 WVAR (w, window_end_valid) = Qnil;
16766 w->desired_matrix->no_scrolling_p = 1;
16767
16768 #ifdef GLYPH_DEBUG
16769 debug_method_add (w, "try_window_reusing_current_matrix 2");
16770 #endif
16771 return 1;
16772 }
16773
16774 return 0;
16775 }
16776
16777
16778 \f
16779 /************************************************************************
16780 Window redisplay reusing current matrix when buffer has changed
16781 ************************************************************************/
16782
16783 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16784 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16785 ptrdiff_t *, ptrdiff_t *);
16786 static struct glyph_row *
16787 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16788 struct glyph_row *);
16789
16790
16791 /* Return the last row in MATRIX displaying text. If row START is
16792 non-null, start searching with that row. IT gives the dimensions
16793 of the display. Value is null if matrix is empty; otherwise it is
16794 a pointer to the row found. */
16795
16796 static struct glyph_row *
16797 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16798 struct glyph_row *start)
16799 {
16800 struct glyph_row *row, *row_found;
16801
16802 /* Set row_found to the last row in IT->w's current matrix
16803 displaying text. The loop looks funny but think of partially
16804 visible lines. */
16805 row_found = NULL;
16806 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16807 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16808 {
16809 eassert (row->enabled_p);
16810 row_found = row;
16811 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16812 break;
16813 ++row;
16814 }
16815
16816 return row_found;
16817 }
16818
16819
16820 /* Return the last row in the current matrix of W that is not affected
16821 by changes at the start of current_buffer that occurred since W's
16822 current matrix was built. Value is null if no such row exists.
16823
16824 BEG_UNCHANGED us the number of characters unchanged at the start of
16825 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16826 first changed character in current_buffer. Characters at positions <
16827 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16828 when the current matrix was built. */
16829
16830 static struct glyph_row *
16831 find_last_unchanged_at_beg_row (struct window *w)
16832 {
16833 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16834 struct glyph_row *row;
16835 struct glyph_row *row_found = NULL;
16836 int yb = window_text_bottom_y (w);
16837
16838 /* Find the last row displaying unchanged text. */
16839 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16840 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16841 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16842 ++row)
16843 {
16844 if (/* If row ends before first_changed_pos, it is unchanged,
16845 except in some case. */
16846 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16847 /* When row ends in ZV and we write at ZV it is not
16848 unchanged. */
16849 && !row->ends_at_zv_p
16850 /* When first_changed_pos is the end of a continued line,
16851 row is not unchanged because it may be no longer
16852 continued. */
16853 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16854 && (row->continued_p
16855 || row->exact_window_width_line_p))
16856 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16857 needs to be recomputed, so don't consider this row as
16858 unchanged. This happens when the last line was
16859 bidi-reordered and was killed immediately before this
16860 redisplay cycle. In that case, ROW->end stores the
16861 buffer position of the first visual-order character of
16862 the killed text, which is now beyond ZV. */
16863 && CHARPOS (row->end.pos) <= ZV)
16864 row_found = row;
16865
16866 /* Stop if last visible row. */
16867 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16868 break;
16869 }
16870
16871 return row_found;
16872 }
16873
16874
16875 /* Find the first glyph row in the current matrix of W that is not
16876 affected by changes at the end of current_buffer since the
16877 time W's current matrix was built.
16878
16879 Return in *DELTA the number of chars by which buffer positions in
16880 unchanged text at the end of current_buffer must be adjusted.
16881
16882 Return in *DELTA_BYTES the corresponding number of bytes.
16883
16884 Value is null if no such row exists, i.e. all rows are affected by
16885 changes. */
16886
16887 static struct glyph_row *
16888 find_first_unchanged_at_end_row (struct window *w,
16889 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16890 {
16891 struct glyph_row *row;
16892 struct glyph_row *row_found = NULL;
16893
16894 *delta = *delta_bytes = 0;
16895
16896 /* Display must not have been paused, otherwise the current matrix
16897 is not up to date. */
16898 eassert (!NILP (WVAR (w, window_end_valid)));
16899
16900 /* A value of window_end_pos >= END_UNCHANGED means that the window
16901 end is in the range of changed text. If so, there is no
16902 unchanged row at the end of W's current matrix. */
16903 if (XFASTINT (WVAR (w, window_end_pos)) >= END_UNCHANGED)
16904 return NULL;
16905
16906 /* Set row to the last row in W's current matrix displaying text. */
16907 row = MATRIX_ROW (w->current_matrix, XFASTINT (WVAR (w, window_end_vpos)));
16908
16909 /* If matrix is entirely empty, no unchanged row exists. */
16910 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16911 {
16912 /* The value of row is the last glyph row in the matrix having a
16913 meaningful buffer position in it. The end position of row
16914 corresponds to window_end_pos. This allows us to translate
16915 buffer positions in the current matrix to current buffer
16916 positions for characters not in changed text. */
16917 ptrdiff_t Z_old =
16918 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (WVAR (w, window_end_pos));
16919 ptrdiff_t Z_BYTE_old =
16920 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16921 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16922 struct glyph_row *first_text_row
16923 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16924
16925 *delta = Z - Z_old;
16926 *delta_bytes = Z_BYTE - Z_BYTE_old;
16927
16928 /* Set last_unchanged_pos to the buffer position of the last
16929 character in the buffer that has not been changed. Z is the
16930 index + 1 of the last character in current_buffer, i.e. by
16931 subtracting END_UNCHANGED we get the index of the last
16932 unchanged character, and we have to add BEG to get its buffer
16933 position. */
16934 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16935 last_unchanged_pos_old = last_unchanged_pos - *delta;
16936
16937 /* Search backward from ROW for a row displaying a line that
16938 starts at a minimum position >= last_unchanged_pos_old. */
16939 for (; row > first_text_row; --row)
16940 {
16941 /* This used to abort, but it can happen.
16942 It is ok to just stop the search instead here. KFS. */
16943 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16944 break;
16945
16946 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16947 row_found = row;
16948 }
16949 }
16950
16951 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16952
16953 return row_found;
16954 }
16955
16956
16957 /* Make sure that glyph rows in the current matrix of window W
16958 reference the same glyph memory as corresponding rows in the
16959 frame's frame matrix. This function is called after scrolling W's
16960 current matrix on a terminal frame in try_window_id and
16961 try_window_reusing_current_matrix. */
16962
16963 static void
16964 sync_frame_with_window_matrix_rows (struct window *w)
16965 {
16966 struct frame *f = XFRAME (WVAR (w, frame));
16967 struct glyph_row *window_row, *window_row_end, *frame_row;
16968
16969 /* Preconditions: W must be a leaf window and full-width. Its frame
16970 must have a frame matrix. */
16971 eassert (NILP (WVAR (w, hchild)) && NILP (WVAR (w, vchild)));
16972 eassert (WINDOW_FULL_WIDTH_P (w));
16973 eassert (!FRAME_WINDOW_P (f));
16974
16975 /* If W is a full-width window, glyph pointers in W's current matrix
16976 have, by definition, to be the same as glyph pointers in the
16977 corresponding frame matrix. Note that frame matrices have no
16978 marginal areas (see build_frame_matrix). */
16979 window_row = w->current_matrix->rows;
16980 window_row_end = window_row + w->current_matrix->nrows;
16981 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16982 while (window_row < window_row_end)
16983 {
16984 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16985 struct glyph *end = window_row->glyphs[LAST_AREA];
16986
16987 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16988 frame_row->glyphs[TEXT_AREA] = start;
16989 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16990 frame_row->glyphs[LAST_AREA] = end;
16991
16992 /* Disable frame rows whose corresponding window rows have
16993 been disabled in try_window_id. */
16994 if (!window_row->enabled_p)
16995 frame_row->enabled_p = 0;
16996
16997 ++window_row, ++frame_row;
16998 }
16999 }
17000
17001
17002 /* Find the glyph row in window W containing CHARPOS. Consider all
17003 rows between START and END (not inclusive). END null means search
17004 all rows to the end of the display area of W. Value is the row
17005 containing CHARPOS or null. */
17006
17007 struct glyph_row *
17008 row_containing_pos (struct window *w, ptrdiff_t charpos,
17009 struct glyph_row *start, struct glyph_row *end, int dy)
17010 {
17011 struct glyph_row *row = start;
17012 struct glyph_row *best_row = NULL;
17013 ptrdiff_t mindif = BUF_ZV (XBUFFER (WVAR (w, buffer))) + 1;
17014 int last_y;
17015
17016 /* If we happen to start on a header-line, skip that. */
17017 if (row->mode_line_p)
17018 ++row;
17019
17020 if ((end && row >= end) || !row->enabled_p)
17021 return NULL;
17022
17023 last_y = window_text_bottom_y (w) - dy;
17024
17025 while (1)
17026 {
17027 /* Give up if we have gone too far. */
17028 if (end && row >= end)
17029 return NULL;
17030 /* This formerly returned if they were equal.
17031 I think that both quantities are of a "last plus one" type;
17032 if so, when they are equal, the row is within the screen. -- rms. */
17033 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17034 return NULL;
17035
17036 /* If it is in this row, return this row. */
17037 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17038 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17039 /* The end position of a row equals the start
17040 position of the next row. If CHARPOS is there, we
17041 would rather display it in the next line, except
17042 when this line ends in ZV. */
17043 && !row->ends_at_zv_p
17044 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17045 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17046 {
17047 struct glyph *g;
17048
17049 if (NILP (BVAR (XBUFFER (WVAR (w, buffer)), bidi_display_reordering))
17050 || (!best_row && !row->continued_p))
17051 return row;
17052 /* In bidi-reordered rows, there could be several rows
17053 occluding point, all of them belonging to the same
17054 continued line. We need to find the row which fits
17055 CHARPOS the best. */
17056 for (g = row->glyphs[TEXT_AREA];
17057 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17058 g++)
17059 {
17060 if (!STRINGP (g->object))
17061 {
17062 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17063 {
17064 mindif = eabs (g->charpos - charpos);
17065 best_row = row;
17066 /* Exact match always wins. */
17067 if (mindif == 0)
17068 return best_row;
17069 }
17070 }
17071 }
17072 }
17073 else if (best_row && !row->continued_p)
17074 return best_row;
17075 ++row;
17076 }
17077 }
17078
17079
17080 /* Try to redisplay window W by reusing its existing display. W's
17081 current matrix must be up to date when this function is called,
17082 i.e. window_end_valid must not be nil.
17083
17084 Value is
17085
17086 1 if display has been updated
17087 0 if otherwise unsuccessful
17088 -1 if redisplay with same window start is known not to succeed
17089
17090 The following steps are performed:
17091
17092 1. Find the last row in the current matrix of W that is not
17093 affected by changes at the start of current_buffer. If no such row
17094 is found, give up.
17095
17096 2. Find the first row in W's current matrix that is not affected by
17097 changes at the end of current_buffer. Maybe there is no such row.
17098
17099 3. Display lines beginning with the row + 1 found in step 1 to the
17100 row found in step 2 or, if step 2 didn't find a row, to the end of
17101 the window.
17102
17103 4. If cursor is not known to appear on the window, give up.
17104
17105 5. If display stopped at the row found in step 2, scroll the
17106 display and current matrix as needed.
17107
17108 6. Maybe display some lines at the end of W, if we must. This can
17109 happen under various circumstances, like a partially visible line
17110 becoming fully visible, or because newly displayed lines are displayed
17111 in smaller font sizes.
17112
17113 7. Update W's window end information. */
17114
17115 static int
17116 try_window_id (struct window *w)
17117 {
17118 struct frame *f = XFRAME (WVAR (w, frame));
17119 struct glyph_matrix *current_matrix = w->current_matrix;
17120 struct glyph_matrix *desired_matrix = w->desired_matrix;
17121 struct glyph_row *last_unchanged_at_beg_row;
17122 struct glyph_row *first_unchanged_at_end_row;
17123 struct glyph_row *row;
17124 struct glyph_row *bottom_row;
17125 int bottom_vpos;
17126 struct it it;
17127 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17128 int dvpos, dy;
17129 struct text_pos start_pos;
17130 struct run run;
17131 int first_unchanged_at_end_vpos = 0;
17132 struct glyph_row *last_text_row, *last_text_row_at_end;
17133 struct text_pos start;
17134 ptrdiff_t first_changed_charpos, last_changed_charpos;
17135
17136 #ifdef GLYPH_DEBUG
17137 if (inhibit_try_window_id)
17138 return 0;
17139 #endif
17140
17141 /* This is handy for debugging. */
17142 #if 0
17143 #define GIVE_UP(X) \
17144 do { \
17145 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17146 return 0; \
17147 } while (0)
17148 #else
17149 #define GIVE_UP(X) return 0
17150 #endif
17151
17152 SET_TEXT_POS_FROM_MARKER (start, WVAR (w, start));
17153
17154 /* Don't use this for mini-windows because these can show
17155 messages and mini-buffers, and we don't handle that here. */
17156 if (MINI_WINDOW_P (w))
17157 GIVE_UP (1);
17158
17159 /* This flag is used to prevent redisplay optimizations. */
17160 if (windows_or_buffers_changed || cursor_type_changed)
17161 GIVE_UP (2);
17162
17163 /* Verify that narrowing has not changed.
17164 Also verify that we were not told to prevent redisplay optimizations.
17165 It would be nice to further
17166 reduce the number of cases where this prevents try_window_id. */
17167 if (current_buffer->clip_changed
17168 || current_buffer->prevent_redisplay_optimizations_p)
17169 GIVE_UP (3);
17170
17171 /* Window must either use window-based redisplay or be full width. */
17172 if (!FRAME_WINDOW_P (f)
17173 && (!FRAME_LINE_INS_DEL_OK (f)
17174 || !WINDOW_FULL_WIDTH_P (w)))
17175 GIVE_UP (4);
17176
17177 /* Give up if point is known NOT to appear in W. */
17178 if (PT < CHARPOS (start))
17179 GIVE_UP (5);
17180
17181 /* Another way to prevent redisplay optimizations. */
17182 if (w->last_modified == 0)
17183 GIVE_UP (6);
17184
17185 /* Verify that window is not hscrolled. */
17186 if (w->hscroll != 0)
17187 GIVE_UP (7);
17188
17189 /* Verify that display wasn't paused. */
17190 if (NILP (WVAR (w, window_end_valid)))
17191 GIVE_UP (8);
17192
17193 /* Can't use this if highlighting a region because a cursor movement
17194 will do more than just set the cursor. */
17195 if (!NILP (Vtransient_mark_mode)
17196 && !NILP (BVAR (current_buffer, mark_active)))
17197 GIVE_UP (9);
17198
17199 /* Likewise if highlighting trailing whitespace. */
17200 if (!NILP (Vshow_trailing_whitespace))
17201 GIVE_UP (11);
17202
17203 /* Likewise if showing a region. */
17204 if (!NILP (WVAR (w, region_showing)))
17205 GIVE_UP (10);
17206
17207 /* Can't use this if overlay arrow position and/or string have
17208 changed. */
17209 if (overlay_arrows_changed_p ())
17210 GIVE_UP (12);
17211
17212 /* When word-wrap is on, adding a space to the first word of a
17213 wrapped line can change the wrap position, altering the line
17214 above it. It might be worthwhile to handle this more
17215 intelligently, but for now just redisplay from scratch. */
17216 if (!NILP (BVAR (XBUFFER (WVAR (w, buffer)), word_wrap)))
17217 GIVE_UP (21);
17218
17219 /* Under bidi reordering, adding or deleting a character in the
17220 beginning of a paragraph, before the first strong directional
17221 character, can change the base direction of the paragraph (unless
17222 the buffer specifies a fixed paragraph direction), which will
17223 require to redisplay the whole paragraph. It might be worthwhile
17224 to find the paragraph limits and widen the range of redisplayed
17225 lines to that, but for now just give up this optimization and
17226 redisplay from scratch. */
17227 if (!NILP (BVAR (XBUFFER (WVAR (w, buffer)), bidi_display_reordering))
17228 && NILP (BVAR (XBUFFER (WVAR (w, buffer)), bidi_paragraph_direction)))
17229 GIVE_UP (22);
17230
17231 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17232 only if buffer has really changed. The reason is that the gap is
17233 initially at Z for freshly visited files. The code below would
17234 set end_unchanged to 0 in that case. */
17235 if (MODIFF > SAVE_MODIFF
17236 /* This seems to happen sometimes after saving a buffer. */
17237 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17238 {
17239 if (GPT - BEG < BEG_UNCHANGED)
17240 BEG_UNCHANGED = GPT - BEG;
17241 if (Z - GPT < END_UNCHANGED)
17242 END_UNCHANGED = Z - GPT;
17243 }
17244
17245 /* The position of the first and last character that has been changed. */
17246 first_changed_charpos = BEG + BEG_UNCHANGED;
17247 last_changed_charpos = Z - END_UNCHANGED;
17248
17249 /* If window starts after a line end, and the last change is in
17250 front of that newline, then changes don't affect the display.
17251 This case happens with stealth-fontification. Note that although
17252 the display is unchanged, glyph positions in the matrix have to
17253 be adjusted, of course. */
17254 row = MATRIX_ROW (w->current_matrix, XFASTINT (WVAR (w, window_end_vpos)));
17255 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17256 && ((last_changed_charpos < CHARPOS (start)
17257 && CHARPOS (start) == BEGV)
17258 || (last_changed_charpos < CHARPOS (start) - 1
17259 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17260 {
17261 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17262 struct glyph_row *r0;
17263
17264 /* Compute how many chars/bytes have been added to or removed
17265 from the buffer. */
17266 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (WVAR (w, window_end_pos));
17267 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17268 Z_delta = Z - Z_old;
17269 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17270
17271 /* Give up if PT is not in the window. Note that it already has
17272 been checked at the start of try_window_id that PT is not in
17273 front of the window start. */
17274 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17275 GIVE_UP (13);
17276
17277 /* If window start is unchanged, we can reuse the whole matrix
17278 as is, after adjusting glyph positions. No need to compute
17279 the window end again, since its offset from Z hasn't changed. */
17280 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17281 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17282 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17283 /* PT must not be in a partially visible line. */
17284 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17285 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17286 {
17287 /* Adjust positions in the glyph matrix. */
17288 if (Z_delta || Z_delta_bytes)
17289 {
17290 struct glyph_row *r1
17291 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17292 increment_matrix_positions (w->current_matrix,
17293 MATRIX_ROW_VPOS (r0, current_matrix),
17294 MATRIX_ROW_VPOS (r1, current_matrix),
17295 Z_delta, Z_delta_bytes);
17296 }
17297
17298 /* Set the cursor. */
17299 row = row_containing_pos (w, PT, r0, NULL, 0);
17300 if (row)
17301 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17302 else
17303 abort ();
17304 return 1;
17305 }
17306 }
17307
17308 /* Handle the case that changes are all below what is displayed in
17309 the window, and that PT is in the window. This shortcut cannot
17310 be taken if ZV is visible in the window, and text has been added
17311 there that is visible in the window. */
17312 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17313 /* ZV is not visible in the window, or there are no
17314 changes at ZV, actually. */
17315 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17316 || first_changed_charpos == last_changed_charpos))
17317 {
17318 struct glyph_row *r0;
17319
17320 /* Give up if PT is not in the window. Note that it already has
17321 been checked at the start of try_window_id that PT is not in
17322 front of the window start. */
17323 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17324 GIVE_UP (14);
17325
17326 /* If window start is unchanged, we can reuse the whole matrix
17327 as is, without changing glyph positions since no text has
17328 been added/removed in front of the window end. */
17329 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17330 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17331 /* PT must not be in a partially visible line. */
17332 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17333 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17334 {
17335 /* We have to compute the window end anew since text
17336 could have been added/removed after it. */
17337 WVAR (w, window_end_pos)
17338 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17339 w->window_end_bytepos
17340 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17341
17342 /* Set the cursor. */
17343 row = row_containing_pos (w, PT, r0, NULL, 0);
17344 if (row)
17345 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17346 else
17347 abort ();
17348 return 2;
17349 }
17350 }
17351
17352 /* Give up if window start is in the changed area.
17353
17354 The condition used to read
17355
17356 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17357
17358 but why that was tested escapes me at the moment. */
17359 if (CHARPOS (start) >= first_changed_charpos
17360 && CHARPOS (start) <= last_changed_charpos)
17361 GIVE_UP (15);
17362
17363 /* Check that window start agrees with the start of the first glyph
17364 row in its current matrix. Check this after we know the window
17365 start is not in changed text, otherwise positions would not be
17366 comparable. */
17367 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17368 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17369 GIVE_UP (16);
17370
17371 /* Give up if the window ends in strings. Overlay strings
17372 at the end are difficult to handle, so don't try. */
17373 row = MATRIX_ROW (current_matrix, XFASTINT (WVAR (w, window_end_vpos)));
17374 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17375 GIVE_UP (20);
17376
17377 /* Compute the position at which we have to start displaying new
17378 lines. Some of the lines at the top of the window might be
17379 reusable because they are not displaying changed text. Find the
17380 last row in W's current matrix not affected by changes at the
17381 start of current_buffer. Value is null if changes start in the
17382 first line of window. */
17383 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17384 if (last_unchanged_at_beg_row)
17385 {
17386 /* Avoid starting to display in the middle of a character, a TAB
17387 for instance. This is easier than to set up the iterator
17388 exactly, and it's not a frequent case, so the additional
17389 effort wouldn't really pay off. */
17390 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17391 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17392 && last_unchanged_at_beg_row > w->current_matrix->rows)
17393 --last_unchanged_at_beg_row;
17394
17395 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17396 GIVE_UP (17);
17397
17398 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17399 GIVE_UP (18);
17400 start_pos = it.current.pos;
17401
17402 /* Start displaying new lines in the desired matrix at the same
17403 vpos we would use in the current matrix, i.e. below
17404 last_unchanged_at_beg_row. */
17405 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17406 current_matrix);
17407 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17408 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17409
17410 eassert (it.hpos == 0 && it.current_x == 0);
17411 }
17412 else
17413 {
17414 /* There are no reusable lines at the start of the window.
17415 Start displaying in the first text line. */
17416 start_display (&it, w, start);
17417 it.vpos = it.first_vpos;
17418 start_pos = it.current.pos;
17419 }
17420
17421 /* Find the first row that is not affected by changes at the end of
17422 the buffer. Value will be null if there is no unchanged row, in
17423 which case we must redisplay to the end of the window. delta
17424 will be set to the value by which buffer positions beginning with
17425 first_unchanged_at_end_row have to be adjusted due to text
17426 changes. */
17427 first_unchanged_at_end_row
17428 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17429 IF_DEBUG (debug_delta = delta);
17430 IF_DEBUG (debug_delta_bytes = delta_bytes);
17431
17432 /* Set stop_pos to the buffer position up to which we will have to
17433 display new lines. If first_unchanged_at_end_row != NULL, this
17434 is the buffer position of the start of the line displayed in that
17435 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17436 that we don't stop at a buffer position. */
17437 stop_pos = 0;
17438 if (first_unchanged_at_end_row)
17439 {
17440 eassert (last_unchanged_at_beg_row == NULL
17441 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17442
17443 /* If this is a continuation line, move forward to the next one
17444 that isn't. Changes in lines above affect this line.
17445 Caution: this may move first_unchanged_at_end_row to a row
17446 not displaying text. */
17447 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17448 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17449 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17450 < it.last_visible_y))
17451 ++first_unchanged_at_end_row;
17452
17453 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17454 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17455 >= it.last_visible_y))
17456 first_unchanged_at_end_row = NULL;
17457 else
17458 {
17459 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17460 + delta);
17461 first_unchanged_at_end_vpos
17462 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17463 eassert (stop_pos >= Z - END_UNCHANGED);
17464 }
17465 }
17466 else if (last_unchanged_at_beg_row == NULL)
17467 GIVE_UP (19);
17468
17469
17470 #ifdef GLYPH_DEBUG
17471
17472 /* Either there is no unchanged row at the end, or the one we have
17473 now displays text. This is a necessary condition for the window
17474 end pos calculation at the end of this function. */
17475 eassert (first_unchanged_at_end_row == NULL
17476 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17477
17478 debug_last_unchanged_at_beg_vpos
17479 = (last_unchanged_at_beg_row
17480 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17481 : -1);
17482 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17483
17484 #endif /* GLYPH_DEBUG */
17485
17486
17487 /* Display new lines. Set last_text_row to the last new line
17488 displayed which has text on it, i.e. might end up as being the
17489 line where the window_end_vpos is. */
17490 w->cursor.vpos = -1;
17491 last_text_row = NULL;
17492 overlay_arrow_seen = 0;
17493 while (it.current_y < it.last_visible_y
17494 && !fonts_changed_p
17495 && (first_unchanged_at_end_row == NULL
17496 || IT_CHARPOS (it) < stop_pos))
17497 {
17498 if (display_line (&it))
17499 last_text_row = it.glyph_row - 1;
17500 }
17501
17502 if (fonts_changed_p)
17503 return -1;
17504
17505
17506 /* Compute differences in buffer positions, y-positions etc. for
17507 lines reused at the bottom of the window. Compute what we can
17508 scroll. */
17509 if (first_unchanged_at_end_row
17510 /* No lines reused because we displayed everything up to the
17511 bottom of the window. */
17512 && it.current_y < it.last_visible_y)
17513 {
17514 dvpos = (it.vpos
17515 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17516 current_matrix));
17517 dy = it.current_y - first_unchanged_at_end_row->y;
17518 run.current_y = first_unchanged_at_end_row->y;
17519 run.desired_y = run.current_y + dy;
17520 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17521 }
17522 else
17523 {
17524 delta = delta_bytes = dvpos = dy
17525 = run.current_y = run.desired_y = run.height = 0;
17526 first_unchanged_at_end_row = NULL;
17527 }
17528 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17529
17530
17531 /* Find the cursor if not already found. We have to decide whether
17532 PT will appear on this window (it sometimes doesn't, but this is
17533 not a very frequent case.) This decision has to be made before
17534 the current matrix is altered. A value of cursor.vpos < 0 means
17535 that PT is either in one of the lines beginning at
17536 first_unchanged_at_end_row or below the window. Don't care for
17537 lines that might be displayed later at the window end; as
17538 mentioned, this is not a frequent case. */
17539 if (w->cursor.vpos < 0)
17540 {
17541 /* Cursor in unchanged rows at the top? */
17542 if (PT < CHARPOS (start_pos)
17543 && last_unchanged_at_beg_row)
17544 {
17545 row = row_containing_pos (w, PT,
17546 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17547 last_unchanged_at_beg_row + 1, 0);
17548 if (row)
17549 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17550 }
17551
17552 /* Start from first_unchanged_at_end_row looking for PT. */
17553 else if (first_unchanged_at_end_row)
17554 {
17555 row = row_containing_pos (w, PT - delta,
17556 first_unchanged_at_end_row, NULL, 0);
17557 if (row)
17558 set_cursor_from_row (w, row, w->current_matrix, delta,
17559 delta_bytes, dy, dvpos);
17560 }
17561
17562 /* Give up if cursor was not found. */
17563 if (w->cursor.vpos < 0)
17564 {
17565 clear_glyph_matrix (w->desired_matrix);
17566 return -1;
17567 }
17568 }
17569
17570 /* Don't let the cursor end in the scroll margins. */
17571 {
17572 int this_scroll_margin, cursor_height;
17573
17574 this_scroll_margin =
17575 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17576 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17577 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17578
17579 if ((w->cursor.y < this_scroll_margin
17580 && CHARPOS (start) > BEGV)
17581 /* Old redisplay didn't take scroll margin into account at the bottom,
17582 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17583 || (w->cursor.y + (make_cursor_line_fully_visible_p
17584 ? cursor_height + this_scroll_margin
17585 : 1)) > it.last_visible_y)
17586 {
17587 w->cursor.vpos = -1;
17588 clear_glyph_matrix (w->desired_matrix);
17589 return -1;
17590 }
17591 }
17592
17593 /* Scroll the display. Do it before changing the current matrix so
17594 that xterm.c doesn't get confused about where the cursor glyph is
17595 found. */
17596 if (dy && run.height)
17597 {
17598 update_begin (f);
17599
17600 if (FRAME_WINDOW_P (f))
17601 {
17602 FRAME_RIF (f)->update_window_begin_hook (w);
17603 FRAME_RIF (f)->clear_window_mouse_face (w);
17604 FRAME_RIF (f)->scroll_run_hook (w, &run);
17605 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17606 }
17607 else
17608 {
17609 /* Terminal frame. In this case, dvpos gives the number of
17610 lines to scroll by; dvpos < 0 means scroll up. */
17611 int from_vpos
17612 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17613 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17614 int end = (WINDOW_TOP_EDGE_LINE (w)
17615 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17616 + window_internal_height (w));
17617
17618 #if defined (HAVE_GPM) || defined (MSDOS)
17619 x_clear_window_mouse_face (w);
17620 #endif
17621 /* Perform the operation on the screen. */
17622 if (dvpos > 0)
17623 {
17624 /* Scroll last_unchanged_at_beg_row to the end of the
17625 window down dvpos lines. */
17626 set_terminal_window (f, end);
17627
17628 /* On dumb terminals delete dvpos lines at the end
17629 before inserting dvpos empty lines. */
17630 if (!FRAME_SCROLL_REGION_OK (f))
17631 ins_del_lines (f, end - dvpos, -dvpos);
17632
17633 /* Insert dvpos empty lines in front of
17634 last_unchanged_at_beg_row. */
17635 ins_del_lines (f, from, dvpos);
17636 }
17637 else if (dvpos < 0)
17638 {
17639 /* Scroll up last_unchanged_at_beg_vpos to the end of
17640 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17641 set_terminal_window (f, end);
17642
17643 /* Delete dvpos lines in front of
17644 last_unchanged_at_beg_vpos. ins_del_lines will set
17645 the cursor to the given vpos and emit |dvpos| delete
17646 line sequences. */
17647 ins_del_lines (f, from + dvpos, dvpos);
17648
17649 /* On a dumb terminal insert dvpos empty lines at the
17650 end. */
17651 if (!FRAME_SCROLL_REGION_OK (f))
17652 ins_del_lines (f, end + dvpos, -dvpos);
17653 }
17654
17655 set_terminal_window (f, 0);
17656 }
17657
17658 update_end (f);
17659 }
17660
17661 /* Shift reused rows of the current matrix to the right position.
17662 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17663 text. */
17664 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17665 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17666 if (dvpos < 0)
17667 {
17668 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17669 bottom_vpos, dvpos);
17670 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17671 bottom_vpos, 0);
17672 }
17673 else if (dvpos > 0)
17674 {
17675 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17676 bottom_vpos, dvpos);
17677 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17678 first_unchanged_at_end_vpos + dvpos, 0);
17679 }
17680
17681 /* For frame-based redisplay, make sure that current frame and window
17682 matrix are in sync with respect to glyph memory. */
17683 if (!FRAME_WINDOW_P (f))
17684 sync_frame_with_window_matrix_rows (w);
17685
17686 /* Adjust buffer positions in reused rows. */
17687 if (delta || delta_bytes)
17688 increment_matrix_positions (current_matrix,
17689 first_unchanged_at_end_vpos + dvpos,
17690 bottom_vpos, delta, delta_bytes);
17691
17692 /* Adjust Y positions. */
17693 if (dy)
17694 shift_glyph_matrix (w, current_matrix,
17695 first_unchanged_at_end_vpos + dvpos,
17696 bottom_vpos, dy);
17697
17698 if (first_unchanged_at_end_row)
17699 {
17700 first_unchanged_at_end_row += dvpos;
17701 if (first_unchanged_at_end_row->y >= it.last_visible_y
17702 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17703 first_unchanged_at_end_row = NULL;
17704 }
17705
17706 /* If scrolling up, there may be some lines to display at the end of
17707 the window. */
17708 last_text_row_at_end = NULL;
17709 if (dy < 0)
17710 {
17711 /* Scrolling up can leave for example a partially visible line
17712 at the end of the window to be redisplayed. */
17713 /* Set last_row to the glyph row in the current matrix where the
17714 window end line is found. It has been moved up or down in
17715 the matrix by dvpos. */
17716 int last_vpos = XFASTINT (WVAR (w, window_end_vpos)) + dvpos;
17717 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17718
17719 /* If last_row is the window end line, it should display text. */
17720 eassert (last_row->displays_text_p);
17721
17722 /* If window end line was partially visible before, begin
17723 displaying at that line. Otherwise begin displaying with the
17724 line following it. */
17725 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17726 {
17727 init_to_row_start (&it, w, last_row);
17728 it.vpos = last_vpos;
17729 it.current_y = last_row->y;
17730 }
17731 else
17732 {
17733 init_to_row_end (&it, w, last_row);
17734 it.vpos = 1 + last_vpos;
17735 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17736 ++last_row;
17737 }
17738
17739 /* We may start in a continuation line. If so, we have to
17740 get the right continuation_lines_width and current_x. */
17741 it.continuation_lines_width = last_row->continuation_lines_width;
17742 it.hpos = it.current_x = 0;
17743
17744 /* Display the rest of the lines at the window end. */
17745 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17746 while (it.current_y < it.last_visible_y
17747 && !fonts_changed_p)
17748 {
17749 /* Is it always sure that the display agrees with lines in
17750 the current matrix? I don't think so, so we mark rows
17751 displayed invalid in the current matrix by setting their
17752 enabled_p flag to zero. */
17753 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17754 if (display_line (&it))
17755 last_text_row_at_end = it.glyph_row - 1;
17756 }
17757 }
17758
17759 /* Update window_end_pos and window_end_vpos. */
17760 if (first_unchanged_at_end_row
17761 && !last_text_row_at_end)
17762 {
17763 /* Window end line if one of the preserved rows from the current
17764 matrix. Set row to the last row displaying text in current
17765 matrix starting at first_unchanged_at_end_row, after
17766 scrolling. */
17767 eassert (first_unchanged_at_end_row->displays_text_p);
17768 row = find_last_row_displaying_text (w->current_matrix, &it,
17769 first_unchanged_at_end_row);
17770 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17771
17772 WVAR (w, window_end_pos) = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17773 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17774 WVAR (w, window_end_vpos)
17775 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17776 eassert (w->window_end_bytepos >= 0);
17777 IF_DEBUG (debug_method_add (w, "A"));
17778 }
17779 else if (last_text_row_at_end)
17780 {
17781 WVAR (w, window_end_pos)
17782 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17783 w->window_end_bytepos
17784 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17785 WVAR (w, window_end_vpos)
17786 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17787 eassert (w->window_end_bytepos >= 0);
17788 IF_DEBUG (debug_method_add (w, "B"));
17789 }
17790 else if (last_text_row)
17791 {
17792 /* We have displayed either to the end of the window or at the
17793 end of the window, i.e. the last row with text is to be found
17794 in the desired matrix. */
17795 WVAR (w, window_end_pos)
17796 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17797 w->window_end_bytepos
17798 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17799 WVAR (w, window_end_vpos)
17800 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17801 eassert (w->window_end_bytepos >= 0);
17802 }
17803 else if (first_unchanged_at_end_row == NULL
17804 && last_text_row == NULL
17805 && last_text_row_at_end == NULL)
17806 {
17807 /* Displayed to end of window, but no line containing text was
17808 displayed. Lines were deleted at the end of the window. */
17809 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17810 int vpos = XFASTINT (WVAR (w, window_end_vpos));
17811 struct glyph_row *current_row = current_matrix->rows + vpos;
17812 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17813
17814 for (row = NULL;
17815 row == NULL && vpos >= first_vpos;
17816 --vpos, --current_row, --desired_row)
17817 {
17818 if (desired_row->enabled_p)
17819 {
17820 if (desired_row->displays_text_p)
17821 row = desired_row;
17822 }
17823 else if (current_row->displays_text_p)
17824 row = current_row;
17825 }
17826
17827 eassert (row != NULL);
17828 WVAR (w, window_end_vpos) = make_number (vpos + 1);
17829 WVAR (w, window_end_pos) = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17830 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17831 eassert (w->window_end_bytepos >= 0);
17832 IF_DEBUG (debug_method_add (w, "C"));
17833 }
17834 else
17835 abort ();
17836
17837 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17838 debug_end_vpos = XFASTINT (w->window_end_vpos));
17839
17840 /* Record that display has not been completed. */
17841 WVAR (w, window_end_valid) = Qnil;
17842 w->desired_matrix->no_scrolling_p = 1;
17843 return 3;
17844
17845 #undef GIVE_UP
17846 }
17847
17848
17849 \f
17850 /***********************************************************************
17851 More debugging support
17852 ***********************************************************************/
17853
17854 #ifdef GLYPH_DEBUG
17855
17856 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17857 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17858 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17859
17860
17861 /* Dump the contents of glyph matrix MATRIX on stderr.
17862
17863 GLYPHS 0 means don't show glyph contents.
17864 GLYPHS 1 means show glyphs in short form
17865 GLYPHS > 1 means show glyphs in long form. */
17866
17867 void
17868 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17869 {
17870 int i;
17871 for (i = 0; i < matrix->nrows; ++i)
17872 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17873 }
17874
17875
17876 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17877 the glyph row and area where the glyph comes from. */
17878
17879 void
17880 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17881 {
17882 if (glyph->type == CHAR_GLYPH)
17883 {
17884 fprintf (stderr,
17885 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17886 glyph - row->glyphs[TEXT_AREA],
17887 'C',
17888 glyph->charpos,
17889 (BUFFERP (glyph->object)
17890 ? 'B'
17891 : (STRINGP (glyph->object)
17892 ? 'S'
17893 : '-')),
17894 glyph->pixel_width,
17895 glyph->u.ch,
17896 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17897 ? glyph->u.ch
17898 : '.'),
17899 glyph->face_id,
17900 glyph->left_box_line_p,
17901 glyph->right_box_line_p);
17902 }
17903 else if (glyph->type == STRETCH_GLYPH)
17904 {
17905 fprintf (stderr,
17906 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17907 glyph - row->glyphs[TEXT_AREA],
17908 'S',
17909 glyph->charpos,
17910 (BUFFERP (glyph->object)
17911 ? 'B'
17912 : (STRINGP (glyph->object)
17913 ? 'S'
17914 : '-')),
17915 glyph->pixel_width,
17916 0,
17917 '.',
17918 glyph->face_id,
17919 glyph->left_box_line_p,
17920 glyph->right_box_line_p);
17921 }
17922 else if (glyph->type == IMAGE_GLYPH)
17923 {
17924 fprintf (stderr,
17925 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17926 glyph - row->glyphs[TEXT_AREA],
17927 'I',
17928 glyph->charpos,
17929 (BUFFERP (glyph->object)
17930 ? 'B'
17931 : (STRINGP (glyph->object)
17932 ? 'S'
17933 : '-')),
17934 glyph->pixel_width,
17935 glyph->u.img_id,
17936 '.',
17937 glyph->face_id,
17938 glyph->left_box_line_p,
17939 glyph->right_box_line_p);
17940 }
17941 else if (glyph->type == COMPOSITE_GLYPH)
17942 {
17943 fprintf (stderr,
17944 " %5td %4c %6"pI"d %c %3d 0x%05x",
17945 glyph - row->glyphs[TEXT_AREA],
17946 '+',
17947 glyph->charpos,
17948 (BUFFERP (glyph->object)
17949 ? 'B'
17950 : (STRINGP (glyph->object)
17951 ? 'S'
17952 : '-')),
17953 glyph->pixel_width,
17954 glyph->u.cmp.id);
17955 if (glyph->u.cmp.automatic)
17956 fprintf (stderr,
17957 "[%d-%d]",
17958 glyph->slice.cmp.from, glyph->slice.cmp.to);
17959 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17960 glyph->face_id,
17961 glyph->left_box_line_p,
17962 glyph->right_box_line_p);
17963 }
17964 }
17965
17966
17967 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17968 GLYPHS 0 means don't show glyph contents.
17969 GLYPHS 1 means show glyphs in short form
17970 GLYPHS > 1 means show glyphs in long form. */
17971
17972 void
17973 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17974 {
17975 if (glyphs != 1)
17976 {
17977 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17978 fprintf (stderr, "======================================================================\n");
17979
17980 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17981 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17982 vpos,
17983 MATRIX_ROW_START_CHARPOS (row),
17984 MATRIX_ROW_END_CHARPOS (row),
17985 row->used[TEXT_AREA],
17986 row->contains_overlapping_glyphs_p,
17987 row->enabled_p,
17988 row->truncated_on_left_p,
17989 row->truncated_on_right_p,
17990 row->continued_p,
17991 MATRIX_ROW_CONTINUATION_LINE_P (row),
17992 row->displays_text_p,
17993 row->ends_at_zv_p,
17994 row->fill_line_p,
17995 row->ends_in_middle_of_char_p,
17996 row->starts_in_middle_of_char_p,
17997 row->mouse_face_p,
17998 row->x,
17999 row->y,
18000 row->pixel_width,
18001 row->height,
18002 row->visible_height,
18003 row->ascent,
18004 row->phys_ascent);
18005 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18006 row->end.overlay_string_index,
18007 row->continuation_lines_width);
18008 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18009 CHARPOS (row->start.string_pos),
18010 CHARPOS (row->end.string_pos));
18011 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18012 row->end.dpvec_index);
18013 }
18014
18015 if (glyphs > 1)
18016 {
18017 int area;
18018
18019 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18020 {
18021 struct glyph *glyph = row->glyphs[area];
18022 struct glyph *glyph_end = glyph + row->used[area];
18023
18024 /* Glyph for a line end in text. */
18025 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18026 ++glyph_end;
18027
18028 if (glyph < glyph_end)
18029 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18030
18031 for (; glyph < glyph_end; ++glyph)
18032 dump_glyph (row, glyph, area);
18033 }
18034 }
18035 else if (glyphs == 1)
18036 {
18037 int area;
18038
18039 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18040 {
18041 char *s = alloca (row->used[area] + 1);
18042 int i;
18043
18044 for (i = 0; i < row->used[area]; ++i)
18045 {
18046 struct glyph *glyph = row->glyphs[area] + i;
18047 if (glyph->type == CHAR_GLYPH
18048 && glyph->u.ch < 0x80
18049 && glyph->u.ch >= ' ')
18050 s[i] = glyph->u.ch;
18051 else
18052 s[i] = '.';
18053 }
18054
18055 s[i] = '\0';
18056 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18057 }
18058 }
18059 }
18060
18061
18062 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18063 Sdump_glyph_matrix, 0, 1, "p",
18064 doc: /* Dump the current matrix of the selected window to stderr.
18065 Shows contents of glyph row structures. With non-nil
18066 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18067 glyphs in short form, otherwise show glyphs in long form. */)
18068 (Lisp_Object glyphs)
18069 {
18070 struct window *w = XWINDOW (selected_window);
18071 struct buffer *buffer = XBUFFER (WVAR (w, buffer));
18072
18073 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18074 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18075 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18076 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18077 fprintf (stderr, "=============================================\n");
18078 dump_glyph_matrix (w->current_matrix,
18079 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18080 return Qnil;
18081 }
18082
18083
18084 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18085 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18086 (void)
18087 {
18088 struct frame *f = XFRAME (selected_frame);
18089 dump_glyph_matrix (f->current_matrix, 1);
18090 return Qnil;
18091 }
18092
18093
18094 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18095 doc: /* Dump glyph row ROW to stderr.
18096 GLYPH 0 means don't dump glyphs.
18097 GLYPH 1 means dump glyphs in short form.
18098 GLYPH > 1 or omitted means dump glyphs in long form. */)
18099 (Lisp_Object row, Lisp_Object glyphs)
18100 {
18101 struct glyph_matrix *matrix;
18102 EMACS_INT vpos;
18103
18104 CHECK_NUMBER (row);
18105 matrix = XWINDOW (selected_window)->current_matrix;
18106 vpos = XINT (row);
18107 if (vpos >= 0 && vpos < matrix->nrows)
18108 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18109 vpos,
18110 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18111 return Qnil;
18112 }
18113
18114
18115 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18116 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18117 GLYPH 0 means don't dump glyphs.
18118 GLYPH 1 means dump glyphs in short form.
18119 GLYPH > 1 or omitted means dump glyphs in long form. */)
18120 (Lisp_Object row, Lisp_Object glyphs)
18121 {
18122 struct frame *sf = SELECTED_FRAME ();
18123 struct glyph_matrix *m = XWINDOW (FVAR (sf, tool_bar_window))->current_matrix;
18124 EMACS_INT vpos;
18125
18126 CHECK_NUMBER (row);
18127 vpos = XINT (row);
18128 if (vpos >= 0 && vpos < m->nrows)
18129 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18130 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18131 return Qnil;
18132 }
18133
18134
18135 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18136 doc: /* Toggle tracing of redisplay.
18137 With ARG, turn tracing on if and only if ARG is positive. */)
18138 (Lisp_Object arg)
18139 {
18140 if (NILP (arg))
18141 trace_redisplay_p = !trace_redisplay_p;
18142 else
18143 {
18144 arg = Fprefix_numeric_value (arg);
18145 trace_redisplay_p = XINT (arg) > 0;
18146 }
18147
18148 return Qnil;
18149 }
18150
18151
18152 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18153 doc: /* Like `format', but print result to stderr.
18154 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18155 (ptrdiff_t nargs, Lisp_Object *args)
18156 {
18157 Lisp_Object s = Fformat (nargs, args);
18158 fprintf (stderr, "%s", SDATA (s));
18159 return Qnil;
18160 }
18161
18162 #endif /* GLYPH_DEBUG */
18163
18164
18165 \f
18166 /***********************************************************************
18167 Building Desired Matrix Rows
18168 ***********************************************************************/
18169
18170 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18171 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18172
18173 static struct glyph_row *
18174 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18175 {
18176 struct frame *f = XFRAME (WINDOW_FRAME (w));
18177 struct buffer *buffer = XBUFFER (WVAR (w, buffer));
18178 struct buffer *old = current_buffer;
18179 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18180 int arrow_len = SCHARS (overlay_arrow_string);
18181 const unsigned char *arrow_end = arrow_string + arrow_len;
18182 const unsigned char *p;
18183 struct it it;
18184 int multibyte_p;
18185 int n_glyphs_before;
18186
18187 set_buffer_temp (buffer);
18188 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18189 it.glyph_row->used[TEXT_AREA] = 0;
18190 SET_TEXT_POS (it.position, 0, 0);
18191
18192 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18193 p = arrow_string;
18194 while (p < arrow_end)
18195 {
18196 Lisp_Object face, ilisp;
18197
18198 /* Get the next character. */
18199 if (multibyte_p)
18200 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18201 else
18202 {
18203 it.c = it.char_to_display = *p, it.len = 1;
18204 if (! ASCII_CHAR_P (it.c))
18205 it.char_to_display = BYTE8_TO_CHAR (it.c);
18206 }
18207 p += it.len;
18208
18209 /* Get its face. */
18210 ilisp = make_number (p - arrow_string);
18211 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18212 it.face_id = compute_char_face (f, it.char_to_display, face);
18213
18214 /* Compute its width, get its glyphs. */
18215 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18216 SET_TEXT_POS (it.position, -1, -1);
18217 PRODUCE_GLYPHS (&it);
18218
18219 /* If this character doesn't fit any more in the line, we have
18220 to remove some glyphs. */
18221 if (it.current_x > it.last_visible_x)
18222 {
18223 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18224 break;
18225 }
18226 }
18227
18228 set_buffer_temp (old);
18229 return it.glyph_row;
18230 }
18231
18232
18233 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18234 glyphs to insert is determined by produce_special_glyphs. */
18235
18236 static void
18237 insert_left_trunc_glyphs (struct it *it)
18238 {
18239 struct it truncate_it;
18240 struct glyph *from, *end, *to, *toend;
18241
18242 eassert (!FRAME_WINDOW_P (it->f)
18243 || (!it->glyph_row->reversed_p
18244 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18245 || (it->glyph_row->reversed_p
18246 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18247
18248 /* Get the truncation glyphs. */
18249 truncate_it = *it;
18250 truncate_it.current_x = 0;
18251 truncate_it.face_id = DEFAULT_FACE_ID;
18252 truncate_it.glyph_row = &scratch_glyph_row;
18253 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18254 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18255 truncate_it.object = make_number (0);
18256 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18257
18258 /* Overwrite glyphs from IT with truncation glyphs. */
18259 if (!it->glyph_row->reversed_p)
18260 {
18261 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18262
18263 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18264 end = from + tused;
18265 to = it->glyph_row->glyphs[TEXT_AREA];
18266 toend = to + it->glyph_row->used[TEXT_AREA];
18267 if (FRAME_WINDOW_P (it->f))
18268 {
18269 /* On GUI frames, when variable-size fonts are displayed,
18270 the truncation glyphs may need more pixels than the row's
18271 glyphs they overwrite. We overwrite more glyphs to free
18272 enough screen real estate, and enlarge the stretch glyph
18273 on the right (see display_line), if there is one, to
18274 preserve the screen position of the truncation glyphs on
18275 the right. */
18276 int w = 0;
18277 struct glyph *g = to;
18278 short used;
18279
18280 /* The first glyph could be partially visible, in which case
18281 it->glyph_row->x will be negative. But we want the left
18282 truncation glyphs to be aligned at the left margin of the
18283 window, so we override the x coordinate at which the row
18284 will begin. */
18285 it->glyph_row->x = 0;
18286 while (g < toend && w < it->truncation_pixel_width)
18287 {
18288 w += g->pixel_width;
18289 ++g;
18290 }
18291 if (g - to - tused > 0)
18292 {
18293 memmove (to + tused, g, (toend - g) * sizeof(*g));
18294 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18295 }
18296 used = it->glyph_row->used[TEXT_AREA];
18297 if (it->glyph_row->truncated_on_right_p
18298 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18299 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18300 == STRETCH_GLYPH)
18301 {
18302 int extra = w - it->truncation_pixel_width;
18303
18304 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18305 }
18306 }
18307
18308 while (from < end)
18309 *to++ = *from++;
18310
18311 /* There may be padding glyphs left over. Overwrite them too. */
18312 if (!FRAME_WINDOW_P (it->f))
18313 {
18314 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18315 {
18316 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18317 while (from < end)
18318 *to++ = *from++;
18319 }
18320 }
18321
18322 if (to > toend)
18323 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18324 }
18325 else
18326 {
18327 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18328
18329 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18330 that back to front. */
18331 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18332 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18333 toend = it->glyph_row->glyphs[TEXT_AREA];
18334 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18335 if (FRAME_WINDOW_P (it->f))
18336 {
18337 int w = 0;
18338 struct glyph *g = to;
18339
18340 while (g >= toend && w < it->truncation_pixel_width)
18341 {
18342 w += g->pixel_width;
18343 --g;
18344 }
18345 if (to - g - tused > 0)
18346 to = g + tused;
18347 if (it->glyph_row->truncated_on_right_p
18348 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18349 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18350 {
18351 int extra = w - it->truncation_pixel_width;
18352
18353 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18354 }
18355 }
18356
18357 while (from >= end && to >= toend)
18358 *to-- = *from--;
18359 if (!FRAME_WINDOW_P (it->f))
18360 {
18361 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18362 {
18363 from =
18364 truncate_it.glyph_row->glyphs[TEXT_AREA]
18365 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18366 while (from >= end && to >= toend)
18367 *to-- = *from--;
18368 }
18369 }
18370 if (from >= end)
18371 {
18372 /* Need to free some room before prepending additional
18373 glyphs. */
18374 int move_by = from - end + 1;
18375 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18376 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18377
18378 for ( ; g >= g0; g--)
18379 g[move_by] = *g;
18380 while (from >= end)
18381 *to-- = *from--;
18382 it->glyph_row->used[TEXT_AREA] += move_by;
18383 }
18384 }
18385 }
18386
18387 /* Compute the hash code for ROW. */
18388 unsigned
18389 row_hash (struct glyph_row *row)
18390 {
18391 int area, k;
18392 unsigned hashval = 0;
18393
18394 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18395 for (k = 0; k < row->used[area]; ++k)
18396 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18397 + row->glyphs[area][k].u.val
18398 + row->glyphs[area][k].face_id
18399 + row->glyphs[area][k].padding_p
18400 + (row->glyphs[area][k].type << 2));
18401
18402 return hashval;
18403 }
18404
18405 /* Compute the pixel height and width of IT->glyph_row.
18406
18407 Most of the time, ascent and height of a display line will be equal
18408 to the max_ascent and max_height values of the display iterator
18409 structure. This is not the case if
18410
18411 1. We hit ZV without displaying anything. In this case, max_ascent
18412 and max_height will be zero.
18413
18414 2. We have some glyphs that don't contribute to the line height.
18415 (The glyph row flag contributes_to_line_height_p is for future
18416 pixmap extensions).
18417
18418 The first case is easily covered by using default values because in
18419 these cases, the line height does not really matter, except that it
18420 must not be zero. */
18421
18422 static void
18423 compute_line_metrics (struct it *it)
18424 {
18425 struct glyph_row *row = it->glyph_row;
18426
18427 if (FRAME_WINDOW_P (it->f))
18428 {
18429 int i, min_y, max_y;
18430
18431 /* The line may consist of one space only, that was added to
18432 place the cursor on it. If so, the row's height hasn't been
18433 computed yet. */
18434 if (row->height == 0)
18435 {
18436 if (it->max_ascent + it->max_descent == 0)
18437 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18438 row->ascent = it->max_ascent;
18439 row->height = it->max_ascent + it->max_descent;
18440 row->phys_ascent = it->max_phys_ascent;
18441 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18442 row->extra_line_spacing = it->max_extra_line_spacing;
18443 }
18444
18445 /* Compute the width of this line. */
18446 row->pixel_width = row->x;
18447 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18448 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18449
18450 eassert (row->pixel_width >= 0);
18451 eassert (row->ascent >= 0 && row->height > 0);
18452
18453 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18454 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18455
18456 /* If first line's physical ascent is larger than its logical
18457 ascent, use the physical ascent, and make the row taller.
18458 This makes accented characters fully visible. */
18459 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18460 && row->phys_ascent > row->ascent)
18461 {
18462 row->height += row->phys_ascent - row->ascent;
18463 row->ascent = row->phys_ascent;
18464 }
18465
18466 /* Compute how much of the line is visible. */
18467 row->visible_height = row->height;
18468
18469 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18470 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18471
18472 if (row->y < min_y)
18473 row->visible_height -= min_y - row->y;
18474 if (row->y + row->height > max_y)
18475 row->visible_height -= row->y + row->height - max_y;
18476 }
18477 else
18478 {
18479 row->pixel_width = row->used[TEXT_AREA];
18480 if (row->continued_p)
18481 row->pixel_width -= it->continuation_pixel_width;
18482 else if (row->truncated_on_right_p)
18483 row->pixel_width -= it->truncation_pixel_width;
18484 row->ascent = row->phys_ascent = 0;
18485 row->height = row->phys_height = row->visible_height = 1;
18486 row->extra_line_spacing = 0;
18487 }
18488
18489 /* Compute a hash code for this row. */
18490 row->hash = row_hash (row);
18491
18492 it->max_ascent = it->max_descent = 0;
18493 it->max_phys_ascent = it->max_phys_descent = 0;
18494 }
18495
18496
18497 /* Append one space to the glyph row of iterator IT if doing a
18498 window-based redisplay. The space has the same face as
18499 IT->face_id. Value is non-zero if a space was added.
18500
18501 This function is called to make sure that there is always one glyph
18502 at the end of a glyph row that the cursor can be set on under
18503 window-systems. (If there weren't such a glyph we would not know
18504 how wide and tall a box cursor should be displayed).
18505
18506 At the same time this space let's a nicely handle clearing to the
18507 end of the line if the row ends in italic text. */
18508
18509 static int
18510 append_space_for_newline (struct it *it, int default_face_p)
18511 {
18512 if (FRAME_WINDOW_P (it->f))
18513 {
18514 int n = it->glyph_row->used[TEXT_AREA];
18515
18516 if (it->glyph_row->glyphs[TEXT_AREA] + n
18517 < it->glyph_row->glyphs[1 + TEXT_AREA])
18518 {
18519 /* Save some values that must not be changed.
18520 Must save IT->c and IT->len because otherwise
18521 ITERATOR_AT_END_P wouldn't work anymore after
18522 append_space_for_newline has been called. */
18523 enum display_element_type saved_what = it->what;
18524 int saved_c = it->c, saved_len = it->len;
18525 int saved_char_to_display = it->char_to_display;
18526 int saved_x = it->current_x;
18527 int saved_face_id = it->face_id;
18528 struct text_pos saved_pos;
18529 Lisp_Object saved_object;
18530 struct face *face;
18531
18532 saved_object = it->object;
18533 saved_pos = it->position;
18534
18535 it->what = IT_CHARACTER;
18536 memset (&it->position, 0, sizeof it->position);
18537 it->object = make_number (0);
18538 it->c = it->char_to_display = ' ';
18539 it->len = 1;
18540
18541 /* If the default face was remapped, be sure to use the
18542 remapped face for the appended newline. */
18543 if (default_face_p)
18544 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18545 else if (it->face_before_selective_p)
18546 it->face_id = it->saved_face_id;
18547 face = FACE_FROM_ID (it->f, it->face_id);
18548 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18549
18550 PRODUCE_GLYPHS (it);
18551
18552 it->override_ascent = -1;
18553 it->constrain_row_ascent_descent_p = 0;
18554 it->current_x = saved_x;
18555 it->object = saved_object;
18556 it->position = saved_pos;
18557 it->what = saved_what;
18558 it->face_id = saved_face_id;
18559 it->len = saved_len;
18560 it->c = saved_c;
18561 it->char_to_display = saved_char_to_display;
18562 return 1;
18563 }
18564 }
18565
18566 return 0;
18567 }
18568
18569
18570 /* Extend the face of the last glyph in the text area of IT->glyph_row
18571 to the end of the display line. Called from display_line. If the
18572 glyph row is empty, add a space glyph to it so that we know the
18573 face to draw. Set the glyph row flag fill_line_p. If the glyph
18574 row is R2L, prepend a stretch glyph to cover the empty space to the
18575 left of the leftmost glyph. */
18576
18577 static void
18578 extend_face_to_end_of_line (struct it *it)
18579 {
18580 struct face *face, *default_face;
18581 struct frame *f = it->f;
18582
18583 /* If line is already filled, do nothing. Non window-system frames
18584 get a grace of one more ``pixel'' because their characters are
18585 1-``pixel'' wide, so they hit the equality too early. This grace
18586 is needed only for R2L rows that are not continued, to produce
18587 one extra blank where we could display the cursor. */
18588 if (it->current_x >= it->last_visible_x
18589 + (!FRAME_WINDOW_P (f)
18590 && it->glyph_row->reversed_p
18591 && !it->glyph_row->continued_p))
18592 return;
18593
18594 /* The default face, possibly remapped. */
18595 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18596
18597 /* Face extension extends the background and box of IT->face_id
18598 to the end of the line. If the background equals the background
18599 of the frame, we don't have to do anything. */
18600 if (it->face_before_selective_p)
18601 face = FACE_FROM_ID (f, it->saved_face_id);
18602 else
18603 face = FACE_FROM_ID (f, it->face_id);
18604
18605 if (FRAME_WINDOW_P (f)
18606 && it->glyph_row->displays_text_p
18607 && face->box == FACE_NO_BOX
18608 && face->background == FRAME_BACKGROUND_PIXEL (f)
18609 && !face->stipple
18610 && !it->glyph_row->reversed_p)
18611 return;
18612
18613 /* Set the glyph row flag indicating that the face of the last glyph
18614 in the text area has to be drawn to the end of the text area. */
18615 it->glyph_row->fill_line_p = 1;
18616
18617 /* If current character of IT is not ASCII, make sure we have the
18618 ASCII face. This will be automatically undone the next time
18619 get_next_display_element returns a multibyte character. Note
18620 that the character will always be single byte in unibyte
18621 text. */
18622 if (!ASCII_CHAR_P (it->c))
18623 {
18624 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18625 }
18626
18627 if (FRAME_WINDOW_P (f))
18628 {
18629 /* If the row is empty, add a space with the current face of IT,
18630 so that we know which face to draw. */
18631 if (it->glyph_row->used[TEXT_AREA] == 0)
18632 {
18633 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18634 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18635 it->glyph_row->used[TEXT_AREA] = 1;
18636 }
18637 #ifdef HAVE_WINDOW_SYSTEM
18638 if (it->glyph_row->reversed_p)
18639 {
18640 /* Prepend a stretch glyph to the row, such that the
18641 rightmost glyph will be drawn flushed all the way to the
18642 right margin of the window. The stretch glyph that will
18643 occupy the empty space, if any, to the left of the
18644 glyphs. */
18645 struct font *font = face->font ? face->font : FRAME_FONT (f);
18646 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18647 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18648 struct glyph *g;
18649 int row_width, stretch_ascent, stretch_width;
18650 struct text_pos saved_pos;
18651 int saved_face_id, saved_avoid_cursor;
18652
18653 for (row_width = 0, g = row_start; g < row_end; g++)
18654 row_width += g->pixel_width;
18655 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18656 if (stretch_width > 0)
18657 {
18658 stretch_ascent =
18659 (((it->ascent + it->descent)
18660 * FONT_BASE (font)) / FONT_HEIGHT (font));
18661 saved_pos = it->position;
18662 memset (&it->position, 0, sizeof it->position);
18663 saved_avoid_cursor = it->avoid_cursor_p;
18664 it->avoid_cursor_p = 1;
18665 saved_face_id = it->face_id;
18666 /* The last row's stretch glyph should get the default
18667 face, to avoid painting the rest of the window with
18668 the region face, if the region ends at ZV. */
18669 if (it->glyph_row->ends_at_zv_p)
18670 it->face_id = default_face->id;
18671 else
18672 it->face_id = face->id;
18673 append_stretch_glyph (it, make_number (0), stretch_width,
18674 it->ascent + it->descent, stretch_ascent);
18675 it->position = saved_pos;
18676 it->avoid_cursor_p = saved_avoid_cursor;
18677 it->face_id = saved_face_id;
18678 }
18679 }
18680 #endif /* HAVE_WINDOW_SYSTEM */
18681 }
18682 else
18683 {
18684 /* Save some values that must not be changed. */
18685 int saved_x = it->current_x;
18686 struct text_pos saved_pos;
18687 Lisp_Object saved_object;
18688 enum display_element_type saved_what = it->what;
18689 int saved_face_id = it->face_id;
18690
18691 saved_object = it->object;
18692 saved_pos = it->position;
18693
18694 it->what = IT_CHARACTER;
18695 memset (&it->position, 0, sizeof it->position);
18696 it->object = make_number (0);
18697 it->c = it->char_to_display = ' ';
18698 it->len = 1;
18699 /* The last row's blank glyphs should get the default face, to
18700 avoid painting the rest of the window with the region face,
18701 if the region ends at ZV. */
18702 if (it->glyph_row->ends_at_zv_p)
18703 it->face_id = default_face->id;
18704 else
18705 it->face_id = face->id;
18706
18707 PRODUCE_GLYPHS (it);
18708
18709 while (it->current_x <= it->last_visible_x)
18710 PRODUCE_GLYPHS (it);
18711
18712 /* Don't count these blanks really. It would let us insert a left
18713 truncation glyph below and make us set the cursor on them, maybe. */
18714 it->current_x = saved_x;
18715 it->object = saved_object;
18716 it->position = saved_pos;
18717 it->what = saved_what;
18718 it->face_id = saved_face_id;
18719 }
18720 }
18721
18722
18723 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18724 trailing whitespace. */
18725
18726 static int
18727 trailing_whitespace_p (ptrdiff_t charpos)
18728 {
18729 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18730 int c = 0;
18731
18732 while (bytepos < ZV_BYTE
18733 && (c = FETCH_CHAR (bytepos),
18734 c == ' ' || c == '\t'))
18735 ++bytepos;
18736
18737 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18738 {
18739 if (bytepos != PT_BYTE)
18740 return 1;
18741 }
18742 return 0;
18743 }
18744
18745
18746 /* Highlight trailing whitespace, if any, in ROW. */
18747
18748 static void
18749 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18750 {
18751 int used = row->used[TEXT_AREA];
18752
18753 if (used)
18754 {
18755 struct glyph *start = row->glyphs[TEXT_AREA];
18756 struct glyph *glyph = start + used - 1;
18757
18758 if (row->reversed_p)
18759 {
18760 /* Right-to-left rows need to be processed in the opposite
18761 direction, so swap the edge pointers. */
18762 glyph = start;
18763 start = row->glyphs[TEXT_AREA] + used - 1;
18764 }
18765
18766 /* Skip over glyphs inserted to display the cursor at the
18767 end of a line, for extending the face of the last glyph
18768 to the end of the line on terminals, and for truncation
18769 and continuation glyphs. */
18770 if (!row->reversed_p)
18771 {
18772 while (glyph >= start
18773 && glyph->type == CHAR_GLYPH
18774 && INTEGERP (glyph->object))
18775 --glyph;
18776 }
18777 else
18778 {
18779 while (glyph <= start
18780 && glyph->type == CHAR_GLYPH
18781 && INTEGERP (glyph->object))
18782 ++glyph;
18783 }
18784
18785 /* If last glyph is a space or stretch, and it's trailing
18786 whitespace, set the face of all trailing whitespace glyphs in
18787 IT->glyph_row to `trailing-whitespace'. */
18788 if ((row->reversed_p ? glyph <= start : glyph >= start)
18789 && BUFFERP (glyph->object)
18790 && (glyph->type == STRETCH_GLYPH
18791 || (glyph->type == CHAR_GLYPH
18792 && glyph->u.ch == ' '))
18793 && trailing_whitespace_p (glyph->charpos))
18794 {
18795 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18796 if (face_id < 0)
18797 return;
18798
18799 if (!row->reversed_p)
18800 {
18801 while (glyph >= start
18802 && BUFFERP (glyph->object)
18803 && (glyph->type == STRETCH_GLYPH
18804 || (glyph->type == CHAR_GLYPH
18805 && glyph->u.ch == ' ')))
18806 (glyph--)->face_id = face_id;
18807 }
18808 else
18809 {
18810 while (glyph <= start
18811 && BUFFERP (glyph->object)
18812 && (glyph->type == STRETCH_GLYPH
18813 || (glyph->type == CHAR_GLYPH
18814 && glyph->u.ch == ' ')))
18815 (glyph++)->face_id = face_id;
18816 }
18817 }
18818 }
18819 }
18820
18821
18822 /* Value is non-zero if glyph row ROW should be
18823 used to hold the cursor. */
18824
18825 static int
18826 cursor_row_p (struct glyph_row *row)
18827 {
18828 int result = 1;
18829
18830 if (PT == CHARPOS (row->end.pos)
18831 || PT == MATRIX_ROW_END_CHARPOS (row))
18832 {
18833 /* Suppose the row ends on a string.
18834 Unless the row is continued, that means it ends on a newline
18835 in the string. If it's anything other than a display string
18836 (e.g., a before-string from an overlay), we don't want the
18837 cursor there. (This heuristic seems to give the optimal
18838 behavior for the various types of multi-line strings.)
18839 One exception: if the string has `cursor' property on one of
18840 its characters, we _do_ want the cursor there. */
18841 if (CHARPOS (row->end.string_pos) >= 0)
18842 {
18843 if (row->continued_p)
18844 result = 1;
18845 else
18846 {
18847 /* Check for `display' property. */
18848 struct glyph *beg = row->glyphs[TEXT_AREA];
18849 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18850 struct glyph *glyph;
18851
18852 result = 0;
18853 for (glyph = end; glyph >= beg; --glyph)
18854 if (STRINGP (glyph->object))
18855 {
18856 Lisp_Object prop
18857 = Fget_char_property (make_number (PT),
18858 Qdisplay, Qnil);
18859 result =
18860 (!NILP (prop)
18861 && display_prop_string_p (prop, glyph->object));
18862 /* If there's a `cursor' property on one of the
18863 string's characters, this row is a cursor row,
18864 even though this is not a display string. */
18865 if (!result)
18866 {
18867 Lisp_Object s = glyph->object;
18868
18869 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18870 {
18871 ptrdiff_t gpos = glyph->charpos;
18872
18873 if (!NILP (Fget_char_property (make_number (gpos),
18874 Qcursor, s)))
18875 {
18876 result = 1;
18877 break;
18878 }
18879 }
18880 }
18881 break;
18882 }
18883 }
18884 }
18885 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18886 {
18887 /* If the row ends in middle of a real character,
18888 and the line is continued, we want the cursor here.
18889 That's because CHARPOS (ROW->end.pos) would equal
18890 PT if PT is before the character. */
18891 if (!row->ends_in_ellipsis_p)
18892 result = row->continued_p;
18893 else
18894 /* If the row ends in an ellipsis, then
18895 CHARPOS (ROW->end.pos) will equal point after the
18896 invisible text. We want that position to be displayed
18897 after the ellipsis. */
18898 result = 0;
18899 }
18900 /* If the row ends at ZV, display the cursor at the end of that
18901 row instead of at the start of the row below. */
18902 else if (row->ends_at_zv_p)
18903 result = 1;
18904 else
18905 result = 0;
18906 }
18907
18908 return result;
18909 }
18910
18911 \f
18912
18913 /* Push the property PROP so that it will be rendered at the current
18914 position in IT. Return 1 if PROP was successfully pushed, 0
18915 otherwise. Called from handle_line_prefix to handle the
18916 `line-prefix' and `wrap-prefix' properties. */
18917
18918 static int
18919 push_prefix_prop (struct it *it, Lisp_Object prop)
18920 {
18921 struct text_pos pos =
18922 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18923
18924 eassert (it->method == GET_FROM_BUFFER
18925 || it->method == GET_FROM_DISPLAY_VECTOR
18926 || it->method == GET_FROM_STRING);
18927
18928 /* We need to save the current buffer/string position, so it will be
18929 restored by pop_it, because iterate_out_of_display_property
18930 depends on that being set correctly, but some situations leave
18931 it->position not yet set when this function is called. */
18932 push_it (it, &pos);
18933
18934 if (STRINGP (prop))
18935 {
18936 if (SCHARS (prop) == 0)
18937 {
18938 pop_it (it);
18939 return 0;
18940 }
18941
18942 it->string = prop;
18943 it->string_from_prefix_prop_p = 1;
18944 it->multibyte_p = STRING_MULTIBYTE (it->string);
18945 it->current.overlay_string_index = -1;
18946 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18947 it->end_charpos = it->string_nchars = SCHARS (it->string);
18948 it->method = GET_FROM_STRING;
18949 it->stop_charpos = 0;
18950 it->prev_stop = 0;
18951 it->base_level_stop = 0;
18952
18953 /* Force paragraph direction to be that of the parent
18954 buffer/string. */
18955 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18956 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18957 else
18958 it->paragraph_embedding = L2R;
18959
18960 /* Set up the bidi iterator for this display string. */
18961 if (it->bidi_p)
18962 {
18963 it->bidi_it.string.lstring = it->string;
18964 it->bidi_it.string.s = NULL;
18965 it->bidi_it.string.schars = it->end_charpos;
18966 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18967 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18968 it->bidi_it.string.unibyte = !it->multibyte_p;
18969 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18970 }
18971 }
18972 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18973 {
18974 it->method = GET_FROM_STRETCH;
18975 it->object = prop;
18976 }
18977 #ifdef HAVE_WINDOW_SYSTEM
18978 else if (IMAGEP (prop))
18979 {
18980 it->what = IT_IMAGE;
18981 it->image_id = lookup_image (it->f, prop);
18982 it->method = GET_FROM_IMAGE;
18983 }
18984 #endif /* HAVE_WINDOW_SYSTEM */
18985 else
18986 {
18987 pop_it (it); /* bogus display property, give up */
18988 return 0;
18989 }
18990
18991 return 1;
18992 }
18993
18994 /* Return the character-property PROP at the current position in IT. */
18995
18996 static Lisp_Object
18997 get_it_property (struct it *it, Lisp_Object prop)
18998 {
18999 Lisp_Object position;
19000
19001 if (STRINGP (it->object))
19002 position = make_number (IT_STRING_CHARPOS (*it));
19003 else if (BUFFERP (it->object))
19004 position = make_number (IT_CHARPOS (*it));
19005 else
19006 return Qnil;
19007
19008 return Fget_char_property (position, prop, it->object);
19009 }
19010
19011 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19012
19013 static void
19014 handle_line_prefix (struct it *it)
19015 {
19016 Lisp_Object prefix;
19017
19018 if (it->continuation_lines_width > 0)
19019 {
19020 prefix = get_it_property (it, Qwrap_prefix);
19021 if (NILP (prefix))
19022 prefix = Vwrap_prefix;
19023 }
19024 else
19025 {
19026 prefix = get_it_property (it, Qline_prefix);
19027 if (NILP (prefix))
19028 prefix = Vline_prefix;
19029 }
19030 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19031 {
19032 /* If the prefix is wider than the window, and we try to wrap
19033 it, it would acquire its own wrap prefix, and so on till the
19034 iterator stack overflows. So, don't wrap the prefix. */
19035 it->line_wrap = TRUNCATE;
19036 it->avoid_cursor_p = 1;
19037 }
19038 }
19039
19040 \f
19041
19042 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19043 only for R2L lines from display_line and display_string, when they
19044 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19045 the line/string needs to be continued on the next glyph row. */
19046 static void
19047 unproduce_glyphs (struct it *it, int n)
19048 {
19049 struct glyph *glyph, *end;
19050
19051 eassert (it->glyph_row);
19052 eassert (it->glyph_row->reversed_p);
19053 eassert (it->area == TEXT_AREA);
19054 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19055
19056 if (n > it->glyph_row->used[TEXT_AREA])
19057 n = it->glyph_row->used[TEXT_AREA];
19058 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19059 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19060 for ( ; glyph < end; glyph++)
19061 glyph[-n] = *glyph;
19062 }
19063
19064 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19065 and ROW->maxpos. */
19066 static void
19067 find_row_edges (struct it *it, struct glyph_row *row,
19068 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19069 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19070 {
19071 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19072 lines' rows is implemented for bidi-reordered rows. */
19073
19074 /* ROW->minpos is the value of min_pos, the minimal buffer position
19075 we have in ROW, or ROW->start.pos if that is smaller. */
19076 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19077 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19078 else
19079 /* We didn't find buffer positions smaller than ROW->start, or
19080 didn't find _any_ valid buffer positions in any of the glyphs,
19081 so we must trust the iterator's computed positions. */
19082 row->minpos = row->start.pos;
19083 if (max_pos <= 0)
19084 {
19085 max_pos = CHARPOS (it->current.pos);
19086 max_bpos = BYTEPOS (it->current.pos);
19087 }
19088
19089 /* Here are the various use-cases for ending the row, and the
19090 corresponding values for ROW->maxpos:
19091
19092 Line ends in a newline from buffer eol_pos + 1
19093 Line is continued from buffer max_pos + 1
19094 Line is truncated on right it->current.pos
19095 Line ends in a newline from string max_pos + 1(*)
19096 (*) + 1 only when line ends in a forward scan
19097 Line is continued from string max_pos
19098 Line is continued from display vector max_pos
19099 Line is entirely from a string min_pos == max_pos
19100 Line is entirely from a display vector min_pos == max_pos
19101 Line that ends at ZV ZV
19102
19103 If you discover other use-cases, please add them here as
19104 appropriate. */
19105 if (row->ends_at_zv_p)
19106 row->maxpos = it->current.pos;
19107 else if (row->used[TEXT_AREA])
19108 {
19109 int seen_this_string = 0;
19110 struct glyph_row *r1 = row - 1;
19111
19112 /* Did we see the same display string on the previous row? */
19113 if (STRINGP (it->object)
19114 /* this is not the first row */
19115 && row > it->w->desired_matrix->rows
19116 /* previous row is not the header line */
19117 && !r1->mode_line_p
19118 /* previous row also ends in a newline from a string */
19119 && r1->ends_in_newline_from_string_p)
19120 {
19121 struct glyph *start, *end;
19122
19123 /* Search for the last glyph of the previous row that came
19124 from buffer or string. Depending on whether the row is
19125 L2R or R2L, we need to process it front to back or the
19126 other way round. */
19127 if (!r1->reversed_p)
19128 {
19129 start = r1->glyphs[TEXT_AREA];
19130 end = start + r1->used[TEXT_AREA];
19131 /* Glyphs inserted by redisplay have an integer (zero)
19132 as their object. */
19133 while (end > start
19134 && INTEGERP ((end - 1)->object)
19135 && (end - 1)->charpos <= 0)
19136 --end;
19137 if (end > start)
19138 {
19139 if (EQ ((end - 1)->object, it->object))
19140 seen_this_string = 1;
19141 }
19142 else
19143 /* If all the glyphs of the previous row were inserted
19144 by redisplay, it means the previous row was
19145 produced from a single newline, which is only
19146 possible if that newline came from the same string
19147 as the one which produced this ROW. */
19148 seen_this_string = 1;
19149 }
19150 else
19151 {
19152 end = r1->glyphs[TEXT_AREA] - 1;
19153 start = end + r1->used[TEXT_AREA];
19154 while (end < start
19155 && INTEGERP ((end + 1)->object)
19156 && (end + 1)->charpos <= 0)
19157 ++end;
19158 if (end < start)
19159 {
19160 if (EQ ((end + 1)->object, it->object))
19161 seen_this_string = 1;
19162 }
19163 else
19164 seen_this_string = 1;
19165 }
19166 }
19167 /* Take note of each display string that covers a newline only
19168 once, the first time we see it. This is for when a display
19169 string includes more than one newline in it. */
19170 if (row->ends_in_newline_from_string_p && !seen_this_string)
19171 {
19172 /* If we were scanning the buffer forward when we displayed
19173 the string, we want to account for at least one buffer
19174 position that belongs to this row (position covered by
19175 the display string), so that cursor positioning will
19176 consider this row as a candidate when point is at the end
19177 of the visual line represented by this row. This is not
19178 required when scanning back, because max_pos will already
19179 have a much larger value. */
19180 if (CHARPOS (row->end.pos) > max_pos)
19181 INC_BOTH (max_pos, max_bpos);
19182 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19183 }
19184 else if (CHARPOS (it->eol_pos) > 0)
19185 SET_TEXT_POS (row->maxpos,
19186 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19187 else if (row->continued_p)
19188 {
19189 /* If max_pos is different from IT's current position, it
19190 means IT->method does not belong to the display element
19191 at max_pos. However, it also means that the display
19192 element at max_pos was displayed in its entirety on this
19193 line, which is equivalent to saying that the next line
19194 starts at the next buffer position. */
19195 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19196 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19197 else
19198 {
19199 INC_BOTH (max_pos, max_bpos);
19200 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19201 }
19202 }
19203 else if (row->truncated_on_right_p)
19204 /* display_line already called reseat_at_next_visible_line_start,
19205 which puts the iterator at the beginning of the next line, in
19206 the logical order. */
19207 row->maxpos = it->current.pos;
19208 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19209 /* A line that is entirely from a string/image/stretch... */
19210 row->maxpos = row->minpos;
19211 else
19212 abort ();
19213 }
19214 else
19215 row->maxpos = it->current.pos;
19216 }
19217
19218 /* Construct the glyph row IT->glyph_row in the desired matrix of
19219 IT->w from text at the current position of IT. See dispextern.h
19220 for an overview of struct it. Value is non-zero if
19221 IT->glyph_row displays text, as opposed to a line displaying ZV
19222 only. */
19223
19224 static int
19225 display_line (struct it *it)
19226 {
19227 struct glyph_row *row = it->glyph_row;
19228 Lisp_Object overlay_arrow_string;
19229 struct it wrap_it;
19230 void *wrap_data = NULL;
19231 int may_wrap = 0, wrap_x IF_LINT (= 0);
19232 int wrap_row_used = -1;
19233 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19234 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19235 int wrap_row_extra_line_spacing IF_LINT (= 0);
19236 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19237 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19238 int cvpos;
19239 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19240 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19241
19242 /* We always start displaying at hpos zero even if hscrolled. */
19243 eassert (it->hpos == 0 && it->current_x == 0);
19244
19245 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19246 >= it->w->desired_matrix->nrows)
19247 {
19248 it->w->nrows_scale_factor++;
19249 fonts_changed_p = 1;
19250 return 0;
19251 }
19252
19253 /* Is IT->w showing the region? */
19254 WVAR (it->w, region_showing) = it->region_beg_charpos > 0 ? Qt : Qnil;
19255
19256 /* Clear the result glyph row and enable it. */
19257 prepare_desired_row (row);
19258
19259 row->y = it->current_y;
19260 row->start = it->start;
19261 row->continuation_lines_width = it->continuation_lines_width;
19262 row->displays_text_p = 1;
19263 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19264 it->starts_in_middle_of_char_p = 0;
19265
19266 /* Arrange the overlays nicely for our purposes. Usually, we call
19267 display_line on only one line at a time, in which case this
19268 can't really hurt too much, or we call it on lines which appear
19269 one after another in the buffer, in which case all calls to
19270 recenter_overlay_lists but the first will be pretty cheap. */
19271 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19272
19273 /* Move over display elements that are not visible because we are
19274 hscrolled. This may stop at an x-position < IT->first_visible_x
19275 if the first glyph is partially visible or if we hit a line end. */
19276 if (it->current_x < it->first_visible_x)
19277 {
19278 enum move_it_result move_result;
19279
19280 this_line_min_pos = row->start.pos;
19281 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19282 MOVE_TO_POS | MOVE_TO_X);
19283 /* If we are under a large hscroll, move_it_in_display_line_to
19284 could hit the end of the line without reaching
19285 it->first_visible_x. Pretend that we did reach it. This is
19286 especially important on a TTY, where we will call
19287 extend_face_to_end_of_line, which needs to know how many
19288 blank glyphs to produce. */
19289 if (it->current_x < it->first_visible_x
19290 && (move_result == MOVE_NEWLINE_OR_CR
19291 || move_result == MOVE_POS_MATCH_OR_ZV))
19292 it->current_x = it->first_visible_x;
19293
19294 /* Record the smallest positions seen while we moved over
19295 display elements that are not visible. This is needed by
19296 redisplay_internal for optimizing the case where the cursor
19297 stays inside the same line. The rest of this function only
19298 considers positions that are actually displayed, so
19299 RECORD_MAX_MIN_POS will not otherwise record positions that
19300 are hscrolled to the left of the left edge of the window. */
19301 min_pos = CHARPOS (this_line_min_pos);
19302 min_bpos = BYTEPOS (this_line_min_pos);
19303 }
19304 else
19305 {
19306 /* We only do this when not calling `move_it_in_display_line_to'
19307 above, because move_it_in_display_line_to calls
19308 handle_line_prefix itself. */
19309 handle_line_prefix (it);
19310 }
19311
19312 /* Get the initial row height. This is either the height of the
19313 text hscrolled, if there is any, or zero. */
19314 row->ascent = it->max_ascent;
19315 row->height = it->max_ascent + it->max_descent;
19316 row->phys_ascent = it->max_phys_ascent;
19317 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19318 row->extra_line_spacing = it->max_extra_line_spacing;
19319
19320 /* Utility macro to record max and min buffer positions seen until now. */
19321 #define RECORD_MAX_MIN_POS(IT) \
19322 do \
19323 { \
19324 int composition_p = !STRINGP ((IT)->string) \
19325 && ((IT)->what == IT_COMPOSITION); \
19326 ptrdiff_t current_pos = \
19327 composition_p ? (IT)->cmp_it.charpos \
19328 : IT_CHARPOS (*(IT)); \
19329 ptrdiff_t current_bpos = \
19330 composition_p ? CHAR_TO_BYTE (current_pos) \
19331 : IT_BYTEPOS (*(IT)); \
19332 if (current_pos < min_pos) \
19333 { \
19334 min_pos = current_pos; \
19335 min_bpos = current_bpos; \
19336 } \
19337 if (IT_CHARPOS (*it) > max_pos) \
19338 { \
19339 max_pos = IT_CHARPOS (*it); \
19340 max_bpos = IT_BYTEPOS (*it); \
19341 } \
19342 } \
19343 while (0)
19344
19345 /* Loop generating characters. The loop is left with IT on the next
19346 character to display. */
19347 while (1)
19348 {
19349 int n_glyphs_before, hpos_before, x_before;
19350 int x, nglyphs;
19351 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19352
19353 /* Retrieve the next thing to display. Value is zero if end of
19354 buffer reached. */
19355 if (!get_next_display_element (it))
19356 {
19357 /* Maybe add a space at the end of this line that is used to
19358 display the cursor there under X. Set the charpos of the
19359 first glyph of blank lines not corresponding to any text
19360 to -1. */
19361 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19362 row->exact_window_width_line_p = 1;
19363 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19364 || row->used[TEXT_AREA] == 0)
19365 {
19366 row->glyphs[TEXT_AREA]->charpos = -1;
19367 row->displays_text_p = 0;
19368
19369 if (!NILP (BVAR (XBUFFER (WVAR (it->w, buffer)), indicate_empty_lines))
19370 && (!MINI_WINDOW_P (it->w)
19371 || (minibuf_level && EQ (it->window, minibuf_window))))
19372 row->indicate_empty_line_p = 1;
19373 }
19374
19375 it->continuation_lines_width = 0;
19376 row->ends_at_zv_p = 1;
19377 /* A row that displays right-to-left text must always have
19378 its last face extended all the way to the end of line,
19379 even if this row ends in ZV, because we still write to
19380 the screen left to right. We also need to extend the
19381 last face if the default face is remapped to some
19382 different face, otherwise the functions that clear
19383 portions of the screen will clear with the default face's
19384 background color. */
19385 if (row->reversed_p
19386 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19387 extend_face_to_end_of_line (it);
19388 break;
19389 }
19390
19391 /* Now, get the metrics of what we want to display. This also
19392 generates glyphs in `row' (which is IT->glyph_row). */
19393 n_glyphs_before = row->used[TEXT_AREA];
19394 x = it->current_x;
19395
19396 /* Remember the line height so far in case the next element doesn't
19397 fit on the line. */
19398 if (it->line_wrap != TRUNCATE)
19399 {
19400 ascent = it->max_ascent;
19401 descent = it->max_descent;
19402 phys_ascent = it->max_phys_ascent;
19403 phys_descent = it->max_phys_descent;
19404
19405 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19406 {
19407 if (IT_DISPLAYING_WHITESPACE (it))
19408 may_wrap = 1;
19409 else if (may_wrap)
19410 {
19411 SAVE_IT (wrap_it, *it, wrap_data);
19412 wrap_x = x;
19413 wrap_row_used = row->used[TEXT_AREA];
19414 wrap_row_ascent = row->ascent;
19415 wrap_row_height = row->height;
19416 wrap_row_phys_ascent = row->phys_ascent;
19417 wrap_row_phys_height = row->phys_height;
19418 wrap_row_extra_line_spacing = row->extra_line_spacing;
19419 wrap_row_min_pos = min_pos;
19420 wrap_row_min_bpos = min_bpos;
19421 wrap_row_max_pos = max_pos;
19422 wrap_row_max_bpos = max_bpos;
19423 may_wrap = 0;
19424 }
19425 }
19426 }
19427
19428 PRODUCE_GLYPHS (it);
19429
19430 /* If this display element was in marginal areas, continue with
19431 the next one. */
19432 if (it->area != TEXT_AREA)
19433 {
19434 row->ascent = max (row->ascent, it->max_ascent);
19435 row->height = max (row->height, it->max_ascent + it->max_descent);
19436 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19437 row->phys_height = max (row->phys_height,
19438 it->max_phys_ascent + it->max_phys_descent);
19439 row->extra_line_spacing = max (row->extra_line_spacing,
19440 it->max_extra_line_spacing);
19441 set_iterator_to_next (it, 1);
19442 continue;
19443 }
19444
19445 /* Does the display element fit on the line? If we truncate
19446 lines, we should draw past the right edge of the window. If
19447 we don't truncate, we want to stop so that we can display the
19448 continuation glyph before the right margin. If lines are
19449 continued, there are two possible strategies for characters
19450 resulting in more than 1 glyph (e.g. tabs): Display as many
19451 glyphs as possible in this line and leave the rest for the
19452 continuation line, or display the whole element in the next
19453 line. Original redisplay did the former, so we do it also. */
19454 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19455 hpos_before = it->hpos;
19456 x_before = x;
19457
19458 if (/* Not a newline. */
19459 nglyphs > 0
19460 /* Glyphs produced fit entirely in the line. */
19461 && it->current_x < it->last_visible_x)
19462 {
19463 it->hpos += nglyphs;
19464 row->ascent = max (row->ascent, it->max_ascent);
19465 row->height = max (row->height, it->max_ascent + it->max_descent);
19466 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19467 row->phys_height = max (row->phys_height,
19468 it->max_phys_ascent + it->max_phys_descent);
19469 row->extra_line_spacing = max (row->extra_line_spacing,
19470 it->max_extra_line_spacing);
19471 if (it->current_x - it->pixel_width < it->first_visible_x)
19472 row->x = x - it->first_visible_x;
19473 /* Record the maximum and minimum buffer positions seen so
19474 far in glyphs that will be displayed by this row. */
19475 if (it->bidi_p)
19476 RECORD_MAX_MIN_POS (it);
19477 }
19478 else
19479 {
19480 int i, new_x;
19481 struct glyph *glyph;
19482
19483 for (i = 0; i < nglyphs; ++i, x = new_x)
19484 {
19485 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19486 new_x = x + glyph->pixel_width;
19487
19488 if (/* Lines are continued. */
19489 it->line_wrap != TRUNCATE
19490 && (/* Glyph doesn't fit on the line. */
19491 new_x > it->last_visible_x
19492 /* Or it fits exactly on a window system frame. */
19493 || (new_x == it->last_visible_x
19494 && FRAME_WINDOW_P (it->f)
19495 && (row->reversed_p
19496 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19497 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19498 {
19499 /* End of a continued line. */
19500
19501 if (it->hpos == 0
19502 || (new_x == it->last_visible_x
19503 && FRAME_WINDOW_P (it->f)
19504 && (row->reversed_p
19505 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19506 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19507 {
19508 /* Current glyph is the only one on the line or
19509 fits exactly on the line. We must continue
19510 the line because we can't draw the cursor
19511 after the glyph. */
19512 row->continued_p = 1;
19513 it->current_x = new_x;
19514 it->continuation_lines_width += new_x;
19515 ++it->hpos;
19516 if (i == nglyphs - 1)
19517 {
19518 /* If line-wrap is on, check if a previous
19519 wrap point was found. */
19520 if (wrap_row_used > 0
19521 /* Even if there is a previous wrap
19522 point, continue the line here as
19523 usual, if (i) the previous character
19524 was a space or tab AND (ii) the
19525 current character is not. */
19526 && (!may_wrap
19527 || IT_DISPLAYING_WHITESPACE (it)))
19528 goto back_to_wrap;
19529
19530 /* Record the maximum and minimum buffer
19531 positions seen so far in glyphs that will be
19532 displayed by this row. */
19533 if (it->bidi_p)
19534 RECORD_MAX_MIN_POS (it);
19535 set_iterator_to_next (it, 1);
19536 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19537 {
19538 if (!get_next_display_element (it))
19539 {
19540 row->exact_window_width_line_p = 1;
19541 it->continuation_lines_width = 0;
19542 row->continued_p = 0;
19543 row->ends_at_zv_p = 1;
19544 }
19545 else if (ITERATOR_AT_END_OF_LINE_P (it))
19546 {
19547 row->continued_p = 0;
19548 row->exact_window_width_line_p = 1;
19549 }
19550 }
19551 }
19552 else if (it->bidi_p)
19553 RECORD_MAX_MIN_POS (it);
19554 }
19555 else if (CHAR_GLYPH_PADDING_P (*glyph)
19556 && !FRAME_WINDOW_P (it->f))
19557 {
19558 /* A padding glyph that doesn't fit on this line.
19559 This means the whole character doesn't fit
19560 on the line. */
19561 if (row->reversed_p)
19562 unproduce_glyphs (it, row->used[TEXT_AREA]
19563 - n_glyphs_before);
19564 row->used[TEXT_AREA] = n_glyphs_before;
19565
19566 /* Fill the rest of the row with continuation
19567 glyphs like in 20.x. */
19568 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19569 < row->glyphs[1 + TEXT_AREA])
19570 produce_special_glyphs (it, IT_CONTINUATION);
19571
19572 row->continued_p = 1;
19573 it->current_x = x_before;
19574 it->continuation_lines_width += x_before;
19575
19576 /* Restore the height to what it was before the
19577 element not fitting on the line. */
19578 it->max_ascent = ascent;
19579 it->max_descent = descent;
19580 it->max_phys_ascent = phys_ascent;
19581 it->max_phys_descent = phys_descent;
19582 }
19583 else if (wrap_row_used > 0)
19584 {
19585 back_to_wrap:
19586 if (row->reversed_p)
19587 unproduce_glyphs (it,
19588 row->used[TEXT_AREA] - wrap_row_used);
19589 RESTORE_IT (it, &wrap_it, wrap_data);
19590 it->continuation_lines_width += wrap_x;
19591 row->used[TEXT_AREA] = wrap_row_used;
19592 row->ascent = wrap_row_ascent;
19593 row->height = wrap_row_height;
19594 row->phys_ascent = wrap_row_phys_ascent;
19595 row->phys_height = wrap_row_phys_height;
19596 row->extra_line_spacing = wrap_row_extra_line_spacing;
19597 min_pos = wrap_row_min_pos;
19598 min_bpos = wrap_row_min_bpos;
19599 max_pos = wrap_row_max_pos;
19600 max_bpos = wrap_row_max_bpos;
19601 row->continued_p = 1;
19602 row->ends_at_zv_p = 0;
19603 row->exact_window_width_line_p = 0;
19604 it->continuation_lines_width += x;
19605
19606 /* Make sure that a non-default face is extended
19607 up to the right margin of the window. */
19608 extend_face_to_end_of_line (it);
19609 }
19610 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19611 {
19612 /* A TAB that extends past the right edge of the
19613 window. This produces a single glyph on
19614 window system frames. We leave the glyph in
19615 this row and let it fill the row, but don't
19616 consume the TAB. */
19617 if ((row->reversed_p
19618 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19619 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19620 produce_special_glyphs (it, IT_CONTINUATION);
19621 it->continuation_lines_width += it->last_visible_x;
19622 row->ends_in_middle_of_char_p = 1;
19623 row->continued_p = 1;
19624 glyph->pixel_width = it->last_visible_x - x;
19625 it->starts_in_middle_of_char_p = 1;
19626 }
19627 else
19628 {
19629 /* Something other than a TAB that draws past
19630 the right edge of the window. Restore
19631 positions to values before the element. */
19632 if (row->reversed_p)
19633 unproduce_glyphs (it, row->used[TEXT_AREA]
19634 - (n_glyphs_before + i));
19635 row->used[TEXT_AREA] = n_glyphs_before + i;
19636
19637 /* Display continuation glyphs. */
19638 it->current_x = x_before;
19639 it->continuation_lines_width += x;
19640 if (!FRAME_WINDOW_P (it->f)
19641 || (row->reversed_p
19642 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19643 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19644 produce_special_glyphs (it, IT_CONTINUATION);
19645 row->continued_p = 1;
19646
19647 extend_face_to_end_of_line (it);
19648
19649 if (nglyphs > 1 && i > 0)
19650 {
19651 row->ends_in_middle_of_char_p = 1;
19652 it->starts_in_middle_of_char_p = 1;
19653 }
19654
19655 /* Restore the height to what it was before the
19656 element not fitting on the line. */
19657 it->max_ascent = ascent;
19658 it->max_descent = descent;
19659 it->max_phys_ascent = phys_ascent;
19660 it->max_phys_descent = phys_descent;
19661 }
19662
19663 break;
19664 }
19665 else if (new_x > it->first_visible_x)
19666 {
19667 /* Increment number of glyphs actually displayed. */
19668 ++it->hpos;
19669
19670 /* Record the maximum and minimum buffer positions
19671 seen so far in glyphs that will be displayed by
19672 this row. */
19673 if (it->bidi_p)
19674 RECORD_MAX_MIN_POS (it);
19675
19676 if (x < it->first_visible_x)
19677 /* Glyph is partially visible, i.e. row starts at
19678 negative X position. */
19679 row->x = x - it->first_visible_x;
19680 }
19681 else
19682 {
19683 /* Glyph is completely off the left margin of the
19684 window. This should not happen because of the
19685 move_it_in_display_line at the start of this
19686 function, unless the text display area of the
19687 window is empty. */
19688 eassert (it->first_visible_x <= it->last_visible_x);
19689 }
19690 }
19691 /* Even if this display element produced no glyphs at all,
19692 we want to record its position. */
19693 if (it->bidi_p && nglyphs == 0)
19694 RECORD_MAX_MIN_POS (it);
19695
19696 row->ascent = max (row->ascent, it->max_ascent);
19697 row->height = max (row->height, it->max_ascent + it->max_descent);
19698 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19699 row->phys_height = max (row->phys_height,
19700 it->max_phys_ascent + it->max_phys_descent);
19701 row->extra_line_spacing = max (row->extra_line_spacing,
19702 it->max_extra_line_spacing);
19703
19704 /* End of this display line if row is continued. */
19705 if (row->continued_p || row->ends_at_zv_p)
19706 break;
19707 }
19708
19709 at_end_of_line:
19710 /* Is this a line end? If yes, we're also done, after making
19711 sure that a non-default face is extended up to the right
19712 margin of the window. */
19713 if (ITERATOR_AT_END_OF_LINE_P (it))
19714 {
19715 int used_before = row->used[TEXT_AREA];
19716
19717 row->ends_in_newline_from_string_p = STRINGP (it->object);
19718
19719 /* Add a space at the end of the line that is used to
19720 display the cursor there. */
19721 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19722 append_space_for_newline (it, 0);
19723
19724 /* Extend the face to the end of the line. */
19725 extend_face_to_end_of_line (it);
19726
19727 /* Make sure we have the position. */
19728 if (used_before == 0)
19729 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19730
19731 /* Record the position of the newline, for use in
19732 find_row_edges. */
19733 it->eol_pos = it->current.pos;
19734
19735 /* Consume the line end. This skips over invisible lines. */
19736 set_iterator_to_next (it, 1);
19737 it->continuation_lines_width = 0;
19738 break;
19739 }
19740
19741 /* Proceed with next display element. Note that this skips
19742 over lines invisible because of selective display. */
19743 set_iterator_to_next (it, 1);
19744
19745 /* If we truncate lines, we are done when the last displayed
19746 glyphs reach past the right margin of the window. */
19747 if (it->line_wrap == TRUNCATE
19748 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19749 ? (it->current_x >= it->last_visible_x)
19750 : (it->current_x > it->last_visible_x)))
19751 {
19752 /* Maybe add truncation glyphs. */
19753 if (!FRAME_WINDOW_P (it->f)
19754 || (row->reversed_p
19755 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19756 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19757 {
19758 int i, n;
19759
19760 if (!row->reversed_p)
19761 {
19762 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19763 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19764 break;
19765 }
19766 else
19767 {
19768 for (i = 0; i < row->used[TEXT_AREA]; i++)
19769 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19770 break;
19771 /* Remove any padding glyphs at the front of ROW, to
19772 make room for the truncation glyphs we will be
19773 adding below. The loop below always inserts at
19774 least one truncation glyph, so also remove the
19775 last glyph added to ROW. */
19776 unproduce_glyphs (it, i + 1);
19777 /* Adjust i for the loop below. */
19778 i = row->used[TEXT_AREA] - (i + 1);
19779 }
19780
19781 it->current_x = x_before;
19782 if (!FRAME_WINDOW_P (it->f))
19783 {
19784 for (n = row->used[TEXT_AREA]; i < n; ++i)
19785 {
19786 row->used[TEXT_AREA] = i;
19787 produce_special_glyphs (it, IT_TRUNCATION);
19788 }
19789 }
19790 else
19791 {
19792 row->used[TEXT_AREA] = i;
19793 produce_special_glyphs (it, IT_TRUNCATION);
19794 }
19795 }
19796 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19797 {
19798 /* Don't truncate if we can overflow newline into fringe. */
19799 if (!get_next_display_element (it))
19800 {
19801 it->continuation_lines_width = 0;
19802 row->ends_at_zv_p = 1;
19803 row->exact_window_width_line_p = 1;
19804 break;
19805 }
19806 if (ITERATOR_AT_END_OF_LINE_P (it))
19807 {
19808 row->exact_window_width_line_p = 1;
19809 goto at_end_of_line;
19810 }
19811 it->current_x = x_before;
19812 }
19813
19814 row->truncated_on_right_p = 1;
19815 it->continuation_lines_width = 0;
19816 reseat_at_next_visible_line_start (it, 0);
19817 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19818 it->hpos = hpos_before;
19819 break;
19820 }
19821 }
19822
19823 if (wrap_data)
19824 bidi_unshelve_cache (wrap_data, 1);
19825
19826 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19827 at the left window margin. */
19828 if (it->first_visible_x
19829 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19830 {
19831 if (!FRAME_WINDOW_P (it->f)
19832 || (row->reversed_p
19833 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19834 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19835 insert_left_trunc_glyphs (it);
19836 row->truncated_on_left_p = 1;
19837 }
19838
19839 /* Remember the position at which this line ends.
19840
19841 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19842 cannot be before the call to find_row_edges below, since that is
19843 where these positions are determined. */
19844 row->end = it->current;
19845 if (!it->bidi_p)
19846 {
19847 row->minpos = row->start.pos;
19848 row->maxpos = row->end.pos;
19849 }
19850 else
19851 {
19852 /* ROW->minpos and ROW->maxpos must be the smallest and
19853 `1 + the largest' buffer positions in ROW. But if ROW was
19854 bidi-reordered, these two positions can be anywhere in the
19855 row, so we must determine them now. */
19856 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19857 }
19858
19859 /* If the start of this line is the overlay arrow-position, then
19860 mark this glyph row as the one containing the overlay arrow.
19861 This is clearly a mess with variable size fonts. It would be
19862 better to let it be displayed like cursors under X. */
19863 if ((row->displays_text_p || !overlay_arrow_seen)
19864 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19865 !NILP (overlay_arrow_string)))
19866 {
19867 /* Overlay arrow in window redisplay is a fringe bitmap. */
19868 if (STRINGP (overlay_arrow_string))
19869 {
19870 struct glyph_row *arrow_row
19871 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19872 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19873 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19874 struct glyph *p = row->glyphs[TEXT_AREA];
19875 struct glyph *p2, *end;
19876
19877 /* Copy the arrow glyphs. */
19878 while (glyph < arrow_end)
19879 *p++ = *glyph++;
19880
19881 /* Throw away padding glyphs. */
19882 p2 = p;
19883 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19884 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19885 ++p2;
19886 if (p2 > p)
19887 {
19888 while (p2 < end)
19889 *p++ = *p2++;
19890 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19891 }
19892 }
19893 else
19894 {
19895 eassert (INTEGERP (overlay_arrow_string));
19896 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19897 }
19898 overlay_arrow_seen = 1;
19899 }
19900
19901 /* Highlight trailing whitespace. */
19902 if (!NILP (Vshow_trailing_whitespace))
19903 highlight_trailing_whitespace (it->f, it->glyph_row);
19904
19905 /* Compute pixel dimensions of this line. */
19906 compute_line_metrics (it);
19907
19908 /* Implementation note: No changes in the glyphs of ROW or in their
19909 faces can be done past this point, because compute_line_metrics
19910 computes ROW's hash value and stores it within the glyph_row
19911 structure. */
19912
19913 /* Record whether this row ends inside an ellipsis. */
19914 row->ends_in_ellipsis_p
19915 = (it->method == GET_FROM_DISPLAY_VECTOR
19916 && it->ellipsis_p);
19917
19918 /* Save fringe bitmaps in this row. */
19919 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19920 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19921 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19922 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19923
19924 it->left_user_fringe_bitmap = 0;
19925 it->left_user_fringe_face_id = 0;
19926 it->right_user_fringe_bitmap = 0;
19927 it->right_user_fringe_face_id = 0;
19928
19929 /* Maybe set the cursor. */
19930 cvpos = it->w->cursor.vpos;
19931 if ((cvpos < 0
19932 /* In bidi-reordered rows, keep checking for proper cursor
19933 position even if one has been found already, because buffer
19934 positions in such rows change non-linearly with ROW->VPOS,
19935 when a line is continued. One exception: when we are at ZV,
19936 display cursor on the first suitable glyph row, since all
19937 the empty rows after that also have their position set to ZV. */
19938 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19939 lines' rows is implemented for bidi-reordered rows. */
19940 || (it->bidi_p
19941 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19942 && PT >= MATRIX_ROW_START_CHARPOS (row)
19943 && PT <= MATRIX_ROW_END_CHARPOS (row)
19944 && cursor_row_p (row))
19945 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19946
19947 /* Prepare for the next line. This line starts horizontally at (X
19948 HPOS) = (0 0). Vertical positions are incremented. As a
19949 convenience for the caller, IT->glyph_row is set to the next
19950 row to be used. */
19951 it->current_x = it->hpos = 0;
19952 it->current_y += row->height;
19953 SET_TEXT_POS (it->eol_pos, 0, 0);
19954 ++it->vpos;
19955 ++it->glyph_row;
19956 /* The next row should by default use the same value of the
19957 reversed_p flag as this one. set_iterator_to_next decides when
19958 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19959 the flag accordingly. */
19960 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19961 it->glyph_row->reversed_p = row->reversed_p;
19962 it->start = row->end;
19963 return row->displays_text_p;
19964
19965 #undef RECORD_MAX_MIN_POS
19966 }
19967
19968 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19969 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19970 doc: /* Return paragraph direction at point in BUFFER.
19971 Value is either `left-to-right' or `right-to-left'.
19972 If BUFFER is omitted or nil, it defaults to the current buffer.
19973
19974 Paragraph direction determines how the text in the paragraph is displayed.
19975 In left-to-right paragraphs, text begins at the left margin of the window
19976 and the reading direction is generally left to right. In right-to-left
19977 paragraphs, text begins at the right margin and is read from right to left.
19978
19979 See also `bidi-paragraph-direction'. */)
19980 (Lisp_Object buffer)
19981 {
19982 struct buffer *buf = current_buffer;
19983 struct buffer *old = buf;
19984
19985 if (! NILP (buffer))
19986 {
19987 CHECK_BUFFER (buffer);
19988 buf = XBUFFER (buffer);
19989 }
19990
19991 if (NILP (BVAR (buf, bidi_display_reordering))
19992 || NILP (BVAR (buf, enable_multibyte_characters))
19993 /* When we are loading loadup.el, the character property tables
19994 needed for bidi iteration are not yet available. */
19995 || !NILP (Vpurify_flag))
19996 return Qleft_to_right;
19997 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19998 return BVAR (buf, bidi_paragraph_direction);
19999 else
20000 {
20001 /* Determine the direction from buffer text. We could try to
20002 use current_matrix if it is up to date, but this seems fast
20003 enough as it is. */
20004 struct bidi_it itb;
20005 ptrdiff_t pos = BUF_PT (buf);
20006 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20007 int c;
20008 void *itb_data = bidi_shelve_cache ();
20009
20010 set_buffer_temp (buf);
20011 /* bidi_paragraph_init finds the base direction of the paragraph
20012 by searching forward from paragraph start. We need the base
20013 direction of the current or _previous_ paragraph, so we need
20014 to make sure we are within that paragraph. To that end, find
20015 the previous non-empty line. */
20016 if (pos >= ZV && pos > BEGV)
20017 {
20018 pos--;
20019 bytepos = CHAR_TO_BYTE (pos);
20020 }
20021 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20022 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20023 {
20024 while ((c = FETCH_BYTE (bytepos)) == '\n'
20025 || c == ' ' || c == '\t' || c == '\f')
20026 {
20027 if (bytepos <= BEGV_BYTE)
20028 break;
20029 bytepos--;
20030 pos--;
20031 }
20032 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20033 bytepos--;
20034 }
20035 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20036 itb.paragraph_dir = NEUTRAL_DIR;
20037 itb.string.s = NULL;
20038 itb.string.lstring = Qnil;
20039 itb.string.bufpos = 0;
20040 itb.string.unibyte = 0;
20041 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20042 bidi_unshelve_cache (itb_data, 0);
20043 set_buffer_temp (old);
20044 switch (itb.paragraph_dir)
20045 {
20046 case L2R:
20047 return Qleft_to_right;
20048 break;
20049 case R2L:
20050 return Qright_to_left;
20051 break;
20052 default:
20053 abort ();
20054 }
20055 }
20056 }
20057
20058
20059 \f
20060 /***********************************************************************
20061 Menu Bar
20062 ***********************************************************************/
20063
20064 /* Redisplay the menu bar in the frame for window W.
20065
20066 The menu bar of X frames that don't have X toolkit support is
20067 displayed in a special window W->frame->menu_bar_window.
20068
20069 The menu bar of terminal frames is treated specially as far as
20070 glyph matrices are concerned. Menu bar lines are not part of
20071 windows, so the update is done directly on the frame matrix rows
20072 for the menu bar. */
20073
20074 static void
20075 display_menu_bar (struct window *w)
20076 {
20077 struct frame *f = XFRAME (WINDOW_FRAME (w));
20078 struct it it;
20079 Lisp_Object items;
20080 int i;
20081
20082 /* Don't do all this for graphical frames. */
20083 #ifdef HAVE_NTGUI
20084 if (FRAME_W32_P (f))
20085 return;
20086 #endif
20087 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20088 if (FRAME_X_P (f))
20089 return;
20090 #endif
20091
20092 #ifdef HAVE_NS
20093 if (FRAME_NS_P (f))
20094 return;
20095 #endif /* HAVE_NS */
20096
20097 #ifdef USE_X_TOOLKIT
20098 eassert (!FRAME_WINDOW_P (f));
20099 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20100 it.first_visible_x = 0;
20101 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20102 #else /* not USE_X_TOOLKIT */
20103 if (FRAME_WINDOW_P (f))
20104 {
20105 /* Menu bar lines are displayed in the desired matrix of the
20106 dummy window menu_bar_window. */
20107 struct window *menu_w;
20108 eassert (WINDOWP (FVAR (f, menu_bar_window)));
20109 menu_w = XWINDOW (FVAR (f, menu_bar_window));
20110 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20111 MENU_FACE_ID);
20112 it.first_visible_x = 0;
20113 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20114 }
20115 else
20116 {
20117 /* This is a TTY frame, i.e. character hpos/vpos are used as
20118 pixel x/y. */
20119 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20120 MENU_FACE_ID);
20121 it.first_visible_x = 0;
20122 it.last_visible_x = FRAME_COLS (f);
20123 }
20124 #endif /* not USE_X_TOOLKIT */
20125
20126 /* FIXME: This should be controlled by a user option. See the
20127 comments in redisplay_tool_bar and display_mode_line about
20128 this. */
20129 it.paragraph_embedding = L2R;
20130
20131 if (! mode_line_inverse_video)
20132 /* Force the menu-bar to be displayed in the default face. */
20133 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20134
20135 /* Clear all rows of the menu bar. */
20136 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20137 {
20138 struct glyph_row *row = it.glyph_row + i;
20139 clear_glyph_row (row);
20140 row->enabled_p = 1;
20141 row->full_width_p = 1;
20142 }
20143
20144 /* Display all items of the menu bar. */
20145 items = FRAME_MENU_BAR_ITEMS (it.f);
20146 for (i = 0; i < ASIZE (items); i += 4)
20147 {
20148 Lisp_Object string;
20149
20150 /* Stop at nil string. */
20151 string = AREF (items, i + 1);
20152 if (NILP (string))
20153 break;
20154
20155 /* Remember where item was displayed. */
20156 ASET (items, i + 3, make_number (it.hpos));
20157
20158 /* Display the item, pad with one space. */
20159 if (it.current_x < it.last_visible_x)
20160 display_string (NULL, string, Qnil, 0, 0, &it,
20161 SCHARS (string) + 1, 0, 0, -1);
20162 }
20163
20164 /* Fill out the line with spaces. */
20165 if (it.current_x < it.last_visible_x)
20166 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20167
20168 /* Compute the total height of the lines. */
20169 compute_line_metrics (&it);
20170 }
20171
20172
20173 \f
20174 /***********************************************************************
20175 Mode Line
20176 ***********************************************************************/
20177
20178 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20179 FORCE is non-zero, redisplay mode lines unconditionally.
20180 Otherwise, redisplay only mode lines that are garbaged. Value is
20181 the number of windows whose mode lines were redisplayed. */
20182
20183 static int
20184 redisplay_mode_lines (Lisp_Object window, int force)
20185 {
20186 int nwindows = 0;
20187
20188 while (!NILP (window))
20189 {
20190 struct window *w = XWINDOW (window);
20191
20192 if (WINDOWP (WVAR (w, hchild)))
20193 nwindows += redisplay_mode_lines (WVAR (w, hchild), force);
20194 else if (WINDOWP (WVAR (w, vchild)))
20195 nwindows += redisplay_mode_lines (WVAR (w, vchild), force);
20196 else if (force
20197 || FRAME_GARBAGED_P (XFRAME (WVAR (w, frame)))
20198 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20199 {
20200 struct text_pos lpoint;
20201 struct buffer *old = current_buffer;
20202
20203 /* Set the window's buffer for the mode line display. */
20204 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20205 set_buffer_internal_1 (XBUFFER (WVAR (w, buffer)));
20206
20207 /* Point refers normally to the selected window. For any
20208 other window, set up appropriate value. */
20209 if (!EQ (window, selected_window))
20210 {
20211 struct text_pos pt;
20212
20213 SET_TEXT_POS_FROM_MARKER (pt, WVAR (w, pointm));
20214 if (CHARPOS (pt) < BEGV)
20215 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20216 else if (CHARPOS (pt) > (ZV - 1))
20217 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20218 else
20219 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20220 }
20221
20222 /* Display mode lines. */
20223 clear_glyph_matrix (w->desired_matrix);
20224 if (display_mode_lines (w))
20225 {
20226 ++nwindows;
20227 w->must_be_updated_p = 1;
20228 }
20229
20230 /* Restore old settings. */
20231 set_buffer_internal_1 (old);
20232 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20233 }
20234
20235 window = WVAR (w, next);
20236 }
20237
20238 return nwindows;
20239 }
20240
20241
20242 /* Display the mode and/or header line of window W. Value is the
20243 sum number of mode lines and header lines displayed. */
20244
20245 static int
20246 display_mode_lines (struct window *w)
20247 {
20248 Lisp_Object old_selected_window, old_selected_frame;
20249 int n = 0;
20250
20251 old_selected_frame = selected_frame;
20252 selected_frame = WVAR (w, frame);
20253 old_selected_window = selected_window;
20254 XSETWINDOW (selected_window, w);
20255
20256 /* These will be set while the mode line specs are processed. */
20257 line_number_displayed = 0;
20258 WVAR (w, column_number_displayed) = Qnil;
20259
20260 if (WINDOW_WANTS_MODELINE_P (w))
20261 {
20262 struct window *sel_w = XWINDOW (old_selected_window);
20263
20264 /* Select mode line face based on the real selected window. */
20265 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20266 BVAR (current_buffer, mode_line_format));
20267 ++n;
20268 }
20269
20270 if (WINDOW_WANTS_HEADER_LINE_P (w))
20271 {
20272 display_mode_line (w, HEADER_LINE_FACE_ID,
20273 BVAR (current_buffer, header_line_format));
20274 ++n;
20275 }
20276
20277 selected_frame = old_selected_frame;
20278 selected_window = old_selected_window;
20279 return n;
20280 }
20281
20282
20283 /* Display mode or header line of window W. FACE_ID specifies which
20284 line to display; it is either MODE_LINE_FACE_ID or
20285 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20286 display. Value is the pixel height of the mode/header line
20287 displayed. */
20288
20289 static int
20290 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20291 {
20292 struct it it;
20293 struct face *face;
20294 ptrdiff_t count = SPECPDL_INDEX ();
20295
20296 init_iterator (&it, w, -1, -1, NULL, face_id);
20297 /* Don't extend on a previously drawn mode-line.
20298 This may happen if called from pos_visible_p. */
20299 it.glyph_row->enabled_p = 0;
20300 prepare_desired_row (it.glyph_row);
20301
20302 it.glyph_row->mode_line_p = 1;
20303
20304 if (! mode_line_inverse_video)
20305 /* Force the mode-line to be displayed in the default face. */
20306 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20307
20308 /* FIXME: This should be controlled by a user option. But
20309 supporting such an option is not trivial, since the mode line is
20310 made up of many separate strings. */
20311 it.paragraph_embedding = L2R;
20312
20313 record_unwind_protect (unwind_format_mode_line,
20314 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20315
20316 mode_line_target = MODE_LINE_DISPLAY;
20317
20318 /* Temporarily make frame's keyboard the current kboard so that
20319 kboard-local variables in the mode_line_format will get the right
20320 values. */
20321 push_kboard (FRAME_KBOARD (it.f));
20322 record_unwind_save_match_data ();
20323 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20324 pop_kboard ();
20325
20326 unbind_to (count, Qnil);
20327
20328 /* Fill up with spaces. */
20329 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20330
20331 compute_line_metrics (&it);
20332 it.glyph_row->full_width_p = 1;
20333 it.glyph_row->continued_p = 0;
20334 it.glyph_row->truncated_on_left_p = 0;
20335 it.glyph_row->truncated_on_right_p = 0;
20336
20337 /* Make a 3D mode-line have a shadow at its right end. */
20338 face = FACE_FROM_ID (it.f, face_id);
20339 extend_face_to_end_of_line (&it);
20340 if (face->box != FACE_NO_BOX)
20341 {
20342 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20343 + it.glyph_row->used[TEXT_AREA] - 1);
20344 last->right_box_line_p = 1;
20345 }
20346
20347 return it.glyph_row->height;
20348 }
20349
20350 /* Move element ELT in LIST to the front of LIST.
20351 Return the updated list. */
20352
20353 static Lisp_Object
20354 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20355 {
20356 register Lisp_Object tail, prev;
20357 register Lisp_Object tem;
20358
20359 tail = list;
20360 prev = Qnil;
20361 while (CONSP (tail))
20362 {
20363 tem = XCAR (tail);
20364
20365 if (EQ (elt, tem))
20366 {
20367 /* Splice out the link TAIL. */
20368 if (NILP (prev))
20369 list = XCDR (tail);
20370 else
20371 Fsetcdr (prev, XCDR (tail));
20372
20373 /* Now make it the first. */
20374 Fsetcdr (tail, list);
20375 return tail;
20376 }
20377 else
20378 prev = tail;
20379 tail = XCDR (tail);
20380 QUIT;
20381 }
20382
20383 /* Not found--return unchanged LIST. */
20384 return list;
20385 }
20386
20387 /* Contribute ELT to the mode line for window IT->w. How it
20388 translates into text depends on its data type.
20389
20390 IT describes the display environment in which we display, as usual.
20391
20392 DEPTH is the depth in recursion. It is used to prevent
20393 infinite recursion here.
20394
20395 FIELD_WIDTH is the number of characters the display of ELT should
20396 occupy in the mode line, and PRECISION is the maximum number of
20397 characters to display from ELT's representation. See
20398 display_string for details.
20399
20400 Returns the hpos of the end of the text generated by ELT.
20401
20402 PROPS is a property list to add to any string we encounter.
20403
20404 If RISKY is nonzero, remove (disregard) any properties in any string
20405 we encounter, and ignore :eval and :propertize.
20406
20407 The global variable `mode_line_target' determines whether the
20408 output is passed to `store_mode_line_noprop',
20409 `store_mode_line_string', or `display_string'. */
20410
20411 static int
20412 display_mode_element (struct it *it, int depth, int field_width, int precision,
20413 Lisp_Object elt, Lisp_Object props, int risky)
20414 {
20415 int n = 0, field, prec;
20416 int literal = 0;
20417
20418 tail_recurse:
20419 if (depth > 100)
20420 elt = build_string ("*too-deep*");
20421
20422 depth++;
20423
20424 switch (XTYPE (elt))
20425 {
20426 case Lisp_String:
20427 {
20428 /* A string: output it and check for %-constructs within it. */
20429 unsigned char c;
20430 ptrdiff_t offset = 0;
20431
20432 if (SCHARS (elt) > 0
20433 && (!NILP (props) || risky))
20434 {
20435 Lisp_Object oprops, aelt;
20436 oprops = Ftext_properties_at (make_number (0), elt);
20437
20438 /* If the starting string's properties are not what
20439 we want, translate the string. Also, if the string
20440 is risky, do that anyway. */
20441
20442 if (NILP (Fequal (props, oprops)) || risky)
20443 {
20444 /* If the starting string has properties,
20445 merge the specified ones onto the existing ones. */
20446 if (! NILP (oprops) && !risky)
20447 {
20448 Lisp_Object tem;
20449
20450 oprops = Fcopy_sequence (oprops);
20451 tem = props;
20452 while (CONSP (tem))
20453 {
20454 oprops = Fplist_put (oprops, XCAR (tem),
20455 XCAR (XCDR (tem)));
20456 tem = XCDR (XCDR (tem));
20457 }
20458 props = oprops;
20459 }
20460
20461 aelt = Fassoc (elt, mode_line_proptrans_alist);
20462 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20463 {
20464 /* AELT is what we want. Move it to the front
20465 without consing. */
20466 elt = XCAR (aelt);
20467 mode_line_proptrans_alist
20468 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20469 }
20470 else
20471 {
20472 Lisp_Object tem;
20473
20474 /* If AELT has the wrong props, it is useless.
20475 so get rid of it. */
20476 if (! NILP (aelt))
20477 mode_line_proptrans_alist
20478 = Fdelq (aelt, mode_line_proptrans_alist);
20479
20480 elt = Fcopy_sequence (elt);
20481 Fset_text_properties (make_number (0), Flength (elt),
20482 props, elt);
20483 /* Add this item to mode_line_proptrans_alist. */
20484 mode_line_proptrans_alist
20485 = Fcons (Fcons (elt, props),
20486 mode_line_proptrans_alist);
20487 /* Truncate mode_line_proptrans_alist
20488 to at most 50 elements. */
20489 tem = Fnthcdr (make_number (50),
20490 mode_line_proptrans_alist);
20491 if (! NILP (tem))
20492 XSETCDR (tem, Qnil);
20493 }
20494 }
20495 }
20496
20497 offset = 0;
20498
20499 if (literal)
20500 {
20501 prec = precision - n;
20502 switch (mode_line_target)
20503 {
20504 case MODE_LINE_NOPROP:
20505 case MODE_LINE_TITLE:
20506 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20507 break;
20508 case MODE_LINE_STRING:
20509 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20510 break;
20511 case MODE_LINE_DISPLAY:
20512 n += display_string (NULL, elt, Qnil, 0, 0, it,
20513 0, prec, 0, STRING_MULTIBYTE (elt));
20514 break;
20515 }
20516
20517 break;
20518 }
20519
20520 /* Handle the non-literal case. */
20521
20522 while ((precision <= 0 || n < precision)
20523 && SREF (elt, offset) != 0
20524 && (mode_line_target != MODE_LINE_DISPLAY
20525 || it->current_x < it->last_visible_x))
20526 {
20527 ptrdiff_t last_offset = offset;
20528
20529 /* Advance to end of string or next format specifier. */
20530 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20531 ;
20532
20533 if (offset - 1 != last_offset)
20534 {
20535 ptrdiff_t nchars, nbytes;
20536
20537 /* Output to end of string or up to '%'. Field width
20538 is length of string. Don't output more than
20539 PRECISION allows us. */
20540 offset--;
20541
20542 prec = c_string_width (SDATA (elt) + last_offset,
20543 offset - last_offset, precision - n,
20544 &nchars, &nbytes);
20545
20546 switch (mode_line_target)
20547 {
20548 case MODE_LINE_NOPROP:
20549 case MODE_LINE_TITLE:
20550 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20551 break;
20552 case MODE_LINE_STRING:
20553 {
20554 ptrdiff_t bytepos = last_offset;
20555 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20556 ptrdiff_t endpos = (precision <= 0
20557 ? string_byte_to_char (elt, offset)
20558 : charpos + nchars);
20559
20560 n += store_mode_line_string (NULL,
20561 Fsubstring (elt, make_number (charpos),
20562 make_number (endpos)),
20563 0, 0, 0, Qnil);
20564 }
20565 break;
20566 case MODE_LINE_DISPLAY:
20567 {
20568 ptrdiff_t bytepos = last_offset;
20569 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20570
20571 if (precision <= 0)
20572 nchars = string_byte_to_char (elt, offset) - charpos;
20573 n += display_string (NULL, elt, Qnil, 0, charpos,
20574 it, 0, nchars, 0,
20575 STRING_MULTIBYTE (elt));
20576 }
20577 break;
20578 }
20579 }
20580 else /* c == '%' */
20581 {
20582 ptrdiff_t percent_position = offset;
20583
20584 /* Get the specified minimum width. Zero means
20585 don't pad. */
20586 field = 0;
20587 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20588 field = field * 10 + c - '0';
20589
20590 /* Don't pad beyond the total padding allowed. */
20591 if (field_width - n > 0 && field > field_width - n)
20592 field = field_width - n;
20593
20594 /* Note that either PRECISION <= 0 or N < PRECISION. */
20595 prec = precision - n;
20596
20597 if (c == 'M')
20598 n += display_mode_element (it, depth, field, prec,
20599 Vglobal_mode_string, props,
20600 risky);
20601 else if (c != 0)
20602 {
20603 int multibyte;
20604 ptrdiff_t bytepos, charpos;
20605 const char *spec;
20606 Lisp_Object string;
20607
20608 bytepos = percent_position;
20609 charpos = (STRING_MULTIBYTE (elt)
20610 ? string_byte_to_char (elt, bytepos)
20611 : bytepos);
20612 spec = decode_mode_spec (it->w, c, field, &string);
20613 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20614
20615 switch (mode_line_target)
20616 {
20617 case MODE_LINE_NOPROP:
20618 case MODE_LINE_TITLE:
20619 n += store_mode_line_noprop (spec, field, prec);
20620 break;
20621 case MODE_LINE_STRING:
20622 {
20623 Lisp_Object tem = build_string (spec);
20624 props = Ftext_properties_at (make_number (charpos), elt);
20625 /* Should only keep face property in props */
20626 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20627 }
20628 break;
20629 case MODE_LINE_DISPLAY:
20630 {
20631 int nglyphs_before, nwritten;
20632
20633 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20634 nwritten = display_string (spec, string, elt,
20635 charpos, 0, it,
20636 field, prec, 0,
20637 multibyte);
20638
20639 /* Assign to the glyphs written above the
20640 string where the `%x' came from, position
20641 of the `%'. */
20642 if (nwritten > 0)
20643 {
20644 struct glyph *glyph
20645 = (it->glyph_row->glyphs[TEXT_AREA]
20646 + nglyphs_before);
20647 int i;
20648
20649 for (i = 0; i < nwritten; ++i)
20650 {
20651 glyph[i].object = elt;
20652 glyph[i].charpos = charpos;
20653 }
20654
20655 n += nwritten;
20656 }
20657 }
20658 break;
20659 }
20660 }
20661 else /* c == 0 */
20662 break;
20663 }
20664 }
20665 }
20666 break;
20667
20668 case Lisp_Symbol:
20669 /* A symbol: process the value of the symbol recursively
20670 as if it appeared here directly. Avoid error if symbol void.
20671 Special case: if value of symbol is a string, output the string
20672 literally. */
20673 {
20674 register Lisp_Object tem;
20675
20676 /* If the variable is not marked as risky to set
20677 then its contents are risky to use. */
20678 if (NILP (Fget (elt, Qrisky_local_variable)))
20679 risky = 1;
20680
20681 tem = Fboundp (elt);
20682 if (!NILP (tem))
20683 {
20684 tem = Fsymbol_value (elt);
20685 /* If value is a string, output that string literally:
20686 don't check for % within it. */
20687 if (STRINGP (tem))
20688 literal = 1;
20689
20690 if (!EQ (tem, elt))
20691 {
20692 /* Give up right away for nil or t. */
20693 elt = tem;
20694 goto tail_recurse;
20695 }
20696 }
20697 }
20698 break;
20699
20700 case Lisp_Cons:
20701 {
20702 register Lisp_Object car, tem;
20703
20704 /* A cons cell: five distinct cases.
20705 If first element is :eval or :propertize, do something special.
20706 If first element is a string or a cons, process all the elements
20707 and effectively concatenate them.
20708 If first element is a negative number, truncate displaying cdr to
20709 at most that many characters. If positive, pad (with spaces)
20710 to at least that many characters.
20711 If first element is a symbol, process the cadr or caddr recursively
20712 according to whether the symbol's value is non-nil or nil. */
20713 car = XCAR (elt);
20714 if (EQ (car, QCeval))
20715 {
20716 /* An element of the form (:eval FORM) means evaluate FORM
20717 and use the result as mode line elements. */
20718
20719 if (risky)
20720 break;
20721
20722 if (CONSP (XCDR (elt)))
20723 {
20724 Lisp_Object spec;
20725 spec = safe_eval (XCAR (XCDR (elt)));
20726 n += display_mode_element (it, depth, field_width - n,
20727 precision - n, spec, props,
20728 risky);
20729 }
20730 }
20731 else if (EQ (car, QCpropertize))
20732 {
20733 /* An element of the form (:propertize ELT PROPS...)
20734 means display ELT but applying properties PROPS. */
20735
20736 if (risky)
20737 break;
20738
20739 if (CONSP (XCDR (elt)))
20740 n += display_mode_element (it, depth, field_width - n,
20741 precision - n, XCAR (XCDR (elt)),
20742 XCDR (XCDR (elt)), risky);
20743 }
20744 else if (SYMBOLP (car))
20745 {
20746 tem = Fboundp (car);
20747 elt = XCDR (elt);
20748 if (!CONSP (elt))
20749 goto invalid;
20750 /* elt is now the cdr, and we know it is a cons cell.
20751 Use its car if CAR has a non-nil value. */
20752 if (!NILP (tem))
20753 {
20754 tem = Fsymbol_value (car);
20755 if (!NILP (tem))
20756 {
20757 elt = XCAR (elt);
20758 goto tail_recurse;
20759 }
20760 }
20761 /* Symbol's value is nil (or symbol is unbound)
20762 Get the cddr of the original list
20763 and if possible find the caddr and use that. */
20764 elt = XCDR (elt);
20765 if (NILP (elt))
20766 break;
20767 else if (!CONSP (elt))
20768 goto invalid;
20769 elt = XCAR (elt);
20770 goto tail_recurse;
20771 }
20772 else if (INTEGERP (car))
20773 {
20774 register int lim = XINT (car);
20775 elt = XCDR (elt);
20776 if (lim < 0)
20777 {
20778 /* Negative int means reduce maximum width. */
20779 if (precision <= 0)
20780 precision = -lim;
20781 else
20782 precision = min (precision, -lim);
20783 }
20784 else if (lim > 0)
20785 {
20786 /* Padding specified. Don't let it be more than
20787 current maximum. */
20788 if (precision > 0)
20789 lim = min (precision, lim);
20790
20791 /* If that's more padding than already wanted, queue it.
20792 But don't reduce padding already specified even if
20793 that is beyond the current truncation point. */
20794 field_width = max (lim, field_width);
20795 }
20796 goto tail_recurse;
20797 }
20798 else if (STRINGP (car) || CONSP (car))
20799 {
20800 Lisp_Object halftail = elt;
20801 int len = 0;
20802
20803 while (CONSP (elt)
20804 && (precision <= 0 || n < precision))
20805 {
20806 n += display_mode_element (it, depth,
20807 /* Do padding only after the last
20808 element in the list. */
20809 (! CONSP (XCDR (elt))
20810 ? field_width - n
20811 : 0),
20812 precision - n, XCAR (elt),
20813 props, risky);
20814 elt = XCDR (elt);
20815 len++;
20816 if ((len & 1) == 0)
20817 halftail = XCDR (halftail);
20818 /* Check for cycle. */
20819 if (EQ (halftail, elt))
20820 break;
20821 }
20822 }
20823 }
20824 break;
20825
20826 default:
20827 invalid:
20828 elt = build_string ("*invalid*");
20829 goto tail_recurse;
20830 }
20831
20832 /* Pad to FIELD_WIDTH. */
20833 if (field_width > 0 && n < field_width)
20834 {
20835 switch (mode_line_target)
20836 {
20837 case MODE_LINE_NOPROP:
20838 case MODE_LINE_TITLE:
20839 n += store_mode_line_noprop ("", field_width - n, 0);
20840 break;
20841 case MODE_LINE_STRING:
20842 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20843 break;
20844 case MODE_LINE_DISPLAY:
20845 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20846 0, 0, 0);
20847 break;
20848 }
20849 }
20850
20851 return n;
20852 }
20853
20854 /* Store a mode-line string element in mode_line_string_list.
20855
20856 If STRING is non-null, display that C string. Otherwise, the Lisp
20857 string LISP_STRING is displayed.
20858
20859 FIELD_WIDTH is the minimum number of output glyphs to produce.
20860 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20861 with spaces. FIELD_WIDTH <= 0 means don't pad.
20862
20863 PRECISION is the maximum number of characters to output from
20864 STRING. PRECISION <= 0 means don't truncate the string.
20865
20866 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20867 properties to the string.
20868
20869 PROPS are the properties to add to the string.
20870 The mode_line_string_face face property is always added to the string.
20871 */
20872
20873 static int
20874 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20875 int field_width, int precision, Lisp_Object props)
20876 {
20877 ptrdiff_t len;
20878 int n = 0;
20879
20880 if (string != NULL)
20881 {
20882 len = strlen (string);
20883 if (precision > 0 && len > precision)
20884 len = precision;
20885 lisp_string = make_string (string, len);
20886 if (NILP (props))
20887 props = mode_line_string_face_prop;
20888 else if (!NILP (mode_line_string_face))
20889 {
20890 Lisp_Object face = Fplist_get (props, Qface);
20891 props = Fcopy_sequence (props);
20892 if (NILP (face))
20893 face = mode_line_string_face;
20894 else
20895 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20896 props = Fplist_put (props, Qface, face);
20897 }
20898 Fadd_text_properties (make_number (0), make_number (len),
20899 props, lisp_string);
20900 }
20901 else
20902 {
20903 len = XFASTINT (Flength (lisp_string));
20904 if (precision > 0 && len > precision)
20905 {
20906 len = precision;
20907 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20908 precision = -1;
20909 }
20910 if (!NILP (mode_line_string_face))
20911 {
20912 Lisp_Object face;
20913 if (NILP (props))
20914 props = Ftext_properties_at (make_number (0), lisp_string);
20915 face = Fplist_get (props, Qface);
20916 if (NILP (face))
20917 face = mode_line_string_face;
20918 else
20919 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20920 props = Fcons (Qface, Fcons (face, Qnil));
20921 if (copy_string)
20922 lisp_string = Fcopy_sequence (lisp_string);
20923 }
20924 if (!NILP (props))
20925 Fadd_text_properties (make_number (0), make_number (len),
20926 props, lisp_string);
20927 }
20928
20929 if (len > 0)
20930 {
20931 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20932 n += len;
20933 }
20934
20935 if (field_width > len)
20936 {
20937 field_width -= len;
20938 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20939 if (!NILP (props))
20940 Fadd_text_properties (make_number (0), make_number (field_width),
20941 props, lisp_string);
20942 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20943 n += field_width;
20944 }
20945
20946 return n;
20947 }
20948
20949
20950 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20951 1, 4, 0,
20952 doc: /* Format a string out of a mode line format specification.
20953 First arg FORMAT specifies the mode line format (see `mode-line-format'
20954 for details) to use.
20955
20956 By default, the format is evaluated for the currently selected window.
20957
20958 Optional second arg FACE specifies the face property to put on all
20959 characters for which no face is specified. The value nil means the
20960 default face. The value t means whatever face the window's mode line
20961 currently uses (either `mode-line' or `mode-line-inactive',
20962 depending on whether the window is the selected window or not).
20963 An integer value means the value string has no text
20964 properties.
20965
20966 Optional third and fourth args WINDOW and BUFFER specify the window
20967 and buffer to use as the context for the formatting (defaults
20968 are the selected window and the WINDOW's buffer). */)
20969 (Lisp_Object format, Lisp_Object face,
20970 Lisp_Object window, Lisp_Object buffer)
20971 {
20972 struct it it;
20973 int len;
20974 struct window *w;
20975 struct buffer *old_buffer = NULL;
20976 int face_id;
20977 int no_props = INTEGERP (face);
20978 ptrdiff_t count = SPECPDL_INDEX ();
20979 Lisp_Object str;
20980 int string_start = 0;
20981
20982 if (NILP (window))
20983 window = selected_window;
20984 CHECK_WINDOW (window);
20985 w = XWINDOW (window);
20986
20987 if (NILP (buffer))
20988 buffer = WVAR (w, buffer);
20989 CHECK_BUFFER (buffer);
20990
20991 /* Make formatting the modeline a non-op when noninteractive, otherwise
20992 there will be problems later caused by a partially initialized frame. */
20993 if (NILP (format) || noninteractive)
20994 return empty_unibyte_string;
20995
20996 if (no_props)
20997 face = Qnil;
20998
20999 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21000 : EQ (face, Qt) ? (EQ (window, selected_window)
21001 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21002 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21003 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21004 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21005 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21006 : DEFAULT_FACE_ID;
21007
21008 if (XBUFFER (buffer) != current_buffer)
21009 old_buffer = current_buffer;
21010
21011 /* Save things including mode_line_proptrans_alist,
21012 and set that to nil so that we don't alter the outer value. */
21013 record_unwind_protect (unwind_format_mode_line,
21014 format_mode_line_unwind_data
21015 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21016 old_buffer, selected_window, 1));
21017 mode_line_proptrans_alist = Qnil;
21018
21019 Fselect_window (window, Qt);
21020 if (old_buffer)
21021 set_buffer_internal_1 (XBUFFER (buffer));
21022
21023 init_iterator (&it, w, -1, -1, NULL, face_id);
21024
21025 if (no_props)
21026 {
21027 mode_line_target = MODE_LINE_NOPROP;
21028 mode_line_string_face_prop = Qnil;
21029 mode_line_string_list = Qnil;
21030 string_start = MODE_LINE_NOPROP_LEN (0);
21031 }
21032 else
21033 {
21034 mode_line_target = MODE_LINE_STRING;
21035 mode_line_string_list = Qnil;
21036 mode_line_string_face = face;
21037 mode_line_string_face_prop
21038 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21039 }
21040
21041 push_kboard (FRAME_KBOARD (it.f));
21042 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21043 pop_kboard ();
21044
21045 if (no_props)
21046 {
21047 len = MODE_LINE_NOPROP_LEN (string_start);
21048 str = make_string (mode_line_noprop_buf + string_start, len);
21049 }
21050 else
21051 {
21052 mode_line_string_list = Fnreverse (mode_line_string_list);
21053 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21054 empty_unibyte_string);
21055 }
21056
21057 unbind_to (count, Qnil);
21058 return str;
21059 }
21060
21061 /* Write a null-terminated, right justified decimal representation of
21062 the positive integer D to BUF using a minimal field width WIDTH. */
21063
21064 static void
21065 pint2str (register char *buf, register int width, register ptrdiff_t d)
21066 {
21067 register char *p = buf;
21068
21069 if (d <= 0)
21070 *p++ = '0';
21071 else
21072 {
21073 while (d > 0)
21074 {
21075 *p++ = d % 10 + '0';
21076 d /= 10;
21077 }
21078 }
21079
21080 for (width -= (int) (p - buf); width > 0; --width)
21081 *p++ = ' ';
21082 *p-- = '\0';
21083 while (p > buf)
21084 {
21085 d = *buf;
21086 *buf++ = *p;
21087 *p-- = d;
21088 }
21089 }
21090
21091 /* Write a null-terminated, right justified decimal and "human
21092 readable" representation of the nonnegative integer D to BUF using
21093 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21094
21095 static const char power_letter[] =
21096 {
21097 0, /* no letter */
21098 'k', /* kilo */
21099 'M', /* mega */
21100 'G', /* giga */
21101 'T', /* tera */
21102 'P', /* peta */
21103 'E', /* exa */
21104 'Z', /* zetta */
21105 'Y' /* yotta */
21106 };
21107
21108 static void
21109 pint2hrstr (char *buf, int width, ptrdiff_t d)
21110 {
21111 /* We aim to represent the nonnegative integer D as
21112 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21113 ptrdiff_t quotient = d;
21114 int remainder = 0;
21115 /* -1 means: do not use TENTHS. */
21116 int tenths = -1;
21117 int exponent = 0;
21118
21119 /* Length of QUOTIENT.TENTHS as a string. */
21120 int length;
21121
21122 char * psuffix;
21123 char * p;
21124
21125 if (1000 <= quotient)
21126 {
21127 /* Scale to the appropriate EXPONENT. */
21128 do
21129 {
21130 remainder = quotient % 1000;
21131 quotient /= 1000;
21132 exponent++;
21133 }
21134 while (1000 <= quotient);
21135
21136 /* Round to nearest and decide whether to use TENTHS or not. */
21137 if (quotient <= 9)
21138 {
21139 tenths = remainder / 100;
21140 if (50 <= remainder % 100)
21141 {
21142 if (tenths < 9)
21143 tenths++;
21144 else
21145 {
21146 quotient++;
21147 if (quotient == 10)
21148 tenths = -1;
21149 else
21150 tenths = 0;
21151 }
21152 }
21153 }
21154 else
21155 if (500 <= remainder)
21156 {
21157 if (quotient < 999)
21158 quotient++;
21159 else
21160 {
21161 quotient = 1;
21162 exponent++;
21163 tenths = 0;
21164 }
21165 }
21166 }
21167
21168 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21169 if (tenths == -1 && quotient <= 99)
21170 if (quotient <= 9)
21171 length = 1;
21172 else
21173 length = 2;
21174 else
21175 length = 3;
21176 p = psuffix = buf + max (width, length);
21177
21178 /* Print EXPONENT. */
21179 *psuffix++ = power_letter[exponent];
21180 *psuffix = '\0';
21181
21182 /* Print TENTHS. */
21183 if (tenths >= 0)
21184 {
21185 *--p = '0' + tenths;
21186 *--p = '.';
21187 }
21188
21189 /* Print QUOTIENT. */
21190 do
21191 {
21192 int digit = quotient % 10;
21193 *--p = '0' + digit;
21194 }
21195 while ((quotient /= 10) != 0);
21196
21197 /* Print leading spaces. */
21198 while (buf < p)
21199 *--p = ' ';
21200 }
21201
21202 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21203 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21204 type of CODING_SYSTEM. Return updated pointer into BUF. */
21205
21206 static unsigned char invalid_eol_type[] = "(*invalid*)";
21207
21208 static char *
21209 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21210 {
21211 Lisp_Object val;
21212 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21213 const unsigned char *eol_str;
21214 int eol_str_len;
21215 /* The EOL conversion we are using. */
21216 Lisp_Object eoltype;
21217
21218 val = CODING_SYSTEM_SPEC (coding_system);
21219 eoltype = Qnil;
21220
21221 if (!VECTORP (val)) /* Not yet decided. */
21222 {
21223 *buf++ = multibyte ? '-' : ' ';
21224 if (eol_flag)
21225 eoltype = eol_mnemonic_undecided;
21226 /* Don't mention EOL conversion if it isn't decided. */
21227 }
21228 else
21229 {
21230 Lisp_Object attrs;
21231 Lisp_Object eolvalue;
21232
21233 attrs = AREF (val, 0);
21234 eolvalue = AREF (val, 2);
21235
21236 *buf++ = multibyte
21237 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21238 : ' ';
21239
21240 if (eol_flag)
21241 {
21242 /* The EOL conversion that is normal on this system. */
21243
21244 if (NILP (eolvalue)) /* Not yet decided. */
21245 eoltype = eol_mnemonic_undecided;
21246 else if (VECTORP (eolvalue)) /* Not yet decided. */
21247 eoltype = eol_mnemonic_undecided;
21248 else /* eolvalue is Qunix, Qdos, or Qmac. */
21249 eoltype = (EQ (eolvalue, Qunix)
21250 ? eol_mnemonic_unix
21251 : (EQ (eolvalue, Qdos) == 1
21252 ? eol_mnemonic_dos : eol_mnemonic_mac));
21253 }
21254 }
21255
21256 if (eol_flag)
21257 {
21258 /* Mention the EOL conversion if it is not the usual one. */
21259 if (STRINGP (eoltype))
21260 {
21261 eol_str = SDATA (eoltype);
21262 eol_str_len = SBYTES (eoltype);
21263 }
21264 else if (CHARACTERP (eoltype))
21265 {
21266 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21267 int c = XFASTINT (eoltype);
21268 eol_str_len = CHAR_STRING (c, tmp);
21269 eol_str = tmp;
21270 }
21271 else
21272 {
21273 eol_str = invalid_eol_type;
21274 eol_str_len = sizeof (invalid_eol_type) - 1;
21275 }
21276 memcpy (buf, eol_str, eol_str_len);
21277 buf += eol_str_len;
21278 }
21279
21280 return buf;
21281 }
21282
21283 /* Return a string for the output of a mode line %-spec for window W,
21284 generated by character C. FIELD_WIDTH > 0 means pad the string
21285 returned with spaces to that value. Return a Lisp string in
21286 *STRING if the resulting string is taken from that Lisp string.
21287
21288 Note we operate on the current buffer for most purposes,
21289 the exception being w->base_line_pos. */
21290
21291 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21292
21293 static const char *
21294 decode_mode_spec (struct window *w, register int c, int field_width,
21295 Lisp_Object *string)
21296 {
21297 Lisp_Object obj;
21298 struct frame *f = XFRAME (WINDOW_FRAME (w));
21299 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21300 struct buffer *b = current_buffer;
21301
21302 obj = Qnil;
21303 *string = Qnil;
21304
21305 switch (c)
21306 {
21307 case '*':
21308 if (!NILP (BVAR (b, read_only)))
21309 return "%";
21310 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21311 return "*";
21312 return "-";
21313
21314 case '+':
21315 /* This differs from %* only for a modified read-only buffer. */
21316 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21317 return "*";
21318 if (!NILP (BVAR (b, read_only)))
21319 return "%";
21320 return "-";
21321
21322 case '&':
21323 /* This differs from %* in ignoring read-only-ness. */
21324 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21325 return "*";
21326 return "-";
21327
21328 case '%':
21329 return "%";
21330
21331 case '[':
21332 {
21333 int i;
21334 char *p;
21335
21336 if (command_loop_level > 5)
21337 return "[[[... ";
21338 p = decode_mode_spec_buf;
21339 for (i = 0; i < command_loop_level; i++)
21340 *p++ = '[';
21341 *p = 0;
21342 return decode_mode_spec_buf;
21343 }
21344
21345 case ']':
21346 {
21347 int i;
21348 char *p;
21349
21350 if (command_loop_level > 5)
21351 return " ...]]]";
21352 p = decode_mode_spec_buf;
21353 for (i = 0; i < command_loop_level; i++)
21354 *p++ = ']';
21355 *p = 0;
21356 return decode_mode_spec_buf;
21357 }
21358
21359 case '-':
21360 {
21361 register int i;
21362
21363 /* Let lots_of_dashes be a string of infinite length. */
21364 if (mode_line_target == MODE_LINE_NOPROP ||
21365 mode_line_target == MODE_LINE_STRING)
21366 return "--";
21367 if (field_width <= 0
21368 || field_width > sizeof (lots_of_dashes))
21369 {
21370 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21371 decode_mode_spec_buf[i] = '-';
21372 decode_mode_spec_buf[i] = '\0';
21373 return decode_mode_spec_buf;
21374 }
21375 else
21376 return lots_of_dashes;
21377 }
21378
21379 case 'b':
21380 obj = BVAR (b, name);
21381 break;
21382
21383 case 'c':
21384 /* %c and %l are ignored in `frame-title-format'.
21385 (In redisplay_internal, the frame title is drawn _before_ the
21386 windows are updated, so the stuff which depends on actual
21387 window contents (such as %l) may fail to render properly, or
21388 even crash emacs.) */
21389 if (mode_line_target == MODE_LINE_TITLE)
21390 return "";
21391 else
21392 {
21393 ptrdiff_t col = current_column ();
21394 WVAR (w, column_number_displayed) = make_number (col);
21395 pint2str (decode_mode_spec_buf, field_width, col);
21396 return decode_mode_spec_buf;
21397 }
21398
21399 case 'e':
21400 #ifndef SYSTEM_MALLOC
21401 {
21402 if (NILP (Vmemory_full))
21403 return "";
21404 else
21405 return "!MEM FULL! ";
21406 }
21407 #else
21408 return "";
21409 #endif
21410
21411 case 'F':
21412 /* %F displays the frame name. */
21413 if (!NILP (FVAR (f, title)))
21414 return SSDATA (FVAR (f, title));
21415 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21416 return SSDATA (FVAR (f, name));
21417 return "Emacs";
21418
21419 case 'f':
21420 obj = BVAR (b, filename);
21421 break;
21422
21423 case 'i':
21424 {
21425 ptrdiff_t size = ZV - BEGV;
21426 pint2str (decode_mode_spec_buf, field_width, size);
21427 return decode_mode_spec_buf;
21428 }
21429
21430 case 'I':
21431 {
21432 ptrdiff_t size = ZV - BEGV;
21433 pint2hrstr (decode_mode_spec_buf, field_width, size);
21434 return decode_mode_spec_buf;
21435 }
21436
21437 case 'l':
21438 {
21439 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21440 ptrdiff_t topline, nlines, height;
21441 ptrdiff_t junk;
21442
21443 /* %c and %l are ignored in `frame-title-format'. */
21444 if (mode_line_target == MODE_LINE_TITLE)
21445 return "";
21446
21447 startpos = XMARKER (WVAR (w, start))->charpos;
21448 startpos_byte = marker_byte_position (WVAR (w, start));
21449 height = WINDOW_TOTAL_LINES (w);
21450
21451 /* If we decided that this buffer isn't suitable for line numbers,
21452 don't forget that too fast. */
21453 if (EQ (WVAR (w, base_line_pos), WVAR (w, buffer)))
21454 goto no_value;
21455 /* But do forget it, if the window shows a different buffer now. */
21456 else if (BUFFERP (WVAR (w, base_line_pos)))
21457 WVAR (w, base_line_pos) = Qnil;
21458
21459 /* If the buffer is very big, don't waste time. */
21460 if (INTEGERP (Vline_number_display_limit)
21461 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21462 {
21463 WVAR (w, base_line_pos) = Qnil;
21464 WVAR (w, base_line_number) = Qnil;
21465 goto no_value;
21466 }
21467
21468 if (INTEGERP (WVAR (w, base_line_number))
21469 && INTEGERP (WVAR (w, base_line_pos))
21470 && XFASTINT (WVAR (w, base_line_pos)) <= startpos)
21471 {
21472 line = XFASTINT (WVAR (w, base_line_number));
21473 linepos = XFASTINT (WVAR (w, base_line_pos));
21474 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21475 }
21476 else
21477 {
21478 line = 1;
21479 linepos = BUF_BEGV (b);
21480 linepos_byte = BUF_BEGV_BYTE (b);
21481 }
21482
21483 /* Count lines from base line to window start position. */
21484 nlines = display_count_lines (linepos_byte,
21485 startpos_byte,
21486 startpos, &junk);
21487
21488 topline = nlines + line;
21489
21490 /* Determine a new base line, if the old one is too close
21491 or too far away, or if we did not have one.
21492 "Too close" means it's plausible a scroll-down would
21493 go back past it. */
21494 if (startpos == BUF_BEGV (b))
21495 {
21496 WVAR (w, base_line_number) = make_number (topline);
21497 WVAR (w, base_line_pos) = make_number (BUF_BEGV (b));
21498 }
21499 else if (nlines < height + 25 || nlines > height * 3 + 50
21500 || linepos == BUF_BEGV (b))
21501 {
21502 ptrdiff_t limit = BUF_BEGV (b);
21503 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21504 ptrdiff_t position;
21505 ptrdiff_t distance =
21506 (height * 2 + 30) * line_number_display_limit_width;
21507
21508 if (startpos - distance > limit)
21509 {
21510 limit = startpos - distance;
21511 limit_byte = CHAR_TO_BYTE (limit);
21512 }
21513
21514 nlines = display_count_lines (startpos_byte,
21515 limit_byte,
21516 - (height * 2 + 30),
21517 &position);
21518 /* If we couldn't find the lines we wanted within
21519 line_number_display_limit_width chars per line,
21520 give up on line numbers for this window. */
21521 if (position == limit_byte && limit == startpos - distance)
21522 {
21523 WVAR (w, base_line_pos) = WVAR (w, buffer);
21524 WVAR (w, base_line_number) = Qnil;
21525 goto no_value;
21526 }
21527
21528 WVAR (w, base_line_number) = make_number (topline - nlines);
21529 WVAR (w, base_line_pos) = make_number (BYTE_TO_CHAR (position));
21530 }
21531
21532 /* Now count lines from the start pos to point. */
21533 nlines = display_count_lines (startpos_byte,
21534 PT_BYTE, PT, &junk);
21535
21536 /* Record that we did display the line number. */
21537 line_number_displayed = 1;
21538
21539 /* Make the string to show. */
21540 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21541 return decode_mode_spec_buf;
21542 no_value:
21543 {
21544 char* p = decode_mode_spec_buf;
21545 int pad = field_width - 2;
21546 while (pad-- > 0)
21547 *p++ = ' ';
21548 *p++ = '?';
21549 *p++ = '?';
21550 *p = '\0';
21551 return decode_mode_spec_buf;
21552 }
21553 }
21554 break;
21555
21556 case 'm':
21557 obj = BVAR (b, mode_name);
21558 break;
21559
21560 case 'n':
21561 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21562 return " Narrow";
21563 break;
21564
21565 case 'p':
21566 {
21567 ptrdiff_t pos = marker_position (WVAR (w, start));
21568 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21569
21570 if (XFASTINT (WVAR (w, window_end_pos)) <= BUF_Z (b) - BUF_ZV (b))
21571 {
21572 if (pos <= BUF_BEGV (b))
21573 return "All";
21574 else
21575 return "Bottom";
21576 }
21577 else if (pos <= BUF_BEGV (b))
21578 return "Top";
21579 else
21580 {
21581 if (total > 1000000)
21582 /* Do it differently for a large value, to avoid overflow. */
21583 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21584 else
21585 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21586 /* We can't normally display a 3-digit number,
21587 so get us a 2-digit number that is close. */
21588 if (total == 100)
21589 total = 99;
21590 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21591 return decode_mode_spec_buf;
21592 }
21593 }
21594
21595 /* Display percentage of size above the bottom of the screen. */
21596 case 'P':
21597 {
21598 ptrdiff_t toppos = marker_position (WVAR (w, start));
21599 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (WVAR (w, window_end_pos));
21600 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21601
21602 if (botpos >= BUF_ZV (b))
21603 {
21604 if (toppos <= BUF_BEGV (b))
21605 return "All";
21606 else
21607 return "Bottom";
21608 }
21609 else
21610 {
21611 if (total > 1000000)
21612 /* Do it differently for a large value, to avoid overflow. */
21613 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21614 else
21615 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21616 /* We can't normally display a 3-digit number,
21617 so get us a 2-digit number that is close. */
21618 if (total == 100)
21619 total = 99;
21620 if (toppos <= BUF_BEGV (b))
21621 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21622 else
21623 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21624 return decode_mode_spec_buf;
21625 }
21626 }
21627
21628 case 's':
21629 /* status of process */
21630 obj = Fget_buffer_process (Fcurrent_buffer ());
21631 if (NILP (obj))
21632 return "no process";
21633 #ifndef MSDOS
21634 obj = Fsymbol_name (Fprocess_status (obj));
21635 #endif
21636 break;
21637
21638 case '@':
21639 {
21640 ptrdiff_t count = inhibit_garbage_collection ();
21641 Lisp_Object val = call1 (intern ("file-remote-p"),
21642 BVAR (current_buffer, directory));
21643 unbind_to (count, Qnil);
21644
21645 if (NILP (val))
21646 return "-";
21647 else
21648 return "@";
21649 }
21650
21651 case 't': /* indicate TEXT or BINARY */
21652 return "T";
21653
21654 case 'z':
21655 /* coding-system (not including end-of-line format) */
21656 case 'Z':
21657 /* coding-system (including end-of-line type) */
21658 {
21659 int eol_flag = (c == 'Z');
21660 char *p = decode_mode_spec_buf;
21661
21662 if (! FRAME_WINDOW_P (f))
21663 {
21664 /* No need to mention EOL here--the terminal never needs
21665 to do EOL conversion. */
21666 p = decode_mode_spec_coding (CODING_ID_NAME
21667 (FRAME_KEYBOARD_CODING (f)->id),
21668 p, 0);
21669 p = decode_mode_spec_coding (CODING_ID_NAME
21670 (FRAME_TERMINAL_CODING (f)->id),
21671 p, 0);
21672 }
21673 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21674 p, eol_flag);
21675
21676 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21677 #ifdef subprocesses
21678 obj = Fget_buffer_process (Fcurrent_buffer ());
21679 if (PROCESSP (obj))
21680 {
21681 p = decode_mode_spec_coding
21682 (PVAR (XPROCESS (obj), decode_coding_system), p, eol_flag);
21683 p = decode_mode_spec_coding
21684 (PVAR (XPROCESS (obj), encode_coding_system), p, eol_flag);
21685 }
21686 #endif /* subprocesses */
21687 #endif /* 0 */
21688 *p = 0;
21689 return decode_mode_spec_buf;
21690 }
21691 }
21692
21693 if (STRINGP (obj))
21694 {
21695 *string = obj;
21696 return SSDATA (obj);
21697 }
21698 else
21699 return "";
21700 }
21701
21702
21703 /* Count up to COUNT lines starting from START_BYTE.
21704 But don't go beyond LIMIT_BYTE.
21705 Return the number of lines thus found (always nonnegative).
21706
21707 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21708
21709 static ptrdiff_t
21710 display_count_lines (ptrdiff_t start_byte,
21711 ptrdiff_t limit_byte, ptrdiff_t count,
21712 ptrdiff_t *byte_pos_ptr)
21713 {
21714 register unsigned char *cursor;
21715 unsigned char *base;
21716
21717 register ptrdiff_t ceiling;
21718 register unsigned char *ceiling_addr;
21719 ptrdiff_t orig_count = count;
21720
21721 /* If we are not in selective display mode,
21722 check only for newlines. */
21723 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21724 && !INTEGERP (BVAR (current_buffer, selective_display)));
21725
21726 if (count > 0)
21727 {
21728 while (start_byte < limit_byte)
21729 {
21730 ceiling = BUFFER_CEILING_OF (start_byte);
21731 ceiling = min (limit_byte - 1, ceiling);
21732 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21733 base = (cursor = BYTE_POS_ADDR (start_byte));
21734 while (1)
21735 {
21736 if (selective_display)
21737 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21738 ;
21739 else
21740 while (*cursor != '\n' && ++cursor != ceiling_addr)
21741 ;
21742
21743 if (cursor != ceiling_addr)
21744 {
21745 if (--count == 0)
21746 {
21747 start_byte += cursor - base + 1;
21748 *byte_pos_ptr = start_byte;
21749 return orig_count;
21750 }
21751 else
21752 if (++cursor == ceiling_addr)
21753 break;
21754 }
21755 else
21756 break;
21757 }
21758 start_byte += cursor - base;
21759 }
21760 }
21761 else
21762 {
21763 while (start_byte > limit_byte)
21764 {
21765 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21766 ceiling = max (limit_byte, ceiling);
21767 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21768 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21769 while (1)
21770 {
21771 if (selective_display)
21772 while (--cursor != ceiling_addr
21773 && *cursor != '\n' && *cursor != 015)
21774 ;
21775 else
21776 while (--cursor != ceiling_addr && *cursor != '\n')
21777 ;
21778
21779 if (cursor != ceiling_addr)
21780 {
21781 if (++count == 0)
21782 {
21783 start_byte += cursor - base + 1;
21784 *byte_pos_ptr = start_byte;
21785 /* When scanning backwards, we should
21786 not count the newline posterior to which we stop. */
21787 return - orig_count - 1;
21788 }
21789 }
21790 else
21791 break;
21792 }
21793 /* Here we add 1 to compensate for the last decrement
21794 of CURSOR, which took it past the valid range. */
21795 start_byte += cursor - base + 1;
21796 }
21797 }
21798
21799 *byte_pos_ptr = limit_byte;
21800
21801 if (count < 0)
21802 return - orig_count + count;
21803 return orig_count - count;
21804
21805 }
21806
21807
21808 \f
21809 /***********************************************************************
21810 Displaying strings
21811 ***********************************************************************/
21812
21813 /* Display a NUL-terminated string, starting with index START.
21814
21815 If STRING is non-null, display that C string. Otherwise, the Lisp
21816 string LISP_STRING is displayed. There's a case that STRING is
21817 non-null and LISP_STRING is not nil. It means STRING is a string
21818 data of LISP_STRING. In that case, we display LISP_STRING while
21819 ignoring its text properties.
21820
21821 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21822 FACE_STRING. Display STRING or LISP_STRING with the face at
21823 FACE_STRING_POS in FACE_STRING:
21824
21825 Display the string in the environment given by IT, but use the
21826 standard display table, temporarily.
21827
21828 FIELD_WIDTH is the minimum number of output glyphs to produce.
21829 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21830 with spaces. If STRING has more characters, more than FIELD_WIDTH
21831 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21832
21833 PRECISION is the maximum number of characters to output from
21834 STRING. PRECISION < 0 means don't truncate the string.
21835
21836 This is roughly equivalent to printf format specifiers:
21837
21838 FIELD_WIDTH PRECISION PRINTF
21839 ----------------------------------------
21840 -1 -1 %s
21841 -1 10 %.10s
21842 10 -1 %10s
21843 20 10 %20.10s
21844
21845 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21846 display them, and < 0 means obey the current buffer's value of
21847 enable_multibyte_characters.
21848
21849 Value is the number of columns displayed. */
21850
21851 static int
21852 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21853 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21854 int field_width, int precision, int max_x, int multibyte)
21855 {
21856 int hpos_at_start = it->hpos;
21857 int saved_face_id = it->face_id;
21858 struct glyph_row *row = it->glyph_row;
21859 ptrdiff_t it_charpos;
21860
21861 /* Initialize the iterator IT for iteration over STRING beginning
21862 with index START. */
21863 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21864 precision, field_width, multibyte);
21865 if (string && STRINGP (lisp_string))
21866 /* LISP_STRING is the one returned by decode_mode_spec. We should
21867 ignore its text properties. */
21868 it->stop_charpos = it->end_charpos;
21869
21870 /* If displaying STRING, set up the face of the iterator from
21871 FACE_STRING, if that's given. */
21872 if (STRINGP (face_string))
21873 {
21874 ptrdiff_t endptr;
21875 struct face *face;
21876
21877 it->face_id
21878 = face_at_string_position (it->w, face_string, face_string_pos,
21879 0, it->region_beg_charpos,
21880 it->region_end_charpos,
21881 &endptr, it->base_face_id, 0);
21882 face = FACE_FROM_ID (it->f, it->face_id);
21883 it->face_box_p = face->box != FACE_NO_BOX;
21884 }
21885
21886 /* Set max_x to the maximum allowed X position. Don't let it go
21887 beyond the right edge of the window. */
21888 if (max_x <= 0)
21889 max_x = it->last_visible_x;
21890 else
21891 max_x = min (max_x, it->last_visible_x);
21892
21893 /* Skip over display elements that are not visible. because IT->w is
21894 hscrolled. */
21895 if (it->current_x < it->first_visible_x)
21896 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21897 MOVE_TO_POS | MOVE_TO_X);
21898
21899 row->ascent = it->max_ascent;
21900 row->height = it->max_ascent + it->max_descent;
21901 row->phys_ascent = it->max_phys_ascent;
21902 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21903 row->extra_line_spacing = it->max_extra_line_spacing;
21904
21905 if (STRINGP (it->string))
21906 it_charpos = IT_STRING_CHARPOS (*it);
21907 else
21908 it_charpos = IT_CHARPOS (*it);
21909
21910 /* This condition is for the case that we are called with current_x
21911 past last_visible_x. */
21912 while (it->current_x < max_x)
21913 {
21914 int x_before, x, n_glyphs_before, i, nglyphs;
21915
21916 /* Get the next display element. */
21917 if (!get_next_display_element (it))
21918 break;
21919
21920 /* Produce glyphs. */
21921 x_before = it->current_x;
21922 n_glyphs_before = row->used[TEXT_AREA];
21923 PRODUCE_GLYPHS (it);
21924
21925 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21926 i = 0;
21927 x = x_before;
21928 while (i < nglyphs)
21929 {
21930 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21931
21932 if (it->line_wrap != TRUNCATE
21933 && x + glyph->pixel_width > max_x)
21934 {
21935 /* End of continued line or max_x reached. */
21936 if (CHAR_GLYPH_PADDING_P (*glyph))
21937 {
21938 /* A wide character is unbreakable. */
21939 if (row->reversed_p)
21940 unproduce_glyphs (it, row->used[TEXT_AREA]
21941 - n_glyphs_before);
21942 row->used[TEXT_AREA] = n_glyphs_before;
21943 it->current_x = x_before;
21944 }
21945 else
21946 {
21947 if (row->reversed_p)
21948 unproduce_glyphs (it, row->used[TEXT_AREA]
21949 - (n_glyphs_before + i));
21950 row->used[TEXT_AREA] = n_glyphs_before + i;
21951 it->current_x = x;
21952 }
21953 break;
21954 }
21955 else if (x + glyph->pixel_width >= it->first_visible_x)
21956 {
21957 /* Glyph is at least partially visible. */
21958 ++it->hpos;
21959 if (x < it->first_visible_x)
21960 row->x = x - it->first_visible_x;
21961 }
21962 else
21963 {
21964 /* Glyph is off the left margin of the display area.
21965 Should not happen. */
21966 abort ();
21967 }
21968
21969 row->ascent = max (row->ascent, it->max_ascent);
21970 row->height = max (row->height, it->max_ascent + it->max_descent);
21971 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21972 row->phys_height = max (row->phys_height,
21973 it->max_phys_ascent + it->max_phys_descent);
21974 row->extra_line_spacing = max (row->extra_line_spacing,
21975 it->max_extra_line_spacing);
21976 x += glyph->pixel_width;
21977 ++i;
21978 }
21979
21980 /* Stop if max_x reached. */
21981 if (i < nglyphs)
21982 break;
21983
21984 /* Stop at line ends. */
21985 if (ITERATOR_AT_END_OF_LINE_P (it))
21986 {
21987 it->continuation_lines_width = 0;
21988 break;
21989 }
21990
21991 set_iterator_to_next (it, 1);
21992 if (STRINGP (it->string))
21993 it_charpos = IT_STRING_CHARPOS (*it);
21994 else
21995 it_charpos = IT_CHARPOS (*it);
21996
21997 /* Stop if truncating at the right edge. */
21998 if (it->line_wrap == TRUNCATE
21999 && it->current_x >= it->last_visible_x)
22000 {
22001 /* Add truncation mark, but don't do it if the line is
22002 truncated at a padding space. */
22003 if (it_charpos < it->string_nchars)
22004 {
22005 if (!FRAME_WINDOW_P (it->f))
22006 {
22007 int ii, n;
22008
22009 if (it->current_x > it->last_visible_x)
22010 {
22011 if (!row->reversed_p)
22012 {
22013 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22014 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22015 break;
22016 }
22017 else
22018 {
22019 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22020 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22021 break;
22022 unproduce_glyphs (it, ii + 1);
22023 ii = row->used[TEXT_AREA] - (ii + 1);
22024 }
22025 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22026 {
22027 row->used[TEXT_AREA] = ii;
22028 produce_special_glyphs (it, IT_TRUNCATION);
22029 }
22030 }
22031 produce_special_glyphs (it, IT_TRUNCATION);
22032 }
22033 row->truncated_on_right_p = 1;
22034 }
22035 break;
22036 }
22037 }
22038
22039 /* Maybe insert a truncation at the left. */
22040 if (it->first_visible_x
22041 && it_charpos > 0)
22042 {
22043 if (!FRAME_WINDOW_P (it->f)
22044 || (row->reversed_p
22045 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22046 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22047 insert_left_trunc_glyphs (it);
22048 row->truncated_on_left_p = 1;
22049 }
22050
22051 it->face_id = saved_face_id;
22052
22053 /* Value is number of columns displayed. */
22054 return it->hpos - hpos_at_start;
22055 }
22056
22057
22058 \f
22059 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22060 appears as an element of LIST or as the car of an element of LIST.
22061 If PROPVAL is a list, compare each element against LIST in that
22062 way, and return 1/2 if any element of PROPVAL is found in LIST.
22063 Otherwise return 0. This function cannot quit.
22064 The return value is 2 if the text is invisible but with an ellipsis
22065 and 1 if it's invisible and without an ellipsis. */
22066
22067 int
22068 invisible_p (register Lisp_Object propval, Lisp_Object list)
22069 {
22070 register Lisp_Object tail, proptail;
22071
22072 for (tail = list; CONSP (tail); tail = XCDR (tail))
22073 {
22074 register Lisp_Object tem;
22075 tem = XCAR (tail);
22076 if (EQ (propval, tem))
22077 return 1;
22078 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22079 return NILP (XCDR (tem)) ? 1 : 2;
22080 }
22081
22082 if (CONSP (propval))
22083 {
22084 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22085 {
22086 Lisp_Object propelt;
22087 propelt = XCAR (proptail);
22088 for (tail = list; CONSP (tail); tail = XCDR (tail))
22089 {
22090 register Lisp_Object tem;
22091 tem = XCAR (tail);
22092 if (EQ (propelt, tem))
22093 return 1;
22094 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22095 return NILP (XCDR (tem)) ? 1 : 2;
22096 }
22097 }
22098 }
22099
22100 return 0;
22101 }
22102
22103 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22104 doc: /* Non-nil if the property makes the text invisible.
22105 POS-OR-PROP can be a marker or number, in which case it is taken to be
22106 a position in the current buffer and the value of the `invisible' property
22107 is checked; or it can be some other value, which is then presumed to be the
22108 value of the `invisible' property of the text of interest.
22109 The non-nil value returned can be t for truly invisible text or something
22110 else if the text is replaced by an ellipsis. */)
22111 (Lisp_Object pos_or_prop)
22112 {
22113 Lisp_Object prop
22114 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22115 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22116 : pos_or_prop);
22117 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22118 return (invis == 0 ? Qnil
22119 : invis == 1 ? Qt
22120 : make_number (invis));
22121 }
22122
22123 /* Calculate a width or height in pixels from a specification using
22124 the following elements:
22125
22126 SPEC ::=
22127 NUM - a (fractional) multiple of the default font width/height
22128 (NUM) - specifies exactly NUM pixels
22129 UNIT - a fixed number of pixels, see below.
22130 ELEMENT - size of a display element in pixels, see below.
22131 (NUM . SPEC) - equals NUM * SPEC
22132 (+ SPEC SPEC ...) - add pixel values
22133 (- SPEC SPEC ...) - subtract pixel values
22134 (- SPEC) - negate pixel value
22135
22136 NUM ::=
22137 INT or FLOAT - a number constant
22138 SYMBOL - use symbol's (buffer local) variable binding.
22139
22140 UNIT ::=
22141 in - pixels per inch *)
22142 mm - pixels per 1/1000 meter *)
22143 cm - pixels per 1/100 meter *)
22144 width - width of current font in pixels.
22145 height - height of current font in pixels.
22146
22147 *) using the ratio(s) defined in display-pixels-per-inch.
22148
22149 ELEMENT ::=
22150
22151 left-fringe - left fringe width in pixels
22152 right-fringe - right fringe width in pixels
22153
22154 left-margin - left margin width in pixels
22155 right-margin - right margin width in pixels
22156
22157 scroll-bar - scroll-bar area width in pixels
22158
22159 Examples:
22160
22161 Pixels corresponding to 5 inches:
22162 (5 . in)
22163
22164 Total width of non-text areas on left side of window (if scroll-bar is on left):
22165 '(space :width (+ left-fringe left-margin scroll-bar))
22166
22167 Align to first text column (in header line):
22168 '(space :align-to 0)
22169
22170 Align to middle of text area minus half the width of variable `my-image'
22171 containing a loaded image:
22172 '(space :align-to (0.5 . (- text my-image)))
22173
22174 Width of left margin minus width of 1 character in the default font:
22175 '(space :width (- left-margin 1))
22176
22177 Width of left margin minus width of 2 characters in the current font:
22178 '(space :width (- left-margin (2 . width)))
22179
22180 Center 1 character over left-margin (in header line):
22181 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22182
22183 Different ways to express width of left fringe plus left margin minus one pixel:
22184 '(space :width (- (+ left-fringe left-margin) (1)))
22185 '(space :width (+ left-fringe left-margin (- (1))))
22186 '(space :width (+ left-fringe left-margin (-1)))
22187
22188 */
22189
22190 #define NUMVAL(X) \
22191 ((INTEGERP (X) || FLOATP (X)) \
22192 ? XFLOATINT (X) \
22193 : - 1)
22194
22195 static int
22196 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22197 struct font *font, int width_p, int *align_to)
22198 {
22199 double pixels;
22200
22201 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22202 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22203
22204 if (NILP (prop))
22205 return OK_PIXELS (0);
22206
22207 eassert (FRAME_LIVE_P (it->f));
22208
22209 if (SYMBOLP (prop))
22210 {
22211 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22212 {
22213 char *unit = SSDATA (SYMBOL_NAME (prop));
22214
22215 if (unit[0] == 'i' && unit[1] == 'n')
22216 pixels = 1.0;
22217 else if (unit[0] == 'm' && unit[1] == 'm')
22218 pixels = 25.4;
22219 else if (unit[0] == 'c' && unit[1] == 'm')
22220 pixels = 2.54;
22221 else
22222 pixels = 0;
22223 if (pixels > 0)
22224 {
22225 double ppi;
22226 #ifdef HAVE_WINDOW_SYSTEM
22227 if (FRAME_WINDOW_P (it->f)
22228 && (ppi = (width_p
22229 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22230 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22231 ppi > 0))
22232 return OK_PIXELS (ppi / pixels);
22233 #endif
22234
22235 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22236 || (CONSP (Vdisplay_pixels_per_inch)
22237 && (ppi = (width_p
22238 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22239 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22240 ppi > 0)))
22241 return OK_PIXELS (ppi / pixels);
22242
22243 return 0;
22244 }
22245 }
22246
22247 #ifdef HAVE_WINDOW_SYSTEM
22248 if (EQ (prop, Qheight))
22249 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22250 if (EQ (prop, Qwidth))
22251 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22252 #else
22253 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22254 return OK_PIXELS (1);
22255 #endif
22256
22257 if (EQ (prop, Qtext))
22258 return OK_PIXELS (width_p
22259 ? window_box_width (it->w, TEXT_AREA)
22260 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22261
22262 if (align_to && *align_to < 0)
22263 {
22264 *res = 0;
22265 if (EQ (prop, Qleft))
22266 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22267 if (EQ (prop, Qright))
22268 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22269 if (EQ (prop, Qcenter))
22270 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22271 + window_box_width (it->w, TEXT_AREA) / 2);
22272 if (EQ (prop, Qleft_fringe))
22273 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22274 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22275 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22276 if (EQ (prop, Qright_fringe))
22277 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22278 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22279 : window_box_right_offset (it->w, TEXT_AREA));
22280 if (EQ (prop, Qleft_margin))
22281 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22282 if (EQ (prop, Qright_margin))
22283 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22284 if (EQ (prop, Qscroll_bar))
22285 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22286 ? 0
22287 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22288 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22289 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22290 : 0)));
22291 }
22292 else
22293 {
22294 if (EQ (prop, Qleft_fringe))
22295 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22296 if (EQ (prop, Qright_fringe))
22297 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22298 if (EQ (prop, Qleft_margin))
22299 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22300 if (EQ (prop, Qright_margin))
22301 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22302 if (EQ (prop, Qscroll_bar))
22303 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22304 }
22305
22306 prop = buffer_local_value_1 (prop, WVAR (it->w, buffer));
22307 if (EQ (prop, Qunbound))
22308 prop = Qnil;
22309 }
22310
22311 if (INTEGERP (prop) || FLOATP (prop))
22312 {
22313 int base_unit = (width_p
22314 ? FRAME_COLUMN_WIDTH (it->f)
22315 : FRAME_LINE_HEIGHT (it->f));
22316 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22317 }
22318
22319 if (CONSP (prop))
22320 {
22321 Lisp_Object car = XCAR (prop);
22322 Lisp_Object cdr = XCDR (prop);
22323
22324 if (SYMBOLP (car))
22325 {
22326 #ifdef HAVE_WINDOW_SYSTEM
22327 if (FRAME_WINDOW_P (it->f)
22328 && valid_image_p (prop))
22329 {
22330 ptrdiff_t id = lookup_image (it->f, prop);
22331 struct image *img = IMAGE_FROM_ID (it->f, id);
22332
22333 return OK_PIXELS (width_p ? img->width : img->height);
22334 }
22335 #endif
22336 if (EQ (car, Qplus) || EQ (car, Qminus))
22337 {
22338 int first = 1;
22339 double px;
22340
22341 pixels = 0;
22342 while (CONSP (cdr))
22343 {
22344 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22345 font, width_p, align_to))
22346 return 0;
22347 if (first)
22348 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22349 else
22350 pixels += px;
22351 cdr = XCDR (cdr);
22352 }
22353 if (EQ (car, Qminus))
22354 pixels = -pixels;
22355 return OK_PIXELS (pixels);
22356 }
22357
22358 car = buffer_local_value_1 (car, WVAR (it->w, buffer));
22359 if (EQ (car, Qunbound))
22360 car = Qnil;
22361 }
22362
22363 if (INTEGERP (car) || FLOATP (car))
22364 {
22365 double fact;
22366 pixels = XFLOATINT (car);
22367 if (NILP (cdr))
22368 return OK_PIXELS (pixels);
22369 if (calc_pixel_width_or_height (&fact, it, cdr,
22370 font, width_p, align_to))
22371 return OK_PIXELS (pixels * fact);
22372 return 0;
22373 }
22374
22375 return 0;
22376 }
22377
22378 return 0;
22379 }
22380
22381 \f
22382 /***********************************************************************
22383 Glyph Display
22384 ***********************************************************************/
22385
22386 #ifdef HAVE_WINDOW_SYSTEM
22387
22388 #ifdef GLYPH_DEBUG
22389
22390 void
22391 dump_glyph_string (struct glyph_string *s)
22392 {
22393 fprintf (stderr, "glyph string\n");
22394 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22395 s->x, s->y, s->width, s->height);
22396 fprintf (stderr, " ybase = %d\n", s->ybase);
22397 fprintf (stderr, " hl = %d\n", s->hl);
22398 fprintf (stderr, " left overhang = %d, right = %d\n",
22399 s->left_overhang, s->right_overhang);
22400 fprintf (stderr, " nchars = %d\n", s->nchars);
22401 fprintf (stderr, " extends to end of line = %d\n",
22402 s->extends_to_end_of_line_p);
22403 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22404 fprintf (stderr, " bg width = %d\n", s->background_width);
22405 }
22406
22407 #endif /* GLYPH_DEBUG */
22408
22409 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22410 of XChar2b structures for S; it can't be allocated in
22411 init_glyph_string because it must be allocated via `alloca'. W
22412 is the window on which S is drawn. ROW and AREA are the glyph row
22413 and area within the row from which S is constructed. START is the
22414 index of the first glyph structure covered by S. HL is a
22415 face-override for drawing S. */
22416
22417 #ifdef HAVE_NTGUI
22418 #define OPTIONAL_HDC(hdc) HDC hdc,
22419 #define DECLARE_HDC(hdc) HDC hdc;
22420 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22421 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22422 #endif
22423
22424 #ifndef OPTIONAL_HDC
22425 #define OPTIONAL_HDC(hdc)
22426 #define DECLARE_HDC(hdc)
22427 #define ALLOCATE_HDC(hdc, f)
22428 #define RELEASE_HDC(hdc, f)
22429 #endif
22430
22431 static void
22432 init_glyph_string (struct glyph_string *s,
22433 OPTIONAL_HDC (hdc)
22434 XChar2b *char2b, struct window *w, struct glyph_row *row,
22435 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22436 {
22437 memset (s, 0, sizeof *s);
22438 s->w = w;
22439 s->f = XFRAME (WVAR (w, frame));
22440 #ifdef HAVE_NTGUI
22441 s->hdc = hdc;
22442 #endif
22443 s->display = FRAME_X_DISPLAY (s->f);
22444 s->window = FRAME_X_WINDOW (s->f);
22445 s->char2b = char2b;
22446 s->hl = hl;
22447 s->row = row;
22448 s->area = area;
22449 s->first_glyph = row->glyphs[area] + start;
22450 s->height = row->height;
22451 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22452 s->ybase = s->y + row->ascent;
22453 }
22454
22455
22456 /* Append the list of glyph strings with head H and tail T to the list
22457 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22458
22459 static inline void
22460 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22461 struct glyph_string *h, struct glyph_string *t)
22462 {
22463 if (h)
22464 {
22465 if (*head)
22466 (*tail)->next = h;
22467 else
22468 *head = h;
22469 h->prev = *tail;
22470 *tail = t;
22471 }
22472 }
22473
22474
22475 /* Prepend the list of glyph strings with head H and tail T to the
22476 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22477 result. */
22478
22479 static inline void
22480 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22481 struct glyph_string *h, struct glyph_string *t)
22482 {
22483 if (h)
22484 {
22485 if (*head)
22486 (*head)->prev = t;
22487 else
22488 *tail = t;
22489 t->next = *head;
22490 *head = h;
22491 }
22492 }
22493
22494
22495 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22496 Set *HEAD and *TAIL to the resulting list. */
22497
22498 static inline void
22499 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22500 struct glyph_string *s)
22501 {
22502 s->next = s->prev = NULL;
22503 append_glyph_string_lists (head, tail, s, s);
22504 }
22505
22506
22507 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22508 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22509 make sure that X resources for the face returned are allocated.
22510 Value is a pointer to a realized face that is ready for display if
22511 DISPLAY_P is non-zero. */
22512
22513 static inline struct face *
22514 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22515 XChar2b *char2b, int display_p)
22516 {
22517 struct face *face = FACE_FROM_ID (f, face_id);
22518
22519 if (face->font)
22520 {
22521 unsigned code = face->font->driver->encode_char (face->font, c);
22522
22523 if (code != FONT_INVALID_CODE)
22524 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22525 else
22526 STORE_XCHAR2B (char2b, 0, 0);
22527 }
22528
22529 /* Make sure X resources of the face are allocated. */
22530 #ifdef HAVE_X_WINDOWS
22531 if (display_p)
22532 #endif
22533 {
22534 eassert (face != NULL);
22535 PREPARE_FACE_FOR_DISPLAY (f, face);
22536 }
22537
22538 return face;
22539 }
22540
22541
22542 /* Get face and two-byte form of character glyph GLYPH on frame F.
22543 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22544 a pointer to a realized face that is ready for display. */
22545
22546 static inline struct face *
22547 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22548 XChar2b *char2b, int *two_byte_p)
22549 {
22550 struct face *face;
22551
22552 eassert (glyph->type == CHAR_GLYPH);
22553 face = FACE_FROM_ID (f, glyph->face_id);
22554
22555 if (two_byte_p)
22556 *two_byte_p = 0;
22557
22558 if (face->font)
22559 {
22560 unsigned code;
22561
22562 if (CHAR_BYTE8_P (glyph->u.ch))
22563 code = CHAR_TO_BYTE8 (glyph->u.ch);
22564 else
22565 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22566
22567 if (code != FONT_INVALID_CODE)
22568 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22569 else
22570 STORE_XCHAR2B (char2b, 0, 0);
22571 }
22572
22573 /* Make sure X resources of the face are allocated. */
22574 eassert (face != NULL);
22575 PREPARE_FACE_FOR_DISPLAY (f, face);
22576 return face;
22577 }
22578
22579
22580 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22581 Return 1 if FONT has a glyph for C, otherwise return 0. */
22582
22583 static inline int
22584 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22585 {
22586 unsigned code;
22587
22588 if (CHAR_BYTE8_P (c))
22589 code = CHAR_TO_BYTE8 (c);
22590 else
22591 code = font->driver->encode_char (font, c);
22592
22593 if (code == FONT_INVALID_CODE)
22594 return 0;
22595 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22596 return 1;
22597 }
22598
22599
22600 /* Fill glyph string S with composition components specified by S->cmp.
22601
22602 BASE_FACE is the base face of the composition.
22603 S->cmp_from is the index of the first component for S.
22604
22605 OVERLAPS non-zero means S should draw the foreground only, and use
22606 its physical height for clipping. See also draw_glyphs.
22607
22608 Value is the index of a component not in S. */
22609
22610 static int
22611 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22612 int overlaps)
22613 {
22614 int i;
22615 /* For all glyphs of this composition, starting at the offset
22616 S->cmp_from, until we reach the end of the definition or encounter a
22617 glyph that requires the different face, add it to S. */
22618 struct face *face;
22619
22620 eassert (s);
22621
22622 s->for_overlaps = overlaps;
22623 s->face = NULL;
22624 s->font = NULL;
22625 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22626 {
22627 int c = COMPOSITION_GLYPH (s->cmp, i);
22628
22629 /* TAB in a composition means display glyphs with padding space
22630 on the left or right. */
22631 if (c != '\t')
22632 {
22633 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22634 -1, Qnil);
22635
22636 face = get_char_face_and_encoding (s->f, c, face_id,
22637 s->char2b + i, 1);
22638 if (face)
22639 {
22640 if (! s->face)
22641 {
22642 s->face = face;
22643 s->font = s->face->font;
22644 }
22645 else if (s->face != face)
22646 break;
22647 }
22648 }
22649 ++s->nchars;
22650 }
22651 s->cmp_to = i;
22652
22653 if (s->face == NULL)
22654 {
22655 s->face = base_face->ascii_face;
22656 s->font = s->face->font;
22657 }
22658
22659 /* All glyph strings for the same composition has the same width,
22660 i.e. the width set for the first component of the composition. */
22661 s->width = s->first_glyph->pixel_width;
22662
22663 /* If the specified font could not be loaded, use the frame's
22664 default font, but record the fact that we couldn't load it in
22665 the glyph string so that we can draw rectangles for the
22666 characters of the glyph string. */
22667 if (s->font == NULL)
22668 {
22669 s->font_not_found_p = 1;
22670 s->font = FRAME_FONT (s->f);
22671 }
22672
22673 /* Adjust base line for subscript/superscript text. */
22674 s->ybase += s->first_glyph->voffset;
22675
22676 /* This glyph string must always be drawn with 16-bit functions. */
22677 s->two_byte_p = 1;
22678
22679 return s->cmp_to;
22680 }
22681
22682 static int
22683 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22684 int start, int end, int overlaps)
22685 {
22686 struct glyph *glyph, *last;
22687 Lisp_Object lgstring;
22688 int i;
22689
22690 s->for_overlaps = overlaps;
22691 glyph = s->row->glyphs[s->area] + start;
22692 last = s->row->glyphs[s->area] + end;
22693 s->cmp_id = glyph->u.cmp.id;
22694 s->cmp_from = glyph->slice.cmp.from;
22695 s->cmp_to = glyph->slice.cmp.to + 1;
22696 s->face = FACE_FROM_ID (s->f, face_id);
22697 lgstring = composition_gstring_from_id (s->cmp_id);
22698 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22699 glyph++;
22700 while (glyph < last
22701 && glyph->u.cmp.automatic
22702 && glyph->u.cmp.id == s->cmp_id
22703 && s->cmp_to == glyph->slice.cmp.from)
22704 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22705
22706 for (i = s->cmp_from; i < s->cmp_to; i++)
22707 {
22708 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22709 unsigned code = LGLYPH_CODE (lglyph);
22710
22711 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22712 }
22713 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22714 return glyph - s->row->glyphs[s->area];
22715 }
22716
22717
22718 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22719 See the comment of fill_glyph_string for arguments.
22720 Value is the index of the first glyph not in S. */
22721
22722
22723 static int
22724 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22725 int start, int end, int overlaps)
22726 {
22727 struct glyph *glyph, *last;
22728 int voffset;
22729
22730 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22731 s->for_overlaps = overlaps;
22732 glyph = s->row->glyphs[s->area] + start;
22733 last = s->row->glyphs[s->area] + end;
22734 voffset = glyph->voffset;
22735 s->face = FACE_FROM_ID (s->f, face_id);
22736 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22737 s->nchars = 1;
22738 s->width = glyph->pixel_width;
22739 glyph++;
22740 while (glyph < last
22741 && glyph->type == GLYPHLESS_GLYPH
22742 && glyph->voffset == voffset
22743 && glyph->face_id == face_id)
22744 {
22745 s->nchars++;
22746 s->width += glyph->pixel_width;
22747 glyph++;
22748 }
22749 s->ybase += voffset;
22750 return glyph - s->row->glyphs[s->area];
22751 }
22752
22753
22754 /* Fill glyph string S from a sequence of character glyphs.
22755
22756 FACE_ID is the face id of the string. START is the index of the
22757 first glyph to consider, END is the index of the last + 1.
22758 OVERLAPS non-zero means S should draw the foreground only, and use
22759 its physical height for clipping. See also draw_glyphs.
22760
22761 Value is the index of the first glyph not in S. */
22762
22763 static int
22764 fill_glyph_string (struct glyph_string *s, int face_id,
22765 int start, int end, int overlaps)
22766 {
22767 struct glyph *glyph, *last;
22768 int voffset;
22769 int glyph_not_available_p;
22770
22771 eassert (s->f == XFRAME (WVAR (s->w, frame)));
22772 eassert (s->nchars == 0);
22773 eassert (start >= 0 && end > start);
22774
22775 s->for_overlaps = overlaps;
22776 glyph = s->row->glyphs[s->area] + start;
22777 last = s->row->glyphs[s->area] + end;
22778 voffset = glyph->voffset;
22779 s->padding_p = glyph->padding_p;
22780 glyph_not_available_p = glyph->glyph_not_available_p;
22781
22782 while (glyph < last
22783 && glyph->type == CHAR_GLYPH
22784 && glyph->voffset == voffset
22785 /* Same face id implies same font, nowadays. */
22786 && glyph->face_id == face_id
22787 && glyph->glyph_not_available_p == glyph_not_available_p)
22788 {
22789 int two_byte_p;
22790
22791 s->face = get_glyph_face_and_encoding (s->f, glyph,
22792 s->char2b + s->nchars,
22793 &two_byte_p);
22794 s->two_byte_p = two_byte_p;
22795 ++s->nchars;
22796 eassert (s->nchars <= end - start);
22797 s->width += glyph->pixel_width;
22798 if (glyph++->padding_p != s->padding_p)
22799 break;
22800 }
22801
22802 s->font = s->face->font;
22803
22804 /* If the specified font could not be loaded, use the frame's font,
22805 but record the fact that we couldn't load it in
22806 S->font_not_found_p so that we can draw rectangles for the
22807 characters of the glyph string. */
22808 if (s->font == NULL || glyph_not_available_p)
22809 {
22810 s->font_not_found_p = 1;
22811 s->font = FRAME_FONT (s->f);
22812 }
22813
22814 /* Adjust base line for subscript/superscript text. */
22815 s->ybase += voffset;
22816
22817 eassert (s->face && s->face->gc);
22818 return glyph - s->row->glyphs[s->area];
22819 }
22820
22821
22822 /* Fill glyph string S from image glyph S->first_glyph. */
22823
22824 static void
22825 fill_image_glyph_string (struct glyph_string *s)
22826 {
22827 eassert (s->first_glyph->type == IMAGE_GLYPH);
22828 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22829 eassert (s->img);
22830 s->slice = s->first_glyph->slice.img;
22831 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22832 s->font = s->face->font;
22833 s->width = s->first_glyph->pixel_width;
22834
22835 /* Adjust base line for subscript/superscript text. */
22836 s->ybase += s->first_glyph->voffset;
22837 }
22838
22839
22840 /* Fill glyph string S from a sequence of stretch glyphs.
22841
22842 START is the index of the first glyph to consider,
22843 END is the index of the last + 1.
22844
22845 Value is the index of the first glyph not in S. */
22846
22847 static int
22848 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22849 {
22850 struct glyph *glyph, *last;
22851 int voffset, face_id;
22852
22853 eassert (s->first_glyph->type == STRETCH_GLYPH);
22854
22855 glyph = s->row->glyphs[s->area] + start;
22856 last = s->row->glyphs[s->area] + end;
22857 face_id = glyph->face_id;
22858 s->face = FACE_FROM_ID (s->f, face_id);
22859 s->font = s->face->font;
22860 s->width = glyph->pixel_width;
22861 s->nchars = 1;
22862 voffset = glyph->voffset;
22863
22864 for (++glyph;
22865 (glyph < last
22866 && glyph->type == STRETCH_GLYPH
22867 && glyph->voffset == voffset
22868 && glyph->face_id == face_id);
22869 ++glyph)
22870 s->width += glyph->pixel_width;
22871
22872 /* Adjust base line for subscript/superscript text. */
22873 s->ybase += voffset;
22874
22875 /* The case that face->gc == 0 is handled when drawing the glyph
22876 string by calling PREPARE_FACE_FOR_DISPLAY. */
22877 eassert (s->face);
22878 return glyph - s->row->glyphs[s->area];
22879 }
22880
22881 static struct font_metrics *
22882 get_per_char_metric (struct font *font, XChar2b *char2b)
22883 {
22884 static struct font_metrics metrics;
22885 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22886
22887 if (! font || code == FONT_INVALID_CODE)
22888 return NULL;
22889 font->driver->text_extents (font, &code, 1, &metrics);
22890 return &metrics;
22891 }
22892
22893 /* EXPORT for RIF:
22894 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22895 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22896 assumed to be zero. */
22897
22898 void
22899 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22900 {
22901 *left = *right = 0;
22902
22903 if (glyph->type == CHAR_GLYPH)
22904 {
22905 struct face *face;
22906 XChar2b char2b;
22907 struct font_metrics *pcm;
22908
22909 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22910 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22911 {
22912 if (pcm->rbearing > pcm->width)
22913 *right = pcm->rbearing - pcm->width;
22914 if (pcm->lbearing < 0)
22915 *left = -pcm->lbearing;
22916 }
22917 }
22918 else if (glyph->type == COMPOSITE_GLYPH)
22919 {
22920 if (! glyph->u.cmp.automatic)
22921 {
22922 struct composition *cmp = composition_table[glyph->u.cmp.id];
22923
22924 if (cmp->rbearing > cmp->pixel_width)
22925 *right = cmp->rbearing - cmp->pixel_width;
22926 if (cmp->lbearing < 0)
22927 *left = - cmp->lbearing;
22928 }
22929 else
22930 {
22931 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22932 struct font_metrics metrics;
22933
22934 composition_gstring_width (gstring, glyph->slice.cmp.from,
22935 glyph->slice.cmp.to + 1, &metrics);
22936 if (metrics.rbearing > metrics.width)
22937 *right = metrics.rbearing - metrics.width;
22938 if (metrics.lbearing < 0)
22939 *left = - metrics.lbearing;
22940 }
22941 }
22942 }
22943
22944
22945 /* Return the index of the first glyph preceding glyph string S that
22946 is overwritten by S because of S's left overhang. Value is -1
22947 if no glyphs are overwritten. */
22948
22949 static int
22950 left_overwritten (struct glyph_string *s)
22951 {
22952 int k;
22953
22954 if (s->left_overhang)
22955 {
22956 int x = 0, i;
22957 struct glyph *glyphs = s->row->glyphs[s->area];
22958 int first = s->first_glyph - glyphs;
22959
22960 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22961 x -= glyphs[i].pixel_width;
22962
22963 k = i + 1;
22964 }
22965 else
22966 k = -1;
22967
22968 return k;
22969 }
22970
22971
22972 /* Return the index of the first glyph preceding glyph string S that
22973 is overwriting S because of its right overhang. Value is -1 if no
22974 glyph in front of S overwrites S. */
22975
22976 static int
22977 left_overwriting (struct glyph_string *s)
22978 {
22979 int i, k, x;
22980 struct glyph *glyphs = s->row->glyphs[s->area];
22981 int first = s->first_glyph - glyphs;
22982
22983 k = -1;
22984 x = 0;
22985 for (i = first - 1; i >= 0; --i)
22986 {
22987 int left, right;
22988 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22989 if (x + right > 0)
22990 k = i;
22991 x -= glyphs[i].pixel_width;
22992 }
22993
22994 return k;
22995 }
22996
22997
22998 /* Return the index of the last glyph following glyph string S that is
22999 overwritten by S because of S's right overhang. Value is -1 if
23000 no such glyph is found. */
23001
23002 static int
23003 right_overwritten (struct glyph_string *s)
23004 {
23005 int k = -1;
23006
23007 if (s->right_overhang)
23008 {
23009 int x = 0, i;
23010 struct glyph *glyphs = s->row->glyphs[s->area];
23011 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23012 int end = s->row->used[s->area];
23013
23014 for (i = first; i < end && s->right_overhang > x; ++i)
23015 x += glyphs[i].pixel_width;
23016
23017 k = i;
23018 }
23019
23020 return k;
23021 }
23022
23023
23024 /* Return the index of the last glyph following glyph string S that
23025 overwrites S because of its left overhang. Value is negative
23026 if no such glyph is found. */
23027
23028 static int
23029 right_overwriting (struct glyph_string *s)
23030 {
23031 int i, k, x;
23032 int end = s->row->used[s->area];
23033 struct glyph *glyphs = s->row->glyphs[s->area];
23034 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23035
23036 k = -1;
23037 x = 0;
23038 for (i = first; i < end; ++i)
23039 {
23040 int left, right;
23041 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23042 if (x - left < 0)
23043 k = i;
23044 x += glyphs[i].pixel_width;
23045 }
23046
23047 return k;
23048 }
23049
23050
23051 /* Set background width of glyph string S. START is the index of the
23052 first glyph following S. LAST_X is the right-most x-position + 1
23053 in the drawing area. */
23054
23055 static inline void
23056 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23057 {
23058 /* If the face of this glyph string has to be drawn to the end of
23059 the drawing area, set S->extends_to_end_of_line_p. */
23060
23061 if (start == s->row->used[s->area]
23062 && s->area == TEXT_AREA
23063 && ((s->row->fill_line_p
23064 && (s->hl == DRAW_NORMAL_TEXT
23065 || s->hl == DRAW_IMAGE_RAISED
23066 || s->hl == DRAW_IMAGE_SUNKEN))
23067 || s->hl == DRAW_MOUSE_FACE))
23068 s->extends_to_end_of_line_p = 1;
23069
23070 /* If S extends its face to the end of the line, set its
23071 background_width to the distance to the right edge of the drawing
23072 area. */
23073 if (s->extends_to_end_of_line_p)
23074 s->background_width = last_x - s->x + 1;
23075 else
23076 s->background_width = s->width;
23077 }
23078
23079
23080 /* Compute overhangs and x-positions for glyph string S and its
23081 predecessors, or successors. X is the starting x-position for S.
23082 BACKWARD_P non-zero means process predecessors. */
23083
23084 static void
23085 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23086 {
23087 if (backward_p)
23088 {
23089 while (s)
23090 {
23091 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23092 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23093 x -= s->width;
23094 s->x = x;
23095 s = s->prev;
23096 }
23097 }
23098 else
23099 {
23100 while (s)
23101 {
23102 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23103 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23104 s->x = x;
23105 x += s->width;
23106 s = s->next;
23107 }
23108 }
23109 }
23110
23111
23112
23113 /* The following macros are only called from draw_glyphs below.
23114 They reference the following parameters of that function directly:
23115 `w', `row', `area', and `overlap_p'
23116 as well as the following local variables:
23117 `s', `f', and `hdc' (in W32) */
23118
23119 #ifdef HAVE_NTGUI
23120 /* On W32, silently add local `hdc' variable to argument list of
23121 init_glyph_string. */
23122 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23123 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23124 #else
23125 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23126 init_glyph_string (s, char2b, w, row, area, start, hl)
23127 #endif
23128
23129 /* Add a glyph string for a stretch glyph to the list of strings
23130 between HEAD and TAIL. START is the index of the stretch glyph in
23131 row area AREA of glyph row ROW. END is the index of the last glyph
23132 in that glyph row area. X is the current output position assigned
23133 to the new glyph string constructed. HL overrides that face of the
23134 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23135 is the right-most x-position of the drawing area. */
23136
23137 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23138 and below -- keep them on one line. */
23139 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23140 do \
23141 { \
23142 s = alloca (sizeof *s); \
23143 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23144 START = fill_stretch_glyph_string (s, START, END); \
23145 append_glyph_string (&HEAD, &TAIL, s); \
23146 s->x = (X); \
23147 } \
23148 while (0)
23149
23150
23151 /* Add a glyph string for an image glyph to the list of strings
23152 between HEAD and TAIL. START is the index of the image glyph in
23153 row area AREA of glyph row ROW. END is the index of the last glyph
23154 in that glyph row area. X is the current output position assigned
23155 to the new glyph string constructed. HL overrides that face of the
23156 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23157 is the right-most x-position of the drawing area. */
23158
23159 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23160 do \
23161 { \
23162 s = alloca (sizeof *s); \
23163 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23164 fill_image_glyph_string (s); \
23165 append_glyph_string (&HEAD, &TAIL, s); \
23166 ++START; \
23167 s->x = (X); \
23168 } \
23169 while (0)
23170
23171
23172 /* Add a glyph string for a sequence of character glyphs to the list
23173 of strings between HEAD and TAIL. START is the index of the first
23174 glyph in row area AREA of glyph row ROW that is part of the new
23175 glyph string. END is the index of the last glyph in that glyph row
23176 area. X is the current output position assigned to the new glyph
23177 string constructed. HL overrides that face of the glyph; e.g. it
23178 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23179 right-most x-position of the drawing area. */
23180
23181 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23182 do \
23183 { \
23184 int face_id; \
23185 XChar2b *char2b; \
23186 \
23187 face_id = (row)->glyphs[area][START].face_id; \
23188 \
23189 s = alloca (sizeof *s); \
23190 char2b = alloca ((END - START) * sizeof *char2b); \
23191 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23192 append_glyph_string (&HEAD, &TAIL, s); \
23193 s->x = (X); \
23194 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23195 } \
23196 while (0)
23197
23198
23199 /* Add a glyph string for a composite sequence to the list of strings
23200 between HEAD and TAIL. START is the index of the first glyph in
23201 row area AREA of glyph row ROW that is part of the new glyph
23202 string. END is the index of the last glyph in that glyph row area.
23203 X is the current output position assigned to the new glyph string
23204 constructed. HL overrides that face of the glyph; e.g. it is
23205 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23206 x-position of the drawing area. */
23207
23208 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23209 do { \
23210 int face_id = (row)->glyphs[area][START].face_id; \
23211 struct face *base_face = FACE_FROM_ID (f, face_id); \
23212 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23213 struct composition *cmp = composition_table[cmp_id]; \
23214 XChar2b *char2b; \
23215 struct glyph_string *first_s = NULL; \
23216 int n; \
23217 \
23218 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23219 \
23220 /* Make glyph_strings for each glyph sequence that is drawable by \
23221 the same face, and append them to HEAD/TAIL. */ \
23222 for (n = 0; n < cmp->glyph_len;) \
23223 { \
23224 s = alloca (sizeof *s); \
23225 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23226 append_glyph_string (&(HEAD), &(TAIL), s); \
23227 s->cmp = cmp; \
23228 s->cmp_from = n; \
23229 s->x = (X); \
23230 if (n == 0) \
23231 first_s = s; \
23232 n = fill_composite_glyph_string (s, base_face, overlaps); \
23233 } \
23234 \
23235 ++START; \
23236 s = first_s; \
23237 } while (0)
23238
23239
23240 /* Add a glyph string for a glyph-string sequence to the list of strings
23241 between HEAD and TAIL. */
23242
23243 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23244 do { \
23245 int face_id; \
23246 XChar2b *char2b; \
23247 Lisp_Object gstring; \
23248 \
23249 face_id = (row)->glyphs[area][START].face_id; \
23250 gstring = (composition_gstring_from_id \
23251 ((row)->glyphs[area][START].u.cmp.id)); \
23252 s = alloca (sizeof *s); \
23253 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23254 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23255 append_glyph_string (&(HEAD), &(TAIL), s); \
23256 s->x = (X); \
23257 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23258 } while (0)
23259
23260
23261 /* Add a glyph string for a sequence of glyphless character's glyphs
23262 to the list of strings between HEAD and TAIL. The meanings of
23263 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23264
23265 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23266 do \
23267 { \
23268 int face_id; \
23269 \
23270 face_id = (row)->glyphs[area][START].face_id; \
23271 \
23272 s = alloca (sizeof *s); \
23273 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23274 append_glyph_string (&HEAD, &TAIL, s); \
23275 s->x = (X); \
23276 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23277 overlaps); \
23278 } \
23279 while (0)
23280
23281
23282 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23283 of AREA of glyph row ROW on window W between indices START and END.
23284 HL overrides the face for drawing glyph strings, e.g. it is
23285 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23286 x-positions of the drawing area.
23287
23288 This is an ugly monster macro construct because we must use alloca
23289 to allocate glyph strings (because draw_glyphs can be called
23290 asynchronously). */
23291
23292 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23293 do \
23294 { \
23295 HEAD = TAIL = NULL; \
23296 while (START < END) \
23297 { \
23298 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23299 switch (first_glyph->type) \
23300 { \
23301 case CHAR_GLYPH: \
23302 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23303 HL, X, LAST_X); \
23304 break; \
23305 \
23306 case COMPOSITE_GLYPH: \
23307 if (first_glyph->u.cmp.automatic) \
23308 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23309 HL, X, LAST_X); \
23310 else \
23311 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23312 HL, X, LAST_X); \
23313 break; \
23314 \
23315 case STRETCH_GLYPH: \
23316 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23317 HL, X, LAST_X); \
23318 break; \
23319 \
23320 case IMAGE_GLYPH: \
23321 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23322 HL, X, LAST_X); \
23323 break; \
23324 \
23325 case GLYPHLESS_GLYPH: \
23326 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23327 HL, X, LAST_X); \
23328 break; \
23329 \
23330 default: \
23331 abort (); \
23332 } \
23333 \
23334 if (s) \
23335 { \
23336 set_glyph_string_background_width (s, START, LAST_X); \
23337 (X) += s->width; \
23338 } \
23339 } \
23340 } while (0)
23341
23342
23343 /* Draw glyphs between START and END in AREA of ROW on window W,
23344 starting at x-position X. X is relative to AREA in W. HL is a
23345 face-override with the following meaning:
23346
23347 DRAW_NORMAL_TEXT draw normally
23348 DRAW_CURSOR draw in cursor face
23349 DRAW_MOUSE_FACE draw in mouse face.
23350 DRAW_INVERSE_VIDEO draw in mode line face
23351 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23352 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23353
23354 If OVERLAPS is non-zero, draw only the foreground of characters and
23355 clip to the physical height of ROW. Non-zero value also defines
23356 the overlapping part to be drawn:
23357
23358 OVERLAPS_PRED overlap with preceding rows
23359 OVERLAPS_SUCC overlap with succeeding rows
23360 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23361 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23362
23363 Value is the x-position reached, relative to AREA of W. */
23364
23365 static int
23366 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23367 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23368 enum draw_glyphs_face hl, int overlaps)
23369 {
23370 struct glyph_string *head, *tail;
23371 struct glyph_string *s;
23372 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23373 int i, j, x_reached, last_x, area_left = 0;
23374 struct frame *f = XFRAME (WINDOW_FRAME (w));
23375 DECLARE_HDC (hdc);
23376
23377 ALLOCATE_HDC (hdc, f);
23378
23379 /* Let's rather be paranoid than getting a SEGV. */
23380 end = min (end, row->used[area]);
23381 start = max (0, start);
23382 start = min (end, start);
23383
23384 /* Translate X to frame coordinates. Set last_x to the right
23385 end of the drawing area. */
23386 if (row->full_width_p)
23387 {
23388 /* X is relative to the left edge of W, without scroll bars
23389 or fringes. */
23390 area_left = WINDOW_LEFT_EDGE_X (w);
23391 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23392 }
23393 else
23394 {
23395 area_left = window_box_left (w, area);
23396 last_x = area_left + window_box_width (w, area);
23397 }
23398 x += area_left;
23399
23400 /* Build a doubly-linked list of glyph_string structures between
23401 head and tail from what we have to draw. Note that the macro
23402 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23403 the reason we use a separate variable `i'. */
23404 i = start;
23405 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23406 if (tail)
23407 x_reached = tail->x + tail->background_width;
23408 else
23409 x_reached = x;
23410
23411 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23412 the row, redraw some glyphs in front or following the glyph
23413 strings built above. */
23414 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23415 {
23416 struct glyph_string *h, *t;
23417 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23418 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23419 int check_mouse_face = 0;
23420 int dummy_x = 0;
23421
23422 /* If mouse highlighting is on, we may need to draw adjacent
23423 glyphs using mouse-face highlighting. */
23424 if (area == TEXT_AREA && row->mouse_face_p)
23425 {
23426 struct glyph_row *mouse_beg_row, *mouse_end_row;
23427
23428 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23429 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23430
23431 if (row >= mouse_beg_row && row <= mouse_end_row)
23432 {
23433 check_mouse_face = 1;
23434 mouse_beg_col = (row == mouse_beg_row)
23435 ? hlinfo->mouse_face_beg_col : 0;
23436 mouse_end_col = (row == mouse_end_row)
23437 ? hlinfo->mouse_face_end_col
23438 : row->used[TEXT_AREA];
23439 }
23440 }
23441
23442 /* Compute overhangs for all glyph strings. */
23443 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23444 for (s = head; s; s = s->next)
23445 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23446
23447 /* Prepend glyph strings for glyphs in front of the first glyph
23448 string that are overwritten because of the first glyph
23449 string's left overhang. The background of all strings
23450 prepended must be drawn because the first glyph string
23451 draws over it. */
23452 i = left_overwritten (head);
23453 if (i >= 0)
23454 {
23455 enum draw_glyphs_face overlap_hl;
23456
23457 /* If this row contains mouse highlighting, attempt to draw
23458 the overlapped glyphs with the correct highlight. This
23459 code fails if the overlap encompasses more than one glyph
23460 and mouse-highlight spans only some of these glyphs.
23461 However, making it work perfectly involves a lot more
23462 code, and I don't know if the pathological case occurs in
23463 practice, so we'll stick to this for now. --- cyd */
23464 if (check_mouse_face
23465 && mouse_beg_col < start && mouse_end_col > i)
23466 overlap_hl = DRAW_MOUSE_FACE;
23467 else
23468 overlap_hl = DRAW_NORMAL_TEXT;
23469
23470 j = i;
23471 BUILD_GLYPH_STRINGS (j, start, h, t,
23472 overlap_hl, dummy_x, last_x);
23473 start = i;
23474 compute_overhangs_and_x (t, head->x, 1);
23475 prepend_glyph_string_lists (&head, &tail, h, t);
23476 clip_head = head;
23477 }
23478
23479 /* Prepend glyph strings for glyphs in front of the first glyph
23480 string that overwrite that glyph string because of their
23481 right overhang. For these strings, only the foreground must
23482 be drawn, because it draws over the glyph string at `head'.
23483 The background must not be drawn because this would overwrite
23484 right overhangs of preceding glyphs for which no glyph
23485 strings exist. */
23486 i = left_overwriting (head);
23487 if (i >= 0)
23488 {
23489 enum draw_glyphs_face overlap_hl;
23490
23491 if (check_mouse_face
23492 && mouse_beg_col < start && mouse_end_col > i)
23493 overlap_hl = DRAW_MOUSE_FACE;
23494 else
23495 overlap_hl = DRAW_NORMAL_TEXT;
23496
23497 clip_head = head;
23498 BUILD_GLYPH_STRINGS (i, start, h, t,
23499 overlap_hl, dummy_x, last_x);
23500 for (s = h; s; s = s->next)
23501 s->background_filled_p = 1;
23502 compute_overhangs_and_x (t, head->x, 1);
23503 prepend_glyph_string_lists (&head, &tail, h, t);
23504 }
23505
23506 /* Append glyphs strings for glyphs following the last glyph
23507 string tail that are overwritten by tail. The background of
23508 these strings has to be drawn because tail's foreground draws
23509 over it. */
23510 i = right_overwritten (tail);
23511 if (i >= 0)
23512 {
23513 enum draw_glyphs_face overlap_hl;
23514
23515 if (check_mouse_face
23516 && mouse_beg_col < i && mouse_end_col > end)
23517 overlap_hl = DRAW_MOUSE_FACE;
23518 else
23519 overlap_hl = DRAW_NORMAL_TEXT;
23520
23521 BUILD_GLYPH_STRINGS (end, i, h, t,
23522 overlap_hl, x, last_x);
23523 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23524 we don't have `end = i;' here. */
23525 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23526 append_glyph_string_lists (&head, &tail, h, t);
23527 clip_tail = tail;
23528 }
23529
23530 /* Append glyph strings for glyphs following the last glyph
23531 string tail that overwrite tail. The foreground of such
23532 glyphs has to be drawn because it writes into the background
23533 of tail. The background must not be drawn because it could
23534 paint over the foreground of following glyphs. */
23535 i = right_overwriting (tail);
23536 if (i >= 0)
23537 {
23538 enum draw_glyphs_face overlap_hl;
23539 if (check_mouse_face
23540 && mouse_beg_col < i && mouse_end_col > end)
23541 overlap_hl = DRAW_MOUSE_FACE;
23542 else
23543 overlap_hl = DRAW_NORMAL_TEXT;
23544
23545 clip_tail = tail;
23546 i++; /* We must include the Ith glyph. */
23547 BUILD_GLYPH_STRINGS (end, i, h, t,
23548 overlap_hl, x, last_x);
23549 for (s = h; s; s = s->next)
23550 s->background_filled_p = 1;
23551 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23552 append_glyph_string_lists (&head, &tail, h, t);
23553 }
23554 if (clip_head || clip_tail)
23555 for (s = head; s; s = s->next)
23556 {
23557 s->clip_head = clip_head;
23558 s->clip_tail = clip_tail;
23559 }
23560 }
23561
23562 /* Draw all strings. */
23563 for (s = head; s; s = s->next)
23564 FRAME_RIF (f)->draw_glyph_string (s);
23565
23566 #ifndef HAVE_NS
23567 /* When focus a sole frame and move horizontally, this sets on_p to 0
23568 causing a failure to erase prev cursor position. */
23569 if (area == TEXT_AREA
23570 && !row->full_width_p
23571 /* When drawing overlapping rows, only the glyph strings'
23572 foreground is drawn, which doesn't erase a cursor
23573 completely. */
23574 && !overlaps)
23575 {
23576 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23577 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23578 : (tail ? tail->x + tail->background_width : x));
23579 x0 -= area_left;
23580 x1 -= area_left;
23581
23582 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23583 row->y, MATRIX_ROW_BOTTOM_Y (row));
23584 }
23585 #endif
23586
23587 /* Value is the x-position up to which drawn, relative to AREA of W.
23588 This doesn't include parts drawn because of overhangs. */
23589 if (row->full_width_p)
23590 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23591 else
23592 x_reached -= area_left;
23593
23594 RELEASE_HDC (hdc, f);
23595
23596 return x_reached;
23597 }
23598
23599 /* Expand row matrix if too narrow. Don't expand if area
23600 is not present. */
23601
23602 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23603 { \
23604 if (!fonts_changed_p \
23605 && (it->glyph_row->glyphs[area] \
23606 < it->glyph_row->glyphs[area + 1])) \
23607 { \
23608 it->w->ncols_scale_factor++; \
23609 fonts_changed_p = 1; \
23610 } \
23611 }
23612
23613 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23614 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23615
23616 static inline void
23617 append_glyph (struct it *it)
23618 {
23619 struct glyph *glyph;
23620 enum glyph_row_area area = it->area;
23621
23622 eassert (it->glyph_row);
23623 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23624
23625 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23626 if (glyph < it->glyph_row->glyphs[area + 1])
23627 {
23628 /* If the glyph row is reversed, we need to prepend the glyph
23629 rather than append it. */
23630 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23631 {
23632 struct glyph *g;
23633
23634 /* Make room for the additional glyph. */
23635 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23636 g[1] = *g;
23637 glyph = it->glyph_row->glyphs[area];
23638 }
23639 glyph->charpos = CHARPOS (it->position);
23640 glyph->object = it->object;
23641 if (it->pixel_width > 0)
23642 {
23643 glyph->pixel_width = it->pixel_width;
23644 glyph->padding_p = 0;
23645 }
23646 else
23647 {
23648 /* Assure at least 1-pixel width. Otherwise, cursor can't
23649 be displayed correctly. */
23650 glyph->pixel_width = 1;
23651 glyph->padding_p = 1;
23652 }
23653 glyph->ascent = it->ascent;
23654 glyph->descent = it->descent;
23655 glyph->voffset = it->voffset;
23656 glyph->type = CHAR_GLYPH;
23657 glyph->avoid_cursor_p = it->avoid_cursor_p;
23658 glyph->multibyte_p = it->multibyte_p;
23659 glyph->left_box_line_p = it->start_of_box_run_p;
23660 glyph->right_box_line_p = it->end_of_box_run_p;
23661 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23662 || it->phys_descent > it->descent);
23663 glyph->glyph_not_available_p = it->glyph_not_available_p;
23664 glyph->face_id = it->face_id;
23665 glyph->u.ch = it->char_to_display;
23666 glyph->slice.img = null_glyph_slice;
23667 glyph->font_type = FONT_TYPE_UNKNOWN;
23668 if (it->bidi_p)
23669 {
23670 glyph->resolved_level = it->bidi_it.resolved_level;
23671 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23672 abort ();
23673 glyph->bidi_type = it->bidi_it.type;
23674 }
23675 else
23676 {
23677 glyph->resolved_level = 0;
23678 glyph->bidi_type = UNKNOWN_BT;
23679 }
23680 ++it->glyph_row->used[area];
23681 }
23682 else
23683 IT_EXPAND_MATRIX_WIDTH (it, area);
23684 }
23685
23686 /* Store one glyph for the composition IT->cmp_it.id in
23687 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23688 non-null. */
23689
23690 static inline void
23691 append_composite_glyph (struct it *it)
23692 {
23693 struct glyph *glyph;
23694 enum glyph_row_area area = it->area;
23695
23696 eassert (it->glyph_row);
23697
23698 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23699 if (glyph < it->glyph_row->glyphs[area + 1])
23700 {
23701 /* If the glyph row is reversed, we need to prepend the glyph
23702 rather than append it. */
23703 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23704 {
23705 struct glyph *g;
23706
23707 /* Make room for the new glyph. */
23708 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23709 g[1] = *g;
23710 glyph = it->glyph_row->glyphs[it->area];
23711 }
23712 glyph->charpos = it->cmp_it.charpos;
23713 glyph->object = it->object;
23714 glyph->pixel_width = it->pixel_width;
23715 glyph->ascent = it->ascent;
23716 glyph->descent = it->descent;
23717 glyph->voffset = it->voffset;
23718 glyph->type = COMPOSITE_GLYPH;
23719 if (it->cmp_it.ch < 0)
23720 {
23721 glyph->u.cmp.automatic = 0;
23722 glyph->u.cmp.id = it->cmp_it.id;
23723 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23724 }
23725 else
23726 {
23727 glyph->u.cmp.automatic = 1;
23728 glyph->u.cmp.id = it->cmp_it.id;
23729 glyph->slice.cmp.from = it->cmp_it.from;
23730 glyph->slice.cmp.to = it->cmp_it.to - 1;
23731 }
23732 glyph->avoid_cursor_p = it->avoid_cursor_p;
23733 glyph->multibyte_p = it->multibyte_p;
23734 glyph->left_box_line_p = it->start_of_box_run_p;
23735 glyph->right_box_line_p = it->end_of_box_run_p;
23736 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23737 || it->phys_descent > it->descent);
23738 glyph->padding_p = 0;
23739 glyph->glyph_not_available_p = 0;
23740 glyph->face_id = it->face_id;
23741 glyph->font_type = FONT_TYPE_UNKNOWN;
23742 if (it->bidi_p)
23743 {
23744 glyph->resolved_level = it->bidi_it.resolved_level;
23745 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23746 abort ();
23747 glyph->bidi_type = it->bidi_it.type;
23748 }
23749 ++it->glyph_row->used[area];
23750 }
23751 else
23752 IT_EXPAND_MATRIX_WIDTH (it, area);
23753 }
23754
23755
23756 /* Change IT->ascent and IT->height according to the setting of
23757 IT->voffset. */
23758
23759 static inline void
23760 take_vertical_position_into_account (struct it *it)
23761 {
23762 if (it->voffset)
23763 {
23764 if (it->voffset < 0)
23765 /* Increase the ascent so that we can display the text higher
23766 in the line. */
23767 it->ascent -= it->voffset;
23768 else
23769 /* Increase the descent so that we can display the text lower
23770 in the line. */
23771 it->descent += it->voffset;
23772 }
23773 }
23774
23775
23776 /* Produce glyphs/get display metrics for the image IT is loaded with.
23777 See the description of struct display_iterator in dispextern.h for
23778 an overview of struct display_iterator. */
23779
23780 static void
23781 produce_image_glyph (struct it *it)
23782 {
23783 struct image *img;
23784 struct face *face;
23785 int glyph_ascent, crop;
23786 struct glyph_slice slice;
23787
23788 eassert (it->what == IT_IMAGE);
23789
23790 face = FACE_FROM_ID (it->f, it->face_id);
23791 eassert (face);
23792 /* Make sure X resources of the face is loaded. */
23793 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23794
23795 if (it->image_id < 0)
23796 {
23797 /* Fringe bitmap. */
23798 it->ascent = it->phys_ascent = 0;
23799 it->descent = it->phys_descent = 0;
23800 it->pixel_width = 0;
23801 it->nglyphs = 0;
23802 return;
23803 }
23804
23805 img = IMAGE_FROM_ID (it->f, it->image_id);
23806 eassert (img);
23807 /* Make sure X resources of the image is loaded. */
23808 prepare_image_for_display (it->f, img);
23809
23810 slice.x = slice.y = 0;
23811 slice.width = img->width;
23812 slice.height = img->height;
23813
23814 if (INTEGERP (it->slice.x))
23815 slice.x = XINT (it->slice.x);
23816 else if (FLOATP (it->slice.x))
23817 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23818
23819 if (INTEGERP (it->slice.y))
23820 slice.y = XINT (it->slice.y);
23821 else if (FLOATP (it->slice.y))
23822 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23823
23824 if (INTEGERP (it->slice.width))
23825 slice.width = XINT (it->slice.width);
23826 else if (FLOATP (it->slice.width))
23827 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23828
23829 if (INTEGERP (it->slice.height))
23830 slice.height = XINT (it->slice.height);
23831 else if (FLOATP (it->slice.height))
23832 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23833
23834 if (slice.x >= img->width)
23835 slice.x = img->width;
23836 if (slice.y >= img->height)
23837 slice.y = img->height;
23838 if (slice.x + slice.width >= img->width)
23839 slice.width = img->width - slice.x;
23840 if (slice.y + slice.height > img->height)
23841 slice.height = img->height - slice.y;
23842
23843 if (slice.width == 0 || slice.height == 0)
23844 return;
23845
23846 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23847
23848 it->descent = slice.height - glyph_ascent;
23849 if (slice.y == 0)
23850 it->descent += img->vmargin;
23851 if (slice.y + slice.height == img->height)
23852 it->descent += img->vmargin;
23853 it->phys_descent = it->descent;
23854
23855 it->pixel_width = slice.width;
23856 if (slice.x == 0)
23857 it->pixel_width += img->hmargin;
23858 if (slice.x + slice.width == img->width)
23859 it->pixel_width += img->hmargin;
23860
23861 /* It's quite possible for images to have an ascent greater than
23862 their height, so don't get confused in that case. */
23863 if (it->descent < 0)
23864 it->descent = 0;
23865
23866 it->nglyphs = 1;
23867
23868 if (face->box != FACE_NO_BOX)
23869 {
23870 if (face->box_line_width > 0)
23871 {
23872 if (slice.y == 0)
23873 it->ascent += face->box_line_width;
23874 if (slice.y + slice.height == img->height)
23875 it->descent += face->box_line_width;
23876 }
23877
23878 if (it->start_of_box_run_p && slice.x == 0)
23879 it->pixel_width += eabs (face->box_line_width);
23880 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23881 it->pixel_width += eabs (face->box_line_width);
23882 }
23883
23884 take_vertical_position_into_account (it);
23885
23886 /* Automatically crop wide image glyphs at right edge so we can
23887 draw the cursor on same display row. */
23888 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23889 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23890 {
23891 it->pixel_width -= crop;
23892 slice.width -= crop;
23893 }
23894
23895 if (it->glyph_row)
23896 {
23897 struct glyph *glyph;
23898 enum glyph_row_area area = it->area;
23899
23900 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23901 if (glyph < it->glyph_row->glyphs[area + 1])
23902 {
23903 glyph->charpos = CHARPOS (it->position);
23904 glyph->object = it->object;
23905 glyph->pixel_width = it->pixel_width;
23906 glyph->ascent = glyph_ascent;
23907 glyph->descent = it->descent;
23908 glyph->voffset = it->voffset;
23909 glyph->type = IMAGE_GLYPH;
23910 glyph->avoid_cursor_p = it->avoid_cursor_p;
23911 glyph->multibyte_p = it->multibyte_p;
23912 glyph->left_box_line_p = it->start_of_box_run_p;
23913 glyph->right_box_line_p = it->end_of_box_run_p;
23914 glyph->overlaps_vertically_p = 0;
23915 glyph->padding_p = 0;
23916 glyph->glyph_not_available_p = 0;
23917 glyph->face_id = it->face_id;
23918 glyph->u.img_id = img->id;
23919 glyph->slice.img = slice;
23920 glyph->font_type = FONT_TYPE_UNKNOWN;
23921 if (it->bidi_p)
23922 {
23923 glyph->resolved_level = it->bidi_it.resolved_level;
23924 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23925 abort ();
23926 glyph->bidi_type = it->bidi_it.type;
23927 }
23928 ++it->glyph_row->used[area];
23929 }
23930 else
23931 IT_EXPAND_MATRIX_WIDTH (it, area);
23932 }
23933 }
23934
23935
23936 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23937 of the glyph, WIDTH and HEIGHT are the width and height of the
23938 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23939
23940 static void
23941 append_stretch_glyph (struct it *it, Lisp_Object object,
23942 int width, int height, int ascent)
23943 {
23944 struct glyph *glyph;
23945 enum glyph_row_area area = it->area;
23946
23947 eassert (ascent >= 0 && ascent <= height);
23948
23949 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23950 if (glyph < it->glyph_row->glyphs[area + 1])
23951 {
23952 /* If the glyph row is reversed, we need to prepend the glyph
23953 rather than append it. */
23954 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23955 {
23956 struct glyph *g;
23957
23958 /* Make room for the additional glyph. */
23959 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23960 g[1] = *g;
23961 glyph = it->glyph_row->glyphs[area];
23962 }
23963 glyph->charpos = CHARPOS (it->position);
23964 glyph->object = object;
23965 glyph->pixel_width = width;
23966 glyph->ascent = ascent;
23967 glyph->descent = height - ascent;
23968 glyph->voffset = it->voffset;
23969 glyph->type = STRETCH_GLYPH;
23970 glyph->avoid_cursor_p = it->avoid_cursor_p;
23971 glyph->multibyte_p = it->multibyte_p;
23972 glyph->left_box_line_p = it->start_of_box_run_p;
23973 glyph->right_box_line_p = it->end_of_box_run_p;
23974 glyph->overlaps_vertically_p = 0;
23975 glyph->padding_p = 0;
23976 glyph->glyph_not_available_p = 0;
23977 glyph->face_id = it->face_id;
23978 glyph->u.stretch.ascent = ascent;
23979 glyph->u.stretch.height = height;
23980 glyph->slice.img = null_glyph_slice;
23981 glyph->font_type = FONT_TYPE_UNKNOWN;
23982 if (it->bidi_p)
23983 {
23984 glyph->resolved_level = it->bidi_it.resolved_level;
23985 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23986 abort ();
23987 glyph->bidi_type = it->bidi_it.type;
23988 }
23989 else
23990 {
23991 glyph->resolved_level = 0;
23992 glyph->bidi_type = UNKNOWN_BT;
23993 }
23994 ++it->glyph_row->used[area];
23995 }
23996 else
23997 IT_EXPAND_MATRIX_WIDTH (it, area);
23998 }
23999
24000 #endif /* HAVE_WINDOW_SYSTEM */
24001
24002 /* Produce a stretch glyph for iterator IT. IT->object is the value
24003 of the glyph property displayed. The value must be a list
24004 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24005 being recognized:
24006
24007 1. `:width WIDTH' specifies that the space should be WIDTH *
24008 canonical char width wide. WIDTH may be an integer or floating
24009 point number.
24010
24011 2. `:relative-width FACTOR' specifies that the width of the stretch
24012 should be computed from the width of the first character having the
24013 `glyph' property, and should be FACTOR times that width.
24014
24015 3. `:align-to HPOS' specifies that the space should be wide enough
24016 to reach HPOS, a value in canonical character units.
24017
24018 Exactly one of the above pairs must be present.
24019
24020 4. `:height HEIGHT' specifies that the height of the stretch produced
24021 should be HEIGHT, measured in canonical character units.
24022
24023 5. `:relative-height FACTOR' specifies that the height of the
24024 stretch should be FACTOR times the height of the characters having
24025 the glyph property.
24026
24027 Either none or exactly one of 4 or 5 must be present.
24028
24029 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24030 of the stretch should be used for the ascent of the stretch.
24031 ASCENT must be in the range 0 <= ASCENT <= 100. */
24032
24033 void
24034 produce_stretch_glyph (struct it *it)
24035 {
24036 /* (space :width WIDTH :height HEIGHT ...) */
24037 Lisp_Object prop, plist;
24038 int width = 0, height = 0, align_to = -1;
24039 int zero_width_ok_p = 0;
24040 int ascent = 0;
24041 double tem;
24042 struct face *face = NULL;
24043 struct font *font = NULL;
24044
24045 #ifdef HAVE_WINDOW_SYSTEM
24046 int zero_height_ok_p = 0;
24047
24048 if (FRAME_WINDOW_P (it->f))
24049 {
24050 face = FACE_FROM_ID (it->f, it->face_id);
24051 font = face->font ? face->font : FRAME_FONT (it->f);
24052 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24053 }
24054 #endif
24055
24056 /* List should start with `space'. */
24057 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24058 plist = XCDR (it->object);
24059
24060 /* Compute the width of the stretch. */
24061 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24062 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24063 {
24064 /* Absolute width `:width WIDTH' specified and valid. */
24065 zero_width_ok_p = 1;
24066 width = (int)tem;
24067 }
24068 #ifdef HAVE_WINDOW_SYSTEM
24069 else if (FRAME_WINDOW_P (it->f)
24070 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24071 {
24072 /* Relative width `:relative-width FACTOR' specified and valid.
24073 Compute the width of the characters having the `glyph'
24074 property. */
24075 struct it it2;
24076 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24077
24078 it2 = *it;
24079 if (it->multibyte_p)
24080 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24081 else
24082 {
24083 it2.c = it2.char_to_display = *p, it2.len = 1;
24084 if (! ASCII_CHAR_P (it2.c))
24085 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24086 }
24087
24088 it2.glyph_row = NULL;
24089 it2.what = IT_CHARACTER;
24090 x_produce_glyphs (&it2);
24091 width = NUMVAL (prop) * it2.pixel_width;
24092 }
24093 #endif /* HAVE_WINDOW_SYSTEM */
24094 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24095 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24096 {
24097 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24098 align_to = (align_to < 0
24099 ? 0
24100 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24101 else if (align_to < 0)
24102 align_to = window_box_left_offset (it->w, TEXT_AREA);
24103 width = max (0, (int)tem + align_to - it->current_x);
24104 zero_width_ok_p = 1;
24105 }
24106 else
24107 /* Nothing specified -> width defaults to canonical char width. */
24108 width = FRAME_COLUMN_WIDTH (it->f);
24109
24110 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24111 width = 1;
24112
24113 #ifdef HAVE_WINDOW_SYSTEM
24114 /* Compute height. */
24115 if (FRAME_WINDOW_P (it->f))
24116 {
24117 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24118 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24119 {
24120 height = (int)tem;
24121 zero_height_ok_p = 1;
24122 }
24123 else if (prop = Fplist_get (plist, QCrelative_height),
24124 NUMVAL (prop) > 0)
24125 height = FONT_HEIGHT (font) * NUMVAL (prop);
24126 else
24127 height = FONT_HEIGHT (font);
24128
24129 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24130 height = 1;
24131
24132 /* Compute percentage of height used for ascent. If
24133 `:ascent ASCENT' is present and valid, use that. Otherwise,
24134 derive the ascent from the font in use. */
24135 if (prop = Fplist_get (plist, QCascent),
24136 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24137 ascent = height * NUMVAL (prop) / 100.0;
24138 else if (!NILP (prop)
24139 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24140 ascent = min (max (0, (int)tem), height);
24141 else
24142 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24143 }
24144 else
24145 #endif /* HAVE_WINDOW_SYSTEM */
24146 height = 1;
24147
24148 if (width > 0 && it->line_wrap != TRUNCATE
24149 && it->current_x + width > it->last_visible_x)
24150 {
24151 width = it->last_visible_x - it->current_x;
24152 #ifdef HAVE_WINDOW_SYSTEM
24153 /* Subtract one more pixel from the stretch width, but only on
24154 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24155 width -= FRAME_WINDOW_P (it->f);
24156 #endif
24157 }
24158
24159 if (width > 0 && height > 0 && it->glyph_row)
24160 {
24161 Lisp_Object o_object = it->object;
24162 Lisp_Object object = it->stack[it->sp - 1].string;
24163 int n = width;
24164
24165 if (!STRINGP (object))
24166 object = WVAR (it->w, buffer);
24167 #ifdef HAVE_WINDOW_SYSTEM
24168 if (FRAME_WINDOW_P (it->f))
24169 append_stretch_glyph (it, object, width, height, ascent);
24170 else
24171 #endif
24172 {
24173 it->object = object;
24174 it->char_to_display = ' ';
24175 it->pixel_width = it->len = 1;
24176 while (n--)
24177 tty_append_glyph (it);
24178 it->object = o_object;
24179 }
24180 }
24181
24182 it->pixel_width = width;
24183 #ifdef HAVE_WINDOW_SYSTEM
24184 if (FRAME_WINDOW_P (it->f))
24185 {
24186 it->ascent = it->phys_ascent = ascent;
24187 it->descent = it->phys_descent = height - it->ascent;
24188 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24189 take_vertical_position_into_account (it);
24190 }
24191 else
24192 #endif
24193 it->nglyphs = width;
24194 }
24195
24196 /* Get information about special display element WHAT in an
24197 environment described by IT. WHAT is one of IT_TRUNCATION or
24198 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24199 non-null glyph_row member. This function ensures that fields like
24200 face_id, c, len of IT are left untouched. */
24201
24202 static void
24203 produce_special_glyphs (struct it *it, enum display_element_type what)
24204 {
24205 struct it temp_it;
24206 Lisp_Object gc;
24207 GLYPH glyph;
24208
24209 temp_it = *it;
24210 temp_it.object = make_number (0);
24211 memset (&temp_it.current, 0, sizeof temp_it.current);
24212
24213 if (what == IT_CONTINUATION)
24214 {
24215 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24216 if (it->bidi_it.paragraph_dir == R2L)
24217 SET_GLYPH_FROM_CHAR (glyph, '/');
24218 else
24219 SET_GLYPH_FROM_CHAR (glyph, '\\');
24220 if (it->dp
24221 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24222 {
24223 /* FIXME: Should we mirror GC for R2L lines? */
24224 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24225 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24226 }
24227 }
24228 else if (what == IT_TRUNCATION)
24229 {
24230 /* Truncation glyph. */
24231 SET_GLYPH_FROM_CHAR (glyph, '$');
24232 if (it->dp
24233 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24234 {
24235 /* FIXME: Should we mirror GC for R2L lines? */
24236 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24237 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24238 }
24239 }
24240 else
24241 abort ();
24242
24243 #ifdef HAVE_WINDOW_SYSTEM
24244 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24245 is turned off, we precede the truncation/continuation glyphs by a
24246 stretch glyph whose width is computed such that these special
24247 glyphs are aligned at the window margin, even when very different
24248 fonts are used in different glyph rows. */
24249 if (FRAME_WINDOW_P (temp_it.f)
24250 /* init_iterator calls this with it->glyph_row == NULL, and it
24251 wants only the pixel width of the truncation/continuation
24252 glyphs. */
24253 && temp_it.glyph_row
24254 /* insert_left_trunc_glyphs calls us at the beginning of the
24255 row, and it has its own calculation of the stretch glyph
24256 width. */
24257 && temp_it.glyph_row->used[TEXT_AREA] > 0
24258 && (temp_it.glyph_row->reversed_p
24259 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24260 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24261 {
24262 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24263
24264 if (stretch_width > 0)
24265 {
24266 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24267 struct font *font =
24268 face->font ? face->font : FRAME_FONT (temp_it.f);
24269 int stretch_ascent =
24270 (((temp_it.ascent + temp_it.descent)
24271 * FONT_BASE (font)) / FONT_HEIGHT (font));
24272
24273 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24274 temp_it.ascent + temp_it.descent,
24275 stretch_ascent);
24276 }
24277 }
24278 #endif
24279
24280 temp_it.dp = NULL;
24281 temp_it.what = IT_CHARACTER;
24282 temp_it.len = 1;
24283 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24284 temp_it.face_id = GLYPH_FACE (glyph);
24285 temp_it.len = CHAR_BYTES (temp_it.c);
24286
24287 PRODUCE_GLYPHS (&temp_it);
24288 it->pixel_width = temp_it.pixel_width;
24289 it->nglyphs = temp_it.pixel_width;
24290 }
24291
24292 #ifdef HAVE_WINDOW_SYSTEM
24293
24294 /* Calculate line-height and line-spacing properties.
24295 An integer value specifies explicit pixel value.
24296 A float value specifies relative value to current face height.
24297 A cons (float . face-name) specifies relative value to
24298 height of specified face font.
24299
24300 Returns height in pixels, or nil. */
24301
24302
24303 static Lisp_Object
24304 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24305 int boff, int override)
24306 {
24307 Lisp_Object face_name = Qnil;
24308 int ascent, descent, height;
24309
24310 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24311 return val;
24312
24313 if (CONSP (val))
24314 {
24315 face_name = XCAR (val);
24316 val = XCDR (val);
24317 if (!NUMBERP (val))
24318 val = make_number (1);
24319 if (NILP (face_name))
24320 {
24321 height = it->ascent + it->descent;
24322 goto scale;
24323 }
24324 }
24325
24326 if (NILP (face_name))
24327 {
24328 font = FRAME_FONT (it->f);
24329 boff = FRAME_BASELINE_OFFSET (it->f);
24330 }
24331 else if (EQ (face_name, Qt))
24332 {
24333 override = 0;
24334 }
24335 else
24336 {
24337 int face_id;
24338 struct face *face;
24339
24340 face_id = lookup_named_face (it->f, face_name, 0);
24341 if (face_id < 0)
24342 return make_number (-1);
24343
24344 face = FACE_FROM_ID (it->f, face_id);
24345 font = face->font;
24346 if (font == NULL)
24347 return make_number (-1);
24348 boff = font->baseline_offset;
24349 if (font->vertical_centering)
24350 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24351 }
24352
24353 ascent = FONT_BASE (font) + boff;
24354 descent = FONT_DESCENT (font) - boff;
24355
24356 if (override)
24357 {
24358 it->override_ascent = ascent;
24359 it->override_descent = descent;
24360 it->override_boff = boff;
24361 }
24362
24363 height = ascent + descent;
24364
24365 scale:
24366 if (FLOATP (val))
24367 height = (int)(XFLOAT_DATA (val) * height);
24368 else if (INTEGERP (val))
24369 height *= XINT (val);
24370
24371 return make_number (height);
24372 }
24373
24374
24375 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24376 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24377 and only if this is for a character for which no font was found.
24378
24379 If the display method (it->glyphless_method) is
24380 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24381 length of the acronym or the hexadecimal string, UPPER_XOFF and
24382 UPPER_YOFF are pixel offsets for the upper part of the string,
24383 LOWER_XOFF and LOWER_YOFF are for the lower part.
24384
24385 For the other display methods, LEN through LOWER_YOFF are zero. */
24386
24387 static void
24388 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24389 short upper_xoff, short upper_yoff,
24390 short lower_xoff, short lower_yoff)
24391 {
24392 struct glyph *glyph;
24393 enum glyph_row_area area = it->area;
24394
24395 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24396 if (glyph < it->glyph_row->glyphs[area + 1])
24397 {
24398 /* If the glyph row is reversed, we need to prepend the glyph
24399 rather than append it. */
24400 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24401 {
24402 struct glyph *g;
24403
24404 /* Make room for the additional glyph. */
24405 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24406 g[1] = *g;
24407 glyph = it->glyph_row->glyphs[area];
24408 }
24409 glyph->charpos = CHARPOS (it->position);
24410 glyph->object = it->object;
24411 glyph->pixel_width = it->pixel_width;
24412 glyph->ascent = it->ascent;
24413 glyph->descent = it->descent;
24414 glyph->voffset = it->voffset;
24415 glyph->type = GLYPHLESS_GLYPH;
24416 glyph->u.glyphless.method = it->glyphless_method;
24417 glyph->u.glyphless.for_no_font = for_no_font;
24418 glyph->u.glyphless.len = len;
24419 glyph->u.glyphless.ch = it->c;
24420 glyph->slice.glyphless.upper_xoff = upper_xoff;
24421 glyph->slice.glyphless.upper_yoff = upper_yoff;
24422 glyph->slice.glyphless.lower_xoff = lower_xoff;
24423 glyph->slice.glyphless.lower_yoff = lower_yoff;
24424 glyph->avoid_cursor_p = it->avoid_cursor_p;
24425 glyph->multibyte_p = it->multibyte_p;
24426 glyph->left_box_line_p = it->start_of_box_run_p;
24427 glyph->right_box_line_p = it->end_of_box_run_p;
24428 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24429 || it->phys_descent > it->descent);
24430 glyph->padding_p = 0;
24431 glyph->glyph_not_available_p = 0;
24432 glyph->face_id = face_id;
24433 glyph->font_type = FONT_TYPE_UNKNOWN;
24434 if (it->bidi_p)
24435 {
24436 glyph->resolved_level = it->bidi_it.resolved_level;
24437 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24438 abort ();
24439 glyph->bidi_type = it->bidi_it.type;
24440 }
24441 ++it->glyph_row->used[area];
24442 }
24443 else
24444 IT_EXPAND_MATRIX_WIDTH (it, area);
24445 }
24446
24447
24448 /* Produce a glyph for a glyphless character for iterator IT.
24449 IT->glyphless_method specifies which method to use for displaying
24450 the character. See the description of enum
24451 glyphless_display_method in dispextern.h for the detail.
24452
24453 FOR_NO_FONT is nonzero if and only if this is for a character for
24454 which no font was found. ACRONYM, if non-nil, is an acronym string
24455 for the character. */
24456
24457 static void
24458 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24459 {
24460 int face_id;
24461 struct face *face;
24462 struct font *font;
24463 int base_width, base_height, width, height;
24464 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24465 int len;
24466
24467 /* Get the metrics of the base font. We always refer to the current
24468 ASCII face. */
24469 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24470 font = face->font ? face->font : FRAME_FONT (it->f);
24471 it->ascent = FONT_BASE (font) + font->baseline_offset;
24472 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24473 base_height = it->ascent + it->descent;
24474 base_width = font->average_width;
24475
24476 /* Get a face ID for the glyph by utilizing a cache (the same way as
24477 done for `escape-glyph' in get_next_display_element). */
24478 if (it->f == last_glyphless_glyph_frame
24479 && it->face_id == last_glyphless_glyph_face_id)
24480 {
24481 face_id = last_glyphless_glyph_merged_face_id;
24482 }
24483 else
24484 {
24485 /* Merge the `glyphless-char' face into the current face. */
24486 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24487 last_glyphless_glyph_frame = it->f;
24488 last_glyphless_glyph_face_id = it->face_id;
24489 last_glyphless_glyph_merged_face_id = face_id;
24490 }
24491
24492 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24493 {
24494 it->pixel_width = THIN_SPACE_WIDTH;
24495 len = 0;
24496 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24497 }
24498 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24499 {
24500 width = CHAR_WIDTH (it->c);
24501 if (width == 0)
24502 width = 1;
24503 else if (width > 4)
24504 width = 4;
24505 it->pixel_width = base_width * width;
24506 len = 0;
24507 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24508 }
24509 else
24510 {
24511 char buf[7];
24512 const char *str;
24513 unsigned int code[6];
24514 int upper_len;
24515 int ascent, descent;
24516 struct font_metrics metrics_upper, metrics_lower;
24517
24518 face = FACE_FROM_ID (it->f, face_id);
24519 font = face->font ? face->font : FRAME_FONT (it->f);
24520 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24521
24522 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24523 {
24524 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24525 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24526 if (CONSP (acronym))
24527 acronym = XCAR (acronym);
24528 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24529 }
24530 else
24531 {
24532 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24533 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24534 str = buf;
24535 }
24536 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24537 code[len] = font->driver->encode_char (font, str[len]);
24538 upper_len = (len + 1) / 2;
24539 font->driver->text_extents (font, code, upper_len,
24540 &metrics_upper);
24541 font->driver->text_extents (font, code + upper_len, len - upper_len,
24542 &metrics_lower);
24543
24544
24545
24546 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24547 width = max (metrics_upper.width, metrics_lower.width) + 4;
24548 upper_xoff = upper_yoff = 2; /* the typical case */
24549 if (base_width >= width)
24550 {
24551 /* Align the upper to the left, the lower to the right. */
24552 it->pixel_width = base_width;
24553 lower_xoff = base_width - 2 - metrics_lower.width;
24554 }
24555 else
24556 {
24557 /* Center the shorter one. */
24558 it->pixel_width = width;
24559 if (metrics_upper.width >= metrics_lower.width)
24560 lower_xoff = (width - metrics_lower.width) / 2;
24561 else
24562 {
24563 /* FIXME: This code doesn't look right. It formerly was
24564 missing the "lower_xoff = 0;", which couldn't have
24565 been right since it left lower_xoff uninitialized. */
24566 lower_xoff = 0;
24567 upper_xoff = (width - metrics_upper.width) / 2;
24568 }
24569 }
24570
24571 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24572 top, bottom, and between upper and lower strings. */
24573 height = (metrics_upper.ascent + metrics_upper.descent
24574 + metrics_lower.ascent + metrics_lower.descent) + 5;
24575 /* Center vertically.
24576 H:base_height, D:base_descent
24577 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24578
24579 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24580 descent = D - H/2 + h/2;
24581 lower_yoff = descent - 2 - ld;
24582 upper_yoff = lower_yoff - la - 1 - ud; */
24583 ascent = - (it->descent - (base_height + height + 1) / 2);
24584 descent = it->descent - (base_height - height) / 2;
24585 lower_yoff = descent - 2 - metrics_lower.descent;
24586 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24587 - metrics_upper.descent);
24588 /* Don't make the height shorter than the base height. */
24589 if (height > base_height)
24590 {
24591 it->ascent = ascent;
24592 it->descent = descent;
24593 }
24594 }
24595
24596 it->phys_ascent = it->ascent;
24597 it->phys_descent = it->descent;
24598 if (it->glyph_row)
24599 append_glyphless_glyph (it, face_id, for_no_font, len,
24600 upper_xoff, upper_yoff,
24601 lower_xoff, lower_yoff);
24602 it->nglyphs = 1;
24603 take_vertical_position_into_account (it);
24604 }
24605
24606
24607 /* RIF:
24608 Produce glyphs/get display metrics for the display element IT is
24609 loaded with. See the description of struct it in dispextern.h
24610 for an overview of struct it. */
24611
24612 void
24613 x_produce_glyphs (struct it *it)
24614 {
24615 int extra_line_spacing = it->extra_line_spacing;
24616
24617 it->glyph_not_available_p = 0;
24618
24619 if (it->what == IT_CHARACTER)
24620 {
24621 XChar2b char2b;
24622 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24623 struct font *font = face->font;
24624 struct font_metrics *pcm = NULL;
24625 int boff; /* baseline offset */
24626
24627 if (font == NULL)
24628 {
24629 /* When no suitable font is found, display this character by
24630 the method specified in the first extra slot of
24631 Vglyphless_char_display. */
24632 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24633
24634 eassert (it->what == IT_GLYPHLESS);
24635 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24636 goto done;
24637 }
24638
24639 boff = font->baseline_offset;
24640 if (font->vertical_centering)
24641 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24642
24643 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24644 {
24645 int stretched_p;
24646
24647 it->nglyphs = 1;
24648
24649 if (it->override_ascent >= 0)
24650 {
24651 it->ascent = it->override_ascent;
24652 it->descent = it->override_descent;
24653 boff = it->override_boff;
24654 }
24655 else
24656 {
24657 it->ascent = FONT_BASE (font) + boff;
24658 it->descent = FONT_DESCENT (font) - boff;
24659 }
24660
24661 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24662 {
24663 pcm = get_per_char_metric (font, &char2b);
24664 if (pcm->width == 0
24665 && pcm->rbearing == 0 && pcm->lbearing == 0)
24666 pcm = NULL;
24667 }
24668
24669 if (pcm)
24670 {
24671 it->phys_ascent = pcm->ascent + boff;
24672 it->phys_descent = pcm->descent - boff;
24673 it->pixel_width = pcm->width;
24674 }
24675 else
24676 {
24677 it->glyph_not_available_p = 1;
24678 it->phys_ascent = it->ascent;
24679 it->phys_descent = it->descent;
24680 it->pixel_width = font->space_width;
24681 }
24682
24683 if (it->constrain_row_ascent_descent_p)
24684 {
24685 if (it->descent > it->max_descent)
24686 {
24687 it->ascent += it->descent - it->max_descent;
24688 it->descent = it->max_descent;
24689 }
24690 if (it->ascent > it->max_ascent)
24691 {
24692 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24693 it->ascent = it->max_ascent;
24694 }
24695 it->phys_ascent = min (it->phys_ascent, it->ascent);
24696 it->phys_descent = min (it->phys_descent, it->descent);
24697 extra_line_spacing = 0;
24698 }
24699
24700 /* If this is a space inside a region of text with
24701 `space-width' property, change its width. */
24702 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24703 if (stretched_p)
24704 it->pixel_width *= XFLOATINT (it->space_width);
24705
24706 /* If face has a box, add the box thickness to the character
24707 height. If character has a box line to the left and/or
24708 right, add the box line width to the character's width. */
24709 if (face->box != FACE_NO_BOX)
24710 {
24711 int thick = face->box_line_width;
24712
24713 if (thick > 0)
24714 {
24715 it->ascent += thick;
24716 it->descent += thick;
24717 }
24718 else
24719 thick = -thick;
24720
24721 if (it->start_of_box_run_p)
24722 it->pixel_width += thick;
24723 if (it->end_of_box_run_p)
24724 it->pixel_width += thick;
24725 }
24726
24727 /* If face has an overline, add the height of the overline
24728 (1 pixel) and a 1 pixel margin to the character height. */
24729 if (face->overline_p)
24730 it->ascent += overline_margin;
24731
24732 if (it->constrain_row_ascent_descent_p)
24733 {
24734 if (it->ascent > it->max_ascent)
24735 it->ascent = it->max_ascent;
24736 if (it->descent > it->max_descent)
24737 it->descent = it->max_descent;
24738 }
24739
24740 take_vertical_position_into_account (it);
24741
24742 /* If we have to actually produce glyphs, do it. */
24743 if (it->glyph_row)
24744 {
24745 if (stretched_p)
24746 {
24747 /* Translate a space with a `space-width' property
24748 into a stretch glyph. */
24749 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24750 / FONT_HEIGHT (font));
24751 append_stretch_glyph (it, it->object, it->pixel_width,
24752 it->ascent + it->descent, ascent);
24753 }
24754 else
24755 append_glyph (it);
24756
24757 /* If characters with lbearing or rbearing are displayed
24758 in this line, record that fact in a flag of the
24759 glyph row. This is used to optimize X output code. */
24760 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24761 it->glyph_row->contains_overlapping_glyphs_p = 1;
24762 }
24763 if (! stretched_p && it->pixel_width == 0)
24764 /* We assure that all visible glyphs have at least 1-pixel
24765 width. */
24766 it->pixel_width = 1;
24767 }
24768 else if (it->char_to_display == '\n')
24769 {
24770 /* A newline has no width, but we need the height of the
24771 line. But if previous part of the line sets a height,
24772 don't increase that height */
24773
24774 Lisp_Object height;
24775 Lisp_Object total_height = Qnil;
24776
24777 it->override_ascent = -1;
24778 it->pixel_width = 0;
24779 it->nglyphs = 0;
24780
24781 height = get_it_property (it, Qline_height);
24782 /* Split (line-height total-height) list */
24783 if (CONSP (height)
24784 && CONSP (XCDR (height))
24785 && NILP (XCDR (XCDR (height))))
24786 {
24787 total_height = XCAR (XCDR (height));
24788 height = XCAR (height);
24789 }
24790 height = calc_line_height_property (it, height, font, boff, 1);
24791
24792 if (it->override_ascent >= 0)
24793 {
24794 it->ascent = it->override_ascent;
24795 it->descent = it->override_descent;
24796 boff = it->override_boff;
24797 }
24798 else
24799 {
24800 it->ascent = FONT_BASE (font) + boff;
24801 it->descent = FONT_DESCENT (font) - boff;
24802 }
24803
24804 if (EQ (height, Qt))
24805 {
24806 if (it->descent > it->max_descent)
24807 {
24808 it->ascent += it->descent - it->max_descent;
24809 it->descent = it->max_descent;
24810 }
24811 if (it->ascent > it->max_ascent)
24812 {
24813 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24814 it->ascent = it->max_ascent;
24815 }
24816 it->phys_ascent = min (it->phys_ascent, it->ascent);
24817 it->phys_descent = min (it->phys_descent, it->descent);
24818 it->constrain_row_ascent_descent_p = 1;
24819 extra_line_spacing = 0;
24820 }
24821 else
24822 {
24823 Lisp_Object spacing;
24824
24825 it->phys_ascent = it->ascent;
24826 it->phys_descent = it->descent;
24827
24828 if ((it->max_ascent > 0 || it->max_descent > 0)
24829 && face->box != FACE_NO_BOX
24830 && face->box_line_width > 0)
24831 {
24832 it->ascent += face->box_line_width;
24833 it->descent += face->box_line_width;
24834 }
24835 if (!NILP (height)
24836 && XINT (height) > it->ascent + it->descent)
24837 it->ascent = XINT (height) - it->descent;
24838
24839 if (!NILP (total_height))
24840 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24841 else
24842 {
24843 spacing = get_it_property (it, Qline_spacing);
24844 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24845 }
24846 if (INTEGERP (spacing))
24847 {
24848 extra_line_spacing = XINT (spacing);
24849 if (!NILP (total_height))
24850 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24851 }
24852 }
24853 }
24854 else /* i.e. (it->char_to_display == '\t') */
24855 {
24856 if (font->space_width > 0)
24857 {
24858 int tab_width = it->tab_width * font->space_width;
24859 int x = it->current_x + it->continuation_lines_width;
24860 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24861
24862 /* If the distance from the current position to the next tab
24863 stop is less than a space character width, use the
24864 tab stop after that. */
24865 if (next_tab_x - x < font->space_width)
24866 next_tab_x += tab_width;
24867
24868 it->pixel_width = next_tab_x - x;
24869 it->nglyphs = 1;
24870 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24871 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24872
24873 if (it->glyph_row)
24874 {
24875 append_stretch_glyph (it, it->object, it->pixel_width,
24876 it->ascent + it->descent, it->ascent);
24877 }
24878 }
24879 else
24880 {
24881 it->pixel_width = 0;
24882 it->nglyphs = 1;
24883 }
24884 }
24885 }
24886 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24887 {
24888 /* A static composition.
24889
24890 Note: A composition is represented as one glyph in the
24891 glyph matrix. There are no padding glyphs.
24892
24893 Important note: pixel_width, ascent, and descent are the
24894 values of what is drawn by draw_glyphs (i.e. the values of
24895 the overall glyphs composed). */
24896 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24897 int boff; /* baseline offset */
24898 struct composition *cmp = composition_table[it->cmp_it.id];
24899 int glyph_len = cmp->glyph_len;
24900 struct font *font = face->font;
24901
24902 it->nglyphs = 1;
24903
24904 /* If we have not yet calculated pixel size data of glyphs of
24905 the composition for the current face font, calculate them
24906 now. Theoretically, we have to check all fonts for the
24907 glyphs, but that requires much time and memory space. So,
24908 here we check only the font of the first glyph. This may
24909 lead to incorrect display, but it's very rare, and C-l
24910 (recenter-top-bottom) can correct the display anyway. */
24911 if (! cmp->font || cmp->font != font)
24912 {
24913 /* Ascent and descent of the font of the first character
24914 of this composition (adjusted by baseline offset).
24915 Ascent and descent of overall glyphs should not be less
24916 than these, respectively. */
24917 int font_ascent, font_descent, font_height;
24918 /* Bounding box of the overall glyphs. */
24919 int leftmost, rightmost, lowest, highest;
24920 int lbearing, rbearing;
24921 int i, width, ascent, descent;
24922 int left_padded = 0, right_padded = 0;
24923 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24924 XChar2b char2b;
24925 struct font_metrics *pcm;
24926 int font_not_found_p;
24927 ptrdiff_t pos;
24928
24929 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24930 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24931 break;
24932 if (glyph_len < cmp->glyph_len)
24933 right_padded = 1;
24934 for (i = 0; i < glyph_len; i++)
24935 {
24936 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24937 break;
24938 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24939 }
24940 if (i > 0)
24941 left_padded = 1;
24942
24943 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24944 : IT_CHARPOS (*it));
24945 /* If no suitable font is found, use the default font. */
24946 font_not_found_p = font == NULL;
24947 if (font_not_found_p)
24948 {
24949 face = face->ascii_face;
24950 font = face->font;
24951 }
24952 boff = font->baseline_offset;
24953 if (font->vertical_centering)
24954 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24955 font_ascent = FONT_BASE (font) + boff;
24956 font_descent = FONT_DESCENT (font) - boff;
24957 font_height = FONT_HEIGHT (font);
24958
24959 cmp->font = (void *) font;
24960
24961 pcm = NULL;
24962 if (! font_not_found_p)
24963 {
24964 get_char_face_and_encoding (it->f, c, it->face_id,
24965 &char2b, 0);
24966 pcm = get_per_char_metric (font, &char2b);
24967 }
24968
24969 /* Initialize the bounding box. */
24970 if (pcm)
24971 {
24972 width = cmp->glyph_len > 0 ? pcm->width : 0;
24973 ascent = pcm->ascent;
24974 descent = pcm->descent;
24975 lbearing = pcm->lbearing;
24976 rbearing = pcm->rbearing;
24977 }
24978 else
24979 {
24980 width = cmp->glyph_len > 0 ? font->space_width : 0;
24981 ascent = FONT_BASE (font);
24982 descent = FONT_DESCENT (font);
24983 lbearing = 0;
24984 rbearing = width;
24985 }
24986
24987 rightmost = width;
24988 leftmost = 0;
24989 lowest = - descent + boff;
24990 highest = ascent + boff;
24991
24992 if (! font_not_found_p
24993 && font->default_ascent
24994 && CHAR_TABLE_P (Vuse_default_ascent)
24995 && !NILP (Faref (Vuse_default_ascent,
24996 make_number (it->char_to_display))))
24997 highest = font->default_ascent + boff;
24998
24999 /* Draw the first glyph at the normal position. It may be
25000 shifted to right later if some other glyphs are drawn
25001 at the left. */
25002 cmp->offsets[i * 2] = 0;
25003 cmp->offsets[i * 2 + 1] = boff;
25004 cmp->lbearing = lbearing;
25005 cmp->rbearing = rbearing;
25006
25007 /* Set cmp->offsets for the remaining glyphs. */
25008 for (i++; i < glyph_len; i++)
25009 {
25010 int left, right, btm, top;
25011 int ch = COMPOSITION_GLYPH (cmp, i);
25012 int face_id;
25013 struct face *this_face;
25014
25015 if (ch == '\t')
25016 ch = ' ';
25017 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25018 this_face = FACE_FROM_ID (it->f, face_id);
25019 font = this_face->font;
25020
25021 if (font == NULL)
25022 pcm = NULL;
25023 else
25024 {
25025 get_char_face_and_encoding (it->f, ch, face_id,
25026 &char2b, 0);
25027 pcm = get_per_char_metric (font, &char2b);
25028 }
25029 if (! pcm)
25030 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25031 else
25032 {
25033 width = pcm->width;
25034 ascent = pcm->ascent;
25035 descent = pcm->descent;
25036 lbearing = pcm->lbearing;
25037 rbearing = pcm->rbearing;
25038 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25039 {
25040 /* Relative composition with or without
25041 alternate chars. */
25042 left = (leftmost + rightmost - width) / 2;
25043 btm = - descent + boff;
25044 if (font->relative_compose
25045 && (! CHAR_TABLE_P (Vignore_relative_composition)
25046 || NILP (Faref (Vignore_relative_composition,
25047 make_number (ch)))))
25048 {
25049
25050 if (- descent >= font->relative_compose)
25051 /* One extra pixel between two glyphs. */
25052 btm = highest + 1;
25053 else if (ascent <= 0)
25054 /* One extra pixel between two glyphs. */
25055 btm = lowest - 1 - ascent - descent;
25056 }
25057 }
25058 else
25059 {
25060 /* A composition rule is specified by an integer
25061 value that encodes global and new reference
25062 points (GREF and NREF). GREF and NREF are
25063 specified by numbers as below:
25064
25065 0---1---2 -- ascent
25066 | |
25067 | |
25068 | |
25069 9--10--11 -- center
25070 | |
25071 ---3---4---5--- baseline
25072 | |
25073 6---7---8 -- descent
25074 */
25075 int rule = COMPOSITION_RULE (cmp, i);
25076 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25077
25078 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25079 grefx = gref % 3, nrefx = nref % 3;
25080 grefy = gref / 3, nrefy = nref / 3;
25081 if (xoff)
25082 xoff = font_height * (xoff - 128) / 256;
25083 if (yoff)
25084 yoff = font_height * (yoff - 128) / 256;
25085
25086 left = (leftmost
25087 + grefx * (rightmost - leftmost) / 2
25088 - nrefx * width / 2
25089 + xoff);
25090
25091 btm = ((grefy == 0 ? highest
25092 : grefy == 1 ? 0
25093 : grefy == 2 ? lowest
25094 : (highest + lowest) / 2)
25095 - (nrefy == 0 ? ascent + descent
25096 : nrefy == 1 ? descent - boff
25097 : nrefy == 2 ? 0
25098 : (ascent + descent) / 2)
25099 + yoff);
25100 }
25101
25102 cmp->offsets[i * 2] = left;
25103 cmp->offsets[i * 2 + 1] = btm + descent;
25104
25105 /* Update the bounding box of the overall glyphs. */
25106 if (width > 0)
25107 {
25108 right = left + width;
25109 if (left < leftmost)
25110 leftmost = left;
25111 if (right > rightmost)
25112 rightmost = right;
25113 }
25114 top = btm + descent + ascent;
25115 if (top > highest)
25116 highest = top;
25117 if (btm < lowest)
25118 lowest = btm;
25119
25120 if (cmp->lbearing > left + lbearing)
25121 cmp->lbearing = left + lbearing;
25122 if (cmp->rbearing < left + rbearing)
25123 cmp->rbearing = left + rbearing;
25124 }
25125 }
25126
25127 /* If there are glyphs whose x-offsets are negative,
25128 shift all glyphs to the right and make all x-offsets
25129 non-negative. */
25130 if (leftmost < 0)
25131 {
25132 for (i = 0; i < cmp->glyph_len; i++)
25133 cmp->offsets[i * 2] -= leftmost;
25134 rightmost -= leftmost;
25135 cmp->lbearing -= leftmost;
25136 cmp->rbearing -= leftmost;
25137 }
25138
25139 if (left_padded && cmp->lbearing < 0)
25140 {
25141 for (i = 0; i < cmp->glyph_len; i++)
25142 cmp->offsets[i * 2] -= cmp->lbearing;
25143 rightmost -= cmp->lbearing;
25144 cmp->rbearing -= cmp->lbearing;
25145 cmp->lbearing = 0;
25146 }
25147 if (right_padded && rightmost < cmp->rbearing)
25148 {
25149 rightmost = cmp->rbearing;
25150 }
25151
25152 cmp->pixel_width = rightmost;
25153 cmp->ascent = highest;
25154 cmp->descent = - lowest;
25155 if (cmp->ascent < font_ascent)
25156 cmp->ascent = font_ascent;
25157 if (cmp->descent < font_descent)
25158 cmp->descent = font_descent;
25159 }
25160
25161 if (it->glyph_row
25162 && (cmp->lbearing < 0
25163 || cmp->rbearing > cmp->pixel_width))
25164 it->glyph_row->contains_overlapping_glyphs_p = 1;
25165
25166 it->pixel_width = cmp->pixel_width;
25167 it->ascent = it->phys_ascent = cmp->ascent;
25168 it->descent = it->phys_descent = cmp->descent;
25169 if (face->box != FACE_NO_BOX)
25170 {
25171 int thick = face->box_line_width;
25172
25173 if (thick > 0)
25174 {
25175 it->ascent += thick;
25176 it->descent += thick;
25177 }
25178 else
25179 thick = - thick;
25180
25181 if (it->start_of_box_run_p)
25182 it->pixel_width += thick;
25183 if (it->end_of_box_run_p)
25184 it->pixel_width += thick;
25185 }
25186
25187 /* If face has an overline, add the height of the overline
25188 (1 pixel) and a 1 pixel margin to the character height. */
25189 if (face->overline_p)
25190 it->ascent += overline_margin;
25191
25192 take_vertical_position_into_account (it);
25193 if (it->ascent < 0)
25194 it->ascent = 0;
25195 if (it->descent < 0)
25196 it->descent = 0;
25197
25198 if (it->glyph_row && cmp->glyph_len > 0)
25199 append_composite_glyph (it);
25200 }
25201 else if (it->what == IT_COMPOSITION)
25202 {
25203 /* A dynamic (automatic) composition. */
25204 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25205 Lisp_Object gstring;
25206 struct font_metrics metrics;
25207
25208 it->nglyphs = 1;
25209
25210 gstring = composition_gstring_from_id (it->cmp_it.id);
25211 it->pixel_width
25212 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25213 &metrics);
25214 if (it->glyph_row
25215 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25216 it->glyph_row->contains_overlapping_glyphs_p = 1;
25217 it->ascent = it->phys_ascent = metrics.ascent;
25218 it->descent = it->phys_descent = metrics.descent;
25219 if (face->box != FACE_NO_BOX)
25220 {
25221 int thick = face->box_line_width;
25222
25223 if (thick > 0)
25224 {
25225 it->ascent += thick;
25226 it->descent += thick;
25227 }
25228 else
25229 thick = - thick;
25230
25231 if (it->start_of_box_run_p)
25232 it->pixel_width += thick;
25233 if (it->end_of_box_run_p)
25234 it->pixel_width += thick;
25235 }
25236 /* If face has an overline, add the height of the overline
25237 (1 pixel) and a 1 pixel margin to the character height. */
25238 if (face->overline_p)
25239 it->ascent += overline_margin;
25240 take_vertical_position_into_account (it);
25241 if (it->ascent < 0)
25242 it->ascent = 0;
25243 if (it->descent < 0)
25244 it->descent = 0;
25245
25246 if (it->glyph_row)
25247 append_composite_glyph (it);
25248 }
25249 else if (it->what == IT_GLYPHLESS)
25250 produce_glyphless_glyph (it, 0, Qnil);
25251 else if (it->what == IT_IMAGE)
25252 produce_image_glyph (it);
25253 else if (it->what == IT_STRETCH)
25254 produce_stretch_glyph (it);
25255
25256 done:
25257 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25258 because this isn't true for images with `:ascent 100'. */
25259 eassert (it->ascent >= 0 && it->descent >= 0);
25260 if (it->area == TEXT_AREA)
25261 it->current_x += it->pixel_width;
25262
25263 if (extra_line_spacing > 0)
25264 {
25265 it->descent += extra_line_spacing;
25266 if (extra_line_spacing > it->max_extra_line_spacing)
25267 it->max_extra_line_spacing = extra_line_spacing;
25268 }
25269
25270 it->max_ascent = max (it->max_ascent, it->ascent);
25271 it->max_descent = max (it->max_descent, it->descent);
25272 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25273 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25274 }
25275
25276 /* EXPORT for RIF:
25277 Output LEN glyphs starting at START at the nominal cursor position.
25278 Advance the nominal cursor over the text. The global variable
25279 updated_window contains the window being updated, updated_row is
25280 the glyph row being updated, and updated_area is the area of that
25281 row being updated. */
25282
25283 void
25284 x_write_glyphs (struct glyph *start, int len)
25285 {
25286 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25287
25288 eassert (updated_window && updated_row);
25289 /* When the window is hscrolled, cursor hpos can legitimately be out
25290 of bounds, but we draw the cursor at the corresponding window
25291 margin in that case. */
25292 if (!updated_row->reversed_p && chpos < 0)
25293 chpos = 0;
25294 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25295 chpos = updated_row->used[TEXT_AREA] - 1;
25296
25297 BLOCK_INPUT;
25298
25299 /* Write glyphs. */
25300
25301 hpos = start - updated_row->glyphs[updated_area];
25302 x = draw_glyphs (updated_window, output_cursor.x,
25303 updated_row, updated_area,
25304 hpos, hpos + len,
25305 DRAW_NORMAL_TEXT, 0);
25306
25307 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25308 if (updated_area == TEXT_AREA
25309 && updated_window->phys_cursor_on_p
25310 && updated_window->phys_cursor.vpos == output_cursor.vpos
25311 && chpos >= hpos
25312 && chpos < hpos + len)
25313 updated_window->phys_cursor_on_p = 0;
25314
25315 UNBLOCK_INPUT;
25316
25317 /* Advance the output cursor. */
25318 output_cursor.hpos += len;
25319 output_cursor.x = x;
25320 }
25321
25322
25323 /* EXPORT for RIF:
25324 Insert LEN glyphs from START at the nominal cursor position. */
25325
25326 void
25327 x_insert_glyphs (struct glyph *start, int len)
25328 {
25329 struct frame *f;
25330 struct window *w;
25331 int line_height, shift_by_width, shifted_region_width;
25332 struct glyph_row *row;
25333 struct glyph *glyph;
25334 int frame_x, frame_y;
25335 ptrdiff_t hpos;
25336
25337 eassert (updated_window && updated_row);
25338 BLOCK_INPUT;
25339 w = updated_window;
25340 f = XFRAME (WINDOW_FRAME (w));
25341
25342 /* Get the height of the line we are in. */
25343 row = updated_row;
25344 line_height = row->height;
25345
25346 /* Get the width of the glyphs to insert. */
25347 shift_by_width = 0;
25348 for (glyph = start; glyph < start + len; ++glyph)
25349 shift_by_width += glyph->pixel_width;
25350
25351 /* Get the width of the region to shift right. */
25352 shifted_region_width = (window_box_width (w, updated_area)
25353 - output_cursor.x
25354 - shift_by_width);
25355
25356 /* Shift right. */
25357 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25358 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25359
25360 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25361 line_height, shift_by_width);
25362
25363 /* Write the glyphs. */
25364 hpos = start - row->glyphs[updated_area];
25365 draw_glyphs (w, output_cursor.x, row, updated_area,
25366 hpos, hpos + len,
25367 DRAW_NORMAL_TEXT, 0);
25368
25369 /* Advance the output cursor. */
25370 output_cursor.hpos += len;
25371 output_cursor.x += shift_by_width;
25372 UNBLOCK_INPUT;
25373 }
25374
25375
25376 /* EXPORT for RIF:
25377 Erase the current text line from the nominal cursor position
25378 (inclusive) to pixel column TO_X (exclusive). The idea is that
25379 everything from TO_X onward is already erased.
25380
25381 TO_X is a pixel position relative to updated_area of
25382 updated_window. TO_X == -1 means clear to the end of this area. */
25383
25384 void
25385 x_clear_end_of_line (int to_x)
25386 {
25387 struct frame *f;
25388 struct window *w = updated_window;
25389 int max_x, min_y, max_y;
25390 int from_x, from_y, to_y;
25391
25392 eassert (updated_window && updated_row);
25393 f = XFRAME (WVAR (w, frame));
25394
25395 if (updated_row->full_width_p)
25396 max_x = WINDOW_TOTAL_WIDTH (w);
25397 else
25398 max_x = window_box_width (w, updated_area);
25399 max_y = window_text_bottom_y (w);
25400
25401 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25402 of window. For TO_X > 0, truncate to end of drawing area. */
25403 if (to_x == 0)
25404 return;
25405 else if (to_x < 0)
25406 to_x = max_x;
25407 else
25408 to_x = min (to_x, max_x);
25409
25410 to_y = min (max_y, output_cursor.y + updated_row->height);
25411
25412 /* Notice if the cursor will be cleared by this operation. */
25413 if (!updated_row->full_width_p)
25414 notice_overwritten_cursor (w, updated_area,
25415 output_cursor.x, -1,
25416 updated_row->y,
25417 MATRIX_ROW_BOTTOM_Y (updated_row));
25418
25419 from_x = output_cursor.x;
25420
25421 /* Translate to frame coordinates. */
25422 if (updated_row->full_width_p)
25423 {
25424 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25425 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25426 }
25427 else
25428 {
25429 int area_left = window_box_left (w, updated_area);
25430 from_x += area_left;
25431 to_x += area_left;
25432 }
25433
25434 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25435 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25436 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25437
25438 /* Prevent inadvertently clearing to end of the X window. */
25439 if (to_x > from_x && to_y > from_y)
25440 {
25441 BLOCK_INPUT;
25442 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25443 to_x - from_x, to_y - from_y);
25444 UNBLOCK_INPUT;
25445 }
25446 }
25447
25448 #endif /* HAVE_WINDOW_SYSTEM */
25449
25450
25451 \f
25452 /***********************************************************************
25453 Cursor types
25454 ***********************************************************************/
25455
25456 /* Value is the internal representation of the specified cursor type
25457 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25458 of the bar cursor. */
25459
25460 static enum text_cursor_kinds
25461 get_specified_cursor_type (Lisp_Object arg, int *width)
25462 {
25463 enum text_cursor_kinds type;
25464
25465 if (NILP (arg))
25466 return NO_CURSOR;
25467
25468 if (EQ (arg, Qbox))
25469 return FILLED_BOX_CURSOR;
25470
25471 if (EQ (arg, Qhollow))
25472 return HOLLOW_BOX_CURSOR;
25473
25474 if (EQ (arg, Qbar))
25475 {
25476 *width = 2;
25477 return BAR_CURSOR;
25478 }
25479
25480 if (CONSP (arg)
25481 && EQ (XCAR (arg), Qbar)
25482 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25483 {
25484 *width = XINT (XCDR (arg));
25485 return BAR_CURSOR;
25486 }
25487
25488 if (EQ (arg, Qhbar))
25489 {
25490 *width = 2;
25491 return HBAR_CURSOR;
25492 }
25493
25494 if (CONSP (arg)
25495 && EQ (XCAR (arg), Qhbar)
25496 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25497 {
25498 *width = XINT (XCDR (arg));
25499 return HBAR_CURSOR;
25500 }
25501
25502 /* Treat anything unknown as "hollow box cursor".
25503 It was bad to signal an error; people have trouble fixing
25504 .Xdefaults with Emacs, when it has something bad in it. */
25505 type = HOLLOW_BOX_CURSOR;
25506
25507 return type;
25508 }
25509
25510 /* Set the default cursor types for specified frame. */
25511 void
25512 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25513 {
25514 int width = 1;
25515 Lisp_Object tem;
25516
25517 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25518 FRAME_CURSOR_WIDTH (f) = width;
25519
25520 /* By default, set up the blink-off state depending on the on-state. */
25521
25522 tem = Fassoc (arg, Vblink_cursor_alist);
25523 if (!NILP (tem))
25524 {
25525 FRAME_BLINK_OFF_CURSOR (f)
25526 = get_specified_cursor_type (XCDR (tem), &width);
25527 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25528 }
25529 else
25530 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25531 }
25532
25533
25534 #ifdef HAVE_WINDOW_SYSTEM
25535
25536 /* Return the cursor we want to be displayed in window W. Return
25537 width of bar/hbar cursor through WIDTH arg. Return with
25538 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25539 (i.e. if the `system caret' should track this cursor).
25540
25541 In a mini-buffer window, we want the cursor only to appear if we
25542 are reading input from this window. For the selected window, we
25543 want the cursor type given by the frame parameter or buffer local
25544 setting of cursor-type. If explicitly marked off, draw no cursor.
25545 In all other cases, we want a hollow box cursor. */
25546
25547 static enum text_cursor_kinds
25548 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25549 int *active_cursor)
25550 {
25551 struct frame *f = XFRAME (WVAR (w, frame));
25552 struct buffer *b = XBUFFER (WVAR (w, buffer));
25553 int cursor_type = DEFAULT_CURSOR;
25554 Lisp_Object alt_cursor;
25555 int non_selected = 0;
25556
25557 *active_cursor = 1;
25558
25559 /* Echo area */
25560 if (cursor_in_echo_area
25561 && FRAME_HAS_MINIBUF_P (f)
25562 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25563 {
25564 if (w == XWINDOW (echo_area_window))
25565 {
25566 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25567 {
25568 *width = FRAME_CURSOR_WIDTH (f);
25569 return FRAME_DESIRED_CURSOR (f);
25570 }
25571 else
25572 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25573 }
25574
25575 *active_cursor = 0;
25576 non_selected = 1;
25577 }
25578
25579 /* Detect a nonselected window or nonselected frame. */
25580 else if (w != XWINDOW (FVAR (f, selected_window))
25581 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25582 {
25583 *active_cursor = 0;
25584
25585 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25586 return NO_CURSOR;
25587
25588 non_selected = 1;
25589 }
25590
25591 /* Never display a cursor in a window in which cursor-type is nil. */
25592 if (NILP (BVAR (b, cursor_type)))
25593 return NO_CURSOR;
25594
25595 /* Get the normal cursor type for this window. */
25596 if (EQ (BVAR (b, cursor_type), Qt))
25597 {
25598 cursor_type = FRAME_DESIRED_CURSOR (f);
25599 *width = FRAME_CURSOR_WIDTH (f);
25600 }
25601 else
25602 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25603
25604 /* Use cursor-in-non-selected-windows instead
25605 for non-selected window or frame. */
25606 if (non_selected)
25607 {
25608 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25609 if (!EQ (Qt, alt_cursor))
25610 return get_specified_cursor_type (alt_cursor, width);
25611 /* t means modify the normal cursor type. */
25612 if (cursor_type == FILLED_BOX_CURSOR)
25613 cursor_type = HOLLOW_BOX_CURSOR;
25614 else if (cursor_type == BAR_CURSOR && *width > 1)
25615 --*width;
25616 return cursor_type;
25617 }
25618
25619 /* Use normal cursor if not blinked off. */
25620 if (!w->cursor_off_p)
25621 {
25622 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25623 {
25624 if (cursor_type == FILLED_BOX_CURSOR)
25625 {
25626 /* Using a block cursor on large images can be very annoying.
25627 So use a hollow cursor for "large" images.
25628 If image is not transparent (no mask), also use hollow cursor. */
25629 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25630 if (img != NULL && IMAGEP (img->spec))
25631 {
25632 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25633 where N = size of default frame font size.
25634 This should cover most of the "tiny" icons people may use. */
25635 if (!img->mask
25636 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25637 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25638 cursor_type = HOLLOW_BOX_CURSOR;
25639 }
25640 }
25641 else if (cursor_type != NO_CURSOR)
25642 {
25643 /* Display current only supports BOX and HOLLOW cursors for images.
25644 So for now, unconditionally use a HOLLOW cursor when cursor is
25645 not a solid box cursor. */
25646 cursor_type = HOLLOW_BOX_CURSOR;
25647 }
25648 }
25649 return cursor_type;
25650 }
25651
25652 /* Cursor is blinked off, so determine how to "toggle" it. */
25653
25654 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25655 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25656 return get_specified_cursor_type (XCDR (alt_cursor), width);
25657
25658 /* Then see if frame has specified a specific blink off cursor type. */
25659 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25660 {
25661 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25662 return FRAME_BLINK_OFF_CURSOR (f);
25663 }
25664
25665 #if 0
25666 /* Some people liked having a permanently visible blinking cursor,
25667 while others had very strong opinions against it. So it was
25668 decided to remove it. KFS 2003-09-03 */
25669
25670 /* Finally perform built-in cursor blinking:
25671 filled box <-> hollow box
25672 wide [h]bar <-> narrow [h]bar
25673 narrow [h]bar <-> no cursor
25674 other type <-> no cursor */
25675
25676 if (cursor_type == FILLED_BOX_CURSOR)
25677 return HOLLOW_BOX_CURSOR;
25678
25679 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25680 {
25681 *width = 1;
25682 return cursor_type;
25683 }
25684 #endif
25685
25686 return NO_CURSOR;
25687 }
25688
25689
25690 /* Notice when the text cursor of window W has been completely
25691 overwritten by a drawing operation that outputs glyphs in AREA
25692 starting at X0 and ending at X1 in the line starting at Y0 and
25693 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25694 the rest of the line after X0 has been written. Y coordinates
25695 are window-relative. */
25696
25697 static void
25698 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25699 int x0, int x1, int y0, int y1)
25700 {
25701 int cx0, cx1, cy0, cy1;
25702 struct glyph_row *row;
25703
25704 if (!w->phys_cursor_on_p)
25705 return;
25706 if (area != TEXT_AREA)
25707 return;
25708
25709 if (w->phys_cursor.vpos < 0
25710 || w->phys_cursor.vpos >= w->current_matrix->nrows
25711 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25712 !(row->enabled_p && row->displays_text_p)))
25713 return;
25714
25715 if (row->cursor_in_fringe_p)
25716 {
25717 row->cursor_in_fringe_p = 0;
25718 draw_fringe_bitmap (w, row, row->reversed_p);
25719 w->phys_cursor_on_p = 0;
25720 return;
25721 }
25722
25723 cx0 = w->phys_cursor.x;
25724 cx1 = cx0 + w->phys_cursor_width;
25725 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25726 return;
25727
25728 /* The cursor image will be completely removed from the
25729 screen if the output area intersects the cursor area in
25730 y-direction. When we draw in [y0 y1[, and some part of
25731 the cursor is at y < y0, that part must have been drawn
25732 before. When scrolling, the cursor is erased before
25733 actually scrolling, so we don't come here. When not
25734 scrolling, the rows above the old cursor row must have
25735 changed, and in this case these rows must have written
25736 over the cursor image.
25737
25738 Likewise if part of the cursor is below y1, with the
25739 exception of the cursor being in the first blank row at
25740 the buffer and window end because update_text_area
25741 doesn't draw that row. (Except when it does, but
25742 that's handled in update_text_area.) */
25743
25744 cy0 = w->phys_cursor.y;
25745 cy1 = cy0 + w->phys_cursor_height;
25746 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25747 return;
25748
25749 w->phys_cursor_on_p = 0;
25750 }
25751
25752 #endif /* HAVE_WINDOW_SYSTEM */
25753
25754 \f
25755 /************************************************************************
25756 Mouse Face
25757 ************************************************************************/
25758
25759 #ifdef HAVE_WINDOW_SYSTEM
25760
25761 /* EXPORT for RIF:
25762 Fix the display of area AREA of overlapping row ROW in window W
25763 with respect to the overlapping part OVERLAPS. */
25764
25765 void
25766 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25767 enum glyph_row_area area, int overlaps)
25768 {
25769 int i, x;
25770
25771 BLOCK_INPUT;
25772
25773 x = 0;
25774 for (i = 0; i < row->used[area];)
25775 {
25776 if (row->glyphs[area][i].overlaps_vertically_p)
25777 {
25778 int start = i, start_x = x;
25779
25780 do
25781 {
25782 x += row->glyphs[area][i].pixel_width;
25783 ++i;
25784 }
25785 while (i < row->used[area]
25786 && row->glyphs[area][i].overlaps_vertically_p);
25787
25788 draw_glyphs (w, start_x, row, area,
25789 start, i,
25790 DRAW_NORMAL_TEXT, overlaps);
25791 }
25792 else
25793 {
25794 x += row->glyphs[area][i].pixel_width;
25795 ++i;
25796 }
25797 }
25798
25799 UNBLOCK_INPUT;
25800 }
25801
25802
25803 /* EXPORT:
25804 Draw the cursor glyph of window W in glyph row ROW. See the
25805 comment of draw_glyphs for the meaning of HL. */
25806
25807 void
25808 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25809 enum draw_glyphs_face hl)
25810 {
25811 /* If cursor hpos is out of bounds, don't draw garbage. This can
25812 happen in mini-buffer windows when switching between echo area
25813 glyphs and mini-buffer. */
25814 if ((row->reversed_p
25815 ? (w->phys_cursor.hpos >= 0)
25816 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25817 {
25818 int on_p = w->phys_cursor_on_p;
25819 int x1;
25820 int hpos = w->phys_cursor.hpos;
25821
25822 /* When the window is hscrolled, cursor hpos can legitimately be
25823 out of bounds, but we draw the cursor at the corresponding
25824 window margin in that case. */
25825 if (!row->reversed_p && hpos < 0)
25826 hpos = 0;
25827 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25828 hpos = row->used[TEXT_AREA] - 1;
25829
25830 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25831 hl, 0);
25832 w->phys_cursor_on_p = on_p;
25833
25834 if (hl == DRAW_CURSOR)
25835 w->phys_cursor_width = x1 - w->phys_cursor.x;
25836 /* When we erase the cursor, and ROW is overlapped by other
25837 rows, make sure that these overlapping parts of other rows
25838 are redrawn. */
25839 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25840 {
25841 w->phys_cursor_width = x1 - w->phys_cursor.x;
25842
25843 if (row > w->current_matrix->rows
25844 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25845 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25846 OVERLAPS_ERASED_CURSOR);
25847
25848 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25849 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25850 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25851 OVERLAPS_ERASED_CURSOR);
25852 }
25853 }
25854 }
25855
25856
25857 /* EXPORT:
25858 Erase the image of a cursor of window W from the screen. */
25859
25860 void
25861 erase_phys_cursor (struct window *w)
25862 {
25863 struct frame *f = XFRAME (WVAR (w, frame));
25864 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25865 int hpos = w->phys_cursor.hpos;
25866 int vpos = w->phys_cursor.vpos;
25867 int mouse_face_here_p = 0;
25868 struct glyph_matrix *active_glyphs = w->current_matrix;
25869 struct glyph_row *cursor_row;
25870 struct glyph *cursor_glyph;
25871 enum draw_glyphs_face hl;
25872
25873 /* No cursor displayed or row invalidated => nothing to do on the
25874 screen. */
25875 if (w->phys_cursor_type == NO_CURSOR)
25876 goto mark_cursor_off;
25877
25878 /* VPOS >= active_glyphs->nrows means that window has been resized.
25879 Don't bother to erase the cursor. */
25880 if (vpos >= active_glyphs->nrows)
25881 goto mark_cursor_off;
25882
25883 /* If row containing cursor is marked invalid, there is nothing we
25884 can do. */
25885 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25886 if (!cursor_row->enabled_p)
25887 goto mark_cursor_off;
25888
25889 /* If line spacing is > 0, old cursor may only be partially visible in
25890 window after split-window. So adjust visible height. */
25891 cursor_row->visible_height = min (cursor_row->visible_height,
25892 window_text_bottom_y (w) - cursor_row->y);
25893
25894 /* If row is completely invisible, don't attempt to delete a cursor which
25895 isn't there. This can happen if cursor is at top of a window, and
25896 we switch to a buffer with a header line in that window. */
25897 if (cursor_row->visible_height <= 0)
25898 goto mark_cursor_off;
25899
25900 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25901 if (cursor_row->cursor_in_fringe_p)
25902 {
25903 cursor_row->cursor_in_fringe_p = 0;
25904 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25905 goto mark_cursor_off;
25906 }
25907
25908 /* This can happen when the new row is shorter than the old one.
25909 In this case, either draw_glyphs or clear_end_of_line
25910 should have cleared the cursor. Note that we wouldn't be
25911 able to erase the cursor in this case because we don't have a
25912 cursor glyph at hand. */
25913 if ((cursor_row->reversed_p
25914 ? (w->phys_cursor.hpos < 0)
25915 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25916 goto mark_cursor_off;
25917
25918 /* When the window is hscrolled, cursor hpos can legitimately be out
25919 of bounds, but we draw the cursor at the corresponding window
25920 margin in that case. */
25921 if (!cursor_row->reversed_p && hpos < 0)
25922 hpos = 0;
25923 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25924 hpos = cursor_row->used[TEXT_AREA] - 1;
25925
25926 /* If the cursor is in the mouse face area, redisplay that when
25927 we clear the cursor. */
25928 if (! NILP (hlinfo->mouse_face_window)
25929 && coords_in_mouse_face_p (w, hpos, vpos)
25930 /* Don't redraw the cursor's spot in mouse face if it is at the
25931 end of a line (on a newline). The cursor appears there, but
25932 mouse highlighting does not. */
25933 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25934 mouse_face_here_p = 1;
25935
25936 /* Maybe clear the display under the cursor. */
25937 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25938 {
25939 int x, y, left_x;
25940 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25941 int width;
25942
25943 cursor_glyph = get_phys_cursor_glyph (w);
25944 if (cursor_glyph == NULL)
25945 goto mark_cursor_off;
25946
25947 width = cursor_glyph->pixel_width;
25948 left_x = window_box_left_offset (w, TEXT_AREA);
25949 x = w->phys_cursor.x;
25950 if (x < left_x)
25951 width -= left_x - x;
25952 width = min (width, window_box_width (w, TEXT_AREA) - x);
25953 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25954 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25955
25956 if (width > 0)
25957 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25958 }
25959
25960 /* Erase the cursor by redrawing the character underneath it. */
25961 if (mouse_face_here_p)
25962 hl = DRAW_MOUSE_FACE;
25963 else
25964 hl = DRAW_NORMAL_TEXT;
25965 draw_phys_cursor_glyph (w, cursor_row, hl);
25966
25967 mark_cursor_off:
25968 w->phys_cursor_on_p = 0;
25969 w->phys_cursor_type = NO_CURSOR;
25970 }
25971
25972
25973 /* EXPORT:
25974 Display or clear cursor of window W. If ON is zero, clear the
25975 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25976 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25977
25978 void
25979 display_and_set_cursor (struct window *w, int on,
25980 int hpos, int vpos, int x, int y)
25981 {
25982 struct frame *f = XFRAME (WVAR (w, frame));
25983 int new_cursor_type;
25984 int new_cursor_width;
25985 int active_cursor;
25986 struct glyph_row *glyph_row;
25987 struct glyph *glyph;
25988
25989 /* This is pointless on invisible frames, and dangerous on garbaged
25990 windows and frames; in the latter case, the frame or window may
25991 be in the midst of changing its size, and x and y may be off the
25992 window. */
25993 if (! FRAME_VISIBLE_P (f)
25994 || FRAME_GARBAGED_P (f)
25995 || vpos >= w->current_matrix->nrows
25996 || hpos >= w->current_matrix->matrix_w)
25997 return;
25998
25999 /* If cursor is off and we want it off, return quickly. */
26000 if (!on && !w->phys_cursor_on_p)
26001 return;
26002
26003 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26004 /* If cursor row is not enabled, we don't really know where to
26005 display the cursor. */
26006 if (!glyph_row->enabled_p)
26007 {
26008 w->phys_cursor_on_p = 0;
26009 return;
26010 }
26011
26012 glyph = NULL;
26013 if (!glyph_row->exact_window_width_line_p
26014 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26015 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26016
26017 eassert (interrupt_input_blocked);
26018
26019 /* Set new_cursor_type to the cursor we want to be displayed. */
26020 new_cursor_type = get_window_cursor_type (w, glyph,
26021 &new_cursor_width, &active_cursor);
26022
26023 /* If cursor is currently being shown and we don't want it to be or
26024 it is in the wrong place, or the cursor type is not what we want,
26025 erase it. */
26026 if (w->phys_cursor_on_p
26027 && (!on
26028 || w->phys_cursor.x != x
26029 || w->phys_cursor.y != y
26030 || new_cursor_type != w->phys_cursor_type
26031 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26032 && new_cursor_width != w->phys_cursor_width)))
26033 erase_phys_cursor (w);
26034
26035 /* Don't check phys_cursor_on_p here because that flag is only set
26036 to zero in some cases where we know that the cursor has been
26037 completely erased, to avoid the extra work of erasing the cursor
26038 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26039 still not be visible, or it has only been partly erased. */
26040 if (on)
26041 {
26042 w->phys_cursor_ascent = glyph_row->ascent;
26043 w->phys_cursor_height = glyph_row->height;
26044
26045 /* Set phys_cursor_.* before x_draw_.* is called because some
26046 of them may need the information. */
26047 w->phys_cursor.x = x;
26048 w->phys_cursor.y = glyph_row->y;
26049 w->phys_cursor.hpos = hpos;
26050 w->phys_cursor.vpos = vpos;
26051 }
26052
26053 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26054 new_cursor_type, new_cursor_width,
26055 on, active_cursor);
26056 }
26057
26058
26059 /* Switch the display of W's cursor on or off, according to the value
26060 of ON. */
26061
26062 static void
26063 update_window_cursor (struct window *w, int on)
26064 {
26065 /* Don't update cursor in windows whose frame is in the process
26066 of being deleted. */
26067 if (w->current_matrix)
26068 {
26069 int hpos = w->phys_cursor.hpos;
26070 int vpos = w->phys_cursor.vpos;
26071 struct glyph_row *row;
26072
26073 if (vpos >= w->current_matrix->nrows
26074 || hpos >= w->current_matrix->matrix_w)
26075 return;
26076
26077 row = MATRIX_ROW (w->current_matrix, vpos);
26078
26079 /* When the window is hscrolled, cursor hpos can legitimately be
26080 out of bounds, but we draw the cursor at the corresponding
26081 window margin in that case. */
26082 if (!row->reversed_p && hpos < 0)
26083 hpos = 0;
26084 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26085 hpos = row->used[TEXT_AREA] - 1;
26086
26087 BLOCK_INPUT;
26088 display_and_set_cursor (w, on, hpos, vpos,
26089 w->phys_cursor.x, w->phys_cursor.y);
26090 UNBLOCK_INPUT;
26091 }
26092 }
26093
26094
26095 /* Call update_window_cursor with parameter ON_P on all leaf windows
26096 in the window tree rooted at W. */
26097
26098 static void
26099 update_cursor_in_window_tree (struct window *w, int on_p)
26100 {
26101 while (w)
26102 {
26103 if (!NILP (WVAR (w, hchild)))
26104 update_cursor_in_window_tree (XWINDOW (WVAR (w, hchild)), on_p);
26105 else if (!NILP (WVAR (w, vchild)))
26106 update_cursor_in_window_tree (XWINDOW (WVAR (w, vchild)), on_p);
26107 else
26108 update_window_cursor (w, on_p);
26109
26110 w = NILP (WVAR (w, next)) ? 0 : XWINDOW (WVAR (w, next));
26111 }
26112 }
26113
26114
26115 /* EXPORT:
26116 Display the cursor on window W, or clear it, according to ON_P.
26117 Don't change the cursor's position. */
26118
26119 void
26120 x_update_cursor (struct frame *f, int on_p)
26121 {
26122 update_cursor_in_window_tree (XWINDOW (FVAR (f, root_window)), on_p);
26123 }
26124
26125
26126 /* EXPORT:
26127 Clear the cursor of window W to background color, and mark the
26128 cursor as not shown. This is used when the text where the cursor
26129 is about to be rewritten. */
26130
26131 void
26132 x_clear_cursor (struct window *w)
26133 {
26134 if (FRAME_VISIBLE_P (XFRAME (WVAR (w, frame))) && w->phys_cursor_on_p)
26135 update_window_cursor (w, 0);
26136 }
26137
26138 #endif /* HAVE_WINDOW_SYSTEM */
26139
26140 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26141 and MSDOS. */
26142 static void
26143 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26144 int start_hpos, int end_hpos,
26145 enum draw_glyphs_face draw)
26146 {
26147 #ifdef HAVE_WINDOW_SYSTEM
26148 if (FRAME_WINDOW_P (XFRAME (WVAR (w, frame))))
26149 {
26150 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26151 return;
26152 }
26153 #endif
26154 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26155 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26156 #endif
26157 }
26158
26159 /* Display the active region described by mouse_face_* according to DRAW. */
26160
26161 static void
26162 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26163 {
26164 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26165 struct frame *f = XFRAME (WINDOW_FRAME (w));
26166
26167 if (/* If window is in the process of being destroyed, don't bother
26168 to do anything. */
26169 w->current_matrix != NULL
26170 /* Don't update mouse highlight if hidden */
26171 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26172 /* Recognize when we are called to operate on rows that don't exist
26173 anymore. This can happen when a window is split. */
26174 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26175 {
26176 int phys_cursor_on_p = w->phys_cursor_on_p;
26177 struct glyph_row *row, *first, *last;
26178
26179 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26180 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26181
26182 for (row = first; row <= last && row->enabled_p; ++row)
26183 {
26184 int start_hpos, end_hpos, start_x;
26185
26186 /* For all but the first row, the highlight starts at column 0. */
26187 if (row == first)
26188 {
26189 /* R2L rows have BEG and END in reversed order, but the
26190 screen drawing geometry is always left to right. So
26191 we need to mirror the beginning and end of the
26192 highlighted area in R2L rows. */
26193 if (!row->reversed_p)
26194 {
26195 start_hpos = hlinfo->mouse_face_beg_col;
26196 start_x = hlinfo->mouse_face_beg_x;
26197 }
26198 else if (row == last)
26199 {
26200 start_hpos = hlinfo->mouse_face_end_col;
26201 start_x = hlinfo->mouse_face_end_x;
26202 }
26203 else
26204 {
26205 start_hpos = 0;
26206 start_x = 0;
26207 }
26208 }
26209 else if (row->reversed_p && row == last)
26210 {
26211 start_hpos = hlinfo->mouse_face_end_col;
26212 start_x = hlinfo->mouse_face_end_x;
26213 }
26214 else
26215 {
26216 start_hpos = 0;
26217 start_x = 0;
26218 }
26219
26220 if (row == last)
26221 {
26222 if (!row->reversed_p)
26223 end_hpos = hlinfo->mouse_face_end_col;
26224 else if (row == first)
26225 end_hpos = hlinfo->mouse_face_beg_col;
26226 else
26227 {
26228 end_hpos = row->used[TEXT_AREA];
26229 if (draw == DRAW_NORMAL_TEXT)
26230 row->fill_line_p = 1; /* Clear to end of line */
26231 }
26232 }
26233 else if (row->reversed_p && row == first)
26234 end_hpos = hlinfo->mouse_face_beg_col;
26235 else
26236 {
26237 end_hpos = row->used[TEXT_AREA];
26238 if (draw == DRAW_NORMAL_TEXT)
26239 row->fill_line_p = 1; /* Clear to end of line */
26240 }
26241
26242 if (end_hpos > start_hpos)
26243 {
26244 draw_row_with_mouse_face (w, start_x, row,
26245 start_hpos, end_hpos, draw);
26246
26247 row->mouse_face_p
26248 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26249 }
26250 }
26251
26252 #ifdef HAVE_WINDOW_SYSTEM
26253 /* When we've written over the cursor, arrange for it to
26254 be displayed again. */
26255 if (FRAME_WINDOW_P (f)
26256 && phys_cursor_on_p && !w->phys_cursor_on_p)
26257 {
26258 int hpos = w->phys_cursor.hpos;
26259
26260 /* When the window is hscrolled, cursor hpos can legitimately be
26261 out of bounds, but we draw the cursor at the corresponding
26262 window margin in that case. */
26263 if (!row->reversed_p && hpos < 0)
26264 hpos = 0;
26265 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26266 hpos = row->used[TEXT_AREA] - 1;
26267
26268 BLOCK_INPUT;
26269 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26270 w->phys_cursor.x, w->phys_cursor.y);
26271 UNBLOCK_INPUT;
26272 }
26273 #endif /* HAVE_WINDOW_SYSTEM */
26274 }
26275
26276 #ifdef HAVE_WINDOW_SYSTEM
26277 /* Change the mouse cursor. */
26278 if (FRAME_WINDOW_P (f))
26279 {
26280 if (draw == DRAW_NORMAL_TEXT
26281 && !EQ (hlinfo->mouse_face_window, FVAR (f, tool_bar_window)))
26282 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26283 else if (draw == DRAW_MOUSE_FACE)
26284 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26285 else
26286 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26287 }
26288 #endif /* HAVE_WINDOW_SYSTEM */
26289 }
26290
26291 /* EXPORT:
26292 Clear out the mouse-highlighted active region.
26293 Redraw it un-highlighted first. Value is non-zero if mouse
26294 face was actually drawn unhighlighted. */
26295
26296 int
26297 clear_mouse_face (Mouse_HLInfo *hlinfo)
26298 {
26299 int cleared = 0;
26300
26301 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26302 {
26303 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26304 cleared = 1;
26305 }
26306
26307 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26308 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26309 hlinfo->mouse_face_window = Qnil;
26310 hlinfo->mouse_face_overlay = Qnil;
26311 return cleared;
26312 }
26313
26314 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26315 within the mouse face on that window. */
26316 static int
26317 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26318 {
26319 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (WVAR (w, frame)));
26320
26321 /* Quickly resolve the easy cases. */
26322 if (!(WINDOWP (hlinfo->mouse_face_window)
26323 && XWINDOW (hlinfo->mouse_face_window) == w))
26324 return 0;
26325 if (vpos < hlinfo->mouse_face_beg_row
26326 || vpos > hlinfo->mouse_face_end_row)
26327 return 0;
26328 if (vpos > hlinfo->mouse_face_beg_row
26329 && vpos < hlinfo->mouse_face_end_row)
26330 return 1;
26331
26332 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26333 {
26334 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26335 {
26336 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26337 return 1;
26338 }
26339 else if ((vpos == hlinfo->mouse_face_beg_row
26340 && hpos >= hlinfo->mouse_face_beg_col)
26341 || (vpos == hlinfo->mouse_face_end_row
26342 && hpos < hlinfo->mouse_face_end_col))
26343 return 1;
26344 }
26345 else
26346 {
26347 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26348 {
26349 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26350 return 1;
26351 }
26352 else if ((vpos == hlinfo->mouse_face_beg_row
26353 && hpos <= hlinfo->mouse_face_beg_col)
26354 || (vpos == hlinfo->mouse_face_end_row
26355 && hpos > hlinfo->mouse_face_end_col))
26356 return 1;
26357 }
26358 return 0;
26359 }
26360
26361
26362 /* EXPORT:
26363 Non-zero if physical cursor of window W is within mouse face. */
26364
26365 int
26366 cursor_in_mouse_face_p (struct window *w)
26367 {
26368 int hpos = w->phys_cursor.hpos;
26369 int vpos = w->phys_cursor.vpos;
26370 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26371
26372 /* When the window is hscrolled, cursor hpos can legitimately be out
26373 of bounds, but we draw the cursor at the corresponding window
26374 margin in that case. */
26375 if (!row->reversed_p && hpos < 0)
26376 hpos = 0;
26377 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26378 hpos = row->used[TEXT_AREA] - 1;
26379
26380 return coords_in_mouse_face_p (w, hpos, vpos);
26381 }
26382
26383
26384 \f
26385 /* Find the glyph rows START_ROW and END_ROW of window W that display
26386 characters between buffer positions START_CHARPOS and END_CHARPOS
26387 (excluding END_CHARPOS). DISP_STRING is a display string that
26388 covers these buffer positions. This is similar to
26389 row_containing_pos, but is more accurate when bidi reordering makes
26390 buffer positions change non-linearly with glyph rows. */
26391 static void
26392 rows_from_pos_range (struct window *w,
26393 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26394 Lisp_Object disp_string,
26395 struct glyph_row **start, struct glyph_row **end)
26396 {
26397 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26398 int last_y = window_text_bottom_y (w);
26399 struct glyph_row *row;
26400
26401 *start = NULL;
26402 *end = NULL;
26403
26404 while (!first->enabled_p
26405 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26406 first++;
26407
26408 /* Find the START row. */
26409 for (row = first;
26410 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26411 row++)
26412 {
26413 /* A row can potentially be the START row if the range of the
26414 characters it displays intersects the range
26415 [START_CHARPOS..END_CHARPOS). */
26416 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26417 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26418 /* See the commentary in row_containing_pos, for the
26419 explanation of the complicated way to check whether
26420 some position is beyond the end of the characters
26421 displayed by a row. */
26422 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26423 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26424 && !row->ends_at_zv_p
26425 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26426 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26427 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26428 && !row->ends_at_zv_p
26429 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26430 {
26431 /* Found a candidate row. Now make sure at least one of the
26432 glyphs it displays has a charpos from the range
26433 [START_CHARPOS..END_CHARPOS).
26434
26435 This is not obvious because bidi reordering could make
26436 buffer positions of a row be 1,2,3,102,101,100, and if we
26437 want to highlight characters in [50..60), we don't want
26438 this row, even though [50..60) does intersect [1..103),
26439 the range of character positions given by the row's start
26440 and end positions. */
26441 struct glyph *g = row->glyphs[TEXT_AREA];
26442 struct glyph *e = g + row->used[TEXT_AREA];
26443
26444 while (g < e)
26445 {
26446 if (((BUFFERP (g->object) || INTEGERP (g->object))
26447 && start_charpos <= g->charpos && g->charpos < end_charpos)
26448 /* A glyph that comes from DISP_STRING is by
26449 definition to be highlighted. */
26450 || EQ (g->object, disp_string))
26451 *start = row;
26452 g++;
26453 }
26454 if (*start)
26455 break;
26456 }
26457 }
26458
26459 /* Find the END row. */
26460 if (!*start
26461 /* If the last row is partially visible, start looking for END
26462 from that row, instead of starting from FIRST. */
26463 && !(row->enabled_p
26464 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26465 row = first;
26466 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26467 {
26468 struct glyph_row *next = row + 1;
26469 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26470
26471 if (!next->enabled_p
26472 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26473 /* The first row >= START whose range of displayed characters
26474 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26475 is the row END + 1. */
26476 || (start_charpos < next_start
26477 && end_charpos < next_start)
26478 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26479 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26480 && !next->ends_at_zv_p
26481 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26482 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26483 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26484 && !next->ends_at_zv_p
26485 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26486 {
26487 *end = row;
26488 break;
26489 }
26490 else
26491 {
26492 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26493 but none of the characters it displays are in the range, it is
26494 also END + 1. */
26495 struct glyph *g = next->glyphs[TEXT_AREA];
26496 struct glyph *s = g;
26497 struct glyph *e = g + next->used[TEXT_AREA];
26498
26499 while (g < e)
26500 {
26501 if (((BUFFERP (g->object) || INTEGERP (g->object))
26502 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26503 /* If the buffer position of the first glyph in
26504 the row is equal to END_CHARPOS, it means
26505 the last character to be highlighted is the
26506 newline of ROW, and we must consider NEXT as
26507 END, not END+1. */
26508 || (((!next->reversed_p && g == s)
26509 || (next->reversed_p && g == e - 1))
26510 && (g->charpos == end_charpos
26511 /* Special case for when NEXT is an
26512 empty line at ZV. */
26513 || (g->charpos == -1
26514 && !row->ends_at_zv_p
26515 && next_start == end_charpos)))))
26516 /* A glyph that comes from DISP_STRING is by
26517 definition to be highlighted. */
26518 || EQ (g->object, disp_string))
26519 break;
26520 g++;
26521 }
26522 if (g == e)
26523 {
26524 *end = row;
26525 break;
26526 }
26527 /* The first row that ends at ZV must be the last to be
26528 highlighted. */
26529 else if (next->ends_at_zv_p)
26530 {
26531 *end = next;
26532 break;
26533 }
26534 }
26535 }
26536 }
26537
26538 /* This function sets the mouse_face_* elements of HLINFO, assuming
26539 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26540 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26541 for the overlay or run of text properties specifying the mouse
26542 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26543 before-string and after-string that must also be highlighted.
26544 DISP_STRING, if non-nil, is a display string that may cover some
26545 or all of the highlighted text. */
26546
26547 static void
26548 mouse_face_from_buffer_pos (Lisp_Object window,
26549 Mouse_HLInfo *hlinfo,
26550 ptrdiff_t mouse_charpos,
26551 ptrdiff_t start_charpos,
26552 ptrdiff_t end_charpos,
26553 Lisp_Object before_string,
26554 Lisp_Object after_string,
26555 Lisp_Object disp_string)
26556 {
26557 struct window *w = XWINDOW (window);
26558 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26559 struct glyph_row *r1, *r2;
26560 struct glyph *glyph, *end;
26561 ptrdiff_t ignore, pos;
26562 int x;
26563
26564 eassert (NILP (disp_string) || STRINGP (disp_string));
26565 eassert (NILP (before_string) || STRINGP (before_string));
26566 eassert (NILP (after_string) || STRINGP (after_string));
26567
26568 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26569 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26570 if (r1 == NULL)
26571 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (WVAR (w, window_end_vpos)));
26572 /* If the before-string or display-string contains newlines,
26573 rows_from_pos_range skips to its last row. Move back. */
26574 if (!NILP (before_string) || !NILP (disp_string))
26575 {
26576 struct glyph_row *prev;
26577 while ((prev = r1 - 1, prev >= first)
26578 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26579 && prev->used[TEXT_AREA] > 0)
26580 {
26581 struct glyph *beg = prev->glyphs[TEXT_AREA];
26582 glyph = beg + prev->used[TEXT_AREA];
26583 while (--glyph >= beg && INTEGERP (glyph->object));
26584 if (glyph < beg
26585 || !(EQ (glyph->object, before_string)
26586 || EQ (glyph->object, disp_string)))
26587 break;
26588 r1 = prev;
26589 }
26590 }
26591 if (r2 == NULL)
26592 {
26593 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (WVAR (w, window_end_vpos)));
26594 hlinfo->mouse_face_past_end = 1;
26595 }
26596 else if (!NILP (after_string))
26597 {
26598 /* If the after-string has newlines, advance to its last row. */
26599 struct glyph_row *next;
26600 struct glyph_row *last
26601 = MATRIX_ROW (w->current_matrix, XFASTINT (WVAR (w, window_end_vpos)));
26602
26603 for (next = r2 + 1;
26604 next <= last
26605 && next->used[TEXT_AREA] > 0
26606 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26607 ++next)
26608 r2 = next;
26609 }
26610 /* The rest of the display engine assumes that mouse_face_beg_row is
26611 either above mouse_face_end_row or identical to it. But with
26612 bidi-reordered continued lines, the row for START_CHARPOS could
26613 be below the row for END_CHARPOS. If so, swap the rows and store
26614 them in correct order. */
26615 if (r1->y > r2->y)
26616 {
26617 struct glyph_row *tem = r2;
26618
26619 r2 = r1;
26620 r1 = tem;
26621 }
26622
26623 hlinfo->mouse_face_beg_y = r1->y;
26624 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26625 hlinfo->mouse_face_end_y = r2->y;
26626 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26627
26628 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26629 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26630 could be anywhere in the row and in any order. The strategy
26631 below is to find the leftmost and the rightmost glyph that
26632 belongs to either of these 3 strings, or whose position is
26633 between START_CHARPOS and END_CHARPOS, and highlight all the
26634 glyphs between those two. This may cover more than just the text
26635 between START_CHARPOS and END_CHARPOS if the range of characters
26636 strides the bidi level boundary, e.g. if the beginning is in R2L
26637 text while the end is in L2R text or vice versa. */
26638 if (!r1->reversed_p)
26639 {
26640 /* This row is in a left to right paragraph. Scan it left to
26641 right. */
26642 glyph = r1->glyphs[TEXT_AREA];
26643 end = glyph + r1->used[TEXT_AREA];
26644 x = r1->x;
26645
26646 /* Skip truncation glyphs at the start of the glyph row. */
26647 if (r1->displays_text_p)
26648 for (; glyph < end
26649 && INTEGERP (glyph->object)
26650 && glyph->charpos < 0;
26651 ++glyph)
26652 x += glyph->pixel_width;
26653
26654 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26655 or DISP_STRING, and the first glyph from buffer whose
26656 position is between START_CHARPOS and END_CHARPOS. */
26657 for (; glyph < end
26658 && !INTEGERP (glyph->object)
26659 && !EQ (glyph->object, disp_string)
26660 && !(BUFFERP (glyph->object)
26661 && (glyph->charpos >= start_charpos
26662 && glyph->charpos < end_charpos));
26663 ++glyph)
26664 {
26665 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26666 are present at buffer positions between START_CHARPOS and
26667 END_CHARPOS, or if they come from an overlay. */
26668 if (EQ (glyph->object, before_string))
26669 {
26670 pos = string_buffer_position (before_string,
26671 start_charpos);
26672 /* If pos == 0, it means before_string came from an
26673 overlay, not from a buffer position. */
26674 if (!pos || (pos >= start_charpos && pos < end_charpos))
26675 break;
26676 }
26677 else if (EQ (glyph->object, after_string))
26678 {
26679 pos = string_buffer_position (after_string, end_charpos);
26680 if (!pos || (pos >= start_charpos && pos < end_charpos))
26681 break;
26682 }
26683 x += glyph->pixel_width;
26684 }
26685 hlinfo->mouse_face_beg_x = x;
26686 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26687 }
26688 else
26689 {
26690 /* This row is in a right to left paragraph. Scan it right to
26691 left. */
26692 struct glyph *g;
26693
26694 end = r1->glyphs[TEXT_AREA] - 1;
26695 glyph = end + r1->used[TEXT_AREA];
26696
26697 /* Skip truncation glyphs at the start of the glyph row. */
26698 if (r1->displays_text_p)
26699 for (; glyph > end
26700 && INTEGERP (glyph->object)
26701 && glyph->charpos < 0;
26702 --glyph)
26703 ;
26704
26705 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26706 or DISP_STRING, and the first glyph from buffer whose
26707 position is between START_CHARPOS and END_CHARPOS. */
26708 for (; glyph > end
26709 && !INTEGERP (glyph->object)
26710 && !EQ (glyph->object, disp_string)
26711 && !(BUFFERP (glyph->object)
26712 && (glyph->charpos >= start_charpos
26713 && glyph->charpos < end_charpos));
26714 --glyph)
26715 {
26716 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26717 are present at buffer positions between START_CHARPOS and
26718 END_CHARPOS, or if they come from an overlay. */
26719 if (EQ (glyph->object, before_string))
26720 {
26721 pos = string_buffer_position (before_string, start_charpos);
26722 /* If pos == 0, it means before_string came from an
26723 overlay, not from a buffer position. */
26724 if (!pos || (pos >= start_charpos && pos < end_charpos))
26725 break;
26726 }
26727 else if (EQ (glyph->object, after_string))
26728 {
26729 pos = string_buffer_position (after_string, end_charpos);
26730 if (!pos || (pos >= start_charpos && pos < end_charpos))
26731 break;
26732 }
26733 }
26734
26735 glyph++; /* first glyph to the right of the highlighted area */
26736 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26737 x += g->pixel_width;
26738 hlinfo->mouse_face_beg_x = x;
26739 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26740 }
26741
26742 /* If the highlight ends in a different row, compute GLYPH and END
26743 for the end row. Otherwise, reuse the values computed above for
26744 the row where the highlight begins. */
26745 if (r2 != r1)
26746 {
26747 if (!r2->reversed_p)
26748 {
26749 glyph = r2->glyphs[TEXT_AREA];
26750 end = glyph + r2->used[TEXT_AREA];
26751 x = r2->x;
26752 }
26753 else
26754 {
26755 end = r2->glyphs[TEXT_AREA] - 1;
26756 glyph = end + r2->used[TEXT_AREA];
26757 }
26758 }
26759
26760 if (!r2->reversed_p)
26761 {
26762 /* Skip truncation and continuation glyphs near the end of the
26763 row, and also blanks and stretch glyphs inserted by
26764 extend_face_to_end_of_line. */
26765 while (end > glyph
26766 && INTEGERP ((end - 1)->object))
26767 --end;
26768 /* Scan the rest of the glyph row from the end, looking for the
26769 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26770 DISP_STRING, or whose position is between START_CHARPOS
26771 and END_CHARPOS */
26772 for (--end;
26773 end > glyph
26774 && !INTEGERP (end->object)
26775 && !EQ (end->object, disp_string)
26776 && !(BUFFERP (end->object)
26777 && (end->charpos >= start_charpos
26778 && end->charpos < end_charpos));
26779 --end)
26780 {
26781 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26782 are present at buffer positions between START_CHARPOS and
26783 END_CHARPOS, or if they come from an overlay. */
26784 if (EQ (end->object, before_string))
26785 {
26786 pos = string_buffer_position (before_string, start_charpos);
26787 if (!pos || (pos >= start_charpos && pos < end_charpos))
26788 break;
26789 }
26790 else if (EQ (end->object, after_string))
26791 {
26792 pos = string_buffer_position (after_string, end_charpos);
26793 if (!pos || (pos >= start_charpos && pos < end_charpos))
26794 break;
26795 }
26796 }
26797 /* Find the X coordinate of the last glyph to be highlighted. */
26798 for (; glyph <= end; ++glyph)
26799 x += glyph->pixel_width;
26800
26801 hlinfo->mouse_face_end_x = x;
26802 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26803 }
26804 else
26805 {
26806 /* Skip truncation and continuation glyphs near the end of the
26807 row, and also blanks and stretch glyphs inserted by
26808 extend_face_to_end_of_line. */
26809 x = r2->x;
26810 end++;
26811 while (end < glyph
26812 && INTEGERP (end->object))
26813 {
26814 x += end->pixel_width;
26815 ++end;
26816 }
26817 /* Scan the rest of the glyph row from the end, looking for the
26818 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26819 DISP_STRING, or whose position is between START_CHARPOS
26820 and END_CHARPOS */
26821 for ( ;
26822 end < glyph
26823 && !INTEGERP (end->object)
26824 && !EQ (end->object, disp_string)
26825 && !(BUFFERP (end->object)
26826 && (end->charpos >= start_charpos
26827 && end->charpos < end_charpos));
26828 ++end)
26829 {
26830 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26831 are present at buffer positions between START_CHARPOS and
26832 END_CHARPOS, or if they come from an overlay. */
26833 if (EQ (end->object, before_string))
26834 {
26835 pos = string_buffer_position (before_string, start_charpos);
26836 if (!pos || (pos >= start_charpos && pos < end_charpos))
26837 break;
26838 }
26839 else if (EQ (end->object, after_string))
26840 {
26841 pos = string_buffer_position (after_string, end_charpos);
26842 if (!pos || (pos >= start_charpos && pos < end_charpos))
26843 break;
26844 }
26845 x += end->pixel_width;
26846 }
26847 /* If we exited the above loop because we arrived at the last
26848 glyph of the row, and its buffer position is still not in
26849 range, it means the last character in range is the preceding
26850 newline. Bump the end column and x values to get past the
26851 last glyph. */
26852 if (end == glyph
26853 && BUFFERP (end->object)
26854 && (end->charpos < start_charpos
26855 || end->charpos >= end_charpos))
26856 {
26857 x += end->pixel_width;
26858 ++end;
26859 }
26860 hlinfo->mouse_face_end_x = x;
26861 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26862 }
26863
26864 hlinfo->mouse_face_window = window;
26865 hlinfo->mouse_face_face_id
26866 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26867 mouse_charpos + 1,
26868 !hlinfo->mouse_face_hidden, -1);
26869 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26870 }
26871
26872 /* The following function is not used anymore (replaced with
26873 mouse_face_from_string_pos), but I leave it here for the time
26874 being, in case someone would. */
26875
26876 #if 0 /* not used */
26877
26878 /* Find the position of the glyph for position POS in OBJECT in
26879 window W's current matrix, and return in *X, *Y the pixel
26880 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26881
26882 RIGHT_P non-zero means return the position of the right edge of the
26883 glyph, RIGHT_P zero means return the left edge position.
26884
26885 If no glyph for POS exists in the matrix, return the position of
26886 the glyph with the next smaller position that is in the matrix, if
26887 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26888 exists in the matrix, return the position of the glyph with the
26889 next larger position in OBJECT.
26890
26891 Value is non-zero if a glyph was found. */
26892
26893 static int
26894 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26895 int *hpos, int *vpos, int *x, int *y, int right_p)
26896 {
26897 int yb = window_text_bottom_y (w);
26898 struct glyph_row *r;
26899 struct glyph *best_glyph = NULL;
26900 struct glyph_row *best_row = NULL;
26901 int best_x = 0;
26902
26903 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26904 r->enabled_p && r->y < yb;
26905 ++r)
26906 {
26907 struct glyph *g = r->glyphs[TEXT_AREA];
26908 struct glyph *e = g + r->used[TEXT_AREA];
26909 int gx;
26910
26911 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26912 if (EQ (g->object, object))
26913 {
26914 if (g->charpos == pos)
26915 {
26916 best_glyph = g;
26917 best_x = gx;
26918 best_row = r;
26919 goto found;
26920 }
26921 else if (best_glyph == NULL
26922 || ((eabs (g->charpos - pos)
26923 < eabs (best_glyph->charpos - pos))
26924 && (right_p
26925 ? g->charpos < pos
26926 : g->charpos > pos)))
26927 {
26928 best_glyph = g;
26929 best_x = gx;
26930 best_row = r;
26931 }
26932 }
26933 }
26934
26935 found:
26936
26937 if (best_glyph)
26938 {
26939 *x = best_x;
26940 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26941
26942 if (right_p)
26943 {
26944 *x += best_glyph->pixel_width;
26945 ++*hpos;
26946 }
26947
26948 *y = best_row->y;
26949 *vpos = best_row - w->current_matrix->rows;
26950 }
26951
26952 return best_glyph != NULL;
26953 }
26954 #endif /* not used */
26955
26956 /* Find the positions of the first and the last glyphs in window W's
26957 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26958 (assumed to be a string), and return in HLINFO's mouse_face_*
26959 members the pixel and column/row coordinates of those glyphs. */
26960
26961 static void
26962 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26963 Lisp_Object object,
26964 ptrdiff_t startpos, ptrdiff_t endpos)
26965 {
26966 int yb = window_text_bottom_y (w);
26967 struct glyph_row *r;
26968 struct glyph *g, *e;
26969 int gx;
26970 int found = 0;
26971
26972 /* Find the glyph row with at least one position in the range
26973 [STARTPOS..ENDPOS], and the first glyph in that row whose
26974 position belongs to that range. */
26975 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26976 r->enabled_p && r->y < yb;
26977 ++r)
26978 {
26979 if (!r->reversed_p)
26980 {
26981 g = r->glyphs[TEXT_AREA];
26982 e = g + r->used[TEXT_AREA];
26983 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26984 if (EQ (g->object, object)
26985 && startpos <= g->charpos && g->charpos <= endpos)
26986 {
26987 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26988 hlinfo->mouse_face_beg_y = r->y;
26989 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26990 hlinfo->mouse_face_beg_x = gx;
26991 found = 1;
26992 break;
26993 }
26994 }
26995 else
26996 {
26997 struct glyph *g1;
26998
26999 e = r->glyphs[TEXT_AREA];
27000 g = e + r->used[TEXT_AREA];
27001 for ( ; g > e; --g)
27002 if (EQ ((g-1)->object, object)
27003 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27004 {
27005 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27006 hlinfo->mouse_face_beg_y = r->y;
27007 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27008 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27009 gx += g1->pixel_width;
27010 hlinfo->mouse_face_beg_x = gx;
27011 found = 1;
27012 break;
27013 }
27014 }
27015 if (found)
27016 break;
27017 }
27018
27019 if (!found)
27020 return;
27021
27022 /* Starting with the next row, look for the first row which does NOT
27023 include any glyphs whose positions are in the range. */
27024 for (++r; r->enabled_p && r->y < yb; ++r)
27025 {
27026 g = r->glyphs[TEXT_AREA];
27027 e = g + r->used[TEXT_AREA];
27028 found = 0;
27029 for ( ; g < e; ++g)
27030 if (EQ (g->object, object)
27031 && startpos <= g->charpos && g->charpos <= endpos)
27032 {
27033 found = 1;
27034 break;
27035 }
27036 if (!found)
27037 break;
27038 }
27039
27040 /* The highlighted region ends on the previous row. */
27041 r--;
27042
27043 /* Set the end row and its vertical pixel coordinate. */
27044 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27045 hlinfo->mouse_face_end_y = r->y;
27046
27047 /* Compute and set the end column and the end column's horizontal
27048 pixel coordinate. */
27049 if (!r->reversed_p)
27050 {
27051 g = r->glyphs[TEXT_AREA];
27052 e = g + r->used[TEXT_AREA];
27053 for ( ; e > g; --e)
27054 if (EQ ((e-1)->object, object)
27055 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27056 break;
27057 hlinfo->mouse_face_end_col = e - g;
27058
27059 for (gx = r->x; g < e; ++g)
27060 gx += g->pixel_width;
27061 hlinfo->mouse_face_end_x = gx;
27062 }
27063 else
27064 {
27065 e = r->glyphs[TEXT_AREA];
27066 g = e + r->used[TEXT_AREA];
27067 for (gx = r->x ; e < g; ++e)
27068 {
27069 if (EQ (e->object, object)
27070 && startpos <= e->charpos && e->charpos <= endpos)
27071 break;
27072 gx += e->pixel_width;
27073 }
27074 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27075 hlinfo->mouse_face_end_x = gx;
27076 }
27077 }
27078
27079 #ifdef HAVE_WINDOW_SYSTEM
27080
27081 /* See if position X, Y is within a hot-spot of an image. */
27082
27083 static int
27084 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27085 {
27086 if (!CONSP (hot_spot))
27087 return 0;
27088
27089 if (EQ (XCAR (hot_spot), Qrect))
27090 {
27091 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27092 Lisp_Object rect = XCDR (hot_spot);
27093 Lisp_Object tem;
27094 if (!CONSP (rect))
27095 return 0;
27096 if (!CONSP (XCAR (rect)))
27097 return 0;
27098 if (!CONSP (XCDR (rect)))
27099 return 0;
27100 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27101 return 0;
27102 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27103 return 0;
27104 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27105 return 0;
27106 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27107 return 0;
27108 return 1;
27109 }
27110 else if (EQ (XCAR (hot_spot), Qcircle))
27111 {
27112 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27113 Lisp_Object circ = XCDR (hot_spot);
27114 Lisp_Object lr, lx0, ly0;
27115 if (CONSP (circ)
27116 && CONSP (XCAR (circ))
27117 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27118 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27119 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27120 {
27121 double r = XFLOATINT (lr);
27122 double dx = XINT (lx0) - x;
27123 double dy = XINT (ly0) - y;
27124 return (dx * dx + dy * dy <= r * r);
27125 }
27126 }
27127 else if (EQ (XCAR (hot_spot), Qpoly))
27128 {
27129 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27130 if (VECTORP (XCDR (hot_spot)))
27131 {
27132 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27133 Lisp_Object *poly = v->contents;
27134 ptrdiff_t n = v->header.size;
27135 ptrdiff_t i;
27136 int inside = 0;
27137 Lisp_Object lx, ly;
27138 int x0, y0;
27139
27140 /* Need an even number of coordinates, and at least 3 edges. */
27141 if (n < 6 || n & 1)
27142 return 0;
27143
27144 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27145 If count is odd, we are inside polygon. Pixels on edges
27146 may or may not be included depending on actual geometry of the
27147 polygon. */
27148 if ((lx = poly[n-2], !INTEGERP (lx))
27149 || (ly = poly[n-1], !INTEGERP (lx)))
27150 return 0;
27151 x0 = XINT (lx), y0 = XINT (ly);
27152 for (i = 0; i < n; i += 2)
27153 {
27154 int x1 = x0, y1 = y0;
27155 if ((lx = poly[i], !INTEGERP (lx))
27156 || (ly = poly[i+1], !INTEGERP (ly)))
27157 return 0;
27158 x0 = XINT (lx), y0 = XINT (ly);
27159
27160 /* Does this segment cross the X line? */
27161 if (x0 >= x)
27162 {
27163 if (x1 >= x)
27164 continue;
27165 }
27166 else if (x1 < x)
27167 continue;
27168 if (y > y0 && y > y1)
27169 continue;
27170 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27171 inside = !inside;
27172 }
27173 return inside;
27174 }
27175 }
27176 return 0;
27177 }
27178
27179 Lisp_Object
27180 find_hot_spot (Lisp_Object map, int x, int y)
27181 {
27182 while (CONSP (map))
27183 {
27184 if (CONSP (XCAR (map))
27185 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27186 return XCAR (map);
27187 map = XCDR (map);
27188 }
27189
27190 return Qnil;
27191 }
27192
27193 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27194 3, 3, 0,
27195 doc: /* Lookup in image map MAP coordinates X and Y.
27196 An image map is an alist where each element has the format (AREA ID PLIST).
27197 An AREA is specified as either a rectangle, a circle, or a polygon:
27198 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27199 pixel coordinates of the upper left and bottom right corners.
27200 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27201 and the radius of the circle; r may be a float or integer.
27202 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27203 vector describes one corner in the polygon.
27204 Returns the alist element for the first matching AREA in MAP. */)
27205 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27206 {
27207 if (NILP (map))
27208 return Qnil;
27209
27210 CHECK_NUMBER (x);
27211 CHECK_NUMBER (y);
27212
27213 return find_hot_spot (map,
27214 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27215 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27216 }
27217
27218
27219 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27220 static void
27221 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27222 {
27223 /* Do not change cursor shape while dragging mouse. */
27224 if (!NILP (do_mouse_tracking))
27225 return;
27226
27227 if (!NILP (pointer))
27228 {
27229 if (EQ (pointer, Qarrow))
27230 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27231 else if (EQ (pointer, Qhand))
27232 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27233 else if (EQ (pointer, Qtext))
27234 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27235 else if (EQ (pointer, intern ("hdrag")))
27236 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27237 #ifdef HAVE_X_WINDOWS
27238 else if (EQ (pointer, intern ("vdrag")))
27239 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27240 #endif
27241 else if (EQ (pointer, intern ("hourglass")))
27242 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27243 else if (EQ (pointer, Qmodeline))
27244 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27245 else
27246 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27247 }
27248
27249 if (cursor != No_Cursor)
27250 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27251 }
27252
27253 #endif /* HAVE_WINDOW_SYSTEM */
27254
27255 /* Take proper action when mouse has moved to the mode or header line
27256 or marginal area AREA of window W, x-position X and y-position Y.
27257 X is relative to the start of the text display area of W, so the
27258 width of bitmap areas and scroll bars must be subtracted to get a
27259 position relative to the start of the mode line. */
27260
27261 static void
27262 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27263 enum window_part area)
27264 {
27265 struct window *w = XWINDOW (window);
27266 struct frame *f = XFRAME (WVAR (w, frame));
27267 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27268 #ifdef HAVE_WINDOW_SYSTEM
27269 Display_Info *dpyinfo;
27270 #endif
27271 Cursor cursor = No_Cursor;
27272 Lisp_Object pointer = Qnil;
27273 int dx, dy, width, height;
27274 ptrdiff_t charpos;
27275 Lisp_Object string, object = Qnil;
27276 Lisp_Object pos IF_LINT (= Qnil), help;
27277
27278 Lisp_Object mouse_face;
27279 int original_x_pixel = x;
27280 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27281 struct glyph_row *row IF_LINT (= 0);
27282
27283 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27284 {
27285 int x0;
27286 struct glyph *end;
27287
27288 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27289 returns them in row/column units! */
27290 string = mode_line_string (w, area, &x, &y, &charpos,
27291 &object, &dx, &dy, &width, &height);
27292
27293 row = (area == ON_MODE_LINE
27294 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27295 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27296
27297 /* Find the glyph under the mouse pointer. */
27298 if (row->mode_line_p && row->enabled_p)
27299 {
27300 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27301 end = glyph + row->used[TEXT_AREA];
27302
27303 for (x0 = original_x_pixel;
27304 glyph < end && x0 >= glyph->pixel_width;
27305 ++glyph)
27306 x0 -= glyph->pixel_width;
27307
27308 if (glyph >= end)
27309 glyph = NULL;
27310 }
27311 }
27312 else
27313 {
27314 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27315 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27316 returns them in row/column units! */
27317 string = marginal_area_string (w, area, &x, &y, &charpos,
27318 &object, &dx, &dy, &width, &height);
27319 }
27320
27321 help = Qnil;
27322
27323 #ifdef HAVE_WINDOW_SYSTEM
27324 if (IMAGEP (object))
27325 {
27326 Lisp_Object image_map, hotspot;
27327 if ((image_map = Fplist_get (XCDR (object), QCmap),
27328 !NILP (image_map))
27329 && (hotspot = find_hot_spot (image_map, dx, dy),
27330 CONSP (hotspot))
27331 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27332 {
27333 Lisp_Object plist;
27334
27335 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27336 If so, we could look for mouse-enter, mouse-leave
27337 properties in PLIST (and do something...). */
27338 hotspot = XCDR (hotspot);
27339 if (CONSP (hotspot)
27340 && (plist = XCAR (hotspot), CONSP (plist)))
27341 {
27342 pointer = Fplist_get (plist, Qpointer);
27343 if (NILP (pointer))
27344 pointer = Qhand;
27345 help = Fplist_get (plist, Qhelp_echo);
27346 if (!NILP (help))
27347 {
27348 help_echo_string = help;
27349 XSETWINDOW (help_echo_window, w);
27350 help_echo_object = WVAR (w, buffer);
27351 help_echo_pos = charpos;
27352 }
27353 }
27354 }
27355 if (NILP (pointer))
27356 pointer = Fplist_get (XCDR (object), QCpointer);
27357 }
27358 #endif /* HAVE_WINDOW_SYSTEM */
27359
27360 if (STRINGP (string))
27361 pos = make_number (charpos);
27362
27363 /* Set the help text and mouse pointer. If the mouse is on a part
27364 of the mode line without any text (e.g. past the right edge of
27365 the mode line text), use the default help text and pointer. */
27366 if (STRINGP (string) || area == ON_MODE_LINE)
27367 {
27368 /* Arrange to display the help by setting the global variables
27369 help_echo_string, help_echo_object, and help_echo_pos. */
27370 if (NILP (help))
27371 {
27372 if (STRINGP (string))
27373 help = Fget_text_property (pos, Qhelp_echo, string);
27374
27375 if (!NILP (help))
27376 {
27377 help_echo_string = help;
27378 XSETWINDOW (help_echo_window, w);
27379 help_echo_object = string;
27380 help_echo_pos = charpos;
27381 }
27382 else if (area == ON_MODE_LINE)
27383 {
27384 Lisp_Object default_help
27385 = buffer_local_value_1 (Qmode_line_default_help_echo,
27386 WVAR (w, buffer));
27387
27388 if (STRINGP (default_help))
27389 {
27390 help_echo_string = default_help;
27391 XSETWINDOW (help_echo_window, w);
27392 help_echo_object = Qnil;
27393 help_echo_pos = -1;
27394 }
27395 }
27396 }
27397
27398 #ifdef HAVE_WINDOW_SYSTEM
27399 /* Change the mouse pointer according to what is under it. */
27400 if (FRAME_WINDOW_P (f))
27401 {
27402 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27403 if (STRINGP (string))
27404 {
27405 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27406
27407 if (NILP (pointer))
27408 pointer = Fget_text_property (pos, Qpointer, string);
27409
27410 /* Change the mouse pointer according to what is under X/Y. */
27411 if (NILP (pointer)
27412 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27413 {
27414 Lisp_Object map;
27415 map = Fget_text_property (pos, Qlocal_map, string);
27416 if (!KEYMAPP (map))
27417 map = Fget_text_property (pos, Qkeymap, string);
27418 if (!KEYMAPP (map))
27419 cursor = dpyinfo->vertical_scroll_bar_cursor;
27420 }
27421 }
27422 else
27423 /* Default mode-line pointer. */
27424 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27425 }
27426 #endif
27427 }
27428
27429 /* Change the mouse face according to what is under X/Y. */
27430 if (STRINGP (string))
27431 {
27432 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27433 if (!NILP (mouse_face)
27434 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27435 && glyph)
27436 {
27437 Lisp_Object b, e;
27438
27439 struct glyph * tmp_glyph;
27440
27441 int gpos;
27442 int gseq_length;
27443 int total_pixel_width;
27444 ptrdiff_t begpos, endpos, ignore;
27445
27446 int vpos, hpos;
27447
27448 b = Fprevious_single_property_change (make_number (charpos + 1),
27449 Qmouse_face, string, Qnil);
27450 if (NILP (b))
27451 begpos = 0;
27452 else
27453 begpos = XINT (b);
27454
27455 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27456 if (NILP (e))
27457 endpos = SCHARS (string);
27458 else
27459 endpos = XINT (e);
27460
27461 /* Calculate the glyph position GPOS of GLYPH in the
27462 displayed string, relative to the beginning of the
27463 highlighted part of the string.
27464
27465 Note: GPOS is different from CHARPOS. CHARPOS is the
27466 position of GLYPH in the internal string object. A mode
27467 line string format has structures which are converted to
27468 a flattened string by the Emacs Lisp interpreter. The
27469 internal string is an element of those structures. The
27470 displayed string is the flattened string. */
27471 tmp_glyph = row_start_glyph;
27472 while (tmp_glyph < glyph
27473 && (!(EQ (tmp_glyph->object, glyph->object)
27474 && begpos <= tmp_glyph->charpos
27475 && tmp_glyph->charpos < endpos)))
27476 tmp_glyph++;
27477 gpos = glyph - tmp_glyph;
27478
27479 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27480 the highlighted part of the displayed string to which
27481 GLYPH belongs. Note: GSEQ_LENGTH is different from
27482 SCHARS (STRING), because the latter returns the length of
27483 the internal string. */
27484 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27485 tmp_glyph > glyph
27486 && (!(EQ (tmp_glyph->object, glyph->object)
27487 && begpos <= tmp_glyph->charpos
27488 && tmp_glyph->charpos < endpos));
27489 tmp_glyph--)
27490 ;
27491 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27492
27493 /* Calculate the total pixel width of all the glyphs between
27494 the beginning of the highlighted area and GLYPH. */
27495 total_pixel_width = 0;
27496 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27497 total_pixel_width += tmp_glyph->pixel_width;
27498
27499 /* Pre calculation of re-rendering position. Note: X is in
27500 column units here, after the call to mode_line_string or
27501 marginal_area_string. */
27502 hpos = x - gpos;
27503 vpos = (area == ON_MODE_LINE
27504 ? (w->current_matrix)->nrows - 1
27505 : 0);
27506
27507 /* If GLYPH's position is included in the region that is
27508 already drawn in mouse face, we have nothing to do. */
27509 if ( EQ (window, hlinfo->mouse_face_window)
27510 && (!row->reversed_p
27511 ? (hlinfo->mouse_face_beg_col <= hpos
27512 && hpos < hlinfo->mouse_face_end_col)
27513 /* In R2L rows we swap BEG and END, see below. */
27514 : (hlinfo->mouse_face_end_col <= hpos
27515 && hpos < hlinfo->mouse_face_beg_col))
27516 && hlinfo->mouse_face_beg_row == vpos )
27517 return;
27518
27519 if (clear_mouse_face (hlinfo))
27520 cursor = No_Cursor;
27521
27522 if (!row->reversed_p)
27523 {
27524 hlinfo->mouse_face_beg_col = hpos;
27525 hlinfo->mouse_face_beg_x = original_x_pixel
27526 - (total_pixel_width + dx);
27527 hlinfo->mouse_face_end_col = hpos + gseq_length;
27528 hlinfo->mouse_face_end_x = 0;
27529 }
27530 else
27531 {
27532 /* In R2L rows, show_mouse_face expects BEG and END
27533 coordinates to be swapped. */
27534 hlinfo->mouse_face_end_col = hpos;
27535 hlinfo->mouse_face_end_x = original_x_pixel
27536 - (total_pixel_width + dx);
27537 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27538 hlinfo->mouse_face_beg_x = 0;
27539 }
27540
27541 hlinfo->mouse_face_beg_row = vpos;
27542 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27543 hlinfo->mouse_face_beg_y = 0;
27544 hlinfo->mouse_face_end_y = 0;
27545 hlinfo->mouse_face_past_end = 0;
27546 hlinfo->mouse_face_window = window;
27547
27548 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27549 charpos,
27550 0, 0, 0,
27551 &ignore,
27552 glyph->face_id,
27553 1);
27554 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27555
27556 if (NILP (pointer))
27557 pointer = Qhand;
27558 }
27559 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27560 clear_mouse_face (hlinfo);
27561 }
27562 #ifdef HAVE_WINDOW_SYSTEM
27563 if (FRAME_WINDOW_P (f))
27564 define_frame_cursor1 (f, cursor, pointer);
27565 #endif
27566 }
27567
27568
27569 /* EXPORT:
27570 Take proper action when the mouse has moved to position X, Y on
27571 frame F as regards highlighting characters that have mouse-face
27572 properties. Also de-highlighting chars where the mouse was before.
27573 X and Y can be negative or out of range. */
27574
27575 void
27576 note_mouse_highlight (struct frame *f, int x, int y)
27577 {
27578 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27579 enum window_part part = ON_NOTHING;
27580 Lisp_Object window;
27581 struct window *w;
27582 Cursor cursor = No_Cursor;
27583 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27584 struct buffer *b;
27585
27586 /* When a menu is active, don't highlight because this looks odd. */
27587 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27588 if (popup_activated ())
27589 return;
27590 #endif
27591
27592 if (NILP (Vmouse_highlight)
27593 || !f->glyphs_initialized_p
27594 || f->pointer_invisible)
27595 return;
27596
27597 hlinfo->mouse_face_mouse_x = x;
27598 hlinfo->mouse_face_mouse_y = y;
27599 hlinfo->mouse_face_mouse_frame = f;
27600
27601 if (hlinfo->mouse_face_defer)
27602 return;
27603
27604 if (gc_in_progress)
27605 {
27606 hlinfo->mouse_face_deferred_gc = 1;
27607 return;
27608 }
27609
27610 /* Which window is that in? */
27611 window = window_from_coordinates (f, x, y, &part, 1);
27612
27613 /* If displaying active text in another window, clear that. */
27614 if (! EQ (window, hlinfo->mouse_face_window)
27615 /* Also clear if we move out of text area in same window. */
27616 || (!NILP (hlinfo->mouse_face_window)
27617 && !NILP (window)
27618 && part != ON_TEXT
27619 && part != ON_MODE_LINE
27620 && part != ON_HEADER_LINE))
27621 clear_mouse_face (hlinfo);
27622
27623 /* Not on a window -> return. */
27624 if (!WINDOWP (window))
27625 return;
27626
27627 /* Reset help_echo_string. It will get recomputed below. */
27628 help_echo_string = Qnil;
27629
27630 /* Convert to window-relative pixel coordinates. */
27631 w = XWINDOW (window);
27632 frame_to_window_pixel_xy (w, &x, &y);
27633
27634 #ifdef HAVE_WINDOW_SYSTEM
27635 /* Handle tool-bar window differently since it doesn't display a
27636 buffer. */
27637 if (EQ (window, FVAR (f, tool_bar_window)))
27638 {
27639 note_tool_bar_highlight (f, x, y);
27640 return;
27641 }
27642 #endif
27643
27644 /* Mouse is on the mode, header line or margin? */
27645 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27646 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27647 {
27648 note_mode_line_or_margin_highlight (window, x, y, part);
27649 return;
27650 }
27651
27652 #ifdef HAVE_WINDOW_SYSTEM
27653 if (part == ON_VERTICAL_BORDER)
27654 {
27655 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27656 help_echo_string = build_string ("drag-mouse-1: resize");
27657 }
27658 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27659 || part == ON_SCROLL_BAR)
27660 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27661 else
27662 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27663 #endif
27664
27665 /* Are we in a window whose display is up to date?
27666 And verify the buffer's text has not changed. */
27667 b = XBUFFER (WVAR (w, buffer));
27668 if (part == ON_TEXT
27669 && EQ (WVAR (w, window_end_valid), WVAR (w, buffer))
27670 && w->last_modified == BUF_MODIFF (b)
27671 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27672 {
27673 int hpos, vpos, dx, dy, area = LAST_AREA;
27674 ptrdiff_t pos;
27675 struct glyph *glyph;
27676 Lisp_Object object;
27677 Lisp_Object mouse_face = Qnil, position;
27678 Lisp_Object *overlay_vec = NULL;
27679 ptrdiff_t i, noverlays;
27680 struct buffer *obuf;
27681 ptrdiff_t obegv, ozv;
27682 int same_region;
27683
27684 /* Find the glyph under X/Y. */
27685 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27686
27687 #ifdef HAVE_WINDOW_SYSTEM
27688 /* Look for :pointer property on image. */
27689 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27690 {
27691 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27692 if (img != NULL && IMAGEP (img->spec))
27693 {
27694 Lisp_Object image_map, hotspot;
27695 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27696 !NILP (image_map))
27697 && (hotspot = find_hot_spot (image_map,
27698 glyph->slice.img.x + dx,
27699 glyph->slice.img.y + dy),
27700 CONSP (hotspot))
27701 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27702 {
27703 Lisp_Object plist;
27704
27705 /* Could check XCAR (hotspot) to see if we enter/leave
27706 this hot-spot.
27707 If so, we could look for mouse-enter, mouse-leave
27708 properties in PLIST (and do something...). */
27709 hotspot = XCDR (hotspot);
27710 if (CONSP (hotspot)
27711 && (plist = XCAR (hotspot), CONSP (plist)))
27712 {
27713 pointer = Fplist_get (plist, Qpointer);
27714 if (NILP (pointer))
27715 pointer = Qhand;
27716 help_echo_string = Fplist_get (plist, Qhelp_echo);
27717 if (!NILP (help_echo_string))
27718 {
27719 help_echo_window = window;
27720 help_echo_object = glyph->object;
27721 help_echo_pos = glyph->charpos;
27722 }
27723 }
27724 }
27725 if (NILP (pointer))
27726 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27727 }
27728 }
27729 #endif /* HAVE_WINDOW_SYSTEM */
27730
27731 /* Clear mouse face if X/Y not over text. */
27732 if (glyph == NULL
27733 || area != TEXT_AREA
27734 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27735 /* Glyph's OBJECT is an integer for glyphs inserted by the
27736 display engine for its internal purposes, like truncation
27737 and continuation glyphs and blanks beyond the end of
27738 line's text on text terminals. If we are over such a
27739 glyph, we are not over any text. */
27740 || INTEGERP (glyph->object)
27741 /* R2L rows have a stretch glyph at their front, which
27742 stands for no text, whereas L2R rows have no glyphs at
27743 all beyond the end of text. Treat such stretch glyphs
27744 like we do with NULL glyphs in L2R rows. */
27745 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27746 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27747 && glyph->type == STRETCH_GLYPH
27748 && glyph->avoid_cursor_p))
27749 {
27750 if (clear_mouse_face (hlinfo))
27751 cursor = No_Cursor;
27752 #ifdef HAVE_WINDOW_SYSTEM
27753 if (FRAME_WINDOW_P (f) && NILP (pointer))
27754 {
27755 if (area != TEXT_AREA)
27756 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27757 else
27758 pointer = Vvoid_text_area_pointer;
27759 }
27760 #endif
27761 goto set_cursor;
27762 }
27763
27764 pos = glyph->charpos;
27765 object = glyph->object;
27766 if (!STRINGP (object) && !BUFFERP (object))
27767 goto set_cursor;
27768
27769 /* If we get an out-of-range value, return now; avoid an error. */
27770 if (BUFFERP (object) && pos > BUF_Z (b))
27771 goto set_cursor;
27772
27773 /* Make the window's buffer temporarily current for
27774 overlays_at and compute_char_face. */
27775 obuf = current_buffer;
27776 current_buffer = b;
27777 obegv = BEGV;
27778 ozv = ZV;
27779 BEGV = BEG;
27780 ZV = Z;
27781
27782 /* Is this char mouse-active or does it have help-echo? */
27783 position = make_number (pos);
27784
27785 if (BUFFERP (object))
27786 {
27787 /* Put all the overlays we want in a vector in overlay_vec. */
27788 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27789 /* Sort overlays into increasing priority order. */
27790 noverlays = sort_overlays (overlay_vec, noverlays, w);
27791 }
27792 else
27793 noverlays = 0;
27794
27795 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27796
27797 if (same_region)
27798 cursor = No_Cursor;
27799
27800 /* Check mouse-face highlighting. */
27801 if (! same_region
27802 /* If there exists an overlay with mouse-face overlapping
27803 the one we are currently highlighting, we have to
27804 check if we enter the overlapping overlay, and then
27805 highlight only that. */
27806 || (OVERLAYP (hlinfo->mouse_face_overlay)
27807 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27808 {
27809 /* Find the highest priority overlay with a mouse-face. */
27810 Lisp_Object overlay = Qnil;
27811 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27812 {
27813 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27814 if (!NILP (mouse_face))
27815 overlay = overlay_vec[i];
27816 }
27817
27818 /* If we're highlighting the same overlay as before, there's
27819 no need to do that again. */
27820 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27821 goto check_help_echo;
27822 hlinfo->mouse_face_overlay = overlay;
27823
27824 /* Clear the display of the old active region, if any. */
27825 if (clear_mouse_face (hlinfo))
27826 cursor = No_Cursor;
27827
27828 /* If no overlay applies, get a text property. */
27829 if (NILP (overlay))
27830 mouse_face = Fget_text_property (position, Qmouse_face, object);
27831
27832 /* Next, compute the bounds of the mouse highlighting and
27833 display it. */
27834 if (!NILP (mouse_face) && STRINGP (object))
27835 {
27836 /* The mouse-highlighting comes from a display string
27837 with a mouse-face. */
27838 Lisp_Object s, e;
27839 ptrdiff_t ignore;
27840
27841 s = Fprevious_single_property_change
27842 (make_number (pos + 1), Qmouse_face, object, Qnil);
27843 e = Fnext_single_property_change
27844 (position, Qmouse_face, object, Qnil);
27845 if (NILP (s))
27846 s = make_number (0);
27847 if (NILP (e))
27848 e = make_number (SCHARS (object) - 1);
27849 mouse_face_from_string_pos (w, hlinfo, object,
27850 XINT (s), XINT (e));
27851 hlinfo->mouse_face_past_end = 0;
27852 hlinfo->mouse_face_window = window;
27853 hlinfo->mouse_face_face_id
27854 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27855 glyph->face_id, 1);
27856 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27857 cursor = No_Cursor;
27858 }
27859 else
27860 {
27861 /* The mouse-highlighting, if any, comes from an overlay
27862 or text property in the buffer. */
27863 Lisp_Object buffer IF_LINT (= Qnil);
27864 Lisp_Object disp_string IF_LINT (= Qnil);
27865
27866 if (STRINGP (object))
27867 {
27868 /* If we are on a display string with no mouse-face,
27869 check if the text under it has one. */
27870 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27871 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27872 pos = string_buffer_position (object, start);
27873 if (pos > 0)
27874 {
27875 mouse_face = get_char_property_and_overlay
27876 (make_number (pos), Qmouse_face, WVAR (w, buffer), &overlay);
27877 buffer = WVAR (w, buffer);
27878 disp_string = object;
27879 }
27880 }
27881 else
27882 {
27883 buffer = object;
27884 disp_string = Qnil;
27885 }
27886
27887 if (!NILP (mouse_face))
27888 {
27889 Lisp_Object before, after;
27890 Lisp_Object before_string, after_string;
27891 /* To correctly find the limits of mouse highlight
27892 in a bidi-reordered buffer, we must not use the
27893 optimization of limiting the search in
27894 previous-single-property-change and
27895 next-single-property-change, because
27896 rows_from_pos_range needs the real start and end
27897 positions to DTRT in this case. That's because
27898 the first row visible in a window does not
27899 necessarily display the character whose position
27900 is the smallest. */
27901 Lisp_Object lim1 =
27902 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27903 ? Fmarker_position (WVAR (w, start))
27904 : Qnil;
27905 Lisp_Object lim2 =
27906 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27907 ? make_number (BUF_Z (XBUFFER (buffer))
27908 - XFASTINT (WVAR (w, window_end_pos)))
27909 : Qnil;
27910
27911 if (NILP (overlay))
27912 {
27913 /* Handle the text property case. */
27914 before = Fprevious_single_property_change
27915 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27916 after = Fnext_single_property_change
27917 (make_number (pos), Qmouse_face, buffer, lim2);
27918 before_string = after_string = Qnil;
27919 }
27920 else
27921 {
27922 /* Handle the overlay case. */
27923 before = Foverlay_start (overlay);
27924 after = Foverlay_end (overlay);
27925 before_string = Foverlay_get (overlay, Qbefore_string);
27926 after_string = Foverlay_get (overlay, Qafter_string);
27927
27928 if (!STRINGP (before_string)) before_string = Qnil;
27929 if (!STRINGP (after_string)) after_string = Qnil;
27930 }
27931
27932 mouse_face_from_buffer_pos (window, hlinfo, pos,
27933 NILP (before)
27934 ? 1
27935 : XFASTINT (before),
27936 NILP (after)
27937 ? BUF_Z (XBUFFER (buffer))
27938 : XFASTINT (after),
27939 before_string, after_string,
27940 disp_string);
27941 cursor = No_Cursor;
27942 }
27943 }
27944 }
27945
27946 check_help_echo:
27947
27948 /* Look for a `help-echo' property. */
27949 if (NILP (help_echo_string)) {
27950 Lisp_Object help, overlay;
27951
27952 /* Check overlays first. */
27953 help = overlay = Qnil;
27954 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27955 {
27956 overlay = overlay_vec[i];
27957 help = Foverlay_get (overlay, Qhelp_echo);
27958 }
27959
27960 if (!NILP (help))
27961 {
27962 help_echo_string = help;
27963 help_echo_window = window;
27964 help_echo_object = overlay;
27965 help_echo_pos = pos;
27966 }
27967 else
27968 {
27969 Lisp_Object obj = glyph->object;
27970 ptrdiff_t charpos = glyph->charpos;
27971
27972 /* Try text properties. */
27973 if (STRINGP (obj)
27974 && charpos >= 0
27975 && charpos < SCHARS (obj))
27976 {
27977 help = Fget_text_property (make_number (charpos),
27978 Qhelp_echo, obj);
27979 if (NILP (help))
27980 {
27981 /* If the string itself doesn't specify a help-echo,
27982 see if the buffer text ``under'' it does. */
27983 struct glyph_row *r
27984 = MATRIX_ROW (w->current_matrix, vpos);
27985 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27986 ptrdiff_t p = string_buffer_position (obj, start);
27987 if (p > 0)
27988 {
27989 help = Fget_char_property (make_number (p),
27990 Qhelp_echo, WVAR (w, buffer));
27991 if (!NILP (help))
27992 {
27993 charpos = p;
27994 obj = WVAR (w, buffer);
27995 }
27996 }
27997 }
27998 }
27999 else if (BUFFERP (obj)
28000 && charpos >= BEGV
28001 && charpos < ZV)
28002 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28003 obj);
28004
28005 if (!NILP (help))
28006 {
28007 help_echo_string = help;
28008 help_echo_window = window;
28009 help_echo_object = obj;
28010 help_echo_pos = charpos;
28011 }
28012 }
28013 }
28014
28015 #ifdef HAVE_WINDOW_SYSTEM
28016 /* Look for a `pointer' property. */
28017 if (FRAME_WINDOW_P (f) && NILP (pointer))
28018 {
28019 /* Check overlays first. */
28020 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28021 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28022
28023 if (NILP (pointer))
28024 {
28025 Lisp_Object obj = glyph->object;
28026 ptrdiff_t charpos = glyph->charpos;
28027
28028 /* Try text properties. */
28029 if (STRINGP (obj)
28030 && charpos >= 0
28031 && charpos < SCHARS (obj))
28032 {
28033 pointer = Fget_text_property (make_number (charpos),
28034 Qpointer, obj);
28035 if (NILP (pointer))
28036 {
28037 /* If the string itself doesn't specify a pointer,
28038 see if the buffer text ``under'' it does. */
28039 struct glyph_row *r
28040 = MATRIX_ROW (w->current_matrix, vpos);
28041 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28042 ptrdiff_t p = string_buffer_position (obj, start);
28043 if (p > 0)
28044 pointer = Fget_char_property (make_number (p),
28045 Qpointer, WVAR (w, buffer));
28046 }
28047 }
28048 else if (BUFFERP (obj)
28049 && charpos >= BEGV
28050 && charpos < ZV)
28051 pointer = Fget_text_property (make_number (charpos),
28052 Qpointer, obj);
28053 }
28054 }
28055 #endif /* HAVE_WINDOW_SYSTEM */
28056
28057 BEGV = obegv;
28058 ZV = ozv;
28059 current_buffer = obuf;
28060 }
28061
28062 set_cursor:
28063
28064 #ifdef HAVE_WINDOW_SYSTEM
28065 if (FRAME_WINDOW_P (f))
28066 define_frame_cursor1 (f, cursor, pointer);
28067 #else
28068 /* This is here to prevent a compiler error, about "label at end of
28069 compound statement". */
28070 return;
28071 #endif
28072 }
28073
28074
28075 /* EXPORT for RIF:
28076 Clear any mouse-face on window W. This function is part of the
28077 redisplay interface, and is called from try_window_id and similar
28078 functions to ensure the mouse-highlight is off. */
28079
28080 void
28081 x_clear_window_mouse_face (struct window *w)
28082 {
28083 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (WVAR (w, frame)));
28084 Lisp_Object window;
28085
28086 BLOCK_INPUT;
28087 XSETWINDOW (window, w);
28088 if (EQ (window, hlinfo->mouse_face_window))
28089 clear_mouse_face (hlinfo);
28090 UNBLOCK_INPUT;
28091 }
28092
28093
28094 /* EXPORT:
28095 Just discard the mouse face information for frame F, if any.
28096 This is used when the size of F is changed. */
28097
28098 void
28099 cancel_mouse_face (struct frame *f)
28100 {
28101 Lisp_Object window;
28102 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28103
28104 window = hlinfo->mouse_face_window;
28105 if (! NILP (window) && XFRAME (WVAR (XWINDOW (window), frame)) == f)
28106 {
28107 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28108 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28109 hlinfo->mouse_face_window = Qnil;
28110 }
28111 }
28112
28113
28114 \f
28115 /***********************************************************************
28116 Exposure Events
28117 ***********************************************************************/
28118
28119 #ifdef HAVE_WINDOW_SYSTEM
28120
28121 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28122 which intersects rectangle R. R is in window-relative coordinates. */
28123
28124 static void
28125 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28126 enum glyph_row_area area)
28127 {
28128 struct glyph *first = row->glyphs[area];
28129 struct glyph *end = row->glyphs[area] + row->used[area];
28130 struct glyph *last;
28131 int first_x, start_x, x;
28132
28133 if (area == TEXT_AREA && row->fill_line_p)
28134 /* If row extends face to end of line write the whole line. */
28135 draw_glyphs (w, 0, row, area,
28136 0, row->used[area],
28137 DRAW_NORMAL_TEXT, 0);
28138 else
28139 {
28140 /* Set START_X to the window-relative start position for drawing glyphs of
28141 AREA. The first glyph of the text area can be partially visible.
28142 The first glyphs of other areas cannot. */
28143 start_x = window_box_left_offset (w, area);
28144 x = start_x;
28145 if (area == TEXT_AREA)
28146 x += row->x;
28147
28148 /* Find the first glyph that must be redrawn. */
28149 while (first < end
28150 && x + first->pixel_width < r->x)
28151 {
28152 x += first->pixel_width;
28153 ++first;
28154 }
28155
28156 /* Find the last one. */
28157 last = first;
28158 first_x = x;
28159 while (last < end
28160 && x < r->x + r->width)
28161 {
28162 x += last->pixel_width;
28163 ++last;
28164 }
28165
28166 /* Repaint. */
28167 if (last > first)
28168 draw_glyphs (w, first_x - start_x, row, area,
28169 first - row->glyphs[area], last - row->glyphs[area],
28170 DRAW_NORMAL_TEXT, 0);
28171 }
28172 }
28173
28174
28175 /* Redraw the parts of the glyph row ROW on window W intersecting
28176 rectangle R. R is in window-relative coordinates. Value is
28177 non-zero if mouse-face was overwritten. */
28178
28179 static int
28180 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28181 {
28182 eassert (row->enabled_p);
28183
28184 if (row->mode_line_p || w->pseudo_window_p)
28185 draw_glyphs (w, 0, row, TEXT_AREA,
28186 0, row->used[TEXT_AREA],
28187 DRAW_NORMAL_TEXT, 0);
28188 else
28189 {
28190 if (row->used[LEFT_MARGIN_AREA])
28191 expose_area (w, row, r, LEFT_MARGIN_AREA);
28192 if (row->used[TEXT_AREA])
28193 expose_area (w, row, r, TEXT_AREA);
28194 if (row->used[RIGHT_MARGIN_AREA])
28195 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28196 draw_row_fringe_bitmaps (w, row);
28197 }
28198
28199 return row->mouse_face_p;
28200 }
28201
28202
28203 /* Redraw those parts of glyphs rows during expose event handling that
28204 overlap other rows. Redrawing of an exposed line writes over parts
28205 of lines overlapping that exposed line; this function fixes that.
28206
28207 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28208 row in W's current matrix that is exposed and overlaps other rows.
28209 LAST_OVERLAPPING_ROW is the last such row. */
28210
28211 static void
28212 expose_overlaps (struct window *w,
28213 struct glyph_row *first_overlapping_row,
28214 struct glyph_row *last_overlapping_row,
28215 XRectangle *r)
28216 {
28217 struct glyph_row *row;
28218
28219 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28220 if (row->overlapping_p)
28221 {
28222 eassert (row->enabled_p && !row->mode_line_p);
28223
28224 row->clip = r;
28225 if (row->used[LEFT_MARGIN_AREA])
28226 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28227
28228 if (row->used[TEXT_AREA])
28229 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28230
28231 if (row->used[RIGHT_MARGIN_AREA])
28232 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28233 row->clip = NULL;
28234 }
28235 }
28236
28237
28238 /* Return non-zero if W's cursor intersects rectangle R. */
28239
28240 static int
28241 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28242 {
28243 XRectangle cr, result;
28244 struct glyph *cursor_glyph;
28245 struct glyph_row *row;
28246
28247 if (w->phys_cursor.vpos >= 0
28248 && w->phys_cursor.vpos < w->current_matrix->nrows
28249 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28250 row->enabled_p)
28251 && row->cursor_in_fringe_p)
28252 {
28253 /* Cursor is in the fringe. */
28254 cr.x = window_box_right_offset (w,
28255 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28256 ? RIGHT_MARGIN_AREA
28257 : TEXT_AREA));
28258 cr.y = row->y;
28259 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28260 cr.height = row->height;
28261 return x_intersect_rectangles (&cr, r, &result);
28262 }
28263
28264 cursor_glyph = get_phys_cursor_glyph (w);
28265 if (cursor_glyph)
28266 {
28267 /* r is relative to W's box, but w->phys_cursor.x is relative
28268 to left edge of W's TEXT area. Adjust it. */
28269 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28270 cr.y = w->phys_cursor.y;
28271 cr.width = cursor_glyph->pixel_width;
28272 cr.height = w->phys_cursor_height;
28273 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28274 I assume the effect is the same -- and this is portable. */
28275 return x_intersect_rectangles (&cr, r, &result);
28276 }
28277 /* If we don't understand the format, pretend we're not in the hot-spot. */
28278 return 0;
28279 }
28280
28281
28282 /* EXPORT:
28283 Draw a vertical window border to the right of window W if W doesn't
28284 have vertical scroll bars. */
28285
28286 void
28287 x_draw_vertical_border (struct window *w)
28288 {
28289 struct frame *f = XFRAME (WINDOW_FRAME (w));
28290
28291 /* We could do better, if we knew what type of scroll-bar the adjacent
28292 windows (on either side) have... But we don't :-(
28293 However, I think this works ok. ++KFS 2003-04-25 */
28294
28295 /* Redraw borders between horizontally adjacent windows. Don't
28296 do it for frames with vertical scroll bars because either the
28297 right scroll bar of a window, or the left scroll bar of its
28298 neighbor will suffice as a border. */
28299 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WVAR (w, frame))))
28300 return;
28301
28302 if (!WINDOW_RIGHTMOST_P (w)
28303 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28304 {
28305 int x0, x1, y0, y1;
28306
28307 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28308 y1 -= 1;
28309
28310 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28311 x1 -= 1;
28312
28313 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28314 }
28315 else if (!WINDOW_LEFTMOST_P (w)
28316 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28317 {
28318 int x0, x1, y0, y1;
28319
28320 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28321 y1 -= 1;
28322
28323 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28324 x0 -= 1;
28325
28326 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28327 }
28328 }
28329
28330
28331 /* Redraw the part of window W intersection rectangle FR. Pixel
28332 coordinates in FR are frame-relative. Call this function with
28333 input blocked. Value is non-zero if the exposure overwrites
28334 mouse-face. */
28335
28336 static int
28337 expose_window (struct window *w, XRectangle *fr)
28338 {
28339 struct frame *f = XFRAME (WVAR (w, frame));
28340 XRectangle wr, r;
28341 int mouse_face_overwritten_p = 0;
28342
28343 /* If window is not yet fully initialized, do nothing. This can
28344 happen when toolkit scroll bars are used and a window is split.
28345 Reconfiguring the scroll bar will generate an expose for a newly
28346 created window. */
28347 if (w->current_matrix == NULL)
28348 return 0;
28349
28350 /* When we're currently updating the window, display and current
28351 matrix usually don't agree. Arrange for a thorough display
28352 later. */
28353 if (w == updated_window)
28354 {
28355 SET_FRAME_GARBAGED (f);
28356 return 0;
28357 }
28358
28359 /* Frame-relative pixel rectangle of W. */
28360 wr.x = WINDOW_LEFT_EDGE_X (w);
28361 wr.y = WINDOW_TOP_EDGE_Y (w);
28362 wr.width = WINDOW_TOTAL_WIDTH (w);
28363 wr.height = WINDOW_TOTAL_HEIGHT (w);
28364
28365 if (x_intersect_rectangles (fr, &wr, &r))
28366 {
28367 int yb = window_text_bottom_y (w);
28368 struct glyph_row *row;
28369 int cursor_cleared_p, phys_cursor_on_p;
28370 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28371
28372 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28373 r.x, r.y, r.width, r.height));
28374
28375 /* Convert to window coordinates. */
28376 r.x -= WINDOW_LEFT_EDGE_X (w);
28377 r.y -= WINDOW_TOP_EDGE_Y (w);
28378
28379 /* Turn off the cursor. */
28380 if (!w->pseudo_window_p
28381 && phys_cursor_in_rect_p (w, &r))
28382 {
28383 x_clear_cursor (w);
28384 cursor_cleared_p = 1;
28385 }
28386 else
28387 cursor_cleared_p = 0;
28388
28389 /* If the row containing the cursor extends face to end of line,
28390 then expose_area might overwrite the cursor outside the
28391 rectangle and thus notice_overwritten_cursor might clear
28392 w->phys_cursor_on_p. We remember the original value and
28393 check later if it is changed. */
28394 phys_cursor_on_p = w->phys_cursor_on_p;
28395
28396 /* Update lines intersecting rectangle R. */
28397 first_overlapping_row = last_overlapping_row = NULL;
28398 for (row = w->current_matrix->rows;
28399 row->enabled_p;
28400 ++row)
28401 {
28402 int y0 = row->y;
28403 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28404
28405 if ((y0 >= r.y && y0 < r.y + r.height)
28406 || (y1 > r.y && y1 < r.y + r.height)
28407 || (r.y >= y0 && r.y < y1)
28408 || (r.y + r.height > y0 && r.y + r.height < y1))
28409 {
28410 /* A header line may be overlapping, but there is no need
28411 to fix overlapping areas for them. KFS 2005-02-12 */
28412 if (row->overlapping_p && !row->mode_line_p)
28413 {
28414 if (first_overlapping_row == NULL)
28415 first_overlapping_row = row;
28416 last_overlapping_row = row;
28417 }
28418
28419 row->clip = fr;
28420 if (expose_line (w, row, &r))
28421 mouse_face_overwritten_p = 1;
28422 row->clip = NULL;
28423 }
28424 else if (row->overlapping_p)
28425 {
28426 /* We must redraw a row overlapping the exposed area. */
28427 if (y0 < r.y
28428 ? y0 + row->phys_height > r.y
28429 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28430 {
28431 if (first_overlapping_row == NULL)
28432 first_overlapping_row = row;
28433 last_overlapping_row = row;
28434 }
28435 }
28436
28437 if (y1 >= yb)
28438 break;
28439 }
28440
28441 /* Display the mode line if there is one. */
28442 if (WINDOW_WANTS_MODELINE_P (w)
28443 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28444 row->enabled_p)
28445 && row->y < r.y + r.height)
28446 {
28447 if (expose_line (w, row, &r))
28448 mouse_face_overwritten_p = 1;
28449 }
28450
28451 if (!w->pseudo_window_p)
28452 {
28453 /* Fix the display of overlapping rows. */
28454 if (first_overlapping_row)
28455 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28456 fr);
28457
28458 /* Draw border between windows. */
28459 x_draw_vertical_border (w);
28460
28461 /* Turn the cursor on again. */
28462 if (cursor_cleared_p
28463 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28464 update_window_cursor (w, 1);
28465 }
28466 }
28467
28468 return mouse_face_overwritten_p;
28469 }
28470
28471
28472
28473 /* Redraw (parts) of all windows in the window tree rooted at W that
28474 intersect R. R contains frame pixel coordinates. Value is
28475 non-zero if the exposure overwrites mouse-face. */
28476
28477 static int
28478 expose_window_tree (struct window *w, XRectangle *r)
28479 {
28480 struct frame *f = XFRAME (WVAR (w, frame));
28481 int mouse_face_overwritten_p = 0;
28482
28483 while (w && !FRAME_GARBAGED_P (f))
28484 {
28485 if (!NILP (WVAR (w, hchild)))
28486 mouse_face_overwritten_p
28487 |= expose_window_tree (XWINDOW (WVAR (w, hchild)), r);
28488 else if (!NILP (WVAR (w, vchild)))
28489 mouse_face_overwritten_p
28490 |= expose_window_tree (XWINDOW (WVAR (w, vchild)), r);
28491 else
28492 mouse_face_overwritten_p |= expose_window (w, r);
28493
28494 w = NILP (WVAR (w, next)) ? NULL : XWINDOW (WVAR (w, next));
28495 }
28496
28497 return mouse_face_overwritten_p;
28498 }
28499
28500
28501 /* EXPORT:
28502 Redisplay an exposed area of frame F. X and Y are the upper-left
28503 corner of the exposed rectangle. W and H are width and height of
28504 the exposed area. All are pixel values. W or H zero means redraw
28505 the entire frame. */
28506
28507 void
28508 expose_frame (struct frame *f, int x, int y, int w, int h)
28509 {
28510 XRectangle r;
28511 int mouse_face_overwritten_p = 0;
28512
28513 TRACE ((stderr, "expose_frame "));
28514
28515 /* No need to redraw if frame will be redrawn soon. */
28516 if (FRAME_GARBAGED_P (f))
28517 {
28518 TRACE ((stderr, " garbaged\n"));
28519 return;
28520 }
28521
28522 /* If basic faces haven't been realized yet, there is no point in
28523 trying to redraw anything. This can happen when we get an expose
28524 event while Emacs is starting, e.g. by moving another window. */
28525 if (FRAME_FACE_CACHE (f) == NULL
28526 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28527 {
28528 TRACE ((stderr, " no faces\n"));
28529 return;
28530 }
28531
28532 if (w == 0 || h == 0)
28533 {
28534 r.x = r.y = 0;
28535 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28536 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28537 }
28538 else
28539 {
28540 r.x = x;
28541 r.y = y;
28542 r.width = w;
28543 r.height = h;
28544 }
28545
28546 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28547 mouse_face_overwritten_p = expose_window_tree (XWINDOW (FVAR (f, root_window)), &r);
28548
28549 if (WINDOWP (FVAR (f, tool_bar_window)))
28550 mouse_face_overwritten_p
28551 |= expose_window (XWINDOW (FVAR (f, tool_bar_window)), &r);
28552
28553 #ifdef HAVE_X_WINDOWS
28554 #ifndef MSDOS
28555 #ifndef USE_X_TOOLKIT
28556 if (WINDOWP (FVAR (f, menu_bar_window)))
28557 mouse_face_overwritten_p
28558 |= expose_window (XWINDOW (FVAR (f, menu_bar_window)), &r);
28559 #endif /* not USE_X_TOOLKIT */
28560 #endif
28561 #endif
28562
28563 /* Some window managers support a focus-follows-mouse style with
28564 delayed raising of frames. Imagine a partially obscured frame,
28565 and moving the mouse into partially obscured mouse-face on that
28566 frame. The visible part of the mouse-face will be highlighted,
28567 then the WM raises the obscured frame. With at least one WM, KDE
28568 2.1, Emacs is not getting any event for the raising of the frame
28569 (even tried with SubstructureRedirectMask), only Expose events.
28570 These expose events will draw text normally, i.e. not
28571 highlighted. Which means we must redo the highlight here.
28572 Subsume it under ``we love X''. --gerd 2001-08-15 */
28573 /* Included in Windows version because Windows most likely does not
28574 do the right thing if any third party tool offers
28575 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28576 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28577 {
28578 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28579 if (f == hlinfo->mouse_face_mouse_frame)
28580 {
28581 int mouse_x = hlinfo->mouse_face_mouse_x;
28582 int mouse_y = hlinfo->mouse_face_mouse_y;
28583 clear_mouse_face (hlinfo);
28584 note_mouse_highlight (f, mouse_x, mouse_y);
28585 }
28586 }
28587 }
28588
28589
28590 /* EXPORT:
28591 Determine the intersection of two rectangles R1 and R2. Return
28592 the intersection in *RESULT. Value is non-zero if RESULT is not
28593 empty. */
28594
28595 int
28596 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28597 {
28598 XRectangle *left, *right;
28599 XRectangle *upper, *lower;
28600 int intersection_p = 0;
28601
28602 /* Rearrange so that R1 is the left-most rectangle. */
28603 if (r1->x < r2->x)
28604 left = r1, right = r2;
28605 else
28606 left = r2, right = r1;
28607
28608 /* X0 of the intersection is right.x0, if this is inside R1,
28609 otherwise there is no intersection. */
28610 if (right->x <= left->x + left->width)
28611 {
28612 result->x = right->x;
28613
28614 /* The right end of the intersection is the minimum of
28615 the right ends of left and right. */
28616 result->width = (min (left->x + left->width, right->x + right->width)
28617 - result->x);
28618
28619 /* Same game for Y. */
28620 if (r1->y < r2->y)
28621 upper = r1, lower = r2;
28622 else
28623 upper = r2, lower = r1;
28624
28625 /* The upper end of the intersection is lower.y0, if this is inside
28626 of upper. Otherwise, there is no intersection. */
28627 if (lower->y <= upper->y + upper->height)
28628 {
28629 result->y = lower->y;
28630
28631 /* The lower end of the intersection is the minimum of the lower
28632 ends of upper and lower. */
28633 result->height = (min (lower->y + lower->height,
28634 upper->y + upper->height)
28635 - result->y);
28636 intersection_p = 1;
28637 }
28638 }
28639
28640 return intersection_p;
28641 }
28642
28643 #endif /* HAVE_WINDOW_SYSTEM */
28644
28645 \f
28646 /***********************************************************************
28647 Initialization
28648 ***********************************************************************/
28649
28650 void
28651 syms_of_xdisp (void)
28652 {
28653 Vwith_echo_area_save_vector = Qnil;
28654 staticpro (&Vwith_echo_area_save_vector);
28655
28656 Vmessage_stack = Qnil;
28657 staticpro (&Vmessage_stack);
28658
28659 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28660
28661 message_dolog_marker1 = Fmake_marker ();
28662 staticpro (&message_dolog_marker1);
28663 message_dolog_marker2 = Fmake_marker ();
28664 staticpro (&message_dolog_marker2);
28665 message_dolog_marker3 = Fmake_marker ();
28666 staticpro (&message_dolog_marker3);
28667
28668 #ifdef GLYPH_DEBUG
28669 defsubr (&Sdump_frame_glyph_matrix);
28670 defsubr (&Sdump_glyph_matrix);
28671 defsubr (&Sdump_glyph_row);
28672 defsubr (&Sdump_tool_bar_row);
28673 defsubr (&Strace_redisplay);
28674 defsubr (&Strace_to_stderr);
28675 #endif
28676 #ifdef HAVE_WINDOW_SYSTEM
28677 defsubr (&Stool_bar_lines_needed);
28678 defsubr (&Slookup_image_map);
28679 #endif
28680 defsubr (&Sformat_mode_line);
28681 defsubr (&Sinvisible_p);
28682 defsubr (&Scurrent_bidi_paragraph_direction);
28683
28684 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28685 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28686 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28687 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28688 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28689 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28690 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28691 DEFSYM (Qeval, "eval");
28692 DEFSYM (QCdata, ":data");
28693 DEFSYM (Qdisplay, "display");
28694 DEFSYM (Qspace_width, "space-width");
28695 DEFSYM (Qraise, "raise");
28696 DEFSYM (Qslice, "slice");
28697 DEFSYM (Qspace, "space");
28698 DEFSYM (Qmargin, "margin");
28699 DEFSYM (Qpointer, "pointer");
28700 DEFSYM (Qleft_margin, "left-margin");
28701 DEFSYM (Qright_margin, "right-margin");
28702 DEFSYM (Qcenter, "center");
28703 DEFSYM (Qline_height, "line-height");
28704 DEFSYM (QCalign_to, ":align-to");
28705 DEFSYM (QCrelative_width, ":relative-width");
28706 DEFSYM (QCrelative_height, ":relative-height");
28707 DEFSYM (QCeval, ":eval");
28708 DEFSYM (QCpropertize, ":propertize");
28709 DEFSYM (QCfile, ":file");
28710 DEFSYM (Qfontified, "fontified");
28711 DEFSYM (Qfontification_functions, "fontification-functions");
28712 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28713 DEFSYM (Qescape_glyph, "escape-glyph");
28714 DEFSYM (Qnobreak_space, "nobreak-space");
28715 DEFSYM (Qimage, "image");
28716 DEFSYM (Qtext, "text");
28717 DEFSYM (Qboth, "both");
28718 DEFSYM (Qboth_horiz, "both-horiz");
28719 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28720 DEFSYM (QCmap, ":map");
28721 DEFSYM (QCpointer, ":pointer");
28722 DEFSYM (Qrect, "rect");
28723 DEFSYM (Qcircle, "circle");
28724 DEFSYM (Qpoly, "poly");
28725 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28726 DEFSYM (Qgrow_only, "grow-only");
28727 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28728 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28729 DEFSYM (Qposition, "position");
28730 DEFSYM (Qbuffer_position, "buffer-position");
28731 DEFSYM (Qobject, "object");
28732 DEFSYM (Qbar, "bar");
28733 DEFSYM (Qhbar, "hbar");
28734 DEFSYM (Qbox, "box");
28735 DEFSYM (Qhollow, "hollow");
28736 DEFSYM (Qhand, "hand");
28737 DEFSYM (Qarrow, "arrow");
28738 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28739
28740 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28741 Fcons (intern_c_string ("void-variable"), Qnil)),
28742 Qnil);
28743 staticpro (&list_of_error);
28744
28745 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28746 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28747 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28748 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28749
28750 echo_buffer[0] = echo_buffer[1] = Qnil;
28751 staticpro (&echo_buffer[0]);
28752 staticpro (&echo_buffer[1]);
28753
28754 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28755 staticpro (&echo_area_buffer[0]);
28756 staticpro (&echo_area_buffer[1]);
28757
28758 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28759 staticpro (&Vmessages_buffer_name);
28760
28761 mode_line_proptrans_alist = Qnil;
28762 staticpro (&mode_line_proptrans_alist);
28763 mode_line_string_list = Qnil;
28764 staticpro (&mode_line_string_list);
28765 mode_line_string_face = Qnil;
28766 staticpro (&mode_line_string_face);
28767 mode_line_string_face_prop = Qnil;
28768 staticpro (&mode_line_string_face_prop);
28769 Vmode_line_unwind_vector = Qnil;
28770 staticpro (&Vmode_line_unwind_vector);
28771
28772 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28773
28774 help_echo_string = Qnil;
28775 staticpro (&help_echo_string);
28776 help_echo_object = Qnil;
28777 staticpro (&help_echo_object);
28778 help_echo_window = Qnil;
28779 staticpro (&help_echo_window);
28780 previous_help_echo_string = Qnil;
28781 staticpro (&previous_help_echo_string);
28782 help_echo_pos = -1;
28783
28784 DEFSYM (Qright_to_left, "right-to-left");
28785 DEFSYM (Qleft_to_right, "left-to-right");
28786
28787 #ifdef HAVE_WINDOW_SYSTEM
28788 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28789 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28790 For example, if a block cursor is over a tab, it will be drawn as
28791 wide as that tab on the display. */);
28792 x_stretch_cursor_p = 0;
28793 #endif
28794
28795 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28796 doc: /* Non-nil means highlight trailing whitespace.
28797 The face used for trailing whitespace is `trailing-whitespace'. */);
28798 Vshow_trailing_whitespace = Qnil;
28799
28800 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28801 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28802 If the value is t, Emacs highlights non-ASCII chars which have the
28803 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28804 or `escape-glyph' face respectively.
28805
28806 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28807 U+2011 (non-breaking hyphen) are affected.
28808
28809 Any other non-nil value means to display these characters as a escape
28810 glyph followed by an ordinary space or hyphen.
28811
28812 A value of nil means no special handling of these characters. */);
28813 Vnobreak_char_display = Qt;
28814
28815 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28816 doc: /* The pointer shape to show in void text areas.
28817 A value of nil means to show the text pointer. Other options are `arrow',
28818 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28819 Vvoid_text_area_pointer = Qarrow;
28820
28821 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28822 doc: /* Non-nil means don't actually do any redisplay.
28823 This is used for internal purposes. */);
28824 Vinhibit_redisplay = Qnil;
28825
28826 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28827 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28828 Vglobal_mode_string = Qnil;
28829
28830 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28831 doc: /* Marker for where to display an arrow on top of the buffer text.
28832 This must be the beginning of a line in order to work.
28833 See also `overlay-arrow-string'. */);
28834 Voverlay_arrow_position = Qnil;
28835
28836 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28837 doc: /* String to display as an arrow in non-window frames.
28838 See also `overlay-arrow-position'. */);
28839 Voverlay_arrow_string = build_pure_c_string ("=>");
28840
28841 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28842 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28843 The symbols on this list are examined during redisplay to determine
28844 where to display overlay arrows. */);
28845 Voverlay_arrow_variable_list
28846 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28847
28848 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28849 doc: /* The number of lines to try scrolling a window by when point moves out.
28850 If that fails to bring point back on frame, point is centered instead.
28851 If this is zero, point is always centered after it moves off frame.
28852 If you want scrolling to always be a line at a time, you should set
28853 `scroll-conservatively' to a large value rather than set this to 1. */);
28854
28855 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28856 doc: /* Scroll up to this many lines, to bring point back on screen.
28857 If point moves off-screen, redisplay will scroll by up to
28858 `scroll-conservatively' lines in order to bring point just barely
28859 onto the screen again. If that cannot be done, then redisplay
28860 recenters point as usual.
28861
28862 If the value is greater than 100, redisplay will never recenter point,
28863 but will always scroll just enough text to bring point into view, even
28864 if you move far away.
28865
28866 A value of zero means always recenter point if it moves off screen. */);
28867 scroll_conservatively = 0;
28868
28869 DEFVAR_INT ("scroll-margin", scroll_margin,
28870 doc: /* Number of lines of margin at the top and bottom of a window.
28871 Recenter the window whenever point gets within this many lines
28872 of the top or bottom of the window. */);
28873 scroll_margin = 0;
28874
28875 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28876 doc: /* Pixels per inch value for non-window system displays.
28877 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28878 Vdisplay_pixels_per_inch = make_float (72.0);
28879
28880 #ifdef GLYPH_DEBUG
28881 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28882 #endif
28883
28884 DEFVAR_LISP ("truncate-partial-width-windows",
28885 Vtruncate_partial_width_windows,
28886 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28887 For an integer value, truncate lines in each window narrower than the
28888 full frame width, provided the window width is less than that integer;
28889 otherwise, respect the value of `truncate-lines'.
28890
28891 For any other non-nil value, truncate lines in all windows that do
28892 not span the full frame width.
28893
28894 A value of nil means to respect the value of `truncate-lines'.
28895
28896 If `word-wrap' is enabled, you might want to reduce this. */);
28897 Vtruncate_partial_width_windows = make_number (50);
28898
28899 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28900 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28901 Any other value means to use the appropriate face, `mode-line',
28902 `header-line', or `menu' respectively. */);
28903 mode_line_inverse_video = 1;
28904
28905 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28906 doc: /* Maximum buffer size for which line number should be displayed.
28907 If the buffer is bigger than this, the line number does not appear
28908 in the mode line. A value of nil means no limit. */);
28909 Vline_number_display_limit = Qnil;
28910
28911 DEFVAR_INT ("line-number-display-limit-width",
28912 line_number_display_limit_width,
28913 doc: /* Maximum line width (in characters) for line number display.
28914 If the average length of the lines near point is bigger than this, then the
28915 line number may be omitted from the mode line. */);
28916 line_number_display_limit_width = 200;
28917
28918 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28919 doc: /* Non-nil means highlight region even in nonselected windows. */);
28920 highlight_nonselected_windows = 0;
28921
28922 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28923 doc: /* Non-nil if more than one frame is visible on this display.
28924 Minibuffer-only frames don't count, but iconified frames do.
28925 This variable is not guaranteed to be accurate except while processing
28926 `frame-title-format' and `icon-title-format'. */);
28927
28928 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28929 doc: /* Template for displaying the title bar of visible frames.
28930 \(Assuming the window manager supports this feature.)
28931
28932 This variable has the same structure as `mode-line-format', except that
28933 the %c and %l constructs are ignored. It is used only on frames for
28934 which no explicit name has been set \(see `modify-frame-parameters'). */);
28935
28936 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28937 doc: /* Template for displaying the title bar of an iconified frame.
28938 \(Assuming the window manager supports this feature.)
28939 This variable has the same structure as `mode-line-format' (which see),
28940 and is used only on frames for which no explicit name has been set
28941 \(see `modify-frame-parameters'). */);
28942 Vicon_title_format
28943 = Vframe_title_format
28944 = listn (CONSTYPE_PURE, 3,
28945 intern_c_string ("multiple-frames"),
28946 build_pure_c_string ("%b"),
28947 listn (CONSTYPE_PURE, 4,
28948 empty_unibyte_string,
28949 intern_c_string ("invocation-name"),
28950 build_pure_c_string ("@"),
28951 intern_c_string ("system-name")));
28952
28953 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28954 doc: /* Maximum number of lines to keep in the message log buffer.
28955 If nil, disable message logging. If t, log messages but don't truncate
28956 the buffer when it becomes large. */);
28957 Vmessage_log_max = make_number (100);
28958
28959 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28960 doc: /* Functions called before redisplay, if window sizes have changed.
28961 The value should be a list of functions that take one argument.
28962 Just before redisplay, for each frame, if any of its windows have changed
28963 size since the last redisplay, or have been split or deleted,
28964 all the functions in the list are called, with the frame as argument. */);
28965 Vwindow_size_change_functions = Qnil;
28966
28967 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28968 doc: /* List of functions to call before redisplaying a window with scrolling.
28969 Each function is called with two arguments, the window and its new
28970 display-start position. Note that these functions are also called by
28971 `set-window-buffer'. Also note that the value of `window-end' is not
28972 valid when these functions are called.
28973
28974 Warning: Do not use this feature to alter the way the window
28975 is scrolled. It is not designed for that, and such use probably won't
28976 work. */);
28977 Vwindow_scroll_functions = Qnil;
28978
28979 DEFVAR_LISP ("window-text-change-functions",
28980 Vwindow_text_change_functions,
28981 doc: /* Functions to call in redisplay when text in the window might change. */);
28982 Vwindow_text_change_functions = Qnil;
28983
28984 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28985 doc: /* Functions called when redisplay of a window reaches the end trigger.
28986 Each function is called with two arguments, the window and the end trigger value.
28987 See `set-window-redisplay-end-trigger'. */);
28988 Vredisplay_end_trigger_functions = Qnil;
28989
28990 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28991 doc: /* Non-nil means autoselect window with mouse pointer.
28992 If nil, do not autoselect windows.
28993 A positive number means delay autoselection by that many seconds: a
28994 window is autoselected only after the mouse has remained in that
28995 window for the duration of the delay.
28996 A negative number has a similar effect, but causes windows to be
28997 autoselected only after the mouse has stopped moving. \(Because of
28998 the way Emacs compares mouse events, you will occasionally wait twice
28999 that time before the window gets selected.\)
29000 Any other value means to autoselect window instantaneously when the
29001 mouse pointer enters it.
29002
29003 Autoselection selects the minibuffer only if it is active, and never
29004 unselects the minibuffer if it is active.
29005
29006 When customizing this variable make sure that the actual value of
29007 `focus-follows-mouse' matches the behavior of your window manager. */);
29008 Vmouse_autoselect_window = Qnil;
29009
29010 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29011 doc: /* Non-nil means automatically resize tool-bars.
29012 This dynamically changes the tool-bar's height to the minimum height
29013 that is needed to make all tool-bar items visible.
29014 If value is `grow-only', the tool-bar's height is only increased
29015 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29016 Vauto_resize_tool_bars = Qt;
29017
29018 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29019 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29020 auto_raise_tool_bar_buttons_p = 1;
29021
29022 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29023 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29024 make_cursor_line_fully_visible_p = 1;
29025
29026 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29027 doc: /* Border below tool-bar in pixels.
29028 If an integer, use it as the height of the border.
29029 If it is one of `internal-border-width' or `border-width', use the
29030 value of the corresponding frame parameter.
29031 Otherwise, no border is added below the tool-bar. */);
29032 Vtool_bar_border = Qinternal_border_width;
29033
29034 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29035 doc: /* Margin around tool-bar buttons in pixels.
29036 If an integer, use that for both horizontal and vertical margins.
29037 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29038 HORZ specifying the horizontal margin, and VERT specifying the
29039 vertical margin. */);
29040 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29041
29042 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29043 doc: /* Relief thickness of tool-bar buttons. */);
29044 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29045
29046 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29047 doc: /* Tool bar style to use.
29048 It can be one of
29049 image - show images only
29050 text - show text only
29051 both - show both, text below image
29052 both-horiz - show text to the right of the image
29053 text-image-horiz - show text to the left of the image
29054 any other - use system default or image if no system default.
29055
29056 This variable only affects the GTK+ toolkit version of Emacs. */);
29057 Vtool_bar_style = Qnil;
29058
29059 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29060 doc: /* Maximum number of characters a label can have to be shown.
29061 The tool bar style must also show labels for this to have any effect, see
29062 `tool-bar-style'. */);
29063 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29064
29065 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29066 doc: /* List of functions to call to fontify regions of text.
29067 Each function is called with one argument POS. Functions must
29068 fontify a region starting at POS in the current buffer, and give
29069 fontified regions the property `fontified'. */);
29070 Vfontification_functions = Qnil;
29071 Fmake_variable_buffer_local (Qfontification_functions);
29072
29073 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29074 unibyte_display_via_language_environment,
29075 doc: /* Non-nil means display unibyte text according to language environment.
29076 Specifically, this means that raw bytes in the range 160-255 decimal
29077 are displayed by converting them to the equivalent multibyte characters
29078 according to the current language environment. As a result, they are
29079 displayed according to the current fontset.
29080
29081 Note that this variable affects only how these bytes are displayed,
29082 but does not change the fact they are interpreted as raw bytes. */);
29083 unibyte_display_via_language_environment = 0;
29084
29085 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29086 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29087 If a float, it specifies a fraction of the mini-window frame's height.
29088 If an integer, it specifies a number of lines. */);
29089 Vmax_mini_window_height = make_float (0.25);
29090
29091 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29092 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29093 A value of nil means don't automatically resize mini-windows.
29094 A value of t means resize them to fit the text displayed in them.
29095 A value of `grow-only', the default, means let mini-windows grow only;
29096 they return to their normal size when the minibuffer is closed, or the
29097 echo area becomes empty. */);
29098 Vresize_mini_windows = Qgrow_only;
29099
29100 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29101 doc: /* Alist specifying how to blink the cursor off.
29102 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29103 `cursor-type' frame-parameter or variable equals ON-STATE,
29104 comparing using `equal', Emacs uses OFF-STATE to specify
29105 how to blink it off. ON-STATE and OFF-STATE are values for
29106 the `cursor-type' frame parameter.
29107
29108 If a frame's ON-STATE has no entry in this list,
29109 the frame's other specifications determine how to blink the cursor off. */);
29110 Vblink_cursor_alist = Qnil;
29111
29112 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29113 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29114 If non-nil, windows are automatically scrolled horizontally to make
29115 point visible. */);
29116 automatic_hscrolling_p = 1;
29117 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29118
29119 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29120 doc: /* How many columns away from the window edge point is allowed to get
29121 before automatic hscrolling will horizontally scroll the window. */);
29122 hscroll_margin = 5;
29123
29124 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29125 doc: /* How many columns to scroll the window when point gets too close to the edge.
29126 When point is less than `hscroll-margin' columns from the window
29127 edge, automatic hscrolling will scroll the window by the amount of columns
29128 determined by this variable. If its value is a positive integer, scroll that
29129 many columns. If it's a positive floating-point number, it specifies the
29130 fraction of the window's width to scroll. If it's nil or zero, point will be
29131 centered horizontally after the scroll. Any other value, including negative
29132 numbers, are treated as if the value were zero.
29133
29134 Automatic hscrolling always moves point outside the scroll margin, so if
29135 point was more than scroll step columns inside the margin, the window will
29136 scroll more than the value given by the scroll step.
29137
29138 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29139 and `scroll-right' overrides this variable's effect. */);
29140 Vhscroll_step = make_number (0);
29141
29142 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29143 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29144 Bind this around calls to `message' to let it take effect. */);
29145 message_truncate_lines = 0;
29146
29147 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29148 doc: /* Normal hook run to update the menu bar definitions.
29149 Redisplay runs this hook before it redisplays the menu bar.
29150 This is used to update submenus such as Buffers,
29151 whose contents depend on various data. */);
29152 Vmenu_bar_update_hook = Qnil;
29153
29154 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29155 doc: /* Frame for which we are updating a menu.
29156 The enable predicate for a menu binding should check this variable. */);
29157 Vmenu_updating_frame = Qnil;
29158
29159 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29160 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29161 inhibit_menubar_update = 0;
29162
29163 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29164 doc: /* Prefix prepended to all continuation lines at display time.
29165 The value may be a string, an image, or a stretch-glyph; it is
29166 interpreted in the same way as the value of a `display' text property.
29167
29168 This variable is overridden by any `wrap-prefix' text or overlay
29169 property.
29170
29171 To add a prefix to non-continuation lines, use `line-prefix'. */);
29172 Vwrap_prefix = Qnil;
29173 DEFSYM (Qwrap_prefix, "wrap-prefix");
29174 Fmake_variable_buffer_local (Qwrap_prefix);
29175
29176 DEFVAR_LISP ("line-prefix", Vline_prefix,
29177 doc: /* Prefix prepended to all non-continuation lines at display time.
29178 The value may be a string, an image, or a stretch-glyph; it is
29179 interpreted in the same way as the value of a `display' text property.
29180
29181 This variable is overridden by any `line-prefix' text or overlay
29182 property.
29183
29184 To add a prefix to continuation lines, use `wrap-prefix'. */);
29185 Vline_prefix = Qnil;
29186 DEFSYM (Qline_prefix, "line-prefix");
29187 Fmake_variable_buffer_local (Qline_prefix);
29188
29189 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29190 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29191 inhibit_eval_during_redisplay = 0;
29192
29193 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29194 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29195 inhibit_free_realized_faces = 0;
29196
29197 #ifdef GLYPH_DEBUG
29198 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29199 doc: /* Inhibit try_window_id display optimization. */);
29200 inhibit_try_window_id = 0;
29201
29202 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29203 doc: /* Inhibit try_window_reusing display optimization. */);
29204 inhibit_try_window_reusing = 0;
29205
29206 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29207 doc: /* Inhibit try_cursor_movement display optimization. */);
29208 inhibit_try_cursor_movement = 0;
29209 #endif /* GLYPH_DEBUG */
29210
29211 DEFVAR_INT ("overline-margin", overline_margin,
29212 doc: /* Space between overline and text, in pixels.
29213 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29214 margin to the character height. */);
29215 overline_margin = 2;
29216
29217 DEFVAR_INT ("underline-minimum-offset",
29218 underline_minimum_offset,
29219 doc: /* Minimum distance between baseline and underline.
29220 This can improve legibility of underlined text at small font sizes,
29221 particularly when using variable `x-use-underline-position-properties'
29222 with fonts that specify an UNDERLINE_POSITION relatively close to the
29223 baseline. The default value is 1. */);
29224 underline_minimum_offset = 1;
29225
29226 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29227 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29228 This feature only works when on a window system that can change
29229 cursor shapes. */);
29230 display_hourglass_p = 1;
29231
29232 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29233 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29234 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29235
29236 hourglass_atimer = NULL;
29237 hourglass_shown_p = 0;
29238
29239 DEFSYM (Qglyphless_char, "glyphless-char");
29240 DEFSYM (Qhex_code, "hex-code");
29241 DEFSYM (Qempty_box, "empty-box");
29242 DEFSYM (Qthin_space, "thin-space");
29243 DEFSYM (Qzero_width, "zero-width");
29244
29245 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29246 /* Intern this now in case it isn't already done.
29247 Setting this variable twice is harmless.
29248 But don't staticpro it here--that is done in alloc.c. */
29249 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29250 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29251
29252 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29253 doc: /* Char-table defining glyphless characters.
29254 Each element, if non-nil, should be one of the following:
29255 an ASCII acronym string: display this string in a box
29256 `hex-code': display the hexadecimal code of a character in a box
29257 `empty-box': display as an empty box
29258 `thin-space': display as 1-pixel width space
29259 `zero-width': don't display
29260 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29261 display method for graphical terminals and text terminals respectively.
29262 GRAPHICAL and TEXT should each have one of the values listed above.
29263
29264 The char-table has one extra slot to control the display of a character for
29265 which no font is found. This slot only takes effect on graphical terminals.
29266 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29267 `thin-space'. The default is `empty-box'. */);
29268 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29269 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29270 Qempty_box);
29271 }
29272
29273
29274 /* Initialize this module when Emacs starts. */
29275
29276 void
29277 init_xdisp (void)
29278 {
29279 current_header_line_height = current_mode_line_height = -1;
29280
29281 CHARPOS (this_line_start_pos) = 0;
29282
29283 if (!noninteractive)
29284 {
29285 struct window *m = XWINDOW (minibuf_window);
29286 Lisp_Object frame = WVAR (m, frame);
29287 struct frame *f = XFRAME (frame);
29288 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29289 struct window *r = XWINDOW (root);
29290 int i;
29291
29292 echo_area_window = minibuf_window;
29293
29294 XSETFASTINT (WVAR (r, top_line), FRAME_TOP_MARGIN (f));
29295 XSETFASTINT (WVAR (r, total_lines), FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29296 XSETFASTINT (WVAR (r, total_cols), FRAME_COLS (f));
29297 XSETFASTINT (WVAR (m, top_line), FRAME_LINES (f) - 1);
29298 XSETFASTINT (WVAR (m, total_lines), 1);
29299 XSETFASTINT (WVAR (m, total_cols), FRAME_COLS (f));
29300
29301 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29302 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29303 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29304
29305 /* The default ellipsis glyphs `...'. */
29306 for (i = 0; i < 3; ++i)
29307 default_invis_vector[i] = make_number ('.');
29308 }
29309
29310 {
29311 /* Allocate the buffer for frame titles.
29312 Also used for `format-mode-line'. */
29313 int size = 100;
29314 mode_line_noprop_buf = xmalloc (size);
29315 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29316 mode_line_noprop_ptr = mode_line_noprop_buf;
29317 mode_line_target = MODE_LINE_DISPLAY;
29318 }
29319
29320 help_echo_showing_p = 0;
29321 }
29322
29323 /* Since w32 does not support atimers, it defines its own implementation of
29324 the following three functions in w32fns.c. */
29325 #ifndef WINDOWSNT
29326
29327 /* Platform-independent portion of hourglass implementation. */
29328
29329 /* Cancel a currently active hourglass timer, and start a new one. */
29330 void
29331 start_hourglass (void)
29332 {
29333 #if defined (HAVE_WINDOW_SYSTEM)
29334 EMACS_TIME delay;
29335
29336 cancel_hourglass ();
29337
29338 if (INTEGERP (Vhourglass_delay)
29339 && XINT (Vhourglass_delay) > 0)
29340 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29341 TYPE_MAXIMUM (time_t)),
29342 0);
29343 else if (FLOATP (Vhourglass_delay)
29344 && XFLOAT_DATA (Vhourglass_delay) > 0)
29345 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29346 else
29347 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29348
29349 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29350 show_hourglass, NULL);
29351 #endif
29352 }
29353
29354
29355 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29356 shown. */
29357 void
29358 cancel_hourglass (void)
29359 {
29360 #if defined (HAVE_WINDOW_SYSTEM)
29361 if (hourglass_atimer)
29362 {
29363 cancel_atimer (hourglass_atimer);
29364 hourglass_atimer = NULL;
29365 }
29366
29367 if (hourglass_shown_p)
29368 hide_hourglass ();
29369 #endif
29370 }
29371 #endif /* ! WINDOWSNT */