Merge from emacs-24; up to 2012-05-01T00:16:02Z!rgm@gnu.org
[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 = alloca (size * sizeof *entries);
5404 USE_SAFE_ALLOCA;
5405
5406 if (charpos <= 0)
5407 charpos = IT_CHARPOS (*it);
5408
5409 /* Append the overlay string STRING of overlay OVERLAY to vector
5410 `entries' which has size `size' and currently contains `n'
5411 elements. AFTER_P non-zero means STRING is an after-string of
5412 OVERLAY. */
5413 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5414 do \
5415 { \
5416 Lisp_Object priority; \
5417 \
5418 if (n == size) \
5419 { \
5420 struct overlay_entry *old = entries; \
5421 SAFE_NALLOCA (entries, 2, size); \
5422 memcpy (entries, old, size * sizeof *entries); \
5423 size *= 2; \
5424 } \
5425 \
5426 entries[n].string = (STRING); \
5427 entries[n].overlay = (OVERLAY); \
5428 priority = Foverlay_get ((OVERLAY), Qpriority); \
5429 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5430 entries[n].after_string_p = (AFTER_P); \
5431 ++n; \
5432 } \
5433 while (0)
5434
5435 /* Process overlay before the overlay center. */
5436 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5437 {
5438 XSETMISC (overlay, ov);
5439 eassert (OVERLAYP (overlay));
5440 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5441 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5442
5443 if (end < charpos)
5444 break;
5445
5446 /* Skip this overlay if it doesn't start or end at IT's current
5447 position. */
5448 if (end != charpos && start != charpos)
5449 continue;
5450
5451 /* Skip this overlay if it doesn't apply to IT->w. */
5452 window = Foverlay_get (overlay, Qwindow);
5453 if (WINDOWP (window) && XWINDOW (window) != it->w)
5454 continue;
5455
5456 /* If the text ``under'' the overlay is invisible, both before-
5457 and after-strings from this overlay are visible; start and
5458 end position are indistinguishable. */
5459 invisible = Foverlay_get (overlay, Qinvisible);
5460 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5461
5462 /* If overlay has a non-empty before-string, record it. */
5463 if ((start == charpos || (end == charpos && invis_p))
5464 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5465 && SCHARS (str))
5466 RECORD_OVERLAY_STRING (overlay, str, 0);
5467
5468 /* If overlay has a non-empty after-string, record it. */
5469 if ((end == charpos || (start == charpos && invis_p))
5470 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5471 && SCHARS (str))
5472 RECORD_OVERLAY_STRING (overlay, str, 1);
5473 }
5474
5475 /* Process overlays after the overlay center. */
5476 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5477 {
5478 XSETMISC (overlay, ov);
5479 eassert (OVERLAYP (overlay));
5480 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5481 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5482
5483 if (start > charpos)
5484 break;
5485
5486 /* Skip this overlay if it doesn't start or end at IT's current
5487 position. */
5488 if (end != charpos && start != charpos)
5489 continue;
5490
5491 /* Skip this overlay if it doesn't apply to IT->w. */
5492 window = Foverlay_get (overlay, Qwindow);
5493 if (WINDOWP (window) && XWINDOW (window) != it->w)
5494 continue;
5495
5496 /* If the text ``under'' the overlay is invisible, it has a zero
5497 dimension, and both before- and after-strings apply. */
5498 invisible = Foverlay_get (overlay, Qinvisible);
5499 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5500
5501 /* If overlay has a non-empty before-string, record it. */
5502 if ((start == charpos || (end == charpos && invis_p))
5503 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5504 && SCHARS (str))
5505 RECORD_OVERLAY_STRING (overlay, str, 0);
5506
5507 /* If overlay has a non-empty after-string, record it. */
5508 if ((end == charpos || (start == charpos && invis_p))
5509 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5510 && SCHARS (str))
5511 RECORD_OVERLAY_STRING (overlay, str, 1);
5512 }
5513
5514 #undef RECORD_OVERLAY_STRING
5515
5516 /* Sort entries. */
5517 if (n > 1)
5518 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5519
5520 /* Record number of overlay strings, and where we computed it. */
5521 it->n_overlay_strings = n;
5522 it->overlay_strings_charpos = charpos;
5523
5524 /* IT->current.overlay_string_index is the number of overlay strings
5525 that have already been consumed by IT. Copy some of the
5526 remaining overlay strings to IT->overlay_strings. */
5527 i = 0;
5528 j = it->current.overlay_string_index;
5529 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5530 {
5531 it->overlay_strings[i] = entries[j].string;
5532 it->string_overlays[i++] = entries[j++].overlay;
5533 }
5534
5535 CHECK_IT (it);
5536 SAFE_FREE ();
5537 }
5538
5539
5540 /* Get the first chunk of overlay strings at IT's current buffer
5541 position, or at CHARPOS if that is > 0. Value is non-zero if at
5542 least one overlay string was found. */
5543
5544 static int
5545 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5546 {
5547 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5548 process. This fills IT->overlay_strings with strings, and sets
5549 IT->n_overlay_strings to the total number of strings to process.
5550 IT->pos.overlay_string_index has to be set temporarily to zero
5551 because load_overlay_strings needs this; it must be set to -1
5552 when no overlay strings are found because a zero value would
5553 indicate a position in the first overlay string. */
5554 it->current.overlay_string_index = 0;
5555 load_overlay_strings (it, charpos);
5556
5557 /* If we found overlay strings, set up IT to deliver display
5558 elements from the first one. Otherwise set up IT to deliver
5559 from current_buffer. */
5560 if (it->n_overlay_strings)
5561 {
5562 /* Make sure we know settings in current_buffer, so that we can
5563 restore meaningful values when we're done with the overlay
5564 strings. */
5565 if (compute_stop_p)
5566 compute_stop_pos (it);
5567 eassert (it->face_id >= 0);
5568
5569 /* Save IT's settings. They are restored after all overlay
5570 strings have been processed. */
5571 eassert (!compute_stop_p || it->sp == 0);
5572
5573 /* When called from handle_stop, there might be an empty display
5574 string loaded. In that case, don't bother saving it. But
5575 don't use this optimization with the bidi iterator, since we
5576 need the corresponding pop_it call to resync the bidi
5577 iterator's position with IT's position, after we are done
5578 with the overlay strings. (The corresponding call to pop_it
5579 in case of an empty display string is in
5580 next_overlay_string.) */
5581 if (!(!it->bidi_p
5582 && STRINGP (it->string) && !SCHARS (it->string)))
5583 push_it (it, NULL);
5584
5585 /* Set up IT to deliver display elements from the first overlay
5586 string. */
5587 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5588 it->string = it->overlay_strings[0];
5589 it->from_overlay = Qnil;
5590 it->stop_charpos = 0;
5591 eassert (STRINGP (it->string));
5592 it->end_charpos = SCHARS (it->string);
5593 it->prev_stop = 0;
5594 it->base_level_stop = 0;
5595 it->multibyte_p = STRING_MULTIBYTE (it->string);
5596 it->method = GET_FROM_STRING;
5597 it->from_disp_prop_p = 0;
5598
5599 /* Force paragraph direction to be that of the parent
5600 buffer. */
5601 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5602 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5603 else
5604 it->paragraph_embedding = L2R;
5605
5606 /* Set up the bidi iterator for this overlay string. */
5607 if (it->bidi_p)
5608 {
5609 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5610
5611 it->bidi_it.string.lstring = it->string;
5612 it->bidi_it.string.s = NULL;
5613 it->bidi_it.string.schars = SCHARS (it->string);
5614 it->bidi_it.string.bufpos = pos;
5615 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5616 it->bidi_it.string.unibyte = !it->multibyte_p;
5617 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5618 }
5619 return 1;
5620 }
5621
5622 it->current.overlay_string_index = -1;
5623 return 0;
5624 }
5625
5626 static int
5627 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5628 {
5629 it->string = Qnil;
5630 it->method = GET_FROM_BUFFER;
5631
5632 (void) get_overlay_strings_1 (it, charpos, 1);
5633
5634 CHECK_IT (it);
5635
5636 /* Value is non-zero if we found at least one overlay string. */
5637 return STRINGP (it->string);
5638 }
5639
5640
5641 \f
5642 /***********************************************************************
5643 Saving and restoring state
5644 ***********************************************************************/
5645
5646 /* Save current settings of IT on IT->stack. Called, for example,
5647 before setting up IT for an overlay string, to be able to restore
5648 IT's settings to what they were after the overlay string has been
5649 processed. If POSITION is non-NULL, it is the position to save on
5650 the stack instead of IT->position. */
5651
5652 static void
5653 push_it (struct it *it, struct text_pos *position)
5654 {
5655 struct iterator_stack_entry *p;
5656
5657 eassert (it->sp < IT_STACK_SIZE);
5658 p = it->stack + it->sp;
5659
5660 p->stop_charpos = it->stop_charpos;
5661 p->prev_stop = it->prev_stop;
5662 p->base_level_stop = it->base_level_stop;
5663 p->cmp_it = it->cmp_it;
5664 eassert (it->face_id >= 0);
5665 p->face_id = it->face_id;
5666 p->string = it->string;
5667 p->method = it->method;
5668 p->from_overlay = it->from_overlay;
5669 switch (p->method)
5670 {
5671 case GET_FROM_IMAGE:
5672 p->u.image.object = it->object;
5673 p->u.image.image_id = it->image_id;
5674 p->u.image.slice = it->slice;
5675 break;
5676 case GET_FROM_STRETCH:
5677 p->u.stretch.object = it->object;
5678 break;
5679 }
5680 p->position = position ? *position : it->position;
5681 p->current = it->current;
5682 p->end_charpos = it->end_charpos;
5683 p->string_nchars = it->string_nchars;
5684 p->area = it->area;
5685 p->multibyte_p = it->multibyte_p;
5686 p->avoid_cursor_p = it->avoid_cursor_p;
5687 p->space_width = it->space_width;
5688 p->font_height = it->font_height;
5689 p->voffset = it->voffset;
5690 p->string_from_display_prop_p = it->string_from_display_prop_p;
5691 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5692 p->display_ellipsis_p = 0;
5693 p->line_wrap = it->line_wrap;
5694 p->bidi_p = it->bidi_p;
5695 p->paragraph_embedding = it->paragraph_embedding;
5696 p->from_disp_prop_p = it->from_disp_prop_p;
5697 ++it->sp;
5698
5699 /* Save the state of the bidi iterator as well. */
5700 if (it->bidi_p)
5701 bidi_push_it (&it->bidi_it);
5702 }
5703
5704 static void
5705 iterate_out_of_display_property (struct it *it)
5706 {
5707 int buffer_p = !STRINGP (it->string);
5708 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5709 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5710
5711 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5712
5713 /* Maybe initialize paragraph direction. If we are at the beginning
5714 of a new paragraph, next_element_from_buffer may not have a
5715 chance to do that. */
5716 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5717 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5718 /* prev_stop can be zero, so check against BEGV as well. */
5719 while (it->bidi_it.charpos >= bob
5720 && it->prev_stop <= it->bidi_it.charpos
5721 && it->bidi_it.charpos < CHARPOS (it->position)
5722 && it->bidi_it.charpos < eob)
5723 bidi_move_to_visually_next (&it->bidi_it);
5724 /* Record the stop_pos we just crossed, for when we cross it
5725 back, maybe. */
5726 if (it->bidi_it.charpos > CHARPOS (it->position))
5727 it->prev_stop = CHARPOS (it->position);
5728 /* If we ended up not where pop_it put us, resync IT's
5729 positional members with the bidi iterator. */
5730 if (it->bidi_it.charpos != CHARPOS (it->position))
5731 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5732 if (buffer_p)
5733 it->current.pos = it->position;
5734 else
5735 it->current.string_pos = it->position;
5736 }
5737
5738 /* Restore IT's settings from IT->stack. Called, for example, when no
5739 more overlay strings must be processed, and we return to delivering
5740 display elements from a buffer, or when the end of a string from a
5741 `display' property is reached and we return to delivering display
5742 elements from an overlay string, or from a buffer. */
5743
5744 static void
5745 pop_it (struct it *it)
5746 {
5747 struct iterator_stack_entry *p;
5748 int from_display_prop = it->from_disp_prop_p;
5749
5750 eassert (it->sp > 0);
5751 --it->sp;
5752 p = it->stack + it->sp;
5753 it->stop_charpos = p->stop_charpos;
5754 it->prev_stop = p->prev_stop;
5755 it->base_level_stop = p->base_level_stop;
5756 it->cmp_it = p->cmp_it;
5757 it->face_id = p->face_id;
5758 it->current = p->current;
5759 it->position = p->position;
5760 it->string = p->string;
5761 it->from_overlay = p->from_overlay;
5762 if (NILP (it->string))
5763 SET_TEXT_POS (it->current.string_pos, -1, -1);
5764 it->method = p->method;
5765 switch (it->method)
5766 {
5767 case GET_FROM_IMAGE:
5768 it->image_id = p->u.image.image_id;
5769 it->object = p->u.image.object;
5770 it->slice = p->u.image.slice;
5771 break;
5772 case GET_FROM_STRETCH:
5773 it->object = p->u.stretch.object;
5774 break;
5775 case GET_FROM_BUFFER:
5776 it->object = it->w->buffer;
5777 break;
5778 case GET_FROM_STRING:
5779 it->object = it->string;
5780 break;
5781 case GET_FROM_DISPLAY_VECTOR:
5782 if (it->s)
5783 it->method = GET_FROM_C_STRING;
5784 else if (STRINGP (it->string))
5785 it->method = GET_FROM_STRING;
5786 else
5787 {
5788 it->method = GET_FROM_BUFFER;
5789 it->object = it->w->buffer;
5790 }
5791 }
5792 it->end_charpos = p->end_charpos;
5793 it->string_nchars = p->string_nchars;
5794 it->area = p->area;
5795 it->multibyte_p = p->multibyte_p;
5796 it->avoid_cursor_p = p->avoid_cursor_p;
5797 it->space_width = p->space_width;
5798 it->font_height = p->font_height;
5799 it->voffset = p->voffset;
5800 it->string_from_display_prop_p = p->string_from_display_prop_p;
5801 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5802 it->line_wrap = p->line_wrap;
5803 it->bidi_p = p->bidi_p;
5804 it->paragraph_embedding = p->paragraph_embedding;
5805 it->from_disp_prop_p = p->from_disp_prop_p;
5806 if (it->bidi_p)
5807 {
5808 bidi_pop_it (&it->bidi_it);
5809 /* Bidi-iterate until we get out of the portion of text, if any,
5810 covered by a `display' text property or by an overlay with
5811 `display' property. (We cannot just jump there, because the
5812 internal coherency of the bidi iterator state can not be
5813 preserved across such jumps.) We also must determine the
5814 paragraph base direction if the overlay we just processed is
5815 at the beginning of a new paragraph. */
5816 if (from_display_prop
5817 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5818 iterate_out_of_display_property (it);
5819
5820 eassert ((BUFFERP (it->object)
5821 && IT_CHARPOS (*it) == it->bidi_it.charpos
5822 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5823 || (STRINGP (it->object)
5824 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5825 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5826 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5827 }
5828 }
5829
5830
5831 \f
5832 /***********************************************************************
5833 Moving over lines
5834 ***********************************************************************/
5835
5836 /* Set IT's current position to the previous line start. */
5837
5838 static void
5839 back_to_previous_line_start (struct it *it)
5840 {
5841 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5842 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5843 }
5844
5845
5846 /* Move IT to the next line start.
5847
5848 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5849 we skipped over part of the text (as opposed to moving the iterator
5850 continuously over the text). Otherwise, don't change the value
5851 of *SKIPPED_P.
5852
5853 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5854 iterator on the newline, if it was found.
5855
5856 Newlines may come from buffer text, overlay strings, or strings
5857 displayed via the `display' property. That's the reason we can't
5858 simply use find_next_newline_no_quit.
5859
5860 Note that this function may not skip over invisible text that is so
5861 because of text properties and immediately follows a newline. If
5862 it would, function reseat_at_next_visible_line_start, when called
5863 from set_iterator_to_next, would effectively make invisible
5864 characters following a newline part of the wrong glyph row, which
5865 leads to wrong cursor motion. */
5866
5867 static int
5868 forward_to_next_line_start (struct it *it, int *skipped_p,
5869 struct bidi_it *bidi_it_prev)
5870 {
5871 ptrdiff_t old_selective;
5872 int newline_found_p, n;
5873 const int MAX_NEWLINE_DISTANCE = 500;
5874
5875 /* If already on a newline, just consume it to avoid unintended
5876 skipping over invisible text below. */
5877 if (it->what == IT_CHARACTER
5878 && it->c == '\n'
5879 && CHARPOS (it->position) == IT_CHARPOS (*it))
5880 {
5881 if (it->bidi_p && bidi_it_prev)
5882 *bidi_it_prev = it->bidi_it;
5883 set_iterator_to_next (it, 0);
5884 it->c = 0;
5885 return 1;
5886 }
5887
5888 /* Don't handle selective display in the following. It's (a)
5889 unnecessary because it's done by the caller, and (b) leads to an
5890 infinite recursion because next_element_from_ellipsis indirectly
5891 calls this function. */
5892 old_selective = it->selective;
5893 it->selective = 0;
5894
5895 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5896 from buffer text. */
5897 for (n = newline_found_p = 0;
5898 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5899 n += STRINGP (it->string) ? 0 : 1)
5900 {
5901 if (!get_next_display_element (it))
5902 return 0;
5903 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5904 if (newline_found_p && it->bidi_p && bidi_it_prev)
5905 *bidi_it_prev = it->bidi_it;
5906 set_iterator_to_next (it, 0);
5907 }
5908
5909 /* If we didn't find a newline near enough, see if we can use a
5910 short-cut. */
5911 if (!newline_found_p)
5912 {
5913 ptrdiff_t start = IT_CHARPOS (*it);
5914 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5915 Lisp_Object pos;
5916
5917 eassert (!STRINGP (it->string));
5918
5919 /* If there isn't any `display' property in sight, and no
5920 overlays, we can just use the position of the newline in
5921 buffer text. */
5922 if (it->stop_charpos >= limit
5923 || ((pos = Fnext_single_property_change (make_number (start),
5924 Qdisplay, Qnil,
5925 make_number (limit)),
5926 NILP (pos))
5927 && next_overlay_change (start) == ZV))
5928 {
5929 if (!it->bidi_p)
5930 {
5931 IT_CHARPOS (*it) = limit;
5932 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5933 }
5934 else
5935 {
5936 struct bidi_it bprev;
5937
5938 /* Help bidi.c avoid expensive searches for display
5939 properties and overlays, by telling it that there are
5940 none up to `limit'. */
5941 if (it->bidi_it.disp_pos < limit)
5942 {
5943 it->bidi_it.disp_pos = limit;
5944 it->bidi_it.disp_prop = 0;
5945 }
5946 do {
5947 bprev = it->bidi_it;
5948 bidi_move_to_visually_next (&it->bidi_it);
5949 } while (it->bidi_it.charpos != limit);
5950 IT_CHARPOS (*it) = limit;
5951 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5952 if (bidi_it_prev)
5953 *bidi_it_prev = bprev;
5954 }
5955 *skipped_p = newline_found_p = 1;
5956 }
5957 else
5958 {
5959 while (get_next_display_element (it)
5960 && !newline_found_p)
5961 {
5962 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5963 if (newline_found_p && it->bidi_p && bidi_it_prev)
5964 *bidi_it_prev = it->bidi_it;
5965 set_iterator_to_next (it, 0);
5966 }
5967 }
5968 }
5969
5970 it->selective = old_selective;
5971 return newline_found_p;
5972 }
5973
5974
5975 /* Set IT's current position to the previous visible line start. Skip
5976 invisible text that is so either due to text properties or due to
5977 selective display. Caution: this does not change IT->current_x and
5978 IT->hpos. */
5979
5980 static void
5981 back_to_previous_visible_line_start (struct it *it)
5982 {
5983 while (IT_CHARPOS (*it) > BEGV)
5984 {
5985 back_to_previous_line_start (it);
5986
5987 if (IT_CHARPOS (*it) <= BEGV)
5988 break;
5989
5990 /* If selective > 0, then lines indented more than its value are
5991 invisible. */
5992 if (it->selective > 0
5993 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5994 it->selective))
5995 continue;
5996
5997 /* Check the newline before point for invisibility. */
5998 {
5999 Lisp_Object prop;
6000 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6001 Qinvisible, it->window);
6002 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6003 continue;
6004 }
6005
6006 if (IT_CHARPOS (*it) <= BEGV)
6007 break;
6008
6009 {
6010 struct it it2;
6011 void *it2data = NULL;
6012 ptrdiff_t pos;
6013 ptrdiff_t beg, end;
6014 Lisp_Object val, overlay;
6015
6016 SAVE_IT (it2, *it, it2data);
6017
6018 /* If newline is part of a composition, continue from start of composition */
6019 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6020 && beg < IT_CHARPOS (*it))
6021 goto replaced;
6022
6023 /* If newline is replaced by a display property, find start of overlay
6024 or interval and continue search from that point. */
6025 pos = --IT_CHARPOS (it2);
6026 --IT_BYTEPOS (it2);
6027 it2.sp = 0;
6028 bidi_unshelve_cache (NULL, 0);
6029 it2.string_from_display_prop_p = 0;
6030 it2.from_disp_prop_p = 0;
6031 if (handle_display_prop (&it2) == HANDLED_RETURN
6032 && !NILP (val = get_char_property_and_overlay
6033 (make_number (pos), Qdisplay, Qnil, &overlay))
6034 && (OVERLAYP (overlay)
6035 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6036 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6037 {
6038 RESTORE_IT (it, it, it2data);
6039 goto replaced;
6040 }
6041
6042 /* Newline is not replaced by anything -- so we are done. */
6043 RESTORE_IT (it, it, it2data);
6044 break;
6045
6046 replaced:
6047 if (beg < BEGV)
6048 beg = BEGV;
6049 IT_CHARPOS (*it) = beg;
6050 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6051 }
6052 }
6053
6054 it->continuation_lines_width = 0;
6055
6056 eassert (IT_CHARPOS (*it) >= BEGV);
6057 eassert (IT_CHARPOS (*it) == BEGV
6058 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6059 CHECK_IT (it);
6060 }
6061
6062
6063 /* Reseat iterator IT at the previous visible line start. Skip
6064 invisible text that is so either due to text properties or due to
6065 selective display. At the end, update IT's overlay information,
6066 face information etc. */
6067
6068 void
6069 reseat_at_previous_visible_line_start (struct it *it)
6070 {
6071 back_to_previous_visible_line_start (it);
6072 reseat (it, it->current.pos, 1);
6073 CHECK_IT (it);
6074 }
6075
6076
6077 /* Reseat iterator IT on the next visible line start in the current
6078 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6079 preceding the line start. Skip over invisible text that is so
6080 because of selective display. Compute faces, overlays etc at the
6081 new position. Note that this function does not skip over text that
6082 is invisible because of text properties. */
6083
6084 static void
6085 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6086 {
6087 int newline_found_p, skipped_p = 0;
6088 struct bidi_it bidi_it_prev;
6089
6090 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6091
6092 /* Skip over lines that are invisible because they are indented
6093 more than the value of IT->selective. */
6094 if (it->selective > 0)
6095 while (IT_CHARPOS (*it) < ZV
6096 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6097 it->selective))
6098 {
6099 eassert (IT_BYTEPOS (*it) == BEGV
6100 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6101 newline_found_p =
6102 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6103 }
6104
6105 /* Position on the newline if that's what's requested. */
6106 if (on_newline_p && newline_found_p)
6107 {
6108 if (STRINGP (it->string))
6109 {
6110 if (IT_STRING_CHARPOS (*it) > 0)
6111 {
6112 if (!it->bidi_p)
6113 {
6114 --IT_STRING_CHARPOS (*it);
6115 --IT_STRING_BYTEPOS (*it);
6116 }
6117 else
6118 {
6119 /* We need to restore the bidi iterator to the state
6120 it had on the newline, and resync the IT's
6121 position with that. */
6122 it->bidi_it = bidi_it_prev;
6123 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6124 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6125 }
6126 }
6127 }
6128 else if (IT_CHARPOS (*it) > BEGV)
6129 {
6130 if (!it->bidi_p)
6131 {
6132 --IT_CHARPOS (*it);
6133 --IT_BYTEPOS (*it);
6134 }
6135 else
6136 {
6137 /* We need to restore the bidi iterator to the state it
6138 had on the newline and resync IT with that. */
6139 it->bidi_it = bidi_it_prev;
6140 IT_CHARPOS (*it) = it->bidi_it.charpos;
6141 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6142 }
6143 reseat (it, it->current.pos, 0);
6144 }
6145 }
6146 else if (skipped_p)
6147 reseat (it, it->current.pos, 0);
6148
6149 CHECK_IT (it);
6150 }
6151
6152
6153 \f
6154 /***********************************************************************
6155 Changing an iterator's position
6156 ***********************************************************************/
6157
6158 /* Change IT's current position to POS in current_buffer. If FORCE_P
6159 is non-zero, always check for text properties at the new position.
6160 Otherwise, text properties are only looked up if POS >=
6161 IT->check_charpos of a property. */
6162
6163 static void
6164 reseat (struct it *it, struct text_pos pos, int force_p)
6165 {
6166 ptrdiff_t original_pos = IT_CHARPOS (*it);
6167
6168 reseat_1 (it, pos, 0);
6169
6170 /* Determine where to check text properties. Avoid doing it
6171 where possible because text property lookup is very expensive. */
6172 if (force_p
6173 || CHARPOS (pos) > it->stop_charpos
6174 || CHARPOS (pos) < original_pos)
6175 {
6176 if (it->bidi_p)
6177 {
6178 /* For bidi iteration, we need to prime prev_stop and
6179 base_level_stop with our best estimations. */
6180 /* Implementation note: Of course, POS is not necessarily a
6181 stop position, so assigning prev_pos to it is a lie; we
6182 should have called compute_stop_backwards. However, if
6183 the current buffer does not include any R2L characters,
6184 that call would be a waste of cycles, because the
6185 iterator will never move back, and thus never cross this
6186 "fake" stop position. So we delay that backward search
6187 until the time we really need it, in next_element_from_buffer. */
6188 if (CHARPOS (pos) != it->prev_stop)
6189 it->prev_stop = CHARPOS (pos);
6190 if (CHARPOS (pos) < it->base_level_stop)
6191 it->base_level_stop = 0; /* meaning it's unknown */
6192 handle_stop (it);
6193 }
6194 else
6195 {
6196 handle_stop (it);
6197 it->prev_stop = it->base_level_stop = 0;
6198 }
6199
6200 }
6201
6202 CHECK_IT (it);
6203 }
6204
6205
6206 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6207 IT->stop_pos to POS, also. */
6208
6209 static void
6210 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6211 {
6212 /* Don't call this function when scanning a C string. */
6213 eassert (it->s == NULL);
6214
6215 /* POS must be a reasonable value. */
6216 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6217
6218 it->current.pos = it->position = pos;
6219 it->end_charpos = ZV;
6220 it->dpvec = NULL;
6221 it->current.dpvec_index = -1;
6222 it->current.overlay_string_index = -1;
6223 IT_STRING_CHARPOS (*it) = -1;
6224 IT_STRING_BYTEPOS (*it) = -1;
6225 it->string = Qnil;
6226 it->method = GET_FROM_BUFFER;
6227 it->object = it->w->buffer;
6228 it->area = TEXT_AREA;
6229 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6230 it->sp = 0;
6231 it->string_from_display_prop_p = 0;
6232 it->string_from_prefix_prop_p = 0;
6233
6234 it->from_disp_prop_p = 0;
6235 it->face_before_selective_p = 0;
6236 if (it->bidi_p)
6237 {
6238 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6239 &it->bidi_it);
6240 bidi_unshelve_cache (NULL, 0);
6241 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6242 it->bidi_it.string.s = NULL;
6243 it->bidi_it.string.lstring = Qnil;
6244 it->bidi_it.string.bufpos = 0;
6245 it->bidi_it.string.unibyte = 0;
6246 }
6247
6248 if (set_stop_p)
6249 {
6250 it->stop_charpos = CHARPOS (pos);
6251 it->base_level_stop = CHARPOS (pos);
6252 }
6253 }
6254
6255
6256 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6257 If S is non-null, it is a C string to iterate over. Otherwise,
6258 STRING gives a Lisp string to iterate over.
6259
6260 If PRECISION > 0, don't return more then PRECISION number of
6261 characters from the string.
6262
6263 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6264 characters have been returned. FIELD_WIDTH < 0 means an infinite
6265 field width.
6266
6267 MULTIBYTE = 0 means disable processing of multibyte characters,
6268 MULTIBYTE > 0 means enable it,
6269 MULTIBYTE < 0 means use IT->multibyte_p.
6270
6271 IT must be initialized via a prior call to init_iterator before
6272 calling this function. */
6273
6274 static void
6275 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6276 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6277 int multibyte)
6278 {
6279 /* No region in strings. */
6280 it->region_beg_charpos = it->region_end_charpos = -1;
6281
6282 /* No text property checks performed by default, but see below. */
6283 it->stop_charpos = -1;
6284
6285 /* Set iterator position and end position. */
6286 memset (&it->current, 0, sizeof it->current);
6287 it->current.overlay_string_index = -1;
6288 it->current.dpvec_index = -1;
6289 eassert (charpos >= 0);
6290
6291 /* If STRING is specified, use its multibyteness, otherwise use the
6292 setting of MULTIBYTE, if specified. */
6293 if (multibyte >= 0)
6294 it->multibyte_p = multibyte > 0;
6295
6296 /* Bidirectional reordering of strings is controlled by the default
6297 value of bidi-display-reordering. Don't try to reorder while
6298 loading loadup.el, as the necessary character property tables are
6299 not yet available. */
6300 it->bidi_p =
6301 NILP (Vpurify_flag)
6302 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6303
6304 if (s == NULL)
6305 {
6306 eassert (STRINGP (string));
6307 it->string = string;
6308 it->s = NULL;
6309 it->end_charpos = it->string_nchars = SCHARS (string);
6310 it->method = GET_FROM_STRING;
6311 it->current.string_pos = string_pos (charpos, string);
6312
6313 if (it->bidi_p)
6314 {
6315 it->bidi_it.string.lstring = string;
6316 it->bidi_it.string.s = NULL;
6317 it->bidi_it.string.schars = it->end_charpos;
6318 it->bidi_it.string.bufpos = 0;
6319 it->bidi_it.string.from_disp_str = 0;
6320 it->bidi_it.string.unibyte = !it->multibyte_p;
6321 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6322 FRAME_WINDOW_P (it->f), &it->bidi_it);
6323 }
6324 }
6325 else
6326 {
6327 it->s = (const unsigned char *) s;
6328 it->string = Qnil;
6329
6330 /* Note that we use IT->current.pos, not it->current.string_pos,
6331 for displaying C strings. */
6332 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6333 if (it->multibyte_p)
6334 {
6335 it->current.pos = c_string_pos (charpos, s, 1);
6336 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6337 }
6338 else
6339 {
6340 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6341 it->end_charpos = it->string_nchars = strlen (s);
6342 }
6343
6344 if (it->bidi_p)
6345 {
6346 it->bidi_it.string.lstring = Qnil;
6347 it->bidi_it.string.s = (const unsigned char *) s;
6348 it->bidi_it.string.schars = it->end_charpos;
6349 it->bidi_it.string.bufpos = 0;
6350 it->bidi_it.string.from_disp_str = 0;
6351 it->bidi_it.string.unibyte = !it->multibyte_p;
6352 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6353 &it->bidi_it);
6354 }
6355 it->method = GET_FROM_C_STRING;
6356 }
6357
6358 /* PRECISION > 0 means don't return more than PRECISION characters
6359 from the string. */
6360 if (precision > 0 && it->end_charpos - charpos > precision)
6361 {
6362 it->end_charpos = it->string_nchars = charpos + precision;
6363 if (it->bidi_p)
6364 it->bidi_it.string.schars = it->end_charpos;
6365 }
6366
6367 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6368 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6369 FIELD_WIDTH < 0 means infinite field width. This is useful for
6370 padding with `-' at the end of a mode line. */
6371 if (field_width < 0)
6372 field_width = INFINITY;
6373 /* Implementation note: We deliberately don't enlarge
6374 it->bidi_it.string.schars here to fit it->end_charpos, because
6375 the bidi iterator cannot produce characters out of thin air. */
6376 if (field_width > it->end_charpos - charpos)
6377 it->end_charpos = charpos + field_width;
6378
6379 /* Use the standard display table for displaying strings. */
6380 if (DISP_TABLE_P (Vstandard_display_table))
6381 it->dp = XCHAR_TABLE (Vstandard_display_table);
6382
6383 it->stop_charpos = charpos;
6384 it->prev_stop = charpos;
6385 it->base_level_stop = 0;
6386 if (it->bidi_p)
6387 {
6388 it->bidi_it.first_elt = 1;
6389 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6390 it->bidi_it.disp_pos = -1;
6391 }
6392 if (s == NULL && it->multibyte_p)
6393 {
6394 ptrdiff_t endpos = SCHARS (it->string);
6395 if (endpos > it->end_charpos)
6396 endpos = it->end_charpos;
6397 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6398 it->string);
6399 }
6400 CHECK_IT (it);
6401 }
6402
6403
6404 \f
6405 /***********************************************************************
6406 Iteration
6407 ***********************************************************************/
6408
6409 /* Map enum it_method value to corresponding next_element_from_* function. */
6410
6411 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6412 {
6413 next_element_from_buffer,
6414 next_element_from_display_vector,
6415 next_element_from_string,
6416 next_element_from_c_string,
6417 next_element_from_image,
6418 next_element_from_stretch
6419 };
6420
6421 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6422
6423
6424 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6425 (possibly with the following characters). */
6426
6427 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6428 ((IT)->cmp_it.id >= 0 \
6429 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6430 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6431 END_CHARPOS, (IT)->w, \
6432 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6433 (IT)->string)))
6434
6435
6436 /* Lookup the char-table Vglyphless_char_display for character C (-1
6437 if we want information for no-font case), and return the display
6438 method symbol. By side-effect, update it->what and
6439 it->glyphless_method. This function is called from
6440 get_next_display_element for each character element, and from
6441 x_produce_glyphs when no suitable font was found. */
6442
6443 Lisp_Object
6444 lookup_glyphless_char_display (int c, struct it *it)
6445 {
6446 Lisp_Object glyphless_method = Qnil;
6447
6448 if (CHAR_TABLE_P (Vglyphless_char_display)
6449 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6450 {
6451 if (c >= 0)
6452 {
6453 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6454 if (CONSP (glyphless_method))
6455 glyphless_method = FRAME_WINDOW_P (it->f)
6456 ? XCAR (glyphless_method)
6457 : XCDR (glyphless_method);
6458 }
6459 else
6460 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6461 }
6462
6463 retry:
6464 if (NILP (glyphless_method))
6465 {
6466 if (c >= 0)
6467 /* The default is to display the character by a proper font. */
6468 return Qnil;
6469 /* The default for the no-font case is to display an empty box. */
6470 glyphless_method = Qempty_box;
6471 }
6472 if (EQ (glyphless_method, Qzero_width))
6473 {
6474 if (c >= 0)
6475 return glyphless_method;
6476 /* This method can't be used for the no-font case. */
6477 glyphless_method = Qempty_box;
6478 }
6479 if (EQ (glyphless_method, Qthin_space))
6480 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6481 else if (EQ (glyphless_method, Qempty_box))
6482 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6483 else if (EQ (glyphless_method, Qhex_code))
6484 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6485 else if (STRINGP (glyphless_method))
6486 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6487 else
6488 {
6489 /* Invalid value. We use the default method. */
6490 glyphless_method = Qnil;
6491 goto retry;
6492 }
6493 it->what = IT_GLYPHLESS;
6494 return glyphless_method;
6495 }
6496
6497 /* Load IT's display element fields with information about the next
6498 display element from the current position of IT. Value is zero if
6499 end of buffer (or C string) is reached. */
6500
6501 static struct frame *last_escape_glyph_frame = NULL;
6502 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6503 static int last_escape_glyph_merged_face_id = 0;
6504
6505 struct frame *last_glyphless_glyph_frame = NULL;
6506 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6507 int last_glyphless_glyph_merged_face_id = 0;
6508
6509 static int
6510 get_next_display_element (struct it *it)
6511 {
6512 /* Non-zero means that we found a display element. Zero means that
6513 we hit the end of what we iterate over. Performance note: the
6514 function pointer `method' used here turns out to be faster than
6515 using a sequence of if-statements. */
6516 int success_p;
6517
6518 get_next:
6519 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6520
6521 if (it->what == IT_CHARACTER)
6522 {
6523 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6524 and only if (a) the resolved directionality of that character
6525 is R..." */
6526 /* FIXME: Do we need an exception for characters from display
6527 tables? */
6528 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6529 it->c = bidi_mirror_char (it->c);
6530 /* Map via display table or translate control characters.
6531 IT->c, IT->len etc. have been set to the next character by
6532 the function call above. If we have a display table, and it
6533 contains an entry for IT->c, translate it. Don't do this if
6534 IT->c itself comes from a display table, otherwise we could
6535 end up in an infinite recursion. (An alternative could be to
6536 count the recursion depth of this function and signal an
6537 error when a certain maximum depth is reached.) Is it worth
6538 it? */
6539 if (success_p && it->dpvec == NULL)
6540 {
6541 Lisp_Object dv;
6542 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6543 int nonascii_space_p = 0;
6544 int nonascii_hyphen_p = 0;
6545 int c = it->c; /* This is the character to display. */
6546
6547 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6548 {
6549 eassert (SINGLE_BYTE_CHAR_P (c));
6550 if (unibyte_display_via_language_environment)
6551 {
6552 c = DECODE_CHAR (unibyte, c);
6553 if (c < 0)
6554 c = BYTE8_TO_CHAR (it->c);
6555 }
6556 else
6557 c = BYTE8_TO_CHAR (it->c);
6558 }
6559
6560 if (it->dp
6561 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6562 VECTORP (dv)))
6563 {
6564 struct Lisp_Vector *v = XVECTOR (dv);
6565
6566 /* Return the first character from the display table
6567 entry, if not empty. If empty, don't display the
6568 current character. */
6569 if (v->header.size)
6570 {
6571 it->dpvec_char_len = it->len;
6572 it->dpvec = v->contents;
6573 it->dpend = v->contents + v->header.size;
6574 it->current.dpvec_index = 0;
6575 it->dpvec_face_id = -1;
6576 it->saved_face_id = it->face_id;
6577 it->method = GET_FROM_DISPLAY_VECTOR;
6578 it->ellipsis_p = 0;
6579 }
6580 else
6581 {
6582 set_iterator_to_next (it, 0);
6583 }
6584 goto get_next;
6585 }
6586
6587 if (! NILP (lookup_glyphless_char_display (c, it)))
6588 {
6589 if (it->what == IT_GLYPHLESS)
6590 goto done;
6591 /* Don't display this character. */
6592 set_iterator_to_next (it, 0);
6593 goto get_next;
6594 }
6595
6596 /* If `nobreak-char-display' is non-nil, we display
6597 non-ASCII spaces and hyphens specially. */
6598 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6599 {
6600 if (c == 0xA0)
6601 nonascii_space_p = 1;
6602 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6603 nonascii_hyphen_p = 1;
6604 }
6605
6606 /* Translate control characters into `\003' or `^C' form.
6607 Control characters coming from a display table entry are
6608 currently not translated because we use IT->dpvec to hold
6609 the translation. This could easily be changed but I
6610 don't believe that it is worth doing.
6611
6612 The characters handled by `nobreak-char-display' must be
6613 translated too.
6614
6615 Non-printable characters and raw-byte characters are also
6616 translated to octal form. */
6617 if (((c < ' ' || c == 127) /* ASCII control chars */
6618 ? (it->area != TEXT_AREA
6619 /* In mode line, treat \n, \t like other crl chars. */
6620 || (c != '\t'
6621 && it->glyph_row
6622 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6623 || (c != '\n' && c != '\t'))
6624 : (nonascii_space_p
6625 || nonascii_hyphen_p
6626 || CHAR_BYTE8_P (c)
6627 || ! CHAR_PRINTABLE_P (c))))
6628 {
6629 /* C is a control character, non-ASCII space/hyphen,
6630 raw-byte, or a non-printable character which must be
6631 displayed either as '\003' or as `^C' where the '\\'
6632 and '^' can be defined in the display table. Fill
6633 IT->ctl_chars with glyphs for what we have to
6634 display. Then, set IT->dpvec to these glyphs. */
6635 Lisp_Object gc;
6636 int ctl_len;
6637 int face_id;
6638 int lface_id = 0;
6639 int escape_glyph;
6640
6641 /* Handle control characters with ^. */
6642
6643 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6644 {
6645 int g;
6646
6647 g = '^'; /* default glyph for Control */
6648 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6649 if (it->dp
6650 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6651 {
6652 g = GLYPH_CODE_CHAR (gc);
6653 lface_id = GLYPH_CODE_FACE (gc);
6654 }
6655 if (lface_id)
6656 {
6657 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6658 }
6659 else if (it->f == last_escape_glyph_frame
6660 && it->face_id == last_escape_glyph_face_id)
6661 {
6662 face_id = last_escape_glyph_merged_face_id;
6663 }
6664 else
6665 {
6666 /* Merge the escape-glyph face into the current face. */
6667 face_id = merge_faces (it->f, Qescape_glyph, 0,
6668 it->face_id);
6669 last_escape_glyph_frame = it->f;
6670 last_escape_glyph_face_id = it->face_id;
6671 last_escape_glyph_merged_face_id = face_id;
6672 }
6673
6674 XSETINT (it->ctl_chars[0], g);
6675 XSETINT (it->ctl_chars[1], c ^ 0100);
6676 ctl_len = 2;
6677 goto display_control;
6678 }
6679
6680 /* Handle non-ascii space in the mode where it only gets
6681 highlighting. */
6682
6683 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6684 {
6685 /* Merge `nobreak-space' into the current face. */
6686 face_id = merge_faces (it->f, Qnobreak_space, 0,
6687 it->face_id);
6688 XSETINT (it->ctl_chars[0], ' ');
6689 ctl_len = 1;
6690 goto display_control;
6691 }
6692
6693 /* Handle sequences that start with the "escape glyph". */
6694
6695 /* the default escape glyph is \. */
6696 escape_glyph = '\\';
6697
6698 if (it->dp
6699 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6700 {
6701 escape_glyph = GLYPH_CODE_CHAR (gc);
6702 lface_id = GLYPH_CODE_FACE (gc);
6703 }
6704 if (lface_id)
6705 {
6706 /* The display table specified a face.
6707 Merge it into face_id and also into escape_glyph. */
6708 face_id = merge_faces (it->f, Qt, lface_id,
6709 it->face_id);
6710 }
6711 else if (it->f == last_escape_glyph_frame
6712 && it->face_id == last_escape_glyph_face_id)
6713 {
6714 face_id = last_escape_glyph_merged_face_id;
6715 }
6716 else
6717 {
6718 /* Merge the escape-glyph face into the current face. */
6719 face_id = merge_faces (it->f, Qescape_glyph, 0,
6720 it->face_id);
6721 last_escape_glyph_frame = it->f;
6722 last_escape_glyph_face_id = it->face_id;
6723 last_escape_glyph_merged_face_id = face_id;
6724 }
6725
6726 /* Draw non-ASCII hyphen with just highlighting: */
6727
6728 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6729 {
6730 XSETINT (it->ctl_chars[0], '-');
6731 ctl_len = 1;
6732 goto display_control;
6733 }
6734
6735 /* Draw non-ASCII space/hyphen with escape glyph: */
6736
6737 if (nonascii_space_p || nonascii_hyphen_p)
6738 {
6739 XSETINT (it->ctl_chars[0], escape_glyph);
6740 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6741 ctl_len = 2;
6742 goto display_control;
6743 }
6744
6745 {
6746 char str[10];
6747 int len, i;
6748
6749 if (CHAR_BYTE8_P (c))
6750 /* Display \200 instead of \17777600. */
6751 c = CHAR_TO_BYTE8 (c);
6752 len = sprintf (str, "%03o", c);
6753
6754 XSETINT (it->ctl_chars[0], escape_glyph);
6755 for (i = 0; i < len; i++)
6756 XSETINT (it->ctl_chars[i + 1], str[i]);
6757 ctl_len = len + 1;
6758 }
6759
6760 display_control:
6761 /* Set up IT->dpvec and return first character from it. */
6762 it->dpvec_char_len = it->len;
6763 it->dpvec = it->ctl_chars;
6764 it->dpend = it->dpvec + ctl_len;
6765 it->current.dpvec_index = 0;
6766 it->dpvec_face_id = face_id;
6767 it->saved_face_id = it->face_id;
6768 it->method = GET_FROM_DISPLAY_VECTOR;
6769 it->ellipsis_p = 0;
6770 goto get_next;
6771 }
6772 it->char_to_display = c;
6773 }
6774 else if (success_p)
6775 {
6776 it->char_to_display = it->c;
6777 }
6778 }
6779
6780 /* Adjust face id for a multibyte character. There are no multibyte
6781 character in unibyte text. */
6782 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6783 && it->multibyte_p
6784 && success_p
6785 && FRAME_WINDOW_P (it->f))
6786 {
6787 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6788
6789 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6790 {
6791 /* Automatic composition with glyph-string. */
6792 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6793
6794 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6795 }
6796 else
6797 {
6798 ptrdiff_t pos = (it->s ? -1
6799 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6800 : IT_CHARPOS (*it));
6801 int c;
6802
6803 if (it->what == IT_CHARACTER)
6804 c = it->char_to_display;
6805 else
6806 {
6807 struct composition *cmp = composition_table[it->cmp_it.id];
6808 int i;
6809
6810 c = ' ';
6811 for (i = 0; i < cmp->glyph_len; i++)
6812 /* TAB in a composition means display glyphs with
6813 padding space on the left or right. */
6814 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6815 break;
6816 }
6817 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6818 }
6819 }
6820
6821 done:
6822 /* Is this character the last one of a run of characters with
6823 box? If yes, set IT->end_of_box_run_p to 1. */
6824 if (it->face_box_p
6825 && it->s == NULL)
6826 {
6827 if (it->method == GET_FROM_STRING && it->sp)
6828 {
6829 int face_id = underlying_face_id (it);
6830 struct face *face = FACE_FROM_ID (it->f, face_id);
6831
6832 if (face)
6833 {
6834 if (face->box == FACE_NO_BOX)
6835 {
6836 /* If the box comes from face properties in a
6837 display string, check faces in that string. */
6838 int string_face_id = face_after_it_pos (it);
6839 it->end_of_box_run_p
6840 = (FACE_FROM_ID (it->f, string_face_id)->box
6841 == FACE_NO_BOX);
6842 }
6843 /* Otherwise, the box comes from the underlying face.
6844 If this is the last string character displayed, check
6845 the next buffer location. */
6846 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6847 && (it->current.overlay_string_index
6848 == it->n_overlay_strings - 1))
6849 {
6850 ptrdiff_t ignore;
6851 int next_face_id;
6852 struct text_pos pos = it->current.pos;
6853 INC_TEXT_POS (pos, it->multibyte_p);
6854
6855 next_face_id = face_at_buffer_position
6856 (it->w, CHARPOS (pos), it->region_beg_charpos,
6857 it->region_end_charpos, &ignore,
6858 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6859 -1);
6860 it->end_of_box_run_p
6861 = (FACE_FROM_ID (it->f, next_face_id)->box
6862 == FACE_NO_BOX);
6863 }
6864 }
6865 }
6866 else
6867 {
6868 int face_id = face_after_it_pos (it);
6869 it->end_of_box_run_p
6870 = (face_id != it->face_id
6871 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6872 }
6873 }
6874 /* If we reached the end of the object we've been iterating (e.g., a
6875 display string or an overlay string), and there's something on
6876 IT->stack, proceed with what's on the stack. It doesn't make
6877 sense to return zero if there's unprocessed stuff on the stack,
6878 because otherwise that stuff will never be displayed. */
6879 if (!success_p && it->sp > 0)
6880 {
6881 set_iterator_to_next (it, 0);
6882 success_p = get_next_display_element (it);
6883 }
6884
6885 /* Value is 0 if end of buffer or string reached. */
6886 return success_p;
6887 }
6888
6889
6890 /* Move IT to the next display element.
6891
6892 RESEAT_P non-zero means if called on a newline in buffer text,
6893 skip to the next visible line start.
6894
6895 Functions get_next_display_element and set_iterator_to_next are
6896 separate because I find this arrangement easier to handle than a
6897 get_next_display_element function that also increments IT's
6898 position. The way it is we can first look at an iterator's current
6899 display element, decide whether it fits on a line, and if it does,
6900 increment the iterator position. The other way around we probably
6901 would either need a flag indicating whether the iterator has to be
6902 incremented the next time, or we would have to implement a
6903 decrement position function which would not be easy to write. */
6904
6905 void
6906 set_iterator_to_next (struct it *it, int reseat_p)
6907 {
6908 /* Reset flags indicating start and end of a sequence of characters
6909 with box. Reset them at the start of this function because
6910 moving the iterator to a new position might set them. */
6911 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6912
6913 switch (it->method)
6914 {
6915 case GET_FROM_BUFFER:
6916 /* The current display element of IT is a character from
6917 current_buffer. Advance in the buffer, and maybe skip over
6918 invisible lines that are so because of selective display. */
6919 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6920 reseat_at_next_visible_line_start (it, 0);
6921 else if (it->cmp_it.id >= 0)
6922 {
6923 /* We are currently getting glyphs from a composition. */
6924 int i;
6925
6926 if (! it->bidi_p)
6927 {
6928 IT_CHARPOS (*it) += it->cmp_it.nchars;
6929 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6930 if (it->cmp_it.to < it->cmp_it.nglyphs)
6931 {
6932 it->cmp_it.from = it->cmp_it.to;
6933 }
6934 else
6935 {
6936 it->cmp_it.id = -1;
6937 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6938 IT_BYTEPOS (*it),
6939 it->end_charpos, Qnil);
6940 }
6941 }
6942 else if (! it->cmp_it.reversed_p)
6943 {
6944 /* Composition created while scanning forward. */
6945 /* Update IT's char/byte positions to point to the first
6946 character of the next grapheme cluster, or to the
6947 character visually after the current composition. */
6948 for (i = 0; i < it->cmp_it.nchars; i++)
6949 bidi_move_to_visually_next (&it->bidi_it);
6950 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6951 IT_CHARPOS (*it) = it->bidi_it.charpos;
6952
6953 if (it->cmp_it.to < it->cmp_it.nglyphs)
6954 {
6955 /* Proceed to the next grapheme cluster. */
6956 it->cmp_it.from = it->cmp_it.to;
6957 }
6958 else
6959 {
6960 /* No more grapheme clusters in this composition.
6961 Find the next stop position. */
6962 ptrdiff_t stop = it->end_charpos;
6963 if (it->bidi_it.scan_dir < 0)
6964 /* Now we are scanning backward and don't know
6965 where to stop. */
6966 stop = -1;
6967 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6968 IT_BYTEPOS (*it), stop, Qnil);
6969 }
6970 }
6971 else
6972 {
6973 /* Composition created while scanning backward. */
6974 /* Update IT's char/byte positions to point to the last
6975 character of the previous grapheme cluster, or the
6976 character visually after the current composition. */
6977 for (i = 0; i < it->cmp_it.nchars; i++)
6978 bidi_move_to_visually_next (&it->bidi_it);
6979 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6980 IT_CHARPOS (*it) = it->bidi_it.charpos;
6981 if (it->cmp_it.from > 0)
6982 {
6983 /* Proceed to the previous grapheme cluster. */
6984 it->cmp_it.to = it->cmp_it.from;
6985 }
6986 else
6987 {
6988 /* No more grapheme clusters in this composition.
6989 Find the next stop position. */
6990 ptrdiff_t stop = it->end_charpos;
6991 if (it->bidi_it.scan_dir < 0)
6992 /* Now we are scanning backward and don't know
6993 where to stop. */
6994 stop = -1;
6995 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6996 IT_BYTEPOS (*it), stop, Qnil);
6997 }
6998 }
6999 }
7000 else
7001 {
7002 eassert (it->len != 0);
7003
7004 if (!it->bidi_p)
7005 {
7006 IT_BYTEPOS (*it) += it->len;
7007 IT_CHARPOS (*it) += 1;
7008 }
7009 else
7010 {
7011 int prev_scan_dir = it->bidi_it.scan_dir;
7012 /* If this is a new paragraph, determine its base
7013 direction (a.k.a. its base embedding level). */
7014 if (it->bidi_it.new_paragraph)
7015 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7016 bidi_move_to_visually_next (&it->bidi_it);
7017 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7018 IT_CHARPOS (*it) = it->bidi_it.charpos;
7019 if (prev_scan_dir != it->bidi_it.scan_dir)
7020 {
7021 /* As the scan direction was changed, we must
7022 re-compute the stop position for composition. */
7023 ptrdiff_t stop = it->end_charpos;
7024 if (it->bidi_it.scan_dir < 0)
7025 stop = -1;
7026 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7027 IT_BYTEPOS (*it), stop, Qnil);
7028 }
7029 }
7030 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7031 }
7032 break;
7033
7034 case GET_FROM_C_STRING:
7035 /* Current display element of IT is from a C string. */
7036 if (!it->bidi_p
7037 /* If the string position is beyond string's end, it means
7038 next_element_from_c_string is padding the string with
7039 blanks, in which case we bypass the bidi iterator,
7040 because it cannot deal with such virtual characters. */
7041 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7042 {
7043 IT_BYTEPOS (*it) += it->len;
7044 IT_CHARPOS (*it) += 1;
7045 }
7046 else
7047 {
7048 bidi_move_to_visually_next (&it->bidi_it);
7049 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7050 IT_CHARPOS (*it) = it->bidi_it.charpos;
7051 }
7052 break;
7053
7054 case GET_FROM_DISPLAY_VECTOR:
7055 /* Current display element of IT is from a display table entry.
7056 Advance in the display table definition. Reset it to null if
7057 end reached, and continue with characters from buffers/
7058 strings. */
7059 ++it->current.dpvec_index;
7060
7061 /* Restore face of the iterator to what they were before the
7062 display vector entry (these entries may contain faces). */
7063 it->face_id = it->saved_face_id;
7064
7065 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7066 {
7067 int recheck_faces = it->ellipsis_p;
7068
7069 if (it->s)
7070 it->method = GET_FROM_C_STRING;
7071 else if (STRINGP (it->string))
7072 it->method = GET_FROM_STRING;
7073 else
7074 {
7075 it->method = GET_FROM_BUFFER;
7076 it->object = it->w->buffer;
7077 }
7078
7079 it->dpvec = NULL;
7080 it->current.dpvec_index = -1;
7081
7082 /* Skip over characters which were displayed via IT->dpvec. */
7083 if (it->dpvec_char_len < 0)
7084 reseat_at_next_visible_line_start (it, 1);
7085 else if (it->dpvec_char_len > 0)
7086 {
7087 if (it->method == GET_FROM_STRING
7088 && it->n_overlay_strings > 0)
7089 it->ignore_overlay_strings_at_pos_p = 1;
7090 it->len = it->dpvec_char_len;
7091 set_iterator_to_next (it, reseat_p);
7092 }
7093
7094 /* Maybe recheck faces after display vector */
7095 if (recheck_faces)
7096 it->stop_charpos = IT_CHARPOS (*it);
7097 }
7098 break;
7099
7100 case GET_FROM_STRING:
7101 /* Current display element is a character from a Lisp string. */
7102 eassert (it->s == NULL && STRINGP (it->string));
7103 /* Don't advance past string end. These conditions are true
7104 when set_iterator_to_next is called at the end of
7105 get_next_display_element, in which case the Lisp string is
7106 already exhausted, and all we want is pop the iterator
7107 stack. */
7108 if (it->current.overlay_string_index >= 0)
7109 {
7110 /* This is an overlay string, so there's no padding with
7111 spaces, and the number of characters in the string is
7112 where the string ends. */
7113 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7114 goto consider_string_end;
7115 }
7116 else
7117 {
7118 /* Not an overlay string. There could be padding, so test
7119 against it->end_charpos . */
7120 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7121 goto consider_string_end;
7122 }
7123 if (it->cmp_it.id >= 0)
7124 {
7125 int i;
7126
7127 if (! it->bidi_p)
7128 {
7129 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7130 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7131 if (it->cmp_it.to < it->cmp_it.nglyphs)
7132 it->cmp_it.from = it->cmp_it.to;
7133 else
7134 {
7135 it->cmp_it.id = -1;
7136 composition_compute_stop_pos (&it->cmp_it,
7137 IT_STRING_CHARPOS (*it),
7138 IT_STRING_BYTEPOS (*it),
7139 it->end_charpos, it->string);
7140 }
7141 }
7142 else if (! it->cmp_it.reversed_p)
7143 {
7144 for (i = 0; i < it->cmp_it.nchars; i++)
7145 bidi_move_to_visually_next (&it->bidi_it);
7146 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7147 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7148
7149 if (it->cmp_it.to < it->cmp_it.nglyphs)
7150 it->cmp_it.from = it->cmp_it.to;
7151 else
7152 {
7153 ptrdiff_t stop = it->end_charpos;
7154 if (it->bidi_it.scan_dir < 0)
7155 stop = -1;
7156 composition_compute_stop_pos (&it->cmp_it,
7157 IT_STRING_CHARPOS (*it),
7158 IT_STRING_BYTEPOS (*it), stop,
7159 it->string);
7160 }
7161 }
7162 else
7163 {
7164 for (i = 0; i < it->cmp_it.nchars; i++)
7165 bidi_move_to_visually_next (&it->bidi_it);
7166 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7167 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7168 if (it->cmp_it.from > 0)
7169 it->cmp_it.to = it->cmp_it.from;
7170 else
7171 {
7172 ptrdiff_t stop = it->end_charpos;
7173 if (it->bidi_it.scan_dir < 0)
7174 stop = -1;
7175 composition_compute_stop_pos (&it->cmp_it,
7176 IT_STRING_CHARPOS (*it),
7177 IT_STRING_BYTEPOS (*it), stop,
7178 it->string);
7179 }
7180 }
7181 }
7182 else
7183 {
7184 if (!it->bidi_p
7185 /* If the string position is beyond string's end, it
7186 means next_element_from_string is padding the string
7187 with blanks, in which case we bypass the bidi
7188 iterator, because it cannot deal with such virtual
7189 characters. */
7190 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7191 {
7192 IT_STRING_BYTEPOS (*it) += it->len;
7193 IT_STRING_CHARPOS (*it) += 1;
7194 }
7195 else
7196 {
7197 int prev_scan_dir = it->bidi_it.scan_dir;
7198
7199 bidi_move_to_visually_next (&it->bidi_it);
7200 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7201 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7202 if (prev_scan_dir != it->bidi_it.scan_dir)
7203 {
7204 ptrdiff_t stop = it->end_charpos;
7205
7206 if (it->bidi_it.scan_dir < 0)
7207 stop = -1;
7208 composition_compute_stop_pos (&it->cmp_it,
7209 IT_STRING_CHARPOS (*it),
7210 IT_STRING_BYTEPOS (*it), stop,
7211 it->string);
7212 }
7213 }
7214 }
7215
7216 consider_string_end:
7217
7218 if (it->current.overlay_string_index >= 0)
7219 {
7220 /* IT->string is an overlay string. Advance to the
7221 next, if there is one. */
7222 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7223 {
7224 it->ellipsis_p = 0;
7225 next_overlay_string (it);
7226 if (it->ellipsis_p)
7227 setup_for_ellipsis (it, 0);
7228 }
7229 }
7230 else
7231 {
7232 /* IT->string is not an overlay string. If we reached
7233 its end, and there is something on IT->stack, proceed
7234 with what is on the stack. This can be either another
7235 string, this time an overlay string, or a buffer. */
7236 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7237 && it->sp > 0)
7238 {
7239 pop_it (it);
7240 if (it->method == GET_FROM_STRING)
7241 goto consider_string_end;
7242 }
7243 }
7244 break;
7245
7246 case GET_FROM_IMAGE:
7247 case GET_FROM_STRETCH:
7248 /* The position etc with which we have to proceed are on
7249 the stack. The position may be at the end of a string,
7250 if the `display' property takes up the whole string. */
7251 eassert (it->sp > 0);
7252 pop_it (it);
7253 if (it->method == GET_FROM_STRING)
7254 goto consider_string_end;
7255 break;
7256
7257 default:
7258 /* There are no other methods defined, so this should be a bug. */
7259 abort ();
7260 }
7261
7262 eassert (it->method != GET_FROM_STRING
7263 || (STRINGP (it->string)
7264 && IT_STRING_CHARPOS (*it) >= 0));
7265 }
7266
7267 /* Load IT's display element fields with information about the next
7268 display element which comes from a display table entry or from the
7269 result of translating a control character to one of the forms `^C'
7270 or `\003'.
7271
7272 IT->dpvec holds the glyphs to return as characters.
7273 IT->saved_face_id holds the face id before the display vector--it
7274 is restored into IT->face_id in set_iterator_to_next. */
7275
7276 static int
7277 next_element_from_display_vector (struct it *it)
7278 {
7279 Lisp_Object gc;
7280
7281 /* Precondition. */
7282 eassert (it->dpvec && it->current.dpvec_index >= 0);
7283
7284 it->face_id = it->saved_face_id;
7285
7286 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7287 That seemed totally bogus - so I changed it... */
7288 gc = it->dpvec[it->current.dpvec_index];
7289
7290 if (GLYPH_CODE_P (gc))
7291 {
7292 it->c = GLYPH_CODE_CHAR (gc);
7293 it->len = CHAR_BYTES (it->c);
7294
7295 /* The entry may contain a face id to use. Such a face id is
7296 the id of a Lisp face, not a realized face. A face id of
7297 zero means no face is specified. */
7298 if (it->dpvec_face_id >= 0)
7299 it->face_id = it->dpvec_face_id;
7300 else
7301 {
7302 int lface_id = GLYPH_CODE_FACE (gc);
7303 if (lface_id > 0)
7304 it->face_id = merge_faces (it->f, Qt, lface_id,
7305 it->saved_face_id);
7306 }
7307 }
7308 else
7309 /* Display table entry is invalid. Return a space. */
7310 it->c = ' ', it->len = 1;
7311
7312 /* Don't change position and object of the iterator here. They are
7313 still the values of the character that had this display table
7314 entry or was translated, and that's what we want. */
7315 it->what = IT_CHARACTER;
7316 return 1;
7317 }
7318
7319 /* Get the first element of string/buffer in the visual order, after
7320 being reseated to a new position in a string or a buffer. */
7321 static void
7322 get_visually_first_element (struct it *it)
7323 {
7324 int string_p = STRINGP (it->string) || it->s;
7325 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7326 ptrdiff_t bob = (string_p ? 0 : BEGV);
7327
7328 if (STRINGP (it->string))
7329 {
7330 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7331 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7332 }
7333 else
7334 {
7335 it->bidi_it.charpos = IT_CHARPOS (*it);
7336 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7337 }
7338
7339 if (it->bidi_it.charpos == eob)
7340 {
7341 /* Nothing to do, but reset the FIRST_ELT flag, like
7342 bidi_paragraph_init does, because we are not going to
7343 call it. */
7344 it->bidi_it.first_elt = 0;
7345 }
7346 else if (it->bidi_it.charpos == bob
7347 || (!string_p
7348 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7349 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7350 {
7351 /* If we are at the beginning of a line/string, we can produce
7352 the next element right away. */
7353 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7354 bidi_move_to_visually_next (&it->bidi_it);
7355 }
7356 else
7357 {
7358 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7359
7360 /* We need to prime the bidi iterator starting at the line's or
7361 string's beginning, before we will be able to produce the
7362 next element. */
7363 if (string_p)
7364 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7365 else
7366 {
7367 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7368 -1);
7369 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7370 }
7371 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7372 do
7373 {
7374 /* Now return to buffer/string position where we were asked
7375 to get the next display element, and produce that. */
7376 bidi_move_to_visually_next (&it->bidi_it);
7377 }
7378 while (it->bidi_it.bytepos != orig_bytepos
7379 && it->bidi_it.charpos < eob);
7380 }
7381
7382 /* Adjust IT's position information to where we ended up. */
7383 if (STRINGP (it->string))
7384 {
7385 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7386 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7387 }
7388 else
7389 {
7390 IT_CHARPOS (*it) = it->bidi_it.charpos;
7391 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7392 }
7393
7394 if (STRINGP (it->string) || !it->s)
7395 {
7396 ptrdiff_t stop, charpos, bytepos;
7397
7398 if (STRINGP (it->string))
7399 {
7400 eassert (!it->s);
7401 stop = SCHARS (it->string);
7402 if (stop > it->end_charpos)
7403 stop = it->end_charpos;
7404 charpos = IT_STRING_CHARPOS (*it);
7405 bytepos = IT_STRING_BYTEPOS (*it);
7406 }
7407 else
7408 {
7409 stop = it->end_charpos;
7410 charpos = IT_CHARPOS (*it);
7411 bytepos = IT_BYTEPOS (*it);
7412 }
7413 if (it->bidi_it.scan_dir < 0)
7414 stop = -1;
7415 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7416 it->string);
7417 }
7418 }
7419
7420 /* Load IT with the next display element from Lisp string IT->string.
7421 IT->current.string_pos is the current position within the string.
7422 If IT->current.overlay_string_index >= 0, the Lisp string is an
7423 overlay string. */
7424
7425 static int
7426 next_element_from_string (struct it *it)
7427 {
7428 struct text_pos position;
7429
7430 eassert (STRINGP (it->string));
7431 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7432 eassert (IT_STRING_CHARPOS (*it) >= 0);
7433 position = it->current.string_pos;
7434
7435 /* With bidi reordering, the character to display might not be the
7436 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7437 that we were reseat()ed to a new string, whose paragraph
7438 direction is not known. */
7439 if (it->bidi_p && it->bidi_it.first_elt)
7440 {
7441 get_visually_first_element (it);
7442 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7443 }
7444
7445 /* Time to check for invisible text? */
7446 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7447 {
7448 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7449 {
7450 if (!(!it->bidi_p
7451 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7452 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7453 {
7454 /* With bidi non-linear iteration, we could find
7455 ourselves far beyond the last computed stop_charpos,
7456 with several other stop positions in between that we
7457 missed. Scan them all now, in buffer's logical
7458 order, until we find and handle the last stop_charpos
7459 that precedes our current position. */
7460 handle_stop_backwards (it, it->stop_charpos);
7461 return GET_NEXT_DISPLAY_ELEMENT (it);
7462 }
7463 else
7464 {
7465 if (it->bidi_p)
7466 {
7467 /* Take note of the stop position we just moved
7468 across, for when we will move back across it. */
7469 it->prev_stop = it->stop_charpos;
7470 /* If we are at base paragraph embedding level, take
7471 note of the last stop position seen at this
7472 level. */
7473 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7474 it->base_level_stop = it->stop_charpos;
7475 }
7476 handle_stop (it);
7477
7478 /* Since a handler may have changed IT->method, we must
7479 recurse here. */
7480 return GET_NEXT_DISPLAY_ELEMENT (it);
7481 }
7482 }
7483 else if (it->bidi_p
7484 /* If we are before prev_stop, we may have overstepped
7485 on our way backwards a stop_pos, and if so, we need
7486 to handle that stop_pos. */
7487 && IT_STRING_CHARPOS (*it) < it->prev_stop
7488 /* We can sometimes back up for reasons that have nothing
7489 to do with bidi reordering. E.g., compositions. The
7490 code below is only needed when we are above the base
7491 embedding level, so test for that explicitly. */
7492 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7493 {
7494 /* If we lost track of base_level_stop, we have no better
7495 place for handle_stop_backwards to start from than string
7496 beginning. This happens, e.g., when we were reseated to
7497 the previous screenful of text by vertical-motion. */
7498 if (it->base_level_stop <= 0
7499 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7500 it->base_level_stop = 0;
7501 handle_stop_backwards (it, it->base_level_stop);
7502 return GET_NEXT_DISPLAY_ELEMENT (it);
7503 }
7504 }
7505
7506 if (it->current.overlay_string_index >= 0)
7507 {
7508 /* Get the next character from an overlay string. In overlay
7509 strings, there is no field width or padding with spaces to
7510 do. */
7511 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7512 {
7513 it->what = IT_EOB;
7514 return 0;
7515 }
7516 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7517 IT_STRING_BYTEPOS (*it),
7518 it->bidi_it.scan_dir < 0
7519 ? -1
7520 : SCHARS (it->string))
7521 && next_element_from_composition (it))
7522 {
7523 return 1;
7524 }
7525 else if (STRING_MULTIBYTE (it->string))
7526 {
7527 const unsigned char *s = (SDATA (it->string)
7528 + IT_STRING_BYTEPOS (*it));
7529 it->c = string_char_and_length (s, &it->len);
7530 }
7531 else
7532 {
7533 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7534 it->len = 1;
7535 }
7536 }
7537 else
7538 {
7539 /* Get the next character from a Lisp string that is not an
7540 overlay string. Such strings come from the mode line, for
7541 example. We may have to pad with spaces, or truncate the
7542 string. See also next_element_from_c_string. */
7543 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7544 {
7545 it->what = IT_EOB;
7546 return 0;
7547 }
7548 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7549 {
7550 /* Pad with spaces. */
7551 it->c = ' ', it->len = 1;
7552 CHARPOS (position) = BYTEPOS (position) = -1;
7553 }
7554 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7555 IT_STRING_BYTEPOS (*it),
7556 it->bidi_it.scan_dir < 0
7557 ? -1
7558 : it->string_nchars)
7559 && next_element_from_composition (it))
7560 {
7561 return 1;
7562 }
7563 else if (STRING_MULTIBYTE (it->string))
7564 {
7565 const unsigned char *s = (SDATA (it->string)
7566 + IT_STRING_BYTEPOS (*it));
7567 it->c = string_char_and_length (s, &it->len);
7568 }
7569 else
7570 {
7571 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7572 it->len = 1;
7573 }
7574 }
7575
7576 /* Record what we have and where it came from. */
7577 it->what = IT_CHARACTER;
7578 it->object = it->string;
7579 it->position = position;
7580 return 1;
7581 }
7582
7583
7584 /* Load IT with next display element from C string IT->s.
7585 IT->string_nchars is the maximum number of characters to return
7586 from the string. IT->end_charpos may be greater than
7587 IT->string_nchars when this function is called, in which case we
7588 may have to return padding spaces. Value is zero if end of string
7589 reached, including padding spaces. */
7590
7591 static int
7592 next_element_from_c_string (struct it *it)
7593 {
7594 int success_p = 1;
7595
7596 eassert (it->s);
7597 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7598 it->what = IT_CHARACTER;
7599 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7600 it->object = Qnil;
7601
7602 /* With bidi reordering, the character to display might not be the
7603 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7604 we were reseated to a new string, whose paragraph direction is
7605 not known. */
7606 if (it->bidi_p && it->bidi_it.first_elt)
7607 get_visually_first_element (it);
7608
7609 /* IT's position can be greater than IT->string_nchars in case a
7610 field width or precision has been specified when the iterator was
7611 initialized. */
7612 if (IT_CHARPOS (*it) >= it->end_charpos)
7613 {
7614 /* End of the game. */
7615 it->what = IT_EOB;
7616 success_p = 0;
7617 }
7618 else if (IT_CHARPOS (*it) >= it->string_nchars)
7619 {
7620 /* Pad with spaces. */
7621 it->c = ' ', it->len = 1;
7622 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7623 }
7624 else if (it->multibyte_p)
7625 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7626 else
7627 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7628
7629 return success_p;
7630 }
7631
7632
7633 /* Set up IT to return characters from an ellipsis, if appropriate.
7634 The definition of the ellipsis glyphs may come from a display table
7635 entry. This function fills IT with the first glyph from the
7636 ellipsis if an ellipsis is to be displayed. */
7637
7638 static int
7639 next_element_from_ellipsis (struct it *it)
7640 {
7641 if (it->selective_display_ellipsis_p)
7642 setup_for_ellipsis (it, it->len);
7643 else
7644 {
7645 /* The face at the current position may be different from the
7646 face we find after the invisible text. Remember what it
7647 was in IT->saved_face_id, and signal that it's there by
7648 setting face_before_selective_p. */
7649 it->saved_face_id = it->face_id;
7650 it->method = GET_FROM_BUFFER;
7651 it->object = it->w->buffer;
7652 reseat_at_next_visible_line_start (it, 1);
7653 it->face_before_selective_p = 1;
7654 }
7655
7656 return GET_NEXT_DISPLAY_ELEMENT (it);
7657 }
7658
7659
7660 /* Deliver an image display element. The iterator IT is already
7661 filled with image information (done in handle_display_prop). Value
7662 is always 1. */
7663
7664
7665 static int
7666 next_element_from_image (struct it *it)
7667 {
7668 it->what = IT_IMAGE;
7669 it->ignore_overlay_strings_at_pos_p = 0;
7670 return 1;
7671 }
7672
7673
7674 /* Fill iterator IT with next display element from a stretch glyph
7675 property. IT->object is the value of the text property. Value is
7676 always 1. */
7677
7678 static int
7679 next_element_from_stretch (struct it *it)
7680 {
7681 it->what = IT_STRETCH;
7682 return 1;
7683 }
7684
7685 /* Scan backwards from IT's current position until we find a stop
7686 position, or until BEGV. This is called when we find ourself
7687 before both the last known prev_stop and base_level_stop while
7688 reordering bidirectional text. */
7689
7690 static void
7691 compute_stop_pos_backwards (struct it *it)
7692 {
7693 const int SCAN_BACK_LIMIT = 1000;
7694 struct text_pos pos;
7695 struct display_pos save_current = it->current;
7696 struct text_pos save_position = it->position;
7697 ptrdiff_t charpos = IT_CHARPOS (*it);
7698 ptrdiff_t where_we_are = charpos;
7699 ptrdiff_t save_stop_pos = it->stop_charpos;
7700 ptrdiff_t save_end_pos = it->end_charpos;
7701
7702 eassert (NILP (it->string) && !it->s);
7703 eassert (it->bidi_p);
7704 it->bidi_p = 0;
7705 do
7706 {
7707 it->end_charpos = min (charpos + 1, ZV);
7708 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7709 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7710 reseat_1 (it, pos, 0);
7711 compute_stop_pos (it);
7712 /* We must advance forward, right? */
7713 if (it->stop_charpos <= charpos)
7714 abort ();
7715 }
7716 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7717
7718 if (it->stop_charpos <= where_we_are)
7719 it->prev_stop = it->stop_charpos;
7720 else
7721 it->prev_stop = BEGV;
7722 it->bidi_p = 1;
7723 it->current = save_current;
7724 it->position = save_position;
7725 it->stop_charpos = save_stop_pos;
7726 it->end_charpos = save_end_pos;
7727 }
7728
7729 /* Scan forward from CHARPOS in the current buffer/string, until we
7730 find a stop position > current IT's position. Then handle the stop
7731 position before that. This is called when we bump into a stop
7732 position while reordering bidirectional text. CHARPOS should be
7733 the last previously processed stop_pos (or BEGV/0, if none were
7734 processed yet) whose position is less that IT's current
7735 position. */
7736
7737 static void
7738 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7739 {
7740 int bufp = !STRINGP (it->string);
7741 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7742 struct display_pos save_current = it->current;
7743 struct text_pos save_position = it->position;
7744 struct text_pos pos1;
7745 ptrdiff_t next_stop;
7746
7747 /* Scan in strict logical order. */
7748 eassert (it->bidi_p);
7749 it->bidi_p = 0;
7750 do
7751 {
7752 it->prev_stop = charpos;
7753 if (bufp)
7754 {
7755 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7756 reseat_1 (it, pos1, 0);
7757 }
7758 else
7759 it->current.string_pos = string_pos (charpos, it->string);
7760 compute_stop_pos (it);
7761 /* We must advance forward, right? */
7762 if (it->stop_charpos <= it->prev_stop)
7763 abort ();
7764 charpos = it->stop_charpos;
7765 }
7766 while (charpos <= where_we_are);
7767
7768 it->bidi_p = 1;
7769 it->current = save_current;
7770 it->position = save_position;
7771 next_stop = it->stop_charpos;
7772 it->stop_charpos = it->prev_stop;
7773 handle_stop (it);
7774 it->stop_charpos = next_stop;
7775 }
7776
7777 /* Load IT with the next display element from current_buffer. Value
7778 is zero if end of buffer reached. IT->stop_charpos is the next
7779 position at which to stop and check for text properties or buffer
7780 end. */
7781
7782 static int
7783 next_element_from_buffer (struct it *it)
7784 {
7785 int success_p = 1;
7786
7787 eassert (IT_CHARPOS (*it) >= BEGV);
7788 eassert (NILP (it->string) && !it->s);
7789 eassert (!it->bidi_p
7790 || (EQ (it->bidi_it.string.lstring, Qnil)
7791 && it->bidi_it.string.s == NULL));
7792
7793 /* With bidi reordering, the character to display might not be the
7794 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7795 we were reseat()ed to a new buffer position, which is potentially
7796 a different paragraph. */
7797 if (it->bidi_p && it->bidi_it.first_elt)
7798 {
7799 get_visually_first_element (it);
7800 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7801 }
7802
7803 if (IT_CHARPOS (*it) >= it->stop_charpos)
7804 {
7805 if (IT_CHARPOS (*it) >= it->end_charpos)
7806 {
7807 int overlay_strings_follow_p;
7808
7809 /* End of the game, except when overlay strings follow that
7810 haven't been returned yet. */
7811 if (it->overlay_strings_at_end_processed_p)
7812 overlay_strings_follow_p = 0;
7813 else
7814 {
7815 it->overlay_strings_at_end_processed_p = 1;
7816 overlay_strings_follow_p = get_overlay_strings (it, 0);
7817 }
7818
7819 if (overlay_strings_follow_p)
7820 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7821 else
7822 {
7823 it->what = IT_EOB;
7824 it->position = it->current.pos;
7825 success_p = 0;
7826 }
7827 }
7828 else if (!(!it->bidi_p
7829 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7830 || IT_CHARPOS (*it) == it->stop_charpos))
7831 {
7832 /* With bidi non-linear iteration, we could find ourselves
7833 far beyond the last computed stop_charpos, with several
7834 other stop positions in between that we missed. Scan
7835 them all now, in buffer's logical order, until we find
7836 and handle the last stop_charpos that precedes our
7837 current position. */
7838 handle_stop_backwards (it, it->stop_charpos);
7839 return GET_NEXT_DISPLAY_ELEMENT (it);
7840 }
7841 else
7842 {
7843 if (it->bidi_p)
7844 {
7845 /* Take note of the stop position we just moved across,
7846 for when we will move back across it. */
7847 it->prev_stop = it->stop_charpos;
7848 /* If we are at base paragraph embedding level, take
7849 note of the last stop position seen at this
7850 level. */
7851 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7852 it->base_level_stop = it->stop_charpos;
7853 }
7854 handle_stop (it);
7855 return GET_NEXT_DISPLAY_ELEMENT (it);
7856 }
7857 }
7858 else if (it->bidi_p
7859 /* If we are before prev_stop, we may have overstepped on
7860 our way backwards a stop_pos, and if so, we need to
7861 handle that stop_pos. */
7862 && IT_CHARPOS (*it) < it->prev_stop
7863 /* We can sometimes back up for reasons that have nothing
7864 to do with bidi reordering. E.g., compositions. The
7865 code below is only needed when we are above the base
7866 embedding level, so test for that explicitly. */
7867 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7868 {
7869 if (it->base_level_stop <= 0
7870 || IT_CHARPOS (*it) < it->base_level_stop)
7871 {
7872 /* If we lost track of base_level_stop, we need to find
7873 prev_stop by looking backwards. This happens, e.g., when
7874 we were reseated to the previous screenful of text by
7875 vertical-motion. */
7876 it->base_level_stop = BEGV;
7877 compute_stop_pos_backwards (it);
7878 handle_stop_backwards (it, it->prev_stop);
7879 }
7880 else
7881 handle_stop_backwards (it, it->base_level_stop);
7882 return GET_NEXT_DISPLAY_ELEMENT (it);
7883 }
7884 else
7885 {
7886 /* No face changes, overlays etc. in sight, so just return a
7887 character from current_buffer. */
7888 unsigned char *p;
7889 ptrdiff_t stop;
7890
7891 /* Maybe run the redisplay end trigger hook. Performance note:
7892 This doesn't seem to cost measurable time. */
7893 if (it->redisplay_end_trigger_charpos
7894 && it->glyph_row
7895 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7896 run_redisplay_end_trigger_hook (it);
7897
7898 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7899 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7900 stop)
7901 && next_element_from_composition (it))
7902 {
7903 return 1;
7904 }
7905
7906 /* Get the next character, maybe multibyte. */
7907 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7908 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7909 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7910 else
7911 it->c = *p, it->len = 1;
7912
7913 /* Record what we have and where it came from. */
7914 it->what = IT_CHARACTER;
7915 it->object = it->w->buffer;
7916 it->position = it->current.pos;
7917
7918 /* Normally we return the character found above, except when we
7919 really want to return an ellipsis for selective display. */
7920 if (it->selective)
7921 {
7922 if (it->c == '\n')
7923 {
7924 /* A value of selective > 0 means hide lines indented more
7925 than that number of columns. */
7926 if (it->selective > 0
7927 && IT_CHARPOS (*it) + 1 < ZV
7928 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7929 IT_BYTEPOS (*it) + 1,
7930 it->selective))
7931 {
7932 success_p = next_element_from_ellipsis (it);
7933 it->dpvec_char_len = -1;
7934 }
7935 }
7936 else if (it->c == '\r' && it->selective == -1)
7937 {
7938 /* A value of selective == -1 means that everything from the
7939 CR to the end of the line is invisible, with maybe an
7940 ellipsis displayed for it. */
7941 success_p = next_element_from_ellipsis (it);
7942 it->dpvec_char_len = -1;
7943 }
7944 }
7945 }
7946
7947 /* Value is zero if end of buffer reached. */
7948 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7949 return success_p;
7950 }
7951
7952
7953 /* Run the redisplay end trigger hook for IT. */
7954
7955 static void
7956 run_redisplay_end_trigger_hook (struct it *it)
7957 {
7958 Lisp_Object args[3];
7959
7960 /* IT->glyph_row should be non-null, i.e. we should be actually
7961 displaying something, or otherwise we should not run the hook. */
7962 eassert (it->glyph_row);
7963
7964 /* Set up hook arguments. */
7965 args[0] = Qredisplay_end_trigger_functions;
7966 args[1] = it->window;
7967 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7968 it->redisplay_end_trigger_charpos = 0;
7969
7970 /* Since we are *trying* to run these functions, don't try to run
7971 them again, even if they get an error. */
7972 it->w->redisplay_end_trigger = Qnil;
7973 Frun_hook_with_args (3, args);
7974
7975 /* Notice if it changed the face of the character we are on. */
7976 handle_face_prop (it);
7977 }
7978
7979
7980 /* Deliver a composition display element. Unlike the other
7981 next_element_from_XXX, this function is not registered in the array
7982 get_next_element[]. It is called from next_element_from_buffer and
7983 next_element_from_string when necessary. */
7984
7985 static int
7986 next_element_from_composition (struct it *it)
7987 {
7988 it->what = IT_COMPOSITION;
7989 it->len = it->cmp_it.nbytes;
7990 if (STRINGP (it->string))
7991 {
7992 if (it->c < 0)
7993 {
7994 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7995 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7996 return 0;
7997 }
7998 it->position = it->current.string_pos;
7999 it->object = it->string;
8000 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8001 IT_STRING_BYTEPOS (*it), it->string);
8002 }
8003 else
8004 {
8005 if (it->c < 0)
8006 {
8007 IT_CHARPOS (*it) += it->cmp_it.nchars;
8008 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8009 if (it->bidi_p)
8010 {
8011 if (it->bidi_it.new_paragraph)
8012 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8013 /* Resync the bidi iterator with IT's new position.
8014 FIXME: this doesn't support bidirectional text. */
8015 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8016 bidi_move_to_visually_next (&it->bidi_it);
8017 }
8018 return 0;
8019 }
8020 it->position = it->current.pos;
8021 it->object = it->w->buffer;
8022 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8023 IT_BYTEPOS (*it), Qnil);
8024 }
8025 return 1;
8026 }
8027
8028
8029 \f
8030 /***********************************************************************
8031 Moving an iterator without producing glyphs
8032 ***********************************************************************/
8033
8034 /* Check if iterator is at a position corresponding to a valid buffer
8035 position after some move_it_ call. */
8036
8037 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8038 ((it)->method == GET_FROM_STRING \
8039 ? IT_STRING_CHARPOS (*it) == 0 \
8040 : 1)
8041
8042
8043 /* Move iterator IT to a specified buffer or X position within one
8044 line on the display without producing glyphs.
8045
8046 OP should be a bit mask including some or all of these bits:
8047 MOVE_TO_X: Stop upon reaching x-position TO_X.
8048 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8049 Regardless of OP's value, stop upon reaching the end of the display line.
8050
8051 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8052 This means, in particular, that TO_X includes window's horizontal
8053 scroll amount.
8054
8055 The return value has several possible values that
8056 say what condition caused the scan to stop:
8057
8058 MOVE_POS_MATCH_OR_ZV
8059 - when TO_POS or ZV was reached.
8060
8061 MOVE_X_REACHED
8062 -when TO_X was reached before TO_POS or ZV were reached.
8063
8064 MOVE_LINE_CONTINUED
8065 - when we reached the end of the display area and the line must
8066 be continued.
8067
8068 MOVE_LINE_TRUNCATED
8069 - when we reached the end of the display area and the line is
8070 truncated.
8071
8072 MOVE_NEWLINE_OR_CR
8073 - when we stopped at a line end, i.e. a newline or a CR and selective
8074 display is on. */
8075
8076 static enum move_it_result
8077 move_it_in_display_line_to (struct it *it,
8078 ptrdiff_t to_charpos, int to_x,
8079 enum move_operation_enum op)
8080 {
8081 enum move_it_result result = MOVE_UNDEFINED;
8082 struct glyph_row *saved_glyph_row;
8083 struct it wrap_it, atpos_it, atx_it, ppos_it;
8084 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8085 void *ppos_data = NULL;
8086 int may_wrap = 0;
8087 enum it_method prev_method = it->method;
8088 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8089 int saw_smaller_pos = prev_pos < to_charpos;
8090
8091 /* Don't produce glyphs in produce_glyphs. */
8092 saved_glyph_row = it->glyph_row;
8093 it->glyph_row = NULL;
8094
8095 /* Use wrap_it to save a copy of IT wherever a word wrap could
8096 occur. Use atpos_it to save a copy of IT at the desired buffer
8097 position, if found, so that we can scan ahead and check if the
8098 word later overshoots the window edge. Use atx_it similarly, for
8099 pixel positions. */
8100 wrap_it.sp = -1;
8101 atpos_it.sp = -1;
8102 atx_it.sp = -1;
8103
8104 /* Use ppos_it under bidi reordering to save a copy of IT for the
8105 position > CHARPOS that is the closest to CHARPOS. We restore
8106 that position in IT when we have scanned the entire display line
8107 without finding a match for CHARPOS and all the character
8108 positions are greater than CHARPOS. */
8109 if (it->bidi_p)
8110 {
8111 SAVE_IT (ppos_it, *it, ppos_data);
8112 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8113 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8114 SAVE_IT (ppos_it, *it, ppos_data);
8115 }
8116
8117 #define BUFFER_POS_REACHED_P() \
8118 ((op & MOVE_TO_POS) != 0 \
8119 && BUFFERP (it->object) \
8120 && (IT_CHARPOS (*it) == to_charpos \
8121 || ((!it->bidi_p \
8122 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8123 && IT_CHARPOS (*it) > to_charpos) \
8124 || (it->what == IT_COMPOSITION \
8125 && ((IT_CHARPOS (*it) > to_charpos \
8126 && to_charpos >= it->cmp_it.charpos) \
8127 || (IT_CHARPOS (*it) < to_charpos \
8128 && to_charpos <= it->cmp_it.charpos)))) \
8129 && (it->method == GET_FROM_BUFFER \
8130 || (it->method == GET_FROM_DISPLAY_VECTOR \
8131 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8132
8133 /* If there's a line-/wrap-prefix, handle it. */
8134 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8135 && it->current_y < it->last_visible_y)
8136 handle_line_prefix (it);
8137
8138 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8139 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8140
8141 while (1)
8142 {
8143 int x, i, ascent = 0, descent = 0;
8144
8145 /* Utility macro to reset an iterator with x, ascent, and descent. */
8146 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8147 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8148 (IT)->max_descent = descent)
8149
8150 /* Stop if we move beyond TO_CHARPOS (after an image or a
8151 display string or stretch glyph). */
8152 if ((op & MOVE_TO_POS) != 0
8153 && BUFFERP (it->object)
8154 && it->method == GET_FROM_BUFFER
8155 && (((!it->bidi_p
8156 /* When the iterator is at base embedding level, we
8157 are guaranteed that characters are delivered for
8158 display in strictly increasing order of their
8159 buffer positions. */
8160 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8161 && IT_CHARPOS (*it) > to_charpos)
8162 || (it->bidi_p
8163 && (prev_method == GET_FROM_IMAGE
8164 || prev_method == GET_FROM_STRETCH
8165 || prev_method == GET_FROM_STRING)
8166 /* Passed TO_CHARPOS from left to right. */
8167 && ((prev_pos < to_charpos
8168 && IT_CHARPOS (*it) > to_charpos)
8169 /* Passed TO_CHARPOS from right to left. */
8170 || (prev_pos > to_charpos
8171 && IT_CHARPOS (*it) < to_charpos)))))
8172 {
8173 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8174 {
8175 result = MOVE_POS_MATCH_OR_ZV;
8176 break;
8177 }
8178 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8179 /* If wrap_it is valid, the current position might be in a
8180 word that is wrapped. So, save the iterator in
8181 atpos_it and continue to see if wrapping happens. */
8182 SAVE_IT (atpos_it, *it, atpos_data);
8183 }
8184
8185 /* Stop when ZV reached.
8186 We used to stop here when TO_CHARPOS reached as well, but that is
8187 too soon if this glyph does not fit on this line. So we handle it
8188 explicitly below. */
8189 if (!get_next_display_element (it))
8190 {
8191 result = MOVE_POS_MATCH_OR_ZV;
8192 break;
8193 }
8194
8195 if (it->line_wrap == TRUNCATE)
8196 {
8197 if (BUFFER_POS_REACHED_P ())
8198 {
8199 result = MOVE_POS_MATCH_OR_ZV;
8200 break;
8201 }
8202 }
8203 else
8204 {
8205 if (it->line_wrap == WORD_WRAP)
8206 {
8207 if (IT_DISPLAYING_WHITESPACE (it))
8208 may_wrap = 1;
8209 else if (may_wrap)
8210 {
8211 /* We have reached a glyph that follows one or more
8212 whitespace characters. If the position is
8213 already found, we are done. */
8214 if (atpos_it.sp >= 0)
8215 {
8216 RESTORE_IT (it, &atpos_it, atpos_data);
8217 result = MOVE_POS_MATCH_OR_ZV;
8218 goto done;
8219 }
8220 if (atx_it.sp >= 0)
8221 {
8222 RESTORE_IT (it, &atx_it, atx_data);
8223 result = MOVE_X_REACHED;
8224 goto done;
8225 }
8226 /* Otherwise, we can wrap here. */
8227 SAVE_IT (wrap_it, *it, wrap_data);
8228 may_wrap = 0;
8229 }
8230 }
8231 }
8232
8233 /* Remember the line height for the current line, in case
8234 the next element doesn't fit on the line. */
8235 ascent = it->max_ascent;
8236 descent = it->max_descent;
8237
8238 /* The call to produce_glyphs will get the metrics of the
8239 display element IT is loaded with. Record the x-position
8240 before this display element, in case it doesn't fit on the
8241 line. */
8242 x = it->current_x;
8243
8244 PRODUCE_GLYPHS (it);
8245
8246 if (it->area != TEXT_AREA)
8247 {
8248 prev_method = it->method;
8249 if (it->method == GET_FROM_BUFFER)
8250 prev_pos = IT_CHARPOS (*it);
8251 set_iterator_to_next (it, 1);
8252 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8253 SET_TEXT_POS (this_line_min_pos,
8254 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8255 if (it->bidi_p
8256 && (op & MOVE_TO_POS)
8257 && IT_CHARPOS (*it) > to_charpos
8258 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8259 SAVE_IT (ppos_it, *it, ppos_data);
8260 continue;
8261 }
8262
8263 /* The number of glyphs we get back in IT->nglyphs will normally
8264 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8265 character on a terminal frame, or (iii) a line end. For the
8266 second case, IT->nglyphs - 1 padding glyphs will be present.
8267 (On X frames, there is only one glyph produced for a
8268 composite character.)
8269
8270 The behavior implemented below means, for continuation lines,
8271 that as many spaces of a TAB as fit on the current line are
8272 displayed there. For terminal frames, as many glyphs of a
8273 multi-glyph character are displayed in the current line, too.
8274 This is what the old redisplay code did, and we keep it that
8275 way. Under X, the whole shape of a complex character must
8276 fit on the line or it will be completely displayed in the
8277 next line.
8278
8279 Note that both for tabs and padding glyphs, all glyphs have
8280 the same width. */
8281 if (it->nglyphs)
8282 {
8283 /* More than one glyph or glyph doesn't fit on line. All
8284 glyphs have the same width. */
8285 int single_glyph_width = it->pixel_width / it->nglyphs;
8286 int new_x;
8287 int x_before_this_char = x;
8288 int hpos_before_this_char = it->hpos;
8289
8290 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8291 {
8292 new_x = x + single_glyph_width;
8293
8294 /* We want to leave anything reaching TO_X to the caller. */
8295 if ((op & MOVE_TO_X) && new_x > to_x)
8296 {
8297 if (BUFFER_POS_REACHED_P ())
8298 {
8299 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8300 goto buffer_pos_reached;
8301 if (atpos_it.sp < 0)
8302 {
8303 SAVE_IT (atpos_it, *it, atpos_data);
8304 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8305 }
8306 }
8307 else
8308 {
8309 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8310 {
8311 it->current_x = x;
8312 result = MOVE_X_REACHED;
8313 break;
8314 }
8315 if (atx_it.sp < 0)
8316 {
8317 SAVE_IT (atx_it, *it, atx_data);
8318 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8319 }
8320 }
8321 }
8322
8323 if (/* Lines are continued. */
8324 it->line_wrap != TRUNCATE
8325 && (/* And glyph doesn't fit on the line. */
8326 new_x > it->last_visible_x
8327 /* Or it fits exactly and we're on a window
8328 system frame. */
8329 || (new_x == it->last_visible_x
8330 && FRAME_WINDOW_P (it->f))))
8331 {
8332 if (/* IT->hpos == 0 means the very first glyph
8333 doesn't fit on the line, e.g. a wide image. */
8334 it->hpos == 0
8335 || (new_x == it->last_visible_x
8336 && FRAME_WINDOW_P (it->f)))
8337 {
8338 ++it->hpos;
8339 it->current_x = new_x;
8340
8341 /* The character's last glyph just barely fits
8342 in this row. */
8343 if (i == it->nglyphs - 1)
8344 {
8345 /* If this is the destination position,
8346 return a position *before* it in this row,
8347 now that we know it fits in this row. */
8348 if (BUFFER_POS_REACHED_P ())
8349 {
8350 if (it->line_wrap != WORD_WRAP
8351 || wrap_it.sp < 0)
8352 {
8353 it->hpos = hpos_before_this_char;
8354 it->current_x = x_before_this_char;
8355 result = MOVE_POS_MATCH_OR_ZV;
8356 break;
8357 }
8358 if (it->line_wrap == WORD_WRAP
8359 && atpos_it.sp < 0)
8360 {
8361 SAVE_IT (atpos_it, *it, atpos_data);
8362 atpos_it.current_x = x_before_this_char;
8363 atpos_it.hpos = hpos_before_this_char;
8364 }
8365 }
8366
8367 prev_method = it->method;
8368 if (it->method == GET_FROM_BUFFER)
8369 prev_pos = IT_CHARPOS (*it);
8370 set_iterator_to_next (it, 1);
8371 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8372 SET_TEXT_POS (this_line_min_pos,
8373 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8374 /* On graphical terminals, newlines may
8375 "overflow" into the fringe if
8376 overflow-newline-into-fringe is non-nil.
8377 On text terminals, newlines may overflow
8378 into the last glyph on the display
8379 line.*/
8380 if (!FRAME_WINDOW_P (it->f)
8381 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8382 {
8383 if (!get_next_display_element (it))
8384 {
8385 result = MOVE_POS_MATCH_OR_ZV;
8386 break;
8387 }
8388 if (BUFFER_POS_REACHED_P ())
8389 {
8390 if (ITERATOR_AT_END_OF_LINE_P (it))
8391 result = MOVE_POS_MATCH_OR_ZV;
8392 else
8393 result = MOVE_LINE_CONTINUED;
8394 break;
8395 }
8396 if (ITERATOR_AT_END_OF_LINE_P (it))
8397 {
8398 result = MOVE_NEWLINE_OR_CR;
8399 break;
8400 }
8401 }
8402 }
8403 }
8404 else
8405 IT_RESET_X_ASCENT_DESCENT (it);
8406
8407 if (wrap_it.sp >= 0)
8408 {
8409 RESTORE_IT (it, &wrap_it, wrap_data);
8410 atpos_it.sp = -1;
8411 atx_it.sp = -1;
8412 }
8413
8414 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8415 IT_CHARPOS (*it)));
8416 result = MOVE_LINE_CONTINUED;
8417 break;
8418 }
8419
8420 if (BUFFER_POS_REACHED_P ())
8421 {
8422 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8423 goto buffer_pos_reached;
8424 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8425 {
8426 SAVE_IT (atpos_it, *it, atpos_data);
8427 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8428 }
8429 }
8430
8431 if (new_x > it->first_visible_x)
8432 {
8433 /* Glyph is visible. Increment number of glyphs that
8434 would be displayed. */
8435 ++it->hpos;
8436 }
8437 }
8438
8439 if (result != MOVE_UNDEFINED)
8440 break;
8441 }
8442 else if (BUFFER_POS_REACHED_P ())
8443 {
8444 buffer_pos_reached:
8445 IT_RESET_X_ASCENT_DESCENT (it);
8446 result = MOVE_POS_MATCH_OR_ZV;
8447 break;
8448 }
8449 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8450 {
8451 /* Stop when TO_X specified and reached. This check is
8452 necessary here because of lines consisting of a line end,
8453 only. The line end will not produce any glyphs and we
8454 would never get MOVE_X_REACHED. */
8455 eassert (it->nglyphs == 0);
8456 result = MOVE_X_REACHED;
8457 break;
8458 }
8459
8460 /* Is this a line end? If yes, we're done. */
8461 if (ITERATOR_AT_END_OF_LINE_P (it))
8462 {
8463 /* If we are past TO_CHARPOS, but never saw any character
8464 positions smaller than TO_CHARPOS, return
8465 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8466 did. */
8467 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8468 {
8469 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8470 {
8471 if (IT_CHARPOS (ppos_it) < ZV)
8472 {
8473 RESTORE_IT (it, &ppos_it, ppos_data);
8474 result = MOVE_POS_MATCH_OR_ZV;
8475 }
8476 else
8477 goto buffer_pos_reached;
8478 }
8479 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8480 && IT_CHARPOS (*it) > to_charpos)
8481 goto buffer_pos_reached;
8482 else
8483 result = MOVE_NEWLINE_OR_CR;
8484 }
8485 else
8486 result = MOVE_NEWLINE_OR_CR;
8487 break;
8488 }
8489
8490 prev_method = it->method;
8491 if (it->method == GET_FROM_BUFFER)
8492 prev_pos = IT_CHARPOS (*it);
8493 /* The current display element has been consumed. Advance
8494 to the next. */
8495 set_iterator_to_next (it, 1);
8496 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8497 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8498 if (IT_CHARPOS (*it) < to_charpos)
8499 saw_smaller_pos = 1;
8500 if (it->bidi_p
8501 && (op & MOVE_TO_POS)
8502 && IT_CHARPOS (*it) >= to_charpos
8503 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8504 SAVE_IT (ppos_it, *it, ppos_data);
8505
8506 /* Stop if lines are truncated and IT's current x-position is
8507 past the right edge of the window now. */
8508 if (it->line_wrap == TRUNCATE
8509 && it->current_x >= it->last_visible_x)
8510 {
8511 if (!FRAME_WINDOW_P (it->f)
8512 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8513 {
8514 int at_eob_p = 0;
8515
8516 if ((at_eob_p = !get_next_display_element (it))
8517 || BUFFER_POS_REACHED_P ()
8518 /* If we are past TO_CHARPOS, but never saw any
8519 character positions smaller than TO_CHARPOS,
8520 return MOVE_POS_MATCH_OR_ZV, like the
8521 unidirectional display did. */
8522 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8523 && !saw_smaller_pos
8524 && IT_CHARPOS (*it) > to_charpos))
8525 {
8526 if (it->bidi_p
8527 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8528 RESTORE_IT (it, &ppos_it, ppos_data);
8529 result = MOVE_POS_MATCH_OR_ZV;
8530 break;
8531 }
8532 if (ITERATOR_AT_END_OF_LINE_P (it))
8533 {
8534 result = MOVE_NEWLINE_OR_CR;
8535 break;
8536 }
8537 }
8538 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8539 && !saw_smaller_pos
8540 && IT_CHARPOS (*it) > to_charpos)
8541 {
8542 if (IT_CHARPOS (ppos_it) < ZV)
8543 RESTORE_IT (it, &ppos_it, ppos_data);
8544 result = MOVE_POS_MATCH_OR_ZV;
8545 break;
8546 }
8547 result = MOVE_LINE_TRUNCATED;
8548 break;
8549 }
8550 #undef IT_RESET_X_ASCENT_DESCENT
8551 }
8552
8553 #undef BUFFER_POS_REACHED_P
8554
8555 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8556 restore the saved iterator. */
8557 if (atpos_it.sp >= 0)
8558 RESTORE_IT (it, &atpos_it, atpos_data);
8559 else if (atx_it.sp >= 0)
8560 RESTORE_IT (it, &atx_it, atx_data);
8561
8562 done:
8563
8564 if (atpos_data)
8565 bidi_unshelve_cache (atpos_data, 1);
8566 if (atx_data)
8567 bidi_unshelve_cache (atx_data, 1);
8568 if (wrap_data)
8569 bidi_unshelve_cache (wrap_data, 1);
8570 if (ppos_data)
8571 bidi_unshelve_cache (ppos_data, 1);
8572
8573 /* Restore the iterator settings altered at the beginning of this
8574 function. */
8575 it->glyph_row = saved_glyph_row;
8576 return result;
8577 }
8578
8579 /* For external use. */
8580 void
8581 move_it_in_display_line (struct it *it,
8582 ptrdiff_t to_charpos, int to_x,
8583 enum move_operation_enum op)
8584 {
8585 if (it->line_wrap == WORD_WRAP
8586 && (op & MOVE_TO_X))
8587 {
8588 struct it save_it;
8589 void *save_data = NULL;
8590 int skip;
8591
8592 SAVE_IT (save_it, *it, save_data);
8593 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8594 /* When word-wrap is on, TO_X may lie past the end
8595 of a wrapped line. Then it->current is the
8596 character on the next line, so backtrack to the
8597 space before the wrap point. */
8598 if (skip == MOVE_LINE_CONTINUED)
8599 {
8600 int prev_x = max (it->current_x - 1, 0);
8601 RESTORE_IT (it, &save_it, save_data);
8602 move_it_in_display_line_to
8603 (it, -1, prev_x, MOVE_TO_X);
8604 }
8605 else
8606 bidi_unshelve_cache (save_data, 1);
8607 }
8608 else
8609 move_it_in_display_line_to (it, to_charpos, to_x, op);
8610 }
8611
8612
8613 /* Move IT forward until it satisfies one or more of the criteria in
8614 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8615
8616 OP is a bit-mask that specifies where to stop, and in particular,
8617 which of those four position arguments makes a difference. See the
8618 description of enum move_operation_enum.
8619
8620 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8621 screen line, this function will set IT to the next position that is
8622 displayed to the right of TO_CHARPOS on the screen. */
8623
8624 void
8625 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8626 {
8627 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8628 int line_height, line_start_x = 0, reached = 0;
8629 void *backup_data = NULL;
8630
8631 for (;;)
8632 {
8633 if (op & MOVE_TO_VPOS)
8634 {
8635 /* If no TO_CHARPOS and no TO_X specified, stop at the
8636 start of the line TO_VPOS. */
8637 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8638 {
8639 if (it->vpos == to_vpos)
8640 {
8641 reached = 1;
8642 break;
8643 }
8644 else
8645 skip = move_it_in_display_line_to (it, -1, -1, 0);
8646 }
8647 else
8648 {
8649 /* TO_VPOS >= 0 means stop at TO_X in the line at
8650 TO_VPOS, or at TO_POS, whichever comes first. */
8651 if (it->vpos == to_vpos)
8652 {
8653 reached = 2;
8654 break;
8655 }
8656
8657 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8658
8659 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8660 {
8661 reached = 3;
8662 break;
8663 }
8664 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8665 {
8666 /* We have reached TO_X but not in the line we want. */
8667 skip = move_it_in_display_line_to (it, to_charpos,
8668 -1, MOVE_TO_POS);
8669 if (skip == MOVE_POS_MATCH_OR_ZV)
8670 {
8671 reached = 4;
8672 break;
8673 }
8674 }
8675 }
8676 }
8677 else if (op & MOVE_TO_Y)
8678 {
8679 struct it it_backup;
8680
8681 if (it->line_wrap == WORD_WRAP)
8682 SAVE_IT (it_backup, *it, backup_data);
8683
8684 /* TO_Y specified means stop at TO_X in the line containing
8685 TO_Y---or at TO_CHARPOS if this is reached first. The
8686 problem is that we can't really tell whether the line
8687 contains TO_Y before we have completely scanned it, and
8688 this may skip past TO_X. What we do is to first scan to
8689 TO_X.
8690
8691 If TO_X is not specified, use a TO_X of zero. The reason
8692 is to make the outcome of this function more predictable.
8693 If we didn't use TO_X == 0, we would stop at the end of
8694 the line which is probably not what a caller would expect
8695 to happen. */
8696 skip = move_it_in_display_line_to
8697 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8698 (MOVE_TO_X | (op & MOVE_TO_POS)));
8699
8700 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8701 if (skip == MOVE_POS_MATCH_OR_ZV)
8702 reached = 5;
8703 else if (skip == MOVE_X_REACHED)
8704 {
8705 /* If TO_X was reached, we want to know whether TO_Y is
8706 in the line. We know this is the case if the already
8707 scanned glyphs make the line tall enough. Otherwise,
8708 we must check by scanning the rest of the line. */
8709 line_height = it->max_ascent + it->max_descent;
8710 if (to_y >= it->current_y
8711 && to_y < it->current_y + line_height)
8712 {
8713 reached = 6;
8714 break;
8715 }
8716 SAVE_IT (it_backup, *it, backup_data);
8717 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8718 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8719 op & MOVE_TO_POS);
8720 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8721 line_height = it->max_ascent + it->max_descent;
8722 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8723
8724 if (to_y >= it->current_y
8725 && to_y < it->current_y + line_height)
8726 {
8727 /* If TO_Y is in this line and TO_X was reached
8728 above, we scanned too far. We have to restore
8729 IT's settings to the ones before skipping. But
8730 keep the more accurate values of max_ascent and
8731 max_descent we've found while skipping the rest
8732 of the line, for the sake of callers, such as
8733 pos_visible_p, that need to know the line
8734 height. */
8735 int max_ascent = it->max_ascent;
8736 int max_descent = it->max_descent;
8737
8738 RESTORE_IT (it, &it_backup, backup_data);
8739 it->max_ascent = max_ascent;
8740 it->max_descent = max_descent;
8741 reached = 6;
8742 }
8743 else
8744 {
8745 skip = skip2;
8746 if (skip == MOVE_POS_MATCH_OR_ZV)
8747 reached = 7;
8748 }
8749 }
8750 else
8751 {
8752 /* Check whether TO_Y is in this line. */
8753 line_height = it->max_ascent + it->max_descent;
8754 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8755
8756 if (to_y >= it->current_y
8757 && to_y < it->current_y + line_height)
8758 {
8759 /* When word-wrap is on, TO_X may lie past the end
8760 of a wrapped line. Then it->current is the
8761 character on the next line, so backtrack to the
8762 space before the wrap point. */
8763 if (skip == MOVE_LINE_CONTINUED
8764 && it->line_wrap == WORD_WRAP)
8765 {
8766 int prev_x = max (it->current_x - 1, 0);
8767 RESTORE_IT (it, &it_backup, backup_data);
8768 skip = move_it_in_display_line_to
8769 (it, -1, prev_x, MOVE_TO_X);
8770 }
8771 reached = 6;
8772 }
8773 }
8774
8775 if (reached)
8776 break;
8777 }
8778 else if (BUFFERP (it->object)
8779 && (it->method == GET_FROM_BUFFER
8780 || it->method == GET_FROM_STRETCH)
8781 && IT_CHARPOS (*it) >= to_charpos
8782 /* Under bidi iteration, a call to set_iterator_to_next
8783 can scan far beyond to_charpos if the initial
8784 portion of the next line needs to be reordered. In
8785 that case, give move_it_in_display_line_to another
8786 chance below. */
8787 && !(it->bidi_p
8788 && it->bidi_it.scan_dir == -1))
8789 skip = MOVE_POS_MATCH_OR_ZV;
8790 else
8791 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8792
8793 switch (skip)
8794 {
8795 case MOVE_POS_MATCH_OR_ZV:
8796 reached = 8;
8797 goto out;
8798
8799 case MOVE_NEWLINE_OR_CR:
8800 set_iterator_to_next (it, 1);
8801 it->continuation_lines_width = 0;
8802 break;
8803
8804 case MOVE_LINE_TRUNCATED:
8805 it->continuation_lines_width = 0;
8806 reseat_at_next_visible_line_start (it, 0);
8807 if ((op & MOVE_TO_POS) != 0
8808 && IT_CHARPOS (*it) > to_charpos)
8809 {
8810 reached = 9;
8811 goto out;
8812 }
8813 break;
8814
8815 case MOVE_LINE_CONTINUED:
8816 /* For continued lines ending in a tab, some of the glyphs
8817 associated with the tab are displayed on the current
8818 line. Since it->current_x does not include these glyphs,
8819 we use it->last_visible_x instead. */
8820 if (it->c == '\t')
8821 {
8822 it->continuation_lines_width += it->last_visible_x;
8823 /* When moving by vpos, ensure that the iterator really
8824 advances to the next line (bug#847, bug#969). Fixme:
8825 do we need to do this in other circumstances? */
8826 if (it->current_x != it->last_visible_x
8827 && (op & MOVE_TO_VPOS)
8828 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8829 {
8830 line_start_x = it->current_x + it->pixel_width
8831 - it->last_visible_x;
8832 set_iterator_to_next (it, 0);
8833 }
8834 }
8835 else
8836 it->continuation_lines_width += it->current_x;
8837 break;
8838
8839 default:
8840 abort ();
8841 }
8842
8843 /* Reset/increment for the next run. */
8844 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8845 it->current_x = line_start_x;
8846 line_start_x = 0;
8847 it->hpos = 0;
8848 it->current_y += it->max_ascent + it->max_descent;
8849 ++it->vpos;
8850 last_height = it->max_ascent + it->max_descent;
8851 last_max_ascent = it->max_ascent;
8852 it->max_ascent = it->max_descent = 0;
8853 }
8854
8855 out:
8856
8857 /* On text terminals, we may stop at the end of a line in the middle
8858 of a multi-character glyph. If the glyph itself is continued,
8859 i.e. it is actually displayed on the next line, don't treat this
8860 stopping point as valid; move to the next line instead (unless
8861 that brings us offscreen). */
8862 if (!FRAME_WINDOW_P (it->f)
8863 && op & MOVE_TO_POS
8864 && IT_CHARPOS (*it) == to_charpos
8865 && it->what == IT_CHARACTER
8866 && it->nglyphs > 1
8867 && it->line_wrap == WINDOW_WRAP
8868 && it->current_x == it->last_visible_x - 1
8869 && it->c != '\n'
8870 && it->c != '\t'
8871 && it->vpos < XFASTINT (it->w->window_end_vpos))
8872 {
8873 it->continuation_lines_width += it->current_x;
8874 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8875 it->current_y += it->max_ascent + it->max_descent;
8876 ++it->vpos;
8877 last_height = it->max_ascent + it->max_descent;
8878 last_max_ascent = it->max_ascent;
8879 }
8880
8881 if (backup_data)
8882 bidi_unshelve_cache (backup_data, 1);
8883
8884 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8885 }
8886
8887
8888 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8889
8890 If DY > 0, move IT backward at least that many pixels. DY = 0
8891 means move IT backward to the preceding line start or BEGV. This
8892 function may move over more than DY pixels if IT->current_y - DY
8893 ends up in the middle of a line; in this case IT->current_y will be
8894 set to the top of the line moved to. */
8895
8896 void
8897 move_it_vertically_backward (struct it *it, int dy)
8898 {
8899 int nlines, h;
8900 struct it it2, it3;
8901 void *it2data = NULL, *it3data = NULL;
8902 ptrdiff_t start_pos;
8903
8904 move_further_back:
8905 eassert (dy >= 0);
8906
8907 start_pos = IT_CHARPOS (*it);
8908
8909 /* Estimate how many newlines we must move back. */
8910 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8911
8912 /* Set the iterator's position that many lines back. */
8913 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8914 back_to_previous_visible_line_start (it);
8915
8916 /* Reseat the iterator here. When moving backward, we don't want
8917 reseat to skip forward over invisible text, set up the iterator
8918 to deliver from overlay strings at the new position etc. So,
8919 use reseat_1 here. */
8920 reseat_1 (it, it->current.pos, 1);
8921
8922 /* We are now surely at a line start. */
8923 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8924 reordering is in effect. */
8925 it->continuation_lines_width = 0;
8926
8927 /* Move forward and see what y-distance we moved. First move to the
8928 start of the next line so that we get its height. We need this
8929 height to be able to tell whether we reached the specified
8930 y-distance. */
8931 SAVE_IT (it2, *it, it2data);
8932 it2.max_ascent = it2.max_descent = 0;
8933 do
8934 {
8935 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8936 MOVE_TO_POS | MOVE_TO_VPOS);
8937 }
8938 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8939 /* If we are in a display string which starts at START_POS,
8940 and that display string includes a newline, and we are
8941 right after that newline (i.e. at the beginning of a
8942 display line), exit the loop, because otherwise we will
8943 infloop, since move_it_to will see that it is already at
8944 START_POS and will not move. */
8945 || (it2.method == GET_FROM_STRING
8946 && IT_CHARPOS (it2) == start_pos
8947 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8948 eassert (IT_CHARPOS (*it) >= BEGV);
8949 SAVE_IT (it3, it2, it3data);
8950
8951 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8952 eassert (IT_CHARPOS (*it) >= BEGV);
8953 /* H is the actual vertical distance from the position in *IT
8954 and the starting position. */
8955 h = it2.current_y - it->current_y;
8956 /* NLINES is the distance in number of lines. */
8957 nlines = it2.vpos - it->vpos;
8958
8959 /* Correct IT's y and vpos position
8960 so that they are relative to the starting point. */
8961 it->vpos -= nlines;
8962 it->current_y -= h;
8963
8964 if (dy == 0)
8965 {
8966 /* DY == 0 means move to the start of the screen line. The
8967 value of nlines is > 0 if continuation lines were involved,
8968 or if the original IT position was at start of a line. */
8969 RESTORE_IT (it, it, it2data);
8970 if (nlines > 0)
8971 move_it_by_lines (it, nlines);
8972 /* The above code moves us to some position NLINES down,
8973 usually to its first glyph (leftmost in an L2R line), but
8974 that's not necessarily the start of the line, under bidi
8975 reordering. We want to get to the character position
8976 that is immediately after the newline of the previous
8977 line. */
8978 if (it->bidi_p
8979 && !it->continuation_lines_width
8980 && !STRINGP (it->string)
8981 && IT_CHARPOS (*it) > BEGV
8982 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8983 {
8984 ptrdiff_t nl_pos =
8985 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8986
8987 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8988 }
8989 bidi_unshelve_cache (it3data, 1);
8990 }
8991 else
8992 {
8993 /* The y-position we try to reach, relative to *IT.
8994 Note that H has been subtracted in front of the if-statement. */
8995 int target_y = it->current_y + h - dy;
8996 int y0 = it3.current_y;
8997 int y1;
8998 int line_height;
8999
9000 RESTORE_IT (&it3, &it3, it3data);
9001 y1 = line_bottom_y (&it3);
9002 line_height = y1 - y0;
9003 RESTORE_IT (it, it, it2data);
9004 /* If we did not reach target_y, try to move further backward if
9005 we can. If we moved too far backward, try to move forward. */
9006 if (target_y < it->current_y
9007 /* This is heuristic. In a window that's 3 lines high, with
9008 a line height of 13 pixels each, recentering with point
9009 on the bottom line will try to move -39/2 = 19 pixels
9010 backward. Try to avoid moving into the first line. */
9011 && (it->current_y - target_y
9012 > min (window_box_height (it->w), line_height * 2 / 3))
9013 && IT_CHARPOS (*it) > BEGV)
9014 {
9015 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9016 target_y - it->current_y));
9017 dy = it->current_y - target_y;
9018 goto move_further_back;
9019 }
9020 else if (target_y >= it->current_y + line_height
9021 && IT_CHARPOS (*it) < ZV)
9022 {
9023 /* Should move forward by at least one line, maybe more.
9024
9025 Note: Calling move_it_by_lines can be expensive on
9026 terminal frames, where compute_motion is used (via
9027 vmotion) to do the job, when there are very long lines
9028 and truncate-lines is nil. That's the reason for
9029 treating terminal frames specially here. */
9030
9031 if (!FRAME_WINDOW_P (it->f))
9032 move_it_vertically (it, target_y - (it->current_y + line_height));
9033 else
9034 {
9035 do
9036 {
9037 move_it_by_lines (it, 1);
9038 }
9039 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9040 }
9041 }
9042 }
9043 }
9044
9045
9046 /* Move IT by a specified amount of pixel lines DY. DY negative means
9047 move backwards. DY = 0 means move to start of screen line. At the
9048 end, IT will be on the start of a screen line. */
9049
9050 void
9051 move_it_vertically (struct it *it, int dy)
9052 {
9053 if (dy <= 0)
9054 move_it_vertically_backward (it, -dy);
9055 else
9056 {
9057 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9058 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9059 MOVE_TO_POS | MOVE_TO_Y);
9060 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9061
9062 /* If buffer ends in ZV without a newline, move to the start of
9063 the line to satisfy the post-condition. */
9064 if (IT_CHARPOS (*it) == ZV
9065 && ZV > BEGV
9066 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9067 move_it_by_lines (it, 0);
9068 }
9069 }
9070
9071
9072 /* Move iterator IT past the end of the text line it is in. */
9073
9074 void
9075 move_it_past_eol (struct it *it)
9076 {
9077 enum move_it_result rc;
9078
9079 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9080 if (rc == MOVE_NEWLINE_OR_CR)
9081 set_iterator_to_next (it, 0);
9082 }
9083
9084
9085 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9086 negative means move up. DVPOS == 0 means move to the start of the
9087 screen line.
9088
9089 Optimization idea: If we would know that IT->f doesn't use
9090 a face with proportional font, we could be faster for
9091 truncate-lines nil. */
9092
9093 void
9094 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9095 {
9096
9097 /* The commented-out optimization uses vmotion on terminals. This
9098 gives bad results, because elements like it->what, on which
9099 callers such as pos_visible_p rely, aren't updated. */
9100 /* struct position pos;
9101 if (!FRAME_WINDOW_P (it->f))
9102 {
9103 struct text_pos textpos;
9104
9105 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9106 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9107 reseat (it, textpos, 1);
9108 it->vpos += pos.vpos;
9109 it->current_y += pos.vpos;
9110 }
9111 else */
9112
9113 if (dvpos == 0)
9114 {
9115 /* DVPOS == 0 means move to the start of the screen line. */
9116 move_it_vertically_backward (it, 0);
9117 /* Let next call to line_bottom_y calculate real line height */
9118 last_height = 0;
9119 }
9120 else if (dvpos > 0)
9121 {
9122 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9123 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9124 {
9125 /* Only move to the next buffer position if we ended up in a
9126 string from display property, not in an overlay string
9127 (before-string or after-string). That is because the
9128 latter don't conceal the underlying buffer position, so
9129 we can ask to move the iterator to the exact position we
9130 are interested in. Note that, even if we are already at
9131 IT_CHARPOS (*it), the call below is not a no-op, as it
9132 will detect that we are at the end of the string, pop the
9133 iterator, and compute it->current_x and it->hpos
9134 correctly. */
9135 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9136 -1, -1, -1, MOVE_TO_POS);
9137 }
9138 }
9139 else
9140 {
9141 struct it it2;
9142 void *it2data = NULL;
9143 ptrdiff_t start_charpos, i;
9144
9145 /* Start at the beginning of the screen line containing IT's
9146 position. This may actually move vertically backwards,
9147 in case of overlays, so adjust dvpos accordingly. */
9148 dvpos += it->vpos;
9149 move_it_vertically_backward (it, 0);
9150 dvpos -= it->vpos;
9151
9152 /* Go back -DVPOS visible lines and reseat the iterator there. */
9153 start_charpos = IT_CHARPOS (*it);
9154 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9155 back_to_previous_visible_line_start (it);
9156 reseat (it, it->current.pos, 1);
9157
9158 /* Move further back if we end up in a string or an image. */
9159 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9160 {
9161 /* First try to move to start of display line. */
9162 dvpos += it->vpos;
9163 move_it_vertically_backward (it, 0);
9164 dvpos -= it->vpos;
9165 if (IT_POS_VALID_AFTER_MOVE_P (it))
9166 break;
9167 /* If start of line is still in string or image,
9168 move further back. */
9169 back_to_previous_visible_line_start (it);
9170 reseat (it, it->current.pos, 1);
9171 dvpos--;
9172 }
9173
9174 it->current_x = it->hpos = 0;
9175
9176 /* Above call may have moved too far if continuation lines
9177 are involved. Scan forward and see if it did. */
9178 SAVE_IT (it2, *it, it2data);
9179 it2.vpos = it2.current_y = 0;
9180 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9181 it->vpos -= it2.vpos;
9182 it->current_y -= it2.current_y;
9183 it->current_x = it->hpos = 0;
9184
9185 /* If we moved too far back, move IT some lines forward. */
9186 if (it2.vpos > -dvpos)
9187 {
9188 int delta = it2.vpos + dvpos;
9189
9190 RESTORE_IT (&it2, &it2, it2data);
9191 SAVE_IT (it2, *it, it2data);
9192 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9193 /* Move back again if we got too far ahead. */
9194 if (IT_CHARPOS (*it) >= start_charpos)
9195 RESTORE_IT (it, &it2, it2data);
9196 else
9197 bidi_unshelve_cache (it2data, 1);
9198 }
9199 else
9200 RESTORE_IT (it, it, it2data);
9201 }
9202 }
9203
9204 /* Return 1 if IT points into the middle of a display vector. */
9205
9206 int
9207 in_display_vector_p (struct it *it)
9208 {
9209 return (it->method == GET_FROM_DISPLAY_VECTOR
9210 && it->current.dpvec_index > 0
9211 && it->dpvec + it->current.dpvec_index != it->dpend);
9212 }
9213
9214 \f
9215 /***********************************************************************
9216 Messages
9217 ***********************************************************************/
9218
9219
9220 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9221 to *Messages*. */
9222
9223 void
9224 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9225 {
9226 Lisp_Object args[3];
9227 Lisp_Object msg, fmt;
9228 char *buffer;
9229 ptrdiff_t len;
9230 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9231 USE_SAFE_ALLOCA;
9232
9233 /* Do nothing if called asynchronously. Inserting text into
9234 a buffer may call after-change-functions and alike and
9235 that would means running Lisp asynchronously. */
9236 if (handling_signal)
9237 return;
9238
9239 fmt = msg = Qnil;
9240 GCPRO4 (fmt, msg, arg1, arg2);
9241
9242 args[0] = fmt = build_string (format);
9243 args[1] = arg1;
9244 args[2] = arg2;
9245 msg = Fformat (3, args);
9246
9247 len = SBYTES (msg) + 1;
9248 SAFE_ALLOCA (buffer, char *, len);
9249 memcpy (buffer, SDATA (msg), len);
9250
9251 message_dolog (buffer, len - 1, 1, 0);
9252 SAFE_FREE ();
9253
9254 UNGCPRO;
9255 }
9256
9257
9258 /* Output a newline in the *Messages* buffer if "needs" one. */
9259
9260 void
9261 message_log_maybe_newline (void)
9262 {
9263 if (message_log_need_newline)
9264 message_dolog ("", 0, 1, 0);
9265 }
9266
9267
9268 /* Add a string M of length NBYTES to the message log, optionally
9269 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9270 nonzero, means interpret the contents of M as multibyte. This
9271 function calls low-level routines in order to bypass text property
9272 hooks, etc. which might not be safe to run.
9273
9274 This may GC (insert may run before/after change hooks),
9275 so the buffer M must NOT point to a Lisp string. */
9276
9277 void
9278 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9279 {
9280 const unsigned char *msg = (const unsigned char *) m;
9281
9282 if (!NILP (Vmemory_full))
9283 return;
9284
9285 if (!NILP (Vmessage_log_max))
9286 {
9287 struct buffer *oldbuf;
9288 Lisp_Object oldpoint, oldbegv, oldzv;
9289 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9290 ptrdiff_t point_at_end = 0;
9291 ptrdiff_t zv_at_end = 0;
9292 Lisp_Object old_deactivate_mark, tem;
9293 struct gcpro gcpro1;
9294
9295 old_deactivate_mark = Vdeactivate_mark;
9296 oldbuf = current_buffer;
9297 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9298 BVAR (current_buffer, undo_list) = Qt;
9299
9300 oldpoint = message_dolog_marker1;
9301 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9302 oldbegv = message_dolog_marker2;
9303 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9304 oldzv = message_dolog_marker3;
9305 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9306 GCPRO1 (old_deactivate_mark);
9307
9308 if (PT == Z)
9309 point_at_end = 1;
9310 if (ZV == Z)
9311 zv_at_end = 1;
9312
9313 BEGV = BEG;
9314 BEGV_BYTE = BEG_BYTE;
9315 ZV = Z;
9316 ZV_BYTE = Z_BYTE;
9317 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9318
9319 /* Insert the string--maybe converting multibyte to single byte
9320 or vice versa, so that all the text fits the buffer. */
9321 if (multibyte
9322 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9323 {
9324 ptrdiff_t i;
9325 int c, char_bytes;
9326 char work[1];
9327
9328 /* Convert a multibyte string to single-byte
9329 for the *Message* buffer. */
9330 for (i = 0; i < nbytes; i += char_bytes)
9331 {
9332 c = string_char_and_length (msg + i, &char_bytes);
9333 work[0] = (ASCII_CHAR_P (c)
9334 ? c
9335 : multibyte_char_to_unibyte (c));
9336 insert_1_both (work, 1, 1, 1, 0, 0);
9337 }
9338 }
9339 else if (! multibyte
9340 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9341 {
9342 ptrdiff_t i;
9343 int c, char_bytes;
9344 unsigned char str[MAX_MULTIBYTE_LENGTH];
9345 /* Convert a single-byte string to multibyte
9346 for the *Message* buffer. */
9347 for (i = 0; i < nbytes; i++)
9348 {
9349 c = msg[i];
9350 MAKE_CHAR_MULTIBYTE (c);
9351 char_bytes = CHAR_STRING (c, str);
9352 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9353 }
9354 }
9355 else if (nbytes)
9356 insert_1 (m, nbytes, 1, 0, 0);
9357
9358 if (nlflag)
9359 {
9360 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9361 printmax_t dups;
9362 insert_1 ("\n", 1, 1, 0, 0);
9363
9364 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9365 this_bol = PT;
9366 this_bol_byte = PT_BYTE;
9367
9368 /* See if this line duplicates the previous one.
9369 If so, combine duplicates. */
9370 if (this_bol > BEG)
9371 {
9372 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9373 prev_bol = PT;
9374 prev_bol_byte = PT_BYTE;
9375
9376 dups = message_log_check_duplicate (prev_bol_byte,
9377 this_bol_byte);
9378 if (dups)
9379 {
9380 del_range_both (prev_bol, prev_bol_byte,
9381 this_bol, this_bol_byte, 0);
9382 if (dups > 1)
9383 {
9384 char dupstr[sizeof " [ times]"
9385 + INT_STRLEN_BOUND (printmax_t)];
9386
9387 /* If you change this format, don't forget to also
9388 change message_log_check_duplicate. */
9389 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9390 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9391 insert_1 (dupstr, duplen, 1, 0, 1);
9392 }
9393 }
9394 }
9395
9396 /* If we have more than the desired maximum number of lines
9397 in the *Messages* buffer now, delete the oldest ones.
9398 This is safe because we don't have undo in this buffer. */
9399
9400 if (NATNUMP (Vmessage_log_max))
9401 {
9402 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9403 -XFASTINT (Vmessage_log_max) - 1, 0);
9404 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9405 }
9406 }
9407 BEGV = XMARKER (oldbegv)->charpos;
9408 BEGV_BYTE = marker_byte_position (oldbegv);
9409
9410 if (zv_at_end)
9411 {
9412 ZV = Z;
9413 ZV_BYTE = Z_BYTE;
9414 }
9415 else
9416 {
9417 ZV = XMARKER (oldzv)->charpos;
9418 ZV_BYTE = marker_byte_position (oldzv);
9419 }
9420
9421 if (point_at_end)
9422 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9423 else
9424 /* We can't do Fgoto_char (oldpoint) because it will run some
9425 Lisp code. */
9426 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9427 XMARKER (oldpoint)->bytepos);
9428
9429 UNGCPRO;
9430 unchain_marker (XMARKER (oldpoint));
9431 unchain_marker (XMARKER (oldbegv));
9432 unchain_marker (XMARKER (oldzv));
9433
9434 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9435 set_buffer_internal (oldbuf);
9436 if (NILP (tem))
9437 windows_or_buffers_changed = old_windows_or_buffers_changed;
9438 message_log_need_newline = !nlflag;
9439 Vdeactivate_mark = old_deactivate_mark;
9440 }
9441 }
9442
9443
9444 /* We are at the end of the buffer after just having inserted a newline.
9445 (Note: We depend on the fact we won't be crossing the gap.)
9446 Check to see if the most recent message looks a lot like the previous one.
9447 Return 0 if different, 1 if the new one should just replace it, or a
9448 value N > 1 if we should also append " [N times]". */
9449
9450 static intmax_t
9451 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9452 {
9453 ptrdiff_t i;
9454 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9455 int seen_dots = 0;
9456 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9457 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9458
9459 for (i = 0; i < len; i++)
9460 {
9461 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9462 seen_dots = 1;
9463 if (p1[i] != p2[i])
9464 return seen_dots;
9465 }
9466 p1 += len;
9467 if (*p1 == '\n')
9468 return 2;
9469 if (*p1++ == ' ' && *p1++ == '[')
9470 {
9471 char *pend;
9472 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9473 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9474 return n+1;
9475 }
9476 return 0;
9477 }
9478 \f
9479
9480 /* Display an echo area message M with a specified length of NBYTES
9481 bytes. The string may include null characters. If M is 0, clear
9482 out any existing message, and let the mini-buffer text show
9483 through.
9484
9485 This may GC, so the buffer M must NOT point to a Lisp string. */
9486
9487 void
9488 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9489 {
9490 /* First flush out any partial line written with print. */
9491 message_log_maybe_newline ();
9492 if (m)
9493 message_dolog (m, nbytes, 1, multibyte);
9494 message2_nolog (m, nbytes, multibyte);
9495 }
9496
9497
9498 /* The non-logging counterpart of message2. */
9499
9500 void
9501 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9502 {
9503 struct frame *sf = SELECTED_FRAME ();
9504 message_enable_multibyte = multibyte;
9505
9506 if (FRAME_INITIAL_P (sf))
9507 {
9508 if (noninteractive_need_newline)
9509 putc ('\n', stderr);
9510 noninteractive_need_newline = 0;
9511 if (m)
9512 fwrite (m, nbytes, 1, stderr);
9513 if (cursor_in_echo_area == 0)
9514 fprintf (stderr, "\n");
9515 fflush (stderr);
9516 }
9517 /* A null message buffer means that the frame hasn't really been
9518 initialized yet. Error messages get reported properly by
9519 cmd_error, so this must be just an informative message; toss it. */
9520 else if (INTERACTIVE
9521 && sf->glyphs_initialized_p
9522 && FRAME_MESSAGE_BUF (sf))
9523 {
9524 Lisp_Object mini_window;
9525 struct frame *f;
9526
9527 /* Get the frame containing the mini-buffer
9528 that the selected frame is using. */
9529 mini_window = FRAME_MINIBUF_WINDOW (sf);
9530 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9531
9532 FRAME_SAMPLE_VISIBILITY (f);
9533 if (FRAME_VISIBLE_P (sf)
9534 && ! FRAME_VISIBLE_P (f))
9535 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9536
9537 if (m)
9538 {
9539 set_message (m, Qnil, nbytes, multibyte);
9540 if (minibuffer_auto_raise)
9541 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9542 }
9543 else
9544 clear_message (1, 1);
9545
9546 do_pending_window_change (0);
9547 echo_area_display (1);
9548 do_pending_window_change (0);
9549 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9550 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9551 }
9552 }
9553
9554
9555 /* Display an echo area message M with a specified length of NBYTES
9556 bytes. The string may include null characters. If M is not a
9557 string, clear out any existing message, and let the mini-buffer
9558 text show through.
9559
9560 This function cancels echoing. */
9561
9562 void
9563 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9564 {
9565 struct gcpro gcpro1;
9566
9567 GCPRO1 (m);
9568 clear_message (1,1);
9569 cancel_echoing ();
9570
9571 /* First flush out any partial line written with print. */
9572 message_log_maybe_newline ();
9573 if (STRINGP (m))
9574 {
9575 char *buffer;
9576 USE_SAFE_ALLOCA;
9577
9578 SAFE_ALLOCA (buffer, char *, nbytes);
9579 memcpy (buffer, SDATA (m), nbytes);
9580 message_dolog (buffer, nbytes, 1, multibyte);
9581 SAFE_FREE ();
9582 }
9583 message3_nolog (m, nbytes, multibyte);
9584
9585 UNGCPRO;
9586 }
9587
9588
9589 /* The non-logging version of message3.
9590 This does not cancel echoing, because it is used for echoing.
9591 Perhaps we need to make a separate function for echoing
9592 and make this cancel echoing. */
9593
9594 void
9595 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9596 {
9597 struct frame *sf = SELECTED_FRAME ();
9598 message_enable_multibyte = multibyte;
9599
9600 if (FRAME_INITIAL_P (sf))
9601 {
9602 if (noninteractive_need_newline)
9603 putc ('\n', stderr);
9604 noninteractive_need_newline = 0;
9605 if (STRINGP (m))
9606 fwrite (SDATA (m), nbytes, 1, stderr);
9607 if (cursor_in_echo_area == 0)
9608 fprintf (stderr, "\n");
9609 fflush (stderr);
9610 }
9611 /* A null message buffer means that the frame hasn't really been
9612 initialized yet. Error messages get reported properly by
9613 cmd_error, so this must be just an informative message; toss it. */
9614 else if (INTERACTIVE
9615 && sf->glyphs_initialized_p
9616 && FRAME_MESSAGE_BUF (sf))
9617 {
9618 Lisp_Object mini_window;
9619 Lisp_Object frame;
9620 struct frame *f;
9621
9622 /* Get the frame containing the mini-buffer
9623 that the selected frame is using. */
9624 mini_window = FRAME_MINIBUF_WINDOW (sf);
9625 frame = XWINDOW (mini_window)->frame;
9626 f = XFRAME (frame);
9627
9628 FRAME_SAMPLE_VISIBILITY (f);
9629 if (FRAME_VISIBLE_P (sf)
9630 && !FRAME_VISIBLE_P (f))
9631 Fmake_frame_visible (frame);
9632
9633 if (STRINGP (m) && SCHARS (m) > 0)
9634 {
9635 set_message (NULL, m, nbytes, multibyte);
9636 if (minibuffer_auto_raise)
9637 Fraise_frame (frame);
9638 /* Assume we are not echoing.
9639 (If we are, echo_now will override this.) */
9640 echo_message_buffer = Qnil;
9641 }
9642 else
9643 clear_message (1, 1);
9644
9645 do_pending_window_change (0);
9646 echo_area_display (1);
9647 do_pending_window_change (0);
9648 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9649 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9650 }
9651 }
9652
9653
9654 /* Display a null-terminated echo area message M. If M is 0, clear
9655 out any existing message, and let the mini-buffer text show through.
9656
9657 The buffer M must continue to exist until after the echo area gets
9658 cleared or some other message gets displayed there. Do not pass
9659 text that is stored in a Lisp string. Do not pass text in a buffer
9660 that was alloca'd. */
9661
9662 void
9663 message1 (const char *m)
9664 {
9665 message2 (m, (m ? strlen (m) : 0), 0);
9666 }
9667
9668
9669 /* The non-logging counterpart of message1. */
9670
9671 void
9672 message1_nolog (const char *m)
9673 {
9674 message2_nolog (m, (m ? strlen (m) : 0), 0);
9675 }
9676
9677 /* Display a message M which contains a single %s
9678 which gets replaced with STRING. */
9679
9680 void
9681 message_with_string (const char *m, Lisp_Object string, int log)
9682 {
9683 CHECK_STRING (string);
9684
9685 if (noninteractive)
9686 {
9687 if (m)
9688 {
9689 if (noninteractive_need_newline)
9690 putc ('\n', stderr);
9691 noninteractive_need_newline = 0;
9692 fprintf (stderr, m, SDATA (string));
9693 if (!cursor_in_echo_area)
9694 fprintf (stderr, "\n");
9695 fflush (stderr);
9696 }
9697 }
9698 else if (INTERACTIVE)
9699 {
9700 /* The frame whose minibuffer we're going to display the message on.
9701 It may be larger than the selected frame, so we need
9702 to use its buffer, not the selected frame's buffer. */
9703 Lisp_Object mini_window;
9704 struct frame *f, *sf = SELECTED_FRAME ();
9705
9706 /* Get the frame containing the minibuffer
9707 that the selected frame is using. */
9708 mini_window = FRAME_MINIBUF_WINDOW (sf);
9709 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9710
9711 /* A null message buffer means that the frame hasn't really been
9712 initialized yet. Error messages get reported properly by
9713 cmd_error, so this must be just an informative message; toss it. */
9714 if (FRAME_MESSAGE_BUF (f))
9715 {
9716 Lisp_Object args[2], msg;
9717 struct gcpro gcpro1, gcpro2;
9718
9719 args[0] = build_string (m);
9720 args[1] = msg = string;
9721 GCPRO2 (args[0], msg);
9722 gcpro1.nvars = 2;
9723
9724 msg = Fformat (2, args);
9725
9726 if (log)
9727 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9728 else
9729 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9730
9731 UNGCPRO;
9732
9733 /* Print should start at the beginning of the message
9734 buffer next time. */
9735 message_buf_print = 0;
9736 }
9737 }
9738 }
9739
9740
9741 /* Dump an informative message to the minibuf. If M is 0, clear out
9742 any existing message, and let the mini-buffer text show through. */
9743
9744 static void
9745 vmessage (const char *m, va_list ap)
9746 {
9747 if (noninteractive)
9748 {
9749 if (m)
9750 {
9751 if (noninteractive_need_newline)
9752 putc ('\n', stderr);
9753 noninteractive_need_newline = 0;
9754 vfprintf (stderr, m, ap);
9755 if (cursor_in_echo_area == 0)
9756 fprintf (stderr, "\n");
9757 fflush (stderr);
9758 }
9759 }
9760 else if (INTERACTIVE)
9761 {
9762 /* The frame whose mini-buffer we're going to display the message
9763 on. It may be larger than the selected frame, so we need to
9764 use its buffer, not the selected frame's buffer. */
9765 Lisp_Object mini_window;
9766 struct frame *f, *sf = SELECTED_FRAME ();
9767
9768 /* Get the frame containing the mini-buffer
9769 that the selected frame is using. */
9770 mini_window = FRAME_MINIBUF_WINDOW (sf);
9771 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9772
9773 /* A null message buffer means that the frame hasn't really been
9774 initialized yet. Error messages get reported properly by
9775 cmd_error, so this must be just an informative message; toss
9776 it. */
9777 if (FRAME_MESSAGE_BUF (f))
9778 {
9779 if (m)
9780 {
9781 ptrdiff_t len;
9782
9783 len = doprnt (FRAME_MESSAGE_BUF (f),
9784 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9785
9786 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9787 }
9788 else
9789 message1 (0);
9790
9791 /* Print should start at the beginning of the message
9792 buffer next time. */
9793 message_buf_print = 0;
9794 }
9795 }
9796 }
9797
9798 void
9799 message (const char *m, ...)
9800 {
9801 va_list ap;
9802 va_start (ap, m);
9803 vmessage (m, ap);
9804 va_end (ap);
9805 }
9806
9807
9808 #if 0
9809 /* The non-logging version of message. */
9810
9811 void
9812 message_nolog (const char *m, ...)
9813 {
9814 Lisp_Object old_log_max;
9815 va_list ap;
9816 va_start (ap, m);
9817 old_log_max = Vmessage_log_max;
9818 Vmessage_log_max = Qnil;
9819 vmessage (m, ap);
9820 Vmessage_log_max = old_log_max;
9821 va_end (ap);
9822 }
9823 #endif
9824
9825
9826 /* Display the current message in the current mini-buffer. This is
9827 only called from error handlers in process.c, and is not time
9828 critical. */
9829
9830 void
9831 update_echo_area (void)
9832 {
9833 if (!NILP (echo_area_buffer[0]))
9834 {
9835 Lisp_Object string;
9836 string = Fcurrent_message ();
9837 message3 (string, SBYTES (string),
9838 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9839 }
9840 }
9841
9842
9843 /* Make sure echo area buffers in `echo_buffers' are live.
9844 If they aren't, make new ones. */
9845
9846 static void
9847 ensure_echo_area_buffers (void)
9848 {
9849 int i;
9850
9851 for (i = 0; i < 2; ++i)
9852 if (!BUFFERP (echo_buffer[i])
9853 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9854 {
9855 char name[30];
9856 Lisp_Object old_buffer;
9857 int j;
9858
9859 old_buffer = echo_buffer[i];
9860 sprintf (name, " *Echo Area %d*", i);
9861 echo_buffer[i] = Fget_buffer_create (build_string (name));
9862 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9863 /* to force word wrap in echo area -
9864 it was decided to postpone this*/
9865 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9866
9867 for (j = 0; j < 2; ++j)
9868 if (EQ (old_buffer, echo_area_buffer[j]))
9869 echo_area_buffer[j] = echo_buffer[i];
9870 }
9871 }
9872
9873
9874 /* Call FN with args A1..A4 with either the current or last displayed
9875 echo_area_buffer as current buffer.
9876
9877 WHICH zero means use the current message buffer
9878 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9879 from echo_buffer[] and clear it.
9880
9881 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9882 suitable buffer from echo_buffer[] and clear it.
9883
9884 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9885 that the current message becomes the last displayed one, make
9886 choose a suitable buffer for echo_area_buffer[0], and clear it.
9887
9888 Value is what FN returns. */
9889
9890 static int
9891 with_echo_area_buffer (struct window *w, int which,
9892 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9893 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9894 {
9895 Lisp_Object buffer;
9896 int this_one, the_other, clear_buffer_p, rc;
9897 ptrdiff_t count = SPECPDL_INDEX ();
9898
9899 /* If buffers aren't live, make new ones. */
9900 ensure_echo_area_buffers ();
9901
9902 clear_buffer_p = 0;
9903
9904 if (which == 0)
9905 this_one = 0, the_other = 1;
9906 else if (which > 0)
9907 this_one = 1, the_other = 0;
9908 else
9909 {
9910 this_one = 0, the_other = 1;
9911 clear_buffer_p = 1;
9912
9913 /* We need a fresh one in case the current echo buffer equals
9914 the one containing the last displayed echo area message. */
9915 if (!NILP (echo_area_buffer[this_one])
9916 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9917 echo_area_buffer[this_one] = Qnil;
9918 }
9919
9920 /* Choose a suitable buffer from echo_buffer[] is we don't
9921 have one. */
9922 if (NILP (echo_area_buffer[this_one]))
9923 {
9924 echo_area_buffer[this_one]
9925 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9926 ? echo_buffer[the_other]
9927 : echo_buffer[this_one]);
9928 clear_buffer_p = 1;
9929 }
9930
9931 buffer = echo_area_buffer[this_one];
9932
9933 /* Don't get confused by reusing the buffer used for echoing
9934 for a different purpose. */
9935 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9936 cancel_echoing ();
9937
9938 record_unwind_protect (unwind_with_echo_area_buffer,
9939 with_echo_area_buffer_unwind_data (w));
9940
9941 /* Make the echo area buffer current. Note that for display
9942 purposes, it is not necessary that the displayed window's buffer
9943 == current_buffer, except for text property lookup. So, let's
9944 only set that buffer temporarily here without doing a full
9945 Fset_window_buffer. We must also change w->pointm, though,
9946 because otherwise an assertions in unshow_buffer fails, and Emacs
9947 aborts. */
9948 set_buffer_internal_1 (XBUFFER (buffer));
9949 if (w)
9950 {
9951 w->buffer = buffer;
9952 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9953 }
9954
9955 BVAR (current_buffer, undo_list) = Qt;
9956 BVAR (current_buffer, read_only) = Qnil;
9957 specbind (Qinhibit_read_only, Qt);
9958 specbind (Qinhibit_modification_hooks, Qt);
9959
9960 if (clear_buffer_p && Z > BEG)
9961 del_range (BEG, Z);
9962
9963 eassert (BEGV >= BEG);
9964 eassert (ZV <= Z && ZV >= BEGV);
9965
9966 rc = fn (a1, a2, a3, a4);
9967
9968 eassert (BEGV >= BEG);
9969 eassert (ZV <= Z && ZV >= BEGV);
9970
9971 unbind_to (count, Qnil);
9972 return rc;
9973 }
9974
9975
9976 /* Save state that should be preserved around the call to the function
9977 FN called in with_echo_area_buffer. */
9978
9979 static Lisp_Object
9980 with_echo_area_buffer_unwind_data (struct window *w)
9981 {
9982 int i = 0;
9983 Lisp_Object vector, tmp;
9984
9985 /* Reduce consing by keeping one vector in
9986 Vwith_echo_area_save_vector. */
9987 vector = Vwith_echo_area_save_vector;
9988 Vwith_echo_area_save_vector = Qnil;
9989
9990 if (NILP (vector))
9991 vector = Fmake_vector (make_number (7), Qnil);
9992
9993 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9994 ASET (vector, i, Vdeactivate_mark); ++i;
9995 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9996
9997 if (w)
9998 {
9999 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10000 ASET (vector, i, w->buffer); ++i;
10001 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10002 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10003 }
10004 else
10005 {
10006 int end = i + 4;
10007 for (; i < end; ++i)
10008 ASET (vector, i, Qnil);
10009 }
10010
10011 eassert (i == ASIZE (vector));
10012 return vector;
10013 }
10014
10015
10016 /* Restore global state from VECTOR which was created by
10017 with_echo_area_buffer_unwind_data. */
10018
10019 static Lisp_Object
10020 unwind_with_echo_area_buffer (Lisp_Object vector)
10021 {
10022 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10023 Vdeactivate_mark = AREF (vector, 1);
10024 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10025
10026 if (WINDOWP (AREF (vector, 3)))
10027 {
10028 struct window *w;
10029 Lisp_Object buffer, charpos, bytepos;
10030
10031 w = XWINDOW (AREF (vector, 3));
10032 buffer = AREF (vector, 4);
10033 charpos = AREF (vector, 5);
10034 bytepos = AREF (vector, 6);
10035
10036 w->buffer = buffer;
10037 set_marker_both (w->pointm, buffer,
10038 XFASTINT (charpos), XFASTINT (bytepos));
10039 }
10040
10041 Vwith_echo_area_save_vector = vector;
10042 return Qnil;
10043 }
10044
10045
10046 /* Set up the echo area for use by print functions. MULTIBYTE_P
10047 non-zero means we will print multibyte. */
10048
10049 void
10050 setup_echo_area_for_printing (int multibyte_p)
10051 {
10052 /* If we can't find an echo area any more, exit. */
10053 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10054 Fkill_emacs (Qnil);
10055
10056 ensure_echo_area_buffers ();
10057
10058 if (!message_buf_print)
10059 {
10060 /* A message has been output since the last time we printed.
10061 Choose a fresh echo area buffer. */
10062 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10063 echo_area_buffer[0] = echo_buffer[1];
10064 else
10065 echo_area_buffer[0] = echo_buffer[0];
10066
10067 /* Switch to that buffer and clear it. */
10068 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10069 BVAR (current_buffer, truncate_lines) = Qnil;
10070
10071 if (Z > BEG)
10072 {
10073 ptrdiff_t count = SPECPDL_INDEX ();
10074 specbind (Qinhibit_read_only, Qt);
10075 /* Note that undo recording is always disabled. */
10076 del_range (BEG, Z);
10077 unbind_to (count, Qnil);
10078 }
10079 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10080
10081 /* Set up the buffer for the multibyteness we need. */
10082 if (multibyte_p
10083 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10084 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10085
10086 /* Raise the frame containing the echo area. */
10087 if (minibuffer_auto_raise)
10088 {
10089 struct frame *sf = SELECTED_FRAME ();
10090 Lisp_Object mini_window;
10091 mini_window = FRAME_MINIBUF_WINDOW (sf);
10092 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10093 }
10094
10095 message_log_maybe_newline ();
10096 message_buf_print = 1;
10097 }
10098 else
10099 {
10100 if (NILP (echo_area_buffer[0]))
10101 {
10102 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10103 echo_area_buffer[0] = echo_buffer[1];
10104 else
10105 echo_area_buffer[0] = echo_buffer[0];
10106 }
10107
10108 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10109 {
10110 /* Someone switched buffers between print requests. */
10111 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10112 BVAR (current_buffer, truncate_lines) = Qnil;
10113 }
10114 }
10115 }
10116
10117
10118 /* Display an echo area message in window W. Value is non-zero if W's
10119 height is changed. If display_last_displayed_message_p is
10120 non-zero, display the message that was last displayed, otherwise
10121 display the current message. */
10122
10123 static int
10124 display_echo_area (struct window *w)
10125 {
10126 int i, no_message_p, window_height_changed_p;
10127
10128 /* Temporarily disable garbage collections while displaying the echo
10129 area. This is done because a GC can print a message itself.
10130 That message would modify the echo area buffer's contents while a
10131 redisplay of the buffer is going on, and seriously confuse
10132 redisplay. */
10133 ptrdiff_t count = inhibit_garbage_collection ();
10134
10135 /* If there is no message, we must call display_echo_area_1
10136 nevertheless because it resizes the window. But we will have to
10137 reset the echo_area_buffer in question to nil at the end because
10138 with_echo_area_buffer will sets it to an empty buffer. */
10139 i = display_last_displayed_message_p ? 1 : 0;
10140 no_message_p = NILP (echo_area_buffer[i]);
10141
10142 window_height_changed_p
10143 = with_echo_area_buffer (w, display_last_displayed_message_p,
10144 display_echo_area_1,
10145 (intptr_t) w, Qnil, 0, 0);
10146
10147 if (no_message_p)
10148 echo_area_buffer[i] = Qnil;
10149
10150 unbind_to (count, Qnil);
10151 return window_height_changed_p;
10152 }
10153
10154
10155 /* Helper for display_echo_area. Display the current buffer which
10156 contains the current echo area message in window W, a mini-window,
10157 a pointer to which is passed in A1. A2..A4 are currently not used.
10158 Change the height of W so that all of the message is displayed.
10159 Value is non-zero if height of W was changed. */
10160
10161 static int
10162 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10163 {
10164 intptr_t i1 = a1;
10165 struct window *w = (struct window *) i1;
10166 Lisp_Object window;
10167 struct text_pos start;
10168 int window_height_changed_p = 0;
10169
10170 /* Do this before displaying, so that we have a large enough glyph
10171 matrix for the display. If we can't get enough space for the
10172 whole text, display the last N lines. That works by setting w->start. */
10173 window_height_changed_p = resize_mini_window (w, 0);
10174
10175 /* Use the starting position chosen by resize_mini_window. */
10176 SET_TEXT_POS_FROM_MARKER (start, w->start);
10177
10178 /* Display. */
10179 clear_glyph_matrix (w->desired_matrix);
10180 XSETWINDOW (window, w);
10181 try_window (window, start, 0);
10182
10183 return window_height_changed_p;
10184 }
10185
10186
10187 /* Resize the echo area window to exactly the size needed for the
10188 currently displayed message, if there is one. If a mini-buffer
10189 is active, don't shrink it. */
10190
10191 void
10192 resize_echo_area_exactly (void)
10193 {
10194 if (BUFFERP (echo_area_buffer[0])
10195 && WINDOWP (echo_area_window))
10196 {
10197 struct window *w = XWINDOW (echo_area_window);
10198 int resized_p;
10199 Lisp_Object resize_exactly;
10200
10201 if (minibuf_level == 0)
10202 resize_exactly = Qt;
10203 else
10204 resize_exactly = Qnil;
10205
10206 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10207 (intptr_t) w, resize_exactly,
10208 0, 0);
10209 if (resized_p)
10210 {
10211 ++windows_or_buffers_changed;
10212 ++update_mode_lines;
10213 redisplay_internal ();
10214 }
10215 }
10216 }
10217
10218
10219 /* Callback function for with_echo_area_buffer, when used from
10220 resize_echo_area_exactly. A1 contains a pointer to the window to
10221 resize, EXACTLY non-nil means resize the mini-window exactly to the
10222 size of the text displayed. A3 and A4 are not used. Value is what
10223 resize_mini_window returns. */
10224
10225 static int
10226 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10227 {
10228 intptr_t i1 = a1;
10229 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10230 }
10231
10232
10233 /* Resize mini-window W to fit the size of its contents. EXACT_P
10234 means size the window exactly to the size needed. Otherwise, it's
10235 only enlarged until W's buffer is empty.
10236
10237 Set W->start to the right place to begin display. If the whole
10238 contents fit, start at the beginning. Otherwise, start so as
10239 to make the end of the contents appear. This is particularly
10240 important for y-or-n-p, but seems desirable generally.
10241
10242 Value is non-zero if the window height has been changed. */
10243
10244 int
10245 resize_mini_window (struct window *w, int exact_p)
10246 {
10247 struct frame *f = XFRAME (w->frame);
10248 int window_height_changed_p = 0;
10249
10250 eassert (MINI_WINDOW_P (w));
10251
10252 /* By default, start display at the beginning. */
10253 set_marker_both (w->start, w->buffer,
10254 BUF_BEGV (XBUFFER (w->buffer)),
10255 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10256
10257 /* Don't resize windows while redisplaying a window; it would
10258 confuse redisplay functions when the size of the window they are
10259 displaying changes from under them. Such a resizing can happen,
10260 for instance, when which-func prints a long message while
10261 we are running fontification-functions. We're running these
10262 functions with safe_call which binds inhibit-redisplay to t. */
10263 if (!NILP (Vinhibit_redisplay))
10264 return 0;
10265
10266 /* Nil means don't try to resize. */
10267 if (NILP (Vresize_mini_windows)
10268 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10269 return 0;
10270
10271 if (!FRAME_MINIBUF_ONLY_P (f))
10272 {
10273 struct it it;
10274 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10275 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10276 int height;
10277 EMACS_INT max_height;
10278 int unit = FRAME_LINE_HEIGHT (f);
10279 struct text_pos start;
10280 struct buffer *old_current_buffer = NULL;
10281
10282 if (current_buffer != XBUFFER (w->buffer))
10283 {
10284 old_current_buffer = current_buffer;
10285 set_buffer_internal (XBUFFER (w->buffer));
10286 }
10287
10288 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10289
10290 /* Compute the max. number of lines specified by the user. */
10291 if (FLOATP (Vmax_mini_window_height))
10292 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10293 else if (INTEGERP (Vmax_mini_window_height))
10294 max_height = XINT (Vmax_mini_window_height);
10295 else
10296 max_height = total_height / 4;
10297
10298 /* Correct that max. height if it's bogus. */
10299 max_height = max (1, max_height);
10300 max_height = min (total_height, max_height);
10301
10302 /* Find out the height of the text in the window. */
10303 if (it.line_wrap == TRUNCATE)
10304 height = 1;
10305 else
10306 {
10307 last_height = 0;
10308 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10309 if (it.max_ascent == 0 && it.max_descent == 0)
10310 height = it.current_y + last_height;
10311 else
10312 height = it.current_y + it.max_ascent + it.max_descent;
10313 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10314 height = (height + unit - 1) / unit;
10315 }
10316
10317 /* Compute a suitable window start. */
10318 if (height > max_height)
10319 {
10320 height = max_height;
10321 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10322 move_it_vertically_backward (&it, (height - 1) * unit);
10323 start = it.current.pos;
10324 }
10325 else
10326 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10327 SET_MARKER_FROM_TEXT_POS (w->start, start);
10328
10329 if (EQ (Vresize_mini_windows, Qgrow_only))
10330 {
10331 /* Let it grow only, until we display an empty message, in which
10332 case the window shrinks again. */
10333 if (height > WINDOW_TOTAL_LINES (w))
10334 {
10335 int old_height = WINDOW_TOTAL_LINES (w);
10336 freeze_window_starts (f, 1);
10337 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10338 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10339 }
10340 else if (height < WINDOW_TOTAL_LINES (w)
10341 && (exact_p || BEGV == ZV))
10342 {
10343 int old_height = WINDOW_TOTAL_LINES (w);
10344 freeze_window_starts (f, 0);
10345 shrink_mini_window (w);
10346 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10347 }
10348 }
10349 else
10350 {
10351 /* Always resize to exact size needed. */
10352 if (height > WINDOW_TOTAL_LINES (w))
10353 {
10354 int old_height = WINDOW_TOTAL_LINES (w);
10355 freeze_window_starts (f, 1);
10356 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10357 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10358 }
10359 else if (height < WINDOW_TOTAL_LINES (w))
10360 {
10361 int old_height = WINDOW_TOTAL_LINES (w);
10362 freeze_window_starts (f, 0);
10363 shrink_mini_window (w);
10364
10365 if (height)
10366 {
10367 freeze_window_starts (f, 1);
10368 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10369 }
10370
10371 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10372 }
10373 }
10374
10375 if (old_current_buffer)
10376 set_buffer_internal (old_current_buffer);
10377 }
10378
10379 return window_height_changed_p;
10380 }
10381
10382
10383 /* Value is the current message, a string, or nil if there is no
10384 current message. */
10385
10386 Lisp_Object
10387 current_message (void)
10388 {
10389 Lisp_Object msg;
10390
10391 if (!BUFFERP (echo_area_buffer[0]))
10392 msg = Qnil;
10393 else
10394 {
10395 with_echo_area_buffer (0, 0, current_message_1,
10396 (intptr_t) &msg, Qnil, 0, 0);
10397 if (NILP (msg))
10398 echo_area_buffer[0] = Qnil;
10399 }
10400
10401 return msg;
10402 }
10403
10404
10405 static int
10406 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10407 {
10408 intptr_t i1 = a1;
10409 Lisp_Object *msg = (Lisp_Object *) i1;
10410
10411 if (Z > BEG)
10412 *msg = make_buffer_string (BEG, Z, 1);
10413 else
10414 *msg = Qnil;
10415 return 0;
10416 }
10417
10418
10419 /* Push the current message on Vmessage_stack for later restoration
10420 by restore_message. Value is non-zero if the current message isn't
10421 empty. This is a relatively infrequent operation, so it's not
10422 worth optimizing. */
10423
10424 int
10425 push_message (void)
10426 {
10427 Lisp_Object msg;
10428 msg = current_message ();
10429 Vmessage_stack = Fcons (msg, Vmessage_stack);
10430 return STRINGP (msg);
10431 }
10432
10433
10434 /* Restore message display from the top of Vmessage_stack. */
10435
10436 void
10437 restore_message (void)
10438 {
10439 Lisp_Object msg;
10440
10441 eassert (CONSP (Vmessage_stack));
10442 msg = XCAR (Vmessage_stack);
10443 if (STRINGP (msg))
10444 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10445 else
10446 message3_nolog (msg, 0, 0);
10447 }
10448
10449
10450 /* Handler for record_unwind_protect calling pop_message. */
10451
10452 Lisp_Object
10453 pop_message_unwind (Lisp_Object dummy)
10454 {
10455 pop_message ();
10456 return Qnil;
10457 }
10458
10459 /* Pop the top-most entry off Vmessage_stack. */
10460
10461 static void
10462 pop_message (void)
10463 {
10464 eassert (CONSP (Vmessage_stack));
10465 Vmessage_stack = XCDR (Vmessage_stack);
10466 }
10467
10468
10469 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10470 exits. If the stack is not empty, we have a missing pop_message
10471 somewhere. */
10472
10473 void
10474 check_message_stack (void)
10475 {
10476 if (!NILP (Vmessage_stack))
10477 abort ();
10478 }
10479
10480
10481 /* Truncate to NCHARS what will be displayed in the echo area the next
10482 time we display it---but don't redisplay it now. */
10483
10484 void
10485 truncate_echo_area (ptrdiff_t nchars)
10486 {
10487 if (nchars == 0)
10488 echo_area_buffer[0] = Qnil;
10489 /* A null message buffer means that the frame hasn't really been
10490 initialized yet. Error messages get reported properly by
10491 cmd_error, so this must be just an informative message; toss it. */
10492 else if (!noninteractive
10493 && INTERACTIVE
10494 && !NILP (echo_area_buffer[0]))
10495 {
10496 struct frame *sf = SELECTED_FRAME ();
10497 if (FRAME_MESSAGE_BUF (sf))
10498 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10499 }
10500 }
10501
10502
10503 /* Helper function for truncate_echo_area. Truncate the current
10504 message to at most NCHARS characters. */
10505
10506 static int
10507 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10508 {
10509 if (BEG + nchars < Z)
10510 del_range (BEG + nchars, Z);
10511 if (Z == BEG)
10512 echo_area_buffer[0] = Qnil;
10513 return 0;
10514 }
10515
10516
10517 /* Set the current message to a substring of S or STRING.
10518
10519 If STRING is a Lisp string, set the message to the first NBYTES
10520 bytes from STRING. NBYTES zero means use the whole string. If
10521 STRING is multibyte, the message will be displayed multibyte.
10522
10523 If S is not null, set the message to the first LEN bytes of S. LEN
10524 zero means use the whole string. MULTIBYTE_P non-zero means S is
10525 multibyte. Display the message multibyte in that case.
10526
10527 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10528 to t before calling set_message_1 (which calls insert).
10529 */
10530
10531 static void
10532 set_message (const char *s, Lisp_Object string,
10533 ptrdiff_t nbytes, int multibyte_p)
10534 {
10535 message_enable_multibyte
10536 = ((s && multibyte_p)
10537 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10538
10539 with_echo_area_buffer (0, -1, set_message_1,
10540 (intptr_t) s, string, nbytes, multibyte_p);
10541 message_buf_print = 0;
10542 help_echo_showing_p = 0;
10543 }
10544
10545
10546 /* Helper function for set_message. Arguments have the same meaning
10547 as there, with A1 corresponding to S and A2 corresponding to STRING
10548 This function is called with the echo area buffer being
10549 current. */
10550
10551 static int
10552 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10553 {
10554 intptr_t i1 = a1;
10555 const char *s = (const char *) i1;
10556 const unsigned char *msg = (const unsigned char *) s;
10557 Lisp_Object string = a2;
10558
10559 /* Change multibyteness of the echo buffer appropriately. */
10560 if (message_enable_multibyte
10561 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10562 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10563
10564 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10565 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10566 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10567
10568 /* Insert new message at BEG. */
10569 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10570
10571 if (STRINGP (string))
10572 {
10573 ptrdiff_t nchars;
10574
10575 if (nbytes == 0)
10576 nbytes = SBYTES (string);
10577 nchars = string_byte_to_char (string, nbytes);
10578
10579 /* This function takes care of single/multibyte conversion. We
10580 just have to ensure that the echo area buffer has the right
10581 setting of enable_multibyte_characters. */
10582 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10583 }
10584 else if (s)
10585 {
10586 if (nbytes == 0)
10587 nbytes = strlen (s);
10588
10589 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10590 {
10591 /* Convert from multi-byte to single-byte. */
10592 ptrdiff_t i;
10593 int c, n;
10594 char work[1];
10595
10596 /* Convert a multibyte string to single-byte. */
10597 for (i = 0; i < nbytes; i += n)
10598 {
10599 c = string_char_and_length (msg + i, &n);
10600 work[0] = (ASCII_CHAR_P (c)
10601 ? c
10602 : multibyte_char_to_unibyte (c));
10603 insert_1_both (work, 1, 1, 1, 0, 0);
10604 }
10605 }
10606 else if (!multibyte_p
10607 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10608 {
10609 /* Convert from single-byte to multi-byte. */
10610 ptrdiff_t i;
10611 int c, n;
10612 unsigned char str[MAX_MULTIBYTE_LENGTH];
10613
10614 /* Convert a single-byte string to multibyte. */
10615 for (i = 0; i < nbytes; i++)
10616 {
10617 c = msg[i];
10618 MAKE_CHAR_MULTIBYTE (c);
10619 n = CHAR_STRING (c, str);
10620 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10621 }
10622 }
10623 else
10624 insert_1 (s, nbytes, 1, 0, 0);
10625 }
10626
10627 return 0;
10628 }
10629
10630
10631 /* Clear messages. CURRENT_P non-zero means clear the current
10632 message. LAST_DISPLAYED_P non-zero means clear the message
10633 last displayed. */
10634
10635 void
10636 clear_message (int current_p, int last_displayed_p)
10637 {
10638 if (current_p)
10639 {
10640 echo_area_buffer[0] = Qnil;
10641 message_cleared_p = 1;
10642 }
10643
10644 if (last_displayed_p)
10645 echo_area_buffer[1] = Qnil;
10646
10647 message_buf_print = 0;
10648 }
10649
10650 /* Clear garbaged frames.
10651
10652 This function is used where the old redisplay called
10653 redraw_garbaged_frames which in turn called redraw_frame which in
10654 turn called clear_frame. The call to clear_frame was a source of
10655 flickering. I believe a clear_frame is not necessary. It should
10656 suffice in the new redisplay to invalidate all current matrices,
10657 and ensure a complete redisplay of all windows. */
10658
10659 static void
10660 clear_garbaged_frames (void)
10661 {
10662 if (frame_garbaged)
10663 {
10664 Lisp_Object tail, frame;
10665 int changed_count = 0;
10666
10667 FOR_EACH_FRAME (tail, frame)
10668 {
10669 struct frame *f = XFRAME (frame);
10670
10671 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10672 {
10673 if (f->resized_p)
10674 {
10675 Fredraw_frame (frame);
10676 f->force_flush_display_p = 1;
10677 }
10678 clear_current_matrices (f);
10679 changed_count++;
10680 f->garbaged = 0;
10681 f->resized_p = 0;
10682 }
10683 }
10684
10685 frame_garbaged = 0;
10686 if (changed_count)
10687 ++windows_or_buffers_changed;
10688 }
10689 }
10690
10691
10692 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10693 is non-zero update selected_frame. Value is non-zero if the
10694 mini-windows height has been changed. */
10695
10696 static int
10697 echo_area_display (int update_frame_p)
10698 {
10699 Lisp_Object mini_window;
10700 struct window *w;
10701 struct frame *f;
10702 int window_height_changed_p = 0;
10703 struct frame *sf = SELECTED_FRAME ();
10704
10705 mini_window = FRAME_MINIBUF_WINDOW (sf);
10706 w = XWINDOW (mini_window);
10707 f = XFRAME (WINDOW_FRAME (w));
10708
10709 /* Don't display if frame is invisible or not yet initialized. */
10710 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10711 return 0;
10712
10713 #ifdef HAVE_WINDOW_SYSTEM
10714 /* When Emacs starts, selected_frame may be the initial terminal
10715 frame. If we let this through, a message would be displayed on
10716 the terminal. */
10717 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10718 return 0;
10719 #endif /* HAVE_WINDOW_SYSTEM */
10720
10721 /* Redraw garbaged frames. */
10722 if (frame_garbaged)
10723 clear_garbaged_frames ();
10724
10725 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10726 {
10727 echo_area_window = mini_window;
10728 window_height_changed_p = display_echo_area (w);
10729 w->must_be_updated_p = 1;
10730
10731 /* Update the display, unless called from redisplay_internal.
10732 Also don't update the screen during redisplay itself. The
10733 update will happen at the end of redisplay, and an update
10734 here could cause confusion. */
10735 if (update_frame_p && !redisplaying_p)
10736 {
10737 int n = 0;
10738
10739 /* If the display update has been interrupted by pending
10740 input, update mode lines in the frame. Due to the
10741 pending input, it might have been that redisplay hasn't
10742 been called, so that mode lines above the echo area are
10743 garbaged. This looks odd, so we prevent it here. */
10744 if (!display_completed)
10745 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10746
10747 if (window_height_changed_p
10748 /* Don't do this if Emacs is shutting down. Redisplay
10749 needs to run hooks. */
10750 && !NILP (Vrun_hooks))
10751 {
10752 /* Must update other windows. Likewise as in other
10753 cases, don't let this update be interrupted by
10754 pending input. */
10755 ptrdiff_t count = SPECPDL_INDEX ();
10756 specbind (Qredisplay_dont_pause, Qt);
10757 windows_or_buffers_changed = 1;
10758 redisplay_internal ();
10759 unbind_to (count, Qnil);
10760 }
10761 else if (FRAME_WINDOW_P (f) && n == 0)
10762 {
10763 /* Window configuration is the same as before.
10764 Can do with a display update of the echo area,
10765 unless we displayed some mode lines. */
10766 update_single_window (w, 1);
10767 FRAME_RIF (f)->flush_display (f);
10768 }
10769 else
10770 update_frame (f, 1, 1);
10771
10772 /* If cursor is in the echo area, make sure that the next
10773 redisplay displays the minibuffer, so that the cursor will
10774 be replaced with what the minibuffer wants. */
10775 if (cursor_in_echo_area)
10776 ++windows_or_buffers_changed;
10777 }
10778 }
10779 else if (!EQ (mini_window, selected_window))
10780 windows_or_buffers_changed++;
10781
10782 /* Last displayed message is now the current message. */
10783 echo_area_buffer[1] = echo_area_buffer[0];
10784 /* Inform read_char that we're not echoing. */
10785 echo_message_buffer = Qnil;
10786
10787 /* Prevent redisplay optimization in redisplay_internal by resetting
10788 this_line_start_pos. This is done because the mini-buffer now
10789 displays the message instead of its buffer text. */
10790 if (EQ (mini_window, selected_window))
10791 CHARPOS (this_line_start_pos) = 0;
10792
10793 return window_height_changed_p;
10794 }
10795
10796
10797 \f
10798 /***********************************************************************
10799 Mode Lines and Frame Titles
10800 ***********************************************************************/
10801
10802 /* A buffer for constructing non-propertized mode-line strings and
10803 frame titles in it; allocated from the heap in init_xdisp and
10804 resized as needed in store_mode_line_noprop_char. */
10805
10806 static char *mode_line_noprop_buf;
10807
10808 /* The buffer's end, and a current output position in it. */
10809
10810 static char *mode_line_noprop_buf_end;
10811 static char *mode_line_noprop_ptr;
10812
10813 #define MODE_LINE_NOPROP_LEN(start) \
10814 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10815
10816 static enum {
10817 MODE_LINE_DISPLAY = 0,
10818 MODE_LINE_TITLE,
10819 MODE_LINE_NOPROP,
10820 MODE_LINE_STRING
10821 } mode_line_target;
10822
10823 /* Alist that caches the results of :propertize.
10824 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10825 static Lisp_Object mode_line_proptrans_alist;
10826
10827 /* List of strings making up the mode-line. */
10828 static Lisp_Object mode_line_string_list;
10829
10830 /* Base face property when building propertized mode line string. */
10831 static Lisp_Object mode_line_string_face;
10832 static Lisp_Object mode_line_string_face_prop;
10833
10834
10835 /* Unwind data for mode line strings */
10836
10837 static Lisp_Object Vmode_line_unwind_vector;
10838
10839 static Lisp_Object
10840 format_mode_line_unwind_data (struct frame *target_frame,
10841 struct buffer *obuf,
10842 Lisp_Object owin,
10843 int save_proptrans)
10844 {
10845 Lisp_Object vector, tmp;
10846
10847 /* Reduce consing by keeping one vector in
10848 Vwith_echo_area_save_vector. */
10849 vector = Vmode_line_unwind_vector;
10850 Vmode_line_unwind_vector = Qnil;
10851
10852 if (NILP (vector))
10853 vector = Fmake_vector (make_number (10), Qnil);
10854
10855 ASET (vector, 0, make_number (mode_line_target));
10856 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10857 ASET (vector, 2, mode_line_string_list);
10858 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10859 ASET (vector, 4, mode_line_string_face);
10860 ASET (vector, 5, mode_line_string_face_prop);
10861
10862 if (obuf)
10863 XSETBUFFER (tmp, obuf);
10864 else
10865 tmp = Qnil;
10866 ASET (vector, 6, tmp);
10867 ASET (vector, 7, owin);
10868 if (target_frame)
10869 {
10870 /* Similarly to `with-selected-window', if the operation selects
10871 a window on another frame, we must restore that frame's
10872 selected window, and (for a tty) the top-frame. */
10873 ASET (vector, 8, target_frame->selected_window);
10874 if (FRAME_TERMCAP_P (target_frame))
10875 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10876 }
10877
10878 return vector;
10879 }
10880
10881 static Lisp_Object
10882 unwind_format_mode_line (Lisp_Object vector)
10883 {
10884 Lisp_Object old_window = AREF (vector, 7);
10885 Lisp_Object target_frame_window = AREF (vector, 8);
10886 Lisp_Object old_top_frame = AREF (vector, 9);
10887
10888 mode_line_target = XINT (AREF (vector, 0));
10889 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10890 mode_line_string_list = AREF (vector, 2);
10891 if (! EQ (AREF (vector, 3), Qt))
10892 mode_line_proptrans_alist = AREF (vector, 3);
10893 mode_line_string_face = AREF (vector, 4);
10894 mode_line_string_face_prop = AREF (vector, 5);
10895
10896 /* Select window before buffer, since it may change the buffer. */
10897 if (!NILP (old_window))
10898 {
10899 /* If the operation that we are unwinding had selected a window
10900 on a different frame, reset its frame-selected-window. For a
10901 text terminal, reset its top-frame if necessary. */
10902 if (!NILP (target_frame_window))
10903 {
10904 Lisp_Object frame
10905 = WINDOW_FRAME (XWINDOW (target_frame_window));
10906
10907 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10908 Fselect_window (target_frame_window, Qt);
10909
10910 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10911 Fselect_frame (old_top_frame, Qt);
10912 }
10913
10914 Fselect_window (old_window, Qt);
10915 }
10916
10917 if (!NILP (AREF (vector, 6)))
10918 {
10919 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10920 ASET (vector, 6, Qnil);
10921 }
10922
10923 Vmode_line_unwind_vector = vector;
10924 return Qnil;
10925 }
10926
10927
10928 /* Store a single character C for the frame title in mode_line_noprop_buf.
10929 Re-allocate mode_line_noprop_buf if necessary. */
10930
10931 static void
10932 store_mode_line_noprop_char (char c)
10933 {
10934 /* If output position has reached the end of the allocated buffer,
10935 increase the buffer's size. */
10936 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10937 {
10938 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10939 ptrdiff_t size = len;
10940 mode_line_noprop_buf =
10941 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10942 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10943 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10944 }
10945
10946 *mode_line_noprop_ptr++ = c;
10947 }
10948
10949
10950 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10951 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10952 characters that yield more columns than PRECISION; PRECISION <= 0
10953 means copy the whole string. Pad with spaces until FIELD_WIDTH
10954 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10955 pad. Called from display_mode_element when it is used to build a
10956 frame title. */
10957
10958 static int
10959 store_mode_line_noprop (const char *string, int field_width, int precision)
10960 {
10961 const unsigned char *str = (const unsigned char *) string;
10962 int n = 0;
10963 ptrdiff_t dummy, nbytes;
10964
10965 /* Copy at most PRECISION chars from STR. */
10966 nbytes = strlen (string);
10967 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10968 while (nbytes--)
10969 store_mode_line_noprop_char (*str++);
10970
10971 /* Fill up with spaces until FIELD_WIDTH reached. */
10972 while (field_width > 0
10973 && n < field_width)
10974 {
10975 store_mode_line_noprop_char (' ');
10976 ++n;
10977 }
10978
10979 return n;
10980 }
10981
10982 /***********************************************************************
10983 Frame Titles
10984 ***********************************************************************/
10985
10986 #ifdef HAVE_WINDOW_SYSTEM
10987
10988 /* Set the title of FRAME, if it has changed. The title format is
10989 Vicon_title_format if FRAME is iconified, otherwise it is
10990 frame_title_format. */
10991
10992 static void
10993 x_consider_frame_title (Lisp_Object frame)
10994 {
10995 struct frame *f = XFRAME (frame);
10996
10997 if (FRAME_WINDOW_P (f)
10998 || FRAME_MINIBUF_ONLY_P (f)
10999 || f->explicit_name)
11000 {
11001 /* Do we have more than one visible frame on this X display? */
11002 Lisp_Object tail;
11003 Lisp_Object fmt;
11004 ptrdiff_t title_start;
11005 char *title;
11006 ptrdiff_t len;
11007 struct it it;
11008 ptrdiff_t count = SPECPDL_INDEX ();
11009
11010 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11011 {
11012 Lisp_Object other_frame = XCAR (tail);
11013 struct frame *tf = XFRAME (other_frame);
11014
11015 if (tf != f
11016 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11017 && !FRAME_MINIBUF_ONLY_P (tf)
11018 && !EQ (other_frame, tip_frame)
11019 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11020 break;
11021 }
11022
11023 /* Set global variable indicating that multiple frames exist. */
11024 multiple_frames = CONSP (tail);
11025
11026 /* Switch to the buffer of selected window of the frame. Set up
11027 mode_line_target so that display_mode_element will output into
11028 mode_line_noprop_buf; then display the title. */
11029 record_unwind_protect (unwind_format_mode_line,
11030 format_mode_line_unwind_data
11031 (f, current_buffer, selected_window, 0));
11032
11033 Fselect_window (f->selected_window, Qt);
11034 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11035 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11036
11037 mode_line_target = MODE_LINE_TITLE;
11038 title_start = MODE_LINE_NOPROP_LEN (0);
11039 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11040 NULL, DEFAULT_FACE_ID);
11041 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11042 len = MODE_LINE_NOPROP_LEN (title_start);
11043 title = mode_line_noprop_buf + title_start;
11044 unbind_to (count, Qnil);
11045
11046 /* Set the title only if it's changed. This avoids consing in
11047 the common case where it hasn't. (If it turns out that we've
11048 already wasted too much time by walking through the list with
11049 display_mode_element, then we might need to optimize at a
11050 higher level than this.) */
11051 if (! STRINGP (f->name)
11052 || SBYTES (f->name) != len
11053 || memcmp (title, SDATA (f->name), len) != 0)
11054 x_implicitly_set_name (f, make_string (title, len), Qnil);
11055 }
11056 }
11057
11058 #endif /* not HAVE_WINDOW_SYSTEM */
11059
11060 \f
11061 /***********************************************************************
11062 Menu Bars
11063 ***********************************************************************/
11064
11065
11066 /* Prepare for redisplay by updating menu-bar item lists when
11067 appropriate. This can call eval. */
11068
11069 void
11070 prepare_menu_bars (void)
11071 {
11072 int all_windows;
11073 struct gcpro gcpro1, gcpro2;
11074 struct frame *f;
11075 Lisp_Object tooltip_frame;
11076
11077 #ifdef HAVE_WINDOW_SYSTEM
11078 tooltip_frame = tip_frame;
11079 #else
11080 tooltip_frame = Qnil;
11081 #endif
11082
11083 /* Update all frame titles based on their buffer names, etc. We do
11084 this before the menu bars so that the buffer-menu will show the
11085 up-to-date frame titles. */
11086 #ifdef HAVE_WINDOW_SYSTEM
11087 if (windows_or_buffers_changed || update_mode_lines)
11088 {
11089 Lisp_Object tail, frame;
11090
11091 FOR_EACH_FRAME (tail, frame)
11092 {
11093 f = XFRAME (frame);
11094 if (!EQ (frame, tooltip_frame)
11095 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11096 x_consider_frame_title (frame);
11097 }
11098 }
11099 #endif /* HAVE_WINDOW_SYSTEM */
11100
11101 /* Update the menu bar item lists, if appropriate. This has to be
11102 done before any actual redisplay or generation of display lines. */
11103 all_windows = (update_mode_lines
11104 || buffer_shared > 1
11105 || windows_or_buffers_changed);
11106 if (all_windows)
11107 {
11108 Lisp_Object tail, frame;
11109 ptrdiff_t count = SPECPDL_INDEX ();
11110 /* 1 means that update_menu_bar has run its hooks
11111 so any further calls to update_menu_bar shouldn't do so again. */
11112 int menu_bar_hooks_run = 0;
11113
11114 record_unwind_save_match_data ();
11115
11116 FOR_EACH_FRAME (tail, frame)
11117 {
11118 f = XFRAME (frame);
11119
11120 /* Ignore tooltip frame. */
11121 if (EQ (frame, tooltip_frame))
11122 continue;
11123
11124 /* If a window on this frame changed size, report that to
11125 the user and clear the size-change flag. */
11126 if (FRAME_WINDOW_SIZES_CHANGED (f))
11127 {
11128 Lisp_Object functions;
11129
11130 /* Clear flag first in case we get an error below. */
11131 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11132 functions = Vwindow_size_change_functions;
11133 GCPRO2 (tail, functions);
11134
11135 while (CONSP (functions))
11136 {
11137 if (!EQ (XCAR (functions), Qt))
11138 call1 (XCAR (functions), frame);
11139 functions = XCDR (functions);
11140 }
11141 UNGCPRO;
11142 }
11143
11144 GCPRO1 (tail);
11145 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11146 #ifdef HAVE_WINDOW_SYSTEM
11147 update_tool_bar (f, 0);
11148 #endif
11149 #ifdef HAVE_NS
11150 if (windows_or_buffers_changed
11151 && FRAME_NS_P (f))
11152 ns_set_doc_edited (f, Fbuffer_modified_p
11153 (XWINDOW (f->selected_window)->buffer));
11154 #endif
11155 UNGCPRO;
11156 }
11157
11158 unbind_to (count, Qnil);
11159 }
11160 else
11161 {
11162 struct frame *sf = SELECTED_FRAME ();
11163 update_menu_bar (sf, 1, 0);
11164 #ifdef HAVE_WINDOW_SYSTEM
11165 update_tool_bar (sf, 1);
11166 #endif
11167 }
11168 }
11169
11170
11171 /* Update the menu bar item list for frame F. This has to be done
11172 before we start to fill in any display lines, because it can call
11173 eval.
11174
11175 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11176
11177 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11178 already ran the menu bar hooks for this redisplay, so there
11179 is no need to run them again. The return value is the
11180 updated value of this flag, to pass to the next call. */
11181
11182 static int
11183 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11184 {
11185 Lisp_Object window;
11186 register struct window *w;
11187
11188 /* If called recursively during a menu update, do nothing. This can
11189 happen when, for instance, an activate-menubar-hook causes a
11190 redisplay. */
11191 if (inhibit_menubar_update)
11192 return hooks_run;
11193
11194 window = FRAME_SELECTED_WINDOW (f);
11195 w = XWINDOW (window);
11196
11197 if (FRAME_WINDOW_P (f)
11198 ?
11199 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11200 || defined (HAVE_NS) || defined (USE_GTK)
11201 FRAME_EXTERNAL_MENU_BAR (f)
11202 #else
11203 FRAME_MENU_BAR_LINES (f) > 0
11204 #endif
11205 : FRAME_MENU_BAR_LINES (f) > 0)
11206 {
11207 /* If the user has switched buffers or windows, we need to
11208 recompute to reflect the new bindings. But we'll
11209 recompute when update_mode_lines is set too; that means
11210 that people can use force-mode-line-update to request
11211 that the menu bar be recomputed. The adverse effect on
11212 the rest of the redisplay algorithm is about the same as
11213 windows_or_buffers_changed anyway. */
11214 if (windows_or_buffers_changed
11215 /* This used to test w->update_mode_line, but we believe
11216 there is no need to recompute the menu in that case. */
11217 || update_mode_lines
11218 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11219 < BUF_MODIFF (XBUFFER (w->buffer)))
11220 != w->last_had_star)
11221 || ((!NILP (Vtransient_mark_mode)
11222 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11223 != !NILP (w->region_showing)))
11224 {
11225 struct buffer *prev = current_buffer;
11226 ptrdiff_t count = SPECPDL_INDEX ();
11227
11228 specbind (Qinhibit_menubar_update, Qt);
11229
11230 set_buffer_internal_1 (XBUFFER (w->buffer));
11231 if (save_match_data)
11232 record_unwind_save_match_data ();
11233 if (NILP (Voverriding_local_map_menu_flag))
11234 {
11235 specbind (Qoverriding_terminal_local_map, Qnil);
11236 specbind (Qoverriding_local_map, Qnil);
11237 }
11238
11239 if (!hooks_run)
11240 {
11241 /* Run the Lucid hook. */
11242 safe_run_hooks (Qactivate_menubar_hook);
11243
11244 /* If it has changed current-menubar from previous value,
11245 really recompute the menu-bar from the value. */
11246 if (! NILP (Vlucid_menu_bar_dirty_flag))
11247 call0 (Qrecompute_lucid_menubar);
11248
11249 safe_run_hooks (Qmenu_bar_update_hook);
11250
11251 hooks_run = 1;
11252 }
11253
11254 XSETFRAME (Vmenu_updating_frame, f);
11255 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11256
11257 /* Redisplay the menu bar in case we changed it. */
11258 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11259 || defined (HAVE_NS) || defined (USE_GTK)
11260 if (FRAME_WINDOW_P (f))
11261 {
11262 #if defined (HAVE_NS)
11263 /* All frames on Mac OS share the same menubar. So only
11264 the selected frame should be allowed to set it. */
11265 if (f == SELECTED_FRAME ())
11266 #endif
11267 set_frame_menubar (f, 0, 0);
11268 }
11269 else
11270 /* On a terminal screen, the menu bar is an ordinary screen
11271 line, and this makes it get updated. */
11272 w->update_mode_line = 1;
11273 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11274 /* In the non-toolkit version, the menu bar is an ordinary screen
11275 line, and this makes it get updated. */
11276 w->update_mode_line = 1;
11277 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11278
11279 unbind_to (count, Qnil);
11280 set_buffer_internal_1 (prev);
11281 }
11282 }
11283
11284 return hooks_run;
11285 }
11286
11287
11288 \f
11289 /***********************************************************************
11290 Output Cursor
11291 ***********************************************************************/
11292
11293 #ifdef HAVE_WINDOW_SYSTEM
11294
11295 /* EXPORT:
11296 Nominal cursor position -- where to draw output.
11297 HPOS and VPOS are window relative glyph matrix coordinates.
11298 X and Y are window relative pixel coordinates. */
11299
11300 struct cursor_pos output_cursor;
11301
11302
11303 /* EXPORT:
11304 Set the global variable output_cursor to CURSOR. All cursor
11305 positions are relative to updated_window. */
11306
11307 void
11308 set_output_cursor (struct cursor_pos *cursor)
11309 {
11310 output_cursor.hpos = cursor->hpos;
11311 output_cursor.vpos = cursor->vpos;
11312 output_cursor.x = cursor->x;
11313 output_cursor.y = cursor->y;
11314 }
11315
11316
11317 /* EXPORT for RIF:
11318 Set a nominal cursor position.
11319
11320 HPOS and VPOS are column/row positions in a window glyph matrix. X
11321 and Y are window text area relative pixel positions.
11322
11323 If this is done during an update, updated_window will contain the
11324 window that is being updated and the position is the future output
11325 cursor position for that window. If updated_window is null, use
11326 selected_window and display the cursor at the given position. */
11327
11328 void
11329 x_cursor_to (int vpos, int hpos, int y, int x)
11330 {
11331 struct window *w;
11332
11333 /* If updated_window is not set, work on selected_window. */
11334 if (updated_window)
11335 w = updated_window;
11336 else
11337 w = XWINDOW (selected_window);
11338
11339 /* Set the output cursor. */
11340 output_cursor.hpos = hpos;
11341 output_cursor.vpos = vpos;
11342 output_cursor.x = x;
11343 output_cursor.y = y;
11344
11345 /* If not called as part of an update, really display the cursor.
11346 This will also set the cursor position of W. */
11347 if (updated_window == NULL)
11348 {
11349 BLOCK_INPUT;
11350 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11351 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11352 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11353 UNBLOCK_INPUT;
11354 }
11355 }
11356
11357 #endif /* HAVE_WINDOW_SYSTEM */
11358
11359 \f
11360 /***********************************************************************
11361 Tool-bars
11362 ***********************************************************************/
11363
11364 #ifdef HAVE_WINDOW_SYSTEM
11365
11366 /* Where the mouse was last time we reported a mouse event. */
11367
11368 FRAME_PTR last_mouse_frame;
11369
11370 /* Tool-bar item index of the item on which a mouse button was pressed
11371 or -1. */
11372
11373 int last_tool_bar_item;
11374
11375
11376 static Lisp_Object
11377 update_tool_bar_unwind (Lisp_Object frame)
11378 {
11379 selected_frame = frame;
11380 return Qnil;
11381 }
11382
11383 /* Update the tool-bar item list for frame F. This has to be done
11384 before we start to fill in any display lines. Called from
11385 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11386 and restore it here. */
11387
11388 static void
11389 update_tool_bar (struct frame *f, int save_match_data)
11390 {
11391 #if defined (USE_GTK) || defined (HAVE_NS)
11392 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11393 #else
11394 int do_update = WINDOWP (f->tool_bar_window)
11395 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11396 #endif
11397
11398 if (do_update)
11399 {
11400 Lisp_Object window;
11401 struct window *w;
11402
11403 window = FRAME_SELECTED_WINDOW (f);
11404 w = XWINDOW (window);
11405
11406 /* If the user has switched buffers or windows, we need to
11407 recompute to reflect the new bindings. But we'll
11408 recompute when update_mode_lines is set too; that means
11409 that people can use force-mode-line-update to request
11410 that the menu bar be recomputed. The adverse effect on
11411 the rest of the redisplay algorithm is about the same as
11412 windows_or_buffers_changed anyway. */
11413 if (windows_or_buffers_changed
11414 || w->update_mode_line
11415 || update_mode_lines
11416 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11417 < BUF_MODIFF (XBUFFER (w->buffer)))
11418 != w->last_had_star)
11419 || ((!NILP (Vtransient_mark_mode)
11420 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11421 != !NILP (w->region_showing)))
11422 {
11423 struct buffer *prev = current_buffer;
11424 ptrdiff_t count = SPECPDL_INDEX ();
11425 Lisp_Object frame, new_tool_bar;
11426 int new_n_tool_bar;
11427 struct gcpro gcpro1;
11428
11429 /* Set current_buffer to the buffer of the selected
11430 window of the frame, so that we get the right local
11431 keymaps. */
11432 set_buffer_internal_1 (XBUFFER (w->buffer));
11433
11434 /* Save match data, if we must. */
11435 if (save_match_data)
11436 record_unwind_save_match_data ();
11437
11438 /* Make sure that we don't accidentally use bogus keymaps. */
11439 if (NILP (Voverriding_local_map_menu_flag))
11440 {
11441 specbind (Qoverriding_terminal_local_map, Qnil);
11442 specbind (Qoverriding_local_map, Qnil);
11443 }
11444
11445 GCPRO1 (new_tool_bar);
11446
11447 /* We must temporarily set the selected frame to this frame
11448 before calling tool_bar_items, because the calculation of
11449 the tool-bar keymap uses the selected frame (see
11450 `tool-bar-make-keymap' in tool-bar.el). */
11451 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11452 XSETFRAME (frame, f);
11453 selected_frame = frame;
11454
11455 /* Build desired tool-bar items from keymaps. */
11456 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11457 &new_n_tool_bar);
11458
11459 /* Redisplay the tool-bar if we changed it. */
11460 if (new_n_tool_bar != f->n_tool_bar_items
11461 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11462 {
11463 /* Redisplay that happens asynchronously due to an expose event
11464 may access f->tool_bar_items. Make sure we update both
11465 variables within BLOCK_INPUT so no such event interrupts. */
11466 BLOCK_INPUT;
11467 f->tool_bar_items = new_tool_bar;
11468 f->n_tool_bar_items = new_n_tool_bar;
11469 w->update_mode_line = 1;
11470 UNBLOCK_INPUT;
11471 }
11472
11473 UNGCPRO;
11474
11475 unbind_to (count, Qnil);
11476 set_buffer_internal_1 (prev);
11477 }
11478 }
11479 }
11480
11481
11482 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11483 F's desired tool-bar contents. F->tool_bar_items must have
11484 been set up previously by calling prepare_menu_bars. */
11485
11486 static void
11487 build_desired_tool_bar_string (struct frame *f)
11488 {
11489 int i, size, size_needed;
11490 struct gcpro gcpro1, gcpro2, gcpro3;
11491 Lisp_Object image, plist, props;
11492
11493 image = plist = props = Qnil;
11494 GCPRO3 (image, plist, props);
11495
11496 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11497 Otherwise, make a new string. */
11498
11499 /* The size of the string we might be able to reuse. */
11500 size = (STRINGP (f->desired_tool_bar_string)
11501 ? SCHARS (f->desired_tool_bar_string)
11502 : 0);
11503
11504 /* We need one space in the string for each image. */
11505 size_needed = f->n_tool_bar_items;
11506
11507 /* Reuse f->desired_tool_bar_string, if possible. */
11508 if (size < size_needed || NILP (f->desired_tool_bar_string))
11509 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11510 make_number (' '));
11511 else
11512 {
11513 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11514 Fremove_text_properties (make_number (0), make_number (size),
11515 props, f->desired_tool_bar_string);
11516 }
11517
11518 /* Put a `display' property on the string for the images to display,
11519 put a `menu_item' property on tool-bar items with a value that
11520 is the index of the item in F's tool-bar item vector. */
11521 for (i = 0; i < f->n_tool_bar_items; ++i)
11522 {
11523 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11524
11525 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11526 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11527 int hmargin, vmargin, relief, idx, end;
11528
11529 /* If image is a vector, choose the image according to the
11530 button state. */
11531 image = PROP (TOOL_BAR_ITEM_IMAGES);
11532 if (VECTORP (image))
11533 {
11534 if (enabled_p)
11535 idx = (selected_p
11536 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11537 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11538 else
11539 idx = (selected_p
11540 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11541 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11542
11543 eassert (ASIZE (image) >= idx);
11544 image = AREF (image, idx);
11545 }
11546 else
11547 idx = -1;
11548
11549 /* Ignore invalid image specifications. */
11550 if (!valid_image_p (image))
11551 continue;
11552
11553 /* Display the tool-bar button pressed, or depressed. */
11554 plist = Fcopy_sequence (XCDR (image));
11555
11556 /* Compute margin and relief to draw. */
11557 relief = (tool_bar_button_relief >= 0
11558 ? tool_bar_button_relief
11559 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11560 hmargin = vmargin = relief;
11561
11562 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11563 INT_MAX - max (hmargin, vmargin)))
11564 {
11565 hmargin += XFASTINT (Vtool_bar_button_margin);
11566 vmargin += XFASTINT (Vtool_bar_button_margin);
11567 }
11568 else if (CONSP (Vtool_bar_button_margin))
11569 {
11570 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11571 INT_MAX - hmargin))
11572 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11573
11574 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11575 INT_MAX - vmargin))
11576 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11577 }
11578
11579 if (auto_raise_tool_bar_buttons_p)
11580 {
11581 /* Add a `:relief' property to the image spec if the item is
11582 selected. */
11583 if (selected_p)
11584 {
11585 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11586 hmargin -= relief;
11587 vmargin -= relief;
11588 }
11589 }
11590 else
11591 {
11592 /* If image is selected, display it pressed, i.e. with a
11593 negative relief. If it's not selected, display it with a
11594 raised relief. */
11595 plist = Fplist_put (plist, QCrelief,
11596 (selected_p
11597 ? make_number (-relief)
11598 : make_number (relief)));
11599 hmargin -= relief;
11600 vmargin -= relief;
11601 }
11602
11603 /* Put a margin around the image. */
11604 if (hmargin || vmargin)
11605 {
11606 if (hmargin == vmargin)
11607 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11608 else
11609 plist = Fplist_put (plist, QCmargin,
11610 Fcons (make_number (hmargin),
11611 make_number (vmargin)));
11612 }
11613
11614 /* If button is not enabled, and we don't have special images
11615 for the disabled state, make the image appear disabled by
11616 applying an appropriate algorithm to it. */
11617 if (!enabled_p && idx < 0)
11618 plist = Fplist_put (plist, QCconversion, Qdisabled);
11619
11620 /* Put a `display' text property on the string for the image to
11621 display. Put a `menu-item' property on the string that gives
11622 the start of this item's properties in the tool-bar items
11623 vector. */
11624 image = Fcons (Qimage, plist);
11625 props = list4 (Qdisplay, image,
11626 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11627
11628 /* Let the last image hide all remaining spaces in the tool bar
11629 string. The string can be longer than needed when we reuse a
11630 previous string. */
11631 if (i + 1 == f->n_tool_bar_items)
11632 end = SCHARS (f->desired_tool_bar_string);
11633 else
11634 end = i + 1;
11635 Fadd_text_properties (make_number (i), make_number (end),
11636 props, f->desired_tool_bar_string);
11637 #undef PROP
11638 }
11639
11640 UNGCPRO;
11641 }
11642
11643
11644 /* Display one line of the tool-bar of frame IT->f.
11645
11646 HEIGHT specifies the desired height of the tool-bar line.
11647 If the actual height of the glyph row is less than HEIGHT, the
11648 row's height is increased to HEIGHT, and the icons are centered
11649 vertically in the new height.
11650
11651 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11652 count a final empty row in case the tool-bar width exactly matches
11653 the window width.
11654 */
11655
11656 static void
11657 display_tool_bar_line (struct it *it, int height)
11658 {
11659 struct glyph_row *row = it->glyph_row;
11660 int max_x = it->last_visible_x;
11661 struct glyph *last;
11662
11663 prepare_desired_row (row);
11664 row->y = it->current_y;
11665
11666 /* Note that this isn't made use of if the face hasn't a box,
11667 so there's no need to check the face here. */
11668 it->start_of_box_run_p = 1;
11669
11670 while (it->current_x < max_x)
11671 {
11672 int x, n_glyphs_before, i, nglyphs;
11673 struct it it_before;
11674
11675 /* Get the next display element. */
11676 if (!get_next_display_element (it))
11677 {
11678 /* Don't count empty row if we are counting needed tool-bar lines. */
11679 if (height < 0 && !it->hpos)
11680 return;
11681 break;
11682 }
11683
11684 /* Produce glyphs. */
11685 n_glyphs_before = row->used[TEXT_AREA];
11686 it_before = *it;
11687
11688 PRODUCE_GLYPHS (it);
11689
11690 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11691 i = 0;
11692 x = it_before.current_x;
11693 while (i < nglyphs)
11694 {
11695 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11696
11697 if (x + glyph->pixel_width > max_x)
11698 {
11699 /* Glyph doesn't fit on line. Backtrack. */
11700 row->used[TEXT_AREA] = n_glyphs_before;
11701 *it = it_before;
11702 /* If this is the only glyph on this line, it will never fit on the
11703 tool-bar, so skip it. But ensure there is at least one glyph,
11704 so we don't accidentally disable the tool-bar. */
11705 if (n_glyphs_before == 0
11706 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11707 break;
11708 goto out;
11709 }
11710
11711 ++it->hpos;
11712 x += glyph->pixel_width;
11713 ++i;
11714 }
11715
11716 /* Stop at line end. */
11717 if (ITERATOR_AT_END_OF_LINE_P (it))
11718 break;
11719
11720 set_iterator_to_next (it, 1);
11721 }
11722
11723 out:;
11724
11725 row->displays_text_p = row->used[TEXT_AREA] != 0;
11726
11727 /* Use default face for the border below the tool bar.
11728
11729 FIXME: When auto-resize-tool-bars is grow-only, there is
11730 no additional border below the possibly empty tool-bar lines.
11731 So to make the extra empty lines look "normal", we have to
11732 use the tool-bar face for the border too. */
11733 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11734 it->face_id = DEFAULT_FACE_ID;
11735
11736 extend_face_to_end_of_line (it);
11737 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11738 last->right_box_line_p = 1;
11739 if (last == row->glyphs[TEXT_AREA])
11740 last->left_box_line_p = 1;
11741
11742 /* Make line the desired height and center it vertically. */
11743 if ((height -= it->max_ascent + it->max_descent) > 0)
11744 {
11745 /* Don't add more than one line height. */
11746 height %= FRAME_LINE_HEIGHT (it->f);
11747 it->max_ascent += height / 2;
11748 it->max_descent += (height + 1) / 2;
11749 }
11750
11751 compute_line_metrics (it);
11752
11753 /* If line is empty, make it occupy the rest of the tool-bar. */
11754 if (!row->displays_text_p)
11755 {
11756 row->height = row->phys_height = it->last_visible_y - row->y;
11757 row->visible_height = row->height;
11758 row->ascent = row->phys_ascent = 0;
11759 row->extra_line_spacing = 0;
11760 }
11761
11762 row->full_width_p = 1;
11763 row->continued_p = 0;
11764 row->truncated_on_left_p = 0;
11765 row->truncated_on_right_p = 0;
11766
11767 it->current_x = it->hpos = 0;
11768 it->current_y += row->height;
11769 ++it->vpos;
11770 ++it->glyph_row;
11771 }
11772
11773
11774 /* Max tool-bar height. */
11775
11776 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11777 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11778
11779 /* Value is the number of screen lines needed to make all tool-bar
11780 items of frame F visible. The number of actual rows needed is
11781 returned in *N_ROWS if non-NULL. */
11782
11783 static int
11784 tool_bar_lines_needed (struct frame *f, int *n_rows)
11785 {
11786 struct window *w = XWINDOW (f->tool_bar_window);
11787 struct it it;
11788 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11789 the desired matrix, so use (unused) mode-line row as temporary row to
11790 avoid destroying the first tool-bar row. */
11791 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11792
11793 /* Initialize an iterator for iteration over
11794 F->desired_tool_bar_string in the tool-bar window of frame F. */
11795 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11796 it.first_visible_x = 0;
11797 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11798 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11799 it.paragraph_embedding = L2R;
11800
11801 while (!ITERATOR_AT_END_P (&it))
11802 {
11803 clear_glyph_row (temp_row);
11804 it.glyph_row = temp_row;
11805 display_tool_bar_line (&it, -1);
11806 }
11807 clear_glyph_row (temp_row);
11808
11809 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11810 if (n_rows)
11811 *n_rows = it.vpos > 0 ? it.vpos : -1;
11812
11813 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11814 }
11815
11816
11817 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11818 0, 1, 0,
11819 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11820 (Lisp_Object frame)
11821 {
11822 struct frame *f;
11823 struct window *w;
11824 int nlines = 0;
11825
11826 if (NILP (frame))
11827 frame = selected_frame;
11828 else
11829 CHECK_FRAME (frame);
11830 f = XFRAME (frame);
11831
11832 if (WINDOWP (f->tool_bar_window)
11833 && (w = XWINDOW (f->tool_bar_window),
11834 WINDOW_TOTAL_LINES (w) > 0))
11835 {
11836 update_tool_bar (f, 1);
11837 if (f->n_tool_bar_items)
11838 {
11839 build_desired_tool_bar_string (f);
11840 nlines = tool_bar_lines_needed (f, NULL);
11841 }
11842 }
11843
11844 return make_number (nlines);
11845 }
11846
11847
11848 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11849 height should be changed. */
11850
11851 static int
11852 redisplay_tool_bar (struct frame *f)
11853 {
11854 struct window *w;
11855 struct it it;
11856 struct glyph_row *row;
11857
11858 #if defined (USE_GTK) || defined (HAVE_NS)
11859 if (FRAME_EXTERNAL_TOOL_BAR (f))
11860 update_frame_tool_bar (f);
11861 return 0;
11862 #endif
11863
11864 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11865 do anything. This means you must start with tool-bar-lines
11866 non-zero to get the auto-sizing effect. Or in other words, you
11867 can turn off tool-bars by specifying tool-bar-lines zero. */
11868 if (!WINDOWP (f->tool_bar_window)
11869 || (w = XWINDOW (f->tool_bar_window),
11870 WINDOW_TOTAL_LINES (w) == 0))
11871 return 0;
11872
11873 /* Set up an iterator for the tool-bar window. */
11874 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11875 it.first_visible_x = 0;
11876 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11877 row = it.glyph_row;
11878
11879 /* Build a string that represents the contents of the tool-bar. */
11880 build_desired_tool_bar_string (f);
11881 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11882 /* FIXME: This should be controlled by a user option. But it
11883 doesn't make sense to have an R2L tool bar if the menu bar cannot
11884 be drawn also R2L, and making the menu bar R2L is tricky due
11885 toolkit-specific code that implements it. If an R2L tool bar is
11886 ever supported, display_tool_bar_line should also be augmented to
11887 call unproduce_glyphs like display_line and display_string
11888 do. */
11889 it.paragraph_embedding = L2R;
11890
11891 if (f->n_tool_bar_rows == 0)
11892 {
11893 int nlines;
11894
11895 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11896 nlines != WINDOW_TOTAL_LINES (w)))
11897 {
11898 Lisp_Object frame;
11899 int old_height = WINDOW_TOTAL_LINES (w);
11900
11901 XSETFRAME (frame, f);
11902 Fmodify_frame_parameters (frame,
11903 Fcons (Fcons (Qtool_bar_lines,
11904 make_number (nlines)),
11905 Qnil));
11906 if (WINDOW_TOTAL_LINES (w) != old_height)
11907 {
11908 clear_glyph_matrix (w->desired_matrix);
11909 fonts_changed_p = 1;
11910 return 1;
11911 }
11912 }
11913 }
11914
11915 /* Display as many lines as needed to display all tool-bar items. */
11916
11917 if (f->n_tool_bar_rows > 0)
11918 {
11919 int border, rows, height, extra;
11920
11921 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11922 border = XINT (Vtool_bar_border);
11923 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11924 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11925 else if (EQ (Vtool_bar_border, Qborder_width))
11926 border = f->border_width;
11927 else
11928 border = 0;
11929 if (border < 0)
11930 border = 0;
11931
11932 rows = f->n_tool_bar_rows;
11933 height = max (1, (it.last_visible_y - border) / rows);
11934 extra = it.last_visible_y - border - height * rows;
11935
11936 while (it.current_y < it.last_visible_y)
11937 {
11938 int h = 0;
11939 if (extra > 0 && rows-- > 0)
11940 {
11941 h = (extra + rows - 1) / rows;
11942 extra -= h;
11943 }
11944 display_tool_bar_line (&it, height + h);
11945 }
11946 }
11947 else
11948 {
11949 while (it.current_y < it.last_visible_y)
11950 display_tool_bar_line (&it, 0);
11951 }
11952
11953 /* It doesn't make much sense to try scrolling in the tool-bar
11954 window, so don't do it. */
11955 w->desired_matrix->no_scrolling_p = 1;
11956 w->must_be_updated_p = 1;
11957
11958 if (!NILP (Vauto_resize_tool_bars))
11959 {
11960 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11961 int change_height_p = 0;
11962
11963 /* If we couldn't display everything, change the tool-bar's
11964 height if there is room for more. */
11965 if (IT_STRING_CHARPOS (it) < it.end_charpos
11966 && it.current_y < max_tool_bar_height)
11967 change_height_p = 1;
11968
11969 row = it.glyph_row - 1;
11970
11971 /* If there are blank lines at the end, except for a partially
11972 visible blank line at the end that is smaller than
11973 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11974 if (!row->displays_text_p
11975 && row->height >= FRAME_LINE_HEIGHT (f))
11976 change_height_p = 1;
11977
11978 /* If row displays tool-bar items, but is partially visible,
11979 change the tool-bar's height. */
11980 if (row->displays_text_p
11981 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11982 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11983 change_height_p = 1;
11984
11985 /* Resize windows as needed by changing the `tool-bar-lines'
11986 frame parameter. */
11987 if (change_height_p)
11988 {
11989 Lisp_Object frame;
11990 int old_height = WINDOW_TOTAL_LINES (w);
11991 int nrows;
11992 int nlines = tool_bar_lines_needed (f, &nrows);
11993
11994 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11995 && !f->minimize_tool_bar_window_p)
11996 ? (nlines > old_height)
11997 : (nlines != old_height));
11998 f->minimize_tool_bar_window_p = 0;
11999
12000 if (change_height_p)
12001 {
12002 XSETFRAME (frame, f);
12003 Fmodify_frame_parameters (frame,
12004 Fcons (Fcons (Qtool_bar_lines,
12005 make_number (nlines)),
12006 Qnil));
12007 if (WINDOW_TOTAL_LINES (w) != old_height)
12008 {
12009 clear_glyph_matrix (w->desired_matrix);
12010 f->n_tool_bar_rows = nrows;
12011 fonts_changed_p = 1;
12012 return 1;
12013 }
12014 }
12015 }
12016 }
12017
12018 f->minimize_tool_bar_window_p = 0;
12019 return 0;
12020 }
12021
12022
12023 /* Get information about the tool-bar item which is displayed in GLYPH
12024 on frame F. Return in *PROP_IDX the index where tool-bar item
12025 properties start in F->tool_bar_items. Value is zero if
12026 GLYPH doesn't display a tool-bar item. */
12027
12028 static int
12029 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12030 {
12031 Lisp_Object prop;
12032 int success_p;
12033 int charpos;
12034
12035 /* This function can be called asynchronously, which means we must
12036 exclude any possibility that Fget_text_property signals an
12037 error. */
12038 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12039 charpos = max (0, charpos);
12040
12041 /* Get the text property `menu-item' at pos. The value of that
12042 property is the start index of this item's properties in
12043 F->tool_bar_items. */
12044 prop = Fget_text_property (make_number (charpos),
12045 Qmenu_item, f->current_tool_bar_string);
12046 if (INTEGERP (prop))
12047 {
12048 *prop_idx = XINT (prop);
12049 success_p = 1;
12050 }
12051 else
12052 success_p = 0;
12053
12054 return success_p;
12055 }
12056
12057 \f
12058 /* Get information about the tool-bar item at position X/Y on frame F.
12059 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12060 the current matrix of the tool-bar window of F, or NULL if not
12061 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12062 item in F->tool_bar_items. Value is
12063
12064 -1 if X/Y is not on a tool-bar item
12065 0 if X/Y is on the same item that was highlighted before.
12066 1 otherwise. */
12067
12068 static int
12069 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12070 int *hpos, int *vpos, int *prop_idx)
12071 {
12072 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12073 struct window *w = XWINDOW (f->tool_bar_window);
12074 int area;
12075
12076 /* Find the glyph under X/Y. */
12077 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12078 if (*glyph == NULL)
12079 return -1;
12080
12081 /* Get the start of this tool-bar item's properties in
12082 f->tool_bar_items. */
12083 if (!tool_bar_item_info (f, *glyph, prop_idx))
12084 return -1;
12085
12086 /* Is mouse on the highlighted item? */
12087 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12088 && *vpos >= hlinfo->mouse_face_beg_row
12089 && *vpos <= hlinfo->mouse_face_end_row
12090 && (*vpos > hlinfo->mouse_face_beg_row
12091 || *hpos >= hlinfo->mouse_face_beg_col)
12092 && (*vpos < hlinfo->mouse_face_end_row
12093 || *hpos < hlinfo->mouse_face_end_col
12094 || hlinfo->mouse_face_past_end))
12095 return 0;
12096
12097 return 1;
12098 }
12099
12100
12101 /* EXPORT:
12102 Handle mouse button event on the tool-bar of frame F, at
12103 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12104 0 for button release. MODIFIERS is event modifiers for button
12105 release. */
12106
12107 void
12108 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12109 int modifiers)
12110 {
12111 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12112 struct window *w = XWINDOW (f->tool_bar_window);
12113 int hpos, vpos, prop_idx;
12114 struct glyph *glyph;
12115 Lisp_Object enabled_p;
12116
12117 /* If not on the highlighted tool-bar item, return. */
12118 frame_to_window_pixel_xy (w, &x, &y);
12119 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12120 return;
12121
12122 /* If item is disabled, do nothing. */
12123 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12124 if (NILP (enabled_p))
12125 return;
12126
12127 if (down_p)
12128 {
12129 /* Show item in pressed state. */
12130 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12131 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12132 last_tool_bar_item = prop_idx;
12133 }
12134 else
12135 {
12136 Lisp_Object key, frame;
12137 struct input_event event;
12138 EVENT_INIT (event);
12139
12140 /* Show item in released state. */
12141 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12142 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12143
12144 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12145
12146 XSETFRAME (frame, f);
12147 event.kind = TOOL_BAR_EVENT;
12148 event.frame_or_window = frame;
12149 event.arg = frame;
12150 kbd_buffer_store_event (&event);
12151
12152 event.kind = TOOL_BAR_EVENT;
12153 event.frame_or_window = frame;
12154 event.arg = key;
12155 event.modifiers = modifiers;
12156 kbd_buffer_store_event (&event);
12157 last_tool_bar_item = -1;
12158 }
12159 }
12160
12161
12162 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12163 tool-bar window-relative coordinates X/Y. Called from
12164 note_mouse_highlight. */
12165
12166 static void
12167 note_tool_bar_highlight (struct frame *f, int x, int y)
12168 {
12169 Lisp_Object window = f->tool_bar_window;
12170 struct window *w = XWINDOW (window);
12171 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12172 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12173 int hpos, vpos;
12174 struct glyph *glyph;
12175 struct glyph_row *row;
12176 int i;
12177 Lisp_Object enabled_p;
12178 int prop_idx;
12179 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12180 int mouse_down_p, rc;
12181
12182 /* Function note_mouse_highlight is called with negative X/Y
12183 values when mouse moves outside of the frame. */
12184 if (x <= 0 || y <= 0)
12185 {
12186 clear_mouse_face (hlinfo);
12187 return;
12188 }
12189
12190 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12191 if (rc < 0)
12192 {
12193 /* Not on tool-bar item. */
12194 clear_mouse_face (hlinfo);
12195 return;
12196 }
12197 else if (rc == 0)
12198 /* On same tool-bar item as before. */
12199 goto set_help_echo;
12200
12201 clear_mouse_face (hlinfo);
12202
12203 /* Mouse is down, but on different tool-bar item? */
12204 mouse_down_p = (dpyinfo->grabbed
12205 && f == last_mouse_frame
12206 && FRAME_LIVE_P (f));
12207 if (mouse_down_p
12208 && last_tool_bar_item != prop_idx)
12209 return;
12210
12211 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12212 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12213
12214 /* If tool-bar item is not enabled, don't highlight it. */
12215 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12216 if (!NILP (enabled_p))
12217 {
12218 /* Compute the x-position of the glyph. In front and past the
12219 image is a space. We include this in the highlighted area. */
12220 row = MATRIX_ROW (w->current_matrix, vpos);
12221 for (i = x = 0; i < hpos; ++i)
12222 x += row->glyphs[TEXT_AREA][i].pixel_width;
12223
12224 /* Record this as the current active region. */
12225 hlinfo->mouse_face_beg_col = hpos;
12226 hlinfo->mouse_face_beg_row = vpos;
12227 hlinfo->mouse_face_beg_x = x;
12228 hlinfo->mouse_face_beg_y = row->y;
12229 hlinfo->mouse_face_past_end = 0;
12230
12231 hlinfo->mouse_face_end_col = hpos + 1;
12232 hlinfo->mouse_face_end_row = vpos;
12233 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12234 hlinfo->mouse_face_end_y = row->y;
12235 hlinfo->mouse_face_window = window;
12236 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12237
12238 /* Display it as active. */
12239 show_mouse_face (hlinfo, draw);
12240 hlinfo->mouse_face_image_state = draw;
12241 }
12242
12243 set_help_echo:
12244
12245 /* Set help_echo_string to a help string to display for this tool-bar item.
12246 XTread_socket does the rest. */
12247 help_echo_object = help_echo_window = Qnil;
12248 help_echo_pos = -1;
12249 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12250 if (NILP (help_echo_string))
12251 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12252 }
12253
12254 #endif /* HAVE_WINDOW_SYSTEM */
12255
12256
12257 \f
12258 /************************************************************************
12259 Horizontal scrolling
12260 ************************************************************************/
12261
12262 static int hscroll_window_tree (Lisp_Object);
12263 static int hscroll_windows (Lisp_Object);
12264
12265 /* For all leaf windows in the window tree rooted at WINDOW, set their
12266 hscroll value so that PT is (i) visible in the window, and (ii) so
12267 that it is not within a certain margin at the window's left and
12268 right border. Value is non-zero if any window's hscroll has been
12269 changed. */
12270
12271 static int
12272 hscroll_window_tree (Lisp_Object window)
12273 {
12274 int hscrolled_p = 0;
12275 int hscroll_relative_p = FLOATP (Vhscroll_step);
12276 int hscroll_step_abs = 0;
12277 double hscroll_step_rel = 0;
12278
12279 if (hscroll_relative_p)
12280 {
12281 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12282 if (hscroll_step_rel < 0)
12283 {
12284 hscroll_relative_p = 0;
12285 hscroll_step_abs = 0;
12286 }
12287 }
12288 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12289 {
12290 hscroll_step_abs = XINT (Vhscroll_step);
12291 if (hscroll_step_abs < 0)
12292 hscroll_step_abs = 0;
12293 }
12294 else
12295 hscroll_step_abs = 0;
12296
12297 while (WINDOWP (window))
12298 {
12299 struct window *w = XWINDOW (window);
12300
12301 if (WINDOWP (w->hchild))
12302 hscrolled_p |= hscroll_window_tree (w->hchild);
12303 else if (WINDOWP (w->vchild))
12304 hscrolled_p |= hscroll_window_tree (w->vchild);
12305 else if (w->cursor.vpos >= 0)
12306 {
12307 int h_margin;
12308 int text_area_width;
12309 struct glyph_row *current_cursor_row
12310 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12311 struct glyph_row *desired_cursor_row
12312 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12313 struct glyph_row *cursor_row
12314 = (desired_cursor_row->enabled_p
12315 ? desired_cursor_row
12316 : current_cursor_row);
12317 int row_r2l_p = cursor_row->reversed_p;
12318
12319 text_area_width = window_box_width (w, TEXT_AREA);
12320
12321 /* Scroll when cursor is inside this scroll margin. */
12322 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12323
12324 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12325 /* For left-to-right rows, hscroll when cursor is either
12326 (i) inside the right hscroll margin, or (ii) if it is
12327 inside the left margin and the window is already
12328 hscrolled. */
12329 && ((!row_r2l_p
12330 && ((w->hscroll
12331 && w->cursor.x <= h_margin)
12332 || (cursor_row->enabled_p
12333 && cursor_row->truncated_on_right_p
12334 && (w->cursor.x >= text_area_width - h_margin))))
12335 /* For right-to-left rows, the logic is similar,
12336 except that rules for scrolling to left and right
12337 are reversed. E.g., if cursor.x <= h_margin, we
12338 need to hscroll "to the right" unconditionally,
12339 and that will scroll the screen to the left so as
12340 to reveal the next portion of the row. */
12341 || (row_r2l_p
12342 && ((cursor_row->enabled_p
12343 /* FIXME: It is confusing to set the
12344 truncated_on_right_p flag when R2L rows
12345 are actually truncated on the left. */
12346 && cursor_row->truncated_on_right_p
12347 && w->cursor.x <= h_margin)
12348 || (w->hscroll
12349 && (w->cursor.x >= text_area_width - h_margin))))))
12350 {
12351 struct it it;
12352 ptrdiff_t hscroll;
12353 struct buffer *saved_current_buffer;
12354 ptrdiff_t pt;
12355 int wanted_x;
12356
12357 /* Find point in a display of infinite width. */
12358 saved_current_buffer = current_buffer;
12359 current_buffer = XBUFFER (w->buffer);
12360
12361 if (w == XWINDOW (selected_window))
12362 pt = PT;
12363 else
12364 {
12365 pt = marker_position (w->pointm);
12366 pt = max (BEGV, pt);
12367 pt = min (ZV, pt);
12368 }
12369
12370 /* Move iterator to pt starting at cursor_row->start in
12371 a line with infinite width. */
12372 init_to_row_start (&it, w, cursor_row);
12373 it.last_visible_x = INFINITY;
12374 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12375 current_buffer = saved_current_buffer;
12376
12377 /* Position cursor in window. */
12378 if (!hscroll_relative_p && hscroll_step_abs == 0)
12379 hscroll = max (0, (it.current_x
12380 - (ITERATOR_AT_END_OF_LINE_P (&it)
12381 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12382 : (text_area_width / 2))))
12383 / FRAME_COLUMN_WIDTH (it.f);
12384 else if ((!row_r2l_p
12385 && w->cursor.x >= text_area_width - h_margin)
12386 || (row_r2l_p && w->cursor.x <= h_margin))
12387 {
12388 if (hscroll_relative_p)
12389 wanted_x = text_area_width * (1 - hscroll_step_rel)
12390 - h_margin;
12391 else
12392 wanted_x = text_area_width
12393 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12394 - h_margin;
12395 hscroll
12396 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12397 }
12398 else
12399 {
12400 if (hscroll_relative_p)
12401 wanted_x = text_area_width * hscroll_step_rel
12402 + h_margin;
12403 else
12404 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12405 + h_margin;
12406 hscroll
12407 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12408 }
12409 hscroll = max (hscroll, w->min_hscroll);
12410
12411 /* Don't prevent redisplay optimizations if hscroll
12412 hasn't changed, as it will unnecessarily slow down
12413 redisplay. */
12414 if (w->hscroll != hscroll)
12415 {
12416 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12417 w->hscroll = hscroll;
12418 hscrolled_p = 1;
12419 }
12420 }
12421 }
12422
12423 window = w->next;
12424 }
12425
12426 /* Value is non-zero if hscroll of any leaf window has been changed. */
12427 return hscrolled_p;
12428 }
12429
12430
12431 /* Set hscroll so that cursor is visible and not inside horizontal
12432 scroll margins for all windows in the tree rooted at WINDOW. See
12433 also hscroll_window_tree above. Value is non-zero if any window's
12434 hscroll has been changed. If it has, desired matrices on the frame
12435 of WINDOW are cleared. */
12436
12437 static int
12438 hscroll_windows (Lisp_Object window)
12439 {
12440 int hscrolled_p = hscroll_window_tree (window);
12441 if (hscrolled_p)
12442 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12443 return hscrolled_p;
12444 }
12445
12446
12447 \f
12448 /************************************************************************
12449 Redisplay
12450 ************************************************************************/
12451
12452 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12453 to a non-zero value. This is sometimes handy to have in a debugger
12454 session. */
12455
12456 #ifdef GLYPH_DEBUG
12457
12458 /* First and last unchanged row for try_window_id. */
12459
12460 static int debug_first_unchanged_at_end_vpos;
12461 static int debug_last_unchanged_at_beg_vpos;
12462
12463 /* Delta vpos and y. */
12464
12465 static int debug_dvpos, debug_dy;
12466
12467 /* Delta in characters and bytes for try_window_id. */
12468
12469 static ptrdiff_t debug_delta, debug_delta_bytes;
12470
12471 /* Values of window_end_pos and window_end_vpos at the end of
12472 try_window_id. */
12473
12474 static ptrdiff_t debug_end_vpos;
12475
12476 /* Append a string to W->desired_matrix->method. FMT is a printf
12477 format string. If trace_redisplay_p is non-zero also printf the
12478 resulting string to stderr. */
12479
12480 static void debug_method_add (struct window *, char const *, ...)
12481 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12482
12483 static void
12484 debug_method_add (struct window *w, char const *fmt, ...)
12485 {
12486 char buffer[512];
12487 char *method = w->desired_matrix->method;
12488 int len = strlen (method);
12489 int size = sizeof w->desired_matrix->method;
12490 int remaining = size - len - 1;
12491 va_list ap;
12492
12493 va_start (ap, fmt);
12494 vsprintf (buffer, fmt, ap);
12495 va_end (ap);
12496 if (len && remaining)
12497 {
12498 method[len] = '|';
12499 --remaining, ++len;
12500 }
12501
12502 strncpy (method + len, buffer, remaining);
12503
12504 if (trace_redisplay_p)
12505 fprintf (stderr, "%p (%s): %s\n",
12506 w,
12507 ((BUFFERP (w->buffer)
12508 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12509 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12510 : "no buffer"),
12511 buffer);
12512 }
12513
12514 #endif /* GLYPH_DEBUG */
12515
12516
12517 /* Value is non-zero if all changes in window W, which displays
12518 current_buffer, are in the text between START and END. START is a
12519 buffer position, END is given as a distance from Z. Used in
12520 redisplay_internal for display optimization. */
12521
12522 static inline int
12523 text_outside_line_unchanged_p (struct window *w,
12524 ptrdiff_t start, ptrdiff_t end)
12525 {
12526 int unchanged_p = 1;
12527
12528 /* If text or overlays have changed, see where. */
12529 if (w->last_modified < MODIFF
12530 || w->last_overlay_modified < OVERLAY_MODIFF)
12531 {
12532 /* Gap in the line? */
12533 if (GPT < start || Z - GPT < end)
12534 unchanged_p = 0;
12535
12536 /* Changes start in front of the line, or end after it? */
12537 if (unchanged_p
12538 && (BEG_UNCHANGED < start - 1
12539 || END_UNCHANGED < end))
12540 unchanged_p = 0;
12541
12542 /* If selective display, can't optimize if changes start at the
12543 beginning of the line. */
12544 if (unchanged_p
12545 && INTEGERP (BVAR (current_buffer, selective_display))
12546 && XINT (BVAR (current_buffer, selective_display)) > 0
12547 && (BEG_UNCHANGED < start || GPT <= start))
12548 unchanged_p = 0;
12549
12550 /* If there are overlays at the start or end of the line, these
12551 may have overlay strings with newlines in them. A change at
12552 START, for instance, may actually concern the display of such
12553 overlay strings as well, and they are displayed on different
12554 lines. So, quickly rule out this case. (For the future, it
12555 might be desirable to implement something more telling than
12556 just BEG/END_UNCHANGED.) */
12557 if (unchanged_p)
12558 {
12559 if (BEG + BEG_UNCHANGED == start
12560 && overlay_touches_p (start))
12561 unchanged_p = 0;
12562 if (END_UNCHANGED == end
12563 && overlay_touches_p (Z - end))
12564 unchanged_p = 0;
12565 }
12566
12567 /* Under bidi reordering, adding or deleting a character in the
12568 beginning of a paragraph, before the first strong directional
12569 character, can change the base direction of the paragraph (unless
12570 the buffer specifies a fixed paragraph direction), which will
12571 require to redisplay the whole paragraph. It might be worthwhile
12572 to find the paragraph limits and widen the range of redisplayed
12573 lines to that, but for now just give up this optimization. */
12574 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12575 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12576 unchanged_p = 0;
12577 }
12578
12579 return unchanged_p;
12580 }
12581
12582
12583 /* Do a frame update, taking possible shortcuts into account. This is
12584 the main external entry point for redisplay.
12585
12586 If the last redisplay displayed an echo area message and that message
12587 is no longer requested, we clear the echo area or bring back the
12588 mini-buffer if that is in use. */
12589
12590 void
12591 redisplay (void)
12592 {
12593 redisplay_internal ();
12594 }
12595
12596
12597 static Lisp_Object
12598 overlay_arrow_string_or_property (Lisp_Object var)
12599 {
12600 Lisp_Object val;
12601
12602 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12603 return val;
12604
12605 return Voverlay_arrow_string;
12606 }
12607
12608 /* Return 1 if there are any overlay-arrows in current_buffer. */
12609 static int
12610 overlay_arrow_in_current_buffer_p (void)
12611 {
12612 Lisp_Object vlist;
12613
12614 for (vlist = Voverlay_arrow_variable_list;
12615 CONSP (vlist);
12616 vlist = XCDR (vlist))
12617 {
12618 Lisp_Object var = XCAR (vlist);
12619 Lisp_Object val;
12620
12621 if (!SYMBOLP (var))
12622 continue;
12623 val = find_symbol_value (var);
12624 if (MARKERP (val)
12625 && current_buffer == XMARKER (val)->buffer)
12626 return 1;
12627 }
12628 return 0;
12629 }
12630
12631
12632 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12633 has changed. */
12634
12635 static int
12636 overlay_arrows_changed_p (void)
12637 {
12638 Lisp_Object vlist;
12639
12640 for (vlist = Voverlay_arrow_variable_list;
12641 CONSP (vlist);
12642 vlist = XCDR (vlist))
12643 {
12644 Lisp_Object var = XCAR (vlist);
12645 Lisp_Object val, pstr;
12646
12647 if (!SYMBOLP (var))
12648 continue;
12649 val = find_symbol_value (var);
12650 if (!MARKERP (val))
12651 continue;
12652 if (! EQ (COERCE_MARKER (val),
12653 Fget (var, Qlast_arrow_position))
12654 || ! (pstr = overlay_arrow_string_or_property (var),
12655 EQ (pstr, Fget (var, Qlast_arrow_string))))
12656 return 1;
12657 }
12658 return 0;
12659 }
12660
12661 /* Mark overlay arrows to be updated on next redisplay. */
12662
12663 static void
12664 update_overlay_arrows (int up_to_date)
12665 {
12666 Lisp_Object vlist;
12667
12668 for (vlist = Voverlay_arrow_variable_list;
12669 CONSP (vlist);
12670 vlist = XCDR (vlist))
12671 {
12672 Lisp_Object var = XCAR (vlist);
12673
12674 if (!SYMBOLP (var))
12675 continue;
12676
12677 if (up_to_date > 0)
12678 {
12679 Lisp_Object val = find_symbol_value (var);
12680 Fput (var, Qlast_arrow_position,
12681 COERCE_MARKER (val));
12682 Fput (var, Qlast_arrow_string,
12683 overlay_arrow_string_or_property (var));
12684 }
12685 else if (up_to_date < 0
12686 || !NILP (Fget (var, Qlast_arrow_position)))
12687 {
12688 Fput (var, Qlast_arrow_position, Qt);
12689 Fput (var, Qlast_arrow_string, Qt);
12690 }
12691 }
12692 }
12693
12694
12695 /* Return overlay arrow string to display at row.
12696 Return integer (bitmap number) for arrow bitmap in left fringe.
12697 Return nil if no overlay arrow. */
12698
12699 static Lisp_Object
12700 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12701 {
12702 Lisp_Object vlist;
12703
12704 for (vlist = Voverlay_arrow_variable_list;
12705 CONSP (vlist);
12706 vlist = XCDR (vlist))
12707 {
12708 Lisp_Object var = XCAR (vlist);
12709 Lisp_Object val;
12710
12711 if (!SYMBOLP (var))
12712 continue;
12713
12714 val = find_symbol_value (var);
12715
12716 if (MARKERP (val)
12717 && current_buffer == XMARKER (val)->buffer
12718 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12719 {
12720 if (FRAME_WINDOW_P (it->f)
12721 /* FIXME: if ROW->reversed_p is set, this should test
12722 the right fringe, not the left one. */
12723 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12724 {
12725 #ifdef HAVE_WINDOW_SYSTEM
12726 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12727 {
12728 int fringe_bitmap;
12729 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12730 return make_number (fringe_bitmap);
12731 }
12732 #endif
12733 return make_number (-1); /* Use default arrow bitmap */
12734 }
12735 return overlay_arrow_string_or_property (var);
12736 }
12737 }
12738
12739 return Qnil;
12740 }
12741
12742 /* Return 1 if point moved out of or into a composition. Otherwise
12743 return 0. PREV_BUF and PREV_PT are the last point buffer and
12744 position. BUF and PT are the current point buffer and position. */
12745
12746 static int
12747 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12748 struct buffer *buf, ptrdiff_t pt)
12749 {
12750 ptrdiff_t start, end;
12751 Lisp_Object prop;
12752 Lisp_Object buffer;
12753
12754 XSETBUFFER (buffer, buf);
12755 /* Check a composition at the last point if point moved within the
12756 same buffer. */
12757 if (prev_buf == buf)
12758 {
12759 if (prev_pt == pt)
12760 /* Point didn't move. */
12761 return 0;
12762
12763 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12764 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12765 && COMPOSITION_VALID_P (start, end, prop)
12766 && start < prev_pt && end > prev_pt)
12767 /* The last point was within the composition. Return 1 iff
12768 point moved out of the composition. */
12769 return (pt <= start || pt >= end);
12770 }
12771
12772 /* Check a composition at the current point. */
12773 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12774 && find_composition (pt, -1, &start, &end, &prop, buffer)
12775 && COMPOSITION_VALID_P (start, end, prop)
12776 && start < pt && end > pt);
12777 }
12778
12779
12780 /* Reconsider the setting of B->clip_changed which is displayed
12781 in window W. */
12782
12783 static inline void
12784 reconsider_clip_changes (struct window *w, struct buffer *b)
12785 {
12786 if (b->clip_changed
12787 && !NILP (w->window_end_valid)
12788 && w->current_matrix->buffer == b
12789 && w->current_matrix->zv == BUF_ZV (b)
12790 && w->current_matrix->begv == BUF_BEGV (b))
12791 b->clip_changed = 0;
12792
12793 /* If display wasn't paused, and W is not a tool bar window, see if
12794 point has been moved into or out of a composition. In that case,
12795 we set b->clip_changed to 1 to force updating the screen. If
12796 b->clip_changed has already been set to 1, we can skip this
12797 check. */
12798 if (!b->clip_changed
12799 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12800 {
12801 ptrdiff_t pt;
12802
12803 if (w == XWINDOW (selected_window))
12804 pt = PT;
12805 else
12806 pt = marker_position (w->pointm);
12807
12808 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12809 || pt != w->last_point)
12810 && check_point_in_composition (w->current_matrix->buffer,
12811 w->last_point,
12812 XBUFFER (w->buffer), pt))
12813 b->clip_changed = 1;
12814 }
12815 }
12816 \f
12817
12818 /* Select FRAME to forward the values of frame-local variables into C
12819 variables so that the redisplay routines can access those values
12820 directly. */
12821
12822 static void
12823 select_frame_for_redisplay (Lisp_Object frame)
12824 {
12825 Lisp_Object tail, tem;
12826 Lisp_Object old = selected_frame;
12827 struct Lisp_Symbol *sym;
12828
12829 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12830
12831 selected_frame = frame;
12832
12833 do {
12834 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12835 if (CONSP (XCAR (tail))
12836 && (tem = XCAR (XCAR (tail)),
12837 SYMBOLP (tem))
12838 && (sym = indirect_variable (XSYMBOL (tem)),
12839 sym->redirect == SYMBOL_LOCALIZED)
12840 && sym->val.blv->frame_local)
12841 /* Use find_symbol_value rather than Fsymbol_value
12842 to avoid an error if it is void. */
12843 find_symbol_value (tem);
12844 } while (!EQ (frame, old) && (frame = old, 1));
12845 }
12846
12847
12848 #define STOP_POLLING \
12849 do { if (! polling_stopped_here) stop_polling (); \
12850 polling_stopped_here = 1; } while (0)
12851
12852 #define RESUME_POLLING \
12853 do { if (polling_stopped_here) start_polling (); \
12854 polling_stopped_here = 0; } while (0)
12855
12856
12857 /* Perhaps in the future avoid recentering windows if it
12858 is not necessary; currently that causes some problems. */
12859
12860 static void
12861 redisplay_internal (void)
12862 {
12863 struct window *w = XWINDOW (selected_window);
12864 struct window *sw;
12865 struct frame *fr;
12866 int pending;
12867 int must_finish = 0;
12868 struct text_pos tlbufpos, tlendpos;
12869 int number_of_visible_frames;
12870 ptrdiff_t count, count1;
12871 struct frame *sf;
12872 int polling_stopped_here = 0;
12873 Lisp_Object old_frame = selected_frame;
12874
12875 /* Non-zero means redisplay has to consider all windows on all
12876 frames. Zero means, only selected_window is considered. */
12877 int consider_all_windows_p;
12878
12879 /* Non-zero means redisplay has to redisplay the miniwindow */
12880 int update_miniwindow_p = 0;
12881
12882 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12883
12884 /* No redisplay if running in batch mode or frame is not yet fully
12885 initialized, or redisplay is explicitly turned off by setting
12886 Vinhibit_redisplay. */
12887 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12888 || !NILP (Vinhibit_redisplay))
12889 return;
12890
12891 /* Don't examine these until after testing Vinhibit_redisplay.
12892 When Emacs is shutting down, perhaps because its connection to
12893 X has dropped, we should not look at them at all. */
12894 fr = XFRAME (w->frame);
12895 sf = SELECTED_FRAME ();
12896
12897 if (!fr->glyphs_initialized_p)
12898 return;
12899
12900 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12901 if (popup_activated ())
12902 return;
12903 #endif
12904
12905 /* I don't think this happens but let's be paranoid. */
12906 if (redisplaying_p)
12907 return;
12908
12909 /* Record a function that resets redisplaying_p to its old value
12910 when we leave this function. */
12911 count = SPECPDL_INDEX ();
12912 record_unwind_protect (unwind_redisplay,
12913 Fcons (make_number (redisplaying_p), selected_frame));
12914 ++redisplaying_p;
12915 specbind (Qinhibit_free_realized_faces, Qnil);
12916
12917 {
12918 Lisp_Object tail, frame;
12919
12920 FOR_EACH_FRAME (tail, frame)
12921 {
12922 struct frame *f = XFRAME (frame);
12923 f->already_hscrolled_p = 0;
12924 }
12925 }
12926
12927 retry:
12928 /* Remember the currently selected window. */
12929 sw = w;
12930
12931 if (!EQ (old_frame, selected_frame)
12932 && FRAME_LIVE_P (XFRAME (old_frame)))
12933 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12934 selected_frame and selected_window to be temporarily out-of-sync so
12935 when we come back here via `goto retry', we need to resync because we
12936 may need to run Elisp code (via prepare_menu_bars). */
12937 select_frame_for_redisplay (old_frame);
12938
12939 pending = 0;
12940 reconsider_clip_changes (w, current_buffer);
12941 last_escape_glyph_frame = NULL;
12942 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12943 last_glyphless_glyph_frame = NULL;
12944 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12945
12946 /* If new fonts have been loaded that make a glyph matrix adjustment
12947 necessary, do it. */
12948 if (fonts_changed_p)
12949 {
12950 adjust_glyphs (NULL);
12951 ++windows_or_buffers_changed;
12952 fonts_changed_p = 0;
12953 }
12954
12955 /* If face_change_count is non-zero, init_iterator will free all
12956 realized faces, which includes the faces referenced from current
12957 matrices. So, we can't reuse current matrices in this case. */
12958 if (face_change_count)
12959 ++windows_or_buffers_changed;
12960
12961 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12962 && FRAME_TTY (sf)->previous_frame != sf)
12963 {
12964 /* Since frames on a single ASCII terminal share the same
12965 display area, displaying a different frame means redisplay
12966 the whole thing. */
12967 windows_or_buffers_changed++;
12968 SET_FRAME_GARBAGED (sf);
12969 #ifndef DOS_NT
12970 set_tty_color_mode (FRAME_TTY (sf), sf);
12971 #endif
12972 FRAME_TTY (sf)->previous_frame = sf;
12973 }
12974
12975 /* Set the visible flags for all frames. Do this before checking
12976 for resized or garbaged frames; they want to know if their frames
12977 are visible. See the comment in frame.h for
12978 FRAME_SAMPLE_VISIBILITY. */
12979 {
12980 Lisp_Object tail, frame;
12981
12982 number_of_visible_frames = 0;
12983
12984 FOR_EACH_FRAME (tail, frame)
12985 {
12986 struct frame *f = XFRAME (frame);
12987
12988 FRAME_SAMPLE_VISIBILITY (f);
12989 if (FRAME_VISIBLE_P (f))
12990 ++number_of_visible_frames;
12991 clear_desired_matrices (f);
12992 }
12993 }
12994
12995 /* Notice any pending interrupt request to change frame size. */
12996 do_pending_window_change (1);
12997
12998 /* do_pending_window_change could change the selected_window due to
12999 frame resizing which makes the selected window too small. */
13000 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13001 {
13002 sw = w;
13003 reconsider_clip_changes (w, current_buffer);
13004 }
13005
13006 /* Clear frames marked as garbaged. */
13007 if (frame_garbaged)
13008 clear_garbaged_frames ();
13009
13010 /* Build menubar and tool-bar items. */
13011 if (NILP (Vmemory_full))
13012 prepare_menu_bars ();
13013
13014 if (windows_or_buffers_changed)
13015 update_mode_lines++;
13016
13017 /* Detect case that we need to write or remove a star in the mode line. */
13018 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13019 {
13020 w->update_mode_line = 1;
13021 if (buffer_shared > 1)
13022 update_mode_lines++;
13023 }
13024
13025 /* Avoid invocation of point motion hooks by `current_column' below. */
13026 count1 = SPECPDL_INDEX ();
13027 specbind (Qinhibit_point_motion_hooks, Qt);
13028
13029 /* If %c is in the mode line, update it if needed. */
13030 if (!NILP (w->column_number_displayed)
13031 /* This alternative quickly identifies a common case
13032 where no change is needed. */
13033 && !(PT == w->last_point
13034 && w->last_modified >= MODIFF
13035 && w->last_overlay_modified >= OVERLAY_MODIFF)
13036 && (XFASTINT (w->column_number_displayed) != current_column ()))
13037 w->update_mode_line = 1;
13038
13039 unbind_to (count1, Qnil);
13040
13041 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13042
13043 /* The variable buffer_shared is set in redisplay_window and
13044 indicates that we redisplay a buffer in different windows. See
13045 there. */
13046 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13047 || cursor_type_changed);
13048
13049 /* If specs for an arrow have changed, do thorough redisplay
13050 to ensure we remove any arrow that should no longer exist. */
13051 if (overlay_arrows_changed_p ())
13052 consider_all_windows_p = windows_or_buffers_changed = 1;
13053
13054 /* Normally the message* functions will have already displayed and
13055 updated the echo area, but the frame may have been trashed, or
13056 the update may have been preempted, so display the echo area
13057 again here. Checking message_cleared_p captures the case that
13058 the echo area should be cleared. */
13059 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13060 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13061 || (message_cleared_p
13062 && minibuf_level == 0
13063 /* If the mini-window is currently selected, this means the
13064 echo-area doesn't show through. */
13065 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13066 {
13067 int window_height_changed_p = echo_area_display (0);
13068
13069 if (message_cleared_p)
13070 update_miniwindow_p = 1;
13071
13072 must_finish = 1;
13073
13074 /* If we don't display the current message, don't clear the
13075 message_cleared_p flag, because, if we did, we wouldn't clear
13076 the echo area in the next redisplay which doesn't preserve
13077 the echo area. */
13078 if (!display_last_displayed_message_p)
13079 message_cleared_p = 0;
13080
13081 if (fonts_changed_p)
13082 goto retry;
13083 else if (window_height_changed_p)
13084 {
13085 consider_all_windows_p = 1;
13086 ++update_mode_lines;
13087 ++windows_or_buffers_changed;
13088
13089 /* If window configuration was changed, frames may have been
13090 marked garbaged. Clear them or we will experience
13091 surprises wrt scrolling. */
13092 if (frame_garbaged)
13093 clear_garbaged_frames ();
13094 }
13095 }
13096 else if (EQ (selected_window, minibuf_window)
13097 && (current_buffer->clip_changed
13098 || w->last_modified < MODIFF
13099 || w->last_overlay_modified < OVERLAY_MODIFF)
13100 && resize_mini_window (w, 0))
13101 {
13102 /* Resized active mini-window to fit the size of what it is
13103 showing if its contents might have changed. */
13104 must_finish = 1;
13105 /* FIXME: this causes all frames to be updated, which seems unnecessary
13106 since only the current frame needs to be considered. This function needs
13107 to be rewritten with two variables, consider_all_windows and
13108 consider_all_frames. */
13109 consider_all_windows_p = 1;
13110 ++windows_or_buffers_changed;
13111 ++update_mode_lines;
13112
13113 /* If window configuration was changed, frames may have been
13114 marked garbaged. Clear them or we will experience
13115 surprises wrt scrolling. */
13116 if (frame_garbaged)
13117 clear_garbaged_frames ();
13118 }
13119
13120
13121 /* If showing the region, and mark has changed, we must redisplay
13122 the whole window. The assignment to this_line_start_pos prevents
13123 the optimization directly below this if-statement. */
13124 if (((!NILP (Vtransient_mark_mode)
13125 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13126 != !NILP (w->region_showing))
13127 || (!NILP (w->region_showing)
13128 && !EQ (w->region_showing,
13129 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13130 CHARPOS (this_line_start_pos) = 0;
13131
13132 /* Optimize the case that only the line containing the cursor in the
13133 selected window has changed. Variables starting with this_ are
13134 set in display_line and record information about the line
13135 containing the cursor. */
13136 tlbufpos = this_line_start_pos;
13137 tlendpos = this_line_end_pos;
13138 if (!consider_all_windows_p
13139 && CHARPOS (tlbufpos) > 0
13140 && !w->update_mode_line
13141 && !current_buffer->clip_changed
13142 && !current_buffer->prevent_redisplay_optimizations_p
13143 && FRAME_VISIBLE_P (XFRAME (w->frame))
13144 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13145 /* Make sure recorded data applies to current buffer, etc. */
13146 && this_line_buffer == current_buffer
13147 && current_buffer == XBUFFER (w->buffer)
13148 && !w->force_start
13149 && !w->optional_new_start
13150 /* Point must be on the line that we have info recorded about. */
13151 && PT >= CHARPOS (tlbufpos)
13152 && PT <= Z - CHARPOS (tlendpos)
13153 /* All text outside that line, including its final newline,
13154 must be unchanged. */
13155 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13156 CHARPOS (tlendpos)))
13157 {
13158 if (CHARPOS (tlbufpos) > BEGV
13159 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13160 && (CHARPOS (tlbufpos) == ZV
13161 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13162 /* Former continuation line has disappeared by becoming empty. */
13163 goto cancel;
13164 else if (w->last_modified < MODIFF
13165 || w->last_overlay_modified < OVERLAY_MODIFF
13166 || MINI_WINDOW_P (w))
13167 {
13168 /* We have to handle the case of continuation around a
13169 wide-column character (see the comment in indent.c around
13170 line 1340).
13171
13172 For instance, in the following case:
13173
13174 -------- Insert --------
13175 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13176 J_I_ ==> J_I_ `^^' are cursors.
13177 ^^ ^^
13178 -------- --------
13179
13180 As we have to redraw the line above, we cannot use this
13181 optimization. */
13182
13183 struct it it;
13184 int line_height_before = this_line_pixel_height;
13185
13186 /* Note that start_display will handle the case that the
13187 line starting at tlbufpos is a continuation line. */
13188 start_display (&it, w, tlbufpos);
13189
13190 /* Implementation note: It this still necessary? */
13191 if (it.current_x != this_line_start_x)
13192 goto cancel;
13193
13194 TRACE ((stderr, "trying display optimization 1\n"));
13195 w->cursor.vpos = -1;
13196 overlay_arrow_seen = 0;
13197 it.vpos = this_line_vpos;
13198 it.current_y = this_line_y;
13199 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13200 display_line (&it);
13201
13202 /* If line contains point, is not continued,
13203 and ends at same distance from eob as before, we win. */
13204 if (w->cursor.vpos >= 0
13205 /* Line is not continued, otherwise this_line_start_pos
13206 would have been set to 0 in display_line. */
13207 && CHARPOS (this_line_start_pos)
13208 /* Line ends as before. */
13209 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13210 /* Line has same height as before. Otherwise other lines
13211 would have to be shifted up or down. */
13212 && this_line_pixel_height == line_height_before)
13213 {
13214 /* If this is not the window's last line, we must adjust
13215 the charstarts of the lines below. */
13216 if (it.current_y < it.last_visible_y)
13217 {
13218 struct glyph_row *row
13219 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13220 ptrdiff_t delta, delta_bytes;
13221
13222 /* We used to distinguish between two cases here,
13223 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13224 when the line ends in a newline or the end of the
13225 buffer's accessible portion. But both cases did
13226 the same, so they were collapsed. */
13227 delta = (Z
13228 - CHARPOS (tlendpos)
13229 - MATRIX_ROW_START_CHARPOS (row));
13230 delta_bytes = (Z_BYTE
13231 - BYTEPOS (tlendpos)
13232 - MATRIX_ROW_START_BYTEPOS (row));
13233
13234 increment_matrix_positions (w->current_matrix,
13235 this_line_vpos + 1,
13236 w->current_matrix->nrows,
13237 delta, delta_bytes);
13238 }
13239
13240 /* If this row displays text now but previously didn't,
13241 or vice versa, w->window_end_vpos may have to be
13242 adjusted. */
13243 if ((it.glyph_row - 1)->displays_text_p)
13244 {
13245 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13246 XSETINT (w->window_end_vpos, this_line_vpos);
13247 }
13248 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13249 && this_line_vpos > 0)
13250 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13251 w->window_end_valid = Qnil;
13252
13253 /* Update hint: No need to try to scroll in update_window. */
13254 w->desired_matrix->no_scrolling_p = 1;
13255
13256 #ifdef GLYPH_DEBUG
13257 *w->desired_matrix->method = 0;
13258 debug_method_add (w, "optimization 1");
13259 #endif
13260 #ifdef HAVE_WINDOW_SYSTEM
13261 update_window_fringes (w, 0);
13262 #endif
13263 goto update;
13264 }
13265 else
13266 goto cancel;
13267 }
13268 else if (/* Cursor position hasn't changed. */
13269 PT == w->last_point
13270 /* Make sure the cursor was last displayed
13271 in this window. Otherwise we have to reposition it. */
13272 && 0 <= w->cursor.vpos
13273 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13274 {
13275 if (!must_finish)
13276 {
13277 do_pending_window_change (1);
13278 /* If selected_window changed, redisplay again. */
13279 if (WINDOWP (selected_window)
13280 && (w = XWINDOW (selected_window)) != sw)
13281 goto retry;
13282
13283 /* We used to always goto end_of_redisplay here, but this
13284 isn't enough if we have a blinking cursor. */
13285 if (w->cursor_off_p == w->last_cursor_off_p)
13286 goto end_of_redisplay;
13287 }
13288 goto update;
13289 }
13290 /* If highlighting the region, or if the cursor is in the echo area,
13291 then we can't just move the cursor. */
13292 else if (! (!NILP (Vtransient_mark_mode)
13293 && !NILP (BVAR (current_buffer, mark_active)))
13294 && (EQ (selected_window,
13295 BVAR (current_buffer, last_selected_window))
13296 || highlight_nonselected_windows)
13297 && NILP (w->region_showing)
13298 && NILP (Vshow_trailing_whitespace)
13299 && !cursor_in_echo_area)
13300 {
13301 struct it it;
13302 struct glyph_row *row;
13303
13304 /* Skip from tlbufpos to PT and see where it is. Note that
13305 PT may be in invisible text. If so, we will end at the
13306 next visible position. */
13307 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13308 NULL, DEFAULT_FACE_ID);
13309 it.current_x = this_line_start_x;
13310 it.current_y = this_line_y;
13311 it.vpos = this_line_vpos;
13312
13313 /* The call to move_it_to stops in front of PT, but
13314 moves over before-strings. */
13315 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13316
13317 if (it.vpos == this_line_vpos
13318 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13319 row->enabled_p))
13320 {
13321 eassert (this_line_vpos == it.vpos);
13322 eassert (this_line_y == it.current_y);
13323 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13324 #ifdef GLYPH_DEBUG
13325 *w->desired_matrix->method = 0;
13326 debug_method_add (w, "optimization 3");
13327 #endif
13328 goto update;
13329 }
13330 else
13331 goto cancel;
13332 }
13333
13334 cancel:
13335 /* Text changed drastically or point moved off of line. */
13336 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13337 }
13338
13339 CHARPOS (this_line_start_pos) = 0;
13340 consider_all_windows_p |= buffer_shared > 1;
13341 ++clear_face_cache_count;
13342 #ifdef HAVE_WINDOW_SYSTEM
13343 ++clear_image_cache_count;
13344 #endif
13345
13346 /* Build desired matrices, and update the display. If
13347 consider_all_windows_p is non-zero, do it for all windows on all
13348 frames. Otherwise do it for selected_window, only. */
13349
13350 if (consider_all_windows_p)
13351 {
13352 Lisp_Object tail, frame;
13353
13354 FOR_EACH_FRAME (tail, frame)
13355 XFRAME (frame)->updated_p = 0;
13356
13357 /* Recompute # windows showing selected buffer. This will be
13358 incremented each time such a window is displayed. */
13359 buffer_shared = 0;
13360
13361 FOR_EACH_FRAME (tail, frame)
13362 {
13363 struct frame *f = XFRAME (frame);
13364
13365 /* We don't have to do anything for unselected terminal
13366 frames. */
13367 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13368 && !EQ (FRAME_TTY (f)->top_frame, frame))
13369 continue;
13370
13371 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13372 {
13373 if (! EQ (frame, selected_frame))
13374 /* Select the frame, for the sake of frame-local
13375 variables. */
13376 select_frame_for_redisplay (frame);
13377
13378 /* Mark all the scroll bars to be removed; we'll redeem
13379 the ones we want when we redisplay their windows. */
13380 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13381 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13382
13383 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13384 redisplay_windows (FRAME_ROOT_WINDOW (f));
13385
13386 /* The X error handler may have deleted that frame. */
13387 if (!FRAME_LIVE_P (f))
13388 continue;
13389
13390 /* Any scroll bars which redisplay_windows should have
13391 nuked should now go away. */
13392 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13393 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13394
13395 /* If fonts changed, display again. */
13396 /* ??? rms: I suspect it is a mistake to jump all the way
13397 back to retry here. It should just retry this frame. */
13398 if (fonts_changed_p)
13399 goto retry;
13400
13401 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13402 {
13403 /* See if we have to hscroll. */
13404 if (!f->already_hscrolled_p)
13405 {
13406 f->already_hscrolled_p = 1;
13407 if (hscroll_windows (f->root_window))
13408 goto retry;
13409 }
13410
13411 /* Prevent various kinds of signals during display
13412 update. stdio is not robust about handling
13413 signals, which can cause an apparent I/O
13414 error. */
13415 if (interrupt_input)
13416 unrequest_sigio ();
13417 STOP_POLLING;
13418
13419 /* Update the display. */
13420 set_window_update_flags (XWINDOW (f->root_window), 1);
13421 pending |= update_frame (f, 0, 0);
13422 f->updated_p = 1;
13423 }
13424 }
13425 }
13426
13427 if (!EQ (old_frame, selected_frame)
13428 && FRAME_LIVE_P (XFRAME (old_frame)))
13429 /* We played a bit fast-and-loose above and allowed selected_frame
13430 and selected_window to be temporarily out-of-sync but let's make
13431 sure this stays contained. */
13432 select_frame_for_redisplay (old_frame);
13433 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13434
13435 if (!pending)
13436 {
13437 /* Do the mark_window_display_accurate after all windows have
13438 been redisplayed because this call resets flags in buffers
13439 which are needed for proper redisplay. */
13440 FOR_EACH_FRAME (tail, frame)
13441 {
13442 struct frame *f = XFRAME (frame);
13443 if (f->updated_p)
13444 {
13445 mark_window_display_accurate (f->root_window, 1);
13446 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13447 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13448 }
13449 }
13450 }
13451 }
13452 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13453 {
13454 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13455 struct frame *mini_frame;
13456
13457 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13458 /* Use list_of_error, not Qerror, so that
13459 we catch only errors and don't run the debugger. */
13460 internal_condition_case_1 (redisplay_window_1, selected_window,
13461 list_of_error,
13462 redisplay_window_error);
13463 if (update_miniwindow_p)
13464 internal_condition_case_1 (redisplay_window_1, mini_window,
13465 list_of_error,
13466 redisplay_window_error);
13467
13468 /* Compare desired and current matrices, perform output. */
13469
13470 update:
13471 /* If fonts changed, display again. */
13472 if (fonts_changed_p)
13473 goto retry;
13474
13475 /* Prevent various kinds of signals during display update.
13476 stdio is not robust about handling signals,
13477 which can cause an apparent I/O error. */
13478 if (interrupt_input)
13479 unrequest_sigio ();
13480 STOP_POLLING;
13481
13482 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13483 {
13484 if (hscroll_windows (selected_window))
13485 goto retry;
13486
13487 XWINDOW (selected_window)->must_be_updated_p = 1;
13488 pending = update_frame (sf, 0, 0);
13489 }
13490
13491 /* We may have called echo_area_display at the top of this
13492 function. If the echo area is on another frame, that may
13493 have put text on a frame other than the selected one, so the
13494 above call to update_frame would not have caught it. Catch
13495 it here. */
13496 mini_window = FRAME_MINIBUF_WINDOW (sf);
13497 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13498
13499 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13500 {
13501 XWINDOW (mini_window)->must_be_updated_p = 1;
13502 pending |= update_frame (mini_frame, 0, 0);
13503 if (!pending && hscroll_windows (mini_window))
13504 goto retry;
13505 }
13506 }
13507
13508 /* If display was paused because of pending input, make sure we do a
13509 thorough update the next time. */
13510 if (pending)
13511 {
13512 /* Prevent the optimization at the beginning of
13513 redisplay_internal that tries a single-line update of the
13514 line containing the cursor in the selected window. */
13515 CHARPOS (this_line_start_pos) = 0;
13516
13517 /* Let the overlay arrow be updated the next time. */
13518 update_overlay_arrows (0);
13519
13520 /* If we pause after scrolling, some rows in the current
13521 matrices of some windows are not valid. */
13522 if (!WINDOW_FULL_WIDTH_P (w)
13523 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13524 update_mode_lines = 1;
13525 }
13526 else
13527 {
13528 if (!consider_all_windows_p)
13529 {
13530 /* This has already been done above if
13531 consider_all_windows_p is set. */
13532 mark_window_display_accurate_1 (w, 1);
13533
13534 /* Say overlay arrows are up to date. */
13535 update_overlay_arrows (1);
13536
13537 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13538 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13539 }
13540
13541 update_mode_lines = 0;
13542 windows_or_buffers_changed = 0;
13543 cursor_type_changed = 0;
13544 }
13545
13546 /* Start SIGIO interrupts coming again. Having them off during the
13547 code above makes it less likely one will discard output, but not
13548 impossible, since there might be stuff in the system buffer here.
13549 But it is much hairier to try to do anything about that. */
13550 if (interrupt_input)
13551 request_sigio ();
13552 RESUME_POLLING;
13553
13554 /* If a frame has become visible which was not before, redisplay
13555 again, so that we display it. Expose events for such a frame
13556 (which it gets when becoming visible) don't call the parts of
13557 redisplay constructing glyphs, so simply exposing a frame won't
13558 display anything in this case. So, we have to display these
13559 frames here explicitly. */
13560 if (!pending)
13561 {
13562 Lisp_Object tail, frame;
13563 int new_count = 0;
13564
13565 FOR_EACH_FRAME (tail, frame)
13566 {
13567 int this_is_visible = 0;
13568
13569 if (XFRAME (frame)->visible)
13570 this_is_visible = 1;
13571 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13572 if (XFRAME (frame)->visible)
13573 this_is_visible = 1;
13574
13575 if (this_is_visible)
13576 new_count++;
13577 }
13578
13579 if (new_count != number_of_visible_frames)
13580 windows_or_buffers_changed++;
13581 }
13582
13583 /* Change frame size now if a change is pending. */
13584 do_pending_window_change (1);
13585
13586 /* If we just did a pending size change, or have additional
13587 visible frames, or selected_window changed, redisplay again. */
13588 if ((windows_or_buffers_changed && !pending)
13589 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13590 goto retry;
13591
13592 /* Clear the face and image caches.
13593
13594 We used to do this only if consider_all_windows_p. But the cache
13595 needs to be cleared if a timer creates images in the current
13596 buffer (e.g. the test case in Bug#6230). */
13597
13598 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13599 {
13600 clear_face_cache (0);
13601 clear_face_cache_count = 0;
13602 }
13603
13604 #ifdef HAVE_WINDOW_SYSTEM
13605 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13606 {
13607 clear_image_caches (Qnil);
13608 clear_image_cache_count = 0;
13609 }
13610 #endif /* HAVE_WINDOW_SYSTEM */
13611
13612 end_of_redisplay:
13613 unbind_to (count, Qnil);
13614 RESUME_POLLING;
13615 }
13616
13617
13618 /* Redisplay, but leave alone any recent echo area message unless
13619 another message has been requested in its place.
13620
13621 This is useful in situations where you need to redisplay but no
13622 user action has occurred, making it inappropriate for the message
13623 area to be cleared. See tracking_off and
13624 wait_reading_process_output for examples of these situations.
13625
13626 FROM_WHERE is an integer saying from where this function was
13627 called. This is useful for debugging. */
13628
13629 void
13630 redisplay_preserve_echo_area (int from_where)
13631 {
13632 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13633
13634 if (!NILP (echo_area_buffer[1]))
13635 {
13636 /* We have a previously displayed message, but no current
13637 message. Redisplay the previous message. */
13638 display_last_displayed_message_p = 1;
13639 redisplay_internal ();
13640 display_last_displayed_message_p = 0;
13641 }
13642 else
13643 redisplay_internal ();
13644
13645 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13646 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13647 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13648 }
13649
13650
13651 /* Function registered with record_unwind_protect in
13652 redisplay_internal. Reset redisplaying_p to the value it had
13653 before redisplay_internal was called, and clear
13654 prevent_freeing_realized_faces_p. It also selects the previously
13655 selected frame, unless it has been deleted (by an X connection
13656 failure during redisplay, for example). */
13657
13658 static Lisp_Object
13659 unwind_redisplay (Lisp_Object val)
13660 {
13661 Lisp_Object old_redisplaying_p, old_frame;
13662
13663 old_redisplaying_p = XCAR (val);
13664 redisplaying_p = XFASTINT (old_redisplaying_p);
13665 old_frame = XCDR (val);
13666 if (! EQ (old_frame, selected_frame)
13667 && FRAME_LIVE_P (XFRAME (old_frame)))
13668 select_frame_for_redisplay (old_frame);
13669 return Qnil;
13670 }
13671
13672
13673 /* Mark the display of window W as accurate or inaccurate. If
13674 ACCURATE_P is non-zero mark display of W as accurate. If
13675 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13676 redisplay_internal is called. */
13677
13678 static void
13679 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13680 {
13681 if (BUFFERP (w->buffer))
13682 {
13683 struct buffer *b = XBUFFER (w->buffer);
13684
13685 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13686 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13687 w->last_had_star
13688 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13689
13690 if (accurate_p)
13691 {
13692 b->clip_changed = 0;
13693 b->prevent_redisplay_optimizations_p = 0;
13694
13695 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13696 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13697 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13698 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13699
13700 w->current_matrix->buffer = b;
13701 w->current_matrix->begv = BUF_BEGV (b);
13702 w->current_matrix->zv = BUF_ZV (b);
13703
13704 w->last_cursor = w->cursor;
13705 w->last_cursor_off_p = w->cursor_off_p;
13706
13707 if (w == XWINDOW (selected_window))
13708 w->last_point = BUF_PT (b);
13709 else
13710 w->last_point = XMARKER (w->pointm)->charpos;
13711 }
13712 }
13713
13714 if (accurate_p)
13715 {
13716 w->window_end_valid = w->buffer;
13717 w->update_mode_line = 0;
13718 }
13719 }
13720
13721
13722 /* Mark the display of windows in the window tree rooted at WINDOW as
13723 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13724 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13725 be redisplayed the next time redisplay_internal is called. */
13726
13727 void
13728 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13729 {
13730 struct window *w;
13731
13732 for (; !NILP (window); window = w->next)
13733 {
13734 w = XWINDOW (window);
13735 mark_window_display_accurate_1 (w, accurate_p);
13736
13737 if (!NILP (w->vchild))
13738 mark_window_display_accurate (w->vchild, accurate_p);
13739 if (!NILP (w->hchild))
13740 mark_window_display_accurate (w->hchild, accurate_p);
13741 }
13742
13743 if (accurate_p)
13744 {
13745 update_overlay_arrows (1);
13746 }
13747 else
13748 {
13749 /* Force a thorough redisplay the next time by setting
13750 last_arrow_position and last_arrow_string to t, which is
13751 unequal to any useful value of Voverlay_arrow_... */
13752 update_overlay_arrows (-1);
13753 }
13754 }
13755
13756
13757 /* Return value in display table DP (Lisp_Char_Table *) for character
13758 C. Since a display table doesn't have any parent, we don't have to
13759 follow parent. Do not call this function directly but use the
13760 macro DISP_CHAR_VECTOR. */
13761
13762 Lisp_Object
13763 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13764 {
13765 Lisp_Object val;
13766
13767 if (ASCII_CHAR_P (c))
13768 {
13769 val = dp->ascii;
13770 if (SUB_CHAR_TABLE_P (val))
13771 val = XSUB_CHAR_TABLE (val)->contents[c];
13772 }
13773 else
13774 {
13775 Lisp_Object table;
13776
13777 XSETCHAR_TABLE (table, dp);
13778 val = char_table_ref (table, c);
13779 }
13780 if (NILP (val))
13781 val = dp->defalt;
13782 return val;
13783 }
13784
13785
13786 \f
13787 /***********************************************************************
13788 Window Redisplay
13789 ***********************************************************************/
13790
13791 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13792
13793 static void
13794 redisplay_windows (Lisp_Object window)
13795 {
13796 while (!NILP (window))
13797 {
13798 struct window *w = XWINDOW (window);
13799
13800 if (!NILP (w->hchild))
13801 redisplay_windows (w->hchild);
13802 else if (!NILP (w->vchild))
13803 redisplay_windows (w->vchild);
13804 else if (!NILP (w->buffer))
13805 {
13806 displayed_buffer = XBUFFER (w->buffer);
13807 /* Use list_of_error, not Qerror, so that
13808 we catch only errors and don't run the debugger. */
13809 internal_condition_case_1 (redisplay_window_0, window,
13810 list_of_error,
13811 redisplay_window_error);
13812 }
13813
13814 window = w->next;
13815 }
13816 }
13817
13818 static Lisp_Object
13819 redisplay_window_error (Lisp_Object ignore)
13820 {
13821 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13822 return Qnil;
13823 }
13824
13825 static Lisp_Object
13826 redisplay_window_0 (Lisp_Object window)
13827 {
13828 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13829 redisplay_window (window, 0);
13830 return Qnil;
13831 }
13832
13833 static Lisp_Object
13834 redisplay_window_1 (Lisp_Object window)
13835 {
13836 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13837 redisplay_window (window, 1);
13838 return Qnil;
13839 }
13840 \f
13841
13842 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13843 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13844 which positions recorded in ROW differ from current buffer
13845 positions.
13846
13847 Return 0 if cursor is not on this row, 1 otherwise. */
13848
13849 static int
13850 set_cursor_from_row (struct window *w, struct glyph_row *row,
13851 struct glyph_matrix *matrix,
13852 ptrdiff_t delta, ptrdiff_t delta_bytes,
13853 int dy, int dvpos)
13854 {
13855 struct glyph *glyph = row->glyphs[TEXT_AREA];
13856 struct glyph *end = glyph + row->used[TEXT_AREA];
13857 struct glyph *cursor = NULL;
13858 /* The last known character position in row. */
13859 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13860 int x = row->x;
13861 ptrdiff_t pt_old = PT - delta;
13862 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13863 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13864 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13865 /* A glyph beyond the edge of TEXT_AREA which we should never
13866 touch. */
13867 struct glyph *glyphs_end = end;
13868 /* Non-zero means we've found a match for cursor position, but that
13869 glyph has the avoid_cursor_p flag set. */
13870 int match_with_avoid_cursor = 0;
13871 /* Non-zero means we've seen at least one glyph that came from a
13872 display string. */
13873 int string_seen = 0;
13874 /* Largest and smallest buffer positions seen so far during scan of
13875 glyph row. */
13876 ptrdiff_t bpos_max = pos_before;
13877 ptrdiff_t bpos_min = pos_after;
13878 /* Last buffer position covered by an overlay string with an integer
13879 `cursor' property. */
13880 ptrdiff_t bpos_covered = 0;
13881 /* Non-zero means the display string on which to display the cursor
13882 comes from a text property, not from an overlay. */
13883 int string_from_text_prop = 0;
13884
13885 /* Don't even try doing anything if called for a mode-line or
13886 header-line row, since the rest of the code isn't prepared to
13887 deal with such calamities. */
13888 eassert (!row->mode_line_p);
13889 if (row->mode_line_p)
13890 return 0;
13891
13892 /* Skip over glyphs not having an object at the start and the end of
13893 the row. These are special glyphs like truncation marks on
13894 terminal frames. */
13895 if (row->displays_text_p)
13896 {
13897 if (!row->reversed_p)
13898 {
13899 while (glyph < end
13900 && INTEGERP (glyph->object)
13901 && glyph->charpos < 0)
13902 {
13903 x += glyph->pixel_width;
13904 ++glyph;
13905 }
13906 while (end > glyph
13907 && INTEGERP ((end - 1)->object)
13908 /* CHARPOS is zero for blanks and stretch glyphs
13909 inserted by extend_face_to_end_of_line. */
13910 && (end - 1)->charpos <= 0)
13911 --end;
13912 glyph_before = glyph - 1;
13913 glyph_after = end;
13914 }
13915 else
13916 {
13917 struct glyph *g;
13918
13919 /* If the glyph row is reversed, we need to process it from back
13920 to front, so swap the edge pointers. */
13921 glyphs_end = end = glyph - 1;
13922 glyph += row->used[TEXT_AREA] - 1;
13923
13924 while (glyph > end + 1
13925 && INTEGERP (glyph->object)
13926 && glyph->charpos < 0)
13927 {
13928 --glyph;
13929 x -= glyph->pixel_width;
13930 }
13931 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13932 --glyph;
13933 /* By default, in reversed rows we put the cursor on the
13934 rightmost (first in the reading order) glyph. */
13935 for (g = end + 1; g < glyph; g++)
13936 x += g->pixel_width;
13937 while (end < glyph
13938 && INTEGERP ((end + 1)->object)
13939 && (end + 1)->charpos <= 0)
13940 ++end;
13941 glyph_before = glyph + 1;
13942 glyph_after = end;
13943 }
13944 }
13945 else if (row->reversed_p)
13946 {
13947 /* In R2L rows that don't display text, put the cursor on the
13948 rightmost glyph. Case in point: an empty last line that is
13949 part of an R2L paragraph. */
13950 cursor = end - 1;
13951 /* Avoid placing the cursor on the last glyph of the row, where
13952 on terminal frames we hold the vertical border between
13953 adjacent windows. */
13954 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13955 && !WINDOW_RIGHTMOST_P (w)
13956 && cursor == row->glyphs[LAST_AREA] - 1)
13957 cursor--;
13958 x = -1; /* will be computed below, at label compute_x */
13959 }
13960
13961 /* Step 1: Try to find the glyph whose character position
13962 corresponds to point. If that's not possible, find 2 glyphs
13963 whose character positions are the closest to point, one before
13964 point, the other after it. */
13965 if (!row->reversed_p)
13966 while (/* not marched to end of glyph row */
13967 glyph < end
13968 /* glyph was not inserted by redisplay for internal purposes */
13969 && !INTEGERP (glyph->object))
13970 {
13971 if (BUFFERP (glyph->object))
13972 {
13973 ptrdiff_t dpos = glyph->charpos - pt_old;
13974
13975 if (glyph->charpos > bpos_max)
13976 bpos_max = glyph->charpos;
13977 if (glyph->charpos < bpos_min)
13978 bpos_min = glyph->charpos;
13979 if (!glyph->avoid_cursor_p)
13980 {
13981 /* If we hit point, we've found the glyph on which to
13982 display the cursor. */
13983 if (dpos == 0)
13984 {
13985 match_with_avoid_cursor = 0;
13986 break;
13987 }
13988 /* See if we've found a better approximation to
13989 POS_BEFORE or to POS_AFTER. */
13990 if (0 > dpos && dpos > pos_before - pt_old)
13991 {
13992 pos_before = glyph->charpos;
13993 glyph_before = glyph;
13994 }
13995 else if (0 < dpos && dpos < pos_after - pt_old)
13996 {
13997 pos_after = glyph->charpos;
13998 glyph_after = glyph;
13999 }
14000 }
14001 else if (dpos == 0)
14002 match_with_avoid_cursor = 1;
14003 }
14004 else if (STRINGP (glyph->object))
14005 {
14006 Lisp_Object chprop;
14007 ptrdiff_t glyph_pos = glyph->charpos;
14008
14009 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14010 glyph->object);
14011 if (!NILP (chprop))
14012 {
14013 /* If the string came from a `display' text property,
14014 look up the buffer position of that property and
14015 use that position to update bpos_max, as if we
14016 actually saw such a position in one of the row's
14017 glyphs. This helps with supporting integer values
14018 of `cursor' property on the display string in
14019 situations where most or all of the row's buffer
14020 text is completely covered by display properties,
14021 so that no glyph with valid buffer positions is
14022 ever seen in the row. */
14023 ptrdiff_t prop_pos =
14024 string_buffer_position_lim (glyph->object, pos_before,
14025 pos_after, 0);
14026
14027 if (prop_pos >= pos_before)
14028 bpos_max = prop_pos - 1;
14029 }
14030 if (INTEGERP (chprop))
14031 {
14032 bpos_covered = bpos_max + XINT (chprop);
14033 /* If the `cursor' property covers buffer positions up
14034 to and including point, we should display cursor on
14035 this glyph. Note that, if a `cursor' property on one
14036 of the string's characters has an integer value, we
14037 will break out of the loop below _before_ we get to
14038 the position match above. IOW, integer values of
14039 the `cursor' property override the "exact match for
14040 point" strategy of positioning the cursor. */
14041 /* Implementation note: bpos_max == pt_old when, e.g.,
14042 we are in an empty line, where bpos_max is set to
14043 MATRIX_ROW_START_CHARPOS, see above. */
14044 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14045 {
14046 cursor = glyph;
14047 break;
14048 }
14049 }
14050
14051 string_seen = 1;
14052 }
14053 x += glyph->pixel_width;
14054 ++glyph;
14055 }
14056 else if (glyph > end) /* row is reversed */
14057 while (!INTEGERP (glyph->object))
14058 {
14059 if (BUFFERP (glyph->object))
14060 {
14061 ptrdiff_t dpos = glyph->charpos - pt_old;
14062
14063 if (glyph->charpos > bpos_max)
14064 bpos_max = glyph->charpos;
14065 if (glyph->charpos < bpos_min)
14066 bpos_min = glyph->charpos;
14067 if (!glyph->avoid_cursor_p)
14068 {
14069 if (dpos == 0)
14070 {
14071 match_with_avoid_cursor = 0;
14072 break;
14073 }
14074 if (0 > dpos && dpos > pos_before - pt_old)
14075 {
14076 pos_before = glyph->charpos;
14077 glyph_before = glyph;
14078 }
14079 else if (0 < dpos && dpos < pos_after - pt_old)
14080 {
14081 pos_after = glyph->charpos;
14082 glyph_after = glyph;
14083 }
14084 }
14085 else if (dpos == 0)
14086 match_with_avoid_cursor = 1;
14087 }
14088 else if (STRINGP (glyph->object))
14089 {
14090 Lisp_Object chprop;
14091 ptrdiff_t glyph_pos = glyph->charpos;
14092
14093 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14094 glyph->object);
14095 if (!NILP (chprop))
14096 {
14097 ptrdiff_t prop_pos =
14098 string_buffer_position_lim (glyph->object, pos_before,
14099 pos_after, 0);
14100
14101 if (prop_pos >= pos_before)
14102 bpos_max = prop_pos - 1;
14103 }
14104 if (INTEGERP (chprop))
14105 {
14106 bpos_covered = bpos_max + XINT (chprop);
14107 /* If the `cursor' property covers buffer positions up
14108 to and including point, we should display cursor on
14109 this glyph. */
14110 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14111 {
14112 cursor = glyph;
14113 break;
14114 }
14115 }
14116 string_seen = 1;
14117 }
14118 --glyph;
14119 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14120 {
14121 x--; /* can't use any pixel_width */
14122 break;
14123 }
14124 x -= glyph->pixel_width;
14125 }
14126
14127 /* Step 2: If we didn't find an exact match for point, we need to
14128 look for a proper place to put the cursor among glyphs between
14129 GLYPH_BEFORE and GLYPH_AFTER. */
14130 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14131 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14132 && bpos_covered < pt_old)
14133 {
14134 /* An empty line has a single glyph whose OBJECT is zero and
14135 whose CHARPOS is the position of a newline on that line.
14136 Note that on a TTY, there are more glyphs after that, which
14137 were produced by extend_face_to_end_of_line, but their
14138 CHARPOS is zero or negative. */
14139 int empty_line_p =
14140 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14141 && INTEGERP (glyph->object) && glyph->charpos > 0;
14142
14143 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14144 {
14145 ptrdiff_t ellipsis_pos;
14146
14147 /* Scan back over the ellipsis glyphs. */
14148 if (!row->reversed_p)
14149 {
14150 ellipsis_pos = (glyph - 1)->charpos;
14151 while (glyph > row->glyphs[TEXT_AREA]
14152 && (glyph - 1)->charpos == ellipsis_pos)
14153 glyph--, x -= glyph->pixel_width;
14154 /* That loop always goes one position too far, including
14155 the glyph before the ellipsis. So scan forward over
14156 that one. */
14157 x += glyph->pixel_width;
14158 glyph++;
14159 }
14160 else /* row is reversed */
14161 {
14162 ellipsis_pos = (glyph + 1)->charpos;
14163 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14164 && (glyph + 1)->charpos == ellipsis_pos)
14165 glyph++, x += glyph->pixel_width;
14166 x -= glyph->pixel_width;
14167 glyph--;
14168 }
14169 }
14170 else if (match_with_avoid_cursor)
14171 {
14172 cursor = glyph_after;
14173 x = -1;
14174 }
14175 else if (string_seen)
14176 {
14177 int incr = row->reversed_p ? -1 : +1;
14178
14179 /* Need to find the glyph that came out of a string which is
14180 present at point. That glyph is somewhere between
14181 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14182 positioned between POS_BEFORE and POS_AFTER in the
14183 buffer. */
14184 struct glyph *start, *stop;
14185 ptrdiff_t pos = pos_before;
14186
14187 x = -1;
14188
14189 /* If the row ends in a newline from a display string,
14190 reordering could have moved the glyphs belonging to the
14191 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14192 in this case we extend the search to the last glyph in
14193 the row that was not inserted by redisplay. */
14194 if (row->ends_in_newline_from_string_p)
14195 {
14196 glyph_after = end;
14197 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14198 }
14199
14200 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14201 correspond to POS_BEFORE and POS_AFTER, respectively. We
14202 need START and STOP in the order that corresponds to the
14203 row's direction as given by its reversed_p flag. If the
14204 directionality of characters between POS_BEFORE and
14205 POS_AFTER is the opposite of the row's base direction,
14206 these characters will have been reordered for display,
14207 and we need to reverse START and STOP. */
14208 if (!row->reversed_p)
14209 {
14210 start = min (glyph_before, glyph_after);
14211 stop = max (glyph_before, glyph_after);
14212 }
14213 else
14214 {
14215 start = max (glyph_before, glyph_after);
14216 stop = min (glyph_before, glyph_after);
14217 }
14218 for (glyph = start + incr;
14219 row->reversed_p ? glyph > stop : glyph < stop; )
14220 {
14221
14222 /* Any glyphs that come from the buffer are here because
14223 of bidi reordering. Skip them, and only pay
14224 attention to glyphs that came from some string. */
14225 if (STRINGP (glyph->object))
14226 {
14227 Lisp_Object str;
14228 ptrdiff_t tem;
14229 /* If the display property covers the newline, we
14230 need to search for it one position farther. */
14231 ptrdiff_t lim = pos_after
14232 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14233
14234 string_from_text_prop = 0;
14235 str = glyph->object;
14236 tem = string_buffer_position_lim (str, pos, lim, 0);
14237 if (tem == 0 /* from overlay */
14238 || pos <= tem)
14239 {
14240 /* If the string from which this glyph came is
14241 found in the buffer at point, or at position
14242 that is closer to point than pos_after, then
14243 we've found the glyph we've been looking for.
14244 If it comes from an overlay (tem == 0), and
14245 it has the `cursor' property on one of its
14246 glyphs, record that glyph as a candidate for
14247 displaying the cursor. (As in the
14248 unidirectional version, we will display the
14249 cursor on the last candidate we find.) */
14250 if (tem == 0
14251 || tem == pt_old
14252 || (tem - pt_old > 0 && tem < pos_after))
14253 {
14254 /* The glyphs from this string could have
14255 been reordered. Find the one with the
14256 smallest string position. Or there could
14257 be a character in the string with the
14258 `cursor' property, which means display
14259 cursor on that character's glyph. */
14260 ptrdiff_t strpos = glyph->charpos;
14261
14262 if (tem)
14263 {
14264 cursor = glyph;
14265 string_from_text_prop = 1;
14266 }
14267 for ( ;
14268 (row->reversed_p ? glyph > stop : glyph < stop)
14269 && EQ (glyph->object, str);
14270 glyph += incr)
14271 {
14272 Lisp_Object cprop;
14273 ptrdiff_t gpos = glyph->charpos;
14274
14275 cprop = Fget_char_property (make_number (gpos),
14276 Qcursor,
14277 glyph->object);
14278 if (!NILP (cprop))
14279 {
14280 cursor = glyph;
14281 break;
14282 }
14283 if (tem && glyph->charpos < strpos)
14284 {
14285 strpos = glyph->charpos;
14286 cursor = glyph;
14287 }
14288 }
14289
14290 if (tem == pt_old
14291 || (tem - pt_old > 0 && tem < pos_after))
14292 goto compute_x;
14293 }
14294 if (tem)
14295 pos = tem + 1; /* don't find previous instances */
14296 }
14297 /* This string is not what we want; skip all of the
14298 glyphs that came from it. */
14299 while ((row->reversed_p ? glyph > stop : glyph < stop)
14300 && EQ (glyph->object, str))
14301 glyph += incr;
14302 }
14303 else
14304 glyph += incr;
14305 }
14306
14307 /* If we reached the end of the line, and END was from a string,
14308 the cursor is not on this line. */
14309 if (cursor == NULL
14310 && (row->reversed_p ? glyph <= end : glyph >= end)
14311 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14312 && STRINGP (end->object)
14313 && row->continued_p)
14314 return 0;
14315 }
14316 /* A truncated row may not include PT among its character positions.
14317 Setting the cursor inside the scroll margin will trigger
14318 recalculation of hscroll in hscroll_window_tree. But if a
14319 display string covers point, defer to the string-handling
14320 code below to figure this out. */
14321 else if (row->truncated_on_left_p && pt_old < bpos_min)
14322 {
14323 cursor = glyph_before;
14324 x = -1;
14325 }
14326 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14327 /* Zero-width characters produce no glyphs. */
14328 || (!empty_line_p
14329 && (row->reversed_p
14330 ? glyph_after > glyphs_end
14331 : glyph_after < glyphs_end)))
14332 {
14333 cursor = glyph_after;
14334 x = -1;
14335 }
14336 }
14337
14338 compute_x:
14339 if (cursor != NULL)
14340 glyph = cursor;
14341 else if (glyph == glyphs_end
14342 && pos_before == pos_after
14343 && STRINGP ((row->reversed_p
14344 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14345 : row->glyphs[TEXT_AREA])->object))
14346 {
14347 /* If all the glyphs of this row came from strings, put the
14348 cursor on the first glyph of the row. This avoids having the
14349 cursor outside of the text area in this very rare and hard
14350 use case. */
14351 glyph =
14352 row->reversed_p
14353 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14354 : row->glyphs[TEXT_AREA];
14355 }
14356 if (x < 0)
14357 {
14358 struct glyph *g;
14359
14360 /* Need to compute x that corresponds to GLYPH. */
14361 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14362 {
14363 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14364 abort ();
14365 x += g->pixel_width;
14366 }
14367 }
14368
14369 /* ROW could be part of a continued line, which, under bidi
14370 reordering, might have other rows whose start and end charpos
14371 occlude point. Only set w->cursor if we found a better
14372 approximation to the cursor position than we have from previously
14373 examined candidate rows belonging to the same continued line. */
14374 if (/* we already have a candidate row */
14375 w->cursor.vpos >= 0
14376 /* that candidate is not the row we are processing */
14377 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14378 /* Make sure cursor.vpos specifies a row whose start and end
14379 charpos occlude point, and it is valid candidate for being a
14380 cursor-row. This is because some callers of this function
14381 leave cursor.vpos at the row where the cursor was displayed
14382 during the last redisplay cycle. */
14383 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14384 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14385 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14386 {
14387 struct glyph *g1 =
14388 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14389
14390 /* Don't consider glyphs that are outside TEXT_AREA. */
14391 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14392 return 0;
14393 /* Keep the candidate whose buffer position is the closest to
14394 point or has the `cursor' property. */
14395 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14396 w->cursor.hpos >= 0
14397 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14398 && ((BUFFERP (g1->object)
14399 && (g1->charpos == pt_old /* an exact match always wins */
14400 || (BUFFERP (glyph->object)
14401 && eabs (g1->charpos - pt_old)
14402 < eabs (glyph->charpos - pt_old))))
14403 /* previous candidate is a glyph from a string that has
14404 a non-nil `cursor' property */
14405 || (STRINGP (g1->object)
14406 && (!NILP (Fget_char_property (make_number (g1->charpos),
14407 Qcursor, g1->object))
14408 /* previous candidate is from the same display
14409 string as this one, and the display string
14410 came from a text property */
14411 || (EQ (g1->object, glyph->object)
14412 && string_from_text_prop)
14413 /* this candidate is from newline and its
14414 position is not an exact match */
14415 || (INTEGERP (glyph->object)
14416 && glyph->charpos != pt_old)))))
14417 return 0;
14418 /* If this candidate gives an exact match, use that. */
14419 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14420 /* If this candidate is a glyph created for the
14421 terminating newline of a line, and point is on that
14422 newline, it wins because it's an exact match. */
14423 || (!row->continued_p
14424 && INTEGERP (glyph->object)
14425 && glyph->charpos == 0
14426 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14427 /* Otherwise, keep the candidate that comes from a row
14428 spanning less buffer positions. This may win when one or
14429 both candidate positions are on glyphs that came from
14430 display strings, for which we cannot compare buffer
14431 positions. */
14432 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14433 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14434 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14435 return 0;
14436 }
14437 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14438 w->cursor.x = x;
14439 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14440 w->cursor.y = row->y + dy;
14441
14442 if (w == XWINDOW (selected_window))
14443 {
14444 if (!row->continued_p
14445 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14446 && row->x == 0)
14447 {
14448 this_line_buffer = XBUFFER (w->buffer);
14449
14450 CHARPOS (this_line_start_pos)
14451 = MATRIX_ROW_START_CHARPOS (row) + delta;
14452 BYTEPOS (this_line_start_pos)
14453 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14454
14455 CHARPOS (this_line_end_pos)
14456 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14457 BYTEPOS (this_line_end_pos)
14458 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14459
14460 this_line_y = w->cursor.y;
14461 this_line_pixel_height = row->height;
14462 this_line_vpos = w->cursor.vpos;
14463 this_line_start_x = row->x;
14464 }
14465 else
14466 CHARPOS (this_line_start_pos) = 0;
14467 }
14468
14469 return 1;
14470 }
14471
14472
14473 /* Run window scroll functions, if any, for WINDOW with new window
14474 start STARTP. Sets the window start of WINDOW to that position.
14475
14476 We assume that the window's buffer is really current. */
14477
14478 static inline struct text_pos
14479 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14480 {
14481 struct window *w = XWINDOW (window);
14482 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14483
14484 if (current_buffer != XBUFFER (w->buffer))
14485 abort ();
14486
14487 if (!NILP (Vwindow_scroll_functions))
14488 {
14489 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14490 make_number (CHARPOS (startp)));
14491 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14492 /* In case the hook functions switch buffers. */
14493 if (current_buffer != XBUFFER (w->buffer))
14494 set_buffer_internal_1 (XBUFFER (w->buffer));
14495 }
14496
14497 return startp;
14498 }
14499
14500
14501 /* Make sure the line containing the cursor is fully visible.
14502 A value of 1 means there is nothing to be done.
14503 (Either the line is fully visible, or it cannot be made so,
14504 or we cannot tell.)
14505
14506 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14507 is higher than window.
14508
14509 A value of 0 means the caller should do scrolling
14510 as if point had gone off the screen. */
14511
14512 static int
14513 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14514 {
14515 struct glyph_matrix *matrix;
14516 struct glyph_row *row;
14517 int window_height;
14518
14519 if (!make_cursor_line_fully_visible_p)
14520 return 1;
14521
14522 /* It's not always possible to find the cursor, e.g, when a window
14523 is full of overlay strings. Don't do anything in that case. */
14524 if (w->cursor.vpos < 0)
14525 return 1;
14526
14527 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14528 row = MATRIX_ROW (matrix, w->cursor.vpos);
14529
14530 /* If the cursor row is not partially visible, there's nothing to do. */
14531 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14532 return 1;
14533
14534 /* If the row the cursor is in is taller than the window's height,
14535 it's not clear what to do, so do nothing. */
14536 window_height = window_box_height (w);
14537 if (row->height >= window_height)
14538 {
14539 if (!force_p || MINI_WINDOW_P (w)
14540 || w->vscroll || w->cursor.vpos == 0)
14541 return 1;
14542 }
14543 return 0;
14544 }
14545
14546
14547 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14548 non-zero means only WINDOW is redisplayed in redisplay_internal.
14549 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14550 in redisplay_window to bring a partially visible line into view in
14551 the case that only the cursor has moved.
14552
14553 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14554 last screen line's vertical height extends past the end of the screen.
14555
14556 Value is
14557
14558 1 if scrolling succeeded
14559
14560 0 if scrolling didn't find point.
14561
14562 -1 if new fonts have been loaded so that we must interrupt
14563 redisplay, adjust glyph matrices, and try again. */
14564
14565 enum
14566 {
14567 SCROLLING_SUCCESS,
14568 SCROLLING_FAILED,
14569 SCROLLING_NEED_LARGER_MATRICES
14570 };
14571
14572 /* If scroll-conservatively is more than this, never recenter.
14573
14574 If you change this, don't forget to update the doc string of
14575 `scroll-conservatively' and the Emacs manual. */
14576 #define SCROLL_LIMIT 100
14577
14578 static int
14579 try_scrolling (Lisp_Object window, int just_this_one_p,
14580 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14581 int temp_scroll_step, int last_line_misfit)
14582 {
14583 struct window *w = XWINDOW (window);
14584 struct frame *f = XFRAME (w->frame);
14585 struct text_pos pos, startp;
14586 struct it it;
14587 int this_scroll_margin, scroll_max, rc, height;
14588 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14589 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14590 Lisp_Object aggressive;
14591 /* We will never try scrolling more than this number of lines. */
14592 int scroll_limit = SCROLL_LIMIT;
14593
14594 #ifdef GLYPH_DEBUG
14595 debug_method_add (w, "try_scrolling");
14596 #endif
14597
14598 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14599
14600 /* Compute scroll margin height in pixels. We scroll when point is
14601 within this distance from the top or bottom of the window. */
14602 if (scroll_margin > 0)
14603 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14604 * FRAME_LINE_HEIGHT (f);
14605 else
14606 this_scroll_margin = 0;
14607
14608 /* Force arg_scroll_conservatively to have a reasonable value, to
14609 avoid scrolling too far away with slow move_it_* functions. Note
14610 that the user can supply scroll-conservatively equal to
14611 `most-positive-fixnum', which can be larger than INT_MAX. */
14612 if (arg_scroll_conservatively > scroll_limit)
14613 {
14614 arg_scroll_conservatively = scroll_limit + 1;
14615 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14616 }
14617 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14618 /* Compute how much we should try to scroll maximally to bring
14619 point into view. */
14620 scroll_max = (max (scroll_step,
14621 max (arg_scroll_conservatively, temp_scroll_step))
14622 * FRAME_LINE_HEIGHT (f));
14623 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14624 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14625 /* We're trying to scroll because of aggressive scrolling but no
14626 scroll_step is set. Choose an arbitrary one. */
14627 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14628 else
14629 scroll_max = 0;
14630
14631 too_near_end:
14632
14633 /* Decide whether to scroll down. */
14634 if (PT > CHARPOS (startp))
14635 {
14636 int scroll_margin_y;
14637
14638 /* Compute the pixel ypos of the scroll margin, then move IT to
14639 either that ypos or PT, whichever comes first. */
14640 start_display (&it, w, startp);
14641 scroll_margin_y = it.last_visible_y - this_scroll_margin
14642 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14643 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14644 (MOVE_TO_POS | MOVE_TO_Y));
14645
14646 if (PT > CHARPOS (it.current.pos))
14647 {
14648 int y0 = line_bottom_y (&it);
14649 /* Compute how many pixels below window bottom to stop searching
14650 for PT. This avoids costly search for PT that is far away if
14651 the user limited scrolling by a small number of lines, but
14652 always finds PT if scroll_conservatively is set to a large
14653 number, such as most-positive-fixnum. */
14654 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14655 int y_to_move = it.last_visible_y + slack;
14656
14657 /* Compute the distance from the scroll margin to PT or to
14658 the scroll limit, whichever comes first. This should
14659 include the height of the cursor line, to make that line
14660 fully visible. */
14661 move_it_to (&it, PT, -1, y_to_move,
14662 -1, MOVE_TO_POS | MOVE_TO_Y);
14663 dy = line_bottom_y (&it) - y0;
14664
14665 if (dy > scroll_max)
14666 return SCROLLING_FAILED;
14667
14668 if (dy > 0)
14669 scroll_down_p = 1;
14670 }
14671 }
14672
14673 if (scroll_down_p)
14674 {
14675 /* Point is in or below the bottom scroll margin, so move the
14676 window start down. If scrolling conservatively, move it just
14677 enough down to make point visible. If scroll_step is set,
14678 move it down by scroll_step. */
14679 if (arg_scroll_conservatively)
14680 amount_to_scroll
14681 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14682 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14683 else if (scroll_step || temp_scroll_step)
14684 amount_to_scroll = scroll_max;
14685 else
14686 {
14687 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14688 height = WINDOW_BOX_TEXT_HEIGHT (w);
14689 if (NUMBERP (aggressive))
14690 {
14691 double float_amount = XFLOATINT (aggressive) * height;
14692 amount_to_scroll = float_amount;
14693 if (amount_to_scroll == 0 && float_amount > 0)
14694 amount_to_scroll = 1;
14695 /* Don't let point enter the scroll margin near top of
14696 the window. */
14697 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14698 amount_to_scroll = height - 2*this_scroll_margin + dy;
14699 }
14700 }
14701
14702 if (amount_to_scroll <= 0)
14703 return SCROLLING_FAILED;
14704
14705 start_display (&it, w, startp);
14706 if (arg_scroll_conservatively <= scroll_limit)
14707 move_it_vertically (&it, amount_to_scroll);
14708 else
14709 {
14710 /* Extra precision for users who set scroll-conservatively
14711 to a large number: make sure the amount we scroll
14712 the window start is never less than amount_to_scroll,
14713 which was computed as distance from window bottom to
14714 point. This matters when lines at window top and lines
14715 below window bottom have different height. */
14716 struct it it1;
14717 void *it1data = NULL;
14718 /* We use a temporary it1 because line_bottom_y can modify
14719 its argument, if it moves one line down; see there. */
14720 int start_y;
14721
14722 SAVE_IT (it1, it, it1data);
14723 start_y = line_bottom_y (&it1);
14724 do {
14725 RESTORE_IT (&it, &it, it1data);
14726 move_it_by_lines (&it, 1);
14727 SAVE_IT (it1, it, it1data);
14728 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14729 }
14730
14731 /* If STARTP is unchanged, move it down another screen line. */
14732 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14733 move_it_by_lines (&it, 1);
14734 startp = it.current.pos;
14735 }
14736 else
14737 {
14738 struct text_pos scroll_margin_pos = startp;
14739
14740 /* See if point is inside the scroll margin at the top of the
14741 window. */
14742 if (this_scroll_margin)
14743 {
14744 start_display (&it, w, startp);
14745 move_it_vertically (&it, this_scroll_margin);
14746 scroll_margin_pos = it.current.pos;
14747 }
14748
14749 if (PT < CHARPOS (scroll_margin_pos))
14750 {
14751 /* Point is in the scroll margin at the top of the window or
14752 above what is displayed in the window. */
14753 int y0, y_to_move;
14754
14755 /* Compute the vertical distance from PT to the scroll
14756 margin position. Move as far as scroll_max allows, or
14757 one screenful, or 10 screen lines, whichever is largest.
14758 Give up if distance is greater than scroll_max. */
14759 SET_TEXT_POS (pos, PT, PT_BYTE);
14760 start_display (&it, w, pos);
14761 y0 = it.current_y;
14762 y_to_move = max (it.last_visible_y,
14763 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14764 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14765 y_to_move, -1,
14766 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14767 dy = it.current_y - y0;
14768 if (dy > scroll_max)
14769 return SCROLLING_FAILED;
14770
14771 /* Compute new window start. */
14772 start_display (&it, w, startp);
14773
14774 if (arg_scroll_conservatively)
14775 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14776 max (scroll_step, temp_scroll_step));
14777 else if (scroll_step || temp_scroll_step)
14778 amount_to_scroll = scroll_max;
14779 else
14780 {
14781 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14782 height = WINDOW_BOX_TEXT_HEIGHT (w);
14783 if (NUMBERP (aggressive))
14784 {
14785 double float_amount = XFLOATINT (aggressive) * height;
14786 amount_to_scroll = float_amount;
14787 if (amount_to_scroll == 0 && float_amount > 0)
14788 amount_to_scroll = 1;
14789 amount_to_scroll -=
14790 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14791 /* Don't let point enter the scroll margin near
14792 bottom of the window. */
14793 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14794 amount_to_scroll = height - 2*this_scroll_margin + dy;
14795 }
14796 }
14797
14798 if (amount_to_scroll <= 0)
14799 return SCROLLING_FAILED;
14800
14801 move_it_vertically_backward (&it, amount_to_scroll);
14802 startp = it.current.pos;
14803 }
14804 }
14805
14806 /* Run window scroll functions. */
14807 startp = run_window_scroll_functions (window, startp);
14808
14809 /* Display the window. Give up if new fonts are loaded, or if point
14810 doesn't appear. */
14811 if (!try_window (window, startp, 0))
14812 rc = SCROLLING_NEED_LARGER_MATRICES;
14813 else if (w->cursor.vpos < 0)
14814 {
14815 clear_glyph_matrix (w->desired_matrix);
14816 rc = SCROLLING_FAILED;
14817 }
14818 else
14819 {
14820 /* Maybe forget recorded base line for line number display. */
14821 if (!just_this_one_p
14822 || current_buffer->clip_changed
14823 || BEG_UNCHANGED < CHARPOS (startp))
14824 w->base_line_number = Qnil;
14825
14826 /* If cursor ends up on a partially visible line,
14827 treat that as being off the bottom of the screen. */
14828 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14829 /* It's possible that the cursor is on the first line of the
14830 buffer, which is partially obscured due to a vscroll
14831 (Bug#7537). In that case, avoid looping forever . */
14832 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14833 {
14834 clear_glyph_matrix (w->desired_matrix);
14835 ++extra_scroll_margin_lines;
14836 goto too_near_end;
14837 }
14838 rc = SCROLLING_SUCCESS;
14839 }
14840
14841 return rc;
14842 }
14843
14844
14845 /* Compute a suitable window start for window W if display of W starts
14846 on a continuation line. Value is non-zero if a new window start
14847 was computed.
14848
14849 The new window start will be computed, based on W's width, starting
14850 from the start of the continued line. It is the start of the
14851 screen line with the minimum distance from the old start W->start. */
14852
14853 static int
14854 compute_window_start_on_continuation_line (struct window *w)
14855 {
14856 struct text_pos pos, start_pos;
14857 int window_start_changed_p = 0;
14858
14859 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14860
14861 /* If window start is on a continuation line... Window start may be
14862 < BEGV in case there's invisible text at the start of the
14863 buffer (M-x rmail, for example). */
14864 if (CHARPOS (start_pos) > BEGV
14865 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14866 {
14867 struct it it;
14868 struct glyph_row *row;
14869
14870 /* Handle the case that the window start is out of range. */
14871 if (CHARPOS (start_pos) < BEGV)
14872 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14873 else if (CHARPOS (start_pos) > ZV)
14874 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14875
14876 /* Find the start of the continued line. This should be fast
14877 because scan_buffer is fast (newline cache). */
14878 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14879 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14880 row, DEFAULT_FACE_ID);
14881 reseat_at_previous_visible_line_start (&it);
14882
14883 /* If the line start is "too far" away from the window start,
14884 say it takes too much time to compute a new window start. */
14885 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14886 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14887 {
14888 int min_distance, distance;
14889
14890 /* Move forward by display lines to find the new window
14891 start. If window width was enlarged, the new start can
14892 be expected to be > the old start. If window width was
14893 decreased, the new window start will be < the old start.
14894 So, we're looking for the display line start with the
14895 minimum distance from the old window start. */
14896 pos = it.current.pos;
14897 min_distance = INFINITY;
14898 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14899 distance < min_distance)
14900 {
14901 min_distance = distance;
14902 pos = it.current.pos;
14903 move_it_by_lines (&it, 1);
14904 }
14905
14906 /* Set the window start there. */
14907 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14908 window_start_changed_p = 1;
14909 }
14910 }
14911
14912 return window_start_changed_p;
14913 }
14914
14915
14916 /* Try cursor movement in case text has not changed in window WINDOW,
14917 with window start STARTP. Value is
14918
14919 CURSOR_MOVEMENT_SUCCESS if successful
14920
14921 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14922
14923 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14924 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14925 we want to scroll as if scroll-step were set to 1. See the code.
14926
14927 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14928 which case we have to abort this redisplay, and adjust matrices
14929 first. */
14930
14931 enum
14932 {
14933 CURSOR_MOVEMENT_SUCCESS,
14934 CURSOR_MOVEMENT_CANNOT_BE_USED,
14935 CURSOR_MOVEMENT_MUST_SCROLL,
14936 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14937 };
14938
14939 static int
14940 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14941 {
14942 struct window *w = XWINDOW (window);
14943 struct frame *f = XFRAME (w->frame);
14944 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14945
14946 #ifdef GLYPH_DEBUG
14947 if (inhibit_try_cursor_movement)
14948 return rc;
14949 #endif
14950
14951 /* Previously, there was a check for Lisp integer in the
14952 if-statement below. Now, this field is converted to
14953 ptrdiff_t, thus zero means invalid position in a buffer. */
14954 eassert (w->last_point > 0);
14955
14956 /* Handle case where text has not changed, only point, and it has
14957 not moved off the frame. */
14958 if (/* Point may be in this window. */
14959 PT >= CHARPOS (startp)
14960 /* Selective display hasn't changed. */
14961 && !current_buffer->clip_changed
14962 /* Function force-mode-line-update is used to force a thorough
14963 redisplay. It sets either windows_or_buffers_changed or
14964 update_mode_lines. So don't take a shortcut here for these
14965 cases. */
14966 && !update_mode_lines
14967 && !windows_or_buffers_changed
14968 && !cursor_type_changed
14969 /* Can't use this case if highlighting a region. When a
14970 region exists, cursor movement has to do more than just
14971 set the cursor. */
14972 && !(!NILP (Vtransient_mark_mode)
14973 && !NILP (BVAR (current_buffer, mark_active)))
14974 && NILP (w->region_showing)
14975 && NILP (Vshow_trailing_whitespace)
14976 /* This code is not used for mini-buffer for the sake of the case
14977 of redisplaying to replace an echo area message; since in
14978 that case the mini-buffer contents per se are usually
14979 unchanged. This code is of no real use in the mini-buffer
14980 since the handling of this_line_start_pos, etc., in redisplay
14981 handles the same cases. */
14982 && !EQ (window, minibuf_window)
14983 /* When splitting windows or for new windows, it happens that
14984 redisplay is called with a nil window_end_vpos or one being
14985 larger than the window. This should really be fixed in
14986 window.c. I don't have this on my list, now, so we do
14987 approximately the same as the old redisplay code. --gerd. */
14988 && INTEGERP (w->window_end_vpos)
14989 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14990 && (FRAME_WINDOW_P (f)
14991 || !overlay_arrow_in_current_buffer_p ()))
14992 {
14993 int this_scroll_margin, top_scroll_margin;
14994 struct glyph_row *row = NULL;
14995
14996 #ifdef GLYPH_DEBUG
14997 debug_method_add (w, "cursor movement");
14998 #endif
14999
15000 /* Scroll if point within this distance from the top or bottom
15001 of the window. This is a pixel value. */
15002 if (scroll_margin > 0)
15003 {
15004 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15005 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15006 }
15007 else
15008 this_scroll_margin = 0;
15009
15010 top_scroll_margin = this_scroll_margin;
15011 if (WINDOW_WANTS_HEADER_LINE_P (w))
15012 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15013
15014 /* Start with the row the cursor was displayed during the last
15015 not paused redisplay. Give up if that row is not valid. */
15016 if (w->last_cursor.vpos < 0
15017 || w->last_cursor.vpos >= w->current_matrix->nrows)
15018 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15019 else
15020 {
15021 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15022 if (row->mode_line_p)
15023 ++row;
15024 if (!row->enabled_p)
15025 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15026 }
15027
15028 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15029 {
15030 int scroll_p = 0, must_scroll = 0;
15031 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15032
15033 if (PT > w->last_point)
15034 {
15035 /* Point has moved forward. */
15036 while (MATRIX_ROW_END_CHARPOS (row) < PT
15037 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15038 {
15039 eassert (row->enabled_p);
15040 ++row;
15041 }
15042
15043 /* If the end position of a row equals the start
15044 position of the next row, and PT is at that position,
15045 we would rather display cursor in the next line. */
15046 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15047 && MATRIX_ROW_END_CHARPOS (row) == PT
15048 && row < w->current_matrix->rows
15049 + w->current_matrix->nrows - 1
15050 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15051 && !cursor_row_p (row))
15052 ++row;
15053
15054 /* If within the scroll margin, scroll. Note that
15055 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15056 the next line would be drawn, and that
15057 this_scroll_margin can be zero. */
15058 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15059 || PT > MATRIX_ROW_END_CHARPOS (row)
15060 /* Line is completely visible last line in window
15061 and PT is to be set in the next line. */
15062 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15063 && PT == MATRIX_ROW_END_CHARPOS (row)
15064 && !row->ends_at_zv_p
15065 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15066 scroll_p = 1;
15067 }
15068 else if (PT < w->last_point)
15069 {
15070 /* Cursor has to be moved backward. Note that PT >=
15071 CHARPOS (startp) because of the outer if-statement. */
15072 while (!row->mode_line_p
15073 && (MATRIX_ROW_START_CHARPOS (row) > PT
15074 || (MATRIX_ROW_START_CHARPOS (row) == PT
15075 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15076 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15077 row > w->current_matrix->rows
15078 && (row-1)->ends_in_newline_from_string_p))))
15079 && (row->y > top_scroll_margin
15080 || CHARPOS (startp) == BEGV))
15081 {
15082 eassert (row->enabled_p);
15083 --row;
15084 }
15085
15086 /* Consider the following case: Window starts at BEGV,
15087 there is invisible, intangible text at BEGV, so that
15088 display starts at some point START > BEGV. It can
15089 happen that we are called with PT somewhere between
15090 BEGV and START. Try to handle that case. */
15091 if (row < w->current_matrix->rows
15092 || row->mode_line_p)
15093 {
15094 row = w->current_matrix->rows;
15095 if (row->mode_line_p)
15096 ++row;
15097 }
15098
15099 /* Due to newlines in overlay strings, we may have to
15100 skip forward over overlay strings. */
15101 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15102 && MATRIX_ROW_END_CHARPOS (row) == PT
15103 && !cursor_row_p (row))
15104 ++row;
15105
15106 /* If within the scroll margin, scroll. */
15107 if (row->y < top_scroll_margin
15108 && CHARPOS (startp) != BEGV)
15109 scroll_p = 1;
15110 }
15111 else
15112 {
15113 /* Cursor did not move. So don't scroll even if cursor line
15114 is partially visible, as it was so before. */
15115 rc = CURSOR_MOVEMENT_SUCCESS;
15116 }
15117
15118 if (PT < MATRIX_ROW_START_CHARPOS (row)
15119 || PT > MATRIX_ROW_END_CHARPOS (row))
15120 {
15121 /* if PT is not in the glyph row, give up. */
15122 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15123 must_scroll = 1;
15124 }
15125 else if (rc != CURSOR_MOVEMENT_SUCCESS
15126 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15127 {
15128 struct glyph_row *row1;
15129
15130 /* If rows are bidi-reordered and point moved, back up
15131 until we find a row that does not belong to a
15132 continuation line. This is because we must consider
15133 all rows of a continued line as candidates for the
15134 new cursor positioning, since row start and end
15135 positions change non-linearly with vertical position
15136 in such rows. */
15137 /* FIXME: Revisit this when glyph ``spilling'' in
15138 continuation lines' rows is implemented for
15139 bidi-reordered rows. */
15140 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15141 MATRIX_ROW_CONTINUATION_LINE_P (row);
15142 --row)
15143 {
15144 /* If we hit the beginning of the displayed portion
15145 without finding the first row of a continued
15146 line, give up. */
15147 if (row <= row1)
15148 {
15149 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15150 break;
15151 }
15152 eassert (row->enabled_p);
15153 }
15154 }
15155 if (must_scroll)
15156 ;
15157 else if (rc != CURSOR_MOVEMENT_SUCCESS
15158 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15159 /* Make sure this isn't a header line by any chance, since
15160 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15161 && !row->mode_line_p
15162 && make_cursor_line_fully_visible_p)
15163 {
15164 if (PT == MATRIX_ROW_END_CHARPOS (row)
15165 && !row->ends_at_zv_p
15166 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15167 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15168 else if (row->height > window_box_height (w))
15169 {
15170 /* If we end up in a partially visible line, let's
15171 make it fully visible, except when it's taller
15172 than the window, in which case we can't do much
15173 about it. */
15174 *scroll_step = 1;
15175 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15176 }
15177 else
15178 {
15179 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15180 if (!cursor_row_fully_visible_p (w, 0, 1))
15181 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15182 else
15183 rc = CURSOR_MOVEMENT_SUCCESS;
15184 }
15185 }
15186 else if (scroll_p)
15187 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15188 else if (rc != CURSOR_MOVEMENT_SUCCESS
15189 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15190 {
15191 /* With bidi-reordered rows, there could be more than
15192 one candidate row whose start and end positions
15193 occlude point. We need to let set_cursor_from_row
15194 find the best candidate. */
15195 /* FIXME: Revisit this when glyph ``spilling'' in
15196 continuation lines' rows is implemented for
15197 bidi-reordered rows. */
15198 int rv = 0;
15199
15200 do
15201 {
15202 int at_zv_p = 0, exact_match_p = 0;
15203
15204 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15205 && PT <= MATRIX_ROW_END_CHARPOS (row)
15206 && cursor_row_p (row))
15207 rv |= set_cursor_from_row (w, row, w->current_matrix,
15208 0, 0, 0, 0);
15209 /* As soon as we've found the exact match for point,
15210 or the first suitable row whose ends_at_zv_p flag
15211 is set, we are done. */
15212 at_zv_p =
15213 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15214 if (rv && !at_zv_p
15215 && w->cursor.hpos >= 0
15216 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15217 w->cursor.vpos))
15218 {
15219 struct glyph_row *candidate =
15220 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15221 struct glyph *g =
15222 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15223 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15224
15225 exact_match_p =
15226 (BUFFERP (g->object) && g->charpos == PT)
15227 || (INTEGERP (g->object)
15228 && (g->charpos == PT
15229 || (g->charpos == 0 && endpos - 1 == PT)));
15230 }
15231 if (rv && (at_zv_p || exact_match_p))
15232 {
15233 rc = CURSOR_MOVEMENT_SUCCESS;
15234 break;
15235 }
15236 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15237 break;
15238 ++row;
15239 }
15240 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15241 || row->continued_p)
15242 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15243 || (MATRIX_ROW_START_CHARPOS (row) == PT
15244 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15245 /* If we didn't find any candidate rows, or exited the
15246 loop before all the candidates were examined, signal
15247 to the caller that this method failed. */
15248 if (rc != CURSOR_MOVEMENT_SUCCESS
15249 && !(rv
15250 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15251 && !row->continued_p))
15252 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15253 else if (rv)
15254 rc = CURSOR_MOVEMENT_SUCCESS;
15255 }
15256 else
15257 {
15258 do
15259 {
15260 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15261 {
15262 rc = CURSOR_MOVEMENT_SUCCESS;
15263 break;
15264 }
15265 ++row;
15266 }
15267 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15268 && MATRIX_ROW_START_CHARPOS (row) == PT
15269 && cursor_row_p (row));
15270 }
15271 }
15272 }
15273
15274 return rc;
15275 }
15276
15277 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15278 static
15279 #endif
15280 void
15281 set_vertical_scroll_bar (struct window *w)
15282 {
15283 ptrdiff_t start, end, whole;
15284
15285 /* Calculate the start and end positions for the current window.
15286 At some point, it would be nice to choose between scrollbars
15287 which reflect the whole buffer size, with special markers
15288 indicating narrowing, and scrollbars which reflect only the
15289 visible region.
15290
15291 Note that mini-buffers sometimes aren't displaying any text. */
15292 if (!MINI_WINDOW_P (w)
15293 || (w == XWINDOW (minibuf_window)
15294 && NILP (echo_area_buffer[0])))
15295 {
15296 struct buffer *buf = XBUFFER (w->buffer);
15297 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15298 start = marker_position (w->start) - BUF_BEGV (buf);
15299 /* I don't think this is guaranteed to be right. For the
15300 moment, we'll pretend it is. */
15301 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15302
15303 if (end < start)
15304 end = start;
15305 if (whole < (end - start))
15306 whole = end - start;
15307 }
15308 else
15309 start = end = whole = 0;
15310
15311 /* Indicate what this scroll bar ought to be displaying now. */
15312 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15313 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15314 (w, end - start, whole, start);
15315 }
15316
15317
15318 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15319 selected_window is redisplayed.
15320
15321 We can return without actually redisplaying the window if
15322 fonts_changed_p is nonzero. In that case, redisplay_internal will
15323 retry. */
15324
15325 static void
15326 redisplay_window (Lisp_Object window, int just_this_one_p)
15327 {
15328 struct window *w = XWINDOW (window);
15329 struct frame *f = XFRAME (w->frame);
15330 struct buffer *buffer = XBUFFER (w->buffer);
15331 struct buffer *old = current_buffer;
15332 struct text_pos lpoint, opoint, startp;
15333 int update_mode_line;
15334 int tem;
15335 struct it it;
15336 /* Record it now because it's overwritten. */
15337 int current_matrix_up_to_date_p = 0;
15338 int used_current_matrix_p = 0;
15339 /* This is less strict than current_matrix_up_to_date_p.
15340 It indicates that the buffer contents and narrowing are unchanged. */
15341 int buffer_unchanged_p = 0;
15342 int temp_scroll_step = 0;
15343 ptrdiff_t count = SPECPDL_INDEX ();
15344 int rc;
15345 int centering_position = -1;
15346 int last_line_misfit = 0;
15347 ptrdiff_t beg_unchanged, end_unchanged;
15348
15349 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15350 opoint = lpoint;
15351
15352 /* W must be a leaf window here. */
15353 eassert (!NILP (w->buffer));
15354 #ifdef GLYPH_DEBUG
15355 *w->desired_matrix->method = 0;
15356 #endif
15357
15358 restart:
15359 reconsider_clip_changes (w, buffer);
15360
15361 /* Has the mode line to be updated? */
15362 update_mode_line = (w->update_mode_line
15363 || update_mode_lines
15364 || buffer->clip_changed
15365 || buffer->prevent_redisplay_optimizations_p);
15366
15367 if (MINI_WINDOW_P (w))
15368 {
15369 if (w == XWINDOW (echo_area_window)
15370 && !NILP (echo_area_buffer[0]))
15371 {
15372 if (update_mode_line)
15373 /* We may have to update a tty frame's menu bar or a
15374 tool-bar. Example `M-x C-h C-h C-g'. */
15375 goto finish_menu_bars;
15376 else
15377 /* We've already displayed the echo area glyphs in this window. */
15378 goto finish_scroll_bars;
15379 }
15380 else if ((w != XWINDOW (minibuf_window)
15381 || minibuf_level == 0)
15382 /* When buffer is nonempty, redisplay window normally. */
15383 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15384 /* Quail displays non-mini buffers in minibuffer window.
15385 In that case, redisplay the window normally. */
15386 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15387 {
15388 /* W is a mini-buffer window, but it's not active, so clear
15389 it. */
15390 int yb = window_text_bottom_y (w);
15391 struct glyph_row *row;
15392 int y;
15393
15394 for (y = 0, row = w->desired_matrix->rows;
15395 y < yb;
15396 y += row->height, ++row)
15397 blank_row (w, row, y);
15398 goto finish_scroll_bars;
15399 }
15400
15401 clear_glyph_matrix (w->desired_matrix);
15402 }
15403
15404 /* Otherwise set up data on this window; select its buffer and point
15405 value. */
15406 /* Really select the buffer, for the sake of buffer-local
15407 variables. */
15408 set_buffer_internal_1 (XBUFFER (w->buffer));
15409
15410 current_matrix_up_to_date_p
15411 = (!NILP (w->window_end_valid)
15412 && !current_buffer->clip_changed
15413 && !current_buffer->prevent_redisplay_optimizations_p
15414 && w->last_modified >= MODIFF
15415 && w->last_overlay_modified >= OVERLAY_MODIFF);
15416
15417 /* Run the window-bottom-change-functions
15418 if it is possible that the text on the screen has changed
15419 (either due to modification of the text, or any other reason). */
15420 if (!current_matrix_up_to_date_p
15421 && !NILP (Vwindow_text_change_functions))
15422 {
15423 safe_run_hooks (Qwindow_text_change_functions);
15424 goto restart;
15425 }
15426
15427 beg_unchanged = BEG_UNCHANGED;
15428 end_unchanged = END_UNCHANGED;
15429
15430 SET_TEXT_POS (opoint, PT, PT_BYTE);
15431
15432 specbind (Qinhibit_point_motion_hooks, Qt);
15433
15434 buffer_unchanged_p
15435 = (!NILP (w->window_end_valid)
15436 && !current_buffer->clip_changed
15437 && w->last_modified >= MODIFF
15438 && w->last_overlay_modified >= OVERLAY_MODIFF);
15439
15440 /* When windows_or_buffers_changed is non-zero, we can't rely on
15441 the window end being valid, so set it to nil there. */
15442 if (windows_or_buffers_changed)
15443 {
15444 /* If window starts on a continuation line, maybe adjust the
15445 window start in case the window's width changed. */
15446 if (XMARKER (w->start)->buffer == current_buffer)
15447 compute_window_start_on_continuation_line (w);
15448
15449 w->window_end_valid = Qnil;
15450 }
15451
15452 /* Some sanity checks. */
15453 CHECK_WINDOW_END (w);
15454 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15455 abort ();
15456 if (BYTEPOS (opoint) < CHARPOS (opoint))
15457 abort ();
15458
15459 /* If %c is in mode line, update it if needed. */
15460 if (!NILP (w->column_number_displayed)
15461 /* This alternative quickly identifies a common case
15462 where no change is needed. */
15463 && !(PT == w->last_point
15464 && w->last_modified >= MODIFF
15465 && w->last_overlay_modified >= OVERLAY_MODIFF)
15466 && (XFASTINT (w->column_number_displayed) != current_column ()))
15467 update_mode_line = 1;
15468
15469 /* Count number of windows showing the selected buffer. An indirect
15470 buffer counts as its base buffer. */
15471 if (!just_this_one_p)
15472 {
15473 struct buffer *current_base, *window_base;
15474 current_base = current_buffer;
15475 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15476 if (current_base->base_buffer)
15477 current_base = current_base->base_buffer;
15478 if (window_base->base_buffer)
15479 window_base = window_base->base_buffer;
15480 if (current_base == window_base)
15481 buffer_shared++;
15482 }
15483
15484 /* Point refers normally to the selected window. For any other
15485 window, set up appropriate value. */
15486 if (!EQ (window, selected_window))
15487 {
15488 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15489 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15490 if (new_pt < BEGV)
15491 {
15492 new_pt = BEGV;
15493 new_pt_byte = BEGV_BYTE;
15494 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15495 }
15496 else if (new_pt > (ZV - 1))
15497 {
15498 new_pt = ZV;
15499 new_pt_byte = ZV_BYTE;
15500 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15501 }
15502
15503 /* We don't use SET_PT so that the point-motion hooks don't run. */
15504 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15505 }
15506
15507 /* If any of the character widths specified in the display table
15508 have changed, invalidate the width run cache. It's true that
15509 this may be a bit late to catch such changes, but the rest of
15510 redisplay goes (non-fatally) haywire when the display table is
15511 changed, so why should we worry about doing any better? */
15512 if (current_buffer->width_run_cache)
15513 {
15514 struct Lisp_Char_Table *disptab = buffer_display_table ();
15515
15516 if (! disptab_matches_widthtab (disptab,
15517 XVECTOR (BVAR (current_buffer, width_table))))
15518 {
15519 invalidate_region_cache (current_buffer,
15520 current_buffer->width_run_cache,
15521 BEG, Z);
15522 recompute_width_table (current_buffer, disptab);
15523 }
15524 }
15525
15526 /* If window-start is screwed up, choose a new one. */
15527 if (XMARKER (w->start)->buffer != current_buffer)
15528 goto recenter;
15529
15530 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15531
15532 /* If someone specified a new starting point but did not insist,
15533 check whether it can be used. */
15534 if (w->optional_new_start
15535 && CHARPOS (startp) >= BEGV
15536 && CHARPOS (startp) <= ZV)
15537 {
15538 w->optional_new_start = 0;
15539 start_display (&it, w, startp);
15540 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15541 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15542 if (IT_CHARPOS (it) == PT)
15543 w->force_start = 1;
15544 /* IT may overshoot PT if text at PT is invisible. */
15545 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15546 w->force_start = 1;
15547 }
15548
15549 force_start:
15550
15551 /* Handle case where place to start displaying has been specified,
15552 unless the specified location is outside the accessible range. */
15553 if (w->force_start || w->frozen_window_start_p)
15554 {
15555 /* We set this later on if we have to adjust point. */
15556 int new_vpos = -1;
15557
15558 w->force_start = 0;
15559 w->vscroll = 0;
15560 w->window_end_valid = Qnil;
15561
15562 /* Forget any recorded base line for line number display. */
15563 if (!buffer_unchanged_p)
15564 w->base_line_number = Qnil;
15565
15566 /* Redisplay the mode line. Select the buffer properly for that.
15567 Also, run the hook window-scroll-functions
15568 because we have scrolled. */
15569 /* Note, we do this after clearing force_start because
15570 if there's an error, it is better to forget about force_start
15571 than to get into an infinite loop calling the hook functions
15572 and having them get more errors. */
15573 if (!update_mode_line
15574 || ! NILP (Vwindow_scroll_functions))
15575 {
15576 update_mode_line = 1;
15577 w->update_mode_line = 1;
15578 startp = run_window_scroll_functions (window, startp);
15579 }
15580
15581 w->last_modified = 0;
15582 w->last_overlay_modified = 0;
15583 if (CHARPOS (startp) < BEGV)
15584 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15585 else if (CHARPOS (startp) > ZV)
15586 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15587
15588 /* Redisplay, then check if cursor has been set during the
15589 redisplay. Give up if new fonts were loaded. */
15590 /* We used to issue a CHECK_MARGINS argument to try_window here,
15591 but this causes scrolling to fail when point begins inside
15592 the scroll margin (bug#148) -- cyd */
15593 if (!try_window (window, startp, 0))
15594 {
15595 w->force_start = 1;
15596 clear_glyph_matrix (w->desired_matrix);
15597 goto need_larger_matrices;
15598 }
15599
15600 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15601 {
15602 /* If point does not appear, try to move point so it does
15603 appear. The desired matrix has been built above, so we
15604 can use it here. */
15605 new_vpos = window_box_height (w) / 2;
15606 }
15607
15608 if (!cursor_row_fully_visible_p (w, 0, 0))
15609 {
15610 /* Point does appear, but on a line partly visible at end of window.
15611 Move it back to a fully-visible line. */
15612 new_vpos = window_box_height (w);
15613 }
15614
15615 /* If we need to move point for either of the above reasons,
15616 now actually do it. */
15617 if (new_vpos >= 0)
15618 {
15619 struct glyph_row *row;
15620
15621 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15622 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15623 ++row;
15624
15625 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15626 MATRIX_ROW_START_BYTEPOS (row));
15627
15628 if (w != XWINDOW (selected_window))
15629 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15630 else if (current_buffer == old)
15631 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15632
15633 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15634
15635 /* If we are highlighting the region, then we just changed
15636 the region, so redisplay to show it. */
15637 if (!NILP (Vtransient_mark_mode)
15638 && !NILP (BVAR (current_buffer, mark_active)))
15639 {
15640 clear_glyph_matrix (w->desired_matrix);
15641 if (!try_window (window, startp, 0))
15642 goto need_larger_matrices;
15643 }
15644 }
15645
15646 #ifdef GLYPH_DEBUG
15647 debug_method_add (w, "forced window start");
15648 #endif
15649 goto done;
15650 }
15651
15652 /* Handle case where text has not changed, only point, and it has
15653 not moved off the frame, and we are not retrying after hscroll.
15654 (current_matrix_up_to_date_p is nonzero when retrying.) */
15655 if (current_matrix_up_to_date_p
15656 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15657 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15658 {
15659 switch (rc)
15660 {
15661 case CURSOR_MOVEMENT_SUCCESS:
15662 used_current_matrix_p = 1;
15663 goto done;
15664
15665 case CURSOR_MOVEMENT_MUST_SCROLL:
15666 goto try_to_scroll;
15667
15668 default:
15669 abort ();
15670 }
15671 }
15672 /* If current starting point was originally the beginning of a line
15673 but no longer is, find a new starting point. */
15674 else if (w->start_at_line_beg
15675 && !(CHARPOS (startp) <= BEGV
15676 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15677 {
15678 #ifdef GLYPH_DEBUG
15679 debug_method_add (w, "recenter 1");
15680 #endif
15681 goto recenter;
15682 }
15683
15684 /* Try scrolling with try_window_id. Value is > 0 if update has
15685 been done, it is -1 if we know that the same window start will
15686 not work. It is 0 if unsuccessful for some other reason. */
15687 else if ((tem = try_window_id (w)) != 0)
15688 {
15689 #ifdef GLYPH_DEBUG
15690 debug_method_add (w, "try_window_id %d", tem);
15691 #endif
15692
15693 if (fonts_changed_p)
15694 goto need_larger_matrices;
15695 if (tem > 0)
15696 goto done;
15697
15698 /* Otherwise try_window_id has returned -1 which means that we
15699 don't want the alternative below this comment to execute. */
15700 }
15701 else if (CHARPOS (startp) >= BEGV
15702 && CHARPOS (startp) <= ZV
15703 && PT >= CHARPOS (startp)
15704 && (CHARPOS (startp) < ZV
15705 /* Avoid starting at end of buffer. */
15706 || CHARPOS (startp) == BEGV
15707 || (w->last_modified >= MODIFF
15708 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15709 {
15710 int d1, d2, d3, d4, d5, d6;
15711
15712 /* If first window line is a continuation line, and window start
15713 is inside the modified region, but the first change is before
15714 current window start, we must select a new window start.
15715
15716 However, if this is the result of a down-mouse event (e.g. by
15717 extending the mouse-drag-overlay), we don't want to select a
15718 new window start, since that would change the position under
15719 the mouse, resulting in an unwanted mouse-movement rather
15720 than a simple mouse-click. */
15721 if (!w->start_at_line_beg
15722 && NILP (do_mouse_tracking)
15723 && CHARPOS (startp) > BEGV
15724 && CHARPOS (startp) > BEG + beg_unchanged
15725 && CHARPOS (startp) <= Z - end_unchanged
15726 /* Even if w->start_at_line_beg is nil, a new window may
15727 start at a line_beg, since that's how set_buffer_window
15728 sets it. So, we need to check the return value of
15729 compute_window_start_on_continuation_line. (See also
15730 bug#197). */
15731 && XMARKER (w->start)->buffer == current_buffer
15732 && compute_window_start_on_continuation_line (w)
15733 /* It doesn't make sense to force the window start like we
15734 do at label force_start if it is already known that point
15735 will not be visible in the resulting window, because
15736 doing so will move point from its correct position
15737 instead of scrolling the window to bring point into view.
15738 See bug#9324. */
15739 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15740 {
15741 w->force_start = 1;
15742 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15743 goto force_start;
15744 }
15745
15746 #ifdef GLYPH_DEBUG
15747 debug_method_add (w, "same window start");
15748 #endif
15749
15750 /* Try to redisplay starting at same place as before.
15751 If point has not moved off frame, accept the results. */
15752 if (!current_matrix_up_to_date_p
15753 /* Don't use try_window_reusing_current_matrix in this case
15754 because a window scroll function can have changed the
15755 buffer. */
15756 || !NILP (Vwindow_scroll_functions)
15757 || MINI_WINDOW_P (w)
15758 || !(used_current_matrix_p
15759 = try_window_reusing_current_matrix (w)))
15760 {
15761 IF_DEBUG (debug_method_add (w, "1"));
15762 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15763 /* -1 means we need to scroll.
15764 0 means we need new matrices, but fonts_changed_p
15765 is set in that case, so we will detect it below. */
15766 goto try_to_scroll;
15767 }
15768
15769 if (fonts_changed_p)
15770 goto need_larger_matrices;
15771
15772 if (w->cursor.vpos >= 0)
15773 {
15774 if (!just_this_one_p
15775 || current_buffer->clip_changed
15776 || BEG_UNCHANGED < CHARPOS (startp))
15777 /* Forget any recorded base line for line number display. */
15778 w->base_line_number = Qnil;
15779
15780 if (!cursor_row_fully_visible_p (w, 1, 0))
15781 {
15782 clear_glyph_matrix (w->desired_matrix);
15783 last_line_misfit = 1;
15784 }
15785 /* Drop through and scroll. */
15786 else
15787 goto done;
15788 }
15789 else
15790 clear_glyph_matrix (w->desired_matrix);
15791 }
15792
15793 try_to_scroll:
15794
15795 w->last_modified = 0;
15796 w->last_overlay_modified = 0;
15797
15798 /* Redisplay the mode line. Select the buffer properly for that. */
15799 if (!update_mode_line)
15800 {
15801 update_mode_line = 1;
15802 w->update_mode_line = 1;
15803 }
15804
15805 /* Try to scroll by specified few lines. */
15806 if ((scroll_conservatively
15807 || emacs_scroll_step
15808 || temp_scroll_step
15809 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15810 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15811 && CHARPOS (startp) >= BEGV
15812 && CHARPOS (startp) <= ZV)
15813 {
15814 /* The function returns -1 if new fonts were loaded, 1 if
15815 successful, 0 if not successful. */
15816 int ss = try_scrolling (window, just_this_one_p,
15817 scroll_conservatively,
15818 emacs_scroll_step,
15819 temp_scroll_step, last_line_misfit);
15820 switch (ss)
15821 {
15822 case SCROLLING_SUCCESS:
15823 goto done;
15824
15825 case SCROLLING_NEED_LARGER_MATRICES:
15826 goto need_larger_matrices;
15827
15828 case SCROLLING_FAILED:
15829 break;
15830
15831 default:
15832 abort ();
15833 }
15834 }
15835
15836 /* Finally, just choose a place to start which positions point
15837 according to user preferences. */
15838
15839 recenter:
15840
15841 #ifdef GLYPH_DEBUG
15842 debug_method_add (w, "recenter");
15843 #endif
15844
15845 /* w->vscroll = 0; */
15846
15847 /* Forget any previously recorded base line for line number display. */
15848 if (!buffer_unchanged_p)
15849 w->base_line_number = Qnil;
15850
15851 /* Determine the window start relative to point. */
15852 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15853 it.current_y = it.last_visible_y;
15854 if (centering_position < 0)
15855 {
15856 int margin =
15857 scroll_margin > 0
15858 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15859 : 0;
15860 ptrdiff_t margin_pos = CHARPOS (startp);
15861 Lisp_Object aggressive;
15862 int scrolling_up;
15863
15864 /* If there is a scroll margin at the top of the window, find
15865 its character position. */
15866 if (margin
15867 /* Cannot call start_display if startp is not in the
15868 accessible region of the buffer. This can happen when we
15869 have just switched to a different buffer and/or changed
15870 its restriction. In that case, startp is initialized to
15871 the character position 1 (BEGV) because we did not yet
15872 have chance to display the buffer even once. */
15873 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15874 {
15875 struct it it1;
15876 void *it1data = NULL;
15877
15878 SAVE_IT (it1, it, it1data);
15879 start_display (&it1, w, startp);
15880 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15881 margin_pos = IT_CHARPOS (it1);
15882 RESTORE_IT (&it, &it, it1data);
15883 }
15884 scrolling_up = PT > margin_pos;
15885 aggressive =
15886 scrolling_up
15887 ? BVAR (current_buffer, scroll_up_aggressively)
15888 : BVAR (current_buffer, scroll_down_aggressively);
15889
15890 if (!MINI_WINDOW_P (w)
15891 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15892 {
15893 int pt_offset = 0;
15894
15895 /* Setting scroll-conservatively overrides
15896 scroll-*-aggressively. */
15897 if (!scroll_conservatively && NUMBERP (aggressive))
15898 {
15899 double float_amount = XFLOATINT (aggressive);
15900
15901 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15902 if (pt_offset == 0 && float_amount > 0)
15903 pt_offset = 1;
15904 if (pt_offset && margin > 0)
15905 margin -= 1;
15906 }
15907 /* Compute how much to move the window start backward from
15908 point so that point will be displayed where the user
15909 wants it. */
15910 if (scrolling_up)
15911 {
15912 centering_position = it.last_visible_y;
15913 if (pt_offset)
15914 centering_position -= pt_offset;
15915 centering_position -=
15916 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15917 + WINDOW_HEADER_LINE_HEIGHT (w);
15918 /* Don't let point enter the scroll margin near top of
15919 the window. */
15920 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15921 centering_position = margin * FRAME_LINE_HEIGHT (f);
15922 }
15923 else
15924 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15925 }
15926 else
15927 /* Set the window start half the height of the window backward
15928 from point. */
15929 centering_position = window_box_height (w) / 2;
15930 }
15931 move_it_vertically_backward (&it, centering_position);
15932
15933 eassert (IT_CHARPOS (it) >= BEGV);
15934
15935 /* The function move_it_vertically_backward may move over more
15936 than the specified y-distance. If it->w is small, e.g. a
15937 mini-buffer window, we may end up in front of the window's
15938 display area. Start displaying at the start of the line
15939 containing PT in this case. */
15940 if (it.current_y <= 0)
15941 {
15942 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15943 move_it_vertically_backward (&it, 0);
15944 it.current_y = 0;
15945 }
15946
15947 it.current_x = it.hpos = 0;
15948
15949 /* Set the window start position here explicitly, to avoid an
15950 infinite loop in case the functions in window-scroll-functions
15951 get errors. */
15952 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15953
15954 /* Run scroll hooks. */
15955 startp = run_window_scroll_functions (window, it.current.pos);
15956
15957 /* Redisplay the window. */
15958 if (!current_matrix_up_to_date_p
15959 || windows_or_buffers_changed
15960 || cursor_type_changed
15961 /* Don't use try_window_reusing_current_matrix in this case
15962 because it can have changed the buffer. */
15963 || !NILP (Vwindow_scroll_functions)
15964 || !just_this_one_p
15965 || MINI_WINDOW_P (w)
15966 || !(used_current_matrix_p
15967 = try_window_reusing_current_matrix (w)))
15968 try_window (window, startp, 0);
15969
15970 /* If new fonts have been loaded (due to fontsets), give up. We
15971 have to start a new redisplay since we need to re-adjust glyph
15972 matrices. */
15973 if (fonts_changed_p)
15974 goto need_larger_matrices;
15975
15976 /* If cursor did not appear assume that the middle of the window is
15977 in the first line of the window. Do it again with the next line.
15978 (Imagine a window of height 100, displaying two lines of height
15979 60. Moving back 50 from it->last_visible_y will end in the first
15980 line.) */
15981 if (w->cursor.vpos < 0)
15982 {
15983 if (!NILP (w->window_end_valid)
15984 && PT >= Z - XFASTINT (w->window_end_pos))
15985 {
15986 clear_glyph_matrix (w->desired_matrix);
15987 move_it_by_lines (&it, 1);
15988 try_window (window, it.current.pos, 0);
15989 }
15990 else if (PT < IT_CHARPOS (it))
15991 {
15992 clear_glyph_matrix (w->desired_matrix);
15993 move_it_by_lines (&it, -1);
15994 try_window (window, it.current.pos, 0);
15995 }
15996 else
15997 {
15998 /* Not much we can do about it. */
15999 }
16000 }
16001
16002 /* Consider the following case: Window starts at BEGV, there is
16003 invisible, intangible text at BEGV, so that display starts at
16004 some point START > BEGV. It can happen that we are called with
16005 PT somewhere between BEGV and START. Try to handle that case. */
16006 if (w->cursor.vpos < 0)
16007 {
16008 struct glyph_row *row = w->current_matrix->rows;
16009 if (row->mode_line_p)
16010 ++row;
16011 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16012 }
16013
16014 if (!cursor_row_fully_visible_p (w, 0, 0))
16015 {
16016 /* If vscroll is enabled, disable it and try again. */
16017 if (w->vscroll)
16018 {
16019 w->vscroll = 0;
16020 clear_glyph_matrix (w->desired_matrix);
16021 goto recenter;
16022 }
16023
16024 /* Users who set scroll-conservatively to a large number want
16025 point just above/below the scroll margin. If we ended up
16026 with point's row partially visible, move the window start to
16027 make that row fully visible and out of the margin. */
16028 if (scroll_conservatively > SCROLL_LIMIT)
16029 {
16030 int margin =
16031 scroll_margin > 0
16032 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16033 : 0;
16034 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16035
16036 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16037 clear_glyph_matrix (w->desired_matrix);
16038 if (1 == try_window (window, it.current.pos,
16039 TRY_WINDOW_CHECK_MARGINS))
16040 goto done;
16041 }
16042
16043 /* If centering point failed to make the whole line visible,
16044 put point at the top instead. That has to make the whole line
16045 visible, if it can be done. */
16046 if (centering_position == 0)
16047 goto done;
16048
16049 clear_glyph_matrix (w->desired_matrix);
16050 centering_position = 0;
16051 goto recenter;
16052 }
16053
16054 done:
16055
16056 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16057 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16058 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16059
16060 /* Display the mode line, if we must. */
16061 if ((update_mode_line
16062 /* If window not full width, must redo its mode line
16063 if (a) the window to its side is being redone and
16064 (b) we do a frame-based redisplay. This is a consequence
16065 of how inverted lines are drawn in frame-based redisplay. */
16066 || (!just_this_one_p
16067 && !FRAME_WINDOW_P (f)
16068 && !WINDOW_FULL_WIDTH_P (w))
16069 /* Line number to display. */
16070 || INTEGERP (w->base_line_pos)
16071 /* Column number is displayed and different from the one displayed. */
16072 || (!NILP (w->column_number_displayed)
16073 && (XFASTINT (w->column_number_displayed) != current_column ())))
16074 /* This means that the window has a mode line. */
16075 && (WINDOW_WANTS_MODELINE_P (w)
16076 || WINDOW_WANTS_HEADER_LINE_P (w)))
16077 {
16078 display_mode_lines (w);
16079
16080 /* If mode line height has changed, arrange for a thorough
16081 immediate redisplay using the correct mode line height. */
16082 if (WINDOW_WANTS_MODELINE_P (w)
16083 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16084 {
16085 fonts_changed_p = 1;
16086 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16087 = DESIRED_MODE_LINE_HEIGHT (w);
16088 }
16089
16090 /* If header line height has changed, arrange for a thorough
16091 immediate redisplay using the correct header line height. */
16092 if (WINDOW_WANTS_HEADER_LINE_P (w)
16093 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16094 {
16095 fonts_changed_p = 1;
16096 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16097 = DESIRED_HEADER_LINE_HEIGHT (w);
16098 }
16099
16100 if (fonts_changed_p)
16101 goto need_larger_matrices;
16102 }
16103
16104 if (!line_number_displayed
16105 && !BUFFERP (w->base_line_pos))
16106 {
16107 w->base_line_pos = Qnil;
16108 w->base_line_number = Qnil;
16109 }
16110
16111 finish_menu_bars:
16112
16113 /* When we reach a frame's selected window, redo the frame's menu bar. */
16114 if (update_mode_line
16115 && EQ (FRAME_SELECTED_WINDOW (f), window))
16116 {
16117 int redisplay_menu_p = 0;
16118
16119 if (FRAME_WINDOW_P (f))
16120 {
16121 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16122 || defined (HAVE_NS) || defined (USE_GTK)
16123 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16124 #else
16125 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16126 #endif
16127 }
16128 else
16129 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16130
16131 if (redisplay_menu_p)
16132 display_menu_bar (w);
16133
16134 #ifdef HAVE_WINDOW_SYSTEM
16135 if (FRAME_WINDOW_P (f))
16136 {
16137 #if defined (USE_GTK) || defined (HAVE_NS)
16138 if (FRAME_EXTERNAL_TOOL_BAR (f))
16139 redisplay_tool_bar (f);
16140 #else
16141 if (WINDOWP (f->tool_bar_window)
16142 && (FRAME_TOOL_BAR_LINES (f) > 0
16143 || !NILP (Vauto_resize_tool_bars))
16144 && redisplay_tool_bar (f))
16145 ignore_mouse_drag_p = 1;
16146 #endif
16147 }
16148 #endif
16149 }
16150
16151 #ifdef HAVE_WINDOW_SYSTEM
16152 if (FRAME_WINDOW_P (f)
16153 && update_window_fringes (w, (just_this_one_p
16154 || (!used_current_matrix_p && !overlay_arrow_seen)
16155 || w->pseudo_window_p)))
16156 {
16157 update_begin (f);
16158 BLOCK_INPUT;
16159 if (draw_window_fringes (w, 1))
16160 x_draw_vertical_border (w);
16161 UNBLOCK_INPUT;
16162 update_end (f);
16163 }
16164 #endif /* HAVE_WINDOW_SYSTEM */
16165
16166 /* We go to this label, with fonts_changed_p nonzero,
16167 if it is necessary to try again using larger glyph matrices.
16168 We have to redeem the scroll bar even in this case,
16169 because the loop in redisplay_internal expects that. */
16170 need_larger_matrices:
16171 ;
16172 finish_scroll_bars:
16173
16174 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16175 {
16176 /* Set the thumb's position and size. */
16177 set_vertical_scroll_bar (w);
16178
16179 /* Note that we actually used the scroll bar attached to this
16180 window, so it shouldn't be deleted at the end of redisplay. */
16181 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16182 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16183 }
16184
16185 /* Restore current_buffer and value of point in it. The window
16186 update may have changed the buffer, so first make sure `opoint'
16187 is still valid (Bug#6177). */
16188 if (CHARPOS (opoint) < BEGV)
16189 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16190 else if (CHARPOS (opoint) > ZV)
16191 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16192 else
16193 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16194
16195 set_buffer_internal_1 (old);
16196 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16197 shorter. This can be caused by log truncation in *Messages*. */
16198 if (CHARPOS (lpoint) <= ZV)
16199 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16200
16201 unbind_to (count, Qnil);
16202 }
16203
16204
16205 /* Build the complete desired matrix of WINDOW with a window start
16206 buffer position POS.
16207
16208 Value is 1 if successful. It is zero if fonts were loaded during
16209 redisplay which makes re-adjusting glyph matrices necessary, and -1
16210 if point would appear in the scroll margins.
16211 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16212 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16213 set in FLAGS.) */
16214
16215 int
16216 try_window (Lisp_Object window, struct text_pos pos, int flags)
16217 {
16218 struct window *w = XWINDOW (window);
16219 struct it it;
16220 struct glyph_row *last_text_row = NULL;
16221 struct frame *f = XFRAME (w->frame);
16222
16223 /* Make POS the new window start. */
16224 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16225
16226 /* Mark cursor position as unknown. No overlay arrow seen. */
16227 w->cursor.vpos = -1;
16228 overlay_arrow_seen = 0;
16229
16230 /* Initialize iterator and info to start at POS. */
16231 start_display (&it, w, pos);
16232
16233 /* Display all lines of W. */
16234 while (it.current_y < it.last_visible_y)
16235 {
16236 if (display_line (&it))
16237 last_text_row = it.glyph_row - 1;
16238 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16239 return 0;
16240 }
16241
16242 /* Don't let the cursor end in the scroll margins. */
16243 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16244 && !MINI_WINDOW_P (w))
16245 {
16246 int this_scroll_margin;
16247
16248 if (scroll_margin > 0)
16249 {
16250 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16251 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16252 }
16253 else
16254 this_scroll_margin = 0;
16255
16256 if ((w->cursor.y >= 0 /* not vscrolled */
16257 && w->cursor.y < this_scroll_margin
16258 && CHARPOS (pos) > BEGV
16259 && IT_CHARPOS (it) < ZV)
16260 /* rms: considering make_cursor_line_fully_visible_p here
16261 seems to give wrong results. We don't want to recenter
16262 when the last line is partly visible, we want to allow
16263 that case to be handled in the usual way. */
16264 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16265 {
16266 w->cursor.vpos = -1;
16267 clear_glyph_matrix (w->desired_matrix);
16268 return -1;
16269 }
16270 }
16271
16272 /* If bottom moved off end of frame, change mode line percentage. */
16273 if (XFASTINT (w->window_end_pos) <= 0
16274 && Z != IT_CHARPOS (it))
16275 w->update_mode_line = 1;
16276
16277 /* Set window_end_pos to the offset of the last character displayed
16278 on the window from the end of current_buffer. Set
16279 window_end_vpos to its row number. */
16280 if (last_text_row)
16281 {
16282 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16283 w->window_end_bytepos
16284 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16285 w->window_end_pos
16286 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16287 w->window_end_vpos
16288 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16289 eassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16290 ->displays_text_p);
16291 }
16292 else
16293 {
16294 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16295 w->window_end_pos = make_number (Z - ZV);
16296 w->window_end_vpos = make_number (0);
16297 }
16298
16299 /* But that is not valid info until redisplay finishes. */
16300 w->window_end_valid = Qnil;
16301 return 1;
16302 }
16303
16304
16305 \f
16306 /************************************************************************
16307 Window redisplay reusing current matrix when buffer has not changed
16308 ************************************************************************/
16309
16310 /* Try redisplay of window W showing an unchanged buffer with a
16311 different window start than the last time it was displayed by
16312 reusing its current matrix. Value is non-zero if successful.
16313 W->start is the new window start. */
16314
16315 static int
16316 try_window_reusing_current_matrix (struct window *w)
16317 {
16318 struct frame *f = XFRAME (w->frame);
16319 struct glyph_row *bottom_row;
16320 struct it it;
16321 struct run run;
16322 struct text_pos start, new_start;
16323 int nrows_scrolled, i;
16324 struct glyph_row *last_text_row;
16325 struct glyph_row *last_reused_text_row;
16326 struct glyph_row *start_row;
16327 int start_vpos, min_y, max_y;
16328
16329 #ifdef GLYPH_DEBUG
16330 if (inhibit_try_window_reusing)
16331 return 0;
16332 #endif
16333
16334 if (/* This function doesn't handle terminal frames. */
16335 !FRAME_WINDOW_P (f)
16336 /* Don't try to reuse the display if windows have been split
16337 or such. */
16338 || windows_or_buffers_changed
16339 || cursor_type_changed)
16340 return 0;
16341
16342 /* Can't do this if region may have changed. */
16343 if ((!NILP (Vtransient_mark_mode)
16344 && !NILP (BVAR (current_buffer, mark_active)))
16345 || !NILP (w->region_showing)
16346 || !NILP (Vshow_trailing_whitespace))
16347 return 0;
16348
16349 /* If top-line visibility has changed, give up. */
16350 if (WINDOW_WANTS_HEADER_LINE_P (w)
16351 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16352 return 0;
16353
16354 /* Give up if old or new display is scrolled vertically. We could
16355 make this function handle this, but right now it doesn't. */
16356 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16357 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16358 return 0;
16359
16360 /* The variable new_start now holds the new window start. The old
16361 start `start' can be determined from the current matrix. */
16362 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16363 start = start_row->minpos;
16364 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16365
16366 /* Clear the desired matrix for the display below. */
16367 clear_glyph_matrix (w->desired_matrix);
16368
16369 if (CHARPOS (new_start) <= CHARPOS (start))
16370 {
16371 /* Don't use this method if the display starts with an ellipsis
16372 displayed for invisible text. It's not easy to handle that case
16373 below, and it's certainly not worth the effort since this is
16374 not a frequent case. */
16375 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16376 return 0;
16377
16378 IF_DEBUG (debug_method_add (w, "twu1"));
16379
16380 /* Display up to a row that can be reused. The variable
16381 last_text_row is set to the last row displayed that displays
16382 text. Note that it.vpos == 0 if or if not there is a
16383 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16384 start_display (&it, w, new_start);
16385 w->cursor.vpos = -1;
16386 last_text_row = last_reused_text_row = NULL;
16387
16388 while (it.current_y < it.last_visible_y
16389 && !fonts_changed_p)
16390 {
16391 /* If we have reached into the characters in the START row,
16392 that means the line boundaries have changed. So we
16393 can't start copying with the row START. Maybe it will
16394 work to start copying with the following row. */
16395 while (IT_CHARPOS (it) > CHARPOS (start))
16396 {
16397 /* Advance to the next row as the "start". */
16398 start_row++;
16399 start = start_row->minpos;
16400 /* If there are no more rows to try, or just one, give up. */
16401 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16402 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16403 || CHARPOS (start) == ZV)
16404 {
16405 clear_glyph_matrix (w->desired_matrix);
16406 return 0;
16407 }
16408
16409 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16410 }
16411 /* If we have reached alignment, we can copy the rest of the
16412 rows. */
16413 if (IT_CHARPOS (it) == CHARPOS (start)
16414 /* Don't accept "alignment" inside a display vector,
16415 since start_row could have started in the middle of
16416 that same display vector (thus their character
16417 positions match), and we have no way of telling if
16418 that is the case. */
16419 && it.current.dpvec_index < 0)
16420 break;
16421
16422 if (display_line (&it))
16423 last_text_row = it.glyph_row - 1;
16424
16425 }
16426
16427 /* A value of current_y < last_visible_y means that we stopped
16428 at the previous window start, which in turn means that we
16429 have at least one reusable row. */
16430 if (it.current_y < it.last_visible_y)
16431 {
16432 struct glyph_row *row;
16433
16434 /* IT.vpos always starts from 0; it counts text lines. */
16435 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16436
16437 /* Find PT if not already found in the lines displayed. */
16438 if (w->cursor.vpos < 0)
16439 {
16440 int dy = it.current_y - start_row->y;
16441
16442 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16443 row = row_containing_pos (w, PT, row, NULL, dy);
16444 if (row)
16445 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16446 dy, nrows_scrolled);
16447 else
16448 {
16449 clear_glyph_matrix (w->desired_matrix);
16450 return 0;
16451 }
16452 }
16453
16454 /* Scroll the display. Do it before the current matrix is
16455 changed. The problem here is that update has not yet
16456 run, i.e. part of the current matrix is not up to date.
16457 scroll_run_hook will clear the cursor, and use the
16458 current matrix to get the height of the row the cursor is
16459 in. */
16460 run.current_y = start_row->y;
16461 run.desired_y = it.current_y;
16462 run.height = it.last_visible_y - it.current_y;
16463
16464 if (run.height > 0 && run.current_y != run.desired_y)
16465 {
16466 update_begin (f);
16467 FRAME_RIF (f)->update_window_begin_hook (w);
16468 FRAME_RIF (f)->clear_window_mouse_face (w);
16469 FRAME_RIF (f)->scroll_run_hook (w, &run);
16470 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16471 update_end (f);
16472 }
16473
16474 /* Shift current matrix down by nrows_scrolled lines. */
16475 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16476 rotate_matrix (w->current_matrix,
16477 start_vpos,
16478 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16479 nrows_scrolled);
16480
16481 /* Disable lines that must be updated. */
16482 for (i = 0; i < nrows_scrolled; ++i)
16483 (start_row + i)->enabled_p = 0;
16484
16485 /* Re-compute Y positions. */
16486 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16487 max_y = it.last_visible_y;
16488 for (row = start_row + nrows_scrolled;
16489 row < bottom_row;
16490 ++row)
16491 {
16492 row->y = it.current_y;
16493 row->visible_height = row->height;
16494
16495 if (row->y < min_y)
16496 row->visible_height -= min_y - row->y;
16497 if (row->y + row->height > max_y)
16498 row->visible_height -= row->y + row->height - max_y;
16499 if (row->fringe_bitmap_periodic_p)
16500 row->redraw_fringe_bitmaps_p = 1;
16501
16502 it.current_y += row->height;
16503
16504 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16505 last_reused_text_row = row;
16506 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16507 break;
16508 }
16509
16510 /* Disable lines in the current matrix which are now
16511 below the window. */
16512 for (++row; row < bottom_row; ++row)
16513 row->enabled_p = row->mode_line_p = 0;
16514 }
16515
16516 /* Update window_end_pos etc.; last_reused_text_row is the last
16517 reused row from the current matrix containing text, if any.
16518 The value of last_text_row is the last displayed line
16519 containing text. */
16520 if (last_reused_text_row)
16521 {
16522 w->window_end_bytepos
16523 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16524 w->window_end_pos
16525 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16526 w->window_end_vpos
16527 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16528 w->current_matrix));
16529 }
16530 else if (last_text_row)
16531 {
16532 w->window_end_bytepos
16533 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16534 w->window_end_pos
16535 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16536 w->window_end_vpos
16537 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16538 }
16539 else
16540 {
16541 /* This window must be completely empty. */
16542 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16543 w->window_end_pos = make_number (Z - ZV);
16544 w->window_end_vpos = make_number (0);
16545 }
16546 w->window_end_valid = Qnil;
16547
16548 /* Update hint: don't try scrolling again in update_window. */
16549 w->desired_matrix->no_scrolling_p = 1;
16550
16551 #ifdef GLYPH_DEBUG
16552 debug_method_add (w, "try_window_reusing_current_matrix 1");
16553 #endif
16554 return 1;
16555 }
16556 else if (CHARPOS (new_start) > CHARPOS (start))
16557 {
16558 struct glyph_row *pt_row, *row;
16559 struct glyph_row *first_reusable_row;
16560 struct glyph_row *first_row_to_display;
16561 int dy;
16562 int yb = window_text_bottom_y (w);
16563
16564 /* Find the row starting at new_start, if there is one. Don't
16565 reuse a partially visible line at the end. */
16566 first_reusable_row = start_row;
16567 while (first_reusable_row->enabled_p
16568 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16569 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16570 < CHARPOS (new_start)))
16571 ++first_reusable_row;
16572
16573 /* Give up if there is no row to reuse. */
16574 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16575 || !first_reusable_row->enabled_p
16576 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16577 != CHARPOS (new_start)))
16578 return 0;
16579
16580 /* We can reuse fully visible rows beginning with
16581 first_reusable_row to the end of the window. Set
16582 first_row_to_display to the first row that cannot be reused.
16583 Set pt_row to the row containing point, if there is any. */
16584 pt_row = NULL;
16585 for (first_row_to_display = first_reusable_row;
16586 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16587 ++first_row_to_display)
16588 {
16589 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16590 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16591 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16592 && first_row_to_display->ends_at_zv_p
16593 && pt_row == NULL)))
16594 pt_row = first_row_to_display;
16595 }
16596
16597 /* Start displaying at the start of first_row_to_display. */
16598 eassert (first_row_to_display->y < yb);
16599 init_to_row_start (&it, w, first_row_to_display);
16600
16601 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16602 - start_vpos);
16603 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16604 - nrows_scrolled);
16605 it.current_y = (first_row_to_display->y - first_reusable_row->y
16606 + WINDOW_HEADER_LINE_HEIGHT (w));
16607
16608 /* Display lines beginning with first_row_to_display in the
16609 desired matrix. Set last_text_row to the last row displayed
16610 that displays text. */
16611 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16612 if (pt_row == NULL)
16613 w->cursor.vpos = -1;
16614 last_text_row = NULL;
16615 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16616 if (display_line (&it))
16617 last_text_row = it.glyph_row - 1;
16618
16619 /* If point is in a reused row, adjust y and vpos of the cursor
16620 position. */
16621 if (pt_row)
16622 {
16623 w->cursor.vpos -= nrows_scrolled;
16624 w->cursor.y -= first_reusable_row->y - start_row->y;
16625 }
16626
16627 /* Give up if point isn't in a row displayed or reused. (This
16628 also handles the case where w->cursor.vpos < nrows_scrolled
16629 after the calls to display_line, which can happen with scroll
16630 margins. See bug#1295.) */
16631 if (w->cursor.vpos < 0)
16632 {
16633 clear_glyph_matrix (w->desired_matrix);
16634 return 0;
16635 }
16636
16637 /* Scroll the display. */
16638 run.current_y = first_reusable_row->y;
16639 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16640 run.height = it.last_visible_y - run.current_y;
16641 dy = run.current_y - run.desired_y;
16642
16643 if (run.height)
16644 {
16645 update_begin (f);
16646 FRAME_RIF (f)->update_window_begin_hook (w);
16647 FRAME_RIF (f)->clear_window_mouse_face (w);
16648 FRAME_RIF (f)->scroll_run_hook (w, &run);
16649 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16650 update_end (f);
16651 }
16652
16653 /* Adjust Y positions of reused rows. */
16654 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16655 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16656 max_y = it.last_visible_y;
16657 for (row = first_reusable_row; row < first_row_to_display; ++row)
16658 {
16659 row->y -= dy;
16660 row->visible_height = row->height;
16661 if (row->y < min_y)
16662 row->visible_height -= min_y - row->y;
16663 if (row->y + row->height > max_y)
16664 row->visible_height -= row->y + row->height - max_y;
16665 if (row->fringe_bitmap_periodic_p)
16666 row->redraw_fringe_bitmaps_p = 1;
16667 }
16668
16669 /* Scroll the current matrix. */
16670 eassert (nrows_scrolled > 0);
16671 rotate_matrix (w->current_matrix,
16672 start_vpos,
16673 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16674 -nrows_scrolled);
16675
16676 /* Disable rows not reused. */
16677 for (row -= nrows_scrolled; row < bottom_row; ++row)
16678 row->enabled_p = 0;
16679
16680 /* Point may have moved to a different line, so we cannot assume that
16681 the previous cursor position is valid; locate the correct row. */
16682 if (pt_row)
16683 {
16684 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16685 row < bottom_row
16686 && PT >= MATRIX_ROW_END_CHARPOS (row)
16687 && !row->ends_at_zv_p;
16688 row++)
16689 {
16690 w->cursor.vpos++;
16691 w->cursor.y = row->y;
16692 }
16693 if (row < bottom_row)
16694 {
16695 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16696 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16697
16698 /* Can't use this optimization with bidi-reordered glyph
16699 rows, unless cursor is already at point. */
16700 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16701 {
16702 if (!(w->cursor.hpos >= 0
16703 && w->cursor.hpos < row->used[TEXT_AREA]
16704 && BUFFERP (glyph->object)
16705 && glyph->charpos == PT))
16706 return 0;
16707 }
16708 else
16709 for (; glyph < end
16710 && (!BUFFERP (glyph->object)
16711 || glyph->charpos < PT);
16712 glyph++)
16713 {
16714 w->cursor.hpos++;
16715 w->cursor.x += glyph->pixel_width;
16716 }
16717 }
16718 }
16719
16720 /* Adjust window end. A null value of last_text_row means that
16721 the window end is in reused rows which in turn means that
16722 only its vpos can have changed. */
16723 if (last_text_row)
16724 {
16725 w->window_end_bytepos
16726 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16727 w->window_end_pos
16728 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16729 w->window_end_vpos
16730 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16731 }
16732 else
16733 {
16734 w->window_end_vpos
16735 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16736 }
16737
16738 w->window_end_valid = Qnil;
16739 w->desired_matrix->no_scrolling_p = 1;
16740
16741 #ifdef GLYPH_DEBUG
16742 debug_method_add (w, "try_window_reusing_current_matrix 2");
16743 #endif
16744 return 1;
16745 }
16746
16747 return 0;
16748 }
16749
16750
16751 \f
16752 /************************************************************************
16753 Window redisplay reusing current matrix when buffer has changed
16754 ************************************************************************/
16755
16756 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16757 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16758 ptrdiff_t *, ptrdiff_t *);
16759 static struct glyph_row *
16760 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16761 struct glyph_row *);
16762
16763
16764 /* Return the last row in MATRIX displaying text. If row START is
16765 non-null, start searching with that row. IT gives the dimensions
16766 of the display. Value is null if matrix is empty; otherwise it is
16767 a pointer to the row found. */
16768
16769 static struct glyph_row *
16770 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16771 struct glyph_row *start)
16772 {
16773 struct glyph_row *row, *row_found;
16774
16775 /* Set row_found to the last row in IT->w's current matrix
16776 displaying text. The loop looks funny but think of partially
16777 visible lines. */
16778 row_found = NULL;
16779 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16780 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16781 {
16782 eassert (row->enabled_p);
16783 row_found = row;
16784 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16785 break;
16786 ++row;
16787 }
16788
16789 return row_found;
16790 }
16791
16792
16793 /* Return the last row in the current matrix of W that is not affected
16794 by changes at the start of current_buffer that occurred since W's
16795 current matrix was built. Value is null if no such row exists.
16796
16797 BEG_UNCHANGED us the number of characters unchanged at the start of
16798 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16799 first changed character in current_buffer. Characters at positions <
16800 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16801 when the current matrix was built. */
16802
16803 static struct glyph_row *
16804 find_last_unchanged_at_beg_row (struct window *w)
16805 {
16806 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16807 struct glyph_row *row;
16808 struct glyph_row *row_found = NULL;
16809 int yb = window_text_bottom_y (w);
16810
16811 /* Find the last row displaying unchanged text. */
16812 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16813 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16814 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16815 ++row)
16816 {
16817 if (/* If row ends before first_changed_pos, it is unchanged,
16818 except in some case. */
16819 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16820 /* When row ends in ZV and we write at ZV it is not
16821 unchanged. */
16822 && !row->ends_at_zv_p
16823 /* When first_changed_pos is the end of a continued line,
16824 row is not unchanged because it may be no longer
16825 continued. */
16826 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16827 && (row->continued_p
16828 || row->exact_window_width_line_p))
16829 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16830 needs to be recomputed, so don't consider this row as
16831 unchanged. This happens when the last line was
16832 bidi-reordered and was killed immediately before this
16833 redisplay cycle. In that case, ROW->end stores the
16834 buffer position of the first visual-order character of
16835 the killed text, which is now beyond ZV. */
16836 && CHARPOS (row->end.pos) <= ZV)
16837 row_found = row;
16838
16839 /* Stop if last visible row. */
16840 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16841 break;
16842 }
16843
16844 return row_found;
16845 }
16846
16847
16848 /* Find the first glyph row in the current matrix of W that is not
16849 affected by changes at the end of current_buffer since the
16850 time W's current matrix was built.
16851
16852 Return in *DELTA the number of chars by which buffer positions in
16853 unchanged text at the end of current_buffer must be adjusted.
16854
16855 Return in *DELTA_BYTES the corresponding number of bytes.
16856
16857 Value is null if no such row exists, i.e. all rows are affected by
16858 changes. */
16859
16860 static struct glyph_row *
16861 find_first_unchanged_at_end_row (struct window *w,
16862 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16863 {
16864 struct glyph_row *row;
16865 struct glyph_row *row_found = NULL;
16866
16867 *delta = *delta_bytes = 0;
16868
16869 /* Display must not have been paused, otherwise the current matrix
16870 is not up to date. */
16871 eassert (!NILP (w->window_end_valid));
16872
16873 /* A value of window_end_pos >= END_UNCHANGED means that the window
16874 end is in the range of changed text. If so, there is no
16875 unchanged row at the end of W's current matrix. */
16876 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16877 return NULL;
16878
16879 /* Set row to the last row in W's current matrix displaying text. */
16880 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16881
16882 /* If matrix is entirely empty, no unchanged row exists. */
16883 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16884 {
16885 /* The value of row is the last glyph row in the matrix having a
16886 meaningful buffer position in it. The end position of row
16887 corresponds to window_end_pos. This allows us to translate
16888 buffer positions in the current matrix to current buffer
16889 positions for characters not in changed text. */
16890 ptrdiff_t Z_old =
16891 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16892 ptrdiff_t Z_BYTE_old =
16893 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16894 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16895 struct glyph_row *first_text_row
16896 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16897
16898 *delta = Z - Z_old;
16899 *delta_bytes = Z_BYTE - Z_BYTE_old;
16900
16901 /* Set last_unchanged_pos to the buffer position of the last
16902 character in the buffer that has not been changed. Z is the
16903 index + 1 of the last character in current_buffer, i.e. by
16904 subtracting END_UNCHANGED we get the index of the last
16905 unchanged character, and we have to add BEG to get its buffer
16906 position. */
16907 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16908 last_unchanged_pos_old = last_unchanged_pos - *delta;
16909
16910 /* Search backward from ROW for a row displaying a line that
16911 starts at a minimum position >= last_unchanged_pos_old. */
16912 for (; row > first_text_row; --row)
16913 {
16914 /* This used to abort, but it can happen.
16915 It is ok to just stop the search instead here. KFS. */
16916 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16917 break;
16918
16919 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16920 row_found = row;
16921 }
16922 }
16923
16924 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16925
16926 return row_found;
16927 }
16928
16929
16930 /* Make sure that glyph rows in the current matrix of window W
16931 reference the same glyph memory as corresponding rows in the
16932 frame's frame matrix. This function is called after scrolling W's
16933 current matrix on a terminal frame in try_window_id and
16934 try_window_reusing_current_matrix. */
16935
16936 static void
16937 sync_frame_with_window_matrix_rows (struct window *w)
16938 {
16939 struct frame *f = XFRAME (w->frame);
16940 struct glyph_row *window_row, *window_row_end, *frame_row;
16941
16942 /* Preconditions: W must be a leaf window and full-width. Its frame
16943 must have a frame matrix. */
16944 eassert (NILP (w->hchild) && NILP (w->vchild));
16945 eassert (WINDOW_FULL_WIDTH_P (w));
16946 eassert (!FRAME_WINDOW_P (f));
16947
16948 /* If W is a full-width window, glyph pointers in W's current matrix
16949 have, by definition, to be the same as glyph pointers in the
16950 corresponding frame matrix. Note that frame matrices have no
16951 marginal areas (see build_frame_matrix). */
16952 window_row = w->current_matrix->rows;
16953 window_row_end = window_row + w->current_matrix->nrows;
16954 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16955 while (window_row < window_row_end)
16956 {
16957 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16958 struct glyph *end = window_row->glyphs[LAST_AREA];
16959
16960 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16961 frame_row->glyphs[TEXT_AREA] = start;
16962 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16963 frame_row->glyphs[LAST_AREA] = end;
16964
16965 /* Disable frame rows whose corresponding window rows have
16966 been disabled in try_window_id. */
16967 if (!window_row->enabled_p)
16968 frame_row->enabled_p = 0;
16969
16970 ++window_row, ++frame_row;
16971 }
16972 }
16973
16974
16975 /* Find the glyph row in window W containing CHARPOS. Consider all
16976 rows between START and END (not inclusive). END null means search
16977 all rows to the end of the display area of W. Value is the row
16978 containing CHARPOS or null. */
16979
16980 struct glyph_row *
16981 row_containing_pos (struct window *w, ptrdiff_t charpos,
16982 struct glyph_row *start, struct glyph_row *end, int dy)
16983 {
16984 struct glyph_row *row = start;
16985 struct glyph_row *best_row = NULL;
16986 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16987 int last_y;
16988
16989 /* If we happen to start on a header-line, skip that. */
16990 if (row->mode_line_p)
16991 ++row;
16992
16993 if ((end && row >= end) || !row->enabled_p)
16994 return NULL;
16995
16996 last_y = window_text_bottom_y (w) - dy;
16997
16998 while (1)
16999 {
17000 /* Give up if we have gone too far. */
17001 if (end && row >= end)
17002 return NULL;
17003 /* This formerly returned if they were equal.
17004 I think that both quantities are of a "last plus one" type;
17005 if so, when they are equal, the row is within the screen. -- rms. */
17006 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17007 return NULL;
17008
17009 /* If it is in this row, return this row. */
17010 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17011 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17012 /* The end position of a row equals the start
17013 position of the next row. If CHARPOS is there, we
17014 would rather display it in the next line, except
17015 when this line ends in ZV. */
17016 && !row->ends_at_zv_p
17017 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17018 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17019 {
17020 struct glyph *g;
17021
17022 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17023 || (!best_row && !row->continued_p))
17024 return row;
17025 /* In bidi-reordered rows, there could be several rows
17026 occluding point, all of them belonging to the same
17027 continued line. We need to find the row which fits
17028 CHARPOS the best. */
17029 for (g = row->glyphs[TEXT_AREA];
17030 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17031 g++)
17032 {
17033 if (!STRINGP (g->object))
17034 {
17035 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17036 {
17037 mindif = eabs (g->charpos - charpos);
17038 best_row = row;
17039 /* Exact match always wins. */
17040 if (mindif == 0)
17041 return best_row;
17042 }
17043 }
17044 }
17045 }
17046 else if (best_row && !row->continued_p)
17047 return best_row;
17048 ++row;
17049 }
17050 }
17051
17052
17053 /* Try to redisplay window W by reusing its existing display. W's
17054 current matrix must be up to date when this function is called,
17055 i.e. window_end_valid must not be nil.
17056
17057 Value is
17058
17059 1 if display has been updated
17060 0 if otherwise unsuccessful
17061 -1 if redisplay with same window start is known not to succeed
17062
17063 The following steps are performed:
17064
17065 1. Find the last row in the current matrix of W that is not
17066 affected by changes at the start of current_buffer. If no such row
17067 is found, give up.
17068
17069 2. Find the first row in W's current matrix that is not affected by
17070 changes at the end of current_buffer. Maybe there is no such row.
17071
17072 3. Display lines beginning with the row + 1 found in step 1 to the
17073 row found in step 2 or, if step 2 didn't find a row, to the end of
17074 the window.
17075
17076 4. If cursor is not known to appear on the window, give up.
17077
17078 5. If display stopped at the row found in step 2, scroll the
17079 display and current matrix as needed.
17080
17081 6. Maybe display some lines at the end of W, if we must. This can
17082 happen under various circumstances, like a partially visible line
17083 becoming fully visible, or because newly displayed lines are displayed
17084 in smaller font sizes.
17085
17086 7. Update W's window end information. */
17087
17088 static int
17089 try_window_id (struct window *w)
17090 {
17091 struct frame *f = XFRAME (w->frame);
17092 struct glyph_matrix *current_matrix = w->current_matrix;
17093 struct glyph_matrix *desired_matrix = w->desired_matrix;
17094 struct glyph_row *last_unchanged_at_beg_row;
17095 struct glyph_row *first_unchanged_at_end_row;
17096 struct glyph_row *row;
17097 struct glyph_row *bottom_row;
17098 int bottom_vpos;
17099 struct it it;
17100 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17101 int dvpos, dy;
17102 struct text_pos start_pos;
17103 struct run run;
17104 int first_unchanged_at_end_vpos = 0;
17105 struct glyph_row *last_text_row, *last_text_row_at_end;
17106 struct text_pos start;
17107 ptrdiff_t first_changed_charpos, last_changed_charpos;
17108
17109 #ifdef GLYPH_DEBUG
17110 if (inhibit_try_window_id)
17111 return 0;
17112 #endif
17113
17114 /* This is handy for debugging. */
17115 #if 0
17116 #define GIVE_UP(X) \
17117 do { \
17118 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17119 return 0; \
17120 } while (0)
17121 #else
17122 #define GIVE_UP(X) return 0
17123 #endif
17124
17125 SET_TEXT_POS_FROM_MARKER (start, w->start);
17126
17127 /* Don't use this for mini-windows because these can show
17128 messages and mini-buffers, and we don't handle that here. */
17129 if (MINI_WINDOW_P (w))
17130 GIVE_UP (1);
17131
17132 /* This flag is used to prevent redisplay optimizations. */
17133 if (windows_or_buffers_changed || cursor_type_changed)
17134 GIVE_UP (2);
17135
17136 /* Verify that narrowing has not changed.
17137 Also verify that we were not told to prevent redisplay optimizations.
17138 It would be nice to further
17139 reduce the number of cases where this prevents try_window_id. */
17140 if (current_buffer->clip_changed
17141 || current_buffer->prevent_redisplay_optimizations_p)
17142 GIVE_UP (3);
17143
17144 /* Window must either use window-based redisplay or be full width. */
17145 if (!FRAME_WINDOW_P (f)
17146 && (!FRAME_LINE_INS_DEL_OK (f)
17147 || !WINDOW_FULL_WIDTH_P (w)))
17148 GIVE_UP (4);
17149
17150 /* Give up if point is known NOT to appear in W. */
17151 if (PT < CHARPOS (start))
17152 GIVE_UP (5);
17153
17154 /* Another way to prevent redisplay optimizations. */
17155 if (w->last_modified == 0)
17156 GIVE_UP (6);
17157
17158 /* Verify that window is not hscrolled. */
17159 if (w->hscroll != 0)
17160 GIVE_UP (7);
17161
17162 /* Verify that display wasn't paused. */
17163 if (NILP (w->window_end_valid))
17164 GIVE_UP (8);
17165
17166 /* Can't use this if highlighting a region because a cursor movement
17167 will do more than just set the cursor. */
17168 if (!NILP (Vtransient_mark_mode)
17169 && !NILP (BVAR (current_buffer, mark_active)))
17170 GIVE_UP (9);
17171
17172 /* Likewise if highlighting trailing whitespace. */
17173 if (!NILP (Vshow_trailing_whitespace))
17174 GIVE_UP (11);
17175
17176 /* Likewise if showing a region. */
17177 if (!NILP (w->region_showing))
17178 GIVE_UP (10);
17179
17180 /* Can't use this if overlay arrow position and/or string have
17181 changed. */
17182 if (overlay_arrows_changed_p ())
17183 GIVE_UP (12);
17184
17185 /* When word-wrap is on, adding a space to the first word of a
17186 wrapped line can change the wrap position, altering the line
17187 above it. It might be worthwhile to handle this more
17188 intelligently, but for now just redisplay from scratch. */
17189 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17190 GIVE_UP (21);
17191
17192 /* Under bidi reordering, adding or deleting a character in the
17193 beginning of a paragraph, before the first strong directional
17194 character, can change the base direction of the paragraph (unless
17195 the buffer specifies a fixed paragraph direction), which will
17196 require to redisplay the whole paragraph. It might be worthwhile
17197 to find the paragraph limits and widen the range of redisplayed
17198 lines to that, but for now just give up this optimization and
17199 redisplay from scratch. */
17200 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17201 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17202 GIVE_UP (22);
17203
17204 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17205 only if buffer has really changed. The reason is that the gap is
17206 initially at Z for freshly visited files. The code below would
17207 set end_unchanged to 0 in that case. */
17208 if (MODIFF > SAVE_MODIFF
17209 /* This seems to happen sometimes after saving a buffer. */
17210 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17211 {
17212 if (GPT - BEG < BEG_UNCHANGED)
17213 BEG_UNCHANGED = GPT - BEG;
17214 if (Z - GPT < END_UNCHANGED)
17215 END_UNCHANGED = Z - GPT;
17216 }
17217
17218 /* The position of the first and last character that has been changed. */
17219 first_changed_charpos = BEG + BEG_UNCHANGED;
17220 last_changed_charpos = Z - END_UNCHANGED;
17221
17222 /* If window starts after a line end, and the last change is in
17223 front of that newline, then changes don't affect the display.
17224 This case happens with stealth-fontification. Note that although
17225 the display is unchanged, glyph positions in the matrix have to
17226 be adjusted, of course. */
17227 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17228 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17229 && ((last_changed_charpos < CHARPOS (start)
17230 && CHARPOS (start) == BEGV)
17231 || (last_changed_charpos < CHARPOS (start) - 1
17232 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17233 {
17234 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17235 struct glyph_row *r0;
17236
17237 /* Compute how many chars/bytes have been added to or removed
17238 from the buffer. */
17239 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17240 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17241 Z_delta = Z - Z_old;
17242 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17243
17244 /* Give up if PT is not in the window. Note that it already has
17245 been checked at the start of try_window_id that PT is not in
17246 front of the window start. */
17247 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17248 GIVE_UP (13);
17249
17250 /* If window start is unchanged, we can reuse the whole matrix
17251 as is, after adjusting glyph positions. No need to compute
17252 the window end again, since its offset from Z hasn't changed. */
17253 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17254 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17255 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17256 /* PT must not be in a partially visible line. */
17257 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17258 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17259 {
17260 /* Adjust positions in the glyph matrix. */
17261 if (Z_delta || Z_delta_bytes)
17262 {
17263 struct glyph_row *r1
17264 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17265 increment_matrix_positions (w->current_matrix,
17266 MATRIX_ROW_VPOS (r0, current_matrix),
17267 MATRIX_ROW_VPOS (r1, current_matrix),
17268 Z_delta, Z_delta_bytes);
17269 }
17270
17271 /* Set the cursor. */
17272 row = row_containing_pos (w, PT, r0, NULL, 0);
17273 if (row)
17274 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17275 else
17276 abort ();
17277 return 1;
17278 }
17279 }
17280
17281 /* Handle the case that changes are all below what is displayed in
17282 the window, and that PT is in the window. This shortcut cannot
17283 be taken if ZV is visible in the window, and text has been added
17284 there that is visible in the window. */
17285 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17286 /* ZV is not visible in the window, or there are no
17287 changes at ZV, actually. */
17288 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17289 || first_changed_charpos == last_changed_charpos))
17290 {
17291 struct glyph_row *r0;
17292
17293 /* Give up if PT is not in the window. Note that it already has
17294 been checked at the start of try_window_id that PT is not in
17295 front of the window start. */
17296 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17297 GIVE_UP (14);
17298
17299 /* If window start is unchanged, we can reuse the whole matrix
17300 as is, without changing glyph positions since no text has
17301 been added/removed in front of the window end. */
17302 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17303 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17304 /* PT must not be in a partially visible line. */
17305 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17306 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17307 {
17308 /* We have to compute the window end anew since text
17309 could have been added/removed after it. */
17310 w->window_end_pos
17311 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17312 w->window_end_bytepos
17313 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17314
17315 /* Set the cursor. */
17316 row = row_containing_pos (w, PT, r0, NULL, 0);
17317 if (row)
17318 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17319 else
17320 abort ();
17321 return 2;
17322 }
17323 }
17324
17325 /* Give up if window start is in the changed area.
17326
17327 The condition used to read
17328
17329 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17330
17331 but why that was tested escapes me at the moment. */
17332 if (CHARPOS (start) >= first_changed_charpos
17333 && CHARPOS (start) <= last_changed_charpos)
17334 GIVE_UP (15);
17335
17336 /* Check that window start agrees with the start of the first glyph
17337 row in its current matrix. Check this after we know the window
17338 start is not in changed text, otherwise positions would not be
17339 comparable. */
17340 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17341 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17342 GIVE_UP (16);
17343
17344 /* Give up if the window ends in strings. Overlay strings
17345 at the end are difficult to handle, so don't try. */
17346 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17347 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17348 GIVE_UP (20);
17349
17350 /* Compute the position at which we have to start displaying new
17351 lines. Some of the lines at the top of the window might be
17352 reusable because they are not displaying changed text. Find the
17353 last row in W's current matrix not affected by changes at the
17354 start of current_buffer. Value is null if changes start in the
17355 first line of window. */
17356 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17357 if (last_unchanged_at_beg_row)
17358 {
17359 /* Avoid starting to display in the middle of a character, a TAB
17360 for instance. This is easier than to set up the iterator
17361 exactly, and it's not a frequent case, so the additional
17362 effort wouldn't really pay off. */
17363 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17364 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17365 && last_unchanged_at_beg_row > w->current_matrix->rows)
17366 --last_unchanged_at_beg_row;
17367
17368 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17369 GIVE_UP (17);
17370
17371 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17372 GIVE_UP (18);
17373 start_pos = it.current.pos;
17374
17375 /* Start displaying new lines in the desired matrix at the same
17376 vpos we would use in the current matrix, i.e. below
17377 last_unchanged_at_beg_row. */
17378 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17379 current_matrix);
17380 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17381 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17382
17383 eassert (it.hpos == 0 && it.current_x == 0);
17384 }
17385 else
17386 {
17387 /* There are no reusable lines at the start of the window.
17388 Start displaying in the first text line. */
17389 start_display (&it, w, start);
17390 it.vpos = it.first_vpos;
17391 start_pos = it.current.pos;
17392 }
17393
17394 /* Find the first row that is not affected by changes at the end of
17395 the buffer. Value will be null if there is no unchanged row, in
17396 which case we must redisplay to the end of the window. delta
17397 will be set to the value by which buffer positions beginning with
17398 first_unchanged_at_end_row have to be adjusted due to text
17399 changes. */
17400 first_unchanged_at_end_row
17401 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17402 IF_DEBUG (debug_delta = delta);
17403 IF_DEBUG (debug_delta_bytes = delta_bytes);
17404
17405 /* Set stop_pos to the buffer position up to which we will have to
17406 display new lines. If first_unchanged_at_end_row != NULL, this
17407 is the buffer position of the start of the line displayed in that
17408 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17409 that we don't stop at a buffer position. */
17410 stop_pos = 0;
17411 if (first_unchanged_at_end_row)
17412 {
17413 eassert (last_unchanged_at_beg_row == NULL
17414 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17415
17416 /* If this is a continuation line, move forward to the next one
17417 that isn't. Changes in lines above affect this line.
17418 Caution: this may move first_unchanged_at_end_row to a row
17419 not displaying text. */
17420 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17421 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17422 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17423 < it.last_visible_y))
17424 ++first_unchanged_at_end_row;
17425
17426 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17427 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17428 >= it.last_visible_y))
17429 first_unchanged_at_end_row = NULL;
17430 else
17431 {
17432 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17433 + delta);
17434 first_unchanged_at_end_vpos
17435 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17436 eassert (stop_pos >= Z - END_UNCHANGED);
17437 }
17438 }
17439 else if (last_unchanged_at_beg_row == NULL)
17440 GIVE_UP (19);
17441
17442
17443 #ifdef GLYPH_DEBUG
17444
17445 /* Either there is no unchanged row at the end, or the one we have
17446 now displays text. This is a necessary condition for the window
17447 end pos calculation at the end of this function. */
17448 eassert (first_unchanged_at_end_row == NULL
17449 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17450
17451 debug_last_unchanged_at_beg_vpos
17452 = (last_unchanged_at_beg_row
17453 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17454 : -1);
17455 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17456
17457 #endif /* GLYPH_DEBUG */
17458
17459
17460 /* Display new lines. Set last_text_row to the last new line
17461 displayed which has text on it, i.e. might end up as being the
17462 line where the window_end_vpos is. */
17463 w->cursor.vpos = -1;
17464 last_text_row = NULL;
17465 overlay_arrow_seen = 0;
17466 while (it.current_y < it.last_visible_y
17467 && !fonts_changed_p
17468 && (first_unchanged_at_end_row == NULL
17469 || IT_CHARPOS (it) < stop_pos))
17470 {
17471 if (display_line (&it))
17472 last_text_row = it.glyph_row - 1;
17473 }
17474
17475 if (fonts_changed_p)
17476 return -1;
17477
17478
17479 /* Compute differences in buffer positions, y-positions etc. for
17480 lines reused at the bottom of the window. Compute what we can
17481 scroll. */
17482 if (first_unchanged_at_end_row
17483 /* No lines reused because we displayed everything up to the
17484 bottom of the window. */
17485 && it.current_y < it.last_visible_y)
17486 {
17487 dvpos = (it.vpos
17488 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17489 current_matrix));
17490 dy = it.current_y - first_unchanged_at_end_row->y;
17491 run.current_y = first_unchanged_at_end_row->y;
17492 run.desired_y = run.current_y + dy;
17493 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17494 }
17495 else
17496 {
17497 delta = delta_bytes = dvpos = dy
17498 = run.current_y = run.desired_y = run.height = 0;
17499 first_unchanged_at_end_row = NULL;
17500 }
17501 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17502
17503
17504 /* Find the cursor if not already found. We have to decide whether
17505 PT will appear on this window (it sometimes doesn't, but this is
17506 not a very frequent case.) This decision has to be made before
17507 the current matrix is altered. A value of cursor.vpos < 0 means
17508 that PT is either in one of the lines beginning at
17509 first_unchanged_at_end_row or below the window. Don't care for
17510 lines that might be displayed later at the window end; as
17511 mentioned, this is not a frequent case. */
17512 if (w->cursor.vpos < 0)
17513 {
17514 /* Cursor in unchanged rows at the top? */
17515 if (PT < CHARPOS (start_pos)
17516 && last_unchanged_at_beg_row)
17517 {
17518 row = row_containing_pos (w, PT,
17519 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17520 last_unchanged_at_beg_row + 1, 0);
17521 if (row)
17522 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17523 }
17524
17525 /* Start from first_unchanged_at_end_row looking for PT. */
17526 else if (first_unchanged_at_end_row)
17527 {
17528 row = row_containing_pos (w, PT - delta,
17529 first_unchanged_at_end_row, NULL, 0);
17530 if (row)
17531 set_cursor_from_row (w, row, w->current_matrix, delta,
17532 delta_bytes, dy, dvpos);
17533 }
17534
17535 /* Give up if cursor was not found. */
17536 if (w->cursor.vpos < 0)
17537 {
17538 clear_glyph_matrix (w->desired_matrix);
17539 return -1;
17540 }
17541 }
17542
17543 /* Don't let the cursor end in the scroll margins. */
17544 {
17545 int this_scroll_margin, cursor_height;
17546
17547 this_scroll_margin =
17548 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17549 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17550 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17551
17552 if ((w->cursor.y < this_scroll_margin
17553 && CHARPOS (start) > BEGV)
17554 /* Old redisplay didn't take scroll margin into account at the bottom,
17555 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17556 || (w->cursor.y + (make_cursor_line_fully_visible_p
17557 ? cursor_height + this_scroll_margin
17558 : 1)) > it.last_visible_y)
17559 {
17560 w->cursor.vpos = -1;
17561 clear_glyph_matrix (w->desired_matrix);
17562 return -1;
17563 }
17564 }
17565
17566 /* Scroll the display. Do it before changing the current matrix so
17567 that xterm.c doesn't get confused about where the cursor glyph is
17568 found. */
17569 if (dy && run.height)
17570 {
17571 update_begin (f);
17572
17573 if (FRAME_WINDOW_P (f))
17574 {
17575 FRAME_RIF (f)->update_window_begin_hook (w);
17576 FRAME_RIF (f)->clear_window_mouse_face (w);
17577 FRAME_RIF (f)->scroll_run_hook (w, &run);
17578 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17579 }
17580 else
17581 {
17582 /* Terminal frame. In this case, dvpos gives the number of
17583 lines to scroll by; dvpos < 0 means scroll up. */
17584 int from_vpos
17585 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17586 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17587 int end = (WINDOW_TOP_EDGE_LINE (w)
17588 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17589 + window_internal_height (w));
17590
17591 #if defined (HAVE_GPM) || defined (MSDOS)
17592 x_clear_window_mouse_face (w);
17593 #endif
17594 /* Perform the operation on the screen. */
17595 if (dvpos > 0)
17596 {
17597 /* Scroll last_unchanged_at_beg_row to the end of the
17598 window down dvpos lines. */
17599 set_terminal_window (f, end);
17600
17601 /* On dumb terminals delete dvpos lines at the end
17602 before inserting dvpos empty lines. */
17603 if (!FRAME_SCROLL_REGION_OK (f))
17604 ins_del_lines (f, end - dvpos, -dvpos);
17605
17606 /* Insert dvpos empty lines in front of
17607 last_unchanged_at_beg_row. */
17608 ins_del_lines (f, from, dvpos);
17609 }
17610 else if (dvpos < 0)
17611 {
17612 /* Scroll up last_unchanged_at_beg_vpos to the end of
17613 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17614 set_terminal_window (f, end);
17615
17616 /* Delete dvpos lines in front of
17617 last_unchanged_at_beg_vpos. ins_del_lines will set
17618 the cursor to the given vpos and emit |dvpos| delete
17619 line sequences. */
17620 ins_del_lines (f, from + dvpos, dvpos);
17621
17622 /* On a dumb terminal insert dvpos empty lines at the
17623 end. */
17624 if (!FRAME_SCROLL_REGION_OK (f))
17625 ins_del_lines (f, end + dvpos, -dvpos);
17626 }
17627
17628 set_terminal_window (f, 0);
17629 }
17630
17631 update_end (f);
17632 }
17633
17634 /* Shift reused rows of the current matrix to the right position.
17635 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17636 text. */
17637 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17638 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17639 if (dvpos < 0)
17640 {
17641 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17642 bottom_vpos, dvpos);
17643 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17644 bottom_vpos, 0);
17645 }
17646 else if (dvpos > 0)
17647 {
17648 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17649 bottom_vpos, dvpos);
17650 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17651 first_unchanged_at_end_vpos + dvpos, 0);
17652 }
17653
17654 /* For frame-based redisplay, make sure that current frame and window
17655 matrix are in sync with respect to glyph memory. */
17656 if (!FRAME_WINDOW_P (f))
17657 sync_frame_with_window_matrix_rows (w);
17658
17659 /* Adjust buffer positions in reused rows. */
17660 if (delta || delta_bytes)
17661 increment_matrix_positions (current_matrix,
17662 first_unchanged_at_end_vpos + dvpos,
17663 bottom_vpos, delta, delta_bytes);
17664
17665 /* Adjust Y positions. */
17666 if (dy)
17667 shift_glyph_matrix (w, current_matrix,
17668 first_unchanged_at_end_vpos + dvpos,
17669 bottom_vpos, dy);
17670
17671 if (first_unchanged_at_end_row)
17672 {
17673 first_unchanged_at_end_row += dvpos;
17674 if (first_unchanged_at_end_row->y >= it.last_visible_y
17675 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17676 first_unchanged_at_end_row = NULL;
17677 }
17678
17679 /* If scrolling up, there may be some lines to display at the end of
17680 the window. */
17681 last_text_row_at_end = NULL;
17682 if (dy < 0)
17683 {
17684 /* Scrolling up can leave for example a partially visible line
17685 at the end of the window to be redisplayed. */
17686 /* Set last_row to the glyph row in the current matrix where the
17687 window end line is found. It has been moved up or down in
17688 the matrix by dvpos. */
17689 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17690 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17691
17692 /* If last_row is the window end line, it should display text. */
17693 eassert (last_row->displays_text_p);
17694
17695 /* If window end line was partially visible before, begin
17696 displaying at that line. Otherwise begin displaying with the
17697 line following it. */
17698 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17699 {
17700 init_to_row_start (&it, w, last_row);
17701 it.vpos = last_vpos;
17702 it.current_y = last_row->y;
17703 }
17704 else
17705 {
17706 init_to_row_end (&it, w, last_row);
17707 it.vpos = 1 + last_vpos;
17708 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17709 ++last_row;
17710 }
17711
17712 /* We may start in a continuation line. If so, we have to
17713 get the right continuation_lines_width and current_x. */
17714 it.continuation_lines_width = last_row->continuation_lines_width;
17715 it.hpos = it.current_x = 0;
17716
17717 /* Display the rest of the lines at the window end. */
17718 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17719 while (it.current_y < it.last_visible_y
17720 && !fonts_changed_p)
17721 {
17722 /* Is it always sure that the display agrees with lines in
17723 the current matrix? I don't think so, so we mark rows
17724 displayed invalid in the current matrix by setting their
17725 enabled_p flag to zero. */
17726 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17727 if (display_line (&it))
17728 last_text_row_at_end = it.glyph_row - 1;
17729 }
17730 }
17731
17732 /* Update window_end_pos and window_end_vpos. */
17733 if (first_unchanged_at_end_row
17734 && !last_text_row_at_end)
17735 {
17736 /* Window end line if one of the preserved rows from the current
17737 matrix. Set row to the last row displaying text in current
17738 matrix starting at first_unchanged_at_end_row, after
17739 scrolling. */
17740 eassert (first_unchanged_at_end_row->displays_text_p);
17741 row = find_last_row_displaying_text (w->current_matrix, &it,
17742 first_unchanged_at_end_row);
17743 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17744
17745 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17746 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17747 w->window_end_vpos
17748 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17749 eassert (w->window_end_bytepos >= 0);
17750 IF_DEBUG (debug_method_add (w, "A"));
17751 }
17752 else if (last_text_row_at_end)
17753 {
17754 w->window_end_pos
17755 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17756 w->window_end_bytepos
17757 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17758 w->window_end_vpos
17759 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17760 eassert (w->window_end_bytepos >= 0);
17761 IF_DEBUG (debug_method_add (w, "B"));
17762 }
17763 else if (last_text_row)
17764 {
17765 /* We have displayed either to the end of the window or at the
17766 end of the window, i.e. the last row with text is to be found
17767 in the desired matrix. */
17768 w->window_end_pos
17769 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17770 w->window_end_bytepos
17771 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17772 w->window_end_vpos
17773 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17774 eassert (w->window_end_bytepos >= 0);
17775 }
17776 else if (first_unchanged_at_end_row == NULL
17777 && last_text_row == NULL
17778 && last_text_row_at_end == NULL)
17779 {
17780 /* Displayed to end of window, but no line containing text was
17781 displayed. Lines were deleted at the end of the window. */
17782 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17783 int vpos = XFASTINT (w->window_end_vpos);
17784 struct glyph_row *current_row = current_matrix->rows + vpos;
17785 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17786
17787 for (row = NULL;
17788 row == NULL && vpos >= first_vpos;
17789 --vpos, --current_row, --desired_row)
17790 {
17791 if (desired_row->enabled_p)
17792 {
17793 if (desired_row->displays_text_p)
17794 row = desired_row;
17795 }
17796 else if (current_row->displays_text_p)
17797 row = current_row;
17798 }
17799
17800 eassert (row != NULL);
17801 w->window_end_vpos = make_number (vpos + 1);
17802 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17803 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17804 eassert (w->window_end_bytepos >= 0);
17805 IF_DEBUG (debug_method_add (w, "C"));
17806 }
17807 else
17808 abort ();
17809
17810 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17811 debug_end_vpos = XFASTINT (w->window_end_vpos));
17812
17813 /* Record that display has not been completed. */
17814 w->window_end_valid = Qnil;
17815 w->desired_matrix->no_scrolling_p = 1;
17816 return 3;
17817
17818 #undef GIVE_UP
17819 }
17820
17821
17822 \f
17823 /***********************************************************************
17824 More debugging support
17825 ***********************************************************************/
17826
17827 #ifdef GLYPH_DEBUG
17828
17829 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17830 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17831 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17832
17833
17834 /* Dump the contents of glyph matrix MATRIX on stderr.
17835
17836 GLYPHS 0 means don't show glyph contents.
17837 GLYPHS 1 means show glyphs in short form
17838 GLYPHS > 1 means show glyphs in long form. */
17839
17840 void
17841 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17842 {
17843 int i;
17844 for (i = 0; i < matrix->nrows; ++i)
17845 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17846 }
17847
17848
17849 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17850 the glyph row and area where the glyph comes from. */
17851
17852 void
17853 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17854 {
17855 if (glyph->type == CHAR_GLYPH)
17856 {
17857 fprintf (stderr,
17858 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17859 glyph - row->glyphs[TEXT_AREA],
17860 'C',
17861 glyph->charpos,
17862 (BUFFERP (glyph->object)
17863 ? 'B'
17864 : (STRINGP (glyph->object)
17865 ? 'S'
17866 : '-')),
17867 glyph->pixel_width,
17868 glyph->u.ch,
17869 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17870 ? glyph->u.ch
17871 : '.'),
17872 glyph->face_id,
17873 glyph->left_box_line_p,
17874 glyph->right_box_line_p);
17875 }
17876 else if (glyph->type == STRETCH_GLYPH)
17877 {
17878 fprintf (stderr,
17879 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17880 glyph - row->glyphs[TEXT_AREA],
17881 'S',
17882 glyph->charpos,
17883 (BUFFERP (glyph->object)
17884 ? 'B'
17885 : (STRINGP (glyph->object)
17886 ? 'S'
17887 : '-')),
17888 glyph->pixel_width,
17889 0,
17890 '.',
17891 glyph->face_id,
17892 glyph->left_box_line_p,
17893 glyph->right_box_line_p);
17894 }
17895 else if (glyph->type == IMAGE_GLYPH)
17896 {
17897 fprintf (stderr,
17898 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17899 glyph - row->glyphs[TEXT_AREA],
17900 'I',
17901 glyph->charpos,
17902 (BUFFERP (glyph->object)
17903 ? 'B'
17904 : (STRINGP (glyph->object)
17905 ? 'S'
17906 : '-')),
17907 glyph->pixel_width,
17908 glyph->u.img_id,
17909 '.',
17910 glyph->face_id,
17911 glyph->left_box_line_p,
17912 glyph->right_box_line_p);
17913 }
17914 else if (glyph->type == COMPOSITE_GLYPH)
17915 {
17916 fprintf (stderr,
17917 " %5td %4c %6"pI"d %c %3d 0x%05x",
17918 glyph - row->glyphs[TEXT_AREA],
17919 '+',
17920 glyph->charpos,
17921 (BUFFERP (glyph->object)
17922 ? 'B'
17923 : (STRINGP (glyph->object)
17924 ? 'S'
17925 : '-')),
17926 glyph->pixel_width,
17927 glyph->u.cmp.id);
17928 if (glyph->u.cmp.automatic)
17929 fprintf (stderr,
17930 "[%d-%d]",
17931 glyph->slice.cmp.from, glyph->slice.cmp.to);
17932 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17933 glyph->face_id,
17934 glyph->left_box_line_p,
17935 glyph->right_box_line_p);
17936 }
17937 }
17938
17939
17940 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17941 GLYPHS 0 means don't show glyph contents.
17942 GLYPHS 1 means show glyphs in short form
17943 GLYPHS > 1 means show glyphs in long form. */
17944
17945 void
17946 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17947 {
17948 if (glyphs != 1)
17949 {
17950 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17951 fprintf (stderr, "======================================================================\n");
17952
17953 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17954 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17955 vpos,
17956 MATRIX_ROW_START_CHARPOS (row),
17957 MATRIX_ROW_END_CHARPOS (row),
17958 row->used[TEXT_AREA],
17959 row->contains_overlapping_glyphs_p,
17960 row->enabled_p,
17961 row->truncated_on_left_p,
17962 row->truncated_on_right_p,
17963 row->continued_p,
17964 MATRIX_ROW_CONTINUATION_LINE_P (row),
17965 row->displays_text_p,
17966 row->ends_at_zv_p,
17967 row->fill_line_p,
17968 row->ends_in_middle_of_char_p,
17969 row->starts_in_middle_of_char_p,
17970 row->mouse_face_p,
17971 row->x,
17972 row->y,
17973 row->pixel_width,
17974 row->height,
17975 row->visible_height,
17976 row->ascent,
17977 row->phys_ascent);
17978 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17979 row->end.overlay_string_index,
17980 row->continuation_lines_width);
17981 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17982 CHARPOS (row->start.string_pos),
17983 CHARPOS (row->end.string_pos));
17984 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17985 row->end.dpvec_index);
17986 }
17987
17988 if (glyphs > 1)
17989 {
17990 int area;
17991
17992 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17993 {
17994 struct glyph *glyph = row->glyphs[area];
17995 struct glyph *glyph_end = glyph + row->used[area];
17996
17997 /* Glyph for a line end in text. */
17998 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17999 ++glyph_end;
18000
18001 if (glyph < glyph_end)
18002 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18003
18004 for (; glyph < glyph_end; ++glyph)
18005 dump_glyph (row, glyph, area);
18006 }
18007 }
18008 else if (glyphs == 1)
18009 {
18010 int area;
18011
18012 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18013 {
18014 char *s = alloca (row->used[area] + 1);
18015 int i;
18016
18017 for (i = 0; i < row->used[area]; ++i)
18018 {
18019 struct glyph *glyph = row->glyphs[area] + i;
18020 if (glyph->type == CHAR_GLYPH
18021 && glyph->u.ch < 0x80
18022 && glyph->u.ch >= ' ')
18023 s[i] = glyph->u.ch;
18024 else
18025 s[i] = '.';
18026 }
18027
18028 s[i] = '\0';
18029 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18030 }
18031 }
18032 }
18033
18034
18035 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18036 Sdump_glyph_matrix, 0, 1, "p",
18037 doc: /* Dump the current matrix of the selected window to stderr.
18038 Shows contents of glyph row structures. With non-nil
18039 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18040 glyphs in short form, otherwise show glyphs in long form. */)
18041 (Lisp_Object glyphs)
18042 {
18043 struct window *w = XWINDOW (selected_window);
18044 struct buffer *buffer = XBUFFER (w->buffer);
18045
18046 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18047 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18048 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18049 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18050 fprintf (stderr, "=============================================\n");
18051 dump_glyph_matrix (w->current_matrix,
18052 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18053 return Qnil;
18054 }
18055
18056
18057 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18058 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18059 (void)
18060 {
18061 struct frame *f = XFRAME (selected_frame);
18062 dump_glyph_matrix (f->current_matrix, 1);
18063 return Qnil;
18064 }
18065
18066
18067 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18068 doc: /* Dump glyph row ROW to stderr.
18069 GLYPH 0 means don't dump glyphs.
18070 GLYPH 1 means dump glyphs in short form.
18071 GLYPH > 1 or omitted means dump glyphs in long form. */)
18072 (Lisp_Object row, Lisp_Object glyphs)
18073 {
18074 struct glyph_matrix *matrix;
18075 EMACS_INT vpos;
18076
18077 CHECK_NUMBER (row);
18078 matrix = XWINDOW (selected_window)->current_matrix;
18079 vpos = XINT (row);
18080 if (vpos >= 0 && vpos < matrix->nrows)
18081 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18082 vpos,
18083 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18084 return Qnil;
18085 }
18086
18087
18088 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18089 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18090 GLYPH 0 means don't dump glyphs.
18091 GLYPH 1 means dump glyphs in short form.
18092 GLYPH > 1 or omitted means dump glyphs in long form. */)
18093 (Lisp_Object row, Lisp_Object glyphs)
18094 {
18095 struct frame *sf = SELECTED_FRAME ();
18096 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18097 EMACS_INT vpos;
18098
18099 CHECK_NUMBER (row);
18100 vpos = XINT (row);
18101 if (vpos >= 0 && vpos < m->nrows)
18102 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18103 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18104 return Qnil;
18105 }
18106
18107
18108 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18109 doc: /* Toggle tracing of redisplay.
18110 With ARG, turn tracing on if and only if ARG is positive. */)
18111 (Lisp_Object arg)
18112 {
18113 if (NILP (arg))
18114 trace_redisplay_p = !trace_redisplay_p;
18115 else
18116 {
18117 arg = Fprefix_numeric_value (arg);
18118 trace_redisplay_p = XINT (arg) > 0;
18119 }
18120
18121 return Qnil;
18122 }
18123
18124
18125 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18126 doc: /* Like `format', but print result to stderr.
18127 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18128 (ptrdiff_t nargs, Lisp_Object *args)
18129 {
18130 Lisp_Object s = Fformat (nargs, args);
18131 fprintf (stderr, "%s", SDATA (s));
18132 return Qnil;
18133 }
18134
18135 #endif /* GLYPH_DEBUG */
18136
18137
18138 \f
18139 /***********************************************************************
18140 Building Desired Matrix Rows
18141 ***********************************************************************/
18142
18143 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18144 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18145
18146 static struct glyph_row *
18147 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18148 {
18149 struct frame *f = XFRAME (WINDOW_FRAME (w));
18150 struct buffer *buffer = XBUFFER (w->buffer);
18151 struct buffer *old = current_buffer;
18152 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18153 int arrow_len = SCHARS (overlay_arrow_string);
18154 const unsigned char *arrow_end = arrow_string + arrow_len;
18155 const unsigned char *p;
18156 struct it it;
18157 int multibyte_p;
18158 int n_glyphs_before;
18159
18160 set_buffer_temp (buffer);
18161 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18162 it.glyph_row->used[TEXT_AREA] = 0;
18163 SET_TEXT_POS (it.position, 0, 0);
18164
18165 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18166 p = arrow_string;
18167 while (p < arrow_end)
18168 {
18169 Lisp_Object face, ilisp;
18170
18171 /* Get the next character. */
18172 if (multibyte_p)
18173 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18174 else
18175 {
18176 it.c = it.char_to_display = *p, it.len = 1;
18177 if (! ASCII_CHAR_P (it.c))
18178 it.char_to_display = BYTE8_TO_CHAR (it.c);
18179 }
18180 p += it.len;
18181
18182 /* Get its face. */
18183 ilisp = make_number (p - arrow_string);
18184 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18185 it.face_id = compute_char_face (f, it.char_to_display, face);
18186
18187 /* Compute its width, get its glyphs. */
18188 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18189 SET_TEXT_POS (it.position, -1, -1);
18190 PRODUCE_GLYPHS (&it);
18191
18192 /* If this character doesn't fit any more in the line, we have
18193 to remove some glyphs. */
18194 if (it.current_x > it.last_visible_x)
18195 {
18196 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18197 break;
18198 }
18199 }
18200
18201 set_buffer_temp (old);
18202 return it.glyph_row;
18203 }
18204
18205
18206 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18207 glyphs are only inserted for terminal frames since we can't really
18208 win with truncation glyphs when partially visible glyphs are
18209 involved. Which glyphs to insert is determined by
18210 produce_special_glyphs. */
18211
18212 static void
18213 insert_left_trunc_glyphs (struct it *it)
18214 {
18215 struct it truncate_it;
18216 struct glyph *from, *end, *to, *toend;
18217
18218 eassert (!FRAME_WINDOW_P (it->f));
18219
18220 /* Get the truncation glyphs. */
18221 truncate_it = *it;
18222 truncate_it.current_x = 0;
18223 truncate_it.face_id = DEFAULT_FACE_ID;
18224 truncate_it.glyph_row = &scratch_glyph_row;
18225 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18226 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18227 truncate_it.object = make_number (0);
18228 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18229
18230 /* Overwrite glyphs from IT with truncation glyphs. */
18231 if (!it->glyph_row->reversed_p)
18232 {
18233 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18234 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18235 to = it->glyph_row->glyphs[TEXT_AREA];
18236 toend = to + it->glyph_row->used[TEXT_AREA];
18237
18238 while (from < end)
18239 *to++ = *from++;
18240
18241 /* There may be padding glyphs left over. Overwrite them too. */
18242 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18243 {
18244 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18245 while (from < end)
18246 *to++ = *from++;
18247 }
18248
18249 if (to > toend)
18250 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18251 }
18252 else
18253 {
18254 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18255 that back to front. */
18256 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18257 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18258 toend = it->glyph_row->glyphs[TEXT_AREA];
18259 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18260
18261 while (from >= end && to >= toend)
18262 *to-- = *from--;
18263 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18264 {
18265 from =
18266 truncate_it.glyph_row->glyphs[TEXT_AREA]
18267 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18268 while (from >= end && to >= toend)
18269 *to-- = *from--;
18270 }
18271 if (from >= end)
18272 {
18273 /* Need to free some room before prepending additional
18274 glyphs. */
18275 int move_by = from - end + 1;
18276 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18277 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18278
18279 for ( ; g >= g0; g--)
18280 g[move_by] = *g;
18281 while (from >= end)
18282 *to-- = *from--;
18283 it->glyph_row->used[TEXT_AREA] += move_by;
18284 }
18285 }
18286 }
18287
18288 /* Compute the hash code for ROW. */
18289 unsigned
18290 row_hash (struct glyph_row *row)
18291 {
18292 int area, k;
18293 unsigned hashval = 0;
18294
18295 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18296 for (k = 0; k < row->used[area]; ++k)
18297 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18298 + row->glyphs[area][k].u.val
18299 + row->glyphs[area][k].face_id
18300 + row->glyphs[area][k].padding_p
18301 + (row->glyphs[area][k].type << 2));
18302
18303 return hashval;
18304 }
18305
18306 /* Compute the pixel height and width of IT->glyph_row.
18307
18308 Most of the time, ascent and height of a display line will be equal
18309 to the max_ascent and max_height values of the display iterator
18310 structure. This is not the case if
18311
18312 1. We hit ZV without displaying anything. In this case, max_ascent
18313 and max_height will be zero.
18314
18315 2. We have some glyphs that don't contribute to the line height.
18316 (The glyph row flag contributes_to_line_height_p is for future
18317 pixmap extensions).
18318
18319 The first case is easily covered by using default values because in
18320 these cases, the line height does not really matter, except that it
18321 must not be zero. */
18322
18323 static void
18324 compute_line_metrics (struct it *it)
18325 {
18326 struct glyph_row *row = it->glyph_row;
18327
18328 if (FRAME_WINDOW_P (it->f))
18329 {
18330 int i, min_y, max_y;
18331
18332 /* The line may consist of one space only, that was added to
18333 place the cursor on it. If so, the row's height hasn't been
18334 computed yet. */
18335 if (row->height == 0)
18336 {
18337 if (it->max_ascent + it->max_descent == 0)
18338 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18339 row->ascent = it->max_ascent;
18340 row->height = it->max_ascent + it->max_descent;
18341 row->phys_ascent = it->max_phys_ascent;
18342 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18343 row->extra_line_spacing = it->max_extra_line_spacing;
18344 }
18345
18346 /* Compute the width of this line. */
18347 row->pixel_width = row->x;
18348 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18349 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18350
18351 eassert (row->pixel_width >= 0);
18352 eassert (row->ascent >= 0 && row->height > 0);
18353
18354 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18355 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18356
18357 /* If first line's physical ascent is larger than its logical
18358 ascent, use the physical ascent, and make the row taller.
18359 This makes accented characters fully visible. */
18360 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18361 && row->phys_ascent > row->ascent)
18362 {
18363 row->height += row->phys_ascent - row->ascent;
18364 row->ascent = row->phys_ascent;
18365 }
18366
18367 /* Compute how much of the line is visible. */
18368 row->visible_height = row->height;
18369
18370 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18371 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18372
18373 if (row->y < min_y)
18374 row->visible_height -= min_y - row->y;
18375 if (row->y + row->height > max_y)
18376 row->visible_height -= row->y + row->height - max_y;
18377 }
18378 else
18379 {
18380 row->pixel_width = row->used[TEXT_AREA];
18381 if (row->continued_p)
18382 row->pixel_width -= it->continuation_pixel_width;
18383 else if (row->truncated_on_right_p)
18384 row->pixel_width -= it->truncation_pixel_width;
18385 row->ascent = row->phys_ascent = 0;
18386 row->height = row->phys_height = row->visible_height = 1;
18387 row->extra_line_spacing = 0;
18388 }
18389
18390 /* Compute a hash code for this row. */
18391 row->hash = row_hash (row);
18392
18393 it->max_ascent = it->max_descent = 0;
18394 it->max_phys_ascent = it->max_phys_descent = 0;
18395 }
18396
18397
18398 /* Append one space to the glyph row of iterator IT if doing a
18399 window-based redisplay. The space has the same face as
18400 IT->face_id. Value is non-zero if a space was added.
18401
18402 This function is called to make sure that there is always one glyph
18403 at the end of a glyph row that the cursor can be set on under
18404 window-systems. (If there weren't such a glyph we would not know
18405 how wide and tall a box cursor should be displayed).
18406
18407 At the same time this space let's a nicely handle clearing to the
18408 end of the line if the row ends in italic text. */
18409
18410 static int
18411 append_space_for_newline (struct it *it, int default_face_p)
18412 {
18413 if (FRAME_WINDOW_P (it->f))
18414 {
18415 int n = it->glyph_row->used[TEXT_AREA];
18416
18417 if (it->glyph_row->glyphs[TEXT_AREA] + n
18418 < it->glyph_row->glyphs[1 + TEXT_AREA])
18419 {
18420 /* Save some values that must not be changed.
18421 Must save IT->c and IT->len because otherwise
18422 ITERATOR_AT_END_P wouldn't work anymore after
18423 append_space_for_newline has been called. */
18424 enum display_element_type saved_what = it->what;
18425 int saved_c = it->c, saved_len = it->len;
18426 int saved_char_to_display = it->char_to_display;
18427 int saved_x = it->current_x;
18428 int saved_face_id = it->face_id;
18429 struct text_pos saved_pos;
18430 Lisp_Object saved_object;
18431 struct face *face;
18432
18433 saved_object = it->object;
18434 saved_pos = it->position;
18435
18436 it->what = IT_CHARACTER;
18437 memset (&it->position, 0, sizeof it->position);
18438 it->object = make_number (0);
18439 it->c = it->char_to_display = ' ';
18440 it->len = 1;
18441
18442 /* If the default face was remapped, be sure to use the
18443 remapped face for the appended newline. */
18444 if (default_face_p)
18445 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18446 else if (it->face_before_selective_p)
18447 it->face_id = it->saved_face_id;
18448 face = FACE_FROM_ID (it->f, it->face_id);
18449 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18450
18451 PRODUCE_GLYPHS (it);
18452
18453 it->override_ascent = -1;
18454 it->constrain_row_ascent_descent_p = 0;
18455 it->current_x = saved_x;
18456 it->object = saved_object;
18457 it->position = saved_pos;
18458 it->what = saved_what;
18459 it->face_id = saved_face_id;
18460 it->len = saved_len;
18461 it->c = saved_c;
18462 it->char_to_display = saved_char_to_display;
18463 return 1;
18464 }
18465 }
18466
18467 return 0;
18468 }
18469
18470
18471 /* Extend the face of the last glyph in the text area of IT->glyph_row
18472 to the end of the display line. Called from display_line. If the
18473 glyph row is empty, add a space glyph to it so that we know the
18474 face to draw. Set the glyph row flag fill_line_p. If the glyph
18475 row is R2L, prepend a stretch glyph to cover the empty space to the
18476 left of the leftmost glyph. */
18477
18478 static void
18479 extend_face_to_end_of_line (struct it *it)
18480 {
18481 struct face *face, *default_face;
18482 struct frame *f = it->f;
18483
18484 /* If line is already filled, do nothing. Non window-system frames
18485 get a grace of one more ``pixel'' because their characters are
18486 1-``pixel'' wide, so they hit the equality too early. This grace
18487 is needed only for R2L rows that are not continued, to produce
18488 one extra blank where we could display the cursor. */
18489 if (it->current_x >= it->last_visible_x
18490 + (!FRAME_WINDOW_P (f)
18491 && it->glyph_row->reversed_p
18492 && !it->glyph_row->continued_p))
18493 return;
18494
18495 /* The default face, possibly remapped. */
18496 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18497
18498 /* Face extension extends the background and box of IT->face_id
18499 to the end of the line. If the background equals the background
18500 of the frame, we don't have to do anything. */
18501 if (it->face_before_selective_p)
18502 face = FACE_FROM_ID (f, it->saved_face_id);
18503 else
18504 face = FACE_FROM_ID (f, it->face_id);
18505
18506 if (FRAME_WINDOW_P (f)
18507 && it->glyph_row->displays_text_p
18508 && face->box == FACE_NO_BOX
18509 && face->background == FRAME_BACKGROUND_PIXEL (f)
18510 && !face->stipple
18511 && !it->glyph_row->reversed_p)
18512 return;
18513
18514 /* Set the glyph row flag indicating that the face of the last glyph
18515 in the text area has to be drawn to the end of the text area. */
18516 it->glyph_row->fill_line_p = 1;
18517
18518 /* If current character of IT is not ASCII, make sure we have the
18519 ASCII face. This will be automatically undone the next time
18520 get_next_display_element returns a multibyte character. Note
18521 that the character will always be single byte in unibyte
18522 text. */
18523 if (!ASCII_CHAR_P (it->c))
18524 {
18525 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18526 }
18527
18528 if (FRAME_WINDOW_P (f))
18529 {
18530 /* If the row is empty, add a space with the current face of IT,
18531 so that we know which face to draw. */
18532 if (it->glyph_row->used[TEXT_AREA] == 0)
18533 {
18534 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18535 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18536 it->glyph_row->used[TEXT_AREA] = 1;
18537 }
18538 #ifdef HAVE_WINDOW_SYSTEM
18539 if (it->glyph_row->reversed_p)
18540 {
18541 /* Prepend a stretch glyph to the row, such that the
18542 rightmost glyph will be drawn flushed all the way to the
18543 right margin of the window. The stretch glyph that will
18544 occupy the empty space, if any, to the left of the
18545 glyphs. */
18546 struct font *font = face->font ? face->font : FRAME_FONT (f);
18547 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18548 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18549 struct glyph *g;
18550 int row_width, stretch_ascent, stretch_width;
18551 struct text_pos saved_pos;
18552 int saved_face_id, saved_avoid_cursor;
18553
18554 for (row_width = 0, g = row_start; g < row_end; g++)
18555 row_width += g->pixel_width;
18556 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18557 if (stretch_width > 0)
18558 {
18559 stretch_ascent =
18560 (((it->ascent + it->descent)
18561 * FONT_BASE (font)) / FONT_HEIGHT (font));
18562 saved_pos = it->position;
18563 memset (&it->position, 0, sizeof it->position);
18564 saved_avoid_cursor = it->avoid_cursor_p;
18565 it->avoid_cursor_p = 1;
18566 saved_face_id = it->face_id;
18567 /* The last row's stretch glyph should get the default
18568 face, to avoid painting the rest of the window with
18569 the region face, if the region ends at ZV. */
18570 if (it->glyph_row->ends_at_zv_p)
18571 it->face_id = default_face->id;
18572 else
18573 it->face_id = face->id;
18574 append_stretch_glyph (it, make_number (0), stretch_width,
18575 it->ascent + it->descent, stretch_ascent);
18576 it->position = saved_pos;
18577 it->avoid_cursor_p = saved_avoid_cursor;
18578 it->face_id = saved_face_id;
18579 }
18580 }
18581 #endif /* HAVE_WINDOW_SYSTEM */
18582 }
18583 else
18584 {
18585 /* Save some values that must not be changed. */
18586 int saved_x = it->current_x;
18587 struct text_pos saved_pos;
18588 Lisp_Object saved_object;
18589 enum display_element_type saved_what = it->what;
18590 int saved_face_id = it->face_id;
18591
18592 saved_object = it->object;
18593 saved_pos = it->position;
18594
18595 it->what = IT_CHARACTER;
18596 memset (&it->position, 0, sizeof it->position);
18597 it->object = make_number (0);
18598 it->c = it->char_to_display = ' ';
18599 it->len = 1;
18600 /* The last row's blank glyphs should get the default face, to
18601 avoid painting the rest of the window with the region face,
18602 if the region ends at ZV. */
18603 if (it->glyph_row->ends_at_zv_p)
18604 it->face_id = default_face->id;
18605 else
18606 it->face_id = face->id;
18607
18608 PRODUCE_GLYPHS (it);
18609
18610 while (it->current_x <= it->last_visible_x)
18611 PRODUCE_GLYPHS (it);
18612
18613 /* Don't count these blanks really. It would let us insert a left
18614 truncation glyph below and make us set the cursor on them, maybe. */
18615 it->current_x = saved_x;
18616 it->object = saved_object;
18617 it->position = saved_pos;
18618 it->what = saved_what;
18619 it->face_id = saved_face_id;
18620 }
18621 }
18622
18623
18624 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18625 trailing whitespace. */
18626
18627 static int
18628 trailing_whitespace_p (ptrdiff_t charpos)
18629 {
18630 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18631 int c = 0;
18632
18633 while (bytepos < ZV_BYTE
18634 && (c = FETCH_CHAR (bytepos),
18635 c == ' ' || c == '\t'))
18636 ++bytepos;
18637
18638 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18639 {
18640 if (bytepos != PT_BYTE)
18641 return 1;
18642 }
18643 return 0;
18644 }
18645
18646
18647 /* Highlight trailing whitespace, if any, in ROW. */
18648
18649 static void
18650 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18651 {
18652 int used = row->used[TEXT_AREA];
18653
18654 if (used)
18655 {
18656 struct glyph *start = row->glyphs[TEXT_AREA];
18657 struct glyph *glyph = start + used - 1;
18658
18659 if (row->reversed_p)
18660 {
18661 /* Right-to-left rows need to be processed in the opposite
18662 direction, so swap the edge pointers. */
18663 glyph = start;
18664 start = row->glyphs[TEXT_AREA] + used - 1;
18665 }
18666
18667 /* Skip over glyphs inserted to display the cursor at the
18668 end of a line, for extending the face of the last glyph
18669 to the end of the line on terminals, and for truncation
18670 and continuation glyphs. */
18671 if (!row->reversed_p)
18672 {
18673 while (glyph >= start
18674 && glyph->type == CHAR_GLYPH
18675 && INTEGERP (glyph->object))
18676 --glyph;
18677 }
18678 else
18679 {
18680 while (glyph <= start
18681 && glyph->type == CHAR_GLYPH
18682 && INTEGERP (glyph->object))
18683 ++glyph;
18684 }
18685
18686 /* If last glyph is a space or stretch, and it's trailing
18687 whitespace, set the face of all trailing whitespace glyphs in
18688 IT->glyph_row to `trailing-whitespace'. */
18689 if ((row->reversed_p ? glyph <= start : glyph >= start)
18690 && BUFFERP (glyph->object)
18691 && (glyph->type == STRETCH_GLYPH
18692 || (glyph->type == CHAR_GLYPH
18693 && glyph->u.ch == ' '))
18694 && trailing_whitespace_p (glyph->charpos))
18695 {
18696 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18697 if (face_id < 0)
18698 return;
18699
18700 if (!row->reversed_p)
18701 {
18702 while (glyph >= start
18703 && BUFFERP (glyph->object)
18704 && (glyph->type == STRETCH_GLYPH
18705 || (glyph->type == CHAR_GLYPH
18706 && glyph->u.ch == ' ')))
18707 (glyph--)->face_id = face_id;
18708 }
18709 else
18710 {
18711 while (glyph <= start
18712 && BUFFERP (glyph->object)
18713 && (glyph->type == STRETCH_GLYPH
18714 || (glyph->type == CHAR_GLYPH
18715 && glyph->u.ch == ' ')))
18716 (glyph++)->face_id = face_id;
18717 }
18718 }
18719 }
18720 }
18721
18722
18723 /* Value is non-zero if glyph row ROW should be
18724 used to hold the cursor. */
18725
18726 static int
18727 cursor_row_p (struct glyph_row *row)
18728 {
18729 int result = 1;
18730
18731 if (PT == CHARPOS (row->end.pos)
18732 || PT == MATRIX_ROW_END_CHARPOS (row))
18733 {
18734 /* Suppose the row ends on a string.
18735 Unless the row is continued, that means it ends on a newline
18736 in the string. If it's anything other than a display string
18737 (e.g., a before-string from an overlay), we don't want the
18738 cursor there. (This heuristic seems to give the optimal
18739 behavior for the various types of multi-line strings.)
18740 One exception: if the string has `cursor' property on one of
18741 its characters, we _do_ want the cursor there. */
18742 if (CHARPOS (row->end.string_pos) >= 0)
18743 {
18744 if (row->continued_p)
18745 result = 1;
18746 else
18747 {
18748 /* Check for `display' property. */
18749 struct glyph *beg = row->glyphs[TEXT_AREA];
18750 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18751 struct glyph *glyph;
18752
18753 result = 0;
18754 for (glyph = end; glyph >= beg; --glyph)
18755 if (STRINGP (glyph->object))
18756 {
18757 Lisp_Object prop
18758 = Fget_char_property (make_number (PT),
18759 Qdisplay, Qnil);
18760 result =
18761 (!NILP (prop)
18762 && display_prop_string_p (prop, glyph->object));
18763 /* If there's a `cursor' property on one of the
18764 string's characters, this row is a cursor row,
18765 even though this is not a display string. */
18766 if (!result)
18767 {
18768 Lisp_Object s = glyph->object;
18769
18770 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18771 {
18772 ptrdiff_t gpos = glyph->charpos;
18773
18774 if (!NILP (Fget_char_property (make_number (gpos),
18775 Qcursor, s)))
18776 {
18777 result = 1;
18778 break;
18779 }
18780 }
18781 }
18782 break;
18783 }
18784 }
18785 }
18786 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18787 {
18788 /* If the row ends in middle of a real character,
18789 and the line is continued, we want the cursor here.
18790 That's because CHARPOS (ROW->end.pos) would equal
18791 PT if PT is before the character. */
18792 if (!row->ends_in_ellipsis_p)
18793 result = row->continued_p;
18794 else
18795 /* If the row ends in an ellipsis, then
18796 CHARPOS (ROW->end.pos) will equal point after the
18797 invisible text. We want that position to be displayed
18798 after the ellipsis. */
18799 result = 0;
18800 }
18801 /* If the row ends at ZV, display the cursor at the end of that
18802 row instead of at the start of the row below. */
18803 else if (row->ends_at_zv_p)
18804 result = 1;
18805 else
18806 result = 0;
18807 }
18808
18809 return result;
18810 }
18811
18812 \f
18813
18814 /* Push the property PROP so that it will be rendered at the current
18815 position in IT. Return 1 if PROP was successfully pushed, 0
18816 otherwise. Called from handle_line_prefix to handle the
18817 `line-prefix' and `wrap-prefix' properties. */
18818
18819 static int
18820 push_prefix_prop (struct it *it, Lisp_Object prop)
18821 {
18822 struct text_pos pos =
18823 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18824
18825 eassert (it->method == GET_FROM_BUFFER
18826 || it->method == GET_FROM_DISPLAY_VECTOR
18827 || it->method == GET_FROM_STRING);
18828
18829 /* We need to save the current buffer/string position, so it will be
18830 restored by pop_it, because iterate_out_of_display_property
18831 depends on that being set correctly, but some situations leave
18832 it->position not yet set when this function is called. */
18833 push_it (it, &pos);
18834
18835 if (STRINGP (prop))
18836 {
18837 if (SCHARS (prop) == 0)
18838 {
18839 pop_it (it);
18840 return 0;
18841 }
18842
18843 it->string = prop;
18844 it->string_from_prefix_prop_p = 1;
18845 it->multibyte_p = STRING_MULTIBYTE (it->string);
18846 it->current.overlay_string_index = -1;
18847 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18848 it->end_charpos = it->string_nchars = SCHARS (it->string);
18849 it->method = GET_FROM_STRING;
18850 it->stop_charpos = 0;
18851 it->prev_stop = 0;
18852 it->base_level_stop = 0;
18853
18854 /* Force paragraph direction to be that of the parent
18855 buffer/string. */
18856 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18857 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18858 else
18859 it->paragraph_embedding = L2R;
18860
18861 /* Set up the bidi iterator for this display string. */
18862 if (it->bidi_p)
18863 {
18864 it->bidi_it.string.lstring = it->string;
18865 it->bidi_it.string.s = NULL;
18866 it->bidi_it.string.schars = it->end_charpos;
18867 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18868 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18869 it->bidi_it.string.unibyte = !it->multibyte_p;
18870 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18871 }
18872 }
18873 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18874 {
18875 it->method = GET_FROM_STRETCH;
18876 it->object = prop;
18877 }
18878 #ifdef HAVE_WINDOW_SYSTEM
18879 else if (IMAGEP (prop))
18880 {
18881 it->what = IT_IMAGE;
18882 it->image_id = lookup_image (it->f, prop);
18883 it->method = GET_FROM_IMAGE;
18884 }
18885 #endif /* HAVE_WINDOW_SYSTEM */
18886 else
18887 {
18888 pop_it (it); /* bogus display property, give up */
18889 return 0;
18890 }
18891
18892 return 1;
18893 }
18894
18895 /* Return the character-property PROP at the current position in IT. */
18896
18897 static Lisp_Object
18898 get_it_property (struct it *it, Lisp_Object prop)
18899 {
18900 Lisp_Object position;
18901
18902 if (STRINGP (it->object))
18903 position = make_number (IT_STRING_CHARPOS (*it));
18904 else if (BUFFERP (it->object))
18905 position = make_number (IT_CHARPOS (*it));
18906 else
18907 return Qnil;
18908
18909 return Fget_char_property (position, prop, it->object);
18910 }
18911
18912 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18913
18914 static void
18915 handle_line_prefix (struct it *it)
18916 {
18917 Lisp_Object prefix;
18918
18919 if (it->continuation_lines_width > 0)
18920 {
18921 prefix = get_it_property (it, Qwrap_prefix);
18922 if (NILP (prefix))
18923 prefix = Vwrap_prefix;
18924 }
18925 else
18926 {
18927 prefix = get_it_property (it, Qline_prefix);
18928 if (NILP (prefix))
18929 prefix = Vline_prefix;
18930 }
18931 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18932 {
18933 /* If the prefix is wider than the window, and we try to wrap
18934 it, it would acquire its own wrap prefix, and so on till the
18935 iterator stack overflows. So, don't wrap the prefix. */
18936 it->line_wrap = TRUNCATE;
18937 it->avoid_cursor_p = 1;
18938 }
18939 }
18940
18941 \f
18942
18943 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18944 only for R2L lines from display_line and display_string, when they
18945 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18946 the line/string needs to be continued on the next glyph row. */
18947 static void
18948 unproduce_glyphs (struct it *it, int n)
18949 {
18950 struct glyph *glyph, *end;
18951
18952 eassert (it->glyph_row);
18953 eassert (it->glyph_row->reversed_p);
18954 eassert (it->area == TEXT_AREA);
18955 eassert (n <= it->glyph_row->used[TEXT_AREA]);
18956
18957 if (n > it->glyph_row->used[TEXT_AREA])
18958 n = it->glyph_row->used[TEXT_AREA];
18959 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18960 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18961 for ( ; glyph < end; glyph++)
18962 glyph[-n] = *glyph;
18963 }
18964
18965 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18966 and ROW->maxpos. */
18967 static void
18968 find_row_edges (struct it *it, struct glyph_row *row,
18969 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18970 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18971 {
18972 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18973 lines' rows is implemented for bidi-reordered rows. */
18974
18975 /* ROW->minpos is the value of min_pos, the minimal buffer position
18976 we have in ROW, or ROW->start.pos if that is smaller. */
18977 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18978 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18979 else
18980 /* We didn't find buffer positions smaller than ROW->start, or
18981 didn't find _any_ valid buffer positions in any of the glyphs,
18982 so we must trust the iterator's computed positions. */
18983 row->minpos = row->start.pos;
18984 if (max_pos <= 0)
18985 {
18986 max_pos = CHARPOS (it->current.pos);
18987 max_bpos = BYTEPOS (it->current.pos);
18988 }
18989
18990 /* Here are the various use-cases for ending the row, and the
18991 corresponding values for ROW->maxpos:
18992
18993 Line ends in a newline from buffer eol_pos + 1
18994 Line is continued from buffer max_pos + 1
18995 Line is truncated on right it->current.pos
18996 Line ends in a newline from string max_pos + 1(*)
18997 (*) + 1 only when line ends in a forward scan
18998 Line is continued from string max_pos
18999 Line is continued from display vector max_pos
19000 Line is entirely from a string min_pos == max_pos
19001 Line is entirely from a display vector min_pos == max_pos
19002 Line that ends at ZV ZV
19003
19004 If you discover other use-cases, please add them here as
19005 appropriate. */
19006 if (row->ends_at_zv_p)
19007 row->maxpos = it->current.pos;
19008 else if (row->used[TEXT_AREA])
19009 {
19010 int seen_this_string = 0;
19011 struct glyph_row *r1 = row - 1;
19012
19013 /* Did we see the same display string on the previous row? */
19014 if (STRINGP (it->object)
19015 /* this is not the first row */
19016 && row > it->w->desired_matrix->rows
19017 /* previous row is not the header line */
19018 && !r1->mode_line_p
19019 /* previous row also ends in a newline from a string */
19020 && r1->ends_in_newline_from_string_p)
19021 {
19022 struct glyph *start, *end;
19023
19024 /* Search for the last glyph of the previous row that came
19025 from buffer or string. Depending on whether the row is
19026 L2R or R2L, we need to process it front to back or the
19027 other way round. */
19028 if (!r1->reversed_p)
19029 {
19030 start = r1->glyphs[TEXT_AREA];
19031 end = start + r1->used[TEXT_AREA];
19032 /* Glyphs inserted by redisplay have an integer (zero)
19033 as their object. */
19034 while (end > start
19035 && INTEGERP ((end - 1)->object)
19036 && (end - 1)->charpos <= 0)
19037 --end;
19038 if (end > start)
19039 {
19040 if (EQ ((end - 1)->object, it->object))
19041 seen_this_string = 1;
19042 }
19043 else
19044 /* If all the glyphs of the previous row were inserted
19045 by redisplay, it means the previous row was
19046 produced from a single newline, which is only
19047 possible if that newline came from the same string
19048 as the one which produced this ROW. */
19049 seen_this_string = 1;
19050 }
19051 else
19052 {
19053 end = r1->glyphs[TEXT_AREA] - 1;
19054 start = end + r1->used[TEXT_AREA];
19055 while (end < start
19056 && INTEGERP ((end + 1)->object)
19057 && (end + 1)->charpos <= 0)
19058 ++end;
19059 if (end < start)
19060 {
19061 if (EQ ((end + 1)->object, it->object))
19062 seen_this_string = 1;
19063 }
19064 else
19065 seen_this_string = 1;
19066 }
19067 }
19068 /* Take note of each display string that covers a newline only
19069 once, the first time we see it. This is for when a display
19070 string includes more than one newline in it. */
19071 if (row->ends_in_newline_from_string_p && !seen_this_string)
19072 {
19073 /* If we were scanning the buffer forward when we displayed
19074 the string, we want to account for at least one buffer
19075 position that belongs to this row (position covered by
19076 the display string), so that cursor positioning will
19077 consider this row as a candidate when point is at the end
19078 of the visual line represented by this row. This is not
19079 required when scanning back, because max_pos will already
19080 have a much larger value. */
19081 if (CHARPOS (row->end.pos) > max_pos)
19082 INC_BOTH (max_pos, max_bpos);
19083 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19084 }
19085 else if (CHARPOS (it->eol_pos) > 0)
19086 SET_TEXT_POS (row->maxpos,
19087 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19088 else if (row->continued_p)
19089 {
19090 /* If max_pos is different from IT's current position, it
19091 means IT->method does not belong to the display element
19092 at max_pos. However, it also means that the display
19093 element at max_pos was displayed in its entirety on this
19094 line, which is equivalent to saying that the next line
19095 starts at the next buffer position. */
19096 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19097 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19098 else
19099 {
19100 INC_BOTH (max_pos, max_bpos);
19101 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19102 }
19103 }
19104 else if (row->truncated_on_right_p)
19105 /* display_line already called reseat_at_next_visible_line_start,
19106 which puts the iterator at the beginning of the next line, in
19107 the logical order. */
19108 row->maxpos = it->current.pos;
19109 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19110 /* A line that is entirely from a string/image/stretch... */
19111 row->maxpos = row->minpos;
19112 else
19113 abort ();
19114 }
19115 else
19116 row->maxpos = it->current.pos;
19117 }
19118
19119 /* Construct the glyph row IT->glyph_row in the desired matrix of
19120 IT->w from text at the current position of IT. See dispextern.h
19121 for an overview of struct it. Value is non-zero if
19122 IT->glyph_row displays text, as opposed to a line displaying ZV
19123 only. */
19124
19125 static int
19126 display_line (struct it *it)
19127 {
19128 struct glyph_row *row = it->glyph_row;
19129 Lisp_Object overlay_arrow_string;
19130 struct it wrap_it;
19131 void *wrap_data = NULL;
19132 int may_wrap = 0, wrap_x IF_LINT (= 0);
19133 int wrap_row_used = -1;
19134 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19135 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19136 int wrap_row_extra_line_spacing IF_LINT (= 0);
19137 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19138 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19139 int cvpos;
19140 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19141 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19142
19143 /* We always start displaying at hpos zero even if hscrolled. */
19144 eassert (it->hpos == 0 && it->current_x == 0);
19145
19146 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19147 >= it->w->desired_matrix->nrows)
19148 {
19149 it->w->nrows_scale_factor++;
19150 fonts_changed_p = 1;
19151 return 0;
19152 }
19153
19154 /* Is IT->w showing the region? */
19155 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19156
19157 /* Clear the result glyph row and enable it. */
19158 prepare_desired_row (row);
19159
19160 row->y = it->current_y;
19161 row->start = it->start;
19162 row->continuation_lines_width = it->continuation_lines_width;
19163 row->displays_text_p = 1;
19164 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19165 it->starts_in_middle_of_char_p = 0;
19166
19167 /* Arrange the overlays nicely for our purposes. Usually, we call
19168 display_line on only one line at a time, in which case this
19169 can't really hurt too much, or we call it on lines which appear
19170 one after another in the buffer, in which case all calls to
19171 recenter_overlay_lists but the first will be pretty cheap. */
19172 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19173
19174 /* Move over display elements that are not visible because we are
19175 hscrolled. This may stop at an x-position < IT->first_visible_x
19176 if the first glyph is partially visible or if we hit a line end. */
19177 if (it->current_x < it->first_visible_x)
19178 {
19179 enum move_it_result move_result;
19180
19181 this_line_min_pos = row->start.pos;
19182 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19183 MOVE_TO_POS | MOVE_TO_X);
19184 /* If we are under a large hscroll, move_it_in_display_line_to
19185 could hit the end of the line without reaching
19186 it->first_visible_x. Pretend that we did reach it. This is
19187 especially important on a TTY, where we will call
19188 extend_face_to_end_of_line, which needs to know how many
19189 blank glyphs to produce. */
19190 if (it->current_x < it->first_visible_x
19191 && (move_result == MOVE_NEWLINE_OR_CR
19192 || move_result == MOVE_POS_MATCH_OR_ZV))
19193 it->current_x = it->first_visible_x;
19194
19195 /* Record the smallest positions seen while we moved over
19196 display elements that are not visible. This is needed by
19197 redisplay_internal for optimizing the case where the cursor
19198 stays inside the same line. The rest of this function only
19199 considers positions that are actually displayed, so
19200 RECORD_MAX_MIN_POS will not otherwise record positions that
19201 are hscrolled to the left of the left edge of the window. */
19202 min_pos = CHARPOS (this_line_min_pos);
19203 min_bpos = BYTEPOS (this_line_min_pos);
19204 }
19205 else
19206 {
19207 /* We only do this when not calling `move_it_in_display_line_to'
19208 above, because move_it_in_display_line_to calls
19209 handle_line_prefix itself. */
19210 handle_line_prefix (it);
19211 }
19212
19213 /* Get the initial row height. This is either the height of the
19214 text hscrolled, if there is any, or zero. */
19215 row->ascent = it->max_ascent;
19216 row->height = it->max_ascent + it->max_descent;
19217 row->phys_ascent = it->max_phys_ascent;
19218 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19219 row->extra_line_spacing = it->max_extra_line_spacing;
19220
19221 /* Utility macro to record max and min buffer positions seen until now. */
19222 #define RECORD_MAX_MIN_POS(IT) \
19223 do \
19224 { \
19225 int composition_p = !STRINGP ((IT)->string) \
19226 && ((IT)->what == IT_COMPOSITION); \
19227 ptrdiff_t current_pos = \
19228 composition_p ? (IT)->cmp_it.charpos \
19229 : IT_CHARPOS (*(IT)); \
19230 ptrdiff_t current_bpos = \
19231 composition_p ? CHAR_TO_BYTE (current_pos) \
19232 : IT_BYTEPOS (*(IT)); \
19233 if (current_pos < min_pos) \
19234 { \
19235 min_pos = current_pos; \
19236 min_bpos = current_bpos; \
19237 } \
19238 if (IT_CHARPOS (*it) > max_pos) \
19239 { \
19240 max_pos = IT_CHARPOS (*it); \
19241 max_bpos = IT_BYTEPOS (*it); \
19242 } \
19243 } \
19244 while (0)
19245
19246 /* Loop generating characters. The loop is left with IT on the next
19247 character to display. */
19248 while (1)
19249 {
19250 int n_glyphs_before, hpos_before, x_before;
19251 int x, nglyphs;
19252 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19253
19254 /* Retrieve the next thing to display. Value is zero if end of
19255 buffer reached. */
19256 if (!get_next_display_element (it))
19257 {
19258 /* Maybe add a space at the end of this line that is used to
19259 display the cursor there under X. Set the charpos of the
19260 first glyph of blank lines not corresponding to any text
19261 to -1. */
19262 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19263 row->exact_window_width_line_p = 1;
19264 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19265 || row->used[TEXT_AREA] == 0)
19266 {
19267 row->glyphs[TEXT_AREA]->charpos = -1;
19268 row->displays_text_p = 0;
19269
19270 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19271 && (!MINI_WINDOW_P (it->w)
19272 || (minibuf_level && EQ (it->window, minibuf_window))))
19273 row->indicate_empty_line_p = 1;
19274 }
19275
19276 it->continuation_lines_width = 0;
19277 row->ends_at_zv_p = 1;
19278 /* A row that displays right-to-left text must always have
19279 its last face extended all the way to the end of line,
19280 even if this row ends in ZV, because we still write to
19281 the screen left to right. We also need to extend the
19282 last face if the default face is remapped to some
19283 different face, otherwise the functions that clear
19284 portions of the screen will clear with the default face's
19285 background color. */
19286 if (row->reversed_p
19287 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19288 extend_face_to_end_of_line (it);
19289 break;
19290 }
19291
19292 /* Now, get the metrics of what we want to display. This also
19293 generates glyphs in `row' (which is IT->glyph_row). */
19294 n_glyphs_before = row->used[TEXT_AREA];
19295 x = it->current_x;
19296
19297 /* Remember the line height so far in case the next element doesn't
19298 fit on the line. */
19299 if (it->line_wrap != TRUNCATE)
19300 {
19301 ascent = it->max_ascent;
19302 descent = it->max_descent;
19303 phys_ascent = it->max_phys_ascent;
19304 phys_descent = it->max_phys_descent;
19305
19306 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19307 {
19308 if (IT_DISPLAYING_WHITESPACE (it))
19309 may_wrap = 1;
19310 else if (may_wrap)
19311 {
19312 SAVE_IT (wrap_it, *it, wrap_data);
19313 wrap_x = x;
19314 wrap_row_used = row->used[TEXT_AREA];
19315 wrap_row_ascent = row->ascent;
19316 wrap_row_height = row->height;
19317 wrap_row_phys_ascent = row->phys_ascent;
19318 wrap_row_phys_height = row->phys_height;
19319 wrap_row_extra_line_spacing = row->extra_line_spacing;
19320 wrap_row_min_pos = min_pos;
19321 wrap_row_min_bpos = min_bpos;
19322 wrap_row_max_pos = max_pos;
19323 wrap_row_max_bpos = max_bpos;
19324 may_wrap = 0;
19325 }
19326 }
19327 }
19328
19329 PRODUCE_GLYPHS (it);
19330
19331 /* If this display element was in marginal areas, continue with
19332 the next one. */
19333 if (it->area != TEXT_AREA)
19334 {
19335 row->ascent = max (row->ascent, it->max_ascent);
19336 row->height = max (row->height, it->max_ascent + it->max_descent);
19337 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19338 row->phys_height = max (row->phys_height,
19339 it->max_phys_ascent + it->max_phys_descent);
19340 row->extra_line_spacing = max (row->extra_line_spacing,
19341 it->max_extra_line_spacing);
19342 set_iterator_to_next (it, 1);
19343 continue;
19344 }
19345
19346 /* Does the display element fit on the line? If we truncate
19347 lines, we should draw past the right edge of the window. If
19348 we don't truncate, we want to stop so that we can display the
19349 continuation glyph before the right margin. If lines are
19350 continued, there are two possible strategies for characters
19351 resulting in more than 1 glyph (e.g. tabs): Display as many
19352 glyphs as possible in this line and leave the rest for the
19353 continuation line, or display the whole element in the next
19354 line. Original redisplay did the former, so we do it also. */
19355 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19356 hpos_before = it->hpos;
19357 x_before = x;
19358
19359 if (/* Not a newline. */
19360 nglyphs > 0
19361 /* Glyphs produced fit entirely in the line. */
19362 && it->current_x < it->last_visible_x)
19363 {
19364 it->hpos += nglyphs;
19365 row->ascent = max (row->ascent, it->max_ascent);
19366 row->height = max (row->height, it->max_ascent + it->max_descent);
19367 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19368 row->phys_height = max (row->phys_height,
19369 it->max_phys_ascent + it->max_phys_descent);
19370 row->extra_line_spacing = max (row->extra_line_spacing,
19371 it->max_extra_line_spacing);
19372 if (it->current_x - it->pixel_width < it->first_visible_x)
19373 row->x = x - it->first_visible_x;
19374 /* Record the maximum and minimum buffer positions seen so
19375 far in glyphs that will be displayed by this row. */
19376 if (it->bidi_p)
19377 RECORD_MAX_MIN_POS (it);
19378 }
19379 else
19380 {
19381 int i, new_x;
19382 struct glyph *glyph;
19383
19384 for (i = 0; i < nglyphs; ++i, x = new_x)
19385 {
19386 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19387 new_x = x + glyph->pixel_width;
19388
19389 if (/* Lines are continued. */
19390 it->line_wrap != TRUNCATE
19391 && (/* Glyph doesn't fit on the line. */
19392 new_x > it->last_visible_x
19393 /* Or it fits exactly on a window system frame. */
19394 || (new_x == it->last_visible_x
19395 && FRAME_WINDOW_P (it->f))))
19396 {
19397 /* End of a continued line. */
19398
19399 if (it->hpos == 0
19400 || (new_x == it->last_visible_x
19401 && FRAME_WINDOW_P (it->f)))
19402 {
19403 /* Current glyph is the only one on the line or
19404 fits exactly on the line. We must continue
19405 the line because we can't draw the cursor
19406 after the glyph. */
19407 row->continued_p = 1;
19408 it->current_x = new_x;
19409 it->continuation_lines_width += new_x;
19410 ++it->hpos;
19411 if (i == nglyphs - 1)
19412 {
19413 /* If line-wrap is on, check if a previous
19414 wrap point was found. */
19415 if (wrap_row_used > 0
19416 /* Even if there is a previous wrap
19417 point, continue the line here as
19418 usual, if (i) the previous character
19419 was a space or tab AND (ii) the
19420 current character is not. */
19421 && (!may_wrap
19422 || IT_DISPLAYING_WHITESPACE (it)))
19423 goto back_to_wrap;
19424
19425 /* Record the maximum and minimum buffer
19426 positions seen so far in glyphs that will be
19427 displayed by this row. */
19428 if (it->bidi_p)
19429 RECORD_MAX_MIN_POS (it);
19430 set_iterator_to_next (it, 1);
19431 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19432 {
19433 if (!get_next_display_element (it))
19434 {
19435 row->exact_window_width_line_p = 1;
19436 it->continuation_lines_width = 0;
19437 row->continued_p = 0;
19438 row->ends_at_zv_p = 1;
19439 }
19440 else if (ITERATOR_AT_END_OF_LINE_P (it))
19441 {
19442 row->continued_p = 0;
19443 row->exact_window_width_line_p = 1;
19444 }
19445 }
19446 }
19447 else if (it->bidi_p)
19448 RECORD_MAX_MIN_POS (it);
19449 }
19450 else if (CHAR_GLYPH_PADDING_P (*glyph)
19451 && !FRAME_WINDOW_P (it->f))
19452 {
19453 /* A padding glyph that doesn't fit on this line.
19454 This means the whole character doesn't fit
19455 on the line. */
19456 if (row->reversed_p)
19457 unproduce_glyphs (it, row->used[TEXT_AREA]
19458 - n_glyphs_before);
19459 row->used[TEXT_AREA] = n_glyphs_before;
19460
19461 /* Fill the rest of the row with continuation
19462 glyphs like in 20.x. */
19463 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19464 < row->glyphs[1 + TEXT_AREA])
19465 produce_special_glyphs (it, IT_CONTINUATION);
19466
19467 row->continued_p = 1;
19468 it->current_x = x_before;
19469 it->continuation_lines_width += x_before;
19470
19471 /* Restore the height to what it was before the
19472 element not fitting on the line. */
19473 it->max_ascent = ascent;
19474 it->max_descent = descent;
19475 it->max_phys_ascent = phys_ascent;
19476 it->max_phys_descent = phys_descent;
19477 }
19478 else if (wrap_row_used > 0)
19479 {
19480 back_to_wrap:
19481 if (row->reversed_p)
19482 unproduce_glyphs (it,
19483 row->used[TEXT_AREA] - wrap_row_used);
19484 RESTORE_IT (it, &wrap_it, wrap_data);
19485 it->continuation_lines_width += wrap_x;
19486 row->used[TEXT_AREA] = wrap_row_used;
19487 row->ascent = wrap_row_ascent;
19488 row->height = wrap_row_height;
19489 row->phys_ascent = wrap_row_phys_ascent;
19490 row->phys_height = wrap_row_phys_height;
19491 row->extra_line_spacing = wrap_row_extra_line_spacing;
19492 min_pos = wrap_row_min_pos;
19493 min_bpos = wrap_row_min_bpos;
19494 max_pos = wrap_row_max_pos;
19495 max_bpos = wrap_row_max_bpos;
19496 row->continued_p = 1;
19497 row->ends_at_zv_p = 0;
19498 row->exact_window_width_line_p = 0;
19499 it->continuation_lines_width += x;
19500
19501 /* Make sure that a non-default face is extended
19502 up to the right margin of the window. */
19503 extend_face_to_end_of_line (it);
19504 }
19505 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19506 {
19507 /* A TAB that extends past the right edge of the
19508 window. This produces a single glyph on
19509 window system frames. We leave the glyph in
19510 this row and let it fill the row, but don't
19511 consume the TAB. */
19512 it->continuation_lines_width += it->last_visible_x;
19513 row->ends_in_middle_of_char_p = 1;
19514 row->continued_p = 1;
19515 glyph->pixel_width = it->last_visible_x - x;
19516 it->starts_in_middle_of_char_p = 1;
19517 }
19518 else
19519 {
19520 /* Something other than a TAB that draws past
19521 the right edge of the window. Restore
19522 positions to values before the element. */
19523 if (row->reversed_p)
19524 unproduce_glyphs (it, row->used[TEXT_AREA]
19525 - (n_glyphs_before + i));
19526 row->used[TEXT_AREA] = n_glyphs_before + i;
19527
19528 /* Display continuation glyphs. */
19529 if (!FRAME_WINDOW_P (it->f))
19530 produce_special_glyphs (it, IT_CONTINUATION);
19531 row->continued_p = 1;
19532
19533 it->current_x = x_before;
19534 it->continuation_lines_width += x;
19535 extend_face_to_end_of_line (it);
19536
19537 if (nglyphs > 1 && i > 0)
19538 {
19539 row->ends_in_middle_of_char_p = 1;
19540 it->starts_in_middle_of_char_p = 1;
19541 }
19542
19543 /* Restore the height to what it was before the
19544 element not fitting on the line. */
19545 it->max_ascent = ascent;
19546 it->max_descent = descent;
19547 it->max_phys_ascent = phys_ascent;
19548 it->max_phys_descent = phys_descent;
19549 }
19550
19551 break;
19552 }
19553 else if (new_x > it->first_visible_x)
19554 {
19555 /* Increment number of glyphs actually displayed. */
19556 ++it->hpos;
19557
19558 /* Record the maximum and minimum buffer positions
19559 seen so far in glyphs that will be displayed by
19560 this row. */
19561 if (it->bidi_p)
19562 RECORD_MAX_MIN_POS (it);
19563
19564 if (x < it->first_visible_x)
19565 /* Glyph is partially visible, i.e. row starts at
19566 negative X position. */
19567 row->x = x - it->first_visible_x;
19568 }
19569 else
19570 {
19571 /* Glyph is completely off the left margin of the
19572 window. This should not happen because of the
19573 move_it_in_display_line at the start of this
19574 function, unless the text display area of the
19575 window is empty. */
19576 eassert (it->first_visible_x <= it->last_visible_x);
19577 }
19578 }
19579 /* Even if this display element produced no glyphs at all,
19580 we want to record its position. */
19581 if (it->bidi_p && nglyphs == 0)
19582 RECORD_MAX_MIN_POS (it);
19583
19584 row->ascent = max (row->ascent, it->max_ascent);
19585 row->height = max (row->height, it->max_ascent + it->max_descent);
19586 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19587 row->phys_height = max (row->phys_height,
19588 it->max_phys_ascent + it->max_phys_descent);
19589 row->extra_line_spacing = max (row->extra_line_spacing,
19590 it->max_extra_line_spacing);
19591
19592 /* End of this display line if row is continued. */
19593 if (row->continued_p || row->ends_at_zv_p)
19594 break;
19595 }
19596
19597 at_end_of_line:
19598 /* Is this a line end? If yes, we're also done, after making
19599 sure that a non-default face is extended up to the right
19600 margin of the window. */
19601 if (ITERATOR_AT_END_OF_LINE_P (it))
19602 {
19603 int used_before = row->used[TEXT_AREA];
19604
19605 row->ends_in_newline_from_string_p = STRINGP (it->object);
19606
19607 /* Add a space at the end of the line that is used to
19608 display the cursor there. */
19609 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19610 append_space_for_newline (it, 0);
19611
19612 /* Extend the face to the end of the line. */
19613 extend_face_to_end_of_line (it);
19614
19615 /* Make sure we have the position. */
19616 if (used_before == 0)
19617 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19618
19619 /* Record the position of the newline, for use in
19620 find_row_edges. */
19621 it->eol_pos = it->current.pos;
19622
19623 /* Consume the line end. This skips over invisible lines. */
19624 set_iterator_to_next (it, 1);
19625 it->continuation_lines_width = 0;
19626 break;
19627 }
19628
19629 /* Proceed with next display element. Note that this skips
19630 over lines invisible because of selective display. */
19631 set_iterator_to_next (it, 1);
19632
19633 /* If we truncate lines, we are done when the last displayed
19634 glyphs reach past the right margin of the window. */
19635 if (it->line_wrap == TRUNCATE
19636 && (FRAME_WINDOW_P (it->f)
19637 ? (it->current_x >= it->last_visible_x)
19638 : (it->current_x > it->last_visible_x)))
19639 {
19640 /* Maybe add truncation glyphs. */
19641 if (!FRAME_WINDOW_P (it->f))
19642 {
19643 int i, n;
19644
19645 if (!row->reversed_p)
19646 {
19647 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19648 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19649 break;
19650 }
19651 else
19652 {
19653 for (i = 0; i < row->used[TEXT_AREA]; i++)
19654 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19655 break;
19656 /* Remove any padding glyphs at the front of ROW, to
19657 make room for the truncation glyphs we will be
19658 adding below. The loop below always inserts at
19659 least one truncation glyph, so also remove the
19660 last glyph added to ROW. */
19661 unproduce_glyphs (it, i + 1);
19662 /* Adjust i for the loop below. */
19663 i = row->used[TEXT_AREA] - (i + 1);
19664 }
19665
19666 for (n = row->used[TEXT_AREA]; i < n; ++i)
19667 {
19668 row->used[TEXT_AREA] = i;
19669 produce_special_glyphs (it, IT_TRUNCATION);
19670 }
19671 }
19672 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19673 {
19674 /* Don't truncate if we can overflow newline into fringe. */
19675 if (!get_next_display_element (it))
19676 {
19677 it->continuation_lines_width = 0;
19678 row->ends_at_zv_p = 1;
19679 row->exact_window_width_line_p = 1;
19680 break;
19681 }
19682 if (ITERATOR_AT_END_OF_LINE_P (it))
19683 {
19684 row->exact_window_width_line_p = 1;
19685 goto at_end_of_line;
19686 }
19687 }
19688
19689 row->truncated_on_right_p = 1;
19690 it->continuation_lines_width = 0;
19691 reseat_at_next_visible_line_start (it, 0);
19692 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19693 it->hpos = hpos_before;
19694 it->current_x = x_before;
19695 break;
19696 }
19697 }
19698
19699 if (wrap_data)
19700 bidi_unshelve_cache (wrap_data, 1);
19701
19702 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19703 at the left window margin. */
19704 if (it->first_visible_x
19705 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19706 {
19707 if (!FRAME_WINDOW_P (it->f))
19708 insert_left_trunc_glyphs (it);
19709 row->truncated_on_left_p = 1;
19710 }
19711
19712 /* Remember the position at which this line ends.
19713
19714 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19715 cannot be before the call to find_row_edges below, since that is
19716 where these positions are determined. */
19717 row->end = it->current;
19718 if (!it->bidi_p)
19719 {
19720 row->minpos = row->start.pos;
19721 row->maxpos = row->end.pos;
19722 }
19723 else
19724 {
19725 /* ROW->minpos and ROW->maxpos must be the smallest and
19726 `1 + the largest' buffer positions in ROW. But if ROW was
19727 bidi-reordered, these two positions can be anywhere in the
19728 row, so we must determine them now. */
19729 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19730 }
19731
19732 /* If the start of this line is the overlay arrow-position, then
19733 mark this glyph row as the one containing the overlay arrow.
19734 This is clearly a mess with variable size fonts. It would be
19735 better to let it be displayed like cursors under X. */
19736 if ((row->displays_text_p || !overlay_arrow_seen)
19737 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19738 !NILP (overlay_arrow_string)))
19739 {
19740 /* Overlay arrow in window redisplay is a fringe bitmap. */
19741 if (STRINGP (overlay_arrow_string))
19742 {
19743 struct glyph_row *arrow_row
19744 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19745 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19746 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19747 struct glyph *p = row->glyphs[TEXT_AREA];
19748 struct glyph *p2, *end;
19749
19750 /* Copy the arrow glyphs. */
19751 while (glyph < arrow_end)
19752 *p++ = *glyph++;
19753
19754 /* Throw away padding glyphs. */
19755 p2 = p;
19756 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19757 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19758 ++p2;
19759 if (p2 > p)
19760 {
19761 while (p2 < end)
19762 *p++ = *p2++;
19763 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19764 }
19765 }
19766 else
19767 {
19768 eassert (INTEGERP (overlay_arrow_string));
19769 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19770 }
19771 overlay_arrow_seen = 1;
19772 }
19773
19774 /* Highlight trailing whitespace. */
19775 if (!NILP (Vshow_trailing_whitespace))
19776 highlight_trailing_whitespace (it->f, it->glyph_row);
19777
19778 /* Compute pixel dimensions of this line. */
19779 compute_line_metrics (it);
19780
19781 /* Implementation note: No changes in the glyphs of ROW or in their
19782 faces can be done past this point, because compute_line_metrics
19783 computes ROW's hash value and stores it within the glyph_row
19784 structure. */
19785
19786 /* Record whether this row ends inside an ellipsis. */
19787 row->ends_in_ellipsis_p
19788 = (it->method == GET_FROM_DISPLAY_VECTOR
19789 && it->ellipsis_p);
19790
19791 /* Save fringe bitmaps in this row. */
19792 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19793 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19794 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19795 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19796
19797 it->left_user_fringe_bitmap = 0;
19798 it->left_user_fringe_face_id = 0;
19799 it->right_user_fringe_bitmap = 0;
19800 it->right_user_fringe_face_id = 0;
19801
19802 /* Maybe set the cursor. */
19803 cvpos = it->w->cursor.vpos;
19804 if ((cvpos < 0
19805 /* In bidi-reordered rows, keep checking for proper cursor
19806 position even if one has been found already, because buffer
19807 positions in such rows change non-linearly with ROW->VPOS,
19808 when a line is continued. One exception: when we are at ZV,
19809 display cursor on the first suitable glyph row, since all
19810 the empty rows after that also have their position set to ZV. */
19811 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19812 lines' rows is implemented for bidi-reordered rows. */
19813 || (it->bidi_p
19814 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19815 && PT >= MATRIX_ROW_START_CHARPOS (row)
19816 && PT <= MATRIX_ROW_END_CHARPOS (row)
19817 && cursor_row_p (row))
19818 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19819
19820 /* Prepare for the next line. This line starts horizontally at (X
19821 HPOS) = (0 0). Vertical positions are incremented. As a
19822 convenience for the caller, IT->glyph_row is set to the next
19823 row to be used. */
19824 it->current_x = it->hpos = 0;
19825 it->current_y += row->height;
19826 SET_TEXT_POS (it->eol_pos, 0, 0);
19827 ++it->vpos;
19828 ++it->glyph_row;
19829 /* The next row should by default use the same value of the
19830 reversed_p flag as this one. set_iterator_to_next decides when
19831 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19832 the flag accordingly. */
19833 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19834 it->glyph_row->reversed_p = row->reversed_p;
19835 it->start = row->end;
19836 return row->displays_text_p;
19837
19838 #undef RECORD_MAX_MIN_POS
19839 }
19840
19841 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19842 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19843 doc: /* Return paragraph direction at point in BUFFER.
19844 Value is either `left-to-right' or `right-to-left'.
19845 If BUFFER is omitted or nil, it defaults to the current buffer.
19846
19847 Paragraph direction determines how the text in the paragraph is displayed.
19848 In left-to-right paragraphs, text begins at the left margin of the window
19849 and the reading direction is generally left to right. In right-to-left
19850 paragraphs, text begins at the right margin and is read from right to left.
19851
19852 See also `bidi-paragraph-direction'. */)
19853 (Lisp_Object buffer)
19854 {
19855 struct buffer *buf = current_buffer;
19856 struct buffer *old = buf;
19857
19858 if (! NILP (buffer))
19859 {
19860 CHECK_BUFFER (buffer);
19861 buf = XBUFFER (buffer);
19862 }
19863
19864 if (NILP (BVAR (buf, bidi_display_reordering))
19865 || NILP (BVAR (buf, enable_multibyte_characters))
19866 /* When we are loading loadup.el, the character property tables
19867 needed for bidi iteration are not yet available. */
19868 || !NILP (Vpurify_flag))
19869 return Qleft_to_right;
19870 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19871 return BVAR (buf, bidi_paragraph_direction);
19872 else
19873 {
19874 /* Determine the direction from buffer text. We could try to
19875 use current_matrix if it is up to date, but this seems fast
19876 enough as it is. */
19877 struct bidi_it itb;
19878 ptrdiff_t pos = BUF_PT (buf);
19879 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19880 int c;
19881 void *itb_data = bidi_shelve_cache ();
19882
19883 set_buffer_temp (buf);
19884 /* bidi_paragraph_init finds the base direction of the paragraph
19885 by searching forward from paragraph start. We need the base
19886 direction of the current or _previous_ paragraph, so we need
19887 to make sure we are within that paragraph. To that end, find
19888 the previous non-empty line. */
19889 if (pos >= ZV && pos > BEGV)
19890 {
19891 pos--;
19892 bytepos = CHAR_TO_BYTE (pos);
19893 }
19894 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19895 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19896 {
19897 while ((c = FETCH_BYTE (bytepos)) == '\n'
19898 || c == ' ' || c == '\t' || c == '\f')
19899 {
19900 if (bytepos <= BEGV_BYTE)
19901 break;
19902 bytepos--;
19903 pos--;
19904 }
19905 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19906 bytepos--;
19907 }
19908 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19909 itb.paragraph_dir = NEUTRAL_DIR;
19910 itb.string.s = NULL;
19911 itb.string.lstring = Qnil;
19912 itb.string.bufpos = 0;
19913 itb.string.unibyte = 0;
19914 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19915 bidi_unshelve_cache (itb_data, 0);
19916 set_buffer_temp (old);
19917 switch (itb.paragraph_dir)
19918 {
19919 case L2R:
19920 return Qleft_to_right;
19921 break;
19922 case R2L:
19923 return Qright_to_left;
19924 break;
19925 default:
19926 abort ();
19927 }
19928 }
19929 }
19930
19931
19932 \f
19933 /***********************************************************************
19934 Menu Bar
19935 ***********************************************************************/
19936
19937 /* Redisplay the menu bar in the frame for window W.
19938
19939 The menu bar of X frames that don't have X toolkit support is
19940 displayed in a special window W->frame->menu_bar_window.
19941
19942 The menu bar of terminal frames is treated specially as far as
19943 glyph matrices are concerned. Menu bar lines are not part of
19944 windows, so the update is done directly on the frame matrix rows
19945 for the menu bar. */
19946
19947 static void
19948 display_menu_bar (struct window *w)
19949 {
19950 struct frame *f = XFRAME (WINDOW_FRAME (w));
19951 struct it it;
19952 Lisp_Object items;
19953 int i;
19954
19955 /* Don't do all this for graphical frames. */
19956 #ifdef HAVE_NTGUI
19957 if (FRAME_W32_P (f))
19958 return;
19959 #endif
19960 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19961 if (FRAME_X_P (f))
19962 return;
19963 #endif
19964
19965 #ifdef HAVE_NS
19966 if (FRAME_NS_P (f))
19967 return;
19968 #endif /* HAVE_NS */
19969
19970 #ifdef USE_X_TOOLKIT
19971 eassert (!FRAME_WINDOW_P (f));
19972 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19973 it.first_visible_x = 0;
19974 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19975 #else /* not USE_X_TOOLKIT */
19976 if (FRAME_WINDOW_P (f))
19977 {
19978 /* Menu bar lines are displayed in the desired matrix of the
19979 dummy window menu_bar_window. */
19980 struct window *menu_w;
19981 eassert (WINDOWP (f->menu_bar_window));
19982 menu_w = XWINDOW (f->menu_bar_window);
19983 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19984 MENU_FACE_ID);
19985 it.first_visible_x = 0;
19986 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19987 }
19988 else
19989 {
19990 /* This is a TTY frame, i.e. character hpos/vpos are used as
19991 pixel x/y. */
19992 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19993 MENU_FACE_ID);
19994 it.first_visible_x = 0;
19995 it.last_visible_x = FRAME_COLS (f);
19996 }
19997 #endif /* not USE_X_TOOLKIT */
19998
19999 /* FIXME: This should be controlled by a user option. See the
20000 comments in redisplay_tool_bar and display_mode_line about
20001 this. */
20002 it.paragraph_embedding = L2R;
20003
20004 if (! mode_line_inverse_video)
20005 /* Force the menu-bar to be displayed in the default face. */
20006 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20007
20008 /* Clear all rows of the menu bar. */
20009 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20010 {
20011 struct glyph_row *row = it.glyph_row + i;
20012 clear_glyph_row (row);
20013 row->enabled_p = 1;
20014 row->full_width_p = 1;
20015 }
20016
20017 /* Display all items of the menu bar. */
20018 items = FRAME_MENU_BAR_ITEMS (it.f);
20019 for (i = 0; i < ASIZE (items); i += 4)
20020 {
20021 Lisp_Object string;
20022
20023 /* Stop at nil string. */
20024 string = AREF (items, i + 1);
20025 if (NILP (string))
20026 break;
20027
20028 /* Remember where item was displayed. */
20029 ASET (items, i + 3, make_number (it.hpos));
20030
20031 /* Display the item, pad with one space. */
20032 if (it.current_x < it.last_visible_x)
20033 display_string (NULL, string, Qnil, 0, 0, &it,
20034 SCHARS (string) + 1, 0, 0, -1);
20035 }
20036
20037 /* Fill out the line with spaces. */
20038 if (it.current_x < it.last_visible_x)
20039 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20040
20041 /* Compute the total height of the lines. */
20042 compute_line_metrics (&it);
20043 }
20044
20045
20046 \f
20047 /***********************************************************************
20048 Mode Line
20049 ***********************************************************************/
20050
20051 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20052 FORCE is non-zero, redisplay mode lines unconditionally.
20053 Otherwise, redisplay only mode lines that are garbaged. Value is
20054 the number of windows whose mode lines were redisplayed. */
20055
20056 static int
20057 redisplay_mode_lines (Lisp_Object window, int force)
20058 {
20059 int nwindows = 0;
20060
20061 while (!NILP (window))
20062 {
20063 struct window *w = XWINDOW (window);
20064
20065 if (WINDOWP (w->hchild))
20066 nwindows += redisplay_mode_lines (w->hchild, force);
20067 else if (WINDOWP (w->vchild))
20068 nwindows += redisplay_mode_lines (w->vchild, force);
20069 else if (force
20070 || FRAME_GARBAGED_P (XFRAME (w->frame))
20071 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20072 {
20073 struct text_pos lpoint;
20074 struct buffer *old = current_buffer;
20075
20076 /* Set the window's buffer for the mode line display. */
20077 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20078 set_buffer_internal_1 (XBUFFER (w->buffer));
20079
20080 /* Point refers normally to the selected window. For any
20081 other window, set up appropriate value. */
20082 if (!EQ (window, selected_window))
20083 {
20084 struct text_pos pt;
20085
20086 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20087 if (CHARPOS (pt) < BEGV)
20088 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20089 else if (CHARPOS (pt) > (ZV - 1))
20090 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20091 else
20092 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20093 }
20094
20095 /* Display mode lines. */
20096 clear_glyph_matrix (w->desired_matrix);
20097 if (display_mode_lines (w))
20098 {
20099 ++nwindows;
20100 w->must_be_updated_p = 1;
20101 }
20102
20103 /* Restore old settings. */
20104 set_buffer_internal_1 (old);
20105 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20106 }
20107
20108 window = w->next;
20109 }
20110
20111 return nwindows;
20112 }
20113
20114
20115 /* Display the mode and/or header line of window W. Value is the
20116 sum number of mode lines and header lines displayed. */
20117
20118 static int
20119 display_mode_lines (struct window *w)
20120 {
20121 Lisp_Object old_selected_window, old_selected_frame;
20122 int n = 0;
20123
20124 old_selected_frame = selected_frame;
20125 selected_frame = w->frame;
20126 old_selected_window = selected_window;
20127 XSETWINDOW (selected_window, w);
20128
20129 /* These will be set while the mode line specs are processed. */
20130 line_number_displayed = 0;
20131 w->column_number_displayed = Qnil;
20132
20133 if (WINDOW_WANTS_MODELINE_P (w))
20134 {
20135 struct window *sel_w = XWINDOW (old_selected_window);
20136
20137 /* Select mode line face based on the real selected window. */
20138 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20139 BVAR (current_buffer, mode_line_format));
20140 ++n;
20141 }
20142
20143 if (WINDOW_WANTS_HEADER_LINE_P (w))
20144 {
20145 display_mode_line (w, HEADER_LINE_FACE_ID,
20146 BVAR (current_buffer, header_line_format));
20147 ++n;
20148 }
20149
20150 selected_frame = old_selected_frame;
20151 selected_window = old_selected_window;
20152 return n;
20153 }
20154
20155
20156 /* Display mode or header line of window W. FACE_ID specifies which
20157 line to display; it is either MODE_LINE_FACE_ID or
20158 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20159 display. Value is the pixel height of the mode/header line
20160 displayed. */
20161
20162 static int
20163 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20164 {
20165 struct it it;
20166 struct face *face;
20167 ptrdiff_t count = SPECPDL_INDEX ();
20168
20169 init_iterator (&it, w, -1, -1, NULL, face_id);
20170 /* Don't extend on a previously drawn mode-line.
20171 This may happen if called from pos_visible_p. */
20172 it.glyph_row->enabled_p = 0;
20173 prepare_desired_row (it.glyph_row);
20174
20175 it.glyph_row->mode_line_p = 1;
20176
20177 if (! mode_line_inverse_video)
20178 /* Force the mode-line to be displayed in the default face. */
20179 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20180
20181 /* FIXME: This should be controlled by a user option. But
20182 supporting such an option is not trivial, since the mode line is
20183 made up of many separate strings. */
20184 it.paragraph_embedding = L2R;
20185
20186 record_unwind_protect (unwind_format_mode_line,
20187 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20188
20189 mode_line_target = MODE_LINE_DISPLAY;
20190
20191 /* Temporarily make frame's keyboard the current kboard so that
20192 kboard-local variables in the mode_line_format will get the right
20193 values. */
20194 push_kboard (FRAME_KBOARD (it.f));
20195 record_unwind_save_match_data ();
20196 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20197 pop_kboard ();
20198
20199 unbind_to (count, Qnil);
20200
20201 /* Fill up with spaces. */
20202 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20203
20204 compute_line_metrics (&it);
20205 it.glyph_row->full_width_p = 1;
20206 it.glyph_row->continued_p = 0;
20207 it.glyph_row->truncated_on_left_p = 0;
20208 it.glyph_row->truncated_on_right_p = 0;
20209
20210 /* Make a 3D mode-line have a shadow at its right end. */
20211 face = FACE_FROM_ID (it.f, face_id);
20212 extend_face_to_end_of_line (&it);
20213 if (face->box != FACE_NO_BOX)
20214 {
20215 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20216 + it.glyph_row->used[TEXT_AREA] - 1);
20217 last->right_box_line_p = 1;
20218 }
20219
20220 return it.glyph_row->height;
20221 }
20222
20223 /* Move element ELT in LIST to the front of LIST.
20224 Return the updated list. */
20225
20226 static Lisp_Object
20227 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20228 {
20229 register Lisp_Object tail, prev;
20230 register Lisp_Object tem;
20231
20232 tail = list;
20233 prev = Qnil;
20234 while (CONSP (tail))
20235 {
20236 tem = XCAR (tail);
20237
20238 if (EQ (elt, tem))
20239 {
20240 /* Splice out the link TAIL. */
20241 if (NILP (prev))
20242 list = XCDR (tail);
20243 else
20244 Fsetcdr (prev, XCDR (tail));
20245
20246 /* Now make it the first. */
20247 Fsetcdr (tail, list);
20248 return tail;
20249 }
20250 else
20251 prev = tail;
20252 tail = XCDR (tail);
20253 QUIT;
20254 }
20255
20256 /* Not found--return unchanged LIST. */
20257 return list;
20258 }
20259
20260 /* Contribute ELT to the mode line for window IT->w. How it
20261 translates into text depends on its data type.
20262
20263 IT describes the display environment in which we display, as usual.
20264
20265 DEPTH is the depth in recursion. It is used to prevent
20266 infinite recursion here.
20267
20268 FIELD_WIDTH is the number of characters the display of ELT should
20269 occupy in the mode line, and PRECISION is the maximum number of
20270 characters to display from ELT's representation. See
20271 display_string for details.
20272
20273 Returns the hpos of the end of the text generated by ELT.
20274
20275 PROPS is a property list to add to any string we encounter.
20276
20277 If RISKY is nonzero, remove (disregard) any properties in any string
20278 we encounter, and ignore :eval and :propertize.
20279
20280 The global variable `mode_line_target' determines whether the
20281 output is passed to `store_mode_line_noprop',
20282 `store_mode_line_string', or `display_string'. */
20283
20284 static int
20285 display_mode_element (struct it *it, int depth, int field_width, int precision,
20286 Lisp_Object elt, Lisp_Object props, int risky)
20287 {
20288 int n = 0, field, prec;
20289 int literal = 0;
20290
20291 tail_recurse:
20292 if (depth > 100)
20293 elt = build_string ("*too-deep*");
20294
20295 depth++;
20296
20297 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20298 {
20299 case Lisp_String:
20300 {
20301 /* A string: output it and check for %-constructs within it. */
20302 unsigned char c;
20303 ptrdiff_t offset = 0;
20304
20305 if (SCHARS (elt) > 0
20306 && (!NILP (props) || risky))
20307 {
20308 Lisp_Object oprops, aelt;
20309 oprops = Ftext_properties_at (make_number (0), elt);
20310
20311 /* If the starting string's properties are not what
20312 we want, translate the string. Also, if the string
20313 is risky, do that anyway. */
20314
20315 if (NILP (Fequal (props, oprops)) || risky)
20316 {
20317 /* If the starting string has properties,
20318 merge the specified ones onto the existing ones. */
20319 if (! NILP (oprops) && !risky)
20320 {
20321 Lisp_Object tem;
20322
20323 oprops = Fcopy_sequence (oprops);
20324 tem = props;
20325 while (CONSP (tem))
20326 {
20327 oprops = Fplist_put (oprops, XCAR (tem),
20328 XCAR (XCDR (tem)));
20329 tem = XCDR (XCDR (tem));
20330 }
20331 props = oprops;
20332 }
20333
20334 aelt = Fassoc (elt, mode_line_proptrans_alist);
20335 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20336 {
20337 /* AELT is what we want. Move it to the front
20338 without consing. */
20339 elt = XCAR (aelt);
20340 mode_line_proptrans_alist
20341 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20342 }
20343 else
20344 {
20345 Lisp_Object tem;
20346
20347 /* If AELT has the wrong props, it is useless.
20348 so get rid of it. */
20349 if (! NILP (aelt))
20350 mode_line_proptrans_alist
20351 = Fdelq (aelt, mode_line_proptrans_alist);
20352
20353 elt = Fcopy_sequence (elt);
20354 Fset_text_properties (make_number (0), Flength (elt),
20355 props, elt);
20356 /* Add this item to mode_line_proptrans_alist. */
20357 mode_line_proptrans_alist
20358 = Fcons (Fcons (elt, props),
20359 mode_line_proptrans_alist);
20360 /* Truncate mode_line_proptrans_alist
20361 to at most 50 elements. */
20362 tem = Fnthcdr (make_number (50),
20363 mode_line_proptrans_alist);
20364 if (! NILP (tem))
20365 XSETCDR (tem, Qnil);
20366 }
20367 }
20368 }
20369
20370 offset = 0;
20371
20372 if (literal)
20373 {
20374 prec = precision - n;
20375 switch (mode_line_target)
20376 {
20377 case MODE_LINE_NOPROP:
20378 case MODE_LINE_TITLE:
20379 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20380 break;
20381 case MODE_LINE_STRING:
20382 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20383 break;
20384 case MODE_LINE_DISPLAY:
20385 n += display_string (NULL, elt, Qnil, 0, 0, it,
20386 0, prec, 0, STRING_MULTIBYTE (elt));
20387 break;
20388 }
20389
20390 break;
20391 }
20392
20393 /* Handle the non-literal case. */
20394
20395 while ((precision <= 0 || n < precision)
20396 && SREF (elt, offset) != 0
20397 && (mode_line_target != MODE_LINE_DISPLAY
20398 || it->current_x < it->last_visible_x))
20399 {
20400 ptrdiff_t last_offset = offset;
20401
20402 /* Advance to end of string or next format specifier. */
20403 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20404 ;
20405
20406 if (offset - 1 != last_offset)
20407 {
20408 ptrdiff_t nchars, nbytes;
20409
20410 /* Output to end of string or up to '%'. Field width
20411 is length of string. Don't output more than
20412 PRECISION allows us. */
20413 offset--;
20414
20415 prec = c_string_width (SDATA (elt) + last_offset,
20416 offset - last_offset, precision - n,
20417 &nchars, &nbytes);
20418
20419 switch (mode_line_target)
20420 {
20421 case MODE_LINE_NOPROP:
20422 case MODE_LINE_TITLE:
20423 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20424 break;
20425 case MODE_LINE_STRING:
20426 {
20427 ptrdiff_t bytepos = last_offset;
20428 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20429 ptrdiff_t endpos = (precision <= 0
20430 ? string_byte_to_char (elt, offset)
20431 : charpos + nchars);
20432
20433 n += store_mode_line_string (NULL,
20434 Fsubstring (elt, make_number (charpos),
20435 make_number (endpos)),
20436 0, 0, 0, Qnil);
20437 }
20438 break;
20439 case MODE_LINE_DISPLAY:
20440 {
20441 ptrdiff_t bytepos = last_offset;
20442 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20443
20444 if (precision <= 0)
20445 nchars = string_byte_to_char (elt, offset) - charpos;
20446 n += display_string (NULL, elt, Qnil, 0, charpos,
20447 it, 0, nchars, 0,
20448 STRING_MULTIBYTE (elt));
20449 }
20450 break;
20451 }
20452 }
20453 else /* c == '%' */
20454 {
20455 ptrdiff_t percent_position = offset;
20456
20457 /* Get the specified minimum width. Zero means
20458 don't pad. */
20459 field = 0;
20460 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20461 field = field * 10 + c - '0';
20462
20463 /* Don't pad beyond the total padding allowed. */
20464 if (field_width - n > 0 && field > field_width - n)
20465 field = field_width - n;
20466
20467 /* Note that either PRECISION <= 0 or N < PRECISION. */
20468 prec = precision - n;
20469
20470 if (c == 'M')
20471 n += display_mode_element (it, depth, field, prec,
20472 Vglobal_mode_string, props,
20473 risky);
20474 else if (c != 0)
20475 {
20476 int multibyte;
20477 ptrdiff_t bytepos, charpos;
20478 const char *spec;
20479 Lisp_Object string;
20480
20481 bytepos = percent_position;
20482 charpos = (STRING_MULTIBYTE (elt)
20483 ? string_byte_to_char (elt, bytepos)
20484 : bytepos);
20485 spec = decode_mode_spec (it->w, c, field, &string);
20486 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20487
20488 switch (mode_line_target)
20489 {
20490 case MODE_LINE_NOPROP:
20491 case MODE_LINE_TITLE:
20492 n += store_mode_line_noprop (spec, field, prec);
20493 break;
20494 case MODE_LINE_STRING:
20495 {
20496 Lisp_Object tem = build_string (spec);
20497 props = Ftext_properties_at (make_number (charpos), elt);
20498 /* Should only keep face property in props */
20499 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20500 }
20501 break;
20502 case MODE_LINE_DISPLAY:
20503 {
20504 int nglyphs_before, nwritten;
20505
20506 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20507 nwritten = display_string (spec, string, elt,
20508 charpos, 0, it,
20509 field, prec, 0,
20510 multibyte);
20511
20512 /* Assign to the glyphs written above the
20513 string where the `%x' came from, position
20514 of the `%'. */
20515 if (nwritten > 0)
20516 {
20517 struct glyph *glyph
20518 = (it->glyph_row->glyphs[TEXT_AREA]
20519 + nglyphs_before);
20520 int i;
20521
20522 for (i = 0; i < nwritten; ++i)
20523 {
20524 glyph[i].object = elt;
20525 glyph[i].charpos = charpos;
20526 }
20527
20528 n += nwritten;
20529 }
20530 }
20531 break;
20532 }
20533 }
20534 else /* c == 0 */
20535 break;
20536 }
20537 }
20538 }
20539 break;
20540
20541 case Lisp_Symbol:
20542 /* A symbol: process the value of the symbol recursively
20543 as if it appeared here directly. Avoid error if symbol void.
20544 Special case: if value of symbol is a string, output the string
20545 literally. */
20546 {
20547 register Lisp_Object tem;
20548
20549 /* If the variable is not marked as risky to set
20550 then its contents are risky to use. */
20551 if (NILP (Fget (elt, Qrisky_local_variable)))
20552 risky = 1;
20553
20554 tem = Fboundp (elt);
20555 if (!NILP (tem))
20556 {
20557 tem = Fsymbol_value (elt);
20558 /* If value is a string, output that string literally:
20559 don't check for % within it. */
20560 if (STRINGP (tem))
20561 literal = 1;
20562
20563 if (!EQ (tem, elt))
20564 {
20565 /* Give up right away for nil or t. */
20566 elt = tem;
20567 goto tail_recurse;
20568 }
20569 }
20570 }
20571 break;
20572
20573 case Lisp_Cons:
20574 {
20575 register Lisp_Object car, tem;
20576
20577 /* A cons cell: five distinct cases.
20578 If first element is :eval or :propertize, do something special.
20579 If first element is a string or a cons, process all the elements
20580 and effectively concatenate them.
20581 If first element is a negative number, truncate displaying cdr to
20582 at most that many characters. If positive, pad (with spaces)
20583 to at least that many characters.
20584 If first element is a symbol, process the cadr or caddr recursively
20585 according to whether the symbol's value is non-nil or nil. */
20586 car = XCAR (elt);
20587 if (EQ (car, QCeval))
20588 {
20589 /* An element of the form (:eval FORM) means evaluate FORM
20590 and use the result as mode line elements. */
20591
20592 if (risky)
20593 break;
20594
20595 if (CONSP (XCDR (elt)))
20596 {
20597 Lisp_Object spec;
20598 spec = safe_eval (XCAR (XCDR (elt)));
20599 n += display_mode_element (it, depth, field_width - n,
20600 precision - n, spec, props,
20601 risky);
20602 }
20603 }
20604 else if (EQ (car, QCpropertize))
20605 {
20606 /* An element of the form (:propertize ELT PROPS...)
20607 means display ELT but applying properties PROPS. */
20608
20609 if (risky)
20610 break;
20611
20612 if (CONSP (XCDR (elt)))
20613 n += display_mode_element (it, depth, field_width - n,
20614 precision - n, XCAR (XCDR (elt)),
20615 XCDR (XCDR (elt)), risky);
20616 }
20617 else if (SYMBOLP (car))
20618 {
20619 tem = Fboundp (car);
20620 elt = XCDR (elt);
20621 if (!CONSP (elt))
20622 goto invalid;
20623 /* elt is now the cdr, and we know it is a cons cell.
20624 Use its car if CAR has a non-nil value. */
20625 if (!NILP (tem))
20626 {
20627 tem = Fsymbol_value (car);
20628 if (!NILP (tem))
20629 {
20630 elt = XCAR (elt);
20631 goto tail_recurse;
20632 }
20633 }
20634 /* Symbol's value is nil (or symbol is unbound)
20635 Get the cddr of the original list
20636 and if possible find the caddr and use that. */
20637 elt = XCDR (elt);
20638 if (NILP (elt))
20639 break;
20640 else if (!CONSP (elt))
20641 goto invalid;
20642 elt = XCAR (elt);
20643 goto tail_recurse;
20644 }
20645 else if (INTEGERP (car))
20646 {
20647 register int lim = XINT (car);
20648 elt = XCDR (elt);
20649 if (lim < 0)
20650 {
20651 /* Negative int means reduce maximum width. */
20652 if (precision <= 0)
20653 precision = -lim;
20654 else
20655 precision = min (precision, -lim);
20656 }
20657 else if (lim > 0)
20658 {
20659 /* Padding specified. Don't let it be more than
20660 current maximum. */
20661 if (precision > 0)
20662 lim = min (precision, lim);
20663
20664 /* If that's more padding than already wanted, queue it.
20665 But don't reduce padding already specified even if
20666 that is beyond the current truncation point. */
20667 field_width = max (lim, field_width);
20668 }
20669 goto tail_recurse;
20670 }
20671 else if (STRINGP (car) || CONSP (car))
20672 {
20673 Lisp_Object halftail = elt;
20674 int len = 0;
20675
20676 while (CONSP (elt)
20677 && (precision <= 0 || n < precision))
20678 {
20679 n += display_mode_element (it, depth,
20680 /* Do padding only after the last
20681 element in the list. */
20682 (! CONSP (XCDR (elt))
20683 ? field_width - n
20684 : 0),
20685 precision - n, XCAR (elt),
20686 props, risky);
20687 elt = XCDR (elt);
20688 len++;
20689 if ((len & 1) == 0)
20690 halftail = XCDR (halftail);
20691 /* Check for cycle. */
20692 if (EQ (halftail, elt))
20693 break;
20694 }
20695 }
20696 }
20697 break;
20698
20699 default:
20700 invalid:
20701 elt = build_string ("*invalid*");
20702 goto tail_recurse;
20703 }
20704
20705 /* Pad to FIELD_WIDTH. */
20706 if (field_width > 0 && n < field_width)
20707 {
20708 switch (mode_line_target)
20709 {
20710 case MODE_LINE_NOPROP:
20711 case MODE_LINE_TITLE:
20712 n += store_mode_line_noprop ("", field_width - n, 0);
20713 break;
20714 case MODE_LINE_STRING:
20715 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20716 break;
20717 case MODE_LINE_DISPLAY:
20718 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20719 0, 0, 0);
20720 break;
20721 }
20722 }
20723
20724 return n;
20725 }
20726
20727 /* Store a mode-line string element in mode_line_string_list.
20728
20729 If STRING is non-null, display that C string. Otherwise, the Lisp
20730 string LISP_STRING is displayed.
20731
20732 FIELD_WIDTH is the minimum number of output glyphs to produce.
20733 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20734 with spaces. FIELD_WIDTH <= 0 means don't pad.
20735
20736 PRECISION is the maximum number of characters to output from
20737 STRING. PRECISION <= 0 means don't truncate the string.
20738
20739 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20740 properties to the string.
20741
20742 PROPS are the properties to add to the string.
20743 The mode_line_string_face face property is always added to the string.
20744 */
20745
20746 static int
20747 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20748 int field_width, int precision, Lisp_Object props)
20749 {
20750 ptrdiff_t len;
20751 int n = 0;
20752
20753 if (string != NULL)
20754 {
20755 len = strlen (string);
20756 if (precision > 0 && len > precision)
20757 len = precision;
20758 lisp_string = make_string (string, len);
20759 if (NILP (props))
20760 props = mode_line_string_face_prop;
20761 else if (!NILP (mode_line_string_face))
20762 {
20763 Lisp_Object face = Fplist_get (props, Qface);
20764 props = Fcopy_sequence (props);
20765 if (NILP (face))
20766 face = mode_line_string_face;
20767 else
20768 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20769 props = Fplist_put (props, Qface, face);
20770 }
20771 Fadd_text_properties (make_number (0), make_number (len),
20772 props, lisp_string);
20773 }
20774 else
20775 {
20776 len = XFASTINT (Flength (lisp_string));
20777 if (precision > 0 && len > precision)
20778 {
20779 len = precision;
20780 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20781 precision = -1;
20782 }
20783 if (!NILP (mode_line_string_face))
20784 {
20785 Lisp_Object face;
20786 if (NILP (props))
20787 props = Ftext_properties_at (make_number (0), lisp_string);
20788 face = Fplist_get (props, Qface);
20789 if (NILP (face))
20790 face = mode_line_string_face;
20791 else
20792 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20793 props = Fcons (Qface, Fcons (face, Qnil));
20794 if (copy_string)
20795 lisp_string = Fcopy_sequence (lisp_string);
20796 }
20797 if (!NILP (props))
20798 Fadd_text_properties (make_number (0), make_number (len),
20799 props, lisp_string);
20800 }
20801
20802 if (len > 0)
20803 {
20804 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20805 n += len;
20806 }
20807
20808 if (field_width > len)
20809 {
20810 field_width -= len;
20811 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20812 if (!NILP (props))
20813 Fadd_text_properties (make_number (0), make_number (field_width),
20814 props, lisp_string);
20815 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20816 n += field_width;
20817 }
20818
20819 return n;
20820 }
20821
20822
20823 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20824 1, 4, 0,
20825 doc: /* Format a string out of a mode line format specification.
20826 First arg FORMAT specifies the mode line format (see `mode-line-format'
20827 for details) to use.
20828
20829 By default, the format is evaluated for the currently selected window.
20830
20831 Optional second arg FACE specifies the face property to put on all
20832 characters for which no face is specified. The value nil means the
20833 default face. The value t means whatever face the window's mode line
20834 currently uses (either `mode-line' or `mode-line-inactive',
20835 depending on whether the window is the selected window or not).
20836 An integer value means the value string has no text
20837 properties.
20838
20839 Optional third and fourth args WINDOW and BUFFER specify the window
20840 and buffer to use as the context for the formatting (defaults
20841 are the selected window and the WINDOW's buffer). */)
20842 (Lisp_Object format, Lisp_Object face,
20843 Lisp_Object window, Lisp_Object buffer)
20844 {
20845 struct it it;
20846 int len;
20847 struct window *w;
20848 struct buffer *old_buffer = NULL;
20849 int face_id;
20850 int no_props = INTEGERP (face);
20851 ptrdiff_t count = SPECPDL_INDEX ();
20852 Lisp_Object str;
20853 int string_start = 0;
20854
20855 if (NILP (window))
20856 window = selected_window;
20857 CHECK_WINDOW (window);
20858 w = XWINDOW (window);
20859
20860 if (NILP (buffer))
20861 buffer = w->buffer;
20862 CHECK_BUFFER (buffer);
20863
20864 /* Make formatting the modeline a non-op when noninteractive, otherwise
20865 there will be problems later caused by a partially initialized frame. */
20866 if (NILP (format) || noninteractive)
20867 return empty_unibyte_string;
20868
20869 if (no_props)
20870 face = Qnil;
20871
20872 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20873 : EQ (face, Qt) ? (EQ (window, selected_window)
20874 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20875 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20876 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20877 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20878 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20879 : DEFAULT_FACE_ID;
20880
20881 if (XBUFFER (buffer) != current_buffer)
20882 old_buffer = current_buffer;
20883
20884 /* Save things including mode_line_proptrans_alist,
20885 and set that to nil so that we don't alter the outer value. */
20886 record_unwind_protect (unwind_format_mode_line,
20887 format_mode_line_unwind_data
20888 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
20889 old_buffer, selected_window, 1));
20890 mode_line_proptrans_alist = Qnil;
20891
20892 Fselect_window (window, Qt);
20893 if (old_buffer)
20894 set_buffer_internal_1 (XBUFFER (buffer));
20895
20896 init_iterator (&it, w, -1, -1, NULL, face_id);
20897
20898 if (no_props)
20899 {
20900 mode_line_target = MODE_LINE_NOPROP;
20901 mode_line_string_face_prop = Qnil;
20902 mode_line_string_list = Qnil;
20903 string_start = MODE_LINE_NOPROP_LEN (0);
20904 }
20905 else
20906 {
20907 mode_line_target = MODE_LINE_STRING;
20908 mode_line_string_list = Qnil;
20909 mode_line_string_face = face;
20910 mode_line_string_face_prop
20911 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20912 }
20913
20914 push_kboard (FRAME_KBOARD (it.f));
20915 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20916 pop_kboard ();
20917
20918 if (no_props)
20919 {
20920 len = MODE_LINE_NOPROP_LEN (string_start);
20921 str = make_string (mode_line_noprop_buf + string_start, len);
20922 }
20923 else
20924 {
20925 mode_line_string_list = Fnreverse (mode_line_string_list);
20926 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20927 empty_unibyte_string);
20928 }
20929
20930 unbind_to (count, Qnil);
20931 return str;
20932 }
20933
20934 /* Write a null-terminated, right justified decimal representation of
20935 the positive integer D to BUF using a minimal field width WIDTH. */
20936
20937 static void
20938 pint2str (register char *buf, register int width, register ptrdiff_t d)
20939 {
20940 register char *p = buf;
20941
20942 if (d <= 0)
20943 *p++ = '0';
20944 else
20945 {
20946 while (d > 0)
20947 {
20948 *p++ = d % 10 + '0';
20949 d /= 10;
20950 }
20951 }
20952
20953 for (width -= (int) (p - buf); width > 0; --width)
20954 *p++ = ' ';
20955 *p-- = '\0';
20956 while (p > buf)
20957 {
20958 d = *buf;
20959 *buf++ = *p;
20960 *p-- = d;
20961 }
20962 }
20963
20964 /* Write a null-terminated, right justified decimal and "human
20965 readable" representation of the nonnegative integer D to BUF using
20966 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20967
20968 static const char power_letter[] =
20969 {
20970 0, /* no letter */
20971 'k', /* kilo */
20972 'M', /* mega */
20973 'G', /* giga */
20974 'T', /* tera */
20975 'P', /* peta */
20976 'E', /* exa */
20977 'Z', /* zetta */
20978 'Y' /* yotta */
20979 };
20980
20981 static void
20982 pint2hrstr (char *buf, int width, ptrdiff_t d)
20983 {
20984 /* We aim to represent the nonnegative integer D as
20985 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20986 ptrdiff_t quotient = d;
20987 int remainder = 0;
20988 /* -1 means: do not use TENTHS. */
20989 int tenths = -1;
20990 int exponent = 0;
20991
20992 /* Length of QUOTIENT.TENTHS as a string. */
20993 int length;
20994
20995 char * psuffix;
20996 char * p;
20997
20998 if (1000 <= quotient)
20999 {
21000 /* Scale to the appropriate EXPONENT. */
21001 do
21002 {
21003 remainder = quotient % 1000;
21004 quotient /= 1000;
21005 exponent++;
21006 }
21007 while (1000 <= quotient);
21008
21009 /* Round to nearest and decide whether to use TENTHS or not. */
21010 if (quotient <= 9)
21011 {
21012 tenths = remainder / 100;
21013 if (50 <= remainder % 100)
21014 {
21015 if (tenths < 9)
21016 tenths++;
21017 else
21018 {
21019 quotient++;
21020 if (quotient == 10)
21021 tenths = -1;
21022 else
21023 tenths = 0;
21024 }
21025 }
21026 }
21027 else
21028 if (500 <= remainder)
21029 {
21030 if (quotient < 999)
21031 quotient++;
21032 else
21033 {
21034 quotient = 1;
21035 exponent++;
21036 tenths = 0;
21037 }
21038 }
21039 }
21040
21041 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21042 if (tenths == -1 && quotient <= 99)
21043 if (quotient <= 9)
21044 length = 1;
21045 else
21046 length = 2;
21047 else
21048 length = 3;
21049 p = psuffix = buf + max (width, length);
21050
21051 /* Print EXPONENT. */
21052 *psuffix++ = power_letter[exponent];
21053 *psuffix = '\0';
21054
21055 /* Print TENTHS. */
21056 if (tenths >= 0)
21057 {
21058 *--p = '0' + tenths;
21059 *--p = '.';
21060 }
21061
21062 /* Print QUOTIENT. */
21063 do
21064 {
21065 int digit = quotient % 10;
21066 *--p = '0' + digit;
21067 }
21068 while ((quotient /= 10) != 0);
21069
21070 /* Print leading spaces. */
21071 while (buf < p)
21072 *--p = ' ';
21073 }
21074
21075 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21076 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21077 type of CODING_SYSTEM. Return updated pointer into BUF. */
21078
21079 static unsigned char invalid_eol_type[] = "(*invalid*)";
21080
21081 static char *
21082 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21083 {
21084 Lisp_Object val;
21085 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21086 const unsigned char *eol_str;
21087 int eol_str_len;
21088 /* The EOL conversion we are using. */
21089 Lisp_Object eoltype;
21090
21091 val = CODING_SYSTEM_SPEC (coding_system);
21092 eoltype = Qnil;
21093
21094 if (!VECTORP (val)) /* Not yet decided. */
21095 {
21096 *buf++ = multibyte ? '-' : ' ';
21097 if (eol_flag)
21098 eoltype = eol_mnemonic_undecided;
21099 /* Don't mention EOL conversion if it isn't decided. */
21100 }
21101 else
21102 {
21103 Lisp_Object attrs;
21104 Lisp_Object eolvalue;
21105
21106 attrs = AREF (val, 0);
21107 eolvalue = AREF (val, 2);
21108
21109 *buf++ = multibyte
21110 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21111 : ' ';
21112
21113 if (eol_flag)
21114 {
21115 /* The EOL conversion that is normal on this system. */
21116
21117 if (NILP (eolvalue)) /* Not yet decided. */
21118 eoltype = eol_mnemonic_undecided;
21119 else if (VECTORP (eolvalue)) /* Not yet decided. */
21120 eoltype = eol_mnemonic_undecided;
21121 else /* eolvalue is Qunix, Qdos, or Qmac. */
21122 eoltype = (EQ (eolvalue, Qunix)
21123 ? eol_mnemonic_unix
21124 : (EQ (eolvalue, Qdos) == 1
21125 ? eol_mnemonic_dos : eol_mnemonic_mac));
21126 }
21127 }
21128
21129 if (eol_flag)
21130 {
21131 /* Mention the EOL conversion if it is not the usual one. */
21132 if (STRINGP (eoltype))
21133 {
21134 eol_str = SDATA (eoltype);
21135 eol_str_len = SBYTES (eoltype);
21136 }
21137 else if (CHARACTERP (eoltype))
21138 {
21139 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21140 int c = XFASTINT (eoltype);
21141 eol_str_len = CHAR_STRING (c, tmp);
21142 eol_str = tmp;
21143 }
21144 else
21145 {
21146 eol_str = invalid_eol_type;
21147 eol_str_len = sizeof (invalid_eol_type) - 1;
21148 }
21149 memcpy (buf, eol_str, eol_str_len);
21150 buf += eol_str_len;
21151 }
21152
21153 return buf;
21154 }
21155
21156 /* Return a string for the output of a mode line %-spec for window W,
21157 generated by character C. FIELD_WIDTH > 0 means pad the string
21158 returned with spaces to that value. Return a Lisp string in
21159 *STRING if the resulting string is taken from that Lisp string.
21160
21161 Note we operate on the current buffer for most purposes,
21162 the exception being w->base_line_pos. */
21163
21164 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21165
21166 static const char *
21167 decode_mode_spec (struct window *w, register int c, int field_width,
21168 Lisp_Object *string)
21169 {
21170 Lisp_Object obj;
21171 struct frame *f = XFRAME (WINDOW_FRAME (w));
21172 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21173 struct buffer *b = current_buffer;
21174
21175 obj = Qnil;
21176 *string = Qnil;
21177
21178 switch (c)
21179 {
21180 case '*':
21181 if (!NILP (BVAR (b, read_only)))
21182 return "%";
21183 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21184 return "*";
21185 return "-";
21186
21187 case '+':
21188 /* This differs from %* only for a modified read-only buffer. */
21189 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21190 return "*";
21191 if (!NILP (BVAR (b, read_only)))
21192 return "%";
21193 return "-";
21194
21195 case '&':
21196 /* This differs from %* in ignoring read-only-ness. */
21197 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21198 return "*";
21199 return "-";
21200
21201 case '%':
21202 return "%";
21203
21204 case '[':
21205 {
21206 int i;
21207 char *p;
21208
21209 if (command_loop_level > 5)
21210 return "[[[... ";
21211 p = decode_mode_spec_buf;
21212 for (i = 0; i < command_loop_level; i++)
21213 *p++ = '[';
21214 *p = 0;
21215 return decode_mode_spec_buf;
21216 }
21217
21218 case ']':
21219 {
21220 int i;
21221 char *p;
21222
21223 if (command_loop_level > 5)
21224 return " ...]]]";
21225 p = decode_mode_spec_buf;
21226 for (i = 0; i < command_loop_level; i++)
21227 *p++ = ']';
21228 *p = 0;
21229 return decode_mode_spec_buf;
21230 }
21231
21232 case '-':
21233 {
21234 register int i;
21235
21236 /* Let lots_of_dashes be a string of infinite length. */
21237 if (mode_line_target == MODE_LINE_NOPROP ||
21238 mode_line_target == MODE_LINE_STRING)
21239 return "--";
21240 if (field_width <= 0
21241 || field_width > sizeof (lots_of_dashes))
21242 {
21243 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21244 decode_mode_spec_buf[i] = '-';
21245 decode_mode_spec_buf[i] = '\0';
21246 return decode_mode_spec_buf;
21247 }
21248 else
21249 return lots_of_dashes;
21250 }
21251
21252 case 'b':
21253 obj = BVAR (b, name);
21254 break;
21255
21256 case 'c':
21257 /* %c and %l are ignored in `frame-title-format'.
21258 (In redisplay_internal, the frame title is drawn _before_ the
21259 windows are updated, so the stuff which depends on actual
21260 window contents (such as %l) may fail to render properly, or
21261 even crash emacs.) */
21262 if (mode_line_target == MODE_LINE_TITLE)
21263 return "";
21264 else
21265 {
21266 ptrdiff_t col = current_column ();
21267 w->column_number_displayed = make_number (col);
21268 pint2str (decode_mode_spec_buf, field_width, col);
21269 return decode_mode_spec_buf;
21270 }
21271
21272 case 'e':
21273 #ifndef SYSTEM_MALLOC
21274 {
21275 if (NILP (Vmemory_full))
21276 return "";
21277 else
21278 return "!MEM FULL! ";
21279 }
21280 #else
21281 return "";
21282 #endif
21283
21284 case 'F':
21285 /* %F displays the frame name. */
21286 if (!NILP (f->title))
21287 return SSDATA (f->title);
21288 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21289 return SSDATA (f->name);
21290 return "Emacs";
21291
21292 case 'f':
21293 obj = BVAR (b, filename);
21294 break;
21295
21296 case 'i':
21297 {
21298 ptrdiff_t size = ZV - BEGV;
21299 pint2str (decode_mode_spec_buf, field_width, size);
21300 return decode_mode_spec_buf;
21301 }
21302
21303 case 'I':
21304 {
21305 ptrdiff_t size = ZV - BEGV;
21306 pint2hrstr (decode_mode_spec_buf, field_width, size);
21307 return decode_mode_spec_buf;
21308 }
21309
21310 case 'l':
21311 {
21312 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21313 ptrdiff_t topline, nlines, height;
21314 ptrdiff_t junk;
21315
21316 /* %c and %l are ignored in `frame-title-format'. */
21317 if (mode_line_target == MODE_LINE_TITLE)
21318 return "";
21319
21320 startpos = XMARKER (w->start)->charpos;
21321 startpos_byte = marker_byte_position (w->start);
21322 height = WINDOW_TOTAL_LINES (w);
21323
21324 /* If we decided that this buffer isn't suitable for line numbers,
21325 don't forget that too fast. */
21326 if (EQ (w->base_line_pos, w->buffer))
21327 goto no_value;
21328 /* But do forget it, if the window shows a different buffer now. */
21329 else if (BUFFERP (w->base_line_pos))
21330 w->base_line_pos = Qnil;
21331
21332 /* If the buffer is very big, don't waste time. */
21333 if (INTEGERP (Vline_number_display_limit)
21334 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21335 {
21336 w->base_line_pos = Qnil;
21337 w->base_line_number = Qnil;
21338 goto no_value;
21339 }
21340
21341 if (INTEGERP (w->base_line_number)
21342 && INTEGERP (w->base_line_pos)
21343 && XFASTINT (w->base_line_pos) <= startpos)
21344 {
21345 line = XFASTINT (w->base_line_number);
21346 linepos = XFASTINT (w->base_line_pos);
21347 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21348 }
21349 else
21350 {
21351 line = 1;
21352 linepos = BUF_BEGV (b);
21353 linepos_byte = BUF_BEGV_BYTE (b);
21354 }
21355
21356 /* Count lines from base line to window start position. */
21357 nlines = display_count_lines (linepos_byte,
21358 startpos_byte,
21359 startpos, &junk);
21360
21361 topline = nlines + line;
21362
21363 /* Determine a new base line, if the old one is too close
21364 or too far away, or if we did not have one.
21365 "Too close" means it's plausible a scroll-down would
21366 go back past it. */
21367 if (startpos == BUF_BEGV (b))
21368 {
21369 w->base_line_number = make_number (topline);
21370 w->base_line_pos = make_number (BUF_BEGV (b));
21371 }
21372 else if (nlines < height + 25 || nlines > height * 3 + 50
21373 || linepos == BUF_BEGV (b))
21374 {
21375 ptrdiff_t limit = BUF_BEGV (b);
21376 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21377 ptrdiff_t position;
21378 ptrdiff_t distance =
21379 (height * 2 + 30) * line_number_display_limit_width;
21380
21381 if (startpos - distance > limit)
21382 {
21383 limit = startpos - distance;
21384 limit_byte = CHAR_TO_BYTE (limit);
21385 }
21386
21387 nlines = display_count_lines (startpos_byte,
21388 limit_byte,
21389 - (height * 2 + 30),
21390 &position);
21391 /* If we couldn't find the lines we wanted within
21392 line_number_display_limit_width chars per line,
21393 give up on line numbers for this window. */
21394 if (position == limit_byte && limit == startpos - distance)
21395 {
21396 w->base_line_pos = w->buffer;
21397 w->base_line_number = Qnil;
21398 goto no_value;
21399 }
21400
21401 w->base_line_number = make_number (topline - nlines);
21402 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21403 }
21404
21405 /* Now count lines from the start pos to point. */
21406 nlines = display_count_lines (startpos_byte,
21407 PT_BYTE, PT, &junk);
21408
21409 /* Record that we did display the line number. */
21410 line_number_displayed = 1;
21411
21412 /* Make the string to show. */
21413 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21414 return decode_mode_spec_buf;
21415 no_value:
21416 {
21417 char* p = decode_mode_spec_buf;
21418 int pad = field_width - 2;
21419 while (pad-- > 0)
21420 *p++ = ' ';
21421 *p++ = '?';
21422 *p++ = '?';
21423 *p = '\0';
21424 return decode_mode_spec_buf;
21425 }
21426 }
21427 break;
21428
21429 case 'm':
21430 obj = BVAR (b, mode_name);
21431 break;
21432
21433 case 'n':
21434 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21435 return " Narrow";
21436 break;
21437
21438 case 'p':
21439 {
21440 ptrdiff_t pos = marker_position (w->start);
21441 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21442
21443 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21444 {
21445 if (pos <= BUF_BEGV (b))
21446 return "All";
21447 else
21448 return "Bottom";
21449 }
21450 else if (pos <= BUF_BEGV (b))
21451 return "Top";
21452 else
21453 {
21454 if (total > 1000000)
21455 /* Do it differently for a large value, to avoid overflow. */
21456 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21457 else
21458 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21459 /* We can't normally display a 3-digit number,
21460 so get us a 2-digit number that is close. */
21461 if (total == 100)
21462 total = 99;
21463 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21464 return decode_mode_spec_buf;
21465 }
21466 }
21467
21468 /* Display percentage of size above the bottom of the screen. */
21469 case 'P':
21470 {
21471 ptrdiff_t toppos = marker_position (w->start);
21472 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21473 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21474
21475 if (botpos >= BUF_ZV (b))
21476 {
21477 if (toppos <= BUF_BEGV (b))
21478 return "All";
21479 else
21480 return "Bottom";
21481 }
21482 else
21483 {
21484 if (total > 1000000)
21485 /* Do it differently for a large value, to avoid overflow. */
21486 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21487 else
21488 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21489 /* We can't normally display a 3-digit number,
21490 so get us a 2-digit number that is close. */
21491 if (total == 100)
21492 total = 99;
21493 if (toppos <= BUF_BEGV (b))
21494 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21495 else
21496 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21497 return decode_mode_spec_buf;
21498 }
21499 }
21500
21501 case 's':
21502 /* status of process */
21503 obj = Fget_buffer_process (Fcurrent_buffer ());
21504 if (NILP (obj))
21505 return "no process";
21506 #ifndef MSDOS
21507 obj = Fsymbol_name (Fprocess_status (obj));
21508 #endif
21509 break;
21510
21511 case '@':
21512 {
21513 ptrdiff_t count = inhibit_garbage_collection ();
21514 Lisp_Object val = call1 (intern ("file-remote-p"),
21515 BVAR (current_buffer, directory));
21516 unbind_to (count, Qnil);
21517
21518 if (NILP (val))
21519 return "-";
21520 else
21521 return "@";
21522 }
21523
21524 case 't': /* indicate TEXT or BINARY */
21525 return "T";
21526
21527 case 'z':
21528 /* coding-system (not including end-of-line format) */
21529 case 'Z':
21530 /* coding-system (including end-of-line type) */
21531 {
21532 int eol_flag = (c == 'Z');
21533 char *p = decode_mode_spec_buf;
21534
21535 if (! FRAME_WINDOW_P (f))
21536 {
21537 /* No need to mention EOL here--the terminal never needs
21538 to do EOL conversion. */
21539 p = decode_mode_spec_coding (CODING_ID_NAME
21540 (FRAME_KEYBOARD_CODING (f)->id),
21541 p, 0);
21542 p = decode_mode_spec_coding (CODING_ID_NAME
21543 (FRAME_TERMINAL_CODING (f)->id),
21544 p, 0);
21545 }
21546 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21547 p, eol_flag);
21548
21549 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21550 #ifdef subprocesses
21551 obj = Fget_buffer_process (Fcurrent_buffer ());
21552 if (PROCESSP (obj))
21553 {
21554 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21555 p, eol_flag);
21556 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21557 p, eol_flag);
21558 }
21559 #endif /* subprocesses */
21560 #endif /* 0 */
21561 *p = 0;
21562 return decode_mode_spec_buf;
21563 }
21564 }
21565
21566 if (STRINGP (obj))
21567 {
21568 *string = obj;
21569 return SSDATA (obj);
21570 }
21571 else
21572 return "";
21573 }
21574
21575
21576 /* Count up to COUNT lines starting from START_BYTE.
21577 But don't go beyond LIMIT_BYTE.
21578 Return the number of lines thus found (always nonnegative).
21579
21580 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21581
21582 static ptrdiff_t
21583 display_count_lines (ptrdiff_t start_byte,
21584 ptrdiff_t limit_byte, ptrdiff_t count,
21585 ptrdiff_t *byte_pos_ptr)
21586 {
21587 register unsigned char *cursor;
21588 unsigned char *base;
21589
21590 register ptrdiff_t ceiling;
21591 register unsigned char *ceiling_addr;
21592 ptrdiff_t orig_count = count;
21593
21594 /* If we are not in selective display mode,
21595 check only for newlines. */
21596 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21597 && !INTEGERP (BVAR (current_buffer, selective_display)));
21598
21599 if (count > 0)
21600 {
21601 while (start_byte < limit_byte)
21602 {
21603 ceiling = BUFFER_CEILING_OF (start_byte);
21604 ceiling = min (limit_byte - 1, ceiling);
21605 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21606 base = (cursor = BYTE_POS_ADDR (start_byte));
21607 while (1)
21608 {
21609 if (selective_display)
21610 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21611 ;
21612 else
21613 while (*cursor != '\n' && ++cursor != ceiling_addr)
21614 ;
21615
21616 if (cursor != ceiling_addr)
21617 {
21618 if (--count == 0)
21619 {
21620 start_byte += cursor - base + 1;
21621 *byte_pos_ptr = start_byte;
21622 return orig_count;
21623 }
21624 else
21625 if (++cursor == ceiling_addr)
21626 break;
21627 }
21628 else
21629 break;
21630 }
21631 start_byte += cursor - base;
21632 }
21633 }
21634 else
21635 {
21636 while (start_byte > limit_byte)
21637 {
21638 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21639 ceiling = max (limit_byte, ceiling);
21640 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21641 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21642 while (1)
21643 {
21644 if (selective_display)
21645 while (--cursor != ceiling_addr
21646 && *cursor != '\n' && *cursor != 015)
21647 ;
21648 else
21649 while (--cursor != ceiling_addr && *cursor != '\n')
21650 ;
21651
21652 if (cursor != ceiling_addr)
21653 {
21654 if (++count == 0)
21655 {
21656 start_byte += cursor - base + 1;
21657 *byte_pos_ptr = start_byte;
21658 /* When scanning backwards, we should
21659 not count the newline posterior to which we stop. */
21660 return - orig_count - 1;
21661 }
21662 }
21663 else
21664 break;
21665 }
21666 /* Here we add 1 to compensate for the last decrement
21667 of CURSOR, which took it past the valid range. */
21668 start_byte += cursor - base + 1;
21669 }
21670 }
21671
21672 *byte_pos_ptr = limit_byte;
21673
21674 if (count < 0)
21675 return - orig_count + count;
21676 return orig_count - count;
21677
21678 }
21679
21680
21681 \f
21682 /***********************************************************************
21683 Displaying strings
21684 ***********************************************************************/
21685
21686 /* Display a NUL-terminated string, starting with index START.
21687
21688 If STRING is non-null, display that C string. Otherwise, the Lisp
21689 string LISP_STRING is displayed. There's a case that STRING is
21690 non-null and LISP_STRING is not nil. It means STRING is a string
21691 data of LISP_STRING. In that case, we display LISP_STRING while
21692 ignoring its text properties.
21693
21694 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21695 FACE_STRING. Display STRING or LISP_STRING with the face at
21696 FACE_STRING_POS in FACE_STRING:
21697
21698 Display the string in the environment given by IT, but use the
21699 standard display table, temporarily.
21700
21701 FIELD_WIDTH is the minimum number of output glyphs to produce.
21702 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21703 with spaces. If STRING has more characters, more than FIELD_WIDTH
21704 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21705
21706 PRECISION is the maximum number of characters to output from
21707 STRING. PRECISION < 0 means don't truncate the string.
21708
21709 This is roughly equivalent to printf format specifiers:
21710
21711 FIELD_WIDTH PRECISION PRINTF
21712 ----------------------------------------
21713 -1 -1 %s
21714 -1 10 %.10s
21715 10 -1 %10s
21716 20 10 %20.10s
21717
21718 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21719 display them, and < 0 means obey the current buffer's value of
21720 enable_multibyte_characters.
21721
21722 Value is the number of columns displayed. */
21723
21724 static int
21725 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21726 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21727 int field_width, int precision, int max_x, int multibyte)
21728 {
21729 int hpos_at_start = it->hpos;
21730 int saved_face_id = it->face_id;
21731 struct glyph_row *row = it->glyph_row;
21732 ptrdiff_t it_charpos;
21733
21734 /* Initialize the iterator IT for iteration over STRING beginning
21735 with index START. */
21736 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21737 precision, field_width, multibyte);
21738 if (string && STRINGP (lisp_string))
21739 /* LISP_STRING is the one returned by decode_mode_spec. We should
21740 ignore its text properties. */
21741 it->stop_charpos = it->end_charpos;
21742
21743 /* If displaying STRING, set up the face of the iterator from
21744 FACE_STRING, if that's given. */
21745 if (STRINGP (face_string))
21746 {
21747 ptrdiff_t endptr;
21748 struct face *face;
21749
21750 it->face_id
21751 = face_at_string_position (it->w, face_string, face_string_pos,
21752 0, it->region_beg_charpos,
21753 it->region_end_charpos,
21754 &endptr, it->base_face_id, 0);
21755 face = FACE_FROM_ID (it->f, it->face_id);
21756 it->face_box_p = face->box != FACE_NO_BOX;
21757 }
21758
21759 /* Set max_x to the maximum allowed X position. Don't let it go
21760 beyond the right edge of the window. */
21761 if (max_x <= 0)
21762 max_x = it->last_visible_x;
21763 else
21764 max_x = min (max_x, it->last_visible_x);
21765
21766 /* Skip over display elements that are not visible. because IT->w is
21767 hscrolled. */
21768 if (it->current_x < it->first_visible_x)
21769 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21770 MOVE_TO_POS | MOVE_TO_X);
21771
21772 row->ascent = it->max_ascent;
21773 row->height = it->max_ascent + it->max_descent;
21774 row->phys_ascent = it->max_phys_ascent;
21775 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21776 row->extra_line_spacing = it->max_extra_line_spacing;
21777
21778 if (STRINGP (it->string))
21779 it_charpos = IT_STRING_CHARPOS (*it);
21780 else
21781 it_charpos = IT_CHARPOS (*it);
21782
21783 /* This condition is for the case that we are called with current_x
21784 past last_visible_x. */
21785 while (it->current_x < max_x)
21786 {
21787 int x_before, x, n_glyphs_before, i, nglyphs;
21788
21789 /* Get the next display element. */
21790 if (!get_next_display_element (it))
21791 break;
21792
21793 /* Produce glyphs. */
21794 x_before = it->current_x;
21795 n_glyphs_before = row->used[TEXT_AREA];
21796 PRODUCE_GLYPHS (it);
21797
21798 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21799 i = 0;
21800 x = x_before;
21801 while (i < nglyphs)
21802 {
21803 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21804
21805 if (it->line_wrap != TRUNCATE
21806 && x + glyph->pixel_width > max_x)
21807 {
21808 /* End of continued line or max_x reached. */
21809 if (CHAR_GLYPH_PADDING_P (*glyph))
21810 {
21811 /* A wide character is unbreakable. */
21812 if (row->reversed_p)
21813 unproduce_glyphs (it, row->used[TEXT_AREA]
21814 - n_glyphs_before);
21815 row->used[TEXT_AREA] = n_glyphs_before;
21816 it->current_x = x_before;
21817 }
21818 else
21819 {
21820 if (row->reversed_p)
21821 unproduce_glyphs (it, row->used[TEXT_AREA]
21822 - (n_glyphs_before + i));
21823 row->used[TEXT_AREA] = n_glyphs_before + i;
21824 it->current_x = x;
21825 }
21826 break;
21827 }
21828 else if (x + glyph->pixel_width >= it->first_visible_x)
21829 {
21830 /* Glyph is at least partially visible. */
21831 ++it->hpos;
21832 if (x < it->first_visible_x)
21833 row->x = x - it->first_visible_x;
21834 }
21835 else
21836 {
21837 /* Glyph is off the left margin of the display area.
21838 Should not happen. */
21839 abort ();
21840 }
21841
21842 row->ascent = max (row->ascent, it->max_ascent);
21843 row->height = max (row->height, it->max_ascent + it->max_descent);
21844 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21845 row->phys_height = max (row->phys_height,
21846 it->max_phys_ascent + it->max_phys_descent);
21847 row->extra_line_spacing = max (row->extra_line_spacing,
21848 it->max_extra_line_spacing);
21849 x += glyph->pixel_width;
21850 ++i;
21851 }
21852
21853 /* Stop if max_x reached. */
21854 if (i < nglyphs)
21855 break;
21856
21857 /* Stop at line ends. */
21858 if (ITERATOR_AT_END_OF_LINE_P (it))
21859 {
21860 it->continuation_lines_width = 0;
21861 break;
21862 }
21863
21864 set_iterator_to_next (it, 1);
21865 if (STRINGP (it->string))
21866 it_charpos = IT_STRING_CHARPOS (*it);
21867 else
21868 it_charpos = IT_CHARPOS (*it);
21869
21870 /* Stop if truncating at the right edge. */
21871 if (it->line_wrap == TRUNCATE
21872 && it->current_x >= it->last_visible_x)
21873 {
21874 /* Add truncation mark, but don't do it if the line is
21875 truncated at a padding space. */
21876 if (it_charpos < it->string_nchars)
21877 {
21878 if (!FRAME_WINDOW_P (it->f))
21879 {
21880 int ii, n;
21881
21882 if (it->current_x > it->last_visible_x)
21883 {
21884 if (!row->reversed_p)
21885 {
21886 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21887 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21888 break;
21889 }
21890 else
21891 {
21892 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21893 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21894 break;
21895 unproduce_glyphs (it, ii + 1);
21896 ii = row->used[TEXT_AREA] - (ii + 1);
21897 }
21898 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21899 {
21900 row->used[TEXT_AREA] = ii;
21901 produce_special_glyphs (it, IT_TRUNCATION);
21902 }
21903 }
21904 produce_special_glyphs (it, IT_TRUNCATION);
21905 }
21906 row->truncated_on_right_p = 1;
21907 }
21908 break;
21909 }
21910 }
21911
21912 /* Maybe insert a truncation at the left. */
21913 if (it->first_visible_x
21914 && it_charpos > 0)
21915 {
21916 if (!FRAME_WINDOW_P (it->f))
21917 insert_left_trunc_glyphs (it);
21918 row->truncated_on_left_p = 1;
21919 }
21920
21921 it->face_id = saved_face_id;
21922
21923 /* Value is number of columns displayed. */
21924 return it->hpos - hpos_at_start;
21925 }
21926
21927
21928 \f
21929 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21930 appears as an element of LIST or as the car of an element of LIST.
21931 If PROPVAL is a list, compare each element against LIST in that
21932 way, and return 1/2 if any element of PROPVAL is found in LIST.
21933 Otherwise return 0. This function cannot quit.
21934 The return value is 2 if the text is invisible but with an ellipsis
21935 and 1 if it's invisible and without an ellipsis. */
21936
21937 int
21938 invisible_p (register Lisp_Object propval, Lisp_Object list)
21939 {
21940 register Lisp_Object tail, proptail;
21941
21942 for (tail = list; CONSP (tail); tail = XCDR (tail))
21943 {
21944 register Lisp_Object tem;
21945 tem = XCAR (tail);
21946 if (EQ (propval, tem))
21947 return 1;
21948 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21949 return NILP (XCDR (tem)) ? 1 : 2;
21950 }
21951
21952 if (CONSP (propval))
21953 {
21954 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21955 {
21956 Lisp_Object propelt;
21957 propelt = XCAR (proptail);
21958 for (tail = list; CONSP (tail); tail = XCDR (tail))
21959 {
21960 register Lisp_Object tem;
21961 tem = XCAR (tail);
21962 if (EQ (propelt, tem))
21963 return 1;
21964 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21965 return NILP (XCDR (tem)) ? 1 : 2;
21966 }
21967 }
21968 }
21969
21970 return 0;
21971 }
21972
21973 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21974 doc: /* Non-nil if the property makes the text invisible.
21975 POS-OR-PROP can be a marker or number, in which case it is taken to be
21976 a position in the current buffer and the value of the `invisible' property
21977 is checked; or it can be some other value, which is then presumed to be the
21978 value of the `invisible' property of the text of interest.
21979 The non-nil value returned can be t for truly invisible text or something
21980 else if the text is replaced by an ellipsis. */)
21981 (Lisp_Object pos_or_prop)
21982 {
21983 Lisp_Object prop
21984 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21985 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21986 : pos_or_prop);
21987 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21988 return (invis == 0 ? Qnil
21989 : invis == 1 ? Qt
21990 : make_number (invis));
21991 }
21992
21993 /* Calculate a width or height in pixels from a specification using
21994 the following elements:
21995
21996 SPEC ::=
21997 NUM - a (fractional) multiple of the default font width/height
21998 (NUM) - specifies exactly NUM pixels
21999 UNIT - a fixed number of pixels, see below.
22000 ELEMENT - size of a display element in pixels, see below.
22001 (NUM . SPEC) - equals NUM * SPEC
22002 (+ SPEC SPEC ...) - add pixel values
22003 (- SPEC SPEC ...) - subtract pixel values
22004 (- SPEC) - negate pixel value
22005
22006 NUM ::=
22007 INT or FLOAT - a number constant
22008 SYMBOL - use symbol's (buffer local) variable binding.
22009
22010 UNIT ::=
22011 in - pixels per inch *)
22012 mm - pixels per 1/1000 meter *)
22013 cm - pixels per 1/100 meter *)
22014 width - width of current font in pixels.
22015 height - height of current font in pixels.
22016
22017 *) using the ratio(s) defined in display-pixels-per-inch.
22018
22019 ELEMENT ::=
22020
22021 left-fringe - left fringe width in pixels
22022 right-fringe - right fringe width in pixels
22023
22024 left-margin - left margin width in pixels
22025 right-margin - right margin width in pixels
22026
22027 scroll-bar - scroll-bar area width in pixels
22028
22029 Examples:
22030
22031 Pixels corresponding to 5 inches:
22032 (5 . in)
22033
22034 Total width of non-text areas on left side of window (if scroll-bar is on left):
22035 '(space :width (+ left-fringe left-margin scroll-bar))
22036
22037 Align to first text column (in header line):
22038 '(space :align-to 0)
22039
22040 Align to middle of text area minus half the width of variable `my-image'
22041 containing a loaded image:
22042 '(space :align-to (0.5 . (- text my-image)))
22043
22044 Width of left margin minus width of 1 character in the default font:
22045 '(space :width (- left-margin 1))
22046
22047 Width of left margin minus width of 2 characters in the current font:
22048 '(space :width (- left-margin (2 . width)))
22049
22050 Center 1 character over left-margin (in header line):
22051 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22052
22053 Different ways to express width of left fringe plus left margin minus one pixel:
22054 '(space :width (- (+ left-fringe left-margin) (1)))
22055 '(space :width (+ left-fringe left-margin (- (1))))
22056 '(space :width (+ left-fringe left-margin (-1)))
22057
22058 */
22059
22060 #define NUMVAL(X) \
22061 ((INTEGERP (X) || FLOATP (X)) \
22062 ? XFLOATINT (X) \
22063 : - 1)
22064
22065 static int
22066 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22067 struct font *font, int width_p, int *align_to)
22068 {
22069 double pixels;
22070
22071 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22072 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22073
22074 if (NILP (prop))
22075 return OK_PIXELS (0);
22076
22077 eassert (FRAME_LIVE_P (it->f));
22078
22079 if (SYMBOLP (prop))
22080 {
22081 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22082 {
22083 char *unit = SSDATA (SYMBOL_NAME (prop));
22084
22085 if (unit[0] == 'i' && unit[1] == 'n')
22086 pixels = 1.0;
22087 else if (unit[0] == 'm' && unit[1] == 'm')
22088 pixels = 25.4;
22089 else if (unit[0] == 'c' && unit[1] == 'm')
22090 pixels = 2.54;
22091 else
22092 pixels = 0;
22093 if (pixels > 0)
22094 {
22095 double ppi;
22096 #ifdef HAVE_WINDOW_SYSTEM
22097 if (FRAME_WINDOW_P (it->f)
22098 && (ppi = (width_p
22099 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22100 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22101 ppi > 0))
22102 return OK_PIXELS (ppi / pixels);
22103 #endif
22104
22105 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22106 || (CONSP (Vdisplay_pixels_per_inch)
22107 && (ppi = (width_p
22108 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22109 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22110 ppi > 0)))
22111 return OK_PIXELS (ppi / pixels);
22112
22113 return 0;
22114 }
22115 }
22116
22117 #ifdef HAVE_WINDOW_SYSTEM
22118 if (EQ (prop, Qheight))
22119 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22120 if (EQ (prop, Qwidth))
22121 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22122 #else
22123 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22124 return OK_PIXELS (1);
22125 #endif
22126
22127 if (EQ (prop, Qtext))
22128 return OK_PIXELS (width_p
22129 ? window_box_width (it->w, TEXT_AREA)
22130 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22131
22132 if (align_to && *align_to < 0)
22133 {
22134 *res = 0;
22135 if (EQ (prop, Qleft))
22136 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22137 if (EQ (prop, Qright))
22138 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22139 if (EQ (prop, Qcenter))
22140 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22141 + window_box_width (it->w, TEXT_AREA) / 2);
22142 if (EQ (prop, Qleft_fringe))
22143 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22144 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22145 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22146 if (EQ (prop, Qright_fringe))
22147 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22148 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22149 : window_box_right_offset (it->w, TEXT_AREA));
22150 if (EQ (prop, Qleft_margin))
22151 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22152 if (EQ (prop, Qright_margin))
22153 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22154 if (EQ (prop, Qscroll_bar))
22155 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22156 ? 0
22157 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22158 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22159 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22160 : 0)));
22161 }
22162 else
22163 {
22164 if (EQ (prop, Qleft_fringe))
22165 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22166 if (EQ (prop, Qright_fringe))
22167 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22168 if (EQ (prop, Qleft_margin))
22169 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22170 if (EQ (prop, Qright_margin))
22171 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22172 if (EQ (prop, Qscroll_bar))
22173 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22174 }
22175
22176 prop = buffer_local_value_1 (prop, it->w->buffer);
22177 if (EQ (prop, Qunbound))
22178 prop = Qnil;
22179 }
22180
22181 if (INTEGERP (prop) || FLOATP (prop))
22182 {
22183 int base_unit = (width_p
22184 ? FRAME_COLUMN_WIDTH (it->f)
22185 : FRAME_LINE_HEIGHT (it->f));
22186 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22187 }
22188
22189 if (CONSP (prop))
22190 {
22191 Lisp_Object car = XCAR (prop);
22192 Lisp_Object cdr = XCDR (prop);
22193
22194 if (SYMBOLP (car))
22195 {
22196 #ifdef HAVE_WINDOW_SYSTEM
22197 if (FRAME_WINDOW_P (it->f)
22198 && valid_image_p (prop))
22199 {
22200 ptrdiff_t id = lookup_image (it->f, prop);
22201 struct image *img = IMAGE_FROM_ID (it->f, id);
22202
22203 return OK_PIXELS (width_p ? img->width : img->height);
22204 }
22205 #endif
22206 if (EQ (car, Qplus) || EQ (car, Qminus))
22207 {
22208 int first = 1;
22209 double px;
22210
22211 pixels = 0;
22212 while (CONSP (cdr))
22213 {
22214 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22215 font, width_p, align_to))
22216 return 0;
22217 if (first)
22218 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22219 else
22220 pixels += px;
22221 cdr = XCDR (cdr);
22222 }
22223 if (EQ (car, Qminus))
22224 pixels = -pixels;
22225 return OK_PIXELS (pixels);
22226 }
22227
22228 car = buffer_local_value_1 (car, it->w->buffer);
22229 if (EQ (car, Qunbound))
22230 car = Qnil;
22231 }
22232
22233 if (INTEGERP (car) || FLOATP (car))
22234 {
22235 double fact;
22236 pixels = XFLOATINT (car);
22237 if (NILP (cdr))
22238 return OK_PIXELS (pixels);
22239 if (calc_pixel_width_or_height (&fact, it, cdr,
22240 font, width_p, align_to))
22241 return OK_PIXELS (pixels * fact);
22242 return 0;
22243 }
22244
22245 return 0;
22246 }
22247
22248 return 0;
22249 }
22250
22251 \f
22252 /***********************************************************************
22253 Glyph Display
22254 ***********************************************************************/
22255
22256 #ifdef HAVE_WINDOW_SYSTEM
22257
22258 #ifdef GLYPH_DEBUG
22259
22260 void
22261 dump_glyph_string (struct glyph_string *s)
22262 {
22263 fprintf (stderr, "glyph string\n");
22264 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22265 s->x, s->y, s->width, s->height);
22266 fprintf (stderr, " ybase = %d\n", s->ybase);
22267 fprintf (stderr, " hl = %d\n", s->hl);
22268 fprintf (stderr, " left overhang = %d, right = %d\n",
22269 s->left_overhang, s->right_overhang);
22270 fprintf (stderr, " nchars = %d\n", s->nchars);
22271 fprintf (stderr, " extends to end of line = %d\n",
22272 s->extends_to_end_of_line_p);
22273 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22274 fprintf (stderr, " bg width = %d\n", s->background_width);
22275 }
22276
22277 #endif /* GLYPH_DEBUG */
22278
22279 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22280 of XChar2b structures for S; it can't be allocated in
22281 init_glyph_string because it must be allocated via `alloca'. W
22282 is the window on which S is drawn. ROW and AREA are the glyph row
22283 and area within the row from which S is constructed. START is the
22284 index of the first glyph structure covered by S. HL is a
22285 face-override for drawing S. */
22286
22287 #ifdef HAVE_NTGUI
22288 #define OPTIONAL_HDC(hdc) HDC hdc,
22289 #define DECLARE_HDC(hdc) HDC hdc;
22290 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22291 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22292 #endif
22293
22294 #ifndef OPTIONAL_HDC
22295 #define OPTIONAL_HDC(hdc)
22296 #define DECLARE_HDC(hdc)
22297 #define ALLOCATE_HDC(hdc, f)
22298 #define RELEASE_HDC(hdc, f)
22299 #endif
22300
22301 static void
22302 init_glyph_string (struct glyph_string *s,
22303 OPTIONAL_HDC (hdc)
22304 XChar2b *char2b, struct window *w, struct glyph_row *row,
22305 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22306 {
22307 memset (s, 0, sizeof *s);
22308 s->w = w;
22309 s->f = XFRAME (w->frame);
22310 #ifdef HAVE_NTGUI
22311 s->hdc = hdc;
22312 #endif
22313 s->display = FRAME_X_DISPLAY (s->f);
22314 s->window = FRAME_X_WINDOW (s->f);
22315 s->char2b = char2b;
22316 s->hl = hl;
22317 s->row = row;
22318 s->area = area;
22319 s->first_glyph = row->glyphs[area] + start;
22320 s->height = row->height;
22321 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22322 s->ybase = s->y + row->ascent;
22323 }
22324
22325
22326 /* Append the list of glyph strings with head H and tail T to the list
22327 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22328
22329 static inline void
22330 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22331 struct glyph_string *h, struct glyph_string *t)
22332 {
22333 if (h)
22334 {
22335 if (*head)
22336 (*tail)->next = h;
22337 else
22338 *head = h;
22339 h->prev = *tail;
22340 *tail = t;
22341 }
22342 }
22343
22344
22345 /* Prepend the list of glyph strings with head H and tail T to the
22346 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22347 result. */
22348
22349 static inline void
22350 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22351 struct glyph_string *h, struct glyph_string *t)
22352 {
22353 if (h)
22354 {
22355 if (*head)
22356 (*head)->prev = t;
22357 else
22358 *tail = t;
22359 t->next = *head;
22360 *head = h;
22361 }
22362 }
22363
22364
22365 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22366 Set *HEAD and *TAIL to the resulting list. */
22367
22368 static inline void
22369 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22370 struct glyph_string *s)
22371 {
22372 s->next = s->prev = NULL;
22373 append_glyph_string_lists (head, tail, s, s);
22374 }
22375
22376
22377 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22378 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22379 make sure that X resources for the face returned are allocated.
22380 Value is a pointer to a realized face that is ready for display if
22381 DISPLAY_P is non-zero. */
22382
22383 static inline struct face *
22384 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22385 XChar2b *char2b, int display_p)
22386 {
22387 struct face *face = FACE_FROM_ID (f, face_id);
22388
22389 if (face->font)
22390 {
22391 unsigned code = face->font->driver->encode_char (face->font, c);
22392
22393 if (code != FONT_INVALID_CODE)
22394 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22395 else
22396 STORE_XCHAR2B (char2b, 0, 0);
22397 }
22398
22399 /* Make sure X resources of the face are allocated. */
22400 #ifdef HAVE_X_WINDOWS
22401 if (display_p)
22402 #endif
22403 {
22404 eassert (face != NULL);
22405 PREPARE_FACE_FOR_DISPLAY (f, face);
22406 }
22407
22408 return face;
22409 }
22410
22411
22412 /* Get face and two-byte form of character glyph GLYPH on frame F.
22413 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22414 a pointer to a realized face that is ready for display. */
22415
22416 static inline struct face *
22417 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22418 XChar2b *char2b, int *two_byte_p)
22419 {
22420 struct face *face;
22421
22422 eassert (glyph->type == CHAR_GLYPH);
22423 face = FACE_FROM_ID (f, glyph->face_id);
22424
22425 if (two_byte_p)
22426 *two_byte_p = 0;
22427
22428 if (face->font)
22429 {
22430 unsigned code;
22431
22432 if (CHAR_BYTE8_P (glyph->u.ch))
22433 code = CHAR_TO_BYTE8 (glyph->u.ch);
22434 else
22435 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22436
22437 if (code != FONT_INVALID_CODE)
22438 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22439 else
22440 STORE_XCHAR2B (char2b, 0, 0);
22441 }
22442
22443 /* Make sure X resources of the face are allocated. */
22444 eassert (face != NULL);
22445 PREPARE_FACE_FOR_DISPLAY (f, face);
22446 return face;
22447 }
22448
22449
22450 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22451 Return 1 if FONT has a glyph for C, otherwise return 0. */
22452
22453 static inline int
22454 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22455 {
22456 unsigned code;
22457
22458 if (CHAR_BYTE8_P (c))
22459 code = CHAR_TO_BYTE8 (c);
22460 else
22461 code = font->driver->encode_char (font, c);
22462
22463 if (code == FONT_INVALID_CODE)
22464 return 0;
22465 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22466 return 1;
22467 }
22468
22469
22470 /* Fill glyph string S with composition components specified by S->cmp.
22471
22472 BASE_FACE is the base face of the composition.
22473 S->cmp_from is the index of the first component for S.
22474
22475 OVERLAPS non-zero means S should draw the foreground only, and use
22476 its physical height for clipping. See also draw_glyphs.
22477
22478 Value is the index of a component not in S. */
22479
22480 static int
22481 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22482 int overlaps)
22483 {
22484 int i;
22485 /* For all glyphs of this composition, starting at the offset
22486 S->cmp_from, until we reach the end of the definition or encounter a
22487 glyph that requires the different face, add it to S. */
22488 struct face *face;
22489
22490 eassert (s);
22491
22492 s->for_overlaps = overlaps;
22493 s->face = NULL;
22494 s->font = NULL;
22495 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22496 {
22497 int c = COMPOSITION_GLYPH (s->cmp, i);
22498
22499 /* TAB in a composition means display glyphs with padding space
22500 on the left or right. */
22501 if (c != '\t')
22502 {
22503 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22504 -1, Qnil);
22505
22506 face = get_char_face_and_encoding (s->f, c, face_id,
22507 s->char2b + i, 1);
22508 if (face)
22509 {
22510 if (! s->face)
22511 {
22512 s->face = face;
22513 s->font = s->face->font;
22514 }
22515 else if (s->face != face)
22516 break;
22517 }
22518 }
22519 ++s->nchars;
22520 }
22521 s->cmp_to = i;
22522
22523 if (s->face == NULL)
22524 {
22525 s->face = base_face->ascii_face;
22526 s->font = s->face->font;
22527 }
22528
22529 /* All glyph strings for the same composition has the same width,
22530 i.e. the width set for the first component of the composition. */
22531 s->width = s->first_glyph->pixel_width;
22532
22533 /* If the specified font could not be loaded, use the frame's
22534 default font, but record the fact that we couldn't load it in
22535 the glyph string so that we can draw rectangles for the
22536 characters of the glyph string. */
22537 if (s->font == NULL)
22538 {
22539 s->font_not_found_p = 1;
22540 s->font = FRAME_FONT (s->f);
22541 }
22542
22543 /* Adjust base line for subscript/superscript text. */
22544 s->ybase += s->first_glyph->voffset;
22545
22546 /* This glyph string must always be drawn with 16-bit functions. */
22547 s->two_byte_p = 1;
22548
22549 return s->cmp_to;
22550 }
22551
22552 static int
22553 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22554 int start, int end, int overlaps)
22555 {
22556 struct glyph *glyph, *last;
22557 Lisp_Object lgstring;
22558 int i;
22559
22560 s->for_overlaps = overlaps;
22561 glyph = s->row->glyphs[s->area] + start;
22562 last = s->row->glyphs[s->area] + end;
22563 s->cmp_id = glyph->u.cmp.id;
22564 s->cmp_from = glyph->slice.cmp.from;
22565 s->cmp_to = glyph->slice.cmp.to + 1;
22566 s->face = FACE_FROM_ID (s->f, face_id);
22567 lgstring = composition_gstring_from_id (s->cmp_id);
22568 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22569 glyph++;
22570 while (glyph < last
22571 && glyph->u.cmp.automatic
22572 && glyph->u.cmp.id == s->cmp_id
22573 && s->cmp_to == glyph->slice.cmp.from)
22574 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22575
22576 for (i = s->cmp_from; i < s->cmp_to; i++)
22577 {
22578 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22579 unsigned code = LGLYPH_CODE (lglyph);
22580
22581 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22582 }
22583 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22584 return glyph - s->row->glyphs[s->area];
22585 }
22586
22587
22588 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22589 See the comment of fill_glyph_string for arguments.
22590 Value is the index of the first glyph not in S. */
22591
22592
22593 static int
22594 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22595 int start, int end, int overlaps)
22596 {
22597 struct glyph *glyph, *last;
22598 int voffset;
22599
22600 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22601 s->for_overlaps = overlaps;
22602 glyph = s->row->glyphs[s->area] + start;
22603 last = s->row->glyphs[s->area] + end;
22604 voffset = glyph->voffset;
22605 s->face = FACE_FROM_ID (s->f, face_id);
22606 s->font = s->face->font;
22607 s->nchars = 1;
22608 s->width = glyph->pixel_width;
22609 glyph++;
22610 while (glyph < last
22611 && glyph->type == GLYPHLESS_GLYPH
22612 && glyph->voffset == voffset
22613 && glyph->face_id == face_id)
22614 {
22615 s->nchars++;
22616 s->width += glyph->pixel_width;
22617 glyph++;
22618 }
22619 s->ybase += voffset;
22620 return glyph - s->row->glyphs[s->area];
22621 }
22622
22623
22624 /* Fill glyph string S from a sequence of character glyphs.
22625
22626 FACE_ID is the face id of the string. START is the index of the
22627 first glyph to consider, END is the index of the last + 1.
22628 OVERLAPS non-zero means S should draw the foreground only, and use
22629 its physical height for clipping. See also draw_glyphs.
22630
22631 Value is the index of the first glyph not in S. */
22632
22633 static int
22634 fill_glyph_string (struct glyph_string *s, int face_id,
22635 int start, int end, int overlaps)
22636 {
22637 struct glyph *glyph, *last;
22638 int voffset;
22639 int glyph_not_available_p;
22640
22641 eassert (s->f == XFRAME (s->w->frame));
22642 eassert (s->nchars == 0);
22643 eassert (start >= 0 && end > start);
22644
22645 s->for_overlaps = overlaps;
22646 glyph = s->row->glyphs[s->area] + start;
22647 last = s->row->glyphs[s->area] + end;
22648 voffset = glyph->voffset;
22649 s->padding_p = glyph->padding_p;
22650 glyph_not_available_p = glyph->glyph_not_available_p;
22651
22652 while (glyph < last
22653 && glyph->type == CHAR_GLYPH
22654 && glyph->voffset == voffset
22655 /* Same face id implies same font, nowadays. */
22656 && glyph->face_id == face_id
22657 && glyph->glyph_not_available_p == glyph_not_available_p)
22658 {
22659 int two_byte_p;
22660
22661 s->face = get_glyph_face_and_encoding (s->f, glyph,
22662 s->char2b + s->nchars,
22663 &two_byte_p);
22664 s->two_byte_p = two_byte_p;
22665 ++s->nchars;
22666 eassert (s->nchars <= end - start);
22667 s->width += glyph->pixel_width;
22668 if (glyph++->padding_p != s->padding_p)
22669 break;
22670 }
22671
22672 s->font = s->face->font;
22673
22674 /* If the specified font could not be loaded, use the frame's font,
22675 but record the fact that we couldn't load it in
22676 S->font_not_found_p so that we can draw rectangles for the
22677 characters of the glyph string. */
22678 if (s->font == NULL || glyph_not_available_p)
22679 {
22680 s->font_not_found_p = 1;
22681 s->font = FRAME_FONT (s->f);
22682 }
22683
22684 /* Adjust base line for subscript/superscript text. */
22685 s->ybase += voffset;
22686
22687 eassert (s->face && s->face->gc);
22688 return glyph - s->row->glyphs[s->area];
22689 }
22690
22691
22692 /* Fill glyph string S from image glyph S->first_glyph. */
22693
22694 static void
22695 fill_image_glyph_string (struct glyph_string *s)
22696 {
22697 eassert (s->first_glyph->type == IMAGE_GLYPH);
22698 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22699 eassert (s->img);
22700 s->slice = s->first_glyph->slice.img;
22701 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22702 s->font = s->face->font;
22703 s->width = s->first_glyph->pixel_width;
22704
22705 /* Adjust base line for subscript/superscript text. */
22706 s->ybase += s->first_glyph->voffset;
22707 }
22708
22709
22710 /* Fill glyph string S from a sequence of stretch glyphs.
22711
22712 START is the index of the first glyph to consider,
22713 END is the index of the last + 1.
22714
22715 Value is the index of the first glyph not in S. */
22716
22717 static int
22718 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22719 {
22720 struct glyph *glyph, *last;
22721 int voffset, face_id;
22722
22723 eassert (s->first_glyph->type == STRETCH_GLYPH);
22724
22725 glyph = s->row->glyphs[s->area] + start;
22726 last = s->row->glyphs[s->area] + end;
22727 face_id = glyph->face_id;
22728 s->face = FACE_FROM_ID (s->f, face_id);
22729 s->font = s->face->font;
22730 s->width = glyph->pixel_width;
22731 s->nchars = 1;
22732 voffset = glyph->voffset;
22733
22734 for (++glyph;
22735 (glyph < last
22736 && glyph->type == STRETCH_GLYPH
22737 && glyph->voffset == voffset
22738 && glyph->face_id == face_id);
22739 ++glyph)
22740 s->width += glyph->pixel_width;
22741
22742 /* Adjust base line for subscript/superscript text. */
22743 s->ybase += voffset;
22744
22745 /* The case that face->gc == 0 is handled when drawing the glyph
22746 string by calling PREPARE_FACE_FOR_DISPLAY. */
22747 eassert (s->face);
22748 return glyph - s->row->glyphs[s->area];
22749 }
22750
22751 static struct font_metrics *
22752 get_per_char_metric (struct font *font, XChar2b *char2b)
22753 {
22754 static struct font_metrics metrics;
22755 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22756
22757 if (! font || code == FONT_INVALID_CODE)
22758 return NULL;
22759 font->driver->text_extents (font, &code, 1, &metrics);
22760 return &metrics;
22761 }
22762
22763 /* EXPORT for RIF:
22764 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22765 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22766 assumed to be zero. */
22767
22768 void
22769 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22770 {
22771 *left = *right = 0;
22772
22773 if (glyph->type == CHAR_GLYPH)
22774 {
22775 struct face *face;
22776 XChar2b char2b;
22777 struct font_metrics *pcm;
22778
22779 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22780 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22781 {
22782 if (pcm->rbearing > pcm->width)
22783 *right = pcm->rbearing - pcm->width;
22784 if (pcm->lbearing < 0)
22785 *left = -pcm->lbearing;
22786 }
22787 }
22788 else if (glyph->type == COMPOSITE_GLYPH)
22789 {
22790 if (! glyph->u.cmp.automatic)
22791 {
22792 struct composition *cmp = composition_table[glyph->u.cmp.id];
22793
22794 if (cmp->rbearing > cmp->pixel_width)
22795 *right = cmp->rbearing - cmp->pixel_width;
22796 if (cmp->lbearing < 0)
22797 *left = - cmp->lbearing;
22798 }
22799 else
22800 {
22801 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22802 struct font_metrics metrics;
22803
22804 composition_gstring_width (gstring, glyph->slice.cmp.from,
22805 glyph->slice.cmp.to + 1, &metrics);
22806 if (metrics.rbearing > metrics.width)
22807 *right = metrics.rbearing - metrics.width;
22808 if (metrics.lbearing < 0)
22809 *left = - metrics.lbearing;
22810 }
22811 }
22812 }
22813
22814
22815 /* Return the index of the first glyph preceding glyph string S that
22816 is overwritten by S because of S's left overhang. Value is -1
22817 if no glyphs are overwritten. */
22818
22819 static int
22820 left_overwritten (struct glyph_string *s)
22821 {
22822 int k;
22823
22824 if (s->left_overhang)
22825 {
22826 int x = 0, i;
22827 struct glyph *glyphs = s->row->glyphs[s->area];
22828 int first = s->first_glyph - glyphs;
22829
22830 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22831 x -= glyphs[i].pixel_width;
22832
22833 k = i + 1;
22834 }
22835 else
22836 k = -1;
22837
22838 return k;
22839 }
22840
22841
22842 /* Return the index of the first glyph preceding glyph string S that
22843 is overwriting S because of its right overhang. Value is -1 if no
22844 glyph in front of S overwrites S. */
22845
22846 static int
22847 left_overwriting (struct glyph_string *s)
22848 {
22849 int i, k, x;
22850 struct glyph *glyphs = s->row->glyphs[s->area];
22851 int first = s->first_glyph - glyphs;
22852
22853 k = -1;
22854 x = 0;
22855 for (i = first - 1; i >= 0; --i)
22856 {
22857 int left, right;
22858 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22859 if (x + right > 0)
22860 k = i;
22861 x -= glyphs[i].pixel_width;
22862 }
22863
22864 return k;
22865 }
22866
22867
22868 /* Return the index of the last glyph following glyph string S that is
22869 overwritten by S because of S's right overhang. Value is -1 if
22870 no such glyph is found. */
22871
22872 static int
22873 right_overwritten (struct glyph_string *s)
22874 {
22875 int k = -1;
22876
22877 if (s->right_overhang)
22878 {
22879 int x = 0, i;
22880 struct glyph *glyphs = s->row->glyphs[s->area];
22881 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22882 int end = s->row->used[s->area];
22883
22884 for (i = first; i < end && s->right_overhang > x; ++i)
22885 x += glyphs[i].pixel_width;
22886
22887 k = i;
22888 }
22889
22890 return k;
22891 }
22892
22893
22894 /* Return the index of the last glyph following glyph string S that
22895 overwrites S because of its left overhang. Value is negative
22896 if no such glyph is found. */
22897
22898 static int
22899 right_overwriting (struct glyph_string *s)
22900 {
22901 int i, k, x;
22902 int end = s->row->used[s->area];
22903 struct glyph *glyphs = s->row->glyphs[s->area];
22904 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22905
22906 k = -1;
22907 x = 0;
22908 for (i = first; i < end; ++i)
22909 {
22910 int left, right;
22911 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22912 if (x - left < 0)
22913 k = i;
22914 x += glyphs[i].pixel_width;
22915 }
22916
22917 return k;
22918 }
22919
22920
22921 /* Set background width of glyph string S. START is the index of the
22922 first glyph following S. LAST_X is the right-most x-position + 1
22923 in the drawing area. */
22924
22925 static inline void
22926 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22927 {
22928 /* If the face of this glyph string has to be drawn to the end of
22929 the drawing area, set S->extends_to_end_of_line_p. */
22930
22931 if (start == s->row->used[s->area]
22932 && s->area == TEXT_AREA
22933 && ((s->row->fill_line_p
22934 && (s->hl == DRAW_NORMAL_TEXT
22935 || s->hl == DRAW_IMAGE_RAISED
22936 || s->hl == DRAW_IMAGE_SUNKEN))
22937 || s->hl == DRAW_MOUSE_FACE))
22938 s->extends_to_end_of_line_p = 1;
22939
22940 /* If S extends its face to the end of the line, set its
22941 background_width to the distance to the right edge of the drawing
22942 area. */
22943 if (s->extends_to_end_of_line_p)
22944 s->background_width = last_x - s->x + 1;
22945 else
22946 s->background_width = s->width;
22947 }
22948
22949
22950 /* Compute overhangs and x-positions for glyph string S and its
22951 predecessors, or successors. X is the starting x-position for S.
22952 BACKWARD_P non-zero means process predecessors. */
22953
22954 static void
22955 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22956 {
22957 if (backward_p)
22958 {
22959 while (s)
22960 {
22961 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22962 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22963 x -= s->width;
22964 s->x = x;
22965 s = s->prev;
22966 }
22967 }
22968 else
22969 {
22970 while (s)
22971 {
22972 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22973 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22974 s->x = x;
22975 x += s->width;
22976 s = s->next;
22977 }
22978 }
22979 }
22980
22981
22982
22983 /* The following macros are only called from draw_glyphs below.
22984 They reference the following parameters of that function directly:
22985 `w', `row', `area', and `overlap_p'
22986 as well as the following local variables:
22987 `s', `f', and `hdc' (in W32) */
22988
22989 #ifdef HAVE_NTGUI
22990 /* On W32, silently add local `hdc' variable to argument list of
22991 init_glyph_string. */
22992 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22993 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22994 #else
22995 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22996 init_glyph_string (s, char2b, w, row, area, start, hl)
22997 #endif
22998
22999 /* Add a glyph string for a stretch glyph to the list of strings
23000 between HEAD and TAIL. START is the index of the stretch glyph in
23001 row area AREA of glyph row ROW. END is the index of the last glyph
23002 in that glyph row area. X is the current output position assigned
23003 to the new glyph string constructed. HL overrides that face of the
23004 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23005 is the right-most x-position of the drawing area. */
23006
23007 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23008 and below -- keep them on one line. */
23009 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23010 do \
23011 { \
23012 s = alloca (sizeof *s); \
23013 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23014 START = fill_stretch_glyph_string (s, START, END); \
23015 append_glyph_string (&HEAD, &TAIL, s); \
23016 s->x = (X); \
23017 } \
23018 while (0)
23019
23020
23021 /* Add a glyph string for an image glyph to the list of strings
23022 between HEAD and TAIL. START is the index of the image glyph in
23023 row area AREA of glyph row ROW. END is the index of the last glyph
23024 in that glyph row area. X is the current output position assigned
23025 to the new glyph string constructed. HL overrides that face of the
23026 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23027 is the right-most x-position of the drawing area. */
23028
23029 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23030 do \
23031 { \
23032 s = alloca (sizeof *s); \
23033 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23034 fill_image_glyph_string (s); \
23035 append_glyph_string (&HEAD, &TAIL, s); \
23036 ++START; \
23037 s->x = (X); \
23038 } \
23039 while (0)
23040
23041
23042 /* Add a glyph string for a sequence of character glyphs to the list
23043 of strings between HEAD and TAIL. START is the index of the first
23044 glyph in row area AREA of glyph row ROW that is part of the new
23045 glyph string. END is the index of the last glyph in that glyph row
23046 area. X is the current output position assigned to the new glyph
23047 string constructed. HL overrides that face of the glyph; e.g. it
23048 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23049 right-most x-position of the drawing area. */
23050
23051 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23052 do \
23053 { \
23054 int face_id; \
23055 XChar2b *char2b; \
23056 \
23057 face_id = (row)->glyphs[area][START].face_id; \
23058 \
23059 s = alloca (sizeof *s); \
23060 char2b = alloca ((END - START) * sizeof *char2b); \
23061 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23062 append_glyph_string (&HEAD, &TAIL, s); \
23063 s->x = (X); \
23064 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23065 } \
23066 while (0)
23067
23068
23069 /* Add a glyph string for a composite sequence to the list of strings
23070 between HEAD and TAIL. START is the index of the first glyph in
23071 row area AREA of glyph row ROW that is part of the new glyph
23072 string. END is the index of the last glyph in that glyph row area.
23073 X is the current output position assigned to the new glyph string
23074 constructed. HL overrides that face of the glyph; e.g. it is
23075 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23076 x-position of the drawing area. */
23077
23078 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23079 do { \
23080 int face_id = (row)->glyphs[area][START].face_id; \
23081 struct face *base_face = FACE_FROM_ID (f, face_id); \
23082 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23083 struct composition *cmp = composition_table[cmp_id]; \
23084 XChar2b *char2b; \
23085 struct glyph_string *first_s = NULL; \
23086 int n; \
23087 \
23088 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23089 \
23090 /* Make glyph_strings for each glyph sequence that is drawable by \
23091 the same face, and append them to HEAD/TAIL. */ \
23092 for (n = 0; n < cmp->glyph_len;) \
23093 { \
23094 s = alloca (sizeof *s); \
23095 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23096 append_glyph_string (&(HEAD), &(TAIL), s); \
23097 s->cmp = cmp; \
23098 s->cmp_from = n; \
23099 s->x = (X); \
23100 if (n == 0) \
23101 first_s = s; \
23102 n = fill_composite_glyph_string (s, base_face, overlaps); \
23103 } \
23104 \
23105 ++START; \
23106 s = first_s; \
23107 } while (0)
23108
23109
23110 /* Add a glyph string for a glyph-string sequence to the list of strings
23111 between HEAD and TAIL. */
23112
23113 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23114 do { \
23115 int face_id; \
23116 XChar2b *char2b; \
23117 Lisp_Object gstring; \
23118 \
23119 face_id = (row)->glyphs[area][START].face_id; \
23120 gstring = (composition_gstring_from_id \
23121 ((row)->glyphs[area][START].u.cmp.id)); \
23122 s = alloca (sizeof *s); \
23123 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23124 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23125 append_glyph_string (&(HEAD), &(TAIL), s); \
23126 s->x = (X); \
23127 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23128 } while (0)
23129
23130
23131 /* Add a glyph string for a sequence of glyphless character's glyphs
23132 to the list of strings between HEAD and TAIL. The meanings of
23133 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23134
23135 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23136 do \
23137 { \
23138 int face_id; \
23139 \
23140 face_id = (row)->glyphs[area][START].face_id; \
23141 \
23142 s = alloca (sizeof *s); \
23143 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23144 append_glyph_string (&HEAD, &TAIL, s); \
23145 s->x = (X); \
23146 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23147 overlaps); \
23148 } \
23149 while (0)
23150
23151
23152 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23153 of AREA of glyph row ROW on window W between indices START and END.
23154 HL overrides the face for drawing glyph strings, e.g. it is
23155 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23156 x-positions of the drawing area.
23157
23158 This is an ugly monster macro construct because we must use alloca
23159 to allocate glyph strings (because draw_glyphs can be called
23160 asynchronously). */
23161
23162 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23163 do \
23164 { \
23165 HEAD = TAIL = NULL; \
23166 while (START < END) \
23167 { \
23168 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23169 switch (first_glyph->type) \
23170 { \
23171 case CHAR_GLYPH: \
23172 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23173 HL, X, LAST_X); \
23174 break; \
23175 \
23176 case COMPOSITE_GLYPH: \
23177 if (first_glyph->u.cmp.automatic) \
23178 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23179 HL, X, LAST_X); \
23180 else \
23181 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23182 HL, X, LAST_X); \
23183 break; \
23184 \
23185 case STRETCH_GLYPH: \
23186 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23187 HL, X, LAST_X); \
23188 break; \
23189 \
23190 case IMAGE_GLYPH: \
23191 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23192 HL, X, LAST_X); \
23193 break; \
23194 \
23195 case GLYPHLESS_GLYPH: \
23196 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23197 HL, X, LAST_X); \
23198 break; \
23199 \
23200 default: \
23201 abort (); \
23202 } \
23203 \
23204 if (s) \
23205 { \
23206 set_glyph_string_background_width (s, START, LAST_X); \
23207 (X) += s->width; \
23208 } \
23209 } \
23210 } while (0)
23211
23212
23213 /* Draw glyphs between START and END in AREA of ROW on window W,
23214 starting at x-position X. X is relative to AREA in W. HL is a
23215 face-override with the following meaning:
23216
23217 DRAW_NORMAL_TEXT draw normally
23218 DRAW_CURSOR draw in cursor face
23219 DRAW_MOUSE_FACE draw in mouse face.
23220 DRAW_INVERSE_VIDEO draw in mode line face
23221 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23222 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23223
23224 If OVERLAPS is non-zero, draw only the foreground of characters and
23225 clip to the physical height of ROW. Non-zero value also defines
23226 the overlapping part to be drawn:
23227
23228 OVERLAPS_PRED overlap with preceding rows
23229 OVERLAPS_SUCC overlap with succeeding rows
23230 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23231 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23232
23233 Value is the x-position reached, relative to AREA of W. */
23234
23235 static int
23236 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23237 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23238 enum draw_glyphs_face hl, int overlaps)
23239 {
23240 struct glyph_string *head, *tail;
23241 struct glyph_string *s;
23242 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23243 int i, j, x_reached, last_x, area_left = 0;
23244 struct frame *f = XFRAME (WINDOW_FRAME (w));
23245 DECLARE_HDC (hdc);
23246
23247 ALLOCATE_HDC (hdc, f);
23248
23249 /* Let's rather be paranoid than getting a SEGV. */
23250 end = min (end, row->used[area]);
23251 start = max (0, start);
23252 start = min (end, start);
23253
23254 /* Translate X to frame coordinates. Set last_x to the right
23255 end of the drawing area. */
23256 if (row->full_width_p)
23257 {
23258 /* X is relative to the left edge of W, without scroll bars
23259 or fringes. */
23260 area_left = WINDOW_LEFT_EDGE_X (w);
23261 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23262 }
23263 else
23264 {
23265 area_left = window_box_left (w, area);
23266 last_x = area_left + window_box_width (w, area);
23267 }
23268 x += area_left;
23269
23270 /* Build a doubly-linked list of glyph_string structures between
23271 head and tail from what we have to draw. Note that the macro
23272 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23273 the reason we use a separate variable `i'. */
23274 i = start;
23275 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23276 if (tail)
23277 x_reached = tail->x + tail->background_width;
23278 else
23279 x_reached = x;
23280
23281 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23282 the row, redraw some glyphs in front or following the glyph
23283 strings built above. */
23284 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23285 {
23286 struct glyph_string *h, *t;
23287 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23288 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23289 int check_mouse_face = 0;
23290 int dummy_x = 0;
23291
23292 /* If mouse highlighting is on, we may need to draw adjacent
23293 glyphs using mouse-face highlighting. */
23294 if (area == TEXT_AREA && row->mouse_face_p)
23295 {
23296 struct glyph_row *mouse_beg_row, *mouse_end_row;
23297
23298 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23299 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23300
23301 if (row >= mouse_beg_row && row <= mouse_end_row)
23302 {
23303 check_mouse_face = 1;
23304 mouse_beg_col = (row == mouse_beg_row)
23305 ? hlinfo->mouse_face_beg_col : 0;
23306 mouse_end_col = (row == mouse_end_row)
23307 ? hlinfo->mouse_face_end_col
23308 : row->used[TEXT_AREA];
23309 }
23310 }
23311
23312 /* Compute overhangs for all glyph strings. */
23313 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23314 for (s = head; s; s = s->next)
23315 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23316
23317 /* Prepend glyph strings for glyphs in front of the first glyph
23318 string that are overwritten because of the first glyph
23319 string's left overhang. The background of all strings
23320 prepended must be drawn because the first glyph string
23321 draws over it. */
23322 i = left_overwritten (head);
23323 if (i >= 0)
23324 {
23325 enum draw_glyphs_face overlap_hl;
23326
23327 /* If this row contains mouse highlighting, attempt to draw
23328 the overlapped glyphs with the correct highlight. This
23329 code fails if the overlap encompasses more than one glyph
23330 and mouse-highlight spans only some of these glyphs.
23331 However, making it work perfectly involves a lot more
23332 code, and I don't know if the pathological case occurs in
23333 practice, so we'll stick to this for now. --- cyd */
23334 if (check_mouse_face
23335 && mouse_beg_col < start && mouse_end_col > i)
23336 overlap_hl = DRAW_MOUSE_FACE;
23337 else
23338 overlap_hl = DRAW_NORMAL_TEXT;
23339
23340 j = i;
23341 BUILD_GLYPH_STRINGS (j, start, h, t,
23342 overlap_hl, dummy_x, last_x);
23343 start = i;
23344 compute_overhangs_and_x (t, head->x, 1);
23345 prepend_glyph_string_lists (&head, &tail, h, t);
23346 clip_head = head;
23347 }
23348
23349 /* Prepend glyph strings for glyphs in front of the first glyph
23350 string that overwrite that glyph string because of their
23351 right overhang. For these strings, only the foreground must
23352 be drawn, because it draws over the glyph string at `head'.
23353 The background must not be drawn because this would overwrite
23354 right overhangs of preceding glyphs for which no glyph
23355 strings exist. */
23356 i = left_overwriting (head);
23357 if (i >= 0)
23358 {
23359 enum draw_glyphs_face overlap_hl;
23360
23361 if (check_mouse_face
23362 && mouse_beg_col < start && mouse_end_col > i)
23363 overlap_hl = DRAW_MOUSE_FACE;
23364 else
23365 overlap_hl = DRAW_NORMAL_TEXT;
23366
23367 clip_head = head;
23368 BUILD_GLYPH_STRINGS (i, start, h, t,
23369 overlap_hl, dummy_x, last_x);
23370 for (s = h; s; s = s->next)
23371 s->background_filled_p = 1;
23372 compute_overhangs_and_x (t, head->x, 1);
23373 prepend_glyph_string_lists (&head, &tail, h, t);
23374 }
23375
23376 /* Append glyphs strings for glyphs following the last glyph
23377 string tail that are overwritten by tail. The background of
23378 these strings has to be drawn because tail's foreground draws
23379 over it. */
23380 i = right_overwritten (tail);
23381 if (i >= 0)
23382 {
23383 enum draw_glyphs_face overlap_hl;
23384
23385 if (check_mouse_face
23386 && mouse_beg_col < i && mouse_end_col > end)
23387 overlap_hl = DRAW_MOUSE_FACE;
23388 else
23389 overlap_hl = DRAW_NORMAL_TEXT;
23390
23391 BUILD_GLYPH_STRINGS (end, i, h, t,
23392 overlap_hl, x, last_x);
23393 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23394 we don't have `end = i;' here. */
23395 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23396 append_glyph_string_lists (&head, &tail, h, t);
23397 clip_tail = tail;
23398 }
23399
23400 /* Append glyph strings for glyphs following the last glyph
23401 string tail that overwrite tail. The foreground of such
23402 glyphs has to be drawn because it writes into the background
23403 of tail. The background must not be drawn because it could
23404 paint over the foreground of following glyphs. */
23405 i = right_overwriting (tail);
23406 if (i >= 0)
23407 {
23408 enum draw_glyphs_face overlap_hl;
23409 if (check_mouse_face
23410 && mouse_beg_col < i && mouse_end_col > end)
23411 overlap_hl = DRAW_MOUSE_FACE;
23412 else
23413 overlap_hl = DRAW_NORMAL_TEXT;
23414
23415 clip_tail = tail;
23416 i++; /* We must include the Ith glyph. */
23417 BUILD_GLYPH_STRINGS (end, i, h, t,
23418 overlap_hl, x, last_x);
23419 for (s = h; s; s = s->next)
23420 s->background_filled_p = 1;
23421 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23422 append_glyph_string_lists (&head, &tail, h, t);
23423 }
23424 if (clip_head || clip_tail)
23425 for (s = head; s; s = s->next)
23426 {
23427 s->clip_head = clip_head;
23428 s->clip_tail = clip_tail;
23429 }
23430 }
23431
23432 /* Draw all strings. */
23433 for (s = head; s; s = s->next)
23434 FRAME_RIF (f)->draw_glyph_string (s);
23435
23436 #ifndef HAVE_NS
23437 /* When focus a sole frame and move horizontally, this sets on_p to 0
23438 causing a failure to erase prev cursor position. */
23439 if (area == TEXT_AREA
23440 && !row->full_width_p
23441 /* When drawing overlapping rows, only the glyph strings'
23442 foreground is drawn, which doesn't erase a cursor
23443 completely. */
23444 && !overlaps)
23445 {
23446 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23447 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23448 : (tail ? tail->x + tail->background_width : x));
23449 x0 -= area_left;
23450 x1 -= area_left;
23451
23452 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23453 row->y, MATRIX_ROW_BOTTOM_Y (row));
23454 }
23455 #endif
23456
23457 /* Value is the x-position up to which drawn, relative to AREA of W.
23458 This doesn't include parts drawn because of overhangs. */
23459 if (row->full_width_p)
23460 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23461 else
23462 x_reached -= area_left;
23463
23464 RELEASE_HDC (hdc, f);
23465
23466 return x_reached;
23467 }
23468
23469 /* Expand row matrix if too narrow. Don't expand if area
23470 is not present. */
23471
23472 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23473 { \
23474 if (!fonts_changed_p \
23475 && (it->glyph_row->glyphs[area] \
23476 < it->glyph_row->glyphs[area + 1])) \
23477 { \
23478 it->w->ncols_scale_factor++; \
23479 fonts_changed_p = 1; \
23480 } \
23481 }
23482
23483 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23484 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23485
23486 static inline void
23487 append_glyph (struct it *it)
23488 {
23489 struct glyph *glyph;
23490 enum glyph_row_area area = it->area;
23491
23492 eassert (it->glyph_row);
23493 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23494
23495 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23496 if (glyph < it->glyph_row->glyphs[area + 1])
23497 {
23498 /* If the glyph row is reversed, we need to prepend the glyph
23499 rather than append it. */
23500 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23501 {
23502 struct glyph *g;
23503
23504 /* Make room for the additional glyph. */
23505 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23506 g[1] = *g;
23507 glyph = it->glyph_row->glyphs[area];
23508 }
23509 glyph->charpos = CHARPOS (it->position);
23510 glyph->object = it->object;
23511 if (it->pixel_width > 0)
23512 {
23513 glyph->pixel_width = it->pixel_width;
23514 glyph->padding_p = 0;
23515 }
23516 else
23517 {
23518 /* Assure at least 1-pixel width. Otherwise, cursor can't
23519 be displayed correctly. */
23520 glyph->pixel_width = 1;
23521 glyph->padding_p = 1;
23522 }
23523 glyph->ascent = it->ascent;
23524 glyph->descent = it->descent;
23525 glyph->voffset = it->voffset;
23526 glyph->type = CHAR_GLYPH;
23527 glyph->avoid_cursor_p = it->avoid_cursor_p;
23528 glyph->multibyte_p = it->multibyte_p;
23529 glyph->left_box_line_p = it->start_of_box_run_p;
23530 glyph->right_box_line_p = it->end_of_box_run_p;
23531 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23532 || it->phys_descent > it->descent);
23533 glyph->glyph_not_available_p = it->glyph_not_available_p;
23534 glyph->face_id = it->face_id;
23535 glyph->u.ch = it->char_to_display;
23536 glyph->slice.img = null_glyph_slice;
23537 glyph->font_type = FONT_TYPE_UNKNOWN;
23538 if (it->bidi_p)
23539 {
23540 glyph->resolved_level = it->bidi_it.resolved_level;
23541 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23542 abort ();
23543 glyph->bidi_type = it->bidi_it.type;
23544 }
23545 else
23546 {
23547 glyph->resolved_level = 0;
23548 glyph->bidi_type = UNKNOWN_BT;
23549 }
23550 ++it->glyph_row->used[area];
23551 }
23552 else
23553 IT_EXPAND_MATRIX_WIDTH (it, area);
23554 }
23555
23556 /* Store one glyph for the composition IT->cmp_it.id in
23557 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23558 non-null. */
23559
23560 static inline void
23561 append_composite_glyph (struct it *it)
23562 {
23563 struct glyph *glyph;
23564 enum glyph_row_area area = it->area;
23565
23566 eassert (it->glyph_row);
23567
23568 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23569 if (glyph < it->glyph_row->glyphs[area + 1])
23570 {
23571 /* If the glyph row is reversed, we need to prepend the glyph
23572 rather than append it. */
23573 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23574 {
23575 struct glyph *g;
23576
23577 /* Make room for the new glyph. */
23578 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23579 g[1] = *g;
23580 glyph = it->glyph_row->glyphs[it->area];
23581 }
23582 glyph->charpos = it->cmp_it.charpos;
23583 glyph->object = it->object;
23584 glyph->pixel_width = it->pixel_width;
23585 glyph->ascent = it->ascent;
23586 glyph->descent = it->descent;
23587 glyph->voffset = it->voffset;
23588 glyph->type = COMPOSITE_GLYPH;
23589 if (it->cmp_it.ch < 0)
23590 {
23591 glyph->u.cmp.automatic = 0;
23592 glyph->u.cmp.id = it->cmp_it.id;
23593 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23594 }
23595 else
23596 {
23597 glyph->u.cmp.automatic = 1;
23598 glyph->u.cmp.id = it->cmp_it.id;
23599 glyph->slice.cmp.from = it->cmp_it.from;
23600 glyph->slice.cmp.to = it->cmp_it.to - 1;
23601 }
23602 glyph->avoid_cursor_p = it->avoid_cursor_p;
23603 glyph->multibyte_p = it->multibyte_p;
23604 glyph->left_box_line_p = it->start_of_box_run_p;
23605 glyph->right_box_line_p = it->end_of_box_run_p;
23606 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23607 || it->phys_descent > it->descent);
23608 glyph->padding_p = 0;
23609 glyph->glyph_not_available_p = 0;
23610 glyph->face_id = it->face_id;
23611 glyph->font_type = FONT_TYPE_UNKNOWN;
23612 if (it->bidi_p)
23613 {
23614 glyph->resolved_level = it->bidi_it.resolved_level;
23615 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23616 abort ();
23617 glyph->bidi_type = it->bidi_it.type;
23618 }
23619 ++it->glyph_row->used[area];
23620 }
23621 else
23622 IT_EXPAND_MATRIX_WIDTH (it, area);
23623 }
23624
23625
23626 /* Change IT->ascent and IT->height according to the setting of
23627 IT->voffset. */
23628
23629 static inline void
23630 take_vertical_position_into_account (struct it *it)
23631 {
23632 if (it->voffset)
23633 {
23634 if (it->voffset < 0)
23635 /* Increase the ascent so that we can display the text higher
23636 in the line. */
23637 it->ascent -= it->voffset;
23638 else
23639 /* Increase the descent so that we can display the text lower
23640 in the line. */
23641 it->descent += it->voffset;
23642 }
23643 }
23644
23645
23646 /* Produce glyphs/get display metrics for the image IT is loaded with.
23647 See the description of struct display_iterator in dispextern.h for
23648 an overview of struct display_iterator. */
23649
23650 static void
23651 produce_image_glyph (struct it *it)
23652 {
23653 struct image *img;
23654 struct face *face;
23655 int glyph_ascent, crop;
23656 struct glyph_slice slice;
23657
23658 eassert (it->what == IT_IMAGE);
23659
23660 face = FACE_FROM_ID (it->f, it->face_id);
23661 eassert (face);
23662 /* Make sure X resources of the face is loaded. */
23663 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23664
23665 if (it->image_id < 0)
23666 {
23667 /* Fringe bitmap. */
23668 it->ascent = it->phys_ascent = 0;
23669 it->descent = it->phys_descent = 0;
23670 it->pixel_width = 0;
23671 it->nglyphs = 0;
23672 return;
23673 }
23674
23675 img = IMAGE_FROM_ID (it->f, it->image_id);
23676 eassert (img);
23677 /* Make sure X resources of the image is loaded. */
23678 prepare_image_for_display (it->f, img);
23679
23680 slice.x = slice.y = 0;
23681 slice.width = img->width;
23682 slice.height = img->height;
23683
23684 if (INTEGERP (it->slice.x))
23685 slice.x = XINT (it->slice.x);
23686 else if (FLOATP (it->slice.x))
23687 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23688
23689 if (INTEGERP (it->slice.y))
23690 slice.y = XINT (it->slice.y);
23691 else if (FLOATP (it->slice.y))
23692 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23693
23694 if (INTEGERP (it->slice.width))
23695 slice.width = XINT (it->slice.width);
23696 else if (FLOATP (it->slice.width))
23697 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23698
23699 if (INTEGERP (it->slice.height))
23700 slice.height = XINT (it->slice.height);
23701 else if (FLOATP (it->slice.height))
23702 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23703
23704 if (slice.x >= img->width)
23705 slice.x = img->width;
23706 if (slice.y >= img->height)
23707 slice.y = img->height;
23708 if (slice.x + slice.width >= img->width)
23709 slice.width = img->width - slice.x;
23710 if (slice.y + slice.height > img->height)
23711 slice.height = img->height - slice.y;
23712
23713 if (slice.width == 0 || slice.height == 0)
23714 return;
23715
23716 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23717
23718 it->descent = slice.height - glyph_ascent;
23719 if (slice.y == 0)
23720 it->descent += img->vmargin;
23721 if (slice.y + slice.height == img->height)
23722 it->descent += img->vmargin;
23723 it->phys_descent = it->descent;
23724
23725 it->pixel_width = slice.width;
23726 if (slice.x == 0)
23727 it->pixel_width += img->hmargin;
23728 if (slice.x + slice.width == img->width)
23729 it->pixel_width += img->hmargin;
23730
23731 /* It's quite possible for images to have an ascent greater than
23732 their height, so don't get confused in that case. */
23733 if (it->descent < 0)
23734 it->descent = 0;
23735
23736 it->nglyphs = 1;
23737
23738 if (face->box != FACE_NO_BOX)
23739 {
23740 if (face->box_line_width > 0)
23741 {
23742 if (slice.y == 0)
23743 it->ascent += face->box_line_width;
23744 if (slice.y + slice.height == img->height)
23745 it->descent += face->box_line_width;
23746 }
23747
23748 if (it->start_of_box_run_p && slice.x == 0)
23749 it->pixel_width += eabs (face->box_line_width);
23750 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23751 it->pixel_width += eabs (face->box_line_width);
23752 }
23753
23754 take_vertical_position_into_account (it);
23755
23756 /* Automatically crop wide image glyphs at right edge so we can
23757 draw the cursor on same display row. */
23758 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23759 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23760 {
23761 it->pixel_width -= crop;
23762 slice.width -= crop;
23763 }
23764
23765 if (it->glyph_row)
23766 {
23767 struct glyph *glyph;
23768 enum glyph_row_area area = it->area;
23769
23770 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23771 if (glyph < it->glyph_row->glyphs[area + 1])
23772 {
23773 glyph->charpos = CHARPOS (it->position);
23774 glyph->object = it->object;
23775 glyph->pixel_width = it->pixel_width;
23776 glyph->ascent = glyph_ascent;
23777 glyph->descent = it->descent;
23778 glyph->voffset = it->voffset;
23779 glyph->type = IMAGE_GLYPH;
23780 glyph->avoid_cursor_p = it->avoid_cursor_p;
23781 glyph->multibyte_p = it->multibyte_p;
23782 glyph->left_box_line_p = it->start_of_box_run_p;
23783 glyph->right_box_line_p = it->end_of_box_run_p;
23784 glyph->overlaps_vertically_p = 0;
23785 glyph->padding_p = 0;
23786 glyph->glyph_not_available_p = 0;
23787 glyph->face_id = it->face_id;
23788 glyph->u.img_id = img->id;
23789 glyph->slice.img = slice;
23790 glyph->font_type = FONT_TYPE_UNKNOWN;
23791 if (it->bidi_p)
23792 {
23793 glyph->resolved_level = it->bidi_it.resolved_level;
23794 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23795 abort ();
23796 glyph->bidi_type = it->bidi_it.type;
23797 }
23798 ++it->glyph_row->used[area];
23799 }
23800 else
23801 IT_EXPAND_MATRIX_WIDTH (it, area);
23802 }
23803 }
23804
23805
23806 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23807 of the glyph, WIDTH and HEIGHT are the width and height of the
23808 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23809
23810 static void
23811 append_stretch_glyph (struct it *it, Lisp_Object object,
23812 int width, int height, int ascent)
23813 {
23814 struct glyph *glyph;
23815 enum glyph_row_area area = it->area;
23816
23817 eassert (ascent >= 0 && ascent <= height);
23818
23819 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23820 if (glyph < it->glyph_row->glyphs[area + 1])
23821 {
23822 /* If the glyph row is reversed, we need to prepend the glyph
23823 rather than append it. */
23824 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23825 {
23826 struct glyph *g;
23827
23828 /* Make room for the additional glyph. */
23829 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23830 g[1] = *g;
23831 glyph = it->glyph_row->glyphs[area];
23832 }
23833 glyph->charpos = CHARPOS (it->position);
23834 glyph->object = object;
23835 glyph->pixel_width = width;
23836 glyph->ascent = ascent;
23837 glyph->descent = height - ascent;
23838 glyph->voffset = it->voffset;
23839 glyph->type = STRETCH_GLYPH;
23840 glyph->avoid_cursor_p = it->avoid_cursor_p;
23841 glyph->multibyte_p = it->multibyte_p;
23842 glyph->left_box_line_p = it->start_of_box_run_p;
23843 glyph->right_box_line_p = it->end_of_box_run_p;
23844 glyph->overlaps_vertically_p = 0;
23845 glyph->padding_p = 0;
23846 glyph->glyph_not_available_p = 0;
23847 glyph->face_id = it->face_id;
23848 glyph->u.stretch.ascent = ascent;
23849 glyph->u.stretch.height = height;
23850 glyph->slice.img = null_glyph_slice;
23851 glyph->font_type = FONT_TYPE_UNKNOWN;
23852 if (it->bidi_p)
23853 {
23854 glyph->resolved_level = it->bidi_it.resolved_level;
23855 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23856 abort ();
23857 glyph->bidi_type = it->bidi_it.type;
23858 }
23859 else
23860 {
23861 glyph->resolved_level = 0;
23862 glyph->bidi_type = UNKNOWN_BT;
23863 }
23864 ++it->glyph_row->used[area];
23865 }
23866 else
23867 IT_EXPAND_MATRIX_WIDTH (it, area);
23868 }
23869
23870 #endif /* HAVE_WINDOW_SYSTEM */
23871
23872 /* Produce a stretch glyph for iterator IT. IT->object is the value
23873 of the glyph property displayed. The value must be a list
23874 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23875 being recognized:
23876
23877 1. `:width WIDTH' specifies that the space should be WIDTH *
23878 canonical char width wide. WIDTH may be an integer or floating
23879 point number.
23880
23881 2. `:relative-width FACTOR' specifies that the width of the stretch
23882 should be computed from the width of the first character having the
23883 `glyph' property, and should be FACTOR times that width.
23884
23885 3. `:align-to HPOS' specifies that the space should be wide enough
23886 to reach HPOS, a value in canonical character units.
23887
23888 Exactly one of the above pairs must be present.
23889
23890 4. `:height HEIGHT' specifies that the height of the stretch produced
23891 should be HEIGHT, measured in canonical character units.
23892
23893 5. `:relative-height FACTOR' specifies that the height of the
23894 stretch should be FACTOR times the height of the characters having
23895 the glyph property.
23896
23897 Either none or exactly one of 4 or 5 must be present.
23898
23899 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23900 of the stretch should be used for the ascent of the stretch.
23901 ASCENT must be in the range 0 <= ASCENT <= 100. */
23902
23903 void
23904 produce_stretch_glyph (struct it *it)
23905 {
23906 /* (space :width WIDTH :height HEIGHT ...) */
23907 Lisp_Object prop, plist;
23908 int width = 0, height = 0, align_to = -1;
23909 int zero_width_ok_p = 0;
23910 int ascent = 0;
23911 double tem;
23912 struct face *face = NULL;
23913 struct font *font = NULL;
23914
23915 #ifdef HAVE_WINDOW_SYSTEM
23916 int zero_height_ok_p = 0;
23917
23918 if (FRAME_WINDOW_P (it->f))
23919 {
23920 face = FACE_FROM_ID (it->f, it->face_id);
23921 font = face->font ? face->font : FRAME_FONT (it->f);
23922 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23923 }
23924 #endif
23925
23926 /* List should start with `space'. */
23927 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23928 plist = XCDR (it->object);
23929
23930 /* Compute the width of the stretch. */
23931 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23932 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23933 {
23934 /* Absolute width `:width WIDTH' specified and valid. */
23935 zero_width_ok_p = 1;
23936 width = (int)tem;
23937 }
23938 #ifdef HAVE_WINDOW_SYSTEM
23939 else if (FRAME_WINDOW_P (it->f)
23940 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23941 {
23942 /* Relative width `:relative-width FACTOR' specified and valid.
23943 Compute the width of the characters having the `glyph'
23944 property. */
23945 struct it it2;
23946 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23947
23948 it2 = *it;
23949 if (it->multibyte_p)
23950 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23951 else
23952 {
23953 it2.c = it2.char_to_display = *p, it2.len = 1;
23954 if (! ASCII_CHAR_P (it2.c))
23955 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23956 }
23957
23958 it2.glyph_row = NULL;
23959 it2.what = IT_CHARACTER;
23960 x_produce_glyphs (&it2);
23961 width = NUMVAL (prop) * it2.pixel_width;
23962 }
23963 #endif /* HAVE_WINDOW_SYSTEM */
23964 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23965 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23966 {
23967 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23968 align_to = (align_to < 0
23969 ? 0
23970 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23971 else if (align_to < 0)
23972 align_to = window_box_left_offset (it->w, TEXT_AREA);
23973 width = max (0, (int)tem + align_to - it->current_x);
23974 zero_width_ok_p = 1;
23975 }
23976 else
23977 /* Nothing specified -> width defaults to canonical char width. */
23978 width = FRAME_COLUMN_WIDTH (it->f);
23979
23980 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23981 width = 1;
23982
23983 #ifdef HAVE_WINDOW_SYSTEM
23984 /* Compute height. */
23985 if (FRAME_WINDOW_P (it->f))
23986 {
23987 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23988 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23989 {
23990 height = (int)tem;
23991 zero_height_ok_p = 1;
23992 }
23993 else if (prop = Fplist_get (plist, QCrelative_height),
23994 NUMVAL (prop) > 0)
23995 height = FONT_HEIGHT (font) * NUMVAL (prop);
23996 else
23997 height = FONT_HEIGHT (font);
23998
23999 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24000 height = 1;
24001
24002 /* Compute percentage of height used for ascent. If
24003 `:ascent ASCENT' is present and valid, use that. Otherwise,
24004 derive the ascent from the font in use. */
24005 if (prop = Fplist_get (plist, QCascent),
24006 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24007 ascent = height * NUMVAL (prop) / 100.0;
24008 else if (!NILP (prop)
24009 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24010 ascent = min (max (0, (int)tem), height);
24011 else
24012 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24013 }
24014 else
24015 #endif /* HAVE_WINDOW_SYSTEM */
24016 height = 1;
24017
24018 if (width > 0 && it->line_wrap != TRUNCATE
24019 && it->current_x + width > it->last_visible_x)
24020 {
24021 width = it->last_visible_x - it->current_x;
24022 #ifdef HAVE_WINDOW_SYSTEM
24023 /* Subtract one more pixel from the stretch width, but only on
24024 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24025 width -= FRAME_WINDOW_P (it->f);
24026 #endif
24027 }
24028
24029 if (width > 0 && height > 0 && it->glyph_row)
24030 {
24031 Lisp_Object o_object = it->object;
24032 Lisp_Object object = it->stack[it->sp - 1].string;
24033 int n = width;
24034
24035 if (!STRINGP (object))
24036 object = it->w->buffer;
24037 #ifdef HAVE_WINDOW_SYSTEM
24038 if (FRAME_WINDOW_P (it->f))
24039 append_stretch_glyph (it, object, width, height, ascent);
24040 else
24041 #endif
24042 {
24043 it->object = object;
24044 it->char_to_display = ' ';
24045 it->pixel_width = it->len = 1;
24046 while (n--)
24047 tty_append_glyph (it);
24048 it->object = o_object;
24049 }
24050 }
24051
24052 it->pixel_width = width;
24053 #ifdef HAVE_WINDOW_SYSTEM
24054 if (FRAME_WINDOW_P (it->f))
24055 {
24056 it->ascent = it->phys_ascent = ascent;
24057 it->descent = it->phys_descent = height - it->ascent;
24058 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24059 take_vertical_position_into_account (it);
24060 }
24061 else
24062 #endif
24063 it->nglyphs = width;
24064 }
24065
24066 #ifdef HAVE_WINDOW_SYSTEM
24067
24068 /* Calculate line-height and line-spacing properties.
24069 An integer value specifies explicit pixel value.
24070 A float value specifies relative value to current face height.
24071 A cons (float . face-name) specifies relative value to
24072 height of specified face font.
24073
24074 Returns height in pixels, or nil. */
24075
24076
24077 static Lisp_Object
24078 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24079 int boff, int override)
24080 {
24081 Lisp_Object face_name = Qnil;
24082 int ascent, descent, height;
24083
24084 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24085 return val;
24086
24087 if (CONSP (val))
24088 {
24089 face_name = XCAR (val);
24090 val = XCDR (val);
24091 if (!NUMBERP (val))
24092 val = make_number (1);
24093 if (NILP (face_name))
24094 {
24095 height = it->ascent + it->descent;
24096 goto scale;
24097 }
24098 }
24099
24100 if (NILP (face_name))
24101 {
24102 font = FRAME_FONT (it->f);
24103 boff = FRAME_BASELINE_OFFSET (it->f);
24104 }
24105 else if (EQ (face_name, Qt))
24106 {
24107 override = 0;
24108 }
24109 else
24110 {
24111 int face_id;
24112 struct face *face;
24113
24114 face_id = lookup_named_face (it->f, face_name, 0);
24115 if (face_id < 0)
24116 return make_number (-1);
24117
24118 face = FACE_FROM_ID (it->f, face_id);
24119 font = face->font;
24120 if (font == NULL)
24121 return make_number (-1);
24122 boff = font->baseline_offset;
24123 if (font->vertical_centering)
24124 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24125 }
24126
24127 ascent = FONT_BASE (font) + boff;
24128 descent = FONT_DESCENT (font) - boff;
24129
24130 if (override)
24131 {
24132 it->override_ascent = ascent;
24133 it->override_descent = descent;
24134 it->override_boff = boff;
24135 }
24136
24137 height = ascent + descent;
24138
24139 scale:
24140 if (FLOATP (val))
24141 height = (int)(XFLOAT_DATA (val) * height);
24142 else if (INTEGERP (val))
24143 height *= XINT (val);
24144
24145 return make_number (height);
24146 }
24147
24148
24149 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24150 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24151 and only if this is for a character for which no font was found.
24152
24153 If the display method (it->glyphless_method) is
24154 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24155 length of the acronym or the hexadecimal string, UPPER_XOFF and
24156 UPPER_YOFF are pixel offsets for the upper part of the string,
24157 LOWER_XOFF and LOWER_YOFF are for the lower part.
24158
24159 For the other display methods, LEN through LOWER_YOFF are zero. */
24160
24161 static void
24162 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24163 short upper_xoff, short upper_yoff,
24164 short lower_xoff, short lower_yoff)
24165 {
24166 struct glyph *glyph;
24167 enum glyph_row_area area = it->area;
24168
24169 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24170 if (glyph < it->glyph_row->glyphs[area + 1])
24171 {
24172 /* If the glyph row is reversed, we need to prepend the glyph
24173 rather than append it. */
24174 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24175 {
24176 struct glyph *g;
24177
24178 /* Make room for the additional glyph. */
24179 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24180 g[1] = *g;
24181 glyph = it->glyph_row->glyphs[area];
24182 }
24183 glyph->charpos = CHARPOS (it->position);
24184 glyph->object = it->object;
24185 glyph->pixel_width = it->pixel_width;
24186 glyph->ascent = it->ascent;
24187 glyph->descent = it->descent;
24188 glyph->voffset = it->voffset;
24189 glyph->type = GLYPHLESS_GLYPH;
24190 glyph->u.glyphless.method = it->glyphless_method;
24191 glyph->u.glyphless.for_no_font = for_no_font;
24192 glyph->u.glyphless.len = len;
24193 glyph->u.glyphless.ch = it->c;
24194 glyph->slice.glyphless.upper_xoff = upper_xoff;
24195 glyph->slice.glyphless.upper_yoff = upper_yoff;
24196 glyph->slice.glyphless.lower_xoff = lower_xoff;
24197 glyph->slice.glyphless.lower_yoff = lower_yoff;
24198 glyph->avoid_cursor_p = it->avoid_cursor_p;
24199 glyph->multibyte_p = it->multibyte_p;
24200 glyph->left_box_line_p = it->start_of_box_run_p;
24201 glyph->right_box_line_p = it->end_of_box_run_p;
24202 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24203 || it->phys_descent > it->descent);
24204 glyph->padding_p = 0;
24205 glyph->glyph_not_available_p = 0;
24206 glyph->face_id = face_id;
24207 glyph->font_type = FONT_TYPE_UNKNOWN;
24208 if (it->bidi_p)
24209 {
24210 glyph->resolved_level = it->bidi_it.resolved_level;
24211 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24212 abort ();
24213 glyph->bidi_type = it->bidi_it.type;
24214 }
24215 ++it->glyph_row->used[area];
24216 }
24217 else
24218 IT_EXPAND_MATRIX_WIDTH (it, area);
24219 }
24220
24221
24222 /* Produce a glyph for a glyphless character for iterator IT.
24223 IT->glyphless_method specifies which method to use for displaying
24224 the character. See the description of enum
24225 glyphless_display_method in dispextern.h for the detail.
24226
24227 FOR_NO_FONT is nonzero if and only if this is for a character for
24228 which no font was found. ACRONYM, if non-nil, is an acronym string
24229 for the character. */
24230
24231 static void
24232 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24233 {
24234 int face_id;
24235 struct face *face;
24236 struct font *font;
24237 int base_width, base_height, width, height;
24238 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24239 int len;
24240
24241 /* Get the metrics of the base font. We always refer to the current
24242 ASCII face. */
24243 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24244 font = face->font ? face->font : FRAME_FONT (it->f);
24245 it->ascent = FONT_BASE (font) + font->baseline_offset;
24246 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24247 base_height = it->ascent + it->descent;
24248 base_width = font->average_width;
24249
24250 /* Get a face ID for the glyph by utilizing a cache (the same way as
24251 done for `escape-glyph' in get_next_display_element). */
24252 if (it->f == last_glyphless_glyph_frame
24253 && it->face_id == last_glyphless_glyph_face_id)
24254 {
24255 face_id = last_glyphless_glyph_merged_face_id;
24256 }
24257 else
24258 {
24259 /* Merge the `glyphless-char' face into the current face. */
24260 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24261 last_glyphless_glyph_frame = it->f;
24262 last_glyphless_glyph_face_id = it->face_id;
24263 last_glyphless_glyph_merged_face_id = face_id;
24264 }
24265
24266 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24267 {
24268 it->pixel_width = THIN_SPACE_WIDTH;
24269 len = 0;
24270 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24271 }
24272 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24273 {
24274 width = CHAR_WIDTH (it->c);
24275 if (width == 0)
24276 width = 1;
24277 else if (width > 4)
24278 width = 4;
24279 it->pixel_width = base_width * width;
24280 len = 0;
24281 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24282 }
24283 else
24284 {
24285 char buf[7];
24286 const char *str;
24287 unsigned int code[6];
24288 int upper_len;
24289 int ascent, descent;
24290 struct font_metrics metrics_upper, metrics_lower;
24291
24292 face = FACE_FROM_ID (it->f, face_id);
24293 font = face->font ? face->font : FRAME_FONT (it->f);
24294 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24295
24296 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24297 {
24298 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24299 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24300 if (CONSP (acronym))
24301 acronym = XCAR (acronym);
24302 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24303 }
24304 else
24305 {
24306 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24307 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24308 str = buf;
24309 }
24310 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24311 code[len] = font->driver->encode_char (font, str[len]);
24312 upper_len = (len + 1) / 2;
24313 font->driver->text_extents (font, code, upper_len,
24314 &metrics_upper);
24315 font->driver->text_extents (font, code + upper_len, len - upper_len,
24316 &metrics_lower);
24317
24318
24319
24320 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24321 width = max (metrics_upper.width, metrics_lower.width) + 4;
24322 upper_xoff = upper_yoff = 2; /* the typical case */
24323 if (base_width >= width)
24324 {
24325 /* Align the upper to the left, the lower to the right. */
24326 it->pixel_width = base_width;
24327 lower_xoff = base_width - 2 - metrics_lower.width;
24328 }
24329 else
24330 {
24331 /* Center the shorter one. */
24332 it->pixel_width = width;
24333 if (metrics_upper.width >= metrics_lower.width)
24334 lower_xoff = (width - metrics_lower.width) / 2;
24335 else
24336 {
24337 /* FIXME: This code doesn't look right. It formerly was
24338 missing the "lower_xoff = 0;", which couldn't have
24339 been right since it left lower_xoff uninitialized. */
24340 lower_xoff = 0;
24341 upper_xoff = (width - metrics_upper.width) / 2;
24342 }
24343 }
24344
24345 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24346 top, bottom, and between upper and lower strings. */
24347 height = (metrics_upper.ascent + metrics_upper.descent
24348 + metrics_lower.ascent + metrics_lower.descent) + 5;
24349 /* Center vertically.
24350 H:base_height, D:base_descent
24351 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24352
24353 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24354 descent = D - H/2 + h/2;
24355 lower_yoff = descent - 2 - ld;
24356 upper_yoff = lower_yoff - la - 1 - ud; */
24357 ascent = - (it->descent - (base_height + height + 1) / 2);
24358 descent = it->descent - (base_height - height) / 2;
24359 lower_yoff = descent - 2 - metrics_lower.descent;
24360 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24361 - metrics_upper.descent);
24362 /* Don't make the height shorter than the base height. */
24363 if (height > base_height)
24364 {
24365 it->ascent = ascent;
24366 it->descent = descent;
24367 }
24368 }
24369
24370 it->phys_ascent = it->ascent;
24371 it->phys_descent = it->descent;
24372 if (it->glyph_row)
24373 append_glyphless_glyph (it, face_id, for_no_font, len,
24374 upper_xoff, upper_yoff,
24375 lower_xoff, lower_yoff);
24376 it->nglyphs = 1;
24377 take_vertical_position_into_account (it);
24378 }
24379
24380
24381 /* RIF:
24382 Produce glyphs/get display metrics for the display element IT is
24383 loaded with. See the description of struct it in dispextern.h
24384 for an overview of struct it. */
24385
24386 void
24387 x_produce_glyphs (struct it *it)
24388 {
24389 int extra_line_spacing = it->extra_line_spacing;
24390
24391 it->glyph_not_available_p = 0;
24392
24393 if (it->what == IT_CHARACTER)
24394 {
24395 XChar2b char2b;
24396 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24397 struct font *font = face->font;
24398 struct font_metrics *pcm = NULL;
24399 int boff; /* baseline offset */
24400
24401 if (font == NULL)
24402 {
24403 /* When no suitable font is found, display this character by
24404 the method specified in the first extra slot of
24405 Vglyphless_char_display. */
24406 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24407
24408 eassert (it->what == IT_GLYPHLESS);
24409 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24410 goto done;
24411 }
24412
24413 boff = font->baseline_offset;
24414 if (font->vertical_centering)
24415 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24416
24417 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24418 {
24419 int stretched_p;
24420
24421 it->nglyphs = 1;
24422
24423 if (it->override_ascent >= 0)
24424 {
24425 it->ascent = it->override_ascent;
24426 it->descent = it->override_descent;
24427 boff = it->override_boff;
24428 }
24429 else
24430 {
24431 it->ascent = FONT_BASE (font) + boff;
24432 it->descent = FONT_DESCENT (font) - boff;
24433 }
24434
24435 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24436 {
24437 pcm = get_per_char_metric (font, &char2b);
24438 if (pcm->width == 0
24439 && pcm->rbearing == 0 && pcm->lbearing == 0)
24440 pcm = NULL;
24441 }
24442
24443 if (pcm)
24444 {
24445 it->phys_ascent = pcm->ascent + boff;
24446 it->phys_descent = pcm->descent - boff;
24447 it->pixel_width = pcm->width;
24448 }
24449 else
24450 {
24451 it->glyph_not_available_p = 1;
24452 it->phys_ascent = it->ascent;
24453 it->phys_descent = it->descent;
24454 it->pixel_width = font->space_width;
24455 }
24456
24457 if (it->constrain_row_ascent_descent_p)
24458 {
24459 if (it->descent > it->max_descent)
24460 {
24461 it->ascent += it->descent - it->max_descent;
24462 it->descent = it->max_descent;
24463 }
24464 if (it->ascent > it->max_ascent)
24465 {
24466 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24467 it->ascent = it->max_ascent;
24468 }
24469 it->phys_ascent = min (it->phys_ascent, it->ascent);
24470 it->phys_descent = min (it->phys_descent, it->descent);
24471 extra_line_spacing = 0;
24472 }
24473
24474 /* If this is a space inside a region of text with
24475 `space-width' property, change its width. */
24476 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24477 if (stretched_p)
24478 it->pixel_width *= XFLOATINT (it->space_width);
24479
24480 /* If face has a box, add the box thickness to the character
24481 height. If character has a box line to the left and/or
24482 right, add the box line width to the character's width. */
24483 if (face->box != FACE_NO_BOX)
24484 {
24485 int thick = face->box_line_width;
24486
24487 if (thick > 0)
24488 {
24489 it->ascent += thick;
24490 it->descent += thick;
24491 }
24492 else
24493 thick = -thick;
24494
24495 if (it->start_of_box_run_p)
24496 it->pixel_width += thick;
24497 if (it->end_of_box_run_p)
24498 it->pixel_width += thick;
24499 }
24500
24501 /* If face has an overline, add the height of the overline
24502 (1 pixel) and a 1 pixel margin to the character height. */
24503 if (face->overline_p)
24504 it->ascent += overline_margin;
24505
24506 if (it->constrain_row_ascent_descent_p)
24507 {
24508 if (it->ascent > it->max_ascent)
24509 it->ascent = it->max_ascent;
24510 if (it->descent > it->max_descent)
24511 it->descent = it->max_descent;
24512 }
24513
24514 take_vertical_position_into_account (it);
24515
24516 /* If we have to actually produce glyphs, do it. */
24517 if (it->glyph_row)
24518 {
24519 if (stretched_p)
24520 {
24521 /* Translate a space with a `space-width' property
24522 into a stretch glyph. */
24523 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24524 / FONT_HEIGHT (font));
24525 append_stretch_glyph (it, it->object, it->pixel_width,
24526 it->ascent + it->descent, ascent);
24527 }
24528 else
24529 append_glyph (it);
24530
24531 /* If characters with lbearing or rbearing are displayed
24532 in this line, record that fact in a flag of the
24533 glyph row. This is used to optimize X output code. */
24534 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24535 it->glyph_row->contains_overlapping_glyphs_p = 1;
24536 }
24537 if (! stretched_p && it->pixel_width == 0)
24538 /* We assure that all visible glyphs have at least 1-pixel
24539 width. */
24540 it->pixel_width = 1;
24541 }
24542 else if (it->char_to_display == '\n')
24543 {
24544 /* A newline has no width, but we need the height of the
24545 line. But if previous part of the line sets a height,
24546 don't increase that height */
24547
24548 Lisp_Object height;
24549 Lisp_Object total_height = Qnil;
24550
24551 it->override_ascent = -1;
24552 it->pixel_width = 0;
24553 it->nglyphs = 0;
24554
24555 height = get_it_property (it, Qline_height);
24556 /* Split (line-height total-height) list */
24557 if (CONSP (height)
24558 && CONSP (XCDR (height))
24559 && NILP (XCDR (XCDR (height))))
24560 {
24561 total_height = XCAR (XCDR (height));
24562 height = XCAR (height);
24563 }
24564 height = calc_line_height_property (it, height, font, boff, 1);
24565
24566 if (it->override_ascent >= 0)
24567 {
24568 it->ascent = it->override_ascent;
24569 it->descent = it->override_descent;
24570 boff = it->override_boff;
24571 }
24572 else
24573 {
24574 it->ascent = FONT_BASE (font) + boff;
24575 it->descent = FONT_DESCENT (font) - boff;
24576 }
24577
24578 if (EQ (height, Qt))
24579 {
24580 if (it->descent > it->max_descent)
24581 {
24582 it->ascent += it->descent - it->max_descent;
24583 it->descent = it->max_descent;
24584 }
24585 if (it->ascent > it->max_ascent)
24586 {
24587 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24588 it->ascent = it->max_ascent;
24589 }
24590 it->phys_ascent = min (it->phys_ascent, it->ascent);
24591 it->phys_descent = min (it->phys_descent, it->descent);
24592 it->constrain_row_ascent_descent_p = 1;
24593 extra_line_spacing = 0;
24594 }
24595 else
24596 {
24597 Lisp_Object spacing;
24598
24599 it->phys_ascent = it->ascent;
24600 it->phys_descent = it->descent;
24601
24602 if ((it->max_ascent > 0 || it->max_descent > 0)
24603 && face->box != FACE_NO_BOX
24604 && face->box_line_width > 0)
24605 {
24606 it->ascent += face->box_line_width;
24607 it->descent += face->box_line_width;
24608 }
24609 if (!NILP (height)
24610 && XINT (height) > it->ascent + it->descent)
24611 it->ascent = XINT (height) - it->descent;
24612
24613 if (!NILP (total_height))
24614 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24615 else
24616 {
24617 spacing = get_it_property (it, Qline_spacing);
24618 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24619 }
24620 if (INTEGERP (spacing))
24621 {
24622 extra_line_spacing = XINT (spacing);
24623 if (!NILP (total_height))
24624 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24625 }
24626 }
24627 }
24628 else /* i.e. (it->char_to_display == '\t') */
24629 {
24630 if (font->space_width > 0)
24631 {
24632 int tab_width = it->tab_width * font->space_width;
24633 int x = it->current_x + it->continuation_lines_width;
24634 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24635
24636 /* If the distance from the current position to the next tab
24637 stop is less than a space character width, use the
24638 tab stop after that. */
24639 if (next_tab_x - x < font->space_width)
24640 next_tab_x += tab_width;
24641
24642 it->pixel_width = next_tab_x - x;
24643 it->nglyphs = 1;
24644 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24645 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24646
24647 if (it->glyph_row)
24648 {
24649 append_stretch_glyph (it, it->object, it->pixel_width,
24650 it->ascent + it->descent, it->ascent);
24651 }
24652 }
24653 else
24654 {
24655 it->pixel_width = 0;
24656 it->nglyphs = 1;
24657 }
24658 }
24659 }
24660 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24661 {
24662 /* A static composition.
24663
24664 Note: A composition is represented as one glyph in the
24665 glyph matrix. There are no padding glyphs.
24666
24667 Important note: pixel_width, ascent, and descent are the
24668 values of what is drawn by draw_glyphs (i.e. the values of
24669 the overall glyphs composed). */
24670 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24671 int boff; /* baseline offset */
24672 struct composition *cmp = composition_table[it->cmp_it.id];
24673 int glyph_len = cmp->glyph_len;
24674 struct font *font = face->font;
24675
24676 it->nglyphs = 1;
24677
24678 /* If we have not yet calculated pixel size data of glyphs of
24679 the composition for the current face font, calculate them
24680 now. Theoretically, we have to check all fonts for the
24681 glyphs, but that requires much time and memory space. So,
24682 here we check only the font of the first glyph. This may
24683 lead to incorrect display, but it's very rare, and C-l
24684 (recenter-top-bottom) can correct the display anyway. */
24685 if (! cmp->font || cmp->font != font)
24686 {
24687 /* Ascent and descent of the font of the first character
24688 of this composition (adjusted by baseline offset).
24689 Ascent and descent of overall glyphs should not be less
24690 than these, respectively. */
24691 int font_ascent, font_descent, font_height;
24692 /* Bounding box of the overall glyphs. */
24693 int leftmost, rightmost, lowest, highest;
24694 int lbearing, rbearing;
24695 int i, width, ascent, descent;
24696 int left_padded = 0, right_padded = 0;
24697 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24698 XChar2b char2b;
24699 struct font_metrics *pcm;
24700 int font_not_found_p;
24701 ptrdiff_t pos;
24702
24703 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24704 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24705 break;
24706 if (glyph_len < cmp->glyph_len)
24707 right_padded = 1;
24708 for (i = 0; i < glyph_len; i++)
24709 {
24710 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24711 break;
24712 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24713 }
24714 if (i > 0)
24715 left_padded = 1;
24716
24717 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24718 : IT_CHARPOS (*it));
24719 /* If no suitable font is found, use the default font. */
24720 font_not_found_p = font == NULL;
24721 if (font_not_found_p)
24722 {
24723 face = face->ascii_face;
24724 font = face->font;
24725 }
24726 boff = font->baseline_offset;
24727 if (font->vertical_centering)
24728 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24729 font_ascent = FONT_BASE (font) + boff;
24730 font_descent = FONT_DESCENT (font) - boff;
24731 font_height = FONT_HEIGHT (font);
24732
24733 cmp->font = (void *) font;
24734
24735 pcm = NULL;
24736 if (! font_not_found_p)
24737 {
24738 get_char_face_and_encoding (it->f, c, it->face_id,
24739 &char2b, 0);
24740 pcm = get_per_char_metric (font, &char2b);
24741 }
24742
24743 /* Initialize the bounding box. */
24744 if (pcm)
24745 {
24746 width = cmp->glyph_len > 0 ? pcm->width : 0;
24747 ascent = pcm->ascent;
24748 descent = pcm->descent;
24749 lbearing = pcm->lbearing;
24750 rbearing = pcm->rbearing;
24751 }
24752 else
24753 {
24754 width = cmp->glyph_len > 0 ? font->space_width : 0;
24755 ascent = FONT_BASE (font);
24756 descent = FONT_DESCENT (font);
24757 lbearing = 0;
24758 rbearing = width;
24759 }
24760
24761 rightmost = width;
24762 leftmost = 0;
24763 lowest = - descent + boff;
24764 highest = ascent + boff;
24765
24766 if (! font_not_found_p
24767 && font->default_ascent
24768 && CHAR_TABLE_P (Vuse_default_ascent)
24769 && !NILP (Faref (Vuse_default_ascent,
24770 make_number (it->char_to_display))))
24771 highest = font->default_ascent + boff;
24772
24773 /* Draw the first glyph at the normal position. It may be
24774 shifted to right later if some other glyphs are drawn
24775 at the left. */
24776 cmp->offsets[i * 2] = 0;
24777 cmp->offsets[i * 2 + 1] = boff;
24778 cmp->lbearing = lbearing;
24779 cmp->rbearing = rbearing;
24780
24781 /* Set cmp->offsets for the remaining glyphs. */
24782 for (i++; i < glyph_len; i++)
24783 {
24784 int left, right, btm, top;
24785 int ch = COMPOSITION_GLYPH (cmp, i);
24786 int face_id;
24787 struct face *this_face;
24788
24789 if (ch == '\t')
24790 ch = ' ';
24791 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24792 this_face = FACE_FROM_ID (it->f, face_id);
24793 font = this_face->font;
24794
24795 if (font == NULL)
24796 pcm = NULL;
24797 else
24798 {
24799 get_char_face_and_encoding (it->f, ch, face_id,
24800 &char2b, 0);
24801 pcm = get_per_char_metric (font, &char2b);
24802 }
24803 if (! pcm)
24804 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24805 else
24806 {
24807 width = pcm->width;
24808 ascent = pcm->ascent;
24809 descent = pcm->descent;
24810 lbearing = pcm->lbearing;
24811 rbearing = pcm->rbearing;
24812 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24813 {
24814 /* Relative composition with or without
24815 alternate chars. */
24816 left = (leftmost + rightmost - width) / 2;
24817 btm = - descent + boff;
24818 if (font->relative_compose
24819 && (! CHAR_TABLE_P (Vignore_relative_composition)
24820 || NILP (Faref (Vignore_relative_composition,
24821 make_number (ch)))))
24822 {
24823
24824 if (- descent >= font->relative_compose)
24825 /* One extra pixel between two glyphs. */
24826 btm = highest + 1;
24827 else if (ascent <= 0)
24828 /* One extra pixel between two glyphs. */
24829 btm = lowest - 1 - ascent - descent;
24830 }
24831 }
24832 else
24833 {
24834 /* A composition rule is specified by an integer
24835 value that encodes global and new reference
24836 points (GREF and NREF). GREF and NREF are
24837 specified by numbers as below:
24838
24839 0---1---2 -- ascent
24840 | |
24841 | |
24842 | |
24843 9--10--11 -- center
24844 | |
24845 ---3---4---5--- baseline
24846 | |
24847 6---7---8 -- descent
24848 */
24849 int rule = COMPOSITION_RULE (cmp, i);
24850 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24851
24852 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24853 grefx = gref % 3, nrefx = nref % 3;
24854 grefy = gref / 3, nrefy = nref / 3;
24855 if (xoff)
24856 xoff = font_height * (xoff - 128) / 256;
24857 if (yoff)
24858 yoff = font_height * (yoff - 128) / 256;
24859
24860 left = (leftmost
24861 + grefx * (rightmost - leftmost) / 2
24862 - nrefx * width / 2
24863 + xoff);
24864
24865 btm = ((grefy == 0 ? highest
24866 : grefy == 1 ? 0
24867 : grefy == 2 ? lowest
24868 : (highest + lowest) / 2)
24869 - (nrefy == 0 ? ascent + descent
24870 : nrefy == 1 ? descent - boff
24871 : nrefy == 2 ? 0
24872 : (ascent + descent) / 2)
24873 + yoff);
24874 }
24875
24876 cmp->offsets[i * 2] = left;
24877 cmp->offsets[i * 2 + 1] = btm + descent;
24878
24879 /* Update the bounding box of the overall glyphs. */
24880 if (width > 0)
24881 {
24882 right = left + width;
24883 if (left < leftmost)
24884 leftmost = left;
24885 if (right > rightmost)
24886 rightmost = right;
24887 }
24888 top = btm + descent + ascent;
24889 if (top > highest)
24890 highest = top;
24891 if (btm < lowest)
24892 lowest = btm;
24893
24894 if (cmp->lbearing > left + lbearing)
24895 cmp->lbearing = left + lbearing;
24896 if (cmp->rbearing < left + rbearing)
24897 cmp->rbearing = left + rbearing;
24898 }
24899 }
24900
24901 /* If there are glyphs whose x-offsets are negative,
24902 shift all glyphs to the right and make all x-offsets
24903 non-negative. */
24904 if (leftmost < 0)
24905 {
24906 for (i = 0; i < cmp->glyph_len; i++)
24907 cmp->offsets[i * 2] -= leftmost;
24908 rightmost -= leftmost;
24909 cmp->lbearing -= leftmost;
24910 cmp->rbearing -= leftmost;
24911 }
24912
24913 if (left_padded && cmp->lbearing < 0)
24914 {
24915 for (i = 0; i < cmp->glyph_len; i++)
24916 cmp->offsets[i * 2] -= cmp->lbearing;
24917 rightmost -= cmp->lbearing;
24918 cmp->rbearing -= cmp->lbearing;
24919 cmp->lbearing = 0;
24920 }
24921 if (right_padded && rightmost < cmp->rbearing)
24922 {
24923 rightmost = cmp->rbearing;
24924 }
24925
24926 cmp->pixel_width = rightmost;
24927 cmp->ascent = highest;
24928 cmp->descent = - lowest;
24929 if (cmp->ascent < font_ascent)
24930 cmp->ascent = font_ascent;
24931 if (cmp->descent < font_descent)
24932 cmp->descent = font_descent;
24933 }
24934
24935 if (it->glyph_row
24936 && (cmp->lbearing < 0
24937 || cmp->rbearing > cmp->pixel_width))
24938 it->glyph_row->contains_overlapping_glyphs_p = 1;
24939
24940 it->pixel_width = cmp->pixel_width;
24941 it->ascent = it->phys_ascent = cmp->ascent;
24942 it->descent = it->phys_descent = cmp->descent;
24943 if (face->box != FACE_NO_BOX)
24944 {
24945 int thick = face->box_line_width;
24946
24947 if (thick > 0)
24948 {
24949 it->ascent += thick;
24950 it->descent += thick;
24951 }
24952 else
24953 thick = - thick;
24954
24955 if (it->start_of_box_run_p)
24956 it->pixel_width += thick;
24957 if (it->end_of_box_run_p)
24958 it->pixel_width += thick;
24959 }
24960
24961 /* If face has an overline, add the height of the overline
24962 (1 pixel) and a 1 pixel margin to the character height. */
24963 if (face->overline_p)
24964 it->ascent += overline_margin;
24965
24966 take_vertical_position_into_account (it);
24967 if (it->ascent < 0)
24968 it->ascent = 0;
24969 if (it->descent < 0)
24970 it->descent = 0;
24971
24972 if (it->glyph_row && cmp->glyph_len > 0)
24973 append_composite_glyph (it);
24974 }
24975 else if (it->what == IT_COMPOSITION)
24976 {
24977 /* A dynamic (automatic) composition. */
24978 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24979 Lisp_Object gstring;
24980 struct font_metrics metrics;
24981
24982 it->nglyphs = 1;
24983
24984 gstring = composition_gstring_from_id (it->cmp_it.id);
24985 it->pixel_width
24986 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24987 &metrics);
24988 if (it->glyph_row
24989 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24990 it->glyph_row->contains_overlapping_glyphs_p = 1;
24991 it->ascent = it->phys_ascent = metrics.ascent;
24992 it->descent = it->phys_descent = metrics.descent;
24993 if (face->box != FACE_NO_BOX)
24994 {
24995 int thick = face->box_line_width;
24996
24997 if (thick > 0)
24998 {
24999 it->ascent += thick;
25000 it->descent += thick;
25001 }
25002 else
25003 thick = - thick;
25004
25005 if (it->start_of_box_run_p)
25006 it->pixel_width += thick;
25007 if (it->end_of_box_run_p)
25008 it->pixel_width += thick;
25009 }
25010 /* If face has an overline, add the height of the overline
25011 (1 pixel) and a 1 pixel margin to the character height. */
25012 if (face->overline_p)
25013 it->ascent += overline_margin;
25014 take_vertical_position_into_account (it);
25015 if (it->ascent < 0)
25016 it->ascent = 0;
25017 if (it->descent < 0)
25018 it->descent = 0;
25019
25020 if (it->glyph_row)
25021 append_composite_glyph (it);
25022 }
25023 else if (it->what == IT_GLYPHLESS)
25024 produce_glyphless_glyph (it, 0, Qnil);
25025 else if (it->what == IT_IMAGE)
25026 produce_image_glyph (it);
25027 else if (it->what == IT_STRETCH)
25028 produce_stretch_glyph (it);
25029
25030 done:
25031 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25032 because this isn't true for images with `:ascent 100'. */
25033 eassert (it->ascent >= 0 && it->descent >= 0);
25034 if (it->area == TEXT_AREA)
25035 it->current_x += it->pixel_width;
25036
25037 if (extra_line_spacing > 0)
25038 {
25039 it->descent += extra_line_spacing;
25040 if (extra_line_spacing > it->max_extra_line_spacing)
25041 it->max_extra_line_spacing = extra_line_spacing;
25042 }
25043
25044 it->max_ascent = max (it->max_ascent, it->ascent);
25045 it->max_descent = max (it->max_descent, it->descent);
25046 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25047 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25048 }
25049
25050 /* EXPORT for RIF:
25051 Output LEN glyphs starting at START at the nominal cursor position.
25052 Advance the nominal cursor over the text. The global variable
25053 updated_window contains the window being updated, updated_row is
25054 the glyph row being updated, and updated_area is the area of that
25055 row being updated. */
25056
25057 void
25058 x_write_glyphs (struct glyph *start, int len)
25059 {
25060 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25061
25062 eassert (updated_window && updated_row);
25063 /* When the window is hscrolled, cursor hpos can legitimately be out
25064 of bounds, but we draw the cursor at the corresponding window
25065 margin in that case. */
25066 if (!updated_row->reversed_p && chpos < 0)
25067 chpos = 0;
25068 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25069 chpos = updated_row->used[TEXT_AREA] - 1;
25070
25071 BLOCK_INPUT;
25072
25073 /* Write glyphs. */
25074
25075 hpos = start - updated_row->glyphs[updated_area];
25076 x = draw_glyphs (updated_window, output_cursor.x,
25077 updated_row, updated_area,
25078 hpos, hpos + len,
25079 DRAW_NORMAL_TEXT, 0);
25080
25081 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25082 if (updated_area == TEXT_AREA
25083 && updated_window->phys_cursor_on_p
25084 && updated_window->phys_cursor.vpos == output_cursor.vpos
25085 && chpos >= hpos
25086 && chpos < hpos + len)
25087 updated_window->phys_cursor_on_p = 0;
25088
25089 UNBLOCK_INPUT;
25090
25091 /* Advance the output cursor. */
25092 output_cursor.hpos += len;
25093 output_cursor.x = x;
25094 }
25095
25096
25097 /* EXPORT for RIF:
25098 Insert LEN glyphs from START at the nominal cursor position. */
25099
25100 void
25101 x_insert_glyphs (struct glyph *start, int len)
25102 {
25103 struct frame *f;
25104 struct window *w;
25105 int line_height, shift_by_width, shifted_region_width;
25106 struct glyph_row *row;
25107 struct glyph *glyph;
25108 int frame_x, frame_y;
25109 ptrdiff_t hpos;
25110
25111 eassert (updated_window && updated_row);
25112 BLOCK_INPUT;
25113 w = updated_window;
25114 f = XFRAME (WINDOW_FRAME (w));
25115
25116 /* Get the height of the line we are in. */
25117 row = updated_row;
25118 line_height = row->height;
25119
25120 /* Get the width of the glyphs to insert. */
25121 shift_by_width = 0;
25122 for (glyph = start; glyph < start + len; ++glyph)
25123 shift_by_width += glyph->pixel_width;
25124
25125 /* Get the width of the region to shift right. */
25126 shifted_region_width = (window_box_width (w, updated_area)
25127 - output_cursor.x
25128 - shift_by_width);
25129
25130 /* Shift right. */
25131 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25132 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25133
25134 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25135 line_height, shift_by_width);
25136
25137 /* Write the glyphs. */
25138 hpos = start - row->glyphs[updated_area];
25139 draw_glyphs (w, output_cursor.x, row, updated_area,
25140 hpos, hpos + len,
25141 DRAW_NORMAL_TEXT, 0);
25142
25143 /* Advance the output cursor. */
25144 output_cursor.hpos += len;
25145 output_cursor.x += shift_by_width;
25146 UNBLOCK_INPUT;
25147 }
25148
25149
25150 /* EXPORT for RIF:
25151 Erase the current text line from the nominal cursor position
25152 (inclusive) to pixel column TO_X (exclusive). The idea is that
25153 everything from TO_X onward is already erased.
25154
25155 TO_X is a pixel position relative to updated_area of
25156 updated_window. TO_X == -1 means clear to the end of this area. */
25157
25158 void
25159 x_clear_end_of_line (int to_x)
25160 {
25161 struct frame *f;
25162 struct window *w = updated_window;
25163 int max_x, min_y, max_y;
25164 int from_x, from_y, to_y;
25165
25166 eassert (updated_window && updated_row);
25167 f = XFRAME (w->frame);
25168
25169 if (updated_row->full_width_p)
25170 max_x = WINDOW_TOTAL_WIDTH (w);
25171 else
25172 max_x = window_box_width (w, updated_area);
25173 max_y = window_text_bottom_y (w);
25174
25175 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25176 of window. For TO_X > 0, truncate to end of drawing area. */
25177 if (to_x == 0)
25178 return;
25179 else if (to_x < 0)
25180 to_x = max_x;
25181 else
25182 to_x = min (to_x, max_x);
25183
25184 to_y = min (max_y, output_cursor.y + updated_row->height);
25185
25186 /* Notice if the cursor will be cleared by this operation. */
25187 if (!updated_row->full_width_p)
25188 notice_overwritten_cursor (w, updated_area,
25189 output_cursor.x, -1,
25190 updated_row->y,
25191 MATRIX_ROW_BOTTOM_Y (updated_row));
25192
25193 from_x = output_cursor.x;
25194
25195 /* Translate to frame coordinates. */
25196 if (updated_row->full_width_p)
25197 {
25198 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25199 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25200 }
25201 else
25202 {
25203 int area_left = window_box_left (w, updated_area);
25204 from_x += area_left;
25205 to_x += area_left;
25206 }
25207
25208 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25209 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25210 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25211
25212 /* Prevent inadvertently clearing to end of the X window. */
25213 if (to_x > from_x && to_y > from_y)
25214 {
25215 BLOCK_INPUT;
25216 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25217 to_x - from_x, to_y - from_y);
25218 UNBLOCK_INPUT;
25219 }
25220 }
25221
25222 #endif /* HAVE_WINDOW_SYSTEM */
25223
25224
25225 \f
25226 /***********************************************************************
25227 Cursor types
25228 ***********************************************************************/
25229
25230 /* Value is the internal representation of the specified cursor type
25231 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25232 of the bar cursor. */
25233
25234 static enum text_cursor_kinds
25235 get_specified_cursor_type (Lisp_Object arg, int *width)
25236 {
25237 enum text_cursor_kinds type;
25238
25239 if (NILP (arg))
25240 return NO_CURSOR;
25241
25242 if (EQ (arg, Qbox))
25243 return FILLED_BOX_CURSOR;
25244
25245 if (EQ (arg, Qhollow))
25246 return HOLLOW_BOX_CURSOR;
25247
25248 if (EQ (arg, Qbar))
25249 {
25250 *width = 2;
25251 return BAR_CURSOR;
25252 }
25253
25254 if (CONSP (arg)
25255 && EQ (XCAR (arg), Qbar)
25256 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25257 {
25258 *width = XINT (XCDR (arg));
25259 return BAR_CURSOR;
25260 }
25261
25262 if (EQ (arg, Qhbar))
25263 {
25264 *width = 2;
25265 return HBAR_CURSOR;
25266 }
25267
25268 if (CONSP (arg)
25269 && EQ (XCAR (arg), Qhbar)
25270 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25271 {
25272 *width = XINT (XCDR (arg));
25273 return HBAR_CURSOR;
25274 }
25275
25276 /* Treat anything unknown as "hollow box cursor".
25277 It was bad to signal an error; people have trouble fixing
25278 .Xdefaults with Emacs, when it has something bad in it. */
25279 type = HOLLOW_BOX_CURSOR;
25280
25281 return type;
25282 }
25283
25284 /* Set the default cursor types for specified frame. */
25285 void
25286 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25287 {
25288 int width = 1;
25289 Lisp_Object tem;
25290
25291 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25292 FRAME_CURSOR_WIDTH (f) = width;
25293
25294 /* By default, set up the blink-off state depending on the on-state. */
25295
25296 tem = Fassoc (arg, Vblink_cursor_alist);
25297 if (!NILP (tem))
25298 {
25299 FRAME_BLINK_OFF_CURSOR (f)
25300 = get_specified_cursor_type (XCDR (tem), &width);
25301 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25302 }
25303 else
25304 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25305 }
25306
25307
25308 #ifdef HAVE_WINDOW_SYSTEM
25309
25310 /* Return the cursor we want to be displayed in window W. Return
25311 width of bar/hbar cursor through WIDTH arg. Return with
25312 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25313 (i.e. if the `system caret' should track this cursor).
25314
25315 In a mini-buffer window, we want the cursor only to appear if we
25316 are reading input from this window. For the selected window, we
25317 want the cursor type given by the frame parameter or buffer local
25318 setting of cursor-type. If explicitly marked off, draw no cursor.
25319 In all other cases, we want a hollow box cursor. */
25320
25321 static enum text_cursor_kinds
25322 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25323 int *active_cursor)
25324 {
25325 struct frame *f = XFRAME (w->frame);
25326 struct buffer *b = XBUFFER (w->buffer);
25327 int cursor_type = DEFAULT_CURSOR;
25328 Lisp_Object alt_cursor;
25329 int non_selected = 0;
25330
25331 *active_cursor = 1;
25332
25333 /* Echo area */
25334 if (cursor_in_echo_area
25335 && FRAME_HAS_MINIBUF_P (f)
25336 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25337 {
25338 if (w == XWINDOW (echo_area_window))
25339 {
25340 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25341 {
25342 *width = FRAME_CURSOR_WIDTH (f);
25343 return FRAME_DESIRED_CURSOR (f);
25344 }
25345 else
25346 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25347 }
25348
25349 *active_cursor = 0;
25350 non_selected = 1;
25351 }
25352
25353 /* Detect a nonselected window or nonselected frame. */
25354 else if (w != XWINDOW (f->selected_window)
25355 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25356 {
25357 *active_cursor = 0;
25358
25359 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25360 return NO_CURSOR;
25361
25362 non_selected = 1;
25363 }
25364
25365 /* Never display a cursor in a window in which cursor-type is nil. */
25366 if (NILP (BVAR (b, cursor_type)))
25367 return NO_CURSOR;
25368
25369 /* Get the normal cursor type for this window. */
25370 if (EQ (BVAR (b, cursor_type), Qt))
25371 {
25372 cursor_type = FRAME_DESIRED_CURSOR (f);
25373 *width = FRAME_CURSOR_WIDTH (f);
25374 }
25375 else
25376 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25377
25378 /* Use cursor-in-non-selected-windows instead
25379 for non-selected window or frame. */
25380 if (non_selected)
25381 {
25382 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25383 if (!EQ (Qt, alt_cursor))
25384 return get_specified_cursor_type (alt_cursor, width);
25385 /* t means modify the normal cursor type. */
25386 if (cursor_type == FILLED_BOX_CURSOR)
25387 cursor_type = HOLLOW_BOX_CURSOR;
25388 else if (cursor_type == BAR_CURSOR && *width > 1)
25389 --*width;
25390 return cursor_type;
25391 }
25392
25393 /* Use normal cursor if not blinked off. */
25394 if (!w->cursor_off_p)
25395 {
25396 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25397 {
25398 if (cursor_type == FILLED_BOX_CURSOR)
25399 {
25400 /* Using a block cursor on large images can be very annoying.
25401 So use a hollow cursor for "large" images.
25402 If image is not transparent (no mask), also use hollow cursor. */
25403 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25404 if (img != NULL && IMAGEP (img->spec))
25405 {
25406 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25407 where N = size of default frame font size.
25408 This should cover most of the "tiny" icons people may use. */
25409 if (!img->mask
25410 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25411 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25412 cursor_type = HOLLOW_BOX_CURSOR;
25413 }
25414 }
25415 else if (cursor_type != NO_CURSOR)
25416 {
25417 /* Display current only supports BOX and HOLLOW cursors for images.
25418 So for now, unconditionally use a HOLLOW cursor when cursor is
25419 not a solid box cursor. */
25420 cursor_type = HOLLOW_BOX_CURSOR;
25421 }
25422 }
25423 return cursor_type;
25424 }
25425
25426 /* Cursor is blinked off, so determine how to "toggle" it. */
25427
25428 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25429 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25430 return get_specified_cursor_type (XCDR (alt_cursor), width);
25431
25432 /* Then see if frame has specified a specific blink off cursor type. */
25433 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25434 {
25435 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25436 return FRAME_BLINK_OFF_CURSOR (f);
25437 }
25438
25439 #if 0
25440 /* Some people liked having a permanently visible blinking cursor,
25441 while others had very strong opinions against it. So it was
25442 decided to remove it. KFS 2003-09-03 */
25443
25444 /* Finally perform built-in cursor blinking:
25445 filled box <-> hollow box
25446 wide [h]bar <-> narrow [h]bar
25447 narrow [h]bar <-> no cursor
25448 other type <-> no cursor */
25449
25450 if (cursor_type == FILLED_BOX_CURSOR)
25451 return HOLLOW_BOX_CURSOR;
25452
25453 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25454 {
25455 *width = 1;
25456 return cursor_type;
25457 }
25458 #endif
25459
25460 return NO_CURSOR;
25461 }
25462
25463
25464 /* Notice when the text cursor of window W has been completely
25465 overwritten by a drawing operation that outputs glyphs in AREA
25466 starting at X0 and ending at X1 in the line starting at Y0 and
25467 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25468 the rest of the line after X0 has been written. Y coordinates
25469 are window-relative. */
25470
25471 static void
25472 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25473 int x0, int x1, int y0, int y1)
25474 {
25475 int cx0, cx1, cy0, cy1;
25476 struct glyph_row *row;
25477
25478 if (!w->phys_cursor_on_p)
25479 return;
25480 if (area != TEXT_AREA)
25481 return;
25482
25483 if (w->phys_cursor.vpos < 0
25484 || w->phys_cursor.vpos >= w->current_matrix->nrows
25485 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25486 !(row->enabled_p && row->displays_text_p)))
25487 return;
25488
25489 if (row->cursor_in_fringe_p)
25490 {
25491 row->cursor_in_fringe_p = 0;
25492 draw_fringe_bitmap (w, row, row->reversed_p);
25493 w->phys_cursor_on_p = 0;
25494 return;
25495 }
25496
25497 cx0 = w->phys_cursor.x;
25498 cx1 = cx0 + w->phys_cursor_width;
25499 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25500 return;
25501
25502 /* The cursor image will be completely removed from the
25503 screen if the output area intersects the cursor area in
25504 y-direction. When we draw in [y0 y1[, and some part of
25505 the cursor is at y < y0, that part must have been drawn
25506 before. When scrolling, the cursor is erased before
25507 actually scrolling, so we don't come here. When not
25508 scrolling, the rows above the old cursor row must have
25509 changed, and in this case these rows must have written
25510 over the cursor image.
25511
25512 Likewise if part of the cursor is below y1, with the
25513 exception of the cursor being in the first blank row at
25514 the buffer and window end because update_text_area
25515 doesn't draw that row. (Except when it does, but
25516 that's handled in update_text_area.) */
25517
25518 cy0 = w->phys_cursor.y;
25519 cy1 = cy0 + w->phys_cursor_height;
25520 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25521 return;
25522
25523 w->phys_cursor_on_p = 0;
25524 }
25525
25526 #endif /* HAVE_WINDOW_SYSTEM */
25527
25528 \f
25529 /************************************************************************
25530 Mouse Face
25531 ************************************************************************/
25532
25533 #ifdef HAVE_WINDOW_SYSTEM
25534
25535 /* EXPORT for RIF:
25536 Fix the display of area AREA of overlapping row ROW in window W
25537 with respect to the overlapping part OVERLAPS. */
25538
25539 void
25540 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25541 enum glyph_row_area area, int overlaps)
25542 {
25543 int i, x;
25544
25545 BLOCK_INPUT;
25546
25547 x = 0;
25548 for (i = 0; i < row->used[area];)
25549 {
25550 if (row->glyphs[area][i].overlaps_vertically_p)
25551 {
25552 int start = i, start_x = x;
25553
25554 do
25555 {
25556 x += row->glyphs[area][i].pixel_width;
25557 ++i;
25558 }
25559 while (i < row->used[area]
25560 && row->glyphs[area][i].overlaps_vertically_p);
25561
25562 draw_glyphs (w, start_x, row, area,
25563 start, i,
25564 DRAW_NORMAL_TEXT, overlaps);
25565 }
25566 else
25567 {
25568 x += row->glyphs[area][i].pixel_width;
25569 ++i;
25570 }
25571 }
25572
25573 UNBLOCK_INPUT;
25574 }
25575
25576
25577 /* EXPORT:
25578 Draw the cursor glyph of window W in glyph row ROW. See the
25579 comment of draw_glyphs for the meaning of HL. */
25580
25581 void
25582 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25583 enum draw_glyphs_face hl)
25584 {
25585 /* If cursor hpos is out of bounds, don't draw garbage. This can
25586 happen in mini-buffer windows when switching between echo area
25587 glyphs and mini-buffer. */
25588 if ((row->reversed_p
25589 ? (w->phys_cursor.hpos >= 0)
25590 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25591 {
25592 int on_p = w->phys_cursor_on_p;
25593 int x1;
25594 int hpos = w->phys_cursor.hpos;
25595
25596 /* When the window is hscrolled, cursor hpos can legitimately be
25597 out of bounds, but we draw the cursor at the corresponding
25598 window margin in that case. */
25599 if (!row->reversed_p && hpos < 0)
25600 hpos = 0;
25601 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25602 hpos = row->used[TEXT_AREA] - 1;
25603
25604 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25605 hl, 0);
25606 w->phys_cursor_on_p = on_p;
25607
25608 if (hl == DRAW_CURSOR)
25609 w->phys_cursor_width = x1 - w->phys_cursor.x;
25610 /* When we erase the cursor, and ROW is overlapped by other
25611 rows, make sure that these overlapping parts of other rows
25612 are redrawn. */
25613 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25614 {
25615 w->phys_cursor_width = x1 - w->phys_cursor.x;
25616
25617 if (row > w->current_matrix->rows
25618 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25619 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25620 OVERLAPS_ERASED_CURSOR);
25621
25622 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25623 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25624 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25625 OVERLAPS_ERASED_CURSOR);
25626 }
25627 }
25628 }
25629
25630
25631 /* EXPORT:
25632 Erase the image of a cursor of window W from the screen. */
25633
25634 void
25635 erase_phys_cursor (struct window *w)
25636 {
25637 struct frame *f = XFRAME (w->frame);
25638 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25639 int hpos = w->phys_cursor.hpos;
25640 int vpos = w->phys_cursor.vpos;
25641 int mouse_face_here_p = 0;
25642 struct glyph_matrix *active_glyphs = w->current_matrix;
25643 struct glyph_row *cursor_row;
25644 struct glyph *cursor_glyph;
25645 enum draw_glyphs_face hl;
25646
25647 /* No cursor displayed or row invalidated => nothing to do on the
25648 screen. */
25649 if (w->phys_cursor_type == NO_CURSOR)
25650 goto mark_cursor_off;
25651
25652 /* VPOS >= active_glyphs->nrows means that window has been resized.
25653 Don't bother to erase the cursor. */
25654 if (vpos >= active_glyphs->nrows)
25655 goto mark_cursor_off;
25656
25657 /* If row containing cursor is marked invalid, there is nothing we
25658 can do. */
25659 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25660 if (!cursor_row->enabled_p)
25661 goto mark_cursor_off;
25662
25663 /* If line spacing is > 0, old cursor may only be partially visible in
25664 window after split-window. So adjust visible height. */
25665 cursor_row->visible_height = min (cursor_row->visible_height,
25666 window_text_bottom_y (w) - cursor_row->y);
25667
25668 /* If row is completely invisible, don't attempt to delete a cursor which
25669 isn't there. This can happen if cursor is at top of a window, and
25670 we switch to a buffer with a header line in that window. */
25671 if (cursor_row->visible_height <= 0)
25672 goto mark_cursor_off;
25673
25674 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25675 if (cursor_row->cursor_in_fringe_p)
25676 {
25677 cursor_row->cursor_in_fringe_p = 0;
25678 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25679 goto mark_cursor_off;
25680 }
25681
25682 /* This can happen when the new row is shorter than the old one.
25683 In this case, either draw_glyphs or clear_end_of_line
25684 should have cleared the cursor. Note that we wouldn't be
25685 able to erase the cursor in this case because we don't have a
25686 cursor glyph at hand. */
25687 if ((cursor_row->reversed_p
25688 ? (w->phys_cursor.hpos < 0)
25689 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25690 goto mark_cursor_off;
25691
25692 /* When the window is hscrolled, cursor hpos can legitimately be out
25693 of bounds, but we draw the cursor at the corresponding window
25694 margin in that case. */
25695 if (!cursor_row->reversed_p && hpos < 0)
25696 hpos = 0;
25697 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25698 hpos = cursor_row->used[TEXT_AREA] - 1;
25699
25700 /* If the cursor is in the mouse face area, redisplay that when
25701 we clear the cursor. */
25702 if (! NILP (hlinfo->mouse_face_window)
25703 && coords_in_mouse_face_p (w, hpos, vpos)
25704 /* Don't redraw the cursor's spot in mouse face if it is at the
25705 end of a line (on a newline). The cursor appears there, but
25706 mouse highlighting does not. */
25707 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25708 mouse_face_here_p = 1;
25709
25710 /* Maybe clear the display under the cursor. */
25711 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25712 {
25713 int x, y, left_x;
25714 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25715 int width;
25716
25717 cursor_glyph = get_phys_cursor_glyph (w);
25718 if (cursor_glyph == NULL)
25719 goto mark_cursor_off;
25720
25721 width = cursor_glyph->pixel_width;
25722 left_x = window_box_left_offset (w, TEXT_AREA);
25723 x = w->phys_cursor.x;
25724 if (x < left_x)
25725 width -= left_x - x;
25726 width = min (width, window_box_width (w, TEXT_AREA) - x);
25727 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25728 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25729
25730 if (width > 0)
25731 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25732 }
25733
25734 /* Erase the cursor by redrawing the character underneath it. */
25735 if (mouse_face_here_p)
25736 hl = DRAW_MOUSE_FACE;
25737 else
25738 hl = DRAW_NORMAL_TEXT;
25739 draw_phys_cursor_glyph (w, cursor_row, hl);
25740
25741 mark_cursor_off:
25742 w->phys_cursor_on_p = 0;
25743 w->phys_cursor_type = NO_CURSOR;
25744 }
25745
25746
25747 /* EXPORT:
25748 Display or clear cursor of window W. If ON is zero, clear the
25749 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25750 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25751
25752 void
25753 display_and_set_cursor (struct window *w, int on,
25754 int hpos, int vpos, int x, int y)
25755 {
25756 struct frame *f = XFRAME (w->frame);
25757 int new_cursor_type;
25758 int new_cursor_width;
25759 int active_cursor;
25760 struct glyph_row *glyph_row;
25761 struct glyph *glyph;
25762
25763 /* This is pointless on invisible frames, and dangerous on garbaged
25764 windows and frames; in the latter case, the frame or window may
25765 be in the midst of changing its size, and x and y may be off the
25766 window. */
25767 if (! FRAME_VISIBLE_P (f)
25768 || FRAME_GARBAGED_P (f)
25769 || vpos >= w->current_matrix->nrows
25770 || hpos >= w->current_matrix->matrix_w)
25771 return;
25772
25773 /* If cursor is off and we want it off, return quickly. */
25774 if (!on && !w->phys_cursor_on_p)
25775 return;
25776
25777 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25778 /* If cursor row is not enabled, we don't really know where to
25779 display the cursor. */
25780 if (!glyph_row->enabled_p)
25781 {
25782 w->phys_cursor_on_p = 0;
25783 return;
25784 }
25785
25786 glyph = NULL;
25787 if (!glyph_row->exact_window_width_line_p
25788 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25789 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25790
25791 eassert (interrupt_input_blocked);
25792
25793 /* Set new_cursor_type to the cursor we want to be displayed. */
25794 new_cursor_type = get_window_cursor_type (w, glyph,
25795 &new_cursor_width, &active_cursor);
25796
25797 /* If cursor is currently being shown and we don't want it to be or
25798 it is in the wrong place, or the cursor type is not what we want,
25799 erase it. */
25800 if (w->phys_cursor_on_p
25801 && (!on
25802 || w->phys_cursor.x != x
25803 || w->phys_cursor.y != y
25804 || new_cursor_type != w->phys_cursor_type
25805 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25806 && new_cursor_width != w->phys_cursor_width)))
25807 erase_phys_cursor (w);
25808
25809 /* Don't check phys_cursor_on_p here because that flag is only set
25810 to zero in some cases where we know that the cursor has been
25811 completely erased, to avoid the extra work of erasing the cursor
25812 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25813 still not be visible, or it has only been partly erased. */
25814 if (on)
25815 {
25816 w->phys_cursor_ascent = glyph_row->ascent;
25817 w->phys_cursor_height = glyph_row->height;
25818
25819 /* Set phys_cursor_.* before x_draw_.* is called because some
25820 of them may need the information. */
25821 w->phys_cursor.x = x;
25822 w->phys_cursor.y = glyph_row->y;
25823 w->phys_cursor.hpos = hpos;
25824 w->phys_cursor.vpos = vpos;
25825 }
25826
25827 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25828 new_cursor_type, new_cursor_width,
25829 on, active_cursor);
25830 }
25831
25832
25833 /* Switch the display of W's cursor on or off, according to the value
25834 of ON. */
25835
25836 static void
25837 update_window_cursor (struct window *w, int on)
25838 {
25839 /* Don't update cursor in windows whose frame is in the process
25840 of being deleted. */
25841 if (w->current_matrix)
25842 {
25843 int hpos = w->phys_cursor.hpos;
25844 int vpos = w->phys_cursor.vpos;
25845 struct glyph_row *row;
25846
25847 if (vpos >= w->current_matrix->nrows
25848 || hpos >= w->current_matrix->matrix_w)
25849 return;
25850
25851 row = MATRIX_ROW (w->current_matrix, vpos);
25852
25853 /* When the window is hscrolled, cursor hpos can legitimately be
25854 out of bounds, but we draw the cursor at the corresponding
25855 window margin in that case. */
25856 if (!row->reversed_p && hpos < 0)
25857 hpos = 0;
25858 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25859 hpos = row->used[TEXT_AREA] - 1;
25860
25861 BLOCK_INPUT;
25862 display_and_set_cursor (w, on, hpos, vpos,
25863 w->phys_cursor.x, w->phys_cursor.y);
25864 UNBLOCK_INPUT;
25865 }
25866 }
25867
25868
25869 /* Call update_window_cursor with parameter ON_P on all leaf windows
25870 in the window tree rooted at W. */
25871
25872 static void
25873 update_cursor_in_window_tree (struct window *w, int on_p)
25874 {
25875 while (w)
25876 {
25877 if (!NILP (w->hchild))
25878 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25879 else if (!NILP (w->vchild))
25880 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25881 else
25882 update_window_cursor (w, on_p);
25883
25884 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25885 }
25886 }
25887
25888
25889 /* EXPORT:
25890 Display the cursor on window W, or clear it, according to ON_P.
25891 Don't change the cursor's position. */
25892
25893 void
25894 x_update_cursor (struct frame *f, int on_p)
25895 {
25896 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25897 }
25898
25899
25900 /* EXPORT:
25901 Clear the cursor of window W to background color, and mark the
25902 cursor as not shown. This is used when the text where the cursor
25903 is about to be rewritten. */
25904
25905 void
25906 x_clear_cursor (struct window *w)
25907 {
25908 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25909 update_window_cursor (w, 0);
25910 }
25911
25912 #endif /* HAVE_WINDOW_SYSTEM */
25913
25914 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25915 and MSDOS. */
25916 static void
25917 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25918 int start_hpos, int end_hpos,
25919 enum draw_glyphs_face draw)
25920 {
25921 #ifdef HAVE_WINDOW_SYSTEM
25922 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25923 {
25924 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25925 return;
25926 }
25927 #endif
25928 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
25929 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25930 #endif
25931 }
25932
25933 /* Display the active region described by mouse_face_* according to DRAW. */
25934
25935 static void
25936 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25937 {
25938 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25939 struct frame *f = XFRAME (WINDOW_FRAME (w));
25940
25941 if (/* If window is in the process of being destroyed, don't bother
25942 to do anything. */
25943 w->current_matrix != NULL
25944 /* Don't update mouse highlight if hidden */
25945 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25946 /* Recognize when we are called to operate on rows that don't exist
25947 anymore. This can happen when a window is split. */
25948 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25949 {
25950 int phys_cursor_on_p = w->phys_cursor_on_p;
25951 struct glyph_row *row, *first, *last;
25952
25953 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25954 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25955
25956 for (row = first; row <= last && row->enabled_p; ++row)
25957 {
25958 int start_hpos, end_hpos, start_x;
25959
25960 /* For all but the first row, the highlight starts at column 0. */
25961 if (row == first)
25962 {
25963 /* R2L rows have BEG and END in reversed order, but the
25964 screen drawing geometry is always left to right. So
25965 we need to mirror the beginning and end of the
25966 highlighted area in R2L rows. */
25967 if (!row->reversed_p)
25968 {
25969 start_hpos = hlinfo->mouse_face_beg_col;
25970 start_x = hlinfo->mouse_face_beg_x;
25971 }
25972 else if (row == last)
25973 {
25974 start_hpos = hlinfo->mouse_face_end_col;
25975 start_x = hlinfo->mouse_face_end_x;
25976 }
25977 else
25978 {
25979 start_hpos = 0;
25980 start_x = 0;
25981 }
25982 }
25983 else if (row->reversed_p && row == last)
25984 {
25985 start_hpos = hlinfo->mouse_face_end_col;
25986 start_x = hlinfo->mouse_face_end_x;
25987 }
25988 else
25989 {
25990 start_hpos = 0;
25991 start_x = 0;
25992 }
25993
25994 if (row == last)
25995 {
25996 if (!row->reversed_p)
25997 end_hpos = hlinfo->mouse_face_end_col;
25998 else if (row == first)
25999 end_hpos = hlinfo->mouse_face_beg_col;
26000 else
26001 {
26002 end_hpos = row->used[TEXT_AREA];
26003 if (draw == DRAW_NORMAL_TEXT)
26004 row->fill_line_p = 1; /* Clear to end of line */
26005 }
26006 }
26007 else if (row->reversed_p && row == first)
26008 end_hpos = hlinfo->mouse_face_beg_col;
26009 else
26010 {
26011 end_hpos = row->used[TEXT_AREA];
26012 if (draw == DRAW_NORMAL_TEXT)
26013 row->fill_line_p = 1; /* Clear to end of line */
26014 }
26015
26016 if (end_hpos > start_hpos)
26017 {
26018 draw_row_with_mouse_face (w, start_x, row,
26019 start_hpos, end_hpos, draw);
26020
26021 row->mouse_face_p
26022 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26023 }
26024 }
26025
26026 #ifdef HAVE_WINDOW_SYSTEM
26027 /* When we've written over the cursor, arrange for it to
26028 be displayed again. */
26029 if (FRAME_WINDOW_P (f)
26030 && phys_cursor_on_p && !w->phys_cursor_on_p)
26031 {
26032 int hpos = w->phys_cursor.hpos;
26033
26034 /* When the window is hscrolled, cursor hpos can legitimately be
26035 out of bounds, but we draw the cursor at the corresponding
26036 window margin in that case. */
26037 if (!row->reversed_p && hpos < 0)
26038 hpos = 0;
26039 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26040 hpos = row->used[TEXT_AREA] - 1;
26041
26042 BLOCK_INPUT;
26043 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26044 w->phys_cursor.x, w->phys_cursor.y);
26045 UNBLOCK_INPUT;
26046 }
26047 #endif /* HAVE_WINDOW_SYSTEM */
26048 }
26049
26050 #ifdef HAVE_WINDOW_SYSTEM
26051 /* Change the mouse cursor. */
26052 if (FRAME_WINDOW_P (f))
26053 {
26054 if (draw == DRAW_NORMAL_TEXT
26055 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26056 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26057 else if (draw == DRAW_MOUSE_FACE)
26058 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26059 else
26060 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26061 }
26062 #endif /* HAVE_WINDOW_SYSTEM */
26063 }
26064
26065 /* EXPORT:
26066 Clear out the mouse-highlighted active region.
26067 Redraw it un-highlighted first. Value is non-zero if mouse
26068 face was actually drawn unhighlighted. */
26069
26070 int
26071 clear_mouse_face (Mouse_HLInfo *hlinfo)
26072 {
26073 int cleared = 0;
26074
26075 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26076 {
26077 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26078 cleared = 1;
26079 }
26080
26081 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26082 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26083 hlinfo->mouse_face_window = Qnil;
26084 hlinfo->mouse_face_overlay = Qnil;
26085 return cleared;
26086 }
26087
26088 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26089 within the mouse face on that window. */
26090 static int
26091 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26092 {
26093 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26094
26095 /* Quickly resolve the easy cases. */
26096 if (!(WINDOWP (hlinfo->mouse_face_window)
26097 && XWINDOW (hlinfo->mouse_face_window) == w))
26098 return 0;
26099 if (vpos < hlinfo->mouse_face_beg_row
26100 || vpos > hlinfo->mouse_face_end_row)
26101 return 0;
26102 if (vpos > hlinfo->mouse_face_beg_row
26103 && vpos < hlinfo->mouse_face_end_row)
26104 return 1;
26105
26106 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26107 {
26108 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26109 {
26110 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26111 return 1;
26112 }
26113 else if ((vpos == hlinfo->mouse_face_beg_row
26114 && hpos >= hlinfo->mouse_face_beg_col)
26115 || (vpos == hlinfo->mouse_face_end_row
26116 && hpos < hlinfo->mouse_face_end_col))
26117 return 1;
26118 }
26119 else
26120 {
26121 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26122 {
26123 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26124 return 1;
26125 }
26126 else if ((vpos == hlinfo->mouse_face_beg_row
26127 && hpos <= hlinfo->mouse_face_beg_col)
26128 || (vpos == hlinfo->mouse_face_end_row
26129 && hpos > hlinfo->mouse_face_end_col))
26130 return 1;
26131 }
26132 return 0;
26133 }
26134
26135
26136 /* EXPORT:
26137 Non-zero if physical cursor of window W is within mouse face. */
26138
26139 int
26140 cursor_in_mouse_face_p (struct window *w)
26141 {
26142 int hpos = w->phys_cursor.hpos;
26143 int vpos = w->phys_cursor.vpos;
26144 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26145
26146 /* When the window is hscrolled, cursor hpos can legitimately be out
26147 of bounds, but we draw the cursor at the corresponding window
26148 margin in that case. */
26149 if (!row->reversed_p && hpos < 0)
26150 hpos = 0;
26151 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26152 hpos = row->used[TEXT_AREA] - 1;
26153
26154 return coords_in_mouse_face_p (w, hpos, vpos);
26155 }
26156
26157
26158 \f
26159 /* Find the glyph rows START_ROW and END_ROW of window W that display
26160 characters between buffer positions START_CHARPOS and END_CHARPOS
26161 (excluding END_CHARPOS). DISP_STRING is a display string that
26162 covers these buffer positions. This is similar to
26163 row_containing_pos, but is more accurate when bidi reordering makes
26164 buffer positions change non-linearly with glyph rows. */
26165 static void
26166 rows_from_pos_range (struct window *w,
26167 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26168 Lisp_Object disp_string,
26169 struct glyph_row **start, struct glyph_row **end)
26170 {
26171 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26172 int last_y = window_text_bottom_y (w);
26173 struct glyph_row *row;
26174
26175 *start = NULL;
26176 *end = NULL;
26177
26178 while (!first->enabled_p
26179 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26180 first++;
26181
26182 /* Find the START row. */
26183 for (row = first;
26184 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26185 row++)
26186 {
26187 /* A row can potentially be the START row if the range of the
26188 characters it displays intersects the range
26189 [START_CHARPOS..END_CHARPOS). */
26190 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26191 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26192 /* See the commentary in row_containing_pos, for the
26193 explanation of the complicated way to check whether
26194 some position is beyond the end of the characters
26195 displayed by a row. */
26196 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26197 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26198 && !row->ends_at_zv_p
26199 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26200 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26201 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26202 && !row->ends_at_zv_p
26203 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26204 {
26205 /* Found a candidate row. Now make sure at least one of the
26206 glyphs it displays has a charpos from the range
26207 [START_CHARPOS..END_CHARPOS).
26208
26209 This is not obvious because bidi reordering could make
26210 buffer positions of a row be 1,2,3,102,101,100, and if we
26211 want to highlight characters in [50..60), we don't want
26212 this row, even though [50..60) does intersect [1..103),
26213 the range of character positions given by the row's start
26214 and end positions. */
26215 struct glyph *g = row->glyphs[TEXT_AREA];
26216 struct glyph *e = g + row->used[TEXT_AREA];
26217
26218 while (g < e)
26219 {
26220 if (((BUFFERP (g->object) || INTEGERP (g->object))
26221 && start_charpos <= g->charpos && g->charpos < end_charpos)
26222 /* A glyph that comes from DISP_STRING is by
26223 definition to be highlighted. */
26224 || EQ (g->object, disp_string))
26225 *start = row;
26226 g++;
26227 }
26228 if (*start)
26229 break;
26230 }
26231 }
26232
26233 /* Find the END row. */
26234 if (!*start
26235 /* If the last row is partially visible, start looking for END
26236 from that row, instead of starting from FIRST. */
26237 && !(row->enabled_p
26238 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26239 row = first;
26240 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26241 {
26242 struct glyph_row *next = row + 1;
26243 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26244
26245 if (!next->enabled_p
26246 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26247 /* The first row >= START whose range of displayed characters
26248 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26249 is the row END + 1. */
26250 || (start_charpos < next_start
26251 && end_charpos < next_start)
26252 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26253 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26254 && !next->ends_at_zv_p
26255 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26256 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26257 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26258 && !next->ends_at_zv_p
26259 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26260 {
26261 *end = row;
26262 break;
26263 }
26264 else
26265 {
26266 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26267 but none of the characters it displays are in the range, it is
26268 also END + 1. */
26269 struct glyph *g = next->glyphs[TEXT_AREA];
26270 struct glyph *s = g;
26271 struct glyph *e = g + next->used[TEXT_AREA];
26272
26273 while (g < e)
26274 {
26275 if (((BUFFERP (g->object) || INTEGERP (g->object))
26276 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26277 /* If the buffer position of the first glyph in
26278 the row is equal to END_CHARPOS, it means
26279 the last character to be highlighted is the
26280 newline of ROW, and we must consider NEXT as
26281 END, not END+1. */
26282 || (((!next->reversed_p && g == s)
26283 || (next->reversed_p && g == e - 1))
26284 && (g->charpos == end_charpos
26285 /* Special case for when NEXT is an
26286 empty line at ZV. */
26287 || (g->charpos == -1
26288 && !row->ends_at_zv_p
26289 && next_start == end_charpos)))))
26290 /* A glyph that comes from DISP_STRING is by
26291 definition to be highlighted. */
26292 || EQ (g->object, disp_string))
26293 break;
26294 g++;
26295 }
26296 if (g == e)
26297 {
26298 *end = row;
26299 break;
26300 }
26301 /* The first row that ends at ZV must be the last to be
26302 highlighted. */
26303 else if (next->ends_at_zv_p)
26304 {
26305 *end = next;
26306 break;
26307 }
26308 }
26309 }
26310 }
26311
26312 /* This function sets the mouse_face_* elements of HLINFO, assuming
26313 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26314 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26315 for the overlay or run of text properties specifying the mouse
26316 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26317 before-string and after-string that must also be highlighted.
26318 DISP_STRING, if non-nil, is a display string that may cover some
26319 or all of the highlighted text. */
26320
26321 static void
26322 mouse_face_from_buffer_pos (Lisp_Object window,
26323 Mouse_HLInfo *hlinfo,
26324 ptrdiff_t mouse_charpos,
26325 ptrdiff_t start_charpos,
26326 ptrdiff_t end_charpos,
26327 Lisp_Object before_string,
26328 Lisp_Object after_string,
26329 Lisp_Object disp_string)
26330 {
26331 struct window *w = XWINDOW (window);
26332 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26333 struct glyph_row *r1, *r2;
26334 struct glyph *glyph, *end;
26335 ptrdiff_t ignore, pos;
26336 int x;
26337
26338 eassert (NILP (disp_string) || STRINGP (disp_string));
26339 eassert (NILP (before_string) || STRINGP (before_string));
26340 eassert (NILP (after_string) || STRINGP (after_string));
26341
26342 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26343 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26344 if (r1 == NULL)
26345 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26346 /* If the before-string or display-string contains newlines,
26347 rows_from_pos_range skips to its last row. Move back. */
26348 if (!NILP (before_string) || !NILP (disp_string))
26349 {
26350 struct glyph_row *prev;
26351 while ((prev = r1 - 1, prev >= first)
26352 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26353 && prev->used[TEXT_AREA] > 0)
26354 {
26355 struct glyph *beg = prev->glyphs[TEXT_AREA];
26356 glyph = beg + prev->used[TEXT_AREA];
26357 while (--glyph >= beg && INTEGERP (glyph->object));
26358 if (glyph < beg
26359 || !(EQ (glyph->object, before_string)
26360 || EQ (glyph->object, disp_string)))
26361 break;
26362 r1 = prev;
26363 }
26364 }
26365 if (r2 == NULL)
26366 {
26367 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26368 hlinfo->mouse_face_past_end = 1;
26369 }
26370 else if (!NILP (after_string))
26371 {
26372 /* If the after-string has newlines, advance to its last row. */
26373 struct glyph_row *next;
26374 struct glyph_row *last
26375 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26376
26377 for (next = r2 + 1;
26378 next <= last
26379 && next->used[TEXT_AREA] > 0
26380 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26381 ++next)
26382 r2 = next;
26383 }
26384 /* The rest of the display engine assumes that mouse_face_beg_row is
26385 either above mouse_face_end_row or identical to it. But with
26386 bidi-reordered continued lines, the row for START_CHARPOS could
26387 be below the row for END_CHARPOS. If so, swap the rows and store
26388 them in correct order. */
26389 if (r1->y > r2->y)
26390 {
26391 struct glyph_row *tem = r2;
26392
26393 r2 = r1;
26394 r1 = tem;
26395 }
26396
26397 hlinfo->mouse_face_beg_y = r1->y;
26398 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26399 hlinfo->mouse_face_end_y = r2->y;
26400 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26401
26402 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26403 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26404 could be anywhere in the row and in any order. The strategy
26405 below is to find the leftmost and the rightmost glyph that
26406 belongs to either of these 3 strings, or whose position is
26407 between START_CHARPOS and END_CHARPOS, and highlight all the
26408 glyphs between those two. This may cover more than just the text
26409 between START_CHARPOS and END_CHARPOS if the range of characters
26410 strides the bidi level boundary, e.g. if the beginning is in R2L
26411 text while the end is in L2R text or vice versa. */
26412 if (!r1->reversed_p)
26413 {
26414 /* This row is in a left to right paragraph. Scan it left to
26415 right. */
26416 glyph = r1->glyphs[TEXT_AREA];
26417 end = glyph + r1->used[TEXT_AREA];
26418 x = r1->x;
26419
26420 /* Skip truncation glyphs at the start of the glyph row. */
26421 if (r1->displays_text_p)
26422 for (; glyph < end
26423 && INTEGERP (glyph->object)
26424 && glyph->charpos < 0;
26425 ++glyph)
26426 x += glyph->pixel_width;
26427
26428 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26429 or DISP_STRING, and the first glyph from buffer whose
26430 position is between START_CHARPOS and END_CHARPOS. */
26431 for (; glyph < end
26432 && !INTEGERP (glyph->object)
26433 && !EQ (glyph->object, disp_string)
26434 && !(BUFFERP (glyph->object)
26435 && (glyph->charpos >= start_charpos
26436 && glyph->charpos < end_charpos));
26437 ++glyph)
26438 {
26439 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26440 are present at buffer positions between START_CHARPOS and
26441 END_CHARPOS, or if they come from an overlay. */
26442 if (EQ (glyph->object, before_string))
26443 {
26444 pos = string_buffer_position (before_string,
26445 start_charpos);
26446 /* If pos == 0, it means before_string came from an
26447 overlay, not from a buffer position. */
26448 if (!pos || (pos >= start_charpos && pos < end_charpos))
26449 break;
26450 }
26451 else if (EQ (glyph->object, after_string))
26452 {
26453 pos = string_buffer_position (after_string, end_charpos);
26454 if (!pos || (pos >= start_charpos && pos < end_charpos))
26455 break;
26456 }
26457 x += glyph->pixel_width;
26458 }
26459 hlinfo->mouse_face_beg_x = x;
26460 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26461 }
26462 else
26463 {
26464 /* This row is in a right to left paragraph. Scan it right to
26465 left. */
26466 struct glyph *g;
26467
26468 end = r1->glyphs[TEXT_AREA] - 1;
26469 glyph = end + r1->used[TEXT_AREA];
26470
26471 /* Skip truncation glyphs at the start of the glyph row. */
26472 if (r1->displays_text_p)
26473 for (; glyph > end
26474 && INTEGERP (glyph->object)
26475 && glyph->charpos < 0;
26476 --glyph)
26477 ;
26478
26479 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26480 or DISP_STRING, and the first glyph from buffer whose
26481 position is between START_CHARPOS and END_CHARPOS. */
26482 for (; glyph > end
26483 && !INTEGERP (glyph->object)
26484 && !EQ (glyph->object, disp_string)
26485 && !(BUFFERP (glyph->object)
26486 && (glyph->charpos >= start_charpos
26487 && glyph->charpos < end_charpos));
26488 --glyph)
26489 {
26490 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26491 are present at buffer positions between START_CHARPOS and
26492 END_CHARPOS, or if they come from an overlay. */
26493 if (EQ (glyph->object, before_string))
26494 {
26495 pos = string_buffer_position (before_string, start_charpos);
26496 /* If pos == 0, it means before_string came from an
26497 overlay, not from a buffer position. */
26498 if (!pos || (pos >= start_charpos && pos < end_charpos))
26499 break;
26500 }
26501 else if (EQ (glyph->object, after_string))
26502 {
26503 pos = string_buffer_position (after_string, end_charpos);
26504 if (!pos || (pos >= start_charpos && pos < end_charpos))
26505 break;
26506 }
26507 }
26508
26509 glyph++; /* first glyph to the right of the highlighted area */
26510 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26511 x += g->pixel_width;
26512 hlinfo->mouse_face_beg_x = x;
26513 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26514 }
26515
26516 /* If the highlight ends in a different row, compute GLYPH and END
26517 for the end row. Otherwise, reuse the values computed above for
26518 the row where the highlight begins. */
26519 if (r2 != r1)
26520 {
26521 if (!r2->reversed_p)
26522 {
26523 glyph = r2->glyphs[TEXT_AREA];
26524 end = glyph + r2->used[TEXT_AREA];
26525 x = r2->x;
26526 }
26527 else
26528 {
26529 end = r2->glyphs[TEXT_AREA] - 1;
26530 glyph = end + r2->used[TEXT_AREA];
26531 }
26532 }
26533
26534 if (!r2->reversed_p)
26535 {
26536 /* Skip truncation and continuation glyphs near the end of the
26537 row, and also blanks and stretch glyphs inserted by
26538 extend_face_to_end_of_line. */
26539 while (end > glyph
26540 && INTEGERP ((end - 1)->object))
26541 --end;
26542 /* Scan the rest of the glyph row from the end, looking for the
26543 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26544 DISP_STRING, or whose position is between START_CHARPOS
26545 and END_CHARPOS */
26546 for (--end;
26547 end > glyph
26548 && !INTEGERP (end->object)
26549 && !EQ (end->object, disp_string)
26550 && !(BUFFERP (end->object)
26551 && (end->charpos >= start_charpos
26552 && end->charpos < end_charpos));
26553 --end)
26554 {
26555 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26556 are present at buffer positions between START_CHARPOS and
26557 END_CHARPOS, or if they come from an overlay. */
26558 if (EQ (end->object, before_string))
26559 {
26560 pos = string_buffer_position (before_string, start_charpos);
26561 if (!pos || (pos >= start_charpos && pos < end_charpos))
26562 break;
26563 }
26564 else if (EQ (end->object, after_string))
26565 {
26566 pos = string_buffer_position (after_string, end_charpos);
26567 if (!pos || (pos >= start_charpos && pos < end_charpos))
26568 break;
26569 }
26570 }
26571 /* Find the X coordinate of the last glyph to be highlighted. */
26572 for (; glyph <= end; ++glyph)
26573 x += glyph->pixel_width;
26574
26575 hlinfo->mouse_face_end_x = x;
26576 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26577 }
26578 else
26579 {
26580 /* Skip truncation and continuation glyphs near the end of the
26581 row, and also blanks and stretch glyphs inserted by
26582 extend_face_to_end_of_line. */
26583 x = r2->x;
26584 end++;
26585 while (end < glyph
26586 && INTEGERP (end->object))
26587 {
26588 x += end->pixel_width;
26589 ++end;
26590 }
26591 /* Scan the rest of the glyph row from the end, looking for the
26592 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26593 DISP_STRING, or whose position is between START_CHARPOS
26594 and END_CHARPOS */
26595 for ( ;
26596 end < glyph
26597 && !INTEGERP (end->object)
26598 && !EQ (end->object, disp_string)
26599 && !(BUFFERP (end->object)
26600 && (end->charpos >= start_charpos
26601 && end->charpos < end_charpos));
26602 ++end)
26603 {
26604 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26605 are present at buffer positions between START_CHARPOS and
26606 END_CHARPOS, or if they come from an overlay. */
26607 if (EQ (end->object, before_string))
26608 {
26609 pos = string_buffer_position (before_string, start_charpos);
26610 if (!pos || (pos >= start_charpos && pos < end_charpos))
26611 break;
26612 }
26613 else if (EQ (end->object, after_string))
26614 {
26615 pos = string_buffer_position (after_string, end_charpos);
26616 if (!pos || (pos >= start_charpos && pos < end_charpos))
26617 break;
26618 }
26619 x += end->pixel_width;
26620 }
26621 /* If we exited the above loop because we arrived at the last
26622 glyph of the row, and its buffer position is still not in
26623 range, it means the last character in range is the preceding
26624 newline. Bump the end column and x values to get past the
26625 last glyph. */
26626 if (end == glyph
26627 && BUFFERP (end->object)
26628 && (end->charpos < start_charpos
26629 || end->charpos >= end_charpos))
26630 {
26631 x += end->pixel_width;
26632 ++end;
26633 }
26634 hlinfo->mouse_face_end_x = x;
26635 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26636 }
26637
26638 hlinfo->mouse_face_window = window;
26639 hlinfo->mouse_face_face_id
26640 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26641 mouse_charpos + 1,
26642 !hlinfo->mouse_face_hidden, -1);
26643 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26644 }
26645
26646 /* The following function is not used anymore (replaced with
26647 mouse_face_from_string_pos), but I leave it here for the time
26648 being, in case someone would. */
26649
26650 #if 0 /* not used */
26651
26652 /* Find the position of the glyph for position POS in OBJECT in
26653 window W's current matrix, and return in *X, *Y the pixel
26654 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26655
26656 RIGHT_P non-zero means return the position of the right edge of the
26657 glyph, RIGHT_P zero means return the left edge position.
26658
26659 If no glyph for POS exists in the matrix, return the position of
26660 the glyph with the next smaller position that is in the matrix, if
26661 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26662 exists in the matrix, return the position of the glyph with the
26663 next larger position in OBJECT.
26664
26665 Value is non-zero if a glyph was found. */
26666
26667 static int
26668 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26669 int *hpos, int *vpos, int *x, int *y, int right_p)
26670 {
26671 int yb = window_text_bottom_y (w);
26672 struct glyph_row *r;
26673 struct glyph *best_glyph = NULL;
26674 struct glyph_row *best_row = NULL;
26675 int best_x = 0;
26676
26677 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26678 r->enabled_p && r->y < yb;
26679 ++r)
26680 {
26681 struct glyph *g = r->glyphs[TEXT_AREA];
26682 struct glyph *e = g + r->used[TEXT_AREA];
26683 int gx;
26684
26685 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26686 if (EQ (g->object, object))
26687 {
26688 if (g->charpos == pos)
26689 {
26690 best_glyph = g;
26691 best_x = gx;
26692 best_row = r;
26693 goto found;
26694 }
26695 else if (best_glyph == NULL
26696 || ((eabs (g->charpos - pos)
26697 < eabs (best_glyph->charpos - pos))
26698 && (right_p
26699 ? g->charpos < pos
26700 : g->charpos > pos)))
26701 {
26702 best_glyph = g;
26703 best_x = gx;
26704 best_row = r;
26705 }
26706 }
26707 }
26708
26709 found:
26710
26711 if (best_glyph)
26712 {
26713 *x = best_x;
26714 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26715
26716 if (right_p)
26717 {
26718 *x += best_glyph->pixel_width;
26719 ++*hpos;
26720 }
26721
26722 *y = best_row->y;
26723 *vpos = best_row - w->current_matrix->rows;
26724 }
26725
26726 return best_glyph != NULL;
26727 }
26728 #endif /* not used */
26729
26730 /* Find the positions of the first and the last glyphs in window W's
26731 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26732 (assumed to be a string), and return in HLINFO's mouse_face_*
26733 members the pixel and column/row coordinates of those glyphs. */
26734
26735 static void
26736 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26737 Lisp_Object object,
26738 ptrdiff_t startpos, ptrdiff_t endpos)
26739 {
26740 int yb = window_text_bottom_y (w);
26741 struct glyph_row *r;
26742 struct glyph *g, *e;
26743 int gx;
26744 int found = 0;
26745
26746 /* Find the glyph row with at least one position in the range
26747 [STARTPOS..ENDPOS], and the first glyph in that row whose
26748 position belongs to that range. */
26749 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26750 r->enabled_p && r->y < yb;
26751 ++r)
26752 {
26753 if (!r->reversed_p)
26754 {
26755 g = r->glyphs[TEXT_AREA];
26756 e = g + r->used[TEXT_AREA];
26757 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26758 if (EQ (g->object, object)
26759 && startpos <= g->charpos && g->charpos <= endpos)
26760 {
26761 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26762 hlinfo->mouse_face_beg_y = r->y;
26763 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26764 hlinfo->mouse_face_beg_x = gx;
26765 found = 1;
26766 break;
26767 }
26768 }
26769 else
26770 {
26771 struct glyph *g1;
26772
26773 e = r->glyphs[TEXT_AREA];
26774 g = e + r->used[TEXT_AREA];
26775 for ( ; g > e; --g)
26776 if (EQ ((g-1)->object, object)
26777 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26778 {
26779 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26780 hlinfo->mouse_face_beg_y = r->y;
26781 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26782 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26783 gx += g1->pixel_width;
26784 hlinfo->mouse_face_beg_x = gx;
26785 found = 1;
26786 break;
26787 }
26788 }
26789 if (found)
26790 break;
26791 }
26792
26793 if (!found)
26794 return;
26795
26796 /* Starting with the next row, look for the first row which does NOT
26797 include any glyphs whose positions are in the range. */
26798 for (++r; r->enabled_p && r->y < yb; ++r)
26799 {
26800 g = r->glyphs[TEXT_AREA];
26801 e = g + r->used[TEXT_AREA];
26802 found = 0;
26803 for ( ; g < e; ++g)
26804 if (EQ (g->object, object)
26805 && startpos <= g->charpos && g->charpos <= endpos)
26806 {
26807 found = 1;
26808 break;
26809 }
26810 if (!found)
26811 break;
26812 }
26813
26814 /* The highlighted region ends on the previous row. */
26815 r--;
26816
26817 /* Set the end row and its vertical pixel coordinate. */
26818 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26819 hlinfo->mouse_face_end_y = r->y;
26820
26821 /* Compute and set the end column and the end column's horizontal
26822 pixel coordinate. */
26823 if (!r->reversed_p)
26824 {
26825 g = r->glyphs[TEXT_AREA];
26826 e = g + r->used[TEXT_AREA];
26827 for ( ; e > g; --e)
26828 if (EQ ((e-1)->object, object)
26829 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26830 break;
26831 hlinfo->mouse_face_end_col = e - g;
26832
26833 for (gx = r->x; g < e; ++g)
26834 gx += g->pixel_width;
26835 hlinfo->mouse_face_end_x = gx;
26836 }
26837 else
26838 {
26839 e = r->glyphs[TEXT_AREA];
26840 g = e + r->used[TEXT_AREA];
26841 for (gx = r->x ; e < g; ++e)
26842 {
26843 if (EQ (e->object, object)
26844 && startpos <= e->charpos && e->charpos <= endpos)
26845 break;
26846 gx += e->pixel_width;
26847 }
26848 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26849 hlinfo->mouse_face_end_x = gx;
26850 }
26851 }
26852
26853 #ifdef HAVE_WINDOW_SYSTEM
26854
26855 /* See if position X, Y is within a hot-spot of an image. */
26856
26857 static int
26858 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26859 {
26860 if (!CONSP (hot_spot))
26861 return 0;
26862
26863 if (EQ (XCAR (hot_spot), Qrect))
26864 {
26865 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26866 Lisp_Object rect = XCDR (hot_spot);
26867 Lisp_Object tem;
26868 if (!CONSP (rect))
26869 return 0;
26870 if (!CONSP (XCAR (rect)))
26871 return 0;
26872 if (!CONSP (XCDR (rect)))
26873 return 0;
26874 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26875 return 0;
26876 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26877 return 0;
26878 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26879 return 0;
26880 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26881 return 0;
26882 return 1;
26883 }
26884 else if (EQ (XCAR (hot_spot), Qcircle))
26885 {
26886 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26887 Lisp_Object circ = XCDR (hot_spot);
26888 Lisp_Object lr, lx0, ly0;
26889 if (CONSP (circ)
26890 && CONSP (XCAR (circ))
26891 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26892 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26893 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26894 {
26895 double r = XFLOATINT (lr);
26896 double dx = XINT (lx0) - x;
26897 double dy = XINT (ly0) - y;
26898 return (dx * dx + dy * dy <= r * r);
26899 }
26900 }
26901 else if (EQ (XCAR (hot_spot), Qpoly))
26902 {
26903 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26904 if (VECTORP (XCDR (hot_spot)))
26905 {
26906 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26907 Lisp_Object *poly = v->contents;
26908 ptrdiff_t n = v->header.size;
26909 ptrdiff_t i;
26910 int inside = 0;
26911 Lisp_Object lx, ly;
26912 int x0, y0;
26913
26914 /* Need an even number of coordinates, and at least 3 edges. */
26915 if (n < 6 || n & 1)
26916 return 0;
26917
26918 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26919 If count is odd, we are inside polygon. Pixels on edges
26920 may or may not be included depending on actual geometry of the
26921 polygon. */
26922 if ((lx = poly[n-2], !INTEGERP (lx))
26923 || (ly = poly[n-1], !INTEGERP (lx)))
26924 return 0;
26925 x0 = XINT (lx), y0 = XINT (ly);
26926 for (i = 0; i < n; i += 2)
26927 {
26928 int x1 = x0, y1 = y0;
26929 if ((lx = poly[i], !INTEGERP (lx))
26930 || (ly = poly[i+1], !INTEGERP (ly)))
26931 return 0;
26932 x0 = XINT (lx), y0 = XINT (ly);
26933
26934 /* Does this segment cross the X line? */
26935 if (x0 >= x)
26936 {
26937 if (x1 >= x)
26938 continue;
26939 }
26940 else if (x1 < x)
26941 continue;
26942 if (y > y0 && y > y1)
26943 continue;
26944 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26945 inside = !inside;
26946 }
26947 return inside;
26948 }
26949 }
26950 return 0;
26951 }
26952
26953 Lisp_Object
26954 find_hot_spot (Lisp_Object map, int x, int y)
26955 {
26956 while (CONSP (map))
26957 {
26958 if (CONSP (XCAR (map))
26959 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26960 return XCAR (map);
26961 map = XCDR (map);
26962 }
26963
26964 return Qnil;
26965 }
26966
26967 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26968 3, 3, 0,
26969 doc: /* Lookup in image map MAP coordinates X and Y.
26970 An image map is an alist where each element has the format (AREA ID PLIST).
26971 An AREA is specified as either a rectangle, a circle, or a polygon:
26972 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26973 pixel coordinates of the upper left and bottom right corners.
26974 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26975 and the radius of the circle; r may be a float or integer.
26976 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26977 vector describes one corner in the polygon.
26978 Returns the alist element for the first matching AREA in MAP. */)
26979 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26980 {
26981 if (NILP (map))
26982 return Qnil;
26983
26984 CHECK_NUMBER (x);
26985 CHECK_NUMBER (y);
26986
26987 return find_hot_spot (map,
26988 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
26989 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
26990 }
26991
26992
26993 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26994 static void
26995 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26996 {
26997 /* Do not change cursor shape while dragging mouse. */
26998 if (!NILP (do_mouse_tracking))
26999 return;
27000
27001 if (!NILP (pointer))
27002 {
27003 if (EQ (pointer, Qarrow))
27004 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27005 else if (EQ (pointer, Qhand))
27006 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27007 else if (EQ (pointer, Qtext))
27008 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27009 else if (EQ (pointer, intern ("hdrag")))
27010 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27011 #ifdef HAVE_X_WINDOWS
27012 else if (EQ (pointer, intern ("vdrag")))
27013 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27014 #endif
27015 else if (EQ (pointer, intern ("hourglass")))
27016 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27017 else if (EQ (pointer, Qmodeline))
27018 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27019 else
27020 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27021 }
27022
27023 if (cursor != No_Cursor)
27024 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27025 }
27026
27027 #endif /* HAVE_WINDOW_SYSTEM */
27028
27029 /* Take proper action when mouse has moved to the mode or header line
27030 or marginal area AREA of window W, x-position X and y-position Y.
27031 X is relative to the start of the text display area of W, so the
27032 width of bitmap areas and scroll bars must be subtracted to get a
27033 position relative to the start of the mode line. */
27034
27035 static void
27036 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27037 enum window_part area)
27038 {
27039 struct window *w = XWINDOW (window);
27040 struct frame *f = XFRAME (w->frame);
27041 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27042 #ifdef HAVE_WINDOW_SYSTEM
27043 Display_Info *dpyinfo;
27044 #endif
27045 Cursor cursor = No_Cursor;
27046 Lisp_Object pointer = Qnil;
27047 int dx, dy, width, height;
27048 ptrdiff_t charpos;
27049 Lisp_Object string, object = Qnil;
27050 Lisp_Object pos IF_LINT (= Qnil), help;
27051
27052 Lisp_Object mouse_face;
27053 int original_x_pixel = x;
27054 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27055 struct glyph_row *row IF_LINT (= 0);
27056
27057 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27058 {
27059 int x0;
27060 struct glyph *end;
27061
27062 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27063 returns them in row/column units! */
27064 string = mode_line_string (w, area, &x, &y, &charpos,
27065 &object, &dx, &dy, &width, &height);
27066
27067 row = (area == ON_MODE_LINE
27068 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27069 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27070
27071 /* Find the glyph under the mouse pointer. */
27072 if (row->mode_line_p && row->enabled_p)
27073 {
27074 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27075 end = glyph + row->used[TEXT_AREA];
27076
27077 for (x0 = original_x_pixel;
27078 glyph < end && x0 >= glyph->pixel_width;
27079 ++glyph)
27080 x0 -= glyph->pixel_width;
27081
27082 if (glyph >= end)
27083 glyph = NULL;
27084 }
27085 }
27086 else
27087 {
27088 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27089 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27090 returns them in row/column units! */
27091 string = marginal_area_string (w, area, &x, &y, &charpos,
27092 &object, &dx, &dy, &width, &height);
27093 }
27094
27095 help = Qnil;
27096
27097 #ifdef HAVE_WINDOW_SYSTEM
27098 if (IMAGEP (object))
27099 {
27100 Lisp_Object image_map, hotspot;
27101 if ((image_map = Fplist_get (XCDR (object), QCmap),
27102 !NILP (image_map))
27103 && (hotspot = find_hot_spot (image_map, dx, dy),
27104 CONSP (hotspot))
27105 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27106 {
27107 Lisp_Object plist;
27108
27109 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27110 If so, we could look for mouse-enter, mouse-leave
27111 properties in PLIST (and do something...). */
27112 hotspot = XCDR (hotspot);
27113 if (CONSP (hotspot)
27114 && (plist = XCAR (hotspot), CONSP (plist)))
27115 {
27116 pointer = Fplist_get (plist, Qpointer);
27117 if (NILP (pointer))
27118 pointer = Qhand;
27119 help = Fplist_get (plist, Qhelp_echo);
27120 if (!NILP (help))
27121 {
27122 help_echo_string = help;
27123 XSETWINDOW (help_echo_window, w);
27124 help_echo_object = w->buffer;
27125 help_echo_pos = charpos;
27126 }
27127 }
27128 }
27129 if (NILP (pointer))
27130 pointer = Fplist_get (XCDR (object), QCpointer);
27131 }
27132 #endif /* HAVE_WINDOW_SYSTEM */
27133
27134 if (STRINGP (string))
27135 pos = make_number (charpos);
27136
27137 /* Set the help text and mouse pointer. If the mouse is on a part
27138 of the mode line without any text (e.g. past the right edge of
27139 the mode line text), use the default help text and pointer. */
27140 if (STRINGP (string) || area == ON_MODE_LINE)
27141 {
27142 /* Arrange to display the help by setting the global variables
27143 help_echo_string, help_echo_object, and help_echo_pos. */
27144 if (NILP (help))
27145 {
27146 if (STRINGP (string))
27147 help = Fget_text_property (pos, Qhelp_echo, string);
27148
27149 if (!NILP (help))
27150 {
27151 help_echo_string = help;
27152 XSETWINDOW (help_echo_window, w);
27153 help_echo_object = string;
27154 help_echo_pos = charpos;
27155 }
27156 else if (area == ON_MODE_LINE)
27157 {
27158 Lisp_Object default_help
27159 = buffer_local_value_1 (Qmode_line_default_help_echo,
27160 w->buffer);
27161
27162 if (STRINGP (default_help))
27163 {
27164 help_echo_string = default_help;
27165 XSETWINDOW (help_echo_window, w);
27166 help_echo_object = Qnil;
27167 help_echo_pos = -1;
27168 }
27169 }
27170 }
27171
27172 #ifdef HAVE_WINDOW_SYSTEM
27173 /* Change the mouse pointer according to what is under it. */
27174 if (FRAME_WINDOW_P (f))
27175 {
27176 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27177 if (STRINGP (string))
27178 {
27179 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27180
27181 if (NILP (pointer))
27182 pointer = Fget_text_property (pos, Qpointer, string);
27183
27184 /* Change the mouse pointer according to what is under X/Y. */
27185 if (NILP (pointer)
27186 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27187 {
27188 Lisp_Object map;
27189 map = Fget_text_property (pos, Qlocal_map, string);
27190 if (!KEYMAPP (map))
27191 map = Fget_text_property (pos, Qkeymap, string);
27192 if (!KEYMAPP (map))
27193 cursor = dpyinfo->vertical_scroll_bar_cursor;
27194 }
27195 }
27196 else
27197 /* Default mode-line pointer. */
27198 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27199 }
27200 #endif
27201 }
27202
27203 /* Change the mouse face according to what is under X/Y. */
27204 if (STRINGP (string))
27205 {
27206 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27207 if (!NILP (mouse_face)
27208 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27209 && glyph)
27210 {
27211 Lisp_Object b, e;
27212
27213 struct glyph * tmp_glyph;
27214
27215 int gpos;
27216 int gseq_length;
27217 int total_pixel_width;
27218 ptrdiff_t begpos, endpos, ignore;
27219
27220 int vpos, hpos;
27221
27222 b = Fprevious_single_property_change (make_number (charpos + 1),
27223 Qmouse_face, string, Qnil);
27224 if (NILP (b))
27225 begpos = 0;
27226 else
27227 begpos = XINT (b);
27228
27229 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27230 if (NILP (e))
27231 endpos = SCHARS (string);
27232 else
27233 endpos = XINT (e);
27234
27235 /* Calculate the glyph position GPOS of GLYPH in the
27236 displayed string, relative to the beginning of the
27237 highlighted part of the string.
27238
27239 Note: GPOS is different from CHARPOS. CHARPOS is the
27240 position of GLYPH in the internal string object. A mode
27241 line string format has structures which are converted to
27242 a flattened string by the Emacs Lisp interpreter. The
27243 internal string is an element of those structures. The
27244 displayed string is the flattened string. */
27245 tmp_glyph = row_start_glyph;
27246 while (tmp_glyph < glyph
27247 && (!(EQ (tmp_glyph->object, glyph->object)
27248 && begpos <= tmp_glyph->charpos
27249 && tmp_glyph->charpos < endpos)))
27250 tmp_glyph++;
27251 gpos = glyph - tmp_glyph;
27252
27253 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27254 the highlighted part of the displayed string to which
27255 GLYPH belongs. Note: GSEQ_LENGTH is different from
27256 SCHARS (STRING), because the latter returns the length of
27257 the internal string. */
27258 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27259 tmp_glyph > glyph
27260 && (!(EQ (tmp_glyph->object, glyph->object)
27261 && begpos <= tmp_glyph->charpos
27262 && tmp_glyph->charpos < endpos));
27263 tmp_glyph--)
27264 ;
27265 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27266
27267 /* Calculate the total pixel width of all the glyphs between
27268 the beginning of the highlighted area and GLYPH. */
27269 total_pixel_width = 0;
27270 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27271 total_pixel_width += tmp_glyph->pixel_width;
27272
27273 /* Pre calculation of re-rendering position. Note: X is in
27274 column units here, after the call to mode_line_string or
27275 marginal_area_string. */
27276 hpos = x - gpos;
27277 vpos = (area == ON_MODE_LINE
27278 ? (w->current_matrix)->nrows - 1
27279 : 0);
27280
27281 /* If GLYPH's position is included in the region that is
27282 already drawn in mouse face, we have nothing to do. */
27283 if ( EQ (window, hlinfo->mouse_face_window)
27284 && (!row->reversed_p
27285 ? (hlinfo->mouse_face_beg_col <= hpos
27286 && hpos < hlinfo->mouse_face_end_col)
27287 /* In R2L rows we swap BEG and END, see below. */
27288 : (hlinfo->mouse_face_end_col <= hpos
27289 && hpos < hlinfo->mouse_face_beg_col))
27290 && hlinfo->mouse_face_beg_row == vpos )
27291 return;
27292
27293 if (clear_mouse_face (hlinfo))
27294 cursor = No_Cursor;
27295
27296 if (!row->reversed_p)
27297 {
27298 hlinfo->mouse_face_beg_col = hpos;
27299 hlinfo->mouse_face_beg_x = original_x_pixel
27300 - (total_pixel_width + dx);
27301 hlinfo->mouse_face_end_col = hpos + gseq_length;
27302 hlinfo->mouse_face_end_x = 0;
27303 }
27304 else
27305 {
27306 /* In R2L rows, show_mouse_face expects BEG and END
27307 coordinates to be swapped. */
27308 hlinfo->mouse_face_end_col = hpos;
27309 hlinfo->mouse_face_end_x = original_x_pixel
27310 - (total_pixel_width + dx);
27311 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27312 hlinfo->mouse_face_beg_x = 0;
27313 }
27314
27315 hlinfo->mouse_face_beg_row = vpos;
27316 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27317 hlinfo->mouse_face_beg_y = 0;
27318 hlinfo->mouse_face_end_y = 0;
27319 hlinfo->mouse_face_past_end = 0;
27320 hlinfo->mouse_face_window = window;
27321
27322 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27323 charpos,
27324 0, 0, 0,
27325 &ignore,
27326 glyph->face_id,
27327 1);
27328 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27329
27330 if (NILP (pointer))
27331 pointer = Qhand;
27332 }
27333 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27334 clear_mouse_face (hlinfo);
27335 }
27336 #ifdef HAVE_WINDOW_SYSTEM
27337 if (FRAME_WINDOW_P (f))
27338 define_frame_cursor1 (f, cursor, pointer);
27339 #endif
27340 }
27341
27342
27343 /* EXPORT:
27344 Take proper action when the mouse has moved to position X, Y on
27345 frame F as regards highlighting characters that have mouse-face
27346 properties. Also de-highlighting chars where the mouse was before.
27347 X and Y can be negative or out of range. */
27348
27349 void
27350 note_mouse_highlight (struct frame *f, int x, int y)
27351 {
27352 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27353 enum window_part part = ON_NOTHING;
27354 Lisp_Object window;
27355 struct window *w;
27356 Cursor cursor = No_Cursor;
27357 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27358 struct buffer *b;
27359
27360 /* When a menu is active, don't highlight because this looks odd. */
27361 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27362 if (popup_activated ())
27363 return;
27364 #endif
27365
27366 if (NILP (Vmouse_highlight)
27367 || !f->glyphs_initialized_p
27368 || f->pointer_invisible)
27369 return;
27370
27371 hlinfo->mouse_face_mouse_x = x;
27372 hlinfo->mouse_face_mouse_y = y;
27373 hlinfo->mouse_face_mouse_frame = f;
27374
27375 if (hlinfo->mouse_face_defer)
27376 return;
27377
27378 if (gc_in_progress)
27379 {
27380 hlinfo->mouse_face_deferred_gc = 1;
27381 return;
27382 }
27383
27384 /* Which window is that in? */
27385 window = window_from_coordinates (f, x, y, &part, 1);
27386
27387 /* If displaying active text in another window, clear that. */
27388 if (! EQ (window, hlinfo->mouse_face_window)
27389 /* Also clear if we move out of text area in same window. */
27390 || (!NILP (hlinfo->mouse_face_window)
27391 && !NILP (window)
27392 && part != ON_TEXT
27393 && part != ON_MODE_LINE
27394 && part != ON_HEADER_LINE))
27395 clear_mouse_face (hlinfo);
27396
27397 /* Not on a window -> return. */
27398 if (!WINDOWP (window))
27399 return;
27400
27401 /* Reset help_echo_string. It will get recomputed below. */
27402 help_echo_string = Qnil;
27403
27404 /* Convert to window-relative pixel coordinates. */
27405 w = XWINDOW (window);
27406 frame_to_window_pixel_xy (w, &x, &y);
27407
27408 #ifdef HAVE_WINDOW_SYSTEM
27409 /* Handle tool-bar window differently since it doesn't display a
27410 buffer. */
27411 if (EQ (window, f->tool_bar_window))
27412 {
27413 note_tool_bar_highlight (f, x, y);
27414 return;
27415 }
27416 #endif
27417
27418 /* Mouse is on the mode, header line or margin? */
27419 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27420 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27421 {
27422 note_mode_line_or_margin_highlight (window, x, y, part);
27423 return;
27424 }
27425
27426 #ifdef HAVE_WINDOW_SYSTEM
27427 if (part == ON_VERTICAL_BORDER)
27428 {
27429 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27430 help_echo_string = build_string ("drag-mouse-1: resize");
27431 }
27432 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27433 || part == ON_SCROLL_BAR)
27434 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27435 else
27436 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27437 #endif
27438
27439 /* Are we in a window whose display is up to date?
27440 And verify the buffer's text has not changed. */
27441 b = XBUFFER (w->buffer);
27442 if (part == ON_TEXT
27443 && EQ (w->window_end_valid, w->buffer)
27444 && w->last_modified == BUF_MODIFF (b)
27445 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27446 {
27447 int hpos, vpos, dx, dy, area = LAST_AREA;
27448 ptrdiff_t pos;
27449 struct glyph *glyph;
27450 Lisp_Object object;
27451 Lisp_Object mouse_face = Qnil, position;
27452 Lisp_Object *overlay_vec = NULL;
27453 ptrdiff_t i, noverlays;
27454 struct buffer *obuf;
27455 ptrdiff_t obegv, ozv;
27456 int same_region;
27457
27458 /* Find the glyph under X/Y. */
27459 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27460
27461 #ifdef HAVE_WINDOW_SYSTEM
27462 /* Look for :pointer property on image. */
27463 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27464 {
27465 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27466 if (img != NULL && IMAGEP (img->spec))
27467 {
27468 Lisp_Object image_map, hotspot;
27469 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27470 !NILP (image_map))
27471 && (hotspot = find_hot_spot (image_map,
27472 glyph->slice.img.x + dx,
27473 glyph->slice.img.y + dy),
27474 CONSP (hotspot))
27475 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27476 {
27477 Lisp_Object plist;
27478
27479 /* Could check XCAR (hotspot) to see if we enter/leave
27480 this hot-spot.
27481 If so, we could look for mouse-enter, mouse-leave
27482 properties in PLIST (and do something...). */
27483 hotspot = XCDR (hotspot);
27484 if (CONSP (hotspot)
27485 && (plist = XCAR (hotspot), CONSP (plist)))
27486 {
27487 pointer = Fplist_get (plist, Qpointer);
27488 if (NILP (pointer))
27489 pointer = Qhand;
27490 help_echo_string = Fplist_get (plist, Qhelp_echo);
27491 if (!NILP (help_echo_string))
27492 {
27493 help_echo_window = window;
27494 help_echo_object = glyph->object;
27495 help_echo_pos = glyph->charpos;
27496 }
27497 }
27498 }
27499 if (NILP (pointer))
27500 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27501 }
27502 }
27503 #endif /* HAVE_WINDOW_SYSTEM */
27504
27505 /* Clear mouse face if X/Y not over text. */
27506 if (glyph == NULL
27507 || area != TEXT_AREA
27508 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27509 /* Glyph's OBJECT is an integer for glyphs inserted by the
27510 display engine for its internal purposes, like truncation
27511 and continuation glyphs and blanks beyond the end of
27512 line's text on text terminals. If we are over such a
27513 glyph, we are not over any text. */
27514 || INTEGERP (glyph->object)
27515 /* R2L rows have a stretch glyph at their front, which
27516 stands for no text, whereas L2R rows have no glyphs at
27517 all beyond the end of text. Treat such stretch glyphs
27518 like we do with NULL glyphs in L2R rows. */
27519 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27520 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27521 && glyph->type == STRETCH_GLYPH
27522 && glyph->avoid_cursor_p))
27523 {
27524 if (clear_mouse_face (hlinfo))
27525 cursor = No_Cursor;
27526 #ifdef HAVE_WINDOW_SYSTEM
27527 if (FRAME_WINDOW_P (f) && NILP (pointer))
27528 {
27529 if (area != TEXT_AREA)
27530 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27531 else
27532 pointer = Vvoid_text_area_pointer;
27533 }
27534 #endif
27535 goto set_cursor;
27536 }
27537
27538 pos = glyph->charpos;
27539 object = glyph->object;
27540 if (!STRINGP (object) && !BUFFERP (object))
27541 goto set_cursor;
27542
27543 /* If we get an out-of-range value, return now; avoid an error. */
27544 if (BUFFERP (object) && pos > BUF_Z (b))
27545 goto set_cursor;
27546
27547 /* Make the window's buffer temporarily current for
27548 overlays_at and compute_char_face. */
27549 obuf = current_buffer;
27550 current_buffer = b;
27551 obegv = BEGV;
27552 ozv = ZV;
27553 BEGV = BEG;
27554 ZV = Z;
27555
27556 /* Is this char mouse-active or does it have help-echo? */
27557 position = make_number (pos);
27558
27559 if (BUFFERP (object))
27560 {
27561 /* Put all the overlays we want in a vector in overlay_vec. */
27562 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27563 /* Sort overlays into increasing priority order. */
27564 noverlays = sort_overlays (overlay_vec, noverlays, w);
27565 }
27566 else
27567 noverlays = 0;
27568
27569 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27570
27571 if (same_region)
27572 cursor = No_Cursor;
27573
27574 /* Check mouse-face highlighting. */
27575 if (! same_region
27576 /* If there exists an overlay with mouse-face overlapping
27577 the one we are currently highlighting, we have to
27578 check if we enter the overlapping overlay, and then
27579 highlight only that. */
27580 || (OVERLAYP (hlinfo->mouse_face_overlay)
27581 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27582 {
27583 /* Find the highest priority overlay with a mouse-face. */
27584 Lisp_Object overlay = Qnil;
27585 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27586 {
27587 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27588 if (!NILP (mouse_face))
27589 overlay = overlay_vec[i];
27590 }
27591
27592 /* If we're highlighting the same overlay as before, there's
27593 no need to do that again. */
27594 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27595 goto check_help_echo;
27596 hlinfo->mouse_face_overlay = overlay;
27597
27598 /* Clear the display of the old active region, if any. */
27599 if (clear_mouse_face (hlinfo))
27600 cursor = No_Cursor;
27601
27602 /* If no overlay applies, get a text property. */
27603 if (NILP (overlay))
27604 mouse_face = Fget_text_property (position, Qmouse_face, object);
27605
27606 /* Next, compute the bounds of the mouse highlighting and
27607 display it. */
27608 if (!NILP (mouse_face) && STRINGP (object))
27609 {
27610 /* The mouse-highlighting comes from a display string
27611 with a mouse-face. */
27612 Lisp_Object s, e;
27613 ptrdiff_t ignore;
27614
27615 s = Fprevious_single_property_change
27616 (make_number (pos + 1), Qmouse_face, object, Qnil);
27617 e = Fnext_single_property_change
27618 (position, Qmouse_face, object, Qnil);
27619 if (NILP (s))
27620 s = make_number (0);
27621 if (NILP (e))
27622 e = make_number (SCHARS (object) - 1);
27623 mouse_face_from_string_pos (w, hlinfo, object,
27624 XINT (s), XINT (e));
27625 hlinfo->mouse_face_past_end = 0;
27626 hlinfo->mouse_face_window = window;
27627 hlinfo->mouse_face_face_id
27628 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27629 glyph->face_id, 1);
27630 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27631 cursor = No_Cursor;
27632 }
27633 else
27634 {
27635 /* The mouse-highlighting, if any, comes from an overlay
27636 or text property in the buffer. */
27637 Lisp_Object buffer IF_LINT (= Qnil);
27638 Lisp_Object disp_string IF_LINT (= Qnil);
27639
27640 if (STRINGP (object))
27641 {
27642 /* If we are on a display string with no mouse-face,
27643 check if the text under it has one. */
27644 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27645 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27646 pos = string_buffer_position (object, start);
27647 if (pos > 0)
27648 {
27649 mouse_face = get_char_property_and_overlay
27650 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27651 buffer = w->buffer;
27652 disp_string = object;
27653 }
27654 }
27655 else
27656 {
27657 buffer = object;
27658 disp_string = Qnil;
27659 }
27660
27661 if (!NILP (mouse_face))
27662 {
27663 Lisp_Object before, after;
27664 Lisp_Object before_string, after_string;
27665 /* To correctly find the limits of mouse highlight
27666 in a bidi-reordered buffer, we must not use the
27667 optimization of limiting the search in
27668 previous-single-property-change and
27669 next-single-property-change, because
27670 rows_from_pos_range needs the real start and end
27671 positions to DTRT in this case. That's because
27672 the first row visible in a window does not
27673 necessarily display the character whose position
27674 is the smallest. */
27675 Lisp_Object lim1 =
27676 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27677 ? Fmarker_position (w->start)
27678 : Qnil;
27679 Lisp_Object lim2 =
27680 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27681 ? make_number (BUF_Z (XBUFFER (buffer))
27682 - XFASTINT (w->window_end_pos))
27683 : Qnil;
27684
27685 if (NILP (overlay))
27686 {
27687 /* Handle the text property case. */
27688 before = Fprevious_single_property_change
27689 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27690 after = Fnext_single_property_change
27691 (make_number (pos), Qmouse_face, buffer, lim2);
27692 before_string = after_string = Qnil;
27693 }
27694 else
27695 {
27696 /* Handle the overlay case. */
27697 before = Foverlay_start (overlay);
27698 after = Foverlay_end (overlay);
27699 before_string = Foverlay_get (overlay, Qbefore_string);
27700 after_string = Foverlay_get (overlay, Qafter_string);
27701
27702 if (!STRINGP (before_string)) before_string = Qnil;
27703 if (!STRINGP (after_string)) after_string = Qnil;
27704 }
27705
27706 mouse_face_from_buffer_pos (window, hlinfo, pos,
27707 NILP (before)
27708 ? 1
27709 : XFASTINT (before),
27710 NILP (after)
27711 ? BUF_Z (XBUFFER (buffer))
27712 : XFASTINT (after),
27713 before_string, after_string,
27714 disp_string);
27715 cursor = No_Cursor;
27716 }
27717 }
27718 }
27719
27720 check_help_echo:
27721
27722 /* Look for a `help-echo' property. */
27723 if (NILP (help_echo_string)) {
27724 Lisp_Object help, overlay;
27725
27726 /* Check overlays first. */
27727 help = overlay = Qnil;
27728 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27729 {
27730 overlay = overlay_vec[i];
27731 help = Foverlay_get (overlay, Qhelp_echo);
27732 }
27733
27734 if (!NILP (help))
27735 {
27736 help_echo_string = help;
27737 help_echo_window = window;
27738 help_echo_object = overlay;
27739 help_echo_pos = pos;
27740 }
27741 else
27742 {
27743 Lisp_Object obj = glyph->object;
27744 ptrdiff_t charpos = glyph->charpos;
27745
27746 /* Try text properties. */
27747 if (STRINGP (obj)
27748 && charpos >= 0
27749 && charpos < SCHARS (obj))
27750 {
27751 help = Fget_text_property (make_number (charpos),
27752 Qhelp_echo, obj);
27753 if (NILP (help))
27754 {
27755 /* If the string itself doesn't specify a help-echo,
27756 see if the buffer text ``under'' it does. */
27757 struct glyph_row *r
27758 = MATRIX_ROW (w->current_matrix, vpos);
27759 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27760 ptrdiff_t p = string_buffer_position (obj, start);
27761 if (p > 0)
27762 {
27763 help = Fget_char_property (make_number (p),
27764 Qhelp_echo, w->buffer);
27765 if (!NILP (help))
27766 {
27767 charpos = p;
27768 obj = w->buffer;
27769 }
27770 }
27771 }
27772 }
27773 else if (BUFFERP (obj)
27774 && charpos >= BEGV
27775 && charpos < ZV)
27776 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27777 obj);
27778
27779 if (!NILP (help))
27780 {
27781 help_echo_string = help;
27782 help_echo_window = window;
27783 help_echo_object = obj;
27784 help_echo_pos = charpos;
27785 }
27786 }
27787 }
27788
27789 #ifdef HAVE_WINDOW_SYSTEM
27790 /* Look for a `pointer' property. */
27791 if (FRAME_WINDOW_P (f) && NILP (pointer))
27792 {
27793 /* Check overlays first. */
27794 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27795 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27796
27797 if (NILP (pointer))
27798 {
27799 Lisp_Object obj = glyph->object;
27800 ptrdiff_t charpos = glyph->charpos;
27801
27802 /* Try text properties. */
27803 if (STRINGP (obj)
27804 && charpos >= 0
27805 && charpos < SCHARS (obj))
27806 {
27807 pointer = Fget_text_property (make_number (charpos),
27808 Qpointer, obj);
27809 if (NILP (pointer))
27810 {
27811 /* If the string itself doesn't specify a pointer,
27812 see if the buffer text ``under'' it does. */
27813 struct glyph_row *r
27814 = MATRIX_ROW (w->current_matrix, vpos);
27815 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27816 ptrdiff_t p = string_buffer_position (obj, start);
27817 if (p > 0)
27818 pointer = Fget_char_property (make_number (p),
27819 Qpointer, w->buffer);
27820 }
27821 }
27822 else if (BUFFERP (obj)
27823 && charpos >= BEGV
27824 && charpos < ZV)
27825 pointer = Fget_text_property (make_number (charpos),
27826 Qpointer, obj);
27827 }
27828 }
27829 #endif /* HAVE_WINDOW_SYSTEM */
27830
27831 BEGV = obegv;
27832 ZV = ozv;
27833 current_buffer = obuf;
27834 }
27835
27836 set_cursor:
27837
27838 #ifdef HAVE_WINDOW_SYSTEM
27839 if (FRAME_WINDOW_P (f))
27840 define_frame_cursor1 (f, cursor, pointer);
27841 #else
27842 /* This is here to prevent a compiler error, about "label at end of
27843 compound statement". */
27844 return;
27845 #endif
27846 }
27847
27848
27849 /* EXPORT for RIF:
27850 Clear any mouse-face on window W. This function is part of the
27851 redisplay interface, and is called from try_window_id and similar
27852 functions to ensure the mouse-highlight is off. */
27853
27854 void
27855 x_clear_window_mouse_face (struct window *w)
27856 {
27857 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27858 Lisp_Object window;
27859
27860 BLOCK_INPUT;
27861 XSETWINDOW (window, w);
27862 if (EQ (window, hlinfo->mouse_face_window))
27863 clear_mouse_face (hlinfo);
27864 UNBLOCK_INPUT;
27865 }
27866
27867
27868 /* EXPORT:
27869 Just discard the mouse face information for frame F, if any.
27870 This is used when the size of F is changed. */
27871
27872 void
27873 cancel_mouse_face (struct frame *f)
27874 {
27875 Lisp_Object window;
27876 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27877
27878 window = hlinfo->mouse_face_window;
27879 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27880 {
27881 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27882 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27883 hlinfo->mouse_face_window = Qnil;
27884 }
27885 }
27886
27887
27888 \f
27889 /***********************************************************************
27890 Exposure Events
27891 ***********************************************************************/
27892
27893 #ifdef HAVE_WINDOW_SYSTEM
27894
27895 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27896 which intersects rectangle R. R is in window-relative coordinates. */
27897
27898 static void
27899 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27900 enum glyph_row_area area)
27901 {
27902 struct glyph *first = row->glyphs[area];
27903 struct glyph *end = row->glyphs[area] + row->used[area];
27904 struct glyph *last;
27905 int first_x, start_x, x;
27906
27907 if (area == TEXT_AREA && row->fill_line_p)
27908 /* If row extends face to end of line write the whole line. */
27909 draw_glyphs (w, 0, row, area,
27910 0, row->used[area],
27911 DRAW_NORMAL_TEXT, 0);
27912 else
27913 {
27914 /* Set START_X to the window-relative start position for drawing glyphs of
27915 AREA. The first glyph of the text area can be partially visible.
27916 The first glyphs of other areas cannot. */
27917 start_x = window_box_left_offset (w, area);
27918 x = start_x;
27919 if (area == TEXT_AREA)
27920 x += row->x;
27921
27922 /* Find the first glyph that must be redrawn. */
27923 while (first < end
27924 && x + first->pixel_width < r->x)
27925 {
27926 x += first->pixel_width;
27927 ++first;
27928 }
27929
27930 /* Find the last one. */
27931 last = first;
27932 first_x = x;
27933 while (last < end
27934 && x < r->x + r->width)
27935 {
27936 x += last->pixel_width;
27937 ++last;
27938 }
27939
27940 /* Repaint. */
27941 if (last > first)
27942 draw_glyphs (w, first_x - start_x, row, area,
27943 first - row->glyphs[area], last - row->glyphs[area],
27944 DRAW_NORMAL_TEXT, 0);
27945 }
27946 }
27947
27948
27949 /* Redraw the parts of the glyph row ROW on window W intersecting
27950 rectangle R. R is in window-relative coordinates. Value is
27951 non-zero if mouse-face was overwritten. */
27952
27953 static int
27954 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27955 {
27956 eassert (row->enabled_p);
27957
27958 if (row->mode_line_p || w->pseudo_window_p)
27959 draw_glyphs (w, 0, row, TEXT_AREA,
27960 0, row->used[TEXT_AREA],
27961 DRAW_NORMAL_TEXT, 0);
27962 else
27963 {
27964 if (row->used[LEFT_MARGIN_AREA])
27965 expose_area (w, row, r, LEFT_MARGIN_AREA);
27966 if (row->used[TEXT_AREA])
27967 expose_area (w, row, r, TEXT_AREA);
27968 if (row->used[RIGHT_MARGIN_AREA])
27969 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27970 draw_row_fringe_bitmaps (w, row);
27971 }
27972
27973 return row->mouse_face_p;
27974 }
27975
27976
27977 /* Redraw those parts of glyphs rows during expose event handling that
27978 overlap other rows. Redrawing of an exposed line writes over parts
27979 of lines overlapping that exposed line; this function fixes that.
27980
27981 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27982 row in W's current matrix that is exposed and overlaps other rows.
27983 LAST_OVERLAPPING_ROW is the last such row. */
27984
27985 static void
27986 expose_overlaps (struct window *w,
27987 struct glyph_row *first_overlapping_row,
27988 struct glyph_row *last_overlapping_row,
27989 XRectangle *r)
27990 {
27991 struct glyph_row *row;
27992
27993 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27994 if (row->overlapping_p)
27995 {
27996 eassert (row->enabled_p && !row->mode_line_p);
27997
27998 row->clip = r;
27999 if (row->used[LEFT_MARGIN_AREA])
28000 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28001
28002 if (row->used[TEXT_AREA])
28003 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28004
28005 if (row->used[RIGHT_MARGIN_AREA])
28006 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28007 row->clip = NULL;
28008 }
28009 }
28010
28011
28012 /* Return non-zero if W's cursor intersects rectangle R. */
28013
28014 static int
28015 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28016 {
28017 XRectangle cr, result;
28018 struct glyph *cursor_glyph;
28019 struct glyph_row *row;
28020
28021 if (w->phys_cursor.vpos >= 0
28022 && w->phys_cursor.vpos < w->current_matrix->nrows
28023 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28024 row->enabled_p)
28025 && row->cursor_in_fringe_p)
28026 {
28027 /* Cursor is in the fringe. */
28028 cr.x = window_box_right_offset (w,
28029 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28030 ? RIGHT_MARGIN_AREA
28031 : TEXT_AREA));
28032 cr.y = row->y;
28033 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28034 cr.height = row->height;
28035 return x_intersect_rectangles (&cr, r, &result);
28036 }
28037
28038 cursor_glyph = get_phys_cursor_glyph (w);
28039 if (cursor_glyph)
28040 {
28041 /* r is relative to W's box, but w->phys_cursor.x is relative
28042 to left edge of W's TEXT area. Adjust it. */
28043 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28044 cr.y = w->phys_cursor.y;
28045 cr.width = cursor_glyph->pixel_width;
28046 cr.height = w->phys_cursor_height;
28047 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28048 I assume the effect is the same -- and this is portable. */
28049 return x_intersect_rectangles (&cr, r, &result);
28050 }
28051 /* If we don't understand the format, pretend we're not in the hot-spot. */
28052 return 0;
28053 }
28054
28055
28056 /* EXPORT:
28057 Draw a vertical window border to the right of window W if W doesn't
28058 have vertical scroll bars. */
28059
28060 void
28061 x_draw_vertical_border (struct window *w)
28062 {
28063 struct frame *f = XFRAME (WINDOW_FRAME (w));
28064
28065 /* We could do better, if we knew what type of scroll-bar the adjacent
28066 windows (on either side) have... But we don't :-(
28067 However, I think this works ok. ++KFS 2003-04-25 */
28068
28069 /* Redraw borders between horizontally adjacent windows. Don't
28070 do it for frames with vertical scroll bars because either the
28071 right scroll bar of a window, or the left scroll bar of its
28072 neighbor will suffice as a border. */
28073 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28074 return;
28075
28076 if (!WINDOW_RIGHTMOST_P (w)
28077 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28078 {
28079 int x0, x1, y0, y1;
28080
28081 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28082 y1 -= 1;
28083
28084 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28085 x1 -= 1;
28086
28087 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28088 }
28089 else if (!WINDOW_LEFTMOST_P (w)
28090 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28091 {
28092 int x0, x1, y0, y1;
28093
28094 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28095 y1 -= 1;
28096
28097 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28098 x0 -= 1;
28099
28100 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28101 }
28102 }
28103
28104
28105 /* Redraw the part of window W intersection rectangle FR. Pixel
28106 coordinates in FR are frame-relative. Call this function with
28107 input blocked. Value is non-zero if the exposure overwrites
28108 mouse-face. */
28109
28110 static int
28111 expose_window (struct window *w, XRectangle *fr)
28112 {
28113 struct frame *f = XFRAME (w->frame);
28114 XRectangle wr, r;
28115 int mouse_face_overwritten_p = 0;
28116
28117 /* If window is not yet fully initialized, do nothing. This can
28118 happen when toolkit scroll bars are used and a window is split.
28119 Reconfiguring the scroll bar will generate an expose for a newly
28120 created window. */
28121 if (w->current_matrix == NULL)
28122 return 0;
28123
28124 /* When we're currently updating the window, display and current
28125 matrix usually don't agree. Arrange for a thorough display
28126 later. */
28127 if (w == updated_window)
28128 {
28129 SET_FRAME_GARBAGED (f);
28130 return 0;
28131 }
28132
28133 /* Frame-relative pixel rectangle of W. */
28134 wr.x = WINDOW_LEFT_EDGE_X (w);
28135 wr.y = WINDOW_TOP_EDGE_Y (w);
28136 wr.width = WINDOW_TOTAL_WIDTH (w);
28137 wr.height = WINDOW_TOTAL_HEIGHT (w);
28138
28139 if (x_intersect_rectangles (fr, &wr, &r))
28140 {
28141 int yb = window_text_bottom_y (w);
28142 struct glyph_row *row;
28143 int cursor_cleared_p, phys_cursor_on_p;
28144 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28145
28146 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28147 r.x, r.y, r.width, r.height));
28148
28149 /* Convert to window coordinates. */
28150 r.x -= WINDOW_LEFT_EDGE_X (w);
28151 r.y -= WINDOW_TOP_EDGE_Y (w);
28152
28153 /* Turn off the cursor. */
28154 if (!w->pseudo_window_p
28155 && phys_cursor_in_rect_p (w, &r))
28156 {
28157 x_clear_cursor (w);
28158 cursor_cleared_p = 1;
28159 }
28160 else
28161 cursor_cleared_p = 0;
28162
28163 /* If the row containing the cursor extends face to end of line,
28164 then expose_area might overwrite the cursor outside the
28165 rectangle and thus notice_overwritten_cursor might clear
28166 w->phys_cursor_on_p. We remember the original value and
28167 check later if it is changed. */
28168 phys_cursor_on_p = w->phys_cursor_on_p;
28169
28170 /* Update lines intersecting rectangle R. */
28171 first_overlapping_row = last_overlapping_row = NULL;
28172 for (row = w->current_matrix->rows;
28173 row->enabled_p;
28174 ++row)
28175 {
28176 int y0 = row->y;
28177 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28178
28179 if ((y0 >= r.y && y0 < r.y + r.height)
28180 || (y1 > r.y && y1 < r.y + r.height)
28181 || (r.y >= y0 && r.y < y1)
28182 || (r.y + r.height > y0 && r.y + r.height < y1))
28183 {
28184 /* A header line may be overlapping, but there is no need
28185 to fix overlapping areas for them. KFS 2005-02-12 */
28186 if (row->overlapping_p && !row->mode_line_p)
28187 {
28188 if (first_overlapping_row == NULL)
28189 first_overlapping_row = row;
28190 last_overlapping_row = row;
28191 }
28192
28193 row->clip = fr;
28194 if (expose_line (w, row, &r))
28195 mouse_face_overwritten_p = 1;
28196 row->clip = NULL;
28197 }
28198 else if (row->overlapping_p)
28199 {
28200 /* We must redraw a row overlapping the exposed area. */
28201 if (y0 < r.y
28202 ? y0 + row->phys_height > r.y
28203 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28204 {
28205 if (first_overlapping_row == NULL)
28206 first_overlapping_row = row;
28207 last_overlapping_row = row;
28208 }
28209 }
28210
28211 if (y1 >= yb)
28212 break;
28213 }
28214
28215 /* Display the mode line if there is one. */
28216 if (WINDOW_WANTS_MODELINE_P (w)
28217 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28218 row->enabled_p)
28219 && row->y < r.y + r.height)
28220 {
28221 if (expose_line (w, row, &r))
28222 mouse_face_overwritten_p = 1;
28223 }
28224
28225 if (!w->pseudo_window_p)
28226 {
28227 /* Fix the display of overlapping rows. */
28228 if (first_overlapping_row)
28229 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28230 fr);
28231
28232 /* Draw border between windows. */
28233 x_draw_vertical_border (w);
28234
28235 /* Turn the cursor on again. */
28236 if (cursor_cleared_p
28237 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28238 update_window_cursor (w, 1);
28239 }
28240 }
28241
28242 return mouse_face_overwritten_p;
28243 }
28244
28245
28246
28247 /* Redraw (parts) of all windows in the window tree rooted at W that
28248 intersect R. R contains frame pixel coordinates. Value is
28249 non-zero if the exposure overwrites mouse-face. */
28250
28251 static int
28252 expose_window_tree (struct window *w, XRectangle *r)
28253 {
28254 struct frame *f = XFRAME (w->frame);
28255 int mouse_face_overwritten_p = 0;
28256
28257 while (w && !FRAME_GARBAGED_P (f))
28258 {
28259 if (!NILP (w->hchild))
28260 mouse_face_overwritten_p
28261 |= expose_window_tree (XWINDOW (w->hchild), r);
28262 else if (!NILP (w->vchild))
28263 mouse_face_overwritten_p
28264 |= expose_window_tree (XWINDOW (w->vchild), r);
28265 else
28266 mouse_face_overwritten_p |= expose_window (w, r);
28267
28268 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28269 }
28270
28271 return mouse_face_overwritten_p;
28272 }
28273
28274
28275 /* EXPORT:
28276 Redisplay an exposed area of frame F. X and Y are the upper-left
28277 corner of the exposed rectangle. W and H are width and height of
28278 the exposed area. All are pixel values. W or H zero means redraw
28279 the entire frame. */
28280
28281 void
28282 expose_frame (struct frame *f, int x, int y, int w, int h)
28283 {
28284 XRectangle r;
28285 int mouse_face_overwritten_p = 0;
28286
28287 TRACE ((stderr, "expose_frame "));
28288
28289 /* No need to redraw if frame will be redrawn soon. */
28290 if (FRAME_GARBAGED_P (f))
28291 {
28292 TRACE ((stderr, " garbaged\n"));
28293 return;
28294 }
28295
28296 /* If basic faces haven't been realized yet, there is no point in
28297 trying to redraw anything. This can happen when we get an expose
28298 event while Emacs is starting, e.g. by moving another window. */
28299 if (FRAME_FACE_CACHE (f) == NULL
28300 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28301 {
28302 TRACE ((stderr, " no faces\n"));
28303 return;
28304 }
28305
28306 if (w == 0 || h == 0)
28307 {
28308 r.x = r.y = 0;
28309 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28310 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28311 }
28312 else
28313 {
28314 r.x = x;
28315 r.y = y;
28316 r.width = w;
28317 r.height = h;
28318 }
28319
28320 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28321 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28322
28323 if (WINDOWP (f->tool_bar_window))
28324 mouse_face_overwritten_p
28325 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28326
28327 #ifdef HAVE_X_WINDOWS
28328 #ifndef MSDOS
28329 #ifndef USE_X_TOOLKIT
28330 if (WINDOWP (f->menu_bar_window))
28331 mouse_face_overwritten_p
28332 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28333 #endif /* not USE_X_TOOLKIT */
28334 #endif
28335 #endif
28336
28337 /* Some window managers support a focus-follows-mouse style with
28338 delayed raising of frames. Imagine a partially obscured frame,
28339 and moving the mouse into partially obscured mouse-face on that
28340 frame. The visible part of the mouse-face will be highlighted,
28341 then the WM raises the obscured frame. With at least one WM, KDE
28342 2.1, Emacs is not getting any event for the raising of the frame
28343 (even tried with SubstructureRedirectMask), only Expose events.
28344 These expose events will draw text normally, i.e. not
28345 highlighted. Which means we must redo the highlight here.
28346 Subsume it under ``we love X''. --gerd 2001-08-15 */
28347 /* Included in Windows version because Windows most likely does not
28348 do the right thing if any third party tool offers
28349 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28350 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28351 {
28352 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28353 if (f == hlinfo->mouse_face_mouse_frame)
28354 {
28355 int mouse_x = hlinfo->mouse_face_mouse_x;
28356 int mouse_y = hlinfo->mouse_face_mouse_y;
28357 clear_mouse_face (hlinfo);
28358 note_mouse_highlight (f, mouse_x, mouse_y);
28359 }
28360 }
28361 }
28362
28363
28364 /* EXPORT:
28365 Determine the intersection of two rectangles R1 and R2. Return
28366 the intersection in *RESULT. Value is non-zero if RESULT is not
28367 empty. */
28368
28369 int
28370 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28371 {
28372 XRectangle *left, *right;
28373 XRectangle *upper, *lower;
28374 int intersection_p = 0;
28375
28376 /* Rearrange so that R1 is the left-most rectangle. */
28377 if (r1->x < r2->x)
28378 left = r1, right = r2;
28379 else
28380 left = r2, right = r1;
28381
28382 /* X0 of the intersection is right.x0, if this is inside R1,
28383 otherwise there is no intersection. */
28384 if (right->x <= left->x + left->width)
28385 {
28386 result->x = right->x;
28387
28388 /* The right end of the intersection is the minimum of
28389 the right ends of left and right. */
28390 result->width = (min (left->x + left->width, right->x + right->width)
28391 - result->x);
28392
28393 /* Same game for Y. */
28394 if (r1->y < r2->y)
28395 upper = r1, lower = r2;
28396 else
28397 upper = r2, lower = r1;
28398
28399 /* The upper end of the intersection is lower.y0, if this is inside
28400 of upper. Otherwise, there is no intersection. */
28401 if (lower->y <= upper->y + upper->height)
28402 {
28403 result->y = lower->y;
28404
28405 /* The lower end of the intersection is the minimum of the lower
28406 ends of upper and lower. */
28407 result->height = (min (lower->y + lower->height,
28408 upper->y + upper->height)
28409 - result->y);
28410 intersection_p = 1;
28411 }
28412 }
28413
28414 return intersection_p;
28415 }
28416
28417 #endif /* HAVE_WINDOW_SYSTEM */
28418
28419 \f
28420 /***********************************************************************
28421 Initialization
28422 ***********************************************************************/
28423
28424 void
28425 syms_of_xdisp (void)
28426 {
28427 Vwith_echo_area_save_vector = Qnil;
28428 staticpro (&Vwith_echo_area_save_vector);
28429
28430 Vmessage_stack = Qnil;
28431 staticpro (&Vmessage_stack);
28432
28433 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28434
28435 message_dolog_marker1 = Fmake_marker ();
28436 staticpro (&message_dolog_marker1);
28437 message_dolog_marker2 = Fmake_marker ();
28438 staticpro (&message_dolog_marker2);
28439 message_dolog_marker3 = Fmake_marker ();
28440 staticpro (&message_dolog_marker3);
28441
28442 #ifdef GLYPH_DEBUG
28443 defsubr (&Sdump_frame_glyph_matrix);
28444 defsubr (&Sdump_glyph_matrix);
28445 defsubr (&Sdump_glyph_row);
28446 defsubr (&Sdump_tool_bar_row);
28447 defsubr (&Strace_redisplay);
28448 defsubr (&Strace_to_stderr);
28449 #endif
28450 #ifdef HAVE_WINDOW_SYSTEM
28451 defsubr (&Stool_bar_lines_needed);
28452 defsubr (&Slookup_image_map);
28453 #endif
28454 defsubr (&Sformat_mode_line);
28455 defsubr (&Sinvisible_p);
28456 defsubr (&Scurrent_bidi_paragraph_direction);
28457
28458 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28459 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28460 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28461 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28462 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28463 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28464 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28465 DEFSYM (Qeval, "eval");
28466 DEFSYM (QCdata, ":data");
28467 DEFSYM (Qdisplay, "display");
28468 DEFSYM (Qspace_width, "space-width");
28469 DEFSYM (Qraise, "raise");
28470 DEFSYM (Qslice, "slice");
28471 DEFSYM (Qspace, "space");
28472 DEFSYM (Qmargin, "margin");
28473 DEFSYM (Qpointer, "pointer");
28474 DEFSYM (Qleft_margin, "left-margin");
28475 DEFSYM (Qright_margin, "right-margin");
28476 DEFSYM (Qcenter, "center");
28477 DEFSYM (Qline_height, "line-height");
28478 DEFSYM (QCalign_to, ":align-to");
28479 DEFSYM (QCrelative_width, ":relative-width");
28480 DEFSYM (QCrelative_height, ":relative-height");
28481 DEFSYM (QCeval, ":eval");
28482 DEFSYM (QCpropertize, ":propertize");
28483 DEFSYM (QCfile, ":file");
28484 DEFSYM (Qfontified, "fontified");
28485 DEFSYM (Qfontification_functions, "fontification-functions");
28486 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28487 DEFSYM (Qescape_glyph, "escape-glyph");
28488 DEFSYM (Qnobreak_space, "nobreak-space");
28489 DEFSYM (Qimage, "image");
28490 DEFSYM (Qtext, "text");
28491 DEFSYM (Qboth, "both");
28492 DEFSYM (Qboth_horiz, "both-horiz");
28493 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28494 DEFSYM (QCmap, ":map");
28495 DEFSYM (QCpointer, ":pointer");
28496 DEFSYM (Qrect, "rect");
28497 DEFSYM (Qcircle, "circle");
28498 DEFSYM (Qpoly, "poly");
28499 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28500 DEFSYM (Qgrow_only, "grow-only");
28501 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28502 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28503 DEFSYM (Qposition, "position");
28504 DEFSYM (Qbuffer_position, "buffer-position");
28505 DEFSYM (Qobject, "object");
28506 DEFSYM (Qbar, "bar");
28507 DEFSYM (Qhbar, "hbar");
28508 DEFSYM (Qbox, "box");
28509 DEFSYM (Qhollow, "hollow");
28510 DEFSYM (Qhand, "hand");
28511 DEFSYM (Qarrow, "arrow");
28512 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28513
28514 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28515 Fcons (intern_c_string ("void-variable"), Qnil)),
28516 Qnil);
28517 staticpro (&list_of_error);
28518
28519 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28520 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28521 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28522 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28523
28524 echo_buffer[0] = echo_buffer[1] = Qnil;
28525 staticpro (&echo_buffer[0]);
28526 staticpro (&echo_buffer[1]);
28527
28528 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28529 staticpro (&echo_area_buffer[0]);
28530 staticpro (&echo_area_buffer[1]);
28531
28532 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28533 staticpro (&Vmessages_buffer_name);
28534
28535 mode_line_proptrans_alist = Qnil;
28536 staticpro (&mode_line_proptrans_alist);
28537 mode_line_string_list = Qnil;
28538 staticpro (&mode_line_string_list);
28539 mode_line_string_face = Qnil;
28540 staticpro (&mode_line_string_face);
28541 mode_line_string_face_prop = Qnil;
28542 staticpro (&mode_line_string_face_prop);
28543 Vmode_line_unwind_vector = Qnil;
28544 staticpro (&Vmode_line_unwind_vector);
28545
28546 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28547
28548 help_echo_string = Qnil;
28549 staticpro (&help_echo_string);
28550 help_echo_object = Qnil;
28551 staticpro (&help_echo_object);
28552 help_echo_window = Qnil;
28553 staticpro (&help_echo_window);
28554 previous_help_echo_string = Qnil;
28555 staticpro (&previous_help_echo_string);
28556 help_echo_pos = -1;
28557
28558 DEFSYM (Qright_to_left, "right-to-left");
28559 DEFSYM (Qleft_to_right, "left-to-right");
28560
28561 #ifdef HAVE_WINDOW_SYSTEM
28562 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28563 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28564 For example, if a block cursor is over a tab, it will be drawn as
28565 wide as that tab on the display. */);
28566 x_stretch_cursor_p = 0;
28567 #endif
28568
28569 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28570 doc: /* Non-nil means highlight trailing whitespace.
28571 The face used for trailing whitespace is `trailing-whitespace'. */);
28572 Vshow_trailing_whitespace = Qnil;
28573
28574 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28575 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28576 If the value is t, Emacs highlights non-ASCII chars which have the
28577 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28578 or `escape-glyph' face respectively.
28579
28580 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28581 U+2011 (non-breaking hyphen) are affected.
28582
28583 Any other non-nil value means to display these characters as a escape
28584 glyph followed by an ordinary space or hyphen.
28585
28586 A value of nil means no special handling of these characters. */);
28587 Vnobreak_char_display = Qt;
28588
28589 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28590 doc: /* The pointer shape to show in void text areas.
28591 A value of nil means to show the text pointer. Other options are `arrow',
28592 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28593 Vvoid_text_area_pointer = Qarrow;
28594
28595 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28596 doc: /* Non-nil means don't actually do any redisplay.
28597 This is used for internal purposes. */);
28598 Vinhibit_redisplay = Qnil;
28599
28600 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28601 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28602 Vglobal_mode_string = Qnil;
28603
28604 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28605 doc: /* Marker for where to display an arrow on top of the buffer text.
28606 This must be the beginning of a line in order to work.
28607 See also `overlay-arrow-string'. */);
28608 Voverlay_arrow_position = Qnil;
28609
28610 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28611 doc: /* String to display as an arrow in non-window frames.
28612 See also `overlay-arrow-position'. */);
28613 Voverlay_arrow_string = make_pure_c_string ("=>");
28614
28615 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28616 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28617 The symbols on this list are examined during redisplay to determine
28618 where to display overlay arrows. */);
28619 Voverlay_arrow_variable_list
28620 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28621
28622 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28623 doc: /* The number of lines to try scrolling a window by when point moves out.
28624 If that fails to bring point back on frame, point is centered instead.
28625 If this is zero, point is always centered after it moves off frame.
28626 If you want scrolling to always be a line at a time, you should set
28627 `scroll-conservatively' to a large value rather than set this to 1. */);
28628
28629 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28630 doc: /* Scroll up to this many lines, to bring point back on screen.
28631 If point moves off-screen, redisplay will scroll by up to
28632 `scroll-conservatively' lines in order to bring point just barely
28633 onto the screen again. If that cannot be done, then redisplay
28634 recenters point as usual.
28635
28636 If the value is greater than 100, redisplay will never recenter point,
28637 but will always scroll just enough text to bring point into view, even
28638 if you move far away.
28639
28640 A value of zero means always recenter point if it moves off screen. */);
28641 scroll_conservatively = 0;
28642
28643 DEFVAR_INT ("scroll-margin", scroll_margin,
28644 doc: /* Number of lines of margin at the top and bottom of a window.
28645 Recenter the window whenever point gets within this many lines
28646 of the top or bottom of the window. */);
28647 scroll_margin = 0;
28648
28649 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28650 doc: /* Pixels per inch value for non-window system displays.
28651 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28652 Vdisplay_pixels_per_inch = make_float (72.0);
28653
28654 #ifdef GLYPH_DEBUG
28655 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28656 #endif
28657
28658 DEFVAR_LISP ("truncate-partial-width-windows",
28659 Vtruncate_partial_width_windows,
28660 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28661 For an integer value, truncate lines in each window narrower than the
28662 full frame width, provided the window width is less than that integer;
28663 otherwise, respect the value of `truncate-lines'.
28664
28665 For any other non-nil value, truncate lines in all windows that do
28666 not span the full frame width.
28667
28668 A value of nil means to respect the value of `truncate-lines'.
28669
28670 If `word-wrap' is enabled, you might want to reduce this. */);
28671 Vtruncate_partial_width_windows = make_number (50);
28672
28673 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28674 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28675 Any other value means to use the appropriate face, `mode-line',
28676 `header-line', or `menu' respectively. */);
28677 mode_line_inverse_video = 1;
28678
28679 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28680 doc: /* Maximum buffer size for which line number should be displayed.
28681 If the buffer is bigger than this, the line number does not appear
28682 in the mode line. A value of nil means no limit. */);
28683 Vline_number_display_limit = Qnil;
28684
28685 DEFVAR_INT ("line-number-display-limit-width",
28686 line_number_display_limit_width,
28687 doc: /* Maximum line width (in characters) for line number display.
28688 If the average length of the lines near point is bigger than this, then the
28689 line number may be omitted from the mode line. */);
28690 line_number_display_limit_width = 200;
28691
28692 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28693 doc: /* Non-nil means highlight region even in nonselected windows. */);
28694 highlight_nonselected_windows = 0;
28695
28696 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28697 doc: /* Non-nil if more than one frame is visible on this display.
28698 Minibuffer-only frames don't count, but iconified frames do.
28699 This variable is not guaranteed to be accurate except while processing
28700 `frame-title-format' and `icon-title-format'. */);
28701
28702 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28703 doc: /* Template for displaying the title bar of visible frames.
28704 \(Assuming the window manager supports this feature.)
28705
28706 This variable has the same structure as `mode-line-format', except that
28707 the %c and %l constructs are ignored. It is used only on frames for
28708 which no explicit name has been set \(see `modify-frame-parameters'). */);
28709
28710 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28711 doc: /* Template for displaying the title bar of an iconified frame.
28712 \(Assuming the window manager supports this feature.)
28713 This variable has the same structure as `mode-line-format' (which see),
28714 and is used only on frames for which no explicit name has been set
28715 \(see `modify-frame-parameters'). */);
28716 Vicon_title_format
28717 = Vframe_title_format
28718 = pure_cons (intern_c_string ("multiple-frames"),
28719 pure_cons (make_pure_c_string ("%b"),
28720 pure_cons (pure_cons (empty_unibyte_string,
28721 pure_cons (intern_c_string ("invocation-name"),
28722 pure_cons (make_pure_c_string ("@"),
28723 pure_cons (intern_c_string ("system-name"),
28724 Qnil)))),
28725 Qnil)));
28726
28727 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28728 doc: /* Maximum number of lines to keep in the message log buffer.
28729 If nil, disable message logging. If t, log messages but don't truncate
28730 the buffer when it becomes large. */);
28731 Vmessage_log_max = make_number (100);
28732
28733 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28734 doc: /* Functions called before redisplay, if window sizes have changed.
28735 The value should be a list of functions that take one argument.
28736 Just before redisplay, for each frame, if any of its windows have changed
28737 size since the last redisplay, or have been split or deleted,
28738 all the functions in the list are called, with the frame as argument. */);
28739 Vwindow_size_change_functions = Qnil;
28740
28741 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28742 doc: /* List of functions to call before redisplaying a window with scrolling.
28743 Each function is called with two arguments, the window and its new
28744 display-start position. Note that these functions are also called by
28745 `set-window-buffer'. Also note that the value of `window-end' is not
28746 valid when these functions are called.
28747
28748 Warning: Do not use this feature to alter the way the window
28749 is scrolled. It is not designed for that, and such use probably won't
28750 work. */);
28751 Vwindow_scroll_functions = Qnil;
28752
28753 DEFVAR_LISP ("window-text-change-functions",
28754 Vwindow_text_change_functions,
28755 doc: /* Functions to call in redisplay when text in the window might change. */);
28756 Vwindow_text_change_functions = Qnil;
28757
28758 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28759 doc: /* Functions called when redisplay of a window reaches the end trigger.
28760 Each function is called with two arguments, the window and the end trigger value.
28761 See `set-window-redisplay-end-trigger'. */);
28762 Vredisplay_end_trigger_functions = Qnil;
28763
28764 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28765 doc: /* Non-nil means autoselect window with mouse pointer.
28766 If nil, do not autoselect windows.
28767 A positive number means delay autoselection by that many seconds: a
28768 window is autoselected only after the mouse has remained in that
28769 window for the duration of the delay.
28770 A negative number has a similar effect, but causes windows to be
28771 autoselected only after the mouse has stopped moving. \(Because of
28772 the way Emacs compares mouse events, you will occasionally wait twice
28773 that time before the window gets selected.\)
28774 Any other value means to autoselect window instantaneously when the
28775 mouse pointer enters it.
28776
28777 Autoselection selects the minibuffer only if it is active, and never
28778 unselects the minibuffer if it is active.
28779
28780 When customizing this variable make sure that the actual value of
28781 `focus-follows-mouse' matches the behavior of your window manager. */);
28782 Vmouse_autoselect_window = Qnil;
28783
28784 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28785 doc: /* Non-nil means automatically resize tool-bars.
28786 This dynamically changes the tool-bar's height to the minimum height
28787 that is needed to make all tool-bar items visible.
28788 If value is `grow-only', the tool-bar's height is only increased
28789 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28790 Vauto_resize_tool_bars = Qt;
28791
28792 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28793 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28794 auto_raise_tool_bar_buttons_p = 1;
28795
28796 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28797 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28798 make_cursor_line_fully_visible_p = 1;
28799
28800 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28801 doc: /* Border below tool-bar in pixels.
28802 If an integer, use it as the height of the border.
28803 If it is one of `internal-border-width' or `border-width', use the
28804 value of the corresponding frame parameter.
28805 Otherwise, no border is added below the tool-bar. */);
28806 Vtool_bar_border = Qinternal_border_width;
28807
28808 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28809 doc: /* Margin around tool-bar buttons in pixels.
28810 If an integer, use that for both horizontal and vertical margins.
28811 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28812 HORZ specifying the horizontal margin, and VERT specifying the
28813 vertical margin. */);
28814 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28815
28816 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28817 doc: /* Relief thickness of tool-bar buttons. */);
28818 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28819
28820 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28821 doc: /* Tool bar style to use.
28822 It can be one of
28823 image - show images only
28824 text - show text only
28825 both - show both, text below image
28826 both-horiz - show text to the right of the image
28827 text-image-horiz - show text to the left of the image
28828 any other - use system default or image if no system default.
28829
28830 This variable only affects the GTK+ toolkit version of Emacs. */);
28831 Vtool_bar_style = Qnil;
28832
28833 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28834 doc: /* Maximum number of characters a label can have to be shown.
28835 The tool bar style must also show labels for this to have any effect, see
28836 `tool-bar-style'. */);
28837 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28838
28839 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28840 doc: /* List of functions to call to fontify regions of text.
28841 Each function is called with one argument POS. Functions must
28842 fontify a region starting at POS in the current buffer, and give
28843 fontified regions the property `fontified'. */);
28844 Vfontification_functions = Qnil;
28845 Fmake_variable_buffer_local (Qfontification_functions);
28846
28847 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28848 unibyte_display_via_language_environment,
28849 doc: /* Non-nil means display unibyte text according to language environment.
28850 Specifically, this means that raw bytes in the range 160-255 decimal
28851 are displayed by converting them to the equivalent multibyte characters
28852 according to the current language environment. As a result, they are
28853 displayed according to the current fontset.
28854
28855 Note that this variable affects only how these bytes are displayed,
28856 but does not change the fact they are interpreted as raw bytes. */);
28857 unibyte_display_via_language_environment = 0;
28858
28859 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28860 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
28861 If a float, it specifies a fraction of the mini-window frame's height.
28862 If an integer, it specifies a number of lines. */);
28863 Vmax_mini_window_height = make_float (0.25);
28864
28865 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28866 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28867 A value of nil means don't automatically resize mini-windows.
28868 A value of t means resize them to fit the text displayed in them.
28869 A value of `grow-only', the default, means let mini-windows grow only;
28870 they return to their normal size when the minibuffer is closed, or the
28871 echo area becomes empty. */);
28872 Vresize_mini_windows = Qgrow_only;
28873
28874 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28875 doc: /* Alist specifying how to blink the cursor off.
28876 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28877 `cursor-type' frame-parameter or variable equals ON-STATE,
28878 comparing using `equal', Emacs uses OFF-STATE to specify
28879 how to blink it off. ON-STATE and OFF-STATE are values for
28880 the `cursor-type' frame parameter.
28881
28882 If a frame's ON-STATE has no entry in this list,
28883 the frame's other specifications determine how to blink the cursor off. */);
28884 Vblink_cursor_alist = Qnil;
28885
28886 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28887 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28888 If non-nil, windows are automatically scrolled horizontally to make
28889 point visible. */);
28890 automatic_hscrolling_p = 1;
28891 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28892
28893 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28894 doc: /* How many columns away from the window edge point is allowed to get
28895 before automatic hscrolling will horizontally scroll the window. */);
28896 hscroll_margin = 5;
28897
28898 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28899 doc: /* How many columns to scroll the window when point gets too close to the edge.
28900 When point is less than `hscroll-margin' columns from the window
28901 edge, automatic hscrolling will scroll the window by the amount of columns
28902 determined by this variable. If its value is a positive integer, scroll that
28903 many columns. If it's a positive floating-point number, it specifies the
28904 fraction of the window's width to scroll. If it's nil or zero, point will be
28905 centered horizontally after the scroll. Any other value, including negative
28906 numbers, are treated as if the value were zero.
28907
28908 Automatic hscrolling always moves point outside the scroll margin, so if
28909 point was more than scroll step columns inside the margin, the window will
28910 scroll more than the value given by the scroll step.
28911
28912 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28913 and `scroll-right' overrides this variable's effect. */);
28914 Vhscroll_step = make_number (0);
28915
28916 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28917 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28918 Bind this around calls to `message' to let it take effect. */);
28919 message_truncate_lines = 0;
28920
28921 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28922 doc: /* Normal hook run to update the menu bar definitions.
28923 Redisplay runs this hook before it redisplays the menu bar.
28924 This is used to update submenus such as Buffers,
28925 whose contents depend on various data. */);
28926 Vmenu_bar_update_hook = Qnil;
28927
28928 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28929 doc: /* Frame for which we are updating a menu.
28930 The enable predicate for a menu binding should check this variable. */);
28931 Vmenu_updating_frame = Qnil;
28932
28933 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28934 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28935 inhibit_menubar_update = 0;
28936
28937 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28938 doc: /* Prefix prepended to all continuation lines at display time.
28939 The value may be a string, an image, or a stretch-glyph; it is
28940 interpreted in the same way as the value of a `display' text property.
28941
28942 This variable is overridden by any `wrap-prefix' text or overlay
28943 property.
28944
28945 To add a prefix to non-continuation lines, use `line-prefix'. */);
28946 Vwrap_prefix = Qnil;
28947 DEFSYM (Qwrap_prefix, "wrap-prefix");
28948 Fmake_variable_buffer_local (Qwrap_prefix);
28949
28950 DEFVAR_LISP ("line-prefix", Vline_prefix,
28951 doc: /* Prefix prepended to all non-continuation lines at display time.
28952 The value may be a string, an image, or a stretch-glyph; it is
28953 interpreted in the same way as the value of a `display' text property.
28954
28955 This variable is overridden by any `line-prefix' text or overlay
28956 property.
28957
28958 To add a prefix to continuation lines, use `wrap-prefix'. */);
28959 Vline_prefix = Qnil;
28960 DEFSYM (Qline_prefix, "line-prefix");
28961 Fmake_variable_buffer_local (Qline_prefix);
28962
28963 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28964 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28965 inhibit_eval_during_redisplay = 0;
28966
28967 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28968 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28969 inhibit_free_realized_faces = 0;
28970
28971 #ifdef GLYPH_DEBUG
28972 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28973 doc: /* Inhibit try_window_id display optimization. */);
28974 inhibit_try_window_id = 0;
28975
28976 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28977 doc: /* Inhibit try_window_reusing display optimization. */);
28978 inhibit_try_window_reusing = 0;
28979
28980 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28981 doc: /* Inhibit try_cursor_movement display optimization. */);
28982 inhibit_try_cursor_movement = 0;
28983 #endif /* GLYPH_DEBUG */
28984
28985 DEFVAR_INT ("overline-margin", overline_margin,
28986 doc: /* Space between overline and text, in pixels.
28987 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28988 margin to the character height. */);
28989 overline_margin = 2;
28990
28991 DEFVAR_INT ("underline-minimum-offset",
28992 underline_minimum_offset,
28993 doc: /* Minimum distance between baseline and underline.
28994 This can improve legibility of underlined text at small font sizes,
28995 particularly when using variable `x-use-underline-position-properties'
28996 with fonts that specify an UNDERLINE_POSITION relatively close to the
28997 baseline. The default value is 1. */);
28998 underline_minimum_offset = 1;
28999
29000 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29001 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29002 This feature only works when on a window system that can change
29003 cursor shapes. */);
29004 display_hourglass_p = 1;
29005
29006 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29007 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29008 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29009
29010 hourglass_atimer = NULL;
29011 hourglass_shown_p = 0;
29012
29013 DEFSYM (Qglyphless_char, "glyphless-char");
29014 DEFSYM (Qhex_code, "hex-code");
29015 DEFSYM (Qempty_box, "empty-box");
29016 DEFSYM (Qthin_space, "thin-space");
29017 DEFSYM (Qzero_width, "zero-width");
29018
29019 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29020 /* Intern this now in case it isn't already done.
29021 Setting this variable twice is harmless.
29022 But don't staticpro it here--that is done in alloc.c. */
29023 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29024 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29025
29026 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29027 doc: /* Char-table defining glyphless characters.
29028 Each element, if non-nil, should be one of the following:
29029 an ASCII acronym string: display this string in a box
29030 `hex-code': display the hexadecimal code of a character in a box
29031 `empty-box': display as an empty box
29032 `thin-space': display as 1-pixel width space
29033 `zero-width': don't display
29034 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29035 display method for graphical terminals and text terminals respectively.
29036 GRAPHICAL and TEXT should each have one of the values listed above.
29037
29038 The char-table has one extra slot to control the display of a character for
29039 which no font is found. This slot only takes effect on graphical terminals.
29040 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29041 `thin-space'. The default is `empty-box'. */);
29042 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29043 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29044 Qempty_box);
29045 }
29046
29047
29048 /* Initialize this module when Emacs starts. */
29049
29050 void
29051 init_xdisp (void)
29052 {
29053 current_header_line_height = current_mode_line_height = -1;
29054
29055 CHARPOS (this_line_start_pos) = 0;
29056
29057 if (!noninteractive)
29058 {
29059 struct window *m = XWINDOW (minibuf_window);
29060 Lisp_Object frame = m->frame;
29061 struct frame *f = XFRAME (frame);
29062 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29063 struct window *r = XWINDOW (root);
29064 int i;
29065
29066 echo_area_window = minibuf_window;
29067
29068 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
29069 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29070 XSETFASTINT (r->total_cols, FRAME_COLS (f));
29071 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
29072 XSETFASTINT (m->total_lines, 1);
29073 XSETFASTINT (m->total_cols, FRAME_COLS (f));
29074
29075 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29076 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29077 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29078
29079 /* The default ellipsis glyphs `...'. */
29080 for (i = 0; i < 3; ++i)
29081 default_invis_vector[i] = make_number ('.');
29082 }
29083
29084 {
29085 /* Allocate the buffer for frame titles.
29086 Also used for `format-mode-line'. */
29087 int size = 100;
29088 mode_line_noprop_buf = xmalloc (size);
29089 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29090 mode_line_noprop_ptr = mode_line_noprop_buf;
29091 mode_line_target = MODE_LINE_DISPLAY;
29092 }
29093
29094 help_echo_showing_p = 0;
29095 }
29096
29097 /* Since w32 does not support atimers, it defines its own implementation of
29098 the following three functions in w32fns.c. */
29099 #ifndef WINDOWSNT
29100
29101 /* Platform-independent portion of hourglass implementation. */
29102
29103 /* Cancel a currently active hourglass timer, and start a new one. */
29104 void
29105 start_hourglass (void)
29106 {
29107 #if defined (HAVE_WINDOW_SYSTEM)
29108 EMACS_TIME delay;
29109
29110 cancel_hourglass ();
29111
29112 if (INTEGERP (Vhourglass_delay)
29113 && XINT (Vhourglass_delay) > 0)
29114 EMACS_SET_SECS_NSECS (delay,
29115 min (XINT (Vhourglass_delay), TYPE_MAXIMUM (time_t)),
29116 0);
29117 else if (FLOATP (Vhourglass_delay)
29118 && XFLOAT_DATA (Vhourglass_delay) > 0)
29119 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29120 else
29121 EMACS_SET_SECS_NSECS (delay, DEFAULT_HOURGLASS_DELAY, 0);
29122
29123 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29124 show_hourglass, NULL);
29125 #endif
29126 }
29127
29128
29129 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29130 shown. */
29131 void
29132 cancel_hourglass (void)
29133 {
29134 #if defined (HAVE_WINDOW_SYSTEM)
29135 if (hourglass_atimer)
29136 {
29137 cancel_atimer (hourglass_atimer);
29138 hourglass_atimer = NULL;
29139 }
29140
29141 if (hourglass_shown_p)
29142 hide_hourglass ();
29143 #endif
29144 }
29145 #endif /* ! WINDOWSNT */