Avoid long futile looping on a TTY under huge values of hscroll.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
277
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT, or the underlying buffer
387 or string character, is a space or a TAB character. This is used
388 to determine where word wrapping can occur. */
389
390 #define IT_DISPLAYING_WHITESPACE(it) \
391 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
392 || ((STRINGP (it->string) \
393 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
394 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
395 || (it->s \
396 && (it->s[IT_BYTEPOS (*it)] == ' ' \
397 || it->s[IT_BYTEPOS (*it)] == '\t')) \
398 || (IT_BYTEPOS (*it) < ZV_BYTE \
399 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
400 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
401
402 /* Name of the face used to highlight trailing whitespace. */
403
404 static Lisp_Object Qtrailing_whitespace;
405
406 /* Name and number of the face used to highlight escape glyphs. */
407
408 static Lisp_Object Qescape_glyph;
409
410 /* Name and number of the face used to highlight non-breaking spaces. */
411
412 static Lisp_Object Qnobreak_space;
413
414 /* The symbol `image' which is the car of the lists used to represent
415 images in Lisp. Also a tool bar style. */
416
417 Lisp_Object Qimage;
418
419 /* The image map types. */
420 Lisp_Object QCmap;
421 static Lisp_Object QCpointer;
422 static Lisp_Object Qrect, Qcircle, Qpoly;
423
424 /* Tool bar styles */
425 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
426
427 /* Non-zero means print newline to stdout before next mini-buffer
428 message. */
429
430 int noninteractive_need_newline;
431
432 /* Non-zero means print newline to message log before next message. */
433
434 static int message_log_need_newline;
435
436 /* Three markers that message_dolog uses.
437 It could allocate them itself, but that causes trouble
438 in handling memory-full errors. */
439 static Lisp_Object message_dolog_marker1;
440 static Lisp_Object message_dolog_marker2;
441 static Lisp_Object message_dolog_marker3;
442 \f
443 /* The buffer position of the first character appearing entirely or
444 partially on the line of the selected window which contains the
445 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
446 redisplay optimization in redisplay_internal. */
447
448 static struct text_pos this_line_start_pos;
449
450 /* Number of characters past the end of the line above, including the
451 terminating newline. */
452
453 static struct text_pos this_line_end_pos;
454
455 /* The vertical positions and the height of this line. */
456
457 static int this_line_vpos;
458 static int this_line_y;
459 static int this_line_pixel_height;
460
461 /* X position at which this display line starts. Usually zero;
462 negative if first character is partially visible. */
463
464 static int this_line_start_x;
465
466 /* The smallest character position seen by move_it_* functions as they
467 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
468 hscrolled lines, see display_line. */
469
470 static struct text_pos this_line_min_pos;
471
472 /* Buffer that this_line_.* variables are referring to. */
473
474 static struct buffer *this_line_buffer;
475
476
477 /* Values of those variables at last redisplay are stored as
478 properties on `overlay-arrow-position' symbol. However, if
479 Voverlay_arrow_position is a marker, last-arrow-position is its
480 numerical position. */
481
482 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
483
484 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
485 properties on a symbol in overlay-arrow-variable-list. */
486
487 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
488
489 Lisp_Object Qmenu_bar_update_hook;
490
491 /* Nonzero if an overlay arrow has been displayed in this window. */
492
493 static int overlay_arrow_seen;
494
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
498
499 int buffer_shared;
500
501 /* Vector containing glyphs for an ellipsis `...'. */
502
503 static Lisp_Object default_invis_vector[3];
504
505 /* This is the window where the echo area message was displayed. It
506 is always a mini-buffer window, but it may not be the same window
507 currently active as a mini-buffer. */
508
509 Lisp_Object echo_area_window;
510
511 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
512 pushes the current message and the value of
513 message_enable_multibyte on the stack, the function restore_message
514 pops the stack and displays MESSAGE again. */
515
516 static Lisp_Object Vmessage_stack;
517
518 /* Nonzero means multibyte characters were enabled when the echo area
519 message was specified. */
520
521 static int message_enable_multibyte;
522
523 /* Nonzero if we should redraw the mode lines on the next redisplay. */
524
525 int update_mode_lines;
526
527 /* Nonzero if window sizes or contents have changed since last
528 redisplay that finished. */
529
530 int windows_or_buffers_changed;
531
532 /* Nonzero means a frame's cursor type has been changed. */
533
534 int cursor_type_changed;
535
536 /* Nonzero after display_mode_line if %l was used and it displayed a
537 line number. */
538
539 static int line_number_displayed;
540
541 /* The name of the *Messages* buffer, a string. */
542
543 static Lisp_Object Vmessages_buffer_name;
544
545 /* Current, index 0, and last displayed echo area message. Either
546 buffers from echo_buffers, or nil to indicate no message. */
547
548 Lisp_Object echo_area_buffer[2];
549
550 /* The buffers referenced from echo_area_buffer. */
551
552 static Lisp_Object echo_buffer[2];
553
554 /* A vector saved used in with_area_buffer to reduce consing. */
555
556 static Lisp_Object Vwith_echo_area_save_vector;
557
558 /* Non-zero means display_echo_area should display the last echo area
559 message again. Set by redisplay_preserve_echo_area. */
560
561 static int display_last_displayed_message_p;
562
563 /* Nonzero if echo area is being used by print; zero if being used by
564 message. */
565
566 static int message_buf_print;
567
568 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
569
570 static Lisp_Object Qinhibit_menubar_update;
571 static Lisp_Object Qmessage_truncate_lines;
572
573 /* Set to 1 in clear_message to make redisplay_internal aware
574 of an emptied echo area. */
575
576 static int message_cleared_p;
577
578 /* A scratch glyph row with contents used for generating truncation
579 glyphs. Also used in direct_output_for_insert. */
580
581 #define MAX_SCRATCH_GLYPHS 100
582 static struct glyph_row scratch_glyph_row;
583 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
584
585 /* Ascent and height of the last line processed by move_it_to. */
586
587 static int last_max_ascent, last_height;
588
589 /* Non-zero if there's a help-echo in the echo area. */
590
591 int help_echo_showing_p;
592
593 /* If >= 0, computed, exact values of mode-line and header-line height
594 to use in the macros CURRENT_MODE_LINE_HEIGHT and
595 CURRENT_HEADER_LINE_HEIGHT. */
596
597 int current_mode_line_height, current_header_line_height;
598
599 /* The maximum distance to look ahead for text properties. Values
600 that are too small let us call compute_char_face and similar
601 functions too often which is expensive. Values that are too large
602 let us call compute_char_face and alike too often because we
603 might not be interested in text properties that far away. */
604
605 #define TEXT_PROP_DISTANCE_LIMIT 100
606
607 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
608 iterator state and later restore it. This is needed because the
609 bidi iterator on bidi.c keeps a stacked cache of its states, which
610 is really a singleton. When we use scratch iterator objects to
611 move around the buffer, we can cause the bidi cache to be pushed or
612 popped, and therefore we need to restore the cache state when we
613 return to the original iterator. */
614 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
615 do { \
616 if (CACHE) \
617 bidi_unshelve_cache (CACHE, 1); \
618 ITCOPY = ITORIG; \
619 CACHE = bidi_shelve_cache (); \
620 } while (0)
621
622 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
623 do { \
624 if (pITORIG != pITCOPY) \
625 *(pITORIG) = *(pITCOPY); \
626 bidi_unshelve_cache (CACHE, 0); \
627 CACHE = NULL; \
628 } while (0)
629
630 #ifdef GLYPH_DEBUG
631
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG defined. */
634
635 int trace_redisplay_p;
636
637 #endif /* GLYPH_DEBUG */
638
639 #ifdef DEBUG_TRACE_MOVE
640 /* Non-zero means trace with TRACE_MOVE to stderr. */
641 int trace_move;
642
643 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
644 #else
645 #define TRACE_MOVE(x) (void) 0
646 #endif
647
648 static Lisp_Object Qauto_hscroll_mode;
649
650 /* Buffer being redisplayed -- for redisplay_window_error. */
651
652 static struct buffer *displayed_buffer;
653
654 /* Value returned from text property handlers (see below). */
655
656 enum prop_handled
657 {
658 HANDLED_NORMALLY,
659 HANDLED_RECOMPUTE_PROPS,
660 HANDLED_OVERLAY_STRING_CONSUMED,
661 HANDLED_RETURN
662 };
663
664 /* A description of text properties that redisplay is interested
665 in. */
666
667 struct props
668 {
669 /* The name of the property. */
670 Lisp_Object *name;
671
672 /* A unique index for the property. */
673 enum prop_idx idx;
674
675 /* A handler function called to set up iterator IT from the property
676 at IT's current position. Value is used to steer handle_stop. */
677 enum prop_handled (*handler) (struct it *it);
678 };
679
680 static enum prop_handled handle_face_prop (struct it *);
681 static enum prop_handled handle_invisible_prop (struct it *);
682 static enum prop_handled handle_display_prop (struct it *);
683 static enum prop_handled handle_composition_prop (struct it *);
684 static enum prop_handled handle_overlay_change (struct it *);
685 static enum prop_handled handle_fontified_prop (struct it *);
686
687 /* Properties handled by iterators. */
688
689 static struct props it_props[] =
690 {
691 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
692 /* Handle `face' before `display' because some sub-properties of
693 `display' need to know the face. */
694 {&Qface, FACE_PROP_IDX, handle_face_prop},
695 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
696 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
697 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
698 {NULL, 0, NULL}
699 };
700
701 /* Value is the position described by X. If X is a marker, value is
702 the marker_position of X. Otherwise, value is X. */
703
704 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
705
706 /* Enumeration returned by some move_it_.* functions internally. */
707
708 enum move_it_result
709 {
710 /* Not used. Undefined value. */
711 MOVE_UNDEFINED,
712
713 /* Move ended at the requested buffer position or ZV. */
714 MOVE_POS_MATCH_OR_ZV,
715
716 /* Move ended at the requested X pixel position. */
717 MOVE_X_REACHED,
718
719 /* Move within a line ended at the end of a line that must be
720 continued. */
721 MOVE_LINE_CONTINUED,
722
723 /* Move within a line ended at the end of a line that would
724 be displayed truncated. */
725 MOVE_LINE_TRUNCATED,
726
727 /* Move within a line ended at a line end. */
728 MOVE_NEWLINE_OR_CR
729 };
730
731 /* This counter is used to clear the face cache every once in a while
732 in redisplay_internal. It is incremented for each redisplay.
733 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
734 cleared. */
735
736 #define CLEAR_FACE_CACHE_COUNT 500
737 static int clear_face_cache_count;
738
739 /* Similarly for the image cache. */
740
741 #ifdef HAVE_WINDOW_SYSTEM
742 #define CLEAR_IMAGE_CACHE_COUNT 101
743 static int clear_image_cache_count;
744
745 /* Null glyph slice */
746 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
747 #endif
748
749 /* Non-zero while redisplay_internal is in progress. */
750
751 int redisplaying_p;
752
753 static Lisp_Object Qinhibit_free_realized_faces;
754 static Lisp_Object Qmode_line_default_help_echo;
755
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
758
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 ptrdiff_t help_echo_pos;
763
764 /* Temporary variable for XTread_socket. */
765
766 Lisp_Object previous_help_echo_string;
767
768 /* Platform-independent portion of hourglass implementation. */
769
770 /* Non-zero means an hourglass cursor is currently shown. */
771 int hourglass_shown_p;
772
773 /* If non-null, an asynchronous timer that, when it expires, displays
774 an hourglass cursor on all frames. */
775 struct atimer *hourglass_atimer;
776
777 /* Name of the face used to display glyphless characters. */
778 Lisp_Object Qglyphless_char;
779
780 /* Symbol for the purpose of Vglyphless_char_display. */
781 static Lisp_Object Qglyphless_char_display;
782
783 /* Method symbols for Vglyphless_char_display. */
784 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
785
786 /* Default pixel width of `thin-space' display method. */
787 #define THIN_SPACE_WIDTH 1
788
789 /* Default number of seconds to wait before displaying an hourglass
790 cursor. */
791 #define DEFAULT_HOURGLASS_DELAY 1
792
793 \f
794 /* Function prototypes. */
795
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, int);
798 static void mark_window_display_accurate_1 (struct window *, int);
799 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
800 static int display_prop_string_p (Lisp_Object, Lisp_Object);
801 static int cursor_row_p (struct glyph_row *);
802 static int redisplay_mode_lines (Lisp_Object, int);
803 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
804
805 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
806
807 static void handle_line_prefix (struct it *);
808
809 static void pint2str (char *, int, ptrdiff_t);
810 static void pint2hrstr (char *, int, ptrdiff_t);
811 static struct text_pos run_window_scroll_functions (Lisp_Object,
812 struct text_pos);
813 static void reconsider_clip_changes (struct window *, struct buffer *);
814 static int text_outside_line_unchanged_p (struct window *,
815 ptrdiff_t, ptrdiff_t);
816 static void store_mode_line_noprop_char (char);
817 static int store_mode_line_noprop (const char *, int, int);
818 static void handle_stop (struct it *);
819 static void handle_stop_backwards (struct it *, ptrdiff_t);
820 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
821 static void ensure_echo_area_buffers (void);
822 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
823 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
824 static int with_echo_area_buffer (struct window *, int,
825 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
826 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
827 static void clear_garbaged_frames (void);
828 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
829 static void pop_message (void);
830 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
831 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
832 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
833 static int display_echo_area (struct window *);
834 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
835 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
836 static Lisp_Object unwind_redisplay (Lisp_Object);
837 static int string_char_and_length (const unsigned char *, int *);
838 static struct text_pos display_prop_end (struct it *, Lisp_Object,
839 struct text_pos);
840 static int compute_window_start_on_continuation_line (struct window *);
841 static Lisp_Object safe_eval_handler (Lisp_Object);
842 static void insert_left_trunc_glyphs (struct it *);
843 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
844 Lisp_Object);
845 static void extend_face_to_end_of_line (struct it *);
846 static int append_space_for_newline (struct it *, int);
847 static int cursor_row_fully_visible_p (struct window *, int, int);
848 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
849 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
850 static int trailing_whitespace_p (ptrdiff_t);
851 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
852 static void push_it (struct it *, struct text_pos *);
853 static void iterate_out_of_display_property (struct it *);
854 static void pop_it (struct it *);
855 static void sync_frame_with_window_matrix_rows (struct window *);
856 static void select_frame_for_redisplay (Lisp_Object);
857 static void redisplay_internal (void);
858 static int echo_area_display (int);
859 static void redisplay_windows (Lisp_Object);
860 static void redisplay_window (Lisp_Object, int);
861 static Lisp_Object redisplay_window_error (Lisp_Object);
862 static Lisp_Object redisplay_window_0 (Lisp_Object);
863 static Lisp_Object redisplay_window_1 (Lisp_Object);
864 static int set_cursor_from_row (struct window *, struct glyph_row *,
865 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
866 int, int);
867 static int update_menu_bar (struct frame *, int, int);
868 static int try_window_reusing_current_matrix (struct window *);
869 static int try_window_id (struct window *);
870 static int display_line (struct it *);
871 static int display_mode_lines (struct window *);
872 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
873 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
874 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
875 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
876 static void display_menu_bar (struct window *);
877 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
878 ptrdiff_t *);
879 static int display_string (const char *, Lisp_Object, Lisp_Object,
880 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
881 static void compute_line_metrics (struct it *);
882 static void run_redisplay_end_trigger_hook (struct it *);
883 static int get_overlay_strings (struct it *, ptrdiff_t);
884 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
885 static void next_overlay_string (struct it *);
886 static void reseat (struct it *, struct text_pos, int);
887 static void reseat_1 (struct it *, struct text_pos, int);
888 static void back_to_previous_visible_line_start (struct it *);
889 void reseat_at_previous_visible_line_start (struct it *);
890 static void reseat_at_next_visible_line_start (struct it *, int);
891 static int next_element_from_ellipsis (struct it *);
892 static int next_element_from_display_vector (struct it *);
893 static int next_element_from_string (struct it *);
894 static int next_element_from_c_string (struct it *);
895 static int next_element_from_buffer (struct it *);
896 static int next_element_from_composition (struct it *);
897 static int next_element_from_image (struct it *);
898 static int next_element_from_stretch (struct it *);
899 static void load_overlay_strings (struct it *, ptrdiff_t);
900 static int init_from_display_pos (struct it *, struct window *,
901 struct display_pos *);
902 static void reseat_to_string (struct it *, const char *,
903 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
904 static int get_next_display_element (struct it *);
905 static enum move_it_result
906 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
907 enum move_operation_enum);
908 void move_it_vertically_backward (struct it *, int);
909 static void init_to_row_start (struct it *, struct window *,
910 struct glyph_row *);
911 static int init_to_row_end (struct it *, struct window *,
912 struct glyph_row *);
913 static void back_to_previous_line_start (struct it *);
914 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
915 static struct text_pos string_pos_nchars_ahead (struct text_pos,
916 Lisp_Object, ptrdiff_t);
917 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
918 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
919 static ptrdiff_t number_of_chars (const char *, int);
920 static void compute_stop_pos (struct it *);
921 static void compute_string_pos (struct text_pos *, struct text_pos,
922 Lisp_Object);
923 static int face_before_or_after_it_pos (struct it *, int);
924 static ptrdiff_t next_overlay_change (ptrdiff_t);
925 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
926 Lisp_Object, struct text_pos *, ptrdiff_t, int);
927 static int handle_single_display_spec (struct it *, Lisp_Object,
928 Lisp_Object, Lisp_Object,
929 struct text_pos *, ptrdiff_t, int, int);
930 static int underlying_face_id (struct it *);
931 static int in_ellipses_for_invisible_text_p (struct display_pos *,
932 struct window *);
933
934 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
935 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
936
937 #ifdef HAVE_WINDOW_SYSTEM
938
939 static void x_consider_frame_title (Lisp_Object);
940 static int tool_bar_lines_needed (struct frame *, int *);
941 static void update_tool_bar (struct frame *, int);
942 static void build_desired_tool_bar_string (struct frame *f);
943 static int redisplay_tool_bar (struct frame *);
944 static void display_tool_bar_line (struct it *, int);
945 static void notice_overwritten_cursor (struct window *,
946 enum glyph_row_area,
947 int, int, int, int);
948 static void append_stretch_glyph (struct it *, Lisp_Object,
949 int, int, int);
950
951
952 #endif /* HAVE_WINDOW_SYSTEM */
953
954 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
955 static int coords_in_mouse_face_p (struct window *, int, int);
956
957
958 \f
959 /***********************************************************************
960 Window display dimensions
961 ***********************************************************************/
962
963 /* Return the bottom boundary y-position for text lines in window W.
964 This is the first y position at which a line cannot start.
965 It is relative to the top of the window.
966
967 This is the height of W minus the height of a mode line, if any. */
968
969 int
970 window_text_bottom_y (struct window *w)
971 {
972 int height = WINDOW_TOTAL_HEIGHT (w);
973
974 if (WINDOW_WANTS_MODELINE_P (w))
975 height -= CURRENT_MODE_LINE_HEIGHT (w);
976 return height;
977 }
978
979 /* Return the pixel width of display area AREA of window W. AREA < 0
980 means return the total width of W, not including fringes to
981 the left and right of the window. */
982
983 int
984 window_box_width (struct window *w, int area)
985 {
986 int cols = XFASTINT (w->total_cols);
987 int pixels = 0;
988
989 if (!w->pseudo_window_p)
990 {
991 cols -= WINDOW_SCROLL_BAR_COLS (w);
992
993 if (area == TEXT_AREA)
994 {
995 if (INTEGERP (w->left_margin_cols))
996 cols -= XFASTINT (w->left_margin_cols);
997 if (INTEGERP (w->right_margin_cols))
998 cols -= XFASTINT (w->right_margin_cols);
999 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1000 }
1001 else if (area == LEFT_MARGIN_AREA)
1002 {
1003 cols = (INTEGERP (w->left_margin_cols)
1004 ? XFASTINT (w->left_margin_cols) : 0);
1005 pixels = 0;
1006 }
1007 else if (area == RIGHT_MARGIN_AREA)
1008 {
1009 cols = (INTEGERP (w->right_margin_cols)
1010 ? XFASTINT (w->right_margin_cols) : 0);
1011 pixels = 0;
1012 }
1013 }
1014
1015 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1016 }
1017
1018
1019 /* Return the pixel height of the display area of window W, not
1020 including mode lines of W, if any. */
1021
1022 int
1023 window_box_height (struct window *w)
1024 {
1025 struct frame *f = XFRAME (w->frame);
1026 int height = WINDOW_TOTAL_HEIGHT (w);
1027
1028 eassert (height >= 0);
1029
1030 /* Note: the code below that determines the mode-line/header-line
1031 height is essentially the same as that contained in the macro
1032 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1033 the appropriate glyph row has its `mode_line_p' flag set,
1034 and if it doesn't, uses estimate_mode_line_height instead. */
1035
1036 if (WINDOW_WANTS_MODELINE_P (w))
1037 {
1038 struct glyph_row *ml_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (ml_row && ml_row->mode_line_p)
1043 height -= ml_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1046 }
1047
1048 if (WINDOW_WANTS_HEADER_LINE_P (w))
1049 {
1050 struct glyph_row *hl_row
1051 = (w->current_matrix && w->current_matrix->rows
1052 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1053 : 0);
1054 if (hl_row && hl_row->mode_line_p)
1055 height -= hl_row->height;
1056 else
1057 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1058 }
1059
1060 /* With a very small font and a mode-line that's taller than
1061 default, we might end up with a negative height. */
1062 return max (0, height);
1063 }
1064
1065 /* Return the window-relative coordinate of the left edge of display
1066 area AREA of window W. AREA < 0 means return the left edge of the
1067 whole window, to the right of the left fringe of W. */
1068
1069 int
1070 window_box_left_offset (struct window *w, int area)
1071 {
1072 int x;
1073
1074 if (w->pseudo_window_p)
1075 return 0;
1076
1077 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1078
1079 if (area == TEXT_AREA)
1080 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1081 + window_box_width (w, LEFT_MARGIN_AREA));
1082 else if (area == RIGHT_MARGIN_AREA)
1083 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1084 + window_box_width (w, LEFT_MARGIN_AREA)
1085 + window_box_width (w, TEXT_AREA)
1086 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1087 ? 0
1088 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1089 else if (area == LEFT_MARGIN_AREA
1090 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1091 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1092
1093 return x;
1094 }
1095
1096
1097 /* Return the window-relative coordinate of the right edge of display
1098 area AREA of window W. AREA < 0 means return the right edge of the
1099 whole window, to the left of the right fringe of W. */
1100
1101 int
1102 window_box_right_offset (struct window *w, int area)
1103 {
1104 return window_box_left_offset (w, area) + window_box_width (w, area);
1105 }
1106
1107 /* Return the frame-relative coordinate of the left edge of display
1108 area AREA of window W. AREA < 0 means return the left edge of the
1109 whole window, to the right of the left fringe of W. */
1110
1111 int
1112 window_box_left (struct window *w, int area)
1113 {
1114 struct frame *f = XFRAME (w->frame);
1115 int x;
1116
1117 if (w->pseudo_window_p)
1118 return FRAME_INTERNAL_BORDER_WIDTH (f);
1119
1120 x = (WINDOW_LEFT_EDGE_X (w)
1121 + window_box_left_offset (w, area));
1122
1123 return x;
1124 }
1125
1126
1127 /* Return the frame-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the right edge of the
1129 whole window, to the left of the right fringe of W. */
1130
1131 int
1132 window_box_right (struct window *w, int area)
1133 {
1134 return window_box_left (w, area) + window_box_width (w, area);
1135 }
1136
1137 /* Get the bounding box of the display area AREA of window W, without
1138 mode lines, in frame-relative coordinates. AREA < 0 means the
1139 whole window, not including the left and right fringes of
1140 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1141 coordinates of the upper-left corner of the box. Return in
1142 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1143
1144 void
1145 window_box (struct window *w, int area, int *box_x, int *box_y,
1146 int *box_width, int *box_height)
1147 {
1148 if (box_width)
1149 *box_width = window_box_width (w, area);
1150 if (box_height)
1151 *box_height = window_box_height (w);
1152 if (box_x)
1153 *box_x = window_box_left (w, area);
1154 if (box_y)
1155 {
1156 *box_y = WINDOW_TOP_EDGE_Y (w);
1157 if (WINDOW_WANTS_HEADER_LINE_P (w))
1158 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1159 }
1160 }
1161
1162
1163 /* Get the bounding box of the display area AREA of window W, without
1164 mode lines. AREA < 0 means the whole window, not including the
1165 left and right fringe of the window. Return in *TOP_LEFT_X
1166 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1167 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1168 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1169 box. */
1170
1171 static inline void
1172 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1173 int *bottom_right_x, int *bottom_right_y)
1174 {
1175 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1176 bottom_right_y);
1177 *bottom_right_x += *top_left_x;
1178 *bottom_right_y += *top_left_y;
1179 }
1180
1181
1182 \f
1183 /***********************************************************************
1184 Utilities
1185 ***********************************************************************/
1186
1187 /* Return the bottom y-position of the line the iterator IT is in.
1188 This can modify IT's settings. */
1189
1190 int
1191 line_bottom_y (struct it *it)
1192 {
1193 int line_height = it->max_ascent + it->max_descent;
1194 int line_top_y = it->current_y;
1195
1196 if (line_height == 0)
1197 {
1198 if (last_height)
1199 line_height = last_height;
1200 else if (IT_CHARPOS (*it) < ZV)
1201 {
1202 move_it_by_lines (it, 1);
1203 line_height = (it->max_ascent || it->max_descent
1204 ? it->max_ascent + it->max_descent
1205 : last_height);
1206 }
1207 else
1208 {
1209 struct glyph_row *row = it->glyph_row;
1210
1211 /* Use the default character height. */
1212 it->glyph_row = NULL;
1213 it->what = IT_CHARACTER;
1214 it->c = ' ';
1215 it->len = 1;
1216 PRODUCE_GLYPHS (it);
1217 line_height = it->ascent + it->descent;
1218 it->glyph_row = row;
1219 }
1220 }
1221
1222 return line_top_y + line_height;
1223 }
1224
1225 /* Subroutine of pos_visible_p below. Extracts a display string, if
1226 any, from the display spec given as its argument. */
1227 static Lisp_Object
1228 string_from_display_spec (Lisp_Object spec)
1229 {
1230 if (CONSP (spec))
1231 {
1232 while (CONSP (spec))
1233 {
1234 if (STRINGP (XCAR (spec)))
1235 return XCAR (spec);
1236 spec = XCDR (spec);
1237 }
1238 }
1239 else if (VECTORP (spec))
1240 {
1241 ptrdiff_t i;
1242
1243 for (i = 0; i < ASIZE (spec); i++)
1244 {
1245 if (STRINGP (AREF (spec, i)))
1246 return AREF (spec, i);
1247 }
1248 return Qnil;
1249 }
1250
1251 return spec;
1252 }
1253
1254
1255 /* Limit insanely large values of W->hscroll on frame F to the largest
1256 value that will still prevent first_visible_x and last_visible_x of
1257 'struct it' from overflowing an int. */
1258 static inline int
1259 window_hscroll_limited (struct window *w, struct frame *f)
1260 {
1261 ptrdiff_t window_hscroll = w->hscroll;
1262 int window_text_width = window_box_width (w, TEXT_AREA);
1263 int colwidth = FRAME_COLUMN_WIDTH (f);
1264
1265 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1266 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1267
1268 return window_hscroll;
1269 }
1270
1271 /* Return 1 if position CHARPOS is visible in window W.
1272 CHARPOS < 0 means return info about WINDOW_END position.
1273 If visible, set *X and *Y to pixel coordinates of top left corner.
1274 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1275 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1276
1277 int
1278 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1279 int *rtop, int *rbot, int *rowh, int *vpos)
1280 {
1281 struct it it;
1282 void *itdata = bidi_shelve_cache ();
1283 struct text_pos top;
1284 int visible_p = 0;
1285 struct buffer *old_buffer = NULL;
1286
1287 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1288 return visible_p;
1289
1290 if (XBUFFER (w->buffer) != current_buffer)
1291 {
1292 old_buffer = current_buffer;
1293 set_buffer_internal_1 (XBUFFER (w->buffer));
1294 }
1295
1296 SET_TEXT_POS_FROM_MARKER (top, w->start);
1297 /* Scrolling a minibuffer window via scroll bar when the echo area
1298 shows long text sometimes resets the minibuffer contents behind
1299 our backs. */
1300 if (CHARPOS (top) > ZV)
1301 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1302
1303 /* Compute exact mode line heights. */
1304 if (WINDOW_WANTS_MODELINE_P (w))
1305 current_mode_line_height
1306 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1307 BVAR (current_buffer, mode_line_format));
1308
1309 if (WINDOW_WANTS_HEADER_LINE_P (w))
1310 current_header_line_height
1311 = display_mode_line (w, HEADER_LINE_FACE_ID,
1312 BVAR (current_buffer, header_line_format));
1313
1314 start_display (&it, w, top);
1315 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1316 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1317
1318 if (charpos >= 0
1319 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1320 && IT_CHARPOS (it) >= charpos)
1321 /* When scanning backwards under bidi iteration, move_it_to
1322 stops at or _before_ CHARPOS, because it stops at or to
1323 the _right_ of the character at CHARPOS. */
1324 || (it.bidi_p && it.bidi_it.scan_dir == -1
1325 && IT_CHARPOS (it) <= charpos)))
1326 {
1327 /* We have reached CHARPOS, or passed it. How the call to
1328 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1329 or covered by a display property, move_it_to stops at the end
1330 of the invisible text, to the right of CHARPOS. (ii) If
1331 CHARPOS is in a display vector, move_it_to stops on its last
1332 glyph. */
1333 int top_x = it.current_x;
1334 int top_y = it.current_y;
1335 /* Calling line_bottom_y may change it.method, it.position, etc. */
1336 enum it_method it_method = it.method;
1337 int bottom_y = (last_height = 0, line_bottom_y (&it));
1338 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1339
1340 if (top_y < window_top_y)
1341 visible_p = bottom_y > window_top_y;
1342 else if (top_y < it.last_visible_y)
1343 visible_p = 1;
1344 if (bottom_y >= it.last_visible_y
1345 && it.bidi_p && it.bidi_it.scan_dir == -1
1346 && IT_CHARPOS (it) < charpos)
1347 {
1348 /* When the last line of the window is scanned backwards
1349 under bidi iteration, we could be duped into thinking
1350 that we have passed CHARPOS, when in fact move_it_to
1351 simply stopped short of CHARPOS because it reached
1352 last_visible_y. To see if that's what happened, we call
1353 move_it_to again with a slightly larger vertical limit,
1354 and see if it actually moved vertically; if it did, we
1355 didn't really reach CHARPOS, which is beyond window end. */
1356 struct it save_it = it;
1357 /* Why 10? because we don't know how many canonical lines
1358 will the height of the next line(s) be. So we guess. */
1359 int ten_more_lines =
1360 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1361
1362 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1363 MOVE_TO_POS | MOVE_TO_Y);
1364 if (it.current_y > top_y)
1365 visible_p = 0;
1366
1367 it = save_it;
1368 }
1369 if (visible_p)
1370 {
1371 if (it_method == GET_FROM_DISPLAY_VECTOR)
1372 {
1373 /* We stopped on the last glyph of a display vector.
1374 Try and recompute. Hack alert! */
1375 if (charpos < 2 || top.charpos >= charpos)
1376 top_x = it.glyph_row->x;
1377 else
1378 {
1379 struct it it2;
1380 start_display (&it2, w, top);
1381 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1382 get_next_display_element (&it2);
1383 PRODUCE_GLYPHS (&it2);
1384 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1385 || it2.current_x > it2.last_visible_x)
1386 top_x = it.glyph_row->x;
1387 else
1388 {
1389 top_x = it2.current_x;
1390 top_y = it2.current_y;
1391 }
1392 }
1393 }
1394 else if (IT_CHARPOS (it) != charpos)
1395 {
1396 Lisp_Object cpos = make_number (charpos);
1397 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1398 Lisp_Object string = string_from_display_spec (spec);
1399 int newline_in_string = 0;
1400
1401 if (STRINGP (string))
1402 {
1403 const char *s = SSDATA (string);
1404 const char *e = s + SBYTES (string);
1405 while (s < e)
1406 {
1407 if (*s++ == '\n')
1408 {
1409 newline_in_string = 1;
1410 break;
1411 }
1412 }
1413 }
1414 /* The tricky code below is needed because there's a
1415 discrepancy between move_it_to and how we set cursor
1416 when the display line ends in a newline from a
1417 display string. move_it_to will stop _after_ such
1418 display strings, whereas set_cursor_from_row
1419 conspires with cursor_row_p to place the cursor on
1420 the first glyph produced from the display string. */
1421
1422 /* We have overshoot PT because it is covered by a
1423 display property whose value is a string. If the
1424 string includes embedded newlines, we are also in the
1425 wrong display line. Backtrack to the correct line,
1426 where the display string begins. */
1427 if (newline_in_string)
1428 {
1429 Lisp_Object startpos, endpos;
1430 EMACS_INT start, end;
1431 struct it it3;
1432 int it3_moved;
1433
1434 /* Find the first and the last buffer positions
1435 covered by the display string. */
1436 endpos =
1437 Fnext_single_char_property_change (cpos, Qdisplay,
1438 Qnil, Qnil);
1439 startpos =
1440 Fprevious_single_char_property_change (endpos, Qdisplay,
1441 Qnil, Qnil);
1442 start = XFASTINT (startpos);
1443 end = XFASTINT (endpos);
1444 /* Move to the last buffer position before the
1445 display property. */
1446 start_display (&it3, w, top);
1447 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1448 /* Move forward one more line if the position before
1449 the display string is a newline or if it is the
1450 rightmost character on a line that is
1451 continued or word-wrapped. */
1452 if (it3.method == GET_FROM_BUFFER
1453 && it3.c == '\n')
1454 move_it_by_lines (&it3, 1);
1455 else if (move_it_in_display_line_to (&it3, -1,
1456 it3.current_x
1457 + it3.pixel_width,
1458 MOVE_TO_X)
1459 == MOVE_LINE_CONTINUED)
1460 {
1461 move_it_by_lines (&it3, 1);
1462 /* When we are under word-wrap, the #$@%!
1463 move_it_by_lines moves 2 lines, so we need to
1464 fix that up. */
1465 if (it3.line_wrap == WORD_WRAP)
1466 move_it_by_lines (&it3, -1);
1467 }
1468
1469 /* Record the vertical coordinate of the display
1470 line where we wound up. */
1471 top_y = it3.current_y;
1472 if (it3.bidi_p)
1473 {
1474 /* When characters are reordered for display,
1475 the character displayed to the left of the
1476 display string could be _after_ the display
1477 property in the logical order. Use the
1478 smallest vertical position of these two. */
1479 start_display (&it3, w, top);
1480 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1481 if (it3.current_y < top_y)
1482 top_y = it3.current_y;
1483 }
1484 /* Move from the top of the window to the beginning
1485 of the display line where the display string
1486 begins. */
1487 start_display (&it3, w, top);
1488 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1489 /* If it3_moved stays zero after the 'while' loop
1490 below, that means we already were at a newline
1491 before the loop (e.g., the display string begins
1492 with a newline), so we don't need to (and cannot)
1493 inspect the glyphs of it3.glyph_row, because
1494 PRODUCE_GLYPHS will not produce anything for a
1495 newline, and thus it3.glyph_row stays at its
1496 stale content it got at top of the window. */
1497 it3_moved = 0;
1498 /* Finally, advance the iterator until we hit the
1499 first display element whose character position is
1500 CHARPOS, or until the first newline from the
1501 display string, which signals the end of the
1502 display line. */
1503 while (get_next_display_element (&it3))
1504 {
1505 PRODUCE_GLYPHS (&it3);
1506 if (IT_CHARPOS (it3) == charpos
1507 || ITERATOR_AT_END_OF_LINE_P (&it3))
1508 break;
1509 it3_moved = 1;
1510 set_iterator_to_next (&it3, 0);
1511 }
1512 top_x = it3.current_x - it3.pixel_width;
1513 /* Normally, we would exit the above loop because we
1514 found the display element whose character
1515 position is CHARPOS. For the contingency that we
1516 didn't, and stopped at the first newline from the
1517 display string, move back over the glyphs
1518 produced from the string, until we find the
1519 rightmost glyph not from the string. */
1520 if (it3_moved
1521 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1522 {
1523 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1524 + it3.glyph_row->used[TEXT_AREA];
1525
1526 while (EQ ((g - 1)->object, string))
1527 {
1528 --g;
1529 top_x -= g->pixel_width;
1530 }
1531 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1532 + it3.glyph_row->used[TEXT_AREA]);
1533 }
1534 }
1535 }
1536
1537 *x = top_x;
1538 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1539 *rtop = max (0, window_top_y - top_y);
1540 *rbot = max (0, bottom_y - it.last_visible_y);
1541 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1542 - max (top_y, window_top_y)));
1543 *vpos = it.vpos;
1544 }
1545 }
1546 else
1547 {
1548 /* We were asked to provide info about WINDOW_END. */
1549 struct it it2;
1550 void *it2data = NULL;
1551
1552 SAVE_IT (it2, it, it2data);
1553 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1554 move_it_by_lines (&it, 1);
1555 if (charpos < IT_CHARPOS (it)
1556 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1557 {
1558 visible_p = 1;
1559 RESTORE_IT (&it2, &it2, it2data);
1560 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1561 *x = it2.current_x;
1562 *y = it2.current_y + it2.max_ascent - it2.ascent;
1563 *rtop = max (0, -it2.current_y);
1564 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1565 - it.last_visible_y));
1566 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1567 it.last_visible_y)
1568 - max (it2.current_y,
1569 WINDOW_HEADER_LINE_HEIGHT (w))));
1570 *vpos = it2.vpos;
1571 }
1572 else
1573 bidi_unshelve_cache (it2data, 1);
1574 }
1575 bidi_unshelve_cache (itdata, 0);
1576
1577 if (old_buffer)
1578 set_buffer_internal_1 (old_buffer);
1579
1580 current_header_line_height = current_mode_line_height = -1;
1581
1582 if (visible_p && w->hscroll > 0)
1583 *x -=
1584 window_hscroll_limited (w, WINDOW_XFRAME (w))
1585 * WINDOW_FRAME_COLUMN_WIDTH (w);
1586
1587 #if 0
1588 /* Debugging code. */
1589 if (visible_p)
1590 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1591 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1592 else
1593 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1594 #endif
1595
1596 return visible_p;
1597 }
1598
1599
1600 /* Return the next character from STR. Return in *LEN the length of
1601 the character. This is like STRING_CHAR_AND_LENGTH but never
1602 returns an invalid character. If we find one, we return a `?', but
1603 with the length of the invalid character. */
1604
1605 static inline int
1606 string_char_and_length (const unsigned char *str, int *len)
1607 {
1608 int c;
1609
1610 c = STRING_CHAR_AND_LENGTH (str, *len);
1611 if (!CHAR_VALID_P (c))
1612 /* We may not change the length here because other places in Emacs
1613 don't use this function, i.e. they silently accept invalid
1614 characters. */
1615 c = '?';
1616
1617 return c;
1618 }
1619
1620
1621
1622 /* Given a position POS containing a valid character and byte position
1623 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1624
1625 static struct text_pos
1626 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1627 {
1628 eassert (STRINGP (string) && nchars >= 0);
1629
1630 if (STRING_MULTIBYTE (string))
1631 {
1632 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1633 int len;
1634
1635 while (nchars--)
1636 {
1637 string_char_and_length (p, &len);
1638 p += len;
1639 CHARPOS (pos) += 1;
1640 BYTEPOS (pos) += len;
1641 }
1642 }
1643 else
1644 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1645
1646 return pos;
1647 }
1648
1649
1650 /* Value is the text position, i.e. character and byte position,
1651 for character position CHARPOS in STRING. */
1652
1653 static inline struct text_pos
1654 string_pos (ptrdiff_t charpos, Lisp_Object string)
1655 {
1656 struct text_pos pos;
1657 eassert (STRINGP (string));
1658 eassert (charpos >= 0);
1659 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1660 return pos;
1661 }
1662
1663
1664 /* Value is a text position, i.e. character and byte position, for
1665 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1666 means recognize multibyte characters. */
1667
1668 static struct text_pos
1669 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1670 {
1671 struct text_pos pos;
1672
1673 eassert (s != NULL);
1674 eassert (charpos >= 0);
1675
1676 if (multibyte_p)
1677 {
1678 int len;
1679
1680 SET_TEXT_POS (pos, 0, 0);
1681 while (charpos--)
1682 {
1683 string_char_and_length ((const unsigned char *) s, &len);
1684 s += len;
1685 CHARPOS (pos) += 1;
1686 BYTEPOS (pos) += len;
1687 }
1688 }
1689 else
1690 SET_TEXT_POS (pos, charpos, charpos);
1691
1692 return pos;
1693 }
1694
1695
1696 /* Value is the number of characters in C string S. MULTIBYTE_P
1697 non-zero means recognize multibyte characters. */
1698
1699 static ptrdiff_t
1700 number_of_chars (const char *s, int multibyte_p)
1701 {
1702 ptrdiff_t nchars;
1703
1704 if (multibyte_p)
1705 {
1706 ptrdiff_t rest = strlen (s);
1707 int len;
1708 const unsigned char *p = (const unsigned char *) s;
1709
1710 for (nchars = 0; rest > 0; ++nchars)
1711 {
1712 string_char_and_length (p, &len);
1713 rest -= len, p += len;
1714 }
1715 }
1716 else
1717 nchars = strlen (s);
1718
1719 return nchars;
1720 }
1721
1722
1723 /* Compute byte position NEWPOS->bytepos corresponding to
1724 NEWPOS->charpos. POS is a known position in string STRING.
1725 NEWPOS->charpos must be >= POS.charpos. */
1726
1727 static void
1728 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1729 {
1730 eassert (STRINGP (string));
1731 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1732
1733 if (STRING_MULTIBYTE (string))
1734 *newpos = string_pos_nchars_ahead (pos, string,
1735 CHARPOS (*newpos) - CHARPOS (pos));
1736 else
1737 BYTEPOS (*newpos) = CHARPOS (*newpos);
1738 }
1739
1740 /* EXPORT:
1741 Return an estimation of the pixel height of mode or header lines on
1742 frame F. FACE_ID specifies what line's height to estimate. */
1743
1744 int
1745 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1746 {
1747 #ifdef HAVE_WINDOW_SYSTEM
1748 if (FRAME_WINDOW_P (f))
1749 {
1750 int height = FONT_HEIGHT (FRAME_FONT (f));
1751
1752 /* This function is called so early when Emacs starts that the face
1753 cache and mode line face are not yet initialized. */
1754 if (FRAME_FACE_CACHE (f))
1755 {
1756 struct face *face = FACE_FROM_ID (f, face_id);
1757 if (face)
1758 {
1759 if (face->font)
1760 height = FONT_HEIGHT (face->font);
1761 if (face->box_line_width > 0)
1762 height += 2 * face->box_line_width;
1763 }
1764 }
1765
1766 return height;
1767 }
1768 #endif
1769
1770 return 1;
1771 }
1772
1773 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1774 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1775 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1776 not force the value into range. */
1777
1778 void
1779 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1780 int *x, int *y, NativeRectangle *bounds, int noclip)
1781 {
1782
1783 #ifdef HAVE_WINDOW_SYSTEM
1784 if (FRAME_WINDOW_P (f))
1785 {
1786 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1787 even for negative values. */
1788 if (pix_x < 0)
1789 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1790 if (pix_y < 0)
1791 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1792
1793 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1794 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1795
1796 if (bounds)
1797 STORE_NATIVE_RECT (*bounds,
1798 FRAME_COL_TO_PIXEL_X (f, pix_x),
1799 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1800 FRAME_COLUMN_WIDTH (f) - 1,
1801 FRAME_LINE_HEIGHT (f) - 1);
1802
1803 if (!noclip)
1804 {
1805 if (pix_x < 0)
1806 pix_x = 0;
1807 else if (pix_x > FRAME_TOTAL_COLS (f))
1808 pix_x = FRAME_TOTAL_COLS (f);
1809
1810 if (pix_y < 0)
1811 pix_y = 0;
1812 else if (pix_y > FRAME_LINES (f))
1813 pix_y = FRAME_LINES (f);
1814 }
1815 }
1816 #endif
1817
1818 *x = pix_x;
1819 *y = pix_y;
1820 }
1821
1822
1823 /* Find the glyph under window-relative coordinates X/Y in window W.
1824 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1825 strings. Return in *HPOS and *VPOS the row and column number of
1826 the glyph found. Return in *AREA the glyph area containing X.
1827 Value is a pointer to the glyph found or null if X/Y is not on
1828 text, or we can't tell because W's current matrix is not up to
1829 date. */
1830
1831 static
1832 struct glyph *
1833 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1834 int *dx, int *dy, int *area)
1835 {
1836 struct glyph *glyph, *end;
1837 struct glyph_row *row = NULL;
1838 int x0, i;
1839
1840 /* Find row containing Y. Give up if some row is not enabled. */
1841 for (i = 0; i < w->current_matrix->nrows; ++i)
1842 {
1843 row = MATRIX_ROW (w->current_matrix, i);
1844 if (!row->enabled_p)
1845 return NULL;
1846 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1847 break;
1848 }
1849
1850 *vpos = i;
1851 *hpos = 0;
1852
1853 /* Give up if Y is not in the window. */
1854 if (i == w->current_matrix->nrows)
1855 return NULL;
1856
1857 /* Get the glyph area containing X. */
1858 if (w->pseudo_window_p)
1859 {
1860 *area = TEXT_AREA;
1861 x0 = 0;
1862 }
1863 else
1864 {
1865 if (x < window_box_left_offset (w, TEXT_AREA))
1866 {
1867 *area = LEFT_MARGIN_AREA;
1868 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1869 }
1870 else if (x < window_box_right_offset (w, TEXT_AREA))
1871 {
1872 *area = TEXT_AREA;
1873 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1874 }
1875 else
1876 {
1877 *area = RIGHT_MARGIN_AREA;
1878 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1879 }
1880 }
1881
1882 /* Find glyph containing X. */
1883 glyph = row->glyphs[*area];
1884 end = glyph + row->used[*area];
1885 x -= x0;
1886 while (glyph < end && x >= glyph->pixel_width)
1887 {
1888 x -= glyph->pixel_width;
1889 ++glyph;
1890 }
1891
1892 if (glyph == end)
1893 return NULL;
1894
1895 if (dx)
1896 {
1897 *dx = x;
1898 *dy = y - (row->y + row->ascent - glyph->ascent);
1899 }
1900
1901 *hpos = glyph - row->glyphs[*area];
1902 return glyph;
1903 }
1904
1905 /* Convert frame-relative x/y to coordinates relative to window W.
1906 Takes pseudo-windows into account. */
1907
1908 static void
1909 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1910 {
1911 if (w->pseudo_window_p)
1912 {
1913 /* A pseudo-window is always full-width, and starts at the
1914 left edge of the frame, plus a frame border. */
1915 struct frame *f = XFRAME (w->frame);
1916 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1917 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1918 }
1919 else
1920 {
1921 *x -= WINDOW_LEFT_EDGE_X (w);
1922 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1923 }
1924 }
1925
1926 #ifdef HAVE_WINDOW_SYSTEM
1927
1928 /* EXPORT:
1929 Return in RECTS[] at most N clipping rectangles for glyph string S.
1930 Return the number of stored rectangles. */
1931
1932 int
1933 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1934 {
1935 XRectangle r;
1936
1937 if (n <= 0)
1938 return 0;
1939
1940 if (s->row->full_width_p)
1941 {
1942 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1943 r.x = WINDOW_LEFT_EDGE_X (s->w);
1944 r.width = WINDOW_TOTAL_WIDTH (s->w);
1945
1946 /* Unless displaying a mode or menu bar line, which are always
1947 fully visible, clip to the visible part of the row. */
1948 if (s->w->pseudo_window_p)
1949 r.height = s->row->visible_height;
1950 else
1951 r.height = s->height;
1952 }
1953 else
1954 {
1955 /* This is a text line that may be partially visible. */
1956 r.x = window_box_left (s->w, s->area);
1957 r.width = window_box_width (s->w, s->area);
1958 r.height = s->row->visible_height;
1959 }
1960
1961 if (s->clip_head)
1962 if (r.x < s->clip_head->x)
1963 {
1964 if (r.width >= s->clip_head->x - r.x)
1965 r.width -= s->clip_head->x - r.x;
1966 else
1967 r.width = 0;
1968 r.x = s->clip_head->x;
1969 }
1970 if (s->clip_tail)
1971 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1972 {
1973 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1974 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1975 else
1976 r.width = 0;
1977 }
1978
1979 /* If S draws overlapping rows, it's sufficient to use the top and
1980 bottom of the window for clipping because this glyph string
1981 intentionally draws over other lines. */
1982 if (s->for_overlaps)
1983 {
1984 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1985 r.height = window_text_bottom_y (s->w) - r.y;
1986
1987 /* Alas, the above simple strategy does not work for the
1988 environments with anti-aliased text: if the same text is
1989 drawn onto the same place multiple times, it gets thicker.
1990 If the overlap we are processing is for the erased cursor, we
1991 take the intersection with the rectangle of the cursor. */
1992 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1993 {
1994 XRectangle rc, r_save = r;
1995
1996 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1997 rc.y = s->w->phys_cursor.y;
1998 rc.width = s->w->phys_cursor_width;
1999 rc.height = s->w->phys_cursor_height;
2000
2001 x_intersect_rectangles (&r_save, &rc, &r);
2002 }
2003 }
2004 else
2005 {
2006 /* Don't use S->y for clipping because it doesn't take partially
2007 visible lines into account. For example, it can be negative for
2008 partially visible lines at the top of a window. */
2009 if (!s->row->full_width_p
2010 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2011 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2012 else
2013 r.y = max (0, s->row->y);
2014 }
2015
2016 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2017
2018 /* If drawing the cursor, don't let glyph draw outside its
2019 advertised boundaries. Cleartype does this under some circumstances. */
2020 if (s->hl == DRAW_CURSOR)
2021 {
2022 struct glyph *glyph = s->first_glyph;
2023 int height, max_y;
2024
2025 if (s->x > r.x)
2026 {
2027 r.width -= s->x - r.x;
2028 r.x = s->x;
2029 }
2030 r.width = min (r.width, glyph->pixel_width);
2031
2032 /* If r.y is below window bottom, ensure that we still see a cursor. */
2033 height = min (glyph->ascent + glyph->descent,
2034 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2035 max_y = window_text_bottom_y (s->w) - height;
2036 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2037 if (s->ybase - glyph->ascent > max_y)
2038 {
2039 r.y = max_y;
2040 r.height = height;
2041 }
2042 else
2043 {
2044 /* Don't draw cursor glyph taller than our actual glyph. */
2045 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2046 if (height < r.height)
2047 {
2048 max_y = r.y + r.height;
2049 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2050 r.height = min (max_y - r.y, height);
2051 }
2052 }
2053 }
2054
2055 if (s->row->clip)
2056 {
2057 XRectangle r_save = r;
2058
2059 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2060 r.width = 0;
2061 }
2062
2063 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2064 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2065 {
2066 #ifdef CONVERT_FROM_XRECT
2067 CONVERT_FROM_XRECT (r, *rects);
2068 #else
2069 *rects = r;
2070 #endif
2071 return 1;
2072 }
2073 else
2074 {
2075 /* If we are processing overlapping and allowed to return
2076 multiple clipping rectangles, we exclude the row of the glyph
2077 string from the clipping rectangle. This is to avoid drawing
2078 the same text on the environment with anti-aliasing. */
2079 #ifdef CONVERT_FROM_XRECT
2080 XRectangle rs[2];
2081 #else
2082 XRectangle *rs = rects;
2083 #endif
2084 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2085
2086 if (s->for_overlaps & OVERLAPS_PRED)
2087 {
2088 rs[i] = r;
2089 if (r.y + r.height > row_y)
2090 {
2091 if (r.y < row_y)
2092 rs[i].height = row_y - r.y;
2093 else
2094 rs[i].height = 0;
2095 }
2096 i++;
2097 }
2098 if (s->for_overlaps & OVERLAPS_SUCC)
2099 {
2100 rs[i] = r;
2101 if (r.y < row_y + s->row->visible_height)
2102 {
2103 if (r.y + r.height > row_y + s->row->visible_height)
2104 {
2105 rs[i].y = row_y + s->row->visible_height;
2106 rs[i].height = r.y + r.height - rs[i].y;
2107 }
2108 else
2109 rs[i].height = 0;
2110 }
2111 i++;
2112 }
2113
2114 n = i;
2115 #ifdef CONVERT_FROM_XRECT
2116 for (i = 0; i < n; i++)
2117 CONVERT_FROM_XRECT (rs[i], rects[i]);
2118 #endif
2119 return n;
2120 }
2121 }
2122
2123 /* EXPORT:
2124 Return in *NR the clipping rectangle for glyph string S. */
2125
2126 void
2127 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2128 {
2129 get_glyph_string_clip_rects (s, nr, 1);
2130 }
2131
2132
2133 /* EXPORT:
2134 Return the position and height of the phys cursor in window W.
2135 Set w->phys_cursor_width to width of phys cursor.
2136 */
2137
2138 void
2139 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2140 struct glyph *glyph, int *xp, int *yp, int *heightp)
2141 {
2142 struct frame *f = XFRAME (WINDOW_FRAME (w));
2143 int x, y, wd, h, h0, y0;
2144
2145 /* Compute the width of the rectangle to draw. If on a stretch
2146 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2147 rectangle as wide as the glyph, but use a canonical character
2148 width instead. */
2149 wd = glyph->pixel_width - 1;
2150 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2151 wd++; /* Why? */
2152 #endif
2153
2154 x = w->phys_cursor.x;
2155 if (x < 0)
2156 {
2157 wd += x;
2158 x = 0;
2159 }
2160
2161 if (glyph->type == STRETCH_GLYPH
2162 && !x_stretch_cursor_p)
2163 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2164 w->phys_cursor_width = wd;
2165
2166 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2167
2168 /* If y is below window bottom, ensure that we still see a cursor. */
2169 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2170
2171 h = max (h0, glyph->ascent + glyph->descent);
2172 h0 = min (h0, glyph->ascent + glyph->descent);
2173
2174 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2175 if (y < y0)
2176 {
2177 h = max (h - (y0 - y) + 1, h0);
2178 y = y0 - 1;
2179 }
2180 else
2181 {
2182 y0 = window_text_bottom_y (w) - h0;
2183 if (y > y0)
2184 {
2185 h += y - y0;
2186 y = y0;
2187 }
2188 }
2189
2190 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2191 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2192 *heightp = h;
2193 }
2194
2195 /*
2196 * Remember which glyph the mouse is over.
2197 */
2198
2199 void
2200 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2201 {
2202 Lisp_Object window;
2203 struct window *w;
2204 struct glyph_row *r, *gr, *end_row;
2205 enum window_part part;
2206 enum glyph_row_area area;
2207 int x, y, width, height;
2208
2209 /* Try to determine frame pixel position and size of the glyph under
2210 frame pixel coordinates X/Y on frame F. */
2211
2212 if (!f->glyphs_initialized_p
2213 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2214 NILP (window)))
2215 {
2216 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2217 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2218 goto virtual_glyph;
2219 }
2220
2221 w = XWINDOW (window);
2222 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2223 height = WINDOW_FRAME_LINE_HEIGHT (w);
2224
2225 x = window_relative_x_coord (w, part, gx);
2226 y = gy - WINDOW_TOP_EDGE_Y (w);
2227
2228 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2229 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2230
2231 if (w->pseudo_window_p)
2232 {
2233 area = TEXT_AREA;
2234 part = ON_MODE_LINE; /* Don't adjust margin. */
2235 goto text_glyph;
2236 }
2237
2238 switch (part)
2239 {
2240 case ON_LEFT_MARGIN:
2241 area = LEFT_MARGIN_AREA;
2242 goto text_glyph;
2243
2244 case ON_RIGHT_MARGIN:
2245 area = RIGHT_MARGIN_AREA;
2246 goto text_glyph;
2247
2248 case ON_HEADER_LINE:
2249 case ON_MODE_LINE:
2250 gr = (part == ON_HEADER_LINE
2251 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2252 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2253 gy = gr->y;
2254 area = TEXT_AREA;
2255 goto text_glyph_row_found;
2256
2257 case ON_TEXT:
2258 area = TEXT_AREA;
2259
2260 text_glyph:
2261 gr = 0; gy = 0;
2262 for (; r <= end_row && r->enabled_p; ++r)
2263 if (r->y + r->height > y)
2264 {
2265 gr = r; gy = r->y;
2266 break;
2267 }
2268
2269 text_glyph_row_found:
2270 if (gr && gy <= y)
2271 {
2272 struct glyph *g = gr->glyphs[area];
2273 struct glyph *end = g + gr->used[area];
2274
2275 height = gr->height;
2276 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2277 if (gx + g->pixel_width > x)
2278 break;
2279
2280 if (g < end)
2281 {
2282 if (g->type == IMAGE_GLYPH)
2283 {
2284 /* Don't remember when mouse is over image, as
2285 image may have hot-spots. */
2286 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2287 return;
2288 }
2289 width = g->pixel_width;
2290 }
2291 else
2292 {
2293 /* Use nominal char spacing at end of line. */
2294 x -= gx;
2295 gx += (x / width) * width;
2296 }
2297
2298 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2299 gx += window_box_left_offset (w, area);
2300 }
2301 else
2302 {
2303 /* Use nominal line height at end of window. */
2304 gx = (x / width) * width;
2305 y -= gy;
2306 gy += (y / height) * height;
2307 }
2308 break;
2309
2310 case ON_LEFT_FRINGE:
2311 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2312 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2313 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2314 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2315 goto row_glyph;
2316
2317 case ON_RIGHT_FRINGE:
2318 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2319 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2320 : window_box_right_offset (w, TEXT_AREA));
2321 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2322 goto row_glyph;
2323
2324 case ON_SCROLL_BAR:
2325 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2326 ? 0
2327 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2328 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2329 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2330 : 0)));
2331 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2332
2333 row_glyph:
2334 gr = 0, gy = 0;
2335 for (; r <= end_row && r->enabled_p; ++r)
2336 if (r->y + r->height > y)
2337 {
2338 gr = r; gy = r->y;
2339 break;
2340 }
2341
2342 if (gr && gy <= y)
2343 height = gr->height;
2344 else
2345 {
2346 /* Use nominal line height at end of window. */
2347 y -= gy;
2348 gy += (y / height) * height;
2349 }
2350 break;
2351
2352 default:
2353 ;
2354 virtual_glyph:
2355 /* If there is no glyph under the mouse, then we divide the screen
2356 into a grid of the smallest glyph in the frame, and use that
2357 as our "glyph". */
2358
2359 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2360 round down even for negative values. */
2361 if (gx < 0)
2362 gx -= width - 1;
2363 if (gy < 0)
2364 gy -= height - 1;
2365
2366 gx = (gx / width) * width;
2367 gy = (gy / height) * height;
2368
2369 goto store_rect;
2370 }
2371
2372 gx += WINDOW_LEFT_EDGE_X (w);
2373 gy += WINDOW_TOP_EDGE_Y (w);
2374
2375 store_rect:
2376 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2377
2378 /* Visible feedback for debugging. */
2379 #if 0
2380 #if HAVE_X_WINDOWS
2381 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2382 f->output_data.x->normal_gc,
2383 gx, gy, width, height);
2384 #endif
2385 #endif
2386 }
2387
2388
2389 #endif /* HAVE_WINDOW_SYSTEM */
2390
2391 \f
2392 /***********************************************************************
2393 Lisp form evaluation
2394 ***********************************************************************/
2395
2396 /* Error handler for safe_eval and safe_call. */
2397
2398 static Lisp_Object
2399 safe_eval_handler (Lisp_Object arg)
2400 {
2401 add_to_log ("Error during redisplay: %S", arg, Qnil);
2402 return Qnil;
2403 }
2404
2405
2406 /* Evaluate SEXPR and return the result, or nil if something went
2407 wrong. Prevent redisplay during the evaluation. */
2408
2409 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2410 Return the result, or nil if something went wrong. Prevent
2411 redisplay during the evaluation. */
2412
2413 Lisp_Object
2414 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2415 {
2416 Lisp_Object val;
2417
2418 if (inhibit_eval_during_redisplay)
2419 val = Qnil;
2420 else
2421 {
2422 ptrdiff_t count = SPECPDL_INDEX ();
2423 struct gcpro gcpro1;
2424
2425 GCPRO1 (args[0]);
2426 gcpro1.nvars = nargs;
2427 specbind (Qinhibit_redisplay, Qt);
2428 /* Use Qt to ensure debugger does not run,
2429 so there is no possibility of wanting to redisplay. */
2430 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2431 safe_eval_handler);
2432 UNGCPRO;
2433 val = unbind_to (count, val);
2434 }
2435
2436 return val;
2437 }
2438
2439
2440 /* Call function FN with one argument ARG.
2441 Return the result, or nil if something went wrong. */
2442
2443 Lisp_Object
2444 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2445 {
2446 Lisp_Object args[2];
2447 args[0] = fn;
2448 args[1] = arg;
2449 return safe_call (2, args);
2450 }
2451
2452 static Lisp_Object Qeval;
2453
2454 Lisp_Object
2455 safe_eval (Lisp_Object sexpr)
2456 {
2457 return safe_call1 (Qeval, sexpr);
2458 }
2459
2460 /* Call function FN with one argument ARG.
2461 Return the result, or nil if something went wrong. */
2462
2463 Lisp_Object
2464 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2465 {
2466 Lisp_Object args[3];
2467 args[0] = fn;
2468 args[1] = arg1;
2469 args[2] = arg2;
2470 return safe_call (3, args);
2471 }
2472
2473
2474 \f
2475 /***********************************************************************
2476 Debugging
2477 ***********************************************************************/
2478
2479 #if 0
2480
2481 /* Define CHECK_IT to perform sanity checks on iterators.
2482 This is for debugging. It is too slow to do unconditionally. */
2483
2484 static void
2485 check_it (struct it *it)
2486 {
2487 if (it->method == GET_FROM_STRING)
2488 {
2489 eassert (STRINGP (it->string));
2490 eassert (IT_STRING_CHARPOS (*it) >= 0);
2491 }
2492 else
2493 {
2494 eassert (IT_STRING_CHARPOS (*it) < 0);
2495 if (it->method == GET_FROM_BUFFER)
2496 {
2497 /* Check that character and byte positions agree. */
2498 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2499 }
2500 }
2501
2502 if (it->dpvec)
2503 eassert (it->current.dpvec_index >= 0);
2504 else
2505 eassert (it->current.dpvec_index < 0);
2506 }
2507
2508 #define CHECK_IT(IT) check_it ((IT))
2509
2510 #else /* not 0 */
2511
2512 #define CHECK_IT(IT) (void) 0
2513
2514 #endif /* not 0 */
2515
2516
2517 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2518
2519 /* Check that the window end of window W is what we expect it
2520 to be---the last row in the current matrix displaying text. */
2521
2522 static void
2523 check_window_end (struct window *w)
2524 {
2525 if (!MINI_WINDOW_P (w)
2526 && !NILP (w->window_end_valid))
2527 {
2528 struct glyph_row *row;
2529 eassert ((row = MATRIX_ROW (w->current_matrix,
2530 XFASTINT (w->window_end_vpos)),
2531 !row->enabled_p
2532 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2533 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2534 }
2535 }
2536
2537 #define CHECK_WINDOW_END(W) check_window_end ((W))
2538
2539 #else
2540
2541 #define CHECK_WINDOW_END(W) (void) 0
2542
2543 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2544
2545
2546 \f
2547 /***********************************************************************
2548 Iterator initialization
2549 ***********************************************************************/
2550
2551 /* Initialize IT for displaying current_buffer in window W, starting
2552 at character position CHARPOS. CHARPOS < 0 means that no buffer
2553 position is specified which is useful when the iterator is assigned
2554 a position later. BYTEPOS is the byte position corresponding to
2555 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2556
2557 If ROW is not null, calls to produce_glyphs with IT as parameter
2558 will produce glyphs in that row.
2559
2560 BASE_FACE_ID is the id of a base face to use. It must be one of
2561 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2562 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2563 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2564
2565 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2566 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2567 will be initialized to use the corresponding mode line glyph row of
2568 the desired matrix of W. */
2569
2570 void
2571 init_iterator (struct it *it, struct window *w,
2572 ptrdiff_t charpos, ptrdiff_t bytepos,
2573 struct glyph_row *row, enum face_id base_face_id)
2574 {
2575 int highlight_region_p;
2576 enum face_id remapped_base_face_id = base_face_id;
2577
2578 /* Some precondition checks. */
2579 eassert (w != NULL && it != NULL);
2580 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2581 && charpos <= ZV));
2582
2583 /* If face attributes have been changed since the last redisplay,
2584 free realized faces now because they depend on face definitions
2585 that might have changed. Don't free faces while there might be
2586 desired matrices pending which reference these faces. */
2587 if (face_change_count && !inhibit_free_realized_faces)
2588 {
2589 face_change_count = 0;
2590 free_all_realized_faces (Qnil);
2591 }
2592
2593 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2594 if (! NILP (Vface_remapping_alist))
2595 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2596
2597 /* Use one of the mode line rows of W's desired matrix if
2598 appropriate. */
2599 if (row == NULL)
2600 {
2601 if (base_face_id == MODE_LINE_FACE_ID
2602 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2603 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2604 else if (base_face_id == HEADER_LINE_FACE_ID)
2605 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2606 }
2607
2608 /* Clear IT. */
2609 memset (it, 0, sizeof *it);
2610 it->current.overlay_string_index = -1;
2611 it->current.dpvec_index = -1;
2612 it->base_face_id = remapped_base_face_id;
2613 it->string = Qnil;
2614 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2615 it->paragraph_embedding = L2R;
2616 it->bidi_it.string.lstring = Qnil;
2617 it->bidi_it.string.s = NULL;
2618 it->bidi_it.string.bufpos = 0;
2619
2620 /* The window in which we iterate over current_buffer: */
2621 XSETWINDOW (it->window, w);
2622 it->w = w;
2623 it->f = XFRAME (w->frame);
2624
2625 it->cmp_it.id = -1;
2626
2627 /* Extra space between lines (on window systems only). */
2628 if (base_face_id == DEFAULT_FACE_ID
2629 && FRAME_WINDOW_P (it->f))
2630 {
2631 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2632 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2633 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2634 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2635 * FRAME_LINE_HEIGHT (it->f));
2636 else if (it->f->extra_line_spacing > 0)
2637 it->extra_line_spacing = it->f->extra_line_spacing;
2638 it->max_extra_line_spacing = 0;
2639 }
2640
2641 /* If realized faces have been removed, e.g. because of face
2642 attribute changes of named faces, recompute them. When running
2643 in batch mode, the face cache of the initial frame is null. If
2644 we happen to get called, make a dummy face cache. */
2645 if (FRAME_FACE_CACHE (it->f) == NULL)
2646 init_frame_faces (it->f);
2647 if (FRAME_FACE_CACHE (it->f)->used == 0)
2648 recompute_basic_faces (it->f);
2649
2650 /* Current value of the `slice', `space-width', and 'height' properties. */
2651 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2652 it->space_width = Qnil;
2653 it->font_height = Qnil;
2654 it->override_ascent = -1;
2655
2656 /* Are control characters displayed as `^C'? */
2657 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2658
2659 /* -1 means everything between a CR and the following line end
2660 is invisible. >0 means lines indented more than this value are
2661 invisible. */
2662 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2663 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2664 selective_display)),
2665 PTRDIFF_MAX)
2666 : (!NILP (BVAR (current_buffer, selective_display))
2667 ? -1 : 0));
2668 it->selective_display_ellipsis_p
2669 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2670
2671 /* Display table to use. */
2672 it->dp = window_display_table (w);
2673
2674 /* Are multibyte characters enabled in current_buffer? */
2675 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2676
2677 /* Non-zero if we should highlight the region. */
2678 highlight_region_p
2679 = (!NILP (Vtransient_mark_mode)
2680 && !NILP (BVAR (current_buffer, mark_active))
2681 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2682
2683 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2684 start and end of a visible region in window IT->w. Set both to
2685 -1 to indicate no region. */
2686 if (highlight_region_p
2687 /* Maybe highlight only in selected window. */
2688 && (/* Either show region everywhere. */
2689 highlight_nonselected_windows
2690 /* Or show region in the selected window. */
2691 || w == XWINDOW (selected_window)
2692 /* Or show the region if we are in the mini-buffer and W is
2693 the window the mini-buffer refers to. */
2694 || (MINI_WINDOW_P (XWINDOW (selected_window))
2695 && WINDOWP (minibuf_selected_window)
2696 && w == XWINDOW (minibuf_selected_window))))
2697 {
2698 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2699 it->region_beg_charpos = min (PT, markpos);
2700 it->region_end_charpos = max (PT, markpos);
2701 }
2702 else
2703 it->region_beg_charpos = it->region_end_charpos = -1;
2704
2705 /* Get the position at which the redisplay_end_trigger hook should
2706 be run, if it is to be run at all. */
2707 if (MARKERP (w->redisplay_end_trigger)
2708 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2709 it->redisplay_end_trigger_charpos
2710 = marker_position (w->redisplay_end_trigger);
2711 else if (INTEGERP (w->redisplay_end_trigger))
2712 it->redisplay_end_trigger_charpos =
2713 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2714
2715 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2716
2717 /* Are lines in the display truncated? */
2718 if (base_face_id != DEFAULT_FACE_ID
2719 || it->w->hscroll
2720 || (! WINDOW_FULL_WIDTH_P (it->w)
2721 && ((!NILP (Vtruncate_partial_width_windows)
2722 && !INTEGERP (Vtruncate_partial_width_windows))
2723 || (INTEGERP (Vtruncate_partial_width_windows)
2724 && (WINDOW_TOTAL_COLS (it->w)
2725 < XINT (Vtruncate_partial_width_windows))))))
2726 it->line_wrap = TRUNCATE;
2727 else if (NILP (BVAR (current_buffer, truncate_lines)))
2728 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2729 ? WINDOW_WRAP : WORD_WRAP;
2730 else
2731 it->line_wrap = TRUNCATE;
2732
2733 /* Get dimensions of truncation and continuation glyphs. These are
2734 displayed as fringe bitmaps under X, so we don't need them for such
2735 frames. */
2736 if (!FRAME_WINDOW_P (it->f))
2737 {
2738 if (it->line_wrap == TRUNCATE)
2739 {
2740 /* We will need the truncation glyph. */
2741 eassert (it->glyph_row == NULL);
2742 produce_special_glyphs (it, IT_TRUNCATION);
2743 it->truncation_pixel_width = it->pixel_width;
2744 }
2745 else
2746 {
2747 /* We will need the continuation glyph. */
2748 eassert (it->glyph_row == NULL);
2749 produce_special_glyphs (it, IT_CONTINUATION);
2750 it->continuation_pixel_width = it->pixel_width;
2751 }
2752
2753 /* Reset these values to zero because the produce_special_glyphs
2754 above has changed them. */
2755 it->pixel_width = it->ascent = it->descent = 0;
2756 it->phys_ascent = it->phys_descent = 0;
2757 }
2758
2759 /* Set this after getting the dimensions of truncation and
2760 continuation glyphs, so that we don't produce glyphs when calling
2761 produce_special_glyphs, above. */
2762 it->glyph_row = row;
2763 it->area = TEXT_AREA;
2764
2765 /* Forget any previous info about this row being reversed. */
2766 if (it->glyph_row)
2767 it->glyph_row->reversed_p = 0;
2768
2769 /* Get the dimensions of the display area. The display area
2770 consists of the visible window area plus a horizontally scrolled
2771 part to the left of the window. All x-values are relative to the
2772 start of this total display area. */
2773 if (base_face_id != DEFAULT_FACE_ID)
2774 {
2775 /* Mode lines, menu bar in terminal frames. */
2776 it->first_visible_x = 0;
2777 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2778 }
2779 else
2780 {
2781 it->first_visible_x =
2782 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2783 it->last_visible_x = (it->first_visible_x
2784 + window_box_width (w, TEXT_AREA));
2785
2786 /* If we truncate lines, leave room for the truncator glyph(s) at
2787 the right margin. Otherwise, leave room for the continuation
2788 glyph(s). Truncation and continuation glyphs are not inserted
2789 for window-based redisplay. */
2790 if (!FRAME_WINDOW_P (it->f))
2791 {
2792 if (it->line_wrap == TRUNCATE)
2793 it->last_visible_x -= it->truncation_pixel_width;
2794 else
2795 it->last_visible_x -= it->continuation_pixel_width;
2796 }
2797
2798 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2799 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2800 }
2801
2802 /* Leave room for a border glyph. */
2803 if (!FRAME_WINDOW_P (it->f)
2804 && !WINDOW_RIGHTMOST_P (it->w))
2805 it->last_visible_x -= 1;
2806
2807 it->last_visible_y = window_text_bottom_y (w);
2808
2809 /* For mode lines and alike, arrange for the first glyph having a
2810 left box line if the face specifies a box. */
2811 if (base_face_id != DEFAULT_FACE_ID)
2812 {
2813 struct face *face;
2814
2815 it->face_id = remapped_base_face_id;
2816
2817 /* If we have a boxed mode line, make the first character appear
2818 with a left box line. */
2819 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2820 if (face->box != FACE_NO_BOX)
2821 it->start_of_box_run_p = 1;
2822 }
2823
2824 /* If a buffer position was specified, set the iterator there,
2825 getting overlays and face properties from that position. */
2826 if (charpos >= BUF_BEG (current_buffer))
2827 {
2828 it->end_charpos = ZV;
2829 IT_CHARPOS (*it) = charpos;
2830
2831 /* We will rely on `reseat' to set this up properly, via
2832 handle_face_prop. */
2833 it->face_id = it->base_face_id;
2834
2835 /* Compute byte position if not specified. */
2836 if (bytepos < charpos)
2837 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2838 else
2839 IT_BYTEPOS (*it) = bytepos;
2840
2841 it->start = it->current;
2842 /* Do we need to reorder bidirectional text? Not if this is a
2843 unibyte buffer: by definition, none of the single-byte
2844 characters are strong R2L, so no reordering is needed. And
2845 bidi.c doesn't support unibyte buffers anyway. Also, don't
2846 reorder while we are loading loadup.el, since the tables of
2847 character properties needed for reordering are not yet
2848 available. */
2849 it->bidi_p =
2850 NILP (Vpurify_flag)
2851 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2852 && it->multibyte_p;
2853
2854 /* If we are to reorder bidirectional text, init the bidi
2855 iterator. */
2856 if (it->bidi_p)
2857 {
2858 /* Note the paragraph direction that this buffer wants to
2859 use. */
2860 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2861 Qleft_to_right))
2862 it->paragraph_embedding = L2R;
2863 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2864 Qright_to_left))
2865 it->paragraph_embedding = R2L;
2866 else
2867 it->paragraph_embedding = NEUTRAL_DIR;
2868 bidi_unshelve_cache (NULL, 0);
2869 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2870 &it->bidi_it);
2871 }
2872
2873 /* Compute faces etc. */
2874 reseat (it, it->current.pos, 1);
2875 }
2876
2877 CHECK_IT (it);
2878 }
2879
2880
2881 /* Initialize IT for the display of window W with window start POS. */
2882
2883 void
2884 start_display (struct it *it, struct window *w, struct text_pos pos)
2885 {
2886 struct glyph_row *row;
2887 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2888
2889 row = w->desired_matrix->rows + first_vpos;
2890 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2891 it->first_vpos = first_vpos;
2892
2893 /* Don't reseat to previous visible line start if current start
2894 position is in a string or image. */
2895 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2896 {
2897 int start_at_line_beg_p;
2898 int first_y = it->current_y;
2899
2900 /* If window start is not at a line start, skip forward to POS to
2901 get the correct continuation lines width. */
2902 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2903 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2904 if (!start_at_line_beg_p)
2905 {
2906 int new_x;
2907
2908 reseat_at_previous_visible_line_start (it);
2909 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2910
2911 new_x = it->current_x + it->pixel_width;
2912
2913 /* If lines are continued, this line may end in the middle
2914 of a multi-glyph character (e.g. a control character
2915 displayed as \003, or in the middle of an overlay
2916 string). In this case move_it_to above will not have
2917 taken us to the start of the continuation line but to the
2918 end of the continued line. */
2919 if (it->current_x > 0
2920 && it->line_wrap != TRUNCATE /* Lines are continued. */
2921 && (/* And glyph doesn't fit on the line. */
2922 new_x > it->last_visible_x
2923 /* Or it fits exactly and we're on a window
2924 system frame. */
2925 || (new_x == it->last_visible_x
2926 && FRAME_WINDOW_P (it->f))))
2927 {
2928 if ((it->current.dpvec_index >= 0
2929 || it->current.overlay_string_index >= 0)
2930 /* If we are on a newline from a display vector or
2931 overlay string, then we are already at the end of
2932 a screen line; no need to go to the next line in
2933 that case, as this line is not really continued.
2934 (If we do go to the next line, C-e will not DTRT.) */
2935 && it->c != '\n')
2936 {
2937 set_iterator_to_next (it, 1);
2938 move_it_in_display_line_to (it, -1, -1, 0);
2939 }
2940
2941 it->continuation_lines_width += it->current_x;
2942 }
2943 /* If the character at POS is displayed via a display
2944 vector, move_it_to above stops at the final glyph of
2945 IT->dpvec. To make the caller redisplay that character
2946 again (a.k.a. start at POS), we need to reset the
2947 dpvec_index to the beginning of IT->dpvec. */
2948 else if (it->current.dpvec_index >= 0)
2949 it->current.dpvec_index = 0;
2950
2951 /* We're starting a new display line, not affected by the
2952 height of the continued line, so clear the appropriate
2953 fields in the iterator structure. */
2954 it->max_ascent = it->max_descent = 0;
2955 it->max_phys_ascent = it->max_phys_descent = 0;
2956
2957 it->current_y = first_y;
2958 it->vpos = 0;
2959 it->current_x = it->hpos = 0;
2960 }
2961 }
2962 }
2963
2964
2965 /* Return 1 if POS is a position in ellipses displayed for invisible
2966 text. W is the window we display, for text property lookup. */
2967
2968 static int
2969 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2970 {
2971 Lisp_Object prop, window;
2972 int ellipses_p = 0;
2973 ptrdiff_t charpos = CHARPOS (pos->pos);
2974
2975 /* If POS specifies a position in a display vector, this might
2976 be for an ellipsis displayed for invisible text. We won't
2977 get the iterator set up for delivering that ellipsis unless
2978 we make sure that it gets aware of the invisible text. */
2979 if (pos->dpvec_index >= 0
2980 && pos->overlay_string_index < 0
2981 && CHARPOS (pos->string_pos) < 0
2982 && charpos > BEGV
2983 && (XSETWINDOW (window, w),
2984 prop = Fget_char_property (make_number (charpos),
2985 Qinvisible, window),
2986 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2987 {
2988 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2989 window);
2990 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2991 }
2992
2993 return ellipses_p;
2994 }
2995
2996
2997 /* Initialize IT for stepping through current_buffer in window W,
2998 starting at position POS that includes overlay string and display
2999 vector/ control character translation position information. Value
3000 is zero if there are overlay strings with newlines at POS. */
3001
3002 static int
3003 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3004 {
3005 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3006 int i, overlay_strings_with_newlines = 0;
3007
3008 /* If POS specifies a position in a display vector, this might
3009 be for an ellipsis displayed for invisible text. We won't
3010 get the iterator set up for delivering that ellipsis unless
3011 we make sure that it gets aware of the invisible text. */
3012 if (in_ellipses_for_invisible_text_p (pos, w))
3013 {
3014 --charpos;
3015 bytepos = 0;
3016 }
3017
3018 /* Keep in mind: the call to reseat in init_iterator skips invisible
3019 text, so we might end up at a position different from POS. This
3020 is only a problem when POS is a row start after a newline and an
3021 overlay starts there with an after-string, and the overlay has an
3022 invisible property. Since we don't skip invisible text in
3023 display_line and elsewhere immediately after consuming the
3024 newline before the row start, such a POS will not be in a string,
3025 but the call to init_iterator below will move us to the
3026 after-string. */
3027 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3028
3029 /* This only scans the current chunk -- it should scan all chunks.
3030 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3031 to 16 in 22.1 to make this a lesser problem. */
3032 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3033 {
3034 const char *s = SSDATA (it->overlay_strings[i]);
3035 const char *e = s + SBYTES (it->overlay_strings[i]);
3036
3037 while (s < e && *s != '\n')
3038 ++s;
3039
3040 if (s < e)
3041 {
3042 overlay_strings_with_newlines = 1;
3043 break;
3044 }
3045 }
3046
3047 /* If position is within an overlay string, set up IT to the right
3048 overlay string. */
3049 if (pos->overlay_string_index >= 0)
3050 {
3051 int relative_index;
3052
3053 /* If the first overlay string happens to have a `display'
3054 property for an image, the iterator will be set up for that
3055 image, and we have to undo that setup first before we can
3056 correct the overlay string index. */
3057 if (it->method == GET_FROM_IMAGE)
3058 pop_it (it);
3059
3060 /* We already have the first chunk of overlay strings in
3061 IT->overlay_strings. Load more until the one for
3062 pos->overlay_string_index is in IT->overlay_strings. */
3063 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3064 {
3065 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3066 it->current.overlay_string_index = 0;
3067 while (n--)
3068 {
3069 load_overlay_strings (it, 0);
3070 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3071 }
3072 }
3073
3074 it->current.overlay_string_index = pos->overlay_string_index;
3075 relative_index = (it->current.overlay_string_index
3076 % OVERLAY_STRING_CHUNK_SIZE);
3077 it->string = it->overlay_strings[relative_index];
3078 eassert (STRINGP (it->string));
3079 it->current.string_pos = pos->string_pos;
3080 it->method = GET_FROM_STRING;
3081 }
3082
3083 if (CHARPOS (pos->string_pos) >= 0)
3084 {
3085 /* Recorded position is not in an overlay string, but in another
3086 string. This can only be a string from a `display' property.
3087 IT should already be filled with that string. */
3088 it->current.string_pos = pos->string_pos;
3089 eassert (STRINGP (it->string));
3090 }
3091
3092 /* Restore position in display vector translations, control
3093 character translations or ellipses. */
3094 if (pos->dpvec_index >= 0)
3095 {
3096 if (it->dpvec == NULL)
3097 get_next_display_element (it);
3098 eassert (it->dpvec && it->current.dpvec_index == 0);
3099 it->current.dpvec_index = pos->dpvec_index;
3100 }
3101
3102 CHECK_IT (it);
3103 return !overlay_strings_with_newlines;
3104 }
3105
3106
3107 /* Initialize IT for stepping through current_buffer in window W
3108 starting at ROW->start. */
3109
3110 static void
3111 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3112 {
3113 init_from_display_pos (it, w, &row->start);
3114 it->start = row->start;
3115 it->continuation_lines_width = row->continuation_lines_width;
3116 CHECK_IT (it);
3117 }
3118
3119
3120 /* Initialize IT for stepping through current_buffer in window W
3121 starting in the line following ROW, i.e. starting at ROW->end.
3122 Value is zero if there are overlay strings with newlines at ROW's
3123 end position. */
3124
3125 static int
3126 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3127 {
3128 int success = 0;
3129
3130 if (init_from_display_pos (it, w, &row->end))
3131 {
3132 if (row->continued_p)
3133 it->continuation_lines_width
3134 = row->continuation_lines_width + row->pixel_width;
3135 CHECK_IT (it);
3136 success = 1;
3137 }
3138
3139 return success;
3140 }
3141
3142
3143
3144 \f
3145 /***********************************************************************
3146 Text properties
3147 ***********************************************************************/
3148
3149 /* Called when IT reaches IT->stop_charpos. Handle text property and
3150 overlay changes. Set IT->stop_charpos to the next position where
3151 to stop. */
3152
3153 static void
3154 handle_stop (struct it *it)
3155 {
3156 enum prop_handled handled;
3157 int handle_overlay_change_p;
3158 struct props *p;
3159
3160 it->dpvec = NULL;
3161 it->current.dpvec_index = -1;
3162 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3163 it->ignore_overlay_strings_at_pos_p = 0;
3164 it->ellipsis_p = 0;
3165
3166 /* Use face of preceding text for ellipsis (if invisible) */
3167 if (it->selective_display_ellipsis_p)
3168 it->saved_face_id = it->face_id;
3169
3170 do
3171 {
3172 handled = HANDLED_NORMALLY;
3173
3174 /* Call text property handlers. */
3175 for (p = it_props; p->handler; ++p)
3176 {
3177 handled = p->handler (it);
3178
3179 if (handled == HANDLED_RECOMPUTE_PROPS)
3180 break;
3181 else if (handled == HANDLED_RETURN)
3182 {
3183 /* We still want to show before and after strings from
3184 overlays even if the actual buffer text is replaced. */
3185 if (!handle_overlay_change_p
3186 || it->sp > 1
3187 /* Don't call get_overlay_strings_1 if we already
3188 have overlay strings loaded, because doing so
3189 will load them again and push the iterator state
3190 onto the stack one more time, which is not
3191 expected by the rest of the code that processes
3192 overlay strings. */
3193 || (it->current.overlay_string_index < 0
3194 ? !get_overlay_strings_1 (it, 0, 0)
3195 : 0))
3196 {
3197 if (it->ellipsis_p)
3198 setup_for_ellipsis (it, 0);
3199 /* When handling a display spec, we might load an
3200 empty string. In that case, discard it here. We
3201 used to discard it in handle_single_display_spec,
3202 but that causes get_overlay_strings_1, above, to
3203 ignore overlay strings that we must check. */
3204 if (STRINGP (it->string) && !SCHARS (it->string))
3205 pop_it (it);
3206 return;
3207 }
3208 else if (STRINGP (it->string) && !SCHARS (it->string))
3209 pop_it (it);
3210 else
3211 {
3212 it->ignore_overlay_strings_at_pos_p = 1;
3213 it->string_from_display_prop_p = 0;
3214 it->from_disp_prop_p = 0;
3215 handle_overlay_change_p = 0;
3216 }
3217 handled = HANDLED_RECOMPUTE_PROPS;
3218 break;
3219 }
3220 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3221 handle_overlay_change_p = 0;
3222 }
3223
3224 if (handled != HANDLED_RECOMPUTE_PROPS)
3225 {
3226 /* Don't check for overlay strings below when set to deliver
3227 characters from a display vector. */
3228 if (it->method == GET_FROM_DISPLAY_VECTOR)
3229 handle_overlay_change_p = 0;
3230
3231 /* Handle overlay changes.
3232 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3233 if it finds overlays. */
3234 if (handle_overlay_change_p)
3235 handled = handle_overlay_change (it);
3236 }
3237
3238 if (it->ellipsis_p)
3239 {
3240 setup_for_ellipsis (it, 0);
3241 break;
3242 }
3243 }
3244 while (handled == HANDLED_RECOMPUTE_PROPS);
3245
3246 /* Determine where to stop next. */
3247 if (handled == HANDLED_NORMALLY)
3248 compute_stop_pos (it);
3249 }
3250
3251
3252 /* Compute IT->stop_charpos from text property and overlay change
3253 information for IT's current position. */
3254
3255 static void
3256 compute_stop_pos (struct it *it)
3257 {
3258 register INTERVAL iv, next_iv;
3259 Lisp_Object object, limit, position;
3260 ptrdiff_t charpos, bytepos;
3261
3262 if (STRINGP (it->string))
3263 {
3264 /* Strings are usually short, so don't limit the search for
3265 properties. */
3266 it->stop_charpos = it->end_charpos;
3267 object = it->string;
3268 limit = Qnil;
3269 charpos = IT_STRING_CHARPOS (*it);
3270 bytepos = IT_STRING_BYTEPOS (*it);
3271 }
3272 else
3273 {
3274 ptrdiff_t pos;
3275
3276 /* If end_charpos is out of range for some reason, such as a
3277 misbehaving display function, rationalize it (Bug#5984). */
3278 if (it->end_charpos > ZV)
3279 it->end_charpos = ZV;
3280 it->stop_charpos = it->end_charpos;
3281
3282 /* If next overlay change is in front of the current stop pos
3283 (which is IT->end_charpos), stop there. Note: value of
3284 next_overlay_change is point-max if no overlay change
3285 follows. */
3286 charpos = IT_CHARPOS (*it);
3287 bytepos = IT_BYTEPOS (*it);
3288 pos = next_overlay_change (charpos);
3289 if (pos < it->stop_charpos)
3290 it->stop_charpos = pos;
3291
3292 /* If showing the region, we have to stop at the region
3293 start or end because the face might change there. */
3294 if (it->region_beg_charpos > 0)
3295 {
3296 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3297 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3298 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3299 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3300 }
3301
3302 /* Set up variables for computing the stop position from text
3303 property changes. */
3304 XSETBUFFER (object, current_buffer);
3305 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3306 }
3307
3308 /* Get the interval containing IT's position. Value is a null
3309 interval if there isn't such an interval. */
3310 position = make_number (charpos);
3311 iv = validate_interval_range (object, &position, &position, 0);
3312 if (!NULL_INTERVAL_P (iv))
3313 {
3314 Lisp_Object values_here[LAST_PROP_IDX];
3315 struct props *p;
3316
3317 /* Get properties here. */
3318 for (p = it_props; p->handler; ++p)
3319 values_here[p->idx] = textget (iv->plist, *p->name);
3320
3321 /* Look for an interval following iv that has different
3322 properties. */
3323 for (next_iv = next_interval (iv);
3324 (!NULL_INTERVAL_P (next_iv)
3325 && (NILP (limit)
3326 || XFASTINT (limit) > next_iv->position));
3327 next_iv = next_interval (next_iv))
3328 {
3329 for (p = it_props; p->handler; ++p)
3330 {
3331 Lisp_Object new_value;
3332
3333 new_value = textget (next_iv->plist, *p->name);
3334 if (!EQ (values_here[p->idx], new_value))
3335 break;
3336 }
3337
3338 if (p->handler)
3339 break;
3340 }
3341
3342 if (!NULL_INTERVAL_P (next_iv))
3343 {
3344 if (INTEGERP (limit)
3345 && next_iv->position >= XFASTINT (limit))
3346 /* No text property change up to limit. */
3347 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3348 else
3349 /* Text properties change in next_iv. */
3350 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3351 }
3352 }
3353
3354 if (it->cmp_it.id < 0)
3355 {
3356 ptrdiff_t stoppos = it->end_charpos;
3357
3358 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3359 stoppos = -1;
3360 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3361 stoppos, it->string);
3362 }
3363
3364 eassert (STRINGP (it->string)
3365 || (it->stop_charpos >= BEGV
3366 && it->stop_charpos >= IT_CHARPOS (*it)));
3367 }
3368
3369
3370 /* Return the position of the next overlay change after POS in
3371 current_buffer. Value is point-max if no overlay change
3372 follows. This is like `next-overlay-change' but doesn't use
3373 xmalloc. */
3374
3375 static ptrdiff_t
3376 next_overlay_change (ptrdiff_t pos)
3377 {
3378 ptrdiff_t i, noverlays;
3379 ptrdiff_t endpos;
3380 Lisp_Object *overlays;
3381
3382 /* Get all overlays at the given position. */
3383 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3384
3385 /* If any of these overlays ends before endpos,
3386 use its ending point instead. */
3387 for (i = 0; i < noverlays; ++i)
3388 {
3389 Lisp_Object oend;
3390 ptrdiff_t oendpos;
3391
3392 oend = OVERLAY_END (overlays[i]);
3393 oendpos = OVERLAY_POSITION (oend);
3394 endpos = min (endpos, oendpos);
3395 }
3396
3397 return endpos;
3398 }
3399
3400 /* How many characters forward to search for a display property or
3401 display string. Searching too far forward makes the bidi display
3402 sluggish, especially in small windows. */
3403 #define MAX_DISP_SCAN 250
3404
3405 /* Return the character position of a display string at or after
3406 position specified by POSITION. If no display string exists at or
3407 after POSITION, return ZV. A display string is either an overlay
3408 with `display' property whose value is a string, or a `display'
3409 text property whose value is a string. STRING is data about the
3410 string to iterate; if STRING->lstring is nil, we are iterating a
3411 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3412 on a GUI frame. DISP_PROP is set to zero if we searched
3413 MAX_DISP_SCAN characters forward without finding any display
3414 strings, non-zero otherwise. It is set to 2 if the display string
3415 uses any kind of `(space ...)' spec that will produce a stretch of
3416 white space in the text area. */
3417 ptrdiff_t
3418 compute_display_string_pos (struct text_pos *position,
3419 struct bidi_string_data *string,
3420 int frame_window_p, int *disp_prop)
3421 {
3422 /* OBJECT = nil means current buffer. */
3423 Lisp_Object object =
3424 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3425 Lisp_Object pos, spec, limpos;
3426 int string_p = (string && (STRINGP (string->lstring) || string->s));
3427 ptrdiff_t eob = string_p ? string->schars : ZV;
3428 ptrdiff_t begb = string_p ? 0 : BEGV;
3429 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3430 ptrdiff_t lim =
3431 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3432 struct text_pos tpos;
3433 int rv = 0;
3434
3435 *disp_prop = 1;
3436
3437 if (charpos >= eob
3438 /* We don't support display properties whose values are strings
3439 that have display string properties. */
3440 || string->from_disp_str
3441 /* C strings cannot have display properties. */
3442 || (string->s && !STRINGP (object)))
3443 {
3444 *disp_prop = 0;
3445 return eob;
3446 }
3447
3448 /* If the character at CHARPOS is where the display string begins,
3449 return CHARPOS. */
3450 pos = make_number (charpos);
3451 if (STRINGP (object))
3452 bufpos = string->bufpos;
3453 else
3454 bufpos = charpos;
3455 tpos = *position;
3456 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3457 && (charpos <= begb
3458 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3459 object),
3460 spec))
3461 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3462 frame_window_p)))
3463 {
3464 if (rv == 2)
3465 *disp_prop = 2;
3466 return charpos;
3467 }
3468
3469 /* Look forward for the first character with a `display' property
3470 that will replace the underlying text when displayed. */
3471 limpos = make_number (lim);
3472 do {
3473 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3474 CHARPOS (tpos) = XFASTINT (pos);
3475 if (CHARPOS (tpos) >= lim)
3476 {
3477 *disp_prop = 0;
3478 break;
3479 }
3480 if (STRINGP (object))
3481 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3482 else
3483 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3484 spec = Fget_char_property (pos, Qdisplay, object);
3485 if (!STRINGP (object))
3486 bufpos = CHARPOS (tpos);
3487 } while (NILP (spec)
3488 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3489 bufpos, frame_window_p)));
3490 if (rv == 2)
3491 *disp_prop = 2;
3492
3493 return CHARPOS (tpos);
3494 }
3495
3496 /* Return the character position of the end of the display string that
3497 started at CHARPOS. If there's no display string at CHARPOS,
3498 return -1. A display string is either an overlay with `display'
3499 property whose value is a string or a `display' text property whose
3500 value is a string. */
3501 ptrdiff_t
3502 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3503 {
3504 /* OBJECT = nil means current buffer. */
3505 Lisp_Object object =
3506 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3507 Lisp_Object pos = make_number (charpos);
3508 ptrdiff_t eob =
3509 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3510
3511 if (charpos >= eob || (string->s && !STRINGP (object)))
3512 return eob;
3513
3514 /* It could happen that the display property or overlay was removed
3515 since we found it in compute_display_string_pos above. One way
3516 this can happen is if JIT font-lock was called (through
3517 handle_fontified_prop), and jit-lock-functions remove text
3518 properties or overlays from the portion of buffer that includes
3519 CHARPOS. Muse mode is known to do that, for example. In this
3520 case, we return -1 to the caller, to signal that no display
3521 string is actually present at CHARPOS. See bidi_fetch_char for
3522 how this is handled.
3523
3524 An alternative would be to never look for display properties past
3525 it->stop_charpos. But neither compute_display_string_pos nor
3526 bidi_fetch_char that calls it know or care where the next
3527 stop_charpos is. */
3528 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3529 return -1;
3530
3531 /* Look forward for the first character where the `display' property
3532 changes. */
3533 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3534
3535 return XFASTINT (pos);
3536 }
3537
3538
3539 \f
3540 /***********************************************************************
3541 Fontification
3542 ***********************************************************************/
3543
3544 /* Handle changes in the `fontified' property of the current buffer by
3545 calling hook functions from Qfontification_functions to fontify
3546 regions of text. */
3547
3548 static enum prop_handled
3549 handle_fontified_prop (struct it *it)
3550 {
3551 Lisp_Object prop, pos;
3552 enum prop_handled handled = HANDLED_NORMALLY;
3553
3554 if (!NILP (Vmemory_full))
3555 return handled;
3556
3557 /* Get the value of the `fontified' property at IT's current buffer
3558 position. (The `fontified' property doesn't have a special
3559 meaning in strings.) If the value is nil, call functions from
3560 Qfontification_functions. */
3561 if (!STRINGP (it->string)
3562 && it->s == NULL
3563 && !NILP (Vfontification_functions)
3564 && !NILP (Vrun_hooks)
3565 && (pos = make_number (IT_CHARPOS (*it)),
3566 prop = Fget_char_property (pos, Qfontified, Qnil),
3567 /* Ignore the special cased nil value always present at EOB since
3568 no amount of fontifying will be able to change it. */
3569 NILP (prop) && IT_CHARPOS (*it) < Z))
3570 {
3571 ptrdiff_t count = SPECPDL_INDEX ();
3572 Lisp_Object val;
3573 struct buffer *obuf = current_buffer;
3574 int begv = BEGV, zv = ZV;
3575 int old_clip_changed = current_buffer->clip_changed;
3576
3577 val = Vfontification_functions;
3578 specbind (Qfontification_functions, Qnil);
3579
3580 eassert (it->end_charpos == ZV);
3581
3582 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3583 safe_call1 (val, pos);
3584 else
3585 {
3586 Lisp_Object fns, fn;
3587 struct gcpro gcpro1, gcpro2;
3588
3589 fns = Qnil;
3590 GCPRO2 (val, fns);
3591
3592 for (; CONSP (val); val = XCDR (val))
3593 {
3594 fn = XCAR (val);
3595
3596 if (EQ (fn, Qt))
3597 {
3598 /* A value of t indicates this hook has a local
3599 binding; it means to run the global binding too.
3600 In a global value, t should not occur. If it
3601 does, we must ignore it to avoid an endless
3602 loop. */
3603 for (fns = Fdefault_value (Qfontification_functions);
3604 CONSP (fns);
3605 fns = XCDR (fns))
3606 {
3607 fn = XCAR (fns);
3608 if (!EQ (fn, Qt))
3609 safe_call1 (fn, pos);
3610 }
3611 }
3612 else
3613 safe_call1 (fn, pos);
3614 }
3615
3616 UNGCPRO;
3617 }
3618
3619 unbind_to (count, Qnil);
3620
3621 /* Fontification functions routinely call `save-restriction'.
3622 Normally, this tags clip_changed, which can confuse redisplay
3623 (see discussion in Bug#6671). Since we don't perform any
3624 special handling of fontification changes in the case where
3625 `save-restriction' isn't called, there's no point doing so in
3626 this case either. So, if the buffer's restrictions are
3627 actually left unchanged, reset clip_changed. */
3628 if (obuf == current_buffer)
3629 {
3630 if (begv == BEGV && zv == ZV)
3631 current_buffer->clip_changed = old_clip_changed;
3632 }
3633 /* There isn't much we can reasonably do to protect against
3634 misbehaving fontification, but here's a fig leaf. */
3635 else if (!NILP (BVAR (obuf, name)))
3636 set_buffer_internal_1 (obuf);
3637
3638 /* The fontification code may have added/removed text.
3639 It could do even a lot worse, but let's at least protect against
3640 the most obvious case where only the text past `pos' gets changed',
3641 as is/was done in grep.el where some escapes sequences are turned
3642 into face properties (bug#7876). */
3643 it->end_charpos = ZV;
3644
3645 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3646 something. This avoids an endless loop if they failed to
3647 fontify the text for which reason ever. */
3648 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3649 handled = HANDLED_RECOMPUTE_PROPS;
3650 }
3651
3652 return handled;
3653 }
3654
3655
3656 \f
3657 /***********************************************************************
3658 Faces
3659 ***********************************************************************/
3660
3661 /* Set up iterator IT from face properties at its current position.
3662 Called from handle_stop. */
3663
3664 static enum prop_handled
3665 handle_face_prop (struct it *it)
3666 {
3667 int new_face_id;
3668 ptrdiff_t next_stop;
3669
3670 if (!STRINGP (it->string))
3671 {
3672 new_face_id
3673 = face_at_buffer_position (it->w,
3674 IT_CHARPOS (*it),
3675 it->region_beg_charpos,
3676 it->region_end_charpos,
3677 &next_stop,
3678 (IT_CHARPOS (*it)
3679 + TEXT_PROP_DISTANCE_LIMIT),
3680 0, it->base_face_id);
3681
3682 /* Is this a start of a run of characters with box face?
3683 Caveat: this can be called for a freshly initialized
3684 iterator; face_id is -1 in this case. We know that the new
3685 face will not change until limit, i.e. if the new face has a
3686 box, all characters up to limit will have one. But, as
3687 usual, we don't know whether limit is really the end. */
3688 if (new_face_id != it->face_id)
3689 {
3690 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3691
3692 /* If new face has a box but old face has not, this is
3693 the start of a run of characters with box, i.e. it has
3694 a shadow on the left side. The value of face_id of the
3695 iterator will be -1 if this is the initial call that gets
3696 the face. In this case, we have to look in front of IT's
3697 position and see whether there is a face != new_face_id. */
3698 it->start_of_box_run_p
3699 = (new_face->box != FACE_NO_BOX
3700 && (it->face_id >= 0
3701 || IT_CHARPOS (*it) == BEG
3702 || new_face_id != face_before_it_pos (it)));
3703 it->face_box_p = new_face->box != FACE_NO_BOX;
3704 }
3705 }
3706 else
3707 {
3708 int base_face_id;
3709 ptrdiff_t bufpos;
3710 int i;
3711 Lisp_Object from_overlay
3712 = (it->current.overlay_string_index >= 0
3713 ? it->string_overlays[it->current.overlay_string_index
3714 % OVERLAY_STRING_CHUNK_SIZE]
3715 : Qnil);
3716
3717 /* See if we got to this string directly or indirectly from
3718 an overlay property. That includes the before-string or
3719 after-string of an overlay, strings in display properties
3720 provided by an overlay, their text properties, etc.
3721
3722 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3723 if (! NILP (from_overlay))
3724 for (i = it->sp - 1; i >= 0; i--)
3725 {
3726 if (it->stack[i].current.overlay_string_index >= 0)
3727 from_overlay
3728 = it->string_overlays[it->stack[i].current.overlay_string_index
3729 % OVERLAY_STRING_CHUNK_SIZE];
3730 else if (! NILP (it->stack[i].from_overlay))
3731 from_overlay = it->stack[i].from_overlay;
3732
3733 if (!NILP (from_overlay))
3734 break;
3735 }
3736
3737 if (! NILP (from_overlay))
3738 {
3739 bufpos = IT_CHARPOS (*it);
3740 /* For a string from an overlay, the base face depends
3741 only on text properties and ignores overlays. */
3742 base_face_id
3743 = face_for_overlay_string (it->w,
3744 IT_CHARPOS (*it),
3745 it->region_beg_charpos,
3746 it->region_end_charpos,
3747 &next_stop,
3748 (IT_CHARPOS (*it)
3749 + TEXT_PROP_DISTANCE_LIMIT),
3750 0,
3751 from_overlay);
3752 }
3753 else
3754 {
3755 bufpos = 0;
3756
3757 /* For strings from a `display' property, use the face at
3758 IT's current buffer position as the base face to merge
3759 with, so that overlay strings appear in the same face as
3760 surrounding text, unless they specify their own
3761 faces. */
3762 base_face_id = it->string_from_prefix_prop_p
3763 ? DEFAULT_FACE_ID
3764 : underlying_face_id (it);
3765 }
3766
3767 new_face_id = face_at_string_position (it->w,
3768 it->string,
3769 IT_STRING_CHARPOS (*it),
3770 bufpos,
3771 it->region_beg_charpos,
3772 it->region_end_charpos,
3773 &next_stop,
3774 base_face_id, 0);
3775
3776 /* Is this a start of a run of characters with box? Caveat:
3777 this can be called for a freshly allocated iterator; face_id
3778 is -1 is this case. We know that the new face will not
3779 change until the next check pos, i.e. if the new face has a
3780 box, all characters up to that position will have a
3781 box. But, as usual, we don't know whether that position
3782 is really the end. */
3783 if (new_face_id != it->face_id)
3784 {
3785 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3786 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3787
3788 /* If new face has a box but old face hasn't, this is the
3789 start of a run of characters with box, i.e. it has a
3790 shadow on the left side. */
3791 it->start_of_box_run_p
3792 = new_face->box && (old_face == NULL || !old_face->box);
3793 it->face_box_p = new_face->box != FACE_NO_BOX;
3794 }
3795 }
3796
3797 it->face_id = new_face_id;
3798 return HANDLED_NORMALLY;
3799 }
3800
3801
3802 /* Return the ID of the face ``underlying'' IT's current position,
3803 which is in a string. If the iterator is associated with a
3804 buffer, return the face at IT's current buffer position.
3805 Otherwise, use the iterator's base_face_id. */
3806
3807 static int
3808 underlying_face_id (struct it *it)
3809 {
3810 int face_id = it->base_face_id, i;
3811
3812 eassert (STRINGP (it->string));
3813
3814 for (i = it->sp - 1; i >= 0; --i)
3815 if (NILP (it->stack[i].string))
3816 face_id = it->stack[i].face_id;
3817
3818 return face_id;
3819 }
3820
3821
3822 /* Compute the face one character before or after the current position
3823 of IT, in the visual order. BEFORE_P non-zero means get the face
3824 in front (to the left in L2R paragraphs, to the right in R2L
3825 paragraphs) of IT's screen position. Value is the ID of the face. */
3826
3827 static int
3828 face_before_or_after_it_pos (struct it *it, int before_p)
3829 {
3830 int face_id, limit;
3831 ptrdiff_t next_check_charpos;
3832 struct it it_copy;
3833 void *it_copy_data = NULL;
3834
3835 eassert (it->s == NULL);
3836
3837 if (STRINGP (it->string))
3838 {
3839 ptrdiff_t bufpos, charpos;
3840 int base_face_id;
3841
3842 /* No face change past the end of the string (for the case
3843 we are padding with spaces). No face change before the
3844 string start. */
3845 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3846 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3847 return it->face_id;
3848
3849 if (!it->bidi_p)
3850 {
3851 /* Set charpos to the position before or after IT's current
3852 position, in the logical order, which in the non-bidi
3853 case is the same as the visual order. */
3854 if (before_p)
3855 charpos = IT_STRING_CHARPOS (*it) - 1;
3856 else if (it->what == IT_COMPOSITION)
3857 /* For composition, we must check the character after the
3858 composition. */
3859 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3860 else
3861 charpos = IT_STRING_CHARPOS (*it) + 1;
3862 }
3863 else
3864 {
3865 if (before_p)
3866 {
3867 /* With bidi iteration, the character before the current
3868 in the visual order cannot be found by simple
3869 iteration, because "reverse" reordering is not
3870 supported. Instead, we need to use the move_it_*
3871 family of functions. */
3872 /* Ignore face changes before the first visible
3873 character on this display line. */
3874 if (it->current_x <= it->first_visible_x)
3875 return it->face_id;
3876 SAVE_IT (it_copy, *it, it_copy_data);
3877 /* Implementation note: Since move_it_in_display_line
3878 works in the iterator geometry, and thinks the first
3879 character is always the leftmost, even in R2L lines,
3880 we don't need to distinguish between the R2L and L2R
3881 cases here. */
3882 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3883 it_copy.current_x - 1, MOVE_TO_X);
3884 charpos = IT_STRING_CHARPOS (it_copy);
3885 RESTORE_IT (it, it, it_copy_data);
3886 }
3887 else
3888 {
3889 /* Set charpos to the string position of the character
3890 that comes after IT's current position in the visual
3891 order. */
3892 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3893
3894 it_copy = *it;
3895 while (n--)
3896 bidi_move_to_visually_next (&it_copy.bidi_it);
3897
3898 charpos = it_copy.bidi_it.charpos;
3899 }
3900 }
3901 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3902
3903 if (it->current.overlay_string_index >= 0)
3904 bufpos = IT_CHARPOS (*it);
3905 else
3906 bufpos = 0;
3907
3908 base_face_id = underlying_face_id (it);
3909
3910 /* Get the face for ASCII, or unibyte. */
3911 face_id = face_at_string_position (it->w,
3912 it->string,
3913 charpos,
3914 bufpos,
3915 it->region_beg_charpos,
3916 it->region_end_charpos,
3917 &next_check_charpos,
3918 base_face_id, 0);
3919
3920 /* Correct the face for charsets different from ASCII. Do it
3921 for the multibyte case only. The face returned above is
3922 suitable for unibyte text if IT->string is unibyte. */
3923 if (STRING_MULTIBYTE (it->string))
3924 {
3925 struct text_pos pos1 = string_pos (charpos, it->string);
3926 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3927 int c, len;
3928 struct face *face = FACE_FROM_ID (it->f, face_id);
3929
3930 c = string_char_and_length (p, &len);
3931 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3932 }
3933 }
3934 else
3935 {
3936 struct text_pos pos;
3937
3938 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3939 || (IT_CHARPOS (*it) <= BEGV && before_p))
3940 return it->face_id;
3941
3942 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3943 pos = it->current.pos;
3944
3945 if (!it->bidi_p)
3946 {
3947 if (before_p)
3948 DEC_TEXT_POS (pos, it->multibyte_p);
3949 else
3950 {
3951 if (it->what == IT_COMPOSITION)
3952 {
3953 /* For composition, we must check the position after
3954 the composition. */
3955 pos.charpos += it->cmp_it.nchars;
3956 pos.bytepos += it->len;
3957 }
3958 else
3959 INC_TEXT_POS (pos, it->multibyte_p);
3960 }
3961 }
3962 else
3963 {
3964 if (before_p)
3965 {
3966 /* With bidi iteration, the character before the current
3967 in the visual order cannot be found by simple
3968 iteration, because "reverse" reordering is not
3969 supported. Instead, we need to use the move_it_*
3970 family of functions. */
3971 /* Ignore face changes before the first visible
3972 character on this display line. */
3973 if (it->current_x <= it->first_visible_x)
3974 return it->face_id;
3975 SAVE_IT (it_copy, *it, it_copy_data);
3976 /* Implementation note: Since move_it_in_display_line
3977 works in the iterator geometry, and thinks the first
3978 character is always the leftmost, even in R2L lines,
3979 we don't need to distinguish between the R2L and L2R
3980 cases here. */
3981 move_it_in_display_line (&it_copy, ZV,
3982 it_copy.current_x - 1, MOVE_TO_X);
3983 pos = it_copy.current.pos;
3984 RESTORE_IT (it, it, it_copy_data);
3985 }
3986 else
3987 {
3988 /* Set charpos to the buffer position of the character
3989 that comes after IT's current position in the visual
3990 order. */
3991 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3992
3993 it_copy = *it;
3994 while (n--)
3995 bidi_move_to_visually_next (&it_copy.bidi_it);
3996
3997 SET_TEXT_POS (pos,
3998 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3999 }
4000 }
4001 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4002
4003 /* Determine face for CHARSET_ASCII, or unibyte. */
4004 face_id = face_at_buffer_position (it->w,
4005 CHARPOS (pos),
4006 it->region_beg_charpos,
4007 it->region_end_charpos,
4008 &next_check_charpos,
4009 limit, 0, -1);
4010
4011 /* Correct the face for charsets different from ASCII. Do it
4012 for the multibyte case only. The face returned above is
4013 suitable for unibyte text if current_buffer is unibyte. */
4014 if (it->multibyte_p)
4015 {
4016 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4017 struct face *face = FACE_FROM_ID (it->f, face_id);
4018 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4019 }
4020 }
4021
4022 return face_id;
4023 }
4024
4025
4026 \f
4027 /***********************************************************************
4028 Invisible text
4029 ***********************************************************************/
4030
4031 /* Set up iterator IT from invisible properties at its current
4032 position. Called from handle_stop. */
4033
4034 static enum prop_handled
4035 handle_invisible_prop (struct it *it)
4036 {
4037 enum prop_handled handled = HANDLED_NORMALLY;
4038
4039 if (STRINGP (it->string))
4040 {
4041 Lisp_Object prop, end_charpos, limit, charpos;
4042
4043 /* Get the value of the invisible text property at the
4044 current position. Value will be nil if there is no such
4045 property. */
4046 charpos = make_number (IT_STRING_CHARPOS (*it));
4047 prop = Fget_text_property (charpos, Qinvisible, it->string);
4048
4049 if (!NILP (prop)
4050 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4051 {
4052 ptrdiff_t endpos;
4053
4054 handled = HANDLED_RECOMPUTE_PROPS;
4055
4056 /* Get the position at which the next change of the
4057 invisible text property can be found in IT->string.
4058 Value will be nil if the property value is the same for
4059 all the rest of IT->string. */
4060 XSETINT (limit, SCHARS (it->string));
4061 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4062 it->string, limit);
4063
4064 /* Text at current position is invisible. The next
4065 change in the property is at position end_charpos.
4066 Move IT's current position to that position. */
4067 if (INTEGERP (end_charpos)
4068 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4069 {
4070 struct text_pos old;
4071 ptrdiff_t oldpos;
4072
4073 old = it->current.string_pos;
4074 oldpos = CHARPOS (old);
4075 if (it->bidi_p)
4076 {
4077 if (it->bidi_it.first_elt
4078 && it->bidi_it.charpos < SCHARS (it->string))
4079 bidi_paragraph_init (it->paragraph_embedding,
4080 &it->bidi_it, 1);
4081 /* Bidi-iterate out of the invisible text. */
4082 do
4083 {
4084 bidi_move_to_visually_next (&it->bidi_it);
4085 }
4086 while (oldpos <= it->bidi_it.charpos
4087 && it->bidi_it.charpos < endpos);
4088
4089 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4090 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4091 if (IT_CHARPOS (*it) >= endpos)
4092 it->prev_stop = endpos;
4093 }
4094 else
4095 {
4096 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4097 compute_string_pos (&it->current.string_pos, old, it->string);
4098 }
4099 }
4100 else
4101 {
4102 /* The rest of the string is invisible. If this is an
4103 overlay string, proceed with the next overlay string
4104 or whatever comes and return a character from there. */
4105 if (it->current.overlay_string_index >= 0)
4106 {
4107 next_overlay_string (it);
4108 /* Don't check for overlay strings when we just
4109 finished processing them. */
4110 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4111 }
4112 else
4113 {
4114 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4115 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4116 }
4117 }
4118 }
4119 }
4120 else
4121 {
4122 int invis_p;
4123 ptrdiff_t newpos, next_stop, start_charpos, tem;
4124 Lisp_Object pos, prop, overlay;
4125
4126 /* First of all, is there invisible text at this position? */
4127 tem = start_charpos = IT_CHARPOS (*it);
4128 pos = make_number (tem);
4129 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4130 &overlay);
4131 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4132
4133 /* If we are on invisible text, skip over it. */
4134 if (invis_p && start_charpos < it->end_charpos)
4135 {
4136 /* Record whether we have to display an ellipsis for the
4137 invisible text. */
4138 int display_ellipsis_p = invis_p == 2;
4139
4140 handled = HANDLED_RECOMPUTE_PROPS;
4141
4142 /* Loop skipping over invisible text. The loop is left at
4143 ZV or with IT on the first char being visible again. */
4144 do
4145 {
4146 /* Try to skip some invisible text. Return value is the
4147 position reached which can be equal to where we start
4148 if there is nothing invisible there. This skips both
4149 over invisible text properties and overlays with
4150 invisible property. */
4151 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4152
4153 /* If we skipped nothing at all we weren't at invisible
4154 text in the first place. If everything to the end of
4155 the buffer was skipped, end the loop. */
4156 if (newpos == tem || newpos >= ZV)
4157 invis_p = 0;
4158 else
4159 {
4160 /* We skipped some characters but not necessarily
4161 all there are. Check if we ended up on visible
4162 text. Fget_char_property returns the property of
4163 the char before the given position, i.e. if we
4164 get invis_p = 0, this means that the char at
4165 newpos is visible. */
4166 pos = make_number (newpos);
4167 prop = Fget_char_property (pos, Qinvisible, it->window);
4168 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4169 }
4170
4171 /* If we ended up on invisible text, proceed to
4172 skip starting with next_stop. */
4173 if (invis_p)
4174 tem = next_stop;
4175
4176 /* If there are adjacent invisible texts, don't lose the
4177 second one's ellipsis. */
4178 if (invis_p == 2)
4179 display_ellipsis_p = 1;
4180 }
4181 while (invis_p);
4182
4183 /* The position newpos is now either ZV or on visible text. */
4184 if (it->bidi_p)
4185 {
4186 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4187 int on_newline =
4188 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4189 int after_newline =
4190 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4191
4192 /* If the invisible text ends on a newline or on a
4193 character after a newline, we can avoid the costly,
4194 character by character, bidi iteration to NEWPOS, and
4195 instead simply reseat the iterator there. That's
4196 because all bidi reordering information is tossed at
4197 the newline. This is a big win for modes that hide
4198 complete lines, like Outline, Org, etc. */
4199 if (on_newline || after_newline)
4200 {
4201 struct text_pos tpos;
4202 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4203
4204 SET_TEXT_POS (tpos, newpos, bpos);
4205 reseat_1 (it, tpos, 0);
4206 /* If we reseat on a newline/ZV, we need to prep the
4207 bidi iterator for advancing to the next character
4208 after the newline/EOB, keeping the current paragraph
4209 direction (so that PRODUCE_GLYPHS does TRT wrt
4210 prepending/appending glyphs to a glyph row). */
4211 if (on_newline)
4212 {
4213 it->bidi_it.first_elt = 0;
4214 it->bidi_it.paragraph_dir = pdir;
4215 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4216 it->bidi_it.nchars = 1;
4217 it->bidi_it.ch_len = 1;
4218 }
4219 }
4220 else /* Must use the slow method. */
4221 {
4222 /* With bidi iteration, the region of invisible text
4223 could start and/or end in the middle of a
4224 non-base embedding level. Therefore, we need to
4225 skip invisible text using the bidi iterator,
4226 starting at IT's current position, until we find
4227 ourselves outside of the invisible text.
4228 Skipping invisible text _after_ bidi iteration
4229 avoids affecting the visual order of the
4230 displayed text when invisible properties are
4231 added or removed. */
4232 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4233 {
4234 /* If we were `reseat'ed to a new paragraph,
4235 determine the paragraph base direction. We
4236 need to do it now because
4237 next_element_from_buffer may not have a
4238 chance to do it, if we are going to skip any
4239 text at the beginning, which resets the
4240 FIRST_ELT flag. */
4241 bidi_paragraph_init (it->paragraph_embedding,
4242 &it->bidi_it, 1);
4243 }
4244 do
4245 {
4246 bidi_move_to_visually_next (&it->bidi_it);
4247 }
4248 while (it->stop_charpos <= it->bidi_it.charpos
4249 && it->bidi_it.charpos < newpos);
4250 IT_CHARPOS (*it) = it->bidi_it.charpos;
4251 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4252 /* If we overstepped NEWPOS, record its position in
4253 the iterator, so that we skip invisible text if
4254 later the bidi iteration lands us in the
4255 invisible region again. */
4256 if (IT_CHARPOS (*it) >= newpos)
4257 it->prev_stop = newpos;
4258 }
4259 }
4260 else
4261 {
4262 IT_CHARPOS (*it) = newpos;
4263 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4264 }
4265
4266 /* If there are before-strings at the start of invisible
4267 text, and the text is invisible because of a text
4268 property, arrange to show before-strings because 20.x did
4269 it that way. (If the text is invisible because of an
4270 overlay property instead of a text property, this is
4271 already handled in the overlay code.) */
4272 if (NILP (overlay)
4273 && get_overlay_strings (it, it->stop_charpos))
4274 {
4275 handled = HANDLED_RECOMPUTE_PROPS;
4276 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4277 }
4278 else if (display_ellipsis_p)
4279 {
4280 /* Make sure that the glyphs of the ellipsis will get
4281 correct `charpos' values. If we would not update
4282 it->position here, the glyphs would belong to the
4283 last visible character _before_ the invisible
4284 text, which confuses `set_cursor_from_row'.
4285
4286 We use the last invisible position instead of the
4287 first because this way the cursor is always drawn on
4288 the first "." of the ellipsis, whenever PT is inside
4289 the invisible text. Otherwise the cursor would be
4290 placed _after_ the ellipsis when the point is after the
4291 first invisible character. */
4292 if (!STRINGP (it->object))
4293 {
4294 it->position.charpos = newpos - 1;
4295 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4296 }
4297 it->ellipsis_p = 1;
4298 /* Let the ellipsis display before
4299 considering any properties of the following char.
4300 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4301 handled = HANDLED_RETURN;
4302 }
4303 }
4304 }
4305
4306 return handled;
4307 }
4308
4309
4310 /* Make iterator IT return `...' next.
4311 Replaces LEN characters from buffer. */
4312
4313 static void
4314 setup_for_ellipsis (struct it *it, int len)
4315 {
4316 /* Use the display table definition for `...'. Invalid glyphs
4317 will be handled by the method returning elements from dpvec. */
4318 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4319 {
4320 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4321 it->dpvec = v->contents;
4322 it->dpend = v->contents + v->header.size;
4323 }
4324 else
4325 {
4326 /* Default `...'. */
4327 it->dpvec = default_invis_vector;
4328 it->dpend = default_invis_vector + 3;
4329 }
4330
4331 it->dpvec_char_len = len;
4332 it->current.dpvec_index = 0;
4333 it->dpvec_face_id = -1;
4334
4335 /* Remember the current face id in case glyphs specify faces.
4336 IT's face is restored in set_iterator_to_next.
4337 saved_face_id was set to preceding char's face in handle_stop. */
4338 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4339 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4340
4341 it->method = GET_FROM_DISPLAY_VECTOR;
4342 it->ellipsis_p = 1;
4343 }
4344
4345
4346 \f
4347 /***********************************************************************
4348 'display' property
4349 ***********************************************************************/
4350
4351 /* Set up iterator IT from `display' property at its current position.
4352 Called from handle_stop.
4353 We return HANDLED_RETURN if some part of the display property
4354 overrides the display of the buffer text itself.
4355 Otherwise we return HANDLED_NORMALLY. */
4356
4357 static enum prop_handled
4358 handle_display_prop (struct it *it)
4359 {
4360 Lisp_Object propval, object, overlay;
4361 struct text_pos *position;
4362 ptrdiff_t bufpos;
4363 /* Nonzero if some property replaces the display of the text itself. */
4364 int display_replaced_p = 0;
4365
4366 if (STRINGP (it->string))
4367 {
4368 object = it->string;
4369 position = &it->current.string_pos;
4370 bufpos = CHARPOS (it->current.pos);
4371 }
4372 else
4373 {
4374 XSETWINDOW (object, it->w);
4375 position = &it->current.pos;
4376 bufpos = CHARPOS (*position);
4377 }
4378
4379 /* Reset those iterator values set from display property values. */
4380 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4381 it->space_width = Qnil;
4382 it->font_height = Qnil;
4383 it->voffset = 0;
4384
4385 /* We don't support recursive `display' properties, i.e. string
4386 values that have a string `display' property, that have a string
4387 `display' property etc. */
4388 if (!it->string_from_display_prop_p)
4389 it->area = TEXT_AREA;
4390
4391 propval = get_char_property_and_overlay (make_number (position->charpos),
4392 Qdisplay, object, &overlay);
4393 if (NILP (propval))
4394 return HANDLED_NORMALLY;
4395 /* Now OVERLAY is the overlay that gave us this property, or nil
4396 if it was a text property. */
4397
4398 if (!STRINGP (it->string))
4399 object = it->w->buffer;
4400
4401 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4402 position, bufpos,
4403 FRAME_WINDOW_P (it->f));
4404
4405 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4406 }
4407
4408 /* Subroutine of handle_display_prop. Returns non-zero if the display
4409 specification in SPEC is a replacing specification, i.e. it would
4410 replace the text covered by `display' property with something else,
4411 such as an image or a display string. If SPEC includes any kind or
4412 `(space ...) specification, the value is 2; this is used by
4413 compute_display_string_pos, which see.
4414
4415 See handle_single_display_spec for documentation of arguments.
4416 frame_window_p is non-zero if the window being redisplayed is on a
4417 GUI frame; this argument is used only if IT is NULL, see below.
4418
4419 IT can be NULL, if this is called by the bidi reordering code
4420 through compute_display_string_pos, which see. In that case, this
4421 function only examines SPEC, but does not otherwise "handle" it, in
4422 the sense that it doesn't set up members of IT from the display
4423 spec. */
4424 static int
4425 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4426 Lisp_Object overlay, struct text_pos *position,
4427 ptrdiff_t bufpos, int frame_window_p)
4428 {
4429 int replacing_p = 0;
4430 int rv;
4431
4432 if (CONSP (spec)
4433 /* Simple specifications. */
4434 && !EQ (XCAR (spec), Qimage)
4435 && !EQ (XCAR (spec), Qspace)
4436 && !EQ (XCAR (spec), Qwhen)
4437 && !EQ (XCAR (spec), Qslice)
4438 && !EQ (XCAR (spec), Qspace_width)
4439 && !EQ (XCAR (spec), Qheight)
4440 && !EQ (XCAR (spec), Qraise)
4441 /* Marginal area specifications. */
4442 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4443 && !EQ (XCAR (spec), Qleft_fringe)
4444 && !EQ (XCAR (spec), Qright_fringe)
4445 && !NILP (XCAR (spec)))
4446 {
4447 for (; CONSP (spec); spec = XCDR (spec))
4448 {
4449 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4450 overlay, position, bufpos,
4451 replacing_p, frame_window_p)))
4452 {
4453 replacing_p = rv;
4454 /* If some text in a string is replaced, `position' no
4455 longer points to the position of `object'. */
4456 if (!it || STRINGP (object))
4457 break;
4458 }
4459 }
4460 }
4461 else if (VECTORP (spec))
4462 {
4463 ptrdiff_t i;
4464 for (i = 0; i < ASIZE (spec); ++i)
4465 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4466 overlay, position, bufpos,
4467 replacing_p, frame_window_p)))
4468 {
4469 replacing_p = rv;
4470 /* If some text in a string is replaced, `position' no
4471 longer points to the position of `object'. */
4472 if (!it || STRINGP (object))
4473 break;
4474 }
4475 }
4476 else
4477 {
4478 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4479 position, bufpos, 0,
4480 frame_window_p)))
4481 replacing_p = rv;
4482 }
4483
4484 return replacing_p;
4485 }
4486
4487 /* Value is the position of the end of the `display' property starting
4488 at START_POS in OBJECT. */
4489
4490 static struct text_pos
4491 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4492 {
4493 Lisp_Object end;
4494 struct text_pos end_pos;
4495
4496 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4497 Qdisplay, object, Qnil);
4498 CHARPOS (end_pos) = XFASTINT (end);
4499 if (STRINGP (object))
4500 compute_string_pos (&end_pos, start_pos, it->string);
4501 else
4502 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4503
4504 return end_pos;
4505 }
4506
4507
4508 /* Set up IT from a single `display' property specification SPEC. OBJECT
4509 is the object in which the `display' property was found. *POSITION
4510 is the position in OBJECT at which the `display' property was found.
4511 BUFPOS is the buffer position of OBJECT (different from POSITION if
4512 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4513 previously saw a display specification which already replaced text
4514 display with something else, for example an image; we ignore such
4515 properties after the first one has been processed.
4516
4517 OVERLAY is the overlay this `display' property came from,
4518 or nil if it was a text property.
4519
4520 If SPEC is a `space' or `image' specification, and in some other
4521 cases too, set *POSITION to the position where the `display'
4522 property ends.
4523
4524 If IT is NULL, only examine the property specification in SPEC, but
4525 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4526 is intended to be displayed in a window on a GUI frame.
4527
4528 Value is non-zero if something was found which replaces the display
4529 of buffer or string text. */
4530
4531 static int
4532 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4533 Lisp_Object overlay, struct text_pos *position,
4534 ptrdiff_t bufpos, int display_replaced_p,
4535 int frame_window_p)
4536 {
4537 Lisp_Object form;
4538 Lisp_Object location, value;
4539 struct text_pos start_pos = *position;
4540 int valid_p;
4541
4542 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4543 If the result is non-nil, use VALUE instead of SPEC. */
4544 form = Qt;
4545 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4546 {
4547 spec = XCDR (spec);
4548 if (!CONSP (spec))
4549 return 0;
4550 form = XCAR (spec);
4551 spec = XCDR (spec);
4552 }
4553
4554 if (!NILP (form) && !EQ (form, Qt))
4555 {
4556 ptrdiff_t count = SPECPDL_INDEX ();
4557 struct gcpro gcpro1;
4558
4559 /* Bind `object' to the object having the `display' property, a
4560 buffer or string. Bind `position' to the position in the
4561 object where the property was found, and `buffer-position'
4562 to the current position in the buffer. */
4563
4564 if (NILP (object))
4565 XSETBUFFER (object, current_buffer);
4566 specbind (Qobject, object);
4567 specbind (Qposition, make_number (CHARPOS (*position)));
4568 specbind (Qbuffer_position, make_number (bufpos));
4569 GCPRO1 (form);
4570 form = safe_eval (form);
4571 UNGCPRO;
4572 unbind_to (count, Qnil);
4573 }
4574
4575 if (NILP (form))
4576 return 0;
4577
4578 /* Handle `(height HEIGHT)' specifications. */
4579 if (CONSP (spec)
4580 && EQ (XCAR (spec), Qheight)
4581 && CONSP (XCDR (spec)))
4582 {
4583 if (it)
4584 {
4585 if (!FRAME_WINDOW_P (it->f))
4586 return 0;
4587
4588 it->font_height = XCAR (XCDR (spec));
4589 if (!NILP (it->font_height))
4590 {
4591 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4592 int new_height = -1;
4593
4594 if (CONSP (it->font_height)
4595 && (EQ (XCAR (it->font_height), Qplus)
4596 || EQ (XCAR (it->font_height), Qminus))
4597 && CONSP (XCDR (it->font_height))
4598 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4599 {
4600 /* `(+ N)' or `(- N)' where N is an integer. */
4601 int steps = XINT (XCAR (XCDR (it->font_height)));
4602 if (EQ (XCAR (it->font_height), Qplus))
4603 steps = - steps;
4604 it->face_id = smaller_face (it->f, it->face_id, steps);
4605 }
4606 else if (FUNCTIONP (it->font_height))
4607 {
4608 /* Call function with current height as argument.
4609 Value is the new height. */
4610 Lisp_Object height;
4611 height = safe_call1 (it->font_height,
4612 face->lface[LFACE_HEIGHT_INDEX]);
4613 if (NUMBERP (height))
4614 new_height = XFLOATINT (height);
4615 }
4616 else if (NUMBERP (it->font_height))
4617 {
4618 /* Value is a multiple of the canonical char height. */
4619 struct face *f;
4620
4621 f = FACE_FROM_ID (it->f,
4622 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4623 new_height = (XFLOATINT (it->font_height)
4624 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4625 }
4626 else
4627 {
4628 /* Evaluate IT->font_height with `height' bound to the
4629 current specified height to get the new height. */
4630 ptrdiff_t count = SPECPDL_INDEX ();
4631
4632 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4633 value = safe_eval (it->font_height);
4634 unbind_to (count, Qnil);
4635
4636 if (NUMBERP (value))
4637 new_height = XFLOATINT (value);
4638 }
4639
4640 if (new_height > 0)
4641 it->face_id = face_with_height (it->f, it->face_id, new_height);
4642 }
4643 }
4644
4645 return 0;
4646 }
4647
4648 /* Handle `(space-width WIDTH)'. */
4649 if (CONSP (spec)
4650 && EQ (XCAR (spec), Qspace_width)
4651 && CONSP (XCDR (spec)))
4652 {
4653 if (it)
4654 {
4655 if (!FRAME_WINDOW_P (it->f))
4656 return 0;
4657
4658 value = XCAR (XCDR (spec));
4659 if (NUMBERP (value) && XFLOATINT (value) > 0)
4660 it->space_width = value;
4661 }
4662
4663 return 0;
4664 }
4665
4666 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4667 if (CONSP (spec)
4668 && EQ (XCAR (spec), Qslice))
4669 {
4670 Lisp_Object tem;
4671
4672 if (it)
4673 {
4674 if (!FRAME_WINDOW_P (it->f))
4675 return 0;
4676
4677 if (tem = XCDR (spec), CONSP (tem))
4678 {
4679 it->slice.x = XCAR (tem);
4680 if (tem = XCDR (tem), CONSP (tem))
4681 {
4682 it->slice.y = XCAR (tem);
4683 if (tem = XCDR (tem), CONSP (tem))
4684 {
4685 it->slice.width = XCAR (tem);
4686 if (tem = XCDR (tem), CONSP (tem))
4687 it->slice.height = XCAR (tem);
4688 }
4689 }
4690 }
4691 }
4692
4693 return 0;
4694 }
4695
4696 /* Handle `(raise FACTOR)'. */
4697 if (CONSP (spec)
4698 && EQ (XCAR (spec), Qraise)
4699 && CONSP (XCDR (spec)))
4700 {
4701 if (it)
4702 {
4703 if (!FRAME_WINDOW_P (it->f))
4704 return 0;
4705
4706 #ifdef HAVE_WINDOW_SYSTEM
4707 value = XCAR (XCDR (spec));
4708 if (NUMBERP (value))
4709 {
4710 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4711 it->voffset = - (XFLOATINT (value)
4712 * (FONT_HEIGHT (face->font)));
4713 }
4714 #endif /* HAVE_WINDOW_SYSTEM */
4715 }
4716
4717 return 0;
4718 }
4719
4720 /* Don't handle the other kinds of display specifications
4721 inside a string that we got from a `display' property. */
4722 if (it && it->string_from_display_prop_p)
4723 return 0;
4724
4725 /* Characters having this form of property are not displayed, so
4726 we have to find the end of the property. */
4727 if (it)
4728 {
4729 start_pos = *position;
4730 *position = display_prop_end (it, object, start_pos);
4731 }
4732 value = Qnil;
4733
4734 /* Stop the scan at that end position--we assume that all
4735 text properties change there. */
4736 if (it)
4737 it->stop_charpos = position->charpos;
4738
4739 /* Handle `(left-fringe BITMAP [FACE])'
4740 and `(right-fringe BITMAP [FACE])'. */
4741 if (CONSP (spec)
4742 && (EQ (XCAR (spec), Qleft_fringe)
4743 || EQ (XCAR (spec), Qright_fringe))
4744 && CONSP (XCDR (spec)))
4745 {
4746 int fringe_bitmap;
4747
4748 if (it)
4749 {
4750 if (!FRAME_WINDOW_P (it->f))
4751 /* If we return here, POSITION has been advanced
4752 across the text with this property. */
4753 {
4754 /* Synchronize the bidi iterator with POSITION. This is
4755 needed because we are not going to push the iterator
4756 on behalf of this display property, so there will be
4757 no pop_it call to do this synchronization for us. */
4758 if (it->bidi_p)
4759 {
4760 it->position = *position;
4761 iterate_out_of_display_property (it);
4762 *position = it->position;
4763 }
4764 return 1;
4765 }
4766 }
4767 else if (!frame_window_p)
4768 return 1;
4769
4770 #ifdef HAVE_WINDOW_SYSTEM
4771 value = XCAR (XCDR (spec));
4772 if (!SYMBOLP (value)
4773 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4774 /* If we return here, POSITION has been advanced
4775 across the text with this property. */
4776 {
4777 if (it && it->bidi_p)
4778 {
4779 it->position = *position;
4780 iterate_out_of_display_property (it);
4781 *position = it->position;
4782 }
4783 return 1;
4784 }
4785
4786 if (it)
4787 {
4788 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4789
4790 if (CONSP (XCDR (XCDR (spec))))
4791 {
4792 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4793 int face_id2 = lookup_derived_face (it->f, face_name,
4794 FRINGE_FACE_ID, 0);
4795 if (face_id2 >= 0)
4796 face_id = face_id2;
4797 }
4798
4799 /* Save current settings of IT so that we can restore them
4800 when we are finished with the glyph property value. */
4801 push_it (it, position);
4802
4803 it->area = TEXT_AREA;
4804 it->what = IT_IMAGE;
4805 it->image_id = -1; /* no image */
4806 it->position = start_pos;
4807 it->object = NILP (object) ? it->w->buffer : object;
4808 it->method = GET_FROM_IMAGE;
4809 it->from_overlay = Qnil;
4810 it->face_id = face_id;
4811 it->from_disp_prop_p = 1;
4812
4813 /* Say that we haven't consumed the characters with
4814 `display' property yet. The call to pop_it in
4815 set_iterator_to_next will clean this up. */
4816 *position = start_pos;
4817
4818 if (EQ (XCAR (spec), Qleft_fringe))
4819 {
4820 it->left_user_fringe_bitmap = fringe_bitmap;
4821 it->left_user_fringe_face_id = face_id;
4822 }
4823 else
4824 {
4825 it->right_user_fringe_bitmap = fringe_bitmap;
4826 it->right_user_fringe_face_id = face_id;
4827 }
4828 }
4829 #endif /* HAVE_WINDOW_SYSTEM */
4830 return 1;
4831 }
4832
4833 /* Prepare to handle `((margin left-margin) ...)',
4834 `((margin right-margin) ...)' and `((margin nil) ...)'
4835 prefixes for display specifications. */
4836 location = Qunbound;
4837 if (CONSP (spec) && CONSP (XCAR (spec)))
4838 {
4839 Lisp_Object tem;
4840
4841 value = XCDR (spec);
4842 if (CONSP (value))
4843 value = XCAR (value);
4844
4845 tem = XCAR (spec);
4846 if (EQ (XCAR (tem), Qmargin)
4847 && (tem = XCDR (tem),
4848 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4849 (NILP (tem)
4850 || EQ (tem, Qleft_margin)
4851 || EQ (tem, Qright_margin))))
4852 location = tem;
4853 }
4854
4855 if (EQ (location, Qunbound))
4856 {
4857 location = Qnil;
4858 value = spec;
4859 }
4860
4861 /* After this point, VALUE is the property after any
4862 margin prefix has been stripped. It must be a string,
4863 an image specification, or `(space ...)'.
4864
4865 LOCATION specifies where to display: `left-margin',
4866 `right-margin' or nil. */
4867
4868 valid_p = (STRINGP (value)
4869 #ifdef HAVE_WINDOW_SYSTEM
4870 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4871 && valid_image_p (value))
4872 #endif /* not HAVE_WINDOW_SYSTEM */
4873 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4874
4875 if (valid_p && !display_replaced_p)
4876 {
4877 int retval = 1;
4878
4879 if (!it)
4880 {
4881 /* Callers need to know whether the display spec is any kind
4882 of `(space ...)' spec that is about to affect text-area
4883 display. */
4884 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4885 retval = 2;
4886 return retval;
4887 }
4888
4889 /* Save current settings of IT so that we can restore them
4890 when we are finished with the glyph property value. */
4891 push_it (it, position);
4892 it->from_overlay = overlay;
4893 it->from_disp_prop_p = 1;
4894
4895 if (NILP (location))
4896 it->area = TEXT_AREA;
4897 else if (EQ (location, Qleft_margin))
4898 it->area = LEFT_MARGIN_AREA;
4899 else
4900 it->area = RIGHT_MARGIN_AREA;
4901
4902 if (STRINGP (value))
4903 {
4904 it->string = value;
4905 it->multibyte_p = STRING_MULTIBYTE (it->string);
4906 it->current.overlay_string_index = -1;
4907 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4908 it->end_charpos = it->string_nchars = SCHARS (it->string);
4909 it->method = GET_FROM_STRING;
4910 it->stop_charpos = 0;
4911 it->prev_stop = 0;
4912 it->base_level_stop = 0;
4913 it->string_from_display_prop_p = 1;
4914 /* Say that we haven't consumed the characters with
4915 `display' property yet. The call to pop_it in
4916 set_iterator_to_next will clean this up. */
4917 if (BUFFERP (object))
4918 *position = start_pos;
4919
4920 /* Force paragraph direction to be that of the parent
4921 object. If the parent object's paragraph direction is
4922 not yet determined, default to L2R. */
4923 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4924 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4925 else
4926 it->paragraph_embedding = L2R;
4927
4928 /* Set up the bidi iterator for this display string. */
4929 if (it->bidi_p)
4930 {
4931 it->bidi_it.string.lstring = it->string;
4932 it->bidi_it.string.s = NULL;
4933 it->bidi_it.string.schars = it->end_charpos;
4934 it->bidi_it.string.bufpos = bufpos;
4935 it->bidi_it.string.from_disp_str = 1;
4936 it->bidi_it.string.unibyte = !it->multibyte_p;
4937 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4938 }
4939 }
4940 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4941 {
4942 it->method = GET_FROM_STRETCH;
4943 it->object = value;
4944 *position = it->position = start_pos;
4945 retval = 1 + (it->area == TEXT_AREA);
4946 }
4947 #ifdef HAVE_WINDOW_SYSTEM
4948 else
4949 {
4950 it->what = IT_IMAGE;
4951 it->image_id = lookup_image (it->f, value);
4952 it->position = start_pos;
4953 it->object = NILP (object) ? it->w->buffer : object;
4954 it->method = GET_FROM_IMAGE;
4955
4956 /* Say that we haven't consumed the characters with
4957 `display' property yet. The call to pop_it in
4958 set_iterator_to_next will clean this up. */
4959 *position = start_pos;
4960 }
4961 #endif /* HAVE_WINDOW_SYSTEM */
4962
4963 return retval;
4964 }
4965
4966 /* Invalid property or property not supported. Restore
4967 POSITION to what it was before. */
4968 *position = start_pos;
4969 return 0;
4970 }
4971
4972 /* Check if PROP is a display property value whose text should be
4973 treated as intangible. OVERLAY is the overlay from which PROP
4974 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4975 specify the buffer position covered by PROP. */
4976
4977 int
4978 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4979 ptrdiff_t charpos, ptrdiff_t bytepos)
4980 {
4981 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4982 struct text_pos position;
4983
4984 SET_TEXT_POS (position, charpos, bytepos);
4985 return handle_display_spec (NULL, prop, Qnil, overlay,
4986 &position, charpos, frame_window_p);
4987 }
4988
4989
4990 /* Return 1 if PROP is a display sub-property value containing STRING.
4991
4992 Implementation note: this and the following function are really
4993 special cases of handle_display_spec and
4994 handle_single_display_spec, and should ideally use the same code.
4995 Until they do, these two pairs must be consistent and must be
4996 modified in sync. */
4997
4998 static int
4999 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5000 {
5001 if (EQ (string, prop))
5002 return 1;
5003
5004 /* Skip over `when FORM'. */
5005 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5006 {
5007 prop = XCDR (prop);
5008 if (!CONSP (prop))
5009 return 0;
5010 /* Actually, the condition following `when' should be eval'ed,
5011 like handle_single_display_spec does, and we should return
5012 zero if it evaluates to nil. However, this function is
5013 called only when the buffer was already displayed and some
5014 glyph in the glyph matrix was found to come from a display
5015 string. Therefore, the condition was already evaluated, and
5016 the result was non-nil, otherwise the display string wouldn't
5017 have been displayed and we would have never been called for
5018 this property. Thus, we can skip the evaluation and assume
5019 its result is non-nil. */
5020 prop = XCDR (prop);
5021 }
5022
5023 if (CONSP (prop))
5024 /* Skip over `margin LOCATION'. */
5025 if (EQ (XCAR (prop), Qmargin))
5026 {
5027 prop = XCDR (prop);
5028 if (!CONSP (prop))
5029 return 0;
5030
5031 prop = XCDR (prop);
5032 if (!CONSP (prop))
5033 return 0;
5034 }
5035
5036 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5037 }
5038
5039
5040 /* Return 1 if STRING appears in the `display' property PROP. */
5041
5042 static int
5043 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5044 {
5045 if (CONSP (prop)
5046 && !EQ (XCAR (prop), Qwhen)
5047 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5048 {
5049 /* A list of sub-properties. */
5050 while (CONSP (prop))
5051 {
5052 if (single_display_spec_string_p (XCAR (prop), string))
5053 return 1;
5054 prop = XCDR (prop);
5055 }
5056 }
5057 else if (VECTORP (prop))
5058 {
5059 /* A vector of sub-properties. */
5060 ptrdiff_t i;
5061 for (i = 0; i < ASIZE (prop); ++i)
5062 if (single_display_spec_string_p (AREF (prop, i), string))
5063 return 1;
5064 }
5065 else
5066 return single_display_spec_string_p (prop, string);
5067
5068 return 0;
5069 }
5070
5071 /* Look for STRING in overlays and text properties in the current
5072 buffer, between character positions FROM and TO (excluding TO).
5073 BACK_P non-zero means look back (in this case, TO is supposed to be
5074 less than FROM).
5075 Value is the first character position where STRING was found, or
5076 zero if it wasn't found before hitting TO.
5077
5078 This function may only use code that doesn't eval because it is
5079 called asynchronously from note_mouse_highlight. */
5080
5081 static ptrdiff_t
5082 string_buffer_position_lim (Lisp_Object string,
5083 ptrdiff_t from, ptrdiff_t to, int back_p)
5084 {
5085 Lisp_Object limit, prop, pos;
5086 int found = 0;
5087
5088 pos = make_number (max (from, BEGV));
5089
5090 if (!back_p) /* looking forward */
5091 {
5092 limit = make_number (min (to, ZV));
5093 while (!found && !EQ (pos, limit))
5094 {
5095 prop = Fget_char_property (pos, Qdisplay, Qnil);
5096 if (!NILP (prop) && display_prop_string_p (prop, string))
5097 found = 1;
5098 else
5099 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5100 limit);
5101 }
5102 }
5103 else /* looking back */
5104 {
5105 limit = make_number (max (to, BEGV));
5106 while (!found && !EQ (pos, limit))
5107 {
5108 prop = Fget_char_property (pos, Qdisplay, Qnil);
5109 if (!NILP (prop) && display_prop_string_p (prop, string))
5110 found = 1;
5111 else
5112 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5113 limit);
5114 }
5115 }
5116
5117 return found ? XINT (pos) : 0;
5118 }
5119
5120 /* Determine which buffer position in current buffer STRING comes from.
5121 AROUND_CHARPOS is an approximate position where it could come from.
5122 Value is the buffer position or 0 if it couldn't be determined.
5123
5124 This function is necessary because we don't record buffer positions
5125 in glyphs generated from strings (to keep struct glyph small).
5126 This function may only use code that doesn't eval because it is
5127 called asynchronously from note_mouse_highlight. */
5128
5129 static ptrdiff_t
5130 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5131 {
5132 const int MAX_DISTANCE = 1000;
5133 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5134 around_charpos + MAX_DISTANCE,
5135 0);
5136
5137 if (!found)
5138 found = string_buffer_position_lim (string, around_charpos,
5139 around_charpos - MAX_DISTANCE, 1);
5140 return found;
5141 }
5142
5143
5144 \f
5145 /***********************************************************************
5146 `composition' property
5147 ***********************************************************************/
5148
5149 /* Set up iterator IT from `composition' property at its current
5150 position. Called from handle_stop. */
5151
5152 static enum prop_handled
5153 handle_composition_prop (struct it *it)
5154 {
5155 Lisp_Object prop, string;
5156 ptrdiff_t pos, pos_byte, start, end;
5157
5158 if (STRINGP (it->string))
5159 {
5160 unsigned char *s;
5161
5162 pos = IT_STRING_CHARPOS (*it);
5163 pos_byte = IT_STRING_BYTEPOS (*it);
5164 string = it->string;
5165 s = SDATA (string) + pos_byte;
5166 it->c = STRING_CHAR (s);
5167 }
5168 else
5169 {
5170 pos = IT_CHARPOS (*it);
5171 pos_byte = IT_BYTEPOS (*it);
5172 string = Qnil;
5173 it->c = FETCH_CHAR (pos_byte);
5174 }
5175
5176 /* If there's a valid composition and point is not inside of the
5177 composition (in the case that the composition is from the current
5178 buffer), draw a glyph composed from the composition components. */
5179 if (find_composition (pos, -1, &start, &end, &prop, string)
5180 && COMPOSITION_VALID_P (start, end, prop)
5181 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5182 {
5183 if (start < pos)
5184 /* As we can't handle this situation (perhaps font-lock added
5185 a new composition), we just return here hoping that next
5186 redisplay will detect this composition much earlier. */
5187 return HANDLED_NORMALLY;
5188 if (start != pos)
5189 {
5190 if (STRINGP (it->string))
5191 pos_byte = string_char_to_byte (it->string, start);
5192 else
5193 pos_byte = CHAR_TO_BYTE (start);
5194 }
5195 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5196 prop, string);
5197
5198 if (it->cmp_it.id >= 0)
5199 {
5200 it->cmp_it.ch = -1;
5201 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5202 it->cmp_it.nglyphs = -1;
5203 }
5204 }
5205
5206 return HANDLED_NORMALLY;
5207 }
5208
5209
5210 \f
5211 /***********************************************************************
5212 Overlay strings
5213 ***********************************************************************/
5214
5215 /* The following structure is used to record overlay strings for
5216 later sorting in load_overlay_strings. */
5217
5218 struct overlay_entry
5219 {
5220 Lisp_Object overlay;
5221 Lisp_Object string;
5222 EMACS_INT priority;
5223 int after_string_p;
5224 };
5225
5226
5227 /* Set up iterator IT from overlay strings at its current position.
5228 Called from handle_stop. */
5229
5230 static enum prop_handled
5231 handle_overlay_change (struct it *it)
5232 {
5233 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5234 return HANDLED_RECOMPUTE_PROPS;
5235 else
5236 return HANDLED_NORMALLY;
5237 }
5238
5239
5240 /* Set up the next overlay string for delivery by IT, if there is an
5241 overlay string to deliver. Called by set_iterator_to_next when the
5242 end of the current overlay string is reached. If there are more
5243 overlay strings to display, IT->string and
5244 IT->current.overlay_string_index are set appropriately here.
5245 Otherwise IT->string is set to nil. */
5246
5247 static void
5248 next_overlay_string (struct it *it)
5249 {
5250 ++it->current.overlay_string_index;
5251 if (it->current.overlay_string_index == it->n_overlay_strings)
5252 {
5253 /* No more overlay strings. Restore IT's settings to what
5254 they were before overlay strings were processed, and
5255 continue to deliver from current_buffer. */
5256
5257 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5258 pop_it (it);
5259 eassert (it->sp > 0
5260 || (NILP (it->string)
5261 && it->method == GET_FROM_BUFFER
5262 && it->stop_charpos >= BEGV
5263 && it->stop_charpos <= it->end_charpos));
5264 it->current.overlay_string_index = -1;
5265 it->n_overlay_strings = 0;
5266 it->overlay_strings_charpos = -1;
5267 /* If there's an empty display string on the stack, pop the
5268 stack, to resync the bidi iterator with IT's position. Such
5269 empty strings are pushed onto the stack in
5270 get_overlay_strings_1. */
5271 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5272 pop_it (it);
5273
5274 /* If we're at the end of the buffer, record that we have
5275 processed the overlay strings there already, so that
5276 next_element_from_buffer doesn't try it again. */
5277 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5278 it->overlay_strings_at_end_processed_p = 1;
5279 }
5280 else
5281 {
5282 /* There are more overlay strings to process. If
5283 IT->current.overlay_string_index has advanced to a position
5284 where we must load IT->overlay_strings with more strings, do
5285 it. We must load at the IT->overlay_strings_charpos where
5286 IT->n_overlay_strings was originally computed; when invisible
5287 text is present, this might not be IT_CHARPOS (Bug#7016). */
5288 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5289
5290 if (it->current.overlay_string_index && i == 0)
5291 load_overlay_strings (it, it->overlay_strings_charpos);
5292
5293 /* Initialize IT to deliver display elements from the overlay
5294 string. */
5295 it->string = it->overlay_strings[i];
5296 it->multibyte_p = STRING_MULTIBYTE (it->string);
5297 SET_TEXT_POS (it->current.string_pos, 0, 0);
5298 it->method = GET_FROM_STRING;
5299 it->stop_charpos = 0;
5300 if (it->cmp_it.stop_pos >= 0)
5301 it->cmp_it.stop_pos = 0;
5302 it->prev_stop = 0;
5303 it->base_level_stop = 0;
5304
5305 /* Set up the bidi iterator for this overlay string. */
5306 if (it->bidi_p)
5307 {
5308 it->bidi_it.string.lstring = it->string;
5309 it->bidi_it.string.s = NULL;
5310 it->bidi_it.string.schars = SCHARS (it->string);
5311 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5312 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5313 it->bidi_it.string.unibyte = !it->multibyte_p;
5314 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5315 }
5316 }
5317
5318 CHECK_IT (it);
5319 }
5320
5321
5322 /* Compare two overlay_entry structures E1 and E2. Used as a
5323 comparison function for qsort in load_overlay_strings. Overlay
5324 strings for the same position are sorted so that
5325
5326 1. All after-strings come in front of before-strings, except
5327 when they come from the same overlay.
5328
5329 2. Within after-strings, strings are sorted so that overlay strings
5330 from overlays with higher priorities come first.
5331
5332 2. Within before-strings, strings are sorted so that overlay
5333 strings from overlays with higher priorities come last.
5334
5335 Value is analogous to strcmp. */
5336
5337
5338 static int
5339 compare_overlay_entries (const void *e1, const void *e2)
5340 {
5341 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5342 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5343 int result;
5344
5345 if (entry1->after_string_p != entry2->after_string_p)
5346 {
5347 /* Let after-strings appear in front of before-strings if
5348 they come from different overlays. */
5349 if (EQ (entry1->overlay, entry2->overlay))
5350 result = entry1->after_string_p ? 1 : -1;
5351 else
5352 result = entry1->after_string_p ? -1 : 1;
5353 }
5354 else if (entry1->priority != entry2->priority)
5355 {
5356 if (entry1->after_string_p)
5357 /* After-strings sorted in order of decreasing priority. */
5358 result = entry2->priority < entry1->priority ? -1 : 1;
5359 else
5360 /* Before-strings sorted in order of increasing priority. */
5361 result = entry1->priority < entry2->priority ? -1 : 1;
5362 }
5363 else
5364 result = 0;
5365
5366 return result;
5367 }
5368
5369
5370 /* Load the vector IT->overlay_strings with overlay strings from IT's
5371 current buffer position, or from CHARPOS if that is > 0. Set
5372 IT->n_overlays to the total number of overlay strings found.
5373
5374 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5375 a time. On entry into load_overlay_strings,
5376 IT->current.overlay_string_index gives the number of overlay
5377 strings that have already been loaded by previous calls to this
5378 function.
5379
5380 IT->add_overlay_start contains an additional overlay start
5381 position to consider for taking overlay strings from, if non-zero.
5382 This position comes into play when the overlay has an `invisible'
5383 property, and both before and after-strings. When we've skipped to
5384 the end of the overlay, because of its `invisible' property, we
5385 nevertheless want its before-string to appear.
5386 IT->add_overlay_start will contain the overlay start position
5387 in this case.
5388
5389 Overlay strings are sorted so that after-string strings come in
5390 front of before-string strings. Within before and after-strings,
5391 strings are sorted by overlay priority. See also function
5392 compare_overlay_entries. */
5393
5394 static void
5395 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5396 {
5397 Lisp_Object overlay, window, str, invisible;
5398 struct Lisp_Overlay *ov;
5399 ptrdiff_t start, end;
5400 ptrdiff_t size = 20;
5401 ptrdiff_t n = 0, i, j;
5402 int invis_p;
5403 struct overlay_entry *entries
5404 = (struct overlay_entry *) alloca (size * sizeof *entries);
5405 USE_SAFE_ALLOCA;
5406
5407 if (charpos <= 0)
5408 charpos = IT_CHARPOS (*it);
5409
5410 /* Append the overlay string STRING of overlay OVERLAY to vector
5411 `entries' which has size `size' and currently contains `n'
5412 elements. AFTER_P non-zero means STRING is an after-string of
5413 OVERLAY. */
5414 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5415 do \
5416 { \
5417 Lisp_Object priority; \
5418 \
5419 if (n == size) \
5420 { \
5421 struct overlay_entry *old = entries; \
5422 SAFE_NALLOCA (entries, 2, size); \
5423 memcpy (entries, old, size * sizeof *entries); \
5424 size *= 2; \
5425 } \
5426 \
5427 entries[n].string = (STRING); \
5428 entries[n].overlay = (OVERLAY); \
5429 priority = Foverlay_get ((OVERLAY), Qpriority); \
5430 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5431 entries[n].after_string_p = (AFTER_P); \
5432 ++n; \
5433 } \
5434 while (0)
5435
5436 /* Process overlay before the overlay center. */
5437 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5438 {
5439 XSETMISC (overlay, ov);
5440 eassert (OVERLAYP (overlay));
5441 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5442 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5443
5444 if (end < charpos)
5445 break;
5446
5447 /* Skip this overlay if it doesn't start or end at IT's current
5448 position. */
5449 if (end != charpos && start != charpos)
5450 continue;
5451
5452 /* Skip this overlay if it doesn't apply to IT->w. */
5453 window = Foverlay_get (overlay, Qwindow);
5454 if (WINDOWP (window) && XWINDOW (window) != it->w)
5455 continue;
5456
5457 /* If the text ``under'' the overlay is invisible, both before-
5458 and after-strings from this overlay are visible; start and
5459 end position are indistinguishable. */
5460 invisible = Foverlay_get (overlay, Qinvisible);
5461 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5462
5463 /* If overlay has a non-empty before-string, record it. */
5464 if ((start == charpos || (end == charpos && invis_p))
5465 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5466 && SCHARS (str))
5467 RECORD_OVERLAY_STRING (overlay, str, 0);
5468
5469 /* If overlay has a non-empty after-string, record it. */
5470 if ((end == charpos || (start == charpos && invis_p))
5471 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5472 && SCHARS (str))
5473 RECORD_OVERLAY_STRING (overlay, str, 1);
5474 }
5475
5476 /* Process overlays after the overlay center. */
5477 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5478 {
5479 XSETMISC (overlay, ov);
5480 eassert (OVERLAYP (overlay));
5481 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5482 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5483
5484 if (start > charpos)
5485 break;
5486
5487 /* Skip this overlay if it doesn't start or end at IT's current
5488 position. */
5489 if (end != charpos && start != charpos)
5490 continue;
5491
5492 /* Skip this overlay if it doesn't apply to IT->w. */
5493 window = Foverlay_get (overlay, Qwindow);
5494 if (WINDOWP (window) && XWINDOW (window) != it->w)
5495 continue;
5496
5497 /* If the text ``under'' the overlay is invisible, it has a zero
5498 dimension, and both before- and after-strings apply. */
5499 invisible = Foverlay_get (overlay, Qinvisible);
5500 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5501
5502 /* If overlay has a non-empty before-string, record it. */
5503 if ((start == charpos || (end == charpos && invis_p))
5504 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5505 && SCHARS (str))
5506 RECORD_OVERLAY_STRING (overlay, str, 0);
5507
5508 /* If overlay has a non-empty after-string, record it. */
5509 if ((end == charpos || (start == charpos && invis_p))
5510 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5511 && SCHARS (str))
5512 RECORD_OVERLAY_STRING (overlay, str, 1);
5513 }
5514
5515 #undef RECORD_OVERLAY_STRING
5516
5517 /* Sort entries. */
5518 if (n > 1)
5519 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5520
5521 /* Record number of overlay strings, and where we computed it. */
5522 it->n_overlay_strings = n;
5523 it->overlay_strings_charpos = charpos;
5524
5525 /* IT->current.overlay_string_index is the number of overlay strings
5526 that have already been consumed by IT. Copy some of the
5527 remaining overlay strings to IT->overlay_strings. */
5528 i = 0;
5529 j = it->current.overlay_string_index;
5530 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5531 {
5532 it->overlay_strings[i] = entries[j].string;
5533 it->string_overlays[i++] = entries[j++].overlay;
5534 }
5535
5536 CHECK_IT (it);
5537 SAFE_FREE ();
5538 }
5539
5540
5541 /* Get the first chunk of overlay strings at IT's current buffer
5542 position, or at CHARPOS if that is > 0. Value is non-zero if at
5543 least one overlay string was found. */
5544
5545 static int
5546 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5547 {
5548 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5549 process. This fills IT->overlay_strings with strings, and sets
5550 IT->n_overlay_strings to the total number of strings to process.
5551 IT->pos.overlay_string_index has to be set temporarily to zero
5552 because load_overlay_strings needs this; it must be set to -1
5553 when no overlay strings are found because a zero value would
5554 indicate a position in the first overlay string. */
5555 it->current.overlay_string_index = 0;
5556 load_overlay_strings (it, charpos);
5557
5558 /* If we found overlay strings, set up IT to deliver display
5559 elements from the first one. Otherwise set up IT to deliver
5560 from current_buffer. */
5561 if (it->n_overlay_strings)
5562 {
5563 /* Make sure we know settings in current_buffer, so that we can
5564 restore meaningful values when we're done with the overlay
5565 strings. */
5566 if (compute_stop_p)
5567 compute_stop_pos (it);
5568 eassert (it->face_id >= 0);
5569
5570 /* Save IT's settings. They are restored after all overlay
5571 strings have been processed. */
5572 eassert (!compute_stop_p || it->sp == 0);
5573
5574 /* When called from handle_stop, there might be an empty display
5575 string loaded. In that case, don't bother saving it. But
5576 don't use this optimization with the bidi iterator, since we
5577 need the corresponding pop_it call to resync the bidi
5578 iterator's position with IT's position, after we are done
5579 with the overlay strings. (The corresponding call to pop_it
5580 in case of an empty display string is in
5581 next_overlay_string.) */
5582 if (!(!it->bidi_p
5583 && STRINGP (it->string) && !SCHARS (it->string)))
5584 push_it (it, NULL);
5585
5586 /* Set up IT to deliver display elements from the first overlay
5587 string. */
5588 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5589 it->string = it->overlay_strings[0];
5590 it->from_overlay = Qnil;
5591 it->stop_charpos = 0;
5592 eassert (STRINGP (it->string));
5593 it->end_charpos = SCHARS (it->string);
5594 it->prev_stop = 0;
5595 it->base_level_stop = 0;
5596 it->multibyte_p = STRING_MULTIBYTE (it->string);
5597 it->method = GET_FROM_STRING;
5598 it->from_disp_prop_p = 0;
5599
5600 /* Force paragraph direction to be that of the parent
5601 buffer. */
5602 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5603 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5604 else
5605 it->paragraph_embedding = L2R;
5606
5607 /* Set up the bidi iterator for this overlay string. */
5608 if (it->bidi_p)
5609 {
5610 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5611
5612 it->bidi_it.string.lstring = it->string;
5613 it->bidi_it.string.s = NULL;
5614 it->bidi_it.string.schars = SCHARS (it->string);
5615 it->bidi_it.string.bufpos = pos;
5616 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5617 it->bidi_it.string.unibyte = !it->multibyte_p;
5618 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5619 }
5620 return 1;
5621 }
5622
5623 it->current.overlay_string_index = -1;
5624 return 0;
5625 }
5626
5627 static int
5628 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5629 {
5630 it->string = Qnil;
5631 it->method = GET_FROM_BUFFER;
5632
5633 (void) get_overlay_strings_1 (it, charpos, 1);
5634
5635 CHECK_IT (it);
5636
5637 /* Value is non-zero if we found at least one overlay string. */
5638 return STRINGP (it->string);
5639 }
5640
5641
5642 \f
5643 /***********************************************************************
5644 Saving and restoring state
5645 ***********************************************************************/
5646
5647 /* Save current settings of IT on IT->stack. Called, for example,
5648 before setting up IT for an overlay string, to be able to restore
5649 IT's settings to what they were after the overlay string has been
5650 processed. If POSITION is non-NULL, it is the position to save on
5651 the stack instead of IT->position. */
5652
5653 static void
5654 push_it (struct it *it, struct text_pos *position)
5655 {
5656 struct iterator_stack_entry *p;
5657
5658 eassert (it->sp < IT_STACK_SIZE);
5659 p = it->stack + it->sp;
5660
5661 p->stop_charpos = it->stop_charpos;
5662 p->prev_stop = it->prev_stop;
5663 p->base_level_stop = it->base_level_stop;
5664 p->cmp_it = it->cmp_it;
5665 eassert (it->face_id >= 0);
5666 p->face_id = it->face_id;
5667 p->string = it->string;
5668 p->method = it->method;
5669 p->from_overlay = it->from_overlay;
5670 switch (p->method)
5671 {
5672 case GET_FROM_IMAGE:
5673 p->u.image.object = it->object;
5674 p->u.image.image_id = it->image_id;
5675 p->u.image.slice = it->slice;
5676 break;
5677 case GET_FROM_STRETCH:
5678 p->u.stretch.object = it->object;
5679 break;
5680 }
5681 p->position = position ? *position : it->position;
5682 p->current = it->current;
5683 p->end_charpos = it->end_charpos;
5684 p->string_nchars = it->string_nchars;
5685 p->area = it->area;
5686 p->multibyte_p = it->multibyte_p;
5687 p->avoid_cursor_p = it->avoid_cursor_p;
5688 p->space_width = it->space_width;
5689 p->font_height = it->font_height;
5690 p->voffset = it->voffset;
5691 p->string_from_display_prop_p = it->string_from_display_prop_p;
5692 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5693 p->display_ellipsis_p = 0;
5694 p->line_wrap = it->line_wrap;
5695 p->bidi_p = it->bidi_p;
5696 p->paragraph_embedding = it->paragraph_embedding;
5697 p->from_disp_prop_p = it->from_disp_prop_p;
5698 ++it->sp;
5699
5700 /* Save the state of the bidi iterator as well. */
5701 if (it->bidi_p)
5702 bidi_push_it (&it->bidi_it);
5703 }
5704
5705 static void
5706 iterate_out_of_display_property (struct it *it)
5707 {
5708 int buffer_p = !STRINGP (it->string);
5709 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5710 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5711
5712 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5713
5714 /* Maybe initialize paragraph direction. If we are at the beginning
5715 of a new paragraph, next_element_from_buffer may not have a
5716 chance to do that. */
5717 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5718 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5719 /* prev_stop can be zero, so check against BEGV as well. */
5720 while (it->bidi_it.charpos >= bob
5721 && it->prev_stop <= it->bidi_it.charpos
5722 && it->bidi_it.charpos < CHARPOS (it->position)
5723 && it->bidi_it.charpos < eob)
5724 bidi_move_to_visually_next (&it->bidi_it);
5725 /* Record the stop_pos we just crossed, for when we cross it
5726 back, maybe. */
5727 if (it->bidi_it.charpos > CHARPOS (it->position))
5728 it->prev_stop = CHARPOS (it->position);
5729 /* If we ended up not where pop_it put us, resync IT's
5730 positional members with the bidi iterator. */
5731 if (it->bidi_it.charpos != CHARPOS (it->position))
5732 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5733 if (buffer_p)
5734 it->current.pos = it->position;
5735 else
5736 it->current.string_pos = it->position;
5737 }
5738
5739 /* Restore IT's settings from IT->stack. Called, for example, when no
5740 more overlay strings must be processed, and we return to delivering
5741 display elements from a buffer, or when the end of a string from a
5742 `display' property is reached and we return to delivering display
5743 elements from an overlay string, or from a buffer. */
5744
5745 static void
5746 pop_it (struct it *it)
5747 {
5748 struct iterator_stack_entry *p;
5749 int from_display_prop = it->from_disp_prop_p;
5750
5751 eassert (it->sp > 0);
5752 --it->sp;
5753 p = it->stack + it->sp;
5754 it->stop_charpos = p->stop_charpos;
5755 it->prev_stop = p->prev_stop;
5756 it->base_level_stop = p->base_level_stop;
5757 it->cmp_it = p->cmp_it;
5758 it->face_id = p->face_id;
5759 it->current = p->current;
5760 it->position = p->position;
5761 it->string = p->string;
5762 it->from_overlay = p->from_overlay;
5763 if (NILP (it->string))
5764 SET_TEXT_POS (it->current.string_pos, -1, -1);
5765 it->method = p->method;
5766 switch (it->method)
5767 {
5768 case GET_FROM_IMAGE:
5769 it->image_id = p->u.image.image_id;
5770 it->object = p->u.image.object;
5771 it->slice = p->u.image.slice;
5772 break;
5773 case GET_FROM_STRETCH:
5774 it->object = p->u.stretch.object;
5775 break;
5776 case GET_FROM_BUFFER:
5777 it->object = it->w->buffer;
5778 break;
5779 case GET_FROM_STRING:
5780 it->object = it->string;
5781 break;
5782 case GET_FROM_DISPLAY_VECTOR:
5783 if (it->s)
5784 it->method = GET_FROM_C_STRING;
5785 else if (STRINGP (it->string))
5786 it->method = GET_FROM_STRING;
5787 else
5788 {
5789 it->method = GET_FROM_BUFFER;
5790 it->object = it->w->buffer;
5791 }
5792 }
5793 it->end_charpos = p->end_charpos;
5794 it->string_nchars = p->string_nchars;
5795 it->area = p->area;
5796 it->multibyte_p = p->multibyte_p;
5797 it->avoid_cursor_p = p->avoid_cursor_p;
5798 it->space_width = p->space_width;
5799 it->font_height = p->font_height;
5800 it->voffset = p->voffset;
5801 it->string_from_display_prop_p = p->string_from_display_prop_p;
5802 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5803 it->line_wrap = p->line_wrap;
5804 it->bidi_p = p->bidi_p;
5805 it->paragraph_embedding = p->paragraph_embedding;
5806 it->from_disp_prop_p = p->from_disp_prop_p;
5807 if (it->bidi_p)
5808 {
5809 bidi_pop_it (&it->bidi_it);
5810 /* Bidi-iterate until we get out of the portion of text, if any,
5811 covered by a `display' text property or by an overlay with
5812 `display' property. (We cannot just jump there, because the
5813 internal coherency of the bidi iterator state can not be
5814 preserved across such jumps.) We also must determine the
5815 paragraph base direction if the overlay we just processed is
5816 at the beginning of a new paragraph. */
5817 if (from_display_prop
5818 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5819 iterate_out_of_display_property (it);
5820
5821 eassert ((BUFFERP (it->object)
5822 && IT_CHARPOS (*it) == it->bidi_it.charpos
5823 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5824 || (STRINGP (it->object)
5825 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5826 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5827 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5828 }
5829 }
5830
5831
5832 \f
5833 /***********************************************************************
5834 Moving over lines
5835 ***********************************************************************/
5836
5837 /* Set IT's current position to the previous line start. */
5838
5839 static void
5840 back_to_previous_line_start (struct it *it)
5841 {
5842 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5843 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5844 }
5845
5846
5847 /* Move IT to the next line start.
5848
5849 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5850 we skipped over part of the text (as opposed to moving the iterator
5851 continuously over the text). Otherwise, don't change the value
5852 of *SKIPPED_P.
5853
5854 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5855 iterator on the newline, if it was found.
5856
5857 Newlines may come from buffer text, overlay strings, or strings
5858 displayed via the `display' property. That's the reason we can't
5859 simply use find_next_newline_no_quit.
5860
5861 Note that this function may not skip over invisible text that is so
5862 because of text properties and immediately follows a newline. If
5863 it would, function reseat_at_next_visible_line_start, when called
5864 from set_iterator_to_next, would effectively make invisible
5865 characters following a newline part of the wrong glyph row, which
5866 leads to wrong cursor motion. */
5867
5868 static int
5869 forward_to_next_line_start (struct it *it, int *skipped_p,
5870 struct bidi_it *bidi_it_prev)
5871 {
5872 ptrdiff_t old_selective;
5873 int newline_found_p, n;
5874 const int MAX_NEWLINE_DISTANCE = 500;
5875
5876 /* If already on a newline, just consume it to avoid unintended
5877 skipping over invisible text below. */
5878 if (it->what == IT_CHARACTER
5879 && it->c == '\n'
5880 && CHARPOS (it->position) == IT_CHARPOS (*it))
5881 {
5882 if (it->bidi_p && bidi_it_prev)
5883 *bidi_it_prev = it->bidi_it;
5884 set_iterator_to_next (it, 0);
5885 it->c = 0;
5886 return 1;
5887 }
5888
5889 /* Don't handle selective display in the following. It's (a)
5890 unnecessary because it's done by the caller, and (b) leads to an
5891 infinite recursion because next_element_from_ellipsis indirectly
5892 calls this function. */
5893 old_selective = it->selective;
5894 it->selective = 0;
5895
5896 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5897 from buffer text. */
5898 for (n = newline_found_p = 0;
5899 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5900 n += STRINGP (it->string) ? 0 : 1)
5901 {
5902 if (!get_next_display_element (it))
5903 return 0;
5904 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5905 if (newline_found_p && it->bidi_p && bidi_it_prev)
5906 *bidi_it_prev = it->bidi_it;
5907 set_iterator_to_next (it, 0);
5908 }
5909
5910 /* If we didn't find a newline near enough, see if we can use a
5911 short-cut. */
5912 if (!newline_found_p)
5913 {
5914 ptrdiff_t start = IT_CHARPOS (*it);
5915 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5916 Lisp_Object pos;
5917
5918 eassert (!STRINGP (it->string));
5919
5920 /* If there isn't any `display' property in sight, and no
5921 overlays, we can just use the position of the newline in
5922 buffer text. */
5923 if (it->stop_charpos >= limit
5924 || ((pos = Fnext_single_property_change (make_number (start),
5925 Qdisplay, Qnil,
5926 make_number (limit)),
5927 NILP (pos))
5928 && next_overlay_change (start) == ZV))
5929 {
5930 if (!it->bidi_p)
5931 {
5932 IT_CHARPOS (*it) = limit;
5933 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5934 }
5935 else
5936 {
5937 struct bidi_it bprev;
5938
5939 /* Help bidi.c avoid expensive searches for display
5940 properties and overlays, by telling it that there are
5941 none up to `limit'. */
5942 if (it->bidi_it.disp_pos < limit)
5943 {
5944 it->bidi_it.disp_pos = limit;
5945 it->bidi_it.disp_prop = 0;
5946 }
5947 do {
5948 bprev = it->bidi_it;
5949 bidi_move_to_visually_next (&it->bidi_it);
5950 } while (it->bidi_it.charpos != limit);
5951 IT_CHARPOS (*it) = limit;
5952 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5953 if (bidi_it_prev)
5954 *bidi_it_prev = bprev;
5955 }
5956 *skipped_p = newline_found_p = 1;
5957 }
5958 else
5959 {
5960 while (get_next_display_element (it)
5961 && !newline_found_p)
5962 {
5963 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5964 if (newline_found_p && it->bidi_p && bidi_it_prev)
5965 *bidi_it_prev = it->bidi_it;
5966 set_iterator_to_next (it, 0);
5967 }
5968 }
5969 }
5970
5971 it->selective = old_selective;
5972 return newline_found_p;
5973 }
5974
5975
5976 /* Set IT's current position to the previous visible line start. Skip
5977 invisible text that is so either due to text properties or due to
5978 selective display. Caution: this does not change IT->current_x and
5979 IT->hpos. */
5980
5981 static void
5982 back_to_previous_visible_line_start (struct it *it)
5983 {
5984 while (IT_CHARPOS (*it) > BEGV)
5985 {
5986 back_to_previous_line_start (it);
5987
5988 if (IT_CHARPOS (*it) <= BEGV)
5989 break;
5990
5991 /* If selective > 0, then lines indented more than its value are
5992 invisible. */
5993 if (it->selective > 0
5994 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5995 it->selective))
5996 continue;
5997
5998 /* Check the newline before point for invisibility. */
5999 {
6000 Lisp_Object prop;
6001 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6002 Qinvisible, it->window);
6003 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6004 continue;
6005 }
6006
6007 if (IT_CHARPOS (*it) <= BEGV)
6008 break;
6009
6010 {
6011 struct it it2;
6012 void *it2data = NULL;
6013 ptrdiff_t pos;
6014 ptrdiff_t beg, end;
6015 Lisp_Object val, overlay;
6016
6017 SAVE_IT (it2, *it, it2data);
6018
6019 /* If newline is part of a composition, continue from start of composition */
6020 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6021 && beg < IT_CHARPOS (*it))
6022 goto replaced;
6023
6024 /* If newline is replaced by a display property, find start of overlay
6025 or interval and continue search from that point. */
6026 pos = --IT_CHARPOS (it2);
6027 --IT_BYTEPOS (it2);
6028 it2.sp = 0;
6029 bidi_unshelve_cache (NULL, 0);
6030 it2.string_from_display_prop_p = 0;
6031 it2.from_disp_prop_p = 0;
6032 if (handle_display_prop (&it2) == HANDLED_RETURN
6033 && !NILP (val = get_char_property_and_overlay
6034 (make_number (pos), Qdisplay, Qnil, &overlay))
6035 && (OVERLAYP (overlay)
6036 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6037 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6038 {
6039 RESTORE_IT (it, it, it2data);
6040 goto replaced;
6041 }
6042
6043 /* Newline is not replaced by anything -- so we are done. */
6044 RESTORE_IT (it, it, it2data);
6045 break;
6046
6047 replaced:
6048 if (beg < BEGV)
6049 beg = BEGV;
6050 IT_CHARPOS (*it) = beg;
6051 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6052 }
6053 }
6054
6055 it->continuation_lines_width = 0;
6056
6057 eassert (IT_CHARPOS (*it) >= BEGV);
6058 eassert (IT_CHARPOS (*it) == BEGV
6059 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6060 CHECK_IT (it);
6061 }
6062
6063
6064 /* Reseat iterator IT at the previous visible line start. Skip
6065 invisible text that is so either due to text properties or due to
6066 selective display. At the end, update IT's overlay information,
6067 face information etc. */
6068
6069 void
6070 reseat_at_previous_visible_line_start (struct it *it)
6071 {
6072 back_to_previous_visible_line_start (it);
6073 reseat (it, it->current.pos, 1);
6074 CHECK_IT (it);
6075 }
6076
6077
6078 /* Reseat iterator IT on the next visible line start in the current
6079 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6080 preceding the line start. Skip over invisible text that is so
6081 because of selective display. Compute faces, overlays etc at the
6082 new position. Note that this function does not skip over text that
6083 is invisible because of text properties. */
6084
6085 static void
6086 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6087 {
6088 int newline_found_p, skipped_p = 0;
6089 struct bidi_it bidi_it_prev;
6090
6091 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6092
6093 /* Skip over lines that are invisible because they are indented
6094 more than the value of IT->selective. */
6095 if (it->selective > 0)
6096 while (IT_CHARPOS (*it) < ZV
6097 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6098 it->selective))
6099 {
6100 eassert (IT_BYTEPOS (*it) == BEGV
6101 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6102 newline_found_p =
6103 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6104 }
6105
6106 /* Position on the newline if that's what's requested. */
6107 if (on_newline_p && newline_found_p)
6108 {
6109 if (STRINGP (it->string))
6110 {
6111 if (IT_STRING_CHARPOS (*it) > 0)
6112 {
6113 if (!it->bidi_p)
6114 {
6115 --IT_STRING_CHARPOS (*it);
6116 --IT_STRING_BYTEPOS (*it);
6117 }
6118 else
6119 {
6120 /* We need to restore the bidi iterator to the state
6121 it had on the newline, and resync the IT's
6122 position with that. */
6123 it->bidi_it = bidi_it_prev;
6124 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6125 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6126 }
6127 }
6128 }
6129 else if (IT_CHARPOS (*it) > BEGV)
6130 {
6131 if (!it->bidi_p)
6132 {
6133 --IT_CHARPOS (*it);
6134 --IT_BYTEPOS (*it);
6135 }
6136 else
6137 {
6138 /* We need to restore the bidi iterator to the state it
6139 had on the newline and resync IT with that. */
6140 it->bidi_it = bidi_it_prev;
6141 IT_CHARPOS (*it) = it->bidi_it.charpos;
6142 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6143 }
6144 reseat (it, it->current.pos, 0);
6145 }
6146 }
6147 else if (skipped_p)
6148 reseat (it, it->current.pos, 0);
6149
6150 CHECK_IT (it);
6151 }
6152
6153
6154 \f
6155 /***********************************************************************
6156 Changing an iterator's position
6157 ***********************************************************************/
6158
6159 /* Change IT's current position to POS in current_buffer. If FORCE_P
6160 is non-zero, always check for text properties at the new position.
6161 Otherwise, text properties are only looked up if POS >=
6162 IT->check_charpos of a property. */
6163
6164 static void
6165 reseat (struct it *it, struct text_pos pos, int force_p)
6166 {
6167 ptrdiff_t original_pos = IT_CHARPOS (*it);
6168
6169 reseat_1 (it, pos, 0);
6170
6171 /* Determine where to check text properties. Avoid doing it
6172 where possible because text property lookup is very expensive. */
6173 if (force_p
6174 || CHARPOS (pos) > it->stop_charpos
6175 || CHARPOS (pos) < original_pos)
6176 {
6177 if (it->bidi_p)
6178 {
6179 /* For bidi iteration, we need to prime prev_stop and
6180 base_level_stop with our best estimations. */
6181 /* Implementation note: Of course, POS is not necessarily a
6182 stop position, so assigning prev_pos to it is a lie; we
6183 should have called compute_stop_backwards. However, if
6184 the current buffer does not include any R2L characters,
6185 that call would be a waste of cycles, because the
6186 iterator will never move back, and thus never cross this
6187 "fake" stop position. So we delay that backward search
6188 until the time we really need it, in next_element_from_buffer. */
6189 if (CHARPOS (pos) != it->prev_stop)
6190 it->prev_stop = CHARPOS (pos);
6191 if (CHARPOS (pos) < it->base_level_stop)
6192 it->base_level_stop = 0; /* meaning it's unknown */
6193 handle_stop (it);
6194 }
6195 else
6196 {
6197 handle_stop (it);
6198 it->prev_stop = it->base_level_stop = 0;
6199 }
6200
6201 }
6202
6203 CHECK_IT (it);
6204 }
6205
6206
6207 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6208 IT->stop_pos to POS, also. */
6209
6210 static void
6211 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6212 {
6213 /* Don't call this function when scanning a C string. */
6214 eassert (it->s == NULL);
6215
6216 /* POS must be a reasonable value. */
6217 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6218
6219 it->current.pos = it->position = pos;
6220 it->end_charpos = ZV;
6221 it->dpvec = NULL;
6222 it->current.dpvec_index = -1;
6223 it->current.overlay_string_index = -1;
6224 IT_STRING_CHARPOS (*it) = -1;
6225 IT_STRING_BYTEPOS (*it) = -1;
6226 it->string = Qnil;
6227 it->method = GET_FROM_BUFFER;
6228 it->object = it->w->buffer;
6229 it->area = TEXT_AREA;
6230 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6231 it->sp = 0;
6232 it->string_from_display_prop_p = 0;
6233 it->string_from_prefix_prop_p = 0;
6234
6235 it->from_disp_prop_p = 0;
6236 it->face_before_selective_p = 0;
6237 if (it->bidi_p)
6238 {
6239 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6240 &it->bidi_it);
6241 bidi_unshelve_cache (NULL, 0);
6242 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6243 it->bidi_it.string.s = NULL;
6244 it->bidi_it.string.lstring = Qnil;
6245 it->bidi_it.string.bufpos = 0;
6246 it->bidi_it.string.unibyte = 0;
6247 }
6248
6249 if (set_stop_p)
6250 {
6251 it->stop_charpos = CHARPOS (pos);
6252 it->base_level_stop = CHARPOS (pos);
6253 }
6254 }
6255
6256
6257 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6258 If S is non-null, it is a C string to iterate over. Otherwise,
6259 STRING gives a Lisp string to iterate over.
6260
6261 If PRECISION > 0, don't return more then PRECISION number of
6262 characters from the string.
6263
6264 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6265 characters have been returned. FIELD_WIDTH < 0 means an infinite
6266 field width.
6267
6268 MULTIBYTE = 0 means disable processing of multibyte characters,
6269 MULTIBYTE > 0 means enable it,
6270 MULTIBYTE < 0 means use IT->multibyte_p.
6271
6272 IT must be initialized via a prior call to init_iterator before
6273 calling this function. */
6274
6275 static void
6276 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6277 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6278 int multibyte)
6279 {
6280 /* No region in strings. */
6281 it->region_beg_charpos = it->region_end_charpos = -1;
6282
6283 /* No text property checks performed by default, but see below. */
6284 it->stop_charpos = -1;
6285
6286 /* Set iterator position and end position. */
6287 memset (&it->current, 0, sizeof it->current);
6288 it->current.overlay_string_index = -1;
6289 it->current.dpvec_index = -1;
6290 eassert (charpos >= 0);
6291
6292 /* If STRING is specified, use its multibyteness, otherwise use the
6293 setting of MULTIBYTE, if specified. */
6294 if (multibyte >= 0)
6295 it->multibyte_p = multibyte > 0;
6296
6297 /* Bidirectional reordering of strings is controlled by the default
6298 value of bidi-display-reordering. Don't try to reorder while
6299 loading loadup.el, as the necessary character property tables are
6300 not yet available. */
6301 it->bidi_p =
6302 NILP (Vpurify_flag)
6303 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6304
6305 if (s == NULL)
6306 {
6307 eassert (STRINGP (string));
6308 it->string = string;
6309 it->s = NULL;
6310 it->end_charpos = it->string_nchars = SCHARS (string);
6311 it->method = GET_FROM_STRING;
6312 it->current.string_pos = string_pos (charpos, string);
6313
6314 if (it->bidi_p)
6315 {
6316 it->bidi_it.string.lstring = string;
6317 it->bidi_it.string.s = NULL;
6318 it->bidi_it.string.schars = it->end_charpos;
6319 it->bidi_it.string.bufpos = 0;
6320 it->bidi_it.string.from_disp_str = 0;
6321 it->bidi_it.string.unibyte = !it->multibyte_p;
6322 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6323 FRAME_WINDOW_P (it->f), &it->bidi_it);
6324 }
6325 }
6326 else
6327 {
6328 it->s = (const unsigned char *) s;
6329 it->string = Qnil;
6330
6331 /* Note that we use IT->current.pos, not it->current.string_pos,
6332 for displaying C strings. */
6333 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6334 if (it->multibyte_p)
6335 {
6336 it->current.pos = c_string_pos (charpos, s, 1);
6337 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6338 }
6339 else
6340 {
6341 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6342 it->end_charpos = it->string_nchars = strlen (s);
6343 }
6344
6345 if (it->bidi_p)
6346 {
6347 it->bidi_it.string.lstring = Qnil;
6348 it->bidi_it.string.s = (const unsigned char *) s;
6349 it->bidi_it.string.schars = it->end_charpos;
6350 it->bidi_it.string.bufpos = 0;
6351 it->bidi_it.string.from_disp_str = 0;
6352 it->bidi_it.string.unibyte = !it->multibyte_p;
6353 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6354 &it->bidi_it);
6355 }
6356 it->method = GET_FROM_C_STRING;
6357 }
6358
6359 /* PRECISION > 0 means don't return more than PRECISION characters
6360 from the string. */
6361 if (precision > 0 && it->end_charpos - charpos > precision)
6362 {
6363 it->end_charpos = it->string_nchars = charpos + precision;
6364 if (it->bidi_p)
6365 it->bidi_it.string.schars = it->end_charpos;
6366 }
6367
6368 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6369 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6370 FIELD_WIDTH < 0 means infinite field width. This is useful for
6371 padding with `-' at the end of a mode line. */
6372 if (field_width < 0)
6373 field_width = INFINITY;
6374 /* Implementation note: We deliberately don't enlarge
6375 it->bidi_it.string.schars here to fit it->end_charpos, because
6376 the bidi iterator cannot produce characters out of thin air. */
6377 if (field_width > it->end_charpos - charpos)
6378 it->end_charpos = charpos + field_width;
6379
6380 /* Use the standard display table for displaying strings. */
6381 if (DISP_TABLE_P (Vstandard_display_table))
6382 it->dp = XCHAR_TABLE (Vstandard_display_table);
6383
6384 it->stop_charpos = charpos;
6385 it->prev_stop = charpos;
6386 it->base_level_stop = 0;
6387 if (it->bidi_p)
6388 {
6389 it->bidi_it.first_elt = 1;
6390 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6391 it->bidi_it.disp_pos = -1;
6392 }
6393 if (s == NULL && it->multibyte_p)
6394 {
6395 ptrdiff_t endpos = SCHARS (it->string);
6396 if (endpos > it->end_charpos)
6397 endpos = it->end_charpos;
6398 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6399 it->string);
6400 }
6401 CHECK_IT (it);
6402 }
6403
6404
6405 \f
6406 /***********************************************************************
6407 Iteration
6408 ***********************************************************************/
6409
6410 /* Map enum it_method value to corresponding next_element_from_* function. */
6411
6412 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6413 {
6414 next_element_from_buffer,
6415 next_element_from_display_vector,
6416 next_element_from_string,
6417 next_element_from_c_string,
6418 next_element_from_image,
6419 next_element_from_stretch
6420 };
6421
6422 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6423
6424
6425 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6426 (possibly with the following characters). */
6427
6428 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6429 ((IT)->cmp_it.id >= 0 \
6430 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6431 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6432 END_CHARPOS, (IT)->w, \
6433 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6434 (IT)->string)))
6435
6436
6437 /* Lookup the char-table Vglyphless_char_display for character C (-1
6438 if we want information for no-font case), and return the display
6439 method symbol. By side-effect, update it->what and
6440 it->glyphless_method. This function is called from
6441 get_next_display_element for each character element, and from
6442 x_produce_glyphs when no suitable font was found. */
6443
6444 Lisp_Object
6445 lookup_glyphless_char_display (int c, struct it *it)
6446 {
6447 Lisp_Object glyphless_method = Qnil;
6448
6449 if (CHAR_TABLE_P (Vglyphless_char_display)
6450 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6451 {
6452 if (c >= 0)
6453 {
6454 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6455 if (CONSP (glyphless_method))
6456 glyphless_method = FRAME_WINDOW_P (it->f)
6457 ? XCAR (glyphless_method)
6458 : XCDR (glyphless_method);
6459 }
6460 else
6461 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6462 }
6463
6464 retry:
6465 if (NILP (glyphless_method))
6466 {
6467 if (c >= 0)
6468 /* The default is to display the character by a proper font. */
6469 return Qnil;
6470 /* The default for the no-font case is to display an empty box. */
6471 glyphless_method = Qempty_box;
6472 }
6473 if (EQ (glyphless_method, Qzero_width))
6474 {
6475 if (c >= 0)
6476 return glyphless_method;
6477 /* This method can't be used for the no-font case. */
6478 glyphless_method = Qempty_box;
6479 }
6480 if (EQ (glyphless_method, Qthin_space))
6481 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6482 else if (EQ (glyphless_method, Qempty_box))
6483 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6484 else if (EQ (glyphless_method, Qhex_code))
6485 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6486 else if (STRINGP (glyphless_method))
6487 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6488 else
6489 {
6490 /* Invalid value. We use the default method. */
6491 glyphless_method = Qnil;
6492 goto retry;
6493 }
6494 it->what = IT_GLYPHLESS;
6495 return glyphless_method;
6496 }
6497
6498 /* Load IT's display element fields with information about the next
6499 display element from the current position of IT. Value is zero if
6500 end of buffer (or C string) is reached. */
6501
6502 static struct frame *last_escape_glyph_frame = NULL;
6503 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6504 static int last_escape_glyph_merged_face_id = 0;
6505
6506 struct frame *last_glyphless_glyph_frame = NULL;
6507 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6508 int last_glyphless_glyph_merged_face_id = 0;
6509
6510 static int
6511 get_next_display_element (struct it *it)
6512 {
6513 /* Non-zero means that we found a display element. Zero means that
6514 we hit the end of what we iterate over. Performance note: the
6515 function pointer `method' used here turns out to be faster than
6516 using a sequence of if-statements. */
6517 int success_p;
6518
6519 get_next:
6520 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6521
6522 if (it->what == IT_CHARACTER)
6523 {
6524 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6525 and only if (a) the resolved directionality of that character
6526 is R..." */
6527 /* FIXME: Do we need an exception for characters from display
6528 tables? */
6529 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6530 it->c = bidi_mirror_char (it->c);
6531 /* Map via display table or translate control characters.
6532 IT->c, IT->len etc. have been set to the next character by
6533 the function call above. If we have a display table, and it
6534 contains an entry for IT->c, translate it. Don't do this if
6535 IT->c itself comes from a display table, otherwise we could
6536 end up in an infinite recursion. (An alternative could be to
6537 count the recursion depth of this function and signal an
6538 error when a certain maximum depth is reached.) Is it worth
6539 it? */
6540 if (success_p && it->dpvec == NULL)
6541 {
6542 Lisp_Object dv;
6543 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6544 int nonascii_space_p = 0;
6545 int nonascii_hyphen_p = 0;
6546 int c = it->c; /* This is the character to display. */
6547
6548 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6549 {
6550 eassert (SINGLE_BYTE_CHAR_P (c));
6551 if (unibyte_display_via_language_environment)
6552 {
6553 c = DECODE_CHAR (unibyte, c);
6554 if (c < 0)
6555 c = BYTE8_TO_CHAR (it->c);
6556 }
6557 else
6558 c = BYTE8_TO_CHAR (it->c);
6559 }
6560
6561 if (it->dp
6562 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6563 VECTORP (dv)))
6564 {
6565 struct Lisp_Vector *v = XVECTOR (dv);
6566
6567 /* Return the first character from the display table
6568 entry, if not empty. If empty, don't display the
6569 current character. */
6570 if (v->header.size)
6571 {
6572 it->dpvec_char_len = it->len;
6573 it->dpvec = v->contents;
6574 it->dpend = v->contents + v->header.size;
6575 it->current.dpvec_index = 0;
6576 it->dpvec_face_id = -1;
6577 it->saved_face_id = it->face_id;
6578 it->method = GET_FROM_DISPLAY_VECTOR;
6579 it->ellipsis_p = 0;
6580 }
6581 else
6582 {
6583 set_iterator_to_next (it, 0);
6584 }
6585 goto get_next;
6586 }
6587
6588 if (! NILP (lookup_glyphless_char_display (c, it)))
6589 {
6590 if (it->what == IT_GLYPHLESS)
6591 goto done;
6592 /* Don't display this character. */
6593 set_iterator_to_next (it, 0);
6594 goto get_next;
6595 }
6596
6597 /* If `nobreak-char-display' is non-nil, we display
6598 non-ASCII spaces and hyphens specially. */
6599 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6600 {
6601 if (c == 0xA0)
6602 nonascii_space_p = 1;
6603 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6604 nonascii_hyphen_p = 1;
6605 }
6606
6607 /* Translate control characters into `\003' or `^C' form.
6608 Control characters coming from a display table entry are
6609 currently not translated because we use IT->dpvec to hold
6610 the translation. This could easily be changed but I
6611 don't believe that it is worth doing.
6612
6613 The characters handled by `nobreak-char-display' must be
6614 translated too.
6615
6616 Non-printable characters and raw-byte characters are also
6617 translated to octal form. */
6618 if (((c < ' ' || c == 127) /* ASCII control chars */
6619 ? (it->area != TEXT_AREA
6620 /* In mode line, treat \n, \t like other crl chars. */
6621 || (c != '\t'
6622 && it->glyph_row
6623 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6624 || (c != '\n' && c != '\t'))
6625 : (nonascii_space_p
6626 || nonascii_hyphen_p
6627 || CHAR_BYTE8_P (c)
6628 || ! CHAR_PRINTABLE_P (c))))
6629 {
6630 /* C is a control character, non-ASCII space/hyphen,
6631 raw-byte, or a non-printable character which must be
6632 displayed either as '\003' or as `^C' where the '\\'
6633 and '^' can be defined in the display table. Fill
6634 IT->ctl_chars with glyphs for what we have to
6635 display. Then, set IT->dpvec to these glyphs. */
6636 Lisp_Object gc;
6637 int ctl_len;
6638 int face_id;
6639 int lface_id = 0;
6640 int escape_glyph;
6641
6642 /* Handle control characters with ^. */
6643
6644 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6645 {
6646 int g;
6647
6648 g = '^'; /* default glyph for Control */
6649 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6650 if (it->dp
6651 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6652 {
6653 g = GLYPH_CODE_CHAR (gc);
6654 lface_id = GLYPH_CODE_FACE (gc);
6655 }
6656 if (lface_id)
6657 {
6658 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6659 }
6660 else if (it->f == last_escape_glyph_frame
6661 && it->face_id == last_escape_glyph_face_id)
6662 {
6663 face_id = last_escape_glyph_merged_face_id;
6664 }
6665 else
6666 {
6667 /* Merge the escape-glyph face into the current face. */
6668 face_id = merge_faces (it->f, Qescape_glyph, 0,
6669 it->face_id);
6670 last_escape_glyph_frame = it->f;
6671 last_escape_glyph_face_id = it->face_id;
6672 last_escape_glyph_merged_face_id = face_id;
6673 }
6674
6675 XSETINT (it->ctl_chars[0], g);
6676 XSETINT (it->ctl_chars[1], c ^ 0100);
6677 ctl_len = 2;
6678 goto display_control;
6679 }
6680
6681 /* Handle non-ascii space in the mode where it only gets
6682 highlighting. */
6683
6684 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6685 {
6686 /* Merge `nobreak-space' into the current face. */
6687 face_id = merge_faces (it->f, Qnobreak_space, 0,
6688 it->face_id);
6689 XSETINT (it->ctl_chars[0], ' ');
6690 ctl_len = 1;
6691 goto display_control;
6692 }
6693
6694 /* Handle sequences that start with the "escape glyph". */
6695
6696 /* the default escape glyph is \. */
6697 escape_glyph = '\\';
6698
6699 if (it->dp
6700 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6701 {
6702 escape_glyph = GLYPH_CODE_CHAR (gc);
6703 lface_id = GLYPH_CODE_FACE (gc);
6704 }
6705 if (lface_id)
6706 {
6707 /* The display table specified a face.
6708 Merge it into face_id and also into escape_glyph. */
6709 face_id = merge_faces (it->f, Qt, lface_id,
6710 it->face_id);
6711 }
6712 else if (it->f == last_escape_glyph_frame
6713 && it->face_id == last_escape_glyph_face_id)
6714 {
6715 face_id = last_escape_glyph_merged_face_id;
6716 }
6717 else
6718 {
6719 /* Merge the escape-glyph face into the current face. */
6720 face_id = merge_faces (it->f, Qescape_glyph, 0,
6721 it->face_id);
6722 last_escape_glyph_frame = it->f;
6723 last_escape_glyph_face_id = it->face_id;
6724 last_escape_glyph_merged_face_id = face_id;
6725 }
6726
6727 /* Draw non-ASCII hyphen with just highlighting: */
6728
6729 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6730 {
6731 XSETINT (it->ctl_chars[0], '-');
6732 ctl_len = 1;
6733 goto display_control;
6734 }
6735
6736 /* Draw non-ASCII space/hyphen with escape glyph: */
6737
6738 if (nonascii_space_p || nonascii_hyphen_p)
6739 {
6740 XSETINT (it->ctl_chars[0], escape_glyph);
6741 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6742 ctl_len = 2;
6743 goto display_control;
6744 }
6745
6746 {
6747 char str[10];
6748 int len, i;
6749
6750 if (CHAR_BYTE8_P (c))
6751 /* Display \200 instead of \17777600. */
6752 c = CHAR_TO_BYTE8 (c);
6753 len = sprintf (str, "%03o", c);
6754
6755 XSETINT (it->ctl_chars[0], escape_glyph);
6756 for (i = 0; i < len; i++)
6757 XSETINT (it->ctl_chars[i + 1], str[i]);
6758 ctl_len = len + 1;
6759 }
6760
6761 display_control:
6762 /* Set up IT->dpvec and return first character from it. */
6763 it->dpvec_char_len = it->len;
6764 it->dpvec = it->ctl_chars;
6765 it->dpend = it->dpvec + ctl_len;
6766 it->current.dpvec_index = 0;
6767 it->dpvec_face_id = face_id;
6768 it->saved_face_id = it->face_id;
6769 it->method = GET_FROM_DISPLAY_VECTOR;
6770 it->ellipsis_p = 0;
6771 goto get_next;
6772 }
6773 it->char_to_display = c;
6774 }
6775 else if (success_p)
6776 {
6777 it->char_to_display = it->c;
6778 }
6779 }
6780
6781 /* Adjust face id for a multibyte character. There are no multibyte
6782 character in unibyte text. */
6783 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6784 && it->multibyte_p
6785 && success_p
6786 && FRAME_WINDOW_P (it->f))
6787 {
6788 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6789
6790 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6791 {
6792 /* Automatic composition with glyph-string. */
6793 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6794
6795 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6796 }
6797 else
6798 {
6799 ptrdiff_t pos = (it->s ? -1
6800 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6801 : IT_CHARPOS (*it));
6802 int c;
6803
6804 if (it->what == IT_CHARACTER)
6805 c = it->char_to_display;
6806 else
6807 {
6808 struct composition *cmp = composition_table[it->cmp_it.id];
6809 int i;
6810
6811 c = ' ';
6812 for (i = 0; i < cmp->glyph_len; i++)
6813 /* TAB in a composition means display glyphs with
6814 padding space on the left or right. */
6815 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6816 break;
6817 }
6818 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6819 }
6820 }
6821
6822 done:
6823 /* Is this character the last one of a run of characters with
6824 box? If yes, set IT->end_of_box_run_p to 1. */
6825 if (it->face_box_p
6826 && it->s == NULL)
6827 {
6828 if (it->method == GET_FROM_STRING && it->sp)
6829 {
6830 int face_id = underlying_face_id (it);
6831 struct face *face = FACE_FROM_ID (it->f, face_id);
6832
6833 if (face)
6834 {
6835 if (face->box == FACE_NO_BOX)
6836 {
6837 /* If the box comes from face properties in a
6838 display string, check faces in that string. */
6839 int string_face_id = face_after_it_pos (it);
6840 it->end_of_box_run_p
6841 = (FACE_FROM_ID (it->f, string_face_id)->box
6842 == FACE_NO_BOX);
6843 }
6844 /* Otherwise, the box comes from the underlying face.
6845 If this is the last string character displayed, check
6846 the next buffer location. */
6847 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6848 && (it->current.overlay_string_index
6849 == it->n_overlay_strings - 1))
6850 {
6851 ptrdiff_t ignore;
6852 int next_face_id;
6853 struct text_pos pos = it->current.pos;
6854 INC_TEXT_POS (pos, it->multibyte_p);
6855
6856 next_face_id = face_at_buffer_position
6857 (it->w, CHARPOS (pos), it->region_beg_charpos,
6858 it->region_end_charpos, &ignore,
6859 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6860 -1);
6861 it->end_of_box_run_p
6862 = (FACE_FROM_ID (it->f, next_face_id)->box
6863 == FACE_NO_BOX);
6864 }
6865 }
6866 }
6867 else
6868 {
6869 int face_id = face_after_it_pos (it);
6870 it->end_of_box_run_p
6871 = (face_id != it->face_id
6872 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6873 }
6874 }
6875 /* If we reached the end of the object we've been iterating (e.g., a
6876 display string or an overlay string), and there's something on
6877 IT->stack, proceed with what's on the stack. It doesn't make
6878 sense to return zero if there's unprocessed stuff on the stack,
6879 because otherwise that stuff will never be displayed. */
6880 if (!success_p && it->sp > 0)
6881 {
6882 set_iterator_to_next (it, 0);
6883 success_p = get_next_display_element (it);
6884 }
6885
6886 /* Value is 0 if end of buffer or string reached. */
6887 return success_p;
6888 }
6889
6890
6891 /* Move IT to the next display element.
6892
6893 RESEAT_P non-zero means if called on a newline in buffer text,
6894 skip to the next visible line start.
6895
6896 Functions get_next_display_element and set_iterator_to_next are
6897 separate because I find this arrangement easier to handle than a
6898 get_next_display_element function that also increments IT's
6899 position. The way it is we can first look at an iterator's current
6900 display element, decide whether it fits on a line, and if it does,
6901 increment the iterator position. The other way around we probably
6902 would either need a flag indicating whether the iterator has to be
6903 incremented the next time, or we would have to implement a
6904 decrement position function which would not be easy to write. */
6905
6906 void
6907 set_iterator_to_next (struct it *it, int reseat_p)
6908 {
6909 /* Reset flags indicating start and end of a sequence of characters
6910 with box. Reset them at the start of this function because
6911 moving the iterator to a new position might set them. */
6912 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6913
6914 switch (it->method)
6915 {
6916 case GET_FROM_BUFFER:
6917 /* The current display element of IT is a character from
6918 current_buffer. Advance in the buffer, and maybe skip over
6919 invisible lines that are so because of selective display. */
6920 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6921 reseat_at_next_visible_line_start (it, 0);
6922 else if (it->cmp_it.id >= 0)
6923 {
6924 /* We are currently getting glyphs from a composition. */
6925 int i;
6926
6927 if (! it->bidi_p)
6928 {
6929 IT_CHARPOS (*it) += it->cmp_it.nchars;
6930 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6931 if (it->cmp_it.to < it->cmp_it.nglyphs)
6932 {
6933 it->cmp_it.from = it->cmp_it.to;
6934 }
6935 else
6936 {
6937 it->cmp_it.id = -1;
6938 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6939 IT_BYTEPOS (*it),
6940 it->end_charpos, Qnil);
6941 }
6942 }
6943 else if (! it->cmp_it.reversed_p)
6944 {
6945 /* Composition created while scanning forward. */
6946 /* Update IT's char/byte positions to point to the first
6947 character of the next grapheme cluster, or to the
6948 character visually after the current composition. */
6949 for (i = 0; i < it->cmp_it.nchars; i++)
6950 bidi_move_to_visually_next (&it->bidi_it);
6951 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6952 IT_CHARPOS (*it) = it->bidi_it.charpos;
6953
6954 if (it->cmp_it.to < it->cmp_it.nglyphs)
6955 {
6956 /* Proceed to the next grapheme cluster. */
6957 it->cmp_it.from = it->cmp_it.to;
6958 }
6959 else
6960 {
6961 /* No more grapheme clusters in this composition.
6962 Find the next stop position. */
6963 ptrdiff_t stop = it->end_charpos;
6964 if (it->bidi_it.scan_dir < 0)
6965 /* Now we are scanning backward and don't know
6966 where to stop. */
6967 stop = -1;
6968 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6969 IT_BYTEPOS (*it), stop, Qnil);
6970 }
6971 }
6972 else
6973 {
6974 /* Composition created while scanning backward. */
6975 /* Update IT's char/byte positions to point to the last
6976 character of the previous grapheme cluster, or the
6977 character visually after the current composition. */
6978 for (i = 0; i < it->cmp_it.nchars; i++)
6979 bidi_move_to_visually_next (&it->bidi_it);
6980 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6981 IT_CHARPOS (*it) = it->bidi_it.charpos;
6982 if (it->cmp_it.from > 0)
6983 {
6984 /* Proceed to the previous grapheme cluster. */
6985 it->cmp_it.to = it->cmp_it.from;
6986 }
6987 else
6988 {
6989 /* No more grapheme clusters in this composition.
6990 Find the next stop position. */
6991 ptrdiff_t stop = it->end_charpos;
6992 if (it->bidi_it.scan_dir < 0)
6993 /* Now we are scanning backward and don't know
6994 where to stop. */
6995 stop = -1;
6996 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6997 IT_BYTEPOS (*it), stop, Qnil);
6998 }
6999 }
7000 }
7001 else
7002 {
7003 eassert (it->len != 0);
7004
7005 if (!it->bidi_p)
7006 {
7007 IT_BYTEPOS (*it) += it->len;
7008 IT_CHARPOS (*it) += 1;
7009 }
7010 else
7011 {
7012 int prev_scan_dir = it->bidi_it.scan_dir;
7013 /* If this is a new paragraph, determine its base
7014 direction (a.k.a. its base embedding level). */
7015 if (it->bidi_it.new_paragraph)
7016 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7017 bidi_move_to_visually_next (&it->bidi_it);
7018 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7019 IT_CHARPOS (*it) = it->bidi_it.charpos;
7020 if (prev_scan_dir != it->bidi_it.scan_dir)
7021 {
7022 /* As the scan direction was changed, we must
7023 re-compute the stop position for composition. */
7024 ptrdiff_t stop = it->end_charpos;
7025 if (it->bidi_it.scan_dir < 0)
7026 stop = -1;
7027 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7028 IT_BYTEPOS (*it), stop, Qnil);
7029 }
7030 }
7031 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7032 }
7033 break;
7034
7035 case GET_FROM_C_STRING:
7036 /* Current display element of IT is from a C string. */
7037 if (!it->bidi_p
7038 /* If the string position is beyond string's end, it means
7039 next_element_from_c_string is padding the string with
7040 blanks, in which case we bypass the bidi iterator,
7041 because it cannot deal with such virtual characters. */
7042 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7043 {
7044 IT_BYTEPOS (*it) += it->len;
7045 IT_CHARPOS (*it) += 1;
7046 }
7047 else
7048 {
7049 bidi_move_to_visually_next (&it->bidi_it);
7050 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7051 IT_CHARPOS (*it) = it->bidi_it.charpos;
7052 }
7053 break;
7054
7055 case GET_FROM_DISPLAY_VECTOR:
7056 /* Current display element of IT is from a display table entry.
7057 Advance in the display table definition. Reset it to null if
7058 end reached, and continue with characters from buffers/
7059 strings. */
7060 ++it->current.dpvec_index;
7061
7062 /* Restore face of the iterator to what they were before the
7063 display vector entry (these entries may contain faces). */
7064 it->face_id = it->saved_face_id;
7065
7066 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7067 {
7068 int recheck_faces = it->ellipsis_p;
7069
7070 if (it->s)
7071 it->method = GET_FROM_C_STRING;
7072 else if (STRINGP (it->string))
7073 it->method = GET_FROM_STRING;
7074 else
7075 {
7076 it->method = GET_FROM_BUFFER;
7077 it->object = it->w->buffer;
7078 }
7079
7080 it->dpvec = NULL;
7081 it->current.dpvec_index = -1;
7082
7083 /* Skip over characters which were displayed via IT->dpvec. */
7084 if (it->dpvec_char_len < 0)
7085 reseat_at_next_visible_line_start (it, 1);
7086 else if (it->dpvec_char_len > 0)
7087 {
7088 if (it->method == GET_FROM_STRING
7089 && it->n_overlay_strings > 0)
7090 it->ignore_overlay_strings_at_pos_p = 1;
7091 it->len = it->dpvec_char_len;
7092 set_iterator_to_next (it, reseat_p);
7093 }
7094
7095 /* Maybe recheck faces after display vector */
7096 if (recheck_faces)
7097 it->stop_charpos = IT_CHARPOS (*it);
7098 }
7099 break;
7100
7101 case GET_FROM_STRING:
7102 /* Current display element is a character from a Lisp string. */
7103 eassert (it->s == NULL && STRINGP (it->string));
7104 /* Don't advance past string end. These conditions are true
7105 when set_iterator_to_next is called at the end of
7106 get_next_display_element, in which case the Lisp string is
7107 already exhausted, and all we want is pop the iterator
7108 stack. */
7109 if (it->current.overlay_string_index >= 0)
7110 {
7111 /* This is an overlay string, so there's no padding with
7112 spaces, and the number of characters in the string is
7113 where the string ends. */
7114 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7115 goto consider_string_end;
7116 }
7117 else
7118 {
7119 /* Not an overlay string. There could be padding, so test
7120 against it->end_charpos . */
7121 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7122 goto consider_string_end;
7123 }
7124 if (it->cmp_it.id >= 0)
7125 {
7126 int i;
7127
7128 if (! it->bidi_p)
7129 {
7130 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7131 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7132 if (it->cmp_it.to < it->cmp_it.nglyphs)
7133 it->cmp_it.from = it->cmp_it.to;
7134 else
7135 {
7136 it->cmp_it.id = -1;
7137 composition_compute_stop_pos (&it->cmp_it,
7138 IT_STRING_CHARPOS (*it),
7139 IT_STRING_BYTEPOS (*it),
7140 it->end_charpos, it->string);
7141 }
7142 }
7143 else if (! it->cmp_it.reversed_p)
7144 {
7145 for (i = 0; i < it->cmp_it.nchars; i++)
7146 bidi_move_to_visually_next (&it->bidi_it);
7147 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7148 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7149
7150 if (it->cmp_it.to < it->cmp_it.nglyphs)
7151 it->cmp_it.from = it->cmp_it.to;
7152 else
7153 {
7154 ptrdiff_t stop = it->end_charpos;
7155 if (it->bidi_it.scan_dir < 0)
7156 stop = -1;
7157 composition_compute_stop_pos (&it->cmp_it,
7158 IT_STRING_CHARPOS (*it),
7159 IT_STRING_BYTEPOS (*it), stop,
7160 it->string);
7161 }
7162 }
7163 else
7164 {
7165 for (i = 0; i < it->cmp_it.nchars; i++)
7166 bidi_move_to_visually_next (&it->bidi_it);
7167 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7168 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7169 if (it->cmp_it.from > 0)
7170 it->cmp_it.to = it->cmp_it.from;
7171 else
7172 {
7173 ptrdiff_t stop = it->end_charpos;
7174 if (it->bidi_it.scan_dir < 0)
7175 stop = -1;
7176 composition_compute_stop_pos (&it->cmp_it,
7177 IT_STRING_CHARPOS (*it),
7178 IT_STRING_BYTEPOS (*it), stop,
7179 it->string);
7180 }
7181 }
7182 }
7183 else
7184 {
7185 if (!it->bidi_p
7186 /* If the string position is beyond string's end, it
7187 means next_element_from_string is padding the string
7188 with blanks, in which case we bypass the bidi
7189 iterator, because it cannot deal with such virtual
7190 characters. */
7191 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7192 {
7193 IT_STRING_BYTEPOS (*it) += it->len;
7194 IT_STRING_CHARPOS (*it) += 1;
7195 }
7196 else
7197 {
7198 int prev_scan_dir = it->bidi_it.scan_dir;
7199
7200 bidi_move_to_visually_next (&it->bidi_it);
7201 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7202 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7203 if (prev_scan_dir != it->bidi_it.scan_dir)
7204 {
7205 ptrdiff_t stop = it->end_charpos;
7206
7207 if (it->bidi_it.scan_dir < 0)
7208 stop = -1;
7209 composition_compute_stop_pos (&it->cmp_it,
7210 IT_STRING_CHARPOS (*it),
7211 IT_STRING_BYTEPOS (*it), stop,
7212 it->string);
7213 }
7214 }
7215 }
7216
7217 consider_string_end:
7218
7219 if (it->current.overlay_string_index >= 0)
7220 {
7221 /* IT->string is an overlay string. Advance to the
7222 next, if there is one. */
7223 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7224 {
7225 it->ellipsis_p = 0;
7226 next_overlay_string (it);
7227 if (it->ellipsis_p)
7228 setup_for_ellipsis (it, 0);
7229 }
7230 }
7231 else
7232 {
7233 /* IT->string is not an overlay string. If we reached
7234 its end, and there is something on IT->stack, proceed
7235 with what is on the stack. This can be either another
7236 string, this time an overlay string, or a buffer. */
7237 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7238 && it->sp > 0)
7239 {
7240 pop_it (it);
7241 if (it->method == GET_FROM_STRING)
7242 goto consider_string_end;
7243 }
7244 }
7245 break;
7246
7247 case GET_FROM_IMAGE:
7248 case GET_FROM_STRETCH:
7249 /* The position etc with which we have to proceed are on
7250 the stack. The position may be at the end of a string,
7251 if the `display' property takes up the whole string. */
7252 eassert (it->sp > 0);
7253 pop_it (it);
7254 if (it->method == GET_FROM_STRING)
7255 goto consider_string_end;
7256 break;
7257
7258 default:
7259 /* There are no other methods defined, so this should be a bug. */
7260 abort ();
7261 }
7262
7263 eassert (it->method != GET_FROM_STRING
7264 || (STRINGP (it->string)
7265 && IT_STRING_CHARPOS (*it) >= 0));
7266 }
7267
7268 /* Load IT's display element fields with information about the next
7269 display element which comes from a display table entry or from the
7270 result of translating a control character to one of the forms `^C'
7271 or `\003'.
7272
7273 IT->dpvec holds the glyphs to return as characters.
7274 IT->saved_face_id holds the face id before the display vector--it
7275 is restored into IT->face_id in set_iterator_to_next. */
7276
7277 static int
7278 next_element_from_display_vector (struct it *it)
7279 {
7280 Lisp_Object gc;
7281
7282 /* Precondition. */
7283 eassert (it->dpvec && it->current.dpvec_index >= 0);
7284
7285 it->face_id = it->saved_face_id;
7286
7287 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7288 That seemed totally bogus - so I changed it... */
7289 gc = it->dpvec[it->current.dpvec_index];
7290
7291 if (GLYPH_CODE_P (gc))
7292 {
7293 it->c = GLYPH_CODE_CHAR (gc);
7294 it->len = CHAR_BYTES (it->c);
7295
7296 /* The entry may contain a face id to use. Such a face id is
7297 the id of a Lisp face, not a realized face. A face id of
7298 zero means no face is specified. */
7299 if (it->dpvec_face_id >= 0)
7300 it->face_id = it->dpvec_face_id;
7301 else
7302 {
7303 int lface_id = GLYPH_CODE_FACE (gc);
7304 if (lface_id > 0)
7305 it->face_id = merge_faces (it->f, Qt, lface_id,
7306 it->saved_face_id);
7307 }
7308 }
7309 else
7310 /* Display table entry is invalid. Return a space. */
7311 it->c = ' ', it->len = 1;
7312
7313 /* Don't change position and object of the iterator here. They are
7314 still the values of the character that had this display table
7315 entry or was translated, and that's what we want. */
7316 it->what = IT_CHARACTER;
7317 return 1;
7318 }
7319
7320 /* Get the first element of string/buffer in the visual order, after
7321 being reseated to a new position in a string or a buffer. */
7322 static void
7323 get_visually_first_element (struct it *it)
7324 {
7325 int string_p = STRINGP (it->string) || it->s;
7326 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7327 ptrdiff_t bob = (string_p ? 0 : BEGV);
7328
7329 if (STRINGP (it->string))
7330 {
7331 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7332 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7333 }
7334 else
7335 {
7336 it->bidi_it.charpos = IT_CHARPOS (*it);
7337 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7338 }
7339
7340 if (it->bidi_it.charpos == eob)
7341 {
7342 /* Nothing to do, but reset the FIRST_ELT flag, like
7343 bidi_paragraph_init does, because we are not going to
7344 call it. */
7345 it->bidi_it.first_elt = 0;
7346 }
7347 else if (it->bidi_it.charpos == bob
7348 || (!string_p
7349 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7350 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7351 {
7352 /* If we are at the beginning of a line/string, we can produce
7353 the next element right away. */
7354 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7355 bidi_move_to_visually_next (&it->bidi_it);
7356 }
7357 else
7358 {
7359 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7360
7361 /* We need to prime the bidi iterator starting at the line's or
7362 string's beginning, before we will be able to produce the
7363 next element. */
7364 if (string_p)
7365 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7366 else
7367 {
7368 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7369 -1);
7370 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7371 }
7372 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7373 do
7374 {
7375 /* Now return to buffer/string position where we were asked
7376 to get the next display element, and produce that. */
7377 bidi_move_to_visually_next (&it->bidi_it);
7378 }
7379 while (it->bidi_it.bytepos != orig_bytepos
7380 && it->bidi_it.charpos < eob);
7381 }
7382
7383 /* Adjust IT's position information to where we ended up. */
7384 if (STRINGP (it->string))
7385 {
7386 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7387 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7388 }
7389 else
7390 {
7391 IT_CHARPOS (*it) = it->bidi_it.charpos;
7392 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7393 }
7394
7395 if (STRINGP (it->string) || !it->s)
7396 {
7397 ptrdiff_t stop, charpos, bytepos;
7398
7399 if (STRINGP (it->string))
7400 {
7401 eassert (!it->s);
7402 stop = SCHARS (it->string);
7403 if (stop > it->end_charpos)
7404 stop = it->end_charpos;
7405 charpos = IT_STRING_CHARPOS (*it);
7406 bytepos = IT_STRING_BYTEPOS (*it);
7407 }
7408 else
7409 {
7410 stop = it->end_charpos;
7411 charpos = IT_CHARPOS (*it);
7412 bytepos = IT_BYTEPOS (*it);
7413 }
7414 if (it->bidi_it.scan_dir < 0)
7415 stop = -1;
7416 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7417 it->string);
7418 }
7419 }
7420
7421 /* Load IT with the next display element from Lisp string IT->string.
7422 IT->current.string_pos is the current position within the string.
7423 If IT->current.overlay_string_index >= 0, the Lisp string is an
7424 overlay string. */
7425
7426 static int
7427 next_element_from_string (struct it *it)
7428 {
7429 struct text_pos position;
7430
7431 eassert (STRINGP (it->string));
7432 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7433 eassert (IT_STRING_CHARPOS (*it) >= 0);
7434 position = it->current.string_pos;
7435
7436 /* With bidi reordering, the character to display might not be the
7437 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7438 that we were reseat()ed to a new string, whose paragraph
7439 direction is not known. */
7440 if (it->bidi_p && it->bidi_it.first_elt)
7441 {
7442 get_visually_first_element (it);
7443 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7444 }
7445
7446 /* Time to check for invisible text? */
7447 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7448 {
7449 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7450 {
7451 if (!(!it->bidi_p
7452 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7453 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7454 {
7455 /* With bidi non-linear iteration, we could find
7456 ourselves far beyond the last computed stop_charpos,
7457 with several other stop positions in between that we
7458 missed. Scan them all now, in buffer's logical
7459 order, until we find and handle the last stop_charpos
7460 that precedes our current position. */
7461 handle_stop_backwards (it, it->stop_charpos);
7462 return GET_NEXT_DISPLAY_ELEMENT (it);
7463 }
7464 else
7465 {
7466 if (it->bidi_p)
7467 {
7468 /* Take note of the stop position we just moved
7469 across, for when we will move back across it. */
7470 it->prev_stop = it->stop_charpos;
7471 /* If we are at base paragraph embedding level, take
7472 note of the last stop position seen at this
7473 level. */
7474 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7475 it->base_level_stop = it->stop_charpos;
7476 }
7477 handle_stop (it);
7478
7479 /* Since a handler may have changed IT->method, we must
7480 recurse here. */
7481 return GET_NEXT_DISPLAY_ELEMENT (it);
7482 }
7483 }
7484 else if (it->bidi_p
7485 /* If we are before prev_stop, we may have overstepped
7486 on our way backwards a stop_pos, and if so, we need
7487 to handle that stop_pos. */
7488 && IT_STRING_CHARPOS (*it) < it->prev_stop
7489 /* We can sometimes back up for reasons that have nothing
7490 to do with bidi reordering. E.g., compositions. The
7491 code below is only needed when we are above the base
7492 embedding level, so test for that explicitly. */
7493 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7494 {
7495 /* If we lost track of base_level_stop, we have no better
7496 place for handle_stop_backwards to start from than string
7497 beginning. This happens, e.g., when we were reseated to
7498 the previous screenful of text by vertical-motion. */
7499 if (it->base_level_stop <= 0
7500 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7501 it->base_level_stop = 0;
7502 handle_stop_backwards (it, it->base_level_stop);
7503 return GET_NEXT_DISPLAY_ELEMENT (it);
7504 }
7505 }
7506
7507 if (it->current.overlay_string_index >= 0)
7508 {
7509 /* Get the next character from an overlay string. In overlay
7510 strings, there is no field width or padding with spaces to
7511 do. */
7512 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7513 {
7514 it->what = IT_EOB;
7515 return 0;
7516 }
7517 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7518 IT_STRING_BYTEPOS (*it),
7519 it->bidi_it.scan_dir < 0
7520 ? -1
7521 : SCHARS (it->string))
7522 && next_element_from_composition (it))
7523 {
7524 return 1;
7525 }
7526 else if (STRING_MULTIBYTE (it->string))
7527 {
7528 const unsigned char *s = (SDATA (it->string)
7529 + IT_STRING_BYTEPOS (*it));
7530 it->c = string_char_and_length (s, &it->len);
7531 }
7532 else
7533 {
7534 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7535 it->len = 1;
7536 }
7537 }
7538 else
7539 {
7540 /* Get the next character from a Lisp string that is not an
7541 overlay string. Such strings come from the mode line, for
7542 example. We may have to pad with spaces, or truncate the
7543 string. See also next_element_from_c_string. */
7544 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7545 {
7546 it->what = IT_EOB;
7547 return 0;
7548 }
7549 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7550 {
7551 /* Pad with spaces. */
7552 it->c = ' ', it->len = 1;
7553 CHARPOS (position) = BYTEPOS (position) = -1;
7554 }
7555 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7556 IT_STRING_BYTEPOS (*it),
7557 it->bidi_it.scan_dir < 0
7558 ? -1
7559 : it->string_nchars)
7560 && next_element_from_composition (it))
7561 {
7562 return 1;
7563 }
7564 else if (STRING_MULTIBYTE (it->string))
7565 {
7566 const unsigned char *s = (SDATA (it->string)
7567 + IT_STRING_BYTEPOS (*it));
7568 it->c = string_char_and_length (s, &it->len);
7569 }
7570 else
7571 {
7572 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7573 it->len = 1;
7574 }
7575 }
7576
7577 /* Record what we have and where it came from. */
7578 it->what = IT_CHARACTER;
7579 it->object = it->string;
7580 it->position = position;
7581 return 1;
7582 }
7583
7584
7585 /* Load IT with next display element from C string IT->s.
7586 IT->string_nchars is the maximum number of characters to return
7587 from the string. IT->end_charpos may be greater than
7588 IT->string_nchars when this function is called, in which case we
7589 may have to return padding spaces. Value is zero if end of string
7590 reached, including padding spaces. */
7591
7592 static int
7593 next_element_from_c_string (struct it *it)
7594 {
7595 int success_p = 1;
7596
7597 eassert (it->s);
7598 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7599 it->what = IT_CHARACTER;
7600 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7601 it->object = Qnil;
7602
7603 /* With bidi reordering, the character to display might not be the
7604 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7605 we were reseated to a new string, whose paragraph direction is
7606 not known. */
7607 if (it->bidi_p && it->bidi_it.first_elt)
7608 get_visually_first_element (it);
7609
7610 /* IT's position can be greater than IT->string_nchars in case a
7611 field width or precision has been specified when the iterator was
7612 initialized. */
7613 if (IT_CHARPOS (*it) >= it->end_charpos)
7614 {
7615 /* End of the game. */
7616 it->what = IT_EOB;
7617 success_p = 0;
7618 }
7619 else if (IT_CHARPOS (*it) >= it->string_nchars)
7620 {
7621 /* Pad with spaces. */
7622 it->c = ' ', it->len = 1;
7623 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7624 }
7625 else if (it->multibyte_p)
7626 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7627 else
7628 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7629
7630 return success_p;
7631 }
7632
7633
7634 /* Set up IT to return characters from an ellipsis, if appropriate.
7635 The definition of the ellipsis glyphs may come from a display table
7636 entry. This function fills IT with the first glyph from the
7637 ellipsis if an ellipsis is to be displayed. */
7638
7639 static int
7640 next_element_from_ellipsis (struct it *it)
7641 {
7642 if (it->selective_display_ellipsis_p)
7643 setup_for_ellipsis (it, it->len);
7644 else
7645 {
7646 /* The face at the current position may be different from the
7647 face we find after the invisible text. Remember what it
7648 was in IT->saved_face_id, and signal that it's there by
7649 setting face_before_selective_p. */
7650 it->saved_face_id = it->face_id;
7651 it->method = GET_FROM_BUFFER;
7652 it->object = it->w->buffer;
7653 reseat_at_next_visible_line_start (it, 1);
7654 it->face_before_selective_p = 1;
7655 }
7656
7657 return GET_NEXT_DISPLAY_ELEMENT (it);
7658 }
7659
7660
7661 /* Deliver an image display element. The iterator IT is already
7662 filled with image information (done in handle_display_prop). Value
7663 is always 1. */
7664
7665
7666 static int
7667 next_element_from_image (struct it *it)
7668 {
7669 it->what = IT_IMAGE;
7670 it->ignore_overlay_strings_at_pos_p = 0;
7671 return 1;
7672 }
7673
7674
7675 /* Fill iterator IT with next display element from a stretch glyph
7676 property. IT->object is the value of the text property. Value is
7677 always 1. */
7678
7679 static int
7680 next_element_from_stretch (struct it *it)
7681 {
7682 it->what = IT_STRETCH;
7683 return 1;
7684 }
7685
7686 /* Scan backwards from IT's current position until we find a stop
7687 position, or until BEGV. This is called when we find ourself
7688 before both the last known prev_stop and base_level_stop while
7689 reordering bidirectional text. */
7690
7691 static void
7692 compute_stop_pos_backwards (struct it *it)
7693 {
7694 const int SCAN_BACK_LIMIT = 1000;
7695 struct text_pos pos;
7696 struct display_pos save_current = it->current;
7697 struct text_pos save_position = it->position;
7698 ptrdiff_t charpos = IT_CHARPOS (*it);
7699 ptrdiff_t where_we_are = charpos;
7700 ptrdiff_t save_stop_pos = it->stop_charpos;
7701 ptrdiff_t save_end_pos = it->end_charpos;
7702
7703 eassert (NILP (it->string) && !it->s);
7704 eassert (it->bidi_p);
7705 it->bidi_p = 0;
7706 do
7707 {
7708 it->end_charpos = min (charpos + 1, ZV);
7709 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7710 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7711 reseat_1 (it, pos, 0);
7712 compute_stop_pos (it);
7713 /* We must advance forward, right? */
7714 if (it->stop_charpos <= charpos)
7715 abort ();
7716 }
7717 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7718
7719 if (it->stop_charpos <= where_we_are)
7720 it->prev_stop = it->stop_charpos;
7721 else
7722 it->prev_stop = BEGV;
7723 it->bidi_p = 1;
7724 it->current = save_current;
7725 it->position = save_position;
7726 it->stop_charpos = save_stop_pos;
7727 it->end_charpos = save_end_pos;
7728 }
7729
7730 /* Scan forward from CHARPOS in the current buffer/string, until we
7731 find a stop position > current IT's position. Then handle the stop
7732 position before that. This is called when we bump into a stop
7733 position while reordering bidirectional text. CHARPOS should be
7734 the last previously processed stop_pos (or BEGV/0, if none were
7735 processed yet) whose position is less that IT's current
7736 position. */
7737
7738 static void
7739 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7740 {
7741 int bufp = !STRINGP (it->string);
7742 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7743 struct display_pos save_current = it->current;
7744 struct text_pos save_position = it->position;
7745 struct text_pos pos1;
7746 ptrdiff_t next_stop;
7747
7748 /* Scan in strict logical order. */
7749 eassert (it->bidi_p);
7750 it->bidi_p = 0;
7751 do
7752 {
7753 it->prev_stop = charpos;
7754 if (bufp)
7755 {
7756 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7757 reseat_1 (it, pos1, 0);
7758 }
7759 else
7760 it->current.string_pos = string_pos (charpos, it->string);
7761 compute_stop_pos (it);
7762 /* We must advance forward, right? */
7763 if (it->stop_charpos <= it->prev_stop)
7764 abort ();
7765 charpos = it->stop_charpos;
7766 }
7767 while (charpos <= where_we_are);
7768
7769 it->bidi_p = 1;
7770 it->current = save_current;
7771 it->position = save_position;
7772 next_stop = it->stop_charpos;
7773 it->stop_charpos = it->prev_stop;
7774 handle_stop (it);
7775 it->stop_charpos = next_stop;
7776 }
7777
7778 /* Load IT with the next display element from current_buffer. Value
7779 is zero if end of buffer reached. IT->stop_charpos is the next
7780 position at which to stop and check for text properties or buffer
7781 end. */
7782
7783 static int
7784 next_element_from_buffer (struct it *it)
7785 {
7786 int success_p = 1;
7787
7788 eassert (IT_CHARPOS (*it) >= BEGV);
7789 eassert (NILP (it->string) && !it->s);
7790 eassert (!it->bidi_p
7791 || (EQ (it->bidi_it.string.lstring, Qnil)
7792 && it->bidi_it.string.s == NULL));
7793
7794 /* With bidi reordering, the character to display might not be the
7795 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7796 we were reseat()ed to a new buffer position, which is potentially
7797 a different paragraph. */
7798 if (it->bidi_p && it->bidi_it.first_elt)
7799 {
7800 get_visually_first_element (it);
7801 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7802 }
7803
7804 if (IT_CHARPOS (*it) >= it->stop_charpos)
7805 {
7806 if (IT_CHARPOS (*it) >= it->end_charpos)
7807 {
7808 int overlay_strings_follow_p;
7809
7810 /* End of the game, except when overlay strings follow that
7811 haven't been returned yet. */
7812 if (it->overlay_strings_at_end_processed_p)
7813 overlay_strings_follow_p = 0;
7814 else
7815 {
7816 it->overlay_strings_at_end_processed_p = 1;
7817 overlay_strings_follow_p = get_overlay_strings (it, 0);
7818 }
7819
7820 if (overlay_strings_follow_p)
7821 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7822 else
7823 {
7824 it->what = IT_EOB;
7825 it->position = it->current.pos;
7826 success_p = 0;
7827 }
7828 }
7829 else if (!(!it->bidi_p
7830 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7831 || IT_CHARPOS (*it) == it->stop_charpos))
7832 {
7833 /* With bidi non-linear iteration, we could find ourselves
7834 far beyond the last computed stop_charpos, with several
7835 other stop positions in between that we missed. Scan
7836 them all now, in buffer's logical order, until we find
7837 and handle the last stop_charpos that precedes our
7838 current position. */
7839 handle_stop_backwards (it, it->stop_charpos);
7840 return GET_NEXT_DISPLAY_ELEMENT (it);
7841 }
7842 else
7843 {
7844 if (it->bidi_p)
7845 {
7846 /* Take note of the stop position we just moved across,
7847 for when we will move back across it. */
7848 it->prev_stop = it->stop_charpos;
7849 /* If we are at base paragraph embedding level, take
7850 note of the last stop position seen at this
7851 level. */
7852 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7853 it->base_level_stop = it->stop_charpos;
7854 }
7855 handle_stop (it);
7856 return GET_NEXT_DISPLAY_ELEMENT (it);
7857 }
7858 }
7859 else if (it->bidi_p
7860 /* If we are before prev_stop, we may have overstepped on
7861 our way backwards a stop_pos, and if so, we need to
7862 handle that stop_pos. */
7863 && IT_CHARPOS (*it) < it->prev_stop
7864 /* We can sometimes back up for reasons that have nothing
7865 to do with bidi reordering. E.g., compositions. The
7866 code below is only needed when we are above the base
7867 embedding level, so test for that explicitly. */
7868 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7869 {
7870 if (it->base_level_stop <= 0
7871 || IT_CHARPOS (*it) < it->base_level_stop)
7872 {
7873 /* If we lost track of base_level_stop, we need to find
7874 prev_stop by looking backwards. This happens, e.g., when
7875 we were reseated to the previous screenful of text by
7876 vertical-motion. */
7877 it->base_level_stop = BEGV;
7878 compute_stop_pos_backwards (it);
7879 handle_stop_backwards (it, it->prev_stop);
7880 }
7881 else
7882 handle_stop_backwards (it, it->base_level_stop);
7883 return GET_NEXT_DISPLAY_ELEMENT (it);
7884 }
7885 else
7886 {
7887 /* No face changes, overlays etc. in sight, so just return a
7888 character from current_buffer. */
7889 unsigned char *p;
7890 ptrdiff_t stop;
7891
7892 /* Maybe run the redisplay end trigger hook. Performance note:
7893 This doesn't seem to cost measurable time. */
7894 if (it->redisplay_end_trigger_charpos
7895 && it->glyph_row
7896 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7897 run_redisplay_end_trigger_hook (it);
7898
7899 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7900 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7901 stop)
7902 && next_element_from_composition (it))
7903 {
7904 return 1;
7905 }
7906
7907 /* Get the next character, maybe multibyte. */
7908 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7909 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7910 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7911 else
7912 it->c = *p, it->len = 1;
7913
7914 /* Record what we have and where it came from. */
7915 it->what = IT_CHARACTER;
7916 it->object = it->w->buffer;
7917 it->position = it->current.pos;
7918
7919 /* Normally we return the character found above, except when we
7920 really want to return an ellipsis for selective display. */
7921 if (it->selective)
7922 {
7923 if (it->c == '\n')
7924 {
7925 /* A value of selective > 0 means hide lines indented more
7926 than that number of columns. */
7927 if (it->selective > 0
7928 && IT_CHARPOS (*it) + 1 < ZV
7929 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7930 IT_BYTEPOS (*it) + 1,
7931 it->selective))
7932 {
7933 success_p = next_element_from_ellipsis (it);
7934 it->dpvec_char_len = -1;
7935 }
7936 }
7937 else if (it->c == '\r' && it->selective == -1)
7938 {
7939 /* A value of selective == -1 means that everything from the
7940 CR to the end of the line is invisible, with maybe an
7941 ellipsis displayed for it. */
7942 success_p = next_element_from_ellipsis (it);
7943 it->dpvec_char_len = -1;
7944 }
7945 }
7946 }
7947
7948 /* Value is zero if end of buffer reached. */
7949 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7950 return success_p;
7951 }
7952
7953
7954 /* Run the redisplay end trigger hook for IT. */
7955
7956 static void
7957 run_redisplay_end_trigger_hook (struct it *it)
7958 {
7959 Lisp_Object args[3];
7960
7961 /* IT->glyph_row should be non-null, i.e. we should be actually
7962 displaying something, or otherwise we should not run the hook. */
7963 eassert (it->glyph_row);
7964
7965 /* Set up hook arguments. */
7966 args[0] = Qredisplay_end_trigger_functions;
7967 args[1] = it->window;
7968 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7969 it->redisplay_end_trigger_charpos = 0;
7970
7971 /* Since we are *trying* to run these functions, don't try to run
7972 them again, even if they get an error. */
7973 it->w->redisplay_end_trigger = Qnil;
7974 Frun_hook_with_args (3, args);
7975
7976 /* Notice if it changed the face of the character we are on. */
7977 handle_face_prop (it);
7978 }
7979
7980
7981 /* Deliver a composition display element. Unlike the other
7982 next_element_from_XXX, this function is not registered in the array
7983 get_next_element[]. It is called from next_element_from_buffer and
7984 next_element_from_string when necessary. */
7985
7986 static int
7987 next_element_from_composition (struct it *it)
7988 {
7989 it->what = IT_COMPOSITION;
7990 it->len = it->cmp_it.nbytes;
7991 if (STRINGP (it->string))
7992 {
7993 if (it->c < 0)
7994 {
7995 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7996 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7997 return 0;
7998 }
7999 it->position = it->current.string_pos;
8000 it->object = it->string;
8001 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8002 IT_STRING_BYTEPOS (*it), it->string);
8003 }
8004 else
8005 {
8006 if (it->c < 0)
8007 {
8008 IT_CHARPOS (*it) += it->cmp_it.nchars;
8009 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8010 if (it->bidi_p)
8011 {
8012 if (it->bidi_it.new_paragraph)
8013 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8014 /* Resync the bidi iterator with IT's new position.
8015 FIXME: this doesn't support bidirectional text. */
8016 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8017 bidi_move_to_visually_next (&it->bidi_it);
8018 }
8019 return 0;
8020 }
8021 it->position = it->current.pos;
8022 it->object = it->w->buffer;
8023 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8024 IT_BYTEPOS (*it), Qnil);
8025 }
8026 return 1;
8027 }
8028
8029
8030 \f
8031 /***********************************************************************
8032 Moving an iterator without producing glyphs
8033 ***********************************************************************/
8034
8035 /* Check if iterator is at a position corresponding to a valid buffer
8036 position after some move_it_ call. */
8037
8038 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8039 ((it)->method == GET_FROM_STRING \
8040 ? IT_STRING_CHARPOS (*it) == 0 \
8041 : 1)
8042
8043
8044 /* Move iterator IT to a specified buffer or X position within one
8045 line on the display without producing glyphs.
8046
8047 OP should be a bit mask including some or all of these bits:
8048 MOVE_TO_X: Stop upon reaching x-position TO_X.
8049 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8050 Regardless of OP's value, stop upon reaching the end of the display line.
8051
8052 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8053 This means, in particular, that TO_X includes window's horizontal
8054 scroll amount.
8055
8056 The return value has several possible values that
8057 say what condition caused the scan to stop:
8058
8059 MOVE_POS_MATCH_OR_ZV
8060 - when TO_POS or ZV was reached.
8061
8062 MOVE_X_REACHED
8063 -when TO_X was reached before TO_POS or ZV were reached.
8064
8065 MOVE_LINE_CONTINUED
8066 - when we reached the end of the display area and the line must
8067 be continued.
8068
8069 MOVE_LINE_TRUNCATED
8070 - when we reached the end of the display area and the line is
8071 truncated.
8072
8073 MOVE_NEWLINE_OR_CR
8074 - when we stopped at a line end, i.e. a newline or a CR and selective
8075 display is on. */
8076
8077 static enum move_it_result
8078 move_it_in_display_line_to (struct it *it,
8079 ptrdiff_t to_charpos, int to_x,
8080 enum move_operation_enum op)
8081 {
8082 enum move_it_result result = MOVE_UNDEFINED;
8083 struct glyph_row *saved_glyph_row;
8084 struct it wrap_it, atpos_it, atx_it, ppos_it;
8085 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8086 void *ppos_data = NULL;
8087 int may_wrap = 0;
8088 enum it_method prev_method = it->method;
8089 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8090 int saw_smaller_pos = prev_pos < to_charpos;
8091
8092 /* Don't produce glyphs in produce_glyphs. */
8093 saved_glyph_row = it->glyph_row;
8094 it->glyph_row = NULL;
8095
8096 /* Use wrap_it to save a copy of IT wherever a word wrap could
8097 occur. Use atpos_it to save a copy of IT at the desired buffer
8098 position, if found, so that we can scan ahead and check if the
8099 word later overshoots the window edge. Use atx_it similarly, for
8100 pixel positions. */
8101 wrap_it.sp = -1;
8102 atpos_it.sp = -1;
8103 atx_it.sp = -1;
8104
8105 /* Use ppos_it under bidi reordering to save a copy of IT for the
8106 position > CHARPOS that is the closest to CHARPOS. We restore
8107 that position in IT when we have scanned the entire display line
8108 without finding a match for CHARPOS and all the character
8109 positions are greater than CHARPOS. */
8110 if (it->bidi_p)
8111 {
8112 SAVE_IT (ppos_it, *it, ppos_data);
8113 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8114 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8115 SAVE_IT (ppos_it, *it, ppos_data);
8116 }
8117
8118 #define BUFFER_POS_REACHED_P() \
8119 ((op & MOVE_TO_POS) != 0 \
8120 && BUFFERP (it->object) \
8121 && (IT_CHARPOS (*it) == to_charpos \
8122 || ((!it->bidi_p \
8123 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8124 && IT_CHARPOS (*it) > to_charpos) \
8125 || (it->what == IT_COMPOSITION \
8126 && ((IT_CHARPOS (*it) > to_charpos \
8127 && to_charpos >= it->cmp_it.charpos) \
8128 || (IT_CHARPOS (*it) < to_charpos \
8129 && to_charpos <= it->cmp_it.charpos)))) \
8130 && (it->method == GET_FROM_BUFFER \
8131 || (it->method == GET_FROM_DISPLAY_VECTOR \
8132 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8133
8134 /* If there's a line-/wrap-prefix, handle it. */
8135 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8136 && it->current_y < it->last_visible_y)
8137 handle_line_prefix (it);
8138
8139 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8140 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8141
8142 while (1)
8143 {
8144 int x, i, ascent = 0, descent = 0;
8145
8146 /* Utility macro to reset an iterator with x, ascent, and descent. */
8147 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8148 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8149 (IT)->max_descent = descent)
8150
8151 /* Stop if we move beyond TO_CHARPOS (after an image or a
8152 display string or stretch glyph). */
8153 if ((op & MOVE_TO_POS) != 0
8154 && BUFFERP (it->object)
8155 && it->method == GET_FROM_BUFFER
8156 && (((!it->bidi_p
8157 /* When the iterator is at base embedding level, we
8158 are guaranteed that characters are delivered for
8159 display in strictly increasing order of their
8160 buffer positions. */
8161 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8162 && IT_CHARPOS (*it) > to_charpos)
8163 || (it->bidi_p
8164 && (prev_method == GET_FROM_IMAGE
8165 || prev_method == GET_FROM_STRETCH
8166 || prev_method == GET_FROM_STRING)
8167 /* Passed TO_CHARPOS from left to right. */
8168 && ((prev_pos < to_charpos
8169 && IT_CHARPOS (*it) > to_charpos)
8170 /* Passed TO_CHARPOS from right to left. */
8171 || (prev_pos > to_charpos
8172 && IT_CHARPOS (*it) < to_charpos)))))
8173 {
8174 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8175 {
8176 result = MOVE_POS_MATCH_OR_ZV;
8177 break;
8178 }
8179 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8180 /* If wrap_it is valid, the current position might be in a
8181 word that is wrapped. So, save the iterator in
8182 atpos_it and continue to see if wrapping happens. */
8183 SAVE_IT (atpos_it, *it, atpos_data);
8184 }
8185
8186 /* Stop when ZV reached.
8187 We used to stop here when TO_CHARPOS reached as well, but that is
8188 too soon if this glyph does not fit on this line. So we handle it
8189 explicitly below. */
8190 if (!get_next_display_element (it))
8191 {
8192 result = MOVE_POS_MATCH_OR_ZV;
8193 break;
8194 }
8195
8196 if (it->line_wrap == TRUNCATE)
8197 {
8198 if (BUFFER_POS_REACHED_P ())
8199 {
8200 result = MOVE_POS_MATCH_OR_ZV;
8201 break;
8202 }
8203 }
8204 else
8205 {
8206 if (it->line_wrap == WORD_WRAP)
8207 {
8208 if (IT_DISPLAYING_WHITESPACE (it))
8209 may_wrap = 1;
8210 else if (may_wrap)
8211 {
8212 /* We have reached a glyph that follows one or more
8213 whitespace characters. If the position is
8214 already found, we are done. */
8215 if (atpos_it.sp >= 0)
8216 {
8217 RESTORE_IT (it, &atpos_it, atpos_data);
8218 result = MOVE_POS_MATCH_OR_ZV;
8219 goto done;
8220 }
8221 if (atx_it.sp >= 0)
8222 {
8223 RESTORE_IT (it, &atx_it, atx_data);
8224 result = MOVE_X_REACHED;
8225 goto done;
8226 }
8227 /* Otherwise, we can wrap here. */
8228 SAVE_IT (wrap_it, *it, wrap_data);
8229 may_wrap = 0;
8230 }
8231 }
8232 }
8233
8234 /* Remember the line height for the current line, in case
8235 the next element doesn't fit on the line. */
8236 ascent = it->max_ascent;
8237 descent = it->max_descent;
8238
8239 /* The call to produce_glyphs will get the metrics of the
8240 display element IT is loaded with. Record the x-position
8241 before this display element, in case it doesn't fit on the
8242 line. */
8243 x = it->current_x;
8244
8245 PRODUCE_GLYPHS (it);
8246
8247 if (it->area != TEXT_AREA)
8248 {
8249 prev_method = it->method;
8250 if (it->method == GET_FROM_BUFFER)
8251 prev_pos = IT_CHARPOS (*it);
8252 set_iterator_to_next (it, 1);
8253 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8254 SET_TEXT_POS (this_line_min_pos,
8255 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8256 if (it->bidi_p
8257 && (op & MOVE_TO_POS)
8258 && IT_CHARPOS (*it) > to_charpos
8259 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8260 SAVE_IT (ppos_it, *it, ppos_data);
8261 continue;
8262 }
8263
8264 /* The number of glyphs we get back in IT->nglyphs will normally
8265 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8266 character on a terminal frame, or (iii) a line end. For the
8267 second case, IT->nglyphs - 1 padding glyphs will be present.
8268 (On X frames, there is only one glyph produced for a
8269 composite character.)
8270
8271 The behavior implemented below means, for continuation lines,
8272 that as many spaces of a TAB as fit on the current line are
8273 displayed there. For terminal frames, as many glyphs of a
8274 multi-glyph character are displayed in the current line, too.
8275 This is what the old redisplay code did, and we keep it that
8276 way. Under X, the whole shape of a complex character must
8277 fit on the line or it will be completely displayed in the
8278 next line.
8279
8280 Note that both for tabs and padding glyphs, all glyphs have
8281 the same width. */
8282 if (it->nglyphs)
8283 {
8284 /* More than one glyph or glyph doesn't fit on line. All
8285 glyphs have the same width. */
8286 int single_glyph_width = it->pixel_width / it->nglyphs;
8287 int new_x;
8288 int x_before_this_char = x;
8289 int hpos_before_this_char = it->hpos;
8290
8291 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8292 {
8293 new_x = x + single_glyph_width;
8294
8295 /* We want to leave anything reaching TO_X to the caller. */
8296 if ((op & MOVE_TO_X) && new_x > to_x)
8297 {
8298 if (BUFFER_POS_REACHED_P ())
8299 {
8300 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8301 goto buffer_pos_reached;
8302 if (atpos_it.sp < 0)
8303 {
8304 SAVE_IT (atpos_it, *it, atpos_data);
8305 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8306 }
8307 }
8308 else
8309 {
8310 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8311 {
8312 it->current_x = x;
8313 result = MOVE_X_REACHED;
8314 break;
8315 }
8316 if (atx_it.sp < 0)
8317 {
8318 SAVE_IT (atx_it, *it, atx_data);
8319 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8320 }
8321 }
8322 }
8323
8324 if (/* Lines are continued. */
8325 it->line_wrap != TRUNCATE
8326 && (/* And glyph doesn't fit on the line. */
8327 new_x > it->last_visible_x
8328 /* Or it fits exactly and we're on a window
8329 system frame. */
8330 || (new_x == it->last_visible_x
8331 && FRAME_WINDOW_P (it->f))))
8332 {
8333 if (/* IT->hpos == 0 means the very first glyph
8334 doesn't fit on the line, e.g. a wide image. */
8335 it->hpos == 0
8336 || (new_x == it->last_visible_x
8337 && FRAME_WINDOW_P (it->f)))
8338 {
8339 ++it->hpos;
8340 it->current_x = new_x;
8341
8342 /* The character's last glyph just barely fits
8343 in this row. */
8344 if (i == it->nglyphs - 1)
8345 {
8346 /* If this is the destination position,
8347 return a position *before* it in this row,
8348 now that we know it fits in this row. */
8349 if (BUFFER_POS_REACHED_P ())
8350 {
8351 if (it->line_wrap != WORD_WRAP
8352 || wrap_it.sp < 0)
8353 {
8354 it->hpos = hpos_before_this_char;
8355 it->current_x = x_before_this_char;
8356 result = MOVE_POS_MATCH_OR_ZV;
8357 break;
8358 }
8359 if (it->line_wrap == WORD_WRAP
8360 && atpos_it.sp < 0)
8361 {
8362 SAVE_IT (atpos_it, *it, atpos_data);
8363 atpos_it.current_x = x_before_this_char;
8364 atpos_it.hpos = hpos_before_this_char;
8365 }
8366 }
8367
8368 prev_method = it->method;
8369 if (it->method == GET_FROM_BUFFER)
8370 prev_pos = IT_CHARPOS (*it);
8371 set_iterator_to_next (it, 1);
8372 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8373 SET_TEXT_POS (this_line_min_pos,
8374 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8375 /* On graphical terminals, newlines may
8376 "overflow" into the fringe if
8377 overflow-newline-into-fringe is non-nil.
8378 On text terminals, newlines may overflow
8379 into the last glyph on the display
8380 line.*/
8381 if (!FRAME_WINDOW_P (it->f)
8382 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8383 {
8384 if (!get_next_display_element (it))
8385 {
8386 result = MOVE_POS_MATCH_OR_ZV;
8387 break;
8388 }
8389 if (BUFFER_POS_REACHED_P ())
8390 {
8391 if (ITERATOR_AT_END_OF_LINE_P (it))
8392 result = MOVE_POS_MATCH_OR_ZV;
8393 else
8394 result = MOVE_LINE_CONTINUED;
8395 break;
8396 }
8397 if (ITERATOR_AT_END_OF_LINE_P (it))
8398 {
8399 result = MOVE_NEWLINE_OR_CR;
8400 break;
8401 }
8402 }
8403 }
8404 }
8405 else
8406 IT_RESET_X_ASCENT_DESCENT (it);
8407
8408 if (wrap_it.sp >= 0)
8409 {
8410 RESTORE_IT (it, &wrap_it, wrap_data);
8411 atpos_it.sp = -1;
8412 atx_it.sp = -1;
8413 }
8414
8415 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8416 IT_CHARPOS (*it)));
8417 result = MOVE_LINE_CONTINUED;
8418 break;
8419 }
8420
8421 if (BUFFER_POS_REACHED_P ())
8422 {
8423 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8424 goto buffer_pos_reached;
8425 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8426 {
8427 SAVE_IT (atpos_it, *it, atpos_data);
8428 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8429 }
8430 }
8431
8432 if (new_x > it->first_visible_x)
8433 {
8434 /* Glyph is visible. Increment number of glyphs that
8435 would be displayed. */
8436 ++it->hpos;
8437 }
8438 }
8439
8440 if (result != MOVE_UNDEFINED)
8441 break;
8442 }
8443 else if (BUFFER_POS_REACHED_P ())
8444 {
8445 buffer_pos_reached:
8446 IT_RESET_X_ASCENT_DESCENT (it);
8447 result = MOVE_POS_MATCH_OR_ZV;
8448 break;
8449 }
8450 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8451 {
8452 /* Stop when TO_X specified and reached. This check is
8453 necessary here because of lines consisting of a line end,
8454 only. The line end will not produce any glyphs and we
8455 would never get MOVE_X_REACHED. */
8456 eassert (it->nglyphs == 0);
8457 result = MOVE_X_REACHED;
8458 break;
8459 }
8460
8461 /* Is this a line end? If yes, we're done. */
8462 if (ITERATOR_AT_END_OF_LINE_P (it))
8463 {
8464 /* If we are past TO_CHARPOS, but never saw any character
8465 positions smaller than TO_CHARPOS, return
8466 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8467 did. */
8468 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8469 {
8470 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8471 {
8472 if (IT_CHARPOS (ppos_it) < ZV)
8473 {
8474 RESTORE_IT (it, &ppos_it, ppos_data);
8475 result = MOVE_POS_MATCH_OR_ZV;
8476 }
8477 else
8478 goto buffer_pos_reached;
8479 }
8480 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8481 && IT_CHARPOS (*it) > to_charpos)
8482 goto buffer_pos_reached;
8483 else
8484 result = MOVE_NEWLINE_OR_CR;
8485 }
8486 else
8487 result = MOVE_NEWLINE_OR_CR;
8488 break;
8489 }
8490
8491 prev_method = it->method;
8492 if (it->method == GET_FROM_BUFFER)
8493 prev_pos = IT_CHARPOS (*it);
8494 /* The current display element has been consumed. Advance
8495 to the next. */
8496 set_iterator_to_next (it, 1);
8497 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8498 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8499 if (IT_CHARPOS (*it) < to_charpos)
8500 saw_smaller_pos = 1;
8501 if (it->bidi_p
8502 && (op & MOVE_TO_POS)
8503 && IT_CHARPOS (*it) >= to_charpos
8504 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8505 SAVE_IT (ppos_it, *it, ppos_data);
8506
8507 /* Stop if lines are truncated and IT's current x-position is
8508 past the right edge of the window now. */
8509 if (it->line_wrap == TRUNCATE
8510 && it->current_x >= it->last_visible_x)
8511 {
8512 if (!FRAME_WINDOW_P (it->f)
8513 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8514 {
8515 int at_eob_p = 0;
8516
8517 if ((at_eob_p = !get_next_display_element (it))
8518 || BUFFER_POS_REACHED_P ()
8519 /* If we are past TO_CHARPOS, but never saw any
8520 character positions smaller than TO_CHARPOS,
8521 return MOVE_POS_MATCH_OR_ZV, like the
8522 unidirectional display did. */
8523 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8524 && !saw_smaller_pos
8525 && IT_CHARPOS (*it) > to_charpos))
8526 {
8527 if (it->bidi_p
8528 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8529 RESTORE_IT (it, &ppos_it, ppos_data);
8530 result = MOVE_POS_MATCH_OR_ZV;
8531 break;
8532 }
8533 if (ITERATOR_AT_END_OF_LINE_P (it))
8534 {
8535 result = MOVE_NEWLINE_OR_CR;
8536 break;
8537 }
8538 }
8539 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8540 && !saw_smaller_pos
8541 && IT_CHARPOS (*it) > to_charpos)
8542 {
8543 if (IT_CHARPOS (ppos_it) < ZV)
8544 RESTORE_IT (it, &ppos_it, ppos_data);
8545 result = MOVE_POS_MATCH_OR_ZV;
8546 break;
8547 }
8548 result = MOVE_LINE_TRUNCATED;
8549 break;
8550 }
8551 #undef IT_RESET_X_ASCENT_DESCENT
8552 }
8553
8554 #undef BUFFER_POS_REACHED_P
8555
8556 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8557 restore the saved iterator. */
8558 if (atpos_it.sp >= 0)
8559 RESTORE_IT (it, &atpos_it, atpos_data);
8560 else if (atx_it.sp >= 0)
8561 RESTORE_IT (it, &atx_it, atx_data);
8562
8563 done:
8564
8565 if (atpos_data)
8566 bidi_unshelve_cache (atpos_data, 1);
8567 if (atx_data)
8568 bidi_unshelve_cache (atx_data, 1);
8569 if (wrap_data)
8570 bidi_unshelve_cache (wrap_data, 1);
8571 if (ppos_data)
8572 bidi_unshelve_cache (ppos_data, 1);
8573
8574 /* Restore the iterator settings altered at the beginning of this
8575 function. */
8576 it->glyph_row = saved_glyph_row;
8577 return result;
8578 }
8579
8580 /* For external use. */
8581 void
8582 move_it_in_display_line (struct it *it,
8583 ptrdiff_t to_charpos, int to_x,
8584 enum move_operation_enum op)
8585 {
8586 if (it->line_wrap == WORD_WRAP
8587 && (op & MOVE_TO_X))
8588 {
8589 struct it save_it;
8590 void *save_data = NULL;
8591 int skip;
8592
8593 SAVE_IT (save_it, *it, save_data);
8594 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8595 /* When word-wrap is on, TO_X may lie past the end
8596 of a wrapped line. Then it->current is the
8597 character on the next line, so backtrack to the
8598 space before the wrap point. */
8599 if (skip == MOVE_LINE_CONTINUED)
8600 {
8601 int prev_x = max (it->current_x - 1, 0);
8602 RESTORE_IT (it, &save_it, save_data);
8603 move_it_in_display_line_to
8604 (it, -1, prev_x, MOVE_TO_X);
8605 }
8606 else
8607 bidi_unshelve_cache (save_data, 1);
8608 }
8609 else
8610 move_it_in_display_line_to (it, to_charpos, to_x, op);
8611 }
8612
8613
8614 /* Move IT forward until it satisfies one or more of the criteria in
8615 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8616
8617 OP is a bit-mask that specifies where to stop, and in particular,
8618 which of those four position arguments makes a difference. See the
8619 description of enum move_operation_enum.
8620
8621 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8622 screen line, this function will set IT to the next position that is
8623 displayed to the right of TO_CHARPOS on the screen. */
8624
8625 void
8626 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8627 {
8628 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8629 int line_height, line_start_x = 0, reached = 0;
8630 void *backup_data = NULL;
8631
8632 for (;;)
8633 {
8634 if (op & MOVE_TO_VPOS)
8635 {
8636 /* If no TO_CHARPOS and no TO_X specified, stop at the
8637 start of the line TO_VPOS. */
8638 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8639 {
8640 if (it->vpos == to_vpos)
8641 {
8642 reached = 1;
8643 break;
8644 }
8645 else
8646 skip = move_it_in_display_line_to (it, -1, -1, 0);
8647 }
8648 else
8649 {
8650 /* TO_VPOS >= 0 means stop at TO_X in the line at
8651 TO_VPOS, or at TO_POS, whichever comes first. */
8652 if (it->vpos == to_vpos)
8653 {
8654 reached = 2;
8655 break;
8656 }
8657
8658 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8659
8660 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8661 {
8662 reached = 3;
8663 break;
8664 }
8665 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8666 {
8667 /* We have reached TO_X but not in the line we want. */
8668 skip = move_it_in_display_line_to (it, to_charpos,
8669 -1, MOVE_TO_POS);
8670 if (skip == MOVE_POS_MATCH_OR_ZV)
8671 {
8672 reached = 4;
8673 break;
8674 }
8675 }
8676 }
8677 }
8678 else if (op & MOVE_TO_Y)
8679 {
8680 struct it it_backup;
8681
8682 if (it->line_wrap == WORD_WRAP)
8683 SAVE_IT (it_backup, *it, backup_data);
8684
8685 /* TO_Y specified means stop at TO_X in the line containing
8686 TO_Y---or at TO_CHARPOS if this is reached first. The
8687 problem is that we can't really tell whether the line
8688 contains TO_Y before we have completely scanned it, and
8689 this may skip past TO_X. What we do is to first scan to
8690 TO_X.
8691
8692 If TO_X is not specified, use a TO_X of zero. The reason
8693 is to make the outcome of this function more predictable.
8694 If we didn't use TO_X == 0, we would stop at the end of
8695 the line which is probably not what a caller would expect
8696 to happen. */
8697 skip = move_it_in_display_line_to
8698 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8699 (MOVE_TO_X | (op & MOVE_TO_POS)));
8700
8701 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8702 if (skip == MOVE_POS_MATCH_OR_ZV)
8703 reached = 5;
8704 else if (skip == MOVE_X_REACHED)
8705 {
8706 /* If TO_X was reached, we want to know whether TO_Y is
8707 in the line. We know this is the case if the already
8708 scanned glyphs make the line tall enough. Otherwise,
8709 we must check by scanning the rest of the line. */
8710 line_height = it->max_ascent + it->max_descent;
8711 if (to_y >= it->current_y
8712 && to_y < it->current_y + line_height)
8713 {
8714 reached = 6;
8715 break;
8716 }
8717 SAVE_IT (it_backup, *it, backup_data);
8718 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8719 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8720 op & MOVE_TO_POS);
8721 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8722 line_height = it->max_ascent + it->max_descent;
8723 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8724
8725 if (to_y >= it->current_y
8726 && to_y < it->current_y + line_height)
8727 {
8728 /* If TO_Y is in this line and TO_X was reached
8729 above, we scanned too far. We have to restore
8730 IT's settings to the ones before skipping. But
8731 keep the more accurate values of max_ascent and
8732 max_descent we've found while skipping the rest
8733 of the line, for the sake of callers, such as
8734 pos_visible_p, that need to know the line
8735 height. */
8736 int max_ascent = it->max_ascent;
8737 int max_descent = it->max_descent;
8738
8739 RESTORE_IT (it, &it_backup, backup_data);
8740 it->max_ascent = max_ascent;
8741 it->max_descent = max_descent;
8742 reached = 6;
8743 }
8744 else
8745 {
8746 skip = skip2;
8747 if (skip == MOVE_POS_MATCH_OR_ZV)
8748 reached = 7;
8749 }
8750 }
8751 else
8752 {
8753 /* Check whether TO_Y is in this line. */
8754 line_height = it->max_ascent + it->max_descent;
8755 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8756
8757 if (to_y >= it->current_y
8758 && to_y < it->current_y + line_height)
8759 {
8760 /* When word-wrap is on, TO_X may lie past the end
8761 of a wrapped line. Then it->current is the
8762 character on the next line, so backtrack to the
8763 space before the wrap point. */
8764 if (skip == MOVE_LINE_CONTINUED
8765 && it->line_wrap == WORD_WRAP)
8766 {
8767 int prev_x = max (it->current_x - 1, 0);
8768 RESTORE_IT (it, &it_backup, backup_data);
8769 skip = move_it_in_display_line_to
8770 (it, -1, prev_x, MOVE_TO_X);
8771 }
8772 reached = 6;
8773 }
8774 }
8775
8776 if (reached)
8777 break;
8778 }
8779 else if (BUFFERP (it->object)
8780 && (it->method == GET_FROM_BUFFER
8781 || it->method == GET_FROM_STRETCH)
8782 && IT_CHARPOS (*it) >= to_charpos
8783 /* Under bidi iteration, a call to set_iterator_to_next
8784 can scan far beyond to_charpos if the initial
8785 portion of the next line needs to be reordered. In
8786 that case, give move_it_in_display_line_to another
8787 chance below. */
8788 && !(it->bidi_p
8789 && it->bidi_it.scan_dir == -1))
8790 skip = MOVE_POS_MATCH_OR_ZV;
8791 else
8792 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8793
8794 switch (skip)
8795 {
8796 case MOVE_POS_MATCH_OR_ZV:
8797 reached = 8;
8798 goto out;
8799
8800 case MOVE_NEWLINE_OR_CR:
8801 set_iterator_to_next (it, 1);
8802 it->continuation_lines_width = 0;
8803 break;
8804
8805 case MOVE_LINE_TRUNCATED:
8806 it->continuation_lines_width = 0;
8807 reseat_at_next_visible_line_start (it, 0);
8808 if ((op & MOVE_TO_POS) != 0
8809 && IT_CHARPOS (*it) > to_charpos)
8810 {
8811 reached = 9;
8812 goto out;
8813 }
8814 break;
8815
8816 case MOVE_LINE_CONTINUED:
8817 /* For continued lines ending in a tab, some of the glyphs
8818 associated with the tab are displayed on the current
8819 line. Since it->current_x does not include these glyphs,
8820 we use it->last_visible_x instead. */
8821 if (it->c == '\t')
8822 {
8823 it->continuation_lines_width += it->last_visible_x;
8824 /* When moving by vpos, ensure that the iterator really
8825 advances to the next line (bug#847, bug#969). Fixme:
8826 do we need to do this in other circumstances? */
8827 if (it->current_x != it->last_visible_x
8828 && (op & MOVE_TO_VPOS)
8829 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8830 {
8831 line_start_x = it->current_x + it->pixel_width
8832 - it->last_visible_x;
8833 set_iterator_to_next (it, 0);
8834 }
8835 }
8836 else
8837 it->continuation_lines_width += it->current_x;
8838 break;
8839
8840 default:
8841 abort ();
8842 }
8843
8844 /* Reset/increment for the next run. */
8845 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8846 it->current_x = line_start_x;
8847 line_start_x = 0;
8848 it->hpos = 0;
8849 it->current_y += it->max_ascent + it->max_descent;
8850 ++it->vpos;
8851 last_height = it->max_ascent + it->max_descent;
8852 last_max_ascent = it->max_ascent;
8853 it->max_ascent = it->max_descent = 0;
8854 }
8855
8856 out:
8857
8858 /* On text terminals, we may stop at the end of a line in the middle
8859 of a multi-character glyph. If the glyph itself is continued,
8860 i.e. it is actually displayed on the next line, don't treat this
8861 stopping point as valid; move to the next line instead (unless
8862 that brings us offscreen). */
8863 if (!FRAME_WINDOW_P (it->f)
8864 && op & MOVE_TO_POS
8865 && IT_CHARPOS (*it) == to_charpos
8866 && it->what == IT_CHARACTER
8867 && it->nglyphs > 1
8868 && it->line_wrap == WINDOW_WRAP
8869 && it->current_x == it->last_visible_x - 1
8870 && it->c != '\n'
8871 && it->c != '\t'
8872 && it->vpos < XFASTINT (it->w->window_end_vpos))
8873 {
8874 it->continuation_lines_width += it->current_x;
8875 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8876 it->current_y += it->max_ascent + it->max_descent;
8877 ++it->vpos;
8878 last_height = it->max_ascent + it->max_descent;
8879 last_max_ascent = it->max_ascent;
8880 }
8881
8882 if (backup_data)
8883 bidi_unshelve_cache (backup_data, 1);
8884
8885 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8886 }
8887
8888
8889 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8890
8891 If DY > 0, move IT backward at least that many pixels. DY = 0
8892 means move IT backward to the preceding line start or BEGV. This
8893 function may move over more than DY pixels if IT->current_y - DY
8894 ends up in the middle of a line; in this case IT->current_y will be
8895 set to the top of the line moved to. */
8896
8897 void
8898 move_it_vertically_backward (struct it *it, int dy)
8899 {
8900 int nlines, h;
8901 struct it it2, it3;
8902 void *it2data = NULL, *it3data = NULL;
8903 ptrdiff_t start_pos;
8904
8905 move_further_back:
8906 eassert (dy >= 0);
8907
8908 start_pos = IT_CHARPOS (*it);
8909
8910 /* Estimate how many newlines we must move back. */
8911 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8912
8913 /* Set the iterator's position that many lines back. */
8914 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8915 back_to_previous_visible_line_start (it);
8916
8917 /* Reseat the iterator here. When moving backward, we don't want
8918 reseat to skip forward over invisible text, set up the iterator
8919 to deliver from overlay strings at the new position etc. So,
8920 use reseat_1 here. */
8921 reseat_1 (it, it->current.pos, 1);
8922
8923 /* We are now surely at a line start. */
8924 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8925 reordering is in effect. */
8926 it->continuation_lines_width = 0;
8927
8928 /* Move forward and see what y-distance we moved. First move to the
8929 start of the next line so that we get its height. We need this
8930 height to be able to tell whether we reached the specified
8931 y-distance. */
8932 SAVE_IT (it2, *it, it2data);
8933 it2.max_ascent = it2.max_descent = 0;
8934 do
8935 {
8936 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8937 MOVE_TO_POS | MOVE_TO_VPOS);
8938 }
8939 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8940 /* If we are in a display string which starts at START_POS,
8941 and that display string includes a newline, and we are
8942 right after that newline (i.e. at the beginning of a
8943 display line), exit the loop, because otherwise we will
8944 infloop, since move_it_to will see that it is already at
8945 START_POS and will not move. */
8946 || (it2.method == GET_FROM_STRING
8947 && IT_CHARPOS (it2) == start_pos
8948 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8949 eassert (IT_CHARPOS (*it) >= BEGV);
8950 SAVE_IT (it3, it2, it3data);
8951
8952 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8953 eassert (IT_CHARPOS (*it) >= BEGV);
8954 /* H is the actual vertical distance from the position in *IT
8955 and the starting position. */
8956 h = it2.current_y - it->current_y;
8957 /* NLINES is the distance in number of lines. */
8958 nlines = it2.vpos - it->vpos;
8959
8960 /* Correct IT's y and vpos position
8961 so that they are relative to the starting point. */
8962 it->vpos -= nlines;
8963 it->current_y -= h;
8964
8965 if (dy == 0)
8966 {
8967 /* DY == 0 means move to the start of the screen line. The
8968 value of nlines is > 0 if continuation lines were involved,
8969 or if the original IT position was at start of a line. */
8970 RESTORE_IT (it, it, it2data);
8971 if (nlines > 0)
8972 move_it_by_lines (it, nlines);
8973 /* The above code moves us to some position NLINES down,
8974 usually to its first glyph (leftmost in an L2R line), but
8975 that's not necessarily the start of the line, under bidi
8976 reordering. We want to get to the character position
8977 that is immediately after the newline of the previous
8978 line. */
8979 if (it->bidi_p
8980 && !it->continuation_lines_width
8981 && !STRINGP (it->string)
8982 && IT_CHARPOS (*it) > BEGV
8983 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8984 {
8985 ptrdiff_t nl_pos =
8986 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8987
8988 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8989 }
8990 bidi_unshelve_cache (it3data, 1);
8991 }
8992 else
8993 {
8994 /* The y-position we try to reach, relative to *IT.
8995 Note that H has been subtracted in front of the if-statement. */
8996 int target_y = it->current_y + h - dy;
8997 int y0 = it3.current_y;
8998 int y1;
8999 int line_height;
9000
9001 RESTORE_IT (&it3, &it3, it3data);
9002 y1 = line_bottom_y (&it3);
9003 line_height = y1 - y0;
9004 RESTORE_IT (it, it, it2data);
9005 /* If we did not reach target_y, try to move further backward if
9006 we can. If we moved too far backward, try to move forward. */
9007 if (target_y < it->current_y
9008 /* This is heuristic. In a window that's 3 lines high, with
9009 a line height of 13 pixels each, recentering with point
9010 on the bottom line will try to move -39/2 = 19 pixels
9011 backward. Try to avoid moving into the first line. */
9012 && (it->current_y - target_y
9013 > min (window_box_height (it->w), line_height * 2 / 3))
9014 && IT_CHARPOS (*it) > BEGV)
9015 {
9016 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9017 target_y - it->current_y));
9018 dy = it->current_y - target_y;
9019 goto move_further_back;
9020 }
9021 else if (target_y >= it->current_y + line_height
9022 && IT_CHARPOS (*it) < ZV)
9023 {
9024 /* Should move forward by at least one line, maybe more.
9025
9026 Note: Calling move_it_by_lines can be expensive on
9027 terminal frames, where compute_motion is used (via
9028 vmotion) to do the job, when there are very long lines
9029 and truncate-lines is nil. That's the reason for
9030 treating terminal frames specially here. */
9031
9032 if (!FRAME_WINDOW_P (it->f))
9033 move_it_vertically (it, target_y - (it->current_y + line_height));
9034 else
9035 {
9036 do
9037 {
9038 move_it_by_lines (it, 1);
9039 }
9040 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9041 }
9042 }
9043 }
9044 }
9045
9046
9047 /* Move IT by a specified amount of pixel lines DY. DY negative means
9048 move backwards. DY = 0 means move to start of screen line. At the
9049 end, IT will be on the start of a screen line. */
9050
9051 void
9052 move_it_vertically (struct it *it, int dy)
9053 {
9054 if (dy <= 0)
9055 move_it_vertically_backward (it, -dy);
9056 else
9057 {
9058 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9059 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9060 MOVE_TO_POS | MOVE_TO_Y);
9061 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9062
9063 /* If buffer ends in ZV without a newline, move to the start of
9064 the line to satisfy the post-condition. */
9065 if (IT_CHARPOS (*it) == ZV
9066 && ZV > BEGV
9067 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9068 move_it_by_lines (it, 0);
9069 }
9070 }
9071
9072
9073 /* Move iterator IT past the end of the text line it is in. */
9074
9075 void
9076 move_it_past_eol (struct it *it)
9077 {
9078 enum move_it_result rc;
9079
9080 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9081 if (rc == MOVE_NEWLINE_OR_CR)
9082 set_iterator_to_next (it, 0);
9083 }
9084
9085
9086 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9087 negative means move up. DVPOS == 0 means move to the start of the
9088 screen line.
9089
9090 Optimization idea: If we would know that IT->f doesn't use
9091 a face with proportional font, we could be faster for
9092 truncate-lines nil. */
9093
9094 void
9095 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9096 {
9097
9098 /* The commented-out optimization uses vmotion on terminals. This
9099 gives bad results, because elements like it->what, on which
9100 callers such as pos_visible_p rely, aren't updated. */
9101 /* struct position pos;
9102 if (!FRAME_WINDOW_P (it->f))
9103 {
9104 struct text_pos textpos;
9105
9106 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9107 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9108 reseat (it, textpos, 1);
9109 it->vpos += pos.vpos;
9110 it->current_y += pos.vpos;
9111 }
9112 else */
9113
9114 if (dvpos == 0)
9115 {
9116 /* DVPOS == 0 means move to the start of the screen line. */
9117 move_it_vertically_backward (it, 0);
9118 /* Let next call to line_bottom_y calculate real line height */
9119 last_height = 0;
9120 }
9121 else if (dvpos > 0)
9122 {
9123 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9124 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9125 {
9126 /* Only move to the next buffer position if we ended up in a
9127 string from display property, not in an overlay string
9128 (before-string or after-string). That is because the
9129 latter don't conceal the underlying buffer position, so
9130 we can ask to move the iterator to the exact position we
9131 are interested in. Note that, even if we are already at
9132 IT_CHARPOS (*it), the call below is not a no-op, as it
9133 will detect that we are at the end of the string, pop the
9134 iterator, and compute it->current_x and it->hpos
9135 correctly. */
9136 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9137 -1, -1, -1, MOVE_TO_POS);
9138 }
9139 }
9140 else
9141 {
9142 struct it it2;
9143 void *it2data = NULL;
9144 ptrdiff_t start_charpos, i;
9145
9146 /* Start at the beginning of the screen line containing IT's
9147 position. This may actually move vertically backwards,
9148 in case of overlays, so adjust dvpos accordingly. */
9149 dvpos += it->vpos;
9150 move_it_vertically_backward (it, 0);
9151 dvpos -= it->vpos;
9152
9153 /* Go back -DVPOS visible lines and reseat the iterator there. */
9154 start_charpos = IT_CHARPOS (*it);
9155 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9156 back_to_previous_visible_line_start (it);
9157 reseat (it, it->current.pos, 1);
9158
9159 /* Move further back if we end up in a string or an image. */
9160 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9161 {
9162 /* First try to move to start of display line. */
9163 dvpos += it->vpos;
9164 move_it_vertically_backward (it, 0);
9165 dvpos -= it->vpos;
9166 if (IT_POS_VALID_AFTER_MOVE_P (it))
9167 break;
9168 /* If start of line is still in string or image,
9169 move further back. */
9170 back_to_previous_visible_line_start (it);
9171 reseat (it, it->current.pos, 1);
9172 dvpos--;
9173 }
9174
9175 it->current_x = it->hpos = 0;
9176
9177 /* Above call may have moved too far if continuation lines
9178 are involved. Scan forward and see if it did. */
9179 SAVE_IT (it2, *it, it2data);
9180 it2.vpos = it2.current_y = 0;
9181 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9182 it->vpos -= it2.vpos;
9183 it->current_y -= it2.current_y;
9184 it->current_x = it->hpos = 0;
9185
9186 /* If we moved too far back, move IT some lines forward. */
9187 if (it2.vpos > -dvpos)
9188 {
9189 int delta = it2.vpos + dvpos;
9190
9191 RESTORE_IT (&it2, &it2, it2data);
9192 SAVE_IT (it2, *it, it2data);
9193 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9194 /* Move back again if we got too far ahead. */
9195 if (IT_CHARPOS (*it) >= start_charpos)
9196 RESTORE_IT (it, &it2, it2data);
9197 else
9198 bidi_unshelve_cache (it2data, 1);
9199 }
9200 else
9201 RESTORE_IT (it, it, it2data);
9202 }
9203 }
9204
9205 /* Return 1 if IT points into the middle of a display vector. */
9206
9207 int
9208 in_display_vector_p (struct it *it)
9209 {
9210 return (it->method == GET_FROM_DISPLAY_VECTOR
9211 && it->current.dpvec_index > 0
9212 && it->dpvec + it->current.dpvec_index != it->dpend);
9213 }
9214
9215 \f
9216 /***********************************************************************
9217 Messages
9218 ***********************************************************************/
9219
9220
9221 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9222 to *Messages*. */
9223
9224 void
9225 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9226 {
9227 Lisp_Object args[3];
9228 Lisp_Object msg, fmt;
9229 char *buffer;
9230 ptrdiff_t len;
9231 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9232 USE_SAFE_ALLOCA;
9233
9234 /* Do nothing if called asynchronously. Inserting text into
9235 a buffer may call after-change-functions and alike and
9236 that would means running Lisp asynchronously. */
9237 if (handling_signal)
9238 return;
9239
9240 fmt = msg = Qnil;
9241 GCPRO4 (fmt, msg, arg1, arg2);
9242
9243 args[0] = fmt = build_string (format);
9244 args[1] = arg1;
9245 args[2] = arg2;
9246 msg = Fformat (3, args);
9247
9248 len = SBYTES (msg) + 1;
9249 SAFE_ALLOCA (buffer, char *, len);
9250 memcpy (buffer, SDATA (msg), len);
9251
9252 message_dolog (buffer, len - 1, 1, 0);
9253 SAFE_FREE ();
9254
9255 UNGCPRO;
9256 }
9257
9258
9259 /* Output a newline in the *Messages* buffer if "needs" one. */
9260
9261 void
9262 message_log_maybe_newline (void)
9263 {
9264 if (message_log_need_newline)
9265 message_dolog ("", 0, 1, 0);
9266 }
9267
9268
9269 /* Add a string M of length NBYTES to the message log, optionally
9270 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9271 nonzero, means interpret the contents of M as multibyte. This
9272 function calls low-level routines in order to bypass text property
9273 hooks, etc. which might not be safe to run.
9274
9275 This may GC (insert may run before/after change hooks),
9276 so the buffer M must NOT point to a Lisp string. */
9277
9278 void
9279 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9280 {
9281 const unsigned char *msg = (const unsigned char *) m;
9282
9283 if (!NILP (Vmemory_full))
9284 return;
9285
9286 if (!NILP (Vmessage_log_max))
9287 {
9288 struct buffer *oldbuf;
9289 Lisp_Object oldpoint, oldbegv, oldzv;
9290 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9291 ptrdiff_t point_at_end = 0;
9292 ptrdiff_t zv_at_end = 0;
9293 Lisp_Object old_deactivate_mark, tem;
9294 struct gcpro gcpro1;
9295
9296 old_deactivate_mark = Vdeactivate_mark;
9297 oldbuf = current_buffer;
9298 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9299 BVAR (current_buffer, undo_list) = Qt;
9300
9301 oldpoint = message_dolog_marker1;
9302 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9303 oldbegv = message_dolog_marker2;
9304 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9305 oldzv = message_dolog_marker3;
9306 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9307 GCPRO1 (old_deactivate_mark);
9308
9309 if (PT == Z)
9310 point_at_end = 1;
9311 if (ZV == Z)
9312 zv_at_end = 1;
9313
9314 BEGV = BEG;
9315 BEGV_BYTE = BEG_BYTE;
9316 ZV = Z;
9317 ZV_BYTE = Z_BYTE;
9318 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9319
9320 /* Insert the string--maybe converting multibyte to single byte
9321 or vice versa, so that all the text fits the buffer. */
9322 if (multibyte
9323 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9324 {
9325 ptrdiff_t i;
9326 int c, char_bytes;
9327 char work[1];
9328
9329 /* Convert a multibyte string to single-byte
9330 for the *Message* buffer. */
9331 for (i = 0; i < nbytes; i += char_bytes)
9332 {
9333 c = string_char_and_length (msg + i, &char_bytes);
9334 work[0] = (ASCII_CHAR_P (c)
9335 ? c
9336 : multibyte_char_to_unibyte (c));
9337 insert_1_both (work, 1, 1, 1, 0, 0);
9338 }
9339 }
9340 else if (! multibyte
9341 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9342 {
9343 ptrdiff_t i;
9344 int c, char_bytes;
9345 unsigned char str[MAX_MULTIBYTE_LENGTH];
9346 /* Convert a single-byte string to multibyte
9347 for the *Message* buffer. */
9348 for (i = 0; i < nbytes; i++)
9349 {
9350 c = msg[i];
9351 MAKE_CHAR_MULTIBYTE (c);
9352 char_bytes = CHAR_STRING (c, str);
9353 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9354 }
9355 }
9356 else if (nbytes)
9357 insert_1 (m, nbytes, 1, 0, 0);
9358
9359 if (nlflag)
9360 {
9361 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9362 printmax_t dups;
9363 insert_1 ("\n", 1, 1, 0, 0);
9364
9365 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9366 this_bol = PT;
9367 this_bol_byte = PT_BYTE;
9368
9369 /* See if this line duplicates the previous one.
9370 If so, combine duplicates. */
9371 if (this_bol > BEG)
9372 {
9373 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9374 prev_bol = PT;
9375 prev_bol_byte = PT_BYTE;
9376
9377 dups = message_log_check_duplicate (prev_bol_byte,
9378 this_bol_byte);
9379 if (dups)
9380 {
9381 del_range_both (prev_bol, prev_bol_byte,
9382 this_bol, this_bol_byte, 0);
9383 if (dups > 1)
9384 {
9385 char dupstr[sizeof " [ times]"
9386 + INT_STRLEN_BOUND (printmax_t)];
9387
9388 /* If you change this format, don't forget to also
9389 change message_log_check_duplicate. */
9390 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9391 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9392 insert_1 (dupstr, duplen, 1, 0, 1);
9393 }
9394 }
9395 }
9396
9397 /* If we have more than the desired maximum number of lines
9398 in the *Messages* buffer now, delete the oldest ones.
9399 This is safe because we don't have undo in this buffer. */
9400
9401 if (NATNUMP (Vmessage_log_max))
9402 {
9403 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9404 -XFASTINT (Vmessage_log_max) - 1, 0);
9405 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9406 }
9407 }
9408 BEGV = XMARKER (oldbegv)->charpos;
9409 BEGV_BYTE = marker_byte_position (oldbegv);
9410
9411 if (zv_at_end)
9412 {
9413 ZV = Z;
9414 ZV_BYTE = Z_BYTE;
9415 }
9416 else
9417 {
9418 ZV = XMARKER (oldzv)->charpos;
9419 ZV_BYTE = marker_byte_position (oldzv);
9420 }
9421
9422 if (point_at_end)
9423 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9424 else
9425 /* We can't do Fgoto_char (oldpoint) because it will run some
9426 Lisp code. */
9427 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9428 XMARKER (oldpoint)->bytepos);
9429
9430 UNGCPRO;
9431 unchain_marker (XMARKER (oldpoint));
9432 unchain_marker (XMARKER (oldbegv));
9433 unchain_marker (XMARKER (oldzv));
9434
9435 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9436 set_buffer_internal (oldbuf);
9437 if (NILP (tem))
9438 windows_or_buffers_changed = old_windows_or_buffers_changed;
9439 message_log_need_newline = !nlflag;
9440 Vdeactivate_mark = old_deactivate_mark;
9441 }
9442 }
9443
9444
9445 /* We are at the end of the buffer after just having inserted a newline.
9446 (Note: We depend on the fact we won't be crossing the gap.)
9447 Check to see if the most recent message looks a lot like the previous one.
9448 Return 0 if different, 1 if the new one should just replace it, or a
9449 value N > 1 if we should also append " [N times]". */
9450
9451 static intmax_t
9452 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9453 {
9454 ptrdiff_t i;
9455 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9456 int seen_dots = 0;
9457 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9458 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9459
9460 for (i = 0; i < len; i++)
9461 {
9462 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9463 seen_dots = 1;
9464 if (p1[i] != p2[i])
9465 return seen_dots;
9466 }
9467 p1 += len;
9468 if (*p1 == '\n')
9469 return 2;
9470 if (*p1++ == ' ' && *p1++ == '[')
9471 {
9472 char *pend;
9473 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9474 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9475 return n+1;
9476 }
9477 return 0;
9478 }
9479 \f
9480
9481 /* Display an echo area message M with a specified length of NBYTES
9482 bytes. The string may include null characters. If M is 0, clear
9483 out any existing message, and let the mini-buffer text show
9484 through.
9485
9486 This may GC, so the buffer M must NOT point to a Lisp string. */
9487
9488 void
9489 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9490 {
9491 /* First flush out any partial line written with print. */
9492 message_log_maybe_newline ();
9493 if (m)
9494 message_dolog (m, nbytes, 1, multibyte);
9495 message2_nolog (m, nbytes, multibyte);
9496 }
9497
9498
9499 /* The non-logging counterpart of message2. */
9500
9501 void
9502 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9503 {
9504 struct frame *sf = SELECTED_FRAME ();
9505 message_enable_multibyte = multibyte;
9506
9507 if (FRAME_INITIAL_P (sf))
9508 {
9509 if (noninteractive_need_newline)
9510 putc ('\n', stderr);
9511 noninteractive_need_newline = 0;
9512 if (m)
9513 fwrite (m, nbytes, 1, stderr);
9514 if (cursor_in_echo_area == 0)
9515 fprintf (stderr, "\n");
9516 fflush (stderr);
9517 }
9518 /* A null message buffer means that the frame hasn't really been
9519 initialized yet. Error messages get reported properly by
9520 cmd_error, so this must be just an informative message; toss it. */
9521 else if (INTERACTIVE
9522 && sf->glyphs_initialized_p
9523 && FRAME_MESSAGE_BUF (sf))
9524 {
9525 Lisp_Object mini_window;
9526 struct frame *f;
9527
9528 /* Get the frame containing the mini-buffer
9529 that the selected frame is using. */
9530 mini_window = FRAME_MINIBUF_WINDOW (sf);
9531 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9532
9533 FRAME_SAMPLE_VISIBILITY (f);
9534 if (FRAME_VISIBLE_P (sf)
9535 && ! FRAME_VISIBLE_P (f))
9536 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9537
9538 if (m)
9539 {
9540 set_message (m, Qnil, nbytes, multibyte);
9541 if (minibuffer_auto_raise)
9542 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9543 }
9544 else
9545 clear_message (1, 1);
9546
9547 do_pending_window_change (0);
9548 echo_area_display (1);
9549 do_pending_window_change (0);
9550 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9551 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9552 }
9553 }
9554
9555
9556 /* Display an echo area message M with a specified length of NBYTES
9557 bytes. The string may include null characters. If M is not a
9558 string, clear out any existing message, and let the mini-buffer
9559 text show through.
9560
9561 This function cancels echoing. */
9562
9563 void
9564 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9565 {
9566 struct gcpro gcpro1;
9567
9568 GCPRO1 (m);
9569 clear_message (1,1);
9570 cancel_echoing ();
9571
9572 /* First flush out any partial line written with print. */
9573 message_log_maybe_newline ();
9574 if (STRINGP (m))
9575 {
9576 char *buffer;
9577 USE_SAFE_ALLOCA;
9578
9579 SAFE_ALLOCA (buffer, char *, nbytes);
9580 memcpy (buffer, SDATA (m), nbytes);
9581 message_dolog (buffer, nbytes, 1, multibyte);
9582 SAFE_FREE ();
9583 }
9584 message3_nolog (m, nbytes, multibyte);
9585
9586 UNGCPRO;
9587 }
9588
9589
9590 /* The non-logging version of message3.
9591 This does not cancel echoing, because it is used for echoing.
9592 Perhaps we need to make a separate function for echoing
9593 and make this cancel echoing. */
9594
9595 void
9596 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9597 {
9598 struct frame *sf = SELECTED_FRAME ();
9599 message_enable_multibyte = multibyte;
9600
9601 if (FRAME_INITIAL_P (sf))
9602 {
9603 if (noninteractive_need_newline)
9604 putc ('\n', stderr);
9605 noninteractive_need_newline = 0;
9606 if (STRINGP (m))
9607 fwrite (SDATA (m), nbytes, 1, stderr);
9608 if (cursor_in_echo_area == 0)
9609 fprintf (stderr, "\n");
9610 fflush (stderr);
9611 }
9612 /* A null message buffer means that the frame hasn't really been
9613 initialized yet. Error messages get reported properly by
9614 cmd_error, so this must be just an informative message; toss it. */
9615 else if (INTERACTIVE
9616 && sf->glyphs_initialized_p
9617 && FRAME_MESSAGE_BUF (sf))
9618 {
9619 Lisp_Object mini_window;
9620 Lisp_Object frame;
9621 struct frame *f;
9622
9623 /* Get the frame containing the mini-buffer
9624 that the selected frame is using. */
9625 mini_window = FRAME_MINIBUF_WINDOW (sf);
9626 frame = XWINDOW (mini_window)->frame;
9627 f = XFRAME (frame);
9628
9629 FRAME_SAMPLE_VISIBILITY (f);
9630 if (FRAME_VISIBLE_P (sf)
9631 && !FRAME_VISIBLE_P (f))
9632 Fmake_frame_visible (frame);
9633
9634 if (STRINGP (m) && SCHARS (m) > 0)
9635 {
9636 set_message (NULL, m, nbytes, multibyte);
9637 if (minibuffer_auto_raise)
9638 Fraise_frame (frame);
9639 /* Assume we are not echoing.
9640 (If we are, echo_now will override this.) */
9641 echo_message_buffer = Qnil;
9642 }
9643 else
9644 clear_message (1, 1);
9645
9646 do_pending_window_change (0);
9647 echo_area_display (1);
9648 do_pending_window_change (0);
9649 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9650 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9651 }
9652 }
9653
9654
9655 /* Display a null-terminated echo area message M. If M is 0, clear
9656 out any existing message, and let the mini-buffer text show through.
9657
9658 The buffer M must continue to exist until after the echo area gets
9659 cleared or some other message gets displayed there. Do not pass
9660 text that is stored in a Lisp string. Do not pass text in a buffer
9661 that was alloca'd. */
9662
9663 void
9664 message1 (const char *m)
9665 {
9666 message2 (m, (m ? strlen (m) : 0), 0);
9667 }
9668
9669
9670 /* The non-logging counterpart of message1. */
9671
9672 void
9673 message1_nolog (const char *m)
9674 {
9675 message2_nolog (m, (m ? strlen (m) : 0), 0);
9676 }
9677
9678 /* Display a message M which contains a single %s
9679 which gets replaced with STRING. */
9680
9681 void
9682 message_with_string (const char *m, Lisp_Object string, int log)
9683 {
9684 CHECK_STRING (string);
9685
9686 if (noninteractive)
9687 {
9688 if (m)
9689 {
9690 if (noninteractive_need_newline)
9691 putc ('\n', stderr);
9692 noninteractive_need_newline = 0;
9693 fprintf (stderr, m, SDATA (string));
9694 if (!cursor_in_echo_area)
9695 fprintf (stderr, "\n");
9696 fflush (stderr);
9697 }
9698 }
9699 else if (INTERACTIVE)
9700 {
9701 /* The frame whose minibuffer we're going to display the message on.
9702 It may be larger than the selected frame, so we need
9703 to use its buffer, not the selected frame's buffer. */
9704 Lisp_Object mini_window;
9705 struct frame *f, *sf = SELECTED_FRAME ();
9706
9707 /* Get the frame containing the minibuffer
9708 that the selected frame is using. */
9709 mini_window = FRAME_MINIBUF_WINDOW (sf);
9710 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9711
9712 /* A null message buffer means that the frame hasn't really been
9713 initialized yet. Error messages get reported properly by
9714 cmd_error, so this must be just an informative message; toss it. */
9715 if (FRAME_MESSAGE_BUF (f))
9716 {
9717 Lisp_Object args[2], msg;
9718 struct gcpro gcpro1, gcpro2;
9719
9720 args[0] = build_string (m);
9721 args[1] = msg = string;
9722 GCPRO2 (args[0], msg);
9723 gcpro1.nvars = 2;
9724
9725 msg = Fformat (2, args);
9726
9727 if (log)
9728 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9729 else
9730 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9731
9732 UNGCPRO;
9733
9734 /* Print should start at the beginning of the message
9735 buffer next time. */
9736 message_buf_print = 0;
9737 }
9738 }
9739 }
9740
9741
9742 /* Dump an informative message to the minibuf. If M is 0, clear out
9743 any existing message, and let the mini-buffer text show through. */
9744
9745 static void
9746 vmessage (const char *m, va_list ap)
9747 {
9748 if (noninteractive)
9749 {
9750 if (m)
9751 {
9752 if (noninteractive_need_newline)
9753 putc ('\n', stderr);
9754 noninteractive_need_newline = 0;
9755 vfprintf (stderr, m, ap);
9756 if (cursor_in_echo_area == 0)
9757 fprintf (stderr, "\n");
9758 fflush (stderr);
9759 }
9760 }
9761 else if (INTERACTIVE)
9762 {
9763 /* The frame whose mini-buffer we're going to display the message
9764 on. It may be larger than the selected frame, so we need to
9765 use its buffer, not the selected frame's buffer. */
9766 Lisp_Object mini_window;
9767 struct frame *f, *sf = SELECTED_FRAME ();
9768
9769 /* Get the frame containing the mini-buffer
9770 that the selected frame is using. */
9771 mini_window = FRAME_MINIBUF_WINDOW (sf);
9772 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9773
9774 /* A null message buffer means that the frame hasn't really been
9775 initialized yet. Error messages get reported properly by
9776 cmd_error, so this must be just an informative message; toss
9777 it. */
9778 if (FRAME_MESSAGE_BUF (f))
9779 {
9780 if (m)
9781 {
9782 ptrdiff_t len;
9783
9784 len = doprnt (FRAME_MESSAGE_BUF (f),
9785 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9786
9787 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9788 }
9789 else
9790 message1 (0);
9791
9792 /* Print should start at the beginning of the message
9793 buffer next time. */
9794 message_buf_print = 0;
9795 }
9796 }
9797 }
9798
9799 void
9800 message (const char *m, ...)
9801 {
9802 va_list ap;
9803 va_start (ap, m);
9804 vmessage (m, ap);
9805 va_end (ap);
9806 }
9807
9808
9809 #if 0
9810 /* The non-logging version of message. */
9811
9812 void
9813 message_nolog (const char *m, ...)
9814 {
9815 Lisp_Object old_log_max;
9816 va_list ap;
9817 va_start (ap, m);
9818 old_log_max = Vmessage_log_max;
9819 Vmessage_log_max = Qnil;
9820 vmessage (m, ap);
9821 Vmessage_log_max = old_log_max;
9822 va_end (ap);
9823 }
9824 #endif
9825
9826
9827 /* Display the current message in the current mini-buffer. This is
9828 only called from error handlers in process.c, and is not time
9829 critical. */
9830
9831 void
9832 update_echo_area (void)
9833 {
9834 if (!NILP (echo_area_buffer[0]))
9835 {
9836 Lisp_Object string;
9837 string = Fcurrent_message ();
9838 message3 (string, SBYTES (string),
9839 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9840 }
9841 }
9842
9843
9844 /* Make sure echo area buffers in `echo_buffers' are live.
9845 If they aren't, make new ones. */
9846
9847 static void
9848 ensure_echo_area_buffers (void)
9849 {
9850 int i;
9851
9852 for (i = 0; i < 2; ++i)
9853 if (!BUFFERP (echo_buffer[i])
9854 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9855 {
9856 char name[30];
9857 Lisp_Object old_buffer;
9858 int j;
9859
9860 old_buffer = echo_buffer[i];
9861 sprintf (name, " *Echo Area %d*", i);
9862 echo_buffer[i] = Fget_buffer_create (build_string (name));
9863 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9864 /* to force word wrap in echo area -
9865 it was decided to postpone this*/
9866 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9867
9868 for (j = 0; j < 2; ++j)
9869 if (EQ (old_buffer, echo_area_buffer[j]))
9870 echo_area_buffer[j] = echo_buffer[i];
9871 }
9872 }
9873
9874
9875 /* Call FN with args A1..A4 with either the current or last displayed
9876 echo_area_buffer as current buffer.
9877
9878 WHICH zero means use the current message buffer
9879 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9880 from echo_buffer[] and clear it.
9881
9882 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9883 suitable buffer from echo_buffer[] and clear it.
9884
9885 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9886 that the current message becomes the last displayed one, make
9887 choose a suitable buffer for echo_area_buffer[0], and clear it.
9888
9889 Value is what FN returns. */
9890
9891 static int
9892 with_echo_area_buffer (struct window *w, int which,
9893 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9894 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9895 {
9896 Lisp_Object buffer;
9897 int this_one, the_other, clear_buffer_p, rc;
9898 ptrdiff_t count = SPECPDL_INDEX ();
9899
9900 /* If buffers aren't live, make new ones. */
9901 ensure_echo_area_buffers ();
9902
9903 clear_buffer_p = 0;
9904
9905 if (which == 0)
9906 this_one = 0, the_other = 1;
9907 else if (which > 0)
9908 this_one = 1, the_other = 0;
9909 else
9910 {
9911 this_one = 0, the_other = 1;
9912 clear_buffer_p = 1;
9913
9914 /* We need a fresh one in case the current echo buffer equals
9915 the one containing the last displayed echo area message. */
9916 if (!NILP (echo_area_buffer[this_one])
9917 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9918 echo_area_buffer[this_one] = Qnil;
9919 }
9920
9921 /* Choose a suitable buffer from echo_buffer[] is we don't
9922 have one. */
9923 if (NILP (echo_area_buffer[this_one]))
9924 {
9925 echo_area_buffer[this_one]
9926 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9927 ? echo_buffer[the_other]
9928 : echo_buffer[this_one]);
9929 clear_buffer_p = 1;
9930 }
9931
9932 buffer = echo_area_buffer[this_one];
9933
9934 /* Don't get confused by reusing the buffer used for echoing
9935 for a different purpose. */
9936 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9937 cancel_echoing ();
9938
9939 record_unwind_protect (unwind_with_echo_area_buffer,
9940 with_echo_area_buffer_unwind_data (w));
9941
9942 /* Make the echo area buffer current. Note that for display
9943 purposes, it is not necessary that the displayed window's buffer
9944 == current_buffer, except for text property lookup. So, let's
9945 only set that buffer temporarily here without doing a full
9946 Fset_window_buffer. We must also change w->pointm, though,
9947 because otherwise an assertions in unshow_buffer fails, and Emacs
9948 aborts. */
9949 set_buffer_internal_1 (XBUFFER (buffer));
9950 if (w)
9951 {
9952 w->buffer = buffer;
9953 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9954 }
9955
9956 BVAR (current_buffer, undo_list) = Qt;
9957 BVAR (current_buffer, read_only) = Qnil;
9958 specbind (Qinhibit_read_only, Qt);
9959 specbind (Qinhibit_modification_hooks, Qt);
9960
9961 if (clear_buffer_p && Z > BEG)
9962 del_range (BEG, Z);
9963
9964 eassert (BEGV >= BEG);
9965 eassert (ZV <= Z && ZV >= BEGV);
9966
9967 rc = fn (a1, a2, a3, a4);
9968
9969 eassert (BEGV >= BEG);
9970 eassert (ZV <= Z && ZV >= BEGV);
9971
9972 unbind_to (count, Qnil);
9973 return rc;
9974 }
9975
9976
9977 /* Save state that should be preserved around the call to the function
9978 FN called in with_echo_area_buffer. */
9979
9980 static Lisp_Object
9981 with_echo_area_buffer_unwind_data (struct window *w)
9982 {
9983 int i = 0;
9984 Lisp_Object vector, tmp;
9985
9986 /* Reduce consing by keeping one vector in
9987 Vwith_echo_area_save_vector. */
9988 vector = Vwith_echo_area_save_vector;
9989 Vwith_echo_area_save_vector = Qnil;
9990
9991 if (NILP (vector))
9992 vector = Fmake_vector (make_number (7), Qnil);
9993
9994 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9995 ASET (vector, i, Vdeactivate_mark); ++i;
9996 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9997
9998 if (w)
9999 {
10000 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10001 ASET (vector, i, w->buffer); ++i;
10002 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10003 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10004 }
10005 else
10006 {
10007 int end = i + 4;
10008 for (; i < end; ++i)
10009 ASET (vector, i, Qnil);
10010 }
10011
10012 eassert (i == ASIZE (vector));
10013 return vector;
10014 }
10015
10016
10017 /* Restore global state from VECTOR which was created by
10018 with_echo_area_buffer_unwind_data. */
10019
10020 static Lisp_Object
10021 unwind_with_echo_area_buffer (Lisp_Object vector)
10022 {
10023 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10024 Vdeactivate_mark = AREF (vector, 1);
10025 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10026
10027 if (WINDOWP (AREF (vector, 3)))
10028 {
10029 struct window *w;
10030 Lisp_Object buffer, charpos, bytepos;
10031
10032 w = XWINDOW (AREF (vector, 3));
10033 buffer = AREF (vector, 4);
10034 charpos = AREF (vector, 5);
10035 bytepos = AREF (vector, 6);
10036
10037 w->buffer = buffer;
10038 set_marker_both (w->pointm, buffer,
10039 XFASTINT (charpos), XFASTINT (bytepos));
10040 }
10041
10042 Vwith_echo_area_save_vector = vector;
10043 return Qnil;
10044 }
10045
10046
10047 /* Set up the echo area for use by print functions. MULTIBYTE_P
10048 non-zero means we will print multibyte. */
10049
10050 void
10051 setup_echo_area_for_printing (int multibyte_p)
10052 {
10053 /* If we can't find an echo area any more, exit. */
10054 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10055 Fkill_emacs (Qnil);
10056
10057 ensure_echo_area_buffers ();
10058
10059 if (!message_buf_print)
10060 {
10061 /* A message has been output since the last time we printed.
10062 Choose a fresh echo area buffer. */
10063 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10064 echo_area_buffer[0] = echo_buffer[1];
10065 else
10066 echo_area_buffer[0] = echo_buffer[0];
10067
10068 /* Switch to that buffer and clear it. */
10069 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10070 BVAR (current_buffer, truncate_lines) = Qnil;
10071
10072 if (Z > BEG)
10073 {
10074 ptrdiff_t count = SPECPDL_INDEX ();
10075 specbind (Qinhibit_read_only, Qt);
10076 /* Note that undo recording is always disabled. */
10077 del_range (BEG, Z);
10078 unbind_to (count, Qnil);
10079 }
10080 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10081
10082 /* Set up the buffer for the multibyteness we need. */
10083 if (multibyte_p
10084 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10085 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10086
10087 /* Raise the frame containing the echo area. */
10088 if (minibuffer_auto_raise)
10089 {
10090 struct frame *sf = SELECTED_FRAME ();
10091 Lisp_Object mini_window;
10092 mini_window = FRAME_MINIBUF_WINDOW (sf);
10093 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10094 }
10095
10096 message_log_maybe_newline ();
10097 message_buf_print = 1;
10098 }
10099 else
10100 {
10101 if (NILP (echo_area_buffer[0]))
10102 {
10103 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10104 echo_area_buffer[0] = echo_buffer[1];
10105 else
10106 echo_area_buffer[0] = echo_buffer[0];
10107 }
10108
10109 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10110 {
10111 /* Someone switched buffers between print requests. */
10112 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10113 BVAR (current_buffer, truncate_lines) = Qnil;
10114 }
10115 }
10116 }
10117
10118
10119 /* Display an echo area message in window W. Value is non-zero if W's
10120 height is changed. If display_last_displayed_message_p is
10121 non-zero, display the message that was last displayed, otherwise
10122 display the current message. */
10123
10124 static int
10125 display_echo_area (struct window *w)
10126 {
10127 int i, no_message_p, window_height_changed_p;
10128
10129 /* Temporarily disable garbage collections while displaying the echo
10130 area. This is done because a GC can print a message itself.
10131 That message would modify the echo area buffer's contents while a
10132 redisplay of the buffer is going on, and seriously confuse
10133 redisplay. */
10134 ptrdiff_t count = inhibit_garbage_collection ();
10135
10136 /* If there is no message, we must call display_echo_area_1
10137 nevertheless because it resizes the window. But we will have to
10138 reset the echo_area_buffer in question to nil at the end because
10139 with_echo_area_buffer will sets it to an empty buffer. */
10140 i = display_last_displayed_message_p ? 1 : 0;
10141 no_message_p = NILP (echo_area_buffer[i]);
10142
10143 window_height_changed_p
10144 = with_echo_area_buffer (w, display_last_displayed_message_p,
10145 display_echo_area_1,
10146 (intptr_t) w, Qnil, 0, 0);
10147
10148 if (no_message_p)
10149 echo_area_buffer[i] = Qnil;
10150
10151 unbind_to (count, Qnil);
10152 return window_height_changed_p;
10153 }
10154
10155
10156 /* Helper for display_echo_area. Display the current buffer which
10157 contains the current echo area message in window W, a mini-window,
10158 a pointer to which is passed in A1. A2..A4 are currently not used.
10159 Change the height of W so that all of the message is displayed.
10160 Value is non-zero if height of W was changed. */
10161
10162 static int
10163 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10164 {
10165 intptr_t i1 = a1;
10166 struct window *w = (struct window *) i1;
10167 Lisp_Object window;
10168 struct text_pos start;
10169 int window_height_changed_p = 0;
10170
10171 /* Do this before displaying, so that we have a large enough glyph
10172 matrix for the display. If we can't get enough space for the
10173 whole text, display the last N lines. That works by setting w->start. */
10174 window_height_changed_p = resize_mini_window (w, 0);
10175
10176 /* Use the starting position chosen by resize_mini_window. */
10177 SET_TEXT_POS_FROM_MARKER (start, w->start);
10178
10179 /* Display. */
10180 clear_glyph_matrix (w->desired_matrix);
10181 XSETWINDOW (window, w);
10182 try_window (window, start, 0);
10183
10184 return window_height_changed_p;
10185 }
10186
10187
10188 /* Resize the echo area window to exactly the size needed for the
10189 currently displayed message, if there is one. If a mini-buffer
10190 is active, don't shrink it. */
10191
10192 void
10193 resize_echo_area_exactly (void)
10194 {
10195 if (BUFFERP (echo_area_buffer[0])
10196 && WINDOWP (echo_area_window))
10197 {
10198 struct window *w = XWINDOW (echo_area_window);
10199 int resized_p;
10200 Lisp_Object resize_exactly;
10201
10202 if (minibuf_level == 0)
10203 resize_exactly = Qt;
10204 else
10205 resize_exactly = Qnil;
10206
10207 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10208 (intptr_t) w, resize_exactly,
10209 0, 0);
10210 if (resized_p)
10211 {
10212 ++windows_or_buffers_changed;
10213 ++update_mode_lines;
10214 redisplay_internal ();
10215 }
10216 }
10217 }
10218
10219
10220 /* Callback function for with_echo_area_buffer, when used from
10221 resize_echo_area_exactly. A1 contains a pointer to the window to
10222 resize, EXACTLY non-nil means resize the mini-window exactly to the
10223 size of the text displayed. A3 and A4 are not used. Value is what
10224 resize_mini_window returns. */
10225
10226 static int
10227 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10228 {
10229 intptr_t i1 = a1;
10230 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10231 }
10232
10233
10234 /* Resize mini-window W to fit the size of its contents. EXACT_P
10235 means size the window exactly to the size needed. Otherwise, it's
10236 only enlarged until W's buffer is empty.
10237
10238 Set W->start to the right place to begin display. If the whole
10239 contents fit, start at the beginning. Otherwise, start so as
10240 to make the end of the contents appear. This is particularly
10241 important for y-or-n-p, but seems desirable generally.
10242
10243 Value is non-zero if the window height has been changed. */
10244
10245 int
10246 resize_mini_window (struct window *w, int exact_p)
10247 {
10248 struct frame *f = XFRAME (w->frame);
10249 int window_height_changed_p = 0;
10250
10251 eassert (MINI_WINDOW_P (w));
10252
10253 /* By default, start display at the beginning. */
10254 set_marker_both (w->start, w->buffer,
10255 BUF_BEGV (XBUFFER (w->buffer)),
10256 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10257
10258 /* Don't resize windows while redisplaying a window; it would
10259 confuse redisplay functions when the size of the window they are
10260 displaying changes from under them. Such a resizing can happen,
10261 for instance, when which-func prints a long message while
10262 we are running fontification-functions. We're running these
10263 functions with safe_call which binds inhibit-redisplay to t. */
10264 if (!NILP (Vinhibit_redisplay))
10265 return 0;
10266
10267 /* Nil means don't try to resize. */
10268 if (NILP (Vresize_mini_windows)
10269 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10270 return 0;
10271
10272 if (!FRAME_MINIBUF_ONLY_P (f))
10273 {
10274 struct it it;
10275 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10276 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10277 int height;
10278 EMACS_INT max_height;
10279 int unit = FRAME_LINE_HEIGHT (f);
10280 struct text_pos start;
10281 struct buffer *old_current_buffer = NULL;
10282
10283 if (current_buffer != XBUFFER (w->buffer))
10284 {
10285 old_current_buffer = current_buffer;
10286 set_buffer_internal (XBUFFER (w->buffer));
10287 }
10288
10289 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10290
10291 /* Compute the max. number of lines specified by the user. */
10292 if (FLOATP (Vmax_mini_window_height))
10293 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10294 else if (INTEGERP (Vmax_mini_window_height))
10295 max_height = XINT (Vmax_mini_window_height);
10296 else
10297 max_height = total_height / 4;
10298
10299 /* Correct that max. height if it's bogus. */
10300 max_height = max (1, max_height);
10301 max_height = min (total_height, max_height);
10302
10303 /* Find out the height of the text in the window. */
10304 if (it.line_wrap == TRUNCATE)
10305 height = 1;
10306 else
10307 {
10308 last_height = 0;
10309 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10310 if (it.max_ascent == 0 && it.max_descent == 0)
10311 height = it.current_y + last_height;
10312 else
10313 height = it.current_y + it.max_ascent + it.max_descent;
10314 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10315 height = (height + unit - 1) / unit;
10316 }
10317
10318 /* Compute a suitable window start. */
10319 if (height > max_height)
10320 {
10321 height = max_height;
10322 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10323 move_it_vertically_backward (&it, (height - 1) * unit);
10324 start = it.current.pos;
10325 }
10326 else
10327 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10328 SET_MARKER_FROM_TEXT_POS (w->start, start);
10329
10330 if (EQ (Vresize_mini_windows, Qgrow_only))
10331 {
10332 /* Let it grow only, until we display an empty message, in which
10333 case the window shrinks again. */
10334 if (height > WINDOW_TOTAL_LINES (w))
10335 {
10336 int old_height = WINDOW_TOTAL_LINES (w);
10337 freeze_window_starts (f, 1);
10338 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10339 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10340 }
10341 else if (height < WINDOW_TOTAL_LINES (w)
10342 && (exact_p || BEGV == ZV))
10343 {
10344 int old_height = WINDOW_TOTAL_LINES (w);
10345 freeze_window_starts (f, 0);
10346 shrink_mini_window (w);
10347 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10348 }
10349 }
10350 else
10351 {
10352 /* Always resize to exact size needed. */
10353 if (height > WINDOW_TOTAL_LINES (w))
10354 {
10355 int old_height = WINDOW_TOTAL_LINES (w);
10356 freeze_window_starts (f, 1);
10357 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10358 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10359 }
10360 else if (height < WINDOW_TOTAL_LINES (w))
10361 {
10362 int old_height = WINDOW_TOTAL_LINES (w);
10363 freeze_window_starts (f, 0);
10364 shrink_mini_window (w);
10365
10366 if (height)
10367 {
10368 freeze_window_starts (f, 1);
10369 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10370 }
10371
10372 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10373 }
10374 }
10375
10376 if (old_current_buffer)
10377 set_buffer_internal (old_current_buffer);
10378 }
10379
10380 return window_height_changed_p;
10381 }
10382
10383
10384 /* Value is the current message, a string, or nil if there is no
10385 current message. */
10386
10387 Lisp_Object
10388 current_message (void)
10389 {
10390 Lisp_Object msg;
10391
10392 if (!BUFFERP (echo_area_buffer[0]))
10393 msg = Qnil;
10394 else
10395 {
10396 with_echo_area_buffer (0, 0, current_message_1,
10397 (intptr_t) &msg, Qnil, 0, 0);
10398 if (NILP (msg))
10399 echo_area_buffer[0] = Qnil;
10400 }
10401
10402 return msg;
10403 }
10404
10405
10406 static int
10407 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10408 {
10409 intptr_t i1 = a1;
10410 Lisp_Object *msg = (Lisp_Object *) i1;
10411
10412 if (Z > BEG)
10413 *msg = make_buffer_string (BEG, Z, 1);
10414 else
10415 *msg = Qnil;
10416 return 0;
10417 }
10418
10419
10420 /* Push the current message on Vmessage_stack for later restoration
10421 by restore_message. Value is non-zero if the current message isn't
10422 empty. This is a relatively infrequent operation, so it's not
10423 worth optimizing. */
10424
10425 int
10426 push_message (void)
10427 {
10428 Lisp_Object msg;
10429 msg = current_message ();
10430 Vmessage_stack = Fcons (msg, Vmessage_stack);
10431 return STRINGP (msg);
10432 }
10433
10434
10435 /* Restore message display from the top of Vmessage_stack. */
10436
10437 void
10438 restore_message (void)
10439 {
10440 Lisp_Object msg;
10441
10442 eassert (CONSP (Vmessage_stack));
10443 msg = XCAR (Vmessage_stack);
10444 if (STRINGP (msg))
10445 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10446 else
10447 message3_nolog (msg, 0, 0);
10448 }
10449
10450
10451 /* Handler for record_unwind_protect calling pop_message. */
10452
10453 Lisp_Object
10454 pop_message_unwind (Lisp_Object dummy)
10455 {
10456 pop_message ();
10457 return Qnil;
10458 }
10459
10460 /* Pop the top-most entry off Vmessage_stack. */
10461
10462 static void
10463 pop_message (void)
10464 {
10465 eassert (CONSP (Vmessage_stack));
10466 Vmessage_stack = XCDR (Vmessage_stack);
10467 }
10468
10469
10470 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10471 exits. If the stack is not empty, we have a missing pop_message
10472 somewhere. */
10473
10474 void
10475 check_message_stack (void)
10476 {
10477 if (!NILP (Vmessage_stack))
10478 abort ();
10479 }
10480
10481
10482 /* Truncate to NCHARS what will be displayed in the echo area the next
10483 time we display it---but don't redisplay it now. */
10484
10485 void
10486 truncate_echo_area (ptrdiff_t nchars)
10487 {
10488 if (nchars == 0)
10489 echo_area_buffer[0] = Qnil;
10490 /* A null message buffer means that the frame hasn't really been
10491 initialized yet. Error messages get reported properly by
10492 cmd_error, so this must be just an informative message; toss it. */
10493 else if (!noninteractive
10494 && INTERACTIVE
10495 && !NILP (echo_area_buffer[0]))
10496 {
10497 struct frame *sf = SELECTED_FRAME ();
10498 if (FRAME_MESSAGE_BUF (sf))
10499 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10500 }
10501 }
10502
10503
10504 /* Helper function for truncate_echo_area. Truncate the current
10505 message to at most NCHARS characters. */
10506
10507 static int
10508 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10509 {
10510 if (BEG + nchars < Z)
10511 del_range (BEG + nchars, Z);
10512 if (Z == BEG)
10513 echo_area_buffer[0] = Qnil;
10514 return 0;
10515 }
10516
10517
10518 /* Set the current message to a substring of S or STRING.
10519
10520 If STRING is a Lisp string, set the message to the first NBYTES
10521 bytes from STRING. NBYTES zero means use the whole string. If
10522 STRING is multibyte, the message will be displayed multibyte.
10523
10524 If S is not null, set the message to the first LEN bytes of S. LEN
10525 zero means use the whole string. MULTIBYTE_P non-zero means S is
10526 multibyte. Display the message multibyte in that case.
10527
10528 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10529 to t before calling set_message_1 (which calls insert).
10530 */
10531
10532 static void
10533 set_message (const char *s, Lisp_Object string,
10534 ptrdiff_t nbytes, int multibyte_p)
10535 {
10536 message_enable_multibyte
10537 = ((s && multibyte_p)
10538 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10539
10540 with_echo_area_buffer (0, -1, set_message_1,
10541 (intptr_t) s, string, nbytes, multibyte_p);
10542 message_buf_print = 0;
10543 help_echo_showing_p = 0;
10544 }
10545
10546
10547 /* Helper function for set_message. Arguments have the same meaning
10548 as there, with A1 corresponding to S and A2 corresponding to STRING
10549 This function is called with the echo area buffer being
10550 current. */
10551
10552 static int
10553 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10554 {
10555 intptr_t i1 = a1;
10556 const char *s = (const char *) i1;
10557 const unsigned char *msg = (const unsigned char *) s;
10558 Lisp_Object string = a2;
10559
10560 /* Change multibyteness of the echo buffer appropriately. */
10561 if (message_enable_multibyte
10562 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10563 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10564
10565 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10566 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10567 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10568
10569 /* Insert new message at BEG. */
10570 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10571
10572 if (STRINGP (string))
10573 {
10574 ptrdiff_t nchars;
10575
10576 if (nbytes == 0)
10577 nbytes = SBYTES (string);
10578 nchars = string_byte_to_char (string, nbytes);
10579
10580 /* This function takes care of single/multibyte conversion. We
10581 just have to ensure that the echo area buffer has the right
10582 setting of enable_multibyte_characters. */
10583 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10584 }
10585 else if (s)
10586 {
10587 if (nbytes == 0)
10588 nbytes = strlen (s);
10589
10590 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10591 {
10592 /* Convert from multi-byte to single-byte. */
10593 ptrdiff_t i;
10594 int c, n;
10595 char work[1];
10596
10597 /* Convert a multibyte string to single-byte. */
10598 for (i = 0; i < nbytes; i += n)
10599 {
10600 c = string_char_and_length (msg + i, &n);
10601 work[0] = (ASCII_CHAR_P (c)
10602 ? c
10603 : multibyte_char_to_unibyte (c));
10604 insert_1_both (work, 1, 1, 1, 0, 0);
10605 }
10606 }
10607 else if (!multibyte_p
10608 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10609 {
10610 /* Convert from single-byte to multi-byte. */
10611 ptrdiff_t i;
10612 int c, n;
10613 unsigned char str[MAX_MULTIBYTE_LENGTH];
10614
10615 /* Convert a single-byte string to multibyte. */
10616 for (i = 0; i < nbytes; i++)
10617 {
10618 c = msg[i];
10619 MAKE_CHAR_MULTIBYTE (c);
10620 n = CHAR_STRING (c, str);
10621 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10622 }
10623 }
10624 else
10625 insert_1 (s, nbytes, 1, 0, 0);
10626 }
10627
10628 return 0;
10629 }
10630
10631
10632 /* Clear messages. CURRENT_P non-zero means clear the current
10633 message. LAST_DISPLAYED_P non-zero means clear the message
10634 last displayed. */
10635
10636 void
10637 clear_message (int current_p, int last_displayed_p)
10638 {
10639 if (current_p)
10640 {
10641 echo_area_buffer[0] = Qnil;
10642 message_cleared_p = 1;
10643 }
10644
10645 if (last_displayed_p)
10646 echo_area_buffer[1] = Qnil;
10647
10648 message_buf_print = 0;
10649 }
10650
10651 /* Clear garbaged frames.
10652
10653 This function is used where the old redisplay called
10654 redraw_garbaged_frames which in turn called redraw_frame which in
10655 turn called clear_frame. The call to clear_frame was a source of
10656 flickering. I believe a clear_frame is not necessary. It should
10657 suffice in the new redisplay to invalidate all current matrices,
10658 and ensure a complete redisplay of all windows. */
10659
10660 static void
10661 clear_garbaged_frames (void)
10662 {
10663 if (frame_garbaged)
10664 {
10665 Lisp_Object tail, frame;
10666 int changed_count = 0;
10667
10668 FOR_EACH_FRAME (tail, frame)
10669 {
10670 struct frame *f = XFRAME (frame);
10671
10672 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10673 {
10674 if (f->resized_p)
10675 {
10676 Fredraw_frame (frame);
10677 f->force_flush_display_p = 1;
10678 }
10679 clear_current_matrices (f);
10680 changed_count++;
10681 f->garbaged = 0;
10682 f->resized_p = 0;
10683 }
10684 }
10685
10686 frame_garbaged = 0;
10687 if (changed_count)
10688 ++windows_or_buffers_changed;
10689 }
10690 }
10691
10692
10693 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10694 is non-zero update selected_frame. Value is non-zero if the
10695 mini-windows height has been changed. */
10696
10697 static int
10698 echo_area_display (int update_frame_p)
10699 {
10700 Lisp_Object mini_window;
10701 struct window *w;
10702 struct frame *f;
10703 int window_height_changed_p = 0;
10704 struct frame *sf = SELECTED_FRAME ();
10705
10706 mini_window = FRAME_MINIBUF_WINDOW (sf);
10707 w = XWINDOW (mini_window);
10708 f = XFRAME (WINDOW_FRAME (w));
10709
10710 /* Don't display if frame is invisible or not yet initialized. */
10711 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10712 return 0;
10713
10714 #ifdef HAVE_WINDOW_SYSTEM
10715 /* When Emacs starts, selected_frame may be the initial terminal
10716 frame. If we let this through, a message would be displayed on
10717 the terminal. */
10718 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10719 return 0;
10720 #endif /* HAVE_WINDOW_SYSTEM */
10721
10722 /* Redraw garbaged frames. */
10723 if (frame_garbaged)
10724 clear_garbaged_frames ();
10725
10726 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10727 {
10728 echo_area_window = mini_window;
10729 window_height_changed_p = display_echo_area (w);
10730 w->must_be_updated_p = 1;
10731
10732 /* Update the display, unless called from redisplay_internal.
10733 Also don't update the screen during redisplay itself. The
10734 update will happen at the end of redisplay, and an update
10735 here could cause confusion. */
10736 if (update_frame_p && !redisplaying_p)
10737 {
10738 int n = 0;
10739
10740 /* If the display update has been interrupted by pending
10741 input, update mode lines in the frame. Due to the
10742 pending input, it might have been that redisplay hasn't
10743 been called, so that mode lines above the echo area are
10744 garbaged. This looks odd, so we prevent it here. */
10745 if (!display_completed)
10746 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10747
10748 if (window_height_changed_p
10749 /* Don't do this if Emacs is shutting down. Redisplay
10750 needs to run hooks. */
10751 && !NILP (Vrun_hooks))
10752 {
10753 /* Must update other windows. Likewise as in other
10754 cases, don't let this update be interrupted by
10755 pending input. */
10756 ptrdiff_t count = SPECPDL_INDEX ();
10757 specbind (Qredisplay_dont_pause, Qt);
10758 windows_or_buffers_changed = 1;
10759 redisplay_internal ();
10760 unbind_to (count, Qnil);
10761 }
10762 else if (FRAME_WINDOW_P (f) && n == 0)
10763 {
10764 /* Window configuration is the same as before.
10765 Can do with a display update of the echo area,
10766 unless we displayed some mode lines. */
10767 update_single_window (w, 1);
10768 FRAME_RIF (f)->flush_display (f);
10769 }
10770 else
10771 update_frame (f, 1, 1);
10772
10773 /* If cursor is in the echo area, make sure that the next
10774 redisplay displays the minibuffer, so that the cursor will
10775 be replaced with what the minibuffer wants. */
10776 if (cursor_in_echo_area)
10777 ++windows_or_buffers_changed;
10778 }
10779 }
10780 else if (!EQ (mini_window, selected_window))
10781 windows_or_buffers_changed++;
10782
10783 /* Last displayed message is now the current message. */
10784 echo_area_buffer[1] = echo_area_buffer[0];
10785 /* Inform read_char that we're not echoing. */
10786 echo_message_buffer = Qnil;
10787
10788 /* Prevent redisplay optimization in redisplay_internal by resetting
10789 this_line_start_pos. This is done because the mini-buffer now
10790 displays the message instead of its buffer text. */
10791 if (EQ (mini_window, selected_window))
10792 CHARPOS (this_line_start_pos) = 0;
10793
10794 return window_height_changed_p;
10795 }
10796
10797
10798 \f
10799 /***********************************************************************
10800 Mode Lines and Frame Titles
10801 ***********************************************************************/
10802
10803 /* A buffer for constructing non-propertized mode-line strings and
10804 frame titles in it; allocated from the heap in init_xdisp and
10805 resized as needed in store_mode_line_noprop_char. */
10806
10807 static char *mode_line_noprop_buf;
10808
10809 /* The buffer's end, and a current output position in it. */
10810
10811 static char *mode_line_noprop_buf_end;
10812 static char *mode_line_noprop_ptr;
10813
10814 #define MODE_LINE_NOPROP_LEN(start) \
10815 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10816
10817 static enum {
10818 MODE_LINE_DISPLAY = 0,
10819 MODE_LINE_TITLE,
10820 MODE_LINE_NOPROP,
10821 MODE_LINE_STRING
10822 } mode_line_target;
10823
10824 /* Alist that caches the results of :propertize.
10825 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10826 static Lisp_Object mode_line_proptrans_alist;
10827
10828 /* List of strings making up the mode-line. */
10829 static Lisp_Object mode_line_string_list;
10830
10831 /* Base face property when building propertized mode line string. */
10832 static Lisp_Object mode_line_string_face;
10833 static Lisp_Object mode_line_string_face_prop;
10834
10835
10836 /* Unwind data for mode line strings */
10837
10838 static Lisp_Object Vmode_line_unwind_vector;
10839
10840 static Lisp_Object
10841 format_mode_line_unwind_data (struct frame *target_frame,
10842 struct buffer *obuf,
10843 Lisp_Object owin,
10844 int save_proptrans)
10845 {
10846 Lisp_Object vector, tmp;
10847
10848 /* Reduce consing by keeping one vector in
10849 Vwith_echo_area_save_vector. */
10850 vector = Vmode_line_unwind_vector;
10851 Vmode_line_unwind_vector = Qnil;
10852
10853 if (NILP (vector))
10854 vector = Fmake_vector (make_number (10), Qnil);
10855
10856 ASET (vector, 0, make_number (mode_line_target));
10857 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10858 ASET (vector, 2, mode_line_string_list);
10859 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10860 ASET (vector, 4, mode_line_string_face);
10861 ASET (vector, 5, mode_line_string_face_prop);
10862
10863 if (obuf)
10864 XSETBUFFER (tmp, obuf);
10865 else
10866 tmp = Qnil;
10867 ASET (vector, 6, tmp);
10868 ASET (vector, 7, owin);
10869 if (target_frame)
10870 {
10871 /* Similarly to `with-selected-window', if the operation selects
10872 a window on another frame, we must restore that frame's
10873 selected window, and (for a tty) the top-frame. */
10874 ASET (vector, 8, target_frame->selected_window);
10875 if (FRAME_TERMCAP_P (target_frame))
10876 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10877 }
10878
10879 return vector;
10880 }
10881
10882 static Lisp_Object
10883 unwind_format_mode_line (Lisp_Object vector)
10884 {
10885 Lisp_Object old_window = AREF (vector, 7);
10886 Lisp_Object target_frame_window = AREF (vector, 8);
10887 Lisp_Object old_top_frame = AREF (vector, 9);
10888
10889 mode_line_target = XINT (AREF (vector, 0));
10890 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10891 mode_line_string_list = AREF (vector, 2);
10892 if (! EQ (AREF (vector, 3), Qt))
10893 mode_line_proptrans_alist = AREF (vector, 3);
10894 mode_line_string_face = AREF (vector, 4);
10895 mode_line_string_face_prop = AREF (vector, 5);
10896
10897 /* Select window before buffer, since it may change the buffer. */
10898 if (!NILP (old_window))
10899 {
10900 /* If the operation that we are unwinding had selected a window
10901 on a different frame, reset its frame-selected-window. For a
10902 text terminal, reset its top-frame if necessary. */
10903 if (!NILP (target_frame_window))
10904 {
10905 Lisp_Object frame
10906 = WINDOW_FRAME (XWINDOW (target_frame_window));
10907
10908 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10909 Fselect_window (target_frame_window, Qt);
10910
10911 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10912 Fselect_frame (old_top_frame, Qt);
10913 }
10914
10915 Fselect_window (old_window, Qt);
10916 }
10917
10918 if (!NILP (AREF (vector, 6)))
10919 {
10920 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10921 ASET (vector, 6, Qnil);
10922 }
10923
10924 Vmode_line_unwind_vector = vector;
10925 return Qnil;
10926 }
10927
10928
10929 /* Store a single character C for the frame title in mode_line_noprop_buf.
10930 Re-allocate mode_line_noprop_buf if necessary. */
10931
10932 static void
10933 store_mode_line_noprop_char (char c)
10934 {
10935 /* If output position has reached the end of the allocated buffer,
10936 increase the buffer's size. */
10937 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10938 {
10939 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10940 ptrdiff_t size = len;
10941 mode_line_noprop_buf =
10942 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10943 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10944 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10945 }
10946
10947 *mode_line_noprop_ptr++ = c;
10948 }
10949
10950
10951 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10952 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10953 characters that yield more columns than PRECISION; PRECISION <= 0
10954 means copy the whole string. Pad with spaces until FIELD_WIDTH
10955 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10956 pad. Called from display_mode_element when it is used to build a
10957 frame title. */
10958
10959 static int
10960 store_mode_line_noprop (const char *string, int field_width, int precision)
10961 {
10962 const unsigned char *str = (const unsigned char *) string;
10963 int n = 0;
10964 ptrdiff_t dummy, nbytes;
10965
10966 /* Copy at most PRECISION chars from STR. */
10967 nbytes = strlen (string);
10968 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10969 while (nbytes--)
10970 store_mode_line_noprop_char (*str++);
10971
10972 /* Fill up with spaces until FIELD_WIDTH reached. */
10973 while (field_width > 0
10974 && n < field_width)
10975 {
10976 store_mode_line_noprop_char (' ');
10977 ++n;
10978 }
10979
10980 return n;
10981 }
10982
10983 /***********************************************************************
10984 Frame Titles
10985 ***********************************************************************/
10986
10987 #ifdef HAVE_WINDOW_SYSTEM
10988
10989 /* Set the title of FRAME, if it has changed. The title format is
10990 Vicon_title_format if FRAME is iconified, otherwise it is
10991 frame_title_format. */
10992
10993 static void
10994 x_consider_frame_title (Lisp_Object frame)
10995 {
10996 struct frame *f = XFRAME (frame);
10997
10998 if (FRAME_WINDOW_P (f)
10999 || FRAME_MINIBUF_ONLY_P (f)
11000 || f->explicit_name)
11001 {
11002 /* Do we have more than one visible frame on this X display? */
11003 Lisp_Object tail;
11004 Lisp_Object fmt;
11005 ptrdiff_t title_start;
11006 char *title;
11007 ptrdiff_t len;
11008 struct it it;
11009 ptrdiff_t count = SPECPDL_INDEX ();
11010
11011 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11012 {
11013 Lisp_Object other_frame = XCAR (tail);
11014 struct frame *tf = XFRAME (other_frame);
11015
11016 if (tf != f
11017 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11018 && !FRAME_MINIBUF_ONLY_P (tf)
11019 && !EQ (other_frame, tip_frame)
11020 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11021 break;
11022 }
11023
11024 /* Set global variable indicating that multiple frames exist. */
11025 multiple_frames = CONSP (tail);
11026
11027 /* Switch to the buffer of selected window of the frame. Set up
11028 mode_line_target so that display_mode_element will output into
11029 mode_line_noprop_buf; then display the title. */
11030 record_unwind_protect (unwind_format_mode_line,
11031 format_mode_line_unwind_data
11032 (f, current_buffer, selected_window, 0));
11033
11034 Fselect_window (f->selected_window, Qt);
11035 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11036 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11037
11038 mode_line_target = MODE_LINE_TITLE;
11039 title_start = MODE_LINE_NOPROP_LEN (0);
11040 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11041 NULL, DEFAULT_FACE_ID);
11042 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11043 len = MODE_LINE_NOPROP_LEN (title_start);
11044 title = mode_line_noprop_buf + title_start;
11045 unbind_to (count, Qnil);
11046
11047 /* Set the title only if it's changed. This avoids consing in
11048 the common case where it hasn't. (If it turns out that we've
11049 already wasted too much time by walking through the list with
11050 display_mode_element, then we might need to optimize at a
11051 higher level than this.) */
11052 if (! STRINGP (f->name)
11053 || SBYTES (f->name) != len
11054 || memcmp (title, SDATA (f->name), len) != 0)
11055 x_implicitly_set_name (f, make_string (title, len), Qnil);
11056 }
11057 }
11058
11059 #endif /* not HAVE_WINDOW_SYSTEM */
11060
11061 \f
11062 /***********************************************************************
11063 Menu Bars
11064 ***********************************************************************/
11065
11066
11067 /* Prepare for redisplay by updating menu-bar item lists when
11068 appropriate. This can call eval. */
11069
11070 void
11071 prepare_menu_bars (void)
11072 {
11073 int all_windows;
11074 struct gcpro gcpro1, gcpro2;
11075 struct frame *f;
11076 Lisp_Object tooltip_frame;
11077
11078 #ifdef HAVE_WINDOW_SYSTEM
11079 tooltip_frame = tip_frame;
11080 #else
11081 tooltip_frame = Qnil;
11082 #endif
11083
11084 /* Update all frame titles based on their buffer names, etc. We do
11085 this before the menu bars so that the buffer-menu will show the
11086 up-to-date frame titles. */
11087 #ifdef HAVE_WINDOW_SYSTEM
11088 if (windows_or_buffers_changed || update_mode_lines)
11089 {
11090 Lisp_Object tail, frame;
11091
11092 FOR_EACH_FRAME (tail, frame)
11093 {
11094 f = XFRAME (frame);
11095 if (!EQ (frame, tooltip_frame)
11096 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11097 x_consider_frame_title (frame);
11098 }
11099 }
11100 #endif /* HAVE_WINDOW_SYSTEM */
11101
11102 /* Update the menu bar item lists, if appropriate. This has to be
11103 done before any actual redisplay or generation of display lines. */
11104 all_windows = (update_mode_lines
11105 || buffer_shared > 1
11106 || windows_or_buffers_changed);
11107 if (all_windows)
11108 {
11109 Lisp_Object tail, frame;
11110 ptrdiff_t count = SPECPDL_INDEX ();
11111 /* 1 means that update_menu_bar has run its hooks
11112 so any further calls to update_menu_bar shouldn't do so again. */
11113 int menu_bar_hooks_run = 0;
11114
11115 record_unwind_save_match_data ();
11116
11117 FOR_EACH_FRAME (tail, frame)
11118 {
11119 f = XFRAME (frame);
11120
11121 /* Ignore tooltip frame. */
11122 if (EQ (frame, tooltip_frame))
11123 continue;
11124
11125 /* If a window on this frame changed size, report that to
11126 the user and clear the size-change flag. */
11127 if (FRAME_WINDOW_SIZES_CHANGED (f))
11128 {
11129 Lisp_Object functions;
11130
11131 /* Clear flag first in case we get an error below. */
11132 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11133 functions = Vwindow_size_change_functions;
11134 GCPRO2 (tail, functions);
11135
11136 while (CONSP (functions))
11137 {
11138 if (!EQ (XCAR (functions), Qt))
11139 call1 (XCAR (functions), frame);
11140 functions = XCDR (functions);
11141 }
11142 UNGCPRO;
11143 }
11144
11145 GCPRO1 (tail);
11146 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11147 #ifdef HAVE_WINDOW_SYSTEM
11148 update_tool_bar (f, 0);
11149 #endif
11150 #ifdef HAVE_NS
11151 if (windows_or_buffers_changed
11152 && FRAME_NS_P (f))
11153 ns_set_doc_edited (f, Fbuffer_modified_p
11154 (XWINDOW (f->selected_window)->buffer));
11155 #endif
11156 UNGCPRO;
11157 }
11158
11159 unbind_to (count, Qnil);
11160 }
11161 else
11162 {
11163 struct frame *sf = SELECTED_FRAME ();
11164 update_menu_bar (sf, 1, 0);
11165 #ifdef HAVE_WINDOW_SYSTEM
11166 update_tool_bar (sf, 1);
11167 #endif
11168 }
11169 }
11170
11171
11172 /* Update the menu bar item list for frame F. This has to be done
11173 before we start to fill in any display lines, because it can call
11174 eval.
11175
11176 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11177
11178 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11179 already ran the menu bar hooks for this redisplay, so there
11180 is no need to run them again. The return value is the
11181 updated value of this flag, to pass to the next call. */
11182
11183 static int
11184 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11185 {
11186 Lisp_Object window;
11187 register struct window *w;
11188
11189 /* If called recursively during a menu update, do nothing. This can
11190 happen when, for instance, an activate-menubar-hook causes a
11191 redisplay. */
11192 if (inhibit_menubar_update)
11193 return hooks_run;
11194
11195 window = FRAME_SELECTED_WINDOW (f);
11196 w = XWINDOW (window);
11197
11198 if (FRAME_WINDOW_P (f)
11199 ?
11200 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11201 || defined (HAVE_NS) || defined (USE_GTK)
11202 FRAME_EXTERNAL_MENU_BAR (f)
11203 #else
11204 FRAME_MENU_BAR_LINES (f) > 0
11205 #endif
11206 : FRAME_MENU_BAR_LINES (f) > 0)
11207 {
11208 /* If the user has switched buffers or windows, we need to
11209 recompute to reflect the new bindings. But we'll
11210 recompute when update_mode_lines is set too; that means
11211 that people can use force-mode-line-update to request
11212 that the menu bar be recomputed. The adverse effect on
11213 the rest of the redisplay algorithm is about the same as
11214 windows_or_buffers_changed anyway. */
11215 if (windows_or_buffers_changed
11216 /* This used to test w->update_mode_line, but we believe
11217 there is no need to recompute the menu in that case. */
11218 || update_mode_lines
11219 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11220 < BUF_MODIFF (XBUFFER (w->buffer)))
11221 != w->last_had_star)
11222 || ((!NILP (Vtransient_mark_mode)
11223 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11224 != !NILP (w->region_showing)))
11225 {
11226 struct buffer *prev = current_buffer;
11227 ptrdiff_t count = SPECPDL_INDEX ();
11228
11229 specbind (Qinhibit_menubar_update, Qt);
11230
11231 set_buffer_internal_1 (XBUFFER (w->buffer));
11232 if (save_match_data)
11233 record_unwind_save_match_data ();
11234 if (NILP (Voverriding_local_map_menu_flag))
11235 {
11236 specbind (Qoverriding_terminal_local_map, Qnil);
11237 specbind (Qoverriding_local_map, Qnil);
11238 }
11239
11240 if (!hooks_run)
11241 {
11242 /* Run the Lucid hook. */
11243 safe_run_hooks (Qactivate_menubar_hook);
11244
11245 /* If it has changed current-menubar from previous value,
11246 really recompute the menu-bar from the value. */
11247 if (! NILP (Vlucid_menu_bar_dirty_flag))
11248 call0 (Qrecompute_lucid_menubar);
11249
11250 safe_run_hooks (Qmenu_bar_update_hook);
11251
11252 hooks_run = 1;
11253 }
11254
11255 XSETFRAME (Vmenu_updating_frame, f);
11256 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11257
11258 /* Redisplay the menu bar in case we changed it. */
11259 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11260 || defined (HAVE_NS) || defined (USE_GTK)
11261 if (FRAME_WINDOW_P (f))
11262 {
11263 #if defined (HAVE_NS)
11264 /* All frames on Mac OS share the same menubar. So only
11265 the selected frame should be allowed to set it. */
11266 if (f == SELECTED_FRAME ())
11267 #endif
11268 set_frame_menubar (f, 0, 0);
11269 }
11270 else
11271 /* On a terminal screen, the menu bar is an ordinary screen
11272 line, and this makes it get updated. */
11273 w->update_mode_line = 1;
11274 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11275 /* In the non-toolkit version, the menu bar is an ordinary screen
11276 line, and this makes it get updated. */
11277 w->update_mode_line = 1;
11278 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11279
11280 unbind_to (count, Qnil);
11281 set_buffer_internal_1 (prev);
11282 }
11283 }
11284
11285 return hooks_run;
11286 }
11287
11288
11289 \f
11290 /***********************************************************************
11291 Output Cursor
11292 ***********************************************************************/
11293
11294 #ifdef HAVE_WINDOW_SYSTEM
11295
11296 /* EXPORT:
11297 Nominal cursor position -- where to draw output.
11298 HPOS and VPOS are window relative glyph matrix coordinates.
11299 X and Y are window relative pixel coordinates. */
11300
11301 struct cursor_pos output_cursor;
11302
11303
11304 /* EXPORT:
11305 Set the global variable output_cursor to CURSOR. All cursor
11306 positions are relative to updated_window. */
11307
11308 void
11309 set_output_cursor (struct cursor_pos *cursor)
11310 {
11311 output_cursor.hpos = cursor->hpos;
11312 output_cursor.vpos = cursor->vpos;
11313 output_cursor.x = cursor->x;
11314 output_cursor.y = cursor->y;
11315 }
11316
11317
11318 /* EXPORT for RIF:
11319 Set a nominal cursor position.
11320
11321 HPOS and VPOS are column/row positions in a window glyph matrix. X
11322 and Y are window text area relative pixel positions.
11323
11324 If this is done during an update, updated_window will contain the
11325 window that is being updated and the position is the future output
11326 cursor position for that window. If updated_window is null, use
11327 selected_window and display the cursor at the given position. */
11328
11329 void
11330 x_cursor_to (int vpos, int hpos, int y, int x)
11331 {
11332 struct window *w;
11333
11334 /* If updated_window is not set, work on selected_window. */
11335 if (updated_window)
11336 w = updated_window;
11337 else
11338 w = XWINDOW (selected_window);
11339
11340 /* Set the output cursor. */
11341 output_cursor.hpos = hpos;
11342 output_cursor.vpos = vpos;
11343 output_cursor.x = x;
11344 output_cursor.y = y;
11345
11346 /* If not called as part of an update, really display the cursor.
11347 This will also set the cursor position of W. */
11348 if (updated_window == NULL)
11349 {
11350 BLOCK_INPUT;
11351 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11352 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11353 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11354 UNBLOCK_INPUT;
11355 }
11356 }
11357
11358 #endif /* HAVE_WINDOW_SYSTEM */
11359
11360 \f
11361 /***********************************************************************
11362 Tool-bars
11363 ***********************************************************************/
11364
11365 #ifdef HAVE_WINDOW_SYSTEM
11366
11367 /* Where the mouse was last time we reported a mouse event. */
11368
11369 FRAME_PTR last_mouse_frame;
11370
11371 /* Tool-bar item index of the item on which a mouse button was pressed
11372 or -1. */
11373
11374 int last_tool_bar_item;
11375
11376
11377 static Lisp_Object
11378 update_tool_bar_unwind (Lisp_Object frame)
11379 {
11380 selected_frame = frame;
11381 return Qnil;
11382 }
11383
11384 /* Update the tool-bar item list for frame F. This has to be done
11385 before we start to fill in any display lines. Called from
11386 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11387 and restore it here. */
11388
11389 static void
11390 update_tool_bar (struct frame *f, int save_match_data)
11391 {
11392 #if defined (USE_GTK) || defined (HAVE_NS)
11393 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11394 #else
11395 int do_update = WINDOWP (f->tool_bar_window)
11396 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11397 #endif
11398
11399 if (do_update)
11400 {
11401 Lisp_Object window;
11402 struct window *w;
11403
11404 window = FRAME_SELECTED_WINDOW (f);
11405 w = XWINDOW (window);
11406
11407 /* If the user has switched buffers or windows, we need to
11408 recompute to reflect the new bindings. But we'll
11409 recompute when update_mode_lines is set too; that means
11410 that people can use force-mode-line-update to request
11411 that the menu bar be recomputed. The adverse effect on
11412 the rest of the redisplay algorithm is about the same as
11413 windows_or_buffers_changed anyway. */
11414 if (windows_or_buffers_changed
11415 || w->update_mode_line
11416 || update_mode_lines
11417 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11418 < BUF_MODIFF (XBUFFER (w->buffer)))
11419 != w->last_had_star)
11420 || ((!NILP (Vtransient_mark_mode)
11421 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11422 != !NILP (w->region_showing)))
11423 {
11424 struct buffer *prev = current_buffer;
11425 ptrdiff_t count = SPECPDL_INDEX ();
11426 Lisp_Object frame, new_tool_bar;
11427 int new_n_tool_bar;
11428 struct gcpro gcpro1;
11429
11430 /* Set current_buffer to the buffer of the selected
11431 window of the frame, so that we get the right local
11432 keymaps. */
11433 set_buffer_internal_1 (XBUFFER (w->buffer));
11434
11435 /* Save match data, if we must. */
11436 if (save_match_data)
11437 record_unwind_save_match_data ();
11438
11439 /* Make sure that we don't accidentally use bogus keymaps. */
11440 if (NILP (Voverriding_local_map_menu_flag))
11441 {
11442 specbind (Qoverriding_terminal_local_map, Qnil);
11443 specbind (Qoverriding_local_map, Qnil);
11444 }
11445
11446 GCPRO1 (new_tool_bar);
11447
11448 /* We must temporarily set the selected frame to this frame
11449 before calling tool_bar_items, because the calculation of
11450 the tool-bar keymap uses the selected frame (see
11451 `tool-bar-make-keymap' in tool-bar.el). */
11452 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11453 XSETFRAME (frame, f);
11454 selected_frame = frame;
11455
11456 /* Build desired tool-bar items from keymaps. */
11457 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11458 &new_n_tool_bar);
11459
11460 /* Redisplay the tool-bar if we changed it. */
11461 if (new_n_tool_bar != f->n_tool_bar_items
11462 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11463 {
11464 /* Redisplay that happens asynchronously due to an expose event
11465 may access f->tool_bar_items. Make sure we update both
11466 variables within BLOCK_INPUT so no such event interrupts. */
11467 BLOCK_INPUT;
11468 f->tool_bar_items = new_tool_bar;
11469 f->n_tool_bar_items = new_n_tool_bar;
11470 w->update_mode_line = 1;
11471 UNBLOCK_INPUT;
11472 }
11473
11474 UNGCPRO;
11475
11476 unbind_to (count, Qnil);
11477 set_buffer_internal_1 (prev);
11478 }
11479 }
11480 }
11481
11482
11483 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11484 F's desired tool-bar contents. F->tool_bar_items must have
11485 been set up previously by calling prepare_menu_bars. */
11486
11487 static void
11488 build_desired_tool_bar_string (struct frame *f)
11489 {
11490 int i, size, size_needed;
11491 struct gcpro gcpro1, gcpro2, gcpro3;
11492 Lisp_Object image, plist, props;
11493
11494 image = plist = props = Qnil;
11495 GCPRO3 (image, plist, props);
11496
11497 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11498 Otherwise, make a new string. */
11499
11500 /* The size of the string we might be able to reuse. */
11501 size = (STRINGP (f->desired_tool_bar_string)
11502 ? SCHARS (f->desired_tool_bar_string)
11503 : 0);
11504
11505 /* We need one space in the string for each image. */
11506 size_needed = f->n_tool_bar_items;
11507
11508 /* Reuse f->desired_tool_bar_string, if possible. */
11509 if (size < size_needed || NILP (f->desired_tool_bar_string))
11510 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11511 make_number (' '));
11512 else
11513 {
11514 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11515 Fremove_text_properties (make_number (0), make_number (size),
11516 props, f->desired_tool_bar_string);
11517 }
11518
11519 /* Put a `display' property on the string for the images to display,
11520 put a `menu_item' property on tool-bar items with a value that
11521 is the index of the item in F's tool-bar item vector. */
11522 for (i = 0; i < f->n_tool_bar_items; ++i)
11523 {
11524 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11525
11526 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11527 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11528 int hmargin, vmargin, relief, idx, end;
11529
11530 /* If image is a vector, choose the image according to the
11531 button state. */
11532 image = PROP (TOOL_BAR_ITEM_IMAGES);
11533 if (VECTORP (image))
11534 {
11535 if (enabled_p)
11536 idx = (selected_p
11537 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11538 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11539 else
11540 idx = (selected_p
11541 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11542 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11543
11544 eassert (ASIZE (image) >= idx);
11545 image = AREF (image, idx);
11546 }
11547 else
11548 idx = -1;
11549
11550 /* Ignore invalid image specifications. */
11551 if (!valid_image_p (image))
11552 continue;
11553
11554 /* Display the tool-bar button pressed, or depressed. */
11555 plist = Fcopy_sequence (XCDR (image));
11556
11557 /* Compute margin and relief to draw. */
11558 relief = (tool_bar_button_relief >= 0
11559 ? tool_bar_button_relief
11560 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11561 hmargin = vmargin = relief;
11562
11563 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11564 INT_MAX - max (hmargin, vmargin)))
11565 {
11566 hmargin += XFASTINT (Vtool_bar_button_margin);
11567 vmargin += XFASTINT (Vtool_bar_button_margin);
11568 }
11569 else if (CONSP (Vtool_bar_button_margin))
11570 {
11571 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11572 INT_MAX - hmargin))
11573 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11574
11575 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11576 INT_MAX - vmargin))
11577 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11578 }
11579
11580 if (auto_raise_tool_bar_buttons_p)
11581 {
11582 /* Add a `:relief' property to the image spec if the item is
11583 selected. */
11584 if (selected_p)
11585 {
11586 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11587 hmargin -= relief;
11588 vmargin -= relief;
11589 }
11590 }
11591 else
11592 {
11593 /* If image is selected, display it pressed, i.e. with a
11594 negative relief. If it's not selected, display it with a
11595 raised relief. */
11596 plist = Fplist_put (plist, QCrelief,
11597 (selected_p
11598 ? make_number (-relief)
11599 : make_number (relief)));
11600 hmargin -= relief;
11601 vmargin -= relief;
11602 }
11603
11604 /* Put a margin around the image. */
11605 if (hmargin || vmargin)
11606 {
11607 if (hmargin == vmargin)
11608 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11609 else
11610 plist = Fplist_put (plist, QCmargin,
11611 Fcons (make_number (hmargin),
11612 make_number (vmargin)));
11613 }
11614
11615 /* If button is not enabled, and we don't have special images
11616 for the disabled state, make the image appear disabled by
11617 applying an appropriate algorithm to it. */
11618 if (!enabled_p && idx < 0)
11619 plist = Fplist_put (plist, QCconversion, Qdisabled);
11620
11621 /* Put a `display' text property on the string for the image to
11622 display. Put a `menu-item' property on the string that gives
11623 the start of this item's properties in the tool-bar items
11624 vector. */
11625 image = Fcons (Qimage, plist);
11626 props = list4 (Qdisplay, image,
11627 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11628
11629 /* Let the last image hide all remaining spaces in the tool bar
11630 string. The string can be longer than needed when we reuse a
11631 previous string. */
11632 if (i + 1 == f->n_tool_bar_items)
11633 end = SCHARS (f->desired_tool_bar_string);
11634 else
11635 end = i + 1;
11636 Fadd_text_properties (make_number (i), make_number (end),
11637 props, f->desired_tool_bar_string);
11638 #undef PROP
11639 }
11640
11641 UNGCPRO;
11642 }
11643
11644
11645 /* Display one line of the tool-bar of frame IT->f.
11646
11647 HEIGHT specifies the desired height of the tool-bar line.
11648 If the actual height of the glyph row is less than HEIGHT, the
11649 row's height is increased to HEIGHT, and the icons are centered
11650 vertically in the new height.
11651
11652 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11653 count a final empty row in case the tool-bar width exactly matches
11654 the window width.
11655 */
11656
11657 static void
11658 display_tool_bar_line (struct it *it, int height)
11659 {
11660 struct glyph_row *row = it->glyph_row;
11661 int max_x = it->last_visible_x;
11662 struct glyph *last;
11663
11664 prepare_desired_row (row);
11665 row->y = it->current_y;
11666
11667 /* Note that this isn't made use of if the face hasn't a box,
11668 so there's no need to check the face here. */
11669 it->start_of_box_run_p = 1;
11670
11671 while (it->current_x < max_x)
11672 {
11673 int x, n_glyphs_before, i, nglyphs;
11674 struct it it_before;
11675
11676 /* Get the next display element. */
11677 if (!get_next_display_element (it))
11678 {
11679 /* Don't count empty row if we are counting needed tool-bar lines. */
11680 if (height < 0 && !it->hpos)
11681 return;
11682 break;
11683 }
11684
11685 /* Produce glyphs. */
11686 n_glyphs_before = row->used[TEXT_AREA];
11687 it_before = *it;
11688
11689 PRODUCE_GLYPHS (it);
11690
11691 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11692 i = 0;
11693 x = it_before.current_x;
11694 while (i < nglyphs)
11695 {
11696 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11697
11698 if (x + glyph->pixel_width > max_x)
11699 {
11700 /* Glyph doesn't fit on line. Backtrack. */
11701 row->used[TEXT_AREA] = n_glyphs_before;
11702 *it = it_before;
11703 /* If this is the only glyph on this line, it will never fit on the
11704 tool-bar, so skip it. But ensure there is at least one glyph,
11705 so we don't accidentally disable the tool-bar. */
11706 if (n_glyphs_before == 0
11707 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11708 break;
11709 goto out;
11710 }
11711
11712 ++it->hpos;
11713 x += glyph->pixel_width;
11714 ++i;
11715 }
11716
11717 /* Stop at line end. */
11718 if (ITERATOR_AT_END_OF_LINE_P (it))
11719 break;
11720
11721 set_iterator_to_next (it, 1);
11722 }
11723
11724 out:;
11725
11726 row->displays_text_p = row->used[TEXT_AREA] != 0;
11727
11728 /* Use default face for the border below the tool bar.
11729
11730 FIXME: When auto-resize-tool-bars is grow-only, there is
11731 no additional border below the possibly empty tool-bar lines.
11732 So to make the extra empty lines look "normal", we have to
11733 use the tool-bar face for the border too. */
11734 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11735 it->face_id = DEFAULT_FACE_ID;
11736
11737 extend_face_to_end_of_line (it);
11738 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11739 last->right_box_line_p = 1;
11740 if (last == row->glyphs[TEXT_AREA])
11741 last->left_box_line_p = 1;
11742
11743 /* Make line the desired height and center it vertically. */
11744 if ((height -= it->max_ascent + it->max_descent) > 0)
11745 {
11746 /* Don't add more than one line height. */
11747 height %= FRAME_LINE_HEIGHT (it->f);
11748 it->max_ascent += height / 2;
11749 it->max_descent += (height + 1) / 2;
11750 }
11751
11752 compute_line_metrics (it);
11753
11754 /* If line is empty, make it occupy the rest of the tool-bar. */
11755 if (!row->displays_text_p)
11756 {
11757 row->height = row->phys_height = it->last_visible_y - row->y;
11758 row->visible_height = row->height;
11759 row->ascent = row->phys_ascent = 0;
11760 row->extra_line_spacing = 0;
11761 }
11762
11763 row->full_width_p = 1;
11764 row->continued_p = 0;
11765 row->truncated_on_left_p = 0;
11766 row->truncated_on_right_p = 0;
11767
11768 it->current_x = it->hpos = 0;
11769 it->current_y += row->height;
11770 ++it->vpos;
11771 ++it->glyph_row;
11772 }
11773
11774
11775 /* Max tool-bar height. */
11776
11777 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11778 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11779
11780 /* Value is the number of screen lines needed to make all tool-bar
11781 items of frame F visible. The number of actual rows needed is
11782 returned in *N_ROWS if non-NULL. */
11783
11784 static int
11785 tool_bar_lines_needed (struct frame *f, int *n_rows)
11786 {
11787 struct window *w = XWINDOW (f->tool_bar_window);
11788 struct it it;
11789 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11790 the desired matrix, so use (unused) mode-line row as temporary row to
11791 avoid destroying the first tool-bar row. */
11792 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11793
11794 /* Initialize an iterator for iteration over
11795 F->desired_tool_bar_string in the tool-bar window of frame F. */
11796 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11797 it.first_visible_x = 0;
11798 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11799 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11800 it.paragraph_embedding = L2R;
11801
11802 while (!ITERATOR_AT_END_P (&it))
11803 {
11804 clear_glyph_row (temp_row);
11805 it.glyph_row = temp_row;
11806 display_tool_bar_line (&it, -1);
11807 }
11808 clear_glyph_row (temp_row);
11809
11810 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11811 if (n_rows)
11812 *n_rows = it.vpos > 0 ? it.vpos : -1;
11813
11814 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11815 }
11816
11817
11818 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11819 0, 1, 0,
11820 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11821 (Lisp_Object frame)
11822 {
11823 struct frame *f;
11824 struct window *w;
11825 int nlines = 0;
11826
11827 if (NILP (frame))
11828 frame = selected_frame;
11829 else
11830 CHECK_FRAME (frame);
11831 f = XFRAME (frame);
11832
11833 if (WINDOWP (f->tool_bar_window)
11834 && (w = XWINDOW (f->tool_bar_window),
11835 WINDOW_TOTAL_LINES (w) > 0))
11836 {
11837 update_tool_bar (f, 1);
11838 if (f->n_tool_bar_items)
11839 {
11840 build_desired_tool_bar_string (f);
11841 nlines = tool_bar_lines_needed (f, NULL);
11842 }
11843 }
11844
11845 return make_number (nlines);
11846 }
11847
11848
11849 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11850 height should be changed. */
11851
11852 static int
11853 redisplay_tool_bar (struct frame *f)
11854 {
11855 struct window *w;
11856 struct it it;
11857 struct glyph_row *row;
11858
11859 #if defined (USE_GTK) || defined (HAVE_NS)
11860 if (FRAME_EXTERNAL_TOOL_BAR (f))
11861 update_frame_tool_bar (f);
11862 return 0;
11863 #endif
11864
11865 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11866 do anything. This means you must start with tool-bar-lines
11867 non-zero to get the auto-sizing effect. Or in other words, you
11868 can turn off tool-bars by specifying tool-bar-lines zero. */
11869 if (!WINDOWP (f->tool_bar_window)
11870 || (w = XWINDOW (f->tool_bar_window),
11871 WINDOW_TOTAL_LINES (w) == 0))
11872 return 0;
11873
11874 /* Set up an iterator for the tool-bar window. */
11875 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11876 it.first_visible_x = 0;
11877 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11878 row = it.glyph_row;
11879
11880 /* Build a string that represents the contents of the tool-bar. */
11881 build_desired_tool_bar_string (f);
11882 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11883 /* FIXME: This should be controlled by a user option. But it
11884 doesn't make sense to have an R2L tool bar if the menu bar cannot
11885 be drawn also R2L, and making the menu bar R2L is tricky due
11886 toolkit-specific code that implements it. If an R2L tool bar is
11887 ever supported, display_tool_bar_line should also be augmented to
11888 call unproduce_glyphs like display_line and display_string
11889 do. */
11890 it.paragraph_embedding = L2R;
11891
11892 if (f->n_tool_bar_rows == 0)
11893 {
11894 int nlines;
11895
11896 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11897 nlines != WINDOW_TOTAL_LINES (w)))
11898 {
11899 Lisp_Object frame;
11900 int old_height = WINDOW_TOTAL_LINES (w);
11901
11902 XSETFRAME (frame, f);
11903 Fmodify_frame_parameters (frame,
11904 Fcons (Fcons (Qtool_bar_lines,
11905 make_number (nlines)),
11906 Qnil));
11907 if (WINDOW_TOTAL_LINES (w) != old_height)
11908 {
11909 clear_glyph_matrix (w->desired_matrix);
11910 fonts_changed_p = 1;
11911 return 1;
11912 }
11913 }
11914 }
11915
11916 /* Display as many lines as needed to display all tool-bar items. */
11917
11918 if (f->n_tool_bar_rows > 0)
11919 {
11920 int border, rows, height, extra;
11921
11922 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11923 border = XINT (Vtool_bar_border);
11924 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11925 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11926 else if (EQ (Vtool_bar_border, Qborder_width))
11927 border = f->border_width;
11928 else
11929 border = 0;
11930 if (border < 0)
11931 border = 0;
11932
11933 rows = f->n_tool_bar_rows;
11934 height = max (1, (it.last_visible_y - border) / rows);
11935 extra = it.last_visible_y - border - height * rows;
11936
11937 while (it.current_y < it.last_visible_y)
11938 {
11939 int h = 0;
11940 if (extra > 0 && rows-- > 0)
11941 {
11942 h = (extra + rows - 1) / rows;
11943 extra -= h;
11944 }
11945 display_tool_bar_line (&it, height + h);
11946 }
11947 }
11948 else
11949 {
11950 while (it.current_y < it.last_visible_y)
11951 display_tool_bar_line (&it, 0);
11952 }
11953
11954 /* It doesn't make much sense to try scrolling in the tool-bar
11955 window, so don't do it. */
11956 w->desired_matrix->no_scrolling_p = 1;
11957 w->must_be_updated_p = 1;
11958
11959 if (!NILP (Vauto_resize_tool_bars))
11960 {
11961 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11962 int change_height_p = 0;
11963
11964 /* If we couldn't display everything, change the tool-bar's
11965 height if there is room for more. */
11966 if (IT_STRING_CHARPOS (it) < it.end_charpos
11967 && it.current_y < max_tool_bar_height)
11968 change_height_p = 1;
11969
11970 row = it.glyph_row - 1;
11971
11972 /* If there are blank lines at the end, except for a partially
11973 visible blank line at the end that is smaller than
11974 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11975 if (!row->displays_text_p
11976 && row->height >= FRAME_LINE_HEIGHT (f))
11977 change_height_p = 1;
11978
11979 /* If row displays tool-bar items, but is partially visible,
11980 change the tool-bar's height. */
11981 if (row->displays_text_p
11982 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11983 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11984 change_height_p = 1;
11985
11986 /* Resize windows as needed by changing the `tool-bar-lines'
11987 frame parameter. */
11988 if (change_height_p)
11989 {
11990 Lisp_Object frame;
11991 int old_height = WINDOW_TOTAL_LINES (w);
11992 int nrows;
11993 int nlines = tool_bar_lines_needed (f, &nrows);
11994
11995 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11996 && !f->minimize_tool_bar_window_p)
11997 ? (nlines > old_height)
11998 : (nlines != old_height));
11999 f->minimize_tool_bar_window_p = 0;
12000
12001 if (change_height_p)
12002 {
12003 XSETFRAME (frame, f);
12004 Fmodify_frame_parameters (frame,
12005 Fcons (Fcons (Qtool_bar_lines,
12006 make_number (nlines)),
12007 Qnil));
12008 if (WINDOW_TOTAL_LINES (w) != old_height)
12009 {
12010 clear_glyph_matrix (w->desired_matrix);
12011 f->n_tool_bar_rows = nrows;
12012 fonts_changed_p = 1;
12013 return 1;
12014 }
12015 }
12016 }
12017 }
12018
12019 f->minimize_tool_bar_window_p = 0;
12020 return 0;
12021 }
12022
12023
12024 /* Get information about the tool-bar item which is displayed in GLYPH
12025 on frame F. Return in *PROP_IDX the index where tool-bar item
12026 properties start in F->tool_bar_items. Value is zero if
12027 GLYPH doesn't display a tool-bar item. */
12028
12029 static int
12030 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12031 {
12032 Lisp_Object prop;
12033 int success_p;
12034 int charpos;
12035
12036 /* This function can be called asynchronously, which means we must
12037 exclude any possibility that Fget_text_property signals an
12038 error. */
12039 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12040 charpos = max (0, charpos);
12041
12042 /* Get the text property `menu-item' at pos. The value of that
12043 property is the start index of this item's properties in
12044 F->tool_bar_items. */
12045 prop = Fget_text_property (make_number (charpos),
12046 Qmenu_item, f->current_tool_bar_string);
12047 if (INTEGERP (prop))
12048 {
12049 *prop_idx = XINT (prop);
12050 success_p = 1;
12051 }
12052 else
12053 success_p = 0;
12054
12055 return success_p;
12056 }
12057
12058 \f
12059 /* Get information about the tool-bar item at position X/Y on frame F.
12060 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12061 the current matrix of the tool-bar window of F, or NULL if not
12062 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12063 item in F->tool_bar_items. Value is
12064
12065 -1 if X/Y is not on a tool-bar item
12066 0 if X/Y is on the same item that was highlighted before.
12067 1 otherwise. */
12068
12069 static int
12070 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12071 int *hpos, int *vpos, int *prop_idx)
12072 {
12073 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12074 struct window *w = XWINDOW (f->tool_bar_window);
12075 int area;
12076
12077 /* Find the glyph under X/Y. */
12078 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12079 if (*glyph == NULL)
12080 return -1;
12081
12082 /* Get the start of this tool-bar item's properties in
12083 f->tool_bar_items. */
12084 if (!tool_bar_item_info (f, *glyph, prop_idx))
12085 return -1;
12086
12087 /* Is mouse on the highlighted item? */
12088 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12089 && *vpos >= hlinfo->mouse_face_beg_row
12090 && *vpos <= hlinfo->mouse_face_end_row
12091 && (*vpos > hlinfo->mouse_face_beg_row
12092 || *hpos >= hlinfo->mouse_face_beg_col)
12093 && (*vpos < hlinfo->mouse_face_end_row
12094 || *hpos < hlinfo->mouse_face_end_col
12095 || hlinfo->mouse_face_past_end))
12096 return 0;
12097
12098 return 1;
12099 }
12100
12101
12102 /* EXPORT:
12103 Handle mouse button event on the tool-bar of frame F, at
12104 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12105 0 for button release. MODIFIERS is event modifiers for button
12106 release. */
12107
12108 void
12109 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12110 int modifiers)
12111 {
12112 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12113 struct window *w = XWINDOW (f->tool_bar_window);
12114 int hpos, vpos, prop_idx;
12115 struct glyph *glyph;
12116 Lisp_Object enabled_p;
12117
12118 /* If not on the highlighted tool-bar item, return. */
12119 frame_to_window_pixel_xy (w, &x, &y);
12120 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12121 return;
12122
12123 /* If item is disabled, do nothing. */
12124 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12125 if (NILP (enabled_p))
12126 return;
12127
12128 if (down_p)
12129 {
12130 /* Show item in pressed state. */
12131 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12132 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12133 last_tool_bar_item = prop_idx;
12134 }
12135 else
12136 {
12137 Lisp_Object key, frame;
12138 struct input_event event;
12139 EVENT_INIT (event);
12140
12141 /* Show item in released state. */
12142 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12143 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12144
12145 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12146
12147 XSETFRAME (frame, f);
12148 event.kind = TOOL_BAR_EVENT;
12149 event.frame_or_window = frame;
12150 event.arg = frame;
12151 kbd_buffer_store_event (&event);
12152
12153 event.kind = TOOL_BAR_EVENT;
12154 event.frame_or_window = frame;
12155 event.arg = key;
12156 event.modifiers = modifiers;
12157 kbd_buffer_store_event (&event);
12158 last_tool_bar_item = -1;
12159 }
12160 }
12161
12162
12163 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12164 tool-bar window-relative coordinates X/Y. Called from
12165 note_mouse_highlight. */
12166
12167 static void
12168 note_tool_bar_highlight (struct frame *f, int x, int y)
12169 {
12170 Lisp_Object window = f->tool_bar_window;
12171 struct window *w = XWINDOW (window);
12172 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12173 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12174 int hpos, vpos;
12175 struct glyph *glyph;
12176 struct glyph_row *row;
12177 int i;
12178 Lisp_Object enabled_p;
12179 int prop_idx;
12180 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12181 int mouse_down_p, rc;
12182
12183 /* Function note_mouse_highlight is called with negative X/Y
12184 values when mouse moves outside of the frame. */
12185 if (x <= 0 || y <= 0)
12186 {
12187 clear_mouse_face (hlinfo);
12188 return;
12189 }
12190
12191 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12192 if (rc < 0)
12193 {
12194 /* Not on tool-bar item. */
12195 clear_mouse_face (hlinfo);
12196 return;
12197 }
12198 else if (rc == 0)
12199 /* On same tool-bar item as before. */
12200 goto set_help_echo;
12201
12202 clear_mouse_face (hlinfo);
12203
12204 /* Mouse is down, but on different tool-bar item? */
12205 mouse_down_p = (dpyinfo->grabbed
12206 && f == last_mouse_frame
12207 && FRAME_LIVE_P (f));
12208 if (mouse_down_p
12209 && last_tool_bar_item != prop_idx)
12210 return;
12211
12212 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12213 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12214
12215 /* If tool-bar item is not enabled, don't highlight it. */
12216 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12217 if (!NILP (enabled_p))
12218 {
12219 /* Compute the x-position of the glyph. In front and past the
12220 image is a space. We include this in the highlighted area. */
12221 row = MATRIX_ROW (w->current_matrix, vpos);
12222 for (i = x = 0; i < hpos; ++i)
12223 x += row->glyphs[TEXT_AREA][i].pixel_width;
12224
12225 /* Record this as the current active region. */
12226 hlinfo->mouse_face_beg_col = hpos;
12227 hlinfo->mouse_face_beg_row = vpos;
12228 hlinfo->mouse_face_beg_x = x;
12229 hlinfo->mouse_face_beg_y = row->y;
12230 hlinfo->mouse_face_past_end = 0;
12231
12232 hlinfo->mouse_face_end_col = hpos + 1;
12233 hlinfo->mouse_face_end_row = vpos;
12234 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12235 hlinfo->mouse_face_end_y = row->y;
12236 hlinfo->mouse_face_window = window;
12237 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12238
12239 /* Display it as active. */
12240 show_mouse_face (hlinfo, draw);
12241 hlinfo->mouse_face_image_state = draw;
12242 }
12243
12244 set_help_echo:
12245
12246 /* Set help_echo_string to a help string to display for this tool-bar item.
12247 XTread_socket does the rest. */
12248 help_echo_object = help_echo_window = Qnil;
12249 help_echo_pos = -1;
12250 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12251 if (NILP (help_echo_string))
12252 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12253 }
12254
12255 #endif /* HAVE_WINDOW_SYSTEM */
12256
12257
12258 \f
12259 /************************************************************************
12260 Horizontal scrolling
12261 ************************************************************************/
12262
12263 static int hscroll_window_tree (Lisp_Object);
12264 static int hscroll_windows (Lisp_Object);
12265
12266 /* For all leaf windows in the window tree rooted at WINDOW, set their
12267 hscroll value so that PT is (i) visible in the window, and (ii) so
12268 that it is not within a certain margin at the window's left and
12269 right border. Value is non-zero if any window's hscroll has been
12270 changed. */
12271
12272 static int
12273 hscroll_window_tree (Lisp_Object window)
12274 {
12275 int hscrolled_p = 0;
12276 int hscroll_relative_p = FLOATP (Vhscroll_step);
12277 int hscroll_step_abs = 0;
12278 double hscroll_step_rel = 0;
12279
12280 if (hscroll_relative_p)
12281 {
12282 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12283 if (hscroll_step_rel < 0)
12284 {
12285 hscroll_relative_p = 0;
12286 hscroll_step_abs = 0;
12287 }
12288 }
12289 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12290 {
12291 hscroll_step_abs = XINT (Vhscroll_step);
12292 if (hscroll_step_abs < 0)
12293 hscroll_step_abs = 0;
12294 }
12295 else
12296 hscroll_step_abs = 0;
12297
12298 while (WINDOWP (window))
12299 {
12300 struct window *w = XWINDOW (window);
12301
12302 if (WINDOWP (w->hchild))
12303 hscrolled_p |= hscroll_window_tree (w->hchild);
12304 else if (WINDOWP (w->vchild))
12305 hscrolled_p |= hscroll_window_tree (w->vchild);
12306 else if (w->cursor.vpos >= 0)
12307 {
12308 int h_margin;
12309 int text_area_width;
12310 struct glyph_row *current_cursor_row
12311 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12312 struct glyph_row *desired_cursor_row
12313 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12314 struct glyph_row *cursor_row
12315 = (desired_cursor_row->enabled_p
12316 ? desired_cursor_row
12317 : current_cursor_row);
12318 int row_r2l_p = cursor_row->reversed_p;
12319
12320 text_area_width = window_box_width (w, TEXT_AREA);
12321
12322 /* Scroll when cursor is inside this scroll margin. */
12323 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12324
12325 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12326 /* For left-to-right rows, hscroll when cursor is either
12327 (i) inside the right hscroll margin, or (ii) if it is
12328 inside the left margin and the window is already
12329 hscrolled. */
12330 && ((!row_r2l_p
12331 && ((w->hscroll
12332 && w->cursor.x <= h_margin)
12333 || (cursor_row->enabled_p
12334 && cursor_row->truncated_on_right_p
12335 && (w->cursor.x >= text_area_width - h_margin))))
12336 /* For right-to-left rows, the logic is similar,
12337 except that rules for scrolling to left and right
12338 are reversed. E.g., if cursor.x <= h_margin, we
12339 need to hscroll "to the right" unconditionally,
12340 and that will scroll the screen to the left so as
12341 to reveal the next portion of the row. */
12342 || (row_r2l_p
12343 && ((cursor_row->enabled_p
12344 /* FIXME: It is confusing to set the
12345 truncated_on_right_p flag when R2L rows
12346 are actually truncated on the left. */
12347 && cursor_row->truncated_on_right_p
12348 && w->cursor.x <= h_margin)
12349 || (w->hscroll
12350 && (w->cursor.x >= text_area_width - h_margin))))))
12351 {
12352 struct it it;
12353 ptrdiff_t hscroll;
12354 struct buffer *saved_current_buffer;
12355 ptrdiff_t pt;
12356 int wanted_x;
12357
12358 /* Find point in a display of infinite width. */
12359 saved_current_buffer = current_buffer;
12360 current_buffer = XBUFFER (w->buffer);
12361
12362 if (w == XWINDOW (selected_window))
12363 pt = PT;
12364 else
12365 {
12366 pt = marker_position (w->pointm);
12367 pt = max (BEGV, pt);
12368 pt = min (ZV, pt);
12369 }
12370
12371 /* Move iterator to pt starting at cursor_row->start in
12372 a line with infinite width. */
12373 init_to_row_start (&it, w, cursor_row);
12374 it.last_visible_x = INFINITY;
12375 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12376 current_buffer = saved_current_buffer;
12377
12378 /* Position cursor in window. */
12379 if (!hscroll_relative_p && hscroll_step_abs == 0)
12380 hscroll = max (0, (it.current_x
12381 - (ITERATOR_AT_END_OF_LINE_P (&it)
12382 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12383 : (text_area_width / 2))))
12384 / FRAME_COLUMN_WIDTH (it.f);
12385 else if ((!row_r2l_p
12386 && w->cursor.x >= text_area_width - h_margin)
12387 || (row_r2l_p && w->cursor.x <= h_margin))
12388 {
12389 if (hscroll_relative_p)
12390 wanted_x = text_area_width * (1 - hscroll_step_rel)
12391 - h_margin;
12392 else
12393 wanted_x = text_area_width
12394 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12395 - h_margin;
12396 hscroll
12397 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12398 }
12399 else
12400 {
12401 if (hscroll_relative_p)
12402 wanted_x = text_area_width * hscroll_step_rel
12403 + h_margin;
12404 else
12405 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12406 + h_margin;
12407 hscroll
12408 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12409 }
12410 hscroll = max (hscroll, w->min_hscroll);
12411
12412 /* Don't prevent redisplay optimizations if hscroll
12413 hasn't changed, as it will unnecessarily slow down
12414 redisplay. */
12415 if (w->hscroll != hscroll)
12416 {
12417 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12418 w->hscroll = hscroll;
12419 hscrolled_p = 1;
12420 }
12421 }
12422 }
12423
12424 window = w->next;
12425 }
12426
12427 /* Value is non-zero if hscroll of any leaf window has been changed. */
12428 return hscrolled_p;
12429 }
12430
12431
12432 /* Set hscroll so that cursor is visible and not inside horizontal
12433 scroll margins for all windows in the tree rooted at WINDOW. See
12434 also hscroll_window_tree above. Value is non-zero if any window's
12435 hscroll has been changed. If it has, desired matrices on the frame
12436 of WINDOW are cleared. */
12437
12438 static int
12439 hscroll_windows (Lisp_Object window)
12440 {
12441 int hscrolled_p = hscroll_window_tree (window);
12442 if (hscrolled_p)
12443 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12444 return hscrolled_p;
12445 }
12446
12447
12448 \f
12449 /************************************************************************
12450 Redisplay
12451 ************************************************************************/
12452
12453 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12454 to a non-zero value. This is sometimes handy to have in a debugger
12455 session. */
12456
12457 #ifdef GLYPH_DEBUG
12458
12459 /* First and last unchanged row for try_window_id. */
12460
12461 static int debug_first_unchanged_at_end_vpos;
12462 static int debug_last_unchanged_at_beg_vpos;
12463
12464 /* Delta vpos and y. */
12465
12466 static int debug_dvpos, debug_dy;
12467
12468 /* Delta in characters and bytes for try_window_id. */
12469
12470 static ptrdiff_t debug_delta, debug_delta_bytes;
12471
12472 /* Values of window_end_pos and window_end_vpos at the end of
12473 try_window_id. */
12474
12475 static ptrdiff_t debug_end_vpos;
12476
12477 /* Append a string to W->desired_matrix->method. FMT is a printf
12478 format string. If trace_redisplay_p is non-zero also printf the
12479 resulting string to stderr. */
12480
12481 static void debug_method_add (struct window *, char const *, ...)
12482 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12483
12484 static void
12485 debug_method_add (struct window *w, char const *fmt, ...)
12486 {
12487 char buffer[512];
12488 char *method = w->desired_matrix->method;
12489 int len = strlen (method);
12490 int size = sizeof w->desired_matrix->method;
12491 int remaining = size - len - 1;
12492 va_list ap;
12493
12494 va_start (ap, fmt);
12495 vsprintf (buffer, fmt, ap);
12496 va_end (ap);
12497 if (len && remaining)
12498 {
12499 method[len] = '|';
12500 --remaining, ++len;
12501 }
12502
12503 strncpy (method + len, buffer, remaining);
12504
12505 if (trace_redisplay_p)
12506 fprintf (stderr, "%p (%s): %s\n",
12507 w,
12508 ((BUFFERP (w->buffer)
12509 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12510 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12511 : "no buffer"),
12512 buffer);
12513 }
12514
12515 #endif /* GLYPH_DEBUG */
12516
12517
12518 /* Value is non-zero if all changes in window W, which displays
12519 current_buffer, are in the text between START and END. START is a
12520 buffer position, END is given as a distance from Z. Used in
12521 redisplay_internal for display optimization. */
12522
12523 static inline int
12524 text_outside_line_unchanged_p (struct window *w,
12525 ptrdiff_t start, ptrdiff_t end)
12526 {
12527 int unchanged_p = 1;
12528
12529 /* If text or overlays have changed, see where. */
12530 if (w->last_modified < MODIFF
12531 || w->last_overlay_modified < OVERLAY_MODIFF)
12532 {
12533 /* Gap in the line? */
12534 if (GPT < start || Z - GPT < end)
12535 unchanged_p = 0;
12536
12537 /* Changes start in front of the line, or end after it? */
12538 if (unchanged_p
12539 && (BEG_UNCHANGED < start - 1
12540 || END_UNCHANGED < end))
12541 unchanged_p = 0;
12542
12543 /* If selective display, can't optimize if changes start at the
12544 beginning of the line. */
12545 if (unchanged_p
12546 && INTEGERP (BVAR (current_buffer, selective_display))
12547 && XINT (BVAR (current_buffer, selective_display)) > 0
12548 && (BEG_UNCHANGED < start || GPT <= start))
12549 unchanged_p = 0;
12550
12551 /* If there are overlays at the start or end of the line, these
12552 may have overlay strings with newlines in them. A change at
12553 START, for instance, may actually concern the display of such
12554 overlay strings as well, and they are displayed on different
12555 lines. So, quickly rule out this case. (For the future, it
12556 might be desirable to implement something more telling than
12557 just BEG/END_UNCHANGED.) */
12558 if (unchanged_p)
12559 {
12560 if (BEG + BEG_UNCHANGED == start
12561 && overlay_touches_p (start))
12562 unchanged_p = 0;
12563 if (END_UNCHANGED == end
12564 && overlay_touches_p (Z - end))
12565 unchanged_p = 0;
12566 }
12567
12568 /* Under bidi reordering, adding or deleting a character in the
12569 beginning of a paragraph, before the first strong directional
12570 character, can change the base direction of the paragraph (unless
12571 the buffer specifies a fixed paragraph direction), which will
12572 require to redisplay the whole paragraph. It might be worthwhile
12573 to find the paragraph limits and widen the range of redisplayed
12574 lines to that, but for now just give up this optimization. */
12575 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12576 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12577 unchanged_p = 0;
12578 }
12579
12580 return unchanged_p;
12581 }
12582
12583
12584 /* Do a frame update, taking possible shortcuts into account. This is
12585 the main external entry point for redisplay.
12586
12587 If the last redisplay displayed an echo area message and that message
12588 is no longer requested, we clear the echo area or bring back the
12589 mini-buffer if that is in use. */
12590
12591 void
12592 redisplay (void)
12593 {
12594 redisplay_internal ();
12595 }
12596
12597
12598 static Lisp_Object
12599 overlay_arrow_string_or_property (Lisp_Object var)
12600 {
12601 Lisp_Object val;
12602
12603 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12604 return val;
12605
12606 return Voverlay_arrow_string;
12607 }
12608
12609 /* Return 1 if there are any overlay-arrows in current_buffer. */
12610 static int
12611 overlay_arrow_in_current_buffer_p (void)
12612 {
12613 Lisp_Object vlist;
12614
12615 for (vlist = Voverlay_arrow_variable_list;
12616 CONSP (vlist);
12617 vlist = XCDR (vlist))
12618 {
12619 Lisp_Object var = XCAR (vlist);
12620 Lisp_Object val;
12621
12622 if (!SYMBOLP (var))
12623 continue;
12624 val = find_symbol_value (var);
12625 if (MARKERP (val)
12626 && current_buffer == XMARKER (val)->buffer)
12627 return 1;
12628 }
12629 return 0;
12630 }
12631
12632
12633 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12634 has changed. */
12635
12636 static int
12637 overlay_arrows_changed_p (void)
12638 {
12639 Lisp_Object vlist;
12640
12641 for (vlist = Voverlay_arrow_variable_list;
12642 CONSP (vlist);
12643 vlist = XCDR (vlist))
12644 {
12645 Lisp_Object var = XCAR (vlist);
12646 Lisp_Object val, pstr;
12647
12648 if (!SYMBOLP (var))
12649 continue;
12650 val = find_symbol_value (var);
12651 if (!MARKERP (val))
12652 continue;
12653 if (! EQ (COERCE_MARKER (val),
12654 Fget (var, Qlast_arrow_position))
12655 || ! (pstr = overlay_arrow_string_or_property (var),
12656 EQ (pstr, Fget (var, Qlast_arrow_string))))
12657 return 1;
12658 }
12659 return 0;
12660 }
12661
12662 /* Mark overlay arrows to be updated on next redisplay. */
12663
12664 static void
12665 update_overlay_arrows (int up_to_date)
12666 {
12667 Lisp_Object vlist;
12668
12669 for (vlist = Voverlay_arrow_variable_list;
12670 CONSP (vlist);
12671 vlist = XCDR (vlist))
12672 {
12673 Lisp_Object var = XCAR (vlist);
12674
12675 if (!SYMBOLP (var))
12676 continue;
12677
12678 if (up_to_date > 0)
12679 {
12680 Lisp_Object val = find_symbol_value (var);
12681 Fput (var, Qlast_arrow_position,
12682 COERCE_MARKER (val));
12683 Fput (var, Qlast_arrow_string,
12684 overlay_arrow_string_or_property (var));
12685 }
12686 else if (up_to_date < 0
12687 || !NILP (Fget (var, Qlast_arrow_position)))
12688 {
12689 Fput (var, Qlast_arrow_position, Qt);
12690 Fput (var, Qlast_arrow_string, Qt);
12691 }
12692 }
12693 }
12694
12695
12696 /* Return overlay arrow string to display at row.
12697 Return integer (bitmap number) for arrow bitmap in left fringe.
12698 Return nil if no overlay arrow. */
12699
12700 static Lisp_Object
12701 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12702 {
12703 Lisp_Object vlist;
12704
12705 for (vlist = Voverlay_arrow_variable_list;
12706 CONSP (vlist);
12707 vlist = XCDR (vlist))
12708 {
12709 Lisp_Object var = XCAR (vlist);
12710 Lisp_Object val;
12711
12712 if (!SYMBOLP (var))
12713 continue;
12714
12715 val = find_symbol_value (var);
12716
12717 if (MARKERP (val)
12718 && current_buffer == XMARKER (val)->buffer
12719 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12720 {
12721 if (FRAME_WINDOW_P (it->f)
12722 /* FIXME: if ROW->reversed_p is set, this should test
12723 the right fringe, not the left one. */
12724 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12725 {
12726 #ifdef HAVE_WINDOW_SYSTEM
12727 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12728 {
12729 int fringe_bitmap;
12730 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12731 return make_number (fringe_bitmap);
12732 }
12733 #endif
12734 return make_number (-1); /* Use default arrow bitmap */
12735 }
12736 return overlay_arrow_string_or_property (var);
12737 }
12738 }
12739
12740 return Qnil;
12741 }
12742
12743 /* Return 1 if point moved out of or into a composition. Otherwise
12744 return 0. PREV_BUF and PREV_PT are the last point buffer and
12745 position. BUF and PT are the current point buffer and position. */
12746
12747 static int
12748 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12749 struct buffer *buf, ptrdiff_t pt)
12750 {
12751 ptrdiff_t start, end;
12752 Lisp_Object prop;
12753 Lisp_Object buffer;
12754
12755 XSETBUFFER (buffer, buf);
12756 /* Check a composition at the last point if point moved within the
12757 same buffer. */
12758 if (prev_buf == buf)
12759 {
12760 if (prev_pt == pt)
12761 /* Point didn't move. */
12762 return 0;
12763
12764 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12765 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12766 && COMPOSITION_VALID_P (start, end, prop)
12767 && start < prev_pt && end > prev_pt)
12768 /* The last point was within the composition. Return 1 iff
12769 point moved out of the composition. */
12770 return (pt <= start || pt >= end);
12771 }
12772
12773 /* Check a composition at the current point. */
12774 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12775 && find_composition (pt, -1, &start, &end, &prop, buffer)
12776 && COMPOSITION_VALID_P (start, end, prop)
12777 && start < pt && end > pt);
12778 }
12779
12780
12781 /* Reconsider the setting of B->clip_changed which is displayed
12782 in window W. */
12783
12784 static inline void
12785 reconsider_clip_changes (struct window *w, struct buffer *b)
12786 {
12787 if (b->clip_changed
12788 && !NILP (w->window_end_valid)
12789 && w->current_matrix->buffer == b
12790 && w->current_matrix->zv == BUF_ZV (b)
12791 && w->current_matrix->begv == BUF_BEGV (b))
12792 b->clip_changed = 0;
12793
12794 /* If display wasn't paused, and W is not a tool bar window, see if
12795 point has been moved into or out of a composition. In that case,
12796 we set b->clip_changed to 1 to force updating the screen. If
12797 b->clip_changed has already been set to 1, we can skip this
12798 check. */
12799 if (!b->clip_changed
12800 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12801 {
12802 ptrdiff_t pt;
12803
12804 if (w == XWINDOW (selected_window))
12805 pt = PT;
12806 else
12807 pt = marker_position (w->pointm);
12808
12809 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12810 || pt != w->last_point)
12811 && check_point_in_composition (w->current_matrix->buffer,
12812 w->last_point,
12813 XBUFFER (w->buffer), pt))
12814 b->clip_changed = 1;
12815 }
12816 }
12817 \f
12818
12819 /* Select FRAME to forward the values of frame-local variables into C
12820 variables so that the redisplay routines can access those values
12821 directly. */
12822
12823 static void
12824 select_frame_for_redisplay (Lisp_Object frame)
12825 {
12826 Lisp_Object tail, tem;
12827 Lisp_Object old = selected_frame;
12828 struct Lisp_Symbol *sym;
12829
12830 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12831
12832 selected_frame = frame;
12833
12834 do {
12835 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12836 if (CONSP (XCAR (tail))
12837 && (tem = XCAR (XCAR (tail)),
12838 SYMBOLP (tem))
12839 && (sym = indirect_variable (XSYMBOL (tem)),
12840 sym->redirect == SYMBOL_LOCALIZED)
12841 && sym->val.blv->frame_local)
12842 /* Use find_symbol_value rather than Fsymbol_value
12843 to avoid an error if it is void. */
12844 find_symbol_value (tem);
12845 } while (!EQ (frame, old) && (frame = old, 1));
12846 }
12847
12848
12849 #define STOP_POLLING \
12850 do { if (! polling_stopped_here) stop_polling (); \
12851 polling_stopped_here = 1; } while (0)
12852
12853 #define RESUME_POLLING \
12854 do { if (polling_stopped_here) start_polling (); \
12855 polling_stopped_here = 0; } while (0)
12856
12857
12858 /* Perhaps in the future avoid recentering windows if it
12859 is not necessary; currently that causes some problems. */
12860
12861 static void
12862 redisplay_internal (void)
12863 {
12864 struct window *w = XWINDOW (selected_window);
12865 struct window *sw;
12866 struct frame *fr;
12867 int pending;
12868 int must_finish = 0;
12869 struct text_pos tlbufpos, tlendpos;
12870 int number_of_visible_frames;
12871 ptrdiff_t count, count1;
12872 struct frame *sf;
12873 int polling_stopped_here = 0;
12874 Lisp_Object old_frame = selected_frame;
12875
12876 /* Non-zero means redisplay has to consider all windows on all
12877 frames. Zero means, only selected_window is considered. */
12878 int consider_all_windows_p;
12879
12880 /* Non-zero means redisplay has to redisplay the miniwindow */
12881 int update_miniwindow_p = 0;
12882
12883 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12884
12885 /* No redisplay if running in batch mode or frame is not yet fully
12886 initialized, or redisplay is explicitly turned off by setting
12887 Vinhibit_redisplay. */
12888 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12889 || !NILP (Vinhibit_redisplay))
12890 return;
12891
12892 /* Don't examine these until after testing Vinhibit_redisplay.
12893 When Emacs is shutting down, perhaps because its connection to
12894 X has dropped, we should not look at them at all. */
12895 fr = XFRAME (w->frame);
12896 sf = SELECTED_FRAME ();
12897
12898 if (!fr->glyphs_initialized_p)
12899 return;
12900
12901 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12902 if (popup_activated ())
12903 return;
12904 #endif
12905
12906 /* I don't think this happens but let's be paranoid. */
12907 if (redisplaying_p)
12908 return;
12909
12910 /* Record a function that resets redisplaying_p to its old value
12911 when we leave this function. */
12912 count = SPECPDL_INDEX ();
12913 record_unwind_protect (unwind_redisplay,
12914 Fcons (make_number (redisplaying_p), selected_frame));
12915 ++redisplaying_p;
12916 specbind (Qinhibit_free_realized_faces, Qnil);
12917
12918 {
12919 Lisp_Object tail, frame;
12920
12921 FOR_EACH_FRAME (tail, frame)
12922 {
12923 struct frame *f = XFRAME (frame);
12924 f->already_hscrolled_p = 0;
12925 }
12926 }
12927
12928 retry:
12929 /* Remember the currently selected window. */
12930 sw = w;
12931
12932 if (!EQ (old_frame, selected_frame)
12933 && FRAME_LIVE_P (XFRAME (old_frame)))
12934 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12935 selected_frame and selected_window to be temporarily out-of-sync so
12936 when we come back here via `goto retry', we need to resync because we
12937 may need to run Elisp code (via prepare_menu_bars). */
12938 select_frame_for_redisplay (old_frame);
12939
12940 pending = 0;
12941 reconsider_clip_changes (w, current_buffer);
12942 last_escape_glyph_frame = NULL;
12943 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12944 last_glyphless_glyph_frame = NULL;
12945 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12946
12947 /* If new fonts have been loaded that make a glyph matrix adjustment
12948 necessary, do it. */
12949 if (fonts_changed_p)
12950 {
12951 adjust_glyphs (NULL);
12952 ++windows_or_buffers_changed;
12953 fonts_changed_p = 0;
12954 }
12955
12956 /* If face_change_count is non-zero, init_iterator will free all
12957 realized faces, which includes the faces referenced from current
12958 matrices. So, we can't reuse current matrices in this case. */
12959 if (face_change_count)
12960 ++windows_or_buffers_changed;
12961
12962 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12963 && FRAME_TTY (sf)->previous_frame != sf)
12964 {
12965 /* Since frames on a single ASCII terminal share the same
12966 display area, displaying a different frame means redisplay
12967 the whole thing. */
12968 windows_or_buffers_changed++;
12969 SET_FRAME_GARBAGED (sf);
12970 #ifndef DOS_NT
12971 set_tty_color_mode (FRAME_TTY (sf), sf);
12972 #endif
12973 FRAME_TTY (sf)->previous_frame = sf;
12974 }
12975
12976 /* Set the visible flags for all frames. Do this before checking
12977 for resized or garbaged frames; they want to know if their frames
12978 are visible. See the comment in frame.h for
12979 FRAME_SAMPLE_VISIBILITY. */
12980 {
12981 Lisp_Object tail, frame;
12982
12983 number_of_visible_frames = 0;
12984
12985 FOR_EACH_FRAME (tail, frame)
12986 {
12987 struct frame *f = XFRAME (frame);
12988
12989 FRAME_SAMPLE_VISIBILITY (f);
12990 if (FRAME_VISIBLE_P (f))
12991 ++number_of_visible_frames;
12992 clear_desired_matrices (f);
12993 }
12994 }
12995
12996 /* Notice any pending interrupt request to change frame size. */
12997 do_pending_window_change (1);
12998
12999 /* do_pending_window_change could change the selected_window due to
13000 frame resizing which makes the selected window too small. */
13001 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13002 {
13003 sw = w;
13004 reconsider_clip_changes (w, current_buffer);
13005 }
13006
13007 /* Clear frames marked as garbaged. */
13008 if (frame_garbaged)
13009 clear_garbaged_frames ();
13010
13011 /* Build menubar and tool-bar items. */
13012 if (NILP (Vmemory_full))
13013 prepare_menu_bars ();
13014
13015 if (windows_or_buffers_changed)
13016 update_mode_lines++;
13017
13018 /* Detect case that we need to write or remove a star in the mode line. */
13019 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13020 {
13021 w->update_mode_line = 1;
13022 if (buffer_shared > 1)
13023 update_mode_lines++;
13024 }
13025
13026 /* Avoid invocation of point motion hooks by `current_column' below. */
13027 count1 = SPECPDL_INDEX ();
13028 specbind (Qinhibit_point_motion_hooks, Qt);
13029
13030 /* If %c is in the mode line, update it if needed. */
13031 if (!NILP (w->column_number_displayed)
13032 /* This alternative quickly identifies a common case
13033 where no change is needed. */
13034 && !(PT == w->last_point
13035 && w->last_modified >= MODIFF
13036 && w->last_overlay_modified >= OVERLAY_MODIFF)
13037 && (XFASTINT (w->column_number_displayed) != current_column ()))
13038 w->update_mode_line = 1;
13039
13040 unbind_to (count1, Qnil);
13041
13042 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13043
13044 /* The variable buffer_shared is set in redisplay_window and
13045 indicates that we redisplay a buffer in different windows. See
13046 there. */
13047 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13048 || cursor_type_changed);
13049
13050 /* If specs for an arrow have changed, do thorough redisplay
13051 to ensure we remove any arrow that should no longer exist. */
13052 if (overlay_arrows_changed_p ())
13053 consider_all_windows_p = windows_or_buffers_changed = 1;
13054
13055 /* Normally the message* functions will have already displayed and
13056 updated the echo area, but the frame may have been trashed, or
13057 the update may have been preempted, so display the echo area
13058 again here. Checking message_cleared_p captures the case that
13059 the echo area should be cleared. */
13060 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13061 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13062 || (message_cleared_p
13063 && minibuf_level == 0
13064 /* If the mini-window is currently selected, this means the
13065 echo-area doesn't show through. */
13066 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13067 {
13068 int window_height_changed_p = echo_area_display (0);
13069
13070 if (message_cleared_p)
13071 update_miniwindow_p = 1;
13072
13073 must_finish = 1;
13074
13075 /* If we don't display the current message, don't clear the
13076 message_cleared_p flag, because, if we did, we wouldn't clear
13077 the echo area in the next redisplay which doesn't preserve
13078 the echo area. */
13079 if (!display_last_displayed_message_p)
13080 message_cleared_p = 0;
13081
13082 if (fonts_changed_p)
13083 goto retry;
13084 else if (window_height_changed_p)
13085 {
13086 consider_all_windows_p = 1;
13087 ++update_mode_lines;
13088 ++windows_or_buffers_changed;
13089
13090 /* If window configuration was changed, frames may have been
13091 marked garbaged. Clear them or we will experience
13092 surprises wrt scrolling. */
13093 if (frame_garbaged)
13094 clear_garbaged_frames ();
13095 }
13096 }
13097 else if (EQ (selected_window, minibuf_window)
13098 && (current_buffer->clip_changed
13099 || w->last_modified < MODIFF
13100 || w->last_overlay_modified < OVERLAY_MODIFF)
13101 && resize_mini_window (w, 0))
13102 {
13103 /* Resized active mini-window to fit the size of what it is
13104 showing if its contents might have changed. */
13105 must_finish = 1;
13106 /* FIXME: this causes all frames to be updated, which seems unnecessary
13107 since only the current frame needs to be considered. This function needs
13108 to be rewritten with two variables, consider_all_windows and
13109 consider_all_frames. */
13110 consider_all_windows_p = 1;
13111 ++windows_or_buffers_changed;
13112 ++update_mode_lines;
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
13122 /* If showing the region, and mark has changed, we must redisplay
13123 the whole window. The assignment to this_line_start_pos prevents
13124 the optimization directly below this if-statement. */
13125 if (((!NILP (Vtransient_mark_mode)
13126 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13127 != !NILP (w->region_showing))
13128 || (!NILP (w->region_showing)
13129 && !EQ (w->region_showing,
13130 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13131 CHARPOS (this_line_start_pos) = 0;
13132
13133 /* Optimize the case that only the line containing the cursor in the
13134 selected window has changed. Variables starting with this_ are
13135 set in display_line and record information about the line
13136 containing the cursor. */
13137 tlbufpos = this_line_start_pos;
13138 tlendpos = this_line_end_pos;
13139 if (!consider_all_windows_p
13140 && CHARPOS (tlbufpos) > 0
13141 && !w->update_mode_line
13142 && !current_buffer->clip_changed
13143 && !current_buffer->prevent_redisplay_optimizations_p
13144 && FRAME_VISIBLE_P (XFRAME (w->frame))
13145 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13146 /* Make sure recorded data applies to current buffer, etc. */
13147 && this_line_buffer == current_buffer
13148 && current_buffer == XBUFFER (w->buffer)
13149 && !w->force_start
13150 && !w->optional_new_start
13151 /* Point must be on the line that we have info recorded about. */
13152 && PT >= CHARPOS (tlbufpos)
13153 && PT <= Z - CHARPOS (tlendpos)
13154 /* All text outside that line, including its final newline,
13155 must be unchanged. */
13156 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13157 CHARPOS (tlendpos)))
13158 {
13159 if (CHARPOS (tlbufpos) > BEGV
13160 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13161 && (CHARPOS (tlbufpos) == ZV
13162 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13163 /* Former continuation line has disappeared by becoming empty. */
13164 goto cancel;
13165 else if (w->last_modified < MODIFF
13166 || w->last_overlay_modified < OVERLAY_MODIFF
13167 || MINI_WINDOW_P (w))
13168 {
13169 /* We have to handle the case of continuation around a
13170 wide-column character (see the comment in indent.c around
13171 line 1340).
13172
13173 For instance, in the following case:
13174
13175 -------- Insert --------
13176 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13177 J_I_ ==> J_I_ `^^' are cursors.
13178 ^^ ^^
13179 -------- --------
13180
13181 As we have to redraw the line above, we cannot use this
13182 optimization. */
13183
13184 struct it it;
13185 int line_height_before = this_line_pixel_height;
13186
13187 /* Note that start_display will handle the case that the
13188 line starting at tlbufpos is a continuation line. */
13189 start_display (&it, w, tlbufpos);
13190
13191 /* Implementation note: It this still necessary? */
13192 if (it.current_x != this_line_start_x)
13193 goto cancel;
13194
13195 TRACE ((stderr, "trying display optimization 1\n"));
13196 w->cursor.vpos = -1;
13197 overlay_arrow_seen = 0;
13198 it.vpos = this_line_vpos;
13199 it.current_y = this_line_y;
13200 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13201 display_line (&it);
13202
13203 /* If line contains point, is not continued,
13204 and ends at same distance from eob as before, we win. */
13205 if (w->cursor.vpos >= 0
13206 /* Line is not continued, otherwise this_line_start_pos
13207 would have been set to 0 in display_line. */
13208 && CHARPOS (this_line_start_pos)
13209 /* Line ends as before. */
13210 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13211 /* Line has same height as before. Otherwise other lines
13212 would have to be shifted up or down. */
13213 && this_line_pixel_height == line_height_before)
13214 {
13215 /* If this is not the window's last line, we must adjust
13216 the charstarts of the lines below. */
13217 if (it.current_y < it.last_visible_y)
13218 {
13219 struct glyph_row *row
13220 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13221 ptrdiff_t delta, delta_bytes;
13222
13223 /* We used to distinguish between two cases here,
13224 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13225 when the line ends in a newline or the end of the
13226 buffer's accessible portion. But both cases did
13227 the same, so they were collapsed. */
13228 delta = (Z
13229 - CHARPOS (tlendpos)
13230 - MATRIX_ROW_START_CHARPOS (row));
13231 delta_bytes = (Z_BYTE
13232 - BYTEPOS (tlendpos)
13233 - MATRIX_ROW_START_BYTEPOS (row));
13234
13235 increment_matrix_positions (w->current_matrix,
13236 this_line_vpos + 1,
13237 w->current_matrix->nrows,
13238 delta, delta_bytes);
13239 }
13240
13241 /* If this row displays text now but previously didn't,
13242 or vice versa, w->window_end_vpos may have to be
13243 adjusted. */
13244 if ((it.glyph_row - 1)->displays_text_p)
13245 {
13246 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13247 XSETINT (w->window_end_vpos, this_line_vpos);
13248 }
13249 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13250 && this_line_vpos > 0)
13251 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13252 w->window_end_valid = Qnil;
13253
13254 /* Update hint: No need to try to scroll in update_window. */
13255 w->desired_matrix->no_scrolling_p = 1;
13256
13257 #ifdef GLYPH_DEBUG
13258 *w->desired_matrix->method = 0;
13259 debug_method_add (w, "optimization 1");
13260 #endif
13261 #ifdef HAVE_WINDOW_SYSTEM
13262 update_window_fringes (w, 0);
13263 #endif
13264 goto update;
13265 }
13266 else
13267 goto cancel;
13268 }
13269 else if (/* Cursor position hasn't changed. */
13270 PT == w->last_point
13271 /* Make sure the cursor was last displayed
13272 in this window. Otherwise we have to reposition it. */
13273 && 0 <= w->cursor.vpos
13274 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13275 {
13276 if (!must_finish)
13277 {
13278 do_pending_window_change (1);
13279 /* If selected_window changed, redisplay again. */
13280 if (WINDOWP (selected_window)
13281 && (w = XWINDOW (selected_window)) != sw)
13282 goto retry;
13283
13284 /* We used to always goto end_of_redisplay here, but this
13285 isn't enough if we have a blinking cursor. */
13286 if (w->cursor_off_p == w->last_cursor_off_p)
13287 goto end_of_redisplay;
13288 }
13289 goto update;
13290 }
13291 /* If highlighting the region, or if the cursor is in the echo area,
13292 then we can't just move the cursor. */
13293 else if (! (!NILP (Vtransient_mark_mode)
13294 && !NILP (BVAR (current_buffer, mark_active)))
13295 && (EQ (selected_window,
13296 BVAR (current_buffer, last_selected_window))
13297 || highlight_nonselected_windows)
13298 && NILP (w->region_showing)
13299 && NILP (Vshow_trailing_whitespace)
13300 && !cursor_in_echo_area)
13301 {
13302 struct it it;
13303 struct glyph_row *row;
13304
13305 /* Skip from tlbufpos to PT and see where it is. Note that
13306 PT may be in invisible text. If so, we will end at the
13307 next visible position. */
13308 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13309 NULL, DEFAULT_FACE_ID);
13310 it.current_x = this_line_start_x;
13311 it.current_y = this_line_y;
13312 it.vpos = this_line_vpos;
13313
13314 /* The call to move_it_to stops in front of PT, but
13315 moves over before-strings. */
13316 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13317
13318 if (it.vpos == this_line_vpos
13319 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13320 row->enabled_p))
13321 {
13322 eassert (this_line_vpos == it.vpos);
13323 eassert (this_line_y == it.current_y);
13324 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13325 #ifdef GLYPH_DEBUG
13326 *w->desired_matrix->method = 0;
13327 debug_method_add (w, "optimization 3");
13328 #endif
13329 goto update;
13330 }
13331 else
13332 goto cancel;
13333 }
13334
13335 cancel:
13336 /* Text changed drastically or point moved off of line. */
13337 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13338 }
13339
13340 CHARPOS (this_line_start_pos) = 0;
13341 consider_all_windows_p |= buffer_shared > 1;
13342 ++clear_face_cache_count;
13343 #ifdef HAVE_WINDOW_SYSTEM
13344 ++clear_image_cache_count;
13345 #endif
13346
13347 /* Build desired matrices, and update the display. If
13348 consider_all_windows_p is non-zero, do it for all windows on all
13349 frames. Otherwise do it for selected_window, only. */
13350
13351 if (consider_all_windows_p)
13352 {
13353 Lisp_Object tail, frame;
13354
13355 FOR_EACH_FRAME (tail, frame)
13356 XFRAME (frame)->updated_p = 0;
13357
13358 /* Recompute # windows showing selected buffer. This will be
13359 incremented each time such a window is displayed. */
13360 buffer_shared = 0;
13361
13362 FOR_EACH_FRAME (tail, frame)
13363 {
13364 struct frame *f = XFRAME (frame);
13365
13366 /* We don't have to do anything for unselected terminal
13367 frames. */
13368 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13369 && !EQ (FRAME_TTY (f)->top_frame, frame))
13370 continue;
13371
13372 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13373 {
13374 if (! EQ (frame, selected_frame))
13375 /* Select the frame, for the sake of frame-local
13376 variables. */
13377 select_frame_for_redisplay (frame);
13378
13379 /* Mark all the scroll bars to be removed; we'll redeem
13380 the ones we want when we redisplay their windows. */
13381 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13382 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13383
13384 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13385 redisplay_windows (FRAME_ROOT_WINDOW (f));
13386
13387 /* The X error handler may have deleted that frame. */
13388 if (!FRAME_LIVE_P (f))
13389 continue;
13390
13391 /* Any scroll bars which redisplay_windows should have
13392 nuked should now go away. */
13393 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13394 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13395
13396 /* If fonts changed, display again. */
13397 /* ??? rms: I suspect it is a mistake to jump all the way
13398 back to retry here. It should just retry this frame. */
13399 if (fonts_changed_p)
13400 goto retry;
13401
13402 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13403 {
13404 /* See if we have to hscroll. */
13405 if (!f->already_hscrolled_p)
13406 {
13407 f->already_hscrolled_p = 1;
13408 if (hscroll_windows (f->root_window))
13409 goto retry;
13410 }
13411
13412 /* Prevent various kinds of signals during display
13413 update. stdio is not robust about handling
13414 signals, which can cause an apparent I/O
13415 error. */
13416 if (interrupt_input)
13417 unrequest_sigio ();
13418 STOP_POLLING;
13419
13420 /* Update the display. */
13421 set_window_update_flags (XWINDOW (f->root_window), 1);
13422 pending |= update_frame (f, 0, 0);
13423 f->updated_p = 1;
13424 }
13425 }
13426 }
13427
13428 if (!EQ (old_frame, selected_frame)
13429 && FRAME_LIVE_P (XFRAME (old_frame)))
13430 /* We played a bit fast-and-loose above and allowed selected_frame
13431 and selected_window to be temporarily out-of-sync but let's make
13432 sure this stays contained. */
13433 select_frame_for_redisplay (old_frame);
13434 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13435
13436 if (!pending)
13437 {
13438 /* Do the mark_window_display_accurate after all windows have
13439 been redisplayed because this call resets flags in buffers
13440 which are needed for proper redisplay. */
13441 FOR_EACH_FRAME (tail, frame)
13442 {
13443 struct frame *f = XFRAME (frame);
13444 if (f->updated_p)
13445 {
13446 mark_window_display_accurate (f->root_window, 1);
13447 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13448 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13449 }
13450 }
13451 }
13452 }
13453 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13454 {
13455 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13456 struct frame *mini_frame;
13457
13458 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13459 /* Use list_of_error, not Qerror, so that
13460 we catch only errors and don't run the debugger. */
13461 internal_condition_case_1 (redisplay_window_1, selected_window,
13462 list_of_error,
13463 redisplay_window_error);
13464 if (update_miniwindow_p)
13465 internal_condition_case_1 (redisplay_window_1, mini_window,
13466 list_of_error,
13467 redisplay_window_error);
13468
13469 /* Compare desired and current matrices, perform output. */
13470
13471 update:
13472 /* If fonts changed, display again. */
13473 if (fonts_changed_p)
13474 goto retry;
13475
13476 /* Prevent various kinds of signals during display update.
13477 stdio is not robust about handling signals,
13478 which can cause an apparent I/O error. */
13479 if (interrupt_input)
13480 unrequest_sigio ();
13481 STOP_POLLING;
13482
13483 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13484 {
13485 if (hscroll_windows (selected_window))
13486 goto retry;
13487
13488 XWINDOW (selected_window)->must_be_updated_p = 1;
13489 pending = update_frame (sf, 0, 0);
13490 }
13491
13492 /* We may have called echo_area_display at the top of this
13493 function. If the echo area is on another frame, that may
13494 have put text on a frame other than the selected one, so the
13495 above call to update_frame would not have caught it. Catch
13496 it here. */
13497 mini_window = FRAME_MINIBUF_WINDOW (sf);
13498 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13499
13500 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13501 {
13502 XWINDOW (mini_window)->must_be_updated_p = 1;
13503 pending |= update_frame (mini_frame, 0, 0);
13504 if (!pending && hscroll_windows (mini_window))
13505 goto retry;
13506 }
13507 }
13508
13509 /* If display was paused because of pending input, make sure we do a
13510 thorough update the next time. */
13511 if (pending)
13512 {
13513 /* Prevent the optimization at the beginning of
13514 redisplay_internal that tries a single-line update of the
13515 line containing the cursor in the selected window. */
13516 CHARPOS (this_line_start_pos) = 0;
13517
13518 /* Let the overlay arrow be updated the next time. */
13519 update_overlay_arrows (0);
13520
13521 /* If we pause after scrolling, some rows in the current
13522 matrices of some windows are not valid. */
13523 if (!WINDOW_FULL_WIDTH_P (w)
13524 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13525 update_mode_lines = 1;
13526 }
13527 else
13528 {
13529 if (!consider_all_windows_p)
13530 {
13531 /* This has already been done above if
13532 consider_all_windows_p is set. */
13533 mark_window_display_accurate_1 (w, 1);
13534
13535 /* Say overlay arrows are up to date. */
13536 update_overlay_arrows (1);
13537
13538 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13539 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13540 }
13541
13542 update_mode_lines = 0;
13543 windows_or_buffers_changed = 0;
13544 cursor_type_changed = 0;
13545 }
13546
13547 /* Start SIGIO interrupts coming again. Having them off during the
13548 code above makes it less likely one will discard output, but not
13549 impossible, since there might be stuff in the system buffer here.
13550 But it is much hairier to try to do anything about that. */
13551 if (interrupt_input)
13552 request_sigio ();
13553 RESUME_POLLING;
13554
13555 /* If a frame has become visible which was not before, redisplay
13556 again, so that we display it. Expose events for such a frame
13557 (which it gets when becoming visible) don't call the parts of
13558 redisplay constructing glyphs, so simply exposing a frame won't
13559 display anything in this case. So, we have to display these
13560 frames here explicitly. */
13561 if (!pending)
13562 {
13563 Lisp_Object tail, frame;
13564 int new_count = 0;
13565
13566 FOR_EACH_FRAME (tail, frame)
13567 {
13568 int this_is_visible = 0;
13569
13570 if (XFRAME (frame)->visible)
13571 this_is_visible = 1;
13572 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13573 if (XFRAME (frame)->visible)
13574 this_is_visible = 1;
13575
13576 if (this_is_visible)
13577 new_count++;
13578 }
13579
13580 if (new_count != number_of_visible_frames)
13581 windows_or_buffers_changed++;
13582 }
13583
13584 /* Change frame size now if a change is pending. */
13585 do_pending_window_change (1);
13586
13587 /* If we just did a pending size change, or have additional
13588 visible frames, or selected_window changed, redisplay again. */
13589 if ((windows_or_buffers_changed && !pending)
13590 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13591 goto retry;
13592
13593 /* Clear the face and image caches.
13594
13595 We used to do this only if consider_all_windows_p. But the cache
13596 needs to be cleared if a timer creates images in the current
13597 buffer (e.g. the test case in Bug#6230). */
13598
13599 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13600 {
13601 clear_face_cache (0);
13602 clear_face_cache_count = 0;
13603 }
13604
13605 #ifdef HAVE_WINDOW_SYSTEM
13606 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13607 {
13608 clear_image_caches (Qnil);
13609 clear_image_cache_count = 0;
13610 }
13611 #endif /* HAVE_WINDOW_SYSTEM */
13612
13613 end_of_redisplay:
13614 unbind_to (count, Qnil);
13615 RESUME_POLLING;
13616 }
13617
13618
13619 /* Redisplay, but leave alone any recent echo area message unless
13620 another message has been requested in its place.
13621
13622 This is useful in situations where you need to redisplay but no
13623 user action has occurred, making it inappropriate for the message
13624 area to be cleared. See tracking_off and
13625 wait_reading_process_output for examples of these situations.
13626
13627 FROM_WHERE is an integer saying from where this function was
13628 called. This is useful for debugging. */
13629
13630 void
13631 redisplay_preserve_echo_area (int from_where)
13632 {
13633 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13634
13635 if (!NILP (echo_area_buffer[1]))
13636 {
13637 /* We have a previously displayed message, but no current
13638 message. Redisplay the previous message. */
13639 display_last_displayed_message_p = 1;
13640 redisplay_internal ();
13641 display_last_displayed_message_p = 0;
13642 }
13643 else
13644 redisplay_internal ();
13645
13646 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13647 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13648 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13649 }
13650
13651
13652 /* Function registered with record_unwind_protect in
13653 redisplay_internal. Reset redisplaying_p to the value it had
13654 before redisplay_internal was called, and clear
13655 prevent_freeing_realized_faces_p. It also selects the previously
13656 selected frame, unless it has been deleted (by an X connection
13657 failure during redisplay, for example). */
13658
13659 static Lisp_Object
13660 unwind_redisplay (Lisp_Object val)
13661 {
13662 Lisp_Object old_redisplaying_p, old_frame;
13663
13664 old_redisplaying_p = XCAR (val);
13665 redisplaying_p = XFASTINT (old_redisplaying_p);
13666 old_frame = XCDR (val);
13667 if (! EQ (old_frame, selected_frame)
13668 && FRAME_LIVE_P (XFRAME (old_frame)))
13669 select_frame_for_redisplay (old_frame);
13670 return Qnil;
13671 }
13672
13673
13674 /* Mark the display of window W as accurate or inaccurate. If
13675 ACCURATE_P is non-zero mark display of W as accurate. If
13676 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13677 redisplay_internal is called. */
13678
13679 static void
13680 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13681 {
13682 if (BUFFERP (w->buffer))
13683 {
13684 struct buffer *b = XBUFFER (w->buffer);
13685
13686 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13687 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13688 w->last_had_star
13689 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13690
13691 if (accurate_p)
13692 {
13693 b->clip_changed = 0;
13694 b->prevent_redisplay_optimizations_p = 0;
13695
13696 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13697 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13698 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13699 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13700
13701 w->current_matrix->buffer = b;
13702 w->current_matrix->begv = BUF_BEGV (b);
13703 w->current_matrix->zv = BUF_ZV (b);
13704
13705 w->last_cursor = w->cursor;
13706 w->last_cursor_off_p = w->cursor_off_p;
13707
13708 if (w == XWINDOW (selected_window))
13709 w->last_point = BUF_PT (b);
13710 else
13711 w->last_point = XMARKER (w->pointm)->charpos;
13712 }
13713 }
13714
13715 if (accurate_p)
13716 {
13717 w->window_end_valid = w->buffer;
13718 w->update_mode_line = 0;
13719 }
13720 }
13721
13722
13723 /* Mark the display of windows in the window tree rooted at WINDOW as
13724 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13725 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13726 be redisplayed the next time redisplay_internal is called. */
13727
13728 void
13729 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13730 {
13731 struct window *w;
13732
13733 for (; !NILP (window); window = w->next)
13734 {
13735 w = XWINDOW (window);
13736 mark_window_display_accurate_1 (w, accurate_p);
13737
13738 if (!NILP (w->vchild))
13739 mark_window_display_accurate (w->vchild, accurate_p);
13740 if (!NILP (w->hchild))
13741 mark_window_display_accurate (w->hchild, accurate_p);
13742 }
13743
13744 if (accurate_p)
13745 {
13746 update_overlay_arrows (1);
13747 }
13748 else
13749 {
13750 /* Force a thorough redisplay the next time by setting
13751 last_arrow_position and last_arrow_string to t, which is
13752 unequal to any useful value of Voverlay_arrow_... */
13753 update_overlay_arrows (-1);
13754 }
13755 }
13756
13757
13758 /* Return value in display table DP (Lisp_Char_Table *) for character
13759 C. Since a display table doesn't have any parent, we don't have to
13760 follow parent. Do not call this function directly but use the
13761 macro DISP_CHAR_VECTOR. */
13762
13763 Lisp_Object
13764 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13765 {
13766 Lisp_Object val;
13767
13768 if (ASCII_CHAR_P (c))
13769 {
13770 val = dp->ascii;
13771 if (SUB_CHAR_TABLE_P (val))
13772 val = XSUB_CHAR_TABLE (val)->contents[c];
13773 }
13774 else
13775 {
13776 Lisp_Object table;
13777
13778 XSETCHAR_TABLE (table, dp);
13779 val = char_table_ref (table, c);
13780 }
13781 if (NILP (val))
13782 val = dp->defalt;
13783 return val;
13784 }
13785
13786
13787 \f
13788 /***********************************************************************
13789 Window Redisplay
13790 ***********************************************************************/
13791
13792 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13793
13794 static void
13795 redisplay_windows (Lisp_Object window)
13796 {
13797 while (!NILP (window))
13798 {
13799 struct window *w = XWINDOW (window);
13800
13801 if (!NILP (w->hchild))
13802 redisplay_windows (w->hchild);
13803 else if (!NILP (w->vchild))
13804 redisplay_windows (w->vchild);
13805 else if (!NILP (w->buffer))
13806 {
13807 displayed_buffer = XBUFFER (w->buffer);
13808 /* Use list_of_error, not Qerror, so that
13809 we catch only errors and don't run the debugger. */
13810 internal_condition_case_1 (redisplay_window_0, window,
13811 list_of_error,
13812 redisplay_window_error);
13813 }
13814
13815 window = w->next;
13816 }
13817 }
13818
13819 static Lisp_Object
13820 redisplay_window_error (Lisp_Object ignore)
13821 {
13822 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13823 return Qnil;
13824 }
13825
13826 static Lisp_Object
13827 redisplay_window_0 (Lisp_Object window)
13828 {
13829 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13830 redisplay_window (window, 0);
13831 return Qnil;
13832 }
13833
13834 static Lisp_Object
13835 redisplay_window_1 (Lisp_Object window)
13836 {
13837 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13838 redisplay_window (window, 1);
13839 return Qnil;
13840 }
13841 \f
13842
13843 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13844 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13845 which positions recorded in ROW differ from current buffer
13846 positions.
13847
13848 Return 0 if cursor is not on this row, 1 otherwise. */
13849
13850 static int
13851 set_cursor_from_row (struct window *w, struct glyph_row *row,
13852 struct glyph_matrix *matrix,
13853 ptrdiff_t delta, ptrdiff_t delta_bytes,
13854 int dy, int dvpos)
13855 {
13856 struct glyph *glyph = row->glyphs[TEXT_AREA];
13857 struct glyph *end = glyph + row->used[TEXT_AREA];
13858 struct glyph *cursor = NULL;
13859 /* The last known character position in row. */
13860 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13861 int x = row->x;
13862 ptrdiff_t pt_old = PT - delta;
13863 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13864 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13865 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13866 /* A glyph beyond the edge of TEXT_AREA which we should never
13867 touch. */
13868 struct glyph *glyphs_end = end;
13869 /* Non-zero means we've found a match for cursor position, but that
13870 glyph has the avoid_cursor_p flag set. */
13871 int match_with_avoid_cursor = 0;
13872 /* Non-zero means we've seen at least one glyph that came from a
13873 display string. */
13874 int string_seen = 0;
13875 /* Largest and smallest buffer positions seen so far during scan of
13876 glyph row. */
13877 ptrdiff_t bpos_max = pos_before;
13878 ptrdiff_t bpos_min = pos_after;
13879 /* Last buffer position covered by an overlay string with an integer
13880 `cursor' property. */
13881 ptrdiff_t bpos_covered = 0;
13882 /* Non-zero means the display string on which to display the cursor
13883 comes from a text property, not from an overlay. */
13884 int string_from_text_prop = 0;
13885
13886 /* Don't even try doing anything if called for a mode-line or
13887 header-line row, since the rest of the code isn't prepared to
13888 deal with such calamities. */
13889 eassert (!row->mode_line_p);
13890 if (row->mode_line_p)
13891 return 0;
13892
13893 /* Skip over glyphs not having an object at the start and the end of
13894 the row. These are special glyphs like truncation marks on
13895 terminal frames. */
13896 if (row->displays_text_p)
13897 {
13898 if (!row->reversed_p)
13899 {
13900 while (glyph < end
13901 && INTEGERP (glyph->object)
13902 && glyph->charpos < 0)
13903 {
13904 x += glyph->pixel_width;
13905 ++glyph;
13906 }
13907 while (end > glyph
13908 && INTEGERP ((end - 1)->object)
13909 /* CHARPOS is zero for blanks and stretch glyphs
13910 inserted by extend_face_to_end_of_line. */
13911 && (end - 1)->charpos <= 0)
13912 --end;
13913 glyph_before = glyph - 1;
13914 glyph_after = end;
13915 }
13916 else
13917 {
13918 struct glyph *g;
13919
13920 /* If the glyph row is reversed, we need to process it from back
13921 to front, so swap the edge pointers. */
13922 glyphs_end = end = glyph - 1;
13923 glyph += row->used[TEXT_AREA] - 1;
13924
13925 while (glyph > end + 1
13926 && INTEGERP (glyph->object)
13927 && glyph->charpos < 0)
13928 {
13929 --glyph;
13930 x -= glyph->pixel_width;
13931 }
13932 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13933 --glyph;
13934 /* By default, in reversed rows we put the cursor on the
13935 rightmost (first in the reading order) glyph. */
13936 for (g = end + 1; g < glyph; g++)
13937 x += g->pixel_width;
13938 while (end < glyph
13939 && INTEGERP ((end + 1)->object)
13940 && (end + 1)->charpos <= 0)
13941 ++end;
13942 glyph_before = glyph + 1;
13943 glyph_after = end;
13944 }
13945 }
13946 else if (row->reversed_p)
13947 {
13948 /* In R2L rows that don't display text, put the cursor on the
13949 rightmost glyph. Case in point: an empty last line that is
13950 part of an R2L paragraph. */
13951 cursor = end - 1;
13952 /* Avoid placing the cursor on the last glyph of the row, where
13953 on terminal frames we hold the vertical border between
13954 adjacent windows. */
13955 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13956 && !WINDOW_RIGHTMOST_P (w)
13957 && cursor == row->glyphs[LAST_AREA] - 1)
13958 cursor--;
13959 x = -1; /* will be computed below, at label compute_x */
13960 }
13961
13962 /* Step 1: Try to find the glyph whose character position
13963 corresponds to point. If that's not possible, find 2 glyphs
13964 whose character positions are the closest to point, one before
13965 point, the other after it. */
13966 if (!row->reversed_p)
13967 while (/* not marched to end of glyph row */
13968 glyph < end
13969 /* glyph was not inserted by redisplay for internal purposes */
13970 && !INTEGERP (glyph->object))
13971 {
13972 if (BUFFERP (glyph->object))
13973 {
13974 ptrdiff_t dpos = glyph->charpos - pt_old;
13975
13976 if (glyph->charpos > bpos_max)
13977 bpos_max = glyph->charpos;
13978 if (glyph->charpos < bpos_min)
13979 bpos_min = glyph->charpos;
13980 if (!glyph->avoid_cursor_p)
13981 {
13982 /* If we hit point, we've found the glyph on which to
13983 display the cursor. */
13984 if (dpos == 0)
13985 {
13986 match_with_avoid_cursor = 0;
13987 break;
13988 }
13989 /* See if we've found a better approximation to
13990 POS_BEFORE or to POS_AFTER. */
13991 if (0 > dpos && dpos > pos_before - pt_old)
13992 {
13993 pos_before = glyph->charpos;
13994 glyph_before = glyph;
13995 }
13996 else if (0 < dpos && dpos < pos_after - pt_old)
13997 {
13998 pos_after = glyph->charpos;
13999 glyph_after = glyph;
14000 }
14001 }
14002 else if (dpos == 0)
14003 match_with_avoid_cursor = 1;
14004 }
14005 else if (STRINGP (glyph->object))
14006 {
14007 Lisp_Object chprop;
14008 ptrdiff_t glyph_pos = glyph->charpos;
14009
14010 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14011 glyph->object);
14012 if (!NILP (chprop))
14013 {
14014 /* If the string came from a `display' text property,
14015 look up the buffer position of that property and
14016 use that position to update bpos_max, as if we
14017 actually saw such a position in one of the row's
14018 glyphs. This helps with supporting integer values
14019 of `cursor' property on the display string in
14020 situations where most or all of the row's buffer
14021 text is completely covered by display properties,
14022 so that no glyph with valid buffer positions is
14023 ever seen in the row. */
14024 ptrdiff_t prop_pos =
14025 string_buffer_position_lim (glyph->object, pos_before,
14026 pos_after, 0);
14027
14028 if (prop_pos >= pos_before)
14029 bpos_max = prop_pos - 1;
14030 }
14031 if (INTEGERP (chprop))
14032 {
14033 bpos_covered = bpos_max + XINT (chprop);
14034 /* If the `cursor' property covers buffer positions up
14035 to and including point, we should display cursor on
14036 this glyph. Note that, if a `cursor' property on one
14037 of the string's characters has an integer value, we
14038 will break out of the loop below _before_ we get to
14039 the position match above. IOW, integer values of
14040 the `cursor' property override the "exact match for
14041 point" strategy of positioning the cursor. */
14042 /* Implementation note: bpos_max == pt_old when, e.g.,
14043 we are in an empty line, where bpos_max is set to
14044 MATRIX_ROW_START_CHARPOS, see above. */
14045 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14046 {
14047 cursor = glyph;
14048 break;
14049 }
14050 }
14051
14052 string_seen = 1;
14053 }
14054 x += glyph->pixel_width;
14055 ++glyph;
14056 }
14057 else if (glyph > end) /* row is reversed */
14058 while (!INTEGERP (glyph->object))
14059 {
14060 if (BUFFERP (glyph->object))
14061 {
14062 ptrdiff_t dpos = glyph->charpos - pt_old;
14063
14064 if (glyph->charpos > bpos_max)
14065 bpos_max = glyph->charpos;
14066 if (glyph->charpos < bpos_min)
14067 bpos_min = glyph->charpos;
14068 if (!glyph->avoid_cursor_p)
14069 {
14070 if (dpos == 0)
14071 {
14072 match_with_avoid_cursor = 0;
14073 break;
14074 }
14075 if (0 > dpos && dpos > pos_before - pt_old)
14076 {
14077 pos_before = glyph->charpos;
14078 glyph_before = glyph;
14079 }
14080 else if (0 < dpos && dpos < pos_after - pt_old)
14081 {
14082 pos_after = glyph->charpos;
14083 glyph_after = glyph;
14084 }
14085 }
14086 else if (dpos == 0)
14087 match_with_avoid_cursor = 1;
14088 }
14089 else if (STRINGP (glyph->object))
14090 {
14091 Lisp_Object chprop;
14092 ptrdiff_t glyph_pos = glyph->charpos;
14093
14094 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14095 glyph->object);
14096 if (!NILP (chprop))
14097 {
14098 ptrdiff_t prop_pos =
14099 string_buffer_position_lim (glyph->object, pos_before,
14100 pos_after, 0);
14101
14102 if (prop_pos >= pos_before)
14103 bpos_max = prop_pos - 1;
14104 }
14105 if (INTEGERP (chprop))
14106 {
14107 bpos_covered = bpos_max + XINT (chprop);
14108 /* If the `cursor' property covers buffer positions up
14109 to and including point, we should display cursor on
14110 this glyph. */
14111 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14112 {
14113 cursor = glyph;
14114 break;
14115 }
14116 }
14117 string_seen = 1;
14118 }
14119 --glyph;
14120 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14121 {
14122 x--; /* can't use any pixel_width */
14123 break;
14124 }
14125 x -= glyph->pixel_width;
14126 }
14127
14128 /* Step 2: If we didn't find an exact match for point, we need to
14129 look for a proper place to put the cursor among glyphs between
14130 GLYPH_BEFORE and GLYPH_AFTER. */
14131 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14132 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14133 && bpos_covered < pt_old)
14134 {
14135 /* An empty line has a single glyph whose OBJECT is zero and
14136 whose CHARPOS is the position of a newline on that line.
14137 Note that on a TTY, there are more glyphs after that, which
14138 were produced by extend_face_to_end_of_line, but their
14139 CHARPOS is zero or negative. */
14140 int empty_line_p =
14141 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14142 && INTEGERP (glyph->object) && glyph->charpos > 0;
14143
14144 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14145 {
14146 ptrdiff_t ellipsis_pos;
14147
14148 /* Scan back over the ellipsis glyphs. */
14149 if (!row->reversed_p)
14150 {
14151 ellipsis_pos = (glyph - 1)->charpos;
14152 while (glyph > row->glyphs[TEXT_AREA]
14153 && (glyph - 1)->charpos == ellipsis_pos)
14154 glyph--, x -= glyph->pixel_width;
14155 /* That loop always goes one position too far, including
14156 the glyph before the ellipsis. So scan forward over
14157 that one. */
14158 x += glyph->pixel_width;
14159 glyph++;
14160 }
14161 else /* row is reversed */
14162 {
14163 ellipsis_pos = (glyph + 1)->charpos;
14164 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14165 && (glyph + 1)->charpos == ellipsis_pos)
14166 glyph++, x += glyph->pixel_width;
14167 x -= glyph->pixel_width;
14168 glyph--;
14169 }
14170 }
14171 else if (match_with_avoid_cursor)
14172 {
14173 cursor = glyph_after;
14174 x = -1;
14175 }
14176 else if (string_seen)
14177 {
14178 int incr = row->reversed_p ? -1 : +1;
14179
14180 /* Need to find the glyph that came out of a string which is
14181 present at point. That glyph is somewhere between
14182 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14183 positioned between POS_BEFORE and POS_AFTER in the
14184 buffer. */
14185 struct glyph *start, *stop;
14186 ptrdiff_t pos = pos_before;
14187
14188 x = -1;
14189
14190 /* If the row ends in a newline from a display string,
14191 reordering could have moved the glyphs belonging to the
14192 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14193 in this case we extend the search to the last glyph in
14194 the row that was not inserted by redisplay. */
14195 if (row->ends_in_newline_from_string_p)
14196 {
14197 glyph_after = end;
14198 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14199 }
14200
14201 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14202 correspond to POS_BEFORE and POS_AFTER, respectively. We
14203 need START and STOP in the order that corresponds to the
14204 row's direction as given by its reversed_p flag. If the
14205 directionality of characters between POS_BEFORE and
14206 POS_AFTER is the opposite of the row's base direction,
14207 these characters will have been reordered for display,
14208 and we need to reverse START and STOP. */
14209 if (!row->reversed_p)
14210 {
14211 start = min (glyph_before, glyph_after);
14212 stop = max (glyph_before, glyph_after);
14213 }
14214 else
14215 {
14216 start = max (glyph_before, glyph_after);
14217 stop = min (glyph_before, glyph_after);
14218 }
14219 for (glyph = start + incr;
14220 row->reversed_p ? glyph > stop : glyph < stop; )
14221 {
14222
14223 /* Any glyphs that come from the buffer are here because
14224 of bidi reordering. Skip them, and only pay
14225 attention to glyphs that came from some string. */
14226 if (STRINGP (glyph->object))
14227 {
14228 Lisp_Object str;
14229 ptrdiff_t tem;
14230 /* If the display property covers the newline, we
14231 need to search for it one position farther. */
14232 ptrdiff_t lim = pos_after
14233 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14234
14235 string_from_text_prop = 0;
14236 str = glyph->object;
14237 tem = string_buffer_position_lim (str, pos, lim, 0);
14238 if (tem == 0 /* from overlay */
14239 || pos <= tem)
14240 {
14241 /* If the string from which this glyph came is
14242 found in the buffer at point, or at position
14243 that is closer to point than pos_after, then
14244 we've found the glyph we've been looking for.
14245 If it comes from an overlay (tem == 0), and
14246 it has the `cursor' property on one of its
14247 glyphs, record that glyph as a candidate for
14248 displaying the cursor. (As in the
14249 unidirectional version, we will display the
14250 cursor on the last candidate we find.) */
14251 if (tem == 0
14252 || tem == pt_old
14253 || (tem - pt_old > 0 && tem < pos_after))
14254 {
14255 /* The glyphs from this string could have
14256 been reordered. Find the one with the
14257 smallest string position. Or there could
14258 be a character in the string with the
14259 `cursor' property, which means display
14260 cursor on that character's glyph. */
14261 ptrdiff_t strpos = glyph->charpos;
14262
14263 if (tem)
14264 {
14265 cursor = glyph;
14266 string_from_text_prop = 1;
14267 }
14268 for ( ;
14269 (row->reversed_p ? glyph > stop : glyph < stop)
14270 && EQ (glyph->object, str);
14271 glyph += incr)
14272 {
14273 Lisp_Object cprop;
14274 ptrdiff_t gpos = glyph->charpos;
14275
14276 cprop = Fget_char_property (make_number (gpos),
14277 Qcursor,
14278 glyph->object);
14279 if (!NILP (cprop))
14280 {
14281 cursor = glyph;
14282 break;
14283 }
14284 if (tem && glyph->charpos < strpos)
14285 {
14286 strpos = glyph->charpos;
14287 cursor = glyph;
14288 }
14289 }
14290
14291 if (tem == pt_old
14292 || (tem - pt_old > 0 && tem < pos_after))
14293 goto compute_x;
14294 }
14295 if (tem)
14296 pos = tem + 1; /* don't find previous instances */
14297 }
14298 /* This string is not what we want; skip all of the
14299 glyphs that came from it. */
14300 while ((row->reversed_p ? glyph > stop : glyph < stop)
14301 && EQ (glyph->object, str))
14302 glyph += incr;
14303 }
14304 else
14305 glyph += incr;
14306 }
14307
14308 /* If we reached the end of the line, and END was from a string,
14309 the cursor is not on this line. */
14310 if (cursor == NULL
14311 && (row->reversed_p ? glyph <= end : glyph >= end)
14312 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14313 && STRINGP (end->object)
14314 && row->continued_p)
14315 return 0;
14316 }
14317 /* A truncated row may not include PT among its character positions.
14318 Setting the cursor inside the scroll margin will trigger
14319 recalculation of hscroll in hscroll_window_tree. But if a
14320 display string covers point, defer to the string-handling
14321 code below to figure this out. */
14322 else if (row->truncated_on_left_p && pt_old < bpos_min)
14323 {
14324 cursor = glyph_before;
14325 x = -1;
14326 }
14327 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14328 /* Zero-width characters produce no glyphs. */
14329 || (!empty_line_p
14330 && (row->reversed_p
14331 ? glyph_after > glyphs_end
14332 : glyph_after < glyphs_end)))
14333 {
14334 cursor = glyph_after;
14335 x = -1;
14336 }
14337 }
14338
14339 compute_x:
14340 if (cursor != NULL)
14341 glyph = cursor;
14342 else if (glyph == glyphs_end
14343 && pos_before == pos_after
14344 && STRINGP ((row->reversed_p
14345 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14346 : row->glyphs[TEXT_AREA])->object))
14347 {
14348 /* If all the glyphs of this row came from strings, put the
14349 cursor on the first glyph of the row. This avoids having the
14350 cursor outside of the text area in this very rare and hard
14351 use case. */
14352 glyph =
14353 row->reversed_p
14354 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14355 : row->glyphs[TEXT_AREA];
14356 }
14357 if (x < 0)
14358 {
14359 struct glyph *g;
14360
14361 /* Need to compute x that corresponds to GLYPH. */
14362 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14363 {
14364 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14365 abort ();
14366 x += g->pixel_width;
14367 }
14368 }
14369
14370 /* ROW could be part of a continued line, which, under bidi
14371 reordering, might have other rows whose start and end charpos
14372 occlude point. Only set w->cursor if we found a better
14373 approximation to the cursor position than we have from previously
14374 examined candidate rows belonging to the same continued line. */
14375 if (/* we already have a candidate row */
14376 w->cursor.vpos >= 0
14377 /* that candidate is not the row we are processing */
14378 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14379 /* Make sure cursor.vpos specifies a row whose start and end
14380 charpos occlude point, and it is valid candidate for being a
14381 cursor-row. This is because some callers of this function
14382 leave cursor.vpos at the row where the cursor was displayed
14383 during the last redisplay cycle. */
14384 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14385 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14386 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14387 {
14388 struct glyph *g1 =
14389 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14390
14391 /* Don't consider glyphs that are outside TEXT_AREA. */
14392 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14393 return 0;
14394 /* Keep the candidate whose buffer position is the closest to
14395 point or has the `cursor' property. */
14396 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14397 w->cursor.hpos >= 0
14398 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14399 && ((BUFFERP (g1->object)
14400 && (g1->charpos == pt_old /* an exact match always wins */
14401 || (BUFFERP (glyph->object)
14402 && eabs (g1->charpos - pt_old)
14403 < eabs (glyph->charpos - pt_old))))
14404 /* previous candidate is a glyph from a string that has
14405 a non-nil `cursor' property */
14406 || (STRINGP (g1->object)
14407 && (!NILP (Fget_char_property (make_number (g1->charpos),
14408 Qcursor, g1->object))
14409 /* previous candidate is from the same display
14410 string as this one, and the display string
14411 came from a text property */
14412 || (EQ (g1->object, glyph->object)
14413 && string_from_text_prop)
14414 /* this candidate is from newline and its
14415 position is not an exact match */
14416 || (INTEGERP (glyph->object)
14417 && glyph->charpos != pt_old)))))
14418 return 0;
14419 /* If this candidate gives an exact match, use that. */
14420 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14421 /* If this candidate is a glyph created for the
14422 terminating newline of a line, and point is on that
14423 newline, it wins because it's an exact match. */
14424 || (!row->continued_p
14425 && INTEGERP (glyph->object)
14426 && glyph->charpos == 0
14427 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14428 /* Otherwise, keep the candidate that comes from a row
14429 spanning less buffer positions. This may win when one or
14430 both candidate positions are on glyphs that came from
14431 display strings, for which we cannot compare buffer
14432 positions. */
14433 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14434 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14435 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14436 return 0;
14437 }
14438 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14439 w->cursor.x = x;
14440 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14441 w->cursor.y = row->y + dy;
14442
14443 if (w == XWINDOW (selected_window))
14444 {
14445 if (!row->continued_p
14446 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14447 && row->x == 0)
14448 {
14449 this_line_buffer = XBUFFER (w->buffer);
14450
14451 CHARPOS (this_line_start_pos)
14452 = MATRIX_ROW_START_CHARPOS (row) + delta;
14453 BYTEPOS (this_line_start_pos)
14454 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14455
14456 CHARPOS (this_line_end_pos)
14457 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14458 BYTEPOS (this_line_end_pos)
14459 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14460
14461 this_line_y = w->cursor.y;
14462 this_line_pixel_height = row->height;
14463 this_line_vpos = w->cursor.vpos;
14464 this_line_start_x = row->x;
14465 }
14466 else
14467 CHARPOS (this_line_start_pos) = 0;
14468 }
14469
14470 return 1;
14471 }
14472
14473
14474 /* Run window scroll functions, if any, for WINDOW with new window
14475 start STARTP. Sets the window start of WINDOW to that position.
14476
14477 We assume that the window's buffer is really current. */
14478
14479 static inline struct text_pos
14480 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14481 {
14482 struct window *w = XWINDOW (window);
14483 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14484
14485 if (current_buffer != XBUFFER (w->buffer))
14486 abort ();
14487
14488 if (!NILP (Vwindow_scroll_functions))
14489 {
14490 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14491 make_number (CHARPOS (startp)));
14492 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14493 /* In case the hook functions switch buffers. */
14494 if (current_buffer != XBUFFER (w->buffer))
14495 set_buffer_internal_1 (XBUFFER (w->buffer));
14496 }
14497
14498 return startp;
14499 }
14500
14501
14502 /* Make sure the line containing the cursor is fully visible.
14503 A value of 1 means there is nothing to be done.
14504 (Either the line is fully visible, or it cannot be made so,
14505 or we cannot tell.)
14506
14507 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14508 is higher than window.
14509
14510 A value of 0 means the caller should do scrolling
14511 as if point had gone off the screen. */
14512
14513 static int
14514 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14515 {
14516 struct glyph_matrix *matrix;
14517 struct glyph_row *row;
14518 int window_height;
14519
14520 if (!make_cursor_line_fully_visible_p)
14521 return 1;
14522
14523 /* It's not always possible to find the cursor, e.g, when a window
14524 is full of overlay strings. Don't do anything in that case. */
14525 if (w->cursor.vpos < 0)
14526 return 1;
14527
14528 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14529 row = MATRIX_ROW (matrix, w->cursor.vpos);
14530
14531 /* If the cursor row is not partially visible, there's nothing to do. */
14532 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14533 return 1;
14534
14535 /* If the row the cursor is in is taller than the window's height,
14536 it's not clear what to do, so do nothing. */
14537 window_height = window_box_height (w);
14538 if (row->height >= window_height)
14539 {
14540 if (!force_p || MINI_WINDOW_P (w)
14541 || w->vscroll || w->cursor.vpos == 0)
14542 return 1;
14543 }
14544 return 0;
14545 }
14546
14547
14548 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14549 non-zero means only WINDOW is redisplayed in redisplay_internal.
14550 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14551 in redisplay_window to bring a partially visible line into view in
14552 the case that only the cursor has moved.
14553
14554 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14555 last screen line's vertical height extends past the end of the screen.
14556
14557 Value is
14558
14559 1 if scrolling succeeded
14560
14561 0 if scrolling didn't find point.
14562
14563 -1 if new fonts have been loaded so that we must interrupt
14564 redisplay, adjust glyph matrices, and try again. */
14565
14566 enum
14567 {
14568 SCROLLING_SUCCESS,
14569 SCROLLING_FAILED,
14570 SCROLLING_NEED_LARGER_MATRICES
14571 };
14572
14573 /* If scroll-conservatively is more than this, never recenter.
14574
14575 If you change this, don't forget to update the doc string of
14576 `scroll-conservatively' and the Emacs manual. */
14577 #define SCROLL_LIMIT 100
14578
14579 static int
14580 try_scrolling (Lisp_Object window, int just_this_one_p,
14581 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14582 int temp_scroll_step, int last_line_misfit)
14583 {
14584 struct window *w = XWINDOW (window);
14585 struct frame *f = XFRAME (w->frame);
14586 struct text_pos pos, startp;
14587 struct it it;
14588 int this_scroll_margin, scroll_max, rc, height;
14589 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14590 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14591 Lisp_Object aggressive;
14592 /* We will never try scrolling more than this number of lines. */
14593 int scroll_limit = SCROLL_LIMIT;
14594
14595 #ifdef GLYPH_DEBUG
14596 debug_method_add (w, "try_scrolling");
14597 #endif
14598
14599 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14600
14601 /* Compute scroll margin height in pixels. We scroll when point is
14602 within this distance from the top or bottom of the window. */
14603 if (scroll_margin > 0)
14604 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14605 * FRAME_LINE_HEIGHT (f);
14606 else
14607 this_scroll_margin = 0;
14608
14609 /* Force arg_scroll_conservatively to have a reasonable value, to
14610 avoid scrolling too far away with slow move_it_* functions. Note
14611 that the user can supply scroll-conservatively equal to
14612 `most-positive-fixnum', which can be larger than INT_MAX. */
14613 if (arg_scroll_conservatively > scroll_limit)
14614 {
14615 arg_scroll_conservatively = scroll_limit + 1;
14616 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14617 }
14618 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14619 /* Compute how much we should try to scroll maximally to bring
14620 point into view. */
14621 scroll_max = (max (scroll_step,
14622 max (arg_scroll_conservatively, temp_scroll_step))
14623 * FRAME_LINE_HEIGHT (f));
14624 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14625 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14626 /* We're trying to scroll because of aggressive scrolling but no
14627 scroll_step is set. Choose an arbitrary one. */
14628 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14629 else
14630 scroll_max = 0;
14631
14632 too_near_end:
14633
14634 /* Decide whether to scroll down. */
14635 if (PT > CHARPOS (startp))
14636 {
14637 int scroll_margin_y;
14638
14639 /* Compute the pixel ypos of the scroll margin, then move IT to
14640 either that ypos or PT, whichever comes first. */
14641 start_display (&it, w, startp);
14642 scroll_margin_y = it.last_visible_y - this_scroll_margin
14643 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14644 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14645 (MOVE_TO_POS | MOVE_TO_Y));
14646
14647 if (PT > CHARPOS (it.current.pos))
14648 {
14649 int y0 = line_bottom_y (&it);
14650 /* Compute how many pixels below window bottom to stop searching
14651 for PT. This avoids costly search for PT that is far away if
14652 the user limited scrolling by a small number of lines, but
14653 always finds PT if scroll_conservatively is set to a large
14654 number, such as most-positive-fixnum. */
14655 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14656 int y_to_move = it.last_visible_y + slack;
14657
14658 /* Compute the distance from the scroll margin to PT or to
14659 the scroll limit, whichever comes first. This should
14660 include the height of the cursor line, to make that line
14661 fully visible. */
14662 move_it_to (&it, PT, -1, y_to_move,
14663 -1, MOVE_TO_POS | MOVE_TO_Y);
14664 dy = line_bottom_y (&it) - y0;
14665
14666 if (dy > scroll_max)
14667 return SCROLLING_FAILED;
14668
14669 if (dy > 0)
14670 scroll_down_p = 1;
14671 }
14672 }
14673
14674 if (scroll_down_p)
14675 {
14676 /* Point is in or below the bottom scroll margin, so move the
14677 window start down. If scrolling conservatively, move it just
14678 enough down to make point visible. If scroll_step is set,
14679 move it down by scroll_step. */
14680 if (arg_scroll_conservatively)
14681 amount_to_scroll
14682 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14683 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14684 else if (scroll_step || temp_scroll_step)
14685 amount_to_scroll = scroll_max;
14686 else
14687 {
14688 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14689 height = WINDOW_BOX_TEXT_HEIGHT (w);
14690 if (NUMBERP (aggressive))
14691 {
14692 double float_amount = XFLOATINT (aggressive) * height;
14693 amount_to_scroll = float_amount;
14694 if (amount_to_scroll == 0 && float_amount > 0)
14695 amount_to_scroll = 1;
14696 /* Don't let point enter the scroll margin near top of
14697 the window. */
14698 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14699 amount_to_scroll = height - 2*this_scroll_margin + dy;
14700 }
14701 }
14702
14703 if (amount_to_scroll <= 0)
14704 return SCROLLING_FAILED;
14705
14706 start_display (&it, w, startp);
14707 if (arg_scroll_conservatively <= scroll_limit)
14708 move_it_vertically (&it, amount_to_scroll);
14709 else
14710 {
14711 /* Extra precision for users who set scroll-conservatively
14712 to a large number: make sure the amount we scroll
14713 the window start is never less than amount_to_scroll,
14714 which was computed as distance from window bottom to
14715 point. This matters when lines at window top and lines
14716 below window bottom have different height. */
14717 struct it it1;
14718 void *it1data = NULL;
14719 /* We use a temporary it1 because line_bottom_y can modify
14720 its argument, if it moves one line down; see there. */
14721 int start_y;
14722
14723 SAVE_IT (it1, it, it1data);
14724 start_y = line_bottom_y (&it1);
14725 do {
14726 RESTORE_IT (&it, &it, it1data);
14727 move_it_by_lines (&it, 1);
14728 SAVE_IT (it1, it, it1data);
14729 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14730 }
14731
14732 /* If STARTP is unchanged, move it down another screen line. */
14733 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14734 move_it_by_lines (&it, 1);
14735 startp = it.current.pos;
14736 }
14737 else
14738 {
14739 struct text_pos scroll_margin_pos = startp;
14740
14741 /* See if point is inside the scroll margin at the top of the
14742 window. */
14743 if (this_scroll_margin)
14744 {
14745 start_display (&it, w, startp);
14746 move_it_vertically (&it, this_scroll_margin);
14747 scroll_margin_pos = it.current.pos;
14748 }
14749
14750 if (PT < CHARPOS (scroll_margin_pos))
14751 {
14752 /* Point is in the scroll margin at the top of the window or
14753 above what is displayed in the window. */
14754 int y0, y_to_move;
14755
14756 /* Compute the vertical distance from PT to the scroll
14757 margin position. Move as far as scroll_max allows, or
14758 one screenful, or 10 screen lines, whichever is largest.
14759 Give up if distance is greater than scroll_max. */
14760 SET_TEXT_POS (pos, PT, PT_BYTE);
14761 start_display (&it, w, pos);
14762 y0 = it.current_y;
14763 y_to_move = max (it.last_visible_y,
14764 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14765 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14766 y_to_move, -1,
14767 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14768 dy = it.current_y - y0;
14769 if (dy > scroll_max)
14770 return SCROLLING_FAILED;
14771
14772 /* Compute new window start. */
14773 start_display (&it, w, startp);
14774
14775 if (arg_scroll_conservatively)
14776 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14777 max (scroll_step, temp_scroll_step));
14778 else if (scroll_step || temp_scroll_step)
14779 amount_to_scroll = scroll_max;
14780 else
14781 {
14782 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14783 height = WINDOW_BOX_TEXT_HEIGHT (w);
14784 if (NUMBERP (aggressive))
14785 {
14786 double float_amount = XFLOATINT (aggressive) * height;
14787 amount_to_scroll = float_amount;
14788 if (amount_to_scroll == 0 && float_amount > 0)
14789 amount_to_scroll = 1;
14790 amount_to_scroll -=
14791 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14792 /* Don't let point enter the scroll margin near
14793 bottom of the window. */
14794 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14795 amount_to_scroll = height - 2*this_scroll_margin + dy;
14796 }
14797 }
14798
14799 if (amount_to_scroll <= 0)
14800 return SCROLLING_FAILED;
14801
14802 move_it_vertically_backward (&it, amount_to_scroll);
14803 startp = it.current.pos;
14804 }
14805 }
14806
14807 /* Run window scroll functions. */
14808 startp = run_window_scroll_functions (window, startp);
14809
14810 /* Display the window. Give up if new fonts are loaded, or if point
14811 doesn't appear. */
14812 if (!try_window (window, startp, 0))
14813 rc = SCROLLING_NEED_LARGER_MATRICES;
14814 else if (w->cursor.vpos < 0)
14815 {
14816 clear_glyph_matrix (w->desired_matrix);
14817 rc = SCROLLING_FAILED;
14818 }
14819 else
14820 {
14821 /* Maybe forget recorded base line for line number display. */
14822 if (!just_this_one_p
14823 || current_buffer->clip_changed
14824 || BEG_UNCHANGED < CHARPOS (startp))
14825 w->base_line_number = Qnil;
14826
14827 /* If cursor ends up on a partially visible line,
14828 treat that as being off the bottom of the screen. */
14829 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14830 /* It's possible that the cursor is on the first line of the
14831 buffer, which is partially obscured due to a vscroll
14832 (Bug#7537). In that case, avoid looping forever . */
14833 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14834 {
14835 clear_glyph_matrix (w->desired_matrix);
14836 ++extra_scroll_margin_lines;
14837 goto too_near_end;
14838 }
14839 rc = SCROLLING_SUCCESS;
14840 }
14841
14842 return rc;
14843 }
14844
14845
14846 /* Compute a suitable window start for window W if display of W starts
14847 on a continuation line. Value is non-zero if a new window start
14848 was computed.
14849
14850 The new window start will be computed, based on W's width, starting
14851 from the start of the continued line. It is the start of the
14852 screen line with the minimum distance from the old start W->start. */
14853
14854 static int
14855 compute_window_start_on_continuation_line (struct window *w)
14856 {
14857 struct text_pos pos, start_pos;
14858 int window_start_changed_p = 0;
14859
14860 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14861
14862 /* If window start is on a continuation line... Window start may be
14863 < BEGV in case there's invisible text at the start of the
14864 buffer (M-x rmail, for example). */
14865 if (CHARPOS (start_pos) > BEGV
14866 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14867 {
14868 struct it it;
14869 struct glyph_row *row;
14870
14871 /* Handle the case that the window start is out of range. */
14872 if (CHARPOS (start_pos) < BEGV)
14873 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14874 else if (CHARPOS (start_pos) > ZV)
14875 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14876
14877 /* Find the start of the continued line. This should be fast
14878 because scan_buffer is fast (newline cache). */
14879 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14880 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14881 row, DEFAULT_FACE_ID);
14882 reseat_at_previous_visible_line_start (&it);
14883
14884 /* If the line start is "too far" away from the window start,
14885 say it takes too much time to compute a new window start. */
14886 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14887 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14888 {
14889 int min_distance, distance;
14890
14891 /* Move forward by display lines to find the new window
14892 start. If window width was enlarged, the new start can
14893 be expected to be > the old start. If window width was
14894 decreased, the new window start will be < the old start.
14895 So, we're looking for the display line start with the
14896 minimum distance from the old window start. */
14897 pos = it.current.pos;
14898 min_distance = INFINITY;
14899 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14900 distance < min_distance)
14901 {
14902 min_distance = distance;
14903 pos = it.current.pos;
14904 move_it_by_lines (&it, 1);
14905 }
14906
14907 /* Set the window start there. */
14908 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14909 window_start_changed_p = 1;
14910 }
14911 }
14912
14913 return window_start_changed_p;
14914 }
14915
14916
14917 /* Try cursor movement in case text has not changed in window WINDOW,
14918 with window start STARTP. Value is
14919
14920 CURSOR_MOVEMENT_SUCCESS if successful
14921
14922 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14923
14924 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14925 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14926 we want to scroll as if scroll-step were set to 1. See the code.
14927
14928 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14929 which case we have to abort this redisplay, and adjust matrices
14930 first. */
14931
14932 enum
14933 {
14934 CURSOR_MOVEMENT_SUCCESS,
14935 CURSOR_MOVEMENT_CANNOT_BE_USED,
14936 CURSOR_MOVEMENT_MUST_SCROLL,
14937 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14938 };
14939
14940 static int
14941 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14942 {
14943 struct window *w = XWINDOW (window);
14944 struct frame *f = XFRAME (w->frame);
14945 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14946
14947 #ifdef GLYPH_DEBUG
14948 if (inhibit_try_cursor_movement)
14949 return rc;
14950 #endif
14951
14952 /* Previously, there was a check for Lisp integer in the
14953 if-statement below. Now, this field is converted to
14954 ptrdiff_t, thus zero means invalid position in a buffer. */
14955 eassert (w->last_point > 0);
14956
14957 /* Handle case where text has not changed, only point, and it has
14958 not moved off the frame. */
14959 if (/* Point may be in this window. */
14960 PT >= CHARPOS (startp)
14961 /* Selective display hasn't changed. */
14962 && !current_buffer->clip_changed
14963 /* Function force-mode-line-update is used to force a thorough
14964 redisplay. It sets either windows_or_buffers_changed or
14965 update_mode_lines. So don't take a shortcut here for these
14966 cases. */
14967 && !update_mode_lines
14968 && !windows_or_buffers_changed
14969 && !cursor_type_changed
14970 /* Can't use this case if highlighting a region. When a
14971 region exists, cursor movement has to do more than just
14972 set the cursor. */
14973 && !(!NILP (Vtransient_mark_mode)
14974 && !NILP (BVAR (current_buffer, mark_active)))
14975 && NILP (w->region_showing)
14976 && NILP (Vshow_trailing_whitespace)
14977 /* This code is not used for mini-buffer for the sake of the case
14978 of redisplaying to replace an echo area message; since in
14979 that case the mini-buffer contents per se are usually
14980 unchanged. This code is of no real use in the mini-buffer
14981 since the handling of this_line_start_pos, etc., in redisplay
14982 handles the same cases. */
14983 && !EQ (window, minibuf_window)
14984 /* When splitting windows or for new windows, it happens that
14985 redisplay is called with a nil window_end_vpos or one being
14986 larger than the window. This should really be fixed in
14987 window.c. I don't have this on my list, now, so we do
14988 approximately the same as the old redisplay code. --gerd. */
14989 && INTEGERP (w->window_end_vpos)
14990 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14991 && (FRAME_WINDOW_P (f)
14992 || !overlay_arrow_in_current_buffer_p ()))
14993 {
14994 int this_scroll_margin, top_scroll_margin;
14995 struct glyph_row *row = NULL;
14996
14997 #ifdef GLYPH_DEBUG
14998 debug_method_add (w, "cursor movement");
14999 #endif
15000
15001 /* Scroll if point within this distance from the top or bottom
15002 of the window. This is a pixel value. */
15003 if (scroll_margin > 0)
15004 {
15005 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15006 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15007 }
15008 else
15009 this_scroll_margin = 0;
15010
15011 top_scroll_margin = this_scroll_margin;
15012 if (WINDOW_WANTS_HEADER_LINE_P (w))
15013 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15014
15015 /* Start with the row the cursor was displayed during the last
15016 not paused redisplay. Give up if that row is not valid. */
15017 if (w->last_cursor.vpos < 0
15018 || w->last_cursor.vpos >= w->current_matrix->nrows)
15019 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15020 else
15021 {
15022 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15023 if (row->mode_line_p)
15024 ++row;
15025 if (!row->enabled_p)
15026 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15027 }
15028
15029 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15030 {
15031 int scroll_p = 0, must_scroll = 0;
15032 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15033
15034 if (PT > w->last_point)
15035 {
15036 /* Point has moved forward. */
15037 while (MATRIX_ROW_END_CHARPOS (row) < PT
15038 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15039 {
15040 eassert (row->enabled_p);
15041 ++row;
15042 }
15043
15044 /* If the end position of a row equals the start
15045 position of the next row, and PT is at that position,
15046 we would rather display cursor in the next line. */
15047 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15048 && MATRIX_ROW_END_CHARPOS (row) == PT
15049 && row < w->current_matrix->rows
15050 + w->current_matrix->nrows - 1
15051 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15052 && !cursor_row_p (row))
15053 ++row;
15054
15055 /* If within the scroll margin, scroll. Note that
15056 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15057 the next line would be drawn, and that
15058 this_scroll_margin can be zero. */
15059 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15060 || PT > MATRIX_ROW_END_CHARPOS (row)
15061 /* Line is completely visible last line in window
15062 and PT is to be set in the next line. */
15063 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15064 && PT == MATRIX_ROW_END_CHARPOS (row)
15065 && !row->ends_at_zv_p
15066 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15067 scroll_p = 1;
15068 }
15069 else if (PT < w->last_point)
15070 {
15071 /* Cursor has to be moved backward. Note that PT >=
15072 CHARPOS (startp) because of the outer if-statement. */
15073 while (!row->mode_line_p
15074 && (MATRIX_ROW_START_CHARPOS (row) > PT
15075 || (MATRIX_ROW_START_CHARPOS (row) == PT
15076 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15077 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15078 row > w->current_matrix->rows
15079 && (row-1)->ends_in_newline_from_string_p))))
15080 && (row->y > top_scroll_margin
15081 || CHARPOS (startp) == BEGV))
15082 {
15083 eassert (row->enabled_p);
15084 --row;
15085 }
15086
15087 /* Consider the following case: Window starts at BEGV,
15088 there is invisible, intangible text at BEGV, so that
15089 display starts at some point START > BEGV. It can
15090 happen that we are called with PT somewhere between
15091 BEGV and START. Try to handle that case. */
15092 if (row < w->current_matrix->rows
15093 || row->mode_line_p)
15094 {
15095 row = w->current_matrix->rows;
15096 if (row->mode_line_p)
15097 ++row;
15098 }
15099
15100 /* Due to newlines in overlay strings, we may have to
15101 skip forward over overlay strings. */
15102 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15103 && MATRIX_ROW_END_CHARPOS (row) == PT
15104 && !cursor_row_p (row))
15105 ++row;
15106
15107 /* If within the scroll margin, scroll. */
15108 if (row->y < top_scroll_margin
15109 && CHARPOS (startp) != BEGV)
15110 scroll_p = 1;
15111 }
15112 else
15113 {
15114 /* Cursor did not move. So don't scroll even if cursor line
15115 is partially visible, as it was so before. */
15116 rc = CURSOR_MOVEMENT_SUCCESS;
15117 }
15118
15119 if (PT < MATRIX_ROW_START_CHARPOS (row)
15120 || PT > MATRIX_ROW_END_CHARPOS (row))
15121 {
15122 /* if PT is not in the glyph row, give up. */
15123 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15124 must_scroll = 1;
15125 }
15126 else if (rc != CURSOR_MOVEMENT_SUCCESS
15127 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15128 {
15129 struct glyph_row *row1;
15130
15131 /* If rows are bidi-reordered and point moved, back up
15132 until we find a row that does not belong to a
15133 continuation line. This is because we must consider
15134 all rows of a continued line as candidates for the
15135 new cursor positioning, since row start and end
15136 positions change non-linearly with vertical position
15137 in such rows. */
15138 /* FIXME: Revisit this when glyph ``spilling'' in
15139 continuation lines' rows is implemented for
15140 bidi-reordered rows. */
15141 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15142 MATRIX_ROW_CONTINUATION_LINE_P (row);
15143 --row)
15144 {
15145 /* If we hit the beginning of the displayed portion
15146 without finding the first row of a continued
15147 line, give up. */
15148 if (row <= row1)
15149 {
15150 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15151 break;
15152 }
15153 eassert (row->enabled_p);
15154 }
15155 }
15156 if (must_scroll)
15157 ;
15158 else if (rc != CURSOR_MOVEMENT_SUCCESS
15159 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15160 /* Make sure this isn't a header line by any chance, since
15161 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15162 && !row->mode_line_p
15163 && make_cursor_line_fully_visible_p)
15164 {
15165 if (PT == MATRIX_ROW_END_CHARPOS (row)
15166 && !row->ends_at_zv_p
15167 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15168 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15169 else if (row->height > window_box_height (w))
15170 {
15171 /* If we end up in a partially visible line, let's
15172 make it fully visible, except when it's taller
15173 than the window, in which case we can't do much
15174 about it. */
15175 *scroll_step = 1;
15176 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15177 }
15178 else
15179 {
15180 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15181 if (!cursor_row_fully_visible_p (w, 0, 1))
15182 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15183 else
15184 rc = CURSOR_MOVEMENT_SUCCESS;
15185 }
15186 }
15187 else if (scroll_p)
15188 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15189 else if (rc != CURSOR_MOVEMENT_SUCCESS
15190 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15191 {
15192 /* With bidi-reordered rows, there could be more than
15193 one candidate row whose start and end positions
15194 occlude point. We need to let set_cursor_from_row
15195 find the best candidate. */
15196 /* FIXME: Revisit this when glyph ``spilling'' in
15197 continuation lines' rows is implemented for
15198 bidi-reordered rows. */
15199 int rv = 0;
15200
15201 do
15202 {
15203 int at_zv_p = 0, exact_match_p = 0;
15204
15205 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15206 && PT <= MATRIX_ROW_END_CHARPOS (row)
15207 && cursor_row_p (row))
15208 rv |= set_cursor_from_row (w, row, w->current_matrix,
15209 0, 0, 0, 0);
15210 /* As soon as we've found the exact match for point,
15211 or the first suitable row whose ends_at_zv_p flag
15212 is set, we are done. */
15213 at_zv_p =
15214 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15215 if (rv && !at_zv_p
15216 && w->cursor.hpos >= 0
15217 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15218 w->cursor.vpos))
15219 {
15220 struct glyph_row *candidate =
15221 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15222 struct glyph *g =
15223 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15224 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15225
15226 exact_match_p =
15227 (BUFFERP (g->object) && g->charpos == PT)
15228 || (INTEGERP (g->object)
15229 && (g->charpos == PT
15230 || (g->charpos == 0 && endpos - 1 == PT)));
15231 }
15232 if (rv && (at_zv_p || exact_match_p))
15233 {
15234 rc = CURSOR_MOVEMENT_SUCCESS;
15235 break;
15236 }
15237 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15238 break;
15239 ++row;
15240 }
15241 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15242 || row->continued_p)
15243 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15244 || (MATRIX_ROW_START_CHARPOS (row) == PT
15245 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15246 /* If we didn't find any candidate rows, or exited the
15247 loop before all the candidates were examined, signal
15248 to the caller that this method failed. */
15249 if (rc != CURSOR_MOVEMENT_SUCCESS
15250 && !(rv
15251 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15252 && !row->continued_p))
15253 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15254 else if (rv)
15255 rc = CURSOR_MOVEMENT_SUCCESS;
15256 }
15257 else
15258 {
15259 do
15260 {
15261 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15262 {
15263 rc = CURSOR_MOVEMENT_SUCCESS;
15264 break;
15265 }
15266 ++row;
15267 }
15268 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15269 && MATRIX_ROW_START_CHARPOS (row) == PT
15270 && cursor_row_p (row));
15271 }
15272 }
15273 }
15274
15275 return rc;
15276 }
15277
15278 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15279 static
15280 #endif
15281 void
15282 set_vertical_scroll_bar (struct window *w)
15283 {
15284 ptrdiff_t start, end, whole;
15285
15286 /* Calculate the start and end positions for the current window.
15287 At some point, it would be nice to choose between scrollbars
15288 which reflect the whole buffer size, with special markers
15289 indicating narrowing, and scrollbars which reflect only the
15290 visible region.
15291
15292 Note that mini-buffers sometimes aren't displaying any text. */
15293 if (!MINI_WINDOW_P (w)
15294 || (w == XWINDOW (minibuf_window)
15295 && NILP (echo_area_buffer[0])))
15296 {
15297 struct buffer *buf = XBUFFER (w->buffer);
15298 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15299 start = marker_position (w->start) - BUF_BEGV (buf);
15300 /* I don't think this is guaranteed to be right. For the
15301 moment, we'll pretend it is. */
15302 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15303
15304 if (end < start)
15305 end = start;
15306 if (whole < (end - start))
15307 whole = end - start;
15308 }
15309 else
15310 start = end = whole = 0;
15311
15312 /* Indicate what this scroll bar ought to be displaying now. */
15313 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15314 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15315 (w, end - start, whole, start);
15316 }
15317
15318
15319 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15320 selected_window is redisplayed.
15321
15322 We can return without actually redisplaying the window if
15323 fonts_changed_p is nonzero. In that case, redisplay_internal will
15324 retry. */
15325
15326 static void
15327 redisplay_window (Lisp_Object window, int just_this_one_p)
15328 {
15329 struct window *w = XWINDOW (window);
15330 struct frame *f = XFRAME (w->frame);
15331 struct buffer *buffer = XBUFFER (w->buffer);
15332 struct buffer *old = current_buffer;
15333 struct text_pos lpoint, opoint, startp;
15334 int update_mode_line;
15335 int tem;
15336 struct it it;
15337 /* Record it now because it's overwritten. */
15338 int current_matrix_up_to_date_p = 0;
15339 int used_current_matrix_p = 0;
15340 /* This is less strict than current_matrix_up_to_date_p.
15341 It indicates that the buffer contents and narrowing are unchanged. */
15342 int buffer_unchanged_p = 0;
15343 int temp_scroll_step = 0;
15344 ptrdiff_t count = SPECPDL_INDEX ();
15345 int rc;
15346 int centering_position = -1;
15347 int last_line_misfit = 0;
15348 ptrdiff_t beg_unchanged, end_unchanged;
15349
15350 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15351 opoint = lpoint;
15352
15353 /* W must be a leaf window here. */
15354 eassert (!NILP (w->buffer));
15355 #ifdef GLYPH_DEBUG
15356 *w->desired_matrix->method = 0;
15357 #endif
15358
15359 restart:
15360 reconsider_clip_changes (w, buffer);
15361
15362 /* Has the mode line to be updated? */
15363 update_mode_line = (w->update_mode_line
15364 || update_mode_lines
15365 || buffer->clip_changed
15366 || buffer->prevent_redisplay_optimizations_p);
15367
15368 if (MINI_WINDOW_P (w))
15369 {
15370 if (w == XWINDOW (echo_area_window)
15371 && !NILP (echo_area_buffer[0]))
15372 {
15373 if (update_mode_line)
15374 /* We may have to update a tty frame's menu bar or a
15375 tool-bar. Example `M-x C-h C-h C-g'. */
15376 goto finish_menu_bars;
15377 else
15378 /* We've already displayed the echo area glyphs in this window. */
15379 goto finish_scroll_bars;
15380 }
15381 else if ((w != XWINDOW (minibuf_window)
15382 || minibuf_level == 0)
15383 /* When buffer is nonempty, redisplay window normally. */
15384 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15385 /* Quail displays non-mini buffers in minibuffer window.
15386 In that case, redisplay the window normally. */
15387 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15388 {
15389 /* W is a mini-buffer window, but it's not active, so clear
15390 it. */
15391 int yb = window_text_bottom_y (w);
15392 struct glyph_row *row;
15393 int y;
15394
15395 for (y = 0, row = w->desired_matrix->rows;
15396 y < yb;
15397 y += row->height, ++row)
15398 blank_row (w, row, y);
15399 goto finish_scroll_bars;
15400 }
15401
15402 clear_glyph_matrix (w->desired_matrix);
15403 }
15404
15405 /* Otherwise set up data on this window; select its buffer and point
15406 value. */
15407 /* Really select the buffer, for the sake of buffer-local
15408 variables. */
15409 set_buffer_internal_1 (XBUFFER (w->buffer));
15410
15411 current_matrix_up_to_date_p
15412 = (!NILP (w->window_end_valid)
15413 && !current_buffer->clip_changed
15414 && !current_buffer->prevent_redisplay_optimizations_p
15415 && w->last_modified >= MODIFF
15416 && w->last_overlay_modified >= OVERLAY_MODIFF);
15417
15418 /* Run the window-bottom-change-functions
15419 if it is possible that the text on the screen has changed
15420 (either due to modification of the text, or any other reason). */
15421 if (!current_matrix_up_to_date_p
15422 && !NILP (Vwindow_text_change_functions))
15423 {
15424 safe_run_hooks (Qwindow_text_change_functions);
15425 goto restart;
15426 }
15427
15428 beg_unchanged = BEG_UNCHANGED;
15429 end_unchanged = END_UNCHANGED;
15430
15431 SET_TEXT_POS (opoint, PT, PT_BYTE);
15432
15433 specbind (Qinhibit_point_motion_hooks, Qt);
15434
15435 buffer_unchanged_p
15436 = (!NILP (w->window_end_valid)
15437 && !current_buffer->clip_changed
15438 && w->last_modified >= MODIFF
15439 && w->last_overlay_modified >= OVERLAY_MODIFF);
15440
15441 /* When windows_or_buffers_changed is non-zero, we can't rely on
15442 the window end being valid, so set it to nil there. */
15443 if (windows_or_buffers_changed)
15444 {
15445 /* If window starts on a continuation line, maybe adjust the
15446 window start in case the window's width changed. */
15447 if (XMARKER (w->start)->buffer == current_buffer)
15448 compute_window_start_on_continuation_line (w);
15449
15450 w->window_end_valid = Qnil;
15451 }
15452
15453 /* Some sanity checks. */
15454 CHECK_WINDOW_END (w);
15455 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15456 abort ();
15457 if (BYTEPOS (opoint) < CHARPOS (opoint))
15458 abort ();
15459
15460 /* If %c is in mode line, update it if needed. */
15461 if (!NILP (w->column_number_displayed)
15462 /* This alternative quickly identifies a common case
15463 where no change is needed. */
15464 && !(PT == w->last_point
15465 && w->last_modified >= MODIFF
15466 && w->last_overlay_modified >= OVERLAY_MODIFF)
15467 && (XFASTINT (w->column_number_displayed) != current_column ()))
15468 update_mode_line = 1;
15469
15470 /* Count number of windows showing the selected buffer. An indirect
15471 buffer counts as its base buffer. */
15472 if (!just_this_one_p)
15473 {
15474 struct buffer *current_base, *window_base;
15475 current_base = current_buffer;
15476 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15477 if (current_base->base_buffer)
15478 current_base = current_base->base_buffer;
15479 if (window_base->base_buffer)
15480 window_base = window_base->base_buffer;
15481 if (current_base == window_base)
15482 buffer_shared++;
15483 }
15484
15485 /* Point refers normally to the selected window. For any other
15486 window, set up appropriate value. */
15487 if (!EQ (window, selected_window))
15488 {
15489 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15490 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15491 if (new_pt < BEGV)
15492 {
15493 new_pt = BEGV;
15494 new_pt_byte = BEGV_BYTE;
15495 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15496 }
15497 else if (new_pt > (ZV - 1))
15498 {
15499 new_pt = ZV;
15500 new_pt_byte = ZV_BYTE;
15501 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15502 }
15503
15504 /* We don't use SET_PT so that the point-motion hooks don't run. */
15505 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15506 }
15507
15508 /* If any of the character widths specified in the display table
15509 have changed, invalidate the width run cache. It's true that
15510 this may be a bit late to catch such changes, but the rest of
15511 redisplay goes (non-fatally) haywire when the display table is
15512 changed, so why should we worry about doing any better? */
15513 if (current_buffer->width_run_cache)
15514 {
15515 struct Lisp_Char_Table *disptab = buffer_display_table ();
15516
15517 if (! disptab_matches_widthtab (disptab,
15518 XVECTOR (BVAR (current_buffer, width_table))))
15519 {
15520 invalidate_region_cache (current_buffer,
15521 current_buffer->width_run_cache,
15522 BEG, Z);
15523 recompute_width_table (current_buffer, disptab);
15524 }
15525 }
15526
15527 /* If window-start is screwed up, choose a new one. */
15528 if (XMARKER (w->start)->buffer != current_buffer)
15529 goto recenter;
15530
15531 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15532
15533 /* If someone specified a new starting point but did not insist,
15534 check whether it can be used. */
15535 if (w->optional_new_start
15536 && CHARPOS (startp) >= BEGV
15537 && CHARPOS (startp) <= ZV)
15538 {
15539 w->optional_new_start = 0;
15540 start_display (&it, w, startp);
15541 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15542 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15543 if (IT_CHARPOS (it) == PT)
15544 w->force_start = 1;
15545 /* IT may overshoot PT if text at PT is invisible. */
15546 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15547 w->force_start = 1;
15548 }
15549
15550 force_start:
15551
15552 /* Handle case where place to start displaying has been specified,
15553 unless the specified location is outside the accessible range. */
15554 if (w->force_start || w->frozen_window_start_p)
15555 {
15556 /* We set this later on if we have to adjust point. */
15557 int new_vpos = -1;
15558
15559 w->force_start = 0;
15560 w->vscroll = 0;
15561 w->window_end_valid = Qnil;
15562
15563 /* Forget any recorded base line for line number display. */
15564 if (!buffer_unchanged_p)
15565 w->base_line_number = Qnil;
15566
15567 /* Redisplay the mode line. Select the buffer properly for that.
15568 Also, run the hook window-scroll-functions
15569 because we have scrolled. */
15570 /* Note, we do this after clearing force_start because
15571 if there's an error, it is better to forget about force_start
15572 than to get into an infinite loop calling the hook functions
15573 and having them get more errors. */
15574 if (!update_mode_line
15575 || ! NILP (Vwindow_scroll_functions))
15576 {
15577 update_mode_line = 1;
15578 w->update_mode_line = 1;
15579 startp = run_window_scroll_functions (window, startp);
15580 }
15581
15582 w->last_modified = 0;
15583 w->last_overlay_modified = 0;
15584 if (CHARPOS (startp) < BEGV)
15585 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15586 else if (CHARPOS (startp) > ZV)
15587 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15588
15589 /* Redisplay, then check if cursor has been set during the
15590 redisplay. Give up if new fonts were loaded. */
15591 /* We used to issue a CHECK_MARGINS argument to try_window here,
15592 but this causes scrolling to fail when point begins inside
15593 the scroll margin (bug#148) -- cyd */
15594 if (!try_window (window, startp, 0))
15595 {
15596 w->force_start = 1;
15597 clear_glyph_matrix (w->desired_matrix);
15598 goto need_larger_matrices;
15599 }
15600
15601 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15602 {
15603 /* If point does not appear, try to move point so it does
15604 appear. The desired matrix has been built above, so we
15605 can use it here. */
15606 new_vpos = window_box_height (w) / 2;
15607 }
15608
15609 if (!cursor_row_fully_visible_p (w, 0, 0))
15610 {
15611 /* Point does appear, but on a line partly visible at end of window.
15612 Move it back to a fully-visible line. */
15613 new_vpos = window_box_height (w);
15614 }
15615
15616 /* If we need to move point for either of the above reasons,
15617 now actually do it. */
15618 if (new_vpos >= 0)
15619 {
15620 struct glyph_row *row;
15621
15622 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15623 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15624 ++row;
15625
15626 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15627 MATRIX_ROW_START_BYTEPOS (row));
15628
15629 if (w != XWINDOW (selected_window))
15630 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15631 else if (current_buffer == old)
15632 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15633
15634 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15635
15636 /* If we are highlighting the region, then we just changed
15637 the region, so redisplay to show it. */
15638 if (!NILP (Vtransient_mark_mode)
15639 && !NILP (BVAR (current_buffer, mark_active)))
15640 {
15641 clear_glyph_matrix (w->desired_matrix);
15642 if (!try_window (window, startp, 0))
15643 goto need_larger_matrices;
15644 }
15645 }
15646
15647 #ifdef GLYPH_DEBUG
15648 debug_method_add (w, "forced window start");
15649 #endif
15650 goto done;
15651 }
15652
15653 /* Handle case where text has not changed, only point, and it has
15654 not moved off the frame, and we are not retrying after hscroll.
15655 (current_matrix_up_to_date_p is nonzero when retrying.) */
15656 if (current_matrix_up_to_date_p
15657 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15658 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15659 {
15660 switch (rc)
15661 {
15662 case CURSOR_MOVEMENT_SUCCESS:
15663 used_current_matrix_p = 1;
15664 goto done;
15665
15666 case CURSOR_MOVEMENT_MUST_SCROLL:
15667 goto try_to_scroll;
15668
15669 default:
15670 abort ();
15671 }
15672 }
15673 /* If current starting point was originally the beginning of a line
15674 but no longer is, find a new starting point. */
15675 else if (w->start_at_line_beg
15676 && !(CHARPOS (startp) <= BEGV
15677 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15678 {
15679 #ifdef GLYPH_DEBUG
15680 debug_method_add (w, "recenter 1");
15681 #endif
15682 goto recenter;
15683 }
15684
15685 /* Try scrolling with try_window_id. Value is > 0 if update has
15686 been done, it is -1 if we know that the same window start will
15687 not work. It is 0 if unsuccessful for some other reason. */
15688 else if ((tem = try_window_id (w)) != 0)
15689 {
15690 #ifdef GLYPH_DEBUG
15691 debug_method_add (w, "try_window_id %d", tem);
15692 #endif
15693
15694 if (fonts_changed_p)
15695 goto need_larger_matrices;
15696 if (tem > 0)
15697 goto done;
15698
15699 /* Otherwise try_window_id has returned -1 which means that we
15700 don't want the alternative below this comment to execute. */
15701 }
15702 else if (CHARPOS (startp) >= BEGV
15703 && CHARPOS (startp) <= ZV
15704 && PT >= CHARPOS (startp)
15705 && (CHARPOS (startp) < ZV
15706 /* Avoid starting at end of buffer. */
15707 || CHARPOS (startp) == BEGV
15708 || (w->last_modified >= MODIFF
15709 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15710 {
15711 int d1, d2, d3, d4, d5, d6;
15712
15713 /* If first window line is a continuation line, and window start
15714 is inside the modified region, but the first change is before
15715 current window start, we must select a new window start.
15716
15717 However, if this is the result of a down-mouse event (e.g. by
15718 extending the mouse-drag-overlay), we don't want to select a
15719 new window start, since that would change the position under
15720 the mouse, resulting in an unwanted mouse-movement rather
15721 than a simple mouse-click. */
15722 if (!w->start_at_line_beg
15723 && NILP (do_mouse_tracking)
15724 && CHARPOS (startp) > BEGV
15725 && CHARPOS (startp) > BEG + beg_unchanged
15726 && CHARPOS (startp) <= Z - end_unchanged
15727 /* Even if w->start_at_line_beg is nil, a new window may
15728 start at a line_beg, since that's how set_buffer_window
15729 sets it. So, we need to check the return value of
15730 compute_window_start_on_continuation_line. (See also
15731 bug#197). */
15732 && XMARKER (w->start)->buffer == current_buffer
15733 && compute_window_start_on_continuation_line (w)
15734 /* It doesn't make sense to force the window start like we
15735 do at label force_start if it is already known that point
15736 will not be visible in the resulting window, because
15737 doing so will move point from its correct position
15738 instead of scrolling the window to bring point into view.
15739 See bug#9324. */
15740 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15741 {
15742 w->force_start = 1;
15743 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15744 goto force_start;
15745 }
15746
15747 #ifdef GLYPH_DEBUG
15748 debug_method_add (w, "same window start");
15749 #endif
15750
15751 /* Try to redisplay starting at same place as before.
15752 If point has not moved off frame, accept the results. */
15753 if (!current_matrix_up_to_date_p
15754 /* Don't use try_window_reusing_current_matrix in this case
15755 because a window scroll function can have changed the
15756 buffer. */
15757 || !NILP (Vwindow_scroll_functions)
15758 || MINI_WINDOW_P (w)
15759 || !(used_current_matrix_p
15760 = try_window_reusing_current_matrix (w)))
15761 {
15762 IF_DEBUG (debug_method_add (w, "1"));
15763 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15764 /* -1 means we need to scroll.
15765 0 means we need new matrices, but fonts_changed_p
15766 is set in that case, so we will detect it below. */
15767 goto try_to_scroll;
15768 }
15769
15770 if (fonts_changed_p)
15771 goto need_larger_matrices;
15772
15773 if (w->cursor.vpos >= 0)
15774 {
15775 if (!just_this_one_p
15776 || current_buffer->clip_changed
15777 || BEG_UNCHANGED < CHARPOS (startp))
15778 /* Forget any recorded base line for line number display. */
15779 w->base_line_number = Qnil;
15780
15781 if (!cursor_row_fully_visible_p (w, 1, 0))
15782 {
15783 clear_glyph_matrix (w->desired_matrix);
15784 last_line_misfit = 1;
15785 }
15786 /* Drop through and scroll. */
15787 else
15788 goto done;
15789 }
15790 else
15791 clear_glyph_matrix (w->desired_matrix);
15792 }
15793
15794 try_to_scroll:
15795
15796 w->last_modified = 0;
15797 w->last_overlay_modified = 0;
15798
15799 /* Redisplay the mode line. Select the buffer properly for that. */
15800 if (!update_mode_line)
15801 {
15802 update_mode_line = 1;
15803 w->update_mode_line = 1;
15804 }
15805
15806 /* Try to scroll by specified few lines. */
15807 if ((scroll_conservatively
15808 || emacs_scroll_step
15809 || temp_scroll_step
15810 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15811 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15812 && CHARPOS (startp) >= BEGV
15813 && CHARPOS (startp) <= ZV)
15814 {
15815 /* The function returns -1 if new fonts were loaded, 1 if
15816 successful, 0 if not successful. */
15817 int ss = try_scrolling (window, just_this_one_p,
15818 scroll_conservatively,
15819 emacs_scroll_step,
15820 temp_scroll_step, last_line_misfit);
15821 switch (ss)
15822 {
15823 case SCROLLING_SUCCESS:
15824 goto done;
15825
15826 case SCROLLING_NEED_LARGER_MATRICES:
15827 goto need_larger_matrices;
15828
15829 case SCROLLING_FAILED:
15830 break;
15831
15832 default:
15833 abort ();
15834 }
15835 }
15836
15837 /* Finally, just choose a place to start which positions point
15838 according to user preferences. */
15839
15840 recenter:
15841
15842 #ifdef GLYPH_DEBUG
15843 debug_method_add (w, "recenter");
15844 #endif
15845
15846 /* w->vscroll = 0; */
15847
15848 /* Forget any previously recorded base line for line number display. */
15849 if (!buffer_unchanged_p)
15850 w->base_line_number = Qnil;
15851
15852 /* Determine the window start relative to point. */
15853 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15854 it.current_y = it.last_visible_y;
15855 if (centering_position < 0)
15856 {
15857 int margin =
15858 scroll_margin > 0
15859 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15860 : 0;
15861 ptrdiff_t margin_pos = CHARPOS (startp);
15862 Lisp_Object aggressive;
15863 int scrolling_up;
15864
15865 /* If there is a scroll margin at the top of the window, find
15866 its character position. */
15867 if (margin
15868 /* Cannot call start_display if startp is not in the
15869 accessible region of the buffer. This can happen when we
15870 have just switched to a different buffer and/or changed
15871 its restriction. In that case, startp is initialized to
15872 the character position 1 (BEGV) because we did not yet
15873 have chance to display the buffer even once. */
15874 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15875 {
15876 struct it it1;
15877 void *it1data = NULL;
15878
15879 SAVE_IT (it1, it, it1data);
15880 start_display (&it1, w, startp);
15881 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15882 margin_pos = IT_CHARPOS (it1);
15883 RESTORE_IT (&it, &it, it1data);
15884 }
15885 scrolling_up = PT > margin_pos;
15886 aggressive =
15887 scrolling_up
15888 ? BVAR (current_buffer, scroll_up_aggressively)
15889 : BVAR (current_buffer, scroll_down_aggressively);
15890
15891 if (!MINI_WINDOW_P (w)
15892 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15893 {
15894 int pt_offset = 0;
15895
15896 /* Setting scroll-conservatively overrides
15897 scroll-*-aggressively. */
15898 if (!scroll_conservatively && NUMBERP (aggressive))
15899 {
15900 double float_amount = XFLOATINT (aggressive);
15901
15902 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15903 if (pt_offset == 0 && float_amount > 0)
15904 pt_offset = 1;
15905 if (pt_offset && margin > 0)
15906 margin -= 1;
15907 }
15908 /* Compute how much to move the window start backward from
15909 point so that point will be displayed where the user
15910 wants it. */
15911 if (scrolling_up)
15912 {
15913 centering_position = it.last_visible_y;
15914 if (pt_offset)
15915 centering_position -= pt_offset;
15916 centering_position -=
15917 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15918 + WINDOW_HEADER_LINE_HEIGHT (w);
15919 /* Don't let point enter the scroll margin near top of
15920 the window. */
15921 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15922 centering_position = margin * FRAME_LINE_HEIGHT (f);
15923 }
15924 else
15925 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15926 }
15927 else
15928 /* Set the window start half the height of the window backward
15929 from point. */
15930 centering_position = window_box_height (w) / 2;
15931 }
15932 move_it_vertically_backward (&it, centering_position);
15933
15934 eassert (IT_CHARPOS (it) >= BEGV);
15935
15936 /* The function move_it_vertically_backward may move over more
15937 than the specified y-distance. If it->w is small, e.g. a
15938 mini-buffer window, we may end up in front of the window's
15939 display area. Start displaying at the start of the line
15940 containing PT in this case. */
15941 if (it.current_y <= 0)
15942 {
15943 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15944 move_it_vertically_backward (&it, 0);
15945 it.current_y = 0;
15946 }
15947
15948 it.current_x = it.hpos = 0;
15949
15950 /* Set the window start position here explicitly, to avoid an
15951 infinite loop in case the functions in window-scroll-functions
15952 get errors. */
15953 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15954
15955 /* Run scroll hooks. */
15956 startp = run_window_scroll_functions (window, it.current.pos);
15957
15958 /* Redisplay the window. */
15959 if (!current_matrix_up_to_date_p
15960 || windows_or_buffers_changed
15961 || cursor_type_changed
15962 /* Don't use try_window_reusing_current_matrix in this case
15963 because it can have changed the buffer. */
15964 || !NILP (Vwindow_scroll_functions)
15965 || !just_this_one_p
15966 || MINI_WINDOW_P (w)
15967 || !(used_current_matrix_p
15968 = try_window_reusing_current_matrix (w)))
15969 try_window (window, startp, 0);
15970
15971 /* If new fonts have been loaded (due to fontsets), give up. We
15972 have to start a new redisplay since we need to re-adjust glyph
15973 matrices. */
15974 if (fonts_changed_p)
15975 goto need_larger_matrices;
15976
15977 /* If cursor did not appear assume that the middle of the window is
15978 in the first line of the window. Do it again with the next line.
15979 (Imagine a window of height 100, displaying two lines of height
15980 60. Moving back 50 from it->last_visible_y will end in the first
15981 line.) */
15982 if (w->cursor.vpos < 0)
15983 {
15984 if (!NILP (w->window_end_valid)
15985 && PT >= Z - XFASTINT (w->window_end_pos))
15986 {
15987 clear_glyph_matrix (w->desired_matrix);
15988 move_it_by_lines (&it, 1);
15989 try_window (window, it.current.pos, 0);
15990 }
15991 else if (PT < IT_CHARPOS (it))
15992 {
15993 clear_glyph_matrix (w->desired_matrix);
15994 move_it_by_lines (&it, -1);
15995 try_window (window, it.current.pos, 0);
15996 }
15997 else
15998 {
15999 /* Not much we can do about it. */
16000 }
16001 }
16002
16003 /* Consider the following case: Window starts at BEGV, there is
16004 invisible, intangible text at BEGV, so that display starts at
16005 some point START > BEGV. It can happen that we are called with
16006 PT somewhere between BEGV and START. Try to handle that case. */
16007 if (w->cursor.vpos < 0)
16008 {
16009 struct glyph_row *row = w->current_matrix->rows;
16010 if (row->mode_line_p)
16011 ++row;
16012 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16013 }
16014
16015 if (!cursor_row_fully_visible_p (w, 0, 0))
16016 {
16017 /* If vscroll is enabled, disable it and try again. */
16018 if (w->vscroll)
16019 {
16020 w->vscroll = 0;
16021 clear_glyph_matrix (w->desired_matrix);
16022 goto recenter;
16023 }
16024
16025 /* Users who set scroll-conservatively to a large number want
16026 point just above/below the scroll margin. If we ended up
16027 with point's row partially visible, move the window start to
16028 make that row fully visible and out of the margin. */
16029 if (scroll_conservatively > SCROLL_LIMIT)
16030 {
16031 int margin =
16032 scroll_margin > 0
16033 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16034 : 0;
16035 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16036
16037 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16038 clear_glyph_matrix (w->desired_matrix);
16039 if (1 == try_window (window, it.current.pos,
16040 TRY_WINDOW_CHECK_MARGINS))
16041 goto done;
16042 }
16043
16044 /* If centering point failed to make the whole line visible,
16045 put point at the top instead. That has to make the whole line
16046 visible, if it can be done. */
16047 if (centering_position == 0)
16048 goto done;
16049
16050 clear_glyph_matrix (w->desired_matrix);
16051 centering_position = 0;
16052 goto recenter;
16053 }
16054
16055 done:
16056
16057 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16058 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16059 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16060
16061 /* Display the mode line, if we must. */
16062 if ((update_mode_line
16063 /* If window not full width, must redo its mode line
16064 if (a) the window to its side is being redone and
16065 (b) we do a frame-based redisplay. This is a consequence
16066 of how inverted lines are drawn in frame-based redisplay. */
16067 || (!just_this_one_p
16068 && !FRAME_WINDOW_P (f)
16069 && !WINDOW_FULL_WIDTH_P (w))
16070 /* Line number to display. */
16071 || INTEGERP (w->base_line_pos)
16072 /* Column number is displayed and different from the one displayed. */
16073 || (!NILP (w->column_number_displayed)
16074 && (XFASTINT (w->column_number_displayed) != current_column ())))
16075 /* This means that the window has a mode line. */
16076 && (WINDOW_WANTS_MODELINE_P (w)
16077 || WINDOW_WANTS_HEADER_LINE_P (w)))
16078 {
16079 display_mode_lines (w);
16080
16081 /* If mode line height has changed, arrange for a thorough
16082 immediate redisplay using the correct mode line height. */
16083 if (WINDOW_WANTS_MODELINE_P (w)
16084 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16085 {
16086 fonts_changed_p = 1;
16087 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16088 = DESIRED_MODE_LINE_HEIGHT (w);
16089 }
16090
16091 /* If header line height has changed, arrange for a thorough
16092 immediate redisplay using the correct header line height. */
16093 if (WINDOW_WANTS_HEADER_LINE_P (w)
16094 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16095 {
16096 fonts_changed_p = 1;
16097 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16098 = DESIRED_HEADER_LINE_HEIGHT (w);
16099 }
16100
16101 if (fonts_changed_p)
16102 goto need_larger_matrices;
16103 }
16104
16105 if (!line_number_displayed
16106 && !BUFFERP (w->base_line_pos))
16107 {
16108 w->base_line_pos = Qnil;
16109 w->base_line_number = Qnil;
16110 }
16111
16112 finish_menu_bars:
16113
16114 /* When we reach a frame's selected window, redo the frame's menu bar. */
16115 if (update_mode_line
16116 && EQ (FRAME_SELECTED_WINDOW (f), window))
16117 {
16118 int redisplay_menu_p = 0;
16119
16120 if (FRAME_WINDOW_P (f))
16121 {
16122 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16123 || defined (HAVE_NS) || defined (USE_GTK)
16124 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16125 #else
16126 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16127 #endif
16128 }
16129 else
16130 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16131
16132 if (redisplay_menu_p)
16133 display_menu_bar (w);
16134
16135 #ifdef HAVE_WINDOW_SYSTEM
16136 if (FRAME_WINDOW_P (f))
16137 {
16138 #if defined (USE_GTK) || defined (HAVE_NS)
16139 if (FRAME_EXTERNAL_TOOL_BAR (f))
16140 redisplay_tool_bar (f);
16141 #else
16142 if (WINDOWP (f->tool_bar_window)
16143 && (FRAME_TOOL_BAR_LINES (f) > 0
16144 || !NILP (Vauto_resize_tool_bars))
16145 && redisplay_tool_bar (f))
16146 ignore_mouse_drag_p = 1;
16147 #endif
16148 }
16149 #endif
16150 }
16151
16152 #ifdef HAVE_WINDOW_SYSTEM
16153 if (FRAME_WINDOW_P (f)
16154 && update_window_fringes (w, (just_this_one_p
16155 || (!used_current_matrix_p && !overlay_arrow_seen)
16156 || w->pseudo_window_p)))
16157 {
16158 update_begin (f);
16159 BLOCK_INPUT;
16160 if (draw_window_fringes (w, 1))
16161 x_draw_vertical_border (w);
16162 UNBLOCK_INPUT;
16163 update_end (f);
16164 }
16165 #endif /* HAVE_WINDOW_SYSTEM */
16166
16167 /* We go to this label, with fonts_changed_p nonzero,
16168 if it is necessary to try again using larger glyph matrices.
16169 We have to redeem the scroll bar even in this case,
16170 because the loop in redisplay_internal expects that. */
16171 need_larger_matrices:
16172 ;
16173 finish_scroll_bars:
16174
16175 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16176 {
16177 /* Set the thumb's position and size. */
16178 set_vertical_scroll_bar (w);
16179
16180 /* Note that we actually used the scroll bar attached to this
16181 window, so it shouldn't be deleted at the end of redisplay. */
16182 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16183 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16184 }
16185
16186 /* Restore current_buffer and value of point in it. The window
16187 update may have changed the buffer, so first make sure `opoint'
16188 is still valid (Bug#6177). */
16189 if (CHARPOS (opoint) < BEGV)
16190 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16191 else if (CHARPOS (opoint) > ZV)
16192 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16193 else
16194 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16195
16196 set_buffer_internal_1 (old);
16197 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16198 shorter. This can be caused by log truncation in *Messages*. */
16199 if (CHARPOS (lpoint) <= ZV)
16200 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16201
16202 unbind_to (count, Qnil);
16203 }
16204
16205
16206 /* Build the complete desired matrix of WINDOW with a window start
16207 buffer position POS.
16208
16209 Value is 1 if successful. It is zero if fonts were loaded during
16210 redisplay which makes re-adjusting glyph matrices necessary, and -1
16211 if point would appear in the scroll margins.
16212 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16213 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16214 set in FLAGS.) */
16215
16216 int
16217 try_window (Lisp_Object window, struct text_pos pos, int flags)
16218 {
16219 struct window *w = XWINDOW (window);
16220 struct it it;
16221 struct glyph_row *last_text_row = NULL;
16222 struct frame *f = XFRAME (w->frame);
16223
16224 /* Make POS the new window start. */
16225 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16226
16227 /* Mark cursor position as unknown. No overlay arrow seen. */
16228 w->cursor.vpos = -1;
16229 overlay_arrow_seen = 0;
16230
16231 /* Initialize iterator and info to start at POS. */
16232 start_display (&it, w, pos);
16233
16234 /* Display all lines of W. */
16235 while (it.current_y < it.last_visible_y)
16236 {
16237 if (display_line (&it))
16238 last_text_row = it.glyph_row - 1;
16239 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16240 return 0;
16241 }
16242
16243 /* Don't let the cursor end in the scroll margins. */
16244 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16245 && !MINI_WINDOW_P (w))
16246 {
16247 int this_scroll_margin;
16248
16249 if (scroll_margin > 0)
16250 {
16251 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16252 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16253 }
16254 else
16255 this_scroll_margin = 0;
16256
16257 if ((w->cursor.y >= 0 /* not vscrolled */
16258 && w->cursor.y < this_scroll_margin
16259 && CHARPOS (pos) > BEGV
16260 && IT_CHARPOS (it) < ZV)
16261 /* rms: considering make_cursor_line_fully_visible_p here
16262 seems to give wrong results. We don't want to recenter
16263 when the last line is partly visible, we want to allow
16264 that case to be handled in the usual way. */
16265 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16266 {
16267 w->cursor.vpos = -1;
16268 clear_glyph_matrix (w->desired_matrix);
16269 return -1;
16270 }
16271 }
16272
16273 /* If bottom moved off end of frame, change mode line percentage. */
16274 if (XFASTINT (w->window_end_pos) <= 0
16275 && Z != IT_CHARPOS (it))
16276 w->update_mode_line = 1;
16277
16278 /* Set window_end_pos to the offset of the last character displayed
16279 on the window from the end of current_buffer. Set
16280 window_end_vpos to its row number. */
16281 if (last_text_row)
16282 {
16283 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16284 w->window_end_bytepos
16285 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16286 w->window_end_pos
16287 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16288 w->window_end_vpos
16289 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16290 eassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16291 ->displays_text_p);
16292 }
16293 else
16294 {
16295 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16296 w->window_end_pos = make_number (Z - ZV);
16297 w->window_end_vpos = make_number (0);
16298 }
16299
16300 /* But that is not valid info until redisplay finishes. */
16301 w->window_end_valid = Qnil;
16302 return 1;
16303 }
16304
16305
16306 \f
16307 /************************************************************************
16308 Window redisplay reusing current matrix when buffer has not changed
16309 ************************************************************************/
16310
16311 /* Try redisplay of window W showing an unchanged buffer with a
16312 different window start than the last time it was displayed by
16313 reusing its current matrix. Value is non-zero if successful.
16314 W->start is the new window start. */
16315
16316 static int
16317 try_window_reusing_current_matrix (struct window *w)
16318 {
16319 struct frame *f = XFRAME (w->frame);
16320 struct glyph_row *bottom_row;
16321 struct it it;
16322 struct run run;
16323 struct text_pos start, new_start;
16324 int nrows_scrolled, i;
16325 struct glyph_row *last_text_row;
16326 struct glyph_row *last_reused_text_row;
16327 struct glyph_row *start_row;
16328 int start_vpos, min_y, max_y;
16329
16330 #ifdef GLYPH_DEBUG
16331 if (inhibit_try_window_reusing)
16332 return 0;
16333 #endif
16334
16335 if (/* This function doesn't handle terminal frames. */
16336 !FRAME_WINDOW_P (f)
16337 /* Don't try to reuse the display if windows have been split
16338 or such. */
16339 || windows_or_buffers_changed
16340 || cursor_type_changed)
16341 return 0;
16342
16343 /* Can't do this if region may have changed. */
16344 if ((!NILP (Vtransient_mark_mode)
16345 && !NILP (BVAR (current_buffer, mark_active)))
16346 || !NILP (w->region_showing)
16347 || !NILP (Vshow_trailing_whitespace))
16348 return 0;
16349
16350 /* If top-line visibility has changed, give up. */
16351 if (WINDOW_WANTS_HEADER_LINE_P (w)
16352 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16353 return 0;
16354
16355 /* Give up if old or new display is scrolled vertically. We could
16356 make this function handle this, but right now it doesn't. */
16357 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16358 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16359 return 0;
16360
16361 /* The variable new_start now holds the new window start. The old
16362 start `start' can be determined from the current matrix. */
16363 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16364 start = start_row->minpos;
16365 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16366
16367 /* Clear the desired matrix for the display below. */
16368 clear_glyph_matrix (w->desired_matrix);
16369
16370 if (CHARPOS (new_start) <= CHARPOS (start))
16371 {
16372 /* Don't use this method if the display starts with an ellipsis
16373 displayed for invisible text. It's not easy to handle that case
16374 below, and it's certainly not worth the effort since this is
16375 not a frequent case. */
16376 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16377 return 0;
16378
16379 IF_DEBUG (debug_method_add (w, "twu1"));
16380
16381 /* Display up to a row that can be reused. The variable
16382 last_text_row is set to the last row displayed that displays
16383 text. Note that it.vpos == 0 if or if not there is a
16384 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16385 start_display (&it, w, new_start);
16386 w->cursor.vpos = -1;
16387 last_text_row = last_reused_text_row = NULL;
16388
16389 while (it.current_y < it.last_visible_y
16390 && !fonts_changed_p)
16391 {
16392 /* If we have reached into the characters in the START row,
16393 that means the line boundaries have changed. So we
16394 can't start copying with the row START. Maybe it will
16395 work to start copying with the following row. */
16396 while (IT_CHARPOS (it) > CHARPOS (start))
16397 {
16398 /* Advance to the next row as the "start". */
16399 start_row++;
16400 start = start_row->minpos;
16401 /* If there are no more rows to try, or just one, give up. */
16402 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16403 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16404 || CHARPOS (start) == ZV)
16405 {
16406 clear_glyph_matrix (w->desired_matrix);
16407 return 0;
16408 }
16409
16410 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16411 }
16412 /* If we have reached alignment, we can copy the rest of the
16413 rows. */
16414 if (IT_CHARPOS (it) == CHARPOS (start)
16415 /* Don't accept "alignment" inside a display vector,
16416 since start_row could have started in the middle of
16417 that same display vector (thus their character
16418 positions match), and we have no way of telling if
16419 that is the case. */
16420 && it.current.dpvec_index < 0)
16421 break;
16422
16423 if (display_line (&it))
16424 last_text_row = it.glyph_row - 1;
16425
16426 }
16427
16428 /* A value of current_y < last_visible_y means that we stopped
16429 at the previous window start, which in turn means that we
16430 have at least one reusable row. */
16431 if (it.current_y < it.last_visible_y)
16432 {
16433 struct glyph_row *row;
16434
16435 /* IT.vpos always starts from 0; it counts text lines. */
16436 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16437
16438 /* Find PT if not already found in the lines displayed. */
16439 if (w->cursor.vpos < 0)
16440 {
16441 int dy = it.current_y - start_row->y;
16442
16443 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16444 row = row_containing_pos (w, PT, row, NULL, dy);
16445 if (row)
16446 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16447 dy, nrows_scrolled);
16448 else
16449 {
16450 clear_glyph_matrix (w->desired_matrix);
16451 return 0;
16452 }
16453 }
16454
16455 /* Scroll the display. Do it before the current matrix is
16456 changed. The problem here is that update has not yet
16457 run, i.e. part of the current matrix is not up to date.
16458 scroll_run_hook will clear the cursor, and use the
16459 current matrix to get the height of the row the cursor is
16460 in. */
16461 run.current_y = start_row->y;
16462 run.desired_y = it.current_y;
16463 run.height = it.last_visible_y - it.current_y;
16464
16465 if (run.height > 0 && run.current_y != run.desired_y)
16466 {
16467 update_begin (f);
16468 FRAME_RIF (f)->update_window_begin_hook (w);
16469 FRAME_RIF (f)->clear_window_mouse_face (w);
16470 FRAME_RIF (f)->scroll_run_hook (w, &run);
16471 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16472 update_end (f);
16473 }
16474
16475 /* Shift current matrix down by nrows_scrolled lines. */
16476 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16477 rotate_matrix (w->current_matrix,
16478 start_vpos,
16479 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16480 nrows_scrolled);
16481
16482 /* Disable lines that must be updated. */
16483 for (i = 0; i < nrows_scrolled; ++i)
16484 (start_row + i)->enabled_p = 0;
16485
16486 /* Re-compute Y positions. */
16487 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16488 max_y = it.last_visible_y;
16489 for (row = start_row + nrows_scrolled;
16490 row < bottom_row;
16491 ++row)
16492 {
16493 row->y = it.current_y;
16494 row->visible_height = row->height;
16495
16496 if (row->y < min_y)
16497 row->visible_height -= min_y - row->y;
16498 if (row->y + row->height > max_y)
16499 row->visible_height -= row->y + row->height - max_y;
16500 if (row->fringe_bitmap_periodic_p)
16501 row->redraw_fringe_bitmaps_p = 1;
16502
16503 it.current_y += row->height;
16504
16505 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16506 last_reused_text_row = row;
16507 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16508 break;
16509 }
16510
16511 /* Disable lines in the current matrix which are now
16512 below the window. */
16513 for (++row; row < bottom_row; ++row)
16514 row->enabled_p = row->mode_line_p = 0;
16515 }
16516
16517 /* Update window_end_pos etc.; last_reused_text_row is the last
16518 reused row from the current matrix containing text, if any.
16519 The value of last_text_row is the last displayed line
16520 containing text. */
16521 if (last_reused_text_row)
16522 {
16523 w->window_end_bytepos
16524 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16525 w->window_end_pos
16526 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16527 w->window_end_vpos
16528 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16529 w->current_matrix));
16530 }
16531 else if (last_text_row)
16532 {
16533 w->window_end_bytepos
16534 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16535 w->window_end_pos
16536 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16537 w->window_end_vpos
16538 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16539 }
16540 else
16541 {
16542 /* This window must be completely empty. */
16543 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16544 w->window_end_pos = make_number (Z - ZV);
16545 w->window_end_vpos = make_number (0);
16546 }
16547 w->window_end_valid = Qnil;
16548
16549 /* Update hint: don't try scrolling again in update_window. */
16550 w->desired_matrix->no_scrolling_p = 1;
16551
16552 #ifdef GLYPH_DEBUG
16553 debug_method_add (w, "try_window_reusing_current_matrix 1");
16554 #endif
16555 return 1;
16556 }
16557 else if (CHARPOS (new_start) > CHARPOS (start))
16558 {
16559 struct glyph_row *pt_row, *row;
16560 struct glyph_row *first_reusable_row;
16561 struct glyph_row *first_row_to_display;
16562 int dy;
16563 int yb = window_text_bottom_y (w);
16564
16565 /* Find the row starting at new_start, if there is one. Don't
16566 reuse a partially visible line at the end. */
16567 first_reusable_row = start_row;
16568 while (first_reusable_row->enabled_p
16569 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16570 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16571 < CHARPOS (new_start)))
16572 ++first_reusable_row;
16573
16574 /* Give up if there is no row to reuse. */
16575 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16576 || !first_reusable_row->enabled_p
16577 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16578 != CHARPOS (new_start)))
16579 return 0;
16580
16581 /* We can reuse fully visible rows beginning with
16582 first_reusable_row to the end of the window. Set
16583 first_row_to_display to the first row that cannot be reused.
16584 Set pt_row to the row containing point, if there is any. */
16585 pt_row = NULL;
16586 for (first_row_to_display = first_reusable_row;
16587 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16588 ++first_row_to_display)
16589 {
16590 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16591 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16592 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16593 && first_row_to_display->ends_at_zv_p
16594 && pt_row == NULL)))
16595 pt_row = first_row_to_display;
16596 }
16597
16598 /* Start displaying at the start of first_row_to_display. */
16599 eassert (first_row_to_display->y < yb);
16600 init_to_row_start (&it, w, first_row_to_display);
16601
16602 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16603 - start_vpos);
16604 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16605 - nrows_scrolled);
16606 it.current_y = (first_row_to_display->y - first_reusable_row->y
16607 + WINDOW_HEADER_LINE_HEIGHT (w));
16608
16609 /* Display lines beginning with first_row_to_display in the
16610 desired matrix. Set last_text_row to the last row displayed
16611 that displays text. */
16612 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16613 if (pt_row == NULL)
16614 w->cursor.vpos = -1;
16615 last_text_row = NULL;
16616 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16617 if (display_line (&it))
16618 last_text_row = it.glyph_row - 1;
16619
16620 /* If point is in a reused row, adjust y and vpos of the cursor
16621 position. */
16622 if (pt_row)
16623 {
16624 w->cursor.vpos -= nrows_scrolled;
16625 w->cursor.y -= first_reusable_row->y - start_row->y;
16626 }
16627
16628 /* Give up if point isn't in a row displayed or reused. (This
16629 also handles the case where w->cursor.vpos < nrows_scrolled
16630 after the calls to display_line, which can happen with scroll
16631 margins. See bug#1295.) */
16632 if (w->cursor.vpos < 0)
16633 {
16634 clear_glyph_matrix (w->desired_matrix);
16635 return 0;
16636 }
16637
16638 /* Scroll the display. */
16639 run.current_y = first_reusable_row->y;
16640 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16641 run.height = it.last_visible_y - run.current_y;
16642 dy = run.current_y - run.desired_y;
16643
16644 if (run.height)
16645 {
16646 update_begin (f);
16647 FRAME_RIF (f)->update_window_begin_hook (w);
16648 FRAME_RIF (f)->clear_window_mouse_face (w);
16649 FRAME_RIF (f)->scroll_run_hook (w, &run);
16650 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16651 update_end (f);
16652 }
16653
16654 /* Adjust Y positions of reused rows. */
16655 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16656 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16657 max_y = it.last_visible_y;
16658 for (row = first_reusable_row; row < first_row_to_display; ++row)
16659 {
16660 row->y -= dy;
16661 row->visible_height = row->height;
16662 if (row->y < min_y)
16663 row->visible_height -= min_y - row->y;
16664 if (row->y + row->height > max_y)
16665 row->visible_height -= row->y + row->height - max_y;
16666 if (row->fringe_bitmap_periodic_p)
16667 row->redraw_fringe_bitmaps_p = 1;
16668 }
16669
16670 /* Scroll the current matrix. */
16671 eassert (nrows_scrolled > 0);
16672 rotate_matrix (w->current_matrix,
16673 start_vpos,
16674 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16675 -nrows_scrolled);
16676
16677 /* Disable rows not reused. */
16678 for (row -= nrows_scrolled; row < bottom_row; ++row)
16679 row->enabled_p = 0;
16680
16681 /* Point may have moved to a different line, so we cannot assume that
16682 the previous cursor position is valid; locate the correct row. */
16683 if (pt_row)
16684 {
16685 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16686 row < bottom_row
16687 && PT >= MATRIX_ROW_END_CHARPOS (row)
16688 && !row->ends_at_zv_p;
16689 row++)
16690 {
16691 w->cursor.vpos++;
16692 w->cursor.y = row->y;
16693 }
16694 if (row < bottom_row)
16695 {
16696 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16697 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16698
16699 /* Can't use this optimization with bidi-reordered glyph
16700 rows, unless cursor is already at point. */
16701 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16702 {
16703 if (!(w->cursor.hpos >= 0
16704 && w->cursor.hpos < row->used[TEXT_AREA]
16705 && BUFFERP (glyph->object)
16706 && glyph->charpos == PT))
16707 return 0;
16708 }
16709 else
16710 for (; glyph < end
16711 && (!BUFFERP (glyph->object)
16712 || glyph->charpos < PT);
16713 glyph++)
16714 {
16715 w->cursor.hpos++;
16716 w->cursor.x += glyph->pixel_width;
16717 }
16718 }
16719 }
16720
16721 /* Adjust window end. A null value of last_text_row means that
16722 the window end is in reused rows which in turn means that
16723 only its vpos can have changed. */
16724 if (last_text_row)
16725 {
16726 w->window_end_bytepos
16727 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16728 w->window_end_pos
16729 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16730 w->window_end_vpos
16731 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16732 }
16733 else
16734 {
16735 w->window_end_vpos
16736 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16737 }
16738
16739 w->window_end_valid = Qnil;
16740 w->desired_matrix->no_scrolling_p = 1;
16741
16742 #ifdef GLYPH_DEBUG
16743 debug_method_add (w, "try_window_reusing_current_matrix 2");
16744 #endif
16745 return 1;
16746 }
16747
16748 return 0;
16749 }
16750
16751
16752 \f
16753 /************************************************************************
16754 Window redisplay reusing current matrix when buffer has changed
16755 ************************************************************************/
16756
16757 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16758 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16759 ptrdiff_t *, ptrdiff_t *);
16760 static struct glyph_row *
16761 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16762 struct glyph_row *);
16763
16764
16765 /* Return the last row in MATRIX displaying text. If row START is
16766 non-null, start searching with that row. IT gives the dimensions
16767 of the display. Value is null if matrix is empty; otherwise it is
16768 a pointer to the row found. */
16769
16770 static struct glyph_row *
16771 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16772 struct glyph_row *start)
16773 {
16774 struct glyph_row *row, *row_found;
16775
16776 /* Set row_found to the last row in IT->w's current matrix
16777 displaying text. The loop looks funny but think of partially
16778 visible lines. */
16779 row_found = NULL;
16780 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16781 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16782 {
16783 eassert (row->enabled_p);
16784 row_found = row;
16785 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16786 break;
16787 ++row;
16788 }
16789
16790 return row_found;
16791 }
16792
16793
16794 /* Return the last row in the current matrix of W that is not affected
16795 by changes at the start of current_buffer that occurred since W's
16796 current matrix was built. Value is null if no such row exists.
16797
16798 BEG_UNCHANGED us the number of characters unchanged at the start of
16799 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16800 first changed character in current_buffer. Characters at positions <
16801 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16802 when the current matrix was built. */
16803
16804 static struct glyph_row *
16805 find_last_unchanged_at_beg_row (struct window *w)
16806 {
16807 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16808 struct glyph_row *row;
16809 struct glyph_row *row_found = NULL;
16810 int yb = window_text_bottom_y (w);
16811
16812 /* Find the last row displaying unchanged text. */
16813 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16814 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16815 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16816 ++row)
16817 {
16818 if (/* If row ends before first_changed_pos, it is unchanged,
16819 except in some case. */
16820 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16821 /* When row ends in ZV and we write at ZV it is not
16822 unchanged. */
16823 && !row->ends_at_zv_p
16824 /* When first_changed_pos is the end of a continued line,
16825 row is not unchanged because it may be no longer
16826 continued. */
16827 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16828 && (row->continued_p
16829 || row->exact_window_width_line_p))
16830 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16831 needs to be recomputed, so don't consider this row as
16832 unchanged. This happens when the last line was
16833 bidi-reordered and was killed immediately before this
16834 redisplay cycle. In that case, ROW->end stores the
16835 buffer position of the first visual-order character of
16836 the killed text, which is now beyond ZV. */
16837 && CHARPOS (row->end.pos) <= ZV)
16838 row_found = row;
16839
16840 /* Stop if last visible row. */
16841 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16842 break;
16843 }
16844
16845 return row_found;
16846 }
16847
16848
16849 /* Find the first glyph row in the current matrix of W that is not
16850 affected by changes at the end of current_buffer since the
16851 time W's current matrix was built.
16852
16853 Return in *DELTA the number of chars by which buffer positions in
16854 unchanged text at the end of current_buffer must be adjusted.
16855
16856 Return in *DELTA_BYTES the corresponding number of bytes.
16857
16858 Value is null if no such row exists, i.e. all rows are affected by
16859 changes. */
16860
16861 static struct glyph_row *
16862 find_first_unchanged_at_end_row (struct window *w,
16863 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16864 {
16865 struct glyph_row *row;
16866 struct glyph_row *row_found = NULL;
16867
16868 *delta = *delta_bytes = 0;
16869
16870 /* Display must not have been paused, otherwise the current matrix
16871 is not up to date. */
16872 eassert (!NILP (w->window_end_valid));
16873
16874 /* A value of window_end_pos >= END_UNCHANGED means that the window
16875 end is in the range of changed text. If so, there is no
16876 unchanged row at the end of W's current matrix. */
16877 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16878 return NULL;
16879
16880 /* Set row to the last row in W's current matrix displaying text. */
16881 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16882
16883 /* If matrix is entirely empty, no unchanged row exists. */
16884 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16885 {
16886 /* The value of row is the last glyph row in the matrix having a
16887 meaningful buffer position in it. The end position of row
16888 corresponds to window_end_pos. This allows us to translate
16889 buffer positions in the current matrix to current buffer
16890 positions for characters not in changed text. */
16891 ptrdiff_t Z_old =
16892 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16893 ptrdiff_t Z_BYTE_old =
16894 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16895 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16896 struct glyph_row *first_text_row
16897 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16898
16899 *delta = Z - Z_old;
16900 *delta_bytes = Z_BYTE - Z_BYTE_old;
16901
16902 /* Set last_unchanged_pos to the buffer position of the last
16903 character in the buffer that has not been changed. Z is the
16904 index + 1 of the last character in current_buffer, i.e. by
16905 subtracting END_UNCHANGED we get the index of the last
16906 unchanged character, and we have to add BEG to get its buffer
16907 position. */
16908 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16909 last_unchanged_pos_old = last_unchanged_pos - *delta;
16910
16911 /* Search backward from ROW for a row displaying a line that
16912 starts at a minimum position >= last_unchanged_pos_old. */
16913 for (; row > first_text_row; --row)
16914 {
16915 /* This used to abort, but it can happen.
16916 It is ok to just stop the search instead here. KFS. */
16917 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16918 break;
16919
16920 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16921 row_found = row;
16922 }
16923 }
16924
16925 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16926
16927 return row_found;
16928 }
16929
16930
16931 /* Make sure that glyph rows in the current matrix of window W
16932 reference the same glyph memory as corresponding rows in the
16933 frame's frame matrix. This function is called after scrolling W's
16934 current matrix on a terminal frame in try_window_id and
16935 try_window_reusing_current_matrix. */
16936
16937 static void
16938 sync_frame_with_window_matrix_rows (struct window *w)
16939 {
16940 struct frame *f = XFRAME (w->frame);
16941 struct glyph_row *window_row, *window_row_end, *frame_row;
16942
16943 /* Preconditions: W must be a leaf window and full-width. Its frame
16944 must have a frame matrix. */
16945 eassert (NILP (w->hchild) && NILP (w->vchild));
16946 eassert (WINDOW_FULL_WIDTH_P (w));
16947 eassert (!FRAME_WINDOW_P (f));
16948
16949 /* If W is a full-width window, glyph pointers in W's current matrix
16950 have, by definition, to be the same as glyph pointers in the
16951 corresponding frame matrix. Note that frame matrices have no
16952 marginal areas (see build_frame_matrix). */
16953 window_row = w->current_matrix->rows;
16954 window_row_end = window_row + w->current_matrix->nrows;
16955 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16956 while (window_row < window_row_end)
16957 {
16958 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16959 struct glyph *end = window_row->glyphs[LAST_AREA];
16960
16961 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16962 frame_row->glyphs[TEXT_AREA] = start;
16963 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16964 frame_row->glyphs[LAST_AREA] = end;
16965
16966 /* Disable frame rows whose corresponding window rows have
16967 been disabled in try_window_id. */
16968 if (!window_row->enabled_p)
16969 frame_row->enabled_p = 0;
16970
16971 ++window_row, ++frame_row;
16972 }
16973 }
16974
16975
16976 /* Find the glyph row in window W containing CHARPOS. Consider all
16977 rows between START and END (not inclusive). END null means search
16978 all rows to the end of the display area of W. Value is the row
16979 containing CHARPOS or null. */
16980
16981 struct glyph_row *
16982 row_containing_pos (struct window *w, ptrdiff_t charpos,
16983 struct glyph_row *start, struct glyph_row *end, int dy)
16984 {
16985 struct glyph_row *row = start;
16986 struct glyph_row *best_row = NULL;
16987 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16988 int last_y;
16989
16990 /* If we happen to start on a header-line, skip that. */
16991 if (row->mode_line_p)
16992 ++row;
16993
16994 if ((end && row >= end) || !row->enabled_p)
16995 return NULL;
16996
16997 last_y = window_text_bottom_y (w) - dy;
16998
16999 while (1)
17000 {
17001 /* Give up if we have gone too far. */
17002 if (end && row >= end)
17003 return NULL;
17004 /* This formerly returned if they were equal.
17005 I think that both quantities are of a "last plus one" type;
17006 if so, when they are equal, the row is within the screen. -- rms. */
17007 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17008 return NULL;
17009
17010 /* If it is in this row, return this row. */
17011 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17012 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17013 /* The end position of a row equals the start
17014 position of the next row. If CHARPOS is there, we
17015 would rather display it in the next line, except
17016 when this line ends in ZV. */
17017 && !row->ends_at_zv_p
17018 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17019 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17020 {
17021 struct glyph *g;
17022
17023 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17024 || (!best_row && !row->continued_p))
17025 return row;
17026 /* In bidi-reordered rows, there could be several rows
17027 occluding point, all of them belonging to the same
17028 continued line. We need to find the row which fits
17029 CHARPOS the best. */
17030 for (g = row->glyphs[TEXT_AREA];
17031 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17032 g++)
17033 {
17034 if (!STRINGP (g->object))
17035 {
17036 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17037 {
17038 mindif = eabs (g->charpos - charpos);
17039 best_row = row;
17040 /* Exact match always wins. */
17041 if (mindif == 0)
17042 return best_row;
17043 }
17044 }
17045 }
17046 }
17047 else if (best_row && !row->continued_p)
17048 return best_row;
17049 ++row;
17050 }
17051 }
17052
17053
17054 /* Try to redisplay window W by reusing its existing display. W's
17055 current matrix must be up to date when this function is called,
17056 i.e. window_end_valid must not be nil.
17057
17058 Value is
17059
17060 1 if display has been updated
17061 0 if otherwise unsuccessful
17062 -1 if redisplay with same window start is known not to succeed
17063
17064 The following steps are performed:
17065
17066 1. Find the last row in the current matrix of W that is not
17067 affected by changes at the start of current_buffer. If no such row
17068 is found, give up.
17069
17070 2. Find the first row in W's current matrix that is not affected by
17071 changes at the end of current_buffer. Maybe there is no such row.
17072
17073 3. Display lines beginning with the row + 1 found in step 1 to the
17074 row found in step 2 or, if step 2 didn't find a row, to the end of
17075 the window.
17076
17077 4. If cursor is not known to appear on the window, give up.
17078
17079 5. If display stopped at the row found in step 2, scroll the
17080 display and current matrix as needed.
17081
17082 6. Maybe display some lines at the end of W, if we must. This can
17083 happen under various circumstances, like a partially visible line
17084 becoming fully visible, or because newly displayed lines are displayed
17085 in smaller font sizes.
17086
17087 7. Update W's window end information. */
17088
17089 static int
17090 try_window_id (struct window *w)
17091 {
17092 struct frame *f = XFRAME (w->frame);
17093 struct glyph_matrix *current_matrix = w->current_matrix;
17094 struct glyph_matrix *desired_matrix = w->desired_matrix;
17095 struct glyph_row *last_unchanged_at_beg_row;
17096 struct glyph_row *first_unchanged_at_end_row;
17097 struct glyph_row *row;
17098 struct glyph_row *bottom_row;
17099 int bottom_vpos;
17100 struct it it;
17101 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17102 int dvpos, dy;
17103 struct text_pos start_pos;
17104 struct run run;
17105 int first_unchanged_at_end_vpos = 0;
17106 struct glyph_row *last_text_row, *last_text_row_at_end;
17107 struct text_pos start;
17108 ptrdiff_t first_changed_charpos, last_changed_charpos;
17109
17110 #ifdef GLYPH_DEBUG
17111 if (inhibit_try_window_id)
17112 return 0;
17113 #endif
17114
17115 /* This is handy for debugging. */
17116 #if 0
17117 #define GIVE_UP(X) \
17118 do { \
17119 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17120 return 0; \
17121 } while (0)
17122 #else
17123 #define GIVE_UP(X) return 0
17124 #endif
17125
17126 SET_TEXT_POS_FROM_MARKER (start, w->start);
17127
17128 /* Don't use this for mini-windows because these can show
17129 messages and mini-buffers, and we don't handle that here. */
17130 if (MINI_WINDOW_P (w))
17131 GIVE_UP (1);
17132
17133 /* This flag is used to prevent redisplay optimizations. */
17134 if (windows_or_buffers_changed || cursor_type_changed)
17135 GIVE_UP (2);
17136
17137 /* Verify that narrowing has not changed.
17138 Also verify that we were not told to prevent redisplay optimizations.
17139 It would be nice to further
17140 reduce the number of cases where this prevents try_window_id. */
17141 if (current_buffer->clip_changed
17142 || current_buffer->prevent_redisplay_optimizations_p)
17143 GIVE_UP (3);
17144
17145 /* Window must either use window-based redisplay or be full width. */
17146 if (!FRAME_WINDOW_P (f)
17147 && (!FRAME_LINE_INS_DEL_OK (f)
17148 || !WINDOW_FULL_WIDTH_P (w)))
17149 GIVE_UP (4);
17150
17151 /* Give up if point is known NOT to appear in W. */
17152 if (PT < CHARPOS (start))
17153 GIVE_UP (5);
17154
17155 /* Another way to prevent redisplay optimizations. */
17156 if (w->last_modified == 0)
17157 GIVE_UP (6);
17158
17159 /* Verify that window is not hscrolled. */
17160 if (w->hscroll != 0)
17161 GIVE_UP (7);
17162
17163 /* Verify that display wasn't paused. */
17164 if (NILP (w->window_end_valid))
17165 GIVE_UP (8);
17166
17167 /* Can't use this if highlighting a region because a cursor movement
17168 will do more than just set the cursor. */
17169 if (!NILP (Vtransient_mark_mode)
17170 && !NILP (BVAR (current_buffer, mark_active)))
17171 GIVE_UP (9);
17172
17173 /* Likewise if highlighting trailing whitespace. */
17174 if (!NILP (Vshow_trailing_whitespace))
17175 GIVE_UP (11);
17176
17177 /* Likewise if showing a region. */
17178 if (!NILP (w->region_showing))
17179 GIVE_UP (10);
17180
17181 /* Can't use this if overlay arrow position and/or string have
17182 changed. */
17183 if (overlay_arrows_changed_p ())
17184 GIVE_UP (12);
17185
17186 /* When word-wrap is on, adding a space to the first word of a
17187 wrapped line can change the wrap position, altering the line
17188 above it. It might be worthwhile to handle this more
17189 intelligently, but for now just redisplay from scratch. */
17190 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17191 GIVE_UP (21);
17192
17193 /* Under bidi reordering, adding or deleting a character in the
17194 beginning of a paragraph, before the first strong directional
17195 character, can change the base direction of the paragraph (unless
17196 the buffer specifies a fixed paragraph direction), which will
17197 require to redisplay the whole paragraph. It might be worthwhile
17198 to find the paragraph limits and widen the range of redisplayed
17199 lines to that, but for now just give up this optimization and
17200 redisplay from scratch. */
17201 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17202 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17203 GIVE_UP (22);
17204
17205 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17206 only if buffer has really changed. The reason is that the gap is
17207 initially at Z for freshly visited files. The code below would
17208 set end_unchanged to 0 in that case. */
17209 if (MODIFF > SAVE_MODIFF
17210 /* This seems to happen sometimes after saving a buffer. */
17211 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17212 {
17213 if (GPT - BEG < BEG_UNCHANGED)
17214 BEG_UNCHANGED = GPT - BEG;
17215 if (Z - GPT < END_UNCHANGED)
17216 END_UNCHANGED = Z - GPT;
17217 }
17218
17219 /* The position of the first and last character that has been changed. */
17220 first_changed_charpos = BEG + BEG_UNCHANGED;
17221 last_changed_charpos = Z - END_UNCHANGED;
17222
17223 /* If window starts after a line end, and the last change is in
17224 front of that newline, then changes don't affect the display.
17225 This case happens with stealth-fontification. Note that although
17226 the display is unchanged, glyph positions in the matrix have to
17227 be adjusted, of course. */
17228 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17229 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17230 && ((last_changed_charpos < CHARPOS (start)
17231 && CHARPOS (start) == BEGV)
17232 || (last_changed_charpos < CHARPOS (start) - 1
17233 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17234 {
17235 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17236 struct glyph_row *r0;
17237
17238 /* Compute how many chars/bytes have been added to or removed
17239 from the buffer. */
17240 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17241 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17242 Z_delta = Z - Z_old;
17243 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17244
17245 /* Give up if PT is not in the window. Note that it already has
17246 been checked at the start of try_window_id that PT is not in
17247 front of the window start. */
17248 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17249 GIVE_UP (13);
17250
17251 /* If window start is unchanged, we can reuse the whole matrix
17252 as is, after adjusting glyph positions. No need to compute
17253 the window end again, since its offset from Z hasn't changed. */
17254 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17255 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17256 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17257 /* PT must not be in a partially visible line. */
17258 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17259 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17260 {
17261 /* Adjust positions in the glyph matrix. */
17262 if (Z_delta || Z_delta_bytes)
17263 {
17264 struct glyph_row *r1
17265 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17266 increment_matrix_positions (w->current_matrix,
17267 MATRIX_ROW_VPOS (r0, current_matrix),
17268 MATRIX_ROW_VPOS (r1, current_matrix),
17269 Z_delta, Z_delta_bytes);
17270 }
17271
17272 /* Set the cursor. */
17273 row = row_containing_pos (w, PT, r0, NULL, 0);
17274 if (row)
17275 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17276 else
17277 abort ();
17278 return 1;
17279 }
17280 }
17281
17282 /* Handle the case that changes are all below what is displayed in
17283 the window, and that PT is in the window. This shortcut cannot
17284 be taken if ZV is visible in the window, and text has been added
17285 there that is visible in the window. */
17286 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17287 /* ZV is not visible in the window, or there are no
17288 changes at ZV, actually. */
17289 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17290 || first_changed_charpos == last_changed_charpos))
17291 {
17292 struct glyph_row *r0;
17293
17294 /* Give up if PT is not in the window. Note that it already has
17295 been checked at the start of try_window_id that PT is not in
17296 front of the window start. */
17297 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17298 GIVE_UP (14);
17299
17300 /* If window start is unchanged, we can reuse the whole matrix
17301 as is, without changing glyph positions since no text has
17302 been added/removed in front of the window end. */
17303 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17304 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17305 /* PT must not be in a partially visible line. */
17306 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17307 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17308 {
17309 /* We have to compute the window end anew since text
17310 could have been added/removed after it. */
17311 w->window_end_pos
17312 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17313 w->window_end_bytepos
17314 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17315
17316 /* Set the cursor. */
17317 row = row_containing_pos (w, PT, r0, NULL, 0);
17318 if (row)
17319 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17320 else
17321 abort ();
17322 return 2;
17323 }
17324 }
17325
17326 /* Give up if window start is in the changed area.
17327
17328 The condition used to read
17329
17330 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17331
17332 but why that was tested escapes me at the moment. */
17333 if (CHARPOS (start) >= first_changed_charpos
17334 && CHARPOS (start) <= last_changed_charpos)
17335 GIVE_UP (15);
17336
17337 /* Check that window start agrees with the start of the first glyph
17338 row in its current matrix. Check this after we know the window
17339 start is not in changed text, otherwise positions would not be
17340 comparable. */
17341 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17342 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17343 GIVE_UP (16);
17344
17345 /* Give up if the window ends in strings. Overlay strings
17346 at the end are difficult to handle, so don't try. */
17347 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17348 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17349 GIVE_UP (20);
17350
17351 /* Compute the position at which we have to start displaying new
17352 lines. Some of the lines at the top of the window might be
17353 reusable because they are not displaying changed text. Find the
17354 last row in W's current matrix not affected by changes at the
17355 start of current_buffer. Value is null if changes start in the
17356 first line of window. */
17357 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17358 if (last_unchanged_at_beg_row)
17359 {
17360 /* Avoid starting to display in the middle of a character, a TAB
17361 for instance. This is easier than to set up the iterator
17362 exactly, and it's not a frequent case, so the additional
17363 effort wouldn't really pay off. */
17364 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17365 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17366 && last_unchanged_at_beg_row > w->current_matrix->rows)
17367 --last_unchanged_at_beg_row;
17368
17369 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17370 GIVE_UP (17);
17371
17372 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17373 GIVE_UP (18);
17374 start_pos = it.current.pos;
17375
17376 /* Start displaying new lines in the desired matrix at the same
17377 vpos we would use in the current matrix, i.e. below
17378 last_unchanged_at_beg_row. */
17379 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17380 current_matrix);
17381 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17382 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17383
17384 eassert (it.hpos == 0 && it.current_x == 0);
17385 }
17386 else
17387 {
17388 /* There are no reusable lines at the start of the window.
17389 Start displaying in the first text line. */
17390 start_display (&it, w, start);
17391 it.vpos = it.first_vpos;
17392 start_pos = it.current.pos;
17393 }
17394
17395 /* Find the first row that is not affected by changes at the end of
17396 the buffer. Value will be null if there is no unchanged row, in
17397 which case we must redisplay to the end of the window. delta
17398 will be set to the value by which buffer positions beginning with
17399 first_unchanged_at_end_row have to be adjusted due to text
17400 changes. */
17401 first_unchanged_at_end_row
17402 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17403 IF_DEBUG (debug_delta = delta);
17404 IF_DEBUG (debug_delta_bytes = delta_bytes);
17405
17406 /* Set stop_pos to the buffer position up to which we will have to
17407 display new lines. If first_unchanged_at_end_row != NULL, this
17408 is the buffer position of the start of the line displayed in that
17409 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17410 that we don't stop at a buffer position. */
17411 stop_pos = 0;
17412 if (first_unchanged_at_end_row)
17413 {
17414 eassert (last_unchanged_at_beg_row == NULL
17415 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17416
17417 /* If this is a continuation line, move forward to the next one
17418 that isn't. Changes in lines above affect this line.
17419 Caution: this may move first_unchanged_at_end_row to a row
17420 not displaying text. */
17421 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17422 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17423 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17424 < it.last_visible_y))
17425 ++first_unchanged_at_end_row;
17426
17427 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17428 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17429 >= it.last_visible_y))
17430 first_unchanged_at_end_row = NULL;
17431 else
17432 {
17433 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17434 + delta);
17435 first_unchanged_at_end_vpos
17436 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17437 eassert (stop_pos >= Z - END_UNCHANGED);
17438 }
17439 }
17440 else if (last_unchanged_at_beg_row == NULL)
17441 GIVE_UP (19);
17442
17443
17444 #ifdef GLYPH_DEBUG
17445
17446 /* Either there is no unchanged row at the end, or the one we have
17447 now displays text. This is a necessary condition for the window
17448 end pos calculation at the end of this function. */
17449 eassert (first_unchanged_at_end_row == NULL
17450 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17451
17452 debug_last_unchanged_at_beg_vpos
17453 = (last_unchanged_at_beg_row
17454 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17455 : -1);
17456 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17457
17458 #endif /* GLYPH_DEBUG */
17459
17460
17461 /* Display new lines. Set last_text_row to the last new line
17462 displayed which has text on it, i.e. might end up as being the
17463 line where the window_end_vpos is. */
17464 w->cursor.vpos = -1;
17465 last_text_row = NULL;
17466 overlay_arrow_seen = 0;
17467 while (it.current_y < it.last_visible_y
17468 && !fonts_changed_p
17469 && (first_unchanged_at_end_row == NULL
17470 || IT_CHARPOS (it) < stop_pos))
17471 {
17472 if (display_line (&it))
17473 last_text_row = it.glyph_row - 1;
17474 }
17475
17476 if (fonts_changed_p)
17477 return -1;
17478
17479
17480 /* Compute differences in buffer positions, y-positions etc. for
17481 lines reused at the bottom of the window. Compute what we can
17482 scroll. */
17483 if (first_unchanged_at_end_row
17484 /* No lines reused because we displayed everything up to the
17485 bottom of the window. */
17486 && it.current_y < it.last_visible_y)
17487 {
17488 dvpos = (it.vpos
17489 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17490 current_matrix));
17491 dy = it.current_y - first_unchanged_at_end_row->y;
17492 run.current_y = first_unchanged_at_end_row->y;
17493 run.desired_y = run.current_y + dy;
17494 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17495 }
17496 else
17497 {
17498 delta = delta_bytes = dvpos = dy
17499 = run.current_y = run.desired_y = run.height = 0;
17500 first_unchanged_at_end_row = NULL;
17501 }
17502 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17503
17504
17505 /* Find the cursor if not already found. We have to decide whether
17506 PT will appear on this window (it sometimes doesn't, but this is
17507 not a very frequent case.) This decision has to be made before
17508 the current matrix is altered. A value of cursor.vpos < 0 means
17509 that PT is either in one of the lines beginning at
17510 first_unchanged_at_end_row or below the window. Don't care for
17511 lines that might be displayed later at the window end; as
17512 mentioned, this is not a frequent case. */
17513 if (w->cursor.vpos < 0)
17514 {
17515 /* Cursor in unchanged rows at the top? */
17516 if (PT < CHARPOS (start_pos)
17517 && last_unchanged_at_beg_row)
17518 {
17519 row = row_containing_pos (w, PT,
17520 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17521 last_unchanged_at_beg_row + 1, 0);
17522 if (row)
17523 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17524 }
17525
17526 /* Start from first_unchanged_at_end_row looking for PT. */
17527 else if (first_unchanged_at_end_row)
17528 {
17529 row = row_containing_pos (w, PT - delta,
17530 first_unchanged_at_end_row, NULL, 0);
17531 if (row)
17532 set_cursor_from_row (w, row, w->current_matrix, delta,
17533 delta_bytes, dy, dvpos);
17534 }
17535
17536 /* Give up if cursor was not found. */
17537 if (w->cursor.vpos < 0)
17538 {
17539 clear_glyph_matrix (w->desired_matrix);
17540 return -1;
17541 }
17542 }
17543
17544 /* Don't let the cursor end in the scroll margins. */
17545 {
17546 int this_scroll_margin, cursor_height;
17547
17548 this_scroll_margin =
17549 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17550 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17551 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17552
17553 if ((w->cursor.y < this_scroll_margin
17554 && CHARPOS (start) > BEGV)
17555 /* Old redisplay didn't take scroll margin into account at the bottom,
17556 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17557 || (w->cursor.y + (make_cursor_line_fully_visible_p
17558 ? cursor_height + this_scroll_margin
17559 : 1)) > it.last_visible_y)
17560 {
17561 w->cursor.vpos = -1;
17562 clear_glyph_matrix (w->desired_matrix);
17563 return -1;
17564 }
17565 }
17566
17567 /* Scroll the display. Do it before changing the current matrix so
17568 that xterm.c doesn't get confused about where the cursor glyph is
17569 found. */
17570 if (dy && run.height)
17571 {
17572 update_begin (f);
17573
17574 if (FRAME_WINDOW_P (f))
17575 {
17576 FRAME_RIF (f)->update_window_begin_hook (w);
17577 FRAME_RIF (f)->clear_window_mouse_face (w);
17578 FRAME_RIF (f)->scroll_run_hook (w, &run);
17579 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17580 }
17581 else
17582 {
17583 /* Terminal frame. In this case, dvpos gives the number of
17584 lines to scroll by; dvpos < 0 means scroll up. */
17585 int from_vpos
17586 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17587 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17588 int end = (WINDOW_TOP_EDGE_LINE (w)
17589 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17590 + window_internal_height (w));
17591
17592 #if defined (HAVE_GPM) || defined (MSDOS)
17593 x_clear_window_mouse_face (w);
17594 #endif
17595 /* Perform the operation on the screen. */
17596 if (dvpos > 0)
17597 {
17598 /* Scroll last_unchanged_at_beg_row to the end of the
17599 window down dvpos lines. */
17600 set_terminal_window (f, end);
17601
17602 /* On dumb terminals delete dvpos lines at the end
17603 before inserting dvpos empty lines. */
17604 if (!FRAME_SCROLL_REGION_OK (f))
17605 ins_del_lines (f, end - dvpos, -dvpos);
17606
17607 /* Insert dvpos empty lines in front of
17608 last_unchanged_at_beg_row. */
17609 ins_del_lines (f, from, dvpos);
17610 }
17611 else if (dvpos < 0)
17612 {
17613 /* Scroll up last_unchanged_at_beg_vpos to the end of
17614 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17615 set_terminal_window (f, end);
17616
17617 /* Delete dvpos lines in front of
17618 last_unchanged_at_beg_vpos. ins_del_lines will set
17619 the cursor to the given vpos and emit |dvpos| delete
17620 line sequences. */
17621 ins_del_lines (f, from + dvpos, dvpos);
17622
17623 /* On a dumb terminal insert dvpos empty lines at the
17624 end. */
17625 if (!FRAME_SCROLL_REGION_OK (f))
17626 ins_del_lines (f, end + dvpos, -dvpos);
17627 }
17628
17629 set_terminal_window (f, 0);
17630 }
17631
17632 update_end (f);
17633 }
17634
17635 /* Shift reused rows of the current matrix to the right position.
17636 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17637 text. */
17638 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17639 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17640 if (dvpos < 0)
17641 {
17642 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17643 bottom_vpos, dvpos);
17644 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17645 bottom_vpos, 0);
17646 }
17647 else if (dvpos > 0)
17648 {
17649 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17650 bottom_vpos, dvpos);
17651 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17652 first_unchanged_at_end_vpos + dvpos, 0);
17653 }
17654
17655 /* For frame-based redisplay, make sure that current frame and window
17656 matrix are in sync with respect to glyph memory. */
17657 if (!FRAME_WINDOW_P (f))
17658 sync_frame_with_window_matrix_rows (w);
17659
17660 /* Adjust buffer positions in reused rows. */
17661 if (delta || delta_bytes)
17662 increment_matrix_positions (current_matrix,
17663 first_unchanged_at_end_vpos + dvpos,
17664 bottom_vpos, delta, delta_bytes);
17665
17666 /* Adjust Y positions. */
17667 if (dy)
17668 shift_glyph_matrix (w, current_matrix,
17669 first_unchanged_at_end_vpos + dvpos,
17670 bottom_vpos, dy);
17671
17672 if (first_unchanged_at_end_row)
17673 {
17674 first_unchanged_at_end_row += dvpos;
17675 if (first_unchanged_at_end_row->y >= it.last_visible_y
17676 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17677 first_unchanged_at_end_row = NULL;
17678 }
17679
17680 /* If scrolling up, there may be some lines to display at the end of
17681 the window. */
17682 last_text_row_at_end = NULL;
17683 if (dy < 0)
17684 {
17685 /* Scrolling up can leave for example a partially visible line
17686 at the end of the window to be redisplayed. */
17687 /* Set last_row to the glyph row in the current matrix where the
17688 window end line is found. It has been moved up or down in
17689 the matrix by dvpos. */
17690 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17691 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17692
17693 /* If last_row is the window end line, it should display text. */
17694 eassert (last_row->displays_text_p);
17695
17696 /* If window end line was partially visible before, begin
17697 displaying at that line. Otherwise begin displaying with the
17698 line following it. */
17699 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17700 {
17701 init_to_row_start (&it, w, last_row);
17702 it.vpos = last_vpos;
17703 it.current_y = last_row->y;
17704 }
17705 else
17706 {
17707 init_to_row_end (&it, w, last_row);
17708 it.vpos = 1 + last_vpos;
17709 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17710 ++last_row;
17711 }
17712
17713 /* We may start in a continuation line. If so, we have to
17714 get the right continuation_lines_width and current_x. */
17715 it.continuation_lines_width = last_row->continuation_lines_width;
17716 it.hpos = it.current_x = 0;
17717
17718 /* Display the rest of the lines at the window end. */
17719 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17720 while (it.current_y < it.last_visible_y
17721 && !fonts_changed_p)
17722 {
17723 /* Is it always sure that the display agrees with lines in
17724 the current matrix? I don't think so, so we mark rows
17725 displayed invalid in the current matrix by setting their
17726 enabled_p flag to zero. */
17727 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17728 if (display_line (&it))
17729 last_text_row_at_end = it.glyph_row - 1;
17730 }
17731 }
17732
17733 /* Update window_end_pos and window_end_vpos. */
17734 if (first_unchanged_at_end_row
17735 && !last_text_row_at_end)
17736 {
17737 /* Window end line if one of the preserved rows from the current
17738 matrix. Set row to the last row displaying text in current
17739 matrix starting at first_unchanged_at_end_row, after
17740 scrolling. */
17741 eassert (first_unchanged_at_end_row->displays_text_p);
17742 row = find_last_row_displaying_text (w->current_matrix, &it,
17743 first_unchanged_at_end_row);
17744 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17745
17746 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17747 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17748 w->window_end_vpos
17749 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17750 eassert (w->window_end_bytepos >= 0);
17751 IF_DEBUG (debug_method_add (w, "A"));
17752 }
17753 else if (last_text_row_at_end)
17754 {
17755 w->window_end_pos
17756 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17757 w->window_end_bytepos
17758 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17759 w->window_end_vpos
17760 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17761 eassert (w->window_end_bytepos >= 0);
17762 IF_DEBUG (debug_method_add (w, "B"));
17763 }
17764 else if (last_text_row)
17765 {
17766 /* We have displayed either to the end of the window or at the
17767 end of the window, i.e. the last row with text is to be found
17768 in the desired matrix. */
17769 w->window_end_pos
17770 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17771 w->window_end_bytepos
17772 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17773 w->window_end_vpos
17774 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17775 eassert (w->window_end_bytepos >= 0);
17776 }
17777 else if (first_unchanged_at_end_row == NULL
17778 && last_text_row == NULL
17779 && last_text_row_at_end == NULL)
17780 {
17781 /* Displayed to end of window, but no line containing text was
17782 displayed. Lines were deleted at the end of the window. */
17783 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17784 int vpos = XFASTINT (w->window_end_vpos);
17785 struct glyph_row *current_row = current_matrix->rows + vpos;
17786 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17787
17788 for (row = NULL;
17789 row == NULL && vpos >= first_vpos;
17790 --vpos, --current_row, --desired_row)
17791 {
17792 if (desired_row->enabled_p)
17793 {
17794 if (desired_row->displays_text_p)
17795 row = desired_row;
17796 }
17797 else if (current_row->displays_text_p)
17798 row = current_row;
17799 }
17800
17801 eassert (row != NULL);
17802 w->window_end_vpos = make_number (vpos + 1);
17803 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17804 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17805 eassert (w->window_end_bytepos >= 0);
17806 IF_DEBUG (debug_method_add (w, "C"));
17807 }
17808 else
17809 abort ();
17810
17811 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17812 debug_end_vpos = XFASTINT (w->window_end_vpos));
17813
17814 /* Record that display has not been completed. */
17815 w->window_end_valid = Qnil;
17816 w->desired_matrix->no_scrolling_p = 1;
17817 return 3;
17818
17819 #undef GIVE_UP
17820 }
17821
17822
17823 \f
17824 /***********************************************************************
17825 More debugging support
17826 ***********************************************************************/
17827
17828 #ifdef GLYPH_DEBUG
17829
17830 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17831 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17832 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17833
17834
17835 /* Dump the contents of glyph matrix MATRIX on stderr.
17836
17837 GLYPHS 0 means don't show glyph contents.
17838 GLYPHS 1 means show glyphs in short form
17839 GLYPHS > 1 means show glyphs in long form. */
17840
17841 void
17842 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17843 {
17844 int i;
17845 for (i = 0; i < matrix->nrows; ++i)
17846 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17847 }
17848
17849
17850 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17851 the glyph row and area where the glyph comes from. */
17852
17853 void
17854 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17855 {
17856 if (glyph->type == CHAR_GLYPH)
17857 {
17858 fprintf (stderr,
17859 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17860 glyph - row->glyphs[TEXT_AREA],
17861 'C',
17862 glyph->charpos,
17863 (BUFFERP (glyph->object)
17864 ? 'B'
17865 : (STRINGP (glyph->object)
17866 ? 'S'
17867 : '-')),
17868 glyph->pixel_width,
17869 glyph->u.ch,
17870 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17871 ? glyph->u.ch
17872 : '.'),
17873 glyph->face_id,
17874 glyph->left_box_line_p,
17875 glyph->right_box_line_p);
17876 }
17877 else if (glyph->type == STRETCH_GLYPH)
17878 {
17879 fprintf (stderr,
17880 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17881 glyph - row->glyphs[TEXT_AREA],
17882 'S',
17883 glyph->charpos,
17884 (BUFFERP (glyph->object)
17885 ? 'B'
17886 : (STRINGP (glyph->object)
17887 ? 'S'
17888 : '-')),
17889 glyph->pixel_width,
17890 0,
17891 '.',
17892 glyph->face_id,
17893 glyph->left_box_line_p,
17894 glyph->right_box_line_p);
17895 }
17896 else if (glyph->type == IMAGE_GLYPH)
17897 {
17898 fprintf (stderr,
17899 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17900 glyph - row->glyphs[TEXT_AREA],
17901 'I',
17902 glyph->charpos,
17903 (BUFFERP (glyph->object)
17904 ? 'B'
17905 : (STRINGP (glyph->object)
17906 ? 'S'
17907 : '-')),
17908 glyph->pixel_width,
17909 glyph->u.img_id,
17910 '.',
17911 glyph->face_id,
17912 glyph->left_box_line_p,
17913 glyph->right_box_line_p);
17914 }
17915 else if (glyph->type == COMPOSITE_GLYPH)
17916 {
17917 fprintf (stderr,
17918 " %5td %4c %6"pI"d %c %3d 0x%05x",
17919 glyph - row->glyphs[TEXT_AREA],
17920 '+',
17921 glyph->charpos,
17922 (BUFFERP (glyph->object)
17923 ? 'B'
17924 : (STRINGP (glyph->object)
17925 ? 'S'
17926 : '-')),
17927 glyph->pixel_width,
17928 glyph->u.cmp.id);
17929 if (glyph->u.cmp.automatic)
17930 fprintf (stderr,
17931 "[%d-%d]",
17932 glyph->slice.cmp.from, glyph->slice.cmp.to);
17933 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17934 glyph->face_id,
17935 glyph->left_box_line_p,
17936 glyph->right_box_line_p);
17937 }
17938 }
17939
17940
17941 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17942 GLYPHS 0 means don't show glyph contents.
17943 GLYPHS 1 means show glyphs in short form
17944 GLYPHS > 1 means show glyphs in long form. */
17945
17946 void
17947 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17948 {
17949 if (glyphs != 1)
17950 {
17951 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17952 fprintf (stderr, "======================================================================\n");
17953
17954 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17955 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17956 vpos,
17957 MATRIX_ROW_START_CHARPOS (row),
17958 MATRIX_ROW_END_CHARPOS (row),
17959 row->used[TEXT_AREA],
17960 row->contains_overlapping_glyphs_p,
17961 row->enabled_p,
17962 row->truncated_on_left_p,
17963 row->truncated_on_right_p,
17964 row->continued_p,
17965 MATRIX_ROW_CONTINUATION_LINE_P (row),
17966 row->displays_text_p,
17967 row->ends_at_zv_p,
17968 row->fill_line_p,
17969 row->ends_in_middle_of_char_p,
17970 row->starts_in_middle_of_char_p,
17971 row->mouse_face_p,
17972 row->x,
17973 row->y,
17974 row->pixel_width,
17975 row->height,
17976 row->visible_height,
17977 row->ascent,
17978 row->phys_ascent);
17979 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17980 row->end.overlay_string_index,
17981 row->continuation_lines_width);
17982 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17983 CHARPOS (row->start.string_pos),
17984 CHARPOS (row->end.string_pos));
17985 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17986 row->end.dpvec_index);
17987 }
17988
17989 if (glyphs > 1)
17990 {
17991 int area;
17992
17993 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17994 {
17995 struct glyph *glyph = row->glyphs[area];
17996 struct glyph *glyph_end = glyph + row->used[area];
17997
17998 /* Glyph for a line end in text. */
17999 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18000 ++glyph_end;
18001
18002 if (glyph < glyph_end)
18003 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18004
18005 for (; glyph < glyph_end; ++glyph)
18006 dump_glyph (row, glyph, area);
18007 }
18008 }
18009 else if (glyphs == 1)
18010 {
18011 int area;
18012
18013 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18014 {
18015 char *s = (char *) alloca (row->used[area] + 1);
18016 int i;
18017
18018 for (i = 0; i < row->used[area]; ++i)
18019 {
18020 struct glyph *glyph = row->glyphs[area] + i;
18021 if (glyph->type == CHAR_GLYPH
18022 && glyph->u.ch < 0x80
18023 && glyph->u.ch >= ' ')
18024 s[i] = glyph->u.ch;
18025 else
18026 s[i] = '.';
18027 }
18028
18029 s[i] = '\0';
18030 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18031 }
18032 }
18033 }
18034
18035
18036 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18037 Sdump_glyph_matrix, 0, 1, "p",
18038 doc: /* Dump the current matrix of the selected window to stderr.
18039 Shows contents of glyph row structures. With non-nil
18040 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18041 glyphs in short form, otherwise show glyphs in long form. */)
18042 (Lisp_Object glyphs)
18043 {
18044 struct window *w = XWINDOW (selected_window);
18045 struct buffer *buffer = XBUFFER (w->buffer);
18046
18047 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18048 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18049 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18050 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18051 fprintf (stderr, "=============================================\n");
18052 dump_glyph_matrix (w->current_matrix,
18053 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18054 return Qnil;
18055 }
18056
18057
18058 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18059 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18060 (void)
18061 {
18062 struct frame *f = XFRAME (selected_frame);
18063 dump_glyph_matrix (f->current_matrix, 1);
18064 return Qnil;
18065 }
18066
18067
18068 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18069 doc: /* Dump glyph row ROW to stderr.
18070 GLYPH 0 means don't dump glyphs.
18071 GLYPH 1 means dump glyphs in short form.
18072 GLYPH > 1 or omitted means dump glyphs in long form. */)
18073 (Lisp_Object row, Lisp_Object glyphs)
18074 {
18075 struct glyph_matrix *matrix;
18076 EMACS_INT vpos;
18077
18078 CHECK_NUMBER (row);
18079 matrix = XWINDOW (selected_window)->current_matrix;
18080 vpos = XINT (row);
18081 if (vpos >= 0 && vpos < matrix->nrows)
18082 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18083 vpos,
18084 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18085 return Qnil;
18086 }
18087
18088
18089 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18090 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18091 GLYPH 0 means don't dump glyphs.
18092 GLYPH 1 means dump glyphs in short form.
18093 GLYPH > 1 or omitted means dump glyphs in long form. */)
18094 (Lisp_Object row, Lisp_Object glyphs)
18095 {
18096 struct frame *sf = SELECTED_FRAME ();
18097 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18098 EMACS_INT vpos;
18099
18100 CHECK_NUMBER (row);
18101 vpos = XINT (row);
18102 if (vpos >= 0 && vpos < m->nrows)
18103 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18104 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18105 return Qnil;
18106 }
18107
18108
18109 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18110 doc: /* Toggle tracing of redisplay.
18111 With ARG, turn tracing on if and only if ARG is positive. */)
18112 (Lisp_Object arg)
18113 {
18114 if (NILP (arg))
18115 trace_redisplay_p = !trace_redisplay_p;
18116 else
18117 {
18118 arg = Fprefix_numeric_value (arg);
18119 trace_redisplay_p = XINT (arg) > 0;
18120 }
18121
18122 return Qnil;
18123 }
18124
18125
18126 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18127 doc: /* Like `format', but print result to stderr.
18128 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18129 (ptrdiff_t nargs, Lisp_Object *args)
18130 {
18131 Lisp_Object s = Fformat (nargs, args);
18132 fprintf (stderr, "%s", SDATA (s));
18133 return Qnil;
18134 }
18135
18136 #endif /* GLYPH_DEBUG */
18137
18138
18139 \f
18140 /***********************************************************************
18141 Building Desired Matrix Rows
18142 ***********************************************************************/
18143
18144 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18145 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18146
18147 static struct glyph_row *
18148 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18149 {
18150 struct frame *f = XFRAME (WINDOW_FRAME (w));
18151 struct buffer *buffer = XBUFFER (w->buffer);
18152 struct buffer *old = current_buffer;
18153 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18154 int arrow_len = SCHARS (overlay_arrow_string);
18155 const unsigned char *arrow_end = arrow_string + arrow_len;
18156 const unsigned char *p;
18157 struct it it;
18158 int multibyte_p;
18159 int n_glyphs_before;
18160
18161 set_buffer_temp (buffer);
18162 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18163 it.glyph_row->used[TEXT_AREA] = 0;
18164 SET_TEXT_POS (it.position, 0, 0);
18165
18166 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18167 p = arrow_string;
18168 while (p < arrow_end)
18169 {
18170 Lisp_Object face, ilisp;
18171
18172 /* Get the next character. */
18173 if (multibyte_p)
18174 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18175 else
18176 {
18177 it.c = it.char_to_display = *p, it.len = 1;
18178 if (! ASCII_CHAR_P (it.c))
18179 it.char_to_display = BYTE8_TO_CHAR (it.c);
18180 }
18181 p += it.len;
18182
18183 /* Get its face. */
18184 ilisp = make_number (p - arrow_string);
18185 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18186 it.face_id = compute_char_face (f, it.char_to_display, face);
18187
18188 /* Compute its width, get its glyphs. */
18189 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18190 SET_TEXT_POS (it.position, -1, -1);
18191 PRODUCE_GLYPHS (&it);
18192
18193 /* If this character doesn't fit any more in the line, we have
18194 to remove some glyphs. */
18195 if (it.current_x > it.last_visible_x)
18196 {
18197 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18198 break;
18199 }
18200 }
18201
18202 set_buffer_temp (old);
18203 return it.glyph_row;
18204 }
18205
18206
18207 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18208 glyphs are only inserted for terminal frames since we can't really
18209 win with truncation glyphs when partially visible glyphs are
18210 involved. Which glyphs to insert is determined by
18211 produce_special_glyphs. */
18212
18213 static void
18214 insert_left_trunc_glyphs (struct it *it)
18215 {
18216 struct it truncate_it;
18217 struct glyph *from, *end, *to, *toend;
18218
18219 eassert (!FRAME_WINDOW_P (it->f));
18220
18221 /* Get the truncation glyphs. */
18222 truncate_it = *it;
18223 truncate_it.current_x = 0;
18224 truncate_it.face_id = DEFAULT_FACE_ID;
18225 truncate_it.glyph_row = &scratch_glyph_row;
18226 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18227 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18228 truncate_it.object = make_number (0);
18229 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18230
18231 /* Overwrite glyphs from IT with truncation glyphs. */
18232 if (!it->glyph_row->reversed_p)
18233 {
18234 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18235 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18236 to = it->glyph_row->glyphs[TEXT_AREA];
18237 toend = to + it->glyph_row->used[TEXT_AREA];
18238
18239 while (from < end)
18240 *to++ = *from++;
18241
18242 /* There may be padding glyphs left over. Overwrite them too. */
18243 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18244 {
18245 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18246 while (from < end)
18247 *to++ = *from++;
18248 }
18249
18250 if (to > toend)
18251 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18252 }
18253 else
18254 {
18255 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18256 that back to front. */
18257 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18258 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18259 toend = it->glyph_row->glyphs[TEXT_AREA];
18260 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18261
18262 while (from >= end && to >= toend)
18263 *to-- = *from--;
18264 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18265 {
18266 from =
18267 truncate_it.glyph_row->glyphs[TEXT_AREA]
18268 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18269 while (from >= end && to >= toend)
18270 *to-- = *from--;
18271 }
18272 if (from >= end)
18273 {
18274 /* Need to free some room before prepending additional
18275 glyphs. */
18276 int move_by = from - end + 1;
18277 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18278 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18279
18280 for ( ; g >= g0; g--)
18281 g[move_by] = *g;
18282 while (from >= end)
18283 *to-- = *from--;
18284 it->glyph_row->used[TEXT_AREA] += move_by;
18285 }
18286 }
18287 }
18288
18289 /* Compute the hash code for ROW. */
18290 unsigned
18291 row_hash (struct glyph_row *row)
18292 {
18293 int area, k;
18294 unsigned hashval = 0;
18295
18296 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18297 for (k = 0; k < row->used[area]; ++k)
18298 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18299 + row->glyphs[area][k].u.val
18300 + row->glyphs[area][k].face_id
18301 + row->glyphs[area][k].padding_p
18302 + (row->glyphs[area][k].type << 2));
18303
18304 return hashval;
18305 }
18306
18307 /* Compute the pixel height and width of IT->glyph_row.
18308
18309 Most of the time, ascent and height of a display line will be equal
18310 to the max_ascent and max_height values of the display iterator
18311 structure. This is not the case if
18312
18313 1. We hit ZV without displaying anything. In this case, max_ascent
18314 and max_height will be zero.
18315
18316 2. We have some glyphs that don't contribute to the line height.
18317 (The glyph row flag contributes_to_line_height_p is for future
18318 pixmap extensions).
18319
18320 The first case is easily covered by using default values because in
18321 these cases, the line height does not really matter, except that it
18322 must not be zero. */
18323
18324 static void
18325 compute_line_metrics (struct it *it)
18326 {
18327 struct glyph_row *row = it->glyph_row;
18328
18329 if (FRAME_WINDOW_P (it->f))
18330 {
18331 int i, min_y, max_y;
18332
18333 /* The line may consist of one space only, that was added to
18334 place the cursor on it. If so, the row's height hasn't been
18335 computed yet. */
18336 if (row->height == 0)
18337 {
18338 if (it->max_ascent + it->max_descent == 0)
18339 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18340 row->ascent = it->max_ascent;
18341 row->height = it->max_ascent + it->max_descent;
18342 row->phys_ascent = it->max_phys_ascent;
18343 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18344 row->extra_line_spacing = it->max_extra_line_spacing;
18345 }
18346
18347 /* Compute the width of this line. */
18348 row->pixel_width = row->x;
18349 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18350 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18351
18352 eassert (row->pixel_width >= 0);
18353 eassert (row->ascent >= 0 && row->height > 0);
18354
18355 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18356 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18357
18358 /* If first line's physical ascent is larger than its logical
18359 ascent, use the physical ascent, and make the row taller.
18360 This makes accented characters fully visible. */
18361 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18362 && row->phys_ascent > row->ascent)
18363 {
18364 row->height += row->phys_ascent - row->ascent;
18365 row->ascent = row->phys_ascent;
18366 }
18367
18368 /* Compute how much of the line is visible. */
18369 row->visible_height = row->height;
18370
18371 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18372 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18373
18374 if (row->y < min_y)
18375 row->visible_height -= min_y - row->y;
18376 if (row->y + row->height > max_y)
18377 row->visible_height -= row->y + row->height - max_y;
18378 }
18379 else
18380 {
18381 row->pixel_width = row->used[TEXT_AREA];
18382 if (row->continued_p)
18383 row->pixel_width -= it->continuation_pixel_width;
18384 else if (row->truncated_on_right_p)
18385 row->pixel_width -= it->truncation_pixel_width;
18386 row->ascent = row->phys_ascent = 0;
18387 row->height = row->phys_height = row->visible_height = 1;
18388 row->extra_line_spacing = 0;
18389 }
18390
18391 /* Compute a hash code for this row. */
18392 row->hash = row_hash (row);
18393
18394 it->max_ascent = it->max_descent = 0;
18395 it->max_phys_ascent = it->max_phys_descent = 0;
18396 }
18397
18398
18399 /* Append one space to the glyph row of iterator IT if doing a
18400 window-based redisplay. The space has the same face as
18401 IT->face_id. Value is non-zero if a space was added.
18402
18403 This function is called to make sure that there is always one glyph
18404 at the end of a glyph row that the cursor can be set on under
18405 window-systems. (If there weren't such a glyph we would not know
18406 how wide and tall a box cursor should be displayed).
18407
18408 At the same time this space let's a nicely handle clearing to the
18409 end of the line if the row ends in italic text. */
18410
18411 static int
18412 append_space_for_newline (struct it *it, int default_face_p)
18413 {
18414 if (FRAME_WINDOW_P (it->f))
18415 {
18416 int n = it->glyph_row->used[TEXT_AREA];
18417
18418 if (it->glyph_row->glyphs[TEXT_AREA] + n
18419 < it->glyph_row->glyphs[1 + TEXT_AREA])
18420 {
18421 /* Save some values that must not be changed.
18422 Must save IT->c and IT->len because otherwise
18423 ITERATOR_AT_END_P wouldn't work anymore after
18424 append_space_for_newline has been called. */
18425 enum display_element_type saved_what = it->what;
18426 int saved_c = it->c, saved_len = it->len;
18427 int saved_char_to_display = it->char_to_display;
18428 int saved_x = it->current_x;
18429 int saved_face_id = it->face_id;
18430 struct text_pos saved_pos;
18431 Lisp_Object saved_object;
18432 struct face *face;
18433
18434 saved_object = it->object;
18435 saved_pos = it->position;
18436
18437 it->what = IT_CHARACTER;
18438 memset (&it->position, 0, sizeof it->position);
18439 it->object = make_number (0);
18440 it->c = it->char_to_display = ' ';
18441 it->len = 1;
18442
18443 /* If the default face was remapped, be sure to use the
18444 remapped face for the appended newline. */
18445 if (default_face_p)
18446 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18447 else if (it->face_before_selective_p)
18448 it->face_id = it->saved_face_id;
18449 face = FACE_FROM_ID (it->f, it->face_id);
18450 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18451
18452 PRODUCE_GLYPHS (it);
18453
18454 it->override_ascent = -1;
18455 it->constrain_row_ascent_descent_p = 0;
18456 it->current_x = saved_x;
18457 it->object = saved_object;
18458 it->position = saved_pos;
18459 it->what = saved_what;
18460 it->face_id = saved_face_id;
18461 it->len = saved_len;
18462 it->c = saved_c;
18463 it->char_to_display = saved_char_to_display;
18464 return 1;
18465 }
18466 }
18467
18468 return 0;
18469 }
18470
18471
18472 /* Extend the face of the last glyph in the text area of IT->glyph_row
18473 to the end of the display line. Called from display_line. If the
18474 glyph row is empty, add a space glyph to it so that we know the
18475 face to draw. Set the glyph row flag fill_line_p. If the glyph
18476 row is R2L, prepend a stretch glyph to cover the empty space to the
18477 left of the leftmost glyph. */
18478
18479 static void
18480 extend_face_to_end_of_line (struct it *it)
18481 {
18482 struct face *face, *default_face;
18483 struct frame *f = it->f;
18484
18485 /* If line is already filled, do nothing. Non window-system frames
18486 get a grace of one more ``pixel'' because their characters are
18487 1-``pixel'' wide, so they hit the equality too early. This grace
18488 is needed only for R2L rows that are not continued, to produce
18489 one extra blank where we could display the cursor. */
18490 if (it->current_x >= it->last_visible_x
18491 + (!FRAME_WINDOW_P (f)
18492 && it->glyph_row->reversed_p
18493 && !it->glyph_row->continued_p))
18494 return;
18495
18496 /* The default face, possibly remapped. */
18497 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18498
18499 /* Face extension extends the background and box of IT->face_id
18500 to the end of the line. If the background equals the background
18501 of the frame, we don't have to do anything. */
18502 if (it->face_before_selective_p)
18503 face = FACE_FROM_ID (f, it->saved_face_id);
18504 else
18505 face = FACE_FROM_ID (f, it->face_id);
18506
18507 if (FRAME_WINDOW_P (f)
18508 && it->glyph_row->displays_text_p
18509 && face->box == FACE_NO_BOX
18510 && face->background == FRAME_BACKGROUND_PIXEL (f)
18511 && !face->stipple
18512 && !it->glyph_row->reversed_p)
18513 return;
18514
18515 /* Set the glyph row flag indicating that the face of the last glyph
18516 in the text area has to be drawn to the end of the text area. */
18517 it->glyph_row->fill_line_p = 1;
18518
18519 /* If current character of IT is not ASCII, make sure we have the
18520 ASCII face. This will be automatically undone the next time
18521 get_next_display_element returns a multibyte character. Note
18522 that the character will always be single byte in unibyte
18523 text. */
18524 if (!ASCII_CHAR_P (it->c))
18525 {
18526 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18527 }
18528
18529 if (FRAME_WINDOW_P (f))
18530 {
18531 /* If the row is empty, add a space with the current face of IT,
18532 so that we know which face to draw. */
18533 if (it->glyph_row->used[TEXT_AREA] == 0)
18534 {
18535 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18536 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18537 it->glyph_row->used[TEXT_AREA] = 1;
18538 }
18539 #ifdef HAVE_WINDOW_SYSTEM
18540 if (it->glyph_row->reversed_p)
18541 {
18542 /* Prepend a stretch glyph to the row, such that the
18543 rightmost glyph will be drawn flushed all the way to the
18544 right margin of the window. The stretch glyph that will
18545 occupy the empty space, if any, to the left of the
18546 glyphs. */
18547 struct font *font = face->font ? face->font : FRAME_FONT (f);
18548 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18549 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18550 struct glyph *g;
18551 int row_width, stretch_ascent, stretch_width;
18552 struct text_pos saved_pos;
18553 int saved_face_id, saved_avoid_cursor;
18554
18555 for (row_width = 0, g = row_start; g < row_end; g++)
18556 row_width += g->pixel_width;
18557 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18558 if (stretch_width > 0)
18559 {
18560 stretch_ascent =
18561 (((it->ascent + it->descent)
18562 * FONT_BASE (font)) / FONT_HEIGHT (font));
18563 saved_pos = it->position;
18564 memset (&it->position, 0, sizeof it->position);
18565 saved_avoid_cursor = it->avoid_cursor_p;
18566 it->avoid_cursor_p = 1;
18567 saved_face_id = it->face_id;
18568 /* The last row's stretch glyph should get the default
18569 face, to avoid painting the rest of the window with
18570 the region face, if the region ends at ZV. */
18571 if (it->glyph_row->ends_at_zv_p)
18572 it->face_id = default_face->id;
18573 else
18574 it->face_id = face->id;
18575 append_stretch_glyph (it, make_number (0), stretch_width,
18576 it->ascent + it->descent, stretch_ascent);
18577 it->position = saved_pos;
18578 it->avoid_cursor_p = saved_avoid_cursor;
18579 it->face_id = saved_face_id;
18580 }
18581 }
18582 #endif /* HAVE_WINDOW_SYSTEM */
18583 }
18584 else
18585 {
18586 /* Save some values that must not be changed. */
18587 int saved_x = it->current_x;
18588 struct text_pos saved_pos;
18589 Lisp_Object saved_object;
18590 enum display_element_type saved_what = it->what;
18591 int saved_face_id = it->face_id;
18592
18593 saved_object = it->object;
18594 saved_pos = it->position;
18595
18596 it->what = IT_CHARACTER;
18597 memset (&it->position, 0, sizeof it->position);
18598 it->object = make_number (0);
18599 it->c = it->char_to_display = ' ';
18600 it->len = 1;
18601 /* The last row's blank glyphs should get the default face, to
18602 avoid painting the rest of the window with the region face,
18603 if the region ends at ZV. */
18604 if (it->glyph_row->ends_at_zv_p)
18605 it->face_id = default_face->id;
18606 else
18607 it->face_id = face->id;
18608
18609 PRODUCE_GLYPHS (it);
18610
18611 while (it->current_x <= it->last_visible_x)
18612 PRODUCE_GLYPHS (it);
18613
18614 /* Don't count these blanks really. It would let us insert a left
18615 truncation glyph below and make us set the cursor on them, maybe. */
18616 it->current_x = saved_x;
18617 it->object = saved_object;
18618 it->position = saved_pos;
18619 it->what = saved_what;
18620 it->face_id = saved_face_id;
18621 }
18622 }
18623
18624
18625 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18626 trailing whitespace. */
18627
18628 static int
18629 trailing_whitespace_p (ptrdiff_t charpos)
18630 {
18631 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18632 int c = 0;
18633
18634 while (bytepos < ZV_BYTE
18635 && (c = FETCH_CHAR (bytepos),
18636 c == ' ' || c == '\t'))
18637 ++bytepos;
18638
18639 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18640 {
18641 if (bytepos != PT_BYTE)
18642 return 1;
18643 }
18644 return 0;
18645 }
18646
18647
18648 /* Highlight trailing whitespace, if any, in ROW. */
18649
18650 static void
18651 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18652 {
18653 int used = row->used[TEXT_AREA];
18654
18655 if (used)
18656 {
18657 struct glyph *start = row->glyphs[TEXT_AREA];
18658 struct glyph *glyph = start + used - 1;
18659
18660 if (row->reversed_p)
18661 {
18662 /* Right-to-left rows need to be processed in the opposite
18663 direction, so swap the edge pointers. */
18664 glyph = start;
18665 start = row->glyphs[TEXT_AREA] + used - 1;
18666 }
18667
18668 /* Skip over glyphs inserted to display the cursor at the
18669 end of a line, for extending the face of the last glyph
18670 to the end of the line on terminals, and for truncation
18671 and continuation glyphs. */
18672 if (!row->reversed_p)
18673 {
18674 while (glyph >= start
18675 && glyph->type == CHAR_GLYPH
18676 && INTEGERP (glyph->object))
18677 --glyph;
18678 }
18679 else
18680 {
18681 while (glyph <= start
18682 && glyph->type == CHAR_GLYPH
18683 && INTEGERP (glyph->object))
18684 ++glyph;
18685 }
18686
18687 /* If last glyph is a space or stretch, and it's trailing
18688 whitespace, set the face of all trailing whitespace glyphs in
18689 IT->glyph_row to `trailing-whitespace'. */
18690 if ((row->reversed_p ? glyph <= start : glyph >= start)
18691 && BUFFERP (glyph->object)
18692 && (glyph->type == STRETCH_GLYPH
18693 || (glyph->type == CHAR_GLYPH
18694 && glyph->u.ch == ' '))
18695 && trailing_whitespace_p (glyph->charpos))
18696 {
18697 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18698 if (face_id < 0)
18699 return;
18700
18701 if (!row->reversed_p)
18702 {
18703 while (glyph >= start
18704 && BUFFERP (glyph->object)
18705 && (glyph->type == STRETCH_GLYPH
18706 || (glyph->type == CHAR_GLYPH
18707 && glyph->u.ch == ' ')))
18708 (glyph--)->face_id = face_id;
18709 }
18710 else
18711 {
18712 while (glyph <= start
18713 && BUFFERP (glyph->object)
18714 && (glyph->type == STRETCH_GLYPH
18715 || (glyph->type == CHAR_GLYPH
18716 && glyph->u.ch == ' ')))
18717 (glyph++)->face_id = face_id;
18718 }
18719 }
18720 }
18721 }
18722
18723
18724 /* Value is non-zero if glyph row ROW should be
18725 used to hold the cursor. */
18726
18727 static int
18728 cursor_row_p (struct glyph_row *row)
18729 {
18730 int result = 1;
18731
18732 if (PT == CHARPOS (row->end.pos)
18733 || PT == MATRIX_ROW_END_CHARPOS (row))
18734 {
18735 /* Suppose the row ends on a string.
18736 Unless the row is continued, that means it ends on a newline
18737 in the string. If it's anything other than a display string
18738 (e.g., a before-string from an overlay), we don't want the
18739 cursor there. (This heuristic seems to give the optimal
18740 behavior for the various types of multi-line strings.)
18741 One exception: if the string has `cursor' property on one of
18742 its characters, we _do_ want the cursor there. */
18743 if (CHARPOS (row->end.string_pos) >= 0)
18744 {
18745 if (row->continued_p)
18746 result = 1;
18747 else
18748 {
18749 /* Check for `display' property. */
18750 struct glyph *beg = row->glyphs[TEXT_AREA];
18751 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18752 struct glyph *glyph;
18753
18754 result = 0;
18755 for (glyph = end; glyph >= beg; --glyph)
18756 if (STRINGP (glyph->object))
18757 {
18758 Lisp_Object prop
18759 = Fget_char_property (make_number (PT),
18760 Qdisplay, Qnil);
18761 result =
18762 (!NILP (prop)
18763 && display_prop_string_p (prop, glyph->object));
18764 /* If there's a `cursor' property on one of the
18765 string's characters, this row is a cursor row,
18766 even though this is not a display string. */
18767 if (!result)
18768 {
18769 Lisp_Object s = glyph->object;
18770
18771 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18772 {
18773 ptrdiff_t gpos = glyph->charpos;
18774
18775 if (!NILP (Fget_char_property (make_number (gpos),
18776 Qcursor, s)))
18777 {
18778 result = 1;
18779 break;
18780 }
18781 }
18782 }
18783 break;
18784 }
18785 }
18786 }
18787 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18788 {
18789 /* If the row ends in middle of a real character,
18790 and the line is continued, we want the cursor here.
18791 That's because CHARPOS (ROW->end.pos) would equal
18792 PT if PT is before the character. */
18793 if (!row->ends_in_ellipsis_p)
18794 result = row->continued_p;
18795 else
18796 /* If the row ends in an ellipsis, then
18797 CHARPOS (ROW->end.pos) will equal point after the
18798 invisible text. We want that position to be displayed
18799 after the ellipsis. */
18800 result = 0;
18801 }
18802 /* If the row ends at ZV, display the cursor at the end of that
18803 row instead of at the start of the row below. */
18804 else if (row->ends_at_zv_p)
18805 result = 1;
18806 else
18807 result = 0;
18808 }
18809
18810 return result;
18811 }
18812
18813 \f
18814
18815 /* Push the property PROP so that it will be rendered at the current
18816 position in IT. Return 1 if PROP was successfully pushed, 0
18817 otherwise. Called from handle_line_prefix to handle the
18818 `line-prefix' and `wrap-prefix' properties. */
18819
18820 static int
18821 push_prefix_prop (struct it *it, Lisp_Object prop)
18822 {
18823 struct text_pos pos =
18824 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18825
18826 eassert (it->method == GET_FROM_BUFFER
18827 || it->method == GET_FROM_DISPLAY_VECTOR
18828 || it->method == GET_FROM_STRING);
18829
18830 /* We need to save the current buffer/string position, so it will be
18831 restored by pop_it, because iterate_out_of_display_property
18832 depends on that being set correctly, but some situations leave
18833 it->position not yet set when this function is called. */
18834 push_it (it, &pos);
18835
18836 if (STRINGP (prop))
18837 {
18838 if (SCHARS (prop) == 0)
18839 {
18840 pop_it (it);
18841 return 0;
18842 }
18843
18844 it->string = prop;
18845 it->string_from_prefix_prop_p = 1;
18846 it->multibyte_p = STRING_MULTIBYTE (it->string);
18847 it->current.overlay_string_index = -1;
18848 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18849 it->end_charpos = it->string_nchars = SCHARS (it->string);
18850 it->method = GET_FROM_STRING;
18851 it->stop_charpos = 0;
18852 it->prev_stop = 0;
18853 it->base_level_stop = 0;
18854
18855 /* Force paragraph direction to be that of the parent
18856 buffer/string. */
18857 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18858 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18859 else
18860 it->paragraph_embedding = L2R;
18861
18862 /* Set up the bidi iterator for this display string. */
18863 if (it->bidi_p)
18864 {
18865 it->bidi_it.string.lstring = it->string;
18866 it->bidi_it.string.s = NULL;
18867 it->bidi_it.string.schars = it->end_charpos;
18868 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18869 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18870 it->bidi_it.string.unibyte = !it->multibyte_p;
18871 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18872 }
18873 }
18874 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18875 {
18876 it->method = GET_FROM_STRETCH;
18877 it->object = prop;
18878 }
18879 #ifdef HAVE_WINDOW_SYSTEM
18880 else if (IMAGEP (prop))
18881 {
18882 it->what = IT_IMAGE;
18883 it->image_id = lookup_image (it->f, prop);
18884 it->method = GET_FROM_IMAGE;
18885 }
18886 #endif /* HAVE_WINDOW_SYSTEM */
18887 else
18888 {
18889 pop_it (it); /* bogus display property, give up */
18890 return 0;
18891 }
18892
18893 return 1;
18894 }
18895
18896 /* Return the character-property PROP at the current position in IT. */
18897
18898 static Lisp_Object
18899 get_it_property (struct it *it, Lisp_Object prop)
18900 {
18901 Lisp_Object position;
18902
18903 if (STRINGP (it->object))
18904 position = make_number (IT_STRING_CHARPOS (*it));
18905 else if (BUFFERP (it->object))
18906 position = make_number (IT_CHARPOS (*it));
18907 else
18908 return Qnil;
18909
18910 return Fget_char_property (position, prop, it->object);
18911 }
18912
18913 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18914
18915 static void
18916 handle_line_prefix (struct it *it)
18917 {
18918 Lisp_Object prefix;
18919
18920 if (it->continuation_lines_width > 0)
18921 {
18922 prefix = get_it_property (it, Qwrap_prefix);
18923 if (NILP (prefix))
18924 prefix = Vwrap_prefix;
18925 }
18926 else
18927 {
18928 prefix = get_it_property (it, Qline_prefix);
18929 if (NILP (prefix))
18930 prefix = Vline_prefix;
18931 }
18932 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18933 {
18934 /* If the prefix is wider than the window, and we try to wrap
18935 it, it would acquire its own wrap prefix, and so on till the
18936 iterator stack overflows. So, don't wrap the prefix. */
18937 it->line_wrap = TRUNCATE;
18938 it->avoid_cursor_p = 1;
18939 }
18940 }
18941
18942 \f
18943
18944 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18945 only for R2L lines from display_line and display_string, when they
18946 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18947 the line/string needs to be continued on the next glyph row. */
18948 static void
18949 unproduce_glyphs (struct it *it, int n)
18950 {
18951 struct glyph *glyph, *end;
18952
18953 eassert (it->glyph_row);
18954 eassert (it->glyph_row->reversed_p);
18955 eassert (it->area == TEXT_AREA);
18956 eassert (n <= it->glyph_row->used[TEXT_AREA]);
18957
18958 if (n > it->glyph_row->used[TEXT_AREA])
18959 n = it->glyph_row->used[TEXT_AREA];
18960 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18961 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18962 for ( ; glyph < end; glyph++)
18963 glyph[-n] = *glyph;
18964 }
18965
18966 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18967 and ROW->maxpos. */
18968 static void
18969 find_row_edges (struct it *it, struct glyph_row *row,
18970 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18971 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18972 {
18973 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18974 lines' rows is implemented for bidi-reordered rows. */
18975
18976 /* ROW->minpos is the value of min_pos, the minimal buffer position
18977 we have in ROW, or ROW->start.pos if that is smaller. */
18978 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18979 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18980 else
18981 /* We didn't find buffer positions smaller than ROW->start, or
18982 didn't find _any_ valid buffer positions in any of the glyphs,
18983 so we must trust the iterator's computed positions. */
18984 row->minpos = row->start.pos;
18985 if (max_pos <= 0)
18986 {
18987 max_pos = CHARPOS (it->current.pos);
18988 max_bpos = BYTEPOS (it->current.pos);
18989 }
18990
18991 /* Here are the various use-cases for ending the row, and the
18992 corresponding values for ROW->maxpos:
18993
18994 Line ends in a newline from buffer eol_pos + 1
18995 Line is continued from buffer max_pos + 1
18996 Line is truncated on right it->current.pos
18997 Line ends in a newline from string max_pos + 1(*)
18998 (*) + 1 only when line ends in a forward scan
18999 Line is continued from string max_pos
19000 Line is continued from display vector max_pos
19001 Line is entirely from a string min_pos == max_pos
19002 Line is entirely from a display vector min_pos == max_pos
19003 Line that ends at ZV ZV
19004
19005 If you discover other use-cases, please add them here as
19006 appropriate. */
19007 if (row->ends_at_zv_p)
19008 row->maxpos = it->current.pos;
19009 else if (row->used[TEXT_AREA])
19010 {
19011 int seen_this_string = 0;
19012 struct glyph_row *r1 = row - 1;
19013
19014 /* Did we see the same display string on the previous row? */
19015 if (STRINGP (it->object)
19016 /* this is not the first row */
19017 && row > it->w->desired_matrix->rows
19018 /* previous row is not the header line */
19019 && !r1->mode_line_p
19020 /* previous row also ends in a newline from a string */
19021 && r1->ends_in_newline_from_string_p)
19022 {
19023 struct glyph *start, *end;
19024
19025 /* Search for the last glyph of the previous row that came
19026 from buffer or string. Depending on whether the row is
19027 L2R or R2L, we need to process it front to back or the
19028 other way round. */
19029 if (!r1->reversed_p)
19030 {
19031 start = r1->glyphs[TEXT_AREA];
19032 end = start + r1->used[TEXT_AREA];
19033 /* Glyphs inserted by redisplay have an integer (zero)
19034 as their object. */
19035 while (end > start
19036 && INTEGERP ((end - 1)->object)
19037 && (end - 1)->charpos <= 0)
19038 --end;
19039 if (end > start)
19040 {
19041 if (EQ ((end - 1)->object, it->object))
19042 seen_this_string = 1;
19043 }
19044 else
19045 /* If all the glyphs of the previous row were inserted
19046 by redisplay, it means the previous row was
19047 produced from a single newline, which is only
19048 possible if that newline came from the same string
19049 as the one which produced this ROW. */
19050 seen_this_string = 1;
19051 }
19052 else
19053 {
19054 end = r1->glyphs[TEXT_AREA] - 1;
19055 start = end + r1->used[TEXT_AREA];
19056 while (end < start
19057 && INTEGERP ((end + 1)->object)
19058 && (end + 1)->charpos <= 0)
19059 ++end;
19060 if (end < start)
19061 {
19062 if (EQ ((end + 1)->object, it->object))
19063 seen_this_string = 1;
19064 }
19065 else
19066 seen_this_string = 1;
19067 }
19068 }
19069 /* Take note of each display string that covers a newline only
19070 once, the first time we see it. This is for when a display
19071 string includes more than one newline in it. */
19072 if (row->ends_in_newline_from_string_p && !seen_this_string)
19073 {
19074 /* If we were scanning the buffer forward when we displayed
19075 the string, we want to account for at least one buffer
19076 position that belongs to this row (position covered by
19077 the display string), so that cursor positioning will
19078 consider this row as a candidate when point is at the end
19079 of the visual line represented by this row. This is not
19080 required when scanning back, because max_pos will already
19081 have a much larger value. */
19082 if (CHARPOS (row->end.pos) > max_pos)
19083 INC_BOTH (max_pos, max_bpos);
19084 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19085 }
19086 else if (CHARPOS (it->eol_pos) > 0)
19087 SET_TEXT_POS (row->maxpos,
19088 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19089 else if (row->continued_p)
19090 {
19091 /* If max_pos is different from IT's current position, it
19092 means IT->method does not belong to the display element
19093 at max_pos. However, it also means that the display
19094 element at max_pos was displayed in its entirety on this
19095 line, which is equivalent to saying that the next line
19096 starts at the next buffer position. */
19097 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19098 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19099 else
19100 {
19101 INC_BOTH (max_pos, max_bpos);
19102 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19103 }
19104 }
19105 else if (row->truncated_on_right_p)
19106 /* display_line already called reseat_at_next_visible_line_start,
19107 which puts the iterator at the beginning of the next line, in
19108 the logical order. */
19109 row->maxpos = it->current.pos;
19110 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19111 /* A line that is entirely from a string/image/stretch... */
19112 row->maxpos = row->minpos;
19113 else
19114 abort ();
19115 }
19116 else
19117 row->maxpos = it->current.pos;
19118 }
19119
19120 /* Construct the glyph row IT->glyph_row in the desired matrix of
19121 IT->w from text at the current position of IT. See dispextern.h
19122 for an overview of struct it. Value is non-zero if
19123 IT->glyph_row displays text, as opposed to a line displaying ZV
19124 only. */
19125
19126 static int
19127 display_line (struct it *it)
19128 {
19129 struct glyph_row *row = it->glyph_row;
19130 Lisp_Object overlay_arrow_string;
19131 struct it wrap_it;
19132 void *wrap_data = NULL;
19133 int may_wrap = 0, wrap_x IF_LINT (= 0);
19134 int wrap_row_used = -1;
19135 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19136 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19137 int wrap_row_extra_line_spacing IF_LINT (= 0);
19138 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19139 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19140 int cvpos;
19141 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19142 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19143
19144 /* We always start displaying at hpos zero even if hscrolled. */
19145 eassert (it->hpos == 0 && it->current_x == 0);
19146
19147 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19148 >= it->w->desired_matrix->nrows)
19149 {
19150 it->w->nrows_scale_factor++;
19151 fonts_changed_p = 1;
19152 return 0;
19153 }
19154
19155 /* Is IT->w showing the region? */
19156 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19157
19158 /* Clear the result glyph row and enable it. */
19159 prepare_desired_row (row);
19160
19161 row->y = it->current_y;
19162 row->start = it->start;
19163 row->continuation_lines_width = it->continuation_lines_width;
19164 row->displays_text_p = 1;
19165 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19166 it->starts_in_middle_of_char_p = 0;
19167
19168 /* Arrange the overlays nicely for our purposes. Usually, we call
19169 display_line on only one line at a time, in which case this
19170 can't really hurt too much, or we call it on lines which appear
19171 one after another in the buffer, in which case all calls to
19172 recenter_overlay_lists but the first will be pretty cheap. */
19173 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19174
19175 /* Move over display elements that are not visible because we are
19176 hscrolled. This may stop at an x-position < IT->first_visible_x
19177 if the first glyph is partially visible or if we hit a line end. */
19178 if (it->current_x < it->first_visible_x)
19179 {
19180 enum move_it_result move_result;
19181
19182 this_line_min_pos = row->start.pos;
19183 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19184 MOVE_TO_POS | MOVE_TO_X);
19185 /* If we are under a large hscroll, move_it_in_display_line_to
19186 could hit the end of the line without reaching
19187 it->first_visible_x. Pretend that we did reach it. This is
19188 especially important on a TTY, where we will call
19189 extend_face_to_end_of_line, which needs to know how many
19190 blank glyphs to produce. */
19191 if (it->current_x < it->first_visible_x
19192 && (move_result == MOVE_NEWLINE_OR_CR
19193 || move_result == MOVE_POS_MATCH_OR_ZV))
19194 it->current_x = it->first_visible_x;
19195
19196 /* Record the smallest positions seen while we moved over
19197 display elements that are not visible. This is needed by
19198 redisplay_internal for optimizing the case where the cursor
19199 stays inside the same line. The rest of this function only
19200 considers positions that are actually displayed, so
19201 RECORD_MAX_MIN_POS will not otherwise record positions that
19202 are hscrolled to the left of the left edge of the window. */
19203 min_pos = CHARPOS (this_line_min_pos);
19204 min_bpos = BYTEPOS (this_line_min_pos);
19205 }
19206 else
19207 {
19208 /* We only do this when not calling `move_it_in_display_line_to'
19209 above, because move_it_in_display_line_to calls
19210 handle_line_prefix itself. */
19211 handle_line_prefix (it);
19212 }
19213
19214 /* Get the initial row height. This is either the height of the
19215 text hscrolled, if there is any, or zero. */
19216 row->ascent = it->max_ascent;
19217 row->height = it->max_ascent + it->max_descent;
19218 row->phys_ascent = it->max_phys_ascent;
19219 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19220 row->extra_line_spacing = it->max_extra_line_spacing;
19221
19222 /* Utility macro to record max and min buffer positions seen until now. */
19223 #define RECORD_MAX_MIN_POS(IT) \
19224 do \
19225 { \
19226 int composition_p = !STRINGP ((IT)->string) \
19227 && ((IT)->what == IT_COMPOSITION); \
19228 ptrdiff_t current_pos = \
19229 composition_p ? (IT)->cmp_it.charpos \
19230 : IT_CHARPOS (*(IT)); \
19231 ptrdiff_t current_bpos = \
19232 composition_p ? CHAR_TO_BYTE (current_pos) \
19233 : IT_BYTEPOS (*(IT)); \
19234 if (current_pos < min_pos) \
19235 { \
19236 min_pos = current_pos; \
19237 min_bpos = current_bpos; \
19238 } \
19239 if (IT_CHARPOS (*it) > max_pos) \
19240 { \
19241 max_pos = IT_CHARPOS (*it); \
19242 max_bpos = IT_BYTEPOS (*it); \
19243 } \
19244 } \
19245 while (0)
19246
19247 /* Loop generating characters. The loop is left with IT on the next
19248 character to display. */
19249 while (1)
19250 {
19251 int n_glyphs_before, hpos_before, x_before;
19252 int x, nglyphs;
19253 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19254
19255 /* Retrieve the next thing to display. Value is zero if end of
19256 buffer reached. */
19257 if (!get_next_display_element (it))
19258 {
19259 /* Maybe add a space at the end of this line that is used to
19260 display the cursor there under X. Set the charpos of the
19261 first glyph of blank lines not corresponding to any text
19262 to -1. */
19263 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19264 row->exact_window_width_line_p = 1;
19265 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19266 || row->used[TEXT_AREA] == 0)
19267 {
19268 row->glyphs[TEXT_AREA]->charpos = -1;
19269 row->displays_text_p = 0;
19270
19271 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19272 && (!MINI_WINDOW_P (it->w)
19273 || (minibuf_level && EQ (it->window, minibuf_window))))
19274 row->indicate_empty_line_p = 1;
19275 }
19276
19277 it->continuation_lines_width = 0;
19278 row->ends_at_zv_p = 1;
19279 /* A row that displays right-to-left text must always have
19280 its last face extended all the way to the end of line,
19281 even if this row ends in ZV, because we still write to
19282 the screen left to right. We also need to extend the
19283 last face if the default face is remapped to some
19284 different face, otherwise the functions that clear
19285 portions of the screen will clear with the default face's
19286 background color. */
19287 if (row->reversed_p
19288 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19289 extend_face_to_end_of_line (it);
19290 break;
19291 }
19292
19293 /* Now, get the metrics of what we want to display. This also
19294 generates glyphs in `row' (which is IT->glyph_row). */
19295 n_glyphs_before = row->used[TEXT_AREA];
19296 x = it->current_x;
19297
19298 /* Remember the line height so far in case the next element doesn't
19299 fit on the line. */
19300 if (it->line_wrap != TRUNCATE)
19301 {
19302 ascent = it->max_ascent;
19303 descent = it->max_descent;
19304 phys_ascent = it->max_phys_ascent;
19305 phys_descent = it->max_phys_descent;
19306
19307 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19308 {
19309 if (IT_DISPLAYING_WHITESPACE (it))
19310 may_wrap = 1;
19311 else if (may_wrap)
19312 {
19313 SAVE_IT (wrap_it, *it, wrap_data);
19314 wrap_x = x;
19315 wrap_row_used = row->used[TEXT_AREA];
19316 wrap_row_ascent = row->ascent;
19317 wrap_row_height = row->height;
19318 wrap_row_phys_ascent = row->phys_ascent;
19319 wrap_row_phys_height = row->phys_height;
19320 wrap_row_extra_line_spacing = row->extra_line_spacing;
19321 wrap_row_min_pos = min_pos;
19322 wrap_row_min_bpos = min_bpos;
19323 wrap_row_max_pos = max_pos;
19324 wrap_row_max_bpos = max_bpos;
19325 may_wrap = 0;
19326 }
19327 }
19328 }
19329
19330 PRODUCE_GLYPHS (it);
19331
19332 /* If this display element was in marginal areas, continue with
19333 the next one. */
19334 if (it->area != TEXT_AREA)
19335 {
19336 row->ascent = max (row->ascent, it->max_ascent);
19337 row->height = max (row->height, it->max_ascent + it->max_descent);
19338 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19339 row->phys_height = max (row->phys_height,
19340 it->max_phys_ascent + it->max_phys_descent);
19341 row->extra_line_spacing = max (row->extra_line_spacing,
19342 it->max_extra_line_spacing);
19343 set_iterator_to_next (it, 1);
19344 continue;
19345 }
19346
19347 /* Does the display element fit on the line? If we truncate
19348 lines, we should draw past the right edge of the window. If
19349 we don't truncate, we want to stop so that we can display the
19350 continuation glyph before the right margin. If lines are
19351 continued, there are two possible strategies for characters
19352 resulting in more than 1 glyph (e.g. tabs): Display as many
19353 glyphs as possible in this line and leave the rest for the
19354 continuation line, or display the whole element in the next
19355 line. Original redisplay did the former, so we do it also. */
19356 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19357 hpos_before = it->hpos;
19358 x_before = x;
19359
19360 if (/* Not a newline. */
19361 nglyphs > 0
19362 /* Glyphs produced fit entirely in the line. */
19363 && it->current_x < it->last_visible_x)
19364 {
19365 it->hpos += nglyphs;
19366 row->ascent = max (row->ascent, it->max_ascent);
19367 row->height = max (row->height, it->max_ascent + it->max_descent);
19368 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19369 row->phys_height = max (row->phys_height,
19370 it->max_phys_ascent + it->max_phys_descent);
19371 row->extra_line_spacing = max (row->extra_line_spacing,
19372 it->max_extra_line_spacing);
19373 if (it->current_x - it->pixel_width < it->first_visible_x)
19374 row->x = x - it->first_visible_x;
19375 /* Record the maximum and minimum buffer positions seen so
19376 far in glyphs that will be displayed by this row. */
19377 if (it->bidi_p)
19378 RECORD_MAX_MIN_POS (it);
19379 }
19380 else
19381 {
19382 int i, new_x;
19383 struct glyph *glyph;
19384
19385 for (i = 0; i < nglyphs; ++i, x = new_x)
19386 {
19387 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19388 new_x = x + glyph->pixel_width;
19389
19390 if (/* Lines are continued. */
19391 it->line_wrap != TRUNCATE
19392 && (/* Glyph doesn't fit on the line. */
19393 new_x > it->last_visible_x
19394 /* Or it fits exactly on a window system frame. */
19395 || (new_x == it->last_visible_x
19396 && FRAME_WINDOW_P (it->f))))
19397 {
19398 /* End of a continued line. */
19399
19400 if (it->hpos == 0
19401 || (new_x == it->last_visible_x
19402 && FRAME_WINDOW_P (it->f)))
19403 {
19404 /* Current glyph is the only one on the line or
19405 fits exactly on the line. We must continue
19406 the line because we can't draw the cursor
19407 after the glyph. */
19408 row->continued_p = 1;
19409 it->current_x = new_x;
19410 it->continuation_lines_width += new_x;
19411 ++it->hpos;
19412 if (i == nglyphs - 1)
19413 {
19414 /* If line-wrap is on, check if a previous
19415 wrap point was found. */
19416 if (wrap_row_used > 0
19417 /* Even if there is a previous wrap
19418 point, continue the line here as
19419 usual, if (i) the previous character
19420 was a space or tab AND (ii) the
19421 current character is not. */
19422 && (!may_wrap
19423 || IT_DISPLAYING_WHITESPACE (it)))
19424 goto back_to_wrap;
19425
19426 /* Record the maximum and minimum buffer
19427 positions seen so far in glyphs that will be
19428 displayed by this row. */
19429 if (it->bidi_p)
19430 RECORD_MAX_MIN_POS (it);
19431 set_iterator_to_next (it, 1);
19432 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19433 {
19434 if (!get_next_display_element (it))
19435 {
19436 row->exact_window_width_line_p = 1;
19437 it->continuation_lines_width = 0;
19438 row->continued_p = 0;
19439 row->ends_at_zv_p = 1;
19440 }
19441 else if (ITERATOR_AT_END_OF_LINE_P (it))
19442 {
19443 row->continued_p = 0;
19444 row->exact_window_width_line_p = 1;
19445 }
19446 }
19447 }
19448 else if (it->bidi_p)
19449 RECORD_MAX_MIN_POS (it);
19450 }
19451 else if (CHAR_GLYPH_PADDING_P (*glyph)
19452 && !FRAME_WINDOW_P (it->f))
19453 {
19454 /* A padding glyph that doesn't fit on this line.
19455 This means the whole character doesn't fit
19456 on the line. */
19457 if (row->reversed_p)
19458 unproduce_glyphs (it, row->used[TEXT_AREA]
19459 - n_glyphs_before);
19460 row->used[TEXT_AREA] = n_glyphs_before;
19461
19462 /* Fill the rest of the row with continuation
19463 glyphs like in 20.x. */
19464 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19465 < row->glyphs[1 + TEXT_AREA])
19466 produce_special_glyphs (it, IT_CONTINUATION);
19467
19468 row->continued_p = 1;
19469 it->current_x = x_before;
19470 it->continuation_lines_width += x_before;
19471
19472 /* Restore the height to what it was before the
19473 element not fitting on the line. */
19474 it->max_ascent = ascent;
19475 it->max_descent = descent;
19476 it->max_phys_ascent = phys_ascent;
19477 it->max_phys_descent = phys_descent;
19478 }
19479 else if (wrap_row_used > 0)
19480 {
19481 back_to_wrap:
19482 if (row->reversed_p)
19483 unproduce_glyphs (it,
19484 row->used[TEXT_AREA] - wrap_row_used);
19485 RESTORE_IT (it, &wrap_it, wrap_data);
19486 it->continuation_lines_width += wrap_x;
19487 row->used[TEXT_AREA] = wrap_row_used;
19488 row->ascent = wrap_row_ascent;
19489 row->height = wrap_row_height;
19490 row->phys_ascent = wrap_row_phys_ascent;
19491 row->phys_height = wrap_row_phys_height;
19492 row->extra_line_spacing = wrap_row_extra_line_spacing;
19493 min_pos = wrap_row_min_pos;
19494 min_bpos = wrap_row_min_bpos;
19495 max_pos = wrap_row_max_pos;
19496 max_bpos = wrap_row_max_bpos;
19497 row->continued_p = 1;
19498 row->ends_at_zv_p = 0;
19499 row->exact_window_width_line_p = 0;
19500 it->continuation_lines_width += x;
19501
19502 /* Make sure that a non-default face is extended
19503 up to the right margin of the window. */
19504 extend_face_to_end_of_line (it);
19505 }
19506 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19507 {
19508 /* A TAB that extends past the right edge of the
19509 window. This produces a single glyph on
19510 window system frames. We leave the glyph in
19511 this row and let it fill the row, but don't
19512 consume the TAB. */
19513 it->continuation_lines_width += it->last_visible_x;
19514 row->ends_in_middle_of_char_p = 1;
19515 row->continued_p = 1;
19516 glyph->pixel_width = it->last_visible_x - x;
19517 it->starts_in_middle_of_char_p = 1;
19518 }
19519 else
19520 {
19521 /* Something other than a TAB that draws past
19522 the right edge of the window. Restore
19523 positions to values before the element. */
19524 if (row->reversed_p)
19525 unproduce_glyphs (it, row->used[TEXT_AREA]
19526 - (n_glyphs_before + i));
19527 row->used[TEXT_AREA] = n_glyphs_before + i;
19528
19529 /* Display continuation glyphs. */
19530 if (!FRAME_WINDOW_P (it->f))
19531 produce_special_glyphs (it, IT_CONTINUATION);
19532 row->continued_p = 1;
19533
19534 it->current_x = x_before;
19535 it->continuation_lines_width += x;
19536 extend_face_to_end_of_line (it);
19537
19538 if (nglyphs > 1 && i > 0)
19539 {
19540 row->ends_in_middle_of_char_p = 1;
19541 it->starts_in_middle_of_char_p = 1;
19542 }
19543
19544 /* Restore the height to what it was before the
19545 element not fitting on the line. */
19546 it->max_ascent = ascent;
19547 it->max_descent = descent;
19548 it->max_phys_ascent = phys_ascent;
19549 it->max_phys_descent = phys_descent;
19550 }
19551
19552 break;
19553 }
19554 else if (new_x > it->first_visible_x)
19555 {
19556 /* Increment number of glyphs actually displayed. */
19557 ++it->hpos;
19558
19559 /* Record the maximum and minimum buffer positions
19560 seen so far in glyphs that will be displayed by
19561 this row. */
19562 if (it->bidi_p)
19563 RECORD_MAX_MIN_POS (it);
19564
19565 if (x < it->first_visible_x)
19566 /* Glyph is partially visible, i.e. row starts at
19567 negative X position. */
19568 row->x = x - it->first_visible_x;
19569 }
19570 else
19571 {
19572 /* Glyph is completely off the left margin of the
19573 window. This should not happen because of the
19574 move_it_in_display_line at the start of this
19575 function, unless the text display area of the
19576 window is empty. */
19577 eassert (it->first_visible_x <= it->last_visible_x);
19578 }
19579 }
19580 /* Even if this display element produced no glyphs at all,
19581 we want to record its position. */
19582 if (it->bidi_p && nglyphs == 0)
19583 RECORD_MAX_MIN_POS (it);
19584
19585 row->ascent = max (row->ascent, it->max_ascent);
19586 row->height = max (row->height, it->max_ascent + it->max_descent);
19587 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19588 row->phys_height = max (row->phys_height,
19589 it->max_phys_ascent + it->max_phys_descent);
19590 row->extra_line_spacing = max (row->extra_line_spacing,
19591 it->max_extra_line_spacing);
19592
19593 /* End of this display line if row is continued. */
19594 if (row->continued_p || row->ends_at_zv_p)
19595 break;
19596 }
19597
19598 at_end_of_line:
19599 /* Is this a line end? If yes, we're also done, after making
19600 sure that a non-default face is extended up to the right
19601 margin of the window. */
19602 if (ITERATOR_AT_END_OF_LINE_P (it))
19603 {
19604 int used_before = row->used[TEXT_AREA];
19605
19606 row->ends_in_newline_from_string_p = STRINGP (it->object);
19607
19608 /* Add a space at the end of the line that is used to
19609 display the cursor there. */
19610 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19611 append_space_for_newline (it, 0);
19612
19613 /* Extend the face to the end of the line. */
19614 extend_face_to_end_of_line (it);
19615
19616 /* Make sure we have the position. */
19617 if (used_before == 0)
19618 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19619
19620 /* Record the position of the newline, for use in
19621 find_row_edges. */
19622 it->eol_pos = it->current.pos;
19623
19624 /* Consume the line end. This skips over invisible lines. */
19625 set_iterator_to_next (it, 1);
19626 it->continuation_lines_width = 0;
19627 break;
19628 }
19629
19630 /* Proceed with next display element. Note that this skips
19631 over lines invisible because of selective display. */
19632 set_iterator_to_next (it, 1);
19633
19634 /* If we truncate lines, we are done when the last displayed
19635 glyphs reach past the right margin of the window. */
19636 if (it->line_wrap == TRUNCATE
19637 && (FRAME_WINDOW_P (it->f)
19638 ? (it->current_x >= it->last_visible_x)
19639 : (it->current_x > it->last_visible_x)))
19640 {
19641 /* Maybe add truncation glyphs. */
19642 if (!FRAME_WINDOW_P (it->f))
19643 {
19644 int i, n;
19645
19646 if (!row->reversed_p)
19647 {
19648 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19649 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19650 break;
19651 }
19652 else
19653 {
19654 for (i = 0; i < row->used[TEXT_AREA]; i++)
19655 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19656 break;
19657 /* Remove any padding glyphs at the front of ROW, to
19658 make room for the truncation glyphs we will be
19659 adding below. The loop below always inserts at
19660 least one truncation glyph, so also remove the
19661 last glyph added to ROW. */
19662 unproduce_glyphs (it, i + 1);
19663 /* Adjust i for the loop below. */
19664 i = row->used[TEXT_AREA] - (i + 1);
19665 }
19666
19667 for (n = row->used[TEXT_AREA]; i < n; ++i)
19668 {
19669 row->used[TEXT_AREA] = i;
19670 produce_special_glyphs (it, IT_TRUNCATION);
19671 }
19672 }
19673 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19674 {
19675 /* Don't truncate if we can overflow newline into fringe. */
19676 if (!get_next_display_element (it))
19677 {
19678 it->continuation_lines_width = 0;
19679 row->ends_at_zv_p = 1;
19680 row->exact_window_width_line_p = 1;
19681 break;
19682 }
19683 if (ITERATOR_AT_END_OF_LINE_P (it))
19684 {
19685 row->exact_window_width_line_p = 1;
19686 goto at_end_of_line;
19687 }
19688 }
19689
19690 row->truncated_on_right_p = 1;
19691 it->continuation_lines_width = 0;
19692 reseat_at_next_visible_line_start (it, 0);
19693 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19694 it->hpos = hpos_before;
19695 it->current_x = x_before;
19696 break;
19697 }
19698 }
19699
19700 if (wrap_data)
19701 bidi_unshelve_cache (wrap_data, 1);
19702
19703 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19704 at the left window margin. */
19705 if (it->first_visible_x
19706 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19707 {
19708 if (!FRAME_WINDOW_P (it->f))
19709 insert_left_trunc_glyphs (it);
19710 row->truncated_on_left_p = 1;
19711 }
19712
19713 /* Remember the position at which this line ends.
19714
19715 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19716 cannot be before the call to find_row_edges below, since that is
19717 where these positions are determined. */
19718 row->end = it->current;
19719 if (!it->bidi_p)
19720 {
19721 row->minpos = row->start.pos;
19722 row->maxpos = row->end.pos;
19723 }
19724 else
19725 {
19726 /* ROW->minpos and ROW->maxpos must be the smallest and
19727 `1 + the largest' buffer positions in ROW. But if ROW was
19728 bidi-reordered, these two positions can be anywhere in the
19729 row, so we must determine them now. */
19730 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19731 }
19732
19733 /* If the start of this line is the overlay arrow-position, then
19734 mark this glyph row as the one containing the overlay arrow.
19735 This is clearly a mess with variable size fonts. It would be
19736 better to let it be displayed like cursors under X. */
19737 if ((row->displays_text_p || !overlay_arrow_seen)
19738 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19739 !NILP (overlay_arrow_string)))
19740 {
19741 /* Overlay arrow in window redisplay is a fringe bitmap. */
19742 if (STRINGP (overlay_arrow_string))
19743 {
19744 struct glyph_row *arrow_row
19745 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19746 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19747 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19748 struct glyph *p = row->glyphs[TEXT_AREA];
19749 struct glyph *p2, *end;
19750
19751 /* Copy the arrow glyphs. */
19752 while (glyph < arrow_end)
19753 *p++ = *glyph++;
19754
19755 /* Throw away padding glyphs. */
19756 p2 = p;
19757 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19758 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19759 ++p2;
19760 if (p2 > p)
19761 {
19762 while (p2 < end)
19763 *p++ = *p2++;
19764 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19765 }
19766 }
19767 else
19768 {
19769 eassert (INTEGERP (overlay_arrow_string));
19770 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19771 }
19772 overlay_arrow_seen = 1;
19773 }
19774
19775 /* Highlight trailing whitespace. */
19776 if (!NILP (Vshow_trailing_whitespace))
19777 highlight_trailing_whitespace (it->f, it->glyph_row);
19778
19779 /* Compute pixel dimensions of this line. */
19780 compute_line_metrics (it);
19781
19782 /* Implementation note: No changes in the glyphs of ROW or in their
19783 faces can be done past this point, because compute_line_metrics
19784 computes ROW's hash value and stores it within the glyph_row
19785 structure. */
19786
19787 /* Record whether this row ends inside an ellipsis. */
19788 row->ends_in_ellipsis_p
19789 = (it->method == GET_FROM_DISPLAY_VECTOR
19790 && it->ellipsis_p);
19791
19792 /* Save fringe bitmaps in this row. */
19793 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19794 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19795 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19796 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19797
19798 it->left_user_fringe_bitmap = 0;
19799 it->left_user_fringe_face_id = 0;
19800 it->right_user_fringe_bitmap = 0;
19801 it->right_user_fringe_face_id = 0;
19802
19803 /* Maybe set the cursor. */
19804 cvpos = it->w->cursor.vpos;
19805 if ((cvpos < 0
19806 /* In bidi-reordered rows, keep checking for proper cursor
19807 position even if one has been found already, because buffer
19808 positions in such rows change non-linearly with ROW->VPOS,
19809 when a line is continued. One exception: when we are at ZV,
19810 display cursor on the first suitable glyph row, since all
19811 the empty rows after that also have their position set to ZV. */
19812 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19813 lines' rows is implemented for bidi-reordered rows. */
19814 || (it->bidi_p
19815 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19816 && PT >= MATRIX_ROW_START_CHARPOS (row)
19817 && PT <= MATRIX_ROW_END_CHARPOS (row)
19818 && cursor_row_p (row))
19819 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19820
19821 /* Prepare for the next line. This line starts horizontally at (X
19822 HPOS) = (0 0). Vertical positions are incremented. As a
19823 convenience for the caller, IT->glyph_row is set to the next
19824 row to be used. */
19825 it->current_x = it->hpos = 0;
19826 it->current_y += row->height;
19827 SET_TEXT_POS (it->eol_pos, 0, 0);
19828 ++it->vpos;
19829 ++it->glyph_row;
19830 /* The next row should by default use the same value of the
19831 reversed_p flag as this one. set_iterator_to_next decides when
19832 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19833 the flag accordingly. */
19834 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19835 it->glyph_row->reversed_p = row->reversed_p;
19836 it->start = row->end;
19837 return row->displays_text_p;
19838
19839 #undef RECORD_MAX_MIN_POS
19840 }
19841
19842 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19843 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19844 doc: /* Return paragraph direction at point in BUFFER.
19845 Value is either `left-to-right' or `right-to-left'.
19846 If BUFFER is omitted or nil, it defaults to the current buffer.
19847
19848 Paragraph direction determines how the text in the paragraph is displayed.
19849 In left-to-right paragraphs, text begins at the left margin of the window
19850 and the reading direction is generally left to right. In right-to-left
19851 paragraphs, text begins at the right margin and is read from right to left.
19852
19853 See also `bidi-paragraph-direction'. */)
19854 (Lisp_Object buffer)
19855 {
19856 struct buffer *buf = current_buffer;
19857 struct buffer *old = buf;
19858
19859 if (! NILP (buffer))
19860 {
19861 CHECK_BUFFER (buffer);
19862 buf = XBUFFER (buffer);
19863 }
19864
19865 if (NILP (BVAR (buf, bidi_display_reordering))
19866 || NILP (BVAR (buf, enable_multibyte_characters))
19867 /* When we are loading loadup.el, the character property tables
19868 needed for bidi iteration are not yet available. */
19869 || !NILP (Vpurify_flag))
19870 return Qleft_to_right;
19871 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19872 return BVAR (buf, bidi_paragraph_direction);
19873 else
19874 {
19875 /* Determine the direction from buffer text. We could try to
19876 use current_matrix if it is up to date, but this seems fast
19877 enough as it is. */
19878 struct bidi_it itb;
19879 ptrdiff_t pos = BUF_PT (buf);
19880 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19881 int c;
19882 void *itb_data = bidi_shelve_cache ();
19883
19884 set_buffer_temp (buf);
19885 /* bidi_paragraph_init finds the base direction of the paragraph
19886 by searching forward from paragraph start. We need the base
19887 direction of the current or _previous_ paragraph, so we need
19888 to make sure we are within that paragraph. To that end, find
19889 the previous non-empty line. */
19890 if (pos >= ZV && pos > BEGV)
19891 {
19892 pos--;
19893 bytepos = CHAR_TO_BYTE (pos);
19894 }
19895 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19896 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19897 {
19898 while ((c = FETCH_BYTE (bytepos)) == '\n'
19899 || c == ' ' || c == '\t' || c == '\f')
19900 {
19901 if (bytepos <= BEGV_BYTE)
19902 break;
19903 bytepos--;
19904 pos--;
19905 }
19906 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19907 bytepos--;
19908 }
19909 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19910 itb.paragraph_dir = NEUTRAL_DIR;
19911 itb.string.s = NULL;
19912 itb.string.lstring = Qnil;
19913 itb.string.bufpos = 0;
19914 itb.string.unibyte = 0;
19915 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19916 bidi_unshelve_cache (itb_data, 0);
19917 set_buffer_temp (old);
19918 switch (itb.paragraph_dir)
19919 {
19920 case L2R:
19921 return Qleft_to_right;
19922 break;
19923 case R2L:
19924 return Qright_to_left;
19925 break;
19926 default:
19927 abort ();
19928 }
19929 }
19930 }
19931
19932
19933 \f
19934 /***********************************************************************
19935 Menu Bar
19936 ***********************************************************************/
19937
19938 /* Redisplay the menu bar in the frame for window W.
19939
19940 The menu bar of X frames that don't have X toolkit support is
19941 displayed in a special window W->frame->menu_bar_window.
19942
19943 The menu bar of terminal frames is treated specially as far as
19944 glyph matrices are concerned. Menu bar lines are not part of
19945 windows, so the update is done directly on the frame matrix rows
19946 for the menu bar. */
19947
19948 static void
19949 display_menu_bar (struct window *w)
19950 {
19951 struct frame *f = XFRAME (WINDOW_FRAME (w));
19952 struct it it;
19953 Lisp_Object items;
19954 int i;
19955
19956 /* Don't do all this for graphical frames. */
19957 #ifdef HAVE_NTGUI
19958 if (FRAME_W32_P (f))
19959 return;
19960 #endif
19961 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19962 if (FRAME_X_P (f))
19963 return;
19964 #endif
19965
19966 #ifdef HAVE_NS
19967 if (FRAME_NS_P (f))
19968 return;
19969 #endif /* HAVE_NS */
19970
19971 #ifdef USE_X_TOOLKIT
19972 eassert (!FRAME_WINDOW_P (f));
19973 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19974 it.first_visible_x = 0;
19975 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19976 #else /* not USE_X_TOOLKIT */
19977 if (FRAME_WINDOW_P (f))
19978 {
19979 /* Menu bar lines are displayed in the desired matrix of the
19980 dummy window menu_bar_window. */
19981 struct window *menu_w;
19982 eassert (WINDOWP (f->menu_bar_window));
19983 menu_w = XWINDOW (f->menu_bar_window);
19984 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19985 MENU_FACE_ID);
19986 it.first_visible_x = 0;
19987 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19988 }
19989 else
19990 {
19991 /* This is a TTY frame, i.e. character hpos/vpos are used as
19992 pixel x/y. */
19993 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19994 MENU_FACE_ID);
19995 it.first_visible_x = 0;
19996 it.last_visible_x = FRAME_COLS (f);
19997 }
19998 #endif /* not USE_X_TOOLKIT */
19999
20000 /* FIXME: This should be controlled by a user option. See the
20001 comments in redisplay_tool_bar and display_mode_line about
20002 this. */
20003 it.paragraph_embedding = L2R;
20004
20005 if (! mode_line_inverse_video)
20006 /* Force the menu-bar to be displayed in the default face. */
20007 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20008
20009 /* Clear all rows of the menu bar. */
20010 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20011 {
20012 struct glyph_row *row = it.glyph_row + i;
20013 clear_glyph_row (row);
20014 row->enabled_p = 1;
20015 row->full_width_p = 1;
20016 }
20017
20018 /* Display all items of the menu bar. */
20019 items = FRAME_MENU_BAR_ITEMS (it.f);
20020 for (i = 0; i < ASIZE (items); i += 4)
20021 {
20022 Lisp_Object string;
20023
20024 /* Stop at nil string. */
20025 string = AREF (items, i + 1);
20026 if (NILP (string))
20027 break;
20028
20029 /* Remember where item was displayed. */
20030 ASET (items, i + 3, make_number (it.hpos));
20031
20032 /* Display the item, pad with one space. */
20033 if (it.current_x < it.last_visible_x)
20034 display_string (NULL, string, Qnil, 0, 0, &it,
20035 SCHARS (string) + 1, 0, 0, -1);
20036 }
20037
20038 /* Fill out the line with spaces. */
20039 if (it.current_x < it.last_visible_x)
20040 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20041
20042 /* Compute the total height of the lines. */
20043 compute_line_metrics (&it);
20044 }
20045
20046
20047 \f
20048 /***********************************************************************
20049 Mode Line
20050 ***********************************************************************/
20051
20052 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20053 FORCE is non-zero, redisplay mode lines unconditionally.
20054 Otherwise, redisplay only mode lines that are garbaged. Value is
20055 the number of windows whose mode lines were redisplayed. */
20056
20057 static int
20058 redisplay_mode_lines (Lisp_Object window, int force)
20059 {
20060 int nwindows = 0;
20061
20062 while (!NILP (window))
20063 {
20064 struct window *w = XWINDOW (window);
20065
20066 if (WINDOWP (w->hchild))
20067 nwindows += redisplay_mode_lines (w->hchild, force);
20068 else if (WINDOWP (w->vchild))
20069 nwindows += redisplay_mode_lines (w->vchild, force);
20070 else if (force
20071 || FRAME_GARBAGED_P (XFRAME (w->frame))
20072 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20073 {
20074 struct text_pos lpoint;
20075 struct buffer *old = current_buffer;
20076
20077 /* Set the window's buffer for the mode line display. */
20078 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20079 set_buffer_internal_1 (XBUFFER (w->buffer));
20080
20081 /* Point refers normally to the selected window. For any
20082 other window, set up appropriate value. */
20083 if (!EQ (window, selected_window))
20084 {
20085 struct text_pos pt;
20086
20087 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20088 if (CHARPOS (pt) < BEGV)
20089 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20090 else if (CHARPOS (pt) > (ZV - 1))
20091 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20092 else
20093 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20094 }
20095
20096 /* Display mode lines. */
20097 clear_glyph_matrix (w->desired_matrix);
20098 if (display_mode_lines (w))
20099 {
20100 ++nwindows;
20101 w->must_be_updated_p = 1;
20102 }
20103
20104 /* Restore old settings. */
20105 set_buffer_internal_1 (old);
20106 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20107 }
20108
20109 window = w->next;
20110 }
20111
20112 return nwindows;
20113 }
20114
20115
20116 /* Display the mode and/or header line of window W. Value is the
20117 sum number of mode lines and header lines displayed. */
20118
20119 static int
20120 display_mode_lines (struct window *w)
20121 {
20122 Lisp_Object old_selected_window, old_selected_frame;
20123 int n = 0;
20124
20125 old_selected_frame = selected_frame;
20126 selected_frame = w->frame;
20127 old_selected_window = selected_window;
20128 XSETWINDOW (selected_window, w);
20129
20130 /* These will be set while the mode line specs are processed. */
20131 line_number_displayed = 0;
20132 w->column_number_displayed = Qnil;
20133
20134 if (WINDOW_WANTS_MODELINE_P (w))
20135 {
20136 struct window *sel_w = XWINDOW (old_selected_window);
20137
20138 /* Select mode line face based on the real selected window. */
20139 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20140 BVAR (current_buffer, mode_line_format));
20141 ++n;
20142 }
20143
20144 if (WINDOW_WANTS_HEADER_LINE_P (w))
20145 {
20146 display_mode_line (w, HEADER_LINE_FACE_ID,
20147 BVAR (current_buffer, header_line_format));
20148 ++n;
20149 }
20150
20151 selected_frame = old_selected_frame;
20152 selected_window = old_selected_window;
20153 return n;
20154 }
20155
20156
20157 /* Display mode or header line of window W. FACE_ID specifies which
20158 line to display; it is either MODE_LINE_FACE_ID or
20159 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20160 display. Value is the pixel height of the mode/header line
20161 displayed. */
20162
20163 static int
20164 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20165 {
20166 struct it it;
20167 struct face *face;
20168 ptrdiff_t count = SPECPDL_INDEX ();
20169
20170 init_iterator (&it, w, -1, -1, NULL, face_id);
20171 /* Don't extend on a previously drawn mode-line.
20172 This may happen if called from pos_visible_p. */
20173 it.glyph_row->enabled_p = 0;
20174 prepare_desired_row (it.glyph_row);
20175
20176 it.glyph_row->mode_line_p = 1;
20177
20178 if (! mode_line_inverse_video)
20179 /* Force the mode-line to be displayed in the default face. */
20180 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20181
20182 /* FIXME: This should be controlled by a user option. But
20183 supporting such an option is not trivial, since the mode line is
20184 made up of many separate strings. */
20185 it.paragraph_embedding = L2R;
20186
20187 record_unwind_protect (unwind_format_mode_line,
20188 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20189
20190 mode_line_target = MODE_LINE_DISPLAY;
20191
20192 /* Temporarily make frame's keyboard the current kboard so that
20193 kboard-local variables in the mode_line_format will get the right
20194 values. */
20195 push_kboard (FRAME_KBOARD (it.f));
20196 record_unwind_save_match_data ();
20197 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20198 pop_kboard ();
20199
20200 unbind_to (count, Qnil);
20201
20202 /* Fill up with spaces. */
20203 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20204
20205 compute_line_metrics (&it);
20206 it.glyph_row->full_width_p = 1;
20207 it.glyph_row->continued_p = 0;
20208 it.glyph_row->truncated_on_left_p = 0;
20209 it.glyph_row->truncated_on_right_p = 0;
20210
20211 /* Make a 3D mode-line have a shadow at its right end. */
20212 face = FACE_FROM_ID (it.f, face_id);
20213 extend_face_to_end_of_line (&it);
20214 if (face->box != FACE_NO_BOX)
20215 {
20216 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20217 + it.glyph_row->used[TEXT_AREA] - 1);
20218 last->right_box_line_p = 1;
20219 }
20220
20221 return it.glyph_row->height;
20222 }
20223
20224 /* Move element ELT in LIST to the front of LIST.
20225 Return the updated list. */
20226
20227 static Lisp_Object
20228 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20229 {
20230 register Lisp_Object tail, prev;
20231 register Lisp_Object tem;
20232
20233 tail = list;
20234 prev = Qnil;
20235 while (CONSP (tail))
20236 {
20237 tem = XCAR (tail);
20238
20239 if (EQ (elt, tem))
20240 {
20241 /* Splice out the link TAIL. */
20242 if (NILP (prev))
20243 list = XCDR (tail);
20244 else
20245 Fsetcdr (prev, XCDR (tail));
20246
20247 /* Now make it the first. */
20248 Fsetcdr (tail, list);
20249 return tail;
20250 }
20251 else
20252 prev = tail;
20253 tail = XCDR (tail);
20254 QUIT;
20255 }
20256
20257 /* Not found--return unchanged LIST. */
20258 return list;
20259 }
20260
20261 /* Contribute ELT to the mode line for window IT->w. How it
20262 translates into text depends on its data type.
20263
20264 IT describes the display environment in which we display, as usual.
20265
20266 DEPTH is the depth in recursion. It is used to prevent
20267 infinite recursion here.
20268
20269 FIELD_WIDTH is the number of characters the display of ELT should
20270 occupy in the mode line, and PRECISION is the maximum number of
20271 characters to display from ELT's representation. See
20272 display_string for details.
20273
20274 Returns the hpos of the end of the text generated by ELT.
20275
20276 PROPS is a property list to add to any string we encounter.
20277
20278 If RISKY is nonzero, remove (disregard) any properties in any string
20279 we encounter, and ignore :eval and :propertize.
20280
20281 The global variable `mode_line_target' determines whether the
20282 output is passed to `store_mode_line_noprop',
20283 `store_mode_line_string', or `display_string'. */
20284
20285 static int
20286 display_mode_element (struct it *it, int depth, int field_width, int precision,
20287 Lisp_Object elt, Lisp_Object props, int risky)
20288 {
20289 int n = 0, field, prec;
20290 int literal = 0;
20291
20292 tail_recurse:
20293 if (depth > 100)
20294 elt = build_string ("*too-deep*");
20295
20296 depth++;
20297
20298 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20299 {
20300 case Lisp_String:
20301 {
20302 /* A string: output it and check for %-constructs within it. */
20303 unsigned char c;
20304 ptrdiff_t offset = 0;
20305
20306 if (SCHARS (elt) > 0
20307 && (!NILP (props) || risky))
20308 {
20309 Lisp_Object oprops, aelt;
20310 oprops = Ftext_properties_at (make_number (0), elt);
20311
20312 /* If the starting string's properties are not what
20313 we want, translate the string. Also, if the string
20314 is risky, do that anyway. */
20315
20316 if (NILP (Fequal (props, oprops)) || risky)
20317 {
20318 /* If the starting string has properties,
20319 merge the specified ones onto the existing ones. */
20320 if (! NILP (oprops) && !risky)
20321 {
20322 Lisp_Object tem;
20323
20324 oprops = Fcopy_sequence (oprops);
20325 tem = props;
20326 while (CONSP (tem))
20327 {
20328 oprops = Fplist_put (oprops, XCAR (tem),
20329 XCAR (XCDR (tem)));
20330 tem = XCDR (XCDR (tem));
20331 }
20332 props = oprops;
20333 }
20334
20335 aelt = Fassoc (elt, mode_line_proptrans_alist);
20336 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20337 {
20338 /* AELT is what we want. Move it to the front
20339 without consing. */
20340 elt = XCAR (aelt);
20341 mode_line_proptrans_alist
20342 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20343 }
20344 else
20345 {
20346 Lisp_Object tem;
20347
20348 /* If AELT has the wrong props, it is useless.
20349 so get rid of it. */
20350 if (! NILP (aelt))
20351 mode_line_proptrans_alist
20352 = Fdelq (aelt, mode_line_proptrans_alist);
20353
20354 elt = Fcopy_sequence (elt);
20355 Fset_text_properties (make_number (0), Flength (elt),
20356 props, elt);
20357 /* Add this item to mode_line_proptrans_alist. */
20358 mode_line_proptrans_alist
20359 = Fcons (Fcons (elt, props),
20360 mode_line_proptrans_alist);
20361 /* Truncate mode_line_proptrans_alist
20362 to at most 50 elements. */
20363 tem = Fnthcdr (make_number (50),
20364 mode_line_proptrans_alist);
20365 if (! NILP (tem))
20366 XSETCDR (tem, Qnil);
20367 }
20368 }
20369 }
20370
20371 offset = 0;
20372
20373 if (literal)
20374 {
20375 prec = precision - n;
20376 switch (mode_line_target)
20377 {
20378 case MODE_LINE_NOPROP:
20379 case MODE_LINE_TITLE:
20380 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20381 break;
20382 case MODE_LINE_STRING:
20383 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20384 break;
20385 case MODE_LINE_DISPLAY:
20386 n += display_string (NULL, elt, Qnil, 0, 0, it,
20387 0, prec, 0, STRING_MULTIBYTE (elt));
20388 break;
20389 }
20390
20391 break;
20392 }
20393
20394 /* Handle the non-literal case. */
20395
20396 while ((precision <= 0 || n < precision)
20397 && SREF (elt, offset) != 0
20398 && (mode_line_target != MODE_LINE_DISPLAY
20399 || it->current_x < it->last_visible_x))
20400 {
20401 ptrdiff_t last_offset = offset;
20402
20403 /* Advance to end of string or next format specifier. */
20404 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20405 ;
20406
20407 if (offset - 1 != last_offset)
20408 {
20409 ptrdiff_t nchars, nbytes;
20410
20411 /* Output to end of string or up to '%'. Field width
20412 is length of string. Don't output more than
20413 PRECISION allows us. */
20414 offset--;
20415
20416 prec = c_string_width (SDATA (elt) + last_offset,
20417 offset - last_offset, precision - n,
20418 &nchars, &nbytes);
20419
20420 switch (mode_line_target)
20421 {
20422 case MODE_LINE_NOPROP:
20423 case MODE_LINE_TITLE:
20424 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20425 break;
20426 case MODE_LINE_STRING:
20427 {
20428 ptrdiff_t bytepos = last_offset;
20429 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20430 ptrdiff_t endpos = (precision <= 0
20431 ? string_byte_to_char (elt, offset)
20432 : charpos + nchars);
20433
20434 n += store_mode_line_string (NULL,
20435 Fsubstring (elt, make_number (charpos),
20436 make_number (endpos)),
20437 0, 0, 0, Qnil);
20438 }
20439 break;
20440 case MODE_LINE_DISPLAY:
20441 {
20442 ptrdiff_t bytepos = last_offset;
20443 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20444
20445 if (precision <= 0)
20446 nchars = string_byte_to_char (elt, offset) - charpos;
20447 n += display_string (NULL, elt, Qnil, 0, charpos,
20448 it, 0, nchars, 0,
20449 STRING_MULTIBYTE (elt));
20450 }
20451 break;
20452 }
20453 }
20454 else /* c == '%' */
20455 {
20456 ptrdiff_t percent_position = offset;
20457
20458 /* Get the specified minimum width. Zero means
20459 don't pad. */
20460 field = 0;
20461 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20462 field = field * 10 + c - '0';
20463
20464 /* Don't pad beyond the total padding allowed. */
20465 if (field_width - n > 0 && field > field_width - n)
20466 field = field_width - n;
20467
20468 /* Note that either PRECISION <= 0 or N < PRECISION. */
20469 prec = precision - n;
20470
20471 if (c == 'M')
20472 n += display_mode_element (it, depth, field, prec,
20473 Vglobal_mode_string, props,
20474 risky);
20475 else if (c != 0)
20476 {
20477 int multibyte;
20478 ptrdiff_t bytepos, charpos;
20479 const char *spec;
20480 Lisp_Object string;
20481
20482 bytepos = percent_position;
20483 charpos = (STRING_MULTIBYTE (elt)
20484 ? string_byte_to_char (elt, bytepos)
20485 : bytepos);
20486 spec = decode_mode_spec (it->w, c, field, &string);
20487 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20488
20489 switch (mode_line_target)
20490 {
20491 case MODE_LINE_NOPROP:
20492 case MODE_LINE_TITLE:
20493 n += store_mode_line_noprop (spec, field, prec);
20494 break;
20495 case MODE_LINE_STRING:
20496 {
20497 Lisp_Object tem = build_string (spec);
20498 props = Ftext_properties_at (make_number (charpos), elt);
20499 /* Should only keep face property in props */
20500 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20501 }
20502 break;
20503 case MODE_LINE_DISPLAY:
20504 {
20505 int nglyphs_before, nwritten;
20506
20507 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20508 nwritten = display_string (spec, string, elt,
20509 charpos, 0, it,
20510 field, prec, 0,
20511 multibyte);
20512
20513 /* Assign to the glyphs written above the
20514 string where the `%x' came from, position
20515 of the `%'. */
20516 if (nwritten > 0)
20517 {
20518 struct glyph *glyph
20519 = (it->glyph_row->glyphs[TEXT_AREA]
20520 + nglyphs_before);
20521 int i;
20522
20523 for (i = 0; i < nwritten; ++i)
20524 {
20525 glyph[i].object = elt;
20526 glyph[i].charpos = charpos;
20527 }
20528
20529 n += nwritten;
20530 }
20531 }
20532 break;
20533 }
20534 }
20535 else /* c == 0 */
20536 break;
20537 }
20538 }
20539 }
20540 break;
20541
20542 case Lisp_Symbol:
20543 /* A symbol: process the value of the symbol recursively
20544 as if it appeared here directly. Avoid error if symbol void.
20545 Special case: if value of symbol is a string, output the string
20546 literally. */
20547 {
20548 register Lisp_Object tem;
20549
20550 /* If the variable is not marked as risky to set
20551 then its contents are risky to use. */
20552 if (NILP (Fget (elt, Qrisky_local_variable)))
20553 risky = 1;
20554
20555 tem = Fboundp (elt);
20556 if (!NILP (tem))
20557 {
20558 tem = Fsymbol_value (elt);
20559 /* If value is a string, output that string literally:
20560 don't check for % within it. */
20561 if (STRINGP (tem))
20562 literal = 1;
20563
20564 if (!EQ (tem, elt))
20565 {
20566 /* Give up right away for nil or t. */
20567 elt = tem;
20568 goto tail_recurse;
20569 }
20570 }
20571 }
20572 break;
20573
20574 case Lisp_Cons:
20575 {
20576 register Lisp_Object car, tem;
20577
20578 /* A cons cell: five distinct cases.
20579 If first element is :eval or :propertize, do something special.
20580 If first element is a string or a cons, process all the elements
20581 and effectively concatenate them.
20582 If first element is a negative number, truncate displaying cdr to
20583 at most that many characters. If positive, pad (with spaces)
20584 to at least that many characters.
20585 If first element is a symbol, process the cadr or caddr recursively
20586 according to whether the symbol's value is non-nil or nil. */
20587 car = XCAR (elt);
20588 if (EQ (car, QCeval))
20589 {
20590 /* An element of the form (:eval FORM) means evaluate FORM
20591 and use the result as mode line elements. */
20592
20593 if (risky)
20594 break;
20595
20596 if (CONSP (XCDR (elt)))
20597 {
20598 Lisp_Object spec;
20599 spec = safe_eval (XCAR (XCDR (elt)));
20600 n += display_mode_element (it, depth, field_width - n,
20601 precision - n, spec, props,
20602 risky);
20603 }
20604 }
20605 else if (EQ (car, QCpropertize))
20606 {
20607 /* An element of the form (:propertize ELT PROPS...)
20608 means display ELT but applying properties PROPS. */
20609
20610 if (risky)
20611 break;
20612
20613 if (CONSP (XCDR (elt)))
20614 n += display_mode_element (it, depth, field_width - n,
20615 precision - n, XCAR (XCDR (elt)),
20616 XCDR (XCDR (elt)), risky);
20617 }
20618 else if (SYMBOLP (car))
20619 {
20620 tem = Fboundp (car);
20621 elt = XCDR (elt);
20622 if (!CONSP (elt))
20623 goto invalid;
20624 /* elt is now the cdr, and we know it is a cons cell.
20625 Use its car if CAR has a non-nil value. */
20626 if (!NILP (tem))
20627 {
20628 tem = Fsymbol_value (car);
20629 if (!NILP (tem))
20630 {
20631 elt = XCAR (elt);
20632 goto tail_recurse;
20633 }
20634 }
20635 /* Symbol's value is nil (or symbol is unbound)
20636 Get the cddr of the original list
20637 and if possible find the caddr and use that. */
20638 elt = XCDR (elt);
20639 if (NILP (elt))
20640 break;
20641 else if (!CONSP (elt))
20642 goto invalid;
20643 elt = XCAR (elt);
20644 goto tail_recurse;
20645 }
20646 else if (INTEGERP (car))
20647 {
20648 register int lim = XINT (car);
20649 elt = XCDR (elt);
20650 if (lim < 0)
20651 {
20652 /* Negative int means reduce maximum width. */
20653 if (precision <= 0)
20654 precision = -lim;
20655 else
20656 precision = min (precision, -lim);
20657 }
20658 else if (lim > 0)
20659 {
20660 /* Padding specified. Don't let it be more than
20661 current maximum. */
20662 if (precision > 0)
20663 lim = min (precision, lim);
20664
20665 /* If that's more padding than already wanted, queue it.
20666 But don't reduce padding already specified even if
20667 that is beyond the current truncation point. */
20668 field_width = max (lim, field_width);
20669 }
20670 goto tail_recurse;
20671 }
20672 else if (STRINGP (car) || CONSP (car))
20673 {
20674 Lisp_Object halftail = elt;
20675 int len = 0;
20676
20677 while (CONSP (elt)
20678 && (precision <= 0 || n < precision))
20679 {
20680 n += display_mode_element (it, depth,
20681 /* Do padding only after the last
20682 element in the list. */
20683 (! CONSP (XCDR (elt))
20684 ? field_width - n
20685 : 0),
20686 precision - n, XCAR (elt),
20687 props, risky);
20688 elt = XCDR (elt);
20689 len++;
20690 if ((len & 1) == 0)
20691 halftail = XCDR (halftail);
20692 /* Check for cycle. */
20693 if (EQ (halftail, elt))
20694 break;
20695 }
20696 }
20697 }
20698 break;
20699
20700 default:
20701 invalid:
20702 elt = build_string ("*invalid*");
20703 goto tail_recurse;
20704 }
20705
20706 /* Pad to FIELD_WIDTH. */
20707 if (field_width > 0 && n < field_width)
20708 {
20709 switch (mode_line_target)
20710 {
20711 case MODE_LINE_NOPROP:
20712 case MODE_LINE_TITLE:
20713 n += store_mode_line_noprop ("", field_width - n, 0);
20714 break;
20715 case MODE_LINE_STRING:
20716 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20717 break;
20718 case MODE_LINE_DISPLAY:
20719 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20720 0, 0, 0);
20721 break;
20722 }
20723 }
20724
20725 return n;
20726 }
20727
20728 /* Store a mode-line string element in mode_line_string_list.
20729
20730 If STRING is non-null, display that C string. Otherwise, the Lisp
20731 string LISP_STRING is displayed.
20732
20733 FIELD_WIDTH is the minimum number of output glyphs to produce.
20734 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20735 with spaces. FIELD_WIDTH <= 0 means don't pad.
20736
20737 PRECISION is the maximum number of characters to output from
20738 STRING. PRECISION <= 0 means don't truncate the string.
20739
20740 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20741 properties to the string.
20742
20743 PROPS are the properties to add to the string.
20744 The mode_line_string_face face property is always added to the string.
20745 */
20746
20747 static int
20748 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20749 int field_width, int precision, Lisp_Object props)
20750 {
20751 ptrdiff_t len;
20752 int n = 0;
20753
20754 if (string != NULL)
20755 {
20756 len = strlen (string);
20757 if (precision > 0 && len > precision)
20758 len = precision;
20759 lisp_string = make_string (string, len);
20760 if (NILP (props))
20761 props = mode_line_string_face_prop;
20762 else if (!NILP (mode_line_string_face))
20763 {
20764 Lisp_Object face = Fplist_get (props, Qface);
20765 props = Fcopy_sequence (props);
20766 if (NILP (face))
20767 face = mode_line_string_face;
20768 else
20769 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20770 props = Fplist_put (props, Qface, face);
20771 }
20772 Fadd_text_properties (make_number (0), make_number (len),
20773 props, lisp_string);
20774 }
20775 else
20776 {
20777 len = XFASTINT (Flength (lisp_string));
20778 if (precision > 0 && len > precision)
20779 {
20780 len = precision;
20781 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20782 precision = -1;
20783 }
20784 if (!NILP (mode_line_string_face))
20785 {
20786 Lisp_Object face;
20787 if (NILP (props))
20788 props = Ftext_properties_at (make_number (0), lisp_string);
20789 face = Fplist_get (props, Qface);
20790 if (NILP (face))
20791 face = mode_line_string_face;
20792 else
20793 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20794 props = Fcons (Qface, Fcons (face, Qnil));
20795 if (copy_string)
20796 lisp_string = Fcopy_sequence (lisp_string);
20797 }
20798 if (!NILP (props))
20799 Fadd_text_properties (make_number (0), make_number (len),
20800 props, lisp_string);
20801 }
20802
20803 if (len > 0)
20804 {
20805 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20806 n += len;
20807 }
20808
20809 if (field_width > len)
20810 {
20811 field_width -= len;
20812 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20813 if (!NILP (props))
20814 Fadd_text_properties (make_number (0), make_number (field_width),
20815 props, lisp_string);
20816 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20817 n += field_width;
20818 }
20819
20820 return n;
20821 }
20822
20823
20824 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20825 1, 4, 0,
20826 doc: /* Format a string out of a mode line format specification.
20827 First arg FORMAT specifies the mode line format (see `mode-line-format'
20828 for details) to use.
20829
20830 By default, the format is evaluated for the currently selected window.
20831
20832 Optional second arg FACE specifies the face property to put on all
20833 characters for which no face is specified. The value nil means the
20834 default face. The value t means whatever face the window's mode line
20835 currently uses (either `mode-line' or `mode-line-inactive',
20836 depending on whether the window is the selected window or not).
20837 An integer value means the value string has no text
20838 properties.
20839
20840 Optional third and fourth args WINDOW and BUFFER specify the window
20841 and buffer to use as the context for the formatting (defaults
20842 are the selected window and the WINDOW's buffer). */)
20843 (Lisp_Object format, Lisp_Object face,
20844 Lisp_Object window, Lisp_Object buffer)
20845 {
20846 struct it it;
20847 int len;
20848 struct window *w;
20849 struct buffer *old_buffer = NULL;
20850 int face_id;
20851 int no_props = INTEGERP (face);
20852 ptrdiff_t count = SPECPDL_INDEX ();
20853 Lisp_Object str;
20854 int string_start = 0;
20855
20856 if (NILP (window))
20857 window = selected_window;
20858 CHECK_WINDOW (window);
20859 w = XWINDOW (window);
20860
20861 if (NILP (buffer))
20862 buffer = w->buffer;
20863 CHECK_BUFFER (buffer);
20864
20865 /* Make formatting the modeline a non-op when noninteractive, otherwise
20866 there will be problems later caused by a partially initialized frame. */
20867 if (NILP (format) || noninteractive)
20868 return empty_unibyte_string;
20869
20870 if (no_props)
20871 face = Qnil;
20872
20873 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20874 : EQ (face, Qt) ? (EQ (window, selected_window)
20875 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20876 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20877 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20878 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20879 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20880 : DEFAULT_FACE_ID;
20881
20882 if (XBUFFER (buffer) != current_buffer)
20883 old_buffer = current_buffer;
20884
20885 /* Save things including mode_line_proptrans_alist,
20886 and set that to nil so that we don't alter the outer value. */
20887 record_unwind_protect (unwind_format_mode_line,
20888 format_mode_line_unwind_data
20889 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
20890 old_buffer, selected_window, 1));
20891 mode_line_proptrans_alist = Qnil;
20892
20893 Fselect_window (window, Qt);
20894 if (old_buffer)
20895 set_buffer_internal_1 (XBUFFER (buffer));
20896
20897 init_iterator (&it, w, -1, -1, NULL, face_id);
20898
20899 if (no_props)
20900 {
20901 mode_line_target = MODE_LINE_NOPROP;
20902 mode_line_string_face_prop = Qnil;
20903 mode_line_string_list = Qnil;
20904 string_start = MODE_LINE_NOPROP_LEN (0);
20905 }
20906 else
20907 {
20908 mode_line_target = MODE_LINE_STRING;
20909 mode_line_string_list = Qnil;
20910 mode_line_string_face = face;
20911 mode_line_string_face_prop
20912 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20913 }
20914
20915 push_kboard (FRAME_KBOARD (it.f));
20916 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20917 pop_kboard ();
20918
20919 if (no_props)
20920 {
20921 len = MODE_LINE_NOPROP_LEN (string_start);
20922 str = make_string (mode_line_noprop_buf + string_start, len);
20923 }
20924 else
20925 {
20926 mode_line_string_list = Fnreverse (mode_line_string_list);
20927 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20928 empty_unibyte_string);
20929 }
20930
20931 unbind_to (count, Qnil);
20932 return str;
20933 }
20934
20935 /* Write a null-terminated, right justified decimal representation of
20936 the positive integer D to BUF using a minimal field width WIDTH. */
20937
20938 static void
20939 pint2str (register char *buf, register int width, register ptrdiff_t d)
20940 {
20941 register char *p = buf;
20942
20943 if (d <= 0)
20944 *p++ = '0';
20945 else
20946 {
20947 while (d > 0)
20948 {
20949 *p++ = d % 10 + '0';
20950 d /= 10;
20951 }
20952 }
20953
20954 for (width -= (int) (p - buf); width > 0; --width)
20955 *p++ = ' ';
20956 *p-- = '\0';
20957 while (p > buf)
20958 {
20959 d = *buf;
20960 *buf++ = *p;
20961 *p-- = d;
20962 }
20963 }
20964
20965 /* Write a null-terminated, right justified decimal and "human
20966 readable" representation of the nonnegative integer D to BUF using
20967 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20968
20969 static const char power_letter[] =
20970 {
20971 0, /* no letter */
20972 'k', /* kilo */
20973 'M', /* mega */
20974 'G', /* giga */
20975 'T', /* tera */
20976 'P', /* peta */
20977 'E', /* exa */
20978 'Z', /* zetta */
20979 'Y' /* yotta */
20980 };
20981
20982 static void
20983 pint2hrstr (char *buf, int width, ptrdiff_t d)
20984 {
20985 /* We aim to represent the nonnegative integer D as
20986 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20987 ptrdiff_t quotient = d;
20988 int remainder = 0;
20989 /* -1 means: do not use TENTHS. */
20990 int tenths = -1;
20991 int exponent = 0;
20992
20993 /* Length of QUOTIENT.TENTHS as a string. */
20994 int length;
20995
20996 char * psuffix;
20997 char * p;
20998
20999 if (1000 <= quotient)
21000 {
21001 /* Scale to the appropriate EXPONENT. */
21002 do
21003 {
21004 remainder = quotient % 1000;
21005 quotient /= 1000;
21006 exponent++;
21007 }
21008 while (1000 <= quotient);
21009
21010 /* Round to nearest and decide whether to use TENTHS or not. */
21011 if (quotient <= 9)
21012 {
21013 tenths = remainder / 100;
21014 if (50 <= remainder % 100)
21015 {
21016 if (tenths < 9)
21017 tenths++;
21018 else
21019 {
21020 quotient++;
21021 if (quotient == 10)
21022 tenths = -1;
21023 else
21024 tenths = 0;
21025 }
21026 }
21027 }
21028 else
21029 if (500 <= remainder)
21030 {
21031 if (quotient < 999)
21032 quotient++;
21033 else
21034 {
21035 quotient = 1;
21036 exponent++;
21037 tenths = 0;
21038 }
21039 }
21040 }
21041
21042 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21043 if (tenths == -1 && quotient <= 99)
21044 if (quotient <= 9)
21045 length = 1;
21046 else
21047 length = 2;
21048 else
21049 length = 3;
21050 p = psuffix = buf + max (width, length);
21051
21052 /* Print EXPONENT. */
21053 *psuffix++ = power_letter[exponent];
21054 *psuffix = '\0';
21055
21056 /* Print TENTHS. */
21057 if (tenths >= 0)
21058 {
21059 *--p = '0' + tenths;
21060 *--p = '.';
21061 }
21062
21063 /* Print QUOTIENT. */
21064 do
21065 {
21066 int digit = quotient % 10;
21067 *--p = '0' + digit;
21068 }
21069 while ((quotient /= 10) != 0);
21070
21071 /* Print leading spaces. */
21072 while (buf < p)
21073 *--p = ' ';
21074 }
21075
21076 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21077 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21078 type of CODING_SYSTEM. Return updated pointer into BUF. */
21079
21080 static unsigned char invalid_eol_type[] = "(*invalid*)";
21081
21082 static char *
21083 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21084 {
21085 Lisp_Object val;
21086 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21087 const unsigned char *eol_str;
21088 int eol_str_len;
21089 /* The EOL conversion we are using. */
21090 Lisp_Object eoltype;
21091
21092 val = CODING_SYSTEM_SPEC (coding_system);
21093 eoltype = Qnil;
21094
21095 if (!VECTORP (val)) /* Not yet decided. */
21096 {
21097 *buf++ = multibyte ? '-' : ' ';
21098 if (eol_flag)
21099 eoltype = eol_mnemonic_undecided;
21100 /* Don't mention EOL conversion if it isn't decided. */
21101 }
21102 else
21103 {
21104 Lisp_Object attrs;
21105 Lisp_Object eolvalue;
21106
21107 attrs = AREF (val, 0);
21108 eolvalue = AREF (val, 2);
21109
21110 *buf++ = multibyte
21111 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21112 : ' ';
21113
21114 if (eol_flag)
21115 {
21116 /* The EOL conversion that is normal on this system. */
21117
21118 if (NILP (eolvalue)) /* Not yet decided. */
21119 eoltype = eol_mnemonic_undecided;
21120 else if (VECTORP (eolvalue)) /* Not yet decided. */
21121 eoltype = eol_mnemonic_undecided;
21122 else /* eolvalue is Qunix, Qdos, or Qmac. */
21123 eoltype = (EQ (eolvalue, Qunix)
21124 ? eol_mnemonic_unix
21125 : (EQ (eolvalue, Qdos) == 1
21126 ? eol_mnemonic_dos : eol_mnemonic_mac));
21127 }
21128 }
21129
21130 if (eol_flag)
21131 {
21132 /* Mention the EOL conversion if it is not the usual one. */
21133 if (STRINGP (eoltype))
21134 {
21135 eol_str = SDATA (eoltype);
21136 eol_str_len = SBYTES (eoltype);
21137 }
21138 else if (CHARACTERP (eoltype))
21139 {
21140 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
21141 int c = XFASTINT (eoltype);
21142 eol_str_len = CHAR_STRING (c, tmp);
21143 eol_str = tmp;
21144 }
21145 else
21146 {
21147 eol_str = invalid_eol_type;
21148 eol_str_len = sizeof (invalid_eol_type) - 1;
21149 }
21150 memcpy (buf, eol_str, eol_str_len);
21151 buf += eol_str_len;
21152 }
21153
21154 return buf;
21155 }
21156
21157 /* Return a string for the output of a mode line %-spec for window W,
21158 generated by character C. FIELD_WIDTH > 0 means pad the string
21159 returned with spaces to that value. Return a Lisp string in
21160 *STRING if the resulting string is taken from that Lisp string.
21161
21162 Note we operate on the current buffer for most purposes,
21163 the exception being w->base_line_pos. */
21164
21165 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21166
21167 static const char *
21168 decode_mode_spec (struct window *w, register int c, int field_width,
21169 Lisp_Object *string)
21170 {
21171 Lisp_Object obj;
21172 struct frame *f = XFRAME (WINDOW_FRAME (w));
21173 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21174 struct buffer *b = current_buffer;
21175
21176 obj = Qnil;
21177 *string = Qnil;
21178
21179 switch (c)
21180 {
21181 case '*':
21182 if (!NILP (BVAR (b, read_only)))
21183 return "%";
21184 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21185 return "*";
21186 return "-";
21187
21188 case '+':
21189 /* This differs from %* only for a modified read-only buffer. */
21190 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21191 return "*";
21192 if (!NILP (BVAR (b, read_only)))
21193 return "%";
21194 return "-";
21195
21196 case '&':
21197 /* This differs from %* in ignoring read-only-ness. */
21198 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21199 return "*";
21200 return "-";
21201
21202 case '%':
21203 return "%";
21204
21205 case '[':
21206 {
21207 int i;
21208 char *p;
21209
21210 if (command_loop_level > 5)
21211 return "[[[... ";
21212 p = decode_mode_spec_buf;
21213 for (i = 0; i < command_loop_level; i++)
21214 *p++ = '[';
21215 *p = 0;
21216 return decode_mode_spec_buf;
21217 }
21218
21219 case ']':
21220 {
21221 int i;
21222 char *p;
21223
21224 if (command_loop_level > 5)
21225 return " ...]]]";
21226 p = decode_mode_spec_buf;
21227 for (i = 0; i < command_loop_level; i++)
21228 *p++ = ']';
21229 *p = 0;
21230 return decode_mode_spec_buf;
21231 }
21232
21233 case '-':
21234 {
21235 register int i;
21236
21237 /* Let lots_of_dashes be a string of infinite length. */
21238 if (mode_line_target == MODE_LINE_NOPROP ||
21239 mode_line_target == MODE_LINE_STRING)
21240 return "--";
21241 if (field_width <= 0
21242 || field_width > sizeof (lots_of_dashes))
21243 {
21244 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21245 decode_mode_spec_buf[i] = '-';
21246 decode_mode_spec_buf[i] = '\0';
21247 return decode_mode_spec_buf;
21248 }
21249 else
21250 return lots_of_dashes;
21251 }
21252
21253 case 'b':
21254 obj = BVAR (b, name);
21255 break;
21256
21257 case 'c':
21258 /* %c and %l are ignored in `frame-title-format'.
21259 (In redisplay_internal, the frame title is drawn _before_ the
21260 windows are updated, so the stuff which depends on actual
21261 window contents (such as %l) may fail to render properly, or
21262 even crash emacs.) */
21263 if (mode_line_target == MODE_LINE_TITLE)
21264 return "";
21265 else
21266 {
21267 ptrdiff_t col = current_column ();
21268 w->column_number_displayed = make_number (col);
21269 pint2str (decode_mode_spec_buf, field_width, col);
21270 return decode_mode_spec_buf;
21271 }
21272
21273 case 'e':
21274 #ifndef SYSTEM_MALLOC
21275 {
21276 if (NILP (Vmemory_full))
21277 return "";
21278 else
21279 return "!MEM FULL! ";
21280 }
21281 #else
21282 return "";
21283 #endif
21284
21285 case 'F':
21286 /* %F displays the frame name. */
21287 if (!NILP (f->title))
21288 return SSDATA (f->title);
21289 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21290 return SSDATA (f->name);
21291 return "Emacs";
21292
21293 case 'f':
21294 obj = BVAR (b, filename);
21295 break;
21296
21297 case 'i':
21298 {
21299 ptrdiff_t size = ZV - BEGV;
21300 pint2str (decode_mode_spec_buf, field_width, size);
21301 return decode_mode_spec_buf;
21302 }
21303
21304 case 'I':
21305 {
21306 ptrdiff_t size = ZV - BEGV;
21307 pint2hrstr (decode_mode_spec_buf, field_width, size);
21308 return decode_mode_spec_buf;
21309 }
21310
21311 case 'l':
21312 {
21313 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21314 ptrdiff_t topline, nlines, height;
21315 ptrdiff_t junk;
21316
21317 /* %c and %l are ignored in `frame-title-format'. */
21318 if (mode_line_target == MODE_LINE_TITLE)
21319 return "";
21320
21321 startpos = XMARKER (w->start)->charpos;
21322 startpos_byte = marker_byte_position (w->start);
21323 height = WINDOW_TOTAL_LINES (w);
21324
21325 /* If we decided that this buffer isn't suitable for line numbers,
21326 don't forget that too fast. */
21327 if (EQ (w->base_line_pos, w->buffer))
21328 goto no_value;
21329 /* But do forget it, if the window shows a different buffer now. */
21330 else if (BUFFERP (w->base_line_pos))
21331 w->base_line_pos = Qnil;
21332
21333 /* If the buffer is very big, don't waste time. */
21334 if (INTEGERP (Vline_number_display_limit)
21335 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21336 {
21337 w->base_line_pos = Qnil;
21338 w->base_line_number = Qnil;
21339 goto no_value;
21340 }
21341
21342 if (INTEGERP (w->base_line_number)
21343 && INTEGERP (w->base_line_pos)
21344 && XFASTINT (w->base_line_pos) <= startpos)
21345 {
21346 line = XFASTINT (w->base_line_number);
21347 linepos = XFASTINT (w->base_line_pos);
21348 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21349 }
21350 else
21351 {
21352 line = 1;
21353 linepos = BUF_BEGV (b);
21354 linepos_byte = BUF_BEGV_BYTE (b);
21355 }
21356
21357 /* Count lines from base line to window start position. */
21358 nlines = display_count_lines (linepos_byte,
21359 startpos_byte,
21360 startpos, &junk);
21361
21362 topline = nlines + line;
21363
21364 /* Determine a new base line, if the old one is too close
21365 or too far away, or if we did not have one.
21366 "Too close" means it's plausible a scroll-down would
21367 go back past it. */
21368 if (startpos == BUF_BEGV (b))
21369 {
21370 w->base_line_number = make_number (topline);
21371 w->base_line_pos = make_number (BUF_BEGV (b));
21372 }
21373 else if (nlines < height + 25 || nlines > height * 3 + 50
21374 || linepos == BUF_BEGV (b))
21375 {
21376 ptrdiff_t limit = BUF_BEGV (b);
21377 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21378 ptrdiff_t position;
21379 ptrdiff_t distance =
21380 (height * 2 + 30) * line_number_display_limit_width;
21381
21382 if (startpos - distance > limit)
21383 {
21384 limit = startpos - distance;
21385 limit_byte = CHAR_TO_BYTE (limit);
21386 }
21387
21388 nlines = display_count_lines (startpos_byte,
21389 limit_byte,
21390 - (height * 2 + 30),
21391 &position);
21392 /* If we couldn't find the lines we wanted within
21393 line_number_display_limit_width chars per line,
21394 give up on line numbers for this window. */
21395 if (position == limit_byte && limit == startpos - distance)
21396 {
21397 w->base_line_pos = w->buffer;
21398 w->base_line_number = Qnil;
21399 goto no_value;
21400 }
21401
21402 w->base_line_number = make_number (topline - nlines);
21403 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21404 }
21405
21406 /* Now count lines from the start pos to point. */
21407 nlines = display_count_lines (startpos_byte,
21408 PT_BYTE, PT, &junk);
21409
21410 /* Record that we did display the line number. */
21411 line_number_displayed = 1;
21412
21413 /* Make the string to show. */
21414 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21415 return decode_mode_spec_buf;
21416 no_value:
21417 {
21418 char* p = decode_mode_spec_buf;
21419 int pad = field_width - 2;
21420 while (pad-- > 0)
21421 *p++ = ' ';
21422 *p++ = '?';
21423 *p++ = '?';
21424 *p = '\0';
21425 return decode_mode_spec_buf;
21426 }
21427 }
21428 break;
21429
21430 case 'm':
21431 obj = BVAR (b, mode_name);
21432 break;
21433
21434 case 'n':
21435 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21436 return " Narrow";
21437 break;
21438
21439 case 'p':
21440 {
21441 ptrdiff_t pos = marker_position (w->start);
21442 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21443
21444 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21445 {
21446 if (pos <= BUF_BEGV (b))
21447 return "All";
21448 else
21449 return "Bottom";
21450 }
21451 else if (pos <= BUF_BEGV (b))
21452 return "Top";
21453 else
21454 {
21455 if (total > 1000000)
21456 /* Do it differently for a large value, to avoid overflow. */
21457 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21458 else
21459 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21460 /* We can't normally display a 3-digit number,
21461 so get us a 2-digit number that is close. */
21462 if (total == 100)
21463 total = 99;
21464 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21465 return decode_mode_spec_buf;
21466 }
21467 }
21468
21469 /* Display percentage of size above the bottom of the screen. */
21470 case 'P':
21471 {
21472 ptrdiff_t toppos = marker_position (w->start);
21473 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21474 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21475
21476 if (botpos >= BUF_ZV (b))
21477 {
21478 if (toppos <= BUF_BEGV (b))
21479 return "All";
21480 else
21481 return "Bottom";
21482 }
21483 else
21484 {
21485 if (total > 1000000)
21486 /* Do it differently for a large value, to avoid overflow. */
21487 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21488 else
21489 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21490 /* We can't normally display a 3-digit number,
21491 so get us a 2-digit number that is close. */
21492 if (total == 100)
21493 total = 99;
21494 if (toppos <= BUF_BEGV (b))
21495 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21496 else
21497 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21498 return decode_mode_spec_buf;
21499 }
21500 }
21501
21502 case 's':
21503 /* status of process */
21504 obj = Fget_buffer_process (Fcurrent_buffer ());
21505 if (NILP (obj))
21506 return "no process";
21507 #ifndef MSDOS
21508 obj = Fsymbol_name (Fprocess_status (obj));
21509 #endif
21510 break;
21511
21512 case '@':
21513 {
21514 ptrdiff_t count = inhibit_garbage_collection ();
21515 Lisp_Object val = call1 (intern ("file-remote-p"),
21516 BVAR (current_buffer, directory));
21517 unbind_to (count, Qnil);
21518
21519 if (NILP (val))
21520 return "-";
21521 else
21522 return "@";
21523 }
21524
21525 case 't': /* indicate TEXT or BINARY */
21526 return "T";
21527
21528 case 'z':
21529 /* coding-system (not including end-of-line format) */
21530 case 'Z':
21531 /* coding-system (including end-of-line type) */
21532 {
21533 int eol_flag = (c == 'Z');
21534 char *p = decode_mode_spec_buf;
21535
21536 if (! FRAME_WINDOW_P (f))
21537 {
21538 /* No need to mention EOL here--the terminal never needs
21539 to do EOL conversion. */
21540 p = decode_mode_spec_coding (CODING_ID_NAME
21541 (FRAME_KEYBOARD_CODING (f)->id),
21542 p, 0);
21543 p = decode_mode_spec_coding (CODING_ID_NAME
21544 (FRAME_TERMINAL_CODING (f)->id),
21545 p, 0);
21546 }
21547 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21548 p, eol_flag);
21549
21550 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21551 #ifdef subprocesses
21552 obj = Fget_buffer_process (Fcurrent_buffer ());
21553 if (PROCESSP (obj))
21554 {
21555 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21556 p, eol_flag);
21557 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21558 p, eol_flag);
21559 }
21560 #endif /* subprocesses */
21561 #endif /* 0 */
21562 *p = 0;
21563 return decode_mode_spec_buf;
21564 }
21565 }
21566
21567 if (STRINGP (obj))
21568 {
21569 *string = obj;
21570 return SSDATA (obj);
21571 }
21572 else
21573 return "";
21574 }
21575
21576
21577 /* Count up to COUNT lines starting from START_BYTE.
21578 But don't go beyond LIMIT_BYTE.
21579 Return the number of lines thus found (always nonnegative).
21580
21581 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21582
21583 static ptrdiff_t
21584 display_count_lines (ptrdiff_t start_byte,
21585 ptrdiff_t limit_byte, ptrdiff_t count,
21586 ptrdiff_t *byte_pos_ptr)
21587 {
21588 register unsigned char *cursor;
21589 unsigned char *base;
21590
21591 register ptrdiff_t ceiling;
21592 register unsigned char *ceiling_addr;
21593 ptrdiff_t orig_count = count;
21594
21595 /* If we are not in selective display mode,
21596 check only for newlines. */
21597 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21598 && !INTEGERP (BVAR (current_buffer, selective_display)));
21599
21600 if (count > 0)
21601 {
21602 while (start_byte < limit_byte)
21603 {
21604 ceiling = BUFFER_CEILING_OF (start_byte);
21605 ceiling = min (limit_byte - 1, ceiling);
21606 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21607 base = (cursor = BYTE_POS_ADDR (start_byte));
21608 while (1)
21609 {
21610 if (selective_display)
21611 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21612 ;
21613 else
21614 while (*cursor != '\n' && ++cursor != ceiling_addr)
21615 ;
21616
21617 if (cursor != ceiling_addr)
21618 {
21619 if (--count == 0)
21620 {
21621 start_byte += cursor - base + 1;
21622 *byte_pos_ptr = start_byte;
21623 return orig_count;
21624 }
21625 else
21626 if (++cursor == ceiling_addr)
21627 break;
21628 }
21629 else
21630 break;
21631 }
21632 start_byte += cursor - base;
21633 }
21634 }
21635 else
21636 {
21637 while (start_byte > limit_byte)
21638 {
21639 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21640 ceiling = max (limit_byte, ceiling);
21641 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21642 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21643 while (1)
21644 {
21645 if (selective_display)
21646 while (--cursor != ceiling_addr
21647 && *cursor != '\n' && *cursor != 015)
21648 ;
21649 else
21650 while (--cursor != ceiling_addr && *cursor != '\n')
21651 ;
21652
21653 if (cursor != ceiling_addr)
21654 {
21655 if (++count == 0)
21656 {
21657 start_byte += cursor - base + 1;
21658 *byte_pos_ptr = start_byte;
21659 /* When scanning backwards, we should
21660 not count the newline posterior to which we stop. */
21661 return - orig_count - 1;
21662 }
21663 }
21664 else
21665 break;
21666 }
21667 /* Here we add 1 to compensate for the last decrement
21668 of CURSOR, which took it past the valid range. */
21669 start_byte += cursor - base + 1;
21670 }
21671 }
21672
21673 *byte_pos_ptr = limit_byte;
21674
21675 if (count < 0)
21676 return - orig_count + count;
21677 return orig_count - count;
21678
21679 }
21680
21681
21682 \f
21683 /***********************************************************************
21684 Displaying strings
21685 ***********************************************************************/
21686
21687 /* Display a NUL-terminated string, starting with index START.
21688
21689 If STRING is non-null, display that C string. Otherwise, the Lisp
21690 string LISP_STRING is displayed. There's a case that STRING is
21691 non-null and LISP_STRING is not nil. It means STRING is a string
21692 data of LISP_STRING. In that case, we display LISP_STRING while
21693 ignoring its text properties.
21694
21695 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21696 FACE_STRING. Display STRING or LISP_STRING with the face at
21697 FACE_STRING_POS in FACE_STRING:
21698
21699 Display the string in the environment given by IT, but use the
21700 standard display table, temporarily.
21701
21702 FIELD_WIDTH is the minimum number of output glyphs to produce.
21703 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21704 with spaces. If STRING has more characters, more than FIELD_WIDTH
21705 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21706
21707 PRECISION is the maximum number of characters to output from
21708 STRING. PRECISION < 0 means don't truncate the string.
21709
21710 This is roughly equivalent to printf format specifiers:
21711
21712 FIELD_WIDTH PRECISION PRINTF
21713 ----------------------------------------
21714 -1 -1 %s
21715 -1 10 %.10s
21716 10 -1 %10s
21717 20 10 %20.10s
21718
21719 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21720 display them, and < 0 means obey the current buffer's value of
21721 enable_multibyte_characters.
21722
21723 Value is the number of columns displayed. */
21724
21725 static int
21726 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21727 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21728 int field_width, int precision, int max_x, int multibyte)
21729 {
21730 int hpos_at_start = it->hpos;
21731 int saved_face_id = it->face_id;
21732 struct glyph_row *row = it->glyph_row;
21733 ptrdiff_t it_charpos;
21734
21735 /* Initialize the iterator IT for iteration over STRING beginning
21736 with index START. */
21737 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21738 precision, field_width, multibyte);
21739 if (string && STRINGP (lisp_string))
21740 /* LISP_STRING is the one returned by decode_mode_spec. We should
21741 ignore its text properties. */
21742 it->stop_charpos = it->end_charpos;
21743
21744 /* If displaying STRING, set up the face of the iterator from
21745 FACE_STRING, if that's given. */
21746 if (STRINGP (face_string))
21747 {
21748 ptrdiff_t endptr;
21749 struct face *face;
21750
21751 it->face_id
21752 = face_at_string_position (it->w, face_string, face_string_pos,
21753 0, it->region_beg_charpos,
21754 it->region_end_charpos,
21755 &endptr, it->base_face_id, 0);
21756 face = FACE_FROM_ID (it->f, it->face_id);
21757 it->face_box_p = face->box != FACE_NO_BOX;
21758 }
21759
21760 /* Set max_x to the maximum allowed X position. Don't let it go
21761 beyond the right edge of the window. */
21762 if (max_x <= 0)
21763 max_x = it->last_visible_x;
21764 else
21765 max_x = min (max_x, it->last_visible_x);
21766
21767 /* Skip over display elements that are not visible. because IT->w is
21768 hscrolled. */
21769 if (it->current_x < it->first_visible_x)
21770 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21771 MOVE_TO_POS | MOVE_TO_X);
21772
21773 row->ascent = it->max_ascent;
21774 row->height = it->max_ascent + it->max_descent;
21775 row->phys_ascent = it->max_phys_ascent;
21776 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21777 row->extra_line_spacing = it->max_extra_line_spacing;
21778
21779 if (STRINGP (it->string))
21780 it_charpos = IT_STRING_CHARPOS (*it);
21781 else
21782 it_charpos = IT_CHARPOS (*it);
21783
21784 /* This condition is for the case that we are called with current_x
21785 past last_visible_x. */
21786 while (it->current_x < max_x)
21787 {
21788 int x_before, x, n_glyphs_before, i, nglyphs;
21789
21790 /* Get the next display element. */
21791 if (!get_next_display_element (it))
21792 break;
21793
21794 /* Produce glyphs. */
21795 x_before = it->current_x;
21796 n_glyphs_before = row->used[TEXT_AREA];
21797 PRODUCE_GLYPHS (it);
21798
21799 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21800 i = 0;
21801 x = x_before;
21802 while (i < nglyphs)
21803 {
21804 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21805
21806 if (it->line_wrap != TRUNCATE
21807 && x + glyph->pixel_width > max_x)
21808 {
21809 /* End of continued line or max_x reached. */
21810 if (CHAR_GLYPH_PADDING_P (*glyph))
21811 {
21812 /* A wide character is unbreakable. */
21813 if (row->reversed_p)
21814 unproduce_glyphs (it, row->used[TEXT_AREA]
21815 - n_glyphs_before);
21816 row->used[TEXT_AREA] = n_glyphs_before;
21817 it->current_x = x_before;
21818 }
21819 else
21820 {
21821 if (row->reversed_p)
21822 unproduce_glyphs (it, row->used[TEXT_AREA]
21823 - (n_glyphs_before + i));
21824 row->used[TEXT_AREA] = n_glyphs_before + i;
21825 it->current_x = x;
21826 }
21827 break;
21828 }
21829 else if (x + glyph->pixel_width >= it->first_visible_x)
21830 {
21831 /* Glyph is at least partially visible. */
21832 ++it->hpos;
21833 if (x < it->first_visible_x)
21834 row->x = x - it->first_visible_x;
21835 }
21836 else
21837 {
21838 /* Glyph is off the left margin of the display area.
21839 Should not happen. */
21840 abort ();
21841 }
21842
21843 row->ascent = max (row->ascent, it->max_ascent);
21844 row->height = max (row->height, it->max_ascent + it->max_descent);
21845 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21846 row->phys_height = max (row->phys_height,
21847 it->max_phys_ascent + it->max_phys_descent);
21848 row->extra_line_spacing = max (row->extra_line_spacing,
21849 it->max_extra_line_spacing);
21850 x += glyph->pixel_width;
21851 ++i;
21852 }
21853
21854 /* Stop if max_x reached. */
21855 if (i < nglyphs)
21856 break;
21857
21858 /* Stop at line ends. */
21859 if (ITERATOR_AT_END_OF_LINE_P (it))
21860 {
21861 it->continuation_lines_width = 0;
21862 break;
21863 }
21864
21865 set_iterator_to_next (it, 1);
21866 if (STRINGP (it->string))
21867 it_charpos = IT_STRING_CHARPOS (*it);
21868 else
21869 it_charpos = IT_CHARPOS (*it);
21870
21871 /* Stop if truncating at the right edge. */
21872 if (it->line_wrap == TRUNCATE
21873 && it->current_x >= it->last_visible_x)
21874 {
21875 /* Add truncation mark, but don't do it if the line is
21876 truncated at a padding space. */
21877 if (it_charpos < it->string_nchars)
21878 {
21879 if (!FRAME_WINDOW_P (it->f))
21880 {
21881 int ii, n;
21882
21883 if (it->current_x > it->last_visible_x)
21884 {
21885 if (!row->reversed_p)
21886 {
21887 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21888 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21889 break;
21890 }
21891 else
21892 {
21893 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21894 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21895 break;
21896 unproduce_glyphs (it, ii + 1);
21897 ii = row->used[TEXT_AREA] - (ii + 1);
21898 }
21899 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21900 {
21901 row->used[TEXT_AREA] = ii;
21902 produce_special_glyphs (it, IT_TRUNCATION);
21903 }
21904 }
21905 produce_special_glyphs (it, IT_TRUNCATION);
21906 }
21907 row->truncated_on_right_p = 1;
21908 }
21909 break;
21910 }
21911 }
21912
21913 /* Maybe insert a truncation at the left. */
21914 if (it->first_visible_x
21915 && it_charpos > 0)
21916 {
21917 if (!FRAME_WINDOW_P (it->f))
21918 insert_left_trunc_glyphs (it);
21919 row->truncated_on_left_p = 1;
21920 }
21921
21922 it->face_id = saved_face_id;
21923
21924 /* Value is number of columns displayed. */
21925 return it->hpos - hpos_at_start;
21926 }
21927
21928
21929 \f
21930 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21931 appears as an element of LIST or as the car of an element of LIST.
21932 If PROPVAL is a list, compare each element against LIST in that
21933 way, and return 1/2 if any element of PROPVAL is found in LIST.
21934 Otherwise return 0. This function cannot quit.
21935 The return value is 2 if the text is invisible but with an ellipsis
21936 and 1 if it's invisible and without an ellipsis. */
21937
21938 int
21939 invisible_p (register Lisp_Object propval, Lisp_Object list)
21940 {
21941 register Lisp_Object tail, proptail;
21942
21943 for (tail = list; CONSP (tail); tail = XCDR (tail))
21944 {
21945 register Lisp_Object tem;
21946 tem = XCAR (tail);
21947 if (EQ (propval, tem))
21948 return 1;
21949 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21950 return NILP (XCDR (tem)) ? 1 : 2;
21951 }
21952
21953 if (CONSP (propval))
21954 {
21955 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21956 {
21957 Lisp_Object propelt;
21958 propelt = XCAR (proptail);
21959 for (tail = list; CONSP (tail); tail = XCDR (tail))
21960 {
21961 register Lisp_Object tem;
21962 tem = XCAR (tail);
21963 if (EQ (propelt, tem))
21964 return 1;
21965 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21966 return NILP (XCDR (tem)) ? 1 : 2;
21967 }
21968 }
21969 }
21970
21971 return 0;
21972 }
21973
21974 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21975 doc: /* Non-nil if the property makes the text invisible.
21976 POS-OR-PROP can be a marker or number, in which case it is taken to be
21977 a position in the current buffer and the value of the `invisible' property
21978 is checked; or it can be some other value, which is then presumed to be the
21979 value of the `invisible' property of the text of interest.
21980 The non-nil value returned can be t for truly invisible text or something
21981 else if the text is replaced by an ellipsis. */)
21982 (Lisp_Object pos_or_prop)
21983 {
21984 Lisp_Object prop
21985 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21986 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21987 : pos_or_prop);
21988 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21989 return (invis == 0 ? Qnil
21990 : invis == 1 ? Qt
21991 : make_number (invis));
21992 }
21993
21994 /* Calculate a width or height in pixels from a specification using
21995 the following elements:
21996
21997 SPEC ::=
21998 NUM - a (fractional) multiple of the default font width/height
21999 (NUM) - specifies exactly NUM pixels
22000 UNIT - a fixed number of pixels, see below.
22001 ELEMENT - size of a display element in pixels, see below.
22002 (NUM . SPEC) - equals NUM * SPEC
22003 (+ SPEC SPEC ...) - add pixel values
22004 (- SPEC SPEC ...) - subtract pixel values
22005 (- SPEC) - negate pixel value
22006
22007 NUM ::=
22008 INT or FLOAT - a number constant
22009 SYMBOL - use symbol's (buffer local) variable binding.
22010
22011 UNIT ::=
22012 in - pixels per inch *)
22013 mm - pixels per 1/1000 meter *)
22014 cm - pixels per 1/100 meter *)
22015 width - width of current font in pixels.
22016 height - height of current font in pixels.
22017
22018 *) using the ratio(s) defined in display-pixels-per-inch.
22019
22020 ELEMENT ::=
22021
22022 left-fringe - left fringe width in pixels
22023 right-fringe - right fringe width in pixels
22024
22025 left-margin - left margin width in pixels
22026 right-margin - right margin width in pixels
22027
22028 scroll-bar - scroll-bar area width in pixels
22029
22030 Examples:
22031
22032 Pixels corresponding to 5 inches:
22033 (5 . in)
22034
22035 Total width of non-text areas on left side of window (if scroll-bar is on left):
22036 '(space :width (+ left-fringe left-margin scroll-bar))
22037
22038 Align to first text column (in header line):
22039 '(space :align-to 0)
22040
22041 Align to middle of text area minus half the width of variable `my-image'
22042 containing a loaded image:
22043 '(space :align-to (0.5 . (- text my-image)))
22044
22045 Width of left margin minus width of 1 character in the default font:
22046 '(space :width (- left-margin 1))
22047
22048 Width of left margin minus width of 2 characters in the current font:
22049 '(space :width (- left-margin (2 . width)))
22050
22051 Center 1 character over left-margin (in header line):
22052 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22053
22054 Different ways to express width of left fringe plus left margin minus one pixel:
22055 '(space :width (- (+ left-fringe left-margin) (1)))
22056 '(space :width (+ left-fringe left-margin (- (1))))
22057 '(space :width (+ left-fringe left-margin (-1)))
22058
22059 */
22060
22061 #define NUMVAL(X) \
22062 ((INTEGERP (X) || FLOATP (X)) \
22063 ? XFLOATINT (X) \
22064 : - 1)
22065
22066 static int
22067 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22068 struct font *font, int width_p, int *align_to)
22069 {
22070 double pixels;
22071
22072 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22073 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22074
22075 if (NILP (prop))
22076 return OK_PIXELS (0);
22077
22078 eassert (FRAME_LIVE_P (it->f));
22079
22080 if (SYMBOLP (prop))
22081 {
22082 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22083 {
22084 char *unit = SSDATA (SYMBOL_NAME (prop));
22085
22086 if (unit[0] == 'i' && unit[1] == 'n')
22087 pixels = 1.0;
22088 else if (unit[0] == 'm' && unit[1] == 'm')
22089 pixels = 25.4;
22090 else if (unit[0] == 'c' && unit[1] == 'm')
22091 pixels = 2.54;
22092 else
22093 pixels = 0;
22094 if (pixels > 0)
22095 {
22096 double ppi;
22097 #ifdef HAVE_WINDOW_SYSTEM
22098 if (FRAME_WINDOW_P (it->f)
22099 && (ppi = (width_p
22100 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22101 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22102 ppi > 0))
22103 return OK_PIXELS (ppi / pixels);
22104 #endif
22105
22106 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22107 || (CONSP (Vdisplay_pixels_per_inch)
22108 && (ppi = (width_p
22109 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22110 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22111 ppi > 0)))
22112 return OK_PIXELS (ppi / pixels);
22113
22114 return 0;
22115 }
22116 }
22117
22118 #ifdef HAVE_WINDOW_SYSTEM
22119 if (EQ (prop, Qheight))
22120 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22121 if (EQ (prop, Qwidth))
22122 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22123 #else
22124 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22125 return OK_PIXELS (1);
22126 #endif
22127
22128 if (EQ (prop, Qtext))
22129 return OK_PIXELS (width_p
22130 ? window_box_width (it->w, TEXT_AREA)
22131 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22132
22133 if (align_to && *align_to < 0)
22134 {
22135 *res = 0;
22136 if (EQ (prop, Qleft))
22137 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22138 if (EQ (prop, Qright))
22139 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22140 if (EQ (prop, Qcenter))
22141 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22142 + window_box_width (it->w, TEXT_AREA) / 2);
22143 if (EQ (prop, Qleft_fringe))
22144 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22145 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22146 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22147 if (EQ (prop, Qright_fringe))
22148 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22149 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22150 : window_box_right_offset (it->w, TEXT_AREA));
22151 if (EQ (prop, Qleft_margin))
22152 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22153 if (EQ (prop, Qright_margin))
22154 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22155 if (EQ (prop, Qscroll_bar))
22156 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22157 ? 0
22158 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22159 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22160 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22161 : 0)));
22162 }
22163 else
22164 {
22165 if (EQ (prop, Qleft_fringe))
22166 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22167 if (EQ (prop, Qright_fringe))
22168 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22169 if (EQ (prop, Qleft_margin))
22170 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22171 if (EQ (prop, Qright_margin))
22172 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22173 if (EQ (prop, Qscroll_bar))
22174 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22175 }
22176
22177 prop = buffer_local_value_1 (prop, it->w->buffer);
22178 if (EQ (prop, Qunbound))
22179 prop = Qnil;
22180 }
22181
22182 if (INTEGERP (prop) || FLOATP (prop))
22183 {
22184 int base_unit = (width_p
22185 ? FRAME_COLUMN_WIDTH (it->f)
22186 : FRAME_LINE_HEIGHT (it->f));
22187 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22188 }
22189
22190 if (CONSP (prop))
22191 {
22192 Lisp_Object car = XCAR (prop);
22193 Lisp_Object cdr = XCDR (prop);
22194
22195 if (SYMBOLP (car))
22196 {
22197 #ifdef HAVE_WINDOW_SYSTEM
22198 if (FRAME_WINDOW_P (it->f)
22199 && valid_image_p (prop))
22200 {
22201 ptrdiff_t id = lookup_image (it->f, prop);
22202 struct image *img = IMAGE_FROM_ID (it->f, id);
22203
22204 return OK_PIXELS (width_p ? img->width : img->height);
22205 }
22206 #endif
22207 if (EQ (car, Qplus) || EQ (car, Qminus))
22208 {
22209 int first = 1;
22210 double px;
22211
22212 pixels = 0;
22213 while (CONSP (cdr))
22214 {
22215 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22216 font, width_p, align_to))
22217 return 0;
22218 if (first)
22219 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22220 else
22221 pixels += px;
22222 cdr = XCDR (cdr);
22223 }
22224 if (EQ (car, Qminus))
22225 pixels = -pixels;
22226 return OK_PIXELS (pixels);
22227 }
22228
22229 car = buffer_local_value_1 (car, it->w->buffer);
22230 if (EQ (car, Qunbound))
22231 car = Qnil;
22232 }
22233
22234 if (INTEGERP (car) || FLOATP (car))
22235 {
22236 double fact;
22237 pixels = XFLOATINT (car);
22238 if (NILP (cdr))
22239 return OK_PIXELS (pixels);
22240 if (calc_pixel_width_or_height (&fact, it, cdr,
22241 font, width_p, align_to))
22242 return OK_PIXELS (pixels * fact);
22243 return 0;
22244 }
22245
22246 return 0;
22247 }
22248
22249 return 0;
22250 }
22251
22252 \f
22253 /***********************************************************************
22254 Glyph Display
22255 ***********************************************************************/
22256
22257 #ifdef HAVE_WINDOW_SYSTEM
22258
22259 #ifdef GLYPH_DEBUG
22260
22261 void
22262 dump_glyph_string (struct glyph_string *s)
22263 {
22264 fprintf (stderr, "glyph string\n");
22265 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22266 s->x, s->y, s->width, s->height);
22267 fprintf (stderr, " ybase = %d\n", s->ybase);
22268 fprintf (stderr, " hl = %d\n", s->hl);
22269 fprintf (stderr, " left overhang = %d, right = %d\n",
22270 s->left_overhang, s->right_overhang);
22271 fprintf (stderr, " nchars = %d\n", s->nchars);
22272 fprintf (stderr, " extends to end of line = %d\n",
22273 s->extends_to_end_of_line_p);
22274 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22275 fprintf (stderr, " bg width = %d\n", s->background_width);
22276 }
22277
22278 #endif /* GLYPH_DEBUG */
22279
22280 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22281 of XChar2b structures for S; it can't be allocated in
22282 init_glyph_string because it must be allocated via `alloca'. W
22283 is the window on which S is drawn. ROW and AREA are the glyph row
22284 and area within the row from which S is constructed. START is the
22285 index of the first glyph structure covered by S. HL is a
22286 face-override for drawing S. */
22287
22288 #ifdef HAVE_NTGUI
22289 #define OPTIONAL_HDC(hdc) HDC hdc,
22290 #define DECLARE_HDC(hdc) HDC hdc;
22291 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22292 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22293 #endif
22294
22295 #ifndef OPTIONAL_HDC
22296 #define OPTIONAL_HDC(hdc)
22297 #define DECLARE_HDC(hdc)
22298 #define ALLOCATE_HDC(hdc, f)
22299 #define RELEASE_HDC(hdc, f)
22300 #endif
22301
22302 static void
22303 init_glyph_string (struct glyph_string *s,
22304 OPTIONAL_HDC (hdc)
22305 XChar2b *char2b, struct window *w, struct glyph_row *row,
22306 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22307 {
22308 memset (s, 0, sizeof *s);
22309 s->w = w;
22310 s->f = XFRAME (w->frame);
22311 #ifdef HAVE_NTGUI
22312 s->hdc = hdc;
22313 #endif
22314 s->display = FRAME_X_DISPLAY (s->f);
22315 s->window = FRAME_X_WINDOW (s->f);
22316 s->char2b = char2b;
22317 s->hl = hl;
22318 s->row = row;
22319 s->area = area;
22320 s->first_glyph = row->glyphs[area] + start;
22321 s->height = row->height;
22322 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22323 s->ybase = s->y + row->ascent;
22324 }
22325
22326
22327 /* Append the list of glyph strings with head H and tail T to the list
22328 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22329
22330 static inline void
22331 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22332 struct glyph_string *h, struct glyph_string *t)
22333 {
22334 if (h)
22335 {
22336 if (*head)
22337 (*tail)->next = h;
22338 else
22339 *head = h;
22340 h->prev = *tail;
22341 *tail = t;
22342 }
22343 }
22344
22345
22346 /* Prepend the list of glyph strings with head H and tail T to the
22347 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22348 result. */
22349
22350 static inline void
22351 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22352 struct glyph_string *h, struct glyph_string *t)
22353 {
22354 if (h)
22355 {
22356 if (*head)
22357 (*head)->prev = t;
22358 else
22359 *tail = t;
22360 t->next = *head;
22361 *head = h;
22362 }
22363 }
22364
22365
22366 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22367 Set *HEAD and *TAIL to the resulting list. */
22368
22369 static inline void
22370 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22371 struct glyph_string *s)
22372 {
22373 s->next = s->prev = NULL;
22374 append_glyph_string_lists (head, tail, s, s);
22375 }
22376
22377
22378 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22379 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22380 make sure that X resources for the face returned are allocated.
22381 Value is a pointer to a realized face that is ready for display if
22382 DISPLAY_P is non-zero. */
22383
22384 static inline struct face *
22385 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22386 XChar2b *char2b, int display_p)
22387 {
22388 struct face *face = FACE_FROM_ID (f, face_id);
22389
22390 if (face->font)
22391 {
22392 unsigned code = face->font->driver->encode_char (face->font, c);
22393
22394 if (code != FONT_INVALID_CODE)
22395 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22396 else
22397 STORE_XCHAR2B (char2b, 0, 0);
22398 }
22399
22400 /* Make sure X resources of the face are allocated. */
22401 #ifdef HAVE_X_WINDOWS
22402 if (display_p)
22403 #endif
22404 {
22405 eassert (face != NULL);
22406 PREPARE_FACE_FOR_DISPLAY (f, face);
22407 }
22408
22409 return face;
22410 }
22411
22412
22413 /* Get face and two-byte form of character glyph GLYPH on frame F.
22414 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22415 a pointer to a realized face that is ready for display. */
22416
22417 static inline struct face *
22418 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22419 XChar2b *char2b, int *two_byte_p)
22420 {
22421 struct face *face;
22422
22423 eassert (glyph->type == CHAR_GLYPH);
22424 face = FACE_FROM_ID (f, glyph->face_id);
22425
22426 if (two_byte_p)
22427 *two_byte_p = 0;
22428
22429 if (face->font)
22430 {
22431 unsigned code;
22432
22433 if (CHAR_BYTE8_P (glyph->u.ch))
22434 code = CHAR_TO_BYTE8 (glyph->u.ch);
22435 else
22436 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22437
22438 if (code != FONT_INVALID_CODE)
22439 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22440 else
22441 STORE_XCHAR2B (char2b, 0, 0);
22442 }
22443
22444 /* Make sure X resources of the face are allocated. */
22445 eassert (face != NULL);
22446 PREPARE_FACE_FOR_DISPLAY (f, face);
22447 return face;
22448 }
22449
22450
22451 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22452 Return 1 if FONT has a glyph for C, otherwise return 0. */
22453
22454 static inline int
22455 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22456 {
22457 unsigned code;
22458
22459 if (CHAR_BYTE8_P (c))
22460 code = CHAR_TO_BYTE8 (c);
22461 else
22462 code = font->driver->encode_char (font, c);
22463
22464 if (code == FONT_INVALID_CODE)
22465 return 0;
22466 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22467 return 1;
22468 }
22469
22470
22471 /* Fill glyph string S with composition components specified by S->cmp.
22472
22473 BASE_FACE is the base face of the composition.
22474 S->cmp_from is the index of the first component for S.
22475
22476 OVERLAPS non-zero means S should draw the foreground only, and use
22477 its physical height for clipping. See also draw_glyphs.
22478
22479 Value is the index of a component not in S. */
22480
22481 static int
22482 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22483 int overlaps)
22484 {
22485 int i;
22486 /* For all glyphs of this composition, starting at the offset
22487 S->cmp_from, until we reach the end of the definition or encounter a
22488 glyph that requires the different face, add it to S. */
22489 struct face *face;
22490
22491 eassert (s);
22492
22493 s->for_overlaps = overlaps;
22494 s->face = NULL;
22495 s->font = NULL;
22496 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22497 {
22498 int c = COMPOSITION_GLYPH (s->cmp, i);
22499
22500 /* TAB in a composition means display glyphs with padding space
22501 on the left or right. */
22502 if (c != '\t')
22503 {
22504 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22505 -1, Qnil);
22506
22507 face = get_char_face_and_encoding (s->f, c, face_id,
22508 s->char2b + i, 1);
22509 if (face)
22510 {
22511 if (! s->face)
22512 {
22513 s->face = face;
22514 s->font = s->face->font;
22515 }
22516 else if (s->face != face)
22517 break;
22518 }
22519 }
22520 ++s->nchars;
22521 }
22522 s->cmp_to = i;
22523
22524 if (s->face == NULL)
22525 {
22526 s->face = base_face->ascii_face;
22527 s->font = s->face->font;
22528 }
22529
22530 /* All glyph strings for the same composition has the same width,
22531 i.e. the width set for the first component of the composition. */
22532 s->width = s->first_glyph->pixel_width;
22533
22534 /* If the specified font could not be loaded, use the frame's
22535 default font, but record the fact that we couldn't load it in
22536 the glyph string so that we can draw rectangles for the
22537 characters of the glyph string. */
22538 if (s->font == NULL)
22539 {
22540 s->font_not_found_p = 1;
22541 s->font = FRAME_FONT (s->f);
22542 }
22543
22544 /* Adjust base line for subscript/superscript text. */
22545 s->ybase += s->first_glyph->voffset;
22546
22547 /* This glyph string must always be drawn with 16-bit functions. */
22548 s->two_byte_p = 1;
22549
22550 return s->cmp_to;
22551 }
22552
22553 static int
22554 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22555 int start, int end, int overlaps)
22556 {
22557 struct glyph *glyph, *last;
22558 Lisp_Object lgstring;
22559 int i;
22560
22561 s->for_overlaps = overlaps;
22562 glyph = s->row->glyphs[s->area] + start;
22563 last = s->row->glyphs[s->area] + end;
22564 s->cmp_id = glyph->u.cmp.id;
22565 s->cmp_from = glyph->slice.cmp.from;
22566 s->cmp_to = glyph->slice.cmp.to + 1;
22567 s->face = FACE_FROM_ID (s->f, face_id);
22568 lgstring = composition_gstring_from_id (s->cmp_id);
22569 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22570 glyph++;
22571 while (glyph < last
22572 && glyph->u.cmp.automatic
22573 && glyph->u.cmp.id == s->cmp_id
22574 && s->cmp_to == glyph->slice.cmp.from)
22575 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22576
22577 for (i = s->cmp_from; i < s->cmp_to; i++)
22578 {
22579 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22580 unsigned code = LGLYPH_CODE (lglyph);
22581
22582 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22583 }
22584 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22585 return glyph - s->row->glyphs[s->area];
22586 }
22587
22588
22589 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22590 See the comment of fill_glyph_string for arguments.
22591 Value is the index of the first glyph not in S. */
22592
22593
22594 static int
22595 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22596 int start, int end, int overlaps)
22597 {
22598 struct glyph *glyph, *last;
22599 int voffset;
22600
22601 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22602 s->for_overlaps = overlaps;
22603 glyph = s->row->glyphs[s->area] + start;
22604 last = s->row->glyphs[s->area] + end;
22605 voffset = glyph->voffset;
22606 s->face = FACE_FROM_ID (s->f, face_id);
22607 s->font = s->face->font;
22608 s->nchars = 1;
22609 s->width = glyph->pixel_width;
22610 glyph++;
22611 while (glyph < last
22612 && glyph->type == GLYPHLESS_GLYPH
22613 && glyph->voffset == voffset
22614 && glyph->face_id == face_id)
22615 {
22616 s->nchars++;
22617 s->width += glyph->pixel_width;
22618 glyph++;
22619 }
22620 s->ybase += voffset;
22621 return glyph - s->row->glyphs[s->area];
22622 }
22623
22624
22625 /* Fill glyph string S from a sequence of character glyphs.
22626
22627 FACE_ID is the face id of the string. START is the index of the
22628 first glyph to consider, END is the index of the last + 1.
22629 OVERLAPS non-zero means S should draw the foreground only, and use
22630 its physical height for clipping. See also draw_glyphs.
22631
22632 Value is the index of the first glyph not in S. */
22633
22634 static int
22635 fill_glyph_string (struct glyph_string *s, int face_id,
22636 int start, int end, int overlaps)
22637 {
22638 struct glyph *glyph, *last;
22639 int voffset;
22640 int glyph_not_available_p;
22641
22642 eassert (s->f == XFRAME (s->w->frame));
22643 eassert (s->nchars == 0);
22644 eassert (start >= 0 && end > start);
22645
22646 s->for_overlaps = overlaps;
22647 glyph = s->row->glyphs[s->area] + start;
22648 last = s->row->glyphs[s->area] + end;
22649 voffset = glyph->voffset;
22650 s->padding_p = glyph->padding_p;
22651 glyph_not_available_p = glyph->glyph_not_available_p;
22652
22653 while (glyph < last
22654 && glyph->type == CHAR_GLYPH
22655 && glyph->voffset == voffset
22656 /* Same face id implies same font, nowadays. */
22657 && glyph->face_id == face_id
22658 && glyph->glyph_not_available_p == glyph_not_available_p)
22659 {
22660 int two_byte_p;
22661
22662 s->face = get_glyph_face_and_encoding (s->f, glyph,
22663 s->char2b + s->nchars,
22664 &two_byte_p);
22665 s->two_byte_p = two_byte_p;
22666 ++s->nchars;
22667 eassert (s->nchars <= end - start);
22668 s->width += glyph->pixel_width;
22669 if (glyph++->padding_p != s->padding_p)
22670 break;
22671 }
22672
22673 s->font = s->face->font;
22674
22675 /* If the specified font could not be loaded, use the frame's font,
22676 but record the fact that we couldn't load it in
22677 S->font_not_found_p so that we can draw rectangles for the
22678 characters of the glyph string. */
22679 if (s->font == NULL || glyph_not_available_p)
22680 {
22681 s->font_not_found_p = 1;
22682 s->font = FRAME_FONT (s->f);
22683 }
22684
22685 /* Adjust base line for subscript/superscript text. */
22686 s->ybase += voffset;
22687
22688 eassert (s->face && s->face->gc);
22689 return glyph - s->row->glyphs[s->area];
22690 }
22691
22692
22693 /* Fill glyph string S from image glyph S->first_glyph. */
22694
22695 static void
22696 fill_image_glyph_string (struct glyph_string *s)
22697 {
22698 eassert (s->first_glyph->type == IMAGE_GLYPH);
22699 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22700 eassert (s->img);
22701 s->slice = s->first_glyph->slice.img;
22702 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22703 s->font = s->face->font;
22704 s->width = s->first_glyph->pixel_width;
22705
22706 /* Adjust base line for subscript/superscript text. */
22707 s->ybase += s->first_glyph->voffset;
22708 }
22709
22710
22711 /* Fill glyph string S from a sequence of stretch glyphs.
22712
22713 START is the index of the first glyph to consider,
22714 END is the index of the last + 1.
22715
22716 Value is the index of the first glyph not in S. */
22717
22718 static int
22719 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22720 {
22721 struct glyph *glyph, *last;
22722 int voffset, face_id;
22723
22724 eassert (s->first_glyph->type == STRETCH_GLYPH);
22725
22726 glyph = s->row->glyphs[s->area] + start;
22727 last = s->row->glyphs[s->area] + end;
22728 face_id = glyph->face_id;
22729 s->face = FACE_FROM_ID (s->f, face_id);
22730 s->font = s->face->font;
22731 s->width = glyph->pixel_width;
22732 s->nchars = 1;
22733 voffset = glyph->voffset;
22734
22735 for (++glyph;
22736 (glyph < last
22737 && glyph->type == STRETCH_GLYPH
22738 && glyph->voffset == voffset
22739 && glyph->face_id == face_id);
22740 ++glyph)
22741 s->width += glyph->pixel_width;
22742
22743 /* Adjust base line for subscript/superscript text. */
22744 s->ybase += voffset;
22745
22746 /* The case that face->gc == 0 is handled when drawing the glyph
22747 string by calling PREPARE_FACE_FOR_DISPLAY. */
22748 eassert (s->face);
22749 return glyph - s->row->glyphs[s->area];
22750 }
22751
22752 static struct font_metrics *
22753 get_per_char_metric (struct font *font, XChar2b *char2b)
22754 {
22755 static struct font_metrics metrics;
22756 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22757
22758 if (! font || code == FONT_INVALID_CODE)
22759 return NULL;
22760 font->driver->text_extents (font, &code, 1, &metrics);
22761 return &metrics;
22762 }
22763
22764 /* EXPORT for RIF:
22765 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22766 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22767 assumed to be zero. */
22768
22769 void
22770 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22771 {
22772 *left = *right = 0;
22773
22774 if (glyph->type == CHAR_GLYPH)
22775 {
22776 struct face *face;
22777 XChar2b char2b;
22778 struct font_metrics *pcm;
22779
22780 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22781 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22782 {
22783 if (pcm->rbearing > pcm->width)
22784 *right = pcm->rbearing - pcm->width;
22785 if (pcm->lbearing < 0)
22786 *left = -pcm->lbearing;
22787 }
22788 }
22789 else if (glyph->type == COMPOSITE_GLYPH)
22790 {
22791 if (! glyph->u.cmp.automatic)
22792 {
22793 struct composition *cmp = composition_table[glyph->u.cmp.id];
22794
22795 if (cmp->rbearing > cmp->pixel_width)
22796 *right = cmp->rbearing - cmp->pixel_width;
22797 if (cmp->lbearing < 0)
22798 *left = - cmp->lbearing;
22799 }
22800 else
22801 {
22802 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22803 struct font_metrics metrics;
22804
22805 composition_gstring_width (gstring, glyph->slice.cmp.from,
22806 glyph->slice.cmp.to + 1, &metrics);
22807 if (metrics.rbearing > metrics.width)
22808 *right = metrics.rbearing - metrics.width;
22809 if (metrics.lbearing < 0)
22810 *left = - metrics.lbearing;
22811 }
22812 }
22813 }
22814
22815
22816 /* Return the index of the first glyph preceding glyph string S that
22817 is overwritten by S because of S's left overhang. Value is -1
22818 if no glyphs are overwritten. */
22819
22820 static int
22821 left_overwritten (struct glyph_string *s)
22822 {
22823 int k;
22824
22825 if (s->left_overhang)
22826 {
22827 int x = 0, i;
22828 struct glyph *glyphs = s->row->glyphs[s->area];
22829 int first = s->first_glyph - glyphs;
22830
22831 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22832 x -= glyphs[i].pixel_width;
22833
22834 k = i + 1;
22835 }
22836 else
22837 k = -1;
22838
22839 return k;
22840 }
22841
22842
22843 /* Return the index of the first glyph preceding glyph string S that
22844 is overwriting S because of its right overhang. Value is -1 if no
22845 glyph in front of S overwrites S. */
22846
22847 static int
22848 left_overwriting (struct glyph_string *s)
22849 {
22850 int i, k, x;
22851 struct glyph *glyphs = s->row->glyphs[s->area];
22852 int first = s->first_glyph - glyphs;
22853
22854 k = -1;
22855 x = 0;
22856 for (i = first - 1; i >= 0; --i)
22857 {
22858 int left, right;
22859 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22860 if (x + right > 0)
22861 k = i;
22862 x -= glyphs[i].pixel_width;
22863 }
22864
22865 return k;
22866 }
22867
22868
22869 /* Return the index of the last glyph following glyph string S that is
22870 overwritten by S because of S's right overhang. Value is -1 if
22871 no such glyph is found. */
22872
22873 static int
22874 right_overwritten (struct glyph_string *s)
22875 {
22876 int k = -1;
22877
22878 if (s->right_overhang)
22879 {
22880 int x = 0, i;
22881 struct glyph *glyphs = s->row->glyphs[s->area];
22882 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22883 int end = s->row->used[s->area];
22884
22885 for (i = first; i < end && s->right_overhang > x; ++i)
22886 x += glyphs[i].pixel_width;
22887
22888 k = i;
22889 }
22890
22891 return k;
22892 }
22893
22894
22895 /* Return the index of the last glyph following glyph string S that
22896 overwrites S because of its left overhang. Value is negative
22897 if no such glyph is found. */
22898
22899 static int
22900 right_overwriting (struct glyph_string *s)
22901 {
22902 int i, k, x;
22903 int end = s->row->used[s->area];
22904 struct glyph *glyphs = s->row->glyphs[s->area];
22905 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22906
22907 k = -1;
22908 x = 0;
22909 for (i = first; i < end; ++i)
22910 {
22911 int left, right;
22912 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22913 if (x - left < 0)
22914 k = i;
22915 x += glyphs[i].pixel_width;
22916 }
22917
22918 return k;
22919 }
22920
22921
22922 /* Set background width of glyph string S. START is the index of the
22923 first glyph following S. LAST_X is the right-most x-position + 1
22924 in the drawing area. */
22925
22926 static inline void
22927 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22928 {
22929 /* If the face of this glyph string has to be drawn to the end of
22930 the drawing area, set S->extends_to_end_of_line_p. */
22931
22932 if (start == s->row->used[s->area]
22933 && s->area == TEXT_AREA
22934 && ((s->row->fill_line_p
22935 && (s->hl == DRAW_NORMAL_TEXT
22936 || s->hl == DRAW_IMAGE_RAISED
22937 || s->hl == DRAW_IMAGE_SUNKEN))
22938 || s->hl == DRAW_MOUSE_FACE))
22939 s->extends_to_end_of_line_p = 1;
22940
22941 /* If S extends its face to the end of the line, set its
22942 background_width to the distance to the right edge of the drawing
22943 area. */
22944 if (s->extends_to_end_of_line_p)
22945 s->background_width = last_x - s->x + 1;
22946 else
22947 s->background_width = s->width;
22948 }
22949
22950
22951 /* Compute overhangs and x-positions for glyph string S and its
22952 predecessors, or successors. X is the starting x-position for S.
22953 BACKWARD_P non-zero means process predecessors. */
22954
22955 static void
22956 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22957 {
22958 if (backward_p)
22959 {
22960 while (s)
22961 {
22962 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22963 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22964 x -= s->width;
22965 s->x = x;
22966 s = s->prev;
22967 }
22968 }
22969 else
22970 {
22971 while (s)
22972 {
22973 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22974 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22975 s->x = x;
22976 x += s->width;
22977 s = s->next;
22978 }
22979 }
22980 }
22981
22982
22983
22984 /* The following macros are only called from draw_glyphs below.
22985 They reference the following parameters of that function directly:
22986 `w', `row', `area', and `overlap_p'
22987 as well as the following local variables:
22988 `s', `f', and `hdc' (in W32) */
22989
22990 #ifdef HAVE_NTGUI
22991 /* On W32, silently add local `hdc' variable to argument list of
22992 init_glyph_string. */
22993 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22994 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22995 #else
22996 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22997 init_glyph_string (s, char2b, w, row, area, start, hl)
22998 #endif
22999
23000 /* Add a glyph string for a stretch glyph to the list of strings
23001 between HEAD and TAIL. START is the index of the stretch glyph in
23002 row area AREA of glyph row ROW. END is the index of the last glyph
23003 in that glyph row area. X is the current output position assigned
23004 to the new glyph string constructed. HL overrides that face of the
23005 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23006 is the right-most x-position of the drawing area. */
23007
23008 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23009 and below -- keep them on one line. */
23010 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23011 do \
23012 { \
23013 s = (struct glyph_string *) alloca (sizeof *s); \
23014 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23015 START = fill_stretch_glyph_string (s, START, END); \
23016 append_glyph_string (&HEAD, &TAIL, s); \
23017 s->x = (X); \
23018 } \
23019 while (0)
23020
23021
23022 /* Add a glyph string for an image glyph to the list of strings
23023 between HEAD and TAIL. START is the index of the image glyph in
23024 row area AREA of glyph row ROW. END is the index of the last glyph
23025 in that glyph row area. X is the current output position assigned
23026 to the new glyph string constructed. HL overrides that face of the
23027 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23028 is the right-most x-position of the drawing area. */
23029
23030 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23031 do \
23032 { \
23033 s = (struct glyph_string *) alloca (sizeof *s); \
23034 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23035 fill_image_glyph_string (s); \
23036 append_glyph_string (&HEAD, &TAIL, s); \
23037 ++START; \
23038 s->x = (X); \
23039 } \
23040 while (0)
23041
23042
23043 /* Add a glyph string for a sequence of character glyphs to the list
23044 of strings between HEAD and TAIL. START is the index of the first
23045 glyph in row area AREA of glyph row ROW that is part of the new
23046 glyph string. END is the index of the last glyph in that glyph row
23047 area. X is the current output position assigned to the new glyph
23048 string constructed. HL overrides that face of the glyph; e.g. it
23049 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23050 right-most x-position of the drawing area. */
23051
23052 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23053 do \
23054 { \
23055 int face_id; \
23056 XChar2b *char2b; \
23057 \
23058 face_id = (row)->glyphs[area][START].face_id; \
23059 \
23060 s = (struct glyph_string *) alloca (sizeof *s); \
23061 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
23062 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23063 append_glyph_string (&HEAD, &TAIL, s); \
23064 s->x = (X); \
23065 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23066 } \
23067 while (0)
23068
23069
23070 /* Add a glyph string for a composite sequence to the list of strings
23071 between HEAD and TAIL. START is the index of the first glyph in
23072 row area AREA of glyph row ROW that is part of the new glyph
23073 string. END is the index of the last glyph in that glyph row area.
23074 X is the current output position assigned to the new glyph string
23075 constructed. HL overrides that face of the glyph; e.g. it is
23076 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23077 x-position of the drawing area. */
23078
23079 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23080 do { \
23081 int face_id = (row)->glyphs[area][START].face_id; \
23082 struct face *base_face = FACE_FROM_ID (f, face_id); \
23083 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23084 struct composition *cmp = composition_table[cmp_id]; \
23085 XChar2b *char2b; \
23086 struct glyph_string *first_s = NULL; \
23087 int n; \
23088 \
23089 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
23090 \
23091 /* Make glyph_strings for each glyph sequence that is drawable by \
23092 the same face, and append them to HEAD/TAIL. */ \
23093 for (n = 0; n < cmp->glyph_len;) \
23094 { \
23095 s = (struct glyph_string *) alloca (sizeof *s); \
23096 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23097 append_glyph_string (&(HEAD), &(TAIL), s); \
23098 s->cmp = cmp; \
23099 s->cmp_from = n; \
23100 s->x = (X); \
23101 if (n == 0) \
23102 first_s = s; \
23103 n = fill_composite_glyph_string (s, base_face, overlaps); \
23104 } \
23105 \
23106 ++START; \
23107 s = first_s; \
23108 } while (0)
23109
23110
23111 /* Add a glyph string for a glyph-string sequence to the list of strings
23112 between HEAD and TAIL. */
23113
23114 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23115 do { \
23116 int face_id; \
23117 XChar2b *char2b; \
23118 Lisp_Object gstring; \
23119 \
23120 face_id = (row)->glyphs[area][START].face_id; \
23121 gstring = (composition_gstring_from_id \
23122 ((row)->glyphs[area][START].u.cmp.id)); \
23123 s = (struct glyph_string *) alloca (sizeof *s); \
23124 char2b = (XChar2b *) alloca ((sizeof *char2b) \
23125 * LGSTRING_GLYPH_LEN (gstring)); \
23126 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23127 append_glyph_string (&(HEAD), &(TAIL), s); \
23128 s->x = (X); \
23129 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23130 } while (0)
23131
23132
23133 /* Add a glyph string for a sequence of glyphless character's glyphs
23134 to the list of strings between HEAD and TAIL. The meanings of
23135 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23136
23137 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23138 do \
23139 { \
23140 int face_id; \
23141 \
23142 face_id = (row)->glyphs[area][START].face_id; \
23143 \
23144 s = (struct glyph_string *) alloca (sizeof *s); \
23145 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23146 append_glyph_string (&HEAD, &TAIL, s); \
23147 s->x = (X); \
23148 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23149 overlaps); \
23150 } \
23151 while (0)
23152
23153
23154 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23155 of AREA of glyph row ROW on window W between indices START and END.
23156 HL overrides the face for drawing glyph strings, e.g. it is
23157 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23158 x-positions of the drawing area.
23159
23160 This is an ugly monster macro construct because we must use alloca
23161 to allocate glyph strings (because draw_glyphs can be called
23162 asynchronously). */
23163
23164 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23165 do \
23166 { \
23167 HEAD = TAIL = NULL; \
23168 while (START < END) \
23169 { \
23170 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23171 switch (first_glyph->type) \
23172 { \
23173 case CHAR_GLYPH: \
23174 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23175 HL, X, LAST_X); \
23176 break; \
23177 \
23178 case COMPOSITE_GLYPH: \
23179 if (first_glyph->u.cmp.automatic) \
23180 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23181 HL, X, LAST_X); \
23182 else \
23183 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23184 HL, X, LAST_X); \
23185 break; \
23186 \
23187 case STRETCH_GLYPH: \
23188 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23189 HL, X, LAST_X); \
23190 break; \
23191 \
23192 case IMAGE_GLYPH: \
23193 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23194 HL, X, LAST_X); \
23195 break; \
23196 \
23197 case GLYPHLESS_GLYPH: \
23198 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23199 HL, X, LAST_X); \
23200 break; \
23201 \
23202 default: \
23203 abort (); \
23204 } \
23205 \
23206 if (s) \
23207 { \
23208 set_glyph_string_background_width (s, START, LAST_X); \
23209 (X) += s->width; \
23210 } \
23211 } \
23212 } while (0)
23213
23214
23215 /* Draw glyphs between START and END in AREA of ROW on window W,
23216 starting at x-position X. X is relative to AREA in W. HL is a
23217 face-override with the following meaning:
23218
23219 DRAW_NORMAL_TEXT draw normally
23220 DRAW_CURSOR draw in cursor face
23221 DRAW_MOUSE_FACE draw in mouse face.
23222 DRAW_INVERSE_VIDEO draw in mode line face
23223 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23224 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23225
23226 If OVERLAPS is non-zero, draw only the foreground of characters and
23227 clip to the physical height of ROW. Non-zero value also defines
23228 the overlapping part to be drawn:
23229
23230 OVERLAPS_PRED overlap with preceding rows
23231 OVERLAPS_SUCC overlap with succeeding rows
23232 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23233 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23234
23235 Value is the x-position reached, relative to AREA of W. */
23236
23237 static int
23238 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23239 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23240 enum draw_glyphs_face hl, int overlaps)
23241 {
23242 struct glyph_string *head, *tail;
23243 struct glyph_string *s;
23244 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23245 int i, j, x_reached, last_x, area_left = 0;
23246 struct frame *f = XFRAME (WINDOW_FRAME (w));
23247 DECLARE_HDC (hdc);
23248
23249 ALLOCATE_HDC (hdc, f);
23250
23251 /* Let's rather be paranoid than getting a SEGV. */
23252 end = min (end, row->used[area]);
23253 start = max (0, start);
23254 start = min (end, start);
23255
23256 /* Translate X to frame coordinates. Set last_x to the right
23257 end of the drawing area. */
23258 if (row->full_width_p)
23259 {
23260 /* X is relative to the left edge of W, without scroll bars
23261 or fringes. */
23262 area_left = WINDOW_LEFT_EDGE_X (w);
23263 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23264 }
23265 else
23266 {
23267 area_left = window_box_left (w, area);
23268 last_x = area_left + window_box_width (w, area);
23269 }
23270 x += area_left;
23271
23272 /* Build a doubly-linked list of glyph_string structures between
23273 head and tail from what we have to draw. Note that the macro
23274 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23275 the reason we use a separate variable `i'. */
23276 i = start;
23277 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23278 if (tail)
23279 x_reached = tail->x + tail->background_width;
23280 else
23281 x_reached = x;
23282
23283 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23284 the row, redraw some glyphs in front or following the glyph
23285 strings built above. */
23286 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23287 {
23288 struct glyph_string *h, *t;
23289 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23290 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23291 int check_mouse_face = 0;
23292 int dummy_x = 0;
23293
23294 /* If mouse highlighting is on, we may need to draw adjacent
23295 glyphs using mouse-face highlighting. */
23296 if (area == TEXT_AREA && row->mouse_face_p)
23297 {
23298 struct glyph_row *mouse_beg_row, *mouse_end_row;
23299
23300 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23301 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23302
23303 if (row >= mouse_beg_row && row <= mouse_end_row)
23304 {
23305 check_mouse_face = 1;
23306 mouse_beg_col = (row == mouse_beg_row)
23307 ? hlinfo->mouse_face_beg_col : 0;
23308 mouse_end_col = (row == mouse_end_row)
23309 ? hlinfo->mouse_face_end_col
23310 : row->used[TEXT_AREA];
23311 }
23312 }
23313
23314 /* Compute overhangs for all glyph strings. */
23315 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23316 for (s = head; s; s = s->next)
23317 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23318
23319 /* Prepend glyph strings for glyphs in front of the first glyph
23320 string that are overwritten because of the first glyph
23321 string's left overhang. The background of all strings
23322 prepended must be drawn because the first glyph string
23323 draws over it. */
23324 i = left_overwritten (head);
23325 if (i >= 0)
23326 {
23327 enum draw_glyphs_face overlap_hl;
23328
23329 /* If this row contains mouse highlighting, attempt to draw
23330 the overlapped glyphs with the correct highlight. This
23331 code fails if the overlap encompasses more than one glyph
23332 and mouse-highlight spans only some of these glyphs.
23333 However, making it work perfectly involves a lot more
23334 code, and I don't know if the pathological case occurs in
23335 practice, so we'll stick to this for now. --- cyd */
23336 if (check_mouse_face
23337 && mouse_beg_col < start && mouse_end_col > i)
23338 overlap_hl = DRAW_MOUSE_FACE;
23339 else
23340 overlap_hl = DRAW_NORMAL_TEXT;
23341
23342 j = i;
23343 BUILD_GLYPH_STRINGS (j, start, h, t,
23344 overlap_hl, dummy_x, last_x);
23345 start = i;
23346 compute_overhangs_and_x (t, head->x, 1);
23347 prepend_glyph_string_lists (&head, &tail, h, t);
23348 clip_head = head;
23349 }
23350
23351 /* Prepend glyph strings for glyphs in front of the first glyph
23352 string that overwrite that glyph string because of their
23353 right overhang. For these strings, only the foreground must
23354 be drawn, because it draws over the glyph string at `head'.
23355 The background must not be drawn because this would overwrite
23356 right overhangs of preceding glyphs for which no glyph
23357 strings exist. */
23358 i = left_overwriting (head);
23359 if (i >= 0)
23360 {
23361 enum draw_glyphs_face overlap_hl;
23362
23363 if (check_mouse_face
23364 && mouse_beg_col < start && mouse_end_col > i)
23365 overlap_hl = DRAW_MOUSE_FACE;
23366 else
23367 overlap_hl = DRAW_NORMAL_TEXT;
23368
23369 clip_head = head;
23370 BUILD_GLYPH_STRINGS (i, start, h, t,
23371 overlap_hl, dummy_x, last_x);
23372 for (s = h; s; s = s->next)
23373 s->background_filled_p = 1;
23374 compute_overhangs_and_x (t, head->x, 1);
23375 prepend_glyph_string_lists (&head, &tail, h, t);
23376 }
23377
23378 /* Append glyphs strings for glyphs following the last glyph
23379 string tail that are overwritten by tail. The background of
23380 these strings has to be drawn because tail's foreground draws
23381 over it. */
23382 i = right_overwritten (tail);
23383 if (i >= 0)
23384 {
23385 enum draw_glyphs_face overlap_hl;
23386
23387 if (check_mouse_face
23388 && mouse_beg_col < i && mouse_end_col > end)
23389 overlap_hl = DRAW_MOUSE_FACE;
23390 else
23391 overlap_hl = DRAW_NORMAL_TEXT;
23392
23393 BUILD_GLYPH_STRINGS (end, i, h, t,
23394 overlap_hl, x, last_x);
23395 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23396 we don't have `end = i;' here. */
23397 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23398 append_glyph_string_lists (&head, &tail, h, t);
23399 clip_tail = tail;
23400 }
23401
23402 /* Append glyph strings for glyphs following the last glyph
23403 string tail that overwrite tail. The foreground of such
23404 glyphs has to be drawn because it writes into the background
23405 of tail. The background must not be drawn because it could
23406 paint over the foreground of following glyphs. */
23407 i = right_overwriting (tail);
23408 if (i >= 0)
23409 {
23410 enum draw_glyphs_face overlap_hl;
23411 if (check_mouse_face
23412 && mouse_beg_col < i && mouse_end_col > end)
23413 overlap_hl = DRAW_MOUSE_FACE;
23414 else
23415 overlap_hl = DRAW_NORMAL_TEXT;
23416
23417 clip_tail = tail;
23418 i++; /* We must include the Ith glyph. */
23419 BUILD_GLYPH_STRINGS (end, i, h, t,
23420 overlap_hl, x, last_x);
23421 for (s = h; s; s = s->next)
23422 s->background_filled_p = 1;
23423 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23424 append_glyph_string_lists (&head, &tail, h, t);
23425 }
23426 if (clip_head || clip_tail)
23427 for (s = head; s; s = s->next)
23428 {
23429 s->clip_head = clip_head;
23430 s->clip_tail = clip_tail;
23431 }
23432 }
23433
23434 /* Draw all strings. */
23435 for (s = head; s; s = s->next)
23436 FRAME_RIF (f)->draw_glyph_string (s);
23437
23438 #ifndef HAVE_NS
23439 /* When focus a sole frame and move horizontally, this sets on_p to 0
23440 causing a failure to erase prev cursor position. */
23441 if (area == TEXT_AREA
23442 && !row->full_width_p
23443 /* When drawing overlapping rows, only the glyph strings'
23444 foreground is drawn, which doesn't erase a cursor
23445 completely. */
23446 && !overlaps)
23447 {
23448 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23449 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23450 : (tail ? tail->x + tail->background_width : x));
23451 x0 -= area_left;
23452 x1 -= area_left;
23453
23454 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23455 row->y, MATRIX_ROW_BOTTOM_Y (row));
23456 }
23457 #endif
23458
23459 /* Value is the x-position up to which drawn, relative to AREA of W.
23460 This doesn't include parts drawn because of overhangs. */
23461 if (row->full_width_p)
23462 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23463 else
23464 x_reached -= area_left;
23465
23466 RELEASE_HDC (hdc, f);
23467
23468 return x_reached;
23469 }
23470
23471 /* Expand row matrix if too narrow. Don't expand if area
23472 is not present. */
23473
23474 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23475 { \
23476 if (!fonts_changed_p \
23477 && (it->glyph_row->glyphs[area] \
23478 < it->glyph_row->glyphs[area + 1])) \
23479 { \
23480 it->w->ncols_scale_factor++; \
23481 fonts_changed_p = 1; \
23482 } \
23483 }
23484
23485 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23486 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23487
23488 static inline void
23489 append_glyph (struct it *it)
23490 {
23491 struct glyph *glyph;
23492 enum glyph_row_area area = it->area;
23493
23494 eassert (it->glyph_row);
23495 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23496
23497 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23498 if (glyph < it->glyph_row->glyphs[area + 1])
23499 {
23500 /* If the glyph row is reversed, we need to prepend the glyph
23501 rather than append it. */
23502 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23503 {
23504 struct glyph *g;
23505
23506 /* Make room for the additional glyph. */
23507 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23508 g[1] = *g;
23509 glyph = it->glyph_row->glyphs[area];
23510 }
23511 glyph->charpos = CHARPOS (it->position);
23512 glyph->object = it->object;
23513 if (it->pixel_width > 0)
23514 {
23515 glyph->pixel_width = it->pixel_width;
23516 glyph->padding_p = 0;
23517 }
23518 else
23519 {
23520 /* Assure at least 1-pixel width. Otherwise, cursor can't
23521 be displayed correctly. */
23522 glyph->pixel_width = 1;
23523 glyph->padding_p = 1;
23524 }
23525 glyph->ascent = it->ascent;
23526 glyph->descent = it->descent;
23527 glyph->voffset = it->voffset;
23528 glyph->type = CHAR_GLYPH;
23529 glyph->avoid_cursor_p = it->avoid_cursor_p;
23530 glyph->multibyte_p = it->multibyte_p;
23531 glyph->left_box_line_p = it->start_of_box_run_p;
23532 glyph->right_box_line_p = it->end_of_box_run_p;
23533 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23534 || it->phys_descent > it->descent);
23535 glyph->glyph_not_available_p = it->glyph_not_available_p;
23536 glyph->face_id = it->face_id;
23537 glyph->u.ch = it->char_to_display;
23538 glyph->slice.img = null_glyph_slice;
23539 glyph->font_type = FONT_TYPE_UNKNOWN;
23540 if (it->bidi_p)
23541 {
23542 glyph->resolved_level = it->bidi_it.resolved_level;
23543 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23544 abort ();
23545 glyph->bidi_type = it->bidi_it.type;
23546 }
23547 else
23548 {
23549 glyph->resolved_level = 0;
23550 glyph->bidi_type = UNKNOWN_BT;
23551 }
23552 ++it->glyph_row->used[area];
23553 }
23554 else
23555 IT_EXPAND_MATRIX_WIDTH (it, area);
23556 }
23557
23558 /* Store one glyph for the composition IT->cmp_it.id in
23559 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23560 non-null. */
23561
23562 static inline void
23563 append_composite_glyph (struct it *it)
23564 {
23565 struct glyph *glyph;
23566 enum glyph_row_area area = it->area;
23567
23568 eassert (it->glyph_row);
23569
23570 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23571 if (glyph < it->glyph_row->glyphs[area + 1])
23572 {
23573 /* If the glyph row is reversed, we need to prepend the glyph
23574 rather than append it. */
23575 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23576 {
23577 struct glyph *g;
23578
23579 /* Make room for the new glyph. */
23580 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23581 g[1] = *g;
23582 glyph = it->glyph_row->glyphs[it->area];
23583 }
23584 glyph->charpos = it->cmp_it.charpos;
23585 glyph->object = it->object;
23586 glyph->pixel_width = it->pixel_width;
23587 glyph->ascent = it->ascent;
23588 glyph->descent = it->descent;
23589 glyph->voffset = it->voffset;
23590 glyph->type = COMPOSITE_GLYPH;
23591 if (it->cmp_it.ch < 0)
23592 {
23593 glyph->u.cmp.automatic = 0;
23594 glyph->u.cmp.id = it->cmp_it.id;
23595 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23596 }
23597 else
23598 {
23599 glyph->u.cmp.automatic = 1;
23600 glyph->u.cmp.id = it->cmp_it.id;
23601 glyph->slice.cmp.from = it->cmp_it.from;
23602 glyph->slice.cmp.to = it->cmp_it.to - 1;
23603 }
23604 glyph->avoid_cursor_p = it->avoid_cursor_p;
23605 glyph->multibyte_p = it->multibyte_p;
23606 glyph->left_box_line_p = it->start_of_box_run_p;
23607 glyph->right_box_line_p = it->end_of_box_run_p;
23608 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23609 || it->phys_descent > it->descent);
23610 glyph->padding_p = 0;
23611 glyph->glyph_not_available_p = 0;
23612 glyph->face_id = it->face_id;
23613 glyph->font_type = FONT_TYPE_UNKNOWN;
23614 if (it->bidi_p)
23615 {
23616 glyph->resolved_level = it->bidi_it.resolved_level;
23617 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23618 abort ();
23619 glyph->bidi_type = it->bidi_it.type;
23620 }
23621 ++it->glyph_row->used[area];
23622 }
23623 else
23624 IT_EXPAND_MATRIX_WIDTH (it, area);
23625 }
23626
23627
23628 /* Change IT->ascent and IT->height according to the setting of
23629 IT->voffset. */
23630
23631 static inline void
23632 take_vertical_position_into_account (struct it *it)
23633 {
23634 if (it->voffset)
23635 {
23636 if (it->voffset < 0)
23637 /* Increase the ascent so that we can display the text higher
23638 in the line. */
23639 it->ascent -= it->voffset;
23640 else
23641 /* Increase the descent so that we can display the text lower
23642 in the line. */
23643 it->descent += it->voffset;
23644 }
23645 }
23646
23647
23648 /* Produce glyphs/get display metrics for the image IT is loaded with.
23649 See the description of struct display_iterator in dispextern.h for
23650 an overview of struct display_iterator. */
23651
23652 static void
23653 produce_image_glyph (struct it *it)
23654 {
23655 struct image *img;
23656 struct face *face;
23657 int glyph_ascent, crop;
23658 struct glyph_slice slice;
23659
23660 eassert (it->what == IT_IMAGE);
23661
23662 face = FACE_FROM_ID (it->f, it->face_id);
23663 eassert (face);
23664 /* Make sure X resources of the face is loaded. */
23665 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23666
23667 if (it->image_id < 0)
23668 {
23669 /* Fringe bitmap. */
23670 it->ascent = it->phys_ascent = 0;
23671 it->descent = it->phys_descent = 0;
23672 it->pixel_width = 0;
23673 it->nglyphs = 0;
23674 return;
23675 }
23676
23677 img = IMAGE_FROM_ID (it->f, it->image_id);
23678 eassert (img);
23679 /* Make sure X resources of the image is loaded. */
23680 prepare_image_for_display (it->f, img);
23681
23682 slice.x = slice.y = 0;
23683 slice.width = img->width;
23684 slice.height = img->height;
23685
23686 if (INTEGERP (it->slice.x))
23687 slice.x = XINT (it->slice.x);
23688 else if (FLOATP (it->slice.x))
23689 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23690
23691 if (INTEGERP (it->slice.y))
23692 slice.y = XINT (it->slice.y);
23693 else if (FLOATP (it->slice.y))
23694 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23695
23696 if (INTEGERP (it->slice.width))
23697 slice.width = XINT (it->slice.width);
23698 else if (FLOATP (it->slice.width))
23699 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23700
23701 if (INTEGERP (it->slice.height))
23702 slice.height = XINT (it->slice.height);
23703 else if (FLOATP (it->slice.height))
23704 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23705
23706 if (slice.x >= img->width)
23707 slice.x = img->width;
23708 if (slice.y >= img->height)
23709 slice.y = img->height;
23710 if (slice.x + slice.width >= img->width)
23711 slice.width = img->width - slice.x;
23712 if (slice.y + slice.height > img->height)
23713 slice.height = img->height - slice.y;
23714
23715 if (slice.width == 0 || slice.height == 0)
23716 return;
23717
23718 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23719
23720 it->descent = slice.height - glyph_ascent;
23721 if (slice.y == 0)
23722 it->descent += img->vmargin;
23723 if (slice.y + slice.height == img->height)
23724 it->descent += img->vmargin;
23725 it->phys_descent = it->descent;
23726
23727 it->pixel_width = slice.width;
23728 if (slice.x == 0)
23729 it->pixel_width += img->hmargin;
23730 if (slice.x + slice.width == img->width)
23731 it->pixel_width += img->hmargin;
23732
23733 /* It's quite possible for images to have an ascent greater than
23734 their height, so don't get confused in that case. */
23735 if (it->descent < 0)
23736 it->descent = 0;
23737
23738 it->nglyphs = 1;
23739
23740 if (face->box != FACE_NO_BOX)
23741 {
23742 if (face->box_line_width > 0)
23743 {
23744 if (slice.y == 0)
23745 it->ascent += face->box_line_width;
23746 if (slice.y + slice.height == img->height)
23747 it->descent += face->box_line_width;
23748 }
23749
23750 if (it->start_of_box_run_p && slice.x == 0)
23751 it->pixel_width += eabs (face->box_line_width);
23752 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23753 it->pixel_width += eabs (face->box_line_width);
23754 }
23755
23756 take_vertical_position_into_account (it);
23757
23758 /* Automatically crop wide image glyphs at right edge so we can
23759 draw the cursor on same display row. */
23760 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23761 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23762 {
23763 it->pixel_width -= crop;
23764 slice.width -= crop;
23765 }
23766
23767 if (it->glyph_row)
23768 {
23769 struct glyph *glyph;
23770 enum glyph_row_area area = it->area;
23771
23772 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23773 if (glyph < it->glyph_row->glyphs[area + 1])
23774 {
23775 glyph->charpos = CHARPOS (it->position);
23776 glyph->object = it->object;
23777 glyph->pixel_width = it->pixel_width;
23778 glyph->ascent = glyph_ascent;
23779 glyph->descent = it->descent;
23780 glyph->voffset = it->voffset;
23781 glyph->type = IMAGE_GLYPH;
23782 glyph->avoid_cursor_p = it->avoid_cursor_p;
23783 glyph->multibyte_p = it->multibyte_p;
23784 glyph->left_box_line_p = it->start_of_box_run_p;
23785 glyph->right_box_line_p = it->end_of_box_run_p;
23786 glyph->overlaps_vertically_p = 0;
23787 glyph->padding_p = 0;
23788 glyph->glyph_not_available_p = 0;
23789 glyph->face_id = it->face_id;
23790 glyph->u.img_id = img->id;
23791 glyph->slice.img = slice;
23792 glyph->font_type = FONT_TYPE_UNKNOWN;
23793 if (it->bidi_p)
23794 {
23795 glyph->resolved_level = it->bidi_it.resolved_level;
23796 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23797 abort ();
23798 glyph->bidi_type = it->bidi_it.type;
23799 }
23800 ++it->glyph_row->used[area];
23801 }
23802 else
23803 IT_EXPAND_MATRIX_WIDTH (it, area);
23804 }
23805 }
23806
23807
23808 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23809 of the glyph, WIDTH and HEIGHT are the width and height of the
23810 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23811
23812 static void
23813 append_stretch_glyph (struct it *it, Lisp_Object object,
23814 int width, int height, int ascent)
23815 {
23816 struct glyph *glyph;
23817 enum glyph_row_area area = it->area;
23818
23819 eassert (ascent >= 0 && ascent <= height);
23820
23821 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23822 if (glyph < it->glyph_row->glyphs[area + 1])
23823 {
23824 /* If the glyph row is reversed, we need to prepend the glyph
23825 rather than append it. */
23826 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23827 {
23828 struct glyph *g;
23829
23830 /* Make room for the additional glyph. */
23831 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23832 g[1] = *g;
23833 glyph = it->glyph_row->glyphs[area];
23834 }
23835 glyph->charpos = CHARPOS (it->position);
23836 glyph->object = object;
23837 glyph->pixel_width = width;
23838 glyph->ascent = ascent;
23839 glyph->descent = height - ascent;
23840 glyph->voffset = it->voffset;
23841 glyph->type = STRETCH_GLYPH;
23842 glyph->avoid_cursor_p = it->avoid_cursor_p;
23843 glyph->multibyte_p = it->multibyte_p;
23844 glyph->left_box_line_p = it->start_of_box_run_p;
23845 glyph->right_box_line_p = it->end_of_box_run_p;
23846 glyph->overlaps_vertically_p = 0;
23847 glyph->padding_p = 0;
23848 glyph->glyph_not_available_p = 0;
23849 glyph->face_id = it->face_id;
23850 glyph->u.stretch.ascent = ascent;
23851 glyph->u.stretch.height = height;
23852 glyph->slice.img = null_glyph_slice;
23853 glyph->font_type = FONT_TYPE_UNKNOWN;
23854 if (it->bidi_p)
23855 {
23856 glyph->resolved_level = it->bidi_it.resolved_level;
23857 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23858 abort ();
23859 glyph->bidi_type = it->bidi_it.type;
23860 }
23861 else
23862 {
23863 glyph->resolved_level = 0;
23864 glyph->bidi_type = UNKNOWN_BT;
23865 }
23866 ++it->glyph_row->used[area];
23867 }
23868 else
23869 IT_EXPAND_MATRIX_WIDTH (it, area);
23870 }
23871
23872 #endif /* HAVE_WINDOW_SYSTEM */
23873
23874 /* Produce a stretch glyph for iterator IT. IT->object is the value
23875 of the glyph property displayed. The value must be a list
23876 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23877 being recognized:
23878
23879 1. `:width WIDTH' specifies that the space should be WIDTH *
23880 canonical char width wide. WIDTH may be an integer or floating
23881 point number.
23882
23883 2. `:relative-width FACTOR' specifies that the width of the stretch
23884 should be computed from the width of the first character having the
23885 `glyph' property, and should be FACTOR times that width.
23886
23887 3. `:align-to HPOS' specifies that the space should be wide enough
23888 to reach HPOS, a value in canonical character units.
23889
23890 Exactly one of the above pairs must be present.
23891
23892 4. `:height HEIGHT' specifies that the height of the stretch produced
23893 should be HEIGHT, measured in canonical character units.
23894
23895 5. `:relative-height FACTOR' specifies that the height of the
23896 stretch should be FACTOR times the height of the characters having
23897 the glyph property.
23898
23899 Either none or exactly one of 4 or 5 must be present.
23900
23901 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23902 of the stretch should be used for the ascent of the stretch.
23903 ASCENT must be in the range 0 <= ASCENT <= 100. */
23904
23905 void
23906 produce_stretch_glyph (struct it *it)
23907 {
23908 /* (space :width WIDTH :height HEIGHT ...) */
23909 Lisp_Object prop, plist;
23910 int width = 0, height = 0, align_to = -1;
23911 int zero_width_ok_p = 0;
23912 int ascent = 0;
23913 double tem;
23914 struct face *face = NULL;
23915 struct font *font = NULL;
23916
23917 #ifdef HAVE_WINDOW_SYSTEM
23918 int zero_height_ok_p = 0;
23919
23920 if (FRAME_WINDOW_P (it->f))
23921 {
23922 face = FACE_FROM_ID (it->f, it->face_id);
23923 font = face->font ? face->font : FRAME_FONT (it->f);
23924 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23925 }
23926 #endif
23927
23928 /* List should start with `space'. */
23929 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23930 plist = XCDR (it->object);
23931
23932 /* Compute the width of the stretch. */
23933 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23934 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23935 {
23936 /* Absolute width `:width WIDTH' specified and valid. */
23937 zero_width_ok_p = 1;
23938 width = (int)tem;
23939 }
23940 #ifdef HAVE_WINDOW_SYSTEM
23941 else if (FRAME_WINDOW_P (it->f)
23942 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23943 {
23944 /* Relative width `:relative-width FACTOR' specified and valid.
23945 Compute the width of the characters having the `glyph'
23946 property. */
23947 struct it it2;
23948 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23949
23950 it2 = *it;
23951 if (it->multibyte_p)
23952 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23953 else
23954 {
23955 it2.c = it2.char_to_display = *p, it2.len = 1;
23956 if (! ASCII_CHAR_P (it2.c))
23957 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23958 }
23959
23960 it2.glyph_row = NULL;
23961 it2.what = IT_CHARACTER;
23962 x_produce_glyphs (&it2);
23963 width = NUMVAL (prop) * it2.pixel_width;
23964 }
23965 #endif /* HAVE_WINDOW_SYSTEM */
23966 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23967 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23968 {
23969 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23970 align_to = (align_to < 0
23971 ? 0
23972 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23973 else if (align_to < 0)
23974 align_to = window_box_left_offset (it->w, TEXT_AREA);
23975 width = max (0, (int)tem + align_to - it->current_x);
23976 zero_width_ok_p = 1;
23977 }
23978 else
23979 /* Nothing specified -> width defaults to canonical char width. */
23980 width = FRAME_COLUMN_WIDTH (it->f);
23981
23982 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23983 width = 1;
23984
23985 #ifdef HAVE_WINDOW_SYSTEM
23986 /* Compute height. */
23987 if (FRAME_WINDOW_P (it->f))
23988 {
23989 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23990 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23991 {
23992 height = (int)tem;
23993 zero_height_ok_p = 1;
23994 }
23995 else if (prop = Fplist_get (plist, QCrelative_height),
23996 NUMVAL (prop) > 0)
23997 height = FONT_HEIGHT (font) * NUMVAL (prop);
23998 else
23999 height = FONT_HEIGHT (font);
24000
24001 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24002 height = 1;
24003
24004 /* Compute percentage of height used for ascent. If
24005 `:ascent ASCENT' is present and valid, use that. Otherwise,
24006 derive the ascent from the font in use. */
24007 if (prop = Fplist_get (plist, QCascent),
24008 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24009 ascent = height * NUMVAL (prop) / 100.0;
24010 else if (!NILP (prop)
24011 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24012 ascent = min (max (0, (int)tem), height);
24013 else
24014 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24015 }
24016 else
24017 #endif /* HAVE_WINDOW_SYSTEM */
24018 height = 1;
24019
24020 if (width > 0 && it->line_wrap != TRUNCATE
24021 && it->current_x + width > it->last_visible_x)
24022 {
24023 width = it->last_visible_x - it->current_x;
24024 #ifdef HAVE_WINDOW_SYSTEM
24025 /* Subtract one more pixel from the stretch width, but only on
24026 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24027 width -= FRAME_WINDOW_P (it->f);
24028 #endif
24029 }
24030
24031 if (width > 0 && height > 0 && it->glyph_row)
24032 {
24033 Lisp_Object o_object = it->object;
24034 Lisp_Object object = it->stack[it->sp - 1].string;
24035 int n = width;
24036
24037 if (!STRINGP (object))
24038 object = it->w->buffer;
24039 #ifdef HAVE_WINDOW_SYSTEM
24040 if (FRAME_WINDOW_P (it->f))
24041 append_stretch_glyph (it, object, width, height, ascent);
24042 else
24043 #endif
24044 {
24045 it->object = object;
24046 it->char_to_display = ' ';
24047 it->pixel_width = it->len = 1;
24048 while (n--)
24049 tty_append_glyph (it);
24050 it->object = o_object;
24051 }
24052 }
24053
24054 it->pixel_width = width;
24055 #ifdef HAVE_WINDOW_SYSTEM
24056 if (FRAME_WINDOW_P (it->f))
24057 {
24058 it->ascent = it->phys_ascent = ascent;
24059 it->descent = it->phys_descent = height - it->ascent;
24060 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24061 take_vertical_position_into_account (it);
24062 }
24063 else
24064 #endif
24065 it->nglyphs = width;
24066 }
24067
24068 #ifdef HAVE_WINDOW_SYSTEM
24069
24070 /* Calculate line-height and line-spacing properties.
24071 An integer value specifies explicit pixel value.
24072 A float value specifies relative value to current face height.
24073 A cons (float . face-name) specifies relative value to
24074 height of specified face font.
24075
24076 Returns height in pixels, or nil. */
24077
24078
24079 static Lisp_Object
24080 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24081 int boff, int override)
24082 {
24083 Lisp_Object face_name = Qnil;
24084 int ascent, descent, height;
24085
24086 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24087 return val;
24088
24089 if (CONSP (val))
24090 {
24091 face_name = XCAR (val);
24092 val = XCDR (val);
24093 if (!NUMBERP (val))
24094 val = make_number (1);
24095 if (NILP (face_name))
24096 {
24097 height = it->ascent + it->descent;
24098 goto scale;
24099 }
24100 }
24101
24102 if (NILP (face_name))
24103 {
24104 font = FRAME_FONT (it->f);
24105 boff = FRAME_BASELINE_OFFSET (it->f);
24106 }
24107 else if (EQ (face_name, Qt))
24108 {
24109 override = 0;
24110 }
24111 else
24112 {
24113 int face_id;
24114 struct face *face;
24115
24116 face_id = lookup_named_face (it->f, face_name, 0);
24117 if (face_id < 0)
24118 return make_number (-1);
24119
24120 face = FACE_FROM_ID (it->f, face_id);
24121 font = face->font;
24122 if (font == NULL)
24123 return make_number (-1);
24124 boff = font->baseline_offset;
24125 if (font->vertical_centering)
24126 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24127 }
24128
24129 ascent = FONT_BASE (font) + boff;
24130 descent = FONT_DESCENT (font) - boff;
24131
24132 if (override)
24133 {
24134 it->override_ascent = ascent;
24135 it->override_descent = descent;
24136 it->override_boff = boff;
24137 }
24138
24139 height = ascent + descent;
24140
24141 scale:
24142 if (FLOATP (val))
24143 height = (int)(XFLOAT_DATA (val) * height);
24144 else if (INTEGERP (val))
24145 height *= XINT (val);
24146
24147 return make_number (height);
24148 }
24149
24150
24151 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24152 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24153 and only if this is for a character for which no font was found.
24154
24155 If the display method (it->glyphless_method) is
24156 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24157 length of the acronym or the hexadecimal string, UPPER_XOFF and
24158 UPPER_YOFF are pixel offsets for the upper part of the string,
24159 LOWER_XOFF and LOWER_YOFF are for the lower part.
24160
24161 For the other display methods, LEN through LOWER_YOFF are zero. */
24162
24163 static void
24164 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24165 short upper_xoff, short upper_yoff,
24166 short lower_xoff, short lower_yoff)
24167 {
24168 struct glyph *glyph;
24169 enum glyph_row_area area = it->area;
24170
24171 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24172 if (glyph < it->glyph_row->glyphs[area + 1])
24173 {
24174 /* If the glyph row is reversed, we need to prepend the glyph
24175 rather than append it. */
24176 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24177 {
24178 struct glyph *g;
24179
24180 /* Make room for the additional glyph. */
24181 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24182 g[1] = *g;
24183 glyph = it->glyph_row->glyphs[area];
24184 }
24185 glyph->charpos = CHARPOS (it->position);
24186 glyph->object = it->object;
24187 glyph->pixel_width = it->pixel_width;
24188 glyph->ascent = it->ascent;
24189 glyph->descent = it->descent;
24190 glyph->voffset = it->voffset;
24191 glyph->type = GLYPHLESS_GLYPH;
24192 glyph->u.glyphless.method = it->glyphless_method;
24193 glyph->u.glyphless.for_no_font = for_no_font;
24194 glyph->u.glyphless.len = len;
24195 glyph->u.glyphless.ch = it->c;
24196 glyph->slice.glyphless.upper_xoff = upper_xoff;
24197 glyph->slice.glyphless.upper_yoff = upper_yoff;
24198 glyph->slice.glyphless.lower_xoff = lower_xoff;
24199 glyph->slice.glyphless.lower_yoff = lower_yoff;
24200 glyph->avoid_cursor_p = it->avoid_cursor_p;
24201 glyph->multibyte_p = it->multibyte_p;
24202 glyph->left_box_line_p = it->start_of_box_run_p;
24203 glyph->right_box_line_p = it->end_of_box_run_p;
24204 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24205 || it->phys_descent > it->descent);
24206 glyph->padding_p = 0;
24207 glyph->glyph_not_available_p = 0;
24208 glyph->face_id = face_id;
24209 glyph->font_type = FONT_TYPE_UNKNOWN;
24210 if (it->bidi_p)
24211 {
24212 glyph->resolved_level = it->bidi_it.resolved_level;
24213 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24214 abort ();
24215 glyph->bidi_type = it->bidi_it.type;
24216 }
24217 ++it->glyph_row->used[area];
24218 }
24219 else
24220 IT_EXPAND_MATRIX_WIDTH (it, area);
24221 }
24222
24223
24224 /* Produce a glyph for a glyphless character for iterator IT.
24225 IT->glyphless_method specifies which method to use for displaying
24226 the character. See the description of enum
24227 glyphless_display_method in dispextern.h for the detail.
24228
24229 FOR_NO_FONT is nonzero if and only if this is for a character for
24230 which no font was found. ACRONYM, if non-nil, is an acronym string
24231 for the character. */
24232
24233 static void
24234 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24235 {
24236 int face_id;
24237 struct face *face;
24238 struct font *font;
24239 int base_width, base_height, width, height;
24240 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24241 int len;
24242
24243 /* Get the metrics of the base font. We always refer to the current
24244 ASCII face. */
24245 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24246 font = face->font ? face->font : FRAME_FONT (it->f);
24247 it->ascent = FONT_BASE (font) + font->baseline_offset;
24248 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24249 base_height = it->ascent + it->descent;
24250 base_width = font->average_width;
24251
24252 /* Get a face ID for the glyph by utilizing a cache (the same way as
24253 done for `escape-glyph' in get_next_display_element). */
24254 if (it->f == last_glyphless_glyph_frame
24255 && it->face_id == last_glyphless_glyph_face_id)
24256 {
24257 face_id = last_glyphless_glyph_merged_face_id;
24258 }
24259 else
24260 {
24261 /* Merge the `glyphless-char' face into the current face. */
24262 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24263 last_glyphless_glyph_frame = it->f;
24264 last_glyphless_glyph_face_id = it->face_id;
24265 last_glyphless_glyph_merged_face_id = face_id;
24266 }
24267
24268 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24269 {
24270 it->pixel_width = THIN_SPACE_WIDTH;
24271 len = 0;
24272 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24273 }
24274 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24275 {
24276 width = CHAR_WIDTH (it->c);
24277 if (width == 0)
24278 width = 1;
24279 else if (width > 4)
24280 width = 4;
24281 it->pixel_width = base_width * width;
24282 len = 0;
24283 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24284 }
24285 else
24286 {
24287 char buf[7];
24288 const char *str;
24289 unsigned int code[6];
24290 int upper_len;
24291 int ascent, descent;
24292 struct font_metrics metrics_upper, metrics_lower;
24293
24294 face = FACE_FROM_ID (it->f, face_id);
24295 font = face->font ? face->font : FRAME_FONT (it->f);
24296 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24297
24298 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24299 {
24300 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24301 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24302 if (CONSP (acronym))
24303 acronym = XCAR (acronym);
24304 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24305 }
24306 else
24307 {
24308 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24309 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24310 str = buf;
24311 }
24312 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24313 code[len] = font->driver->encode_char (font, str[len]);
24314 upper_len = (len + 1) / 2;
24315 font->driver->text_extents (font, code, upper_len,
24316 &metrics_upper);
24317 font->driver->text_extents (font, code + upper_len, len - upper_len,
24318 &metrics_lower);
24319
24320
24321
24322 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24323 width = max (metrics_upper.width, metrics_lower.width) + 4;
24324 upper_xoff = upper_yoff = 2; /* the typical case */
24325 if (base_width >= width)
24326 {
24327 /* Align the upper to the left, the lower to the right. */
24328 it->pixel_width = base_width;
24329 lower_xoff = base_width - 2 - metrics_lower.width;
24330 }
24331 else
24332 {
24333 /* Center the shorter one. */
24334 it->pixel_width = width;
24335 if (metrics_upper.width >= metrics_lower.width)
24336 lower_xoff = (width - metrics_lower.width) / 2;
24337 else
24338 {
24339 /* FIXME: This code doesn't look right. It formerly was
24340 missing the "lower_xoff = 0;", which couldn't have
24341 been right since it left lower_xoff uninitialized. */
24342 lower_xoff = 0;
24343 upper_xoff = (width - metrics_upper.width) / 2;
24344 }
24345 }
24346
24347 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24348 top, bottom, and between upper and lower strings. */
24349 height = (metrics_upper.ascent + metrics_upper.descent
24350 + metrics_lower.ascent + metrics_lower.descent) + 5;
24351 /* Center vertically.
24352 H:base_height, D:base_descent
24353 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24354
24355 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24356 descent = D - H/2 + h/2;
24357 lower_yoff = descent - 2 - ld;
24358 upper_yoff = lower_yoff - la - 1 - ud; */
24359 ascent = - (it->descent - (base_height + height + 1) / 2);
24360 descent = it->descent - (base_height - height) / 2;
24361 lower_yoff = descent - 2 - metrics_lower.descent;
24362 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24363 - metrics_upper.descent);
24364 /* Don't make the height shorter than the base height. */
24365 if (height > base_height)
24366 {
24367 it->ascent = ascent;
24368 it->descent = descent;
24369 }
24370 }
24371
24372 it->phys_ascent = it->ascent;
24373 it->phys_descent = it->descent;
24374 if (it->glyph_row)
24375 append_glyphless_glyph (it, face_id, for_no_font, len,
24376 upper_xoff, upper_yoff,
24377 lower_xoff, lower_yoff);
24378 it->nglyphs = 1;
24379 take_vertical_position_into_account (it);
24380 }
24381
24382
24383 /* RIF:
24384 Produce glyphs/get display metrics for the display element IT is
24385 loaded with. See the description of struct it in dispextern.h
24386 for an overview of struct it. */
24387
24388 void
24389 x_produce_glyphs (struct it *it)
24390 {
24391 int extra_line_spacing = it->extra_line_spacing;
24392
24393 it->glyph_not_available_p = 0;
24394
24395 if (it->what == IT_CHARACTER)
24396 {
24397 XChar2b char2b;
24398 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24399 struct font *font = face->font;
24400 struct font_metrics *pcm = NULL;
24401 int boff; /* baseline offset */
24402
24403 if (font == NULL)
24404 {
24405 /* When no suitable font is found, display this character by
24406 the method specified in the first extra slot of
24407 Vglyphless_char_display. */
24408 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24409
24410 eassert (it->what == IT_GLYPHLESS);
24411 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24412 goto done;
24413 }
24414
24415 boff = font->baseline_offset;
24416 if (font->vertical_centering)
24417 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24418
24419 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24420 {
24421 int stretched_p;
24422
24423 it->nglyphs = 1;
24424
24425 if (it->override_ascent >= 0)
24426 {
24427 it->ascent = it->override_ascent;
24428 it->descent = it->override_descent;
24429 boff = it->override_boff;
24430 }
24431 else
24432 {
24433 it->ascent = FONT_BASE (font) + boff;
24434 it->descent = FONT_DESCENT (font) - boff;
24435 }
24436
24437 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24438 {
24439 pcm = get_per_char_metric (font, &char2b);
24440 if (pcm->width == 0
24441 && pcm->rbearing == 0 && pcm->lbearing == 0)
24442 pcm = NULL;
24443 }
24444
24445 if (pcm)
24446 {
24447 it->phys_ascent = pcm->ascent + boff;
24448 it->phys_descent = pcm->descent - boff;
24449 it->pixel_width = pcm->width;
24450 }
24451 else
24452 {
24453 it->glyph_not_available_p = 1;
24454 it->phys_ascent = it->ascent;
24455 it->phys_descent = it->descent;
24456 it->pixel_width = font->space_width;
24457 }
24458
24459 if (it->constrain_row_ascent_descent_p)
24460 {
24461 if (it->descent > it->max_descent)
24462 {
24463 it->ascent += it->descent - it->max_descent;
24464 it->descent = it->max_descent;
24465 }
24466 if (it->ascent > it->max_ascent)
24467 {
24468 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24469 it->ascent = it->max_ascent;
24470 }
24471 it->phys_ascent = min (it->phys_ascent, it->ascent);
24472 it->phys_descent = min (it->phys_descent, it->descent);
24473 extra_line_spacing = 0;
24474 }
24475
24476 /* If this is a space inside a region of text with
24477 `space-width' property, change its width. */
24478 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24479 if (stretched_p)
24480 it->pixel_width *= XFLOATINT (it->space_width);
24481
24482 /* If face has a box, add the box thickness to the character
24483 height. If character has a box line to the left and/or
24484 right, add the box line width to the character's width. */
24485 if (face->box != FACE_NO_BOX)
24486 {
24487 int thick = face->box_line_width;
24488
24489 if (thick > 0)
24490 {
24491 it->ascent += thick;
24492 it->descent += thick;
24493 }
24494 else
24495 thick = -thick;
24496
24497 if (it->start_of_box_run_p)
24498 it->pixel_width += thick;
24499 if (it->end_of_box_run_p)
24500 it->pixel_width += thick;
24501 }
24502
24503 /* If face has an overline, add the height of the overline
24504 (1 pixel) and a 1 pixel margin to the character height. */
24505 if (face->overline_p)
24506 it->ascent += overline_margin;
24507
24508 if (it->constrain_row_ascent_descent_p)
24509 {
24510 if (it->ascent > it->max_ascent)
24511 it->ascent = it->max_ascent;
24512 if (it->descent > it->max_descent)
24513 it->descent = it->max_descent;
24514 }
24515
24516 take_vertical_position_into_account (it);
24517
24518 /* If we have to actually produce glyphs, do it. */
24519 if (it->glyph_row)
24520 {
24521 if (stretched_p)
24522 {
24523 /* Translate a space with a `space-width' property
24524 into a stretch glyph. */
24525 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24526 / FONT_HEIGHT (font));
24527 append_stretch_glyph (it, it->object, it->pixel_width,
24528 it->ascent + it->descent, ascent);
24529 }
24530 else
24531 append_glyph (it);
24532
24533 /* If characters with lbearing or rbearing are displayed
24534 in this line, record that fact in a flag of the
24535 glyph row. This is used to optimize X output code. */
24536 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24537 it->glyph_row->contains_overlapping_glyphs_p = 1;
24538 }
24539 if (! stretched_p && it->pixel_width == 0)
24540 /* We assure that all visible glyphs have at least 1-pixel
24541 width. */
24542 it->pixel_width = 1;
24543 }
24544 else if (it->char_to_display == '\n')
24545 {
24546 /* A newline has no width, but we need the height of the
24547 line. But if previous part of the line sets a height,
24548 don't increase that height */
24549
24550 Lisp_Object height;
24551 Lisp_Object total_height = Qnil;
24552
24553 it->override_ascent = -1;
24554 it->pixel_width = 0;
24555 it->nglyphs = 0;
24556
24557 height = get_it_property (it, Qline_height);
24558 /* Split (line-height total-height) list */
24559 if (CONSP (height)
24560 && CONSP (XCDR (height))
24561 && NILP (XCDR (XCDR (height))))
24562 {
24563 total_height = XCAR (XCDR (height));
24564 height = XCAR (height);
24565 }
24566 height = calc_line_height_property (it, height, font, boff, 1);
24567
24568 if (it->override_ascent >= 0)
24569 {
24570 it->ascent = it->override_ascent;
24571 it->descent = it->override_descent;
24572 boff = it->override_boff;
24573 }
24574 else
24575 {
24576 it->ascent = FONT_BASE (font) + boff;
24577 it->descent = FONT_DESCENT (font) - boff;
24578 }
24579
24580 if (EQ (height, Qt))
24581 {
24582 if (it->descent > it->max_descent)
24583 {
24584 it->ascent += it->descent - it->max_descent;
24585 it->descent = it->max_descent;
24586 }
24587 if (it->ascent > it->max_ascent)
24588 {
24589 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24590 it->ascent = it->max_ascent;
24591 }
24592 it->phys_ascent = min (it->phys_ascent, it->ascent);
24593 it->phys_descent = min (it->phys_descent, it->descent);
24594 it->constrain_row_ascent_descent_p = 1;
24595 extra_line_spacing = 0;
24596 }
24597 else
24598 {
24599 Lisp_Object spacing;
24600
24601 it->phys_ascent = it->ascent;
24602 it->phys_descent = it->descent;
24603
24604 if ((it->max_ascent > 0 || it->max_descent > 0)
24605 && face->box != FACE_NO_BOX
24606 && face->box_line_width > 0)
24607 {
24608 it->ascent += face->box_line_width;
24609 it->descent += face->box_line_width;
24610 }
24611 if (!NILP (height)
24612 && XINT (height) > it->ascent + it->descent)
24613 it->ascent = XINT (height) - it->descent;
24614
24615 if (!NILP (total_height))
24616 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24617 else
24618 {
24619 spacing = get_it_property (it, Qline_spacing);
24620 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24621 }
24622 if (INTEGERP (spacing))
24623 {
24624 extra_line_spacing = XINT (spacing);
24625 if (!NILP (total_height))
24626 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24627 }
24628 }
24629 }
24630 else /* i.e. (it->char_to_display == '\t') */
24631 {
24632 if (font->space_width > 0)
24633 {
24634 int tab_width = it->tab_width * font->space_width;
24635 int x = it->current_x + it->continuation_lines_width;
24636 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24637
24638 /* If the distance from the current position to the next tab
24639 stop is less than a space character width, use the
24640 tab stop after that. */
24641 if (next_tab_x - x < font->space_width)
24642 next_tab_x += tab_width;
24643
24644 it->pixel_width = next_tab_x - x;
24645 it->nglyphs = 1;
24646 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24647 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24648
24649 if (it->glyph_row)
24650 {
24651 append_stretch_glyph (it, it->object, it->pixel_width,
24652 it->ascent + it->descent, it->ascent);
24653 }
24654 }
24655 else
24656 {
24657 it->pixel_width = 0;
24658 it->nglyphs = 1;
24659 }
24660 }
24661 }
24662 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24663 {
24664 /* A static composition.
24665
24666 Note: A composition is represented as one glyph in the
24667 glyph matrix. There are no padding glyphs.
24668
24669 Important note: pixel_width, ascent, and descent are the
24670 values of what is drawn by draw_glyphs (i.e. the values of
24671 the overall glyphs composed). */
24672 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24673 int boff; /* baseline offset */
24674 struct composition *cmp = composition_table[it->cmp_it.id];
24675 int glyph_len = cmp->glyph_len;
24676 struct font *font = face->font;
24677
24678 it->nglyphs = 1;
24679
24680 /* If we have not yet calculated pixel size data of glyphs of
24681 the composition for the current face font, calculate them
24682 now. Theoretically, we have to check all fonts for the
24683 glyphs, but that requires much time and memory space. So,
24684 here we check only the font of the first glyph. This may
24685 lead to incorrect display, but it's very rare, and C-l
24686 (recenter-top-bottom) can correct the display anyway. */
24687 if (! cmp->font || cmp->font != font)
24688 {
24689 /* Ascent and descent of the font of the first character
24690 of this composition (adjusted by baseline offset).
24691 Ascent and descent of overall glyphs should not be less
24692 than these, respectively. */
24693 int font_ascent, font_descent, font_height;
24694 /* Bounding box of the overall glyphs. */
24695 int leftmost, rightmost, lowest, highest;
24696 int lbearing, rbearing;
24697 int i, width, ascent, descent;
24698 int left_padded = 0, right_padded = 0;
24699 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24700 XChar2b char2b;
24701 struct font_metrics *pcm;
24702 int font_not_found_p;
24703 ptrdiff_t pos;
24704
24705 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24706 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24707 break;
24708 if (glyph_len < cmp->glyph_len)
24709 right_padded = 1;
24710 for (i = 0; i < glyph_len; i++)
24711 {
24712 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24713 break;
24714 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24715 }
24716 if (i > 0)
24717 left_padded = 1;
24718
24719 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24720 : IT_CHARPOS (*it));
24721 /* If no suitable font is found, use the default font. */
24722 font_not_found_p = font == NULL;
24723 if (font_not_found_p)
24724 {
24725 face = face->ascii_face;
24726 font = face->font;
24727 }
24728 boff = font->baseline_offset;
24729 if (font->vertical_centering)
24730 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24731 font_ascent = FONT_BASE (font) + boff;
24732 font_descent = FONT_DESCENT (font) - boff;
24733 font_height = FONT_HEIGHT (font);
24734
24735 cmp->font = (void *) font;
24736
24737 pcm = NULL;
24738 if (! font_not_found_p)
24739 {
24740 get_char_face_and_encoding (it->f, c, it->face_id,
24741 &char2b, 0);
24742 pcm = get_per_char_metric (font, &char2b);
24743 }
24744
24745 /* Initialize the bounding box. */
24746 if (pcm)
24747 {
24748 width = cmp->glyph_len > 0 ? pcm->width : 0;
24749 ascent = pcm->ascent;
24750 descent = pcm->descent;
24751 lbearing = pcm->lbearing;
24752 rbearing = pcm->rbearing;
24753 }
24754 else
24755 {
24756 width = cmp->glyph_len > 0 ? font->space_width : 0;
24757 ascent = FONT_BASE (font);
24758 descent = FONT_DESCENT (font);
24759 lbearing = 0;
24760 rbearing = width;
24761 }
24762
24763 rightmost = width;
24764 leftmost = 0;
24765 lowest = - descent + boff;
24766 highest = ascent + boff;
24767
24768 if (! font_not_found_p
24769 && font->default_ascent
24770 && CHAR_TABLE_P (Vuse_default_ascent)
24771 && !NILP (Faref (Vuse_default_ascent,
24772 make_number (it->char_to_display))))
24773 highest = font->default_ascent + boff;
24774
24775 /* Draw the first glyph at the normal position. It may be
24776 shifted to right later if some other glyphs are drawn
24777 at the left. */
24778 cmp->offsets[i * 2] = 0;
24779 cmp->offsets[i * 2 + 1] = boff;
24780 cmp->lbearing = lbearing;
24781 cmp->rbearing = rbearing;
24782
24783 /* Set cmp->offsets for the remaining glyphs. */
24784 for (i++; i < glyph_len; i++)
24785 {
24786 int left, right, btm, top;
24787 int ch = COMPOSITION_GLYPH (cmp, i);
24788 int face_id;
24789 struct face *this_face;
24790
24791 if (ch == '\t')
24792 ch = ' ';
24793 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24794 this_face = FACE_FROM_ID (it->f, face_id);
24795 font = this_face->font;
24796
24797 if (font == NULL)
24798 pcm = NULL;
24799 else
24800 {
24801 get_char_face_and_encoding (it->f, ch, face_id,
24802 &char2b, 0);
24803 pcm = get_per_char_metric (font, &char2b);
24804 }
24805 if (! pcm)
24806 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24807 else
24808 {
24809 width = pcm->width;
24810 ascent = pcm->ascent;
24811 descent = pcm->descent;
24812 lbearing = pcm->lbearing;
24813 rbearing = pcm->rbearing;
24814 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24815 {
24816 /* Relative composition with or without
24817 alternate chars. */
24818 left = (leftmost + rightmost - width) / 2;
24819 btm = - descent + boff;
24820 if (font->relative_compose
24821 && (! CHAR_TABLE_P (Vignore_relative_composition)
24822 || NILP (Faref (Vignore_relative_composition,
24823 make_number (ch)))))
24824 {
24825
24826 if (- descent >= font->relative_compose)
24827 /* One extra pixel between two glyphs. */
24828 btm = highest + 1;
24829 else if (ascent <= 0)
24830 /* One extra pixel between two glyphs. */
24831 btm = lowest - 1 - ascent - descent;
24832 }
24833 }
24834 else
24835 {
24836 /* A composition rule is specified by an integer
24837 value that encodes global and new reference
24838 points (GREF and NREF). GREF and NREF are
24839 specified by numbers as below:
24840
24841 0---1---2 -- ascent
24842 | |
24843 | |
24844 | |
24845 9--10--11 -- center
24846 | |
24847 ---3---4---5--- baseline
24848 | |
24849 6---7---8 -- descent
24850 */
24851 int rule = COMPOSITION_RULE (cmp, i);
24852 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24853
24854 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24855 grefx = gref % 3, nrefx = nref % 3;
24856 grefy = gref / 3, nrefy = nref / 3;
24857 if (xoff)
24858 xoff = font_height * (xoff - 128) / 256;
24859 if (yoff)
24860 yoff = font_height * (yoff - 128) / 256;
24861
24862 left = (leftmost
24863 + grefx * (rightmost - leftmost) / 2
24864 - nrefx * width / 2
24865 + xoff);
24866
24867 btm = ((grefy == 0 ? highest
24868 : grefy == 1 ? 0
24869 : grefy == 2 ? lowest
24870 : (highest + lowest) / 2)
24871 - (nrefy == 0 ? ascent + descent
24872 : nrefy == 1 ? descent - boff
24873 : nrefy == 2 ? 0
24874 : (ascent + descent) / 2)
24875 + yoff);
24876 }
24877
24878 cmp->offsets[i * 2] = left;
24879 cmp->offsets[i * 2 + 1] = btm + descent;
24880
24881 /* Update the bounding box of the overall glyphs. */
24882 if (width > 0)
24883 {
24884 right = left + width;
24885 if (left < leftmost)
24886 leftmost = left;
24887 if (right > rightmost)
24888 rightmost = right;
24889 }
24890 top = btm + descent + ascent;
24891 if (top > highest)
24892 highest = top;
24893 if (btm < lowest)
24894 lowest = btm;
24895
24896 if (cmp->lbearing > left + lbearing)
24897 cmp->lbearing = left + lbearing;
24898 if (cmp->rbearing < left + rbearing)
24899 cmp->rbearing = left + rbearing;
24900 }
24901 }
24902
24903 /* If there are glyphs whose x-offsets are negative,
24904 shift all glyphs to the right and make all x-offsets
24905 non-negative. */
24906 if (leftmost < 0)
24907 {
24908 for (i = 0; i < cmp->glyph_len; i++)
24909 cmp->offsets[i * 2] -= leftmost;
24910 rightmost -= leftmost;
24911 cmp->lbearing -= leftmost;
24912 cmp->rbearing -= leftmost;
24913 }
24914
24915 if (left_padded && cmp->lbearing < 0)
24916 {
24917 for (i = 0; i < cmp->glyph_len; i++)
24918 cmp->offsets[i * 2] -= cmp->lbearing;
24919 rightmost -= cmp->lbearing;
24920 cmp->rbearing -= cmp->lbearing;
24921 cmp->lbearing = 0;
24922 }
24923 if (right_padded && rightmost < cmp->rbearing)
24924 {
24925 rightmost = cmp->rbearing;
24926 }
24927
24928 cmp->pixel_width = rightmost;
24929 cmp->ascent = highest;
24930 cmp->descent = - lowest;
24931 if (cmp->ascent < font_ascent)
24932 cmp->ascent = font_ascent;
24933 if (cmp->descent < font_descent)
24934 cmp->descent = font_descent;
24935 }
24936
24937 if (it->glyph_row
24938 && (cmp->lbearing < 0
24939 || cmp->rbearing > cmp->pixel_width))
24940 it->glyph_row->contains_overlapping_glyphs_p = 1;
24941
24942 it->pixel_width = cmp->pixel_width;
24943 it->ascent = it->phys_ascent = cmp->ascent;
24944 it->descent = it->phys_descent = cmp->descent;
24945 if (face->box != FACE_NO_BOX)
24946 {
24947 int thick = face->box_line_width;
24948
24949 if (thick > 0)
24950 {
24951 it->ascent += thick;
24952 it->descent += thick;
24953 }
24954 else
24955 thick = - thick;
24956
24957 if (it->start_of_box_run_p)
24958 it->pixel_width += thick;
24959 if (it->end_of_box_run_p)
24960 it->pixel_width += thick;
24961 }
24962
24963 /* If face has an overline, add the height of the overline
24964 (1 pixel) and a 1 pixel margin to the character height. */
24965 if (face->overline_p)
24966 it->ascent += overline_margin;
24967
24968 take_vertical_position_into_account (it);
24969 if (it->ascent < 0)
24970 it->ascent = 0;
24971 if (it->descent < 0)
24972 it->descent = 0;
24973
24974 if (it->glyph_row && cmp->glyph_len > 0)
24975 append_composite_glyph (it);
24976 }
24977 else if (it->what == IT_COMPOSITION)
24978 {
24979 /* A dynamic (automatic) composition. */
24980 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24981 Lisp_Object gstring;
24982 struct font_metrics metrics;
24983
24984 it->nglyphs = 1;
24985
24986 gstring = composition_gstring_from_id (it->cmp_it.id);
24987 it->pixel_width
24988 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24989 &metrics);
24990 if (it->glyph_row
24991 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24992 it->glyph_row->contains_overlapping_glyphs_p = 1;
24993 it->ascent = it->phys_ascent = metrics.ascent;
24994 it->descent = it->phys_descent = metrics.descent;
24995 if (face->box != FACE_NO_BOX)
24996 {
24997 int thick = face->box_line_width;
24998
24999 if (thick > 0)
25000 {
25001 it->ascent += thick;
25002 it->descent += thick;
25003 }
25004 else
25005 thick = - thick;
25006
25007 if (it->start_of_box_run_p)
25008 it->pixel_width += thick;
25009 if (it->end_of_box_run_p)
25010 it->pixel_width += thick;
25011 }
25012 /* If face has an overline, add the height of the overline
25013 (1 pixel) and a 1 pixel margin to the character height. */
25014 if (face->overline_p)
25015 it->ascent += overline_margin;
25016 take_vertical_position_into_account (it);
25017 if (it->ascent < 0)
25018 it->ascent = 0;
25019 if (it->descent < 0)
25020 it->descent = 0;
25021
25022 if (it->glyph_row)
25023 append_composite_glyph (it);
25024 }
25025 else if (it->what == IT_GLYPHLESS)
25026 produce_glyphless_glyph (it, 0, Qnil);
25027 else if (it->what == IT_IMAGE)
25028 produce_image_glyph (it);
25029 else if (it->what == IT_STRETCH)
25030 produce_stretch_glyph (it);
25031
25032 done:
25033 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25034 because this isn't true for images with `:ascent 100'. */
25035 eassert (it->ascent >= 0 && it->descent >= 0);
25036 if (it->area == TEXT_AREA)
25037 it->current_x += it->pixel_width;
25038
25039 if (extra_line_spacing > 0)
25040 {
25041 it->descent += extra_line_spacing;
25042 if (extra_line_spacing > it->max_extra_line_spacing)
25043 it->max_extra_line_spacing = extra_line_spacing;
25044 }
25045
25046 it->max_ascent = max (it->max_ascent, it->ascent);
25047 it->max_descent = max (it->max_descent, it->descent);
25048 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25049 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25050 }
25051
25052 /* EXPORT for RIF:
25053 Output LEN glyphs starting at START at the nominal cursor position.
25054 Advance the nominal cursor over the text. The global variable
25055 updated_window contains the window being updated, updated_row is
25056 the glyph row being updated, and updated_area is the area of that
25057 row being updated. */
25058
25059 void
25060 x_write_glyphs (struct glyph *start, int len)
25061 {
25062 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25063
25064 eassert (updated_window && updated_row);
25065 /* When the window is hscrolled, cursor hpos can legitimately be out
25066 of bounds, but we draw the cursor at the corresponding window
25067 margin in that case. */
25068 if (!updated_row->reversed_p && chpos < 0)
25069 chpos = 0;
25070 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25071 chpos = updated_row->used[TEXT_AREA] - 1;
25072
25073 BLOCK_INPUT;
25074
25075 /* Write glyphs. */
25076
25077 hpos = start - updated_row->glyphs[updated_area];
25078 x = draw_glyphs (updated_window, output_cursor.x,
25079 updated_row, updated_area,
25080 hpos, hpos + len,
25081 DRAW_NORMAL_TEXT, 0);
25082
25083 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25084 if (updated_area == TEXT_AREA
25085 && updated_window->phys_cursor_on_p
25086 && updated_window->phys_cursor.vpos == output_cursor.vpos
25087 && chpos >= hpos
25088 && chpos < hpos + len)
25089 updated_window->phys_cursor_on_p = 0;
25090
25091 UNBLOCK_INPUT;
25092
25093 /* Advance the output cursor. */
25094 output_cursor.hpos += len;
25095 output_cursor.x = x;
25096 }
25097
25098
25099 /* EXPORT for RIF:
25100 Insert LEN glyphs from START at the nominal cursor position. */
25101
25102 void
25103 x_insert_glyphs (struct glyph *start, int len)
25104 {
25105 struct frame *f;
25106 struct window *w;
25107 int line_height, shift_by_width, shifted_region_width;
25108 struct glyph_row *row;
25109 struct glyph *glyph;
25110 int frame_x, frame_y;
25111 ptrdiff_t hpos;
25112
25113 eassert (updated_window && updated_row);
25114 BLOCK_INPUT;
25115 w = updated_window;
25116 f = XFRAME (WINDOW_FRAME (w));
25117
25118 /* Get the height of the line we are in. */
25119 row = updated_row;
25120 line_height = row->height;
25121
25122 /* Get the width of the glyphs to insert. */
25123 shift_by_width = 0;
25124 for (glyph = start; glyph < start + len; ++glyph)
25125 shift_by_width += glyph->pixel_width;
25126
25127 /* Get the width of the region to shift right. */
25128 shifted_region_width = (window_box_width (w, updated_area)
25129 - output_cursor.x
25130 - shift_by_width);
25131
25132 /* Shift right. */
25133 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25134 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25135
25136 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25137 line_height, shift_by_width);
25138
25139 /* Write the glyphs. */
25140 hpos = start - row->glyphs[updated_area];
25141 draw_glyphs (w, output_cursor.x, row, updated_area,
25142 hpos, hpos + len,
25143 DRAW_NORMAL_TEXT, 0);
25144
25145 /* Advance the output cursor. */
25146 output_cursor.hpos += len;
25147 output_cursor.x += shift_by_width;
25148 UNBLOCK_INPUT;
25149 }
25150
25151
25152 /* EXPORT for RIF:
25153 Erase the current text line from the nominal cursor position
25154 (inclusive) to pixel column TO_X (exclusive). The idea is that
25155 everything from TO_X onward is already erased.
25156
25157 TO_X is a pixel position relative to updated_area of
25158 updated_window. TO_X == -1 means clear to the end of this area. */
25159
25160 void
25161 x_clear_end_of_line (int to_x)
25162 {
25163 struct frame *f;
25164 struct window *w = updated_window;
25165 int max_x, min_y, max_y;
25166 int from_x, from_y, to_y;
25167
25168 eassert (updated_window && updated_row);
25169 f = XFRAME (w->frame);
25170
25171 if (updated_row->full_width_p)
25172 max_x = WINDOW_TOTAL_WIDTH (w);
25173 else
25174 max_x = window_box_width (w, updated_area);
25175 max_y = window_text_bottom_y (w);
25176
25177 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25178 of window. For TO_X > 0, truncate to end of drawing area. */
25179 if (to_x == 0)
25180 return;
25181 else if (to_x < 0)
25182 to_x = max_x;
25183 else
25184 to_x = min (to_x, max_x);
25185
25186 to_y = min (max_y, output_cursor.y + updated_row->height);
25187
25188 /* Notice if the cursor will be cleared by this operation. */
25189 if (!updated_row->full_width_p)
25190 notice_overwritten_cursor (w, updated_area,
25191 output_cursor.x, -1,
25192 updated_row->y,
25193 MATRIX_ROW_BOTTOM_Y (updated_row));
25194
25195 from_x = output_cursor.x;
25196
25197 /* Translate to frame coordinates. */
25198 if (updated_row->full_width_p)
25199 {
25200 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25201 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25202 }
25203 else
25204 {
25205 int area_left = window_box_left (w, updated_area);
25206 from_x += area_left;
25207 to_x += area_left;
25208 }
25209
25210 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25211 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25212 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25213
25214 /* Prevent inadvertently clearing to end of the X window. */
25215 if (to_x > from_x && to_y > from_y)
25216 {
25217 BLOCK_INPUT;
25218 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25219 to_x - from_x, to_y - from_y);
25220 UNBLOCK_INPUT;
25221 }
25222 }
25223
25224 #endif /* HAVE_WINDOW_SYSTEM */
25225
25226
25227 \f
25228 /***********************************************************************
25229 Cursor types
25230 ***********************************************************************/
25231
25232 /* Value is the internal representation of the specified cursor type
25233 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25234 of the bar cursor. */
25235
25236 static enum text_cursor_kinds
25237 get_specified_cursor_type (Lisp_Object arg, int *width)
25238 {
25239 enum text_cursor_kinds type;
25240
25241 if (NILP (arg))
25242 return NO_CURSOR;
25243
25244 if (EQ (arg, Qbox))
25245 return FILLED_BOX_CURSOR;
25246
25247 if (EQ (arg, Qhollow))
25248 return HOLLOW_BOX_CURSOR;
25249
25250 if (EQ (arg, Qbar))
25251 {
25252 *width = 2;
25253 return BAR_CURSOR;
25254 }
25255
25256 if (CONSP (arg)
25257 && EQ (XCAR (arg), Qbar)
25258 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25259 {
25260 *width = XINT (XCDR (arg));
25261 return BAR_CURSOR;
25262 }
25263
25264 if (EQ (arg, Qhbar))
25265 {
25266 *width = 2;
25267 return HBAR_CURSOR;
25268 }
25269
25270 if (CONSP (arg)
25271 && EQ (XCAR (arg), Qhbar)
25272 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25273 {
25274 *width = XINT (XCDR (arg));
25275 return HBAR_CURSOR;
25276 }
25277
25278 /* Treat anything unknown as "hollow box cursor".
25279 It was bad to signal an error; people have trouble fixing
25280 .Xdefaults with Emacs, when it has something bad in it. */
25281 type = HOLLOW_BOX_CURSOR;
25282
25283 return type;
25284 }
25285
25286 /* Set the default cursor types for specified frame. */
25287 void
25288 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25289 {
25290 int width = 1;
25291 Lisp_Object tem;
25292
25293 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25294 FRAME_CURSOR_WIDTH (f) = width;
25295
25296 /* By default, set up the blink-off state depending on the on-state. */
25297
25298 tem = Fassoc (arg, Vblink_cursor_alist);
25299 if (!NILP (tem))
25300 {
25301 FRAME_BLINK_OFF_CURSOR (f)
25302 = get_specified_cursor_type (XCDR (tem), &width);
25303 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25304 }
25305 else
25306 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25307 }
25308
25309
25310 #ifdef HAVE_WINDOW_SYSTEM
25311
25312 /* Return the cursor we want to be displayed in window W. Return
25313 width of bar/hbar cursor through WIDTH arg. Return with
25314 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25315 (i.e. if the `system caret' should track this cursor).
25316
25317 In a mini-buffer window, we want the cursor only to appear if we
25318 are reading input from this window. For the selected window, we
25319 want the cursor type given by the frame parameter or buffer local
25320 setting of cursor-type. If explicitly marked off, draw no cursor.
25321 In all other cases, we want a hollow box cursor. */
25322
25323 static enum text_cursor_kinds
25324 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25325 int *active_cursor)
25326 {
25327 struct frame *f = XFRAME (w->frame);
25328 struct buffer *b = XBUFFER (w->buffer);
25329 int cursor_type = DEFAULT_CURSOR;
25330 Lisp_Object alt_cursor;
25331 int non_selected = 0;
25332
25333 *active_cursor = 1;
25334
25335 /* Echo area */
25336 if (cursor_in_echo_area
25337 && FRAME_HAS_MINIBUF_P (f)
25338 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25339 {
25340 if (w == XWINDOW (echo_area_window))
25341 {
25342 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25343 {
25344 *width = FRAME_CURSOR_WIDTH (f);
25345 return FRAME_DESIRED_CURSOR (f);
25346 }
25347 else
25348 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25349 }
25350
25351 *active_cursor = 0;
25352 non_selected = 1;
25353 }
25354
25355 /* Detect a nonselected window or nonselected frame. */
25356 else if (w != XWINDOW (f->selected_window)
25357 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25358 {
25359 *active_cursor = 0;
25360
25361 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25362 return NO_CURSOR;
25363
25364 non_selected = 1;
25365 }
25366
25367 /* Never display a cursor in a window in which cursor-type is nil. */
25368 if (NILP (BVAR (b, cursor_type)))
25369 return NO_CURSOR;
25370
25371 /* Get the normal cursor type for this window. */
25372 if (EQ (BVAR (b, cursor_type), Qt))
25373 {
25374 cursor_type = FRAME_DESIRED_CURSOR (f);
25375 *width = FRAME_CURSOR_WIDTH (f);
25376 }
25377 else
25378 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25379
25380 /* Use cursor-in-non-selected-windows instead
25381 for non-selected window or frame. */
25382 if (non_selected)
25383 {
25384 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25385 if (!EQ (Qt, alt_cursor))
25386 return get_specified_cursor_type (alt_cursor, width);
25387 /* t means modify the normal cursor type. */
25388 if (cursor_type == FILLED_BOX_CURSOR)
25389 cursor_type = HOLLOW_BOX_CURSOR;
25390 else if (cursor_type == BAR_CURSOR && *width > 1)
25391 --*width;
25392 return cursor_type;
25393 }
25394
25395 /* Use normal cursor if not blinked off. */
25396 if (!w->cursor_off_p)
25397 {
25398 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25399 {
25400 if (cursor_type == FILLED_BOX_CURSOR)
25401 {
25402 /* Using a block cursor on large images can be very annoying.
25403 So use a hollow cursor for "large" images.
25404 If image is not transparent (no mask), also use hollow cursor. */
25405 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25406 if (img != NULL && IMAGEP (img->spec))
25407 {
25408 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25409 where N = size of default frame font size.
25410 This should cover most of the "tiny" icons people may use. */
25411 if (!img->mask
25412 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25413 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25414 cursor_type = HOLLOW_BOX_CURSOR;
25415 }
25416 }
25417 else if (cursor_type != NO_CURSOR)
25418 {
25419 /* Display current only supports BOX and HOLLOW cursors for images.
25420 So for now, unconditionally use a HOLLOW cursor when cursor is
25421 not a solid box cursor. */
25422 cursor_type = HOLLOW_BOX_CURSOR;
25423 }
25424 }
25425 return cursor_type;
25426 }
25427
25428 /* Cursor is blinked off, so determine how to "toggle" it. */
25429
25430 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25431 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25432 return get_specified_cursor_type (XCDR (alt_cursor), width);
25433
25434 /* Then see if frame has specified a specific blink off cursor type. */
25435 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25436 {
25437 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25438 return FRAME_BLINK_OFF_CURSOR (f);
25439 }
25440
25441 #if 0
25442 /* Some people liked having a permanently visible blinking cursor,
25443 while others had very strong opinions against it. So it was
25444 decided to remove it. KFS 2003-09-03 */
25445
25446 /* Finally perform built-in cursor blinking:
25447 filled box <-> hollow box
25448 wide [h]bar <-> narrow [h]bar
25449 narrow [h]bar <-> no cursor
25450 other type <-> no cursor */
25451
25452 if (cursor_type == FILLED_BOX_CURSOR)
25453 return HOLLOW_BOX_CURSOR;
25454
25455 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25456 {
25457 *width = 1;
25458 return cursor_type;
25459 }
25460 #endif
25461
25462 return NO_CURSOR;
25463 }
25464
25465
25466 /* Notice when the text cursor of window W has been completely
25467 overwritten by a drawing operation that outputs glyphs in AREA
25468 starting at X0 and ending at X1 in the line starting at Y0 and
25469 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25470 the rest of the line after X0 has been written. Y coordinates
25471 are window-relative. */
25472
25473 static void
25474 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25475 int x0, int x1, int y0, int y1)
25476 {
25477 int cx0, cx1, cy0, cy1;
25478 struct glyph_row *row;
25479
25480 if (!w->phys_cursor_on_p)
25481 return;
25482 if (area != TEXT_AREA)
25483 return;
25484
25485 if (w->phys_cursor.vpos < 0
25486 || w->phys_cursor.vpos >= w->current_matrix->nrows
25487 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25488 !(row->enabled_p && row->displays_text_p)))
25489 return;
25490
25491 if (row->cursor_in_fringe_p)
25492 {
25493 row->cursor_in_fringe_p = 0;
25494 draw_fringe_bitmap (w, row, row->reversed_p);
25495 w->phys_cursor_on_p = 0;
25496 return;
25497 }
25498
25499 cx0 = w->phys_cursor.x;
25500 cx1 = cx0 + w->phys_cursor_width;
25501 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25502 return;
25503
25504 /* The cursor image will be completely removed from the
25505 screen if the output area intersects the cursor area in
25506 y-direction. When we draw in [y0 y1[, and some part of
25507 the cursor is at y < y0, that part must have been drawn
25508 before. When scrolling, the cursor is erased before
25509 actually scrolling, so we don't come here. When not
25510 scrolling, the rows above the old cursor row must have
25511 changed, and in this case these rows must have written
25512 over the cursor image.
25513
25514 Likewise if part of the cursor is below y1, with the
25515 exception of the cursor being in the first blank row at
25516 the buffer and window end because update_text_area
25517 doesn't draw that row. (Except when it does, but
25518 that's handled in update_text_area.) */
25519
25520 cy0 = w->phys_cursor.y;
25521 cy1 = cy0 + w->phys_cursor_height;
25522 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25523 return;
25524
25525 w->phys_cursor_on_p = 0;
25526 }
25527
25528 #endif /* HAVE_WINDOW_SYSTEM */
25529
25530 \f
25531 /************************************************************************
25532 Mouse Face
25533 ************************************************************************/
25534
25535 #ifdef HAVE_WINDOW_SYSTEM
25536
25537 /* EXPORT for RIF:
25538 Fix the display of area AREA of overlapping row ROW in window W
25539 with respect to the overlapping part OVERLAPS. */
25540
25541 void
25542 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25543 enum glyph_row_area area, int overlaps)
25544 {
25545 int i, x;
25546
25547 BLOCK_INPUT;
25548
25549 x = 0;
25550 for (i = 0; i < row->used[area];)
25551 {
25552 if (row->glyphs[area][i].overlaps_vertically_p)
25553 {
25554 int start = i, start_x = x;
25555
25556 do
25557 {
25558 x += row->glyphs[area][i].pixel_width;
25559 ++i;
25560 }
25561 while (i < row->used[area]
25562 && row->glyphs[area][i].overlaps_vertically_p);
25563
25564 draw_glyphs (w, start_x, row, area,
25565 start, i,
25566 DRAW_NORMAL_TEXT, overlaps);
25567 }
25568 else
25569 {
25570 x += row->glyphs[area][i].pixel_width;
25571 ++i;
25572 }
25573 }
25574
25575 UNBLOCK_INPUT;
25576 }
25577
25578
25579 /* EXPORT:
25580 Draw the cursor glyph of window W in glyph row ROW. See the
25581 comment of draw_glyphs for the meaning of HL. */
25582
25583 void
25584 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25585 enum draw_glyphs_face hl)
25586 {
25587 /* If cursor hpos is out of bounds, don't draw garbage. This can
25588 happen in mini-buffer windows when switching between echo area
25589 glyphs and mini-buffer. */
25590 if ((row->reversed_p
25591 ? (w->phys_cursor.hpos >= 0)
25592 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25593 {
25594 int on_p = w->phys_cursor_on_p;
25595 int x1;
25596 int hpos = w->phys_cursor.hpos;
25597
25598 /* When the window is hscrolled, cursor hpos can legitimately be
25599 out of bounds, but we draw the cursor at the corresponding
25600 window margin in that case. */
25601 if (!row->reversed_p && hpos < 0)
25602 hpos = 0;
25603 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25604 hpos = row->used[TEXT_AREA] - 1;
25605
25606 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25607 hl, 0);
25608 w->phys_cursor_on_p = on_p;
25609
25610 if (hl == DRAW_CURSOR)
25611 w->phys_cursor_width = x1 - w->phys_cursor.x;
25612 /* When we erase the cursor, and ROW is overlapped by other
25613 rows, make sure that these overlapping parts of other rows
25614 are redrawn. */
25615 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25616 {
25617 w->phys_cursor_width = x1 - w->phys_cursor.x;
25618
25619 if (row > w->current_matrix->rows
25620 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25621 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25622 OVERLAPS_ERASED_CURSOR);
25623
25624 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25625 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25626 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25627 OVERLAPS_ERASED_CURSOR);
25628 }
25629 }
25630 }
25631
25632
25633 /* EXPORT:
25634 Erase the image of a cursor of window W from the screen. */
25635
25636 void
25637 erase_phys_cursor (struct window *w)
25638 {
25639 struct frame *f = XFRAME (w->frame);
25640 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25641 int hpos = w->phys_cursor.hpos;
25642 int vpos = w->phys_cursor.vpos;
25643 int mouse_face_here_p = 0;
25644 struct glyph_matrix *active_glyphs = w->current_matrix;
25645 struct glyph_row *cursor_row;
25646 struct glyph *cursor_glyph;
25647 enum draw_glyphs_face hl;
25648
25649 /* No cursor displayed or row invalidated => nothing to do on the
25650 screen. */
25651 if (w->phys_cursor_type == NO_CURSOR)
25652 goto mark_cursor_off;
25653
25654 /* VPOS >= active_glyphs->nrows means that window has been resized.
25655 Don't bother to erase the cursor. */
25656 if (vpos >= active_glyphs->nrows)
25657 goto mark_cursor_off;
25658
25659 /* If row containing cursor is marked invalid, there is nothing we
25660 can do. */
25661 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25662 if (!cursor_row->enabled_p)
25663 goto mark_cursor_off;
25664
25665 /* If line spacing is > 0, old cursor may only be partially visible in
25666 window after split-window. So adjust visible height. */
25667 cursor_row->visible_height = min (cursor_row->visible_height,
25668 window_text_bottom_y (w) - cursor_row->y);
25669
25670 /* If row is completely invisible, don't attempt to delete a cursor which
25671 isn't there. This can happen if cursor is at top of a window, and
25672 we switch to a buffer with a header line in that window. */
25673 if (cursor_row->visible_height <= 0)
25674 goto mark_cursor_off;
25675
25676 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25677 if (cursor_row->cursor_in_fringe_p)
25678 {
25679 cursor_row->cursor_in_fringe_p = 0;
25680 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25681 goto mark_cursor_off;
25682 }
25683
25684 /* This can happen when the new row is shorter than the old one.
25685 In this case, either draw_glyphs or clear_end_of_line
25686 should have cleared the cursor. Note that we wouldn't be
25687 able to erase the cursor in this case because we don't have a
25688 cursor glyph at hand. */
25689 if ((cursor_row->reversed_p
25690 ? (w->phys_cursor.hpos < 0)
25691 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25692 goto mark_cursor_off;
25693
25694 /* When the window is hscrolled, cursor hpos can legitimately be out
25695 of bounds, but we draw the cursor at the corresponding window
25696 margin in that case. */
25697 if (!cursor_row->reversed_p && hpos < 0)
25698 hpos = 0;
25699 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25700 hpos = cursor_row->used[TEXT_AREA] - 1;
25701
25702 /* If the cursor is in the mouse face area, redisplay that when
25703 we clear the cursor. */
25704 if (! NILP (hlinfo->mouse_face_window)
25705 && coords_in_mouse_face_p (w, hpos, vpos)
25706 /* Don't redraw the cursor's spot in mouse face if it is at the
25707 end of a line (on a newline). The cursor appears there, but
25708 mouse highlighting does not. */
25709 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25710 mouse_face_here_p = 1;
25711
25712 /* Maybe clear the display under the cursor. */
25713 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25714 {
25715 int x, y, left_x;
25716 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25717 int width;
25718
25719 cursor_glyph = get_phys_cursor_glyph (w);
25720 if (cursor_glyph == NULL)
25721 goto mark_cursor_off;
25722
25723 width = cursor_glyph->pixel_width;
25724 left_x = window_box_left_offset (w, TEXT_AREA);
25725 x = w->phys_cursor.x;
25726 if (x < left_x)
25727 width -= left_x - x;
25728 width = min (width, window_box_width (w, TEXT_AREA) - x);
25729 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25730 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25731
25732 if (width > 0)
25733 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25734 }
25735
25736 /* Erase the cursor by redrawing the character underneath it. */
25737 if (mouse_face_here_p)
25738 hl = DRAW_MOUSE_FACE;
25739 else
25740 hl = DRAW_NORMAL_TEXT;
25741 draw_phys_cursor_glyph (w, cursor_row, hl);
25742
25743 mark_cursor_off:
25744 w->phys_cursor_on_p = 0;
25745 w->phys_cursor_type = NO_CURSOR;
25746 }
25747
25748
25749 /* EXPORT:
25750 Display or clear cursor of window W. If ON is zero, clear the
25751 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25752 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25753
25754 void
25755 display_and_set_cursor (struct window *w, int on,
25756 int hpos, int vpos, int x, int y)
25757 {
25758 struct frame *f = XFRAME (w->frame);
25759 int new_cursor_type;
25760 int new_cursor_width;
25761 int active_cursor;
25762 struct glyph_row *glyph_row;
25763 struct glyph *glyph;
25764
25765 /* This is pointless on invisible frames, and dangerous on garbaged
25766 windows and frames; in the latter case, the frame or window may
25767 be in the midst of changing its size, and x and y may be off the
25768 window. */
25769 if (! FRAME_VISIBLE_P (f)
25770 || FRAME_GARBAGED_P (f)
25771 || vpos >= w->current_matrix->nrows
25772 || hpos >= w->current_matrix->matrix_w)
25773 return;
25774
25775 /* If cursor is off and we want it off, return quickly. */
25776 if (!on && !w->phys_cursor_on_p)
25777 return;
25778
25779 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25780 /* If cursor row is not enabled, we don't really know where to
25781 display the cursor. */
25782 if (!glyph_row->enabled_p)
25783 {
25784 w->phys_cursor_on_p = 0;
25785 return;
25786 }
25787
25788 glyph = NULL;
25789 if (!glyph_row->exact_window_width_line_p
25790 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25791 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25792
25793 eassert (interrupt_input_blocked);
25794
25795 /* Set new_cursor_type to the cursor we want to be displayed. */
25796 new_cursor_type = get_window_cursor_type (w, glyph,
25797 &new_cursor_width, &active_cursor);
25798
25799 /* If cursor is currently being shown and we don't want it to be or
25800 it is in the wrong place, or the cursor type is not what we want,
25801 erase it. */
25802 if (w->phys_cursor_on_p
25803 && (!on
25804 || w->phys_cursor.x != x
25805 || w->phys_cursor.y != y
25806 || new_cursor_type != w->phys_cursor_type
25807 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25808 && new_cursor_width != w->phys_cursor_width)))
25809 erase_phys_cursor (w);
25810
25811 /* Don't check phys_cursor_on_p here because that flag is only set
25812 to zero in some cases where we know that the cursor has been
25813 completely erased, to avoid the extra work of erasing the cursor
25814 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25815 still not be visible, or it has only been partly erased. */
25816 if (on)
25817 {
25818 w->phys_cursor_ascent = glyph_row->ascent;
25819 w->phys_cursor_height = glyph_row->height;
25820
25821 /* Set phys_cursor_.* before x_draw_.* is called because some
25822 of them may need the information. */
25823 w->phys_cursor.x = x;
25824 w->phys_cursor.y = glyph_row->y;
25825 w->phys_cursor.hpos = hpos;
25826 w->phys_cursor.vpos = vpos;
25827 }
25828
25829 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25830 new_cursor_type, new_cursor_width,
25831 on, active_cursor);
25832 }
25833
25834
25835 /* Switch the display of W's cursor on or off, according to the value
25836 of ON. */
25837
25838 static void
25839 update_window_cursor (struct window *w, int on)
25840 {
25841 /* Don't update cursor in windows whose frame is in the process
25842 of being deleted. */
25843 if (w->current_matrix)
25844 {
25845 int hpos = w->phys_cursor.hpos;
25846 int vpos = w->phys_cursor.vpos;
25847 struct glyph_row *row;
25848
25849 if (vpos >= w->current_matrix->nrows
25850 || hpos >= w->current_matrix->matrix_w)
25851 return;
25852
25853 row = MATRIX_ROW (w->current_matrix, vpos);
25854
25855 /* When the window is hscrolled, cursor hpos can legitimately be
25856 out of bounds, but we draw the cursor at the corresponding
25857 window margin in that case. */
25858 if (!row->reversed_p && hpos < 0)
25859 hpos = 0;
25860 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25861 hpos = row->used[TEXT_AREA] - 1;
25862
25863 BLOCK_INPUT;
25864 display_and_set_cursor (w, on, hpos, vpos,
25865 w->phys_cursor.x, w->phys_cursor.y);
25866 UNBLOCK_INPUT;
25867 }
25868 }
25869
25870
25871 /* Call update_window_cursor with parameter ON_P on all leaf windows
25872 in the window tree rooted at W. */
25873
25874 static void
25875 update_cursor_in_window_tree (struct window *w, int on_p)
25876 {
25877 while (w)
25878 {
25879 if (!NILP (w->hchild))
25880 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25881 else if (!NILP (w->vchild))
25882 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25883 else
25884 update_window_cursor (w, on_p);
25885
25886 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25887 }
25888 }
25889
25890
25891 /* EXPORT:
25892 Display the cursor on window W, or clear it, according to ON_P.
25893 Don't change the cursor's position. */
25894
25895 void
25896 x_update_cursor (struct frame *f, int on_p)
25897 {
25898 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25899 }
25900
25901
25902 /* EXPORT:
25903 Clear the cursor of window W to background color, and mark the
25904 cursor as not shown. This is used when the text where the cursor
25905 is about to be rewritten. */
25906
25907 void
25908 x_clear_cursor (struct window *w)
25909 {
25910 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25911 update_window_cursor (w, 0);
25912 }
25913
25914 #endif /* HAVE_WINDOW_SYSTEM */
25915
25916 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25917 and MSDOS. */
25918 static void
25919 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25920 int start_hpos, int end_hpos,
25921 enum draw_glyphs_face draw)
25922 {
25923 #ifdef HAVE_WINDOW_SYSTEM
25924 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25925 {
25926 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25927 return;
25928 }
25929 #endif
25930 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
25931 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25932 #endif
25933 }
25934
25935 /* Display the active region described by mouse_face_* according to DRAW. */
25936
25937 static void
25938 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25939 {
25940 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25941 struct frame *f = XFRAME (WINDOW_FRAME (w));
25942
25943 if (/* If window is in the process of being destroyed, don't bother
25944 to do anything. */
25945 w->current_matrix != NULL
25946 /* Don't update mouse highlight if hidden */
25947 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25948 /* Recognize when we are called to operate on rows that don't exist
25949 anymore. This can happen when a window is split. */
25950 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25951 {
25952 int phys_cursor_on_p = w->phys_cursor_on_p;
25953 struct glyph_row *row, *first, *last;
25954
25955 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25956 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25957
25958 for (row = first; row <= last && row->enabled_p; ++row)
25959 {
25960 int start_hpos, end_hpos, start_x;
25961
25962 /* For all but the first row, the highlight starts at column 0. */
25963 if (row == first)
25964 {
25965 /* R2L rows have BEG and END in reversed order, but the
25966 screen drawing geometry is always left to right. So
25967 we need to mirror the beginning and end of the
25968 highlighted area in R2L rows. */
25969 if (!row->reversed_p)
25970 {
25971 start_hpos = hlinfo->mouse_face_beg_col;
25972 start_x = hlinfo->mouse_face_beg_x;
25973 }
25974 else if (row == last)
25975 {
25976 start_hpos = hlinfo->mouse_face_end_col;
25977 start_x = hlinfo->mouse_face_end_x;
25978 }
25979 else
25980 {
25981 start_hpos = 0;
25982 start_x = 0;
25983 }
25984 }
25985 else if (row->reversed_p && row == last)
25986 {
25987 start_hpos = hlinfo->mouse_face_end_col;
25988 start_x = hlinfo->mouse_face_end_x;
25989 }
25990 else
25991 {
25992 start_hpos = 0;
25993 start_x = 0;
25994 }
25995
25996 if (row == last)
25997 {
25998 if (!row->reversed_p)
25999 end_hpos = hlinfo->mouse_face_end_col;
26000 else if (row == first)
26001 end_hpos = hlinfo->mouse_face_beg_col;
26002 else
26003 {
26004 end_hpos = row->used[TEXT_AREA];
26005 if (draw == DRAW_NORMAL_TEXT)
26006 row->fill_line_p = 1; /* Clear to end of line */
26007 }
26008 }
26009 else if (row->reversed_p && row == first)
26010 end_hpos = hlinfo->mouse_face_beg_col;
26011 else
26012 {
26013 end_hpos = row->used[TEXT_AREA];
26014 if (draw == DRAW_NORMAL_TEXT)
26015 row->fill_line_p = 1; /* Clear to end of line */
26016 }
26017
26018 if (end_hpos > start_hpos)
26019 {
26020 draw_row_with_mouse_face (w, start_x, row,
26021 start_hpos, end_hpos, draw);
26022
26023 row->mouse_face_p
26024 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26025 }
26026 }
26027
26028 #ifdef HAVE_WINDOW_SYSTEM
26029 /* When we've written over the cursor, arrange for it to
26030 be displayed again. */
26031 if (FRAME_WINDOW_P (f)
26032 && phys_cursor_on_p && !w->phys_cursor_on_p)
26033 {
26034 int hpos = w->phys_cursor.hpos;
26035
26036 /* When the window is hscrolled, cursor hpos can legitimately be
26037 out of bounds, but we draw the cursor at the corresponding
26038 window margin in that case. */
26039 if (!row->reversed_p && hpos < 0)
26040 hpos = 0;
26041 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26042 hpos = row->used[TEXT_AREA] - 1;
26043
26044 BLOCK_INPUT;
26045 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26046 w->phys_cursor.x, w->phys_cursor.y);
26047 UNBLOCK_INPUT;
26048 }
26049 #endif /* HAVE_WINDOW_SYSTEM */
26050 }
26051
26052 #ifdef HAVE_WINDOW_SYSTEM
26053 /* Change the mouse cursor. */
26054 if (FRAME_WINDOW_P (f))
26055 {
26056 if (draw == DRAW_NORMAL_TEXT
26057 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26058 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26059 else if (draw == DRAW_MOUSE_FACE)
26060 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26061 else
26062 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26063 }
26064 #endif /* HAVE_WINDOW_SYSTEM */
26065 }
26066
26067 /* EXPORT:
26068 Clear out the mouse-highlighted active region.
26069 Redraw it un-highlighted first. Value is non-zero if mouse
26070 face was actually drawn unhighlighted. */
26071
26072 int
26073 clear_mouse_face (Mouse_HLInfo *hlinfo)
26074 {
26075 int cleared = 0;
26076
26077 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26078 {
26079 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26080 cleared = 1;
26081 }
26082
26083 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26084 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26085 hlinfo->mouse_face_window = Qnil;
26086 hlinfo->mouse_face_overlay = Qnil;
26087 return cleared;
26088 }
26089
26090 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26091 within the mouse face on that window. */
26092 static int
26093 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26094 {
26095 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26096
26097 /* Quickly resolve the easy cases. */
26098 if (!(WINDOWP (hlinfo->mouse_face_window)
26099 && XWINDOW (hlinfo->mouse_face_window) == w))
26100 return 0;
26101 if (vpos < hlinfo->mouse_face_beg_row
26102 || vpos > hlinfo->mouse_face_end_row)
26103 return 0;
26104 if (vpos > hlinfo->mouse_face_beg_row
26105 && vpos < hlinfo->mouse_face_end_row)
26106 return 1;
26107
26108 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26109 {
26110 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26111 {
26112 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26113 return 1;
26114 }
26115 else if ((vpos == hlinfo->mouse_face_beg_row
26116 && hpos >= hlinfo->mouse_face_beg_col)
26117 || (vpos == hlinfo->mouse_face_end_row
26118 && hpos < hlinfo->mouse_face_end_col))
26119 return 1;
26120 }
26121 else
26122 {
26123 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26124 {
26125 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26126 return 1;
26127 }
26128 else if ((vpos == hlinfo->mouse_face_beg_row
26129 && hpos <= hlinfo->mouse_face_beg_col)
26130 || (vpos == hlinfo->mouse_face_end_row
26131 && hpos > hlinfo->mouse_face_end_col))
26132 return 1;
26133 }
26134 return 0;
26135 }
26136
26137
26138 /* EXPORT:
26139 Non-zero if physical cursor of window W is within mouse face. */
26140
26141 int
26142 cursor_in_mouse_face_p (struct window *w)
26143 {
26144 int hpos = w->phys_cursor.hpos;
26145 int vpos = w->phys_cursor.vpos;
26146 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26147
26148 /* When the window is hscrolled, cursor hpos can legitimately be out
26149 of bounds, but we draw the cursor at the corresponding window
26150 margin in that case. */
26151 if (!row->reversed_p && hpos < 0)
26152 hpos = 0;
26153 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26154 hpos = row->used[TEXT_AREA] - 1;
26155
26156 return coords_in_mouse_face_p (w, hpos, vpos);
26157 }
26158
26159
26160 \f
26161 /* Find the glyph rows START_ROW and END_ROW of window W that display
26162 characters between buffer positions START_CHARPOS and END_CHARPOS
26163 (excluding END_CHARPOS). DISP_STRING is a display string that
26164 covers these buffer positions. This is similar to
26165 row_containing_pos, but is more accurate when bidi reordering makes
26166 buffer positions change non-linearly with glyph rows. */
26167 static void
26168 rows_from_pos_range (struct window *w,
26169 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26170 Lisp_Object disp_string,
26171 struct glyph_row **start, struct glyph_row **end)
26172 {
26173 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26174 int last_y = window_text_bottom_y (w);
26175 struct glyph_row *row;
26176
26177 *start = NULL;
26178 *end = NULL;
26179
26180 while (!first->enabled_p
26181 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26182 first++;
26183
26184 /* Find the START row. */
26185 for (row = first;
26186 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26187 row++)
26188 {
26189 /* A row can potentially be the START row if the range of the
26190 characters it displays intersects the range
26191 [START_CHARPOS..END_CHARPOS). */
26192 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26193 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26194 /* See the commentary in row_containing_pos, for the
26195 explanation of the complicated way to check whether
26196 some position is beyond the end of the characters
26197 displayed by a row. */
26198 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26199 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26200 && !row->ends_at_zv_p
26201 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26202 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26203 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26204 && !row->ends_at_zv_p
26205 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26206 {
26207 /* Found a candidate row. Now make sure at least one of the
26208 glyphs it displays has a charpos from the range
26209 [START_CHARPOS..END_CHARPOS).
26210
26211 This is not obvious because bidi reordering could make
26212 buffer positions of a row be 1,2,3,102,101,100, and if we
26213 want to highlight characters in [50..60), we don't want
26214 this row, even though [50..60) does intersect [1..103),
26215 the range of character positions given by the row's start
26216 and end positions. */
26217 struct glyph *g = row->glyphs[TEXT_AREA];
26218 struct glyph *e = g + row->used[TEXT_AREA];
26219
26220 while (g < e)
26221 {
26222 if (((BUFFERP (g->object) || INTEGERP (g->object))
26223 && start_charpos <= g->charpos && g->charpos < end_charpos)
26224 /* A glyph that comes from DISP_STRING is by
26225 definition to be highlighted. */
26226 || EQ (g->object, disp_string))
26227 *start = row;
26228 g++;
26229 }
26230 if (*start)
26231 break;
26232 }
26233 }
26234
26235 /* Find the END row. */
26236 if (!*start
26237 /* If the last row is partially visible, start looking for END
26238 from that row, instead of starting from FIRST. */
26239 && !(row->enabled_p
26240 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26241 row = first;
26242 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26243 {
26244 struct glyph_row *next = row + 1;
26245 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26246
26247 if (!next->enabled_p
26248 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26249 /* The first row >= START whose range of displayed characters
26250 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26251 is the row END + 1. */
26252 || (start_charpos < next_start
26253 && end_charpos < next_start)
26254 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26255 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26256 && !next->ends_at_zv_p
26257 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26258 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26259 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26260 && !next->ends_at_zv_p
26261 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26262 {
26263 *end = row;
26264 break;
26265 }
26266 else
26267 {
26268 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26269 but none of the characters it displays are in the range, it is
26270 also END + 1. */
26271 struct glyph *g = next->glyphs[TEXT_AREA];
26272 struct glyph *s = g;
26273 struct glyph *e = g + next->used[TEXT_AREA];
26274
26275 while (g < e)
26276 {
26277 if (((BUFFERP (g->object) || INTEGERP (g->object))
26278 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26279 /* If the buffer position of the first glyph in
26280 the row is equal to END_CHARPOS, it means
26281 the last character to be highlighted is the
26282 newline of ROW, and we must consider NEXT as
26283 END, not END+1. */
26284 || (((!next->reversed_p && g == s)
26285 || (next->reversed_p && g == e - 1))
26286 && (g->charpos == end_charpos
26287 /* Special case for when NEXT is an
26288 empty line at ZV. */
26289 || (g->charpos == -1
26290 && !row->ends_at_zv_p
26291 && next_start == end_charpos)))))
26292 /* A glyph that comes from DISP_STRING is by
26293 definition to be highlighted. */
26294 || EQ (g->object, disp_string))
26295 break;
26296 g++;
26297 }
26298 if (g == e)
26299 {
26300 *end = row;
26301 break;
26302 }
26303 /* The first row that ends at ZV must be the last to be
26304 highlighted. */
26305 else if (next->ends_at_zv_p)
26306 {
26307 *end = next;
26308 break;
26309 }
26310 }
26311 }
26312 }
26313
26314 /* This function sets the mouse_face_* elements of HLINFO, assuming
26315 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26316 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26317 for the overlay or run of text properties specifying the mouse
26318 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26319 before-string and after-string that must also be highlighted.
26320 DISP_STRING, if non-nil, is a display string that may cover some
26321 or all of the highlighted text. */
26322
26323 static void
26324 mouse_face_from_buffer_pos (Lisp_Object window,
26325 Mouse_HLInfo *hlinfo,
26326 ptrdiff_t mouse_charpos,
26327 ptrdiff_t start_charpos,
26328 ptrdiff_t end_charpos,
26329 Lisp_Object before_string,
26330 Lisp_Object after_string,
26331 Lisp_Object disp_string)
26332 {
26333 struct window *w = XWINDOW (window);
26334 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26335 struct glyph_row *r1, *r2;
26336 struct glyph *glyph, *end;
26337 ptrdiff_t ignore, pos;
26338 int x;
26339
26340 eassert (NILP (disp_string) || STRINGP (disp_string));
26341 eassert (NILP (before_string) || STRINGP (before_string));
26342 eassert (NILP (after_string) || STRINGP (after_string));
26343
26344 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26345 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26346 if (r1 == NULL)
26347 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26348 /* If the before-string or display-string contains newlines,
26349 rows_from_pos_range skips to its last row. Move back. */
26350 if (!NILP (before_string) || !NILP (disp_string))
26351 {
26352 struct glyph_row *prev;
26353 while ((prev = r1 - 1, prev >= first)
26354 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26355 && prev->used[TEXT_AREA] > 0)
26356 {
26357 struct glyph *beg = prev->glyphs[TEXT_AREA];
26358 glyph = beg + prev->used[TEXT_AREA];
26359 while (--glyph >= beg && INTEGERP (glyph->object));
26360 if (glyph < beg
26361 || !(EQ (glyph->object, before_string)
26362 || EQ (glyph->object, disp_string)))
26363 break;
26364 r1 = prev;
26365 }
26366 }
26367 if (r2 == NULL)
26368 {
26369 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26370 hlinfo->mouse_face_past_end = 1;
26371 }
26372 else if (!NILP (after_string))
26373 {
26374 /* If the after-string has newlines, advance to its last row. */
26375 struct glyph_row *next;
26376 struct glyph_row *last
26377 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26378
26379 for (next = r2 + 1;
26380 next <= last
26381 && next->used[TEXT_AREA] > 0
26382 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26383 ++next)
26384 r2 = next;
26385 }
26386 /* The rest of the display engine assumes that mouse_face_beg_row is
26387 either above mouse_face_end_row or identical to it. But with
26388 bidi-reordered continued lines, the row for START_CHARPOS could
26389 be below the row for END_CHARPOS. If so, swap the rows and store
26390 them in correct order. */
26391 if (r1->y > r2->y)
26392 {
26393 struct glyph_row *tem = r2;
26394
26395 r2 = r1;
26396 r1 = tem;
26397 }
26398
26399 hlinfo->mouse_face_beg_y = r1->y;
26400 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26401 hlinfo->mouse_face_end_y = r2->y;
26402 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26403
26404 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26405 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26406 could be anywhere in the row and in any order. The strategy
26407 below is to find the leftmost and the rightmost glyph that
26408 belongs to either of these 3 strings, or whose position is
26409 between START_CHARPOS and END_CHARPOS, and highlight all the
26410 glyphs between those two. This may cover more than just the text
26411 between START_CHARPOS and END_CHARPOS if the range of characters
26412 strides the bidi level boundary, e.g. if the beginning is in R2L
26413 text while the end is in L2R text or vice versa. */
26414 if (!r1->reversed_p)
26415 {
26416 /* This row is in a left to right paragraph. Scan it left to
26417 right. */
26418 glyph = r1->glyphs[TEXT_AREA];
26419 end = glyph + r1->used[TEXT_AREA];
26420 x = r1->x;
26421
26422 /* Skip truncation glyphs at the start of the glyph row. */
26423 if (r1->displays_text_p)
26424 for (; glyph < end
26425 && INTEGERP (glyph->object)
26426 && glyph->charpos < 0;
26427 ++glyph)
26428 x += glyph->pixel_width;
26429
26430 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26431 or DISP_STRING, and the first glyph from buffer whose
26432 position is between START_CHARPOS and END_CHARPOS. */
26433 for (; glyph < end
26434 && !INTEGERP (glyph->object)
26435 && !EQ (glyph->object, disp_string)
26436 && !(BUFFERP (glyph->object)
26437 && (glyph->charpos >= start_charpos
26438 && glyph->charpos < end_charpos));
26439 ++glyph)
26440 {
26441 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26442 are present at buffer positions between START_CHARPOS and
26443 END_CHARPOS, or if they come from an overlay. */
26444 if (EQ (glyph->object, before_string))
26445 {
26446 pos = string_buffer_position (before_string,
26447 start_charpos);
26448 /* If pos == 0, it means before_string came from an
26449 overlay, not from a buffer position. */
26450 if (!pos || (pos >= start_charpos && pos < end_charpos))
26451 break;
26452 }
26453 else if (EQ (glyph->object, after_string))
26454 {
26455 pos = string_buffer_position (after_string, end_charpos);
26456 if (!pos || (pos >= start_charpos && pos < end_charpos))
26457 break;
26458 }
26459 x += glyph->pixel_width;
26460 }
26461 hlinfo->mouse_face_beg_x = x;
26462 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26463 }
26464 else
26465 {
26466 /* This row is in a right to left paragraph. Scan it right to
26467 left. */
26468 struct glyph *g;
26469
26470 end = r1->glyphs[TEXT_AREA] - 1;
26471 glyph = end + r1->used[TEXT_AREA];
26472
26473 /* Skip truncation glyphs at the start of the glyph row. */
26474 if (r1->displays_text_p)
26475 for (; glyph > end
26476 && INTEGERP (glyph->object)
26477 && glyph->charpos < 0;
26478 --glyph)
26479 ;
26480
26481 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26482 or DISP_STRING, and the first glyph from buffer whose
26483 position is between START_CHARPOS and END_CHARPOS. */
26484 for (; glyph > end
26485 && !INTEGERP (glyph->object)
26486 && !EQ (glyph->object, disp_string)
26487 && !(BUFFERP (glyph->object)
26488 && (glyph->charpos >= start_charpos
26489 && glyph->charpos < end_charpos));
26490 --glyph)
26491 {
26492 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26493 are present at buffer positions between START_CHARPOS and
26494 END_CHARPOS, or if they come from an overlay. */
26495 if (EQ (glyph->object, before_string))
26496 {
26497 pos = string_buffer_position (before_string, start_charpos);
26498 /* If pos == 0, it means before_string came from an
26499 overlay, not from a buffer position. */
26500 if (!pos || (pos >= start_charpos && pos < end_charpos))
26501 break;
26502 }
26503 else if (EQ (glyph->object, after_string))
26504 {
26505 pos = string_buffer_position (after_string, end_charpos);
26506 if (!pos || (pos >= start_charpos && pos < end_charpos))
26507 break;
26508 }
26509 }
26510
26511 glyph++; /* first glyph to the right of the highlighted area */
26512 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26513 x += g->pixel_width;
26514 hlinfo->mouse_face_beg_x = x;
26515 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26516 }
26517
26518 /* If the highlight ends in a different row, compute GLYPH and END
26519 for the end row. Otherwise, reuse the values computed above for
26520 the row where the highlight begins. */
26521 if (r2 != r1)
26522 {
26523 if (!r2->reversed_p)
26524 {
26525 glyph = r2->glyphs[TEXT_AREA];
26526 end = glyph + r2->used[TEXT_AREA];
26527 x = r2->x;
26528 }
26529 else
26530 {
26531 end = r2->glyphs[TEXT_AREA] - 1;
26532 glyph = end + r2->used[TEXT_AREA];
26533 }
26534 }
26535
26536 if (!r2->reversed_p)
26537 {
26538 /* Skip truncation and continuation glyphs near the end of the
26539 row, and also blanks and stretch glyphs inserted by
26540 extend_face_to_end_of_line. */
26541 while (end > glyph
26542 && INTEGERP ((end - 1)->object))
26543 --end;
26544 /* Scan the rest of the glyph row from the end, looking for the
26545 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26546 DISP_STRING, or whose position is between START_CHARPOS
26547 and END_CHARPOS */
26548 for (--end;
26549 end > glyph
26550 && !INTEGERP (end->object)
26551 && !EQ (end->object, disp_string)
26552 && !(BUFFERP (end->object)
26553 && (end->charpos >= start_charpos
26554 && end->charpos < end_charpos));
26555 --end)
26556 {
26557 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26558 are present at buffer positions between START_CHARPOS and
26559 END_CHARPOS, or if they come from an overlay. */
26560 if (EQ (end->object, before_string))
26561 {
26562 pos = string_buffer_position (before_string, start_charpos);
26563 if (!pos || (pos >= start_charpos && pos < end_charpos))
26564 break;
26565 }
26566 else if (EQ (end->object, after_string))
26567 {
26568 pos = string_buffer_position (after_string, end_charpos);
26569 if (!pos || (pos >= start_charpos && pos < end_charpos))
26570 break;
26571 }
26572 }
26573 /* Find the X coordinate of the last glyph to be highlighted. */
26574 for (; glyph <= end; ++glyph)
26575 x += glyph->pixel_width;
26576
26577 hlinfo->mouse_face_end_x = x;
26578 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26579 }
26580 else
26581 {
26582 /* Skip truncation and continuation glyphs near the end of the
26583 row, and also blanks and stretch glyphs inserted by
26584 extend_face_to_end_of_line. */
26585 x = r2->x;
26586 end++;
26587 while (end < glyph
26588 && INTEGERP (end->object))
26589 {
26590 x += end->pixel_width;
26591 ++end;
26592 }
26593 /* Scan the rest of the glyph row from the end, looking for the
26594 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26595 DISP_STRING, or whose position is between START_CHARPOS
26596 and END_CHARPOS */
26597 for ( ;
26598 end < glyph
26599 && !INTEGERP (end->object)
26600 && !EQ (end->object, disp_string)
26601 && !(BUFFERP (end->object)
26602 && (end->charpos >= start_charpos
26603 && end->charpos < end_charpos));
26604 ++end)
26605 {
26606 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26607 are present at buffer positions between START_CHARPOS and
26608 END_CHARPOS, or if they come from an overlay. */
26609 if (EQ (end->object, before_string))
26610 {
26611 pos = string_buffer_position (before_string, start_charpos);
26612 if (!pos || (pos >= start_charpos && pos < end_charpos))
26613 break;
26614 }
26615 else if (EQ (end->object, after_string))
26616 {
26617 pos = string_buffer_position (after_string, end_charpos);
26618 if (!pos || (pos >= start_charpos && pos < end_charpos))
26619 break;
26620 }
26621 x += end->pixel_width;
26622 }
26623 /* If we exited the above loop because we arrived at the last
26624 glyph of the row, and its buffer position is still not in
26625 range, it means the last character in range is the preceding
26626 newline. Bump the end column and x values to get past the
26627 last glyph. */
26628 if (end == glyph
26629 && BUFFERP (end->object)
26630 && (end->charpos < start_charpos
26631 || end->charpos >= end_charpos))
26632 {
26633 x += end->pixel_width;
26634 ++end;
26635 }
26636 hlinfo->mouse_face_end_x = x;
26637 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26638 }
26639
26640 hlinfo->mouse_face_window = window;
26641 hlinfo->mouse_face_face_id
26642 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26643 mouse_charpos + 1,
26644 !hlinfo->mouse_face_hidden, -1);
26645 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26646 }
26647
26648 /* The following function is not used anymore (replaced with
26649 mouse_face_from_string_pos), but I leave it here for the time
26650 being, in case someone would. */
26651
26652 #if 0 /* not used */
26653
26654 /* Find the position of the glyph for position POS in OBJECT in
26655 window W's current matrix, and return in *X, *Y the pixel
26656 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26657
26658 RIGHT_P non-zero means return the position of the right edge of the
26659 glyph, RIGHT_P zero means return the left edge position.
26660
26661 If no glyph for POS exists in the matrix, return the position of
26662 the glyph with the next smaller position that is in the matrix, if
26663 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26664 exists in the matrix, return the position of the glyph with the
26665 next larger position in OBJECT.
26666
26667 Value is non-zero if a glyph was found. */
26668
26669 static int
26670 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26671 int *hpos, int *vpos, int *x, int *y, int right_p)
26672 {
26673 int yb = window_text_bottom_y (w);
26674 struct glyph_row *r;
26675 struct glyph *best_glyph = NULL;
26676 struct glyph_row *best_row = NULL;
26677 int best_x = 0;
26678
26679 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26680 r->enabled_p && r->y < yb;
26681 ++r)
26682 {
26683 struct glyph *g = r->glyphs[TEXT_AREA];
26684 struct glyph *e = g + r->used[TEXT_AREA];
26685 int gx;
26686
26687 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26688 if (EQ (g->object, object))
26689 {
26690 if (g->charpos == pos)
26691 {
26692 best_glyph = g;
26693 best_x = gx;
26694 best_row = r;
26695 goto found;
26696 }
26697 else if (best_glyph == NULL
26698 || ((eabs (g->charpos - pos)
26699 < eabs (best_glyph->charpos - pos))
26700 && (right_p
26701 ? g->charpos < pos
26702 : g->charpos > pos)))
26703 {
26704 best_glyph = g;
26705 best_x = gx;
26706 best_row = r;
26707 }
26708 }
26709 }
26710
26711 found:
26712
26713 if (best_glyph)
26714 {
26715 *x = best_x;
26716 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26717
26718 if (right_p)
26719 {
26720 *x += best_glyph->pixel_width;
26721 ++*hpos;
26722 }
26723
26724 *y = best_row->y;
26725 *vpos = best_row - w->current_matrix->rows;
26726 }
26727
26728 return best_glyph != NULL;
26729 }
26730 #endif /* not used */
26731
26732 /* Find the positions of the first and the last glyphs in window W's
26733 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26734 (assumed to be a string), and return in HLINFO's mouse_face_*
26735 members the pixel and column/row coordinates of those glyphs. */
26736
26737 static void
26738 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26739 Lisp_Object object,
26740 ptrdiff_t startpos, ptrdiff_t endpos)
26741 {
26742 int yb = window_text_bottom_y (w);
26743 struct glyph_row *r;
26744 struct glyph *g, *e;
26745 int gx;
26746 int found = 0;
26747
26748 /* Find the glyph row with at least one position in the range
26749 [STARTPOS..ENDPOS], and the first glyph in that row whose
26750 position belongs to that range. */
26751 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26752 r->enabled_p && r->y < yb;
26753 ++r)
26754 {
26755 if (!r->reversed_p)
26756 {
26757 g = r->glyphs[TEXT_AREA];
26758 e = g + r->used[TEXT_AREA];
26759 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26760 if (EQ (g->object, object)
26761 && startpos <= g->charpos && g->charpos <= endpos)
26762 {
26763 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26764 hlinfo->mouse_face_beg_y = r->y;
26765 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26766 hlinfo->mouse_face_beg_x = gx;
26767 found = 1;
26768 break;
26769 }
26770 }
26771 else
26772 {
26773 struct glyph *g1;
26774
26775 e = r->glyphs[TEXT_AREA];
26776 g = e + r->used[TEXT_AREA];
26777 for ( ; g > e; --g)
26778 if (EQ ((g-1)->object, object)
26779 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26780 {
26781 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26782 hlinfo->mouse_face_beg_y = r->y;
26783 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26784 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26785 gx += g1->pixel_width;
26786 hlinfo->mouse_face_beg_x = gx;
26787 found = 1;
26788 break;
26789 }
26790 }
26791 if (found)
26792 break;
26793 }
26794
26795 if (!found)
26796 return;
26797
26798 /* Starting with the next row, look for the first row which does NOT
26799 include any glyphs whose positions are in the range. */
26800 for (++r; r->enabled_p && r->y < yb; ++r)
26801 {
26802 g = r->glyphs[TEXT_AREA];
26803 e = g + r->used[TEXT_AREA];
26804 found = 0;
26805 for ( ; g < e; ++g)
26806 if (EQ (g->object, object)
26807 && startpos <= g->charpos && g->charpos <= endpos)
26808 {
26809 found = 1;
26810 break;
26811 }
26812 if (!found)
26813 break;
26814 }
26815
26816 /* The highlighted region ends on the previous row. */
26817 r--;
26818
26819 /* Set the end row and its vertical pixel coordinate. */
26820 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26821 hlinfo->mouse_face_end_y = r->y;
26822
26823 /* Compute and set the end column and the end column's horizontal
26824 pixel coordinate. */
26825 if (!r->reversed_p)
26826 {
26827 g = r->glyphs[TEXT_AREA];
26828 e = g + r->used[TEXT_AREA];
26829 for ( ; e > g; --e)
26830 if (EQ ((e-1)->object, object)
26831 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26832 break;
26833 hlinfo->mouse_face_end_col = e - g;
26834
26835 for (gx = r->x; g < e; ++g)
26836 gx += g->pixel_width;
26837 hlinfo->mouse_face_end_x = gx;
26838 }
26839 else
26840 {
26841 e = r->glyphs[TEXT_AREA];
26842 g = e + r->used[TEXT_AREA];
26843 for (gx = r->x ; e < g; ++e)
26844 {
26845 if (EQ (e->object, object)
26846 && startpos <= e->charpos && e->charpos <= endpos)
26847 break;
26848 gx += e->pixel_width;
26849 }
26850 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26851 hlinfo->mouse_face_end_x = gx;
26852 }
26853 }
26854
26855 #ifdef HAVE_WINDOW_SYSTEM
26856
26857 /* See if position X, Y is within a hot-spot of an image. */
26858
26859 static int
26860 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26861 {
26862 if (!CONSP (hot_spot))
26863 return 0;
26864
26865 if (EQ (XCAR (hot_spot), Qrect))
26866 {
26867 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26868 Lisp_Object rect = XCDR (hot_spot);
26869 Lisp_Object tem;
26870 if (!CONSP (rect))
26871 return 0;
26872 if (!CONSP (XCAR (rect)))
26873 return 0;
26874 if (!CONSP (XCDR (rect)))
26875 return 0;
26876 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26877 return 0;
26878 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26879 return 0;
26880 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26881 return 0;
26882 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26883 return 0;
26884 return 1;
26885 }
26886 else if (EQ (XCAR (hot_spot), Qcircle))
26887 {
26888 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26889 Lisp_Object circ = XCDR (hot_spot);
26890 Lisp_Object lr, lx0, ly0;
26891 if (CONSP (circ)
26892 && CONSP (XCAR (circ))
26893 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26894 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26895 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26896 {
26897 double r = XFLOATINT (lr);
26898 double dx = XINT (lx0) - x;
26899 double dy = XINT (ly0) - y;
26900 return (dx * dx + dy * dy <= r * r);
26901 }
26902 }
26903 else if (EQ (XCAR (hot_spot), Qpoly))
26904 {
26905 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26906 if (VECTORP (XCDR (hot_spot)))
26907 {
26908 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26909 Lisp_Object *poly = v->contents;
26910 ptrdiff_t n = v->header.size;
26911 ptrdiff_t i;
26912 int inside = 0;
26913 Lisp_Object lx, ly;
26914 int x0, y0;
26915
26916 /* Need an even number of coordinates, and at least 3 edges. */
26917 if (n < 6 || n & 1)
26918 return 0;
26919
26920 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26921 If count is odd, we are inside polygon. Pixels on edges
26922 may or may not be included depending on actual geometry of the
26923 polygon. */
26924 if ((lx = poly[n-2], !INTEGERP (lx))
26925 || (ly = poly[n-1], !INTEGERP (lx)))
26926 return 0;
26927 x0 = XINT (lx), y0 = XINT (ly);
26928 for (i = 0; i < n; i += 2)
26929 {
26930 int x1 = x0, y1 = y0;
26931 if ((lx = poly[i], !INTEGERP (lx))
26932 || (ly = poly[i+1], !INTEGERP (ly)))
26933 return 0;
26934 x0 = XINT (lx), y0 = XINT (ly);
26935
26936 /* Does this segment cross the X line? */
26937 if (x0 >= x)
26938 {
26939 if (x1 >= x)
26940 continue;
26941 }
26942 else if (x1 < x)
26943 continue;
26944 if (y > y0 && y > y1)
26945 continue;
26946 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26947 inside = !inside;
26948 }
26949 return inside;
26950 }
26951 }
26952 return 0;
26953 }
26954
26955 Lisp_Object
26956 find_hot_spot (Lisp_Object map, int x, int y)
26957 {
26958 while (CONSP (map))
26959 {
26960 if (CONSP (XCAR (map))
26961 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26962 return XCAR (map);
26963 map = XCDR (map);
26964 }
26965
26966 return Qnil;
26967 }
26968
26969 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26970 3, 3, 0,
26971 doc: /* Lookup in image map MAP coordinates X and Y.
26972 An image map is an alist where each element has the format (AREA ID PLIST).
26973 An AREA is specified as either a rectangle, a circle, or a polygon:
26974 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26975 pixel coordinates of the upper left and bottom right corners.
26976 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26977 and the radius of the circle; r may be a float or integer.
26978 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26979 vector describes one corner in the polygon.
26980 Returns the alist element for the first matching AREA in MAP. */)
26981 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26982 {
26983 if (NILP (map))
26984 return Qnil;
26985
26986 CHECK_NUMBER (x);
26987 CHECK_NUMBER (y);
26988
26989 return find_hot_spot (map,
26990 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
26991 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
26992 }
26993
26994
26995 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26996 static void
26997 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26998 {
26999 /* Do not change cursor shape while dragging mouse. */
27000 if (!NILP (do_mouse_tracking))
27001 return;
27002
27003 if (!NILP (pointer))
27004 {
27005 if (EQ (pointer, Qarrow))
27006 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27007 else if (EQ (pointer, Qhand))
27008 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27009 else if (EQ (pointer, Qtext))
27010 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27011 else if (EQ (pointer, intern ("hdrag")))
27012 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27013 #ifdef HAVE_X_WINDOWS
27014 else if (EQ (pointer, intern ("vdrag")))
27015 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27016 #endif
27017 else if (EQ (pointer, intern ("hourglass")))
27018 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27019 else if (EQ (pointer, Qmodeline))
27020 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27021 else
27022 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27023 }
27024
27025 if (cursor != No_Cursor)
27026 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27027 }
27028
27029 #endif /* HAVE_WINDOW_SYSTEM */
27030
27031 /* Take proper action when mouse has moved to the mode or header line
27032 or marginal area AREA of window W, x-position X and y-position Y.
27033 X is relative to the start of the text display area of W, so the
27034 width of bitmap areas and scroll bars must be subtracted to get a
27035 position relative to the start of the mode line. */
27036
27037 static void
27038 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27039 enum window_part area)
27040 {
27041 struct window *w = XWINDOW (window);
27042 struct frame *f = XFRAME (w->frame);
27043 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27044 #ifdef HAVE_WINDOW_SYSTEM
27045 Display_Info *dpyinfo;
27046 #endif
27047 Cursor cursor = No_Cursor;
27048 Lisp_Object pointer = Qnil;
27049 int dx, dy, width, height;
27050 ptrdiff_t charpos;
27051 Lisp_Object string, object = Qnil;
27052 Lisp_Object pos IF_LINT (= Qnil), help;
27053
27054 Lisp_Object mouse_face;
27055 int original_x_pixel = x;
27056 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27057 struct glyph_row *row IF_LINT (= 0);
27058
27059 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27060 {
27061 int x0;
27062 struct glyph *end;
27063
27064 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27065 returns them in row/column units! */
27066 string = mode_line_string (w, area, &x, &y, &charpos,
27067 &object, &dx, &dy, &width, &height);
27068
27069 row = (area == ON_MODE_LINE
27070 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27071 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27072
27073 /* Find the glyph under the mouse pointer. */
27074 if (row->mode_line_p && row->enabled_p)
27075 {
27076 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27077 end = glyph + row->used[TEXT_AREA];
27078
27079 for (x0 = original_x_pixel;
27080 glyph < end && x0 >= glyph->pixel_width;
27081 ++glyph)
27082 x0 -= glyph->pixel_width;
27083
27084 if (glyph >= end)
27085 glyph = NULL;
27086 }
27087 }
27088 else
27089 {
27090 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27091 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27092 returns them in row/column units! */
27093 string = marginal_area_string (w, area, &x, &y, &charpos,
27094 &object, &dx, &dy, &width, &height);
27095 }
27096
27097 help = Qnil;
27098
27099 #ifdef HAVE_WINDOW_SYSTEM
27100 if (IMAGEP (object))
27101 {
27102 Lisp_Object image_map, hotspot;
27103 if ((image_map = Fplist_get (XCDR (object), QCmap),
27104 !NILP (image_map))
27105 && (hotspot = find_hot_spot (image_map, dx, dy),
27106 CONSP (hotspot))
27107 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27108 {
27109 Lisp_Object plist;
27110
27111 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27112 If so, we could look for mouse-enter, mouse-leave
27113 properties in PLIST (and do something...). */
27114 hotspot = XCDR (hotspot);
27115 if (CONSP (hotspot)
27116 && (plist = XCAR (hotspot), CONSP (plist)))
27117 {
27118 pointer = Fplist_get (plist, Qpointer);
27119 if (NILP (pointer))
27120 pointer = Qhand;
27121 help = Fplist_get (plist, Qhelp_echo);
27122 if (!NILP (help))
27123 {
27124 help_echo_string = help;
27125 XSETWINDOW (help_echo_window, w);
27126 help_echo_object = w->buffer;
27127 help_echo_pos = charpos;
27128 }
27129 }
27130 }
27131 if (NILP (pointer))
27132 pointer = Fplist_get (XCDR (object), QCpointer);
27133 }
27134 #endif /* HAVE_WINDOW_SYSTEM */
27135
27136 if (STRINGP (string))
27137 pos = make_number (charpos);
27138
27139 /* Set the help text and mouse pointer. If the mouse is on a part
27140 of the mode line without any text (e.g. past the right edge of
27141 the mode line text), use the default help text and pointer. */
27142 if (STRINGP (string) || area == ON_MODE_LINE)
27143 {
27144 /* Arrange to display the help by setting the global variables
27145 help_echo_string, help_echo_object, and help_echo_pos. */
27146 if (NILP (help))
27147 {
27148 if (STRINGP (string))
27149 help = Fget_text_property (pos, Qhelp_echo, string);
27150
27151 if (!NILP (help))
27152 {
27153 help_echo_string = help;
27154 XSETWINDOW (help_echo_window, w);
27155 help_echo_object = string;
27156 help_echo_pos = charpos;
27157 }
27158 else if (area == ON_MODE_LINE)
27159 {
27160 Lisp_Object default_help
27161 = buffer_local_value_1 (Qmode_line_default_help_echo,
27162 w->buffer);
27163
27164 if (STRINGP (default_help))
27165 {
27166 help_echo_string = default_help;
27167 XSETWINDOW (help_echo_window, w);
27168 help_echo_object = Qnil;
27169 help_echo_pos = -1;
27170 }
27171 }
27172 }
27173
27174 #ifdef HAVE_WINDOW_SYSTEM
27175 /* Change the mouse pointer according to what is under it. */
27176 if (FRAME_WINDOW_P (f))
27177 {
27178 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27179 if (STRINGP (string))
27180 {
27181 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27182
27183 if (NILP (pointer))
27184 pointer = Fget_text_property (pos, Qpointer, string);
27185
27186 /* Change the mouse pointer according to what is under X/Y. */
27187 if (NILP (pointer)
27188 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27189 {
27190 Lisp_Object map;
27191 map = Fget_text_property (pos, Qlocal_map, string);
27192 if (!KEYMAPP (map))
27193 map = Fget_text_property (pos, Qkeymap, string);
27194 if (!KEYMAPP (map))
27195 cursor = dpyinfo->vertical_scroll_bar_cursor;
27196 }
27197 }
27198 else
27199 /* Default mode-line pointer. */
27200 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27201 }
27202 #endif
27203 }
27204
27205 /* Change the mouse face according to what is under X/Y. */
27206 if (STRINGP (string))
27207 {
27208 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27209 if (!NILP (mouse_face)
27210 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27211 && glyph)
27212 {
27213 Lisp_Object b, e;
27214
27215 struct glyph * tmp_glyph;
27216
27217 int gpos;
27218 int gseq_length;
27219 int total_pixel_width;
27220 ptrdiff_t begpos, endpos, ignore;
27221
27222 int vpos, hpos;
27223
27224 b = Fprevious_single_property_change (make_number (charpos + 1),
27225 Qmouse_face, string, Qnil);
27226 if (NILP (b))
27227 begpos = 0;
27228 else
27229 begpos = XINT (b);
27230
27231 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27232 if (NILP (e))
27233 endpos = SCHARS (string);
27234 else
27235 endpos = XINT (e);
27236
27237 /* Calculate the glyph position GPOS of GLYPH in the
27238 displayed string, relative to the beginning of the
27239 highlighted part of the string.
27240
27241 Note: GPOS is different from CHARPOS. CHARPOS is the
27242 position of GLYPH in the internal string object. A mode
27243 line string format has structures which are converted to
27244 a flattened string by the Emacs Lisp interpreter. The
27245 internal string is an element of those structures. The
27246 displayed string is the flattened string. */
27247 tmp_glyph = row_start_glyph;
27248 while (tmp_glyph < glyph
27249 && (!(EQ (tmp_glyph->object, glyph->object)
27250 && begpos <= tmp_glyph->charpos
27251 && tmp_glyph->charpos < endpos)))
27252 tmp_glyph++;
27253 gpos = glyph - tmp_glyph;
27254
27255 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27256 the highlighted part of the displayed string to which
27257 GLYPH belongs. Note: GSEQ_LENGTH is different from
27258 SCHARS (STRING), because the latter returns the length of
27259 the internal string. */
27260 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27261 tmp_glyph > glyph
27262 && (!(EQ (tmp_glyph->object, glyph->object)
27263 && begpos <= tmp_glyph->charpos
27264 && tmp_glyph->charpos < endpos));
27265 tmp_glyph--)
27266 ;
27267 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27268
27269 /* Calculate the total pixel width of all the glyphs between
27270 the beginning of the highlighted area and GLYPH. */
27271 total_pixel_width = 0;
27272 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27273 total_pixel_width += tmp_glyph->pixel_width;
27274
27275 /* Pre calculation of re-rendering position. Note: X is in
27276 column units here, after the call to mode_line_string or
27277 marginal_area_string. */
27278 hpos = x - gpos;
27279 vpos = (area == ON_MODE_LINE
27280 ? (w->current_matrix)->nrows - 1
27281 : 0);
27282
27283 /* If GLYPH's position is included in the region that is
27284 already drawn in mouse face, we have nothing to do. */
27285 if ( EQ (window, hlinfo->mouse_face_window)
27286 && (!row->reversed_p
27287 ? (hlinfo->mouse_face_beg_col <= hpos
27288 && hpos < hlinfo->mouse_face_end_col)
27289 /* In R2L rows we swap BEG and END, see below. */
27290 : (hlinfo->mouse_face_end_col <= hpos
27291 && hpos < hlinfo->mouse_face_beg_col))
27292 && hlinfo->mouse_face_beg_row == vpos )
27293 return;
27294
27295 if (clear_mouse_face (hlinfo))
27296 cursor = No_Cursor;
27297
27298 if (!row->reversed_p)
27299 {
27300 hlinfo->mouse_face_beg_col = hpos;
27301 hlinfo->mouse_face_beg_x = original_x_pixel
27302 - (total_pixel_width + dx);
27303 hlinfo->mouse_face_end_col = hpos + gseq_length;
27304 hlinfo->mouse_face_end_x = 0;
27305 }
27306 else
27307 {
27308 /* In R2L rows, show_mouse_face expects BEG and END
27309 coordinates to be swapped. */
27310 hlinfo->mouse_face_end_col = hpos;
27311 hlinfo->mouse_face_end_x = original_x_pixel
27312 - (total_pixel_width + dx);
27313 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27314 hlinfo->mouse_face_beg_x = 0;
27315 }
27316
27317 hlinfo->mouse_face_beg_row = vpos;
27318 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27319 hlinfo->mouse_face_beg_y = 0;
27320 hlinfo->mouse_face_end_y = 0;
27321 hlinfo->mouse_face_past_end = 0;
27322 hlinfo->mouse_face_window = window;
27323
27324 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27325 charpos,
27326 0, 0, 0,
27327 &ignore,
27328 glyph->face_id,
27329 1);
27330 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27331
27332 if (NILP (pointer))
27333 pointer = Qhand;
27334 }
27335 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27336 clear_mouse_face (hlinfo);
27337 }
27338 #ifdef HAVE_WINDOW_SYSTEM
27339 if (FRAME_WINDOW_P (f))
27340 define_frame_cursor1 (f, cursor, pointer);
27341 #endif
27342 }
27343
27344
27345 /* EXPORT:
27346 Take proper action when the mouse has moved to position X, Y on
27347 frame F as regards highlighting characters that have mouse-face
27348 properties. Also de-highlighting chars where the mouse was before.
27349 X and Y can be negative or out of range. */
27350
27351 void
27352 note_mouse_highlight (struct frame *f, int x, int y)
27353 {
27354 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27355 enum window_part part = ON_NOTHING;
27356 Lisp_Object window;
27357 struct window *w;
27358 Cursor cursor = No_Cursor;
27359 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27360 struct buffer *b;
27361
27362 /* When a menu is active, don't highlight because this looks odd. */
27363 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27364 if (popup_activated ())
27365 return;
27366 #endif
27367
27368 if (NILP (Vmouse_highlight)
27369 || !f->glyphs_initialized_p
27370 || f->pointer_invisible)
27371 return;
27372
27373 hlinfo->mouse_face_mouse_x = x;
27374 hlinfo->mouse_face_mouse_y = y;
27375 hlinfo->mouse_face_mouse_frame = f;
27376
27377 if (hlinfo->mouse_face_defer)
27378 return;
27379
27380 if (gc_in_progress)
27381 {
27382 hlinfo->mouse_face_deferred_gc = 1;
27383 return;
27384 }
27385
27386 /* Which window is that in? */
27387 window = window_from_coordinates (f, x, y, &part, 1);
27388
27389 /* If displaying active text in another window, clear that. */
27390 if (! EQ (window, hlinfo->mouse_face_window)
27391 /* Also clear if we move out of text area in same window. */
27392 || (!NILP (hlinfo->mouse_face_window)
27393 && !NILP (window)
27394 && part != ON_TEXT
27395 && part != ON_MODE_LINE
27396 && part != ON_HEADER_LINE))
27397 clear_mouse_face (hlinfo);
27398
27399 /* Not on a window -> return. */
27400 if (!WINDOWP (window))
27401 return;
27402
27403 /* Reset help_echo_string. It will get recomputed below. */
27404 help_echo_string = Qnil;
27405
27406 /* Convert to window-relative pixel coordinates. */
27407 w = XWINDOW (window);
27408 frame_to_window_pixel_xy (w, &x, &y);
27409
27410 #ifdef HAVE_WINDOW_SYSTEM
27411 /* Handle tool-bar window differently since it doesn't display a
27412 buffer. */
27413 if (EQ (window, f->tool_bar_window))
27414 {
27415 note_tool_bar_highlight (f, x, y);
27416 return;
27417 }
27418 #endif
27419
27420 /* Mouse is on the mode, header line or margin? */
27421 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27422 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27423 {
27424 note_mode_line_or_margin_highlight (window, x, y, part);
27425 return;
27426 }
27427
27428 #ifdef HAVE_WINDOW_SYSTEM
27429 if (part == ON_VERTICAL_BORDER)
27430 {
27431 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27432 help_echo_string = build_string ("drag-mouse-1: resize");
27433 }
27434 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27435 || part == ON_SCROLL_BAR)
27436 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27437 else
27438 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27439 #endif
27440
27441 /* Are we in a window whose display is up to date?
27442 And verify the buffer's text has not changed. */
27443 b = XBUFFER (w->buffer);
27444 if (part == ON_TEXT
27445 && EQ (w->window_end_valid, w->buffer)
27446 && w->last_modified == BUF_MODIFF (b)
27447 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27448 {
27449 int hpos, vpos, dx, dy, area = LAST_AREA;
27450 ptrdiff_t pos;
27451 struct glyph *glyph;
27452 Lisp_Object object;
27453 Lisp_Object mouse_face = Qnil, position;
27454 Lisp_Object *overlay_vec = NULL;
27455 ptrdiff_t i, noverlays;
27456 struct buffer *obuf;
27457 ptrdiff_t obegv, ozv;
27458 int same_region;
27459
27460 /* Find the glyph under X/Y. */
27461 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27462
27463 #ifdef HAVE_WINDOW_SYSTEM
27464 /* Look for :pointer property on image. */
27465 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27466 {
27467 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27468 if (img != NULL && IMAGEP (img->spec))
27469 {
27470 Lisp_Object image_map, hotspot;
27471 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27472 !NILP (image_map))
27473 && (hotspot = find_hot_spot (image_map,
27474 glyph->slice.img.x + dx,
27475 glyph->slice.img.y + dy),
27476 CONSP (hotspot))
27477 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27478 {
27479 Lisp_Object plist;
27480
27481 /* Could check XCAR (hotspot) to see if we enter/leave
27482 this hot-spot.
27483 If so, we could look for mouse-enter, mouse-leave
27484 properties in PLIST (and do something...). */
27485 hotspot = XCDR (hotspot);
27486 if (CONSP (hotspot)
27487 && (plist = XCAR (hotspot), CONSP (plist)))
27488 {
27489 pointer = Fplist_get (plist, Qpointer);
27490 if (NILP (pointer))
27491 pointer = Qhand;
27492 help_echo_string = Fplist_get (plist, Qhelp_echo);
27493 if (!NILP (help_echo_string))
27494 {
27495 help_echo_window = window;
27496 help_echo_object = glyph->object;
27497 help_echo_pos = glyph->charpos;
27498 }
27499 }
27500 }
27501 if (NILP (pointer))
27502 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27503 }
27504 }
27505 #endif /* HAVE_WINDOW_SYSTEM */
27506
27507 /* Clear mouse face if X/Y not over text. */
27508 if (glyph == NULL
27509 || area != TEXT_AREA
27510 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27511 /* Glyph's OBJECT is an integer for glyphs inserted by the
27512 display engine for its internal purposes, like truncation
27513 and continuation glyphs and blanks beyond the end of
27514 line's text on text terminals. If we are over such a
27515 glyph, we are not over any text. */
27516 || INTEGERP (glyph->object)
27517 /* R2L rows have a stretch glyph at their front, which
27518 stands for no text, whereas L2R rows have no glyphs at
27519 all beyond the end of text. Treat such stretch glyphs
27520 like we do with NULL glyphs in L2R rows. */
27521 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27522 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27523 && glyph->type == STRETCH_GLYPH
27524 && glyph->avoid_cursor_p))
27525 {
27526 if (clear_mouse_face (hlinfo))
27527 cursor = No_Cursor;
27528 #ifdef HAVE_WINDOW_SYSTEM
27529 if (FRAME_WINDOW_P (f) && NILP (pointer))
27530 {
27531 if (area != TEXT_AREA)
27532 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27533 else
27534 pointer = Vvoid_text_area_pointer;
27535 }
27536 #endif
27537 goto set_cursor;
27538 }
27539
27540 pos = glyph->charpos;
27541 object = glyph->object;
27542 if (!STRINGP (object) && !BUFFERP (object))
27543 goto set_cursor;
27544
27545 /* If we get an out-of-range value, return now; avoid an error. */
27546 if (BUFFERP (object) && pos > BUF_Z (b))
27547 goto set_cursor;
27548
27549 /* Make the window's buffer temporarily current for
27550 overlays_at and compute_char_face. */
27551 obuf = current_buffer;
27552 current_buffer = b;
27553 obegv = BEGV;
27554 ozv = ZV;
27555 BEGV = BEG;
27556 ZV = Z;
27557
27558 /* Is this char mouse-active or does it have help-echo? */
27559 position = make_number (pos);
27560
27561 if (BUFFERP (object))
27562 {
27563 /* Put all the overlays we want in a vector in overlay_vec. */
27564 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27565 /* Sort overlays into increasing priority order. */
27566 noverlays = sort_overlays (overlay_vec, noverlays, w);
27567 }
27568 else
27569 noverlays = 0;
27570
27571 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27572
27573 if (same_region)
27574 cursor = No_Cursor;
27575
27576 /* Check mouse-face highlighting. */
27577 if (! same_region
27578 /* If there exists an overlay with mouse-face overlapping
27579 the one we are currently highlighting, we have to
27580 check if we enter the overlapping overlay, and then
27581 highlight only that. */
27582 || (OVERLAYP (hlinfo->mouse_face_overlay)
27583 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27584 {
27585 /* Find the highest priority overlay with a mouse-face. */
27586 Lisp_Object overlay = Qnil;
27587 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27588 {
27589 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27590 if (!NILP (mouse_face))
27591 overlay = overlay_vec[i];
27592 }
27593
27594 /* If we're highlighting the same overlay as before, there's
27595 no need to do that again. */
27596 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27597 goto check_help_echo;
27598 hlinfo->mouse_face_overlay = overlay;
27599
27600 /* Clear the display of the old active region, if any. */
27601 if (clear_mouse_face (hlinfo))
27602 cursor = No_Cursor;
27603
27604 /* If no overlay applies, get a text property. */
27605 if (NILP (overlay))
27606 mouse_face = Fget_text_property (position, Qmouse_face, object);
27607
27608 /* Next, compute the bounds of the mouse highlighting and
27609 display it. */
27610 if (!NILP (mouse_face) && STRINGP (object))
27611 {
27612 /* The mouse-highlighting comes from a display string
27613 with a mouse-face. */
27614 Lisp_Object s, e;
27615 ptrdiff_t ignore;
27616
27617 s = Fprevious_single_property_change
27618 (make_number (pos + 1), Qmouse_face, object, Qnil);
27619 e = Fnext_single_property_change
27620 (position, Qmouse_face, object, Qnil);
27621 if (NILP (s))
27622 s = make_number (0);
27623 if (NILP (e))
27624 e = make_number (SCHARS (object) - 1);
27625 mouse_face_from_string_pos (w, hlinfo, object,
27626 XINT (s), XINT (e));
27627 hlinfo->mouse_face_past_end = 0;
27628 hlinfo->mouse_face_window = window;
27629 hlinfo->mouse_face_face_id
27630 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27631 glyph->face_id, 1);
27632 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27633 cursor = No_Cursor;
27634 }
27635 else
27636 {
27637 /* The mouse-highlighting, if any, comes from an overlay
27638 or text property in the buffer. */
27639 Lisp_Object buffer IF_LINT (= Qnil);
27640 Lisp_Object disp_string IF_LINT (= Qnil);
27641
27642 if (STRINGP (object))
27643 {
27644 /* If we are on a display string with no mouse-face,
27645 check if the text under it has one. */
27646 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27647 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27648 pos = string_buffer_position (object, start);
27649 if (pos > 0)
27650 {
27651 mouse_face = get_char_property_and_overlay
27652 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27653 buffer = w->buffer;
27654 disp_string = object;
27655 }
27656 }
27657 else
27658 {
27659 buffer = object;
27660 disp_string = Qnil;
27661 }
27662
27663 if (!NILP (mouse_face))
27664 {
27665 Lisp_Object before, after;
27666 Lisp_Object before_string, after_string;
27667 /* To correctly find the limits of mouse highlight
27668 in a bidi-reordered buffer, we must not use the
27669 optimization of limiting the search in
27670 previous-single-property-change and
27671 next-single-property-change, because
27672 rows_from_pos_range needs the real start and end
27673 positions to DTRT in this case. That's because
27674 the first row visible in a window does not
27675 necessarily display the character whose position
27676 is the smallest. */
27677 Lisp_Object lim1 =
27678 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27679 ? Fmarker_position (w->start)
27680 : Qnil;
27681 Lisp_Object lim2 =
27682 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27683 ? make_number (BUF_Z (XBUFFER (buffer))
27684 - XFASTINT (w->window_end_pos))
27685 : Qnil;
27686
27687 if (NILP (overlay))
27688 {
27689 /* Handle the text property case. */
27690 before = Fprevious_single_property_change
27691 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27692 after = Fnext_single_property_change
27693 (make_number (pos), Qmouse_face, buffer, lim2);
27694 before_string = after_string = Qnil;
27695 }
27696 else
27697 {
27698 /* Handle the overlay case. */
27699 before = Foverlay_start (overlay);
27700 after = Foverlay_end (overlay);
27701 before_string = Foverlay_get (overlay, Qbefore_string);
27702 after_string = Foverlay_get (overlay, Qafter_string);
27703
27704 if (!STRINGP (before_string)) before_string = Qnil;
27705 if (!STRINGP (after_string)) after_string = Qnil;
27706 }
27707
27708 mouse_face_from_buffer_pos (window, hlinfo, pos,
27709 NILP (before)
27710 ? 1
27711 : XFASTINT (before),
27712 NILP (after)
27713 ? BUF_Z (XBUFFER (buffer))
27714 : XFASTINT (after),
27715 before_string, after_string,
27716 disp_string);
27717 cursor = No_Cursor;
27718 }
27719 }
27720 }
27721
27722 check_help_echo:
27723
27724 /* Look for a `help-echo' property. */
27725 if (NILP (help_echo_string)) {
27726 Lisp_Object help, overlay;
27727
27728 /* Check overlays first. */
27729 help = overlay = Qnil;
27730 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27731 {
27732 overlay = overlay_vec[i];
27733 help = Foverlay_get (overlay, Qhelp_echo);
27734 }
27735
27736 if (!NILP (help))
27737 {
27738 help_echo_string = help;
27739 help_echo_window = window;
27740 help_echo_object = overlay;
27741 help_echo_pos = pos;
27742 }
27743 else
27744 {
27745 Lisp_Object obj = glyph->object;
27746 ptrdiff_t charpos = glyph->charpos;
27747
27748 /* Try text properties. */
27749 if (STRINGP (obj)
27750 && charpos >= 0
27751 && charpos < SCHARS (obj))
27752 {
27753 help = Fget_text_property (make_number (charpos),
27754 Qhelp_echo, obj);
27755 if (NILP (help))
27756 {
27757 /* If the string itself doesn't specify a help-echo,
27758 see if the buffer text ``under'' it does. */
27759 struct glyph_row *r
27760 = MATRIX_ROW (w->current_matrix, vpos);
27761 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27762 ptrdiff_t p = string_buffer_position (obj, start);
27763 if (p > 0)
27764 {
27765 help = Fget_char_property (make_number (p),
27766 Qhelp_echo, w->buffer);
27767 if (!NILP (help))
27768 {
27769 charpos = p;
27770 obj = w->buffer;
27771 }
27772 }
27773 }
27774 }
27775 else if (BUFFERP (obj)
27776 && charpos >= BEGV
27777 && charpos < ZV)
27778 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27779 obj);
27780
27781 if (!NILP (help))
27782 {
27783 help_echo_string = help;
27784 help_echo_window = window;
27785 help_echo_object = obj;
27786 help_echo_pos = charpos;
27787 }
27788 }
27789 }
27790
27791 #ifdef HAVE_WINDOW_SYSTEM
27792 /* Look for a `pointer' property. */
27793 if (FRAME_WINDOW_P (f) && NILP (pointer))
27794 {
27795 /* Check overlays first. */
27796 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27797 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27798
27799 if (NILP (pointer))
27800 {
27801 Lisp_Object obj = glyph->object;
27802 ptrdiff_t charpos = glyph->charpos;
27803
27804 /* Try text properties. */
27805 if (STRINGP (obj)
27806 && charpos >= 0
27807 && charpos < SCHARS (obj))
27808 {
27809 pointer = Fget_text_property (make_number (charpos),
27810 Qpointer, obj);
27811 if (NILP (pointer))
27812 {
27813 /* If the string itself doesn't specify a pointer,
27814 see if the buffer text ``under'' it does. */
27815 struct glyph_row *r
27816 = MATRIX_ROW (w->current_matrix, vpos);
27817 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27818 ptrdiff_t p = string_buffer_position (obj, start);
27819 if (p > 0)
27820 pointer = Fget_char_property (make_number (p),
27821 Qpointer, w->buffer);
27822 }
27823 }
27824 else if (BUFFERP (obj)
27825 && charpos >= BEGV
27826 && charpos < ZV)
27827 pointer = Fget_text_property (make_number (charpos),
27828 Qpointer, obj);
27829 }
27830 }
27831 #endif /* HAVE_WINDOW_SYSTEM */
27832
27833 BEGV = obegv;
27834 ZV = ozv;
27835 current_buffer = obuf;
27836 }
27837
27838 set_cursor:
27839
27840 #ifdef HAVE_WINDOW_SYSTEM
27841 if (FRAME_WINDOW_P (f))
27842 define_frame_cursor1 (f, cursor, pointer);
27843 #else
27844 /* This is here to prevent a compiler error, about "label at end of
27845 compound statement". */
27846 return;
27847 #endif
27848 }
27849
27850
27851 /* EXPORT for RIF:
27852 Clear any mouse-face on window W. This function is part of the
27853 redisplay interface, and is called from try_window_id and similar
27854 functions to ensure the mouse-highlight is off. */
27855
27856 void
27857 x_clear_window_mouse_face (struct window *w)
27858 {
27859 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27860 Lisp_Object window;
27861
27862 BLOCK_INPUT;
27863 XSETWINDOW (window, w);
27864 if (EQ (window, hlinfo->mouse_face_window))
27865 clear_mouse_face (hlinfo);
27866 UNBLOCK_INPUT;
27867 }
27868
27869
27870 /* EXPORT:
27871 Just discard the mouse face information for frame F, if any.
27872 This is used when the size of F is changed. */
27873
27874 void
27875 cancel_mouse_face (struct frame *f)
27876 {
27877 Lisp_Object window;
27878 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27879
27880 window = hlinfo->mouse_face_window;
27881 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27882 {
27883 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27884 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27885 hlinfo->mouse_face_window = Qnil;
27886 }
27887 }
27888
27889
27890 \f
27891 /***********************************************************************
27892 Exposure Events
27893 ***********************************************************************/
27894
27895 #ifdef HAVE_WINDOW_SYSTEM
27896
27897 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27898 which intersects rectangle R. R is in window-relative coordinates. */
27899
27900 static void
27901 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27902 enum glyph_row_area area)
27903 {
27904 struct glyph *first = row->glyphs[area];
27905 struct glyph *end = row->glyphs[area] + row->used[area];
27906 struct glyph *last;
27907 int first_x, start_x, x;
27908
27909 if (area == TEXT_AREA && row->fill_line_p)
27910 /* If row extends face to end of line write the whole line. */
27911 draw_glyphs (w, 0, row, area,
27912 0, row->used[area],
27913 DRAW_NORMAL_TEXT, 0);
27914 else
27915 {
27916 /* Set START_X to the window-relative start position for drawing glyphs of
27917 AREA. The first glyph of the text area can be partially visible.
27918 The first glyphs of other areas cannot. */
27919 start_x = window_box_left_offset (w, area);
27920 x = start_x;
27921 if (area == TEXT_AREA)
27922 x += row->x;
27923
27924 /* Find the first glyph that must be redrawn. */
27925 while (first < end
27926 && x + first->pixel_width < r->x)
27927 {
27928 x += first->pixel_width;
27929 ++first;
27930 }
27931
27932 /* Find the last one. */
27933 last = first;
27934 first_x = x;
27935 while (last < end
27936 && x < r->x + r->width)
27937 {
27938 x += last->pixel_width;
27939 ++last;
27940 }
27941
27942 /* Repaint. */
27943 if (last > first)
27944 draw_glyphs (w, first_x - start_x, row, area,
27945 first - row->glyphs[area], last - row->glyphs[area],
27946 DRAW_NORMAL_TEXT, 0);
27947 }
27948 }
27949
27950
27951 /* Redraw the parts of the glyph row ROW on window W intersecting
27952 rectangle R. R is in window-relative coordinates. Value is
27953 non-zero if mouse-face was overwritten. */
27954
27955 static int
27956 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27957 {
27958 eassert (row->enabled_p);
27959
27960 if (row->mode_line_p || w->pseudo_window_p)
27961 draw_glyphs (w, 0, row, TEXT_AREA,
27962 0, row->used[TEXT_AREA],
27963 DRAW_NORMAL_TEXT, 0);
27964 else
27965 {
27966 if (row->used[LEFT_MARGIN_AREA])
27967 expose_area (w, row, r, LEFT_MARGIN_AREA);
27968 if (row->used[TEXT_AREA])
27969 expose_area (w, row, r, TEXT_AREA);
27970 if (row->used[RIGHT_MARGIN_AREA])
27971 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27972 draw_row_fringe_bitmaps (w, row);
27973 }
27974
27975 return row->mouse_face_p;
27976 }
27977
27978
27979 /* Redraw those parts of glyphs rows during expose event handling that
27980 overlap other rows. Redrawing of an exposed line writes over parts
27981 of lines overlapping that exposed line; this function fixes that.
27982
27983 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27984 row in W's current matrix that is exposed and overlaps other rows.
27985 LAST_OVERLAPPING_ROW is the last such row. */
27986
27987 static void
27988 expose_overlaps (struct window *w,
27989 struct glyph_row *first_overlapping_row,
27990 struct glyph_row *last_overlapping_row,
27991 XRectangle *r)
27992 {
27993 struct glyph_row *row;
27994
27995 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27996 if (row->overlapping_p)
27997 {
27998 eassert (row->enabled_p && !row->mode_line_p);
27999
28000 row->clip = r;
28001 if (row->used[LEFT_MARGIN_AREA])
28002 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28003
28004 if (row->used[TEXT_AREA])
28005 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28006
28007 if (row->used[RIGHT_MARGIN_AREA])
28008 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28009 row->clip = NULL;
28010 }
28011 }
28012
28013
28014 /* Return non-zero if W's cursor intersects rectangle R. */
28015
28016 static int
28017 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28018 {
28019 XRectangle cr, result;
28020 struct glyph *cursor_glyph;
28021 struct glyph_row *row;
28022
28023 if (w->phys_cursor.vpos >= 0
28024 && w->phys_cursor.vpos < w->current_matrix->nrows
28025 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28026 row->enabled_p)
28027 && row->cursor_in_fringe_p)
28028 {
28029 /* Cursor is in the fringe. */
28030 cr.x = window_box_right_offset (w,
28031 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28032 ? RIGHT_MARGIN_AREA
28033 : TEXT_AREA));
28034 cr.y = row->y;
28035 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28036 cr.height = row->height;
28037 return x_intersect_rectangles (&cr, r, &result);
28038 }
28039
28040 cursor_glyph = get_phys_cursor_glyph (w);
28041 if (cursor_glyph)
28042 {
28043 /* r is relative to W's box, but w->phys_cursor.x is relative
28044 to left edge of W's TEXT area. Adjust it. */
28045 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28046 cr.y = w->phys_cursor.y;
28047 cr.width = cursor_glyph->pixel_width;
28048 cr.height = w->phys_cursor_height;
28049 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28050 I assume the effect is the same -- and this is portable. */
28051 return x_intersect_rectangles (&cr, r, &result);
28052 }
28053 /* If we don't understand the format, pretend we're not in the hot-spot. */
28054 return 0;
28055 }
28056
28057
28058 /* EXPORT:
28059 Draw a vertical window border to the right of window W if W doesn't
28060 have vertical scroll bars. */
28061
28062 void
28063 x_draw_vertical_border (struct window *w)
28064 {
28065 struct frame *f = XFRAME (WINDOW_FRAME (w));
28066
28067 /* We could do better, if we knew what type of scroll-bar the adjacent
28068 windows (on either side) have... But we don't :-(
28069 However, I think this works ok. ++KFS 2003-04-25 */
28070
28071 /* Redraw borders between horizontally adjacent windows. Don't
28072 do it for frames with vertical scroll bars because either the
28073 right scroll bar of a window, or the left scroll bar of its
28074 neighbor will suffice as a border. */
28075 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28076 return;
28077
28078 if (!WINDOW_RIGHTMOST_P (w)
28079 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28080 {
28081 int x0, x1, y0, y1;
28082
28083 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28084 y1 -= 1;
28085
28086 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28087 x1 -= 1;
28088
28089 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28090 }
28091 else if (!WINDOW_LEFTMOST_P (w)
28092 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28093 {
28094 int x0, x1, y0, y1;
28095
28096 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28097 y1 -= 1;
28098
28099 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28100 x0 -= 1;
28101
28102 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28103 }
28104 }
28105
28106
28107 /* Redraw the part of window W intersection rectangle FR. Pixel
28108 coordinates in FR are frame-relative. Call this function with
28109 input blocked. Value is non-zero if the exposure overwrites
28110 mouse-face. */
28111
28112 static int
28113 expose_window (struct window *w, XRectangle *fr)
28114 {
28115 struct frame *f = XFRAME (w->frame);
28116 XRectangle wr, r;
28117 int mouse_face_overwritten_p = 0;
28118
28119 /* If window is not yet fully initialized, do nothing. This can
28120 happen when toolkit scroll bars are used and a window is split.
28121 Reconfiguring the scroll bar will generate an expose for a newly
28122 created window. */
28123 if (w->current_matrix == NULL)
28124 return 0;
28125
28126 /* When we're currently updating the window, display and current
28127 matrix usually don't agree. Arrange for a thorough display
28128 later. */
28129 if (w == updated_window)
28130 {
28131 SET_FRAME_GARBAGED (f);
28132 return 0;
28133 }
28134
28135 /* Frame-relative pixel rectangle of W. */
28136 wr.x = WINDOW_LEFT_EDGE_X (w);
28137 wr.y = WINDOW_TOP_EDGE_Y (w);
28138 wr.width = WINDOW_TOTAL_WIDTH (w);
28139 wr.height = WINDOW_TOTAL_HEIGHT (w);
28140
28141 if (x_intersect_rectangles (fr, &wr, &r))
28142 {
28143 int yb = window_text_bottom_y (w);
28144 struct glyph_row *row;
28145 int cursor_cleared_p, phys_cursor_on_p;
28146 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28147
28148 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28149 r.x, r.y, r.width, r.height));
28150
28151 /* Convert to window coordinates. */
28152 r.x -= WINDOW_LEFT_EDGE_X (w);
28153 r.y -= WINDOW_TOP_EDGE_Y (w);
28154
28155 /* Turn off the cursor. */
28156 if (!w->pseudo_window_p
28157 && phys_cursor_in_rect_p (w, &r))
28158 {
28159 x_clear_cursor (w);
28160 cursor_cleared_p = 1;
28161 }
28162 else
28163 cursor_cleared_p = 0;
28164
28165 /* If the row containing the cursor extends face to end of line,
28166 then expose_area might overwrite the cursor outside the
28167 rectangle and thus notice_overwritten_cursor might clear
28168 w->phys_cursor_on_p. We remember the original value and
28169 check later if it is changed. */
28170 phys_cursor_on_p = w->phys_cursor_on_p;
28171
28172 /* Update lines intersecting rectangle R. */
28173 first_overlapping_row = last_overlapping_row = NULL;
28174 for (row = w->current_matrix->rows;
28175 row->enabled_p;
28176 ++row)
28177 {
28178 int y0 = row->y;
28179 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28180
28181 if ((y0 >= r.y && y0 < r.y + r.height)
28182 || (y1 > r.y && y1 < r.y + r.height)
28183 || (r.y >= y0 && r.y < y1)
28184 || (r.y + r.height > y0 && r.y + r.height < y1))
28185 {
28186 /* A header line may be overlapping, but there is no need
28187 to fix overlapping areas for them. KFS 2005-02-12 */
28188 if (row->overlapping_p && !row->mode_line_p)
28189 {
28190 if (first_overlapping_row == NULL)
28191 first_overlapping_row = row;
28192 last_overlapping_row = row;
28193 }
28194
28195 row->clip = fr;
28196 if (expose_line (w, row, &r))
28197 mouse_face_overwritten_p = 1;
28198 row->clip = NULL;
28199 }
28200 else if (row->overlapping_p)
28201 {
28202 /* We must redraw a row overlapping the exposed area. */
28203 if (y0 < r.y
28204 ? y0 + row->phys_height > r.y
28205 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28206 {
28207 if (first_overlapping_row == NULL)
28208 first_overlapping_row = row;
28209 last_overlapping_row = row;
28210 }
28211 }
28212
28213 if (y1 >= yb)
28214 break;
28215 }
28216
28217 /* Display the mode line if there is one. */
28218 if (WINDOW_WANTS_MODELINE_P (w)
28219 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28220 row->enabled_p)
28221 && row->y < r.y + r.height)
28222 {
28223 if (expose_line (w, row, &r))
28224 mouse_face_overwritten_p = 1;
28225 }
28226
28227 if (!w->pseudo_window_p)
28228 {
28229 /* Fix the display of overlapping rows. */
28230 if (first_overlapping_row)
28231 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28232 fr);
28233
28234 /* Draw border between windows. */
28235 x_draw_vertical_border (w);
28236
28237 /* Turn the cursor on again. */
28238 if (cursor_cleared_p
28239 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28240 update_window_cursor (w, 1);
28241 }
28242 }
28243
28244 return mouse_face_overwritten_p;
28245 }
28246
28247
28248
28249 /* Redraw (parts) of all windows in the window tree rooted at W that
28250 intersect R. R contains frame pixel coordinates. Value is
28251 non-zero if the exposure overwrites mouse-face. */
28252
28253 static int
28254 expose_window_tree (struct window *w, XRectangle *r)
28255 {
28256 struct frame *f = XFRAME (w->frame);
28257 int mouse_face_overwritten_p = 0;
28258
28259 while (w && !FRAME_GARBAGED_P (f))
28260 {
28261 if (!NILP (w->hchild))
28262 mouse_face_overwritten_p
28263 |= expose_window_tree (XWINDOW (w->hchild), r);
28264 else if (!NILP (w->vchild))
28265 mouse_face_overwritten_p
28266 |= expose_window_tree (XWINDOW (w->vchild), r);
28267 else
28268 mouse_face_overwritten_p |= expose_window (w, r);
28269
28270 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28271 }
28272
28273 return mouse_face_overwritten_p;
28274 }
28275
28276
28277 /* EXPORT:
28278 Redisplay an exposed area of frame F. X and Y are the upper-left
28279 corner of the exposed rectangle. W and H are width and height of
28280 the exposed area. All are pixel values. W or H zero means redraw
28281 the entire frame. */
28282
28283 void
28284 expose_frame (struct frame *f, int x, int y, int w, int h)
28285 {
28286 XRectangle r;
28287 int mouse_face_overwritten_p = 0;
28288
28289 TRACE ((stderr, "expose_frame "));
28290
28291 /* No need to redraw if frame will be redrawn soon. */
28292 if (FRAME_GARBAGED_P (f))
28293 {
28294 TRACE ((stderr, " garbaged\n"));
28295 return;
28296 }
28297
28298 /* If basic faces haven't been realized yet, there is no point in
28299 trying to redraw anything. This can happen when we get an expose
28300 event while Emacs is starting, e.g. by moving another window. */
28301 if (FRAME_FACE_CACHE (f) == NULL
28302 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28303 {
28304 TRACE ((stderr, " no faces\n"));
28305 return;
28306 }
28307
28308 if (w == 0 || h == 0)
28309 {
28310 r.x = r.y = 0;
28311 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28312 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28313 }
28314 else
28315 {
28316 r.x = x;
28317 r.y = y;
28318 r.width = w;
28319 r.height = h;
28320 }
28321
28322 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28323 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28324
28325 if (WINDOWP (f->tool_bar_window))
28326 mouse_face_overwritten_p
28327 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28328
28329 #ifdef HAVE_X_WINDOWS
28330 #ifndef MSDOS
28331 #ifndef USE_X_TOOLKIT
28332 if (WINDOWP (f->menu_bar_window))
28333 mouse_face_overwritten_p
28334 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28335 #endif /* not USE_X_TOOLKIT */
28336 #endif
28337 #endif
28338
28339 /* Some window managers support a focus-follows-mouse style with
28340 delayed raising of frames. Imagine a partially obscured frame,
28341 and moving the mouse into partially obscured mouse-face on that
28342 frame. The visible part of the mouse-face will be highlighted,
28343 then the WM raises the obscured frame. With at least one WM, KDE
28344 2.1, Emacs is not getting any event for the raising of the frame
28345 (even tried with SubstructureRedirectMask), only Expose events.
28346 These expose events will draw text normally, i.e. not
28347 highlighted. Which means we must redo the highlight here.
28348 Subsume it under ``we love X''. --gerd 2001-08-15 */
28349 /* Included in Windows version because Windows most likely does not
28350 do the right thing if any third party tool offers
28351 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28352 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28353 {
28354 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28355 if (f == hlinfo->mouse_face_mouse_frame)
28356 {
28357 int mouse_x = hlinfo->mouse_face_mouse_x;
28358 int mouse_y = hlinfo->mouse_face_mouse_y;
28359 clear_mouse_face (hlinfo);
28360 note_mouse_highlight (f, mouse_x, mouse_y);
28361 }
28362 }
28363 }
28364
28365
28366 /* EXPORT:
28367 Determine the intersection of two rectangles R1 and R2. Return
28368 the intersection in *RESULT. Value is non-zero if RESULT is not
28369 empty. */
28370
28371 int
28372 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28373 {
28374 XRectangle *left, *right;
28375 XRectangle *upper, *lower;
28376 int intersection_p = 0;
28377
28378 /* Rearrange so that R1 is the left-most rectangle. */
28379 if (r1->x < r2->x)
28380 left = r1, right = r2;
28381 else
28382 left = r2, right = r1;
28383
28384 /* X0 of the intersection is right.x0, if this is inside R1,
28385 otherwise there is no intersection. */
28386 if (right->x <= left->x + left->width)
28387 {
28388 result->x = right->x;
28389
28390 /* The right end of the intersection is the minimum of
28391 the right ends of left and right. */
28392 result->width = (min (left->x + left->width, right->x + right->width)
28393 - result->x);
28394
28395 /* Same game for Y. */
28396 if (r1->y < r2->y)
28397 upper = r1, lower = r2;
28398 else
28399 upper = r2, lower = r1;
28400
28401 /* The upper end of the intersection is lower.y0, if this is inside
28402 of upper. Otherwise, there is no intersection. */
28403 if (lower->y <= upper->y + upper->height)
28404 {
28405 result->y = lower->y;
28406
28407 /* The lower end of the intersection is the minimum of the lower
28408 ends of upper and lower. */
28409 result->height = (min (lower->y + lower->height,
28410 upper->y + upper->height)
28411 - result->y);
28412 intersection_p = 1;
28413 }
28414 }
28415
28416 return intersection_p;
28417 }
28418
28419 #endif /* HAVE_WINDOW_SYSTEM */
28420
28421 \f
28422 /***********************************************************************
28423 Initialization
28424 ***********************************************************************/
28425
28426 void
28427 syms_of_xdisp (void)
28428 {
28429 Vwith_echo_area_save_vector = Qnil;
28430 staticpro (&Vwith_echo_area_save_vector);
28431
28432 Vmessage_stack = Qnil;
28433 staticpro (&Vmessage_stack);
28434
28435 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28436
28437 message_dolog_marker1 = Fmake_marker ();
28438 staticpro (&message_dolog_marker1);
28439 message_dolog_marker2 = Fmake_marker ();
28440 staticpro (&message_dolog_marker2);
28441 message_dolog_marker3 = Fmake_marker ();
28442 staticpro (&message_dolog_marker3);
28443
28444 #ifdef GLYPH_DEBUG
28445 defsubr (&Sdump_frame_glyph_matrix);
28446 defsubr (&Sdump_glyph_matrix);
28447 defsubr (&Sdump_glyph_row);
28448 defsubr (&Sdump_tool_bar_row);
28449 defsubr (&Strace_redisplay);
28450 defsubr (&Strace_to_stderr);
28451 #endif
28452 #ifdef HAVE_WINDOW_SYSTEM
28453 defsubr (&Stool_bar_lines_needed);
28454 defsubr (&Slookup_image_map);
28455 #endif
28456 defsubr (&Sformat_mode_line);
28457 defsubr (&Sinvisible_p);
28458 defsubr (&Scurrent_bidi_paragraph_direction);
28459
28460 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28461 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28462 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28463 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28464 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28465 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28466 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28467 DEFSYM (Qeval, "eval");
28468 DEFSYM (QCdata, ":data");
28469 DEFSYM (Qdisplay, "display");
28470 DEFSYM (Qspace_width, "space-width");
28471 DEFSYM (Qraise, "raise");
28472 DEFSYM (Qslice, "slice");
28473 DEFSYM (Qspace, "space");
28474 DEFSYM (Qmargin, "margin");
28475 DEFSYM (Qpointer, "pointer");
28476 DEFSYM (Qleft_margin, "left-margin");
28477 DEFSYM (Qright_margin, "right-margin");
28478 DEFSYM (Qcenter, "center");
28479 DEFSYM (Qline_height, "line-height");
28480 DEFSYM (QCalign_to, ":align-to");
28481 DEFSYM (QCrelative_width, ":relative-width");
28482 DEFSYM (QCrelative_height, ":relative-height");
28483 DEFSYM (QCeval, ":eval");
28484 DEFSYM (QCpropertize, ":propertize");
28485 DEFSYM (QCfile, ":file");
28486 DEFSYM (Qfontified, "fontified");
28487 DEFSYM (Qfontification_functions, "fontification-functions");
28488 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28489 DEFSYM (Qescape_glyph, "escape-glyph");
28490 DEFSYM (Qnobreak_space, "nobreak-space");
28491 DEFSYM (Qimage, "image");
28492 DEFSYM (Qtext, "text");
28493 DEFSYM (Qboth, "both");
28494 DEFSYM (Qboth_horiz, "both-horiz");
28495 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28496 DEFSYM (QCmap, ":map");
28497 DEFSYM (QCpointer, ":pointer");
28498 DEFSYM (Qrect, "rect");
28499 DEFSYM (Qcircle, "circle");
28500 DEFSYM (Qpoly, "poly");
28501 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28502 DEFSYM (Qgrow_only, "grow-only");
28503 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28504 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28505 DEFSYM (Qposition, "position");
28506 DEFSYM (Qbuffer_position, "buffer-position");
28507 DEFSYM (Qobject, "object");
28508 DEFSYM (Qbar, "bar");
28509 DEFSYM (Qhbar, "hbar");
28510 DEFSYM (Qbox, "box");
28511 DEFSYM (Qhollow, "hollow");
28512 DEFSYM (Qhand, "hand");
28513 DEFSYM (Qarrow, "arrow");
28514 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28515
28516 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28517 Fcons (intern_c_string ("void-variable"), Qnil)),
28518 Qnil);
28519 staticpro (&list_of_error);
28520
28521 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28522 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28523 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28524 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28525
28526 echo_buffer[0] = echo_buffer[1] = Qnil;
28527 staticpro (&echo_buffer[0]);
28528 staticpro (&echo_buffer[1]);
28529
28530 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28531 staticpro (&echo_area_buffer[0]);
28532 staticpro (&echo_area_buffer[1]);
28533
28534 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28535 staticpro (&Vmessages_buffer_name);
28536
28537 mode_line_proptrans_alist = Qnil;
28538 staticpro (&mode_line_proptrans_alist);
28539 mode_line_string_list = Qnil;
28540 staticpro (&mode_line_string_list);
28541 mode_line_string_face = Qnil;
28542 staticpro (&mode_line_string_face);
28543 mode_line_string_face_prop = Qnil;
28544 staticpro (&mode_line_string_face_prop);
28545 Vmode_line_unwind_vector = Qnil;
28546 staticpro (&Vmode_line_unwind_vector);
28547
28548 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28549
28550 help_echo_string = Qnil;
28551 staticpro (&help_echo_string);
28552 help_echo_object = Qnil;
28553 staticpro (&help_echo_object);
28554 help_echo_window = Qnil;
28555 staticpro (&help_echo_window);
28556 previous_help_echo_string = Qnil;
28557 staticpro (&previous_help_echo_string);
28558 help_echo_pos = -1;
28559
28560 DEFSYM (Qright_to_left, "right-to-left");
28561 DEFSYM (Qleft_to_right, "left-to-right");
28562
28563 #ifdef HAVE_WINDOW_SYSTEM
28564 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28565 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28566 For example, if a block cursor is over a tab, it will be drawn as
28567 wide as that tab on the display. */);
28568 x_stretch_cursor_p = 0;
28569 #endif
28570
28571 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28572 doc: /* Non-nil means highlight trailing whitespace.
28573 The face used for trailing whitespace is `trailing-whitespace'. */);
28574 Vshow_trailing_whitespace = Qnil;
28575
28576 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28577 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28578 If the value is t, Emacs highlights non-ASCII chars which have the
28579 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28580 or `escape-glyph' face respectively.
28581
28582 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28583 U+2011 (non-breaking hyphen) are affected.
28584
28585 Any other non-nil value means to display these characters as a escape
28586 glyph followed by an ordinary space or hyphen.
28587
28588 A value of nil means no special handling of these characters. */);
28589 Vnobreak_char_display = Qt;
28590
28591 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28592 doc: /* The pointer shape to show in void text areas.
28593 A value of nil means to show the text pointer. Other options are `arrow',
28594 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28595 Vvoid_text_area_pointer = Qarrow;
28596
28597 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28598 doc: /* Non-nil means don't actually do any redisplay.
28599 This is used for internal purposes. */);
28600 Vinhibit_redisplay = Qnil;
28601
28602 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28603 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28604 Vglobal_mode_string = Qnil;
28605
28606 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28607 doc: /* Marker for where to display an arrow on top of the buffer text.
28608 This must be the beginning of a line in order to work.
28609 See also `overlay-arrow-string'. */);
28610 Voverlay_arrow_position = Qnil;
28611
28612 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28613 doc: /* String to display as an arrow in non-window frames.
28614 See also `overlay-arrow-position'. */);
28615 Voverlay_arrow_string = make_pure_c_string ("=>");
28616
28617 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28618 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28619 The symbols on this list are examined during redisplay to determine
28620 where to display overlay arrows. */);
28621 Voverlay_arrow_variable_list
28622 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28623
28624 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28625 doc: /* The number of lines to try scrolling a window by when point moves out.
28626 If that fails to bring point back on frame, point is centered instead.
28627 If this is zero, point is always centered after it moves off frame.
28628 If you want scrolling to always be a line at a time, you should set
28629 `scroll-conservatively' to a large value rather than set this to 1. */);
28630
28631 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28632 doc: /* Scroll up to this many lines, to bring point back on screen.
28633 If point moves off-screen, redisplay will scroll by up to
28634 `scroll-conservatively' lines in order to bring point just barely
28635 onto the screen again. If that cannot be done, then redisplay
28636 recenters point as usual.
28637
28638 If the value is greater than 100, redisplay will never recenter point,
28639 but will always scroll just enough text to bring point into view, even
28640 if you move far away.
28641
28642 A value of zero means always recenter point if it moves off screen. */);
28643 scroll_conservatively = 0;
28644
28645 DEFVAR_INT ("scroll-margin", scroll_margin,
28646 doc: /* Number of lines of margin at the top and bottom of a window.
28647 Recenter the window whenever point gets within this many lines
28648 of the top or bottom of the window. */);
28649 scroll_margin = 0;
28650
28651 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28652 doc: /* Pixels per inch value for non-window system displays.
28653 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28654 Vdisplay_pixels_per_inch = make_float (72.0);
28655
28656 #ifdef GLYPH_DEBUG
28657 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28658 #endif
28659
28660 DEFVAR_LISP ("truncate-partial-width-windows",
28661 Vtruncate_partial_width_windows,
28662 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28663 For an integer value, truncate lines in each window narrower than the
28664 full frame width, provided the window width is less than that integer;
28665 otherwise, respect the value of `truncate-lines'.
28666
28667 For any other non-nil value, truncate lines in all windows that do
28668 not span the full frame width.
28669
28670 A value of nil means to respect the value of `truncate-lines'.
28671
28672 If `word-wrap' is enabled, you might want to reduce this. */);
28673 Vtruncate_partial_width_windows = make_number (50);
28674
28675 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28676 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28677 Any other value means to use the appropriate face, `mode-line',
28678 `header-line', or `menu' respectively. */);
28679 mode_line_inverse_video = 1;
28680
28681 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28682 doc: /* Maximum buffer size for which line number should be displayed.
28683 If the buffer is bigger than this, the line number does not appear
28684 in the mode line. A value of nil means no limit. */);
28685 Vline_number_display_limit = Qnil;
28686
28687 DEFVAR_INT ("line-number-display-limit-width",
28688 line_number_display_limit_width,
28689 doc: /* Maximum line width (in characters) for line number display.
28690 If the average length of the lines near point is bigger than this, then the
28691 line number may be omitted from the mode line. */);
28692 line_number_display_limit_width = 200;
28693
28694 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28695 doc: /* Non-nil means highlight region even in nonselected windows. */);
28696 highlight_nonselected_windows = 0;
28697
28698 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28699 doc: /* Non-nil if more than one frame is visible on this display.
28700 Minibuffer-only frames don't count, but iconified frames do.
28701 This variable is not guaranteed to be accurate except while processing
28702 `frame-title-format' and `icon-title-format'. */);
28703
28704 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28705 doc: /* Template for displaying the title bar of visible frames.
28706 \(Assuming the window manager supports this feature.)
28707
28708 This variable has the same structure as `mode-line-format', except that
28709 the %c and %l constructs are ignored. It is used only on frames for
28710 which no explicit name has been set \(see `modify-frame-parameters'). */);
28711
28712 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28713 doc: /* Template for displaying the title bar of an iconified frame.
28714 \(Assuming the window manager supports this feature.)
28715 This variable has the same structure as `mode-line-format' (which see),
28716 and is used only on frames for which no explicit name has been set
28717 \(see `modify-frame-parameters'). */);
28718 Vicon_title_format
28719 = Vframe_title_format
28720 = pure_cons (intern_c_string ("multiple-frames"),
28721 pure_cons (make_pure_c_string ("%b"),
28722 pure_cons (pure_cons (empty_unibyte_string,
28723 pure_cons (intern_c_string ("invocation-name"),
28724 pure_cons (make_pure_c_string ("@"),
28725 pure_cons (intern_c_string ("system-name"),
28726 Qnil)))),
28727 Qnil)));
28728
28729 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28730 doc: /* Maximum number of lines to keep in the message log buffer.
28731 If nil, disable message logging. If t, log messages but don't truncate
28732 the buffer when it becomes large. */);
28733 Vmessage_log_max = make_number (100);
28734
28735 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28736 doc: /* Functions called before redisplay, if window sizes have changed.
28737 The value should be a list of functions that take one argument.
28738 Just before redisplay, for each frame, if any of its windows have changed
28739 size since the last redisplay, or have been split or deleted,
28740 all the functions in the list are called, with the frame as argument. */);
28741 Vwindow_size_change_functions = Qnil;
28742
28743 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28744 doc: /* List of functions to call before redisplaying a window with scrolling.
28745 Each function is called with two arguments, the window and its new
28746 display-start position. Note that these functions are also called by
28747 `set-window-buffer'. Also note that the value of `window-end' is not
28748 valid when these functions are called.
28749
28750 Warning: Do not use this feature to alter the way the window
28751 is scrolled. It is not designed for that, and such use probably won't
28752 work. */);
28753 Vwindow_scroll_functions = Qnil;
28754
28755 DEFVAR_LISP ("window-text-change-functions",
28756 Vwindow_text_change_functions,
28757 doc: /* Functions to call in redisplay when text in the window might change. */);
28758 Vwindow_text_change_functions = Qnil;
28759
28760 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28761 doc: /* Functions called when redisplay of a window reaches the end trigger.
28762 Each function is called with two arguments, the window and the end trigger value.
28763 See `set-window-redisplay-end-trigger'. */);
28764 Vredisplay_end_trigger_functions = Qnil;
28765
28766 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28767 doc: /* Non-nil means autoselect window with mouse pointer.
28768 If nil, do not autoselect windows.
28769 A positive number means delay autoselection by that many seconds: a
28770 window is autoselected only after the mouse has remained in that
28771 window for the duration of the delay.
28772 A negative number has a similar effect, but causes windows to be
28773 autoselected only after the mouse has stopped moving. \(Because of
28774 the way Emacs compares mouse events, you will occasionally wait twice
28775 that time before the window gets selected.\)
28776 Any other value means to autoselect window instantaneously when the
28777 mouse pointer enters it.
28778
28779 Autoselection selects the minibuffer only if it is active, and never
28780 unselects the minibuffer if it is active.
28781
28782 When customizing this variable make sure that the actual value of
28783 `focus-follows-mouse' matches the behavior of your window manager. */);
28784 Vmouse_autoselect_window = Qnil;
28785
28786 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28787 doc: /* Non-nil means automatically resize tool-bars.
28788 This dynamically changes the tool-bar's height to the minimum height
28789 that is needed to make all tool-bar items visible.
28790 If value is `grow-only', the tool-bar's height is only increased
28791 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28792 Vauto_resize_tool_bars = Qt;
28793
28794 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28795 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28796 auto_raise_tool_bar_buttons_p = 1;
28797
28798 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28799 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28800 make_cursor_line_fully_visible_p = 1;
28801
28802 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28803 doc: /* Border below tool-bar in pixels.
28804 If an integer, use it as the height of the border.
28805 If it is one of `internal-border-width' or `border-width', use the
28806 value of the corresponding frame parameter.
28807 Otherwise, no border is added below the tool-bar. */);
28808 Vtool_bar_border = Qinternal_border_width;
28809
28810 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28811 doc: /* Margin around tool-bar buttons in pixels.
28812 If an integer, use that for both horizontal and vertical margins.
28813 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28814 HORZ specifying the horizontal margin, and VERT specifying the
28815 vertical margin. */);
28816 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28817
28818 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28819 doc: /* Relief thickness of tool-bar buttons. */);
28820 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28821
28822 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28823 doc: /* Tool bar style to use.
28824 It can be one of
28825 image - show images only
28826 text - show text only
28827 both - show both, text below image
28828 both-horiz - show text to the right of the image
28829 text-image-horiz - show text to the left of the image
28830 any other - use system default or image if no system default.
28831
28832 This variable only affects the GTK+ toolkit version of Emacs. */);
28833 Vtool_bar_style = Qnil;
28834
28835 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28836 doc: /* Maximum number of characters a label can have to be shown.
28837 The tool bar style must also show labels for this to have any effect, see
28838 `tool-bar-style'. */);
28839 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28840
28841 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28842 doc: /* List of functions to call to fontify regions of text.
28843 Each function is called with one argument POS. Functions must
28844 fontify a region starting at POS in the current buffer, and give
28845 fontified regions the property `fontified'. */);
28846 Vfontification_functions = Qnil;
28847 Fmake_variable_buffer_local (Qfontification_functions);
28848
28849 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28850 unibyte_display_via_language_environment,
28851 doc: /* Non-nil means display unibyte text according to language environment.
28852 Specifically, this means that raw bytes in the range 160-255 decimal
28853 are displayed by converting them to the equivalent multibyte characters
28854 according to the current language environment. As a result, they are
28855 displayed according to the current fontset.
28856
28857 Note that this variable affects only how these bytes are displayed,
28858 but does not change the fact they are interpreted as raw bytes. */);
28859 unibyte_display_via_language_environment = 0;
28860
28861 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28862 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
28863 If a float, it specifies a fraction of the mini-window frame's height.
28864 If an integer, it specifies a number of lines. */);
28865 Vmax_mini_window_height = make_float (0.25);
28866
28867 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28868 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28869 A value of nil means don't automatically resize mini-windows.
28870 A value of t means resize them to fit the text displayed in them.
28871 A value of `grow-only', the default, means let mini-windows grow only;
28872 they return to their normal size when the minibuffer is closed, or the
28873 echo area becomes empty. */);
28874 Vresize_mini_windows = Qgrow_only;
28875
28876 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28877 doc: /* Alist specifying how to blink the cursor off.
28878 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28879 `cursor-type' frame-parameter or variable equals ON-STATE,
28880 comparing using `equal', Emacs uses OFF-STATE to specify
28881 how to blink it off. ON-STATE and OFF-STATE are values for
28882 the `cursor-type' frame parameter.
28883
28884 If a frame's ON-STATE has no entry in this list,
28885 the frame's other specifications determine how to blink the cursor off. */);
28886 Vblink_cursor_alist = Qnil;
28887
28888 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28889 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28890 If non-nil, windows are automatically scrolled horizontally to make
28891 point visible. */);
28892 automatic_hscrolling_p = 1;
28893 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28894
28895 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28896 doc: /* How many columns away from the window edge point is allowed to get
28897 before automatic hscrolling will horizontally scroll the window. */);
28898 hscroll_margin = 5;
28899
28900 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28901 doc: /* How many columns to scroll the window when point gets too close to the edge.
28902 When point is less than `hscroll-margin' columns from the window
28903 edge, automatic hscrolling will scroll the window by the amount of columns
28904 determined by this variable. If its value is a positive integer, scroll that
28905 many columns. If it's a positive floating-point number, it specifies the
28906 fraction of the window's width to scroll. If it's nil or zero, point will be
28907 centered horizontally after the scroll. Any other value, including negative
28908 numbers, are treated as if the value were zero.
28909
28910 Automatic hscrolling always moves point outside the scroll margin, so if
28911 point was more than scroll step columns inside the margin, the window will
28912 scroll more than the value given by the scroll step.
28913
28914 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28915 and `scroll-right' overrides this variable's effect. */);
28916 Vhscroll_step = make_number (0);
28917
28918 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28919 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28920 Bind this around calls to `message' to let it take effect. */);
28921 message_truncate_lines = 0;
28922
28923 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28924 doc: /* Normal hook run to update the menu bar definitions.
28925 Redisplay runs this hook before it redisplays the menu bar.
28926 This is used to update submenus such as Buffers,
28927 whose contents depend on various data. */);
28928 Vmenu_bar_update_hook = Qnil;
28929
28930 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28931 doc: /* Frame for which we are updating a menu.
28932 The enable predicate for a menu binding should check this variable. */);
28933 Vmenu_updating_frame = Qnil;
28934
28935 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28936 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28937 inhibit_menubar_update = 0;
28938
28939 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28940 doc: /* Prefix prepended to all continuation lines at display time.
28941 The value may be a string, an image, or a stretch-glyph; it is
28942 interpreted in the same way as the value of a `display' text property.
28943
28944 This variable is overridden by any `wrap-prefix' text or overlay
28945 property.
28946
28947 To add a prefix to non-continuation lines, use `line-prefix'. */);
28948 Vwrap_prefix = Qnil;
28949 DEFSYM (Qwrap_prefix, "wrap-prefix");
28950 Fmake_variable_buffer_local (Qwrap_prefix);
28951
28952 DEFVAR_LISP ("line-prefix", Vline_prefix,
28953 doc: /* Prefix prepended to all non-continuation lines at display time.
28954 The value may be a string, an image, or a stretch-glyph; it is
28955 interpreted in the same way as the value of a `display' text property.
28956
28957 This variable is overridden by any `line-prefix' text or overlay
28958 property.
28959
28960 To add a prefix to continuation lines, use `wrap-prefix'. */);
28961 Vline_prefix = Qnil;
28962 DEFSYM (Qline_prefix, "line-prefix");
28963 Fmake_variable_buffer_local (Qline_prefix);
28964
28965 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28966 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28967 inhibit_eval_during_redisplay = 0;
28968
28969 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28970 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28971 inhibit_free_realized_faces = 0;
28972
28973 #ifdef GLYPH_DEBUG
28974 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28975 doc: /* Inhibit try_window_id display optimization. */);
28976 inhibit_try_window_id = 0;
28977
28978 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28979 doc: /* Inhibit try_window_reusing display optimization. */);
28980 inhibit_try_window_reusing = 0;
28981
28982 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28983 doc: /* Inhibit try_cursor_movement display optimization. */);
28984 inhibit_try_cursor_movement = 0;
28985 #endif /* GLYPH_DEBUG */
28986
28987 DEFVAR_INT ("overline-margin", overline_margin,
28988 doc: /* Space between overline and text, in pixels.
28989 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28990 margin to the character height. */);
28991 overline_margin = 2;
28992
28993 DEFVAR_INT ("underline-minimum-offset",
28994 underline_minimum_offset,
28995 doc: /* Minimum distance between baseline and underline.
28996 This can improve legibility of underlined text at small font sizes,
28997 particularly when using variable `x-use-underline-position-properties'
28998 with fonts that specify an UNDERLINE_POSITION relatively close to the
28999 baseline. The default value is 1. */);
29000 underline_minimum_offset = 1;
29001
29002 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29003 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29004 This feature only works when on a window system that can change
29005 cursor shapes. */);
29006 display_hourglass_p = 1;
29007
29008 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29009 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29010 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29011
29012 hourglass_atimer = NULL;
29013 hourglass_shown_p = 0;
29014
29015 DEFSYM (Qglyphless_char, "glyphless-char");
29016 DEFSYM (Qhex_code, "hex-code");
29017 DEFSYM (Qempty_box, "empty-box");
29018 DEFSYM (Qthin_space, "thin-space");
29019 DEFSYM (Qzero_width, "zero-width");
29020
29021 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29022 /* Intern this now in case it isn't already done.
29023 Setting this variable twice is harmless.
29024 But don't staticpro it here--that is done in alloc.c. */
29025 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29026 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29027
29028 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29029 doc: /* Char-table defining glyphless characters.
29030 Each element, if non-nil, should be one of the following:
29031 an ASCII acronym string: display this string in a box
29032 `hex-code': display the hexadecimal code of a character in a box
29033 `empty-box': display as an empty box
29034 `thin-space': display as 1-pixel width space
29035 `zero-width': don't display
29036 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29037 display method for graphical terminals and text terminals respectively.
29038 GRAPHICAL and TEXT should each have one of the values listed above.
29039
29040 The char-table has one extra slot to control the display of a character for
29041 which no font is found. This slot only takes effect on graphical terminals.
29042 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29043 `thin-space'. The default is `empty-box'. */);
29044 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29045 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29046 Qempty_box);
29047 }
29048
29049
29050 /* Initialize this module when Emacs starts. */
29051
29052 void
29053 init_xdisp (void)
29054 {
29055 current_header_line_height = current_mode_line_height = -1;
29056
29057 CHARPOS (this_line_start_pos) = 0;
29058
29059 if (!noninteractive)
29060 {
29061 struct window *m = XWINDOW (minibuf_window);
29062 Lisp_Object frame = m->frame;
29063 struct frame *f = XFRAME (frame);
29064 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29065 struct window *r = XWINDOW (root);
29066 int i;
29067
29068 echo_area_window = minibuf_window;
29069
29070 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
29071 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29072 XSETFASTINT (r->total_cols, FRAME_COLS (f));
29073 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
29074 XSETFASTINT (m->total_lines, 1);
29075 XSETFASTINT (m->total_cols, FRAME_COLS (f));
29076
29077 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29078 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29079 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29080
29081 /* The default ellipsis glyphs `...'. */
29082 for (i = 0; i < 3; ++i)
29083 default_invis_vector[i] = make_number ('.');
29084 }
29085
29086 {
29087 /* Allocate the buffer for frame titles.
29088 Also used for `format-mode-line'. */
29089 int size = 100;
29090 mode_line_noprop_buf = xmalloc (size);
29091 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29092 mode_line_noprop_ptr = mode_line_noprop_buf;
29093 mode_line_target = MODE_LINE_DISPLAY;
29094 }
29095
29096 help_echo_showing_p = 0;
29097 }
29098
29099 /* Since w32 does not support atimers, it defines its own implementation of
29100 the following three functions in w32fns.c. */
29101 #ifndef WINDOWSNT
29102
29103 /* Platform-independent portion of hourglass implementation. */
29104
29105 /* Cancel a currently active hourglass timer, and start a new one. */
29106 void
29107 start_hourglass (void)
29108 {
29109 #if defined (HAVE_WINDOW_SYSTEM)
29110 EMACS_TIME delay;
29111
29112 cancel_hourglass ();
29113
29114 if (INTEGERP (Vhourglass_delay)
29115 && XINT (Vhourglass_delay) > 0)
29116 EMACS_SET_SECS_NSECS (delay,
29117 min (XINT (Vhourglass_delay), TYPE_MAXIMUM (time_t)),
29118 0);
29119 else if (FLOATP (Vhourglass_delay)
29120 && XFLOAT_DATA (Vhourglass_delay) > 0)
29121 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29122 else
29123 EMACS_SET_SECS_NSECS (delay, DEFAULT_HOURGLASS_DELAY, 0);
29124
29125 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29126 show_hourglass, NULL);
29127 #endif
29128 }
29129
29130
29131 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29132 shown. */
29133 void
29134 cancel_hourglass (void)
29135 {
29136 #if defined (HAVE_WINDOW_SYSTEM)
29137 if (hourglass_atimer)
29138 {
29139 cancel_atimer (hourglass_atimer);
29140 hourglass_atimer = NULL;
29141 }
29142
29143 if (hourglass_shown_p)
29144 hide_hourglass ();
29145 #endif
29146 }
29147 #endif /* ! WINDOWSNT */