Merge from trunk.
[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 "buffer.h"
285 #include "character.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 #if GLYPH_DEBUG
631
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG != 0. */
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
755 /* If a string, XTread_socket generates an event to display that string.
756 (The display is done in read_char.) */
757
758 Lisp_Object help_echo_string;
759 Lisp_Object help_echo_window;
760 Lisp_Object help_echo_object;
761 ptrdiff_t help_echo_pos;
762
763 /* Temporary variable for XTread_socket. */
764
765 Lisp_Object previous_help_echo_string;
766
767 /* Platform-independent portion of hourglass implementation. */
768
769 /* Non-zero means an hourglass cursor is currently shown. */
770 int hourglass_shown_p;
771
772 /* If non-null, an asynchronous timer that, when it expires, displays
773 an hourglass cursor on all frames. */
774 struct atimer *hourglass_atimer;
775
776 /* Name of the face used to display glyphless characters. */
777 Lisp_Object Qglyphless_char;
778
779 /* Symbol for the purpose of Vglyphless_char_display. */
780 static Lisp_Object Qglyphless_char_display;
781
782 /* Method symbols for Vglyphless_char_display. */
783 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
784
785 /* Default pixel width of `thin-space' display method. */
786 #define THIN_SPACE_WIDTH 1
787
788 /* Default number of seconds to wait before displaying an hourglass
789 cursor. */
790 #define DEFAULT_HOURGLASS_DELAY 1
791
792 \f
793 /* Function prototypes. */
794
795 static void setup_for_ellipsis (struct it *, int);
796 static void set_iterator_to_next (struct it *, int);
797 static void mark_window_display_accurate_1 (struct window *, int);
798 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
799 static int display_prop_string_p (Lisp_Object, Lisp_Object);
800 static int cursor_row_p (struct glyph_row *);
801 static int redisplay_mode_lines (Lisp_Object, int);
802 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
803
804 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
805
806 static void handle_line_prefix (struct it *);
807
808 static void pint2str (char *, int, ptrdiff_t);
809 static void pint2hrstr (char *, int, ptrdiff_t);
810 static struct text_pos run_window_scroll_functions (Lisp_Object,
811 struct text_pos);
812 static void reconsider_clip_changes (struct window *, struct buffer *);
813 static int text_outside_line_unchanged_p (struct window *,
814 ptrdiff_t, ptrdiff_t);
815 static void store_mode_line_noprop_char (char);
816 static int store_mode_line_noprop (const char *, int, int);
817 static void handle_stop (struct it *);
818 static void handle_stop_backwards (struct it *, ptrdiff_t);
819 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
820 static void ensure_echo_area_buffers (void);
821 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
822 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
823 static int with_echo_area_buffer (struct window *, int,
824 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
825 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
826 static void clear_garbaged_frames (void);
827 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
828 static void pop_message (void);
829 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
830 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
831 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
832 static int display_echo_area (struct window *);
833 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
834 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
835 static Lisp_Object unwind_redisplay (Lisp_Object);
836 static int string_char_and_length (const unsigned char *, int *);
837 static struct text_pos display_prop_end (struct it *, Lisp_Object,
838 struct text_pos);
839 static int compute_window_start_on_continuation_line (struct window *);
840 static Lisp_Object safe_eval_handler (Lisp_Object);
841 static void insert_left_trunc_glyphs (struct it *);
842 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
843 Lisp_Object);
844 static void extend_face_to_end_of_line (struct it *);
845 static int append_space_for_newline (struct it *, int);
846 static int cursor_row_fully_visible_p (struct window *, int, int);
847 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
848 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
849 static int trailing_whitespace_p (ptrdiff_t);
850 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
851 static void push_it (struct it *, struct text_pos *);
852 static void iterate_out_of_display_property (struct it *);
853 static void pop_it (struct it *);
854 static void sync_frame_with_window_matrix_rows (struct window *);
855 static void select_frame_for_redisplay (Lisp_Object);
856 static void redisplay_internal (void);
857 static int echo_area_display (int);
858 static void redisplay_windows (Lisp_Object);
859 static void redisplay_window (Lisp_Object, int);
860 static Lisp_Object redisplay_window_error (Lisp_Object);
861 static Lisp_Object redisplay_window_0 (Lisp_Object);
862 static Lisp_Object redisplay_window_1 (Lisp_Object);
863 static int set_cursor_from_row (struct window *, struct glyph_row *,
864 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
865 int, int);
866 static int update_menu_bar (struct frame *, int, int);
867 static int try_window_reusing_current_matrix (struct window *);
868 static int try_window_id (struct window *);
869 static int display_line (struct it *);
870 static int display_mode_lines (struct window *);
871 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
872 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
873 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
874 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
875 static void display_menu_bar (struct window *);
876 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
877 ptrdiff_t *);
878 static int display_string (const char *, Lisp_Object, Lisp_Object,
879 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
880 static void compute_line_metrics (struct it *);
881 static void run_redisplay_end_trigger_hook (struct it *);
882 static int get_overlay_strings (struct it *, ptrdiff_t);
883 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
884 static void next_overlay_string (struct it *);
885 static void reseat (struct it *, struct text_pos, int);
886 static void reseat_1 (struct it *, struct text_pos, int);
887 static void back_to_previous_visible_line_start (struct it *);
888 void reseat_at_previous_visible_line_start (struct it *);
889 static void reseat_at_next_visible_line_start (struct it *, int);
890 static int next_element_from_ellipsis (struct it *);
891 static int next_element_from_display_vector (struct it *);
892 static int next_element_from_string (struct it *);
893 static int next_element_from_c_string (struct it *);
894 static int next_element_from_buffer (struct it *);
895 static int next_element_from_composition (struct it *);
896 static int next_element_from_image (struct it *);
897 static int next_element_from_stretch (struct it *);
898 static void load_overlay_strings (struct it *, ptrdiff_t);
899 static int init_from_display_pos (struct it *, struct window *,
900 struct display_pos *);
901 static void reseat_to_string (struct it *, const char *,
902 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
903 static int get_next_display_element (struct it *);
904 static enum move_it_result
905 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
906 enum move_operation_enum);
907 void move_it_vertically_backward (struct it *, int);
908 static void init_to_row_start (struct it *, struct window *,
909 struct glyph_row *);
910 static int init_to_row_end (struct it *, struct window *,
911 struct glyph_row *);
912 static void back_to_previous_line_start (struct it *);
913 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
914 static struct text_pos string_pos_nchars_ahead (struct text_pos,
915 Lisp_Object, ptrdiff_t);
916 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
917 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
918 static ptrdiff_t number_of_chars (const char *, int);
919 static void compute_stop_pos (struct it *);
920 static void compute_string_pos (struct text_pos *, struct text_pos,
921 Lisp_Object);
922 static int face_before_or_after_it_pos (struct it *, int);
923 static ptrdiff_t next_overlay_change (ptrdiff_t);
924 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
925 Lisp_Object, struct text_pos *, ptrdiff_t, int);
926 static int handle_single_display_spec (struct it *, Lisp_Object,
927 Lisp_Object, Lisp_Object,
928 struct text_pos *, ptrdiff_t, int, int);
929 static int underlying_face_id (struct it *);
930 static int in_ellipses_for_invisible_text_p (struct display_pos *,
931 struct window *);
932
933 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
934 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
935
936 #ifdef HAVE_WINDOW_SYSTEM
937
938 static void x_consider_frame_title (Lisp_Object);
939 static int tool_bar_lines_needed (struct frame *, int *);
940 static void update_tool_bar (struct frame *, int);
941 static void build_desired_tool_bar_string (struct frame *f);
942 static int redisplay_tool_bar (struct frame *);
943 static void display_tool_bar_line (struct it *, int);
944 static void notice_overwritten_cursor (struct window *,
945 enum glyph_row_area,
946 int, int, int, int);
947 static void append_stretch_glyph (struct it *, Lisp_Object,
948 int, int, int);
949
950
951 #endif /* HAVE_WINDOW_SYSTEM */
952
953 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
954 static int coords_in_mouse_face_p (struct window *, int, int);
955
956
957 \f
958 /***********************************************************************
959 Window display dimensions
960 ***********************************************************************/
961
962 /* Return the bottom boundary y-position for text lines in window W.
963 This is the first y position at which a line cannot start.
964 It is relative to the top of the window.
965
966 This is the height of W minus the height of a mode line, if any. */
967
968 int
969 window_text_bottom_y (struct window *w)
970 {
971 int height = WINDOW_TOTAL_HEIGHT (w);
972
973 if (WINDOW_WANTS_MODELINE_P (w))
974 height -= CURRENT_MODE_LINE_HEIGHT (w);
975 return height;
976 }
977
978 /* Return the pixel width of display area AREA of window W. AREA < 0
979 means return the total width of W, not including fringes to
980 the left and right of the window. */
981
982 int
983 window_box_width (struct window *w, int area)
984 {
985 int cols = XFASTINT (w->total_cols);
986 int pixels = 0;
987
988 if (!w->pseudo_window_p)
989 {
990 cols -= WINDOW_SCROLL_BAR_COLS (w);
991
992 if (area == TEXT_AREA)
993 {
994 if (INTEGERP (w->left_margin_cols))
995 cols -= XFASTINT (w->left_margin_cols);
996 if (INTEGERP (w->right_margin_cols))
997 cols -= XFASTINT (w->right_margin_cols);
998 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
999 }
1000 else if (area == LEFT_MARGIN_AREA)
1001 {
1002 cols = (INTEGERP (w->left_margin_cols)
1003 ? XFASTINT (w->left_margin_cols) : 0);
1004 pixels = 0;
1005 }
1006 else if (area == RIGHT_MARGIN_AREA)
1007 {
1008 cols = (INTEGERP (w->right_margin_cols)
1009 ? XFASTINT (w->right_margin_cols) : 0);
1010 pixels = 0;
1011 }
1012 }
1013
1014 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1015 }
1016
1017
1018 /* Return the pixel height of the display area of window W, not
1019 including mode lines of W, if any. */
1020
1021 int
1022 window_box_height (struct window *w)
1023 {
1024 struct frame *f = XFRAME (w->frame);
1025 int height = WINDOW_TOTAL_HEIGHT (w);
1026
1027 xassert (height >= 0);
1028
1029 /* Note: the code below that determines the mode-line/header-line
1030 height is essentially the same as that contained in the macro
1031 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1032 the appropriate glyph row has its `mode_line_p' flag set,
1033 and if it doesn't, uses estimate_mode_line_height instead. */
1034
1035 if (WINDOW_WANTS_MODELINE_P (w))
1036 {
1037 struct glyph_row *ml_row
1038 = (w->current_matrix && w->current_matrix->rows
1039 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1040 : 0);
1041 if (ml_row && ml_row->mode_line_p)
1042 height -= ml_row->height;
1043 else
1044 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1045 }
1046
1047 if (WINDOW_WANTS_HEADER_LINE_P (w))
1048 {
1049 struct glyph_row *hl_row
1050 = (w->current_matrix && w->current_matrix->rows
1051 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1052 : 0);
1053 if (hl_row && hl_row->mode_line_p)
1054 height -= hl_row->height;
1055 else
1056 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1057 }
1058
1059 /* With a very small font and a mode-line that's taller than
1060 default, we might end up with a negative height. */
1061 return max (0, height);
1062 }
1063
1064 /* Return the window-relative coordinate of the left edge of display
1065 area AREA of window W. AREA < 0 means return the left edge of the
1066 whole window, to the right of the left fringe of W. */
1067
1068 int
1069 window_box_left_offset (struct window *w, int area)
1070 {
1071 int x;
1072
1073 if (w->pseudo_window_p)
1074 return 0;
1075
1076 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1077
1078 if (area == TEXT_AREA)
1079 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1080 + window_box_width (w, LEFT_MARGIN_AREA));
1081 else if (area == RIGHT_MARGIN_AREA)
1082 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1083 + window_box_width (w, LEFT_MARGIN_AREA)
1084 + window_box_width (w, TEXT_AREA)
1085 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1086 ? 0
1087 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1088 else if (area == LEFT_MARGIN_AREA
1089 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1090 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1091
1092 return x;
1093 }
1094
1095
1096 /* Return the window-relative coordinate of the right edge of display
1097 area AREA of window W. AREA < 0 means return the right edge of the
1098 whole window, to the left of the right fringe of W. */
1099
1100 int
1101 window_box_right_offset (struct window *w, int area)
1102 {
1103 return window_box_left_offset (w, area) + window_box_width (w, area);
1104 }
1105
1106 /* Return the frame-relative coordinate of the left edge of display
1107 area AREA of window W. AREA < 0 means return the left edge of the
1108 whole window, to the right of the left fringe of W. */
1109
1110 int
1111 window_box_left (struct window *w, int area)
1112 {
1113 struct frame *f = XFRAME (w->frame);
1114 int x;
1115
1116 if (w->pseudo_window_p)
1117 return FRAME_INTERNAL_BORDER_WIDTH (f);
1118
1119 x = (WINDOW_LEFT_EDGE_X (w)
1120 + window_box_left_offset (w, area));
1121
1122 return x;
1123 }
1124
1125
1126 /* Return the frame-relative coordinate of the right edge of display
1127 area AREA of window W. AREA < 0 means return the right edge of the
1128 whole window, to the left of the right fringe of W. */
1129
1130 int
1131 window_box_right (struct window *w, int area)
1132 {
1133 return window_box_left (w, area) + window_box_width (w, area);
1134 }
1135
1136 /* Get the bounding box of the display area AREA of window W, without
1137 mode lines, in frame-relative coordinates. AREA < 0 means the
1138 whole window, not including the left and right fringes of
1139 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1140 coordinates of the upper-left corner of the box. Return in
1141 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1142
1143 void
1144 window_box (struct window *w, int area, int *box_x, int *box_y,
1145 int *box_width, int *box_height)
1146 {
1147 if (box_width)
1148 *box_width = window_box_width (w, area);
1149 if (box_height)
1150 *box_height = window_box_height (w);
1151 if (box_x)
1152 *box_x = window_box_left (w, area);
1153 if (box_y)
1154 {
1155 *box_y = WINDOW_TOP_EDGE_Y (w);
1156 if (WINDOW_WANTS_HEADER_LINE_P (w))
1157 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1158 }
1159 }
1160
1161
1162 /* Get the bounding box of the display area AREA of window W, without
1163 mode lines. AREA < 0 means the whole window, not including the
1164 left and right fringe of the window. Return in *TOP_LEFT_X
1165 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1166 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1167 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1168 box. */
1169
1170 static inline void
1171 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1172 int *bottom_right_x, int *bottom_right_y)
1173 {
1174 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1175 bottom_right_y);
1176 *bottom_right_x += *top_left_x;
1177 *bottom_right_y += *top_left_y;
1178 }
1179
1180
1181 \f
1182 /***********************************************************************
1183 Utilities
1184 ***********************************************************************/
1185
1186 /* Return the bottom y-position of the line the iterator IT is in.
1187 This can modify IT's settings. */
1188
1189 int
1190 line_bottom_y (struct it *it)
1191 {
1192 int line_height = it->max_ascent + it->max_descent;
1193 int line_top_y = it->current_y;
1194
1195 if (line_height == 0)
1196 {
1197 if (last_height)
1198 line_height = last_height;
1199 else if (IT_CHARPOS (*it) < ZV)
1200 {
1201 move_it_by_lines (it, 1);
1202 line_height = (it->max_ascent || it->max_descent
1203 ? it->max_ascent + it->max_descent
1204 : last_height);
1205 }
1206 else
1207 {
1208 struct glyph_row *row = it->glyph_row;
1209
1210 /* Use the default character height. */
1211 it->glyph_row = NULL;
1212 it->what = IT_CHARACTER;
1213 it->c = ' ';
1214 it->len = 1;
1215 PRODUCE_GLYPHS (it);
1216 line_height = it->ascent + it->descent;
1217 it->glyph_row = row;
1218 }
1219 }
1220
1221 return line_top_y + line_height;
1222 }
1223
1224 /* Subroutine of pos_visible_p below. Extracts a display string, if
1225 any, from the display spec given as its argument. */
1226 static Lisp_Object
1227 string_from_display_spec (Lisp_Object spec)
1228 {
1229 if (CONSP (spec))
1230 {
1231 while (CONSP (spec))
1232 {
1233 if (STRINGP (XCAR (spec)))
1234 return XCAR (spec);
1235 spec = XCDR (spec);
1236 }
1237 }
1238 else if (VECTORP (spec))
1239 {
1240 ptrdiff_t i;
1241
1242 for (i = 0; i < ASIZE (spec); i++)
1243 {
1244 if (STRINGP (AREF (spec, i)))
1245 return AREF (spec, i);
1246 }
1247 return Qnil;
1248 }
1249
1250 return spec;
1251 }
1252
1253 /* Return 1 if position CHARPOS is visible in window W.
1254 CHARPOS < 0 means return info about WINDOW_END position.
1255 If visible, set *X and *Y to pixel coordinates of top left corner.
1256 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1257 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1258
1259 int
1260 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1261 int *rtop, int *rbot, int *rowh, int *vpos)
1262 {
1263 struct it it;
1264 void *itdata = bidi_shelve_cache ();
1265 struct text_pos top;
1266 int visible_p = 0;
1267 struct buffer *old_buffer = NULL;
1268
1269 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1270 return visible_p;
1271
1272 if (XBUFFER (w->buffer) != current_buffer)
1273 {
1274 old_buffer = current_buffer;
1275 set_buffer_internal_1 (XBUFFER (w->buffer));
1276 }
1277
1278 SET_TEXT_POS_FROM_MARKER (top, w->start);
1279 /* Scrolling a minibuffer window via scroll bar when the echo area
1280 shows long text sometimes resets the minibuffer contents behind
1281 our backs. */
1282 if (CHARPOS (top) > ZV)
1283 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1284
1285 /* Compute exact mode line heights. */
1286 if (WINDOW_WANTS_MODELINE_P (w))
1287 current_mode_line_height
1288 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1289 BVAR (current_buffer, mode_line_format));
1290
1291 if (WINDOW_WANTS_HEADER_LINE_P (w))
1292 current_header_line_height
1293 = display_mode_line (w, HEADER_LINE_FACE_ID,
1294 BVAR (current_buffer, header_line_format));
1295
1296 start_display (&it, w, top);
1297 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1298 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1299
1300 if (charpos >= 0
1301 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1302 && IT_CHARPOS (it) >= charpos)
1303 /* When scanning backwards under bidi iteration, move_it_to
1304 stops at or _before_ CHARPOS, because it stops at or to
1305 the _right_ of the character at CHARPOS. */
1306 || (it.bidi_p && it.bidi_it.scan_dir == -1
1307 && IT_CHARPOS (it) <= charpos)))
1308 {
1309 /* We have reached CHARPOS, or passed it. How the call to
1310 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1311 or covered by a display property, move_it_to stops at the end
1312 of the invisible text, to the right of CHARPOS. (ii) If
1313 CHARPOS is in a display vector, move_it_to stops on its last
1314 glyph. */
1315 int top_x = it.current_x;
1316 int top_y = it.current_y;
1317 /* Calling line_bottom_y may change it.method, it.position, etc. */
1318 enum it_method it_method = it.method;
1319 int bottom_y = (last_height = 0, line_bottom_y (&it));
1320 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1321
1322 if (top_y < window_top_y)
1323 visible_p = bottom_y > window_top_y;
1324 else if (top_y < it.last_visible_y)
1325 visible_p = 1;
1326 if (bottom_y >= it.last_visible_y
1327 && it.bidi_p && it.bidi_it.scan_dir == -1
1328 && IT_CHARPOS (it) < charpos)
1329 {
1330 /* When the last line of the window is scanned backwards
1331 under bidi iteration, we could be duped into thinking
1332 that we have passed CHARPOS, when in fact move_it_to
1333 simply stopped short of CHARPOS because it reached
1334 last_visible_y. To see if that's what happened, we call
1335 move_it_to again with a slightly larger vertical limit,
1336 and see if it actually moved vertically; if it did, we
1337 didn't really reach CHARPOS, which is beyond window end. */
1338 struct it save_it = it;
1339 /* Why 10? because we don't know how many canonical lines
1340 will the height of the next line(s) be. So we guess. */
1341 int ten_more_lines =
1342 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1343
1344 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1345 MOVE_TO_POS | MOVE_TO_Y);
1346 if (it.current_y > top_y)
1347 visible_p = 0;
1348
1349 it = save_it;
1350 }
1351 if (visible_p)
1352 {
1353 if (it_method == GET_FROM_DISPLAY_VECTOR)
1354 {
1355 /* We stopped on the last glyph of a display vector.
1356 Try and recompute. Hack alert! */
1357 if (charpos < 2 || top.charpos >= charpos)
1358 top_x = it.glyph_row->x;
1359 else
1360 {
1361 struct it it2;
1362 start_display (&it2, w, top);
1363 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1364 get_next_display_element (&it2);
1365 PRODUCE_GLYPHS (&it2);
1366 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1367 || it2.current_x > it2.last_visible_x)
1368 top_x = it.glyph_row->x;
1369 else
1370 {
1371 top_x = it2.current_x;
1372 top_y = it2.current_y;
1373 }
1374 }
1375 }
1376 else if (IT_CHARPOS (it) != charpos)
1377 {
1378 Lisp_Object cpos = make_number (charpos);
1379 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1380 Lisp_Object string = string_from_display_spec (spec);
1381 int newline_in_string = 0;
1382
1383 if (STRINGP (string))
1384 {
1385 const char *s = SSDATA (string);
1386 const char *e = s + SBYTES (string);
1387 while (s < e)
1388 {
1389 if (*s++ == '\n')
1390 {
1391 newline_in_string = 1;
1392 break;
1393 }
1394 }
1395 }
1396 /* The tricky code below is needed because there's a
1397 discrepancy between move_it_to and how we set cursor
1398 when the display line ends in a newline from a
1399 display string. move_it_to will stop _after_ such
1400 display strings, whereas set_cursor_from_row
1401 conspires with cursor_row_p to place the cursor on
1402 the first glyph produced from the display string. */
1403
1404 /* We have overshoot PT because it is covered by a
1405 display property whose value is a string. If the
1406 string includes embedded newlines, we are also in the
1407 wrong display line. Backtrack to the correct line,
1408 where the display string begins. */
1409 if (newline_in_string)
1410 {
1411 Lisp_Object startpos, endpos;
1412 EMACS_INT start, end;
1413 struct it it3;
1414 int it3_moved;
1415
1416 /* Find the first and the last buffer positions
1417 covered by the display string. */
1418 endpos =
1419 Fnext_single_char_property_change (cpos, Qdisplay,
1420 Qnil, Qnil);
1421 startpos =
1422 Fprevious_single_char_property_change (endpos, Qdisplay,
1423 Qnil, Qnil);
1424 start = XFASTINT (startpos);
1425 end = XFASTINT (endpos);
1426 /* Move to the last buffer position before the
1427 display property. */
1428 start_display (&it3, w, top);
1429 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1430 /* Move forward one more line if the position before
1431 the display string is a newline or if it is the
1432 rightmost character on a line that is
1433 continued or word-wrapped. */
1434 if (it3.method == GET_FROM_BUFFER
1435 && it3.c == '\n')
1436 move_it_by_lines (&it3, 1);
1437 else if (move_it_in_display_line_to (&it3, -1,
1438 it3.current_x
1439 + it3.pixel_width,
1440 MOVE_TO_X)
1441 == MOVE_LINE_CONTINUED)
1442 {
1443 move_it_by_lines (&it3, 1);
1444 /* When we are under word-wrap, the #$@%!
1445 move_it_by_lines moves 2 lines, so we need to
1446 fix that up. */
1447 if (it3.line_wrap == WORD_WRAP)
1448 move_it_by_lines (&it3, -1);
1449 }
1450
1451 /* Record the vertical coordinate of the display
1452 line where we wound up. */
1453 top_y = it3.current_y;
1454 if (it3.bidi_p)
1455 {
1456 /* When characters are reordered for display,
1457 the character displayed to the left of the
1458 display string could be _after_ the display
1459 property in the logical order. Use the
1460 smallest vertical position of these two. */
1461 start_display (&it3, w, top);
1462 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1463 if (it3.current_y < top_y)
1464 top_y = it3.current_y;
1465 }
1466 /* Move from the top of the window to the beginning
1467 of the display line where the display string
1468 begins. */
1469 start_display (&it3, w, top);
1470 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1471 /* If it3_moved stays zero after the 'while' loop
1472 below, that means we already were at a newline
1473 before the loop (e.g., the display string begins
1474 with a newline), so we don't need to (and cannot)
1475 inspect the glyphs of it3.glyph_row, because
1476 PRODUCE_GLYPHS will not produce anything for a
1477 newline, and thus it3.glyph_row stays at its
1478 stale content it got at top of the window. */
1479 it3_moved = 0;
1480 /* Finally, advance the iterator until we hit the
1481 first display element whose character position is
1482 CHARPOS, or until the first newline from the
1483 display string, which signals the end of the
1484 display line. */
1485 while (get_next_display_element (&it3))
1486 {
1487 PRODUCE_GLYPHS (&it3);
1488 if (IT_CHARPOS (it3) == charpos
1489 || ITERATOR_AT_END_OF_LINE_P (&it3))
1490 break;
1491 it3_moved = 1;
1492 set_iterator_to_next (&it3, 0);
1493 }
1494 top_x = it3.current_x - it3.pixel_width;
1495 /* Normally, we would exit the above loop because we
1496 found the display element whose character
1497 position is CHARPOS. For the contingency that we
1498 didn't, and stopped at the first newline from the
1499 display string, move back over the glyphs
1500 produced from the string, until we find the
1501 rightmost glyph not from the string. */
1502 if (it3_moved
1503 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1504 {
1505 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1506 + it3.glyph_row->used[TEXT_AREA];
1507
1508 while (EQ ((g - 1)->object, string))
1509 {
1510 --g;
1511 top_x -= g->pixel_width;
1512 }
1513 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1514 + it3.glyph_row->used[TEXT_AREA]);
1515 }
1516 }
1517 }
1518
1519 *x = top_x;
1520 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1521 *rtop = max (0, window_top_y - top_y);
1522 *rbot = max (0, bottom_y - it.last_visible_y);
1523 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1524 - max (top_y, window_top_y)));
1525 *vpos = it.vpos;
1526 }
1527 }
1528 else
1529 {
1530 /* We were asked to provide info about WINDOW_END. */
1531 struct it it2;
1532 void *it2data = NULL;
1533
1534 SAVE_IT (it2, it, it2data);
1535 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1536 move_it_by_lines (&it, 1);
1537 if (charpos < IT_CHARPOS (it)
1538 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1539 {
1540 visible_p = 1;
1541 RESTORE_IT (&it2, &it2, it2data);
1542 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1543 *x = it2.current_x;
1544 *y = it2.current_y + it2.max_ascent - it2.ascent;
1545 *rtop = max (0, -it2.current_y);
1546 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1547 - it.last_visible_y));
1548 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1549 it.last_visible_y)
1550 - max (it2.current_y,
1551 WINDOW_HEADER_LINE_HEIGHT (w))));
1552 *vpos = it2.vpos;
1553 }
1554 else
1555 bidi_unshelve_cache (it2data, 1);
1556 }
1557 bidi_unshelve_cache (itdata, 0);
1558
1559 if (old_buffer)
1560 set_buffer_internal_1 (old_buffer);
1561
1562 current_header_line_height = current_mode_line_height = -1;
1563
1564 if (visible_p && XFASTINT (w->hscroll) > 0)
1565 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1566
1567 #if 0
1568 /* Debugging code. */
1569 if (visible_p)
1570 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1571 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1572 else
1573 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1574 #endif
1575
1576 return visible_p;
1577 }
1578
1579
1580 /* Return the next character from STR. Return in *LEN the length of
1581 the character. This is like STRING_CHAR_AND_LENGTH but never
1582 returns an invalid character. If we find one, we return a `?', but
1583 with the length of the invalid character. */
1584
1585 static inline int
1586 string_char_and_length (const unsigned char *str, int *len)
1587 {
1588 int c;
1589
1590 c = STRING_CHAR_AND_LENGTH (str, *len);
1591 if (!CHAR_VALID_P (c))
1592 /* We may not change the length here because other places in Emacs
1593 don't use this function, i.e. they silently accept invalid
1594 characters. */
1595 c = '?';
1596
1597 return c;
1598 }
1599
1600
1601
1602 /* Given a position POS containing a valid character and byte position
1603 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1604
1605 static struct text_pos
1606 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1607 {
1608 xassert (STRINGP (string) && nchars >= 0);
1609
1610 if (STRING_MULTIBYTE (string))
1611 {
1612 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1613 int len;
1614
1615 while (nchars--)
1616 {
1617 string_char_and_length (p, &len);
1618 p += len;
1619 CHARPOS (pos) += 1;
1620 BYTEPOS (pos) += len;
1621 }
1622 }
1623 else
1624 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1625
1626 return pos;
1627 }
1628
1629
1630 /* Value is the text position, i.e. character and byte position,
1631 for character position CHARPOS in STRING. */
1632
1633 static inline struct text_pos
1634 string_pos (ptrdiff_t charpos, Lisp_Object string)
1635 {
1636 struct text_pos pos;
1637 xassert (STRINGP (string));
1638 xassert (charpos >= 0);
1639 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1640 return pos;
1641 }
1642
1643
1644 /* Value is a text position, i.e. character and byte position, for
1645 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1646 means recognize multibyte characters. */
1647
1648 static struct text_pos
1649 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1650 {
1651 struct text_pos pos;
1652
1653 xassert (s != NULL);
1654 xassert (charpos >= 0);
1655
1656 if (multibyte_p)
1657 {
1658 int len;
1659
1660 SET_TEXT_POS (pos, 0, 0);
1661 while (charpos--)
1662 {
1663 string_char_and_length ((const unsigned char *) s, &len);
1664 s += len;
1665 CHARPOS (pos) += 1;
1666 BYTEPOS (pos) += len;
1667 }
1668 }
1669 else
1670 SET_TEXT_POS (pos, charpos, charpos);
1671
1672 return pos;
1673 }
1674
1675
1676 /* Value is the number of characters in C string S. MULTIBYTE_P
1677 non-zero means recognize multibyte characters. */
1678
1679 static ptrdiff_t
1680 number_of_chars (const char *s, int multibyte_p)
1681 {
1682 ptrdiff_t nchars;
1683
1684 if (multibyte_p)
1685 {
1686 ptrdiff_t rest = strlen (s);
1687 int len;
1688 const unsigned char *p = (const unsigned char *) s;
1689
1690 for (nchars = 0; rest > 0; ++nchars)
1691 {
1692 string_char_and_length (p, &len);
1693 rest -= len, p += len;
1694 }
1695 }
1696 else
1697 nchars = strlen (s);
1698
1699 return nchars;
1700 }
1701
1702
1703 /* Compute byte position NEWPOS->bytepos corresponding to
1704 NEWPOS->charpos. POS is a known position in string STRING.
1705 NEWPOS->charpos must be >= POS.charpos. */
1706
1707 static void
1708 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1709 {
1710 xassert (STRINGP (string));
1711 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1712
1713 if (STRING_MULTIBYTE (string))
1714 *newpos = string_pos_nchars_ahead (pos, string,
1715 CHARPOS (*newpos) - CHARPOS (pos));
1716 else
1717 BYTEPOS (*newpos) = CHARPOS (*newpos);
1718 }
1719
1720 /* EXPORT:
1721 Return an estimation of the pixel height of mode or header lines on
1722 frame F. FACE_ID specifies what line's height to estimate. */
1723
1724 int
1725 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1726 {
1727 #ifdef HAVE_WINDOW_SYSTEM
1728 if (FRAME_WINDOW_P (f))
1729 {
1730 int height = FONT_HEIGHT (FRAME_FONT (f));
1731
1732 /* This function is called so early when Emacs starts that the face
1733 cache and mode line face are not yet initialized. */
1734 if (FRAME_FACE_CACHE (f))
1735 {
1736 struct face *face = FACE_FROM_ID (f, face_id);
1737 if (face)
1738 {
1739 if (face->font)
1740 height = FONT_HEIGHT (face->font);
1741 if (face->box_line_width > 0)
1742 height += 2 * face->box_line_width;
1743 }
1744 }
1745
1746 return height;
1747 }
1748 #endif
1749
1750 return 1;
1751 }
1752
1753 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1754 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1755 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1756 not force the value into range. */
1757
1758 void
1759 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1760 int *x, int *y, NativeRectangle *bounds, int noclip)
1761 {
1762
1763 #ifdef HAVE_WINDOW_SYSTEM
1764 if (FRAME_WINDOW_P (f))
1765 {
1766 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1767 even for negative values. */
1768 if (pix_x < 0)
1769 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1770 if (pix_y < 0)
1771 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1772
1773 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1774 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1775
1776 if (bounds)
1777 STORE_NATIVE_RECT (*bounds,
1778 FRAME_COL_TO_PIXEL_X (f, pix_x),
1779 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1780 FRAME_COLUMN_WIDTH (f) - 1,
1781 FRAME_LINE_HEIGHT (f) - 1);
1782
1783 if (!noclip)
1784 {
1785 if (pix_x < 0)
1786 pix_x = 0;
1787 else if (pix_x > FRAME_TOTAL_COLS (f))
1788 pix_x = FRAME_TOTAL_COLS (f);
1789
1790 if (pix_y < 0)
1791 pix_y = 0;
1792 else if (pix_y > FRAME_LINES (f))
1793 pix_y = FRAME_LINES (f);
1794 }
1795 }
1796 #endif
1797
1798 *x = pix_x;
1799 *y = pix_y;
1800 }
1801
1802
1803 /* Find the glyph under window-relative coordinates X/Y in window W.
1804 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1805 strings. Return in *HPOS and *VPOS the row and column number of
1806 the glyph found. Return in *AREA the glyph area containing X.
1807 Value is a pointer to the glyph found or null if X/Y is not on
1808 text, or we can't tell because W's current matrix is not up to
1809 date. */
1810
1811 static
1812 struct glyph *
1813 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1814 int *dx, int *dy, int *area)
1815 {
1816 struct glyph *glyph, *end;
1817 struct glyph_row *row = NULL;
1818 int x0, i;
1819
1820 /* Find row containing Y. Give up if some row is not enabled. */
1821 for (i = 0; i < w->current_matrix->nrows; ++i)
1822 {
1823 row = MATRIX_ROW (w->current_matrix, i);
1824 if (!row->enabled_p)
1825 return NULL;
1826 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1827 break;
1828 }
1829
1830 *vpos = i;
1831 *hpos = 0;
1832
1833 /* Give up if Y is not in the window. */
1834 if (i == w->current_matrix->nrows)
1835 return NULL;
1836
1837 /* Get the glyph area containing X. */
1838 if (w->pseudo_window_p)
1839 {
1840 *area = TEXT_AREA;
1841 x0 = 0;
1842 }
1843 else
1844 {
1845 if (x < window_box_left_offset (w, TEXT_AREA))
1846 {
1847 *area = LEFT_MARGIN_AREA;
1848 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1849 }
1850 else if (x < window_box_right_offset (w, TEXT_AREA))
1851 {
1852 *area = TEXT_AREA;
1853 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1854 }
1855 else
1856 {
1857 *area = RIGHT_MARGIN_AREA;
1858 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1859 }
1860 }
1861
1862 /* Find glyph containing X. */
1863 glyph = row->glyphs[*area];
1864 end = glyph + row->used[*area];
1865 x -= x0;
1866 while (glyph < end && x >= glyph->pixel_width)
1867 {
1868 x -= glyph->pixel_width;
1869 ++glyph;
1870 }
1871
1872 if (glyph == end)
1873 return NULL;
1874
1875 if (dx)
1876 {
1877 *dx = x;
1878 *dy = y - (row->y + row->ascent - glyph->ascent);
1879 }
1880
1881 *hpos = glyph - row->glyphs[*area];
1882 return glyph;
1883 }
1884
1885 /* Convert frame-relative x/y to coordinates relative to window W.
1886 Takes pseudo-windows into account. */
1887
1888 static void
1889 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1890 {
1891 if (w->pseudo_window_p)
1892 {
1893 /* A pseudo-window is always full-width, and starts at the
1894 left edge of the frame, plus a frame border. */
1895 struct frame *f = XFRAME (w->frame);
1896 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1897 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1898 }
1899 else
1900 {
1901 *x -= WINDOW_LEFT_EDGE_X (w);
1902 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1903 }
1904 }
1905
1906 #ifdef HAVE_WINDOW_SYSTEM
1907
1908 /* EXPORT:
1909 Return in RECTS[] at most N clipping rectangles for glyph string S.
1910 Return the number of stored rectangles. */
1911
1912 int
1913 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1914 {
1915 XRectangle r;
1916
1917 if (n <= 0)
1918 return 0;
1919
1920 if (s->row->full_width_p)
1921 {
1922 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1923 r.x = WINDOW_LEFT_EDGE_X (s->w);
1924 r.width = WINDOW_TOTAL_WIDTH (s->w);
1925
1926 /* Unless displaying a mode or menu bar line, which are always
1927 fully visible, clip to the visible part of the row. */
1928 if (s->w->pseudo_window_p)
1929 r.height = s->row->visible_height;
1930 else
1931 r.height = s->height;
1932 }
1933 else
1934 {
1935 /* This is a text line that may be partially visible. */
1936 r.x = window_box_left (s->w, s->area);
1937 r.width = window_box_width (s->w, s->area);
1938 r.height = s->row->visible_height;
1939 }
1940
1941 if (s->clip_head)
1942 if (r.x < s->clip_head->x)
1943 {
1944 if (r.width >= s->clip_head->x - r.x)
1945 r.width -= s->clip_head->x - r.x;
1946 else
1947 r.width = 0;
1948 r.x = s->clip_head->x;
1949 }
1950 if (s->clip_tail)
1951 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1952 {
1953 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1954 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1955 else
1956 r.width = 0;
1957 }
1958
1959 /* If S draws overlapping rows, it's sufficient to use the top and
1960 bottom of the window for clipping because this glyph string
1961 intentionally draws over other lines. */
1962 if (s->for_overlaps)
1963 {
1964 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1965 r.height = window_text_bottom_y (s->w) - r.y;
1966
1967 /* Alas, the above simple strategy does not work for the
1968 environments with anti-aliased text: if the same text is
1969 drawn onto the same place multiple times, it gets thicker.
1970 If the overlap we are processing is for the erased cursor, we
1971 take the intersection with the rectangle of the cursor. */
1972 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1973 {
1974 XRectangle rc, r_save = r;
1975
1976 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1977 rc.y = s->w->phys_cursor.y;
1978 rc.width = s->w->phys_cursor_width;
1979 rc.height = s->w->phys_cursor_height;
1980
1981 x_intersect_rectangles (&r_save, &rc, &r);
1982 }
1983 }
1984 else
1985 {
1986 /* Don't use S->y for clipping because it doesn't take partially
1987 visible lines into account. For example, it can be negative for
1988 partially visible lines at the top of a window. */
1989 if (!s->row->full_width_p
1990 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1991 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1992 else
1993 r.y = max (0, s->row->y);
1994 }
1995
1996 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1997
1998 /* If drawing the cursor, don't let glyph draw outside its
1999 advertised boundaries. Cleartype does this under some circumstances. */
2000 if (s->hl == DRAW_CURSOR)
2001 {
2002 struct glyph *glyph = s->first_glyph;
2003 int height, max_y;
2004
2005 if (s->x > r.x)
2006 {
2007 r.width -= s->x - r.x;
2008 r.x = s->x;
2009 }
2010 r.width = min (r.width, glyph->pixel_width);
2011
2012 /* If r.y is below window bottom, ensure that we still see a cursor. */
2013 height = min (glyph->ascent + glyph->descent,
2014 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2015 max_y = window_text_bottom_y (s->w) - height;
2016 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2017 if (s->ybase - glyph->ascent > max_y)
2018 {
2019 r.y = max_y;
2020 r.height = height;
2021 }
2022 else
2023 {
2024 /* Don't draw cursor glyph taller than our actual glyph. */
2025 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2026 if (height < r.height)
2027 {
2028 max_y = r.y + r.height;
2029 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2030 r.height = min (max_y - r.y, height);
2031 }
2032 }
2033 }
2034
2035 if (s->row->clip)
2036 {
2037 XRectangle r_save = r;
2038
2039 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2040 r.width = 0;
2041 }
2042
2043 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2044 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2045 {
2046 #ifdef CONVERT_FROM_XRECT
2047 CONVERT_FROM_XRECT (r, *rects);
2048 #else
2049 *rects = r;
2050 #endif
2051 return 1;
2052 }
2053 else
2054 {
2055 /* If we are processing overlapping and allowed to return
2056 multiple clipping rectangles, we exclude the row of the glyph
2057 string from the clipping rectangle. This is to avoid drawing
2058 the same text on the environment with anti-aliasing. */
2059 #ifdef CONVERT_FROM_XRECT
2060 XRectangle rs[2];
2061 #else
2062 XRectangle *rs = rects;
2063 #endif
2064 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2065
2066 if (s->for_overlaps & OVERLAPS_PRED)
2067 {
2068 rs[i] = r;
2069 if (r.y + r.height > row_y)
2070 {
2071 if (r.y < row_y)
2072 rs[i].height = row_y - r.y;
2073 else
2074 rs[i].height = 0;
2075 }
2076 i++;
2077 }
2078 if (s->for_overlaps & OVERLAPS_SUCC)
2079 {
2080 rs[i] = r;
2081 if (r.y < row_y + s->row->visible_height)
2082 {
2083 if (r.y + r.height > row_y + s->row->visible_height)
2084 {
2085 rs[i].y = row_y + s->row->visible_height;
2086 rs[i].height = r.y + r.height - rs[i].y;
2087 }
2088 else
2089 rs[i].height = 0;
2090 }
2091 i++;
2092 }
2093
2094 n = i;
2095 #ifdef CONVERT_FROM_XRECT
2096 for (i = 0; i < n; i++)
2097 CONVERT_FROM_XRECT (rs[i], rects[i]);
2098 #endif
2099 return n;
2100 }
2101 }
2102
2103 /* EXPORT:
2104 Return in *NR the clipping rectangle for glyph string S. */
2105
2106 void
2107 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2108 {
2109 get_glyph_string_clip_rects (s, nr, 1);
2110 }
2111
2112
2113 /* EXPORT:
2114 Return the position and height of the phys cursor in window W.
2115 Set w->phys_cursor_width to width of phys cursor.
2116 */
2117
2118 void
2119 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2120 struct glyph *glyph, int *xp, int *yp, int *heightp)
2121 {
2122 struct frame *f = XFRAME (WINDOW_FRAME (w));
2123 int x, y, wd, h, h0, y0;
2124
2125 /* Compute the width of the rectangle to draw. If on a stretch
2126 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2127 rectangle as wide as the glyph, but use a canonical character
2128 width instead. */
2129 wd = glyph->pixel_width - 1;
2130 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2131 wd++; /* Why? */
2132 #endif
2133
2134 x = w->phys_cursor.x;
2135 if (x < 0)
2136 {
2137 wd += x;
2138 x = 0;
2139 }
2140
2141 if (glyph->type == STRETCH_GLYPH
2142 && !x_stretch_cursor_p)
2143 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2144 w->phys_cursor_width = wd;
2145
2146 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2147
2148 /* If y is below window bottom, ensure that we still see a cursor. */
2149 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2150
2151 h = max (h0, glyph->ascent + glyph->descent);
2152 h0 = min (h0, glyph->ascent + glyph->descent);
2153
2154 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2155 if (y < y0)
2156 {
2157 h = max (h - (y0 - y) + 1, h0);
2158 y = y0 - 1;
2159 }
2160 else
2161 {
2162 y0 = window_text_bottom_y (w) - h0;
2163 if (y > y0)
2164 {
2165 h += y - y0;
2166 y = y0;
2167 }
2168 }
2169
2170 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2171 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2172 *heightp = h;
2173 }
2174
2175 /*
2176 * Remember which glyph the mouse is over.
2177 */
2178
2179 void
2180 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2181 {
2182 Lisp_Object window;
2183 struct window *w;
2184 struct glyph_row *r, *gr, *end_row;
2185 enum window_part part;
2186 enum glyph_row_area area;
2187 int x, y, width, height;
2188
2189 /* Try to determine frame pixel position and size of the glyph under
2190 frame pixel coordinates X/Y on frame F. */
2191
2192 if (!f->glyphs_initialized_p
2193 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2194 NILP (window)))
2195 {
2196 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2197 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2198 goto virtual_glyph;
2199 }
2200
2201 w = XWINDOW (window);
2202 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2203 height = WINDOW_FRAME_LINE_HEIGHT (w);
2204
2205 x = window_relative_x_coord (w, part, gx);
2206 y = gy - WINDOW_TOP_EDGE_Y (w);
2207
2208 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2209 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2210
2211 if (w->pseudo_window_p)
2212 {
2213 area = TEXT_AREA;
2214 part = ON_MODE_LINE; /* Don't adjust margin. */
2215 goto text_glyph;
2216 }
2217
2218 switch (part)
2219 {
2220 case ON_LEFT_MARGIN:
2221 area = LEFT_MARGIN_AREA;
2222 goto text_glyph;
2223
2224 case ON_RIGHT_MARGIN:
2225 area = RIGHT_MARGIN_AREA;
2226 goto text_glyph;
2227
2228 case ON_HEADER_LINE:
2229 case ON_MODE_LINE:
2230 gr = (part == ON_HEADER_LINE
2231 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2232 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2233 gy = gr->y;
2234 area = TEXT_AREA;
2235 goto text_glyph_row_found;
2236
2237 case ON_TEXT:
2238 area = TEXT_AREA;
2239
2240 text_glyph:
2241 gr = 0; gy = 0;
2242 for (; r <= end_row && r->enabled_p; ++r)
2243 if (r->y + r->height > y)
2244 {
2245 gr = r; gy = r->y;
2246 break;
2247 }
2248
2249 text_glyph_row_found:
2250 if (gr && gy <= y)
2251 {
2252 struct glyph *g = gr->glyphs[area];
2253 struct glyph *end = g + gr->used[area];
2254
2255 height = gr->height;
2256 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2257 if (gx + g->pixel_width > x)
2258 break;
2259
2260 if (g < end)
2261 {
2262 if (g->type == IMAGE_GLYPH)
2263 {
2264 /* Don't remember when mouse is over image, as
2265 image may have hot-spots. */
2266 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2267 return;
2268 }
2269 width = g->pixel_width;
2270 }
2271 else
2272 {
2273 /* Use nominal char spacing at end of line. */
2274 x -= gx;
2275 gx += (x / width) * width;
2276 }
2277
2278 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2279 gx += window_box_left_offset (w, area);
2280 }
2281 else
2282 {
2283 /* Use nominal line height at end of window. */
2284 gx = (x / width) * width;
2285 y -= gy;
2286 gy += (y / height) * height;
2287 }
2288 break;
2289
2290 case ON_LEFT_FRINGE:
2291 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2292 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2293 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2294 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2295 goto row_glyph;
2296
2297 case ON_RIGHT_FRINGE:
2298 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2299 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2300 : window_box_right_offset (w, TEXT_AREA));
2301 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2302 goto row_glyph;
2303
2304 case ON_SCROLL_BAR:
2305 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2306 ? 0
2307 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2308 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2309 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2310 : 0)));
2311 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2312
2313 row_glyph:
2314 gr = 0, gy = 0;
2315 for (; r <= end_row && r->enabled_p; ++r)
2316 if (r->y + r->height > y)
2317 {
2318 gr = r; gy = r->y;
2319 break;
2320 }
2321
2322 if (gr && gy <= y)
2323 height = gr->height;
2324 else
2325 {
2326 /* Use nominal line height at end of window. */
2327 y -= gy;
2328 gy += (y / height) * height;
2329 }
2330 break;
2331
2332 default:
2333 ;
2334 virtual_glyph:
2335 /* If there is no glyph under the mouse, then we divide the screen
2336 into a grid of the smallest glyph in the frame, and use that
2337 as our "glyph". */
2338
2339 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2340 round down even for negative values. */
2341 if (gx < 0)
2342 gx -= width - 1;
2343 if (gy < 0)
2344 gy -= height - 1;
2345
2346 gx = (gx / width) * width;
2347 gy = (gy / height) * height;
2348
2349 goto store_rect;
2350 }
2351
2352 gx += WINDOW_LEFT_EDGE_X (w);
2353 gy += WINDOW_TOP_EDGE_Y (w);
2354
2355 store_rect:
2356 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2357
2358 /* Visible feedback for debugging. */
2359 #if 0
2360 #if HAVE_X_WINDOWS
2361 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2362 f->output_data.x->normal_gc,
2363 gx, gy, width, height);
2364 #endif
2365 #endif
2366 }
2367
2368
2369 #endif /* HAVE_WINDOW_SYSTEM */
2370
2371 \f
2372 /***********************************************************************
2373 Lisp form evaluation
2374 ***********************************************************************/
2375
2376 /* Error handler for safe_eval and safe_call. */
2377
2378 static Lisp_Object
2379 safe_eval_handler (Lisp_Object arg)
2380 {
2381 add_to_log ("Error during redisplay: %S", arg, Qnil);
2382 return Qnil;
2383 }
2384
2385
2386 /* Evaluate SEXPR and return the result, or nil if something went
2387 wrong. Prevent redisplay during the evaluation. */
2388
2389 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2390 Return the result, or nil if something went wrong. Prevent
2391 redisplay during the evaluation. */
2392
2393 Lisp_Object
2394 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2395 {
2396 Lisp_Object val;
2397
2398 if (inhibit_eval_during_redisplay)
2399 val = Qnil;
2400 else
2401 {
2402 ptrdiff_t count = SPECPDL_INDEX ();
2403 struct gcpro gcpro1;
2404
2405 GCPRO1 (args[0]);
2406 gcpro1.nvars = nargs;
2407 specbind (Qinhibit_redisplay, Qt);
2408 /* Use Qt to ensure debugger does not run,
2409 so there is no possibility of wanting to redisplay. */
2410 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2411 safe_eval_handler);
2412 UNGCPRO;
2413 val = unbind_to (count, val);
2414 }
2415
2416 return val;
2417 }
2418
2419
2420 /* Call function FN with one argument ARG.
2421 Return the result, or nil if something went wrong. */
2422
2423 Lisp_Object
2424 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2425 {
2426 Lisp_Object args[2];
2427 args[0] = fn;
2428 args[1] = arg;
2429 return safe_call (2, args);
2430 }
2431
2432 static Lisp_Object Qeval;
2433
2434 Lisp_Object
2435 safe_eval (Lisp_Object sexpr)
2436 {
2437 return safe_call1 (Qeval, sexpr);
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_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2445 {
2446 Lisp_Object args[3];
2447 args[0] = fn;
2448 args[1] = arg1;
2449 args[2] = arg2;
2450 return safe_call (3, args);
2451 }
2452
2453
2454 \f
2455 /***********************************************************************
2456 Debugging
2457 ***********************************************************************/
2458
2459 #if 0
2460
2461 /* Define CHECK_IT to perform sanity checks on iterators.
2462 This is for debugging. It is too slow to do unconditionally. */
2463
2464 static void
2465 check_it (struct it *it)
2466 {
2467 if (it->method == GET_FROM_STRING)
2468 {
2469 xassert (STRINGP (it->string));
2470 xassert (IT_STRING_CHARPOS (*it) >= 0);
2471 }
2472 else
2473 {
2474 xassert (IT_STRING_CHARPOS (*it) < 0);
2475 if (it->method == GET_FROM_BUFFER)
2476 {
2477 /* Check that character and byte positions agree. */
2478 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2479 }
2480 }
2481
2482 if (it->dpvec)
2483 xassert (it->current.dpvec_index >= 0);
2484 else
2485 xassert (it->current.dpvec_index < 0);
2486 }
2487
2488 #define CHECK_IT(IT) check_it ((IT))
2489
2490 #else /* not 0 */
2491
2492 #define CHECK_IT(IT) (void) 0
2493
2494 #endif /* not 0 */
2495
2496
2497 #if GLYPH_DEBUG && XASSERTS
2498
2499 /* Check that the window end of window W is what we expect it
2500 to be---the last row in the current matrix displaying text. */
2501
2502 static void
2503 check_window_end (struct window *w)
2504 {
2505 if (!MINI_WINDOW_P (w)
2506 && !NILP (w->window_end_valid))
2507 {
2508 struct glyph_row *row;
2509 xassert ((row = MATRIX_ROW (w->current_matrix,
2510 XFASTINT (w->window_end_vpos)),
2511 !row->enabled_p
2512 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2513 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2514 }
2515 }
2516
2517 #define CHECK_WINDOW_END(W) check_window_end ((W))
2518
2519 #else
2520
2521 #define CHECK_WINDOW_END(W) (void) 0
2522
2523 #endif
2524
2525
2526 \f
2527 /***********************************************************************
2528 Iterator initialization
2529 ***********************************************************************/
2530
2531 /* Initialize IT for displaying current_buffer in window W, starting
2532 at character position CHARPOS. CHARPOS < 0 means that no buffer
2533 position is specified which is useful when the iterator is assigned
2534 a position later. BYTEPOS is the byte position corresponding to
2535 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2536
2537 If ROW is not null, calls to produce_glyphs with IT as parameter
2538 will produce glyphs in that row.
2539
2540 BASE_FACE_ID is the id of a base face to use. It must be one of
2541 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2542 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2543 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2544
2545 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2546 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2547 will be initialized to use the corresponding mode line glyph row of
2548 the desired matrix of W. */
2549
2550 void
2551 init_iterator (struct it *it, struct window *w,
2552 ptrdiff_t charpos, ptrdiff_t bytepos,
2553 struct glyph_row *row, enum face_id base_face_id)
2554 {
2555 int highlight_region_p;
2556 enum face_id remapped_base_face_id = base_face_id;
2557
2558 /* Some precondition checks. */
2559 xassert (w != NULL && it != NULL);
2560 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2561 && charpos <= ZV));
2562
2563 /* If face attributes have been changed since the last redisplay,
2564 free realized faces now because they depend on face definitions
2565 that might have changed. Don't free faces while there might be
2566 desired matrices pending which reference these faces. */
2567 if (face_change_count && !inhibit_free_realized_faces)
2568 {
2569 face_change_count = 0;
2570 free_all_realized_faces (Qnil);
2571 }
2572
2573 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2574 if (! NILP (Vface_remapping_alist))
2575 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2576
2577 /* Use one of the mode line rows of W's desired matrix if
2578 appropriate. */
2579 if (row == NULL)
2580 {
2581 if (base_face_id == MODE_LINE_FACE_ID
2582 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2583 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2584 else if (base_face_id == HEADER_LINE_FACE_ID)
2585 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2586 }
2587
2588 /* Clear IT. */
2589 memset (it, 0, sizeof *it);
2590 it->current.overlay_string_index = -1;
2591 it->current.dpvec_index = -1;
2592 it->base_face_id = remapped_base_face_id;
2593 it->string = Qnil;
2594 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2595 it->paragraph_embedding = L2R;
2596 it->bidi_it.string.lstring = Qnil;
2597 it->bidi_it.string.s = NULL;
2598 it->bidi_it.string.bufpos = 0;
2599
2600 /* The window in which we iterate over current_buffer: */
2601 XSETWINDOW (it->window, w);
2602 it->w = w;
2603 it->f = XFRAME (w->frame);
2604
2605 it->cmp_it.id = -1;
2606
2607 /* Extra space between lines (on window systems only). */
2608 if (base_face_id == DEFAULT_FACE_ID
2609 && FRAME_WINDOW_P (it->f))
2610 {
2611 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2612 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2613 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2614 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2615 * FRAME_LINE_HEIGHT (it->f));
2616 else if (it->f->extra_line_spacing > 0)
2617 it->extra_line_spacing = it->f->extra_line_spacing;
2618 it->max_extra_line_spacing = 0;
2619 }
2620
2621 /* If realized faces have been removed, e.g. because of face
2622 attribute changes of named faces, recompute them. When running
2623 in batch mode, the face cache of the initial frame is null. If
2624 we happen to get called, make a dummy face cache. */
2625 if (FRAME_FACE_CACHE (it->f) == NULL)
2626 init_frame_faces (it->f);
2627 if (FRAME_FACE_CACHE (it->f)->used == 0)
2628 recompute_basic_faces (it->f);
2629
2630 /* Current value of the `slice', `space-width', and 'height' properties. */
2631 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2632 it->space_width = Qnil;
2633 it->font_height = Qnil;
2634 it->override_ascent = -1;
2635
2636 /* Are control characters displayed as `^C'? */
2637 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2638
2639 /* -1 means everything between a CR and the following line end
2640 is invisible. >0 means lines indented more than this value are
2641 invisible. */
2642 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2643 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2644 selective_display)),
2645 PTRDIFF_MAX)
2646 : (!NILP (BVAR (current_buffer, selective_display))
2647 ? -1 : 0));
2648 it->selective_display_ellipsis_p
2649 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2650
2651 /* Display table to use. */
2652 it->dp = window_display_table (w);
2653
2654 /* Are multibyte characters enabled in current_buffer? */
2655 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2656
2657 /* Non-zero if we should highlight the region. */
2658 highlight_region_p
2659 = (!NILP (Vtransient_mark_mode)
2660 && !NILP (BVAR (current_buffer, mark_active))
2661 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2662
2663 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2664 start and end of a visible region in window IT->w. Set both to
2665 -1 to indicate no region. */
2666 if (highlight_region_p
2667 /* Maybe highlight only in selected window. */
2668 && (/* Either show region everywhere. */
2669 highlight_nonselected_windows
2670 /* Or show region in the selected window. */
2671 || w == XWINDOW (selected_window)
2672 /* Or show the region if we are in the mini-buffer and W is
2673 the window the mini-buffer refers to. */
2674 || (MINI_WINDOW_P (XWINDOW (selected_window))
2675 && WINDOWP (minibuf_selected_window)
2676 && w == XWINDOW (minibuf_selected_window))))
2677 {
2678 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2679 it->region_beg_charpos = min (PT, markpos);
2680 it->region_end_charpos = max (PT, markpos);
2681 }
2682 else
2683 it->region_beg_charpos = it->region_end_charpos = -1;
2684
2685 /* Get the position at which the redisplay_end_trigger hook should
2686 be run, if it is to be run at all. */
2687 if (MARKERP (w->redisplay_end_trigger)
2688 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2689 it->redisplay_end_trigger_charpos
2690 = marker_position (w->redisplay_end_trigger);
2691 else if (INTEGERP (w->redisplay_end_trigger))
2692 it->redisplay_end_trigger_charpos =
2693 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2694
2695 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2696
2697 /* Are lines in the display truncated? */
2698 if (base_face_id != DEFAULT_FACE_ID
2699 || XINT (it->w->hscroll)
2700 || (! WINDOW_FULL_WIDTH_P (it->w)
2701 && ((!NILP (Vtruncate_partial_width_windows)
2702 && !INTEGERP (Vtruncate_partial_width_windows))
2703 || (INTEGERP (Vtruncate_partial_width_windows)
2704 && (WINDOW_TOTAL_COLS (it->w)
2705 < XINT (Vtruncate_partial_width_windows))))))
2706 it->line_wrap = TRUNCATE;
2707 else if (NILP (BVAR (current_buffer, truncate_lines)))
2708 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2709 ? WINDOW_WRAP : WORD_WRAP;
2710 else
2711 it->line_wrap = TRUNCATE;
2712
2713 /* Get dimensions of truncation and continuation glyphs. These are
2714 displayed as fringe bitmaps under X, so we don't need them for such
2715 frames. */
2716 if (!FRAME_WINDOW_P (it->f))
2717 {
2718 if (it->line_wrap == TRUNCATE)
2719 {
2720 /* We will need the truncation glyph. */
2721 xassert (it->glyph_row == NULL);
2722 produce_special_glyphs (it, IT_TRUNCATION);
2723 it->truncation_pixel_width = it->pixel_width;
2724 }
2725 else
2726 {
2727 /* We will need the continuation glyph. */
2728 xassert (it->glyph_row == NULL);
2729 produce_special_glyphs (it, IT_CONTINUATION);
2730 it->continuation_pixel_width = it->pixel_width;
2731 }
2732
2733 /* Reset these values to zero because the produce_special_glyphs
2734 above has changed them. */
2735 it->pixel_width = it->ascent = it->descent = 0;
2736 it->phys_ascent = it->phys_descent = 0;
2737 }
2738
2739 /* Set this after getting the dimensions of truncation and
2740 continuation glyphs, so that we don't produce glyphs when calling
2741 produce_special_glyphs, above. */
2742 it->glyph_row = row;
2743 it->area = TEXT_AREA;
2744
2745 /* Forget any previous info about this row being reversed. */
2746 if (it->glyph_row)
2747 it->glyph_row->reversed_p = 0;
2748
2749 /* Get the dimensions of the display area. The display area
2750 consists of the visible window area plus a horizontally scrolled
2751 part to the left of the window. All x-values are relative to the
2752 start of this total display area. */
2753 if (base_face_id != DEFAULT_FACE_ID)
2754 {
2755 /* Mode lines, menu bar in terminal frames. */
2756 it->first_visible_x = 0;
2757 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2758 }
2759 else
2760 {
2761 it->first_visible_x
2762 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2763 it->last_visible_x = (it->first_visible_x
2764 + window_box_width (w, TEXT_AREA));
2765
2766 /* If we truncate lines, leave room for the truncator glyph(s) at
2767 the right margin. Otherwise, leave room for the continuation
2768 glyph(s). Truncation and continuation glyphs are not inserted
2769 for window-based redisplay. */
2770 if (!FRAME_WINDOW_P (it->f))
2771 {
2772 if (it->line_wrap == TRUNCATE)
2773 it->last_visible_x -= it->truncation_pixel_width;
2774 else
2775 it->last_visible_x -= it->continuation_pixel_width;
2776 }
2777
2778 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2779 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2780 }
2781
2782 /* Leave room for a border glyph. */
2783 if (!FRAME_WINDOW_P (it->f)
2784 && !WINDOW_RIGHTMOST_P (it->w))
2785 it->last_visible_x -= 1;
2786
2787 it->last_visible_y = window_text_bottom_y (w);
2788
2789 /* For mode lines and alike, arrange for the first glyph having a
2790 left box line if the face specifies a box. */
2791 if (base_face_id != DEFAULT_FACE_ID)
2792 {
2793 struct face *face;
2794
2795 it->face_id = remapped_base_face_id;
2796
2797 /* If we have a boxed mode line, make the first character appear
2798 with a left box line. */
2799 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2800 if (face->box != FACE_NO_BOX)
2801 it->start_of_box_run_p = 1;
2802 }
2803
2804 /* If a buffer position was specified, set the iterator there,
2805 getting overlays and face properties from that position. */
2806 if (charpos >= BUF_BEG (current_buffer))
2807 {
2808 it->end_charpos = ZV;
2809 IT_CHARPOS (*it) = charpos;
2810
2811 /* We will rely on `reseat' to set this up properly, via
2812 handle_face_prop. */
2813 it->face_id = it->base_face_id;
2814
2815 /* Compute byte position if not specified. */
2816 if (bytepos < charpos)
2817 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2818 else
2819 IT_BYTEPOS (*it) = bytepos;
2820
2821 it->start = it->current;
2822 /* Do we need to reorder bidirectional text? Not if this is a
2823 unibyte buffer: by definition, none of the single-byte
2824 characters are strong R2L, so no reordering is needed. And
2825 bidi.c doesn't support unibyte buffers anyway. Also, don't
2826 reorder while we are loading loadup.el, since the tables of
2827 character properties needed for reordering are not yet
2828 available. */
2829 it->bidi_p =
2830 NILP (Vpurify_flag)
2831 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2832 && it->multibyte_p;
2833
2834 /* If we are to reorder bidirectional text, init the bidi
2835 iterator. */
2836 if (it->bidi_p)
2837 {
2838 /* Note the paragraph direction that this buffer wants to
2839 use. */
2840 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2841 Qleft_to_right))
2842 it->paragraph_embedding = L2R;
2843 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2844 Qright_to_left))
2845 it->paragraph_embedding = R2L;
2846 else
2847 it->paragraph_embedding = NEUTRAL_DIR;
2848 bidi_unshelve_cache (NULL, 0);
2849 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2850 &it->bidi_it);
2851 }
2852
2853 /* Compute faces etc. */
2854 reseat (it, it->current.pos, 1);
2855 }
2856
2857 CHECK_IT (it);
2858 }
2859
2860
2861 /* Initialize IT for the display of window W with window start POS. */
2862
2863 void
2864 start_display (struct it *it, struct window *w, struct text_pos pos)
2865 {
2866 struct glyph_row *row;
2867 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2868
2869 row = w->desired_matrix->rows + first_vpos;
2870 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2871 it->first_vpos = first_vpos;
2872
2873 /* Don't reseat to previous visible line start if current start
2874 position is in a string or image. */
2875 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2876 {
2877 int start_at_line_beg_p;
2878 int first_y = it->current_y;
2879
2880 /* If window start is not at a line start, skip forward to POS to
2881 get the correct continuation lines width. */
2882 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2883 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2884 if (!start_at_line_beg_p)
2885 {
2886 int new_x;
2887
2888 reseat_at_previous_visible_line_start (it);
2889 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2890
2891 new_x = it->current_x + it->pixel_width;
2892
2893 /* If lines are continued, this line may end in the middle
2894 of a multi-glyph character (e.g. a control character
2895 displayed as \003, or in the middle of an overlay
2896 string). In this case move_it_to above will not have
2897 taken us to the start of the continuation line but to the
2898 end of the continued line. */
2899 if (it->current_x > 0
2900 && it->line_wrap != TRUNCATE /* Lines are continued. */
2901 && (/* And glyph doesn't fit on the line. */
2902 new_x > it->last_visible_x
2903 /* Or it fits exactly and we're on a window
2904 system frame. */
2905 || (new_x == it->last_visible_x
2906 && FRAME_WINDOW_P (it->f))))
2907 {
2908 if ((it->current.dpvec_index >= 0
2909 || it->current.overlay_string_index >= 0)
2910 /* If we are on a newline from a display vector or
2911 overlay string, then we are already at the end of
2912 a screen line; no need to go to the next line in
2913 that case, as this line is not really continued.
2914 (If we do go to the next line, C-e will not DTRT.) */
2915 && it->c != '\n')
2916 {
2917 set_iterator_to_next (it, 1);
2918 move_it_in_display_line_to (it, -1, -1, 0);
2919 }
2920
2921 it->continuation_lines_width += it->current_x;
2922 }
2923 /* If the character at POS is displayed via a display
2924 vector, move_it_to above stops at the final glyph of
2925 IT->dpvec. To make the caller redisplay that character
2926 again (a.k.a. start at POS), we need to reset the
2927 dpvec_index to the beginning of IT->dpvec. */
2928 else if (it->current.dpvec_index >= 0)
2929 it->current.dpvec_index = 0;
2930
2931 /* We're starting a new display line, not affected by the
2932 height of the continued line, so clear the appropriate
2933 fields in the iterator structure. */
2934 it->max_ascent = it->max_descent = 0;
2935 it->max_phys_ascent = it->max_phys_descent = 0;
2936
2937 it->current_y = first_y;
2938 it->vpos = 0;
2939 it->current_x = it->hpos = 0;
2940 }
2941 }
2942 }
2943
2944
2945 /* Return 1 if POS is a position in ellipses displayed for invisible
2946 text. W is the window we display, for text property lookup. */
2947
2948 static int
2949 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2950 {
2951 Lisp_Object prop, window;
2952 int ellipses_p = 0;
2953 ptrdiff_t charpos = CHARPOS (pos->pos);
2954
2955 /* If POS specifies a position in a display vector, this might
2956 be for an ellipsis displayed for invisible text. We won't
2957 get the iterator set up for delivering that ellipsis unless
2958 we make sure that it gets aware of the invisible text. */
2959 if (pos->dpvec_index >= 0
2960 && pos->overlay_string_index < 0
2961 && CHARPOS (pos->string_pos) < 0
2962 && charpos > BEGV
2963 && (XSETWINDOW (window, w),
2964 prop = Fget_char_property (make_number (charpos),
2965 Qinvisible, window),
2966 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2967 {
2968 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2969 window);
2970 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2971 }
2972
2973 return ellipses_p;
2974 }
2975
2976
2977 /* Initialize IT for stepping through current_buffer in window W,
2978 starting at position POS that includes overlay string and display
2979 vector/ control character translation position information. Value
2980 is zero if there are overlay strings with newlines at POS. */
2981
2982 static int
2983 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2984 {
2985 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2986 int i, overlay_strings_with_newlines = 0;
2987
2988 /* If POS specifies a position in a display vector, this might
2989 be for an ellipsis displayed for invisible text. We won't
2990 get the iterator set up for delivering that ellipsis unless
2991 we make sure that it gets aware of the invisible text. */
2992 if (in_ellipses_for_invisible_text_p (pos, w))
2993 {
2994 --charpos;
2995 bytepos = 0;
2996 }
2997
2998 /* Keep in mind: the call to reseat in init_iterator skips invisible
2999 text, so we might end up at a position different from POS. This
3000 is only a problem when POS is a row start after a newline and an
3001 overlay starts there with an after-string, and the overlay has an
3002 invisible property. Since we don't skip invisible text in
3003 display_line and elsewhere immediately after consuming the
3004 newline before the row start, such a POS will not be in a string,
3005 but the call to init_iterator below will move us to the
3006 after-string. */
3007 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3008
3009 /* This only scans the current chunk -- it should scan all chunks.
3010 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3011 to 16 in 22.1 to make this a lesser problem. */
3012 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3013 {
3014 const char *s = SSDATA (it->overlay_strings[i]);
3015 const char *e = s + SBYTES (it->overlay_strings[i]);
3016
3017 while (s < e && *s != '\n')
3018 ++s;
3019
3020 if (s < e)
3021 {
3022 overlay_strings_with_newlines = 1;
3023 break;
3024 }
3025 }
3026
3027 /* If position is within an overlay string, set up IT to the right
3028 overlay string. */
3029 if (pos->overlay_string_index >= 0)
3030 {
3031 int relative_index;
3032
3033 /* If the first overlay string happens to have a `display'
3034 property for an image, the iterator will be set up for that
3035 image, and we have to undo that setup first before we can
3036 correct the overlay string index. */
3037 if (it->method == GET_FROM_IMAGE)
3038 pop_it (it);
3039
3040 /* We already have the first chunk of overlay strings in
3041 IT->overlay_strings. Load more until the one for
3042 pos->overlay_string_index is in IT->overlay_strings. */
3043 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3044 {
3045 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3046 it->current.overlay_string_index = 0;
3047 while (n--)
3048 {
3049 load_overlay_strings (it, 0);
3050 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3051 }
3052 }
3053
3054 it->current.overlay_string_index = pos->overlay_string_index;
3055 relative_index = (it->current.overlay_string_index
3056 % OVERLAY_STRING_CHUNK_SIZE);
3057 it->string = it->overlay_strings[relative_index];
3058 xassert (STRINGP (it->string));
3059 it->current.string_pos = pos->string_pos;
3060 it->method = GET_FROM_STRING;
3061 }
3062
3063 if (CHARPOS (pos->string_pos) >= 0)
3064 {
3065 /* Recorded position is not in an overlay string, but in another
3066 string. This can only be a string from a `display' property.
3067 IT should already be filled with that string. */
3068 it->current.string_pos = pos->string_pos;
3069 xassert (STRINGP (it->string));
3070 }
3071
3072 /* Restore position in display vector translations, control
3073 character translations or ellipses. */
3074 if (pos->dpvec_index >= 0)
3075 {
3076 if (it->dpvec == NULL)
3077 get_next_display_element (it);
3078 xassert (it->dpvec && it->current.dpvec_index == 0);
3079 it->current.dpvec_index = pos->dpvec_index;
3080 }
3081
3082 CHECK_IT (it);
3083 return !overlay_strings_with_newlines;
3084 }
3085
3086
3087 /* Initialize IT for stepping through current_buffer in window W
3088 starting at ROW->start. */
3089
3090 static void
3091 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3092 {
3093 init_from_display_pos (it, w, &row->start);
3094 it->start = row->start;
3095 it->continuation_lines_width = row->continuation_lines_width;
3096 CHECK_IT (it);
3097 }
3098
3099
3100 /* Initialize IT for stepping through current_buffer in window W
3101 starting in the line following ROW, i.e. starting at ROW->end.
3102 Value is zero if there are overlay strings with newlines at ROW's
3103 end position. */
3104
3105 static int
3106 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3107 {
3108 int success = 0;
3109
3110 if (init_from_display_pos (it, w, &row->end))
3111 {
3112 if (row->continued_p)
3113 it->continuation_lines_width
3114 = row->continuation_lines_width + row->pixel_width;
3115 CHECK_IT (it);
3116 success = 1;
3117 }
3118
3119 return success;
3120 }
3121
3122
3123
3124 \f
3125 /***********************************************************************
3126 Text properties
3127 ***********************************************************************/
3128
3129 /* Called when IT reaches IT->stop_charpos. Handle text property and
3130 overlay changes. Set IT->stop_charpos to the next position where
3131 to stop. */
3132
3133 static void
3134 handle_stop (struct it *it)
3135 {
3136 enum prop_handled handled;
3137 int handle_overlay_change_p;
3138 struct props *p;
3139
3140 it->dpvec = NULL;
3141 it->current.dpvec_index = -1;
3142 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3143 it->ignore_overlay_strings_at_pos_p = 0;
3144 it->ellipsis_p = 0;
3145
3146 /* Use face of preceding text for ellipsis (if invisible) */
3147 if (it->selective_display_ellipsis_p)
3148 it->saved_face_id = it->face_id;
3149
3150 do
3151 {
3152 handled = HANDLED_NORMALLY;
3153
3154 /* Call text property handlers. */
3155 for (p = it_props; p->handler; ++p)
3156 {
3157 handled = p->handler (it);
3158
3159 if (handled == HANDLED_RECOMPUTE_PROPS)
3160 break;
3161 else if (handled == HANDLED_RETURN)
3162 {
3163 /* We still want to show before and after strings from
3164 overlays even if the actual buffer text is replaced. */
3165 if (!handle_overlay_change_p
3166 || it->sp > 1
3167 /* Don't call get_overlay_strings_1 if we already
3168 have overlay strings loaded, because doing so
3169 will load them again and push the iterator state
3170 onto the stack one more time, which is not
3171 expected by the rest of the code that processes
3172 overlay strings. */
3173 || (it->n_overlay_strings <= 0
3174 ? !get_overlay_strings_1 (it, 0, 0)
3175 : 0))
3176 {
3177 if (it->ellipsis_p)
3178 setup_for_ellipsis (it, 0);
3179 /* When handling a display spec, we might load an
3180 empty string. In that case, discard it here. We
3181 used to discard it in handle_single_display_spec,
3182 but that causes get_overlay_strings_1, above, to
3183 ignore overlay strings that we must check. */
3184 if (STRINGP (it->string) && !SCHARS (it->string))
3185 pop_it (it);
3186 return;
3187 }
3188 else if (STRINGP (it->string) && !SCHARS (it->string))
3189 pop_it (it);
3190 else
3191 {
3192 it->ignore_overlay_strings_at_pos_p = 1;
3193 it->string_from_display_prop_p = 0;
3194 it->from_disp_prop_p = 0;
3195 handle_overlay_change_p = 0;
3196 }
3197 handled = HANDLED_RECOMPUTE_PROPS;
3198 break;
3199 }
3200 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3201 handle_overlay_change_p = 0;
3202 }
3203
3204 if (handled != HANDLED_RECOMPUTE_PROPS)
3205 {
3206 /* Don't check for overlay strings below when set to deliver
3207 characters from a display vector. */
3208 if (it->method == GET_FROM_DISPLAY_VECTOR)
3209 handle_overlay_change_p = 0;
3210
3211 /* Handle overlay changes.
3212 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3213 if it finds overlays. */
3214 if (handle_overlay_change_p)
3215 handled = handle_overlay_change (it);
3216 }
3217
3218 if (it->ellipsis_p)
3219 {
3220 setup_for_ellipsis (it, 0);
3221 break;
3222 }
3223 }
3224 while (handled == HANDLED_RECOMPUTE_PROPS);
3225
3226 /* Determine where to stop next. */
3227 if (handled == HANDLED_NORMALLY)
3228 compute_stop_pos (it);
3229 }
3230
3231
3232 /* Compute IT->stop_charpos from text property and overlay change
3233 information for IT's current position. */
3234
3235 static void
3236 compute_stop_pos (struct it *it)
3237 {
3238 register INTERVAL iv, next_iv;
3239 Lisp_Object object, limit, position;
3240 ptrdiff_t charpos, bytepos;
3241
3242 if (STRINGP (it->string))
3243 {
3244 /* Strings are usually short, so don't limit the search for
3245 properties. */
3246 it->stop_charpos = it->end_charpos;
3247 object = it->string;
3248 limit = Qnil;
3249 charpos = IT_STRING_CHARPOS (*it);
3250 bytepos = IT_STRING_BYTEPOS (*it);
3251 }
3252 else
3253 {
3254 ptrdiff_t pos;
3255
3256 /* If end_charpos is out of range for some reason, such as a
3257 misbehaving display function, rationalize it (Bug#5984). */
3258 if (it->end_charpos > ZV)
3259 it->end_charpos = ZV;
3260 it->stop_charpos = it->end_charpos;
3261
3262 /* If next overlay change is in front of the current stop pos
3263 (which is IT->end_charpos), stop there. Note: value of
3264 next_overlay_change is point-max if no overlay change
3265 follows. */
3266 charpos = IT_CHARPOS (*it);
3267 bytepos = IT_BYTEPOS (*it);
3268 pos = next_overlay_change (charpos);
3269 if (pos < it->stop_charpos)
3270 it->stop_charpos = pos;
3271
3272 /* If showing the region, we have to stop at the region
3273 start or end because the face might change there. */
3274 if (it->region_beg_charpos > 0)
3275 {
3276 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3277 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3278 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3279 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3280 }
3281
3282 /* Set up variables for computing the stop position from text
3283 property changes. */
3284 XSETBUFFER (object, current_buffer);
3285 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3286 }
3287
3288 /* Get the interval containing IT's position. Value is a null
3289 interval if there isn't such an interval. */
3290 position = make_number (charpos);
3291 iv = validate_interval_range (object, &position, &position, 0);
3292 if (!NULL_INTERVAL_P (iv))
3293 {
3294 Lisp_Object values_here[LAST_PROP_IDX];
3295 struct props *p;
3296
3297 /* Get properties here. */
3298 for (p = it_props; p->handler; ++p)
3299 values_here[p->idx] = textget (iv->plist, *p->name);
3300
3301 /* Look for an interval following iv that has different
3302 properties. */
3303 for (next_iv = next_interval (iv);
3304 (!NULL_INTERVAL_P (next_iv)
3305 && (NILP (limit)
3306 || XFASTINT (limit) > next_iv->position));
3307 next_iv = next_interval (next_iv))
3308 {
3309 for (p = it_props; p->handler; ++p)
3310 {
3311 Lisp_Object new_value;
3312
3313 new_value = textget (next_iv->plist, *p->name);
3314 if (!EQ (values_here[p->idx], new_value))
3315 break;
3316 }
3317
3318 if (p->handler)
3319 break;
3320 }
3321
3322 if (!NULL_INTERVAL_P (next_iv))
3323 {
3324 if (INTEGERP (limit)
3325 && next_iv->position >= XFASTINT (limit))
3326 /* No text property change up to limit. */
3327 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3328 else
3329 /* Text properties change in next_iv. */
3330 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3331 }
3332 }
3333
3334 if (it->cmp_it.id < 0)
3335 {
3336 ptrdiff_t stoppos = it->end_charpos;
3337
3338 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3339 stoppos = -1;
3340 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3341 stoppos, it->string);
3342 }
3343
3344 xassert (STRINGP (it->string)
3345 || (it->stop_charpos >= BEGV
3346 && it->stop_charpos >= IT_CHARPOS (*it)));
3347 }
3348
3349
3350 /* Return the position of the next overlay change after POS in
3351 current_buffer. Value is point-max if no overlay change
3352 follows. This is like `next-overlay-change' but doesn't use
3353 xmalloc. */
3354
3355 static ptrdiff_t
3356 next_overlay_change (ptrdiff_t pos)
3357 {
3358 ptrdiff_t i, noverlays;
3359 ptrdiff_t endpos;
3360 Lisp_Object *overlays;
3361
3362 /* Get all overlays at the given position. */
3363 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3364
3365 /* If any of these overlays ends before endpos,
3366 use its ending point instead. */
3367 for (i = 0; i < noverlays; ++i)
3368 {
3369 Lisp_Object oend;
3370 ptrdiff_t oendpos;
3371
3372 oend = OVERLAY_END (overlays[i]);
3373 oendpos = OVERLAY_POSITION (oend);
3374 endpos = min (endpos, oendpos);
3375 }
3376
3377 return endpos;
3378 }
3379
3380 /* How many characters forward to search for a display property or
3381 display string. Searching too far forward makes the bidi display
3382 sluggish, especially in small windows. */
3383 #define MAX_DISP_SCAN 250
3384
3385 /* Return the character position of a display string at or after
3386 position specified by POSITION. If no display string exists at or
3387 after POSITION, return ZV. A display string is either an overlay
3388 with `display' property whose value is a string, or a `display'
3389 text property whose value is a string. STRING is data about the
3390 string to iterate; if STRING->lstring is nil, we are iterating a
3391 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3392 on a GUI frame. DISP_PROP is set to zero if we searched
3393 MAX_DISP_SCAN characters forward without finding any display
3394 strings, non-zero otherwise. It is set to 2 if the display string
3395 uses any kind of `(space ...)' spec that will produce a stretch of
3396 white space in the text area. */
3397 ptrdiff_t
3398 compute_display_string_pos (struct text_pos *position,
3399 struct bidi_string_data *string,
3400 int frame_window_p, int *disp_prop)
3401 {
3402 /* OBJECT = nil means current buffer. */
3403 Lisp_Object object =
3404 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3405 Lisp_Object pos, spec, limpos;
3406 int string_p = (string && (STRINGP (string->lstring) || string->s));
3407 ptrdiff_t eob = string_p ? string->schars : ZV;
3408 ptrdiff_t begb = string_p ? 0 : BEGV;
3409 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3410 ptrdiff_t lim =
3411 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3412 struct text_pos tpos;
3413 int rv = 0;
3414
3415 *disp_prop = 1;
3416
3417 if (charpos >= eob
3418 /* We don't support display properties whose values are strings
3419 that have display string properties. */
3420 || string->from_disp_str
3421 /* C strings cannot have display properties. */
3422 || (string->s && !STRINGP (object)))
3423 {
3424 *disp_prop = 0;
3425 return eob;
3426 }
3427
3428 /* If the character at CHARPOS is where the display string begins,
3429 return CHARPOS. */
3430 pos = make_number (charpos);
3431 if (STRINGP (object))
3432 bufpos = string->bufpos;
3433 else
3434 bufpos = charpos;
3435 tpos = *position;
3436 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3437 && (charpos <= begb
3438 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3439 object),
3440 spec))
3441 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3442 frame_window_p)))
3443 {
3444 if (rv == 2)
3445 *disp_prop = 2;
3446 return charpos;
3447 }
3448
3449 /* Look forward for the first character with a `display' property
3450 that will replace the underlying text when displayed. */
3451 limpos = make_number (lim);
3452 do {
3453 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3454 CHARPOS (tpos) = XFASTINT (pos);
3455 if (CHARPOS (tpos) >= lim)
3456 {
3457 *disp_prop = 0;
3458 break;
3459 }
3460 if (STRINGP (object))
3461 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3462 else
3463 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3464 spec = Fget_char_property (pos, Qdisplay, object);
3465 if (!STRINGP (object))
3466 bufpos = CHARPOS (tpos);
3467 } while (NILP (spec)
3468 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3469 bufpos, frame_window_p)));
3470 if (rv == 2)
3471 *disp_prop = 2;
3472
3473 return CHARPOS (tpos);
3474 }
3475
3476 /* Return the character position of the end of the display string that
3477 started at CHARPOS. If there's no display string at CHARPOS,
3478 return -1. A display string is either an overlay with `display'
3479 property whose value is a string or a `display' text property whose
3480 value is a string. */
3481 ptrdiff_t
3482 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3483 {
3484 /* OBJECT = nil means current buffer. */
3485 Lisp_Object object =
3486 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3487 Lisp_Object pos = make_number (charpos);
3488 ptrdiff_t eob =
3489 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3490
3491 if (charpos >= eob || (string->s && !STRINGP (object)))
3492 return eob;
3493
3494 /* It could happen that the display property or overlay was removed
3495 since we found it in compute_display_string_pos above. One way
3496 this can happen is if JIT font-lock was called (through
3497 handle_fontified_prop), and jit-lock-functions remove text
3498 properties or overlays from the portion of buffer that includes
3499 CHARPOS. Muse mode is known to do that, for example. In this
3500 case, we return -1 to the caller, to signal that no display
3501 string is actually present at CHARPOS. See bidi_fetch_char for
3502 how this is handled.
3503
3504 An alternative would be to never look for display properties past
3505 it->stop_charpos. But neither compute_display_string_pos nor
3506 bidi_fetch_char that calls it know or care where the next
3507 stop_charpos is. */
3508 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3509 return -1;
3510
3511 /* Look forward for the first character where the `display' property
3512 changes. */
3513 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3514
3515 return XFASTINT (pos);
3516 }
3517
3518
3519 \f
3520 /***********************************************************************
3521 Fontification
3522 ***********************************************************************/
3523
3524 /* Handle changes in the `fontified' property of the current buffer by
3525 calling hook functions from Qfontification_functions to fontify
3526 regions of text. */
3527
3528 static enum prop_handled
3529 handle_fontified_prop (struct it *it)
3530 {
3531 Lisp_Object prop, pos;
3532 enum prop_handled handled = HANDLED_NORMALLY;
3533
3534 if (!NILP (Vmemory_full))
3535 return handled;
3536
3537 /* Get the value of the `fontified' property at IT's current buffer
3538 position. (The `fontified' property doesn't have a special
3539 meaning in strings.) If the value is nil, call functions from
3540 Qfontification_functions. */
3541 if (!STRINGP (it->string)
3542 && it->s == NULL
3543 && !NILP (Vfontification_functions)
3544 && !NILP (Vrun_hooks)
3545 && (pos = make_number (IT_CHARPOS (*it)),
3546 prop = Fget_char_property (pos, Qfontified, Qnil),
3547 /* Ignore the special cased nil value always present at EOB since
3548 no amount of fontifying will be able to change it. */
3549 NILP (prop) && IT_CHARPOS (*it) < Z))
3550 {
3551 ptrdiff_t count = SPECPDL_INDEX ();
3552 Lisp_Object val;
3553 struct buffer *obuf = current_buffer;
3554 int begv = BEGV, zv = ZV;
3555 int old_clip_changed = current_buffer->clip_changed;
3556
3557 val = Vfontification_functions;
3558 specbind (Qfontification_functions, Qnil);
3559
3560 xassert (it->end_charpos == ZV);
3561
3562 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3563 safe_call1 (val, pos);
3564 else
3565 {
3566 Lisp_Object fns, fn;
3567 struct gcpro gcpro1, gcpro2;
3568
3569 fns = Qnil;
3570 GCPRO2 (val, fns);
3571
3572 for (; CONSP (val); val = XCDR (val))
3573 {
3574 fn = XCAR (val);
3575
3576 if (EQ (fn, Qt))
3577 {
3578 /* A value of t indicates this hook has a local
3579 binding; it means to run the global binding too.
3580 In a global value, t should not occur. If it
3581 does, we must ignore it to avoid an endless
3582 loop. */
3583 for (fns = Fdefault_value (Qfontification_functions);
3584 CONSP (fns);
3585 fns = XCDR (fns))
3586 {
3587 fn = XCAR (fns);
3588 if (!EQ (fn, Qt))
3589 safe_call1 (fn, pos);
3590 }
3591 }
3592 else
3593 safe_call1 (fn, pos);
3594 }
3595
3596 UNGCPRO;
3597 }
3598
3599 unbind_to (count, Qnil);
3600
3601 /* Fontification functions routinely call `save-restriction'.
3602 Normally, this tags clip_changed, which can confuse redisplay
3603 (see discussion in Bug#6671). Since we don't perform any
3604 special handling of fontification changes in the case where
3605 `save-restriction' isn't called, there's no point doing so in
3606 this case either. So, if the buffer's restrictions are
3607 actually left unchanged, reset clip_changed. */
3608 if (obuf == current_buffer)
3609 {
3610 if (begv == BEGV && zv == ZV)
3611 current_buffer->clip_changed = old_clip_changed;
3612 }
3613 /* There isn't much we can reasonably do to protect against
3614 misbehaving fontification, but here's a fig leaf. */
3615 else if (!NILP (BVAR (obuf, name)))
3616 set_buffer_internal_1 (obuf);
3617
3618 /* The fontification code may have added/removed text.
3619 It could do even a lot worse, but let's at least protect against
3620 the most obvious case where only the text past `pos' gets changed',
3621 as is/was done in grep.el where some escapes sequences are turned
3622 into face properties (bug#7876). */
3623 it->end_charpos = ZV;
3624
3625 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3626 something. This avoids an endless loop if they failed to
3627 fontify the text for which reason ever. */
3628 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3629 handled = HANDLED_RECOMPUTE_PROPS;
3630 }
3631
3632 return handled;
3633 }
3634
3635
3636 \f
3637 /***********************************************************************
3638 Faces
3639 ***********************************************************************/
3640
3641 /* Set up iterator IT from face properties at its current position.
3642 Called from handle_stop. */
3643
3644 static enum prop_handled
3645 handle_face_prop (struct it *it)
3646 {
3647 int new_face_id;
3648 ptrdiff_t next_stop;
3649
3650 if (!STRINGP (it->string))
3651 {
3652 new_face_id
3653 = face_at_buffer_position (it->w,
3654 IT_CHARPOS (*it),
3655 it->region_beg_charpos,
3656 it->region_end_charpos,
3657 &next_stop,
3658 (IT_CHARPOS (*it)
3659 + TEXT_PROP_DISTANCE_LIMIT),
3660 0, it->base_face_id);
3661
3662 /* Is this a start of a run of characters with box face?
3663 Caveat: this can be called for a freshly initialized
3664 iterator; face_id is -1 in this case. We know that the new
3665 face will not change until limit, i.e. if the new face has a
3666 box, all characters up to limit will have one. But, as
3667 usual, we don't know whether limit is really the end. */
3668 if (new_face_id != it->face_id)
3669 {
3670 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3671
3672 /* If new face has a box but old face has not, this is
3673 the start of a run of characters with box, i.e. it has
3674 a shadow on the left side. The value of face_id of the
3675 iterator will be -1 if this is the initial call that gets
3676 the face. In this case, we have to look in front of IT's
3677 position and see whether there is a face != new_face_id. */
3678 it->start_of_box_run_p
3679 = (new_face->box != FACE_NO_BOX
3680 && (it->face_id >= 0
3681 || IT_CHARPOS (*it) == BEG
3682 || new_face_id != face_before_it_pos (it)));
3683 it->face_box_p = new_face->box != FACE_NO_BOX;
3684 }
3685 }
3686 else
3687 {
3688 int base_face_id;
3689 ptrdiff_t bufpos;
3690 int i;
3691 Lisp_Object from_overlay
3692 = (it->current.overlay_string_index >= 0
3693 ? it->string_overlays[it->current.overlay_string_index]
3694 : Qnil);
3695
3696 /* See if we got to this string directly or indirectly from
3697 an overlay property. That includes the before-string or
3698 after-string of an overlay, strings in display properties
3699 provided by an overlay, their text properties, etc.
3700
3701 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3702 if (! NILP (from_overlay))
3703 for (i = it->sp - 1; i >= 0; i--)
3704 {
3705 if (it->stack[i].current.overlay_string_index >= 0)
3706 from_overlay
3707 = it->string_overlays[it->stack[i].current.overlay_string_index];
3708 else if (! NILP (it->stack[i].from_overlay))
3709 from_overlay = it->stack[i].from_overlay;
3710
3711 if (!NILP (from_overlay))
3712 break;
3713 }
3714
3715 if (! NILP (from_overlay))
3716 {
3717 bufpos = IT_CHARPOS (*it);
3718 /* For a string from an overlay, the base face depends
3719 only on text properties and ignores overlays. */
3720 base_face_id
3721 = face_for_overlay_string (it->w,
3722 IT_CHARPOS (*it),
3723 it->region_beg_charpos,
3724 it->region_end_charpos,
3725 &next_stop,
3726 (IT_CHARPOS (*it)
3727 + TEXT_PROP_DISTANCE_LIMIT),
3728 0,
3729 from_overlay);
3730 }
3731 else
3732 {
3733 bufpos = 0;
3734
3735 /* For strings from a `display' property, use the face at
3736 IT's current buffer position as the base face to merge
3737 with, so that overlay strings appear in the same face as
3738 surrounding text, unless they specify their own
3739 faces. */
3740 base_face_id = it->string_from_prefix_prop_p
3741 ? DEFAULT_FACE_ID
3742 : underlying_face_id (it);
3743 }
3744
3745 new_face_id = face_at_string_position (it->w,
3746 it->string,
3747 IT_STRING_CHARPOS (*it),
3748 bufpos,
3749 it->region_beg_charpos,
3750 it->region_end_charpos,
3751 &next_stop,
3752 base_face_id, 0);
3753
3754 /* Is this a start of a run of characters with box? Caveat:
3755 this can be called for a freshly allocated iterator; face_id
3756 is -1 is this case. We know that the new face will not
3757 change until the next check pos, i.e. if the new face has a
3758 box, all characters up to that position will have a
3759 box. But, as usual, we don't know whether that position
3760 is really the end. */
3761 if (new_face_id != it->face_id)
3762 {
3763 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3764 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3765
3766 /* If new face has a box but old face hasn't, this is the
3767 start of a run of characters with box, i.e. it has a
3768 shadow on the left side. */
3769 it->start_of_box_run_p
3770 = new_face->box && (old_face == NULL || !old_face->box);
3771 it->face_box_p = new_face->box != FACE_NO_BOX;
3772 }
3773 }
3774
3775 it->face_id = new_face_id;
3776 return HANDLED_NORMALLY;
3777 }
3778
3779
3780 /* Return the ID of the face ``underlying'' IT's current position,
3781 which is in a string. If the iterator is associated with a
3782 buffer, return the face at IT's current buffer position.
3783 Otherwise, use the iterator's base_face_id. */
3784
3785 static int
3786 underlying_face_id (struct it *it)
3787 {
3788 int face_id = it->base_face_id, i;
3789
3790 xassert (STRINGP (it->string));
3791
3792 for (i = it->sp - 1; i >= 0; --i)
3793 if (NILP (it->stack[i].string))
3794 face_id = it->stack[i].face_id;
3795
3796 return face_id;
3797 }
3798
3799
3800 /* Compute the face one character before or after the current position
3801 of IT, in the visual order. BEFORE_P non-zero means get the face
3802 in front (to the left in L2R paragraphs, to the right in R2L
3803 paragraphs) of IT's screen position. Value is the ID of the face. */
3804
3805 static int
3806 face_before_or_after_it_pos (struct it *it, int before_p)
3807 {
3808 int face_id, limit;
3809 ptrdiff_t next_check_charpos;
3810 struct it it_copy;
3811 void *it_copy_data = NULL;
3812
3813 xassert (it->s == NULL);
3814
3815 if (STRINGP (it->string))
3816 {
3817 ptrdiff_t bufpos, charpos;
3818 int base_face_id;
3819
3820 /* No face change past the end of the string (for the case
3821 we are padding with spaces). No face change before the
3822 string start. */
3823 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3824 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3825 return it->face_id;
3826
3827 if (!it->bidi_p)
3828 {
3829 /* Set charpos to the position before or after IT's current
3830 position, in the logical order, which in the non-bidi
3831 case is the same as the visual order. */
3832 if (before_p)
3833 charpos = IT_STRING_CHARPOS (*it) - 1;
3834 else if (it->what == IT_COMPOSITION)
3835 /* For composition, we must check the character after the
3836 composition. */
3837 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3838 else
3839 charpos = IT_STRING_CHARPOS (*it) + 1;
3840 }
3841 else
3842 {
3843 if (before_p)
3844 {
3845 /* With bidi iteration, the character before the current
3846 in the visual order cannot be found by simple
3847 iteration, because "reverse" reordering is not
3848 supported. Instead, we need to use the move_it_*
3849 family of functions. */
3850 /* Ignore face changes before the first visible
3851 character on this display line. */
3852 if (it->current_x <= it->first_visible_x)
3853 return it->face_id;
3854 SAVE_IT (it_copy, *it, it_copy_data);
3855 /* Implementation note: Since move_it_in_display_line
3856 works in the iterator geometry, and thinks the first
3857 character is always the leftmost, even in R2L lines,
3858 we don't need to distinguish between the R2L and L2R
3859 cases here. */
3860 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3861 it_copy.current_x - 1, MOVE_TO_X);
3862 charpos = IT_STRING_CHARPOS (it_copy);
3863 RESTORE_IT (it, it, it_copy_data);
3864 }
3865 else
3866 {
3867 /* Set charpos to the string position of the character
3868 that comes after IT's current position in the visual
3869 order. */
3870 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3871
3872 it_copy = *it;
3873 while (n--)
3874 bidi_move_to_visually_next (&it_copy.bidi_it);
3875
3876 charpos = it_copy.bidi_it.charpos;
3877 }
3878 }
3879 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3880
3881 if (it->current.overlay_string_index >= 0)
3882 bufpos = IT_CHARPOS (*it);
3883 else
3884 bufpos = 0;
3885
3886 base_face_id = underlying_face_id (it);
3887
3888 /* Get the face for ASCII, or unibyte. */
3889 face_id = face_at_string_position (it->w,
3890 it->string,
3891 charpos,
3892 bufpos,
3893 it->region_beg_charpos,
3894 it->region_end_charpos,
3895 &next_check_charpos,
3896 base_face_id, 0);
3897
3898 /* Correct the face for charsets different from ASCII. Do it
3899 for the multibyte case only. The face returned above is
3900 suitable for unibyte text if IT->string is unibyte. */
3901 if (STRING_MULTIBYTE (it->string))
3902 {
3903 struct text_pos pos1 = string_pos (charpos, it->string);
3904 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3905 int c, len;
3906 struct face *face = FACE_FROM_ID (it->f, face_id);
3907
3908 c = string_char_and_length (p, &len);
3909 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3910 }
3911 }
3912 else
3913 {
3914 struct text_pos pos;
3915
3916 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3917 || (IT_CHARPOS (*it) <= BEGV && before_p))
3918 return it->face_id;
3919
3920 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3921 pos = it->current.pos;
3922
3923 if (!it->bidi_p)
3924 {
3925 if (before_p)
3926 DEC_TEXT_POS (pos, it->multibyte_p);
3927 else
3928 {
3929 if (it->what == IT_COMPOSITION)
3930 {
3931 /* For composition, we must check the position after
3932 the composition. */
3933 pos.charpos += it->cmp_it.nchars;
3934 pos.bytepos += it->len;
3935 }
3936 else
3937 INC_TEXT_POS (pos, it->multibyte_p);
3938 }
3939 }
3940 else
3941 {
3942 if (before_p)
3943 {
3944 /* With bidi iteration, the character before the current
3945 in the visual order cannot be found by simple
3946 iteration, because "reverse" reordering is not
3947 supported. Instead, we need to use the move_it_*
3948 family of functions. */
3949 /* Ignore face changes before the first visible
3950 character on this display line. */
3951 if (it->current_x <= it->first_visible_x)
3952 return it->face_id;
3953 SAVE_IT (it_copy, *it, it_copy_data);
3954 /* Implementation note: Since move_it_in_display_line
3955 works in the iterator geometry, and thinks the first
3956 character is always the leftmost, even in R2L lines,
3957 we don't need to distinguish between the R2L and L2R
3958 cases here. */
3959 move_it_in_display_line (&it_copy, ZV,
3960 it_copy.current_x - 1, MOVE_TO_X);
3961 pos = it_copy.current.pos;
3962 RESTORE_IT (it, it, it_copy_data);
3963 }
3964 else
3965 {
3966 /* Set charpos to the buffer position of the character
3967 that comes after IT's current position in the visual
3968 order. */
3969 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3970
3971 it_copy = *it;
3972 while (n--)
3973 bidi_move_to_visually_next (&it_copy.bidi_it);
3974
3975 SET_TEXT_POS (pos,
3976 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3977 }
3978 }
3979 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3980
3981 /* Determine face for CHARSET_ASCII, or unibyte. */
3982 face_id = face_at_buffer_position (it->w,
3983 CHARPOS (pos),
3984 it->region_beg_charpos,
3985 it->region_end_charpos,
3986 &next_check_charpos,
3987 limit, 0, -1);
3988
3989 /* Correct the face for charsets different from ASCII. Do it
3990 for the multibyte case only. The face returned above is
3991 suitable for unibyte text if current_buffer is unibyte. */
3992 if (it->multibyte_p)
3993 {
3994 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3995 struct face *face = FACE_FROM_ID (it->f, face_id);
3996 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3997 }
3998 }
3999
4000 return face_id;
4001 }
4002
4003
4004 \f
4005 /***********************************************************************
4006 Invisible text
4007 ***********************************************************************/
4008
4009 /* Set up iterator IT from invisible properties at its current
4010 position. Called from handle_stop. */
4011
4012 static enum prop_handled
4013 handle_invisible_prop (struct it *it)
4014 {
4015 enum prop_handled handled = HANDLED_NORMALLY;
4016
4017 if (STRINGP (it->string))
4018 {
4019 Lisp_Object prop, end_charpos, limit, charpos;
4020
4021 /* Get the value of the invisible text property at the
4022 current position. Value will be nil if there is no such
4023 property. */
4024 charpos = make_number (IT_STRING_CHARPOS (*it));
4025 prop = Fget_text_property (charpos, Qinvisible, it->string);
4026
4027 if (!NILP (prop)
4028 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4029 {
4030 ptrdiff_t endpos;
4031
4032 handled = HANDLED_RECOMPUTE_PROPS;
4033
4034 /* Get the position at which the next change of the
4035 invisible text property can be found in IT->string.
4036 Value will be nil if the property value is the same for
4037 all the rest of IT->string. */
4038 XSETINT (limit, SCHARS (it->string));
4039 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4040 it->string, limit);
4041
4042 /* Text at current position is invisible. The next
4043 change in the property is at position end_charpos.
4044 Move IT's current position to that position. */
4045 if (INTEGERP (end_charpos)
4046 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4047 {
4048 struct text_pos old;
4049 ptrdiff_t oldpos;
4050
4051 old = it->current.string_pos;
4052 oldpos = CHARPOS (old);
4053 if (it->bidi_p)
4054 {
4055 if (it->bidi_it.first_elt
4056 && it->bidi_it.charpos < SCHARS (it->string))
4057 bidi_paragraph_init (it->paragraph_embedding,
4058 &it->bidi_it, 1);
4059 /* Bidi-iterate out of the invisible text. */
4060 do
4061 {
4062 bidi_move_to_visually_next (&it->bidi_it);
4063 }
4064 while (oldpos <= it->bidi_it.charpos
4065 && it->bidi_it.charpos < endpos);
4066
4067 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4068 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4069 if (IT_CHARPOS (*it) >= endpos)
4070 it->prev_stop = endpos;
4071 }
4072 else
4073 {
4074 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4075 compute_string_pos (&it->current.string_pos, old, it->string);
4076 }
4077 }
4078 else
4079 {
4080 /* The rest of the string is invisible. If this is an
4081 overlay string, proceed with the next overlay string
4082 or whatever comes and return a character from there. */
4083 if (it->current.overlay_string_index >= 0)
4084 {
4085 next_overlay_string (it);
4086 /* Don't check for overlay strings when we just
4087 finished processing them. */
4088 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4089 }
4090 else
4091 {
4092 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4093 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4094 }
4095 }
4096 }
4097 }
4098 else
4099 {
4100 int invis_p;
4101 ptrdiff_t newpos, next_stop, start_charpos, tem;
4102 Lisp_Object pos, prop, overlay;
4103
4104 /* First of all, is there invisible text at this position? */
4105 tem = start_charpos = IT_CHARPOS (*it);
4106 pos = make_number (tem);
4107 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4108 &overlay);
4109 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4110
4111 /* If we are on invisible text, skip over it. */
4112 if (invis_p && start_charpos < it->end_charpos)
4113 {
4114 /* Record whether we have to display an ellipsis for the
4115 invisible text. */
4116 int display_ellipsis_p = invis_p == 2;
4117
4118 handled = HANDLED_RECOMPUTE_PROPS;
4119
4120 /* Loop skipping over invisible text. The loop is left at
4121 ZV or with IT on the first char being visible again. */
4122 do
4123 {
4124 /* Try to skip some invisible text. Return value is the
4125 position reached which can be equal to where we start
4126 if there is nothing invisible there. This skips both
4127 over invisible text properties and overlays with
4128 invisible property. */
4129 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4130
4131 /* If we skipped nothing at all we weren't at invisible
4132 text in the first place. If everything to the end of
4133 the buffer was skipped, end the loop. */
4134 if (newpos == tem || newpos >= ZV)
4135 invis_p = 0;
4136 else
4137 {
4138 /* We skipped some characters but not necessarily
4139 all there are. Check if we ended up on visible
4140 text. Fget_char_property returns the property of
4141 the char before the given position, i.e. if we
4142 get invis_p = 0, this means that the char at
4143 newpos is visible. */
4144 pos = make_number (newpos);
4145 prop = Fget_char_property (pos, Qinvisible, it->window);
4146 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4147 }
4148
4149 /* If we ended up on invisible text, proceed to
4150 skip starting with next_stop. */
4151 if (invis_p)
4152 tem = next_stop;
4153
4154 /* If there are adjacent invisible texts, don't lose the
4155 second one's ellipsis. */
4156 if (invis_p == 2)
4157 display_ellipsis_p = 1;
4158 }
4159 while (invis_p);
4160
4161 /* The position newpos is now either ZV or on visible text. */
4162 if (it->bidi_p)
4163 {
4164 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4165 int on_newline =
4166 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4167 int after_newline =
4168 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4169
4170 /* If the invisible text ends on a newline or on a
4171 character after a newline, we can avoid the costly,
4172 character by character, bidi iteration to NEWPOS, and
4173 instead simply reseat the iterator there. That's
4174 because all bidi reordering information is tossed at
4175 the newline. This is a big win for modes that hide
4176 complete lines, like Outline, Org, etc. */
4177 if (on_newline || after_newline)
4178 {
4179 struct text_pos tpos;
4180 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4181
4182 SET_TEXT_POS (tpos, newpos, bpos);
4183 reseat_1 (it, tpos, 0);
4184 /* If we reseat on a newline/ZV, we need to prep the
4185 bidi iterator for advancing to the next character
4186 after the newline/EOB, keeping the current paragraph
4187 direction (so that PRODUCE_GLYPHS does TRT wrt
4188 prepending/appending glyphs to a glyph row). */
4189 if (on_newline)
4190 {
4191 it->bidi_it.first_elt = 0;
4192 it->bidi_it.paragraph_dir = pdir;
4193 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4194 it->bidi_it.nchars = 1;
4195 it->bidi_it.ch_len = 1;
4196 }
4197 }
4198 else /* Must use the slow method. */
4199 {
4200 /* With bidi iteration, the region of invisible text
4201 could start and/or end in the middle of a
4202 non-base embedding level. Therefore, we need to
4203 skip invisible text using the bidi iterator,
4204 starting at IT's current position, until we find
4205 ourselves outside of the invisible text.
4206 Skipping invisible text _after_ bidi iteration
4207 avoids affecting the visual order of the
4208 displayed text when invisible properties are
4209 added or removed. */
4210 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4211 {
4212 /* If we were `reseat'ed to a new paragraph,
4213 determine the paragraph base direction. We
4214 need to do it now because
4215 next_element_from_buffer may not have a
4216 chance to do it, if we are going to skip any
4217 text at the beginning, which resets the
4218 FIRST_ELT flag. */
4219 bidi_paragraph_init (it->paragraph_embedding,
4220 &it->bidi_it, 1);
4221 }
4222 do
4223 {
4224 bidi_move_to_visually_next (&it->bidi_it);
4225 }
4226 while (it->stop_charpos <= it->bidi_it.charpos
4227 && it->bidi_it.charpos < newpos);
4228 IT_CHARPOS (*it) = it->bidi_it.charpos;
4229 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4230 /* If we overstepped NEWPOS, record its position in
4231 the iterator, so that we skip invisible text if
4232 later the bidi iteration lands us in the
4233 invisible region again. */
4234 if (IT_CHARPOS (*it) >= newpos)
4235 it->prev_stop = newpos;
4236 }
4237 }
4238 else
4239 {
4240 IT_CHARPOS (*it) = newpos;
4241 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4242 }
4243
4244 /* If there are before-strings at the start of invisible
4245 text, and the text is invisible because of a text
4246 property, arrange to show before-strings because 20.x did
4247 it that way. (If the text is invisible because of an
4248 overlay property instead of a text property, this is
4249 already handled in the overlay code.) */
4250 if (NILP (overlay)
4251 && get_overlay_strings (it, it->stop_charpos))
4252 {
4253 handled = HANDLED_RECOMPUTE_PROPS;
4254 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4255 }
4256 else if (display_ellipsis_p)
4257 {
4258 /* Make sure that the glyphs of the ellipsis will get
4259 correct `charpos' values. If we would not update
4260 it->position here, the glyphs would belong to the
4261 last visible character _before_ the invisible
4262 text, which confuses `set_cursor_from_row'.
4263
4264 We use the last invisible position instead of the
4265 first because this way the cursor is always drawn on
4266 the first "." of the ellipsis, whenever PT is inside
4267 the invisible text. Otherwise the cursor would be
4268 placed _after_ the ellipsis when the point is after the
4269 first invisible character. */
4270 if (!STRINGP (it->object))
4271 {
4272 it->position.charpos = newpos - 1;
4273 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4274 }
4275 it->ellipsis_p = 1;
4276 /* Let the ellipsis display before
4277 considering any properties of the following char.
4278 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4279 handled = HANDLED_RETURN;
4280 }
4281 }
4282 }
4283
4284 return handled;
4285 }
4286
4287
4288 /* Make iterator IT return `...' next.
4289 Replaces LEN characters from buffer. */
4290
4291 static void
4292 setup_for_ellipsis (struct it *it, int len)
4293 {
4294 /* Use the display table definition for `...'. Invalid glyphs
4295 will be handled by the method returning elements from dpvec. */
4296 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4297 {
4298 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4299 it->dpvec = v->contents;
4300 it->dpend = v->contents + v->header.size;
4301 }
4302 else
4303 {
4304 /* Default `...'. */
4305 it->dpvec = default_invis_vector;
4306 it->dpend = default_invis_vector + 3;
4307 }
4308
4309 it->dpvec_char_len = len;
4310 it->current.dpvec_index = 0;
4311 it->dpvec_face_id = -1;
4312
4313 /* Remember the current face id in case glyphs specify faces.
4314 IT's face is restored in set_iterator_to_next.
4315 saved_face_id was set to preceding char's face in handle_stop. */
4316 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4317 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4318
4319 it->method = GET_FROM_DISPLAY_VECTOR;
4320 it->ellipsis_p = 1;
4321 }
4322
4323
4324 \f
4325 /***********************************************************************
4326 'display' property
4327 ***********************************************************************/
4328
4329 /* Set up iterator IT from `display' property at its current position.
4330 Called from handle_stop.
4331 We return HANDLED_RETURN if some part of the display property
4332 overrides the display of the buffer text itself.
4333 Otherwise we return HANDLED_NORMALLY. */
4334
4335 static enum prop_handled
4336 handle_display_prop (struct it *it)
4337 {
4338 Lisp_Object propval, object, overlay;
4339 struct text_pos *position;
4340 ptrdiff_t bufpos;
4341 /* Nonzero if some property replaces the display of the text itself. */
4342 int display_replaced_p = 0;
4343
4344 if (STRINGP (it->string))
4345 {
4346 object = it->string;
4347 position = &it->current.string_pos;
4348 bufpos = CHARPOS (it->current.pos);
4349 }
4350 else
4351 {
4352 XSETWINDOW (object, it->w);
4353 position = &it->current.pos;
4354 bufpos = CHARPOS (*position);
4355 }
4356
4357 /* Reset those iterator values set from display property values. */
4358 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4359 it->space_width = Qnil;
4360 it->font_height = Qnil;
4361 it->voffset = 0;
4362
4363 /* We don't support recursive `display' properties, i.e. string
4364 values that have a string `display' property, that have a string
4365 `display' property etc. */
4366 if (!it->string_from_display_prop_p)
4367 it->area = TEXT_AREA;
4368
4369 propval = get_char_property_and_overlay (make_number (position->charpos),
4370 Qdisplay, object, &overlay);
4371 if (NILP (propval))
4372 return HANDLED_NORMALLY;
4373 /* Now OVERLAY is the overlay that gave us this property, or nil
4374 if it was a text property. */
4375
4376 if (!STRINGP (it->string))
4377 object = it->w->buffer;
4378
4379 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4380 position, bufpos,
4381 FRAME_WINDOW_P (it->f));
4382
4383 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4384 }
4385
4386 /* Subroutine of handle_display_prop. Returns non-zero if the display
4387 specification in SPEC is a replacing specification, i.e. it would
4388 replace the text covered by `display' property with something else,
4389 such as an image or a display string. If SPEC includes any kind or
4390 `(space ...) specification, the value is 2; this is used by
4391 compute_display_string_pos, which see.
4392
4393 See handle_single_display_spec for documentation of arguments.
4394 frame_window_p is non-zero if the window being redisplayed is on a
4395 GUI frame; this argument is used only if IT is NULL, see below.
4396
4397 IT can be NULL, if this is called by the bidi reordering code
4398 through compute_display_string_pos, which see. In that case, this
4399 function only examines SPEC, but does not otherwise "handle" it, in
4400 the sense that it doesn't set up members of IT from the display
4401 spec. */
4402 static int
4403 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4404 Lisp_Object overlay, struct text_pos *position,
4405 ptrdiff_t bufpos, int frame_window_p)
4406 {
4407 int replacing_p = 0;
4408 int rv;
4409
4410 if (CONSP (spec)
4411 /* Simple specifications. */
4412 && !EQ (XCAR (spec), Qimage)
4413 && !EQ (XCAR (spec), Qspace)
4414 && !EQ (XCAR (spec), Qwhen)
4415 && !EQ (XCAR (spec), Qslice)
4416 && !EQ (XCAR (spec), Qspace_width)
4417 && !EQ (XCAR (spec), Qheight)
4418 && !EQ (XCAR (spec), Qraise)
4419 /* Marginal area specifications. */
4420 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4421 && !EQ (XCAR (spec), Qleft_fringe)
4422 && !EQ (XCAR (spec), Qright_fringe)
4423 && !NILP (XCAR (spec)))
4424 {
4425 for (; CONSP (spec); spec = XCDR (spec))
4426 {
4427 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4428 overlay, position, bufpos,
4429 replacing_p, frame_window_p)))
4430 {
4431 replacing_p = rv;
4432 /* If some text in a string is replaced, `position' no
4433 longer points to the position of `object'. */
4434 if (!it || STRINGP (object))
4435 break;
4436 }
4437 }
4438 }
4439 else if (VECTORP (spec))
4440 {
4441 ptrdiff_t i;
4442 for (i = 0; i < ASIZE (spec); ++i)
4443 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4444 overlay, position, bufpos,
4445 replacing_p, frame_window_p)))
4446 {
4447 replacing_p = rv;
4448 /* If some text in a string is replaced, `position' no
4449 longer points to the position of `object'. */
4450 if (!it || STRINGP (object))
4451 break;
4452 }
4453 }
4454 else
4455 {
4456 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4457 position, bufpos, 0,
4458 frame_window_p)))
4459 replacing_p = rv;
4460 }
4461
4462 return replacing_p;
4463 }
4464
4465 /* Value is the position of the end of the `display' property starting
4466 at START_POS in OBJECT. */
4467
4468 static struct text_pos
4469 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4470 {
4471 Lisp_Object end;
4472 struct text_pos end_pos;
4473
4474 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4475 Qdisplay, object, Qnil);
4476 CHARPOS (end_pos) = XFASTINT (end);
4477 if (STRINGP (object))
4478 compute_string_pos (&end_pos, start_pos, it->string);
4479 else
4480 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4481
4482 return end_pos;
4483 }
4484
4485
4486 /* Set up IT from a single `display' property specification SPEC. OBJECT
4487 is the object in which the `display' property was found. *POSITION
4488 is the position in OBJECT at which the `display' property was found.
4489 BUFPOS is the buffer position of OBJECT (different from POSITION if
4490 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4491 previously saw a display specification which already replaced text
4492 display with something else, for example an image; we ignore such
4493 properties after the first one has been processed.
4494
4495 OVERLAY is the overlay this `display' property came from,
4496 or nil if it was a text property.
4497
4498 If SPEC is a `space' or `image' specification, and in some other
4499 cases too, set *POSITION to the position where the `display'
4500 property ends.
4501
4502 If IT is NULL, only examine the property specification in SPEC, but
4503 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4504 is intended to be displayed in a window on a GUI frame.
4505
4506 Value is non-zero if something was found which replaces the display
4507 of buffer or string text. */
4508
4509 static int
4510 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4511 Lisp_Object overlay, struct text_pos *position,
4512 ptrdiff_t bufpos, int display_replaced_p,
4513 int frame_window_p)
4514 {
4515 Lisp_Object form;
4516 Lisp_Object location, value;
4517 struct text_pos start_pos = *position;
4518 int valid_p;
4519
4520 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4521 If the result is non-nil, use VALUE instead of SPEC. */
4522 form = Qt;
4523 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4524 {
4525 spec = XCDR (spec);
4526 if (!CONSP (spec))
4527 return 0;
4528 form = XCAR (spec);
4529 spec = XCDR (spec);
4530 }
4531
4532 if (!NILP (form) && !EQ (form, Qt))
4533 {
4534 ptrdiff_t count = SPECPDL_INDEX ();
4535 struct gcpro gcpro1;
4536
4537 /* Bind `object' to the object having the `display' property, a
4538 buffer or string. Bind `position' to the position in the
4539 object where the property was found, and `buffer-position'
4540 to the current position in the buffer. */
4541
4542 if (NILP (object))
4543 XSETBUFFER (object, current_buffer);
4544 specbind (Qobject, object);
4545 specbind (Qposition, make_number (CHARPOS (*position)));
4546 specbind (Qbuffer_position, make_number (bufpos));
4547 GCPRO1 (form);
4548 form = safe_eval (form);
4549 UNGCPRO;
4550 unbind_to (count, Qnil);
4551 }
4552
4553 if (NILP (form))
4554 return 0;
4555
4556 /* Handle `(height HEIGHT)' specifications. */
4557 if (CONSP (spec)
4558 && EQ (XCAR (spec), Qheight)
4559 && CONSP (XCDR (spec)))
4560 {
4561 if (it)
4562 {
4563 if (!FRAME_WINDOW_P (it->f))
4564 return 0;
4565
4566 it->font_height = XCAR (XCDR (spec));
4567 if (!NILP (it->font_height))
4568 {
4569 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4570 int new_height = -1;
4571
4572 if (CONSP (it->font_height)
4573 && (EQ (XCAR (it->font_height), Qplus)
4574 || EQ (XCAR (it->font_height), Qminus))
4575 && CONSP (XCDR (it->font_height))
4576 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4577 {
4578 /* `(+ N)' or `(- N)' where N is an integer. */
4579 int steps = XINT (XCAR (XCDR (it->font_height)));
4580 if (EQ (XCAR (it->font_height), Qplus))
4581 steps = - steps;
4582 it->face_id = smaller_face (it->f, it->face_id, steps);
4583 }
4584 else if (FUNCTIONP (it->font_height))
4585 {
4586 /* Call function with current height as argument.
4587 Value is the new height. */
4588 Lisp_Object height;
4589 height = safe_call1 (it->font_height,
4590 face->lface[LFACE_HEIGHT_INDEX]);
4591 if (NUMBERP (height))
4592 new_height = XFLOATINT (height);
4593 }
4594 else if (NUMBERP (it->font_height))
4595 {
4596 /* Value is a multiple of the canonical char height. */
4597 struct face *f;
4598
4599 f = FACE_FROM_ID (it->f,
4600 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4601 new_height = (XFLOATINT (it->font_height)
4602 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4603 }
4604 else
4605 {
4606 /* Evaluate IT->font_height with `height' bound to the
4607 current specified height to get the new height. */
4608 ptrdiff_t count = SPECPDL_INDEX ();
4609
4610 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4611 value = safe_eval (it->font_height);
4612 unbind_to (count, Qnil);
4613
4614 if (NUMBERP (value))
4615 new_height = XFLOATINT (value);
4616 }
4617
4618 if (new_height > 0)
4619 it->face_id = face_with_height (it->f, it->face_id, new_height);
4620 }
4621 }
4622
4623 return 0;
4624 }
4625
4626 /* Handle `(space-width WIDTH)'. */
4627 if (CONSP (spec)
4628 && EQ (XCAR (spec), Qspace_width)
4629 && CONSP (XCDR (spec)))
4630 {
4631 if (it)
4632 {
4633 if (!FRAME_WINDOW_P (it->f))
4634 return 0;
4635
4636 value = XCAR (XCDR (spec));
4637 if (NUMBERP (value) && XFLOATINT (value) > 0)
4638 it->space_width = value;
4639 }
4640
4641 return 0;
4642 }
4643
4644 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4645 if (CONSP (spec)
4646 && EQ (XCAR (spec), Qslice))
4647 {
4648 Lisp_Object tem;
4649
4650 if (it)
4651 {
4652 if (!FRAME_WINDOW_P (it->f))
4653 return 0;
4654
4655 if (tem = XCDR (spec), CONSP (tem))
4656 {
4657 it->slice.x = XCAR (tem);
4658 if (tem = XCDR (tem), CONSP (tem))
4659 {
4660 it->slice.y = XCAR (tem);
4661 if (tem = XCDR (tem), CONSP (tem))
4662 {
4663 it->slice.width = XCAR (tem);
4664 if (tem = XCDR (tem), CONSP (tem))
4665 it->slice.height = XCAR (tem);
4666 }
4667 }
4668 }
4669 }
4670
4671 return 0;
4672 }
4673
4674 /* Handle `(raise FACTOR)'. */
4675 if (CONSP (spec)
4676 && EQ (XCAR (spec), Qraise)
4677 && CONSP (XCDR (spec)))
4678 {
4679 if (it)
4680 {
4681 if (!FRAME_WINDOW_P (it->f))
4682 return 0;
4683
4684 #ifdef HAVE_WINDOW_SYSTEM
4685 value = XCAR (XCDR (spec));
4686 if (NUMBERP (value))
4687 {
4688 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4689 it->voffset = - (XFLOATINT (value)
4690 * (FONT_HEIGHT (face->font)));
4691 }
4692 #endif /* HAVE_WINDOW_SYSTEM */
4693 }
4694
4695 return 0;
4696 }
4697
4698 /* Don't handle the other kinds of display specifications
4699 inside a string that we got from a `display' property. */
4700 if (it && it->string_from_display_prop_p)
4701 return 0;
4702
4703 /* Characters having this form of property are not displayed, so
4704 we have to find the end of the property. */
4705 if (it)
4706 {
4707 start_pos = *position;
4708 *position = display_prop_end (it, object, start_pos);
4709 }
4710 value = Qnil;
4711
4712 /* Stop the scan at that end position--we assume that all
4713 text properties change there. */
4714 if (it)
4715 it->stop_charpos = position->charpos;
4716
4717 /* Handle `(left-fringe BITMAP [FACE])'
4718 and `(right-fringe BITMAP [FACE])'. */
4719 if (CONSP (spec)
4720 && (EQ (XCAR (spec), Qleft_fringe)
4721 || EQ (XCAR (spec), Qright_fringe))
4722 && CONSP (XCDR (spec)))
4723 {
4724 int fringe_bitmap;
4725
4726 if (it)
4727 {
4728 if (!FRAME_WINDOW_P (it->f))
4729 /* If we return here, POSITION has been advanced
4730 across the text with this property. */
4731 {
4732 /* Synchronize the bidi iterator with POSITION. This is
4733 needed because we are not going to push the iterator
4734 on behalf of this display property, so there will be
4735 no pop_it call to do this synchronization for us. */
4736 if (it->bidi_p)
4737 {
4738 it->position = *position;
4739 iterate_out_of_display_property (it);
4740 *position = it->position;
4741 }
4742 return 1;
4743 }
4744 }
4745 else if (!frame_window_p)
4746 return 1;
4747
4748 #ifdef HAVE_WINDOW_SYSTEM
4749 value = XCAR (XCDR (spec));
4750 if (!SYMBOLP (value)
4751 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4752 /* If we return here, POSITION has been advanced
4753 across the text with this property. */
4754 {
4755 if (it && it->bidi_p)
4756 {
4757 it->position = *position;
4758 iterate_out_of_display_property (it);
4759 *position = it->position;
4760 }
4761 return 1;
4762 }
4763
4764 if (it)
4765 {
4766 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4767
4768 if (CONSP (XCDR (XCDR (spec))))
4769 {
4770 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4771 int face_id2 = lookup_derived_face (it->f, face_name,
4772 FRINGE_FACE_ID, 0);
4773 if (face_id2 >= 0)
4774 face_id = face_id2;
4775 }
4776
4777 /* Save current settings of IT so that we can restore them
4778 when we are finished with the glyph property value. */
4779 push_it (it, position);
4780
4781 it->area = TEXT_AREA;
4782 it->what = IT_IMAGE;
4783 it->image_id = -1; /* no image */
4784 it->position = start_pos;
4785 it->object = NILP (object) ? it->w->buffer : object;
4786 it->method = GET_FROM_IMAGE;
4787 it->from_overlay = Qnil;
4788 it->face_id = face_id;
4789 it->from_disp_prop_p = 1;
4790
4791 /* Say that we haven't consumed the characters with
4792 `display' property yet. The call to pop_it in
4793 set_iterator_to_next will clean this up. */
4794 *position = start_pos;
4795
4796 if (EQ (XCAR (spec), Qleft_fringe))
4797 {
4798 it->left_user_fringe_bitmap = fringe_bitmap;
4799 it->left_user_fringe_face_id = face_id;
4800 }
4801 else
4802 {
4803 it->right_user_fringe_bitmap = fringe_bitmap;
4804 it->right_user_fringe_face_id = face_id;
4805 }
4806 }
4807 #endif /* HAVE_WINDOW_SYSTEM */
4808 return 1;
4809 }
4810
4811 /* Prepare to handle `((margin left-margin) ...)',
4812 `((margin right-margin) ...)' and `((margin nil) ...)'
4813 prefixes for display specifications. */
4814 location = Qunbound;
4815 if (CONSP (spec) && CONSP (XCAR (spec)))
4816 {
4817 Lisp_Object tem;
4818
4819 value = XCDR (spec);
4820 if (CONSP (value))
4821 value = XCAR (value);
4822
4823 tem = XCAR (spec);
4824 if (EQ (XCAR (tem), Qmargin)
4825 && (tem = XCDR (tem),
4826 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4827 (NILP (tem)
4828 || EQ (tem, Qleft_margin)
4829 || EQ (tem, Qright_margin))))
4830 location = tem;
4831 }
4832
4833 if (EQ (location, Qunbound))
4834 {
4835 location = Qnil;
4836 value = spec;
4837 }
4838
4839 /* After this point, VALUE is the property after any
4840 margin prefix has been stripped. It must be a string,
4841 an image specification, or `(space ...)'.
4842
4843 LOCATION specifies where to display: `left-margin',
4844 `right-margin' or nil. */
4845
4846 valid_p = (STRINGP (value)
4847 #ifdef HAVE_WINDOW_SYSTEM
4848 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4849 && valid_image_p (value))
4850 #endif /* not HAVE_WINDOW_SYSTEM */
4851 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4852
4853 if (valid_p && !display_replaced_p)
4854 {
4855 int retval = 1;
4856
4857 if (!it)
4858 {
4859 /* Callers need to know whether the display spec is any kind
4860 of `(space ...)' spec that is about to affect text-area
4861 display. */
4862 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4863 retval = 2;
4864 return retval;
4865 }
4866
4867 /* Save current settings of IT so that we can restore them
4868 when we are finished with the glyph property value. */
4869 push_it (it, position);
4870 it->from_overlay = overlay;
4871 it->from_disp_prop_p = 1;
4872
4873 if (NILP (location))
4874 it->area = TEXT_AREA;
4875 else if (EQ (location, Qleft_margin))
4876 it->area = LEFT_MARGIN_AREA;
4877 else
4878 it->area = RIGHT_MARGIN_AREA;
4879
4880 if (STRINGP (value))
4881 {
4882 it->string = value;
4883 it->multibyte_p = STRING_MULTIBYTE (it->string);
4884 it->current.overlay_string_index = -1;
4885 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4886 it->end_charpos = it->string_nchars = SCHARS (it->string);
4887 it->method = GET_FROM_STRING;
4888 it->stop_charpos = 0;
4889 it->prev_stop = 0;
4890 it->base_level_stop = 0;
4891 it->string_from_display_prop_p = 1;
4892 /* Say that we haven't consumed the characters with
4893 `display' property yet. The call to pop_it in
4894 set_iterator_to_next will clean this up. */
4895 if (BUFFERP (object))
4896 *position = start_pos;
4897
4898 /* Force paragraph direction to be that of the parent
4899 object. If the parent object's paragraph direction is
4900 not yet determined, default to L2R. */
4901 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4902 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4903 else
4904 it->paragraph_embedding = L2R;
4905
4906 /* Set up the bidi iterator for this display string. */
4907 if (it->bidi_p)
4908 {
4909 it->bidi_it.string.lstring = it->string;
4910 it->bidi_it.string.s = NULL;
4911 it->bidi_it.string.schars = it->end_charpos;
4912 it->bidi_it.string.bufpos = bufpos;
4913 it->bidi_it.string.from_disp_str = 1;
4914 it->bidi_it.string.unibyte = !it->multibyte_p;
4915 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4916 }
4917 }
4918 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4919 {
4920 it->method = GET_FROM_STRETCH;
4921 it->object = value;
4922 *position = it->position = start_pos;
4923 retval = 1 + (it->area == TEXT_AREA);
4924 }
4925 #ifdef HAVE_WINDOW_SYSTEM
4926 else
4927 {
4928 it->what = IT_IMAGE;
4929 it->image_id = lookup_image (it->f, value);
4930 it->position = start_pos;
4931 it->object = NILP (object) ? it->w->buffer : object;
4932 it->method = GET_FROM_IMAGE;
4933
4934 /* Say that we haven't consumed the characters with
4935 `display' property yet. The call to pop_it in
4936 set_iterator_to_next will clean this up. */
4937 *position = start_pos;
4938 }
4939 #endif /* HAVE_WINDOW_SYSTEM */
4940
4941 return retval;
4942 }
4943
4944 /* Invalid property or property not supported. Restore
4945 POSITION to what it was before. */
4946 *position = start_pos;
4947 return 0;
4948 }
4949
4950 /* Check if PROP is a display property value whose text should be
4951 treated as intangible. OVERLAY is the overlay from which PROP
4952 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4953 specify the buffer position covered by PROP. */
4954
4955 int
4956 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4957 ptrdiff_t charpos, ptrdiff_t bytepos)
4958 {
4959 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4960 struct text_pos position;
4961
4962 SET_TEXT_POS (position, charpos, bytepos);
4963 return handle_display_spec (NULL, prop, Qnil, overlay,
4964 &position, charpos, frame_window_p);
4965 }
4966
4967
4968 /* Return 1 if PROP is a display sub-property value containing STRING.
4969
4970 Implementation note: this and the following function are really
4971 special cases of handle_display_spec and
4972 handle_single_display_spec, and should ideally use the same code.
4973 Until they do, these two pairs must be consistent and must be
4974 modified in sync. */
4975
4976 static int
4977 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4978 {
4979 if (EQ (string, prop))
4980 return 1;
4981
4982 /* Skip over `when FORM'. */
4983 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4984 {
4985 prop = XCDR (prop);
4986 if (!CONSP (prop))
4987 return 0;
4988 /* Actually, the condition following `when' should be eval'ed,
4989 like handle_single_display_spec does, and we should return
4990 zero if it evaluates to nil. However, this function is
4991 called only when the buffer was already displayed and some
4992 glyph in the glyph matrix was found to come from a display
4993 string. Therefore, the condition was already evaluated, and
4994 the result was non-nil, otherwise the display string wouldn't
4995 have been displayed and we would have never been called for
4996 this property. Thus, we can skip the evaluation and assume
4997 its result is non-nil. */
4998 prop = XCDR (prop);
4999 }
5000
5001 if (CONSP (prop))
5002 /* Skip over `margin LOCATION'. */
5003 if (EQ (XCAR (prop), Qmargin))
5004 {
5005 prop = XCDR (prop);
5006 if (!CONSP (prop))
5007 return 0;
5008
5009 prop = XCDR (prop);
5010 if (!CONSP (prop))
5011 return 0;
5012 }
5013
5014 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5015 }
5016
5017
5018 /* Return 1 if STRING appears in the `display' property PROP. */
5019
5020 static int
5021 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5022 {
5023 if (CONSP (prop)
5024 && !EQ (XCAR (prop), Qwhen)
5025 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5026 {
5027 /* A list of sub-properties. */
5028 while (CONSP (prop))
5029 {
5030 if (single_display_spec_string_p (XCAR (prop), string))
5031 return 1;
5032 prop = XCDR (prop);
5033 }
5034 }
5035 else if (VECTORP (prop))
5036 {
5037 /* A vector of sub-properties. */
5038 ptrdiff_t i;
5039 for (i = 0; i < ASIZE (prop); ++i)
5040 if (single_display_spec_string_p (AREF (prop, i), string))
5041 return 1;
5042 }
5043 else
5044 return single_display_spec_string_p (prop, string);
5045
5046 return 0;
5047 }
5048
5049 /* Look for STRING in overlays and text properties in the current
5050 buffer, between character positions FROM and TO (excluding TO).
5051 BACK_P non-zero means look back (in this case, TO is supposed to be
5052 less than FROM).
5053 Value is the first character position where STRING was found, or
5054 zero if it wasn't found before hitting TO.
5055
5056 This function may only use code that doesn't eval because it is
5057 called asynchronously from note_mouse_highlight. */
5058
5059 static ptrdiff_t
5060 string_buffer_position_lim (Lisp_Object string,
5061 ptrdiff_t from, ptrdiff_t to, int back_p)
5062 {
5063 Lisp_Object limit, prop, pos;
5064 int found = 0;
5065
5066 pos = make_number (max (from, BEGV));
5067
5068 if (!back_p) /* looking forward */
5069 {
5070 limit = make_number (min (to, ZV));
5071 while (!found && !EQ (pos, limit))
5072 {
5073 prop = Fget_char_property (pos, Qdisplay, Qnil);
5074 if (!NILP (prop) && display_prop_string_p (prop, string))
5075 found = 1;
5076 else
5077 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5078 limit);
5079 }
5080 }
5081 else /* looking back */
5082 {
5083 limit = make_number (max (to, BEGV));
5084 while (!found && !EQ (pos, limit))
5085 {
5086 prop = Fget_char_property (pos, Qdisplay, Qnil);
5087 if (!NILP (prop) && display_prop_string_p (prop, string))
5088 found = 1;
5089 else
5090 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5091 limit);
5092 }
5093 }
5094
5095 return found ? XINT (pos) : 0;
5096 }
5097
5098 /* Determine which buffer position in current buffer STRING comes from.
5099 AROUND_CHARPOS is an approximate position where it could come from.
5100 Value is the buffer position or 0 if it couldn't be determined.
5101
5102 This function is necessary because we don't record buffer positions
5103 in glyphs generated from strings (to keep struct glyph small).
5104 This function may only use code that doesn't eval because it is
5105 called asynchronously from note_mouse_highlight. */
5106
5107 static ptrdiff_t
5108 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5109 {
5110 const int MAX_DISTANCE = 1000;
5111 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5112 around_charpos + MAX_DISTANCE,
5113 0);
5114
5115 if (!found)
5116 found = string_buffer_position_lim (string, around_charpos,
5117 around_charpos - MAX_DISTANCE, 1);
5118 return found;
5119 }
5120
5121
5122 \f
5123 /***********************************************************************
5124 `composition' property
5125 ***********************************************************************/
5126
5127 /* Set up iterator IT from `composition' property at its current
5128 position. Called from handle_stop. */
5129
5130 static enum prop_handled
5131 handle_composition_prop (struct it *it)
5132 {
5133 Lisp_Object prop, string;
5134 ptrdiff_t pos, pos_byte, start, end;
5135
5136 if (STRINGP (it->string))
5137 {
5138 unsigned char *s;
5139
5140 pos = IT_STRING_CHARPOS (*it);
5141 pos_byte = IT_STRING_BYTEPOS (*it);
5142 string = it->string;
5143 s = SDATA (string) + pos_byte;
5144 it->c = STRING_CHAR (s);
5145 }
5146 else
5147 {
5148 pos = IT_CHARPOS (*it);
5149 pos_byte = IT_BYTEPOS (*it);
5150 string = Qnil;
5151 it->c = FETCH_CHAR (pos_byte);
5152 }
5153
5154 /* If there's a valid composition and point is not inside of the
5155 composition (in the case that the composition is from the current
5156 buffer), draw a glyph composed from the composition components. */
5157 if (find_composition (pos, -1, &start, &end, &prop, string)
5158 && COMPOSITION_VALID_P (start, end, prop)
5159 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5160 {
5161 if (start < pos)
5162 /* As we can't handle this situation (perhaps font-lock added
5163 a new composition), we just return here hoping that next
5164 redisplay will detect this composition much earlier. */
5165 return HANDLED_NORMALLY;
5166 if (start != pos)
5167 {
5168 if (STRINGP (it->string))
5169 pos_byte = string_char_to_byte (it->string, start);
5170 else
5171 pos_byte = CHAR_TO_BYTE (start);
5172 }
5173 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5174 prop, string);
5175
5176 if (it->cmp_it.id >= 0)
5177 {
5178 it->cmp_it.ch = -1;
5179 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5180 it->cmp_it.nglyphs = -1;
5181 }
5182 }
5183
5184 return HANDLED_NORMALLY;
5185 }
5186
5187
5188 \f
5189 /***********************************************************************
5190 Overlay strings
5191 ***********************************************************************/
5192
5193 /* The following structure is used to record overlay strings for
5194 later sorting in load_overlay_strings. */
5195
5196 struct overlay_entry
5197 {
5198 Lisp_Object overlay;
5199 Lisp_Object string;
5200 EMACS_INT priority;
5201 int after_string_p;
5202 };
5203
5204
5205 /* Set up iterator IT from overlay strings at its current position.
5206 Called from handle_stop. */
5207
5208 static enum prop_handled
5209 handle_overlay_change (struct it *it)
5210 {
5211 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5212 return HANDLED_RECOMPUTE_PROPS;
5213 else
5214 return HANDLED_NORMALLY;
5215 }
5216
5217
5218 /* Set up the next overlay string for delivery by IT, if there is an
5219 overlay string to deliver. Called by set_iterator_to_next when the
5220 end of the current overlay string is reached. If there are more
5221 overlay strings to display, IT->string and
5222 IT->current.overlay_string_index are set appropriately here.
5223 Otherwise IT->string is set to nil. */
5224
5225 static void
5226 next_overlay_string (struct it *it)
5227 {
5228 ++it->current.overlay_string_index;
5229 if (it->current.overlay_string_index == it->n_overlay_strings)
5230 {
5231 /* No more overlay strings. Restore IT's settings to what
5232 they were before overlay strings were processed, and
5233 continue to deliver from current_buffer. */
5234
5235 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5236 pop_it (it);
5237 xassert (it->sp > 0
5238 || (NILP (it->string)
5239 && it->method == GET_FROM_BUFFER
5240 && it->stop_charpos >= BEGV
5241 && it->stop_charpos <= it->end_charpos));
5242 it->current.overlay_string_index = -1;
5243 it->n_overlay_strings = 0;
5244 it->overlay_strings_charpos = -1;
5245 /* If there's an empty display string on the stack, pop the
5246 stack, to resync the bidi iterator with IT's position. Such
5247 empty strings are pushed onto the stack in
5248 get_overlay_strings_1. */
5249 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5250 pop_it (it);
5251
5252 /* If we're at the end of the buffer, record that we have
5253 processed the overlay strings there already, so that
5254 next_element_from_buffer doesn't try it again. */
5255 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5256 it->overlay_strings_at_end_processed_p = 1;
5257 }
5258 else
5259 {
5260 /* There are more overlay strings to process. If
5261 IT->current.overlay_string_index has advanced to a position
5262 where we must load IT->overlay_strings with more strings, do
5263 it. We must load at the IT->overlay_strings_charpos where
5264 IT->n_overlay_strings was originally computed; when invisible
5265 text is present, this might not be IT_CHARPOS (Bug#7016). */
5266 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5267
5268 if (it->current.overlay_string_index && i == 0)
5269 load_overlay_strings (it, it->overlay_strings_charpos);
5270
5271 /* Initialize IT to deliver display elements from the overlay
5272 string. */
5273 it->string = it->overlay_strings[i];
5274 it->multibyte_p = STRING_MULTIBYTE (it->string);
5275 SET_TEXT_POS (it->current.string_pos, 0, 0);
5276 it->method = GET_FROM_STRING;
5277 it->stop_charpos = 0;
5278 if (it->cmp_it.stop_pos >= 0)
5279 it->cmp_it.stop_pos = 0;
5280 it->prev_stop = 0;
5281 it->base_level_stop = 0;
5282
5283 /* Set up the bidi iterator for this overlay string. */
5284 if (it->bidi_p)
5285 {
5286 it->bidi_it.string.lstring = it->string;
5287 it->bidi_it.string.s = NULL;
5288 it->bidi_it.string.schars = SCHARS (it->string);
5289 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5290 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5291 it->bidi_it.string.unibyte = !it->multibyte_p;
5292 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5293 }
5294 }
5295
5296 CHECK_IT (it);
5297 }
5298
5299
5300 /* Compare two overlay_entry structures E1 and E2. Used as a
5301 comparison function for qsort in load_overlay_strings. Overlay
5302 strings for the same position are sorted so that
5303
5304 1. All after-strings come in front of before-strings, except
5305 when they come from the same overlay.
5306
5307 2. Within after-strings, strings are sorted so that overlay strings
5308 from overlays with higher priorities come first.
5309
5310 2. Within before-strings, strings are sorted so that overlay
5311 strings from overlays with higher priorities come last.
5312
5313 Value is analogous to strcmp. */
5314
5315
5316 static int
5317 compare_overlay_entries (const void *e1, const void *e2)
5318 {
5319 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5320 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5321 int result;
5322
5323 if (entry1->after_string_p != entry2->after_string_p)
5324 {
5325 /* Let after-strings appear in front of before-strings if
5326 they come from different overlays. */
5327 if (EQ (entry1->overlay, entry2->overlay))
5328 result = entry1->after_string_p ? 1 : -1;
5329 else
5330 result = entry1->after_string_p ? -1 : 1;
5331 }
5332 else if (entry1->priority != entry2->priority)
5333 {
5334 if (entry1->after_string_p)
5335 /* After-strings sorted in order of decreasing priority. */
5336 result = entry2->priority < entry1->priority ? -1 : 1;
5337 else
5338 /* Before-strings sorted in order of increasing priority. */
5339 result = entry1->priority < entry2->priority ? -1 : 1;
5340 }
5341 else
5342 result = 0;
5343
5344 return result;
5345 }
5346
5347
5348 /* Load the vector IT->overlay_strings with overlay strings from IT's
5349 current buffer position, or from CHARPOS if that is > 0. Set
5350 IT->n_overlays to the total number of overlay strings found.
5351
5352 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5353 a time. On entry into load_overlay_strings,
5354 IT->current.overlay_string_index gives the number of overlay
5355 strings that have already been loaded by previous calls to this
5356 function.
5357
5358 IT->add_overlay_start contains an additional overlay start
5359 position to consider for taking overlay strings from, if non-zero.
5360 This position comes into play when the overlay has an `invisible'
5361 property, and both before and after-strings. When we've skipped to
5362 the end of the overlay, because of its `invisible' property, we
5363 nevertheless want its before-string to appear.
5364 IT->add_overlay_start will contain the overlay start position
5365 in this case.
5366
5367 Overlay strings are sorted so that after-string strings come in
5368 front of before-string strings. Within before and after-strings,
5369 strings are sorted by overlay priority. See also function
5370 compare_overlay_entries. */
5371
5372 static void
5373 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5374 {
5375 Lisp_Object overlay, window, str, invisible;
5376 struct Lisp_Overlay *ov;
5377 ptrdiff_t start, end;
5378 ptrdiff_t size = 20;
5379 ptrdiff_t n = 0, i, j;
5380 int invis_p;
5381 struct overlay_entry *entries
5382 = (struct overlay_entry *) alloca (size * sizeof *entries);
5383 USE_SAFE_ALLOCA;
5384
5385 if (charpos <= 0)
5386 charpos = IT_CHARPOS (*it);
5387
5388 /* Append the overlay string STRING of overlay OVERLAY to vector
5389 `entries' which has size `size' and currently contains `n'
5390 elements. AFTER_P non-zero means STRING is an after-string of
5391 OVERLAY. */
5392 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5393 do \
5394 { \
5395 Lisp_Object priority; \
5396 \
5397 if (n == size) \
5398 { \
5399 struct overlay_entry *old = entries; \
5400 SAFE_NALLOCA (entries, 2, size); \
5401 memcpy (entries, old, size * sizeof *entries); \
5402 size *= 2; \
5403 } \
5404 \
5405 entries[n].string = (STRING); \
5406 entries[n].overlay = (OVERLAY); \
5407 priority = Foverlay_get ((OVERLAY), Qpriority); \
5408 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5409 entries[n].after_string_p = (AFTER_P); \
5410 ++n; \
5411 } \
5412 while (0)
5413
5414 /* Process overlay before the overlay center. */
5415 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5416 {
5417 XSETMISC (overlay, ov);
5418 xassert (OVERLAYP (overlay));
5419 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5420 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5421
5422 if (end < charpos)
5423 break;
5424
5425 /* Skip this overlay if it doesn't start or end at IT's current
5426 position. */
5427 if (end != charpos && start != charpos)
5428 continue;
5429
5430 /* Skip this overlay if it doesn't apply to IT->w. */
5431 window = Foverlay_get (overlay, Qwindow);
5432 if (WINDOWP (window) && XWINDOW (window) != it->w)
5433 continue;
5434
5435 /* If the text ``under'' the overlay is invisible, both before-
5436 and after-strings from this overlay are visible; start and
5437 end position are indistinguishable. */
5438 invisible = Foverlay_get (overlay, Qinvisible);
5439 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5440
5441 /* If overlay has a non-empty before-string, record it. */
5442 if ((start == charpos || (end == charpos && invis_p))
5443 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5444 && SCHARS (str))
5445 RECORD_OVERLAY_STRING (overlay, str, 0);
5446
5447 /* If overlay has a non-empty after-string, record it. */
5448 if ((end == charpos || (start == charpos && invis_p))
5449 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5450 && SCHARS (str))
5451 RECORD_OVERLAY_STRING (overlay, str, 1);
5452 }
5453
5454 /* Process overlays after the overlay center. */
5455 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5456 {
5457 XSETMISC (overlay, ov);
5458 xassert (OVERLAYP (overlay));
5459 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5460 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5461
5462 if (start > charpos)
5463 break;
5464
5465 /* Skip this overlay if it doesn't start or end at IT's current
5466 position. */
5467 if (end != charpos && start != charpos)
5468 continue;
5469
5470 /* Skip this overlay if it doesn't apply to IT->w. */
5471 window = Foverlay_get (overlay, Qwindow);
5472 if (WINDOWP (window) && XWINDOW (window) != it->w)
5473 continue;
5474
5475 /* If the text ``under'' the overlay is invisible, it has a zero
5476 dimension, and both before- and after-strings apply. */
5477 invisible = Foverlay_get (overlay, Qinvisible);
5478 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5479
5480 /* If overlay has a non-empty before-string, record it. */
5481 if ((start == charpos || (end == charpos && invis_p))
5482 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5483 && SCHARS (str))
5484 RECORD_OVERLAY_STRING (overlay, str, 0);
5485
5486 /* If overlay has a non-empty after-string, record it. */
5487 if ((end == charpos || (start == charpos && invis_p))
5488 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5489 && SCHARS (str))
5490 RECORD_OVERLAY_STRING (overlay, str, 1);
5491 }
5492
5493 #undef RECORD_OVERLAY_STRING
5494
5495 /* Sort entries. */
5496 if (n > 1)
5497 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5498
5499 /* Record number of overlay strings, and where we computed it. */
5500 it->n_overlay_strings = n;
5501 it->overlay_strings_charpos = charpos;
5502
5503 /* IT->current.overlay_string_index is the number of overlay strings
5504 that have already been consumed by IT. Copy some of the
5505 remaining overlay strings to IT->overlay_strings. */
5506 i = 0;
5507 j = it->current.overlay_string_index;
5508 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5509 {
5510 it->overlay_strings[i] = entries[j].string;
5511 it->string_overlays[i++] = entries[j++].overlay;
5512 }
5513
5514 CHECK_IT (it);
5515 SAFE_FREE ();
5516 }
5517
5518
5519 /* Get the first chunk of overlay strings at IT's current buffer
5520 position, or at CHARPOS if that is > 0. Value is non-zero if at
5521 least one overlay string was found. */
5522
5523 static int
5524 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5525 {
5526 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5527 process. This fills IT->overlay_strings with strings, and sets
5528 IT->n_overlay_strings to the total number of strings to process.
5529 IT->pos.overlay_string_index has to be set temporarily to zero
5530 because load_overlay_strings needs this; it must be set to -1
5531 when no overlay strings are found because a zero value would
5532 indicate a position in the first overlay string. */
5533 it->current.overlay_string_index = 0;
5534 load_overlay_strings (it, charpos);
5535
5536 /* If we found overlay strings, set up IT to deliver display
5537 elements from the first one. Otherwise set up IT to deliver
5538 from current_buffer. */
5539 if (it->n_overlay_strings)
5540 {
5541 /* Make sure we know settings in current_buffer, so that we can
5542 restore meaningful values when we're done with the overlay
5543 strings. */
5544 if (compute_stop_p)
5545 compute_stop_pos (it);
5546 xassert (it->face_id >= 0);
5547
5548 /* Save IT's settings. They are restored after all overlay
5549 strings have been processed. */
5550 xassert (!compute_stop_p || it->sp == 0);
5551
5552 /* When called from handle_stop, there might be an empty display
5553 string loaded. In that case, don't bother saving it. But
5554 don't use this optimization with the bidi iterator, since we
5555 need the corresponding pop_it call to resync the bidi
5556 iterator's position with IT's position, after we are done
5557 with the overlay strings. (The corresponding call to pop_it
5558 in case of an empty display string is in
5559 next_overlay_string.) */
5560 if (!(!it->bidi_p
5561 && STRINGP (it->string) && !SCHARS (it->string)))
5562 push_it (it, NULL);
5563
5564 /* Set up IT to deliver display elements from the first overlay
5565 string. */
5566 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5567 it->string = it->overlay_strings[0];
5568 it->from_overlay = Qnil;
5569 it->stop_charpos = 0;
5570 xassert (STRINGP (it->string));
5571 it->end_charpos = SCHARS (it->string);
5572 it->prev_stop = 0;
5573 it->base_level_stop = 0;
5574 it->multibyte_p = STRING_MULTIBYTE (it->string);
5575 it->method = GET_FROM_STRING;
5576 it->from_disp_prop_p = 0;
5577
5578 /* Force paragraph direction to be that of the parent
5579 buffer. */
5580 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5581 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5582 else
5583 it->paragraph_embedding = L2R;
5584
5585 /* Set up the bidi iterator for this overlay string. */
5586 if (it->bidi_p)
5587 {
5588 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5589
5590 it->bidi_it.string.lstring = it->string;
5591 it->bidi_it.string.s = NULL;
5592 it->bidi_it.string.schars = SCHARS (it->string);
5593 it->bidi_it.string.bufpos = pos;
5594 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5595 it->bidi_it.string.unibyte = !it->multibyte_p;
5596 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5597 }
5598 return 1;
5599 }
5600
5601 it->current.overlay_string_index = -1;
5602 return 0;
5603 }
5604
5605 static int
5606 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5607 {
5608 it->string = Qnil;
5609 it->method = GET_FROM_BUFFER;
5610
5611 (void) get_overlay_strings_1 (it, charpos, 1);
5612
5613 CHECK_IT (it);
5614
5615 /* Value is non-zero if we found at least one overlay string. */
5616 return STRINGP (it->string);
5617 }
5618
5619
5620 \f
5621 /***********************************************************************
5622 Saving and restoring state
5623 ***********************************************************************/
5624
5625 /* Save current settings of IT on IT->stack. Called, for example,
5626 before setting up IT for an overlay string, to be able to restore
5627 IT's settings to what they were after the overlay string has been
5628 processed. If POSITION is non-NULL, it is the position to save on
5629 the stack instead of IT->position. */
5630
5631 static void
5632 push_it (struct it *it, struct text_pos *position)
5633 {
5634 struct iterator_stack_entry *p;
5635
5636 xassert (it->sp < IT_STACK_SIZE);
5637 p = it->stack + it->sp;
5638
5639 p->stop_charpos = it->stop_charpos;
5640 p->prev_stop = it->prev_stop;
5641 p->base_level_stop = it->base_level_stop;
5642 p->cmp_it = it->cmp_it;
5643 xassert (it->face_id >= 0);
5644 p->face_id = it->face_id;
5645 p->string = it->string;
5646 p->method = it->method;
5647 p->from_overlay = it->from_overlay;
5648 switch (p->method)
5649 {
5650 case GET_FROM_IMAGE:
5651 p->u.image.object = it->object;
5652 p->u.image.image_id = it->image_id;
5653 p->u.image.slice = it->slice;
5654 break;
5655 case GET_FROM_STRETCH:
5656 p->u.stretch.object = it->object;
5657 break;
5658 }
5659 p->position = position ? *position : it->position;
5660 p->current = it->current;
5661 p->end_charpos = it->end_charpos;
5662 p->string_nchars = it->string_nchars;
5663 p->area = it->area;
5664 p->multibyte_p = it->multibyte_p;
5665 p->avoid_cursor_p = it->avoid_cursor_p;
5666 p->space_width = it->space_width;
5667 p->font_height = it->font_height;
5668 p->voffset = it->voffset;
5669 p->string_from_display_prop_p = it->string_from_display_prop_p;
5670 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5671 p->display_ellipsis_p = 0;
5672 p->line_wrap = it->line_wrap;
5673 p->bidi_p = it->bidi_p;
5674 p->paragraph_embedding = it->paragraph_embedding;
5675 p->from_disp_prop_p = it->from_disp_prop_p;
5676 ++it->sp;
5677
5678 /* Save the state of the bidi iterator as well. */
5679 if (it->bidi_p)
5680 bidi_push_it (&it->bidi_it);
5681 }
5682
5683 static void
5684 iterate_out_of_display_property (struct it *it)
5685 {
5686 int buffer_p = !STRINGP (it->string);
5687 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5688 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5689
5690 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5691
5692 /* Maybe initialize paragraph direction. If we are at the beginning
5693 of a new paragraph, next_element_from_buffer may not have a
5694 chance to do that. */
5695 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5696 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5697 /* prev_stop can be zero, so check against BEGV as well. */
5698 while (it->bidi_it.charpos >= bob
5699 && it->prev_stop <= it->bidi_it.charpos
5700 && it->bidi_it.charpos < CHARPOS (it->position)
5701 && it->bidi_it.charpos < eob)
5702 bidi_move_to_visually_next (&it->bidi_it);
5703 /* Record the stop_pos we just crossed, for when we cross it
5704 back, maybe. */
5705 if (it->bidi_it.charpos > CHARPOS (it->position))
5706 it->prev_stop = CHARPOS (it->position);
5707 /* If we ended up not where pop_it put us, resync IT's
5708 positional members with the bidi iterator. */
5709 if (it->bidi_it.charpos != CHARPOS (it->position))
5710 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5711 if (buffer_p)
5712 it->current.pos = it->position;
5713 else
5714 it->current.string_pos = it->position;
5715 }
5716
5717 /* Restore IT's settings from IT->stack. Called, for example, when no
5718 more overlay strings must be processed, and we return to delivering
5719 display elements from a buffer, or when the end of a string from a
5720 `display' property is reached and we return to delivering display
5721 elements from an overlay string, or from a buffer. */
5722
5723 static void
5724 pop_it (struct it *it)
5725 {
5726 struct iterator_stack_entry *p;
5727 int from_display_prop = it->from_disp_prop_p;
5728
5729 xassert (it->sp > 0);
5730 --it->sp;
5731 p = it->stack + it->sp;
5732 it->stop_charpos = p->stop_charpos;
5733 it->prev_stop = p->prev_stop;
5734 it->base_level_stop = p->base_level_stop;
5735 it->cmp_it = p->cmp_it;
5736 it->face_id = p->face_id;
5737 it->current = p->current;
5738 it->position = p->position;
5739 it->string = p->string;
5740 it->from_overlay = p->from_overlay;
5741 if (NILP (it->string))
5742 SET_TEXT_POS (it->current.string_pos, -1, -1);
5743 it->method = p->method;
5744 switch (it->method)
5745 {
5746 case GET_FROM_IMAGE:
5747 it->image_id = p->u.image.image_id;
5748 it->object = p->u.image.object;
5749 it->slice = p->u.image.slice;
5750 break;
5751 case GET_FROM_STRETCH:
5752 it->object = p->u.stretch.object;
5753 break;
5754 case GET_FROM_BUFFER:
5755 it->object = it->w->buffer;
5756 break;
5757 case GET_FROM_STRING:
5758 it->object = it->string;
5759 break;
5760 case GET_FROM_DISPLAY_VECTOR:
5761 if (it->s)
5762 it->method = GET_FROM_C_STRING;
5763 else if (STRINGP (it->string))
5764 it->method = GET_FROM_STRING;
5765 else
5766 {
5767 it->method = GET_FROM_BUFFER;
5768 it->object = it->w->buffer;
5769 }
5770 }
5771 it->end_charpos = p->end_charpos;
5772 it->string_nchars = p->string_nchars;
5773 it->area = p->area;
5774 it->multibyte_p = p->multibyte_p;
5775 it->avoid_cursor_p = p->avoid_cursor_p;
5776 it->space_width = p->space_width;
5777 it->font_height = p->font_height;
5778 it->voffset = p->voffset;
5779 it->string_from_display_prop_p = p->string_from_display_prop_p;
5780 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5781 it->line_wrap = p->line_wrap;
5782 it->bidi_p = p->bidi_p;
5783 it->paragraph_embedding = p->paragraph_embedding;
5784 it->from_disp_prop_p = p->from_disp_prop_p;
5785 if (it->bidi_p)
5786 {
5787 bidi_pop_it (&it->bidi_it);
5788 /* Bidi-iterate until we get out of the portion of text, if any,
5789 covered by a `display' text property or by an overlay with
5790 `display' property. (We cannot just jump there, because the
5791 internal coherency of the bidi iterator state can not be
5792 preserved across such jumps.) We also must determine the
5793 paragraph base direction if the overlay we just processed is
5794 at the beginning of a new paragraph. */
5795 if (from_display_prop
5796 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5797 iterate_out_of_display_property (it);
5798
5799 xassert ((BUFFERP (it->object)
5800 && IT_CHARPOS (*it) == it->bidi_it.charpos
5801 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5802 || (STRINGP (it->object)
5803 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5804 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5805 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5806 }
5807 }
5808
5809
5810 \f
5811 /***********************************************************************
5812 Moving over lines
5813 ***********************************************************************/
5814
5815 /* Set IT's current position to the previous line start. */
5816
5817 static void
5818 back_to_previous_line_start (struct it *it)
5819 {
5820 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5821 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5822 }
5823
5824
5825 /* Move IT to the next line start.
5826
5827 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5828 we skipped over part of the text (as opposed to moving the iterator
5829 continuously over the text). Otherwise, don't change the value
5830 of *SKIPPED_P.
5831
5832 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5833 iterator on the newline, if it was found.
5834
5835 Newlines may come from buffer text, overlay strings, or strings
5836 displayed via the `display' property. That's the reason we can't
5837 simply use find_next_newline_no_quit.
5838
5839 Note that this function may not skip over invisible text that is so
5840 because of text properties and immediately follows a newline. If
5841 it would, function reseat_at_next_visible_line_start, when called
5842 from set_iterator_to_next, would effectively make invisible
5843 characters following a newline part of the wrong glyph row, which
5844 leads to wrong cursor motion. */
5845
5846 static int
5847 forward_to_next_line_start (struct it *it, int *skipped_p,
5848 struct bidi_it *bidi_it_prev)
5849 {
5850 ptrdiff_t old_selective;
5851 int newline_found_p, n;
5852 const int MAX_NEWLINE_DISTANCE = 500;
5853
5854 /* If already on a newline, just consume it to avoid unintended
5855 skipping over invisible text below. */
5856 if (it->what == IT_CHARACTER
5857 && it->c == '\n'
5858 && CHARPOS (it->position) == IT_CHARPOS (*it))
5859 {
5860 if (it->bidi_p && bidi_it_prev)
5861 *bidi_it_prev = it->bidi_it;
5862 set_iterator_to_next (it, 0);
5863 it->c = 0;
5864 return 1;
5865 }
5866
5867 /* Don't handle selective display in the following. It's (a)
5868 unnecessary because it's done by the caller, and (b) leads to an
5869 infinite recursion because next_element_from_ellipsis indirectly
5870 calls this function. */
5871 old_selective = it->selective;
5872 it->selective = 0;
5873
5874 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5875 from buffer text. */
5876 for (n = newline_found_p = 0;
5877 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5878 n += STRINGP (it->string) ? 0 : 1)
5879 {
5880 if (!get_next_display_element (it))
5881 return 0;
5882 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5883 if (newline_found_p && it->bidi_p && bidi_it_prev)
5884 *bidi_it_prev = it->bidi_it;
5885 set_iterator_to_next (it, 0);
5886 }
5887
5888 /* If we didn't find a newline near enough, see if we can use a
5889 short-cut. */
5890 if (!newline_found_p)
5891 {
5892 ptrdiff_t start = IT_CHARPOS (*it);
5893 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5894 Lisp_Object pos;
5895
5896 xassert (!STRINGP (it->string));
5897
5898 /* If there isn't any `display' property in sight, and no
5899 overlays, we can just use the position of the newline in
5900 buffer text. */
5901 if (it->stop_charpos >= limit
5902 || ((pos = Fnext_single_property_change (make_number (start),
5903 Qdisplay, Qnil,
5904 make_number (limit)),
5905 NILP (pos))
5906 && next_overlay_change (start) == ZV))
5907 {
5908 if (!it->bidi_p)
5909 {
5910 IT_CHARPOS (*it) = limit;
5911 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5912 }
5913 else
5914 {
5915 struct bidi_it bprev;
5916
5917 /* Help bidi.c avoid expensive searches for display
5918 properties and overlays, by telling it that there are
5919 none up to `limit'. */
5920 if (it->bidi_it.disp_pos < limit)
5921 {
5922 it->bidi_it.disp_pos = limit;
5923 it->bidi_it.disp_prop = 0;
5924 }
5925 do {
5926 bprev = it->bidi_it;
5927 bidi_move_to_visually_next (&it->bidi_it);
5928 } while (it->bidi_it.charpos != limit);
5929 IT_CHARPOS (*it) = limit;
5930 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5931 if (bidi_it_prev)
5932 *bidi_it_prev = bprev;
5933 }
5934 *skipped_p = newline_found_p = 1;
5935 }
5936 else
5937 {
5938 while (get_next_display_element (it)
5939 && !newline_found_p)
5940 {
5941 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5942 if (newline_found_p && it->bidi_p && bidi_it_prev)
5943 *bidi_it_prev = it->bidi_it;
5944 set_iterator_to_next (it, 0);
5945 }
5946 }
5947 }
5948
5949 it->selective = old_selective;
5950 return newline_found_p;
5951 }
5952
5953
5954 /* Set IT's current position to the previous visible line start. Skip
5955 invisible text that is so either due to text properties or due to
5956 selective display. Caution: this does not change IT->current_x and
5957 IT->hpos. */
5958
5959 static void
5960 back_to_previous_visible_line_start (struct it *it)
5961 {
5962 while (IT_CHARPOS (*it) > BEGV)
5963 {
5964 back_to_previous_line_start (it);
5965
5966 if (IT_CHARPOS (*it) <= BEGV)
5967 break;
5968
5969 /* If selective > 0, then lines indented more than its value are
5970 invisible. */
5971 if (it->selective > 0
5972 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5973 it->selective))
5974 continue;
5975
5976 /* Check the newline before point for invisibility. */
5977 {
5978 Lisp_Object prop;
5979 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5980 Qinvisible, it->window);
5981 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5982 continue;
5983 }
5984
5985 if (IT_CHARPOS (*it) <= BEGV)
5986 break;
5987
5988 {
5989 struct it it2;
5990 void *it2data = NULL;
5991 ptrdiff_t pos;
5992 ptrdiff_t beg, end;
5993 Lisp_Object val, overlay;
5994
5995 SAVE_IT (it2, *it, it2data);
5996
5997 /* If newline is part of a composition, continue from start of composition */
5998 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5999 && beg < IT_CHARPOS (*it))
6000 goto replaced;
6001
6002 /* If newline is replaced by a display property, find start of overlay
6003 or interval and continue search from that point. */
6004 pos = --IT_CHARPOS (it2);
6005 --IT_BYTEPOS (it2);
6006 it2.sp = 0;
6007 bidi_unshelve_cache (NULL, 0);
6008 it2.string_from_display_prop_p = 0;
6009 it2.from_disp_prop_p = 0;
6010 if (handle_display_prop (&it2) == HANDLED_RETURN
6011 && !NILP (val = get_char_property_and_overlay
6012 (make_number (pos), Qdisplay, Qnil, &overlay))
6013 && (OVERLAYP (overlay)
6014 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6015 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6016 {
6017 RESTORE_IT (it, it, it2data);
6018 goto replaced;
6019 }
6020
6021 /* Newline is not replaced by anything -- so we are done. */
6022 RESTORE_IT (it, it, it2data);
6023 break;
6024
6025 replaced:
6026 if (beg < BEGV)
6027 beg = BEGV;
6028 IT_CHARPOS (*it) = beg;
6029 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6030 }
6031 }
6032
6033 it->continuation_lines_width = 0;
6034
6035 xassert (IT_CHARPOS (*it) >= BEGV);
6036 xassert (IT_CHARPOS (*it) == BEGV
6037 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6038 CHECK_IT (it);
6039 }
6040
6041
6042 /* Reseat iterator IT at the previous visible line start. Skip
6043 invisible text that is so either due to text properties or due to
6044 selective display. At the end, update IT's overlay information,
6045 face information etc. */
6046
6047 void
6048 reseat_at_previous_visible_line_start (struct it *it)
6049 {
6050 back_to_previous_visible_line_start (it);
6051 reseat (it, it->current.pos, 1);
6052 CHECK_IT (it);
6053 }
6054
6055
6056 /* Reseat iterator IT on the next visible line start in the current
6057 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6058 preceding the line start. Skip over invisible text that is so
6059 because of selective display. Compute faces, overlays etc at the
6060 new position. Note that this function does not skip over text that
6061 is invisible because of text properties. */
6062
6063 static void
6064 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6065 {
6066 int newline_found_p, skipped_p = 0;
6067 struct bidi_it bidi_it_prev;
6068
6069 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6070
6071 /* Skip over lines that are invisible because they are indented
6072 more than the value of IT->selective. */
6073 if (it->selective > 0)
6074 while (IT_CHARPOS (*it) < ZV
6075 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6076 it->selective))
6077 {
6078 xassert (IT_BYTEPOS (*it) == BEGV
6079 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6080 newline_found_p =
6081 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6082 }
6083
6084 /* Position on the newline if that's what's requested. */
6085 if (on_newline_p && newline_found_p)
6086 {
6087 if (STRINGP (it->string))
6088 {
6089 if (IT_STRING_CHARPOS (*it) > 0)
6090 {
6091 if (!it->bidi_p)
6092 {
6093 --IT_STRING_CHARPOS (*it);
6094 --IT_STRING_BYTEPOS (*it);
6095 }
6096 else
6097 {
6098 /* We need to restore the bidi iterator to the state
6099 it had on the newline, and resync the IT's
6100 position with that. */
6101 it->bidi_it = bidi_it_prev;
6102 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6103 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6104 }
6105 }
6106 }
6107 else if (IT_CHARPOS (*it) > BEGV)
6108 {
6109 if (!it->bidi_p)
6110 {
6111 --IT_CHARPOS (*it);
6112 --IT_BYTEPOS (*it);
6113 }
6114 else
6115 {
6116 /* We need to restore the bidi iterator to the state it
6117 had on the newline and resync IT with that. */
6118 it->bidi_it = bidi_it_prev;
6119 IT_CHARPOS (*it) = it->bidi_it.charpos;
6120 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6121 }
6122 reseat (it, it->current.pos, 0);
6123 }
6124 }
6125 else if (skipped_p)
6126 reseat (it, it->current.pos, 0);
6127
6128 CHECK_IT (it);
6129 }
6130
6131
6132 \f
6133 /***********************************************************************
6134 Changing an iterator's position
6135 ***********************************************************************/
6136
6137 /* Change IT's current position to POS in current_buffer. If FORCE_P
6138 is non-zero, always check for text properties at the new position.
6139 Otherwise, text properties are only looked up if POS >=
6140 IT->check_charpos of a property. */
6141
6142 static void
6143 reseat (struct it *it, struct text_pos pos, int force_p)
6144 {
6145 ptrdiff_t original_pos = IT_CHARPOS (*it);
6146
6147 reseat_1 (it, pos, 0);
6148
6149 /* Determine where to check text properties. Avoid doing it
6150 where possible because text property lookup is very expensive. */
6151 if (force_p
6152 || CHARPOS (pos) > it->stop_charpos
6153 || CHARPOS (pos) < original_pos)
6154 {
6155 if (it->bidi_p)
6156 {
6157 /* For bidi iteration, we need to prime prev_stop and
6158 base_level_stop with our best estimations. */
6159 /* Implementation note: Of course, POS is not necessarily a
6160 stop position, so assigning prev_pos to it is a lie; we
6161 should have called compute_stop_backwards. However, if
6162 the current buffer does not include any R2L characters,
6163 that call would be a waste of cycles, because the
6164 iterator will never move back, and thus never cross this
6165 "fake" stop position. So we delay that backward search
6166 until the time we really need it, in next_element_from_buffer. */
6167 if (CHARPOS (pos) != it->prev_stop)
6168 it->prev_stop = CHARPOS (pos);
6169 if (CHARPOS (pos) < it->base_level_stop)
6170 it->base_level_stop = 0; /* meaning it's unknown */
6171 handle_stop (it);
6172 }
6173 else
6174 {
6175 handle_stop (it);
6176 it->prev_stop = it->base_level_stop = 0;
6177 }
6178
6179 }
6180
6181 CHECK_IT (it);
6182 }
6183
6184
6185 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6186 IT->stop_pos to POS, also. */
6187
6188 static void
6189 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6190 {
6191 /* Don't call this function when scanning a C string. */
6192 xassert (it->s == NULL);
6193
6194 /* POS must be a reasonable value. */
6195 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6196
6197 it->current.pos = it->position = pos;
6198 it->end_charpos = ZV;
6199 it->dpvec = NULL;
6200 it->current.dpvec_index = -1;
6201 it->current.overlay_string_index = -1;
6202 IT_STRING_CHARPOS (*it) = -1;
6203 IT_STRING_BYTEPOS (*it) = -1;
6204 it->string = Qnil;
6205 it->method = GET_FROM_BUFFER;
6206 it->object = it->w->buffer;
6207 it->area = TEXT_AREA;
6208 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6209 it->sp = 0;
6210 it->string_from_display_prop_p = 0;
6211 it->string_from_prefix_prop_p = 0;
6212
6213 it->from_disp_prop_p = 0;
6214 it->face_before_selective_p = 0;
6215 if (it->bidi_p)
6216 {
6217 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6218 &it->bidi_it);
6219 bidi_unshelve_cache (NULL, 0);
6220 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6221 it->bidi_it.string.s = NULL;
6222 it->bidi_it.string.lstring = Qnil;
6223 it->bidi_it.string.bufpos = 0;
6224 it->bidi_it.string.unibyte = 0;
6225 }
6226
6227 if (set_stop_p)
6228 {
6229 it->stop_charpos = CHARPOS (pos);
6230 it->base_level_stop = CHARPOS (pos);
6231 }
6232 }
6233
6234
6235 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6236 If S is non-null, it is a C string to iterate over. Otherwise,
6237 STRING gives a Lisp string to iterate over.
6238
6239 If PRECISION > 0, don't return more then PRECISION number of
6240 characters from the string.
6241
6242 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6243 characters have been returned. FIELD_WIDTH < 0 means an infinite
6244 field width.
6245
6246 MULTIBYTE = 0 means disable processing of multibyte characters,
6247 MULTIBYTE > 0 means enable it,
6248 MULTIBYTE < 0 means use IT->multibyte_p.
6249
6250 IT must be initialized via a prior call to init_iterator before
6251 calling this function. */
6252
6253 static void
6254 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6255 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6256 int multibyte)
6257 {
6258 /* No region in strings. */
6259 it->region_beg_charpos = it->region_end_charpos = -1;
6260
6261 /* No text property checks performed by default, but see below. */
6262 it->stop_charpos = -1;
6263
6264 /* Set iterator position and end position. */
6265 memset (&it->current, 0, sizeof it->current);
6266 it->current.overlay_string_index = -1;
6267 it->current.dpvec_index = -1;
6268 xassert (charpos >= 0);
6269
6270 /* If STRING is specified, use its multibyteness, otherwise use the
6271 setting of MULTIBYTE, if specified. */
6272 if (multibyte >= 0)
6273 it->multibyte_p = multibyte > 0;
6274
6275 /* Bidirectional reordering of strings is controlled by the default
6276 value of bidi-display-reordering. Don't try to reorder while
6277 loading loadup.el, as the necessary character property tables are
6278 not yet available. */
6279 it->bidi_p =
6280 NILP (Vpurify_flag)
6281 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6282
6283 if (s == NULL)
6284 {
6285 xassert (STRINGP (string));
6286 it->string = string;
6287 it->s = NULL;
6288 it->end_charpos = it->string_nchars = SCHARS (string);
6289 it->method = GET_FROM_STRING;
6290 it->current.string_pos = string_pos (charpos, string);
6291
6292 if (it->bidi_p)
6293 {
6294 it->bidi_it.string.lstring = string;
6295 it->bidi_it.string.s = NULL;
6296 it->bidi_it.string.schars = it->end_charpos;
6297 it->bidi_it.string.bufpos = 0;
6298 it->bidi_it.string.from_disp_str = 0;
6299 it->bidi_it.string.unibyte = !it->multibyte_p;
6300 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6301 FRAME_WINDOW_P (it->f), &it->bidi_it);
6302 }
6303 }
6304 else
6305 {
6306 it->s = (const unsigned char *) s;
6307 it->string = Qnil;
6308
6309 /* Note that we use IT->current.pos, not it->current.string_pos,
6310 for displaying C strings. */
6311 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6312 if (it->multibyte_p)
6313 {
6314 it->current.pos = c_string_pos (charpos, s, 1);
6315 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6316 }
6317 else
6318 {
6319 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6320 it->end_charpos = it->string_nchars = strlen (s);
6321 }
6322
6323 if (it->bidi_p)
6324 {
6325 it->bidi_it.string.lstring = Qnil;
6326 it->bidi_it.string.s = (const unsigned char *) s;
6327 it->bidi_it.string.schars = it->end_charpos;
6328 it->bidi_it.string.bufpos = 0;
6329 it->bidi_it.string.from_disp_str = 0;
6330 it->bidi_it.string.unibyte = !it->multibyte_p;
6331 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6332 &it->bidi_it);
6333 }
6334 it->method = GET_FROM_C_STRING;
6335 }
6336
6337 /* PRECISION > 0 means don't return more than PRECISION characters
6338 from the string. */
6339 if (precision > 0 && it->end_charpos - charpos > precision)
6340 {
6341 it->end_charpos = it->string_nchars = charpos + precision;
6342 if (it->bidi_p)
6343 it->bidi_it.string.schars = it->end_charpos;
6344 }
6345
6346 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6347 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6348 FIELD_WIDTH < 0 means infinite field width. This is useful for
6349 padding with `-' at the end of a mode line. */
6350 if (field_width < 0)
6351 field_width = INFINITY;
6352 /* Implementation note: We deliberately don't enlarge
6353 it->bidi_it.string.schars here to fit it->end_charpos, because
6354 the bidi iterator cannot produce characters out of thin air. */
6355 if (field_width > it->end_charpos - charpos)
6356 it->end_charpos = charpos + field_width;
6357
6358 /* Use the standard display table for displaying strings. */
6359 if (DISP_TABLE_P (Vstandard_display_table))
6360 it->dp = XCHAR_TABLE (Vstandard_display_table);
6361
6362 it->stop_charpos = charpos;
6363 it->prev_stop = charpos;
6364 it->base_level_stop = 0;
6365 if (it->bidi_p)
6366 {
6367 it->bidi_it.first_elt = 1;
6368 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6369 it->bidi_it.disp_pos = -1;
6370 }
6371 if (s == NULL && it->multibyte_p)
6372 {
6373 ptrdiff_t endpos = SCHARS (it->string);
6374 if (endpos > it->end_charpos)
6375 endpos = it->end_charpos;
6376 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6377 it->string);
6378 }
6379 CHECK_IT (it);
6380 }
6381
6382
6383 \f
6384 /***********************************************************************
6385 Iteration
6386 ***********************************************************************/
6387
6388 /* Map enum it_method value to corresponding next_element_from_* function. */
6389
6390 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6391 {
6392 next_element_from_buffer,
6393 next_element_from_display_vector,
6394 next_element_from_string,
6395 next_element_from_c_string,
6396 next_element_from_image,
6397 next_element_from_stretch
6398 };
6399
6400 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6401
6402
6403 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6404 (possibly with the following characters). */
6405
6406 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6407 ((IT)->cmp_it.id >= 0 \
6408 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6409 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6410 END_CHARPOS, (IT)->w, \
6411 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6412 (IT)->string)))
6413
6414
6415 /* Lookup the char-table Vglyphless_char_display for character C (-1
6416 if we want information for no-font case), and return the display
6417 method symbol. By side-effect, update it->what and
6418 it->glyphless_method. This function is called from
6419 get_next_display_element for each character element, and from
6420 x_produce_glyphs when no suitable font was found. */
6421
6422 Lisp_Object
6423 lookup_glyphless_char_display (int c, struct it *it)
6424 {
6425 Lisp_Object glyphless_method = Qnil;
6426
6427 if (CHAR_TABLE_P (Vglyphless_char_display)
6428 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6429 {
6430 if (c >= 0)
6431 {
6432 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6433 if (CONSP (glyphless_method))
6434 glyphless_method = FRAME_WINDOW_P (it->f)
6435 ? XCAR (glyphless_method)
6436 : XCDR (glyphless_method);
6437 }
6438 else
6439 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6440 }
6441
6442 retry:
6443 if (NILP (glyphless_method))
6444 {
6445 if (c >= 0)
6446 /* The default is to display the character by a proper font. */
6447 return Qnil;
6448 /* The default for the no-font case is to display an empty box. */
6449 glyphless_method = Qempty_box;
6450 }
6451 if (EQ (glyphless_method, Qzero_width))
6452 {
6453 if (c >= 0)
6454 return glyphless_method;
6455 /* This method can't be used for the no-font case. */
6456 glyphless_method = Qempty_box;
6457 }
6458 if (EQ (glyphless_method, Qthin_space))
6459 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6460 else if (EQ (glyphless_method, Qempty_box))
6461 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6462 else if (EQ (glyphless_method, Qhex_code))
6463 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6464 else if (STRINGP (glyphless_method))
6465 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6466 else
6467 {
6468 /* Invalid value. We use the default method. */
6469 glyphless_method = Qnil;
6470 goto retry;
6471 }
6472 it->what = IT_GLYPHLESS;
6473 return glyphless_method;
6474 }
6475
6476 /* Load IT's display element fields with information about the next
6477 display element from the current position of IT. Value is zero if
6478 end of buffer (or C string) is reached. */
6479
6480 static struct frame *last_escape_glyph_frame = NULL;
6481 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6482 static int last_escape_glyph_merged_face_id = 0;
6483
6484 struct frame *last_glyphless_glyph_frame = NULL;
6485 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6486 int last_glyphless_glyph_merged_face_id = 0;
6487
6488 static int
6489 get_next_display_element (struct it *it)
6490 {
6491 /* Non-zero means that we found a display element. Zero means that
6492 we hit the end of what we iterate over. Performance note: the
6493 function pointer `method' used here turns out to be faster than
6494 using a sequence of if-statements. */
6495 int success_p;
6496
6497 get_next:
6498 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6499
6500 if (it->what == IT_CHARACTER)
6501 {
6502 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6503 and only if (a) the resolved directionality of that character
6504 is R..." */
6505 /* FIXME: Do we need an exception for characters from display
6506 tables? */
6507 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6508 it->c = bidi_mirror_char (it->c);
6509 /* Map via display table or translate control characters.
6510 IT->c, IT->len etc. have been set to the next character by
6511 the function call above. If we have a display table, and it
6512 contains an entry for IT->c, translate it. Don't do this if
6513 IT->c itself comes from a display table, otherwise we could
6514 end up in an infinite recursion. (An alternative could be to
6515 count the recursion depth of this function and signal an
6516 error when a certain maximum depth is reached.) Is it worth
6517 it? */
6518 if (success_p && it->dpvec == NULL)
6519 {
6520 Lisp_Object dv;
6521 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6522 int nonascii_space_p = 0;
6523 int nonascii_hyphen_p = 0;
6524 int c = it->c; /* This is the character to display. */
6525
6526 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6527 {
6528 xassert (SINGLE_BYTE_CHAR_P (c));
6529 if (unibyte_display_via_language_environment)
6530 {
6531 c = DECODE_CHAR (unibyte, c);
6532 if (c < 0)
6533 c = BYTE8_TO_CHAR (it->c);
6534 }
6535 else
6536 c = BYTE8_TO_CHAR (it->c);
6537 }
6538
6539 if (it->dp
6540 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6541 VECTORP (dv)))
6542 {
6543 struct Lisp_Vector *v = XVECTOR (dv);
6544
6545 /* Return the first character from the display table
6546 entry, if not empty. If empty, don't display the
6547 current character. */
6548 if (v->header.size)
6549 {
6550 it->dpvec_char_len = it->len;
6551 it->dpvec = v->contents;
6552 it->dpend = v->contents + v->header.size;
6553 it->current.dpvec_index = 0;
6554 it->dpvec_face_id = -1;
6555 it->saved_face_id = it->face_id;
6556 it->method = GET_FROM_DISPLAY_VECTOR;
6557 it->ellipsis_p = 0;
6558 }
6559 else
6560 {
6561 set_iterator_to_next (it, 0);
6562 }
6563 goto get_next;
6564 }
6565
6566 if (! NILP (lookup_glyphless_char_display (c, it)))
6567 {
6568 if (it->what == IT_GLYPHLESS)
6569 goto done;
6570 /* Don't display this character. */
6571 set_iterator_to_next (it, 0);
6572 goto get_next;
6573 }
6574
6575 /* If `nobreak-char-display' is non-nil, we display
6576 non-ASCII spaces and hyphens specially. */
6577 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6578 {
6579 if (c == 0xA0)
6580 nonascii_space_p = 1;
6581 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6582 nonascii_hyphen_p = 1;
6583 }
6584
6585 /* Translate control characters into `\003' or `^C' form.
6586 Control characters coming from a display table entry are
6587 currently not translated because we use IT->dpvec to hold
6588 the translation. This could easily be changed but I
6589 don't believe that it is worth doing.
6590
6591 The characters handled by `nobreak-char-display' must be
6592 translated too.
6593
6594 Non-printable characters and raw-byte characters are also
6595 translated to octal form. */
6596 if (((c < ' ' || c == 127) /* ASCII control chars */
6597 ? (it->area != TEXT_AREA
6598 /* In mode line, treat \n, \t like other crl chars. */
6599 || (c != '\t'
6600 && it->glyph_row
6601 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6602 || (c != '\n' && c != '\t'))
6603 : (nonascii_space_p
6604 || nonascii_hyphen_p
6605 || CHAR_BYTE8_P (c)
6606 || ! CHAR_PRINTABLE_P (c))))
6607 {
6608 /* C is a control character, non-ASCII space/hyphen,
6609 raw-byte, or a non-printable character which must be
6610 displayed either as '\003' or as `^C' where the '\\'
6611 and '^' can be defined in the display table. Fill
6612 IT->ctl_chars with glyphs for what we have to
6613 display. Then, set IT->dpvec to these glyphs. */
6614 Lisp_Object gc;
6615 int ctl_len;
6616 int face_id;
6617 int lface_id = 0;
6618 int escape_glyph;
6619
6620 /* Handle control characters with ^. */
6621
6622 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6623 {
6624 int g;
6625
6626 g = '^'; /* default glyph for Control */
6627 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6628 if (it->dp
6629 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6630 {
6631 g = GLYPH_CODE_CHAR (gc);
6632 lface_id = GLYPH_CODE_FACE (gc);
6633 }
6634 if (lface_id)
6635 {
6636 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6637 }
6638 else if (it->f == last_escape_glyph_frame
6639 && it->face_id == last_escape_glyph_face_id)
6640 {
6641 face_id = last_escape_glyph_merged_face_id;
6642 }
6643 else
6644 {
6645 /* Merge the escape-glyph face into the current face. */
6646 face_id = merge_faces (it->f, Qescape_glyph, 0,
6647 it->face_id);
6648 last_escape_glyph_frame = it->f;
6649 last_escape_glyph_face_id = it->face_id;
6650 last_escape_glyph_merged_face_id = face_id;
6651 }
6652
6653 XSETINT (it->ctl_chars[0], g);
6654 XSETINT (it->ctl_chars[1], c ^ 0100);
6655 ctl_len = 2;
6656 goto display_control;
6657 }
6658
6659 /* Handle non-ascii space in the mode where it only gets
6660 highlighting. */
6661
6662 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6663 {
6664 /* Merge `nobreak-space' into the current face. */
6665 face_id = merge_faces (it->f, Qnobreak_space, 0,
6666 it->face_id);
6667 XSETINT (it->ctl_chars[0], ' ');
6668 ctl_len = 1;
6669 goto display_control;
6670 }
6671
6672 /* Handle sequences that start with the "escape glyph". */
6673
6674 /* the default escape glyph is \. */
6675 escape_glyph = '\\';
6676
6677 if (it->dp
6678 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6679 {
6680 escape_glyph = GLYPH_CODE_CHAR (gc);
6681 lface_id = GLYPH_CODE_FACE (gc);
6682 }
6683 if (lface_id)
6684 {
6685 /* The display table specified a face.
6686 Merge it into face_id and also into escape_glyph. */
6687 face_id = merge_faces (it->f, Qt, lface_id,
6688 it->face_id);
6689 }
6690 else if (it->f == last_escape_glyph_frame
6691 && it->face_id == last_escape_glyph_face_id)
6692 {
6693 face_id = last_escape_glyph_merged_face_id;
6694 }
6695 else
6696 {
6697 /* Merge the escape-glyph face into the current face. */
6698 face_id = merge_faces (it->f, Qescape_glyph, 0,
6699 it->face_id);
6700 last_escape_glyph_frame = it->f;
6701 last_escape_glyph_face_id = it->face_id;
6702 last_escape_glyph_merged_face_id = face_id;
6703 }
6704
6705 /* Draw non-ASCII hyphen with just highlighting: */
6706
6707 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6708 {
6709 XSETINT (it->ctl_chars[0], '-');
6710 ctl_len = 1;
6711 goto display_control;
6712 }
6713
6714 /* Draw non-ASCII space/hyphen with escape glyph: */
6715
6716 if (nonascii_space_p || nonascii_hyphen_p)
6717 {
6718 XSETINT (it->ctl_chars[0], escape_glyph);
6719 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6720 ctl_len = 2;
6721 goto display_control;
6722 }
6723
6724 {
6725 char str[10];
6726 int len, i;
6727
6728 if (CHAR_BYTE8_P (c))
6729 /* Display \200 instead of \17777600. */
6730 c = CHAR_TO_BYTE8 (c);
6731 len = sprintf (str, "%03o", c);
6732
6733 XSETINT (it->ctl_chars[0], escape_glyph);
6734 for (i = 0; i < len; i++)
6735 XSETINT (it->ctl_chars[i + 1], str[i]);
6736 ctl_len = len + 1;
6737 }
6738
6739 display_control:
6740 /* Set up IT->dpvec and return first character from it. */
6741 it->dpvec_char_len = it->len;
6742 it->dpvec = it->ctl_chars;
6743 it->dpend = it->dpvec + ctl_len;
6744 it->current.dpvec_index = 0;
6745 it->dpvec_face_id = face_id;
6746 it->saved_face_id = it->face_id;
6747 it->method = GET_FROM_DISPLAY_VECTOR;
6748 it->ellipsis_p = 0;
6749 goto get_next;
6750 }
6751 it->char_to_display = c;
6752 }
6753 else if (success_p)
6754 {
6755 it->char_to_display = it->c;
6756 }
6757 }
6758
6759 /* Adjust face id for a multibyte character. There are no multibyte
6760 character in unibyte text. */
6761 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6762 && it->multibyte_p
6763 && success_p
6764 && FRAME_WINDOW_P (it->f))
6765 {
6766 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6767
6768 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6769 {
6770 /* Automatic composition with glyph-string. */
6771 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6772
6773 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6774 }
6775 else
6776 {
6777 ptrdiff_t pos = (it->s ? -1
6778 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6779 : IT_CHARPOS (*it));
6780 int c;
6781
6782 if (it->what == IT_CHARACTER)
6783 c = it->char_to_display;
6784 else
6785 {
6786 struct composition *cmp = composition_table[it->cmp_it.id];
6787 int i;
6788
6789 c = ' ';
6790 for (i = 0; i < cmp->glyph_len; i++)
6791 /* TAB in a composition means display glyphs with
6792 padding space on the left or right. */
6793 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6794 break;
6795 }
6796 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6797 }
6798 }
6799
6800 done:
6801 /* Is this character the last one of a run of characters with
6802 box? If yes, set IT->end_of_box_run_p to 1. */
6803 if (it->face_box_p
6804 && it->s == NULL)
6805 {
6806 if (it->method == GET_FROM_STRING && it->sp)
6807 {
6808 int face_id = underlying_face_id (it);
6809 struct face *face = FACE_FROM_ID (it->f, face_id);
6810
6811 if (face)
6812 {
6813 if (face->box == FACE_NO_BOX)
6814 {
6815 /* If the box comes from face properties in a
6816 display string, check faces in that string. */
6817 int string_face_id = face_after_it_pos (it);
6818 it->end_of_box_run_p
6819 = (FACE_FROM_ID (it->f, string_face_id)->box
6820 == FACE_NO_BOX);
6821 }
6822 /* Otherwise, the box comes from the underlying face.
6823 If this is the last string character displayed, check
6824 the next buffer location. */
6825 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6826 && (it->current.overlay_string_index
6827 == it->n_overlay_strings - 1))
6828 {
6829 ptrdiff_t ignore;
6830 int next_face_id;
6831 struct text_pos pos = it->current.pos;
6832 INC_TEXT_POS (pos, it->multibyte_p);
6833
6834 next_face_id = face_at_buffer_position
6835 (it->w, CHARPOS (pos), it->region_beg_charpos,
6836 it->region_end_charpos, &ignore,
6837 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6838 -1);
6839 it->end_of_box_run_p
6840 = (FACE_FROM_ID (it->f, next_face_id)->box
6841 == FACE_NO_BOX);
6842 }
6843 }
6844 }
6845 else
6846 {
6847 int face_id = face_after_it_pos (it);
6848 it->end_of_box_run_p
6849 = (face_id != it->face_id
6850 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6851 }
6852 }
6853 /* If we reached the end of the object we've been iterating (e.g., a
6854 display string or an overlay string), and there's something on
6855 IT->stack, proceed with what's on the stack. It doesn't make
6856 sense to return zero if there's unprocessed stuff on the stack,
6857 because otherwise that stuff will never be displayed. */
6858 if (!success_p && it->sp > 0)
6859 {
6860 set_iterator_to_next (it, 0);
6861 success_p = get_next_display_element (it);
6862 }
6863
6864 /* Value is 0 if end of buffer or string reached. */
6865 return success_p;
6866 }
6867
6868
6869 /* Move IT to the next display element.
6870
6871 RESEAT_P non-zero means if called on a newline in buffer text,
6872 skip to the next visible line start.
6873
6874 Functions get_next_display_element and set_iterator_to_next are
6875 separate because I find this arrangement easier to handle than a
6876 get_next_display_element function that also increments IT's
6877 position. The way it is we can first look at an iterator's current
6878 display element, decide whether it fits on a line, and if it does,
6879 increment the iterator position. The other way around we probably
6880 would either need a flag indicating whether the iterator has to be
6881 incremented the next time, or we would have to implement a
6882 decrement position function which would not be easy to write. */
6883
6884 void
6885 set_iterator_to_next (struct it *it, int reseat_p)
6886 {
6887 /* Reset flags indicating start and end of a sequence of characters
6888 with box. Reset them at the start of this function because
6889 moving the iterator to a new position might set them. */
6890 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6891
6892 switch (it->method)
6893 {
6894 case GET_FROM_BUFFER:
6895 /* The current display element of IT is a character from
6896 current_buffer. Advance in the buffer, and maybe skip over
6897 invisible lines that are so because of selective display. */
6898 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6899 reseat_at_next_visible_line_start (it, 0);
6900 else if (it->cmp_it.id >= 0)
6901 {
6902 /* We are currently getting glyphs from a composition. */
6903 int i;
6904
6905 if (! it->bidi_p)
6906 {
6907 IT_CHARPOS (*it) += it->cmp_it.nchars;
6908 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6909 if (it->cmp_it.to < it->cmp_it.nglyphs)
6910 {
6911 it->cmp_it.from = it->cmp_it.to;
6912 }
6913 else
6914 {
6915 it->cmp_it.id = -1;
6916 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6917 IT_BYTEPOS (*it),
6918 it->end_charpos, Qnil);
6919 }
6920 }
6921 else if (! it->cmp_it.reversed_p)
6922 {
6923 /* Composition created while scanning forward. */
6924 /* Update IT's char/byte positions to point to the first
6925 character of the next grapheme cluster, or to the
6926 character visually after the current composition. */
6927 for (i = 0; i < it->cmp_it.nchars; i++)
6928 bidi_move_to_visually_next (&it->bidi_it);
6929 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6930 IT_CHARPOS (*it) = it->bidi_it.charpos;
6931
6932 if (it->cmp_it.to < it->cmp_it.nglyphs)
6933 {
6934 /* Proceed to the next grapheme cluster. */
6935 it->cmp_it.from = it->cmp_it.to;
6936 }
6937 else
6938 {
6939 /* No more grapheme clusters in this composition.
6940 Find the next stop position. */
6941 ptrdiff_t stop = it->end_charpos;
6942 if (it->bidi_it.scan_dir < 0)
6943 /* Now we are scanning backward and don't know
6944 where to stop. */
6945 stop = -1;
6946 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6947 IT_BYTEPOS (*it), stop, Qnil);
6948 }
6949 }
6950 else
6951 {
6952 /* Composition created while scanning backward. */
6953 /* Update IT's char/byte positions to point to the last
6954 character of the previous grapheme cluster, or the
6955 character visually after the current composition. */
6956 for (i = 0; i < it->cmp_it.nchars; i++)
6957 bidi_move_to_visually_next (&it->bidi_it);
6958 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6959 IT_CHARPOS (*it) = it->bidi_it.charpos;
6960 if (it->cmp_it.from > 0)
6961 {
6962 /* Proceed to the previous grapheme cluster. */
6963 it->cmp_it.to = it->cmp_it.from;
6964 }
6965 else
6966 {
6967 /* No more grapheme clusters in this composition.
6968 Find the next stop position. */
6969 ptrdiff_t stop = it->end_charpos;
6970 if (it->bidi_it.scan_dir < 0)
6971 /* Now we are scanning backward and don't know
6972 where to stop. */
6973 stop = -1;
6974 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6975 IT_BYTEPOS (*it), stop, Qnil);
6976 }
6977 }
6978 }
6979 else
6980 {
6981 xassert (it->len != 0);
6982
6983 if (!it->bidi_p)
6984 {
6985 IT_BYTEPOS (*it) += it->len;
6986 IT_CHARPOS (*it) += 1;
6987 }
6988 else
6989 {
6990 int prev_scan_dir = it->bidi_it.scan_dir;
6991 /* If this is a new paragraph, determine its base
6992 direction (a.k.a. its base embedding level). */
6993 if (it->bidi_it.new_paragraph)
6994 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6995 bidi_move_to_visually_next (&it->bidi_it);
6996 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6997 IT_CHARPOS (*it) = it->bidi_it.charpos;
6998 if (prev_scan_dir != it->bidi_it.scan_dir)
6999 {
7000 /* As the scan direction was changed, we must
7001 re-compute the stop position for composition. */
7002 ptrdiff_t stop = it->end_charpos;
7003 if (it->bidi_it.scan_dir < 0)
7004 stop = -1;
7005 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7006 IT_BYTEPOS (*it), stop, Qnil);
7007 }
7008 }
7009 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7010 }
7011 break;
7012
7013 case GET_FROM_C_STRING:
7014 /* Current display element of IT is from a C string. */
7015 if (!it->bidi_p
7016 /* If the string position is beyond string's end, it means
7017 next_element_from_c_string is padding the string with
7018 blanks, in which case we bypass the bidi iterator,
7019 because it cannot deal with such virtual characters. */
7020 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7021 {
7022 IT_BYTEPOS (*it) += it->len;
7023 IT_CHARPOS (*it) += 1;
7024 }
7025 else
7026 {
7027 bidi_move_to_visually_next (&it->bidi_it);
7028 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7029 IT_CHARPOS (*it) = it->bidi_it.charpos;
7030 }
7031 break;
7032
7033 case GET_FROM_DISPLAY_VECTOR:
7034 /* Current display element of IT is from a display table entry.
7035 Advance in the display table definition. Reset it to null if
7036 end reached, and continue with characters from buffers/
7037 strings. */
7038 ++it->current.dpvec_index;
7039
7040 /* Restore face of the iterator to what they were before the
7041 display vector entry (these entries may contain faces). */
7042 it->face_id = it->saved_face_id;
7043
7044 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7045 {
7046 int recheck_faces = it->ellipsis_p;
7047
7048 if (it->s)
7049 it->method = GET_FROM_C_STRING;
7050 else if (STRINGP (it->string))
7051 it->method = GET_FROM_STRING;
7052 else
7053 {
7054 it->method = GET_FROM_BUFFER;
7055 it->object = it->w->buffer;
7056 }
7057
7058 it->dpvec = NULL;
7059 it->current.dpvec_index = -1;
7060
7061 /* Skip over characters which were displayed via IT->dpvec. */
7062 if (it->dpvec_char_len < 0)
7063 reseat_at_next_visible_line_start (it, 1);
7064 else if (it->dpvec_char_len > 0)
7065 {
7066 if (it->method == GET_FROM_STRING
7067 && it->n_overlay_strings > 0)
7068 it->ignore_overlay_strings_at_pos_p = 1;
7069 it->len = it->dpvec_char_len;
7070 set_iterator_to_next (it, reseat_p);
7071 }
7072
7073 /* Maybe recheck faces after display vector */
7074 if (recheck_faces)
7075 it->stop_charpos = IT_CHARPOS (*it);
7076 }
7077 break;
7078
7079 case GET_FROM_STRING:
7080 /* Current display element is a character from a Lisp string. */
7081 xassert (it->s == NULL && STRINGP (it->string));
7082 /* Don't advance past string end. These conditions are true
7083 when set_iterator_to_next is called at the end of
7084 get_next_display_element, in which case the Lisp string is
7085 already exhausted, and all we want is pop the iterator
7086 stack. */
7087 if (it->current.overlay_string_index >= 0)
7088 {
7089 /* This is an overlay string, so there's no padding with
7090 spaces, and the number of characters in the string is
7091 where the string ends. */
7092 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7093 goto consider_string_end;
7094 }
7095 else
7096 {
7097 /* Not an overlay string. There could be padding, so test
7098 against it->end_charpos . */
7099 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7100 goto consider_string_end;
7101 }
7102 if (it->cmp_it.id >= 0)
7103 {
7104 int i;
7105
7106 if (! it->bidi_p)
7107 {
7108 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7109 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7110 if (it->cmp_it.to < it->cmp_it.nglyphs)
7111 it->cmp_it.from = it->cmp_it.to;
7112 else
7113 {
7114 it->cmp_it.id = -1;
7115 composition_compute_stop_pos (&it->cmp_it,
7116 IT_STRING_CHARPOS (*it),
7117 IT_STRING_BYTEPOS (*it),
7118 it->end_charpos, it->string);
7119 }
7120 }
7121 else if (! it->cmp_it.reversed_p)
7122 {
7123 for (i = 0; i < it->cmp_it.nchars; i++)
7124 bidi_move_to_visually_next (&it->bidi_it);
7125 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7126 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7127
7128 if (it->cmp_it.to < it->cmp_it.nglyphs)
7129 it->cmp_it.from = it->cmp_it.to;
7130 else
7131 {
7132 ptrdiff_t stop = it->end_charpos;
7133 if (it->bidi_it.scan_dir < 0)
7134 stop = -1;
7135 composition_compute_stop_pos (&it->cmp_it,
7136 IT_STRING_CHARPOS (*it),
7137 IT_STRING_BYTEPOS (*it), stop,
7138 it->string);
7139 }
7140 }
7141 else
7142 {
7143 for (i = 0; i < it->cmp_it.nchars; i++)
7144 bidi_move_to_visually_next (&it->bidi_it);
7145 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7146 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7147 if (it->cmp_it.from > 0)
7148 it->cmp_it.to = it->cmp_it.from;
7149 else
7150 {
7151 ptrdiff_t stop = it->end_charpos;
7152 if (it->bidi_it.scan_dir < 0)
7153 stop = -1;
7154 composition_compute_stop_pos (&it->cmp_it,
7155 IT_STRING_CHARPOS (*it),
7156 IT_STRING_BYTEPOS (*it), stop,
7157 it->string);
7158 }
7159 }
7160 }
7161 else
7162 {
7163 if (!it->bidi_p
7164 /* If the string position is beyond string's end, it
7165 means next_element_from_string is padding the string
7166 with blanks, in which case we bypass the bidi
7167 iterator, because it cannot deal with such virtual
7168 characters. */
7169 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7170 {
7171 IT_STRING_BYTEPOS (*it) += it->len;
7172 IT_STRING_CHARPOS (*it) += 1;
7173 }
7174 else
7175 {
7176 int prev_scan_dir = it->bidi_it.scan_dir;
7177
7178 bidi_move_to_visually_next (&it->bidi_it);
7179 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7180 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7181 if (prev_scan_dir != it->bidi_it.scan_dir)
7182 {
7183 ptrdiff_t stop = it->end_charpos;
7184
7185 if (it->bidi_it.scan_dir < 0)
7186 stop = -1;
7187 composition_compute_stop_pos (&it->cmp_it,
7188 IT_STRING_CHARPOS (*it),
7189 IT_STRING_BYTEPOS (*it), stop,
7190 it->string);
7191 }
7192 }
7193 }
7194
7195 consider_string_end:
7196
7197 if (it->current.overlay_string_index >= 0)
7198 {
7199 /* IT->string is an overlay string. Advance to the
7200 next, if there is one. */
7201 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7202 {
7203 it->ellipsis_p = 0;
7204 next_overlay_string (it);
7205 if (it->ellipsis_p)
7206 setup_for_ellipsis (it, 0);
7207 }
7208 }
7209 else
7210 {
7211 /* IT->string is not an overlay string. If we reached
7212 its end, and there is something on IT->stack, proceed
7213 with what is on the stack. This can be either another
7214 string, this time an overlay string, or a buffer. */
7215 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7216 && it->sp > 0)
7217 {
7218 pop_it (it);
7219 if (it->method == GET_FROM_STRING)
7220 goto consider_string_end;
7221 }
7222 }
7223 break;
7224
7225 case GET_FROM_IMAGE:
7226 case GET_FROM_STRETCH:
7227 /* The position etc with which we have to proceed are on
7228 the stack. The position may be at the end of a string,
7229 if the `display' property takes up the whole string. */
7230 xassert (it->sp > 0);
7231 pop_it (it);
7232 if (it->method == GET_FROM_STRING)
7233 goto consider_string_end;
7234 break;
7235
7236 default:
7237 /* There are no other methods defined, so this should be a bug. */
7238 abort ();
7239 }
7240
7241 xassert (it->method != GET_FROM_STRING
7242 || (STRINGP (it->string)
7243 && IT_STRING_CHARPOS (*it) >= 0));
7244 }
7245
7246 /* Load IT's display element fields with information about the next
7247 display element which comes from a display table entry or from the
7248 result of translating a control character to one of the forms `^C'
7249 or `\003'.
7250
7251 IT->dpvec holds the glyphs to return as characters.
7252 IT->saved_face_id holds the face id before the display vector--it
7253 is restored into IT->face_id in set_iterator_to_next. */
7254
7255 static int
7256 next_element_from_display_vector (struct it *it)
7257 {
7258 Lisp_Object gc;
7259
7260 /* Precondition. */
7261 xassert (it->dpvec && it->current.dpvec_index >= 0);
7262
7263 it->face_id = it->saved_face_id;
7264
7265 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7266 That seemed totally bogus - so I changed it... */
7267 gc = it->dpvec[it->current.dpvec_index];
7268
7269 if (GLYPH_CODE_P (gc))
7270 {
7271 it->c = GLYPH_CODE_CHAR (gc);
7272 it->len = CHAR_BYTES (it->c);
7273
7274 /* The entry may contain a face id to use. Such a face id is
7275 the id of a Lisp face, not a realized face. A face id of
7276 zero means no face is specified. */
7277 if (it->dpvec_face_id >= 0)
7278 it->face_id = it->dpvec_face_id;
7279 else
7280 {
7281 int lface_id = GLYPH_CODE_FACE (gc);
7282 if (lface_id > 0)
7283 it->face_id = merge_faces (it->f, Qt, lface_id,
7284 it->saved_face_id);
7285 }
7286 }
7287 else
7288 /* Display table entry is invalid. Return a space. */
7289 it->c = ' ', it->len = 1;
7290
7291 /* Don't change position and object of the iterator here. They are
7292 still the values of the character that had this display table
7293 entry or was translated, and that's what we want. */
7294 it->what = IT_CHARACTER;
7295 return 1;
7296 }
7297
7298 /* Get the first element of string/buffer in the visual order, after
7299 being reseated to a new position in a string or a buffer. */
7300 static void
7301 get_visually_first_element (struct it *it)
7302 {
7303 int string_p = STRINGP (it->string) || it->s;
7304 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7305 ptrdiff_t bob = (string_p ? 0 : BEGV);
7306
7307 if (STRINGP (it->string))
7308 {
7309 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7310 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7311 }
7312 else
7313 {
7314 it->bidi_it.charpos = IT_CHARPOS (*it);
7315 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7316 }
7317
7318 if (it->bidi_it.charpos == eob)
7319 {
7320 /* Nothing to do, but reset the FIRST_ELT flag, like
7321 bidi_paragraph_init does, because we are not going to
7322 call it. */
7323 it->bidi_it.first_elt = 0;
7324 }
7325 else if (it->bidi_it.charpos == bob
7326 || (!string_p
7327 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7328 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7329 {
7330 /* If we are at the beginning of a line/string, we can produce
7331 the next element right away. */
7332 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7333 bidi_move_to_visually_next (&it->bidi_it);
7334 }
7335 else
7336 {
7337 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7338
7339 /* We need to prime the bidi iterator starting at the line's or
7340 string's beginning, before we will be able to produce the
7341 next element. */
7342 if (string_p)
7343 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7344 else
7345 {
7346 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7347 -1);
7348 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7349 }
7350 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7351 do
7352 {
7353 /* Now return to buffer/string position where we were asked
7354 to get the next display element, and produce that. */
7355 bidi_move_to_visually_next (&it->bidi_it);
7356 }
7357 while (it->bidi_it.bytepos != orig_bytepos
7358 && it->bidi_it.charpos < eob);
7359 }
7360
7361 /* Adjust IT's position information to where we ended up. */
7362 if (STRINGP (it->string))
7363 {
7364 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7365 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7366 }
7367 else
7368 {
7369 IT_CHARPOS (*it) = it->bidi_it.charpos;
7370 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7371 }
7372
7373 if (STRINGP (it->string) || !it->s)
7374 {
7375 ptrdiff_t stop, charpos, bytepos;
7376
7377 if (STRINGP (it->string))
7378 {
7379 xassert (!it->s);
7380 stop = SCHARS (it->string);
7381 if (stop > it->end_charpos)
7382 stop = it->end_charpos;
7383 charpos = IT_STRING_CHARPOS (*it);
7384 bytepos = IT_STRING_BYTEPOS (*it);
7385 }
7386 else
7387 {
7388 stop = it->end_charpos;
7389 charpos = IT_CHARPOS (*it);
7390 bytepos = IT_BYTEPOS (*it);
7391 }
7392 if (it->bidi_it.scan_dir < 0)
7393 stop = -1;
7394 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7395 it->string);
7396 }
7397 }
7398
7399 /* Load IT with the next display element from Lisp string IT->string.
7400 IT->current.string_pos is the current position within the string.
7401 If IT->current.overlay_string_index >= 0, the Lisp string is an
7402 overlay string. */
7403
7404 static int
7405 next_element_from_string (struct it *it)
7406 {
7407 struct text_pos position;
7408
7409 xassert (STRINGP (it->string));
7410 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7411 xassert (IT_STRING_CHARPOS (*it) >= 0);
7412 position = it->current.string_pos;
7413
7414 /* With bidi reordering, the character to display might not be the
7415 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7416 that we were reseat()ed to a new string, whose paragraph
7417 direction is not known. */
7418 if (it->bidi_p && it->bidi_it.first_elt)
7419 {
7420 get_visually_first_element (it);
7421 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7422 }
7423
7424 /* Time to check for invisible text? */
7425 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7426 {
7427 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7428 {
7429 if (!(!it->bidi_p
7430 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7431 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7432 {
7433 /* With bidi non-linear iteration, we could find
7434 ourselves far beyond the last computed stop_charpos,
7435 with several other stop positions in between that we
7436 missed. Scan them all now, in buffer's logical
7437 order, until we find and handle the last stop_charpos
7438 that precedes our current position. */
7439 handle_stop_backwards (it, it->stop_charpos);
7440 return GET_NEXT_DISPLAY_ELEMENT (it);
7441 }
7442 else
7443 {
7444 if (it->bidi_p)
7445 {
7446 /* Take note of the stop position we just moved
7447 across, for when we will move back across it. */
7448 it->prev_stop = it->stop_charpos;
7449 /* If we are at base paragraph embedding level, take
7450 note of the last stop position seen at this
7451 level. */
7452 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7453 it->base_level_stop = it->stop_charpos;
7454 }
7455 handle_stop (it);
7456
7457 /* Since a handler may have changed IT->method, we must
7458 recurse here. */
7459 return GET_NEXT_DISPLAY_ELEMENT (it);
7460 }
7461 }
7462 else if (it->bidi_p
7463 /* If we are before prev_stop, we may have overstepped
7464 on our way backwards a stop_pos, and if so, we need
7465 to handle that stop_pos. */
7466 && IT_STRING_CHARPOS (*it) < it->prev_stop
7467 /* We can sometimes back up for reasons that have nothing
7468 to do with bidi reordering. E.g., compositions. The
7469 code below is only needed when we are above the base
7470 embedding level, so test for that explicitly. */
7471 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7472 {
7473 /* If we lost track of base_level_stop, we have no better
7474 place for handle_stop_backwards to start from than string
7475 beginning. This happens, e.g., when we were reseated to
7476 the previous screenful of text by vertical-motion. */
7477 if (it->base_level_stop <= 0
7478 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7479 it->base_level_stop = 0;
7480 handle_stop_backwards (it, it->base_level_stop);
7481 return GET_NEXT_DISPLAY_ELEMENT (it);
7482 }
7483 }
7484
7485 if (it->current.overlay_string_index >= 0)
7486 {
7487 /* Get the next character from an overlay string. In overlay
7488 strings, there is no field width or padding with spaces to
7489 do. */
7490 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7491 {
7492 it->what = IT_EOB;
7493 return 0;
7494 }
7495 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7496 IT_STRING_BYTEPOS (*it),
7497 it->bidi_it.scan_dir < 0
7498 ? -1
7499 : SCHARS (it->string))
7500 && next_element_from_composition (it))
7501 {
7502 return 1;
7503 }
7504 else if (STRING_MULTIBYTE (it->string))
7505 {
7506 const unsigned char *s = (SDATA (it->string)
7507 + IT_STRING_BYTEPOS (*it));
7508 it->c = string_char_and_length (s, &it->len);
7509 }
7510 else
7511 {
7512 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7513 it->len = 1;
7514 }
7515 }
7516 else
7517 {
7518 /* Get the next character from a Lisp string that is not an
7519 overlay string. Such strings come from the mode line, for
7520 example. We may have to pad with spaces, or truncate the
7521 string. See also next_element_from_c_string. */
7522 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7523 {
7524 it->what = IT_EOB;
7525 return 0;
7526 }
7527 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7528 {
7529 /* Pad with spaces. */
7530 it->c = ' ', it->len = 1;
7531 CHARPOS (position) = BYTEPOS (position) = -1;
7532 }
7533 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7534 IT_STRING_BYTEPOS (*it),
7535 it->bidi_it.scan_dir < 0
7536 ? -1
7537 : it->string_nchars)
7538 && next_element_from_composition (it))
7539 {
7540 return 1;
7541 }
7542 else if (STRING_MULTIBYTE (it->string))
7543 {
7544 const unsigned char *s = (SDATA (it->string)
7545 + IT_STRING_BYTEPOS (*it));
7546 it->c = string_char_and_length (s, &it->len);
7547 }
7548 else
7549 {
7550 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7551 it->len = 1;
7552 }
7553 }
7554
7555 /* Record what we have and where it came from. */
7556 it->what = IT_CHARACTER;
7557 it->object = it->string;
7558 it->position = position;
7559 return 1;
7560 }
7561
7562
7563 /* Load IT with next display element from C string IT->s.
7564 IT->string_nchars is the maximum number of characters to return
7565 from the string. IT->end_charpos may be greater than
7566 IT->string_nchars when this function is called, in which case we
7567 may have to return padding spaces. Value is zero if end of string
7568 reached, including padding spaces. */
7569
7570 static int
7571 next_element_from_c_string (struct it *it)
7572 {
7573 int success_p = 1;
7574
7575 xassert (it->s);
7576 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7577 it->what = IT_CHARACTER;
7578 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7579 it->object = Qnil;
7580
7581 /* With bidi reordering, the character to display might not be the
7582 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7583 we were reseated to a new string, whose paragraph direction is
7584 not known. */
7585 if (it->bidi_p && it->bidi_it.first_elt)
7586 get_visually_first_element (it);
7587
7588 /* IT's position can be greater than IT->string_nchars in case a
7589 field width or precision has been specified when the iterator was
7590 initialized. */
7591 if (IT_CHARPOS (*it) >= it->end_charpos)
7592 {
7593 /* End of the game. */
7594 it->what = IT_EOB;
7595 success_p = 0;
7596 }
7597 else if (IT_CHARPOS (*it) >= it->string_nchars)
7598 {
7599 /* Pad with spaces. */
7600 it->c = ' ', it->len = 1;
7601 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7602 }
7603 else if (it->multibyte_p)
7604 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7605 else
7606 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7607
7608 return success_p;
7609 }
7610
7611
7612 /* Set up IT to return characters from an ellipsis, if appropriate.
7613 The definition of the ellipsis glyphs may come from a display table
7614 entry. This function fills IT with the first glyph from the
7615 ellipsis if an ellipsis is to be displayed. */
7616
7617 static int
7618 next_element_from_ellipsis (struct it *it)
7619 {
7620 if (it->selective_display_ellipsis_p)
7621 setup_for_ellipsis (it, it->len);
7622 else
7623 {
7624 /* The face at the current position may be different from the
7625 face we find after the invisible text. Remember what it
7626 was in IT->saved_face_id, and signal that it's there by
7627 setting face_before_selective_p. */
7628 it->saved_face_id = it->face_id;
7629 it->method = GET_FROM_BUFFER;
7630 it->object = it->w->buffer;
7631 reseat_at_next_visible_line_start (it, 1);
7632 it->face_before_selective_p = 1;
7633 }
7634
7635 return GET_NEXT_DISPLAY_ELEMENT (it);
7636 }
7637
7638
7639 /* Deliver an image display element. The iterator IT is already
7640 filled with image information (done in handle_display_prop). Value
7641 is always 1. */
7642
7643
7644 static int
7645 next_element_from_image (struct it *it)
7646 {
7647 it->what = IT_IMAGE;
7648 it->ignore_overlay_strings_at_pos_p = 0;
7649 return 1;
7650 }
7651
7652
7653 /* Fill iterator IT with next display element from a stretch glyph
7654 property. IT->object is the value of the text property. Value is
7655 always 1. */
7656
7657 static int
7658 next_element_from_stretch (struct it *it)
7659 {
7660 it->what = IT_STRETCH;
7661 return 1;
7662 }
7663
7664 /* Scan backwards from IT's current position until we find a stop
7665 position, or until BEGV. This is called when we find ourself
7666 before both the last known prev_stop and base_level_stop while
7667 reordering bidirectional text. */
7668
7669 static void
7670 compute_stop_pos_backwards (struct it *it)
7671 {
7672 const int SCAN_BACK_LIMIT = 1000;
7673 struct text_pos pos;
7674 struct display_pos save_current = it->current;
7675 struct text_pos save_position = it->position;
7676 ptrdiff_t charpos = IT_CHARPOS (*it);
7677 ptrdiff_t where_we_are = charpos;
7678 ptrdiff_t save_stop_pos = it->stop_charpos;
7679 ptrdiff_t save_end_pos = it->end_charpos;
7680
7681 xassert (NILP (it->string) && !it->s);
7682 xassert (it->bidi_p);
7683 it->bidi_p = 0;
7684 do
7685 {
7686 it->end_charpos = min (charpos + 1, ZV);
7687 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7688 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7689 reseat_1 (it, pos, 0);
7690 compute_stop_pos (it);
7691 /* We must advance forward, right? */
7692 if (it->stop_charpos <= charpos)
7693 abort ();
7694 }
7695 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7696
7697 if (it->stop_charpos <= where_we_are)
7698 it->prev_stop = it->stop_charpos;
7699 else
7700 it->prev_stop = BEGV;
7701 it->bidi_p = 1;
7702 it->current = save_current;
7703 it->position = save_position;
7704 it->stop_charpos = save_stop_pos;
7705 it->end_charpos = save_end_pos;
7706 }
7707
7708 /* Scan forward from CHARPOS in the current buffer/string, until we
7709 find a stop position > current IT's position. Then handle the stop
7710 position before that. This is called when we bump into a stop
7711 position while reordering bidirectional text. CHARPOS should be
7712 the last previously processed stop_pos (or BEGV/0, if none were
7713 processed yet) whose position is less that IT's current
7714 position. */
7715
7716 static void
7717 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7718 {
7719 int bufp = !STRINGP (it->string);
7720 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7721 struct display_pos save_current = it->current;
7722 struct text_pos save_position = it->position;
7723 struct text_pos pos1;
7724 ptrdiff_t next_stop;
7725
7726 /* Scan in strict logical order. */
7727 xassert (it->bidi_p);
7728 it->bidi_p = 0;
7729 do
7730 {
7731 it->prev_stop = charpos;
7732 if (bufp)
7733 {
7734 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7735 reseat_1 (it, pos1, 0);
7736 }
7737 else
7738 it->current.string_pos = string_pos (charpos, it->string);
7739 compute_stop_pos (it);
7740 /* We must advance forward, right? */
7741 if (it->stop_charpos <= it->prev_stop)
7742 abort ();
7743 charpos = it->stop_charpos;
7744 }
7745 while (charpos <= where_we_are);
7746
7747 it->bidi_p = 1;
7748 it->current = save_current;
7749 it->position = save_position;
7750 next_stop = it->stop_charpos;
7751 it->stop_charpos = it->prev_stop;
7752 handle_stop (it);
7753 it->stop_charpos = next_stop;
7754 }
7755
7756 /* Load IT with the next display element from current_buffer. Value
7757 is zero if end of buffer reached. IT->stop_charpos is the next
7758 position at which to stop and check for text properties or buffer
7759 end. */
7760
7761 static int
7762 next_element_from_buffer (struct it *it)
7763 {
7764 int success_p = 1;
7765
7766 xassert (IT_CHARPOS (*it) >= BEGV);
7767 xassert (NILP (it->string) && !it->s);
7768 xassert (!it->bidi_p
7769 || (EQ (it->bidi_it.string.lstring, Qnil)
7770 && it->bidi_it.string.s == NULL));
7771
7772 /* With bidi reordering, the character to display might not be the
7773 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7774 we were reseat()ed to a new buffer position, which is potentially
7775 a different paragraph. */
7776 if (it->bidi_p && it->bidi_it.first_elt)
7777 {
7778 get_visually_first_element (it);
7779 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7780 }
7781
7782 if (IT_CHARPOS (*it) >= it->stop_charpos)
7783 {
7784 if (IT_CHARPOS (*it) >= it->end_charpos)
7785 {
7786 int overlay_strings_follow_p;
7787
7788 /* End of the game, except when overlay strings follow that
7789 haven't been returned yet. */
7790 if (it->overlay_strings_at_end_processed_p)
7791 overlay_strings_follow_p = 0;
7792 else
7793 {
7794 it->overlay_strings_at_end_processed_p = 1;
7795 overlay_strings_follow_p = get_overlay_strings (it, 0);
7796 }
7797
7798 if (overlay_strings_follow_p)
7799 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7800 else
7801 {
7802 it->what = IT_EOB;
7803 it->position = it->current.pos;
7804 success_p = 0;
7805 }
7806 }
7807 else if (!(!it->bidi_p
7808 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7809 || IT_CHARPOS (*it) == it->stop_charpos))
7810 {
7811 /* With bidi non-linear iteration, we could find ourselves
7812 far beyond the last computed stop_charpos, with several
7813 other stop positions in between that we missed. Scan
7814 them all now, in buffer's logical order, until we find
7815 and handle the last stop_charpos that precedes our
7816 current position. */
7817 handle_stop_backwards (it, it->stop_charpos);
7818 return GET_NEXT_DISPLAY_ELEMENT (it);
7819 }
7820 else
7821 {
7822 if (it->bidi_p)
7823 {
7824 /* Take note of the stop position we just moved across,
7825 for when we will move back across it. */
7826 it->prev_stop = it->stop_charpos;
7827 /* If we are at base paragraph embedding level, take
7828 note of the last stop position seen at this
7829 level. */
7830 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7831 it->base_level_stop = it->stop_charpos;
7832 }
7833 handle_stop (it);
7834 return GET_NEXT_DISPLAY_ELEMENT (it);
7835 }
7836 }
7837 else if (it->bidi_p
7838 /* If we are before prev_stop, we may have overstepped on
7839 our way backwards a stop_pos, and if so, we need to
7840 handle that stop_pos. */
7841 && IT_CHARPOS (*it) < it->prev_stop
7842 /* We can sometimes back up for reasons that have nothing
7843 to do with bidi reordering. E.g., compositions. The
7844 code below is only needed when we are above the base
7845 embedding level, so test for that explicitly. */
7846 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7847 {
7848 if (it->base_level_stop <= 0
7849 || IT_CHARPOS (*it) < it->base_level_stop)
7850 {
7851 /* If we lost track of base_level_stop, we need to find
7852 prev_stop by looking backwards. This happens, e.g., when
7853 we were reseated to the previous screenful of text by
7854 vertical-motion. */
7855 it->base_level_stop = BEGV;
7856 compute_stop_pos_backwards (it);
7857 handle_stop_backwards (it, it->prev_stop);
7858 }
7859 else
7860 handle_stop_backwards (it, it->base_level_stop);
7861 return GET_NEXT_DISPLAY_ELEMENT (it);
7862 }
7863 else
7864 {
7865 /* No face changes, overlays etc. in sight, so just return a
7866 character from current_buffer. */
7867 unsigned char *p;
7868 ptrdiff_t stop;
7869
7870 /* Maybe run the redisplay end trigger hook. Performance note:
7871 This doesn't seem to cost measurable time. */
7872 if (it->redisplay_end_trigger_charpos
7873 && it->glyph_row
7874 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7875 run_redisplay_end_trigger_hook (it);
7876
7877 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7878 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7879 stop)
7880 && next_element_from_composition (it))
7881 {
7882 return 1;
7883 }
7884
7885 /* Get the next character, maybe multibyte. */
7886 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7887 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7888 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7889 else
7890 it->c = *p, it->len = 1;
7891
7892 /* Record what we have and where it came from. */
7893 it->what = IT_CHARACTER;
7894 it->object = it->w->buffer;
7895 it->position = it->current.pos;
7896
7897 /* Normally we return the character found above, except when we
7898 really want to return an ellipsis for selective display. */
7899 if (it->selective)
7900 {
7901 if (it->c == '\n')
7902 {
7903 /* A value of selective > 0 means hide lines indented more
7904 than that number of columns. */
7905 if (it->selective > 0
7906 && IT_CHARPOS (*it) + 1 < ZV
7907 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7908 IT_BYTEPOS (*it) + 1,
7909 it->selective))
7910 {
7911 success_p = next_element_from_ellipsis (it);
7912 it->dpvec_char_len = -1;
7913 }
7914 }
7915 else if (it->c == '\r' && it->selective == -1)
7916 {
7917 /* A value of selective == -1 means that everything from the
7918 CR to the end of the line is invisible, with maybe an
7919 ellipsis displayed for it. */
7920 success_p = next_element_from_ellipsis (it);
7921 it->dpvec_char_len = -1;
7922 }
7923 }
7924 }
7925
7926 /* Value is zero if end of buffer reached. */
7927 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7928 return success_p;
7929 }
7930
7931
7932 /* Run the redisplay end trigger hook for IT. */
7933
7934 static void
7935 run_redisplay_end_trigger_hook (struct it *it)
7936 {
7937 Lisp_Object args[3];
7938
7939 /* IT->glyph_row should be non-null, i.e. we should be actually
7940 displaying something, or otherwise we should not run the hook. */
7941 xassert (it->glyph_row);
7942
7943 /* Set up hook arguments. */
7944 args[0] = Qredisplay_end_trigger_functions;
7945 args[1] = it->window;
7946 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7947 it->redisplay_end_trigger_charpos = 0;
7948
7949 /* Since we are *trying* to run these functions, don't try to run
7950 them again, even if they get an error. */
7951 it->w->redisplay_end_trigger = Qnil;
7952 Frun_hook_with_args (3, args);
7953
7954 /* Notice if it changed the face of the character we are on. */
7955 handle_face_prop (it);
7956 }
7957
7958
7959 /* Deliver a composition display element. Unlike the other
7960 next_element_from_XXX, this function is not registered in the array
7961 get_next_element[]. It is called from next_element_from_buffer and
7962 next_element_from_string when necessary. */
7963
7964 static int
7965 next_element_from_composition (struct it *it)
7966 {
7967 it->what = IT_COMPOSITION;
7968 it->len = it->cmp_it.nbytes;
7969 if (STRINGP (it->string))
7970 {
7971 if (it->c < 0)
7972 {
7973 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7974 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7975 return 0;
7976 }
7977 it->position = it->current.string_pos;
7978 it->object = it->string;
7979 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7980 IT_STRING_BYTEPOS (*it), it->string);
7981 }
7982 else
7983 {
7984 if (it->c < 0)
7985 {
7986 IT_CHARPOS (*it) += it->cmp_it.nchars;
7987 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7988 if (it->bidi_p)
7989 {
7990 if (it->bidi_it.new_paragraph)
7991 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7992 /* Resync the bidi iterator with IT's new position.
7993 FIXME: this doesn't support bidirectional text. */
7994 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7995 bidi_move_to_visually_next (&it->bidi_it);
7996 }
7997 return 0;
7998 }
7999 it->position = it->current.pos;
8000 it->object = it->w->buffer;
8001 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8002 IT_BYTEPOS (*it), Qnil);
8003 }
8004 return 1;
8005 }
8006
8007
8008 \f
8009 /***********************************************************************
8010 Moving an iterator without producing glyphs
8011 ***********************************************************************/
8012
8013 /* Check if iterator is at a position corresponding to a valid buffer
8014 position after some move_it_ call. */
8015
8016 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8017 ((it)->method == GET_FROM_STRING \
8018 ? IT_STRING_CHARPOS (*it) == 0 \
8019 : 1)
8020
8021
8022 /* Move iterator IT to a specified buffer or X position within one
8023 line on the display without producing glyphs.
8024
8025 OP should be a bit mask including some or all of these bits:
8026 MOVE_TO_X: Stop upon reaching x-position TO_X.
8027 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8028 Regardless of OP's value, stop upon reaching the end of the display line.
8029
8030 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8031 This means, in particular, that TO_X includes window's horizontal
8032 scroll amount.
8033
8034 The return value has several possible values that
8035 say what condition caused the scan to stop:
8036
8037 MOVE_POS_MATCH_OR_ZV
8038 - when TO_POS or ZV was reached.
8039
8040 MOVE_X_REACHED
8041 -when TO_X was reached before TO_POS or ZV were reached.
8042
8043 MOVE_LINE_CONTINUED
8044 - when we reached the end of the display area and the line must
8045 be continued.
8046
8047 MOVE_LINE_TRUNCATED
8048 - when we reached the end of the display area and the line is
8049 truncated.
8050
8051 MOVE_NEWLINE_OR_CR
8052 - when we stopped at a line end, i.e. a newline or a CR and selective
8053 display is on. */
8054
8055 static enum move_it_result
8056 move_it_in_display_line_to (struct it *it,
8057 ptrdiff_t to_charpos, int to_x,
8058 enum move_operation_enum op)
8059 {
8060 enum move_it_result result = MOVE_UNDEFINED;
8061 struct glyph_row *saved_glyph_row;
8062 struct it wrap_it, atpos_it, atx_it, ppos_it;
8063 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8064 void *ppos_data = NULL;
8065 int may_wrap = 0;
8066 enum it_method prev_method = it->method;
8067 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8068 int saw_smaller_pos = prev_pos < to_charpos;
8069
8070 /* Don't produce glyphs in produce_glyphs. */
8071 saved_glyph_row = it->glyph_row;
8072 it->glyph_row = NULL;
8073
8074 /* Use wrap_it to save a copy of IT wherever a word wrap could
8075 occur. Use atpos_it to save a copy of IT at the desired buffer
8076 position, if found, so that we can scan ahead and check if the
8077 word later overshoots the window edge. Use atx_it similarly, for
8078 pixel positions. */
8079 wrap_it.sp = -1;
8080 atpos_it.sp = -1;
8081 atx_it.sp = -1;
8082
8083 /* Use ppos_it under bidi reordering to save a copy of IT for the
8084 position > CHARPOS that is the closest to CHARPOS. We restore
8085 that position in IT when we have scanned the entire display line
8086 without finding a match for CHARPOS and all the character
8087 positions are greater than CHARPOS. */
8088 if (it->bidi_p)
8089 {
8090 SAVE_IT (ppos_it, *it, ppos_data);
8091 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8092 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8093 SAVE_IT (ppos_it, *it, ppos_data);
8094 }
8095
8096 #define BUFFER_POS_REACHED_P() \
8097 ((op & MOVE_TO_POS) != 0 \
8098 && BUFFERP (it->object) \
8099 && (IT_CHARPOS (*it) == to_charpos \
8100 || ((!it->bidi_p \
8101 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8102 && IT_CHARPOS (*it) > to_charpos) \
8103 || (it->what == IT_COMPOSITION \
8104 && ((IT_CHARPOS (*it) > to_charpos \
8105 && to_charpos >= it->cmp_it.charpos) \
8106 || (IT_CHARPOS (*it) < to_charpos \
8107 && to_charpos <= it->cmp_it.charpos)))) \
8108 && (it->method == GET_FROM_BUFFER \
8109 || (it->method == GET_FROM_DISPLAY_VECTOR \
8110 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8111
8112 /* If there's a line-/wrap-prefix, handle it. */
8113 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8114 && it->current_y < it->last_visible_y)
8115 handle_line_prefix (it);
8116
8117 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8118 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8119
8120 while (1)
8121 {
8122 int x, i, ascent = 0, descent = 0;
8123
8124 /* Utility macro to reset an iterator with x, ascent, and descent. */
8125 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8126 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8127 (IT)->max_descent = descent)
8128
8129 /* Stop if we move beyond TO_CHARPOS (after an image or a
8130 display string or stretch glyph). */
8131 if ((op & MOVE_TO_POS) != 0
8132 && BUFFERP (it->object)
8133 && it->method == GET_FROM_BUFFER
8134 && (((!it->bidi_p
8135 /* When the iterator is at base embedding level, we
8136 are guaranteed that characters are delivered for
8137 display in strictly increasing order of their
8138 buffer positions. */
8139 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8140 && IT_CHARPOS (*it) > to_charpos)
8141 || (it->bidi_p
8142 && (prev_method == GET_FROM_IMAGE
8143 || prev_method == GET_FROM_STRETCH
8144 || prev_method == GET_FROM_STRING)
8145 /* Passed TO_CHARPOS from left to right. */
8146 && ((prev_pos < to_charpos
8147 && IT_CHARPOS (*it) > to_charpos)
8148 /* Passed TO_CHARPOS from right to left. */
8149 || (prev_pos > to_charpos
8150 && IT_CHARPOS (*it) < to_charpos)))))
8151 {
8152 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8153 {
8154 result = MOVE_POS_MATCH_OR_ZV;
8155 break;
8156 }
8157 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8158 /* If wrap_it is valid, the current position might be in a
8159 word that is wrapped. So, save the iterator in
8160 atpos_it and continue to see if wrapping happens. */
8161 SAVE_IT (atpos_it, *it, atpos_data);
8162 }
8163
8164 /* Stop when ZV reached.
8165 We used to stop here when TO_CHARPOS reached as well, but that is
8166 too soon if this glyph does not fit on this line. So we handle it
8167 explicitly below. */
8168 if (!get_next_display_element (it))
8169 {
8170 result = MOVE_POS_MATCH_OR_ZV;
8171 break;
8172 }
8173
8174 if (it->line_wrap == TRUNCATE)
8175 {
8176 if (BUFFER_POS_REACHED_P ())
8177 {
8178 result = MOVE_POS_MATCH_OR_ZV;
8179 break;
8180 }
8181 }
8182 else
8183 {
8184 if (it->line_wrap == WORD_WRAP)
8185 {
8186 if (IT_DISPLAYING_WHITESPACE (it))
8187 may_wrap = 1;
8188 else if (may_wrap)
8189 {
8190 /* We have reached a glyph that follows one or more
8191 whitespace characters. If the position is
8192 already found, we are done. */
8193 if (atpos_it.sp >= 0)
8194 {
8195 RESTORE_IT (it, &atpos_it, atpos_data);
8196 result = MOVE_POS_MATCH_OR_ZV;
8197 goto done;
8198 }
8199 if (atx_it.sp >= 0)
8200 {
8201 RESTORE_IT (it, &atx_it, atx_data);
8202 result = MOVE_X_REACHED;
8203 goto done;
8204 }
8205 /* Otherwise, we can wrap here. */
8206 SAVE_IT (wrap_it, *it, wrap_data);
8207 may_wrap = 0;
8208 }
8209 }
8210 }
8211
8212 /* Remember the line height for the current line, in case
8213 the next element doesn't fit on the line. */
8214 ascent = it->max_ascent;
8215 descent = it->max_descent;
8216
8217 /* The call to produce_glyphs will get the metrics of the
8218 display element IT is loaded with. Record the x-position
8219 before this display element, in case it doesn't fit on the
8220 line. */
8221 x = it->current_x;
8222
8223 PRODUCE_GLYPHS (it);
8224
8225 if (it->area != TEXT_AREA)
8226 {
8227 prev_method = it->method;
8228 if (it->method == GET_FROM_BUFFER)
8229 prev_pos = IT_CHARPOS (*it);
8230 set_iterator_to_next (it, 1);
8231 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8232 SET_TEXT_POS (this_line_min_pos,
8233 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8234 if (it->bidi_p
8235 && (op & MOVE_TO_POS)
8236 && IT_CHARPOS (*it) > to_charpos
8237 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8238 SAVE_IT (ppos_it, *it, ppos_data);
8239 continue;
8240 }
8241
8242 /* The number of glyphs we get back in IT->nglyphs will normally
8243 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8244 character on a terminal frame, or (iii) a line end. For the
8245 second case, IT->nglyphs - 1 padding glyphs will be present.
8246 (On X frames, there is only one glyph produced for a
8247 composite character.)
8248
8249 The behavior implemented below means, for continuation lines,
8250 that as many spaces of a TAB as fit on the current line are
8251 displayed there. For terminal frames, as many glyphs of a
8252 multi-glyph character are displayed in the current line, too.
8253 This is what the old redisplay code did, and we keep it that
8254 way. Under X, the whole shape of a complex character must
8255 fit on the line or it will be completely displayed in the
8256 next line.
8257
8258 Note that both for tabs and padding glyphs, all glyphs have
8259 the same width. */
8260 if (it->nglyphs)
8261 {
8262 /* More than one glyph or glyph doesn't fit on line. All
8263 glyphs have the same width. */
8264 int single_glyph_width = it->pixel_width / it->nglyphs;
8265 int new_x;
8266 int x_before_this_char = x;
8267 int hpos_before_this_char = it->hpos;
8268
8269 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8270 {
8271 new_x = x + single_glyph_width;
8272
8273 /* We want to leave anything reaching TO_X to the caller. */
8274 if ((op & MOVE_TO_X) && new_x > to_x)
8275 {
8276 if (BUFFER_POS_REACHED_P ())
8277 {
8278 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8279 goto buffer_pos_reached;
8280 if (atpos_it.sp < 0)
8281 {
8282 SAVE_IT (atpos_it, *it, atpos_data);
8283 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8284 }
8285 }
8286 else
8287 {
8288 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8289 {
8290 it->current_x = x;
8291 result = MOVE_X_REACHED;
8292 break;
8293 }
8294 if (atx_it.sp < 0)
8295 {
8296 SAVE_IT (atx_it, *it, atx_data);
8297 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8298 }
8299 }
8300 }
8301
8302 if (/* Lines are continued. */
8303 it->line_wrap != TRUNCATE
8304 && (/* And glyph doesn't fit on the line. */
8305 new_x > it->last_visible_x
8306 /* Or it fits exactly and we're on a window
8307 system frame. */
8308 || (new_x == it->last_visible_x
8309 && FRAME_WINDOW_P (it->f))))
8310 {
8311 if (/* IT->hpos == 0 means the very first glyph
8312 doesn't fit on the line, e.g. a wide image. */
8313 it->hpos == 0
8314 || (new_x == it->last_visible_x
8315 && FRAME_WINDOW_P (it->f)))
8316 {
8317 ++it->hpos;
8318 it->current_x = new_x;
8319
8320 /* The character's last glyph just barely fits
8321 in this row. */
8322 if (i == it->nglyphs - 1)
8323 {
8324 /* If this is the destination position,
8325 return a position *before* it in this row,
8326 now that we know it fits in this row. */
8327 if (BUFFER_POS_REACHED_P ())
8328 {
8329 if (it->line_wrap != WORD_WRAP
8330 || wrap_it.sp < 0)
8331 {
8332 it->hpos = hpos_before_this_char;
8333 it->current_x = x_before_this_char;
8334 result = MOVE_POS_MATCH_OR_ZV;
8335 break;
8336 }
8337 if (it->line_wrap == WORD_WRAP
8338 && atpos_it.sp < 0)
8339 {
8340 SAVE_IT (atpos_it, *it, atpos_data);
8341 atpos_it.current_x = x_before_this_char;
8342 atpos_it.hpos = hpos_before_this_char;
8343 }
8344 }
8345
8346 prev_method = it->method;
8347 if (it->method == GET_FROM_BUFFER)
8348 prev_pos = IT_CHARPOS (*it);
8349 set_iterator_to_next (it, 1);
8350 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8351 SET_TEXT_POS (this_line_min_pos,
8352 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8353 /* On graphical terminals, newlines may
8354 "overflow" into the fringe if
8355 overflow-newline-into-fringe is non-nil.
8356 On text-only terminals, newlines may
8357 overflow into the last glyph on the
8358 display line.*/
8359 if (!FRAME_WINDOW_P (it->f)
8360 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8361 {
8362 if (!get_next_display_element (it))
8363 {
8364 result = MOVE_POS_MATCH_OR_ZV;
8365 break;
8366 }
8367 if (BUFFER_POS_REACHED_P ())
8368 {
8369 if (ITERATOR_AT_END_OF_LINE_P (it))
8370 result = MOVE_POS_MATCH_OR_ZV;
8371 else
8372 result = MOVE_LINE_CONTINUED;
8373 break;
8374 }
8375 if (ITERATOR_AT_END_OF_LINE_P (it))
8376 {
8377 result = MOVE_NEWLINE_OR_CR;
8378 break;
8379 }
8380 }
8381 }
8382 }
8383 else
8384 IT_RESET_X_ASCENT_DESCENT (it);
8385
8386 if (wrap_it.sp >= 0)
8387 {
8388 RESTORE_IT (it, &wrap_it, wrap_data);
8389 atpos_it.sp = -1;
8390 atx_it.sp = -1;
8391 }
8392
8393 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8394 IT_CHARPOS (*it)));
8395 result = MOVE_LINE_CONTINUED;
8396 break;
8397 }
8398
8399 if (BUFFER_POS_REACHED_P ())
8400 {
8401 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8402 goto buffer_pos_reached;
8403 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8404 {
8405 SAVE_IT (atpos_it, *it, atpos_data);
8406 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8407 }
8408 }
8409
8410 if (new_x > it->first_visible_x)
8411 {
8412 /* Glyph is visible. Increment number of glyphs that
8413 would be displayed. */
8414 ++it->hpos;
8415 }
8416 }
8417
8418 if (result != MOVE_UNDEFINED)
8419 break;
8420 }
8421 else if (BUFFER_POS_REACHED_P ())
8422 {
8423 buffer_pos_reached:
8424 IT_RESET_X_ASCENT_DESCENT (it);
8425 result = MOVE_POS_MATCH_OR_ZV;
8426 break;
8427 }
8428 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8429 {
8430 /* Stop when TO_X specified and reached. This check is
8431 necessary here because of lines consisting of a line end,
8432 only. The line end will not produce any glyphs and we
8433 would never get MOVE_X_REACHED. */
8434 xassert (it->nglyphs == 0);
8435 result = MOVE_X_REACHED;
8436 break;
8437 }
8438
8439 /* Is this a line end? If yes, we're done. */
8440 if (ITERATOR_AT_END_OF_LINE_P (it))
8441 {
8442 /* If we are past TO_CHARPOS, but never saw any character
8443 positions smaller than TO_CHARPOS, return
8444 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8445 did. */
8446 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8447 {
8448 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8449 {
8450 if (IT_CHARPOS (ppos_it) < ZV)
8451 {
8452 RESTORE_IT (it, &ppos_it, ppos_data);
8453 result = MOVE_POS_MATCH_OR_ZV;
8454 }
8455 else
8456 goto buffer_pos_reached;
8457 }
8458 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8459 && IT_CHARPOS (*it) > to_charpos)
8460 goto buffer_pos_reached;
8461 else
8462 result = MOVE_NEWLINE_OR_CR;
8463 }
8464 else
8465 result = MOVE_NEWLINE_OR_CR;
8466 break;
8467 }
8468
8469 prev_method = it->method;
8470 if (it->method == GET_FROM_BUFFER)
8471 prev_pos = IT_CHARPOS (*it);
8472 /* The current display element has been consumed. Advance
8473 to the next. */
8474 set_iterator_to_next (it, 1);
8475 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8476 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8477 if (IT_CHARPOS (*it) < to_charpos)
8478 saw_smaller_pos = 1;
8479 if (it->bidi_p
8480 && (op & MOVE_TO_POS)
8481 && IT_CHARPOS (*it) >= to_charpos
8482 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8483 SAVE_IT (ppos_it, *it, ppos_data);
8484
8485 /* Stop if lines are truncated and IT's current x-position is
8486 past the right edge of the window now. */
8487 if (it->line_wrap == TRUNCATE
8488 && it->current_x >= it->last_visible_x)
8489 {
8490 if (!FRAME_WINDOW_P (it->f)
8491 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8492 {
8493 int at_eob_p = 0;
8494
8495 if ((at_eob_p = !get_next_display_element (it))
8496 || BUFFER_POS_REACHED_P ()
8497 /* If we are past TO_CHARPOS, but never saw any
8498 character positions smaller than TO_CHARPOS,
8499 return MOVE_POS_MATCH_OR_ZV, like the
8500 unidirectional display did. */
8501 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8502 && !saw_smaller_pos
8503 && IT_CHARPOS (*it) > to_charpos))
8504 {
8505 if (it->bidi_p
8506 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8507 RESTORE_IT (it, &ppos_it, ppos_data);
8508 result = MOVE_POS_MATCH_OR_ZV;
8509 break;
8510 }
8511 if (ITERATOR_AT_END_OF_LINE_P (it))
8512 {
8513 result = MOVE_NEWLINE_OR_CR;
8514 break;
8515 }
8516 }
8517 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8518 && !saw_smaller_pos
8519 && IT_CHARPOS (*it) > to_charpos)
8520 {
8521 if (IT_CHARPOS (ppos_it) < ZV)
8522 RESTORE_IT (it, &ppos_it, ppos_data);
8523 result = MOVE_POS_MATCH_OR_ZV;
8524 break;
8525 }
8526 result = MOVE_LINE_TRUNCATED;
8527 break;
8528 }
8529 #undef IT_RESET_X_ASCENT_DESCENT
8530 }
8531
8532 #undef BUFFER_POS_REACHED_P
8533
8534 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8535 restore the saved iterator. */
8536 if (atpos_it.sp >= 0)
8537 RESTORE_IT (it, &atpos_it, atpos_data);
8538 else if (atx_it.sp >= 0)
8539 RESTORE_IT (it, &atx_it, atx_data);
8540
8541 done:
8542
8543 if (atpos_data)
8544 bidi_unshelve_cache (atpos_data, 1);
8545 if (atx_data)
8546 bidi_unshelve_cache (atx_data, 1);
8547 if (wrap_data)
8548 bidi_unshelve_cache (wrap_data, 1);
8549 if (ppos_data)
8550 bidi_unshelve_cache (ppos_data, 1);
8551
8552 /* Restore the iterator settings altered at the beginning of this
8553 function. */
8554 it->glyph_row = saved_glyph_row;
8555 return result;
8556 }
8557
8558 /* For external use. */
8559 void
8560 move_it_in_display_line (struct it *it,
8561 ptrdiff_t to_charpos, int to_x,
8562 enum move_operation_enum op)
8563 {
8564 if (it->line_wrap == WORD_WRAP
8565 && (op & MOVE_TO_X))
8566 {
8567 struct it save_it;
8568 void *save_data = NULL;
8569 int skip;
8570
8571 SAVE_IT (save_it, *it, save_data);
8572 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8573 /* When word-wrap is on, TO_X may lie past the end
8574 of a wrapped line. Then it->current is the
8575 character on the next line, so backtrack to the
8576 space before the wrap point. */
8577 if (skip == MOVE_LINE_CONTINUED)
8578 {
8579 int prev_x = max (it->current_x - 1, 0);
8580 RESTORE_IT (it, &save_it, save_data);
8581 move_it_in_display_line_to
8582 (it, -1, prev_x, MOVE_TO_X);
8583 }
8584 else
8585 bidi_unshelve_cache (save_data, 1);
8586 }
8587 else
8588 move_it_in_display_line_to (it, to_charpos, to_x, op);
8589 }
8590
8591
8592 /* Move IT forward until it satisfies one or more of the criteria in
8593 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8594
8595 OP is a bit-mask that specifies where to stop, and in particular,
8596 which of those four position arguments makes a difference. See the
8597 description of enum move_operation_enum.
8598
8599 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8600 screen line, this function will set IT to the next position that is
8601 displayed to the right of TO_CHARPOS on the screen. */
8602
8603 void
8604 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8605 {
8606 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8607 int line_height, line_start_x = 0, reached = 0;
8608 void *backup_data = NULL;
8609
8610 for (;;)
8611 {
8612 if (op & MOVE_TO_VPOS)
8613 {
8614 /* If no TO_CHARPOS and no TO_X specified, stop at the
8615 start of the line TO_VPOS. */
8616 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8617 {
8618 if (it->vpos == to_vpos)
8619 {
8620 reached = 1;
8621 break;
8622 }
8623 else
8624 skip = move_it_in_display_line_to (it, -1, -1, 0);
8625 }
8626 else
8627 {
8628 /* TO_VPOS >= 0 means stop at TO_X in the line at
8629 TO_VPOS, or at TO_POS, whichever comes first. */
8630 if (it->vpos == to_vpos)
8631 {
8632 reached = 2;
8633 break;
8634 }
8635
8636 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8637
8638 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8639 {
8640 reached = 3;
8641 break;
8642 }
8643 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8644 {
8645 /* We have reached TO_X but not in the line we want. */
8646 skip = move_it_in_display_line_to (it, to_charpos,
8647 -1, MOVE_TO_POS);
8648 if (skip == MOVE_POS_MATCH_OR_ZV)
8649 {
8650 reached = 4;
8651 break;
8652 }
8653 }
8654 }
8655 }
8656 else if (op & MOVE_TO_Y)
8657 {
8658 struct it it_backup;
8659
8660 if (it->line_wrap == WORD_WRAP)
8661 SAVE_IT (it_backup, *it, backup_data);
8662
8663 /* TO_Y specified means stop at TO_X in the line containing
8664 TO_Y---or at TO_CHARPOS if this is reached first. The
8665 problem is that we can't really tell whether the line
8666 contains TO_Y before we have completely scanned it, and
8667 this may skip past TO_X. What we do is to first scan to
8668 TO_X.
8669
8670 If TO_X is not specified, use a TO_X of zero. The reason
8671 is to make the outcome of this function more predictable.
8672 If we didn't use TO_X == 0, we would stop at the end of
8673 the line which is probably not what a caller would expect
8674 to happen. */
8675 skip = move_it_in_display_line_to
8676 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8677 (MOVE_TO_X | (op & MOVE_TO_POS)));
8678
8679 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8680 if (skip == MOVE_POS_MATCH_OR_ZV)
8681 reached = 5;
8682 else if (skip == MOVE_X_REACHED)
8683 {
8684 /* If TO_X was reached, we want to know whether TO_Y is
8685 in the line. We know this is the case if the already
8686 scanned glyphs make the line tall enough. Otherwise,
8687 we must check by scanning the rest of the line. */
8688 line_height = it->max_ascent + it->max_descent;
8689 if (to_y >= it->current_y
8690 && to_y < it->current_y + line_height)
8691 {
8692 reached = 6;
8693 break;
8694 }
8695 SAVE_IT (it_backup, *it, backup_data);
8696 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8697 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8698 op & MOVE_TO_POS);
8699 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8700 line_height = it->max_ascent + it->max_descent;
8701 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8702
8703 if (to_y >= it->current_y
8704 && to_y < it->current_y + line_height)
8705 {
8706 /* If TO_Y is in this line and TO_X was reached
8707 above, we scanned too far. We have to restore
8708 IT's settings to the ones before skipping. */
8709 RESTORE_IT (it, &it_backup, backup_data);
8710 reached = 6;
8711 }
8712 else
8713 {
8714 skip = skip2;
8715 if (skip == MOVE_POS_MATCH_OR_ZV)
8716 reached = 7;
8717 }
8718 }
8719 else
8720 {
8721 /* Check whether TO_Y is in this line. */
8722 line_height = it->max_ascent + it->max_descent;
8723 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8724
8725 if (to_y >= it->current_y
8726 && to_y < it->current_y + line_height)
8727 {
8728 /* When word-wrap is on, TO_X may lie past the end
8729 of a wrapped line. Then it->current is the
8730 character on the next line, so backtrack to the
8731 space before the wrap point. */
8732 if (skip == MOVE_LINE_CONTINUED
8733 && it->line_wrap == WORD_WRAP)
8734 {
8735 int prev_x = max (it->current_x - 1, 0);
8736 RESTORE_IT (it, &it_backup, backup_data);
8737 skip = move_it_in_display_line_to
8738 (it, -1, prev_x, MOVE_TO_X);
8739 }
8740 reached = 6;
8741 }
8742 }
8743
8744 if (reached)
8745 break;
8746 }
8747 else if (BUFFERP (it->object)
8748 && (it->method == GET_FROM_BUFFER
8749 || it->method == GET_FROM_STRETCH)
8750 && IT_CHARPOS (*it) >= to_charpos
8751 /* Under bidi iteration, a call to set_iterator_to_next
8752 can scan far beyond to_charpos if the initial
8753 portion of the next line needs to be reordered. In
8754 that case, give move_it_in_display_line_to another
8755 chance below. */
8756 && !(it->bidi_p
8757 && it->bidi_it.scan_dir == -1))
8758 skip = MOVE_POS_MATCH_OR_ZV;
8759 else
8760 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8761
8762 switch (skip)
8763 {
8764 case MOVE_POS_MATCH_OR_ZV:
8765 reached = 8;
8766 goto out;
8767
8768 case MOVE_NEWLINE_OR_CR:
8769 set_iterator_to_next (it, 1);
8770 it->continuation_lines_width = 0;
8771 break;
8772
8773 case MOVE_LINE_TRUNCATED:
8774 it->continuation_lines_width = 0;
8775 reseat_at_next_visible_line_start (it, 0);
8776 if ((op & MOVE_TO_POS) != 0
8777 && IT_CHARPOS (*it) > to_charpos)
8778 {
8779 reached = 9;
8780 goto out;
8781 }
8782 break;
8783
8784 case MOVE_LINE_CONTINUED:
8785 /* For continued lines ending in a tab, some of the glyphs
8786 associated with the tab are displayed on the current
8787 line. Since it->current_x does not include these glyphs,
8788 we use it->last_visible_x instead. */
8789 if (it->c == '\t')
8790 {
8791 it->continuation_lines_width += it->last_visible_x;
8792 /* When moving by vpos, ensure that the iterator really
8793 advances to the next line (bug#847, bug#969). Fixme:
8794 do we need to do this in other circumstances? */
8795 if (it->current_x != it->last_visible_x
8796 && (op & MOVE_TO_VPOS)
8797 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8798 {
8799 line_start_x = it->current_x + it->pixel_width
8800 - it->last_visible_x;
8801 set_iterator_to_next (it, 0);
8802 }
8803 }
8804 else
8805 it->continuation_lines_width += it->current_x;
8806 break;
8807
8808 default:
8809 abort ();
8810 }
8811
8812 /* Reset/increment for the next run. */
8813 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8814 it->current_x = line_start_x;
8815 line_start_x = 0;
8816 it->hpos = 0;
8817 it->current_y += it->max_ascent + it->max_descent;
8818 ++it->vpos;
8819 last_height = it->max_ascent + it->max_descent;
8820 last_max_ascent = it->max_ascent;
8821 it->max_ascent = it->max_descent = 0;
8822 }
8823
8824 out:
8825
8826 /* On text terminals, we may stop at the end of a line in the middle
8827 of a multi-character glyph. If the glyph itself is continued,
8828 i.e. it is actually displayed on the next line, don't treat this
8829 stopping point as valid; move to the next line instead (unless
8830 that brings us offscreen). */
8831 if (!FRAME_WINDOW_P (it->f)
8832 && op & MOVE_TO_POS
8833 && IT_CHARPOS (*it) == to_charpos
8834 && it->what == IT_CHARACTER
8835 && it->nglyphs > 1
8836 && it->line_wrap == WINDOW_WRAP
8837 && it->current_x == it->last_visible_x - 1
8838 && it->c != '\n'
8839 && it->c != '\t'
8840 && it->vpos < XFASTINT (it->w->window_end_vpos))
8841 {
8842 it->continuation_lines_width += it->current_x;
8843 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8844 it->current_y += it->max_ascent + it->max_descent;
8845 ++it->vpos;
8846 last_height = it->max_ascent + it->max_descent;
8847 last_max_ascent = it->max_ascent;
8848 }
8849
8850 if (backup_data)
8851 bidi_unshelve_cache (backup_data, 1);
8852
8853 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8854 }
8855
8856
8857 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8858
8859 If DY > 0, move IT backward at least that many pixels. DY = 0
8860 means move IT backward to the preceding line start or BEGV. This
8861 function may move over more than DY pixels if IT->current_y - DY
8862 ends up in the middle of a line; in this case IT->current_y will be
8863 set to the top of the line moved to. */
8864
8865 void
8866 move_it_vertically_backward (struct it *it, int dy)
8867 {
8868 int nlines, h;
8869 struct it it2, it3;
8870 void *it2data = NULL, *it3data = NULL;
8871 ptrdiff_t start_pos;
8872
8873 move_further_back:
8874 xassert (dy >= 0);
8875
8876 start_pos = IT_CHARPOS (*it);
8877
8878 /* Estimate how many newlines we must move back. */
8879 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8880
8881 /* Set the iterator's position that many lines back. */
8882 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8883 back_to_previous_visible_line_start (it);
8884
8885 /* Reseat the iterator here. When moving backward, we don't want
8886 reseat to skip forward over invisible text, set up the iterator
8887 to deliver from overlay strings at the new position etc. So,
8888 use reseat_1 here. */
8889 reseat_1 (it, it->current.pos, 1);
8890
8891 /* We are now surely at a line start. */
8892 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8893 reordering is in effect. */
8894 it->continuation_lines_width = 0;
8895
8896 /* Move forward and see what y-distance we moved. First move to the
8897 start of the next line so that we get its height. We need this
8898 height to be able to tell whether we reached the specified
8899 y-distance. */
8900 SAVE_IT (it2, *it, it2data);
8901 it2.max_ascent = it2.max_descent = 0;
8902 do
8903 {
8904 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8905 MOVE_TO_POS | MOVE_TO_VPOS);
8906 }
8907 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8908 /* If we are in a display string which starts at START_POS,
8909 and that display string includes a newline, and we are
8910 right after that newline (i.e. at the beginning of a
8911 display line), exit the loop, because otherwise we will
8912 infloop, since move_it_to will see that it is already at
8913 START_POS and will not move. */
8914 || (it2.method == GET_FROM_STRING
8915 && IT_CHARPOS (it2) == start_pos
8916 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8917 xassert (IT_CHARPOS (*it) >= BEGV);
8918 SAVE_IT (it3, it2, it3data);
8919
8920 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8921 xassert (IT_CHARPOS (*it) >= BEGV);
8922 /* H is the actual vertical distance from the position in *IT
8923 and the starting position. */
8924 h = it2.current_y - it->current_y;
8925 /* NLINES is the distance in number of lines. */
8926 nlines = it2.vpos - it->vpos;
8927
8928 /* Correct IT's y and vpos position
8929 so that they are relative to the starting point. */
8930 it->vpos -= nlines;
8931 it->current_y -= h;
8932
8933 if (dy == 0)
8934 {
8935 /* DY == 0 means move to the start of the screen line. The
8936 value of nlines is > 0 if continuation lines were involved,
8937 or if the original IT position was at start of a line. */
8938 RESTORE_IT (it, it, it2data);
8939 if (nlines > 0)
8940 move_it_by_lines (it, nlines);
8941 /* The above code moves us to some position NLINES down,
8942 usually to its first glyph (leftmost in an L2R line), but
8943 that's not necessarily the start of the line, under bidi
8944 reordering. We want to get to the character position
8945 that is immediately after the newline of the previous
8946 line. */
8947 if (it->bidi_p
8948 && !it->continuation_lines_width
8949 && !STRINGP (it->string)
8950 && IT_CHARPOS (*it) > BEGV
8951 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8952 {
8953 ptrdiff_t nl_pos =
8954 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8955
8956 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8957 }
8958 bidi_unshelve_cache (it3data, 1);
8959 }
8960 else
8961 {
8962 /* The y-position we try to reach, relative to *IT.
8963 Note that H has been subtracted in front of the if-statement. */
8964 int target_y = it->current_y + h - dy;
8965 int y0 = it3.current_y;
8966 int y1;
8967 int line_height;
8968
8969 RESTORE_IT (&it3, &it3, it3data);
8970 y1 = line_bottom_y (&it3);
8971 line_height = y1 - y0;
8972 RESTORE_IT (it, it, it2data);
8973 /* If we did not reach target_y, try to move further backward if
8974 we can. If we moved too far backward, try to move forward. */
8975 if (target_y < it->current_y
8976 /* This is heuristic. In a window that's 3 lines high, with
8977 a line height of 13 pixels each, recentering with point
8978 on the bottom line will try to move -39/2 = 19 pixels
8979 backward. Try to avoid moving into the first line. */
8980 && (it->current_y - target_y
8981 > min (window_box_height (it->w), line_height * 2 / 3))
8982 && IT_CHARPOS (*it) > BEGV)
8983 {
8984 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8985 target_y - it->current_y));
8986 dy = it->current_y - target_y;
8987 goto move_further_back;
8988 }
8989 else if (target_y >= it->current_y + line_height
8990 && IT_CHARPOS (*it) < ZV)
8991 {
8992 /* Should move forward by at least one line, maybe more.
8993
8994 Note: Calling move_it_by_lines can be expensive on
8995 terminal frames, where compute_motion is used (via
8996 vmotion) to do the job, when there are very long lines
8997 and truncate-lines is nil. That's the reason for
8998 treating terminal frames specially here. */
8999
9000 if (!FRAME_WINDOW_P (it->f))
9001 move_it_vertically (it, target_y - (it->current_y + line_height));
9002 else
9003 {
9004 do
9005 {
9006 move_it_by_lines (it, 1);
9007 }
9008 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9009 }
9010 }
9011 }
9012 }
9013
9014
9015 /* Move IT by a specified amount of pixel lines DY. DY negative means
9016 move backwards. DY = 0 means move to start of screen line. At the
9017 end, IT will be on the start of a screen line. */
9018
9019 void
9020 move_it_vertically (struct it *it, int dy)
9021 {
9022 if (dy <= 0)
9023 move_it_vertically_backward (it, -dy);
9024 else
9025 {
9026 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9027 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9028 MOVE_TO_POS | MOVE_TO_Y);
9029 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9030
9031 /* If buffer ends in ZV without a newline, move to the start of
9032 the line to satisfy the post-condition. */
9033 if (IT_CHARPOS (*it) == ZV
9034 && ZV > BEGV
9035 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9036 move_it_by_lines (it, 0);
9037 }
9038 }
9039
9040
9041 /* Move iterator IT past the end of the text line it is in. */
9042
9043 void
9044 move_it_past_eol (struct it *it)
9045 {
9046 enum move_it_result rc;
9047
9048 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9049 if (rc == MOVE_NEWLINE_OR_CR)
9050 set_iterator_to_next (it, 0);
9051 }
9052
9053
9054 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9055 negative means move up. DVPOS == 0 means move to the start of the
9056 screen line.
9057
9058 Optimization idea: If we would know that IT->f doesn't use
9059 a face with proportional font, we could be faster for
9060 truncate-lines nil. */
9061
9062 void
9063 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9064 {
9065
9066 /* The commented-out optimization uses vmotion on terminals. This
9067 gives bad results, because elements like it->what, on which
9068 callers such as pos_visible_p rely, aren't updated. */
9069 /* struct position pos;
9070 if (!FRAME_WINDOW_P (it->f))
9071 {
9072 struct text_pos textpos;
9073
9074 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9075 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9076 reseat (it, textpos, 1);
9077 it->vpos += pos.vpos;
9078 it->current_y += pos.vpos;
9079 }
9080 else */
9081
9082 if (dvpos == 0)
9083 {
9084 /* DVPOS == 0 means move to the start of the screen line. */
9085 move_it_vertically_backward (it, 0);
9086 /* Let next call to line_bottom_y calculate real line height */
9087 last_height = 0;
9088 }
9089 else if (dvpos > 0)
9090 {
9091 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9092 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9093 {
9094 /* Only move to the next buffer position if we ended up in a
9095 string from display property, not in an overlay string
9096 (before-string or after-string). That is because the
9097 latter don't conceal the underlying buffer position, so
9098 we can ask to move the iterator to the exact position we
9099 are interested in. Note that, even if we are already at
9100 IT_CHARPOS (*it), the call below is not a no-op, as it
9101 will detect that we are at the end of the string, pop the
9102 iterator, and compute it->current_x and it->hpos
9103 correctly. */
9104 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9105 -1, -1, -1, MOVE_TO_POS);
9106 }
9107 }
9108 else
9109 {
9110 struct it it2;
9111 void *it2data = NULL;
9112 ptrdiff_t start_charpos, i;
9113
9114 /* Start at the beginning of the screen line containing IT's
9115 position. This may actually move vertically backwards,
9116 in case of overlays, so adjust dvpos accordingly. */
9117 dvpos += it->vpos;
9118 move_it_vertically_backward (it, 0);
9119 dvpos -= it->vpos;
9120
9121 /* Go back -DVPOS visible lines and reseat the iterator there. */
9122 start_charpos = IT_CHARPOS (*it);
9123 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9124 back_to_previous_visible_line_start (it);
9125 reseat (it, it->current.pos, 1);
9126
9127 /* Move further back if we end up in a string or an image. */
9128 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9129 {
9130 /* First try to move to start of display line. */
9131 dvpos += it->vpos;
9132 move_it_vertically_backward (it, 0);
9133 dvpos -= it->vpos;
9134 if (IT_POS_VALID_AFTER_MOVE_P (it))
9135 break;
9136 /* If start of line is still in string or image,
9137 move further back. */
9138 back_to_previous_visible_line_start (it);
9139 reseat (it, it->current.pos, 1);
9140 dvpos--;
9141 }
9142
9143 it->current_x = it->hpos = 0;
9144
9145 /* Above call may have moved too far if continuation lines
9146 are involved. Scan forward and see if it did. */
9147 SAVE_IT (it2, *it, it2data);
9148 it2.vpos = it2.current_y = 0;
9149 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9150 it->vpos -= it2.vpos;
9151 it->current_y -= it2.current_y;
9152 it->current_x = it->hpos = 0;
9153
9154 /* If we moved too far back, move IT some lines forward. */
9155 if (it2.vpos > -dvpos)
9156 {
9157 int delta = it2.vpos + dvpos;
9158
9159 RESTORE_IT (&it2, &it2, it2data);
9160 SAVE_IT (it2, *it, it2data);
9161 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9162 /* Move back again if we got too far ahead. */
9163 if (IT_CHARPOS (*it) >= start_charpos)
9164 RESTORE_IT (it, &it2, it2data);
9165 else
9166 bidi_unshelve_cache (it2data, 1);
9167 }
9168 else
9169 RESTORE_IT (it, it, it2data);
9170 }
9171 }
9172
9173 /* Return 1 if IT points into the middle of a display vector. */
9174
9175 int
9176 in_display_vector_p (struct it *it)
9177 {
9178 return (it->method == GET_FROM_DISPLAY_VECTOR
9179 && it->current.dpvec_index > 0
9180 && it->dpvec + it->current.dpvec_index != it->dpend);
9181 }
9182
9183 \f
9184 /***********************************************************************
9185 Messages
9186 ***********************************************************************/
9187
9188
9189 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9190 to *Messages*. */
9191
9192 void
9193 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9194 {
9195 Lisp_Object args[3];
9196 Lisp_Object msg, fmt;
9197 char *buffer;
9198 ptrdiff_t len;
9199 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9200 USE_SAFE_ALLOCA;
9201
9202 /* Do nothing if called asynchronously. Inserting text into
9203 a buffer may call after-change-functions and alike and
9204 that would means running Lisp asynchronously. */
9205 if (handling_signal)
9206 return;
9207
9208 fmt = msg = Qnil;
9209 GCPRO4 (fmt, msg, arg1, arg2);
9210
9211 args[0] = fmt = build_string (format);
9212 args[1] = arg1;
9213 args[2] = arg2;
9214 msg = Fformat (3, args);
9215
9216 len = SBYTES (msg) + 1;
9217 SAFE_ALLOCA (buffer, char *, len);
9218 memcpy (buffer, SDATA (msg), len);
9219
9220 message_dolog (buffer, len - 1, 1, 0);
9221 SAFE_FREE ();
9222
9223 UNGCPRO;
9224 }
9225
9226
9227 /* Output a newline in the *Messages* buffer if "needs" one. */
9228
9229 void
9230 message_log_maybe_newline (void)
9231 {
9232 if (message_log_need_newline)
9233 message_dolog ("", 0, 1, 0);
9234 }
9235
9236
9237 /* Add a string M of length NBYTES to the message log, optionally
9238 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9239 nonzero, means interpret the contents of M as multibyte. This
9240 function calls low-level routines in order to bypass text property
9241 hooks, etc. which might not be safe to run.
9242
9243 This may GC (insert may run before/after change hooks),
9244 so the buffer M must NOT point to a Lisp string. */
9245
9246 void
9247 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9248 {
9249 const unsigned char *msg = (const unsigned char *) m;
9250
9251 if (!NILP (Vmemory_full))
9252 return;
9253
9254 if (!NILP (Vmessage_log_max))
9255 {
9256 struct buffer *oldbuf;
9257 Lisp_Object oldpoint, oldbegv, oldzv;
9258 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9259 ptrdiff_t point_at_end = 0;
9260 ptrdiff_t zv_at_end = 0;
9261 Lisp_Object old_deactivate_mark, tem;
9262 struct gcpro gcpro1;
9263
9264 old_deactivate_mark = Vdeactivate_mark;
9265 oldbuf = current_buffer;
9266 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9267 BVAR (current_buffer, undo_list) = Qt;
9268
9269 oldpoint = message_dolog_marker1;
9270 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9271 oldbegv = message_dolog_marker2;
9272 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9273 oldzv = message_dolog_marker3;
9274 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9275 GCPRO1 (old_deactivate_mark);
9276
9277 if (PT == Z)
9278 point_at_end = 1;
9279 if (ZV == Z)
9280 zv_at_end = 1;
9281
9282 BEGV = BEG;
9283 BEGV_BYTE = BEG_BYTE;
9284 ZV = Z;
9285 ZV_BYTE = Z_BYTE;
9286 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9287
9288 /* Insert the string--maybe converting multibyte to single byte
9289 or vice versa, so that all the text fits the buffer. */
9290 if (multibyte
9291 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9292 {
9293 ptrdiff_t i;
9294 int c, char_bytes;
9295 char work[1];
9296
9297 /* Convert a multibyte string to single-byte
9298 for the *Message* buffer. */
9299 for (i = 0; i < nbytes; i += char_bytes)
9300 {
9301 c = string_char_and_length (msg + i, &char_bytes);
9302 work[0] = (ASCII_CHAR_P (c)
9303 ? c
9304 : multibyte_char_to_unibyte (c));
9305 insert_1_both (work, 1, 1, 1, 0, 0);
9306 }
9307 }
9308 else if (! multibyte
9309 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9310 {
9311 ptrdiff_t i;
9312 int c, char_bytes;
9313 unsigned char str[MAX_MULTIBYTE_LENGTH];
9314 /* Convert a single-byte string to multibyte
9315 for the *Message* buffer. */
9316 for (i = 0; i < nbytes; i++)
9317 {
9318 c = msg[i];
9319 MAKE_CHAR_MULTIBYTE (c);
9320 char_bytes = CHAR_STRING (c, str);
9321 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9322 }
9323 }
9324 else if (nbytes)
9325 insert_1 (m, nbytes, 1, 0, 0);
9326
9327 if (nlflag)
9328 {
9329 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9330 printmax_t dups;
9331 insert_1 ("\n", 1, 1, 0, 0);
9332
9333 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9334 this_bol = PT;
9335 this_bol_byte = PT_BYTE;
9336
9337 /* See if this line duplicates the previous one.
9338 If so, combine duplicates. */
9339 if (this_bol > BEG)
9340 {
9341 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9342 prev_bol = PT;
9343 prev_bol_byte = PT_BYTE;
9344
9345 dups = message_log_check_duplicate (prev_bol_byte,
9346 this_bol_byte);
9347 if (dups)
9348 {
9349 del_range_both (prev_bol, prev_bol_byte,
9350 this_bol, this_bol_byte, 0);
9351 if (dups > 1)
9352 {
9353 char dupstr[sizeof " [ times]"
9354 + INT_STRLEN_BOUND (printmax_t)];
9355 int duplen;
9356
9357 /* If you change this format, don't forget to also
9358 change message_log_check_duplicate. */
9359 sprintf (dupstr, " [%"pMd" times]", dups);
9360 duplen = strlen (dupstr);
9361 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9362 insert_1 (dupstr, duplen, 1, 0, 1);
9363 }
9364 }
9365 }
9366
9367 /* If we have more than the desired maximum number of lines
9368 in the *Messages* buffer now, delete the oldest ones.
9369 This is safe because we don't have undo in this buffer. */
9370
9371 if (NATNUMP (Vmessage_log_max))
9372 {
9373 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9374 -XFASTINT (Vmessage_log_max) - 1, 0);
9375 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9376 }
9377 }
9378 BEGV = XMARKER (oldbegv)->charpos;
9379 BEGV_BYTE = marker_byte_position (oldbegv);
9380
9381 if (zv_at_end)
9382 {
9383 ZV = Z;
9384 ZV_BYTE = Z_BYTE;
9385 }
9386 else
9387 {
9388 ZV = XMARKER (oldzv)->charpos;
9389 ZV_BYTE = marker_byte_position (oldzv);
9390 }
9391
9392 if (point_at_end)
9393 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9394 else
9395 /* We can't do Fgoto_char (oldpoint) because it will run some
9396 Lisp code. */
9397 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9398 XMARKER (oldpoint)->bytepos);
9399
9400 UNGCPRO;
9401 unchain_marker (XMARKER (oldpoint));
9402 unchain_marker (XMARKER (oldbegv));
9403 unchain_marker (XMARKER (oldzv));
9404
9405 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9406 set_buffer_internal (oldbuf);
9407 if (NILP (tem))
9408 windows_or_buffers_changed = old_windows_or_buffers_changed;
9409 message_log_need_newline = !nlflag;
9410 Vdeactivate_mark = old_deactivate_mark;
9411 }
9412 }
9413
9414
9415 /* We are at the end of the buffer after just having inserted a newline.
9416 (Note: We depend on the fact we won't be crossing the gap.)
9417 Check to see if the most recent message looks a lot like the previous one.
9418 Return 0 if different, 1 if the new one should just replace it, or a
9419 value N > 1 if we should also append " [N times]". */
9420
9421 static intmax_t
9422 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9423 {
9424 ptrdiff_t i;
9425 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9426 int seen_dots = 0;
9427 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9428 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9429
9430 for (i = 0; i < len; i++)
9431 {
9432 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9433 seen_dots = 1;
9434 if (p1[i] != p2[i])
9435 return seen_dots;
9436 }
9437 p1 += len;
9438 if (*p1 == '\n')
9439 return 2;
9440 if (*p1++ == ' ' && *p1++ == '[')
9441 {
9442 char *pend;
9443 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9444 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9445 return n+1;
9446 }
9447 return 0;
9448 }
9449 \f
9450
9451 /* Display an echo area message M with a specified length of NBYTES
9452 bytes. The string may include null characters. If M is 0, clear
9453 out any existing message, and let the mini-buffer text show
9454 through.
9455
9456 This may GC, so the buffer M must NOT point to a Lisp string. */
9457
9458 void
9459 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9460 {
9461 /* First flush out any partial line written with print. */
9462 message_log_maybe_newline ();
9463 if (m)
9464 message_dolog (m, nbytes, 1, multibyte);
9465 message2_nolog (m, nbytes, multibyte);
9466 }
9467
9468
9469 /* The non-logging counterpart of message2. */
9470
9471 void
9472 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9473 {
9474 struct frame *sf = SELECTED_FRAME ();
9475 message_enable_multibyte = multibyte;
9476
9477 if (FRAME_INITIAL_P (sf))
9478 {
9479 if (noninteractive_need_newline)
9480 putc ('\n', stderr);
9481 noninteractive_need_newline = 0;
9482 if (m)
9483 fwrite (m, nbytes, 1, stderr);
9484 if (cursor_in_echo_area == 0)
9485 fprintf (stderr, "\n");
9486 fflush (stderr);
9487 }
9488 /* A null message buffer means that the frame hasn't really been
9489 initialized yet. Error messages get reported properly by
9490 cmd_error, so this must be just an informative message; toss it. */
9491 else if (INTERACTIVE
9492 && sf->glyphs_initialized_p
9493 && FRAME_MESSAGE_BUF (sf))
9494 {
9495 Lisp_Object mini_window;
9496 struct frame *f;
9497
9498 /* Get the frame containing the mini-buffer
9499 that the selected frame is using. */
9500 mini_window = FRAME_MINIBUF_WINDOW (sf);
9501 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9502
9503 FRAME_SAMPLE_VISIBILITY (f);
9504 if (FRAME_VISIBLE_P (sf)
9505 && ! FRAME_VISIBLE_P (f))
9506 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9507
9508 if (m)
9509 {
9510 set_message (m, Qnil, nbytes, multibyte);
9511 if (minibuffer_auto_raise)
9512 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9513 }
9514 else
9515 clear_message (1, 1);
9516
9517 do_pending_window_change (0);
9518 echo_area_display (1);
9519 do_pending_window_change (0);
9520 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9521 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9522 }
9523 }
9524
9525
9526 /* Display an echo area message M with a specified length of NBYTES
9527 bytes. The string may include null characters. If M is not a
9528 string, clear out any existing message, and let the mini-buffer
9529 text show through.
9530
9531 This function cancels echoing. */
9532
9533 void
9534 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9535 {
9536 struct gcpro gcpro1;
9537
9538 GCPRO1 (m);
9539 clear_message (1,1);
9540 cancel_echoing ();
9541
9542 /* First flush out any partial line written with print. */
9543 message_log_maybe_newline ();
9544 if (STRINGP (m))
9545 {
9546 char *buffer;
9547 USE_SAFE_ALLOCA;
9548
9549 SAFE_ALLOCA (buffer, char *, nbytes);
9550 memcpy (buffer, SDATA (m), nbytes);
9551 message_dolog (buffer, nbytes, 1, multibyte);
9552 SAFE_FREE ();
9553 }
9554 message3_nolog (m, nbytes, multibyte);
9555
9556 UNGCPRO;
9557 }
9558
9559
9560 /* The non-logging version of message3.
9561 This does not cancel echoing, because it is used for echoing.
9562 Perhaps we need to make a separate function for echoing
9563 and make this cancel echoing. */
9564
9565 void
9566 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9567 {
9568 struct frame *sf = SELECTED_FRAME ();
9569 message_enable_multibyte = multibyte;
9570
9571 if (FRAME_INITIAL_P (sf))
9572 {
9573 if (noninteractive_need_newline)
9574 putc ('\n', stderr);
9575 noninteractive_need_newline = 0;
9576 if (STRINGP (m))
9577 fwrite (SDATA (m), nbytes, 1, stderr);
9578 if (cursor_in_echo_area == 0)
9579 fprintf (stderr, "\n");
9580 fflush (stderr);
9581 }
9582 /* A null message buffer means that the frame hasn't really been
9583 initialized yet. Error messages get reported properly by
9584 cmd_error, so this must be just an informative message; toss it. */
9585 else if (INTERACTIVE
9586 && sf->glyphs_initialized_p
9587 && FRAME_MESSAGE_BUF (sf))
9588 {
9589 Lisp_Object mini_window;
9590 Lisp_Object frame;
9591 struct frame *f;
9592
9593 /* Get the frame containing the mini-buffer
9594 that the selected frame is using. */
9595 mini_window = FRAME_MINIBUF_WINDOW (sf);
9596 frame = XWINDOW (mini_window)->frame;
9597 f = XFRAME (frame);
9598
9599 FRAME_SAMPLE_VISIBILITY (f);
9600 if (FRAME_VISIBLE_P (sf)
9601 && !FRAME_VISIBLE_P (f))
9602 Fmake_frame_visible (frame);
9603
9604 if (STRINGP (m) && SCHARS (m) > 0)
9605 {
9606 set_message (NULL, m, nbytes, multibyte);
9607 if (minibuffer_auto_raise)
9608 Fraise_frame (frame);
9609 /* Assume we are not echoing.
9610 (If we are, echo_now will override this.) */
9611 echo_message_buffer = Qnil;
9612 }
9613 else
9614 clear_message (1, 1);
9615
9616 do_pending_window_change (0);
9617 echo_area_display (1);
9618 do_pending_window_change (0);
9619 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9620 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9621 }
9622 }
9623
9624
9625 /* Display a null-terminated echo area message M. If M is 0, clear
9626 out any existing message, and let the mini-buffer text show through.
9627
9628 The buffer M must continue to exist until after the echo area gets
9629 cleared or some other message gets displayed there. Do not pass
9630 text that is stored in a Lisp string. Do not pass text in a buffer
9631 that was alloca'd. */
9632
9633 void
9634 message1 (const char *m)
9635 {
9636 message2 (m, (m ? strlen (m) : 0), 0);
9637 }
9638
9639
9640 /* The non-logging counterpart of message1. */
9641
9642 void
9643 message1_nolog (const char *m)
9644 {
9645 message2_nolog (m, (m ? strlen (m) : 0), 0);
9646 }
9647
9648 /* Display a message M which contains a single %s
9649 which gets replaced with STRING. */
9650
9651 void
9652 message_with_string (const char *m, Lisp_Object string, int log)
9653 {
9654 CHECK_STRING (string);
9655
9656 if (noninteractive)
9657 {
9658 if (m)
9659 {
9660 if (noninteractive_need_newline)
9661 putc ('\n', stderr);
9662 noninteractive_need_newline = 0;
9663 fprintf (stderr, m, SDATA (string));
9664 if (!cursor_in_echo_area)
9665 fprintf (stderr, "\n");
9666 fflush (stderr);
9667 }
9668 }
9669 else if (INTERACTIVE)
9670 {
9671 /* The frame whose minibuffer we're going to display the message on.
9672 It may be larger than the selected frame, so we need
9673 to use its buffer, not the selected frame's buffer. */
9674 Lisp_Object mini_window;
9675 struct frame *f, *sf = SELECTED_FRAME ();
9676
9677 /* Get the frame containing the minibuffer
9678 that the selected frame is using. */
9679 mini_window = FRAME_MINIBUF_WINDOW (sf);
9680 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9681
9682 /* A null message buffer means that the frame hasn't really been
9683 initialized yet. Error messages get reported properly by
9684 cmd_error, so this must be just an informative message; toss it. */
9685 if (FRAME_MESSAGE_BUF (f))
9686 {
9687 Lisp_Object args[2], msg;
9688 struct gcpro gcpro1, gcpro2;
9689
9690 args[0] = build_string (m);
9691 args[1] = msg = string;
9692 GCPRO2 (args[0], msg);
9693 gcpro1.nvars = 2;
9694
9695 msg = Fformat (2, args);
9696
9697 if (log)
9698 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9699 else
9700 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9701
9702 UNGCPRO;
9703
9704 /* Print should start at the beginning of the message
9705 buffer next time. */
9706 message_buf_print = 0;
9707 }
9708 }
9709 }
9710
9711
9712 /* Dump an informative message to the minibuf. If M is 0, clear out
9713 any existing message, and let the mini-buffer text show through. */
9714
9715 static void
9716 vmessage (const char *m, va_list ap)
9717 {
9718 if (noninteractive)
9719 {
9720 if (m)
9721 {
9722 if (noninteractive_need_newline)
9723 putc ('\n', stderr);
9724 noninteractive_need_newline = 0;
9725 vfprintf (stderr, m, ap);
9726 if (cursor_in_echo_area == 0)
9727 fprintf (stderr, "\n");
9728 fflush (stderr);
9729 }
9730 }
9731 else if (INTERACTIVE)
9732 {
9733 /* The frame whose mini-buffer we're going to display the message
9734 on. It may be larger than the selected frame, so we need to
9735 use its buffer, not the selected frame's buffer. */
9736 Lisp_Object mini_window;
9737 struct frame *f, *sf = SELECTED_FRAME ();
9738
9739 /* Get the frame containing the mini-buffer
9740 that the selected frame is using. */
9741 mini_window = FRAME_MINIBUF_WINDOW (sf);
9742 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9743
9744 /* A null message buffer means that the frame hasn't really been
9745 initialized yet. Error messages get reported properly by
9746 cmd_error, so this must be just an informative message; toss
9747 it. */
9748 if (FRAME_MESSAGE_BUF (f))
9749 {
9750 if (m)
9751 {
9752 ptrdiff_t len;
9753
9754 len = doprnt (FRAME_MESSAGE_BUF (f),
9755 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9756
9757 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9758 }
9759 else
9760 message1 (0);
9761
9762 /* Print should start at the beginning of the message
9763 buffer next time. */
9764 message_buf_print = 0;
9765 }
9766 }
9767 }
9768
9769 void
9770 message (const char *m, ...)
9771 {
9772 va_list ap;
9773 va_start (ap, m);
9774 vmessage (m, ap);
9775 va_end (ap);
9776 }
9777
9778
9779 #if 0
9780 /* The non-logging version of message. */
9781
9782 void
9783 message_nolog (const char *m, ...)
9784 {
9785 Lisp_Object old_log_max;
9786 va_list ap;
9787 va_start (ap, m);
9788 old_log_max = Vmessage_log_max;
9789 Vmessage_log_max = Qnil;
9790 vmessage (m, ap);
9791 Vmessage_log_max = old_log_max;
9792 va_end (ap);
9793 }
9794 #endif
9795
9796
9797 /* Display the current message in the current mini-buffer. This is
9798 only called from error handlers in process.c, and is not time
9799 critical. */
9800
9801 void
9802 update_echo_area (void)
9803 {
9804 if (!NILP (echo_area_buffer[0]))
9805 {
9806 Lisp_Object string;
9807 string = Fcurrent_message ();
9808 message3 (string, SBYTES (string),
9809 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9810 }
9811 }
9812
9813
9814 /* Make sure echo area buffers in `echo_buffers' are live.
9815 If they aren't, make new ones. */
9816
9817 static void
9818 ensure_echo_area_buffers (void)
9819 {
9820 int i;
9821
9822 for (i = 0; i < 2; ++i)
9823 if (!BUFFERP (echo_buffer[i])
9824 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9825 {
9826 char name[30];
9827 Lisp_Object old_buffer;
9828 int j;
9829
9830 old_buffer = echo_buffer[i];
9831 sprintf (name, " *Echo Area %d*", i);
9832 echo_buffer[i] = Fget_buffer_create (build_string (name));
9833 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9834 /* to force word wrap in echo area -
9835 it was decided to postpone this*/
9836 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9837
9838 for (j = 0; j < 2; ++j)
9839 if (EQ (old_buffer, echo_area_buffer[j]))
9840 echo_area_buffer[j] = echo_buffer[i];
9841 }
9842 }
9843
9844
9845 /* Call FN with args A1..A4 with either the current or last displayed
9846 echo_area_buffer as current buffer.
9847
9848 WHICH zero means use the current message buffer
9849 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9850 from echo_buffer[] and clear it.
9851
9852 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9853 suitable buffer from echo_buffer[] and clear it.
9854
9855 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9856 that the current message becomes the last displayed one, make
9857 choose a suitable buffer for echo_area_buffer[0], and clear it.
9858
9859 Value is what FN returns. */
9860
9861 static int
9862 with_echo_area_buffer (struct window *w, int which,
9863 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9864 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9865 {
9866 Lisp_Object buffer;
9867 int this_one, the_other, clear_buffer_p, rc;
9868 ptrdiff_t count = SPECPDL_INDEX ();
9869
9870 /* If buffers aren't live, make new ones. */
9871 ensure_echo_area_buffers ();
9872
9873 clear_buffer_p = 0;
9874
9875 if (which == 0)
9876 this_one = 0, the_other = 1;
9877 else if (which > 0)
9878 this_one = 1, the_other = 0;
9879 else
9880 {
9881 this_one = 0, the_other = 1;
9882 clear_buffer_p = 1;
9883
9884 /* We need a fresh one in case the current echo buffer equals
9885 the one containing the last displayed echo area message. */
9886 if (!NILP (echo_area_buffer[this_one])
9887 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9888 echo_area_buffer[this_one] = Qnil;
9889 }
9890
9891 /* Choose a suitable buffer from echo_buffer[] is we don't
9892 have one. */
9893 if (NILP (echo_area_buffer[this_one]))
9894 {
9895 echo_area_buffer[this_one]
9896 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9897 ? echo_buffer[the_other]
9898 : echo_buffer[this_one]);
9899 clear_buffer_p = 1;
9900 }
9901
9902 buffer = echo_area_buffer[this_one];
9903
9904 /* Don't get confused by reusing the buffer used for echoing
9905 for a different purpose. */
9906 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9907 cancel_echoing ();
9908
9909 record_unwind_protect (unwind_with_echo_area_buffer,
9910 with_echo_area_buffer_unwind_data (w));
9911
9912 /* Make the echo area buffer current. Note that for display
9913 purposes, it is not necessary that the displayed window's buffer
9914 == current_buffer, except for text property lookup. So, let's
9915 only set that buffer temporarily here without doing a full
9916 Fset_window_buffer. We must also change w->pointm, though,
9917 because otherwise an assertions in unshow_buffer fails, and Emacs
9918 aborts. */
9919 set_buffer_internal_1 (XBUFFER (buffer));
9920 if (w)
9921 {
9922 w->buffer = buffer;
9923 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9924 }
9925
9926 BVAR (current_buffer, undo_list) = Qt;
9927 BVAR (current_buffer, read_only) = Qnil;
9928 specbind (Qinhibit_read_only, Qt);
9929 specbind (Qinhibit_modification_hooks, Qt);
9930
9931 if (clear_buffer_p && Z > BEG)
9932 del_range (BEG, Z);
9933
9934 xassert (BEGV >= BEG);
9935 xassert (ZV <= Z && ZV >= BEGV);
9936
9937 rc = fn (a1, a2, a3, a4);
9938
9939 xassert (BEGV >= BEG);
9940 xassert (ZV <= Z && ZV >= BEGV);
9941
9942 unbind_to (count, Qnil);
9943 return rc;
9944 }
9945
9946
9947 /* Save state that should be preserved around the call to the function
9948 FN called in with_echo_area_buffer. */
9949
9950 static Lisp_Object
9951 with_echo_area_buffer_unwind_data (struct window *w)
9952 {
9953 int i = 0;
9954 Lisp_Object vector, tmp;
9955
9956 /* Reduce consing by keeping one vector in
9957 Vwith_echo_area_save_vector. */
9958 vector = Vwith_echo_area_save_vector;
9959 Vwith_echo_area_save_vector = Qnil;
9960
9961 if (NILP (vector))
9962 vector = Fmake_vector (make_number (7), Qnil);
9963
9964 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9965 ASET (vector, i, Vdeactivate_mark); ++i;
9966 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9967
9968 if (w)
9969 {
9970 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9971 ASET (vector, i, w->buffer); ++i;
9972 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9973 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9974 }
9975 else
9976 {
9977 int end = i + 4;
9978 for (; i < end; ++i)
9979 ASET (vector, i, Qnil);
9980 }
9981
9982 xassert (i == ASIZE (vector));
9983 return vector;
9984 }
9985
9986
9987 /* Restore global state from VECTOR which was created by
9988 with_echo_area_buffer_unwind_data. */
9989
9990 static Lisp_Object
9991 unwind_with_echo_area_buffer (Lisp_Object vector)
9992 {
9993 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9994 Vdeactivate_mark = AREF (vector, 1);
9995 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9996
9997 if (WINDOWP (AREF (vector, 3)))
9998 {
9999 struct window *w;
10000 Lisp_Object buffer, charpos, bytepos;
10001
10002 w = XWINDOW (AREF (vector, 3));
10003 buffer = AREF (vector, 4);
10004 charpos = AREF (vector, 5);
10005 bytepos = AREF (vector, 6);
10006
10007 w->buffer = buffer;
10008 set_marker_both (w->pointm, buffer,
10009 XFASTINT (charpos), XFASTINT (bytepos));
10010 }
10011
10012 Vwith_echo_area_save_vector = vector;
10013 return Qnil;
10014 }
10015
10016
10017 /* Set up the echo area for use by print functions. MULTIBYTE_P
10018 non-zero means we will print multibyte. */
10019
10020 void
10021 setup_echo_area_for_printing (int multibyte_p)
10022 {
10023 /* If we can't find an echo area any more, exit. */
10024 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10025 Fkill_emacs (Qnil);
10026
10027 ensure_echo_area_buffers ();
10028
10029 if (!message_buf_print)
10030 {
10031 /* A message has been output since the last time we printed.
10032 Choose a fresh echo area buffer. */
10033 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10034 echo_area_buffer[0] = echo_buffer[1];
10035 else
10036 echo_area_buffer[0] = echo_buffer[0];
10037
10038 /* Switch to that buffer and clear it. */
10039 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10040 BVAR (current_buffer, truncate_lines) = Qnil;
10041
10042 if (Z > BEG)
10043 {
10044 ptrdiff_t count = SPECPDL_INDEX ();
10045 specbind (Qinhibit_read_only, Qt);
10046 /* Note that undo recording is always disabled. */
10047 del_range (BEG, Z);
10048 unbind_to (count, Qnil);
10049 }
10050 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10051
10052 /* Set up the buffer for the multibyteness we need. */
10053 if (multibyte_p
10054 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10055 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10056
10057 /* Raise the frame containing the echo area. */
10058 if (minibuffer_auto_raise)
10059 {
10060 struct frame *sf = SELECTED_FRAME ();
10061 Lisp_Object mini_window;
10062 mini_window = FRAME_MINIBUF_WINDOW (sf);
10063 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10064 }
10065
10066 message_log_maybe_newline ();
10067 message_buf_print = 1;
10068 }
10069 else
10070 {
10071 if (NILP (echo_area_buffer[0]))
10072 {
10073 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10074 echo_area_buffer[0] = echo_buffer[1];
10075 else
10076 echo_area_buffer[0] = echo_buffer[0];
10077 }
10078
10079 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10080 {
10081 /* Someone switched buffers between print requests. */
10082 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10083 BVAR (current_buffer, truncate_lines) = Qnil;
10084 }
10085 }
10086 }
10087
10088
10089 /* Display an echo area message in window W. Value is non-zero if W's
10090 height is changed. If display_last_displayed_message_p is
10091 non-zero, display the message that was last displayed, otherwise
10092 display the current message. */
10093
10094 static int
10095 display_echo_area (struct window *w)
10096 {
10097 int i, no_message_p, window_height_changed_p;
10098
10099 /* Temporarily disable garbage collections while displaying the echo
10100 area. This is done because a GC can print a message itself.
10101 That message would modify the echo area buffer's contents while a
10102 redisplay of the buffer is going on, and seriously confuse
10103 redisplay. */
10104 ptrdiff_t count = inhibit_garbage_collection ();
10105
10106 /* If there is no message, we must call display_echo_area_1
10107 nevertheless because it resizes the window. But we will have to
10108 reset the echo_area_buffer in question to nil at the end because
10109 with_echo_area_buffer will sets it to an empty buffer. */
10110 i = display_last_displayed_message_p ? 1 : 0;
10111 no_message_p = NILP (echo_area_buffer[i]);
10112
10113 window_height_changed_p
10114 = with_echo_area_buffer (w, display_last_displayed_message_p,
10115 display_echo_area_1,
10116 (intptr_t) w, Qnil, 0, 0);
10117
10118 if (no_message_p)
10119 echo_area_buffer[i] = Qnil;
10120
10121 unbind_to (count, Qnil);
10122 return window_height_changed_p;
10123 }
10124
10125
10126 /* Helper for display_echo_area. Display the current buffer which
10127 contains the current echo area message in window W, a mini-window,
10128 a pointer to which is passed in A1. A2..A4 are currently not used.
10129 Change the height of W so that all of the message is displayed.
10130 Value is non-zero if height of W was changed. */
10131
10132 static int
10133 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10134 {
10135 intptr_t i1 = a1;
10136 struct window *w = (struct window *) i1;
10137 Lisp_Object window;
10138 struct text_pos start;
10139 int window_height_changed_p = 0;
10140
10141 /* Do this before displaying, so that we have a large enough glyph
10142 matrix for the display. If we can't get enough space for the
10143 whole text, display the last N lines. That works by setting w->start. */
10144 window_height_changed_p = resize_mini_window (w, 0);
10145
10146 /* Use the starting position chosen by resize_mini_window. */
10147 SET_TEXT_POS_FROM_MARKER (start, w->start);
10148
10149 /* Display. */
10150 clear_glyph_matrix (w->desired_matrix);
10151 XSETWINDOW (window, w);
10152 try_window (window, start, 0);
10153
10154 return window_height_changed_p;
10155 }
10156
10157
10158 /* Resize the echo area window to exactly the size needed for the
10159 currently displayed message, if there is one. If a mini-buffer
10160 is active, don't shrink it. */
10161
10162 void
10163 resize_echo_area_exactly (void)
10164 {
10165 if (BUFFERP (echo_area_buffer[0])
10166 && WINDOWP (echo_area_window))
10167 {
10168 struct window *w = XWINDOW (echo_area_window);
10169 int resized_p;
10170 Lisp_Object resize_exactly;
10171
10172 if (minibuf_level == 0)
10173 resize_exactly = Qt;
10174 else
10175 resize_exactly = Qnil;
10176
10177 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10178 (intptr_t) w, resize_exactly,
10179 0, 0);
10180 if (resized_p)
10181 {
10182 ++windows_or_buffers_changed;
10183 ++update_mode_lines;
10184 redisplay_internal ();
10185 }
10186 }
10187 }
10188
10189
10190 /* Callback function for with_echo_area_buffer, when used from
10191 resize_echo_area_exactly. A1 contains a pointer to the window to
10192 resize, EXACTLY non-nil means resize the mini-window exactly to the
10193 size of the text displayed. A3 and A4 are not used. Value is what
10194 resize_mini_window returns. */
10195
10196 static int
10197 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10198 {
10199 intptr_t i1 = a1;
10200 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10201 }
10202
10203
10204 /* Resize mini-window W to fit the size of its contents. EXACT_P
10205 means size the window exactly to the size needed. Otherwise, it's
10206 only enlarged until W's buffer is empty.
10207
10208 Set W->start to the right place to begin display. If the whole
10209 contents fit, start at the beginning. Otherwise, start so as
10210 to make the end of the contents appear. This is particularly
10211 important for y-or-n-p, but seems desirable generally.
10212
10213 Value is non-zero if the window height has been changed. */
10214
10215 int
10216 resize_mini_window (struct window *w, int exact_p)
10217 {
10218 struct frame *f = XFRAME (w->frame);
10219 int window_height_changed_p = 0;
10220
10221 xassert (MINI_WINDOW_P (w));
10222
10223 /* By default, start display at the beginning. */
10224 set_marker_both (w->start, w->buffer,
10225 BUF_BEGV (XBUFFER (w->buffer)),
10226 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10227
10228 /* Don't resize windows while redisplaying a window; it would
10229 confuse redisplay functions when the size of the window they are
10230 displaying changes from under them. Such a resizing can happen,
10231 for instance, when which-func prints a long message while
10232 we are running fontification-functions. We're running these
10233 functions with safe_call which binds inhibit-redisplay to t. */
10234 if (!NILP (Vinhibit_redisplay))
10235 return 0;
10236
10237 /* Nil means don't try to resize. */
10238 if (NILP (Vresize_mini_windows)
10239 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10240 return 0;
10241
10242 if (!FRAME_MINIBUF_ONLY_P (f))
10243 {
10244 struct it it;
10245 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10246 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10247 int height;
10248 EMACS_INT max_height;
10249 int unit = FRAME_LINE_HEIGHT (f);
10250 struct text_pos start;
10251 struct buffer *old_current_buffer = NULL;
10252
10253 if (current_buffer != XBUFFER (w->buffer))
10254 {
10255 old_current_buffer = current_buffer;
10256 set_buffer_internal (XBUFFER (w->buffer));
10257 }
10258
10259 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10260
10261 /* Compute the max. number of lines specified by the user. */
10262 if (FLOATP (Vmax_mini_window_height))
10263 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10264 else if (INTEGERP (Vmax_mini_window_height))
10265 max_height = XINT (Vmax_mini_window_height);
10266 else
10267 max_height = total_height / 4;
10268
10269 /* Correct that max. height if it's bogus. */
10270 max_height = max (1, max_height);
10271 max_height = min (total_height, max_height);
10272
10273 /* Find out the height of the text in the window. */
10274 if (it.line_wrap == TRUNCATE)
10275 height = 1;
10276 else
10277 {
10278 last_height = 0;
10279 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10280 if (it.max_ascent == 0 && it.max_descent == 0)
10281 height = it.current_y + last_height;
10282 else
10283 height = it.current_y + it.max_ascent + it.max_descent;
10284 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10285 height = (height + unit - 1) / unit;
10286 }
10287
10288 /* Compute a suitable window start. */
10289 if (height > max_height)
10290 {
10291 height = max_height;
10292 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10293 move_it_vertically_backward (&it, (height - 1) * unit);
10294 start = it.current.pos;
10295 }
10296 else
10297 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10298 SET_MARKER_FROM_TEXT_POS (w->start, start);
10299
10300 if (EQ (Vresize_mini_windows, Qgrow_only))
10301 {
10302 /* Let it grow only, until we display an empty message, in which
10303 case the window shrinks again. */
10304 if (height > WINDOW_TOTAL_LINES (w))
10305 {
10306 int old_height = WINDOW_TOTAL_LINES (w);
10307 freeze_window_starts (f, 1);
10308 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10309 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10310 }
10311 else if (height < WINDOW_TOTAL_LINES (w)
10312 && (exact_p || BEGV == ZV))
10313 {
10314 int old_height = WINDOW_TOTAL_LINES (w);
10315 freeze_window_starts (f, 0);
10316 shrink_mini_window (w);
10317 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10318 }
10319 }
10320 else
10321 {
10322 /* Always resize to exact size needed. */
10323 if (height > WINDOW_TOTAL_LINES (w))
10324 {
10325 int old_height = WINDOW_TOTAL_LINES (w);
10326 freeze_window_starts (f, 1);
10327 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10328 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10329 }
10330 else if (height < WINDOW_TOTAL_LINES (w))
10331 {
10332 int old_height = WINDOW_TOTAL_LINES (w);
10333 freeze_window_starts (f, 0);
10334 shrink_mini_window (w);
10335
10336 if (height)
10337 {
10338 freeze_window_starts (f, 1);
10339 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10340 }
10341
10342 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10343 }
10344 }
10345
10346 if (old_current_buffer)
10347 set_buffer_internal (old_current_buffer);
10348 }
10349
10350 return window_height_changed_p;
10351 }
10352
10353
10354 /* Value is the current message, a string, or nil if there is no
10355 current message. */
10356
10357 Lisp_Object
10358 current_message (void)
10359 {
10360 Lisp_Object msg;
10361
10362 if (!BUFFERP (echo_area_buffer[0]))
10363 msg = Qnil;
10364 else
10365 {
10366 with_echo_area_buffer (0, 0, current_message_1,
10367 (intptr_t) &msg, Qnil, 0, 0);
10368 if (NILP (msg))
10369 echo_area_buffer[0] = Qnil;
10370 }
10371
10372 return msg;
10373 }
10374
10375
10376 static int
10377 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10378 {
10379 intptr_t i1 = a1;
10380 Lisp_Object *msg = (Lisp_Object *) i1;
10381
10382 if (Z > BEG)
10383 *msg = make_buffer_string (BEG, Z, 1);
10384 else
10385 *msg = Qnil;
10386 return 0;
10387 }
10388
10389
10390 /* Push the current message on Vmessage_stack for later restoration
10391 by restore_message. Value is non-zero if the current message isn't
10392 empty. This is a relatively infrequent operation, so it's not
10393 worth optimizing. */
10394
10395 int
10396 push_message (void)
10397 {
10398 Lisp_Object msg;
10399 msg = current_message ();
10400 Vmessage_stack = Fcons (msg, Vmessage_stack);
10401 return STRINGP (msg);
10402 }
10403
10404
10405 /* Restore message display from the top of Vmessage_stack. */
10406
10407 void
10408 restore_message (void)
10409 {
10410 Lisp_Object msg;
10411
10412 xassert (CONSP (Vmessage_stack));
10413 msg = XCAR (Vmessage_stack);
10414 if (STRINGP (msg))
10415 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10416 else
10417 message3_nolog (msg, 0, 0);
10418 }
10419
10420
10421 /* Handler for record_unwind_protect calling pop_message. */
10422
10423 Lisp_Object
10424 pop_message_unwind (Lisp_Object dummy)
10425 {
10426 pop_message ();
10427 return Qnil;
10428 }
10429
10430 /* Pop the top-most entry off Vmessage_stack. */
10431
10432 static void
10433 pop_message (void)
10434 {
10435 xassert (CONSP (Vmessage_stack));
10436 Vmessage_stack = XCDR (Vmessage_stack);
10437 }
10438
10439
10440 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10441 exits. If the stack is not empty, we have a missing pop_message
10442 somewhere. */
10443
10444 void
10445 check_message_stack (void)
10446 {
10447 if (!NILP (Vmessage_stack))
10448 abort ();
10449 }
10450
10451
10452 /* Truncate to NCHARS what will be displayed in the echo area the next
10453 time we display it---but don't redisplay it now. */
10454
10455 void
10456 truncate_echo_area (ptrdiff_t nchars)
10457 {
10458 if (nchars == 0)
10459 echo_area_buffer[0] = Qnil;
10460 /* A null message buffer means that the frame hasn't really been
10461 initialized yet. Error messages get reported properly by
10462 cmd_error, so this must be just an informative message; toss it. */
10463 else if (!noninteractive
10464 && INTERACTIVE
10465 && !NILP (echo_area_buffer[0]))
10466 {
10467 struct frame *sf = SELECTED_FRAME ();
10468 if (FRAME_MESSAGE_BUF (sf))
10469 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10470 }
10471 }
10472
10473
10474 /* Helper function for truncate_echo_area. Truncate the current
10475 message to at most NCHARS characters. */
10476
10477 static int
10478 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10479 {
10480 if (BEG + nchars < Z)
10481 del_range (BEG + nchars, Z);
10482 if (Z == BEG)
10483 echo_area_buffer[0] = Qnil;
10484 return 0;
10485 }
10486
10487
10488 /* Set the current message to a substring of S or STRING.
10489
10490 If STRING is a Lisp string, set the message to the first NBYTES
10491 bytes from STRING. NBYTES zero means use the whole string. If
10492 STRING is multibyte, the message will be displayed multibyte.
10493
10494 If S is not null, set the message to the first LEN bytes of S. LEN
10495 zero means use the whole string. MULTIBYTE_P non-zero means S is
10496 multibyte. Display the message multibyte in that case.
10497
10498 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10499 to t before calling set_message_1 (which calls insert).
10500 */
10501
10502 static void
10503 set_message (const char *s, Lisp_Object string,
10504 ptrdiff_t nbytes, int multibyte_p)
10505 {
10506 message_enable_multibyte
10507 = ((s && multibyte_p)
10508 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10509
10510 with_echo_area_buffer (0, -1, set_message_1,
10511 (intptr_t) s, string, nbytes, multibyte_p);
10512 message_buf_print = 0;
10513 help_echo_showing_p = 0;
10514 }
10515
10516
10517 /* Helper function for set_message. Arguments have the same meaning
10518 as there, with A1 corresponding to S and A2 corresponding to STRING
10519 This function is called with the echo area buffer being
10520 current. */
10521
10522 static int
10523 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10524 {
10525 intptr_t i1 = a1;
10526 const char *s = (const char *) i1;
10527 const unsigned char *msg = (const unsigned char *) s;
10528 Lisp_Object string = a2;
10529
10530 /* Change multibyteness of the echo buffer appropriately. */
10531 if (message_enable_multibyte
10532 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10533 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10534
10535 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10536 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10537 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10538
10539 /* Insert new message at BEG. */
10540 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10541
10542 if (STRINGP (string))
10543 {
10544 ptrdiff_t nchars;
10545
10546 if (nbytes == 0)
10547 nbytes = SBYTES (string);
10548 nchars = string_byte_to_char (string, nbytes);
10549
10550 /* This function takes care of single/multibyte conversion. We
10551 just have to ensure that the echo area buffer has the right
10552 setting of enable_multibyte_characters. */
10553 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10554 }
10555 else if (s)
10556 {
10557 if (nbytes == 0)
10558 nbytes = strlen (s);
10559
10560 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10561 {
10562 /* Convert from multi-byte to single-byte. */
10563 ptrdiff_t i;
10564 int c, n;
10565 char work[1];
10566
10567 /* Convert a multibyte string to single-byte. */
10568 for (i = 0; i < nbytes; i += n)
10569 {
10570 c = string_char_and_length (msg + i, &n);
10571 work[0] = (ASCII_CHAR_P (c)
10572 ? c
10573 : multibyte_char_to_unibyte (c));
10574 insert_1_both (work, 1, 1, 1, 0, 0);
10575 }
10576 }
10577 else if (!multibyte_p
10578 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10579 {
10580 /* Convert from single-byte to multi-byte. */
10581 ptrdiff_t i;
10582 int c, n;
10583 unsigned char str[MAX_MULTIBYTE_LENGTH];
10584
10585 /* Convert a single-byte string to multibyte. */
10586 for (i = 0; i < nbytes; i++)
10587 {
10588 c = msg[i];
10589 MAKE_CHAR_MULTIBYTE (c);
10590 n = CHAR_STRING (c, str);
10591 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10592 }
10593 }
10594 else
10595 insert_1 (s, nbytes, 1, 0, 0);
10596 }
10597
10598 return 0;
10599 }
10600
10601
10602 /* Clear messages. CURRENT_P non-zero means clear the current
10603 message. LAST_DISPLAYED_P non-zero means clear the message
10604 last displayed. */
10605
10606 void
10607 clear_message (int current_p, int last_displayed_p)
10608 {
10609 if (current_p)
10610 {
10611 echo_area_buffer[0] = Qnil;
10612 message_cleared_p = 1;
10613 }
10614
10615 if (last_displayed_p)
10616 echo_area_buffer[1] = Qnil;
10617
10618 message_buf_print = 0;
10619 }
10620
10621 /* Clear garbaged frames.
10622
10623 This function is used where the old redisplay called
10624 redraw_garbaged_frames which in turn called redraw_frame which in
10625 turn called clear_frame. The call to clear_frame was a source of
10626 flickering. I believe a clear_frame is not necessary. It should
10627 suffice in the new redisplay to invalidate all current matrices,
10628 and ensure a complete redisplay of all windows. */
10629
10630 static void
10631 clear_garbaged_frames (void)
10632 {
10633 if (frame_garbaged)
10634 {
10635 Lisp_Object tail, frame;
10636 int changed_count = 0;
10637
10638 FOR_EACH_FRAME (tail, frame)
10639 {
10640 struct frame *f = XFRAME (frame);
10641
10642 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10643 {
10644 if (f->resized_p)
10645 {
10646 Fredraw_frame (frame);
10647 f->force_flush_display_p = 1;
10648 }
10649 clear_current_matrices (f);
10650 changed_count++;
10651 f->garbaged = 0;
10652 f->resized_p = 0;
10653 }
10654 }
10655
10656 frame_garbaged = 0;
10657 if (changed_count)
10658 ++windows_or_buffers_changed;
10659 }
10660 }
10661
10662
10663 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10664 is non-zero update selected_frame. Value is non-zero if the
10665 mini-windows height has been changed. */
10666
10667 static int
10668 echo_area_display (int update_frame_p)
10669 {
10670 Lisp_Object mini_window;
10671 struct window *w;
10672 struct frame *f;
10673 int window_height_changed_p = 0;
10674 struct frame *sf = SELECTED_FRAME ();
10675
10676 mini_window = FRAME_MINIBUF_WINDOW (sf);
10677 w = XWINDOW (mini_window);
10678 f = XFRAME (WINDOW_FRAME (w));
10679
10680 /* Don't display if frame is invisible or not yet initialized. */
10681 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10682 return 0;
10683
10684 #ifdef HAVE_WINDOW_SYSTEM
10685 /* When Emacs starts, selected_frame may be the initial terminal
10686 frame. If we let this through, a message would be displayed on
10687 the terminal. */
10688 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10689 return 0;
10690 #endif /* HAVE_WINDOW_SYSTEM */
10691
10692 /* Redraw garbaged frames. */
10693 if (frame_garbaged)
10694 clear_garbaged_frames ();
10695
10696 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10697 {
10698 echo_area_window = mini_window;
10699 window_height_changed_p = display_echo_area (w);
10700 w->must_be_updated_p = 1;
10701
10702 /* Update the display, unless called from redisplay_internal.
10703 Also don't update the screen during redisplay itself. The
10704 update will happen at the end of redisplay, and an update
10705 here could cause confusion. */
10706 if (update_frame_p && !redisplaying_p)
10707 {
10708 int n = 0;
10709
10710 /* If the display update has been interrupted by pending
10711 input, update mode lines in the frame. Due to the
10712 pending input, it might have been that redisplay hasn't
10713 been called, so that mode lines above the echo area are
10714 garbaged. This looks odd, so we prevent it here. */
10715 if (!display_completed)
10716 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10717
10718 if (window_height_changed_p
10719 /* Don't do this if Emacs is shutting down. Redisplay
10720 needs to run hooks. */
10721 && !NILP (Vrun_hooks))
10722 {
10723 /* Must update other windows. Likewise as in other
10724 cases, don't let this update be interrupted by
10725 pending input. */
10726 ptrdiff_t count = SPECPDL_INDEX ();
10727 specbind (Qredisplay_dont_pause, Qt);
10728 windows_or_buffers_changed = 1;
10729 redisplay_internal ();
10730 unbind_to (count, Qnil);
10731 }
10732 else if (FRAME_WINDOW_P (f) && n == 0)
10733 {
10734 /* Window configuration is the same as before.
10735 Can do with a display update of the echo area,
10736 unless we displayed some mode lines. */
10737 update_single_window (w, 1);
10738 FRAME_RIF (f)->flush_display (f);
10739 }
10740 else
10741 update_frame (f, 1, 1);
10742
10743 /* If cursor is in the echo area, make sure that the next
10744 redisplay displays the minibuffer, so that the cursor will
10745 be replaced with what the minibuffer wants. */
10746 if (cursor_in_echo_area)
10747 ++windows_or_buffers_changed;
10748 }
10749 }
10750 else if (!EQ (mini_window, selected_window))
10751 windows_or_buffers_changed++;
10752
10753 /* Last displayed message is now the current message. */
10754 echo_area_buffer[1] = echo_area_buffer[0];
10755 /* Inform read_char that we're not echoing. */
10756 echo_message_buffer = Qnil;
10757
10758 /* Prevent redisplay optimization in redisplay_internal by resetting
10759 this_line_start_pos. This is done because the mini-buffer now
10760 displays the message instead of its buffer text. */
10761 if (EQ (mini_window, selected_window))
10762 CHARPOS (this_line_start_pos) = 0;
10763
10764 return window_height_changed_p;
10765 }
10766
10767
10768 \f
10769 /***********************************************************************
10770 Mode Lines and Frame Titles
10771 ***********************************************************************/
10772
10773 /* A buffer for constructing non-propertized mode-line strings and
10774 frame titles in it; allocated from the heap in init_xdisp and
10775 resized as needed in store_mode_line_noprop_char. */
10776
10777 static char *mode_line_noprop_buf;
10778
10779 /* The buffer's end, and a current output position in it. */
10780
10781 static char *mode_line_noprop_buf_end;
10782 static char *mode_line_noprop_ptr;
10783
10784 #define MODE_LINE_NOPROP_LEN(start) \
10785 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10786
10787 static enum {
10788 MODE_LINE_DISPLAY = 0,
10789 MODE_LINE_TITLE,
10790 MODE_LINE_NOPROP,
10791 MODE_LINE_STRING
10792 } mode_line_target;
10793
10794 /* Alist that caches the results of :propertize.
10795 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10796 static Lisp_Object mode_line_proptrans_alist;
10797
10798 /* List of strings making up the mode-line. */
10799 static Lisp_Object mode_line_string_list;
10800
10801 /* Base face property when building propertized mode line string. */
10802 static Lisp_Object mode_line_string_face;
10803 static Lisp_Object mode_line_string_face_prop;
10804
10805
10806 /* Unwind data for mode line strings */
10807
10808 static Lisp_Object Vmode_line_unwind_vector;
10809
10810 static Lisp_Object
10811 format_mode_line_unwind_data (struct buffer *obuf,
10812 Lisp_Object owin,
10813 int save_proptrans)
10814 {
10815 Lisp_Object vector, tmp;
10816
10817 /* Reduce consing by keeping one vector in
10818 Vwith_echo_area_save_vector. */
10819 vector = Vmode_line_unwind_vector;
10820 Vmode_line_unwind_vector = Qnil;
10821
10822 if (NILP (vector))
10823 vector = Fmake_vector (make_number (8), Qnil);
10824
10825 ASET (vector, 0, make_number (mode_line_target));
10826 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10827 ASET (vector, 2, mode_line_string_list);
10828 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10829 ASET (vector, 4, mode_line_string_face);
10830 ASET (vector, 5, mode_line_string_face_prop);
10831
10832 if (obuf)
10833 XSETBUFFER (tmp, obuf);
10834 else
10835 tmp = Qnil;
10836 ASET (vector, 6, tmp);
10837 ASET (vector, 7, owin);
10838
10839 return vector;
10840 }
10841
10842 static Lisp_Object
10843 unwind_format_mode_line (Lisp_Object vector)
10844 {
10845 mode_line_target = XINT (AREF (vector, 0));
10846 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10847 mode_line_string_list = AREF (vector, 2);
10848 if (! EQ (AREF (vector, 3), Qt))
10849 mode_line_proptrans_alist = AREF (vector, 3);
10850 mode_line_string_face = AREF (vector, 4);
10851 mode_line_string_face_prop = AREF (vector, 5);
10852
10853 if (!NILP (AREF (vector, 7)))
10854 /* Select window before buffer, since it may change the buffer. */
10855 Fselect_window (AREF (vector, 7), Qt);
10856
10857 if (!NILP (AREF (vector, 6)))
10858 {
10859 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10860 ASET (vector, 6, Qnil);
10861 }
10862
10863 Vmode_line_unwind_vector = vector;
10864 return Qnil;
10865 }
10866
10867
10868 /* Store a single character C for the frame title in mode_line_noprop_buf.
10869 Re-allocate mode_line_noprop_buf if necessary. */
10870
10871 static void
10872 store_mode_line_noprop_char (char c)
10873 {
10874 /* If output position has reached the end of the allocated buffer,
10875 increase the buffer's size. */
10876 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10877 {
10878 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10879 ptrdiff_t size = len;
10880 mode_line_noprop_buf =
10881 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10882 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10883 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10884 }
10885
10886 *mode_line_noprop_ptr++ = c;
10887 }
10888
10889
10890 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10891 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10892 characters that yield more columns than PRECISION; PRECISION <= 0
10893 means copy the whole string. Pad with spaces until FIELD_WIDTH
10894 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10895 pad. Called from display_mode_element when it is used to build a
10896 frame title. */
10897
10898 static int
10899 store_mode_line_noprop (const char *string, int field_width, int precision)
10900 {
10901 const unsigned char *str = (const unsigned char *) string;
10902 int n = 0;
10903 ptrdiff_t dummy, nbytes;
10904
10905 /* Copy at most PRECISION chars from STR. */
10906 nbytes = strlen (string);
10907 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10908 while (nbytes--)
10909 store_mode_line_noprop_char (*str++);
10910
10911 /* Fill up with spaces until FIELD_WIDTH reached. */
10912 while (field_width > 0
10913 && n < field_width)
10914 {
10915 store_mode_line_noprop_char (' ');
10916 ++n;
10917 }
10918
10919 return n;
10920 }
10921
10922 /***********************************************************************
10923 Frame Titles
10924 ***********************************************************************/
10925
10926 #ifdef HAVE_WINDOW_SYSTEM
10927
10928 /* Set the title of FRAME, if it has changed. The title format is
10929 Vicon_title_format if FRAME is iconified, otherwise it is
10930 frame_title_format. */
10931
10932 static void
10933 x_consider_frame_title (Lisp_Object frame)
10934 {
10935 struct frame *f = XFRAME (frame);
10936
10937 if (FRAME_WINDOW_P (f)
10938 || FRAME_MINIBUF_ONLY_P (f)
10939 || f->explicit_name)
10940 {
10941 /* Do we have more than one visible frame on this X display? */
10942 Lisp_Object tail;
10943 Lisp_Object fmt;
10944 ptrdiff_t title_start;
10945 char *title;
10946 ptrdiff_t len;
10947 struct it it;
10948 ptrdiff_t count = SPECPDL_INDEX ();
10949
10950 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10951 {
10952 Lisp_Object other_frame = XCAR (tail);
10953 struct frame *tf = XFRAME (other_frame);
10954
10955 if (tf != f
10956 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10957 && !FRAME_MINIBUF_ONLY_P (tf)
10958 && !EQ (other_frame, tip_frame)
10959 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10960 break;
10961 }
10962
10963 /* Set global variable indicating that multiple frames exist. */
10964 multiple_frames = CONSP (tail);
10965
10966 /* Switch to the buffer of selected window of the frame. Set up
10967 mode_line_target so that display_mode_element will output into
10968 mode_line_noprop_buf; then display the title. */
10969 record_unwind_protect (unwind_format_mode_line,
10970 format_mode_line_unwind_data
10971 (current_buffer, selected_window, 0));
10972
10973 Fselect_window (f->selected_window, Qt);
10974 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10975 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10976
10977 mode_line_target = MODE_LINE_TITLE;
10978 title_start = MODE_LINE_NOPROP_LEN (0);
10979 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10980 NULL, DEFAULT_FACE_ID);
10981 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10982 len = MODE_LINE_NOPROP_LEN (title_start);
10983 title = mode_line_noprop_buf + title_start;
10984 unbind_to (count, Qnil);
10985
10986 /* Set the title only if it's changed. This avoids consing in
10987 the common case where it hasn't. (If it turns out that we've
10988 already wasted too much time by walking through the list with
10989 display_mode_element, then we might need to optimize at a
10990 higher level than this.) */
10991 if (! STRINGP (f->name)
10992 || SBYTES (f->name) != len
10993 || memcmp (title, SDATA (f->name), len) != 0)
10994 x_implicitly_set_name (f, make_string (title, len), Qnil);
10995 }
10996 }
10997
10998 #endif /* not HAVE_WINDOW_SYSTEM */
10999
11000
11001
11002 \f
11003 /***********************************************************************
11004 Menu Bars
11005 ***********************************************************************/
11006
11007
11008 /* Prepare for redisplay by updating menu-bar item lists when
11009 appropriate. This can call eval. */
11010
11011 void
11012 prepare_menu_bars (void)
11013 {
11014 int all_windows;
11015 struct gcpro gcpro1, gcpro2;
11016 struct frame *f;
11017 Lisp_Object tooltip_frame;
11018
11019 #ifdef HAVE_WINDOW_SYSTEM
11020 tooltip_frame = tip_frame;
11021 #else
11022 tooltip_frame = Qnil;
11023 #endif
11024
11025 /* Update all frame titles based on their buffer names, etc. We do
11026 this before the menu bars so that the buffer-menu will show the
11027 up-to-date frame titles. */
11028 #ifdef HAVE_WINDOW_SYSTEM
11029 if (windows_or_buffers_changed || update_mode_lines)
11030 {
11031 Lisp_Object tail, frame;
11032
11033 FOR_EACH_FRAME (tail, frame)
11034 {
11035 f = XFRAME (frame);
11036 if (!EQ (frame, tooltip_frame)
11037 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11038 x_consider_frame_title (frame);
11039 }
11040 }
11041 #endif /* HAVE_WINDOW_SYSTEM */
11042
11043 /* Update the menu bar item lists, if appropriate. This has to be
11044 done before any actual redisplay or generation of display lines. */
11045 all_windows = (update_mode_lines
11046 || buffer_shared > 1
11047 || windows_or_buffers_changed);
11048 if (all_windows)
11049 {
11050 Lisp_Object tail, frame;
11051 ptrdiff_t count = SPECPDL_INDEX ();
11052 /* 1 means that update_menu_bar has run its hooks
11053 so any further calls to update_menu_bar shouldn't do so again. */
11054 int menu_bar_hooks_run = 0;
11055
11056 record_unwind_save_match_data ();
11057
11058 FOR_EACH_FRAME (tail, frame)
11059 {
11060 f = XFRAME (frame);
11061
11062 /* Ignore tooltip frame. */
11063 if (EQ (frame, tooltip_frame))
11064 continue;
11065
11066 /* If a window on this frame changed size, report that to
11067 the user and clear the size-change flag. */
11068 if (FRAME_WINDOW_SIZES_CHANGED (f))
11069 {
11070 Lisp_Object functions;
11071
11072 /* Clear flag first in case we get an error below. */
11073 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11074 functions = Vwindow_size_change_functions;
11075 GCPRO2 (tail, functions);
11076
11077 while (CONSP (functions))
11078 {
11079 if (!EQ (XCAR (functions), Qt))
11080 call1 (XCAR (functions), frame);
11081 functions = XCDR (functions);
11082 }
11083 UNGCPRO;
11084 }
11085
11086 GCPRO1 (tail);
11087 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11088 #ifdef HAVE_WINDOW_SYSTEM
11089 update_tool_bar (f, 0);
11090 #endif
11091 #ifdef HAVE_NS
11092 if (windows_or_buffers_changed
11093 && FRAME_NS_P (f))
11094 ns_set_doc_edited (f, Fbuffer_modified_p
11095 (XWINDOW (f->selected_window)->buffer));
11096 #endif
11097 UNGCPRO;
11098 }
11099
11100 unbind_to (count, Qnil);
11101 }
11102 else
11103 {
11104 struct frame *sf = SELECTED_FRAME ();
11105 update_menu_bar (sf, 1, 0);
11106 #ifdef HAVE_WINDOW_SYSTEM
11107 update_tool_bar (sf, 1);
11108 #endif
11109 }
11110 }
11111
11112
11113 /* Update the menu bar item list for frame F. This has to be done
11114 before we start to fill in any display lines, because it can call
11115 eval.
11116
11117 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11118
11119 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11120 already ran the menu bar hooks for this redisplay, so there
11121 is no need to run them again. The return value is the
11122 updated value of this flag, to pass to the next call. */
11123
11124 static int
11125 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11126 {
11127 Lisp_Object window;
11128 register struct window *w;
11129
11130 /* If called recursively during a menu update, do nothing. This can
11131 happen when, for instance, an activate-menubar-hook causes a
11132 redisplay. */
11133 if (inhibit_menubar_update)
11134 return hooks_run;
11135
11136 window = FRAME_SELECTED_WINDOW (f);
11137 w = XWINDOW (window);
11138
11139 if (FRAME_WINDOW_P (f)
11140 ?
11141 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11142 || defined (HAVE_NS) || defined (USE_GTK)
11143 FRAME_EXTERNAL_MENU_BAR (f)
11144 #else
11145 FRAME_MENU_BAR_LINES (f) > 0
11146 #endif
11147 : FRAME_MENU_BAR_LINES (f) > 0)
11148 {
11149 /* If the user has switched buffers or windows, we need to
11150 recompute to reflect the new bindings. But we'll
11151 recompute when update_mode_lines is set too; that means
11152 that people can use force-mode-line-update to request
11153 that the menu bar be recomputed. The adverse effect on
11154 the rest of the redisplay algorithm is about the same as
11155 windows_or_buffers_changed anyway. */
11156 if (windows_or_buffers_changed
11157 /* This used to test w->update_mode_line, but we believe
11158 there is no need to recompute the menu in that case. */
11159 || update_mode_lines
11160 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11161 < BUF_MODIFF (XBUFFER (w->buffer)))
11162 != !NILP (w->last_had_star))
11163 || ((!NILP (Vtransient_mark_mode)
11164 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11165 != !NILP (w->region_showing)))
11166 {
11167 struct buffer *prev = current_buffer;
11168 ptrdiff_t count = SPECPDL_INDEX ();
11169
11170 specbind (Qinhibit_menubar_update, Qt);
11171
11172 set_buffer_internal_1 (XBUFFER (w->buffer));
11173 if (save_match_data)
11174 record_unwind_save_match_data ();
11175 if (NILP (Voverriding_local_map_menu_flag))
11176 {
11177 specbind (Qoverriding_terminal_local_map, Qnil);
11178 specbind (Qoverriding_local_map, Qnil);
11179 }
11180
11181 if (!hooks_run)
11182 {
11183 /* Run the Lucid hook. */
11184 safe_run_hooks (Qactivate_menubar_hook);
11185
11186 /* If it has changed current-menubar from previous value,
11187 really recompute the menu-bar from the value. */
11188 if (! NILP (Vlucid_menu_bar_dirty_flag))
11189 call0 (Qrecompute_lucid_menubar);
11190
11191 safe_run_hooks (Qmenu_bar_update_hook);
11192
11193 hooks_run = 1;
11194 }
11195
11196 XSETFRAME (Vmenu_updating_frame, f);
11197 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11198
11199 /* Redisplay the menu bar in case we changed it. */
11200 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11201 || defined (HAVE_NS) || defined (USE_GTK)
11202 if (FRAME_WINDOW_P (f))
11203 {
11204 #if defined (HAVE_NS)
11205 /* All frames on Mac OS share the same menubar. So only
11206 the selected frame should be allowed to set it. */
11207 if (f == SELECTED_FRAME ())
11208 #endif
11209 set_frame_menubar (f, 0, 0);
11210 }
11211 else
11212 /* On a terminal screen, the menu bar is an ordinary screen
11213 line, and this makes it get updated. */
11214 w->update_mode_line = Qt;
11215 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11216 /* In the non-toolkit version, the menu bar is an ordinary screen
11217 line, and this makes it get updated. */
11218 w->update_mode_line = Qt;
11219 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11220
11221 unbind_to (count, Qnil);
11222 set_buffer_internal_1 (prev);
11223 }
11224 }
11225
11226 return hooks_run;
11227 }
11228
11229
11230 \f
11231 /***********************************************************************
11232 Output Cursor
11233 ***********************************************************************/
11234
11235 #ifdef HAVE_WINDOW_SYSTEM
11236
11237 /* EXPORT:
11238 Nominal cursor position -- where to draw output.
11239 HPOS and VPOS are window relative glyph matrix coordinates.
11240 X and Y are window relative pixel coordinates. */
11241
11242 struct cursor_pos output_cursor;
11243
11244
11245 /* EXPORT:
11246 Set the global variable output_cursor to CURSOR. All cursor
11247 positions are relative to updated_window. */
11248
11249 void
11250 set_output_cursor (struct cursor_pos *cursor)
11251 {
11252 output_cursor.hpos = cursor->hpos;
11253 output_cursor.vpos = cursor->vpos;
11254 output_cursor.x = cursor->x;
11255 output_cursor.y = cursor->y;
11256 }
11257
11258
11259 /* EXPORT for RIF:
11260 Set a nominal cursor position.
11261
11262 HPOS and VPOS are column/row positions in a window glyph matrix. X
11263 and Y are window text area relative pixel positions.
11264
11265 If this is done during an update, updated_window will contain the
11266 window that is being updated and the position is the future output
11267 cursor position for that window. If updated_window is null, use
11268 selected_window and display the cursor at the given position. */
11269
11270 void
11271 x_cursor_to (int vpos, int hpos, int y, int x)
11272 {
11273 struct window *w;
11274
11275 /* If updated_window is not set, work on selected_window. */
11276 if (updated_window)
11277 w = updated_window;
11278 else
11279 w = XWINDOW (selected_window);
11280
11281 /* Set the output cursor. */
11282 output_cursor.hpos = hpos;
11283 output_cursor.vpos = vpos;
11284 output_cursor.x = x;
11285 output_cursor.y = y;
11286
11287 /* If not called as part of an update, really display the cursor.
11288 This will also set the cursor position of W. */
11289 if (updated_window == NULL)
11290 {
11291 BLOCK_INPUT;
11292 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11293 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11294 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11295 UNBLOCK_INPUT;
11296 }
11297 }
11298
11299 #endif /* HAVE_WINDOW_SYSTEM */
11300
11301 \f
11302 /***********************************************************************
11303 Tool-bars
11304 ***********************************************************************/
11305
11306 #ifdef HAVE_WINDOW_SYSTEM
11307
11308 /* Where the mouse was last time we reported a mouse event. */
11309
11310 FRAME_PTR last_mouse_frame;
11311
11312 /* Tool-bar item index of the item on which a mouse button was pressed
11313 or -1. */
11314
11315 int last_tool_bar_item;
11316
11317
11318 static Lisp_Object
11319 update_tool_bar_unwind (Lisp_Object frame)
11320 {
11321 selected_frame = frame;
11322 return Qnil;
11323 }
11324
11325 /* Update the tool-bar item list for frame F. This has to be done
11326 before we start to fill in any display lines. Called from
11327 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11328 and restore it here. */
11329
11330 static void
11331 update_tool_bar (struct frame *f, int save_match_data)
11332 {
11333 #if defined (USE_GTK) || defined (HAVE_NS)
11334 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11335 #else
11336 int do_update = WINDOWP (f->tool_bar_window)
11337 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11338 #endif
11339
11340 if (do_update)
11341 {
11342 Lisp_Object window;
11343 struct window *w;
11344
11345 window = FRAME_SELECTED_WINDOW (f);
11346 w = XWINDOW (window);
11347
11348 /* If the user has switched buffers or windows, we need to
11349 recompute to reflect the new bindings. But we'll
11350 recompute when update_mode_lines is set too; that means
11351 that people can use force-mode-line-update to request
11352 that the menu bar be recomputed. The adverse effect on
11353 the rest of the redisplay algorithm is about the same as
11354 windows_or_buffers_changed anyway. */
11355 if (windows_or_buffers_changed
11356 || !NILP (w->update_mode_line)
11357 || update_mode_lines
11358 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11359 < BUF_MODIFF (XBUFFER (w->buffer)))
11360 != !NILP (w->last_had_star))
11361 || ((!NILP (Vtransient_mark_mode)
11362 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11363 != !NILP (w->region_showing)))
11364 {
11365 struct buffer *prev = current_buffer;
11366 ptrdiff_t count = SPECPDL_INDEX ();
11367 Lisp_Object frame, new_tool_bar;
11368 int new_n_tool_bar;
11369 struct gcpro gcpro1;
11370
11371 /* Set current_buffer to the buffer of the selected
11372 window of the frame, so that we get the right local
11373 keymaps. */
11374 set_buffer_internal_1 (XBUFFER (w->buffer));
11375
11376 /* Save match data, if we must. */
11377 if (save_match_data)
11378 record_unwind_save_match_data ();
11379
11380 /* Make sure that we don't accidentally use bogus keymaps. */
11381 if (NILP (Voverriding_local_map_menu_flag))
11382 {
11383 specbind (Qoverriding_terminal_local_map, Qnil);
11384 specbind (Qoverriding_local_map, Qnil);
11385 }
11386
11387 GCPRO1 (new_tool_bar);
11388
11389 /* We must temporarily set the selected frame to this frame
11390 before calling tool_bar_items, because the calculation of
11391 the tool-bar keymap uses the selected frame (see
11392 `tool-bar-make-keymap' in tool-bar.el). */
11393 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11394 XSETFRAME (frame, f);
11395 selected_frame = frame;
11396
11397 /* Build desired tool-bar items from keymaps. */
11398 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11399 &new_n_tool_bar);
11400
11401 /* Redisplay the tool-bar if we changed it. */
11402 if (new_n_tool_bar != f->n_tool_bar_items
11403 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11404 {
11405 /* Redisplay that happens asynchronously due to an expose event
11406 may access f->tool_bar_items. Make sure we update both
11407 variables within BLOCK_INPUT so no such event interrupts. */
11408 BLOCK_INPUT;
11409 f->tool_bar_items = new_tool_bar;
11410 f->n_tool_bar_items = new_n_tool_bar;
11411 w->update_mode_line = Qt;
11412 UNBLOCK_INPUT;
11413 }
11414
11415 UNGCPRO;
11416
11417 unbind_to (count, Qnil);
11418 set_buffer_internal_1 (prev);
11419 }
11420 }
11421 }
11422
11423
11424 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11425 F's desired tool-bar contents. F->tool_bar_items must have
11426 been set up previously by calling prepare_menu_bars. */
11427
11428 static void
11429 build_desired_tool_bar_string (struct frame *f)
11430 {
11431 int i, size, size_needed;
11432 struct gcpro gcpro1, gcpro2, gcpro3;
11433 Lisp_Object image, plist, props;
11434
11435 image = plist = props = Qnil;
11436 GCPRO3 (image, plist, props);
11437
11438 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11439 Otherwise, make a new string. */
11440
11441 /* The size of the string we might be able to reuse. */
11442 size = (STRINGP (f->desired_tool_bar_string)
11443 ? SCHARS (f->desired_tool_bar_string)
11444 : 0);
11445
11446 /* We need one space in the string for each image. */
11447 size_needed = f->n_tool_bar_items;
11448
11449 /* Reuse f->desired_tool_bar_string, if possible. */
11450 if (size < size_needed || NILP (f->desired_tool_bar_string))
11451 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11452 make_number (' '));
11453 else
11454 {
11455 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11456 Fremove_text_properties (make_number (0), make_number (size),
11457 props, f->desired_tool_bar_string);
11458 }
11459
11460 /* Put a `display' property on the string for the images to display,
11461 put a `menu_item' property on tool-bar items with a value that
11462 is the index of the item in F's tool-bar item vector. */
11463 for (i = 0; i < f->n_tool_bar_items; ++i)
11464 {
11465 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11466
11467 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11468 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11469 int hmargin, vmargin, relief, idx, end;
11470
11471 /* If image is a vector, choose the image according to the
11472 button state. */
11473 image = PROP (TOOL_BAR_ITEM_IMAGES);
11474 if (VECTORP (image))
11475 {
11476 if (enabled_p)
11477 idx = (selected_p
11478 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11479 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11480 else
11481 idx = (selected_p
11482 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11483 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11484
11485 xassert (ASIZE (image) >= idx);
11486 image = AREF (image, idx);
11487 }
11488 else
11489 idx = -1;
11490
11491 /* Ignore invalid image specifications. */
11492 if (!valid_image_p (image))
11493 continue;
11494
11495 /* Display the tool-bar button pressed, or depressed. */
11496 plist = Fcopy_sequence (XCDR (image));
11497
11498 /* Compute margin and relief to draw. */
11499 relief = (tool_bar_button_relief >= 0
11500 ? tool_bar_button_relief
11501 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11502 hmargin = vmargin = relief;
11503
11504 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11505 INT_MAX - max (hmargin, vmargin)))
11506 {
11507 hmargin += XFASTINT (Vtool_bar_button_margin);
11508 vmargin += XFASTINT (Vtool_bar_button_margin);
11509 }
11510 else if (CONSP (Vtool_bar_button_margin))
11511 {
11512 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11513 INT_MAX - hmargin))
11514 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11515
11516 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11517 INT_MAX - vmargin))
11518 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11519 }
11520
11521 if (auto_raise_tool_bar_buttons_p)
11522 {
11523 /* Add a `:relief' property to the image spec if the item is
11524 selected. */
11525 if (selected_p)
11526 {
11527 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11528 hmargin -= relief;
11529 vmargin -= relief;
11530 }
11531 }
11532 else
11533 {
11534 /* If image is selected, display it pressed, i.e. with a
11535 negative relief. If it's not selected, display it with a
11536 raised relief. */
11537 plist = Fplist_put (plist, QCrelief,
11538 (selected_p
11539 ? make_number (-relief)
11540 : make_number (relief)));
11541 hmargin -= relief;
11542 vmargin -= relief;
11543 }
11544
11545 /* Put a margin around the image. */
11546 if (hmargin || vmargin)
11547 {
11548 if (hmargin == vmargin)
11549 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11550 else
11551 plist = Fplist_put (plist, QCmargin,
11552 Fcons (make_number (hmargin),
11553 make_number (vmargin)));
11554 }
11555
11556 /* If button is not enabled, and we don't have special images
11557 for the disabled state, make the image appear disabled by
11558 applying an appropriate algorithm to it. */
11559 if (!enabled_p && idx < 0)
11560 plist = Fplist_put (plist, QCconversion, Qdisabled);
11561
11562 /* Put a `display' text property on the string for the image to
11563 display. Put a `menu-item' property on the string that gives
11564 the start of this item's properties in the tool-bar items
11565 vector. */
11566 image = Fcons (Qimage, plist);
11567 props = list4 (Qdisplay, image,
11568 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11569
11570 /* Let the last image hide all remaining spaces in the tool bar
11571 string. The string can be longer than needed when we reuse a
11572 previous string. */
11573 if (i + 1 == f->n_tool_bar_items)
11574 end = SCHARS (f->desired_tool_bar_string);
11575 else
11576 end = i + 1;
11577 Fadd_text_properties (make_number (i), make_number (end),
11578 props, f->desired_tool_bar_string);
11579 #undef PROP
11580 }
11581
11582 UNGCPRO;
11583 }
11584
11585
11586 /* Display one line of the tool-bar of frame IT->f.
11587
11588 HEIGHT specifies the desired height of the tool-bar line.
11589 If the actual height of the glyph row is less than HEIGHT, the
11590 row's height is increased to HEIGHT, and the icons are centered
11591 vertically in the new height.
11592
11593 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11594 count a final empty row in case the tool-bar width exactly matches
11595 the window width.
11596 */
11597
11598 static void
11599 display_tool_bar_line (struct it *it, int height)
11600 {
11601 struct glyph_row *row = it->glyph_row;
11602 int max_x = it->last_visible_x;
11603 struct glyph *last;
11604
11605 prepare_desired_row (row);
11606 row->y = it->current_y;
11607
11608 /* Note that this isn't made use of if the face hasn't a box,
11609 so there's no need to check the face here. */
11610 it->start_of_box_run_p = 1;
11611
11612 while (it->current_x < max_x)
11613 {
11614 int x, n_glyphs_before, i, nglyphs;
11615 struct it it_before;
11616
11617 /* Get the next display element. */
11618 if (!get_next_display_element (it))
11619 {
11620 /* Don't count empty row if we are counting needed tool-bar lines. */
11621 if (height < 0 && !it->hpos)
11622 return;
11623 break;
11624 }
11625
11626 /* Produce glyphs. */
11627 n_glyphs_before = row->used[TEXT_AREA];
11628 it_before = *it;
11629
11630 PRODUCE_GLYPHS (it);
11631
11632 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11633 i = 0;
11634 x = it_before.current_x;
11635 while (i < nglyphs)
11636 {
11637 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11638
11639 if (x + glyph->pixel_width > max_x)
11640 {
11641 /* Glyph doesn't fit on line. Backtrack. */
11642 row->used[TEXT_AREA] = n_glyphs_before;
11643 *it = it_before;
11644 /* If this is the only glyph on this line, it will never fit on the
11645 tool-bar, so skip it. But ensure there is at least one glyph,
11646 so we don't accidentally disable the tool-bar. */
11647 if (n_glyphs_before == 0
11648 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11649 break;
11650 goto out;
11651 }
11652
11653 ++it->hpos;
11654 x += glyph->pixel_width;
11655 ++i;
11656 }
11657
11658 /* Stop at line end. */
11659 if (ITERATOR_AT_END_OF_LINE_P (it))
11660 break;
11661
11662 set_iterator_to_next (it, 1);
11663 }
11664
11665 out:;
11666
11667 row->displays_text_p = row->used[TEXT_AREA] != 0;
11668
11669 /* Use default face for the border below the tool bar.
11670
11671 FIXME: When auto-resize-tool-bars is grow-only, there is
11672 no additional border below the possibly empty tool-bar lines.
11673 So to make the extra empty lines look "normal", we have to
11674 use the tool-bar face for the border too. */
11675 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11676 it->face_id = DEFAULT_FACE_ID;
11677
11678 extend_face_to_end_of_line (it);
11679 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11680 last->right_box_line_p = 1;
11681 if (last == row->glyphs[TEXT_AREA])
11682 last->left_box_line_p = 1;
11683
11684 /* Make line the desired height and center it vertically. */
11685 if ((height -= it->max_ascent + it->max_descent) > 0)
11686 {
11687 /* Don't add more than one line height. */
11688 height %= FRAME_LINE_HEIGHT (it->f);
11689 it->max_ascent += height / 2;
11690 it->max_descent += (height + 1) / 2;
11691 }
11692
11693 compute_line_metrics (it);
11694
11695 /* If line is empty, make it occupy the rest of the tool-bar. */
11696 if (!row->displays_text_p)
11697 {
11698 row->height = row->phys_height = it->last_visible_y - row->y;
11699 row->visible_height = row->height;
11700 row->ascent = row->phys_ascent = 0;
11701 row->extra_line_spacing = 0;
11702 }
11703
11704 row->full_width_p = 1;
11705 row->continued_p = 0;
11706 row->truncated_on_left_p = 0;
11707 row->truncated_on_right_p = 0;
11708
11709 it->current_x = it->hpos = 0;
11710 it->current_y += row->height;
11711 ++it->vpos;
11712 ++it->glyph_row;
11713 }
11714
11715
11716 /* Max tool-bar height. */
11717
11718 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11719 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11720
11721 /* Value is the number of screen lines needed to make all tool-bar
11722 items of frame F visible. The number of actual rows needed is
11723 returned in *N_ROWS if non-NULL. */
11724
11725 static int
11726 tool_bar_lines_needed (struct frame *f, int *n_rows)
11727 {
11728 struct window *w = XWINDOW (f->tool_bar_window);
11729 struct it it;
11730 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11731 the desired matrix, so use (unused) mode-line row as temporary row to
11732 avoid destroying the first tool-bar row. */
11733 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11734
11735 /* Initialize an iterator for iteration over
11736 F->desired_tool_bar_string in the tool-bar window of frame F. */
11737 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11738 it.first_visible_x = 0;
11739 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11740 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11741 it.paragraph_embedding = L2R;
11742
11743 while (!ITERATOR_AT_END_P (&it))
11744 {
11745 clear_glyph_row (temp_row);
11746 it.glyph_row = temp_row;
11747 display_tool_bar_line (&it, -1);
11748 }
11749 clear_glyph_row (temp_row);
11750
11751 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11752 if (n_rows)
11753 *n_rows = it.vpos > 0 ? it.vpos : -1;
11754
11755 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11756 }
11757
11758
11759 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11760 0, 1, 0,
11761 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11762 (Lisp_Object frame)
11763 {
11764 struct frame *f;
11765 struct window *w;
11766 int nlines = 0;
11767
11768 if (NILP (frame))
11769 frame = selected_frame;
11770 else
11771 CHECK_FRAME (frame);
11772 f = XFRAME (frame);
11773
11774 if (WINDOWP (f->tool_bar_window)
11775 && (w = XWINDOW (f->tool_bar_window),
11776 WINDOW_TOTAL_LINES (w) > 0))
11777 {
11778 update_tool_bar (f, 1);
11779 if (f->n_tool_bar_items)
11780 {
11781 build_desired_tool_bar_string (f);
11782 nlines = tool_bar_lines_needed (f, NULL);
11783 }
11784 }
11785
11786 return make_number (nlines);
11787 }
11788
11789
11790 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11791 height should be changed. */
11792
11793 static int
11794 redisplay_tool_bar (struct frame *f)
11795 {
11796 struct window *w;
11797 struct it it;
11798 struct glyph_row *row;
11799
11800 #if defined (USE_GTK) || defined (HAVE_NS)
11801 if (FRAME_EXTERNAL_TOOL_BAR (f))
11802 update_frame_tool_bar (f);
11803 return 0;
11804 #endif
11805
11806 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11807 do anything. This means you must start with tool-bar-lines
11808 non-zero to get the auto-sizing effect. Or in other words, you
11809 can turn off tool-bars by specifying tool-bar-lines zero. */
11810 if (!WINDOWP (f->tool_bar_window)
11811 || (w = XWINDOW (f->tool_bar_window),
11812 WINDOW_TOTAL_LINES (w) == 0))
11813 return 0;
11814
11815 /* Set up an iterator for the tool-bar window. */
11816 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11817 it.first_visible_x = 0;
11818 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11819 row = it.glyph_row;
11820
11821 /* Build a string that represents the contents of the tool-bar. */
11822 build_desired_tool_bar_string (f);
11823 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11824 /* FIXME: This should be controlled by a user option. But it
11825 doesn't make sense to have an R2L tool bar if the menu bar cannot
11826 be drawn also R2L, and making the menu bar R2L is tricky due
11827 toolkit-specific code that implements it. If an R2L tool bar is
11828 ever supported, display_tool_bar_line should also be augmented to
11829 call unproduce_glyphs like display_line and display_string
11830 do. */
11831 it.paragraph_embedding = L2R;
11832
11833 if (f->n_tool_bar_rows == 0)
11834 {
11835 int nlines;
11836
11837 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11838 nlines != WINDOW_TOTAL_LINES (w)))
11839 {
11840 Lisp_Object frame;
11841 int old_height = WINDOW_TOTAL_LINES (w);
11842
11843 XSETFRAME (frame, f);
11844 Fmodify_frame_parameters (frame,
11845 Fcons (Fcons (Qtool_bar_lines,
11846 make_number (nlines)),
11847 Qnil));
11848 if (WINDOW_TOTAL_LINES (w) != old_height)
11849 {
11850 clear_glyph_matrix (w->desired_matrix);
11851 fonts_changed_p = 1;
11852 return 1;
11853 }
11854 }
11855 }
11856
11857 /* Display as many lines as needed to display all tool-bar items. */
11858
11859 if (f->n_tool_bar_rows > 0)
11860 {
11861 int border, rows, height, extra;
11862
11863 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11864 border = XINT (Vtool_bar_border);
11865 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11866 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11867 else if (EQ (Vtool_bar_border, Qborder_width))
11868 border = f->border_width;
11869 else
11870 border = 0;
11871 if (border < 0)
11872 border = 0;
11873
11874 rows = f->n_tool_bar_rows;
11875 height = max (1, (it.last_visible_y - border) / rows);
11876 extra = it.last_visible_y - border - height * rows;
11877
11878 while (it.current_y < it.last_visible_y)
11879 {
11880 int h = 0;
11881 if (extra > 0 && rows-- > 0)
11882 {
11883 h = (extra + rows - 1) / rows;
11884 extra -= h;
11885 }
11886 display_tool_bar_line (&it, height + h);
11887 }
11888 }
11889 else
11890 {
11891 while (it.current_y < it.last_visible_y)
11892 display_tool_bar_line (&it, 0);
11893 }
11894
11895 /* It doesn't make much sense to try scrolling in the tool-bar
11896 window, so don't do it. */
11897 w->desired_matrix->no_scrolling_p = 1;
11898 w->must_be_updated_p = 1;
11899
11900 if (!NILP (Vauto_resize_tool_bars))
11901 {
11902 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11903 int change_height_p = 0;
11904
11905 /* If we couldn't display everything, change the tool-bar's
11906 height if there is room for more. */
11907 if (IT_STRING_CHARPOS (it) < it.end_charpos
11908 && it.current_y < max_tool_bar_height)
11909 change_height_p = 1;
11910
11911 row = it.glyph_row - 1;
11912
11913 /* If there are blank lines at the end, except for a partially
11914 visible blank line at the end that is smaller than
11915 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11916 if (!row->displays_text_p
11917 && row->height >= FRAME_LINE_HEIGHT (f))
11918 change_height_p = 1;
11919
11920 /* If row displays tool-bar items, but is partially visible,
11921 change the tool-bar's height. */
11922 if (row->displays_text_p
11923 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11924 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11925 change_height_p = 1;
11926
11927 /* Resize windows as needed by changing the `tool-bar-lines'
11928 frame parameter. */
11929 if (change_height_p)
11930 {
11931 Lisp_Object frame;
11932 int old_height = WINDOW_TOTAL_LINES (w);
11933 int nrows;
11934 int nlines = tool_bar_lines_needed (f, &nrows);
11935
11936 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11937 && !f->minimize_tool_bar_window_p)
11938 ? (nlines > old_height)
11939 : (nlines != old_height));
11940 f->minimize_tool_bar_window_p = 0;
11941
11942 if (change_height_p)
11943 {
11944 XSETFRAME (frame, f);
11945 Fmodify_frame_parameters (frame,
11946 Fcons (Fcons (Qtool_bar_lines,
11947 make_number (nlines)),
11948 Qnil));
11949 if (WINDOW_TOTAL_LINES (w) != old_height)
11950 {
11951 clear_glyph_matrix (w->desired_matrix);
11952 f->n_tool_bar_rows = nrows;
11953 fonts_changed_p = 1;
11954 return 1;
11955 }
11956 }
11957 }
11958 }
11959
11960 f->minimize_tool_bar_window_p = 0;
11961 return 0;
11962 }
11963
11964
11965 /* Get information about the tool-bar item which is displayed in GLYPH
11966 on frame F. Return in *PROP_IDX the index where tool-bar item
11967 properties start in F->tool_bar_items. Value is zero if
11968 GLYPH doesn't display a tool-bar item. */
11969
11970 static int
11971 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11972 {
11973 Lisp_Object prop;
11974 int success_p;
11975 int charpos;
11976
11977 /* This function can be called asynchronously, which means we must
11978 exclude any possibility that Fget_text_property signals an
11979 error. */
11980 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11981 charpos = max (0, charpos);
11982
11983 /* Get the text property `menu-item' at pos. The value of that
11984 property is the start index of this item's properties in
11985 F->tool_bar_items. */
11986 prop = Fget_text_property (make_number (charpos),
11987 Qmenu_item, f->current_tool_bar_string);
11988 if (INTEGERP (prop))
11989 {
11990 *prop_idx = XINT (prop);
11991 success_p = 1;
11992 }
11993 else
11994 success_p = 0;
11995
11996 return success_p;
11997 }
11998
11999 \f
12000 /* Get information about the tool-bar item at position X/Y on frame F.
12001 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12002 the current matrix of the tool-bar window of F, or NULL if not
12003 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12004 item in F->tool_bar_items. Value is
12005
12006 -1 if X/Y is not on a tool-bar item
12007 0 if X/Y is on the same item that was highlighted before.
12008 1 otherwise. */
12009
12010 static int
12011 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12012 int *hpos, int *vpos, int *prop_idx)
12013 {
12014 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12015 struct window *w = XWINDOW (f->tool_bar_window);
12016 int area;
12017
12018 /* Find the glyph under X/Y. */
12019 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12020 if (*glyph == NULL)
12021 return -1;
12022
12023 /* Get the start of this tool-bar item's properties in
12024 f->tool_bar_items. */
12025 if (!tool_bar_item_info (f, *glyph, prop_idx))
12026 return -1;
12027
12028 /* Is mouse on the highlighted item? */
12029 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12030 && *vpos >= hlinfo->mouse_face_beg_row
12031 && *vpos <= hlinfo->mouse_face_end_row
12032 && (*vpos > hlinfo->mouse_face_beg_row
12033 || *hpos >= hlinfo->mouse_face_beg_col)
12034 && (*vpos < hlinfo->mouse_face_end_row
12035 || *hpos < hlinfo->mouse_face_end_col
12036 || hlinfo->mouse_face_past_end))
12037 return 0;
12038
12039 return 1;
12040 }
12041
12042
12043 /* EXPORT:
12044 Handle mouse button event on the tool-bar of frame F, at
12045 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12046 0 for button release. MODIFIERS is event modifiers for button
12047 release. */
12048
12049 void
12050 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12051 int modifiers)
12052 {
12053 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12054 struct window *w = XWINDOW (f->tool_bar_window);
12055 int hpos, vpos, prop_idx;
12056 struct glyph *glyph;
12057 Lisp_Object enabled_p;
12058
12059 /* If not on the highlighted tool-bar item, return. */
12060 frame_to_window_pixel_xy (w, &x, &y);
12061 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12062 return;
12063
12064 /* If item is disabled, do nothing. */
12065 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12066 if (NILP (enabled_p))
12067 return;
12068
12069 if (down_p)
12070 {
12071 /* Show item in pressed state. */
12072 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12073 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12074 last_tool_bar_item = prop_idx;
12075 }
12076 else
12077 {
12078 Lisp_Object key, frame;
12079 struct input_event event;
12080 EVENT_INIT (event);
12081
12082 /* Show item in released state. */
12083 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12084 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12085
12086 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12087
12088 XSETFRAME (frame, f);
12089 event.kind = TOOL_BAR_EVENT;
12090 event.frame_or_window = frame;
12091 event.arg = frame;
12092 kbd_buffer_store_event (&event);
12093
12094 event.kind = TOOL_BAR_EVENT;
12095 event.frame_or_window = frame;
12096 event.arg = key;
12097 event.modifiers = modifiers;
12098 kbd_buffer_store_event (&event);
12099 last_tool_bar_item = -1;
12100 }
12101 }
12102
12103
12104 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12105 tool-bar window-relative coordinates X/Y. Called from
12106 note_mouse_highlight. */
12107
12108 static void
12109 note_tool_bar_highlight (struct frame *f, int x, int y)
12110 {
12111 Lisp_Object window = f->tool_bar_window;
12112 struct window *w = XWINDOW (window);
12113 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12114 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12115 int hpos, vpos;
12116 struct glyph *glyph;
12117 struct glyph_row *row;
12118 int i;
12119 Lisp_Object enabled_p;
12120 int prop_idx;
12121 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12122 int mouse_down_p, rc;
12123
12124 /* Function note_mouse_highlight is called with negative X/Y
12125 values when mouse moves outside of the frame. */
12126 if (x <= 0 || y <= 0)
12127 {
12128 clear_mouse_face (hlinfo);
12129 return;
12130 }
12131
12132 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12133 if (rc < 0)
12134 {
12135 /* Not on tool-bar item. */
12136 clear_mouse_face (hlinfo);
12137 return;
12138 }
12139 else if (rc == 0)
12140 /* On same tool-bar item as before. */
12141 goto set_help_echo;
12142
12143 clear_mouse_face (hlinfo);
12144
12145 /* Mouse is down, but on different tool-bar item? */
12146 mouse_down_p = (dpyinfo->grabbed
12147 && f == last_mouse_frame
12148 && FRAME_LIVE_P (f));
12149 if (mouse_down_p
12150 && last_tool_bar_item != prop_idx)
12151 return;
12152
12153 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12154 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12155
12156 /* If tool-bar item is not enabled, don't highlight it. */
12157 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12158 if (!NILP (enabled_p))
12159 {
12160 /* Compute the x-position of the glyph. In front and past the
12161 image is a space. We include this in the highlighted area. */
12162 row = MATRIX_ROW (w->current_matrix, vpos);
12163 for (i = x = 0; i < hpos; ++i)
12164 x += row->glyphs[TEXT_AREA][i].pixel_width;
12165
12166 /* Record this as the current active region. */
12167 hlinfo->mouse_face_beg_col = hpos;
12168 hlinfo->mouse_face_beg_row = vpos;
12169 hlinfo->mouse_face_beg_x = x;
12170 hlinfo->mouse_face_beg_y = row->y;
12171 hlinfo->mouse_face_past_end = 0;
12172
12173 hlinfo->mouse_face_end_col = hpos + 1;
12174 hlinfo->mouse_face_end_row = vpos;
12175 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12176 hlinfo->mouse_face_end_y = row->y;
12177 hlinfo->mouse_face_window = window;
12178 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12179
12180 /* Display it as active. */
12181 show_mouse_face (hlinfo, draw);
12182 hlinfo->mouse_face_image_state = draw;
12183 }
12184
12185 set_help_echo:
12186
12187 /* Set help_echo_string to a help string to display for this tool-bar item.
12188 XTread_socket does the rest. */
12189 help_echo_object = help_echo_window = Qnil;
12190 help_echo_pos = -1;
12191 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12192 if (NILP (help_echo_string))
12193 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12194 }
12195
12196 #endif /* HAVE_WINDOW_SYSTEM */
12197
12198
12199 \f
12200 /************************************************************************
12201 Horizontal scrolling
12202 ************************************************************************/
12203
12204 static int hscroll_window_tree (Lisp_Object);
12205 static int hscroll_windows (Lisp_Object);
12206
12207 /* For all leaf windows in the window tree rooted at WINDOW, set their
12208 hscroll value so that PT is (i) visible in the window, and (ii) so
12209 that it is not within a certain margin at the window's left and
12210 right border. Value is non-zero if any window's hscroll has been
12211 changed. */
12212
12213 static int
12214 hscroll_window_tree (Lisp_Object window)
12215 {
12216 int hscrolled_p = 0;
12217 int hscroll_relative_p = FLOATP (Vhscroll_step);
12218 int hscroll_step_abs = 0;
12219 double hscroll_step_rel = 0;
12220
12221 if (hscroll_relative_p)
12222 {
12223 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12224 if (hscroll_step_rel < 0)
12225 {
12226 hscroll_relative_p = 0;
12227 hscroll_step_abs = 0;
12228 }
12229 }
12230 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12231 {
12232 hscroll_step_abs = XINT (Vhscroll_step);
12233 if (hscroll_step_abs < 0)
12234 hscroll_step_abs = 0;
12235 }
12236 else
12237 hscroll_step_abs = 0;
12238
12239 while (WINDOWP (window))
12240 {
12241 struct window *w = XWINDOW (window);
12242
12243 if (WINDOWP (w->hchild))
12244 hscrolled_p |= hscroll_window_tree (w->hchild);
12245 else if (WINDOWP (w->vchild))
12246 hscrolled_p |= hscroll_window_tree (w->vchild);
12247 else if (w->cursor.vpos >= 0)
12248 {
12249 int h_margin;
12250 int text_area_width;
12251 struct glyph_row *current_cursor_row
12252 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12253 struct glyph_row *desired_cursor_row
12254 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12255 struct glyph_row *cursor_row
12256 = (desired_cursor_row->enabled_p
12257 ? desired_cursor_row
12258 : current_cursor_row);
12259 int row_r2l_p = cursor_row->reversed_p;
12260
12261 text_area_width = window_box_width (w, TEXT_AREA);
12262
12263 /* Scroll when cursor is inside this scroll margin. */
12264 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12265
12266 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12267 /* For left-to-right rows, hscroll when cursor is either
12268 (i) inside the right hscroll margin, or (ii) if it is
12269 inside the left margin and the window is already
12270 hscrolled. */
12271 && ((!row_r2l_p
12272 && ((XFASTINT (w->hscroll)
12273 && w->cursor.x <= h_margin)
12274 || (cursor_row->enabled_p
12275 && cursor_row->truncated_on_right_p
12276 && (w->cursor.x >= text_area_width - h_margin))))
12277 /* For right-to-left rows, the logic is similar,
12278 except that rules for scrolling to left and right
12279 are reversed. E.g., if cursor.x <= h_margin, we
12280 need to hscroll "to the right" unconditionally,
12281 and that will scroll the screen to the left so as
12282 to reveal the next portion of the row. */
12283 || (row_r2l_p
12284 && ((cursor_row->enabled_p
12285 /* FIXME: It is confusing to set the
12286 truncated_on_right_p flag when R2L rows
12287 are actually truncated on the left. */
12288 && cursor_row->truncated_on_right_p
12289 && w->cursor.x <= h_margin)
12290 || (XFASTINT (w->hscroll)
12291 && (w->cursor.x >= text_area_width - h_margin))))))
12292 {
12293 struct it it;
12294 ptrdiff_t hscroll;
12295 struct buffer *saved_current_buffer;
12296 ptrdiff_t pt;
12297 int wanted_x;
12298
12299 /* Find point in a display of infinite width. */
12300 saved_current_buffer = current_buffer;
12301 current_buffer = XBUFFER (w->buffer);
12302
12303 if (w == XWINDOW (selected_window))
12304 pt = PT;
12305 else
12306 {
12307 pt = marker_position (w->pointm);
12308 pt = max (BEGV, pt);
12309 pt = min (ZV, pt);
12310 }
12311
12312 /* Move iterator to pt starting at cursor_row->start in
12313 a line with infinite width. */
12314 init_to_row_start (&it, w, cursor_row);
12315 it.last_visible_x = INFINITY;
12316 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12317 current_buffer = saved_current_buffer;
12318
12319 /* Position cursor in window. */
12320 if (!hscroll_relative_p && hscroll_step_abs == 0)
12321 hscroll = max (0, (it.current_x
12322 - (ITERATOR_AT_END_OF_LINE_P (&it)
12323 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12324 : (text_area_width / 2))))
12325 / FRAME_COLUMN_WIDTH (it.f);
12326 else if ((!row_r2l_p
12327 && w->cursor.x >= text_area_width - h_margin)
12328 || (row_r2l_p && w->cursor.x <= h_margin))
12329 {
12330 if (hscroll_relative_p)
12331 wanted_x = text_area_width * (1 - hscroll_step_rel)
12332 - h_margin;
12333 else
12334 wanted_x = text_area_width
12335 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12336 - h_margin;
12337 hscroll
12338 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12339 }
12340 else
12341 {
12342 if (hscroll_relative_p)
12343 wanted_x = text_area_width * hscroll_step_rel
12344 + h_margin;
12345 else
12346 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12347 + h_margin;
12348 hscroll
12349 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12350 }
12351 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12352
12353 /* Don't prevent redisplay optimizations if hscroll
12354 hasn't changed, as it will unnecessarily slow down
12355 redisplay. */
12356 if (XFASTINT (w->hscroll) != hscroll)
12357 {
12358 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12359 w->hscroll = make_number (hscroll);
12360 hscrolled_p = 1;
12361 }
12362 }
12363 }
12364
12365 window = w->next;
12366 }
12367
12368 /* Value is non-zero if hscroll of any leaf window has been changed. */
12369 return hscrolled_p;
12370 }
12371
12372
12373 /* Set hscroll so that cursor is visible and not inside horizontal
12374 scroll margins for all windows in the tree rooted at WINDOW. See
12375 also hscroll_window_tree above. Value is non-zero if any window's
12376 hscroll has been changed. If it has, desired matrices on the frame
12377 of WINDOW are cleared. */
12378
12379 static int
12380 hscroll_windows (Lisp_Object window)
12381 {
12382 int hscrolled_p = hscroll_window_tree (window);
12383 if (hscrolled_p)
12384 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12385 return hscrolled_p;
12386 }
12387
12388
12389 \f
12390 /************************************************************************
12391 Redisplay
12392 ************************************************************************/
12393
12394 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12395 to a non-zero value. This is sometimes handy to have in a debugger
12396 session. */
12397
12398 #if GLYPH_DEBUG
12399
12400 /* First and last unchanged row for try_window_id. */
12401
12402 static int debug_first_unchanged_at_end_vpos;
12403 static int debug_last_unchanged_at_beg_vpos;
12404
12405 /* Delta vpos and y. */
12406
12407 static int debug_dvpos, debug_dy;
12408
12409 /* Delta in characters and bytes for try_window_id. */
12410
12411 static ptrdiff_t debug_delta, debug_delta_bytes;
12412
12413 /* Values of window_end_pos and window_end_vpos at the end of
12414 try_window_id. */
12415
12416 static ptrdiff_t debug_end_vpos;
12417
12418 /* Append a string to W->desired_matrix->method. FMT is a printf
12419 format string. If trace_redisplay_p is non-zero also printf the
12420 resulting string to stderr. */
12421
12422 static void debug_method_add (struct window *, char const *, ...)
12423 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12424
12425 static void
12426 debug_method_add (struct window *w, char const *fmt, ...)
12427 {
12428 char buffer[512];
12429 char *method = w->desired_matrix->method;
12430 int len = strlen (method);
12431 int size = sizeof w->desired_matrix->method;
12432 int remaining = size - len - 1;
12433 va_list ap;
12434
12435 va_start (ap, fmt);
12436 vsprintf (buffer, fmt, ap);
12437 va_end (ap);
12438 if (len && remaining)
12439 {
12440 method[len] = '|';
12441 --remaining, ++len;
12442 }
12443
12444 strncpy (method + len, buffer, remaining);
12445
12446 if (trace_redisplay_p)
12447 fprintf (stderr, "%p (%s): %s\n",
12448 w,
12449 ((BUFFERP (w->buffer)
12450 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12451 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12452 : "no buffer"),
12453 buffer);
12454 }
12455
12456 #endif /* GLYPH_DEBUG */
12457
12458
12459 /* Value is non-zero if all changes in window W, which displays
12460 current_buffer, are in the text between START and END. START is a
12461 buffer position, END is given as a distance from Z. Used in
12462 redisplay_internal for display optimization. */
12463
12464 static inline int
12465 text_outside_line_unchanged_p (struct window *w,
12466 ptrdiff_t start, ptrdiff_t end)
12467 {
12468 int unchanged_p = 1;
12469
12470 /* If text or overlays have changed, see where. */
12471 if (XFASTINT (w->last_modified) < MODIFF
12472 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12473 {
12474 /* Gap in the line? */
12475 if (GPT < start || Z - GPT < end)
12476 unchanged_p = 0;
12477
12478 /* Changes start in front of the line, or end after it? */
12479 if (unchanged_p
12480 && (BEG_UNCHANGED < start - 1
12481 || END_UNCHANGED < end))
12482 unchanged_p = 0;
12483
12484 /* If selective display, can't optimize if changes start at the
12485 beginning of the line. */
12486 if (unchanged_p
12487 && INTEGERP (BVAR (current_buffer, selective_display))
12488 && XINT (BVAR (current_buffer, selective_display)) > 0
12489 && (BEG_UNCHANGED < start || GPT <= start))
12490 unchanged_p = 0;
12491
12492 /* If there are overlays at the start or end of the line, these
12493 may have overlay strings with newlines in them. A change at
12494 START, for instance, may actually concern the display of such
12495 overlay strings as well, and they are displayed on different
12496 lines. So, quickly rule out this case. (For the future, it
12497 might be desirable to implement something more telling than
12498 just BEG/END_UNCHANGED.) */
12499 if (unchanged_p)
12500 {
12501 if (BEG + BEG_UNCHANGED == start
12502 && overlay_touches_p (start))
12503 unchanged_p = 0;
12504 if (END_UNCHANGED == end
12505 && overlay_touches_p (Z - end))
12506 unchanged_p = 0;
12507 }
12508
12509 /* Under bidi reordering, adding or deleting a character in the
12510 beginning of a paragraph, before the first strong directional
12511 character, can change the base direction of the paragraph (unless
12512 the buffer specifies a fixed paragraph direction), which will
12513 require to redisplay the whole paragraph. It might be worthwhile
12514 to find the paragraph limits and widen the range of redisplayed
12515 lines to that, but for now just give up this optimization. */
12516 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12517 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12518 unchanged_p = 0;
12519 }
12520
12521 return unchanged_p;
12522 }
12523
12524
12525 /* Do a frame update, taking possible shortcuts into account. This is
12526 the main external entry point for redisplay.
12527
12528 If the last redisplay displayed an echo area message and that message
12529 is no longer requested, we clear the echo area or bring back the
12530 mini-buffer if that is in use. */
12531
12532 void
12533 redisplay (void)
12534 {
12535 redisplay_internal ();
12536 }
12537
12538
12539 static Lisp_Object
12540 overlay_arrow_string_or_property (Lisp_Object var)
12541 {
12542 Lisp_Object val;
12543
12544 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12545 return val;
12546
12547 return Voverlay_arrow_string;
12548 }
12549
12550 /* Return 1 if there are any overlay-arrows in current_buffer. */
12551 static int
12552 overlay_arrow_in_current_buffer_p (void)
12553 {
12554 Lisp_Object vlist;
12555
12556 for (vlist = Voverlay_arrow_variable_list;
12557 CONSP (vlist);
12558 vlist = XCDR (vlist))
12559 {
12560 Lisp_Object var = XCAR (vlist);
12561 Lisp_Object val;
12562
12563 if (!SYMBOLP (var))
12564 continue;
12565 val = find_symbol_value (var);
12566 if (MARKERP (val)
12567 && current_buffer == XMARKER (val)->buffer)
12568 return 1;
12569 }
12570 return 0;
12571 }
12572
12573
12574 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12575 has changed. */
12576
12577 static int
12578 overlay_arrows_changed_p (void)
12579 {
12580 Lisp_Object vlist;
12581
12582 for (vlist = Voverlay_arrow_variable_list;
12583 CONSP (vlist);
12584 vlist = XCDR (vlist))
12585 {
12586 Lisp_Object var = XCAR (vlist);
12587 Lisp_Object val, pstr;
12588
12589 if (!SYMBOLP (var))
12590 continue;
12591 val = find_symbol_value (var);
12592 if (!MARKERP (val))
12593 continue;
12594 if (! EQ (COERCE_MARKER (val),
12595 Fget (var, Qlast_arrow_position))
12596 || ! (pstr = overlay_arrow_string_or_property (var),
12597 EQ (pstr, Fget (var, Qlast_arrow_string))))
12598 return 1;
12599 }
12600 return 0;
12601 }
12602
12603 /* Mark overlay arrows to be updated on next redisplay. */
12604
12605 static void
12606 update_overlay_arrows (int up_to_date)
12607 {
12608 Lisp_Object vlist;
12609
12610 for (vlist = Voverlay_arrow_variable_list;
12611 CONSP (vlist);
12612 vlist = XCDR (vlist))
12613 {
12614 Lisp_Object var = XCAR (vlist);
12615
12616 if (!SYMBOLP (var))
12617 continue;
12618
12619 if (up_to_date > 0)
12620 {
12621 Lisp_Object val = find_symbol_value (var);
12622 Fput (var, Qlast_arrow_position,
12623 COERCE_MARKER (val));
12624 Fput (var, Qlast_arrow_string,
12625 overlay_arrow_string_or_property (var));
12626 }
12627 else if (up_to_date < 0
12628 || !NILP (Fget (var, Qlast_arrow_position)))
12629 {
12630 Fput (var, Qlast_arrow_position, Qt);
12631 Fput (var, Qlast_arrow_string, Qt);
12632 }
12633 }
12634 }
12635
12636
12637 /* Return overlay arrow string to display at row.
12638 Return integer (bitmap number) for arrow bitmap in left fringe.
12639 Return nil if no overlay arrow. */
12640
12641 static Lisp_Object
12642 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12643 {
12644 Lisp_Object vlist;
12645
12646 for (vlist = Voverlay_arrow_variable_list;
12647 CONSP (vlist);
12648 vlist = XCDR (vlist))
12649 {
12650 Lisp_Object var = XCAR (vlist);
12651 Lisp_Object val;
12652
12653 if (!SYMBOLP (var))
12654 continue;
12655
12656 val = find_symbol_value (var);
12657
12658 if (MARKERP (val)
12659 && current_buffer == XMARKER (val)->buffer
12660 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12661 {
12662 if (FRAME_WINDOW_P (it->f)
12663 /* FIXME: if ROW->reversed_p is set, this should test
12664 the right fringe, not the left one. */
12665 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12666 {
12667 #ifdef HAVE_WINDOW_SYSTEM
12668 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12669 {
12670 int fringe_bitmap;
12671 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12672 return make_number (fringe_bitmap);
12673 }
12674 #endif
12675 return make_number (-1); /* Use default arrow bitmap */
12676 }
12677 return overlay_arrow_string_or_property (var);
12678 }
12679 }
12680
12681 return Qnil;
12682 }
12683
12684 /* Return 1 if point moved out of or into a composition. Otherwise
12685 return 0. PREV_BUF and PREV_PT are the last point buffer and
12686 position. BUF and PT are the current point buffer and position. */
12687
12688 static int
12689 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12690 struct buffer *buf, ptrdiff_t pt)
12691 {
12692 ptrdiff_t start, end;
12693 Lisp_Object prop;
12694 Lisp_Object buffer;
12695
12696 XSETBUFFER (buffer, buf);
12697 /* Check a composition at the last point if point moved within the
12698 same buffer. */
12699 if (prev_buf == buf)
12700 {
12701 if (prev_pt == pt)
12702 /* Point didn't move. */
12703 return 0;
12704
12705 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12706 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12707 && COMPOSITION_VALID_P (start, end, prop)
12708 && start < prev_pt && end > prev_pt)
12709 /* The last point was within the composition. Return 1 iff
12710 point moved out of the composition. */
12711 return (pt <= start || pt >= end);
12712 }
12713
12714 /* Check a composition at the current point. */
12715 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12716 && find_composition (pt, -1, &start, &end, &prop, buffer)
12717 && COMPOSITION_VALID_P (start, end, prop)
12718 && start < pt && end > pt);
12719 }
12720
12721
12722 /* Reconsider the setting of B->clip_changed which is displayed
12723 in window W. */
12724
12725 static inline void
12726 reconsider_clip_changes (struct window *w, struct buffer *b)
12727 {
12728 if (b->clip_changed
12729 && !NILP (w->window_end_valid)
12730 && w->current_matrix->buffer == b
12731 && w->current_matrix->zv == BUF_ZV (b)
12732 && w->current_matrix->begv == BUF_BEGV (b))
12733 b->clip_changed = 0;
12734
12735 /* If display wasn't paused, and W is not a tool bar window, see if
12736 point has been moved into or out of a composition. In that case,
12737 we set b->clip_changed to 1 to force updating the screen. If
12738 b->clip_changed has already been set to 1, we can skip this
12739 check. */
12740 if (!b->clip_changed
12741 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12742 {
12743 ptrdiff_t pt;
12744
12745 if (w == XWINDOW (selected_window))
12746 pt = PT;
12747 else
12748 pt = marker_position (w->pointm);
12749
12750 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12751 || pt != XINT (w->last_point))
12752 && check_point_in_composition (w->current_matrix->buffer,
12753 XINT (w->last_point),
12754 XBUFFER (w->buffer), pt))
12755 b->clip_changed = 1;
12756 }
12757 }
12758 \f
12759
12760 /* Select FRAME to forward the values of frame-local variables into C
12761 variables so that the redisplay routines can access those values
12762 directly. */
12763
12764 static void
12765 select_frame_for_redisplay (Lisp_Object frame)
12766 {
12767 Lisp_Object tail, tem;
12768 Lisp_Object old = selected_frame;
12769 struct Lisp_Symbol *sym;
12770
12771 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12772
12773 selected_frame = frame;
12774
12775 do {
12776 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12777 if (CONSP (XCAR (tail))
12778 && (tem = XCAR (XCAR (tail)),
12779 SYMBOLP (tem))
12780 && (sym = indirect_variable (XSYMBOL (tem)),
12781 sym->redirect == SYMBOL_LOCALIZED)
12782 && sym->val.blv->frame_local)
12783 /* Use find_symbol_value rather than Fsymbol_value
12784 to avoid an error if it is void. */
12785 find_symbol_value (tem);
12786 } while (!EQ (frame, old) && (frame = old, 1));
12787 }
12788
12789
12790 #define STOP_POLLING \
12791 do { if (! polling_stopped_here) stop_polling (); \
12792 polling_stopped_here = 1; } while (0)
12793
12794 #define RESUME_POLLING \
12795 do { if (polling_stopped_here) start_polling (); \
12796 polling_stopped_here = 0; } while (0)
12797
12798
12799 /* Perhaps in the future avoid recentering windows if it
12800 is not necessary; currently that causes some problems. */
12801
12802 static void
12803 redisplay_internal (void)
12804 {
12805 struct window *w = XWINDOW (selected_window);
12806 struct window *sw;
12807 struct frame *fr;
12808 int pending;
12809 int must_finish = 0;
12810 struct text_pos tlbufpos, tlendpos;
12811 int number_of_visible_frames;
12812 ptrdiff_t count, count1;
12813 struct frame *sf;
12814 int polling_stopped_here = 0;
12815 Lisp_Object old_frame = selected_frame;
12816
12817 /* Non-zero means redisplay has to consider all windows on all
12818 frames. Zero means, only selected_window is considered. */
12819 int consider_all_windows_p;
12820
12821 /* Non-zero means redisplay has to redisplay the miniwindow */
12822 int update_miniwindow_p = 0;
12823
12824 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12825
12826 /* No redisplay if running in batch mode or frame is not yet fully
12827 initialized, or redisplay is explicitly turned off by setting
12828 Vinhibit_redisplay. */
12829 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12830 || !NILP (Vinhibit_redisplay))
12831 return;
12832
12833 /* Don't examine these until after testing Vinhibit_redisplay.
12834 When Emacs is shutting down, perhaps because its connection to
12835 X has dropped, we should not look at them at all. */
12836 fr = XFRAME (w->frame);
12837 sf = SELECTED_FRAME ();
12838
12839 if (!fr->glyphs_initialized_p)
12840 return;
12841
12842 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12843 if (popup_activated ())
12844 return;
12845 #endif
12846
12847 /* I don't think this happens but let's be paranoid. */
12848 if (redisplaying_p)
12849 return;
12850
12851 /* Record a function that resets redisplaying_p to its old value
12852 when we leave this function. */
12853 count = SPECPDL_INDEX ();
12854 record_unwind_protect (unwind_redisplay,
12855 Fcons (make_number (redisplaying_p), selected_frame));
12856 ++redisplaying_p;
12857 specbind (Qinhibit_free_realized_faces, Qnil);
12858
12859 {
12860 Lisp_Object tail, frame;
12861
12862 FOR_EACH_FRAME (tail, frame)
12863 {
12864 struct frame *f = XFRAME (frame);
12865 f->already_hscrolled_p = 0;
12866 }
12867 }
12868
12869 retry:
12870 /* Remember the currently selected window. */
12871 sw = w;
12872
12873 if (!EQ (old_frame, selected_frame)
12874 && FRAME_LIVE_P (XFRAME (old_frame)))
12875 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12876 selected_frame and selected_window to be temporarily out-of-sync so
12877 when we come back here via `goto retry', we need to resync because we
12878 may need to run Elisp code (via prepare_menu_bars). */
12879 select_frame_for_redisplay (old_frame);
12880
12881 pending = 0;
12882 reconsider_clip_changes (w, current_buffer);
12883 last_escape_glyph_frame = NULL;
12884 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12885 last_glyphless_glyph_frame = NULL;
12886 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12887
12888 /* If new fonts have been loaded that make a glyph matrix adjustment
12889 necessary, do it. */
12890 if (fonts_changed_p)
12891 {
12892 adjust_glyphs (NULL);
12893 ++windows_or_buffers_changed;
12894 fonts_changed_p = 0;
12895 }
12896
12897 /* If face_change_count is non-zero, init_iterator will free all
12898 realized faces, which includes the faces referenced from current
12899 matrices. So, we can't reuse current matrices in this case. */
12900 if (face_change_count)
12901 ++windows_or_buffers_changed;
12902
12903 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12904 && FRAME_TTY (sf)->previous_frame != sf)
12905 {
12906 /* Since frames on a single ASCII terminal share the same
12907 display area, displaying a different frame means redisplay
12908 the whole thing. */
12909 windows_or_buffers_changed++;
12910 SET_FRAME_GARBAGED (sf);
12911 #ifndef DOS_NT
12912 set_tty_color_mode (FRAME_TTY (sf), sf);
12913 #endif
12914 FRAME_TTY (sf)->previous_frame = sf;
12915 }
12916
12917 /* Set the visible flags for all frames. Do this before checking
12918 for resized or garbaged frames; they want to know if their frames
12919 are visible. See the comment in frame.h for
12920 FRAME_SAMPLE_VISIBILITY. */
12921 {
12922 Lisp_Object tail, frame;
12923
12924 number_of_visible_frames = 0;
12925
12926 FOR_EACH_FRAME (tail, frame)
12927 {
12928 struct frame *f = XFRAME (frame);
12929
12930 FRAME_SAMPLE_VISIBILITY (f);
12931 if (FRAME_VISIBLE_P (f))
12932 ++number_of_visible_frames;
12933 clear_desired_matrices (f);
12934 }
12935 }
12936
12937 /* Notice any pending interrupt request to change frame size. */
12938 do_pending_window_change (1);
12939
12940 /* do_pending_window_change could change the selected_window due to
12941 frame resizing which makes the selected window too small. */
12942 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12943 {
12944 sw = w;
12945 reconsider_clip_changes (w, current_buffer);
12946 }
12947
12948 /* Clear frames marked as garbaged. */
12949 if (frame_garbaged)
12950 clear_garbaged_frames ();
12951
12952 /* Build menubar and tool-bar items. */
12953 if (NILP (Vmemory_full))
12954 prepare_menu_bars ();
12955
12956 if (windows_or_buffers_changed)
12957 update_mode_lines++;
12958
12959 /* Detect case that we need to write or remove a star in the mode line. */
12960 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12961 {
12962 w->update_mode_line = Qt;
12963 if (buffer_shared > 1)
12964 update_mode_lines++;
12965 }
12966
12967 /* Avoid invocation of point motion hooks by `current_column' below. */
12968 count1 = SPECPDL_INDEX ();
12969 specbind (Qinhibit_point_motion_hooks, Qt);
12970
12971 /* If %c is in the mode line, update it if needed. */
12972 if (!NILP (w->column_number_displayed)
12973 /* This alternative quickly identifies a common case
12974 where no change is needed. */
12975 && !(PT == XFASTINT (w->last_point)
12976 && XFASTINT (w->last_modified) >= MODIFF
12977 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12978 && (XFASTINT (w->column_number_displayed) != current_column ()))
12979 w->update_mode_line = Qt;
12980
12981 unbind_to (count1, Qnil);
12982
12983 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12984
12985 /* The variable buffer_shared is set in redisplay_window and
12986 indicates that we redisplay a buffer in different windows. See
12987 there. */
12988 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12989 || cursor_type_changed);
12990
12991 /* If specs for an arrow have changed, do thorough redisplay
12992 to ensure we remove any arrow that should no longer exist. */
12993 if (overlay_arrows_changed_p ())
12994 consider_all_windows_p = windows_or_buffers_changed = 1;
12995
12996 /* Normally the message* functions will have already displayed and
12997 updated the echo area, but the frame may have been trashed, or
12998 the update may have been preempted, so display the echo area
12999 again here. Checking message_cleared_p captures the case that
13000 the echo area should be cleared. */
13001 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13002 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13003 || (message_cleared_p
13004 && minibuf_level == 0
13005 /* If the mini-window is currently selected, this means the
13006 echo-area doesn't show through. */
13007 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13008 {
13009 int window_height_changed_p = echo_area_display (0);
13010
13011 if (message_cleared_p)
13012 update_miniwindow_p = 1;
13013
13014 must_finish = 1;
13015
13016 /* If we don't display the current message, don't clear the
13017 message_cleared_p flag, because, if we did, we wouldn't clear
13018 the echo area in the next redisplay which doesn't preserve
13019 the echo area. */
13020 if (!display_last_displayed_message_p)
13021 message_cleared_p = 0;
13022
13023 if (fonts_changed_p)
13024 goto retry;
13025 else if (window_height_changed_p)
13026 {
13027 consider_all_windows_p = 1;
13028 ++update_mode_lines;
13029 ++windows_or_buffers_changed;
13030
13031 /* If window configuration was changed, frames may have been
13032 marked garbaged. Clear them or we will experience
13033 surprises wrt scrolling. */
13034 if (frame_garbaged)
13035 clear_garbaged_frames ();
13036 }
13037 }
13038 else if (EQ (selected_window, minibuf_window)
13039 && (current_buffer->clip_changed
13040 || XFASTINT (w->last_modified) < MODIFF
13041 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
13042 && resize_mini_window (w, 0))
13043 {
13044 /* Resized active mini-window to fit the size of what it is
13045 showing if its contents might have changed. */
13046 must_finish = 1;
13047 /* FIXME: this causes all frames to be updated, which seems unnecessary
13048 since only the current frame needs to be considered. This function needs
13049 to be rewritten with two variables, consider_all_windows and
13050 consider_all_frames. */
13051 consider_all_windows_p = 1;
13052 ++windows_or_buffers_changed;
13053 ++update_mode_lines;
13054
13055 /* If window configuration was changed, frames may have been
13056 marked garbaged. Clear them or we will experience
13057 surprises wrt scrolling. */
13058 if (frame_garbaged)
13059 clear_garbaged_frames ();
13060 }
13061
13062
13063 /* If showing the region, and mark has changed, we must redisplay
13064 the whole window. The assignment to this_line_start_pos prevents
13065 the optimization directly below this if-statement. */
13066 if (((!NILP (Vtransient_mark_mode)
13067 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13068 != !NILP (w->region_showing))
13069 || (!NILP (w->region_showing)
13070 && !EQ (w->region_showing,
13071 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13072 CHARPOS (this_line_start_pos) = 0;
13073
13074 /* Optimize the case that only the line containing the cursor in the
13075 selected window has changed. Variables starting with this_ are
13076 set in display_line and record information about the line
13077 containing the cursor. */
13078 tlbufpos = this_line_start_pos;
13079 tlendpos = this_line_end_pos;
13080 if (!consider_all_windows_p
13081 && CHARPOS (tlbufpos) > 0
13082 && NILP (w->update_mode_line)
13083 && !current_buffer->clip_changed
13084 && !current_buffer->prevent_redisplay_optimizations_p
13085 && FRAME_VISIBLE_P (XFRAME (w->frame))
13086 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13087 /* Make sure recorded data applies to current buffer, etc. */
13088 && this_line_buffer == current_buffer
13089 && current_buffer == XBUFFER (w->buffer)
13090 && NILP (w->force_start)
13091 && NILP (w->optional_new_start)
13092 /* Point must be on the line that we have info recorded about. */
13093 && PT >= CHARPOS (tlbufpos)
13094 && PT <= Z - CHARPOS (tlendpos)
13095 /* All text outside that line, including its final newline,
13096 must be unchanged. */
13097 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13098 CHARPOS (tlendpos)))
13099 {
13100 if (CHARPOS (tlbufpos) > BEGV
13101 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13102 && (CHARPOS (tlbufpos) == ZV
13103 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13104 /* Former continuation line has disappeared by becoming empty. */
13105 goto cancel;
13106 else if (XFASTINT (w->last_modified) < MODIFF
13107 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
13108 || MINI_WINDOW_P (w))
13109 {
13110 /* We have to handle the case of continuation around a
13111 wide-column character (see the comment in indent.c around
13112 line 1340).
13113
13114 For instance, in the following case:
13115
13116 -------- Insert --------
13117 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13118 J_I_ ==> J_I_ `^^' are cursors.
13119 ^^ ^^
13120 -------- --------
13121
13122 As we have to redraw the line above, we cannot use this
13123 optimization. */
13124
13125 struct it it;
13126 int line_height_before = this_line_pixel_height;
13127
13128 /* Note that start_display will handle the case that the
13129 line starting at tlbufpos is a continuation line. */
13130 start_display (&it, w, tlbufpos);
13131
13132 /* Implementation note: It this still necessary? */
13133 if (it.current_x != this_line_start_x)
13134 goto cancel;
13135
13136 TRACE ((stderr, "trying display optimization 1\n"));
13137 w->cursor.vpos = -1;
13138 overlay_arrow_seen = 0;
13139 it.vpos = this_line_vpos;
13140 it.current_y = this_line_y;
13141 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13142 display_line (&it);
13143
13144 /* If line contains point, is not continued,
13145 and ends at same distance from eob as before, we win. */
13146 if (w->cursor.vpos >= 0
13147 /* Line is not continued, otherwise this_line_start_pos
13148 would have been set to 0 in display_line. */
13149 && CHARPOS (this_line_start_pos)
13150 /* Line ends as before. */
13151 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13152 /* Line has same height as before. Otherwise other lines
13153 would have to be shifted up or down. */
13154 && this_line_pixel_height == line_height_before)
13155 {
13156 /* If this is not the window's last line, we must adjust
13157 the charstarts of the lines below. */
13158 if (it.current_y < it.last_visible_y)
13159 {
13160 struct glyph_row *row
13161 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13162 ptrdiff_t delta, delta_bytes;
13163
13164 /* We used to distinguish between two cases here,
13165 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13166 when the line ends in a newline or the end of the
13167 buffer's accessible portion. But both cases did
13168 the same, so they were collapsed. */
13169 delta = (Z
13170 - CHARPOS (tlendpos)
13171 - MATRIX_ROW_START_CHARPOS (row));
13172 delta_bytes = (Z_BYTE
13173 - BYTEPOS (tlendpos)
13174 - MATRIX_ROW_START_BYTEPOS (row));
13175
13176 increment_matrix_positions (w->current_matrix,
13177 this_line_vpos + 1,
13178 w->current_matrix->nrows,
13179 delta, delta_bytes);
13180 }
13181
13182 /* If this row displays text now but previously didn't,
13183 or vice versa, w->window_end_vpos may have to be
13184 adjusted. */
13185 if ((it.glyph_row - 1)->displays_text_p)
13186 {
13187 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13188 XSETINT (w->window_end_vpos, this_line_vpos);
13189 }
13190 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13191 && this_line_vpos > 0)
13192 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13193 w->window_end_valid = Qnil;
13194
13195 /* Update hint: No need to try to scroll in update_window. */
13196 w->desired_matrix->no_scrolling_p = 1;
13197
13198 #if GLYPH_DEBUG
13199 *w->desired_matrix->method = 0;
13200 debug_method_add (w, "optimization 1");
13201 #endif
13202 #ifdef HAVE_WINDOW_SYSTEM
13203 update_window_fringes (w, 0);
13204 #endif
13205 goto update;
13206 }
13207 else
13208 goto cancel;
13209 }
13210 else if (/* Cursor position hasn't changed. */
13211 PT == XFASTINT (w->last_point)
13212 /* Make sure the cursor was last displayed
13213 in this window. Otherwise we have to reposition it. */
13214 && 0 <= w->cursor.vpos
13215 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13216 {
13217 if (!must_finish)
13218 {
13219 do_pending_window_change (1);
13220 /* If selected_window changed, redisplay again. */
13221 if (WINDOWP (selected_window)
13222 && (w = XWINDOW (selected_window)) != sw)
13223 goto retry;
13224
13225 /* We used to always goto end_of_redisplay here, but this
13226 isn't enough if we have a blinking cursor. */
13227 if (w->cursor_off_p == w->last_cursor_off_p)
13228 goto end_of_redisplay;
13229 }
13230 goto update;
13231 }
13232 /* If highlighting the region, or if the cursor is in the echo area,
13233 then we can't just move the cursor. */
13234 else if (! (!NILP (Vtransient_mark_mode)
13235 && !NILP (BVAR (current_buffer, mark_active)))
13236 && (EQ (selected_window,
13237 BVAR (current_buffer, last_selected_window))
13238 || highlight_nonselected_windows)
13239 && NILP (w->region_showing)
13240 && NILP (Vshow_trailing_whitespace)
13241 && !cursor_in_echo_area)
13242 {
13243 struct it it;
13244 struct glyph_row *row;
13245
13246 /* Skip from tlbufpos to PT and see where it is. Note that
13247 PT may be in invisible text. If so, we will end at the
13248 next visible position. */
13249 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13250 NULL, DEFAULT_FACE_ID);
13251 it.current_x = this_line_start_x;
13252 it.current_y = this_line_y;
13253 it.vpos = this_line_vpos;
13254
13255 /* The call to move_it_to stops in front of PT, but
13256 moves over before-strings. */
13257 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13258
13259 if (it.vpos == this_line_vpos
13260 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13261 row->enabled_p))
13262 {
13263 xassert (this_line_vpos == it.vpos);
13264 xassert (this_line_y == it.current_y);
13265 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13266 #if GLYPH_DEBUG
13267 *w->desired_matrix->method = 0;
13268 debug_method_add (w, "optimization 3");
13269 #endif
13270 goto update;
13271 }
13272 else
13273 goto cancel;
13274 }
13275
13276 cancel:
13277 /* Text changed drastically or point moved off of line. */
13278 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13279 }
13280
13281 CHARPOS (this_line_start_pos) = 0;
13282 consider_all_windows_p |= buffer_shared > 1;
13283 ++clear_face_cache_count;
13284 #ifdef HAVE_WINDOW_SYSTEM
13285 ++clear_image_cache_count;
13286 #endif
13287
13288 /* Build desired matrices, and update the display. If
13289 consider_all_windows_p is non-zero, do it for all windows on all
13290 frames. Otherwise do it for selected_window, only. */
13291
13292 if (consider_all_windows_p)
13293 {
13294 Lisp_Object tail, frame;
13295
13296 FOR_EACH_FRAME (tail, frame)
13297 XFRAME (frame)->updated_p = 0;
13298
13299 /* Recompute # windows showing selected buffer. This will be
13300 incremented each time such a window is displayed. */
13301 buffer_shared = 0;
13302
13303 FOR_EACH_FRAME (tail, frame)
13304 {
13305 struct frame *f = XFRAME (frame);
13306
13307 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13308 {
13309 if (! EQ (frame, selected_frame))
13310 /* Select the frame, for the sake of frame-local
13311 variables. */
13312 select_frame_for_redisplay (frame);
13313
13314 /* Mark all the scroll bars to be removed; we'll redeem
13315 the ones we want when we redisplay their windows. */
13316 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13317 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13318
13319 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13320 redisplay_windows (FRAME_ROOT_WINDOW (f));
13321
13322 /* The X error handler may have deleted that frame. */
13323 if (!FRAME_LIVE_P (f))
13324 continue;
13325
13326 /* Any scroll bars which redisplay_windows should have
13327 nuked should now go away. */
13328 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13329 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13330
13331 /* If fonts changed, display again. */
13332 /* ??? rms: I suspect it is a mistake to jump all the way
13333 back to retry here. It should just retry this frame. */
13334 if (fonts_changed_p)
13335 goto retry;
13336
13337 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13338 {
13339 /* See if we have to hscroll. */
13340 if (!f->already_hscrolled_p)
13341 {
13342 f->already_hscrolled_p = 1;
13343 if (hscroll_windows (f->root_window))
13344 goto retry;
13345 }
13346
13347 /* Prevent various kinds of signals during display
13348 update. stdio is not robust about handling
13349 signals, which can cause an apparent I/O
13350 error. */
13351 if (interrupt_input)
13352 unrequest_sigio ();
13353 STOP_POLLING;
13354
13355 /* Update the display. */
13356 set_window_update_flags (XWINDOW (f->root_window), 1);
13357 pending |= update_frame (f, 0, 0);
13358 f->updated_p = 1;
13359 }
13360 }
13361 }
13362
13363 if (!EQ (old_frame, selected_frame)
13364 && FRAME_LIVE_P (XFRAME (old_frame)))
13365 /* We played a bit fast-and-loose above and allowed selected_frame
13366 and selected_window to be temporarily out-of-sync but let's make
13367 sure this stays contained. */
13368 select_frame_for_redisplay (old_frame);
13369 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13370
13371 if (!pending)
13372 {
13373 /* Do the mark_window_display_accurate after all windows have
13374 been redisplayed because this call resets flags in buffers
13375 which are needed for proper redisplay. */
13376 FOR_EACH_FRAME (tail, frame)
13377 {
13378 struct frame *f = XFRAME (frame);
13379 if (f->updated_p)
13380 {
13381 mark_window_display_accurate (f->root_window, 1);
13382 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13383 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13384 }
13385 }
13386 }
13387 }
13388 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13389 {
13390 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13391 struct frame *mini_frame;
13392
13393 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13394 /* Use list_of_error, not Qerror, so that
13395 we catch only errors and don't run the debugger. */
13396 internal_condition_case_1 (redisplay_window_1, selected_window,
13397 list_of_error,
13398 redisplay_window_error);
13399 if (update_miniwindow_p)
13400 internal_condition_case_1 (redisplay_window_1, mini_window,
13401 list_of_error,
13402 redisplay_window_error);
13403
13404 /* Compare desired and current matrices, perform output. */
13405
13406 update:
13407 /* If fonts changed, display again. */
13408 if (fonts_changed_p)
13409 goto retry;
13410
13411 /* Prevent various kinds of signals during display update.
13412 stdio is not robust about handling signals,
13413 which can cause an apparent I/O error. */
13414 if (interrupt_input)
13415 unrequest_sigio ();
13416 STOP_POLLING;
13417
13418 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13419 {
13420 if (hscroll_windows (selected_window))
13421 goto retry;
13422
13423 XWINDOW (selected_window)->must_be_updated_p = 1;
13424 pending = update_frame (sf, 0, 0);
13425 }
13426
13427 /* We may have called echo_area_display at the top of this
13428 function. If the echo area is on another frame, that may
13429 have put text on a frame other than the selected one, so the
13430 above call to update_frame would not have caught it. Catch
13431 it here. */
13432 mini_window = FRAME_MINIBUF_WINDOW (sf);
13433 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13434
13435 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13436 {
13437 XWINDOW (mini_window)->must_be_updated_p = 1;
13438 pending |= update_frame (mini_frame, 0, 0);
13439 if (!pending && hscroll_windows (mini_window))
13440 goto retry;
13441 }
13442 }
13443
13444 /* If display was paused because of pending input, make sure we do a
13445 thorough update the next time. */
13446 if (pending)
13447 {
13448 /* Prevent the optimization at the beginning of
13449 redisplay_internal that tries a single-line update of the
13450 line containing the cursor in the selected window. */
13451 CHARPOS (this_line_start_pos) = 0;
13452
13453 /* Let the overlay arrow be updated the next time. */
13454 update_overlay_arrows (0);
13455
13456 /* If we pause after scrolling, some rows in the current
13457 matrices of some windows are not valid. */
13458 if (!WINDOW_FULL_WIDTH_P (w)
13459 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13460 update_mode_lines = 1;
13461 }
13462 else
13463 {
13464 if (!consider_all_windows_p)
13465 {
13466 /* This has already been done above if
13467 consider_all_windows_p is set. */
13468 mark_window_display_accurate_1 (w, 1);
13469
13470 /* Say overlay arrows are up to date. */
13471 update_overlay_arrows (1);
13472
13473 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13474 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13475 }
13476
13477 update_mode_lines = 0;
13478 windows_or_buffers_changed = 0;
13479 cursor_type_changed = 0;
13480 }
13481
13482 /* Start SIGIO interrupts coming again. Having them off during the
13483 code above makes it less likely one will discard output, but not
13484 impossible, since there might be stuff in the system buffer here.
13485 But it is much hairier to try to do anything about that. */
13486 if (interrupt_input)
13487 request_sigio ();
13488 RESUME_POLLING;
13489
13490 /* If a frame has become visible which was not before, redisplay
13491 again, so that we display it. Expose events for such a frame
13492 (which it gets when becoming visible) don't call the parts of
13493 redisplay constructing glyphs, so simply exposing a frame won't
13494 display anything in this case. So, we have to display these
13495 frames here explicitly. */
13496 if (!pending)
13497 {
13498 Lisp_Object tail, frame;
13499 int new_count = 0;
13500
13501 FOR_EACH_FRAME (tail, frame)
13502 {
13503 int this_is_visible = 0;
13504
13505 if (XFRAME (frame)->visible)
13506 this_is_visible = 1;
13507 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13508 if (XFRAME (frame)->visible)
13509 this_is_visible = 1;
13510
13511 if (this_is_visible)
13512 new_count++;
13513 }
13514
13515 if (new_count != number_of_visible_frames)
13516 windows_or_buffers_changed++;
13517 }
13518
13519 /* Change frame size now if a change is pending. */
13520 do_pending_window_change (1);
13521
13522 /* If we just did a pending size change, or have additional
13523 visible frames, or selected_window changed, redisplay again. */
13524 if ((windows_or_buffers_changed && !pending)
13525 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13526 goto retry;
13527
13528 /* Clear the face and image caches.
13529
13530 We used to do this only if consider_all_windows_p. But the cache
13531 needs to be cleared if a timer creates images in the current
13532 buffer (e.g. the test case in Bug#6230). */
13533
13534 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13535 {
13536 clear_face_cache (0);
13537 clear_face_cache_count = 0;
13538 }
13539
13540 #ifdef HAVE_WINDOW_SYSTEM
13541 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13542 {
13543 clear_image_caches (Qnil);
13544 clear_image_cache_count = 0;
13545 }
13546 #endif /* HAVE_WINDOW_SYSTEM */
13547
13548 end_of_redisplay:
13549 unbind_to (count, Qnil);
13550 RESUME_POLLING;
13551 }
13552
13553
13554 /* Redisplay, but leave alone any recent echo area message unless
13555 another message has been requested in its place.
13556
13557 This is useful in situations where you need to redisplay but no
13558 user action has occurred, making it inappropriate for the message
13559 area to be cleared. See tracking_off and
13560 wait_reading_process_output for examples of these situations.
13561
13562 FROM_WHERE is an integer saying from where this function was
13563 called. This is useful for debugging. */
13564
13565 void
13566 redisplay_preserve_echo_area (int from_where)
13567 {
13568 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13569
13570 if (!NILP (echo_area_buffer[1]))
13571 {
13572 /* We have a previously displayed message, but no current
13573 message. Redisplay the previous message. */
13574 display_last_displayed_message_p = 1;
13575 redisplay_internal ();
13576 display_last_displayed_message_p = 0;
13577 }
13578 else
13579 redisplay_internal ();
13580
13581 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13582 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13583 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13584 }
13585
13586
13587 /* Function registered with record_unwind_protect in
13588 redisplay_internal. Reset redisplaying_p to the value it had
13589 before redisplay_internal was called, and clear
13590 prevent_freeing_realized_faces_p. It also selects the previously
13591 selected frame, unless it has been deleted (by an X connection
13592 failure during redisplay, for example). */
13593
13594 static Lisp_Object
13595 unwind_redisplay (Lisp_Object val)
13596 {
13597 Lisp_Object old_redisplaying_p, old_frame;
13598
13599 old_redisplaying_p = XCAR (val);
13600 redisplaying_p = XFASTINT (old_redisplaying_p);
13601 old_frame = XCDR (val);
13602 if (! EQ (old_frame, selected_frame)
13603 && FRAME_LIVE_P (XFRAME (old_frame)))
13604 select_frame_for_redisplay (old_frame);
13605 return Qnil;
13606 }
13607
13608
13609 /* Mark the display of window W as accurate or inaccurate. If
13610 ACCURATE_P is non-zero mark display of W as accurate. If
13611 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13612 redisplay_internal is called. */
13613
13614 static void
13615 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13616 {
13617 if (BUFFERP (w->buffer))
13618 {
13619 struct buffer *b = XBUFFER (w->buffer);
13620
13621 w->last_modified
13622 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13623 w->last_overlay_modified
13624 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13625 w->last_had_star
13626 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13627
13628 if (accurate_p)
13629 {
13630 b->clip_changed = 0;
13631 b->prevent_redisplay_optimizations_p = 0;
13632
13633 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13634 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13635 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13636 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13637
13638 w->current_matrix->buffer = b;
13639 w->current_matrix->begv = BUF_BEGV (b);
13640 w->current_matrix->zv = BUF_ZV (b);
13641
13642 w->last_cursor = w->cursor;
13643 w->last_cursor_off_p = w->cursor_off_p;
13644
13645 if (w == XWINDOW (selected_window))
13646 w->last_point = make_number (BUF_PT (b));
13647 else
13648 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13649 }
13650 }
13651
13652 if (accurate_p)
13653 {
13654 w->window_end_valid = w->buffer;
13655 w->update_mode_line = Qnil;
13656 }
13657 }
13658
13659
13660 /* Mark the display of windows in the window tree rooted at WINDOW as
13661 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13662 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13663 be redisplayed the next time redisplay_internal is called. */
13664
13665 void
13666 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13667 {
13668 struct window *w;
13669
13670 for (; !NILP (window); window = w->next)
13671 {
13672 w = XWINDOW (window);
13673 mark_window_display_accurate_1 (w, accurate_p);
13674
13675 if (!NILP (w->vchild))
13676 mark_window_display_accurate (w->vchild, accurate_p);
13677 if (!NILP (w->hchild))
13678 mark_window_display_accurate (w->hchild, accurate_p);
13679 }
13680
13681 if (accurate_p)
13682 {
13683 update_overlay_arrows (1);
13684 }
13685 else
13686 {
13687 /* Force a thorough redisplay the next time by setting
13688 last_arrow_position and last_arrow_string to t, which is
13689 unequal to any useful value of Voverlay_arrow_... */
13690 update_overlay_arrows (-1);
13691 }
13692 }
13693
13694
13695 /* Return value in display table DP (Lisp_Char_Table *) for character
13696 C. Since a display table doesn't have any parent, we don't have to
13697 follow parent. Do not call this function directly but use the
13698 macro DISP_CHAR_VECTOR. */
13699
13700 Lisp_Object
13701 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13702 {
13703 Lisp_Object val;
13704
13705 if (ASCII_CHAR_P (c))
13706 {
13707 val = dp->ascii;
13708 if (SUB_CHAR_TABLE_P (val))
13709 val = XSUB_CHAR_TABLE (val)->contents[c];
13710 }
13711 else
13712 {
13713 Lisp_Object table;
13714
13715 XSETCHAR_TABLE (table, dp);
13716 val = char_table_ref (table, c);
13717 }
13718 if (NILP (val))
13719 val = dp->defalt;
13720 return val;
13721 }
13722
13723
13724 \f
13725 /***********************************************************************
13726 Window Redisplay
13727 ***********************************************************************/
13728
13729 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13730
13731 static void
13732 redisplay_windows (Lisp_Object window)
13733 {
13734 while (!NILP (window))
13735 {
13736 struct window *w = XWINDOW (window);
13737
13738 if (!NILP (w->hchild))
13739 redisplay_windows (w->hchild);
13740 else if (!NILP (w->vchild))
13741 redisplay_windows (w->vchild);
13742 else if (!NILP (w->buffer))
13743 {
13744 displayed_buffer = XBUFFER (w->buffer);
13745 /* Use list_of_error, not Qerror, so that
13746 we catch only errors and don't run the debugger. */
13747 internal_condition_case_1 (redisplay_window_0, window,
13748 list_of_error,
13749 redisplay_window_error);
13750 }
13751
13752 window = w->next;
13753 }
13754 }
13755
13756 static Lisp_Object
13757 redisplay_window_error (Lisp_Object ignore)
13758 {
13759 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13760 return Qnil;
13761 }
13762
13763 static Lisp_Object
13764 redisplay_window_0 (Lisp_Object window)
13765 {
13766 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13767 redisplay_window (window, 0);
13768 return Qnil;
13769 }
13770
13771 static Lisp_Object
13772 redisplay_window_1 (Lisp_Object window)
13773 {
13774 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13775 redisplay_window (window, 1);
13776 return Qnil;
13777 }
13778 \f
13779
13780 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13781 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13782 which positions recorded in ROW differ from current buffer
13783 positions.
13784
13785 Return 0 if cursor is not on this row, 1 otherwise. */
13786
13787 static int
13788 set_cursor_from_row (struct window *w, struct glyph_row *row,
13789 struct glyph_matrix *matrix,
13790 ptrdiff_t delta, ptrdiff_t delta_bytes,
13791 int dy, int dvpos)
13792 {
13793 struct glyph *glyph = row->glyphs[TEXT_AREA];
13794 struct glyph *end = glyph + row->used[TEXT_AREA];
13795 struct glyph *cursor = NULL;
13796 /* The last known character position in row. */
13797 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13798 int x = row->x;
13799 ptrdiff_t pt_old = PT - delta;
13800 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13801 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13802 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13803 /* A glyph beyond the edge of TEXT_AREA which we should never
13804 touch. */
13805 struct glyph *glyphs_end = end;
13806 /* Non-zero means we've found a match for cursor position, but that
13807 glyph has the avoid_cursor_p flag set. */
13808 int match_with_avoid_cursor = 0;
13809 /* Non-zero means we've seen at least one glyph that came from a
13810 display string. */
13811 int string_seen = 0;
13812 /* Largest and smallest buffer positions seen so far during scan of
13813 glyph row. */
13814 ptrdiff_t bpos_max = pos_before;
13815 ptrdiff_t bpos_min = pos_after;
13816 /* Last buffer position covered by an overlay string with an integer
13817 `cursor' property. */
13818 ptrdiff_t bpos_covered = 0;
13819 /* Non-zero means the display string on which to display the cursor
13820 comes from a text property, not from an overlay. */
13821 int string_from_text_prop = 0;
13822
13823 /* Don't even try doing anything if called for a mode-line or
13824 header-line row, since the rest of the code isn't prepared to
13825 deal with such calamities. */
13826 xassert (!row->mode_line_p);
13827 if (row->mode_line_p)
13828 return 0;
13829
13830 /* Skip over glyphs not having an object at the start and the end of
13831 the row. These are special glyphs like truncation marks on
13832 terminal frames. */
13833 if (row->displays_text_p)
13834 {
13835 if (!row->reversed_p)
13836 {
13837 while (glyph < end
13838 && INTEGERP (glyph->object)
13839 && glyph->charpos < 0)
13840 {
13841 x += glyph->pixel_width;
13842 ++glyph;
13843 }
13844 while (end > glyph
13845 && INTEGERP ((end - 1)->object)
13846 /* CHARPOS is zero for blanks and stretch glyphs
13847 inserted by extend_face_to_end_of_line. */
13848 && (end - 1)->charpos <= 0)
13849 --end;
13850 glyph_before = glyph - 1;
13851 glyph_after = end;
13852 }
13853 else
13854 {
13855 struct glyph *g;
13856
13857 /* If the glyph row is reversed, we need to process it from back
13858 to front, so swap the edge pointers. */
13859 glyphs_end = end = glyph - 1;
13860 glyph += row->used[TEXT_AREA] - 1;
13861
13862 while (glyph > end + 1
13863 && INTEGERP (glyph->object)
13864 && glyph->charpos < 0)
13865 {
13866 --glyph;
13867 x -= glyph->pixel_width;
13868 }
13869 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13870 --glyph;
13871 /* By default, in reversed rows we put the cursor on the
13872 rightmost (first in the reading order) glyph. */
13873 for (g = end + 1; g < glyph; g++)
13874 x += g->pixel_width;
13875 while (end < glyph
13876 && INTEGERP ((end + 1)->object)
13877 && (end + 1)->charpos <= 0)
13878 ++end;
13879 glyph_before = glyph + 1;
13880 glyph_after = end;
13881 }
13882 }
13883 else if (row->reversed_p)
13884 {
13885 /* In R2L rows that don't display text, put the cursor on the
13886 rightmost glyph. Case in point: an empty last line that is
13887 part of an R2L paragraph. */
13888 cursor = end - 1;
13889 /* Avoid placing the cursor on the last glyph of the row, where
13890 on terminal frames we hold the vertical border between
13891 adjacent windows. */
13892 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13893 && !WINDOW_RIGHTMOST_P (w)
13894 && cursor == row->glyphs[LAST_AREA] - 1)
13895 cursor--;
13896 x = -1; /* will be computed below, at label compute_x */
13897 }
13898
13899 /* Step 1: Try to find the glyph whose character position
13900 corresponds to point. If that's not possible, find 2 glyphs
13901 whose character positions are the closest to point, one before
13902 point, the other after it. */
13903 if (!row->reversed_p)
13904 while (/* not marched to end of glyph row */
13905 glyph < end
13906 /* glyph was not inserted by redisplay for internal purposes */
13907 && !INTEGERP (glyph->object))
13908 {
13909 if (BUFFERP (glyph->object))
13910 {
13911 ptrdiff_t dpos = glyph->charpos - pt_old;
13912
13913 if (glyph->charpos > bpos_max)
13914 bpos_max = glyph->charpos;
13915 if (glyph->charpos < bpos_min)
13916 bpos_min = glyph->charpos;
13917 if (!glyph->avoid_cursor_p)
13918 {
13919 /* If we hit point, we've found the glyph on which to
13920 display the cursor. */
13921 if (dpos == 0)
13922 {
13923 match_with_avoid_cursor = 0;
13924 break;
13925 }
13926 /* See if we've found a better approximation to
13927 POS_BEFORE or to POS_AFTER. Note that we want the
13928 first (leftmost) glyph of all those that are the
13929 closest from below, and the last (rightmost) of all
13930 those from above. */
13931 if (0 > dpos && dpos > pos_before - pt_old)
13932 {
13933 pos_before = glyph->charpos;
13934 glyph_before = glyph;
13935 }
13936 else if (0 < dpos && dpos <= pos_after - pt_old)
13937 {
13938 pos_after = glyph->charpos;
13939 glyph_after = glyph;
13940 }
13941 }
13942 else if (dpos == 0)
13943 match_with_avoid_cursor = 1;
13944 }
13945 else if (STRINGP (glyph->object))
13946 {
13947 Lisp_Object chprop;
13948 ptrdiff_t glyph_pos = glyph->charpos;
13949
13950 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13951 glyph->object);
13952 if (!NILP (chprop))
13953 {
13954 /* If the string came from a `display' text property,
13955 look up the buffer position of that property and
13956 use that position to update bpos_max, as if we
13957 actually saw such a position in one of the row's
13958 glyphs. This helps with supporting integer values
13959 of `cursor' property on the display string in
13960 situations where most or all of the row's buffer
13961 text is completely covered by display properties,
13962 so that no glyph with valid buffer positions is
13963 ever seen in the row. */
13964 ptrdiff_t prop_pos =
13965 string_buffer_position_lim (glyph->object, pos_before,
13966 pos_after, 0);
13967
13968 if (prop_pos >= pos_before)
13969 bpos_max = prop_pos - 1;
13970 }
13971 if (INTEGERP (chprop))
13972 {
13973 bpos_covered = bpos_max + XINT (chprop);
13974 /* If the `cursor' property covers buffer positions up
13975 to and including point, we should display cursor on
13976 this glyph. Note that, if a `cursor' property on one
13977 of the string's characters has an integer value, we
13978 will break out of the loop below _before_ we get to
13979 the position match above. IOW, integer values of
13980 the `cursor' property override the "exact match for
13981 point" strategy of positioning the cursor. */
13982 /* Implementation note: bpos_max == pt_old when, e.g.,
13983 we are in an empty line, where bpos_max is set to
13984 MATRIX_ROW_START_CHARPOS, see above. */
13985 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13986 {
13987 cursor = glyph;
13988 break;
13989 }
13990 }
13991
13992 string_seen = 1;
13993 }
13994 x += glyph->pixel_width;
13995 ++glyph;
13996 }
13997 else if (glyph > end) /* row is reversed */
13998 while (!INTEGERP (glyph->object))
13999 {
14000 if (BUFFERP (glyph->object))
14001 {
14002 ptrdiff_t dpos = glyph->charpos - pt_old;
14003
14004 if (glyph->charpos > bpos_max)
14005 bpos_max = glyph->charpos;
14006 if (glyph->charpos < bpos_min)
14007 bpos_min = glyph->charpos;
14008 if (!glyph->avoid_cursor_p)
14009 {
14010 if (dpos == 0)
14011 {
14012 match_with_avoid_cursor = 0;
14013 break;
14014 }
14015 if (0 > dpos && dpos > pos_before - pt_old)
14016 {
14017 pos_before = glyph->charpos;
14018 glyph_before = glyph;
14019 }
14020 else if (0 < dpos && dpos <= pos_after - pt_old)
14021 {
14022 pos_after = glyph->charpos;
14023 glyph_after = glyph;
14024 }
14025 }
14026 else if (dpos == 0)
14027 match_with_avoid_cursor = 1;
14028 }
14029 else if (STRINGP (glyph->object))
14030 {
14031 Lisp_Object chprop;
14032 ptrdiff_t glyph_pos = glyph->charpos;
14033
14034 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14035 glyph->object);
14036 if (!NILP (chprop))
14037 {
14038 ptrdiff_t prop_pos =
14039 string_buffer_position_lim (glyph->object, pos_before,
14040 pos_after, 0);
14041
14042 if (prop_pos >= pos_before)
14043 bpos_max = prop_pos - 1;
14044 }
14045 if (INTEGERP (chprop))
14046 {
14047 bpos_covered = bpos_max + XINT (chprop);
14048 /* If the `cursor' property covers buffer positions up
14049 to and including point, we should display cursor on
14050 this glyph. */
14051 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14052 {
14053 cursor = glyph;
14054 break;
14055 }
14056 }
14057 string_seen = 1;
14058 }
14059 --glyph;
14060 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14061 {
14062 x--; /* can't use any pixel_width */
14063 break;
14064 }
14065 x -= glyph->pixel_width;
14066 }
14067
14068 /* Step 2: If we didn't find an exact match for point, we need to
14069 look for a proper place to put the cursor among glyphs between
14070 GLYPH_BEFORE and GLYPH_AFTER. */
14071 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14072 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14073 && bpos_covered < pt_old)
14074 {
14075 /* An empty line has a single glyph whose OBJECT is zero and
14076 whose CHARPOS is the position of a newline on that line.
14077 Note that on a TTY, there are more glyphs after that, which
14078 were produced by extend_face_to_end_of_line, but their
14079 CHARPOS is zero or negative. */
14080 int empty_line_p =
14081 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14082 && INTEGERP (glyph->object) && glyph->charpos > 0;
14083
14084 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14085 {
14086 ptrdiff_t ellipsis_pos;
14087
14088 /* Scan back over the ellipsis glyphs. */
14089 if (!row->reversed_p)
14090 {
14091 ellipsis_pos = (glyph - 1)->charpos;
14092 while (glyph > row->glyphs[TEXT_AREA]
14093 && (glyph - 1)->charpos == ellipsis_pos)
14094 glyph--, x -= glyph->pixel_width;
14095 /* That loop always goes one position too far, including
14096 the glyph before the ellipsis. So scan forward over
14097 that one. */
14098 x += glyph->pixel_width;
14099 glyph++;
14100 }
14101 else /* row is reversed */
14102 {
14103 ellipsis_pos = (glyph + 1)->charpos;
14104 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14105 && (glyph + 1)->charpos == ellipsis_pos)
14106 glyph++, x += glyph->pixel_width;
14107 x -= glyph->pixel_width;
14108 glyph--;
14109 }
14110 }
14111 else if (match_with_avoid_cursor)
14112 {
14113 cursor = glyph_after;
14114 x = -1;
14115 }
14116 else if (string_seen)
14117 {
14118 int incr = row->reversed_p ? -1 : +1;
14119
14120 /* Need to find the glyph that came out of a string which is
14121 present at point. That glyph is somewhere between
14122 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14123 positioned between POS_BEFORE and POS_AFTER in the
14124 buffer. */
14125 struct glyph *start, *stop;
14126 ptrdiff_t pos = pos_before;
14127
14128 x = -1;
14129
14130 /* If the row ends in a newline from a display string,
14131 reordering could have moved the glyphs belonging to the
14132 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14133 in this case we extend the search to the last glyph in
14134 the row that was not inserted by redisplay. */
14135 if (row->ends_in_newline_from_string_p)
14136 {
14137 glyph_after = end;
14138 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14139 }
14140
14141 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14142 correspond to POS_BEFORE and POS_AFTER, respectively. We
14143 need START and STOP in the order that corresponds to the
14144 row's direction as given by its reversed_p flag. If the
14145 directionality of characters between POS_BEFORE and
14146 POS_AFTER is the opposite of the row's base direction,
14147 these characters will have been reordered for display,
14148 and we need to reverse START and STOP. */
14149 if (!row->reversed_p)
14150 {
14151 start = min (glyph_before, glyph_after);
14152 stop = max (glyph_before, glyph_after);
14153 }
14154 else
14155 {
14156 start = max (glyph_before, glyph_after);
14157 stop = min (glyph_before, glyph_after);
14158 }
14159 for (glyph = start + incr;
14160 row->reversed_p ? glyph > stop : glyph < stop; )
14161 {
14162
14163 /* Any glyphs that come from the buffer are here because
14164 of bidi reordering. Skip them, and only pay
14165 attention to glyphs that came from some string. */
14166 if (STRINGP (glyph->object))
14167 {
14168 Lisp_Object str;
14169 ptrdiff_t tem;
14170 /* If the display property covers the newline, we
14171 need to search for it one position farther. */
14172 ptrdiff_t lim = pos_after
14173 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14174
14175 string_from_text_prop = 0;
14176 str = glyph->object;
14177 tem = string_buffer_position_lim (str, pos, lim, 0);
14178 if (tem == 0 /* from overlay */
14179 || pos <= tem)
14180 {
14181 /* If the string from which this glyph came is
14182 found in the buffer at point, or at position
14183 that is closer to point than pos_after, then
14184 we've found the glyph we've been looking for.
14185 If it comes from an overlay (tem == 0), and
14186 it has the `cursor' property on one of its
14187 glyphs, record that glyph as a candidate for
14188 displaying the cursor. (As in the
14189 unidirectional version, we will display the
14190 cursor on the last candidate we find.) */
14191 if (tem == 0
14192 || tem == pt_old
14193 || (tem - pt_old > 0 && tem < pos_after))
14194 {
14195 /* The glyphs from this string could have
14196 been reordered. Find the one with the
14197 smallest string position. Or there could
14198 be a character in the string with the
14199 `cursor' property, which means display
14200 cursor on that character's glyph. */
14201 ptrdiff_t strpos = glyph->charpos;
14202
14203 if (tem)
14204 {
14205 cursor = glyph;
14206 string_from_text_prop = 1;
14207 }
14208 for ( ;
14209 (row->reversed_p ? glyph > stop : glyph < stop)
14210 && EQ (glyph->object, str);
14211 glyph += incr)
14212 {
14213 Lisp_Object cprop;
14214 ptrdiff_t gpos = glyph->charpos;
14215
14216 cprop = Fget_char_property (make_number (gpos),
14217 Qcursor,
14218 glyph->object);
14219 if (!NILP (cprop))
14220 {
14221 cursor = glyph;
14222 break;
14223 }
14224 if (tem && glyph->charpos < strpos)
14225 {
14226 strpos = glyph->charpos;
14227 cursor = glyph;
14228 }
14229 }
14230
14231 if (tem == pt_old
14232 || (tem - pt_old > 0 && tem < pos_after))
14233 goto compute_x;
14234 }
14235 if (tem)
14236 pos = tem + 1; /* don't find previous instances */
14237 }
14238 /* This string is not what we want; skip all of the
14239 glyphs that came from it. */
14240 while ((row->reversed_p ? glyph > stop : glyph < stop)
14241 && EQ (glyph->object, str))
14242 glyph += incr;
14243 }
14244 else
14245 glyph += incr;
14246 }
14247
14248 /* If we reached the end of the line, and END was from a string,
14249 the cursor is not on this line. */
14250 if (cursor == NULL
14251 && (row->reversed_p ? glyph <= end : glyph >= end)
14252 && STRINGP (end->object)
14253 && row->continued_p)
14254 return 0;
14255 }
14256 /* A truncated row may not include PT among its character positions.
14257 Setting the cursor inside the scroll margin will trigger
14258 recalculation of hscroll in hscroll_window_tree. But if a
14259 display string covers point, defer to the string-handling
14260 code below to figure this out. */
14261 else if (row->truncated_on_left_p && pt_old < bpos_min)
14262 {
14263 cursor = glyph_before;
14264 x = -1;
14265 }
14266 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14267 /* Zero-width characters produce no glyphs. */
14268 || (!empty_line_p
14269 && (row->reversed_p
14270 ? glyph_after > glyphs_end
14271 : glyph_after < glyphs_end)))
14272 {
14273 cursor = glyph_after;
14274 x = -1;
14275 }
14276 }
14277
14278 compute_x:
14279 if (cursor != NULL)
14280 glyph = cursor;
14281 if (x < 0)
14282 {
14283 struct glyph *g;
14284
14285 /* Need to compute x that corresponds to GLYPH. */
14286 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14287 {
14288 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14289 abort ();
14290 x += g->pixel_width;
14291 }
14292 }
14293
14294 /* ROW could be part of a continued line, which, under bidi
14295 reordering, might have other rows whose start and end charpos
14296 occlude point. Only set w->cursor if we found a better
14297 approximation to the cursor position than we have from previously
14298 examined candidate rows belonging to the same continued line. */
14299 if (/* we already have a candidate row */
14300 w->cursor.vpos >= 0
14301 /* that candidate is not the row we are processing */
14302 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14303 /* Make sure cursor.vpos specifies a row whose start and end
14304 charpos occlude point, and it is valid candidate for being a
14305 cursor-row. This is because some callers of this function
14306 leave cursor.vpos at the row where the cursor was displayed
14307 during the last redisplay cycle. */
14308 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14309 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14310 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14311 {
14312 struct glyph *g1 =
14313 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14314
14315 /* Don't consider glyphs that are outside TEXT_AREA. */
14316 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14317 return 0;
14318 /* Keep the candidate whose buffer position is the closest to
14319 point or has the `cursor' property. */
14320 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14321 w->cursor.hpos >= 0
14322 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14323 && ((BUFFERP (g1->object)
14324 && (g1->charpos == pt_old /* an exact match always wins */
14325 || (BUFFERP (glyph->object)
14326 && eabs (g1->charpos - pt_old)
14327 < eabs (glyph->charpos - pt_old))))
14328 /* previous candidate is a glyph from a string that has
14329 a non-nil `cursor' property */
14330 || (STRINGP (g1->object)
14331 && (!NILP (Fget_char_property (make_number (g1->charpos),
14332 Qcursor, g1->object))
14333 /* previous candidate is from the same display
14334 string as this one, and the display string
14335 came from a text property */
14336 || (EQ (g1->object, glyph->object)
14337 && string_from_text_prop)
14338 /* this candidate is from newline and its
14339 position is not an exact match */
14340 || (INTEGERP (glyph->object)
14341 && glyph->charpos != pt_old)))))
14342 return 0;
14343 /* If this candidate gives an exact match, use that. */
14344 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14345 /* If this candidate is a glyph created for the
14346 terminating newline of a line, and point is on that
14347 newline, it wins because it's an exact match. */
14348 || (!row->continued_p
14349 && INTEGERP (glyph->object)
14350 && glyph->charpos == 0
14351 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14352 /* Otherwise, keep the candidate that comes from a row
14353 spanning less buffer positions. This may win when one or
14354 both candidate positions are on glyphs that came from
14355 display strings, for which we cannot compare buffer
14356 positions. */
14357 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14358 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14359 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14360 return 0;
14361 }
14362 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14363 w->cursor.x = x;
14364 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14365 w->cursor.y = row->y + dy;
14366
14367 if (w == XWINDOW (selected_window))
14368 {
14369 if (!row->continued_p
14370 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14371 && row->x == 0)
14372 {
14373 this_line_buffer = XBUFFER (w->buffer);
14374
14375 CHARPOS (this_line_start_pos)
14376 = MATRIX_ROW_START_CHARPOS (row) + delta;
14377 BYTEPOS (this_line_start_pos)
14378 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14379
14380 CHARPOS (this_line_end_pos)
14381 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14382 BYTEPOS (this_line_end_pos)
14383 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14384
14385 this_line_y = w->cursor.y;
14386 this_line_pixel_height = row->height;
14387 this_line_vpos = w->cursor.vpos;
14388 this_line_start_x = row->x;
14389 }
14390 else
14391 CHARPOS (this_line_start_pos) = 0;
14392 }
14393
14394 return 1;
14395 }
14396
14397
14398 /* Run window scroll functions, if any, for WINDOW with new window
14399 start STARTP. Sets the window start of WINDOW to that position.
14400
14401 We assume that the window's buffer is really current. */
14402
14403 static inline struct text_pos
14404 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14405 {
14406 struct window *w = XWINDOW (window);
14407 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14408
14409 if (current_buffer != XBUFFER (w->buffer))
14410 abort ();
14411
14412 if (!NILP (Vwindow_scroll_functions))
14413 {
14414 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14415 make_number (CHARPOS (startp)));
14416 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14417 /* In case the hook functions switch buffers. */
14418 if (current_buffer != XBUFFER (w->buffer))
14419 set_buffer_internal_1 (XBUFFER (w->buffer));
14420 }
14421
14422 return startp;
14423 }
14424
14425
14426 /* Make sure the line containing the cursor is fully visible.
14427 A value of 1 means there is nothing to be done.
14428 (Either the line is fully visible, or it cannot be made so,
14429 or we cannot tell.)
14430
14431 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14432 is higher than window.
14433
14434 A value of 0 means the caller should do scrolling
14435 as if point had gone off the screen. */
14436
14437 static int
14438 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14439 {
14440 struct glyph_matrix *matrix;
14441 struct glyph_row *row;
14442 int window_height;
14443
14444 if (!make_cursor_line_fully_visible_p)
14445 return 1;
14446
14447 /* It's not always possible to find the cursor, e.g, when a window
14448 is full of overlay strings. Don't do anything in that case. */
14449 if (w->cursor.vpos < 0)
14450 return 1;
14451
14452 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14453 row = MATRIX_ROW (matrix, w->cursor.vpos);
14454
14455 /* If the cursor row is not partially visible, there's nothing to do. */
14456 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14457 return 1;
14458
14459 /* If the row the cursor is in is taller than the window's height,
14460 it's not clear what to do, so do nothing. */
14461 window_height = window_box_height (w);
14462 if (row->height >= window_height)
14463 {
14464 if (!force_p || MINI_WINDOW_P (w)
14465 || w->vscroll || w->cursor.vpos == 0)
14466 return 1;
14467 }
14468 return 0;
14469 }
14470
14471
14472 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14473 non-zero means only WINDOW is redisplayed in redisplay_internal.
14474 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14475 in redisplay_window to bring a partially visible line into view in
14476 the case that only the cursor has moved.
14477
14478 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14479 last screen line's vertical height extends past the end of the screen.
14480
14481 Value is
14482
14483 1 if scrolling succeeded
14484
14485 0 if scrolling didn't find point.
14486
14487 -1 if new fonts have been loaded so that we must interrupt
14488 redisplay, adjust glyph matrices, and try again. */
14489
14490 enum
14491 {
14492 SCROLLING_SUCCESS,
14493 SCROLLING_FAILED,
14494 SCROLLING_NEED_LARGER_MATRICES
14495 };
14496
14497 /* If scroll-conservatively is more than this, never recenter.
14498
14499 If you change this, don't forget to update the doc string of
14500 `scroll-conservatively' and the Emacs manual. */
14501 #define SCROLL_LIMIT 100
14502
14503 static int
14504 try_scrolling (Lisp_Object window, int just_this_one_p,
14505 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14506 int temp_scroll_step, int last_line_misfit)
14507 {
14508 struct window *w = XWINDOW (window);
14509 struct frame *f = XFRAME (w->frame);
14510 struct text_pos pos, startp;
14511 struct it it;
14512 int this_scroll_margin, scroll_max, rc, height;
14513 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14514 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14515 Lisp_Object aggressive;
14516 /* We will never try scrolling more than this number of lines. */
14517 int scroll_limit = SCROLL_LIMIT;
14518
14519 #if GLYPH_DEBUG
14520 debug_method_add (w, "try_scrolling");
14521 #endif
14522
14523 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14524
14525 /* Compute scroll margin height in pixels. We scroll when point is
14526 within this distance from the top or bottom of the window. */
14527 if (scroll_margin > 0)
14528 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14529 * FRAME_LINE_HEIGHT (f);
14530 else
14531 this_scroll_margin = 0;
14532
14533 /* Force arg_scroll_conservatively to have a reasonable value, to
14534 avoid scrolling too far away with slow move_it_* functions. Note
14535 that the user can supply scroll-conservatively equal to
14536 `most-positive-fixnum', which can be larger than INT_MAX. */
14537 if (arg_scroll_conservatively > scroll_limit)
14538 {
14539 arg_scroll_conservatively = scroll_limit + 1;
14540 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14541 }
14542 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14543 /* Compute how much we should try to scroll maximally to bring
14544 point into view. */
14545 scroll_max = (max (scroll_step,
14546 max (arg_scroll_conservatively, temp_scroll_step))
14547 * FRAME_LINE_HEIGHT (f));
14548 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14549 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14550 /* We're trying to scroll because of aggressive scrolling but no
14551 scroll_step is set. Choose an arbitrary one. */
14552 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14553 else
14554 scroll_max = 0;
14555
14556 too_near_end:
14557
14558 /* Decide whether to scroll down. */
14559 if (PT > CHARPOS (startp))
14560 {
14561 int scroll_margin_y;
14562
14563 /* Compute the pixel ypos of the scroll margin, then move IT to
14564 either that ypos or PT, whichever comes first. */
14565 start_display (&it, w, startp);
14566 scroll_margin_y = it.last_visible_y - this_scroll_margin
14567 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14568 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14569 (MOVE_TO_POS | MOVE_TO_Y));
14570
14571 if (PT > CHARPOS (it.current.pos))
14572 {
14573 int y0 = line_bottom_y (&it);
14574 /* Compute how many pixels below window bottom to stop searching
14575 for PT. This avoids costly search for PT that is far away if
14576 the user limited scrolling by a small number of lines, but
14577 always finds PT if scroll_conservatively is set to a large
14578 number, such as most-positive-fixnum. */
14579 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14580 int y_to_move = it.last_visible_y + slack;
14581
14582 /* Compute the distance from the scroll margin to PT or to
14583 the scroll limit, whichever comes first. This should
14584 include the height of the cursor line, to make that line
14585 fully visible. */
14586 move_it_to (&it, PT, -1, y_to_move,
14587 -1, MOVE_TO_POS | MOVE_TO_Y);
14588 dy = line_bottom_y (&it) - y0;
14589
14590 if (dy > scroll_max)
14591 return SCROLLING_FAILED;
14592
14593 if (dy > 0)
14594 scroll_down_p = 1;
14595 }
14596 }
14597
14598 if (scroll_down_p)
14599 {
14600 /* Point is in or below the bottom scroll margin, so move the
14601 window start down. If scrolling conservatively, move it just
14602 enough down to make point visible. If scroll_step is set,
14603 move it down by scroll_step. */
14604 if (arg_scroll_conservatively)
14605 amount_to_scroll
14606 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14607 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14608 else if (scroll_step || temp_scroll_step)
14609 amount_to_scroll = scroll_max;
14610 else
14611 {
14612 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14613 height = WINDOW_BOX_TEXT_HEIGHT (w);
14614 if (NUMBERP (aggressive))
14615 {
14616 double float_amount = XFLOATINT (aggressive) * height;
14617 amount_to_scroll = float_amount;
14618 if (amount_to_scroll == 0 && float_amount > 0)
14619 amount_to_scroll = 1;
14620 /* Don't let point enter the scroll margin near top of
14621 the window. */
14622 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14623 amount_to_scroll = height - 2*this_scroll_margin + dy;
14624 }
14625 }
14626
14627 if (amount_to_scroll <= 0)
14628 return SCROLLING_FAILED;
14629
14630 start_display (&it, w, startp);
14631 if (arg_scroll_conservatively <= scroll_limit)
14632 move_it_vertically (&it, amount_to_scroll);
14633 else
14634 {
14635 /* Extra precision for users who set scroll-conservatively
14636 to a large number: make sure the amount we scroll
14637 the window start is never less than amount_to_scroll,
14638 which was computed as distance from window bottom to
14639 point. This matters when lines at window top and lines
14640 below window bottom have different height. */
14641 struct it it1;
14642 void *it1data = NULL;
14643 /* We use a temporary it1 because line_bottom_y can modify
14644 its argument, if it moves one line down; see there. */
14645 int start_y;
14646
14647 SAVE_IT (it1, it, it1data);
14648 start_y = line_bottom_y (&it1);
14649 do {
14650 RESTORE_IT (&it, &it, it1data);
14651 move_it_by_lines (&it, 1);
14652 SAVE_IT (it1, it, it1data);
14653 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14654 }
14655
14656 /* If STARTP is unchanged, move it down another screen line. */
14657 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14658 move_it_by_lines (&it, 1);
14659 startp = it.current.pos;
14660 }
14661 else
14662 {
14663 struct text_pos scroll_margin_pos = startp;
14664
14665 /* See if point is inside the scroll margin at the top of the
14666 window. */
14667 if (this_scroll_margin)
14668 {
14669 start_display (&it, w, startp);
14670 move_it_vertically (&it, this_scroll_margin);
14671 scroll_margin_pos = it.current.pos;
14672 }
14673
14674 if (PT < CHARPOS (scroll_margin_pos))
14675 {
14676 /* Point is in the scroll margin at the top of the window or
14677 above what is displayed in the window. */
14678 int y0, y_to_move;
14679
14680 /* Compute the vertical distance from PT to the scroll
14681 margin position. Move as far as scroll_max allows, or
14682 one screenful, or 10 screen lines, whichever is largest.
14683 Give up if distance is greater than scroll_max. */
14684 SET_TEXT_POS (pos, PT, PT_BYTE);
14685 start_display (&it, w, pos);
14686 y0 = it.current_y;
14687 y_to_move = max (it.last_visible_y,
14688 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14689 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14690 y_to_move, -1,
14691 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14692 dy = it.current_y - y0;
14693 if (dy > scroll_max)
14694 return SCROLLING_FAILED;
14695
14696 /* Compute new window start. */
14697 start_display (&it, w, startp);
14698
14699 if (arg_scroll_conservatively)
14700 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14701 max (scroll_step, temp_scroll_step));
14702 else if (scroll_step || temp_scroll_step)
14703 amount_to_scroll = scroll_max;
14704 else
14705 {
14706 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14707 height = WINDOW_BOX_TEXT_HEIGHT (w);
14708 if (NUMBERP (aggressive))
14709 {
14710 double float_amount = XFLOATINT (aggressive) * height;
14711 amount_to_scroll = float_amount;
14712 if (amount_to_scroll == 0 && float_amount > 0)
14713 amount_to_scroll = 1;
14714 amount_to_scroll -=
14715 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14716 /* Don't let point enter the scroll margin near
14717 bottom of the window. */
14718 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14719 amount_to_scroll = height - 2*this_scroll_margin + dy;
14720 }
14721 }
14722
14723 if (amount_to_scroll <= 0)
14724 return SCROLLING_FAILED;
14725
14726 move_it_vertically_backward (&it, amount_to_scroll);
14727 startp = it.current.pos;
14728 }
14729 }
14730
14731 /* Run window scroll functions. */
14732 startp = run_window_scroll_functions (window, startp);
14733
14734 /* Display the window. Give up if new fonts are loaded, or if point
14735 doesn't appear. */
14736 if (!try_window (window, startp, 0))
14737 rc = SCROLLING_NEED_LARGER_MATRICES;
14738 else if (w->cursor.vpos < 0)
14739 {
14740 clear_glyph_matrix (w->desired_matrix);
14741 rc = SCROLLING_FAILED;
14742 }
14743 else
14744 {
14745 /* Maybe forget recorded base line for line number display. */
14746 if (!just_this_one_p
14747 || current_buffer->clip_changed
14748 || BEG_UNCHANGED < CHARPOS (startp))
14749 w->base_line_number = Qnil;
14750
14751 /* If cursor ends up on a partially visible line,
14752 treat that as being off the bottom of the screen. */
14753 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14754 /* It's possible that the cursor is on the first line of the
14755 buffer, which is partially obscured due to a vscroll
14756 (Bug#7537). In that case, avoid looping forever . */
14757 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14758 {
14759 clear_glyph_matrix (w->desired_matrix);
14760 ++extra_scroll_margin_lines;
14761 goto too_near_end;
14762 }
14763 rc = SCROLLING_SUCCESS;
14764 }
14765
14766 return rc;
14767 }
14768
14769
14770 /* Compute a suitable window start for window W if display of W starts
14771 on a continuation line. Value is non-zero if a new window start
14772 was computed.
14773
14774 The new window start will be computed, based on W's width, starting
14775 from the start of the continued line. It is the start of the
14776 screen line with the minimum distance from the old start W->start. */
14777
14778 static int
14779 compute_window_start_on_continuation_line (struct window *w)
14780 {
14781 struct text_pos pos, start_pos;
14782 int window_start_changed_p = 0;
14783
14784 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14785
14786 /* If window start is on a continuation line... Window start may be
14787 < BEGV in case there's invisible text at the start of the
14788 buffer (M-x rmail, for example). */
14789 if (CHARPOS (start_pos) > BEGV
14790 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14791 {
14792 struct it it;
14793 struct glyph_row *row;
14794
14795 /* Handle the case that the window start is out of range. */
14796 if (CHARPOS (start_pos) < BEGV)
14797 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14798 else if (CHARPOS (start_pos) > ZV)
14799 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14800
14801 /* Find the start of the continued line. This should be fast
14802 because scan_buffer is fast (newline cache). */
14803 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14804 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14805 row, DEFAULT_FACE_ID);
14806 reseat_at_previous_visible_line_start (&it);
14807
14808 /* If the line start is "too far" away from the window start,
14809 say it takes too much time to compute a new window start. */
14810 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14811 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14812 {
14813 int min_distance, distance;
14814
14815 /* Move forward by display lines to find the new window
14816 start. If window width was enlarged, the new start can
14817 be expected to be > the old start. If window width was
14818 decreased, the new window start will be < the old start.
14819 So, we're looking for the display line start with the
14820 minimum distance from the old window start. */
14821 pos = it.current.pos;
14822 min_distance = INFINITY;
14823 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14824 distance < min_distance)
14825 {
14826 min_distance = distance;
14827 pos = it.current.pos;
14828 move_it_by_lines (&it, 1);
14829 }
14830
14831 /* Set the window start there. */
14832 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14833 window_start_changed_p = 1;
14834 }
14835 }
14836
14837 return window_start_changed_p;
14838 }
14839
14840
14841 /* Try cursor movement in case text has not changed in window WINDOW,
14842 with window start STARTP. Value is
14843
14844 CURSOR_MOVEMENT_SUCCESS if successful
14845
14846 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14847
14848 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14849 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14850 we want to scroll as if scroll-step were set to 1. See the code.
14851
14852 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14853 which case we have to abort this redisplay, and adjust matrices
14854 first. */
14855
14856 enum
14857 {
14858 CURSOR_MOVEMENT_SUCCESS,
14859 CURSOR_MOVEMENT_CANNOT_BE_USED,
14860 CURSOR_MOVEMENT_MUST_SCROLL,
14861 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14862 };
14863
14864 static int
14865 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14866 {
14867 struct window *w = XWINDOW (window);
14868 struct frame *f = XFRAME (w->frame);
14869 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14870
14871 #if GLYPH_DEBUG
14872 if (inhibit_try_cursor_movement)
14873 return rc;
14874 #endif
14875
14876 /* Handle case where text has not changed, only point, and it has
14877 not moved off the frame. */
14878 if (/* Point may be in this window. */
14879 PT >= CHARPOS (startp)
14880 /* Selective display hasn't changed. */
14881 && !current_buffer->clip_changed
14882 /* Function force-mode-line-update is used to force a thorough
14883 redisplay. It sets either windows_or_buffers_changed or
14884 update_mode_lines. So don't take a shortcut here for these
14885 cases. */
14886 && !update_mode_lines
14887 && !windows_or_buffers_changed
14888 && !cursor_type_changed
14889 /* Can't use this case if highlighting a region. When a
14890 region exists, cursor movement has to do more than just
14891 set the cursor. */
14892 && !(!NILP (Vtransient_mark_mode)
14893 && !NILP (BVAR (current_buffer, mark_active)))
14894 && NILP (w->region_showing)
14895 && NILP (Vshow_trailing_whitespace)
14896 /* Right after splitting windows, last_point may be nil. */
14897 && INTEGERP (w->last_point)
14898 /* This code is not used for mini-buffer for the sake of the case
14899 of redisplaying to replace an echo area message; since in
14900 that case the mini-buffer contents per se are usually
14901 unchanged. This code is of no real use in the mini-buffer
14902 since the handling of this_line_start_pos, etc., in redisplay
14903 handles the same cases. */
14904 && !EQ (window, minibuf_window)
14905 /* When splitting windows or for new windows, it happens that
14906 redisplay is called with a nil window_end_vpos or one being
14907 larger than the window. This should really be fixed in
14908 window.c. I don't have this on my list, now, so we do
14909 approximately the same as the old redisplay code. --gerd. */
14910 && INTEGERP (w->window_end_vpos)
14911 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14912 && (FRAME_WINDOW_P (f)
14913 || !overlay_arrow_in_current_buffer_p ()))
14914 {
14915 int this_scroll_margin, top_scroll_margin;
14916 struct glyph_row *row = NULL;
14917
14918 #if GLYPH_DEBUG
14919 debug_method_add (w, "cursor movement");
14920 #endif
14921
14922 /* Scroll if point within this distance from the top or bottom
14923 of the window. This is a pixel value. */
14924 if (scroll_margin > 0)
14925 {
14926 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14927 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14928 }
14929 else
14930 this_scroll_margin = 0;
14931
14932 top_scroll_margin = this_scroll_margin;
14933 if (WINDOW_WANTS_HEADER_LINE_P (w))
14934 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14935
14936 /* Start with the row the cursor was displayed during the last
14937 not paused redisplay. Give up if that row is not valid. */
14938 if (w->last_cursor.vpos < 0
14939 || w->last_cursor.vpos >= w->current_matrix->nrows)
14940 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14941 else
14942 {
14943 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14944 if (row->mode_line_p)
14945 ++row;
14946 if (!row->enabled_p)
14947 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14948 }
14949
14950 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14951 {
14952 int scroll_p = 0, must_scroll = 0;
14953 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14954
14955 if (PT > XFASTINT (w->last_point))
14956 {
14957 /* Point has moved forward. */
14958 while (MATRIX_ROW_END_CHARPOS (row) < PT
14959 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14960 {
14961 xassert (row->enabled_p);
14962 ++row;
14963 }
14964
14965 /* If the end position of a row equals the start
14966 position of the next row, and PT is at that position,
14967 we would rather display cursor in the next line. */
14968 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14969 && MATRIX_ROW_END_CHARPOS (row) == PT
14970 && row < w->current_matrix->rows
14971 + w->current_matrix->nrows - 1
14972 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14973 && !cursor_row_p (row))
14974 ++row;
14975
14976 /* If within the scroll margin, scroll. Note that
14977 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14978 the next line would be drawn, and that
14979 this_scroll_margin can be zero. */
14980 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14981 || PT > MATRIX_ROW_END_CHARPOS (row)
14982 /* Line is completely visible last line in window
14983 and PT is to be set in the next line. */
14984 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14985 && PT == MATRIX_ROW_END_CHARPOS (row)
14986 && !row->ends_at_zv_p
14987 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14988 scroll_p = 1;
14989 }
14990 else if (PT < XFASTINT (w->last_point))
14991 {
14992 /* Cursor has to be moved backward. Note that PT >=
14993 CHARPOS (startp) because of the outer if-statement. */
14994 while (!row->mode_line_p
14995 && (MATRIX_ROW_START_CHARPOS (row) > PT
14996 || (MATRIX_ROW_START_CHARPOS (row) == PT
14997 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14998 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14999 row > w->current_matrix->rows
15000 && (row-1)->ends_in_newline_from_string_p))))
15001 && (row->y > top_scroll_margin
15002 || CHARPOS (startp) == BEGV))
15003 {
15004 xassert (row->enabled_p);
15005 --row;
15006 }
15007
15008 /* Consider the following case: Window starts at BEGV,
15009 there is invisible, intangible text at BEGV, so that
15010 display starts at some point START > BEGV. It can
15011 happen that we are called with PT somewhere between
15012 BEGV and START. Try to handle that case. */
15013 if (row < w->current_matrix->rows
15014 || row->mode_line_p)
15015 {
15016 row = w->current_matrix->rows;
15017 if (row->mode_line_p)
15018 ++row;
15019 }
15020
15021 /* Due to newlines in overlay strings, we may have to
15022 skip forward over overlay strings. */
15023 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15024 && MATRIX_ROW_END_CHARPOS (row) == PT
15025 && !cursor_row_p (row))
15026 ++row;
15027
15028 /* If within the scroll margin, scroll. */
15029 if (row->y < top_scroll_margin
15030 && CHARPOS (startp) != BEGV)
15031 scroll_p = 1;
15032 }
15033 else
15034 {
15035 /* Cursor did not move. So don't scroll even if cursor line
15036 is partially visible, as it was so before. */
15037 rc = CURSOR_MOVEMENT_SUCCESS;
15038 }
15039
15040 if (PT < MATRIX_ROW_START_CHARPOS (row)
15041 || PT > MATRIX_ROW_END_CHARPOS (row))
15042 {
15043 /* if PT is not in the glyph row, give up. */
15044 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15045 must_scroll = 1;
15046 }
15047 else if (rc != CURSOR_MOVEMENT_SUCCESS
15048 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15049 {
15050 struct glyph_row *row1;
15051
15052 /* If rows are bidi-reordered and point moved, back up
15053 until we find a row that does not belong to a
15054 continuation line. This is because we must consider
15055 all rows of a continued line as candidates for the
15056 new cursor positioning, since row start and end
15057 positions change non-linearly with vertical position
15058 in such rows. */
15059 /* FIXME: Revisit this when glyph ``spilling'' in
15060 continuation lines' rows is implemented for
15061 bidi-reordered rows. */
15062 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15063 MATRIX_ROW_CONTINUATION_LINE_P (row);
15064 --row)
15065 {
15066 /* If we hit the beginning of the displayed portion
15067 without finding the first row of a continued
15068 line, give up. */
15069 if (row <= row1)
15070 {
15071 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15072 break;
15073 }
15074 xassert (row->enabled_p);
15075 }
15076 }
15077 if (must_scroll)
15078 ;
15079 else if (rc != CURSOR_MOVEMENT_SUCCESS
15080 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15081 /* Make sure this isn't a header line by any chance, since
15082 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15083 && !row->mode_line_p
15084 && make_cursor_line_fully_visible_p)
15085 {
15086 if (PT == MATRIX_ROW_END_CHARPOS (row)
15087 && !row->ends_at_zv_p
15088 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15089 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15090 else if (row->height > window_box_height (w))
15091 {
15092 /* If we end up in a partially visible line, let's
15093 make it fully visible, except when it's taller
15094 than the window, in which case we can't do much
15095 about it. */
15096 *scroll_step = 1;
15097 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15098 }
15099 else
15100 {
15101 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15102 if (!cursor_row_fully_visible_p (w, 0, 1))
15103 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15104 else
15105 rc = CURSOR_MOVEMENT_SUCCESS;
15106 }
15107 }
15108 else if (scroll_p)
15109 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15110 else if (rc != CURSOR_MOVEMENT_SUCCESS
15111 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15112 {
15113 /* With bidi-reordered rows, there could be more than
15114 one candidate row whose start and end positions
15115 occlude point. We need to let set_cursor_from_row
15116 find the best candidate. */
15117 /* FIXME: Revisit this when glyph ``spilling'' in
15118 continuation lines' rows is implemented for
15119 bidi-reordered rows. */
15120 int rv = 0;
15121
15122 do
15123 {
15124 int at_zv_p = 0, exact_match_p = 0;
15125
15126 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15127 && PT <= MATRIX_ROW_END_CHARPOS (row)
15128 && cursor_row_p (row))
15129 rv |= set_cursor_from_row (w, row, w->current_matrix,
15130 0, 0, 0, 0);
15131 /* As soon as we've found the exact match for point,
15132 or the first suitable row whose ends_at_zv_p flag
15133 is set, we are done. */
15134 at_zv_p =
15135 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15136 if (rv && !at_zv_p
15137 && w->cursor.hpos >= 0
15138 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15139 w->cursor.vpos))
15140 {
15141 struct glyph_row *candidate =
15142 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15143 struct glyph *g =
15144 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15145 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15146
15147 exact_match_p =
15148 (BUFFERP (g->object) && g->charpos == PT)
15149 || (INTEGERP (g->object)
15150 && (g->charpos == PT
15151 || (g->charpos == 0 && endpos - 1 == PT)));
15152 }
15153 if (rv && (at_zv_p || exact_match_p))
15154 {
15155 rc = CURSOR_MOVEMENT_SUCCESS;
15156 break;
15157 }
15158 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15159 break;
15160 ++row;
15161 }
15162 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15163 || row->continued_p)
15164 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15165 || (MATRIX_ROW_START_CHARPOS (row) == PT
15166 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15167 /* If we didn't find any candidate rows, or exited the
15168 loop before all the candidates were examined, signal
15169 to the caller that this method failed. */
15170 if (rc != CURSOR_MOVEMENT_SUCCESS
15171 && !(rv
15172 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15173 && !row->continued_p))
15174 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15175 else if (rv)
15176 rc = CURSOR_MOVEMENT_SUCCESS;
15177 }
15178 else
15179 {
15180 do
15181 {
15182 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15183 {
15184 rc = CURSOR_MOVEMENT_SUCCESS;
15185 break;
15186 }
15187 ++row;
15188 }
15189 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15190 && MATRIX_ROW_START_CHARPOS (row) == PT
15191 && cursor_row_p (row));
15192 }
15193 }
15194 }
15195
15196 return rc;
15197 }
15198
15199 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15200 static
15201 #endif
15202 void
15203 set_vertical_scroll_bar (struct window *w)
15204 {
15205 ptrdiff_t start, end, whole;
15206
15207 /* Calculate the start and end positions for the current window.
15208 At some point, it would be nice to choose between scrollbars
15209 which reflect the whole buffer size, with special markers
15210 indicating narrowing, and scrollbars which reflect only the
15211 visible region.
15212
15213 Note that mini-buffers sometimes aren't displaying any text. */
15214 if (!MINI_WINDOW_P (w)
15215 || (w == XWINDOW (minibuf_window)
15216 && NILP (echo_area_buffer[0])))
15217 {
15218 struct buffer *buf = XBUFFER (w->buffer);
15219 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15220 start = marker_position (w->start) - BUF_BEGV (buf);
15221 /* I don't think this is guaranteed to be right. For the
15222 moment, we'll pretend it is. */
15223 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15224
15225 if (end < start)
15226 end = start;
15227 if (whole < (end - start))
15228 whole = end - start;
15229 }
15230 else
15231 start = end = whole = 0;
15232
15233 /* Indicate what this scroll bar ought to be displaying now. */
15234 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15235 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15236 (w, end - start, whole, start);
15237 }
15238
15239
15240 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15241 selected_window is redisplayed.
15242
15243 We can return without actually redisplaying the window if
15244 fonts_changed_p is nonzero. In that case, redisplay_internal will
15245 retry. */
15246
15247 static void
15248 redisplay_window (Lisp_Object window, int just_this_one_p)
15249 {
15250 struct window *w = XWINDOW (window);
15251 struct frame *f = XFRAME (w->frame);
15252 struct buffer *buffer = XBUFFER (w->buffer);
15253 struct buffer *old = current_buffer;
15254 struct text_pos lpoint, opoint, startp;
15255 int update_mode_line;
15256 int tem;
15257 struct it it;
15258 /* Record it now because it's overwritten. */
15259 int current_matrix_up_to_date_p = 0;
15260 int used_current_matrix_p = 0;
15261 /* This is less strict than current_matrix_up_to_date_p.
15262 It indicates that the buffer contents and narrowing are unchanged. */
15263 int buffer_unchanged_p = 0;
15264 int temp_scroll_step = 0;
15265 ptrdiff_t count = SPECPDL_INDEX ();
15266 int rc;
15267 int centering_position = -1;
15268 int last_line_misfit = 0;
15269 ptrdiff_t beg_unchanged, end_unchanged;
15270
15271 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15272 opoint = lpoint;
15273
15274 /* W must be a leaf window here. */
15275 xassert (!NILP (w->buffer));
15276 #if GLYPH_DEBUG
15277 *w->desired_matrix->method = 0;
15278 #endif
15279
15280 restart:
15281 reconsider_clip_changes (w, buffer);
15282
15283 /* Has the mode line to be updated? */
15284 update_mode_line = (!NILP (w->update_mode_line)
15285 || update_mode_lines
15286 || buffer->clip_changed
15287 || buffer->prevent_redisplay_optimizations_p);
15288
15289 if (MINI_WINDOW_P (w))
15290 {
15291 if (w == XWINDOW (echo_area_window)
15292 && !NILP (echo_area_buffer[0]))
15293 {
15294 if (update_mode_line)
15295 /* We may have to update a tty frame's menu bar or a
15296 tool-bar. Example `M-x C-h C-h C-g'. */
15297 goto finish_menu_bars;
15298 else
15299 /* We've already displayed the echo area glyphs in this window. */
15300 goto finish_scroll_bars;
15301 }
15302 else if ((w != XWINDOW (minibuf_window)
15303 || minibuf_level == 0)
15304 /* When buffer is nonempty, redisplay window normally. */
15305 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15306 /* Quail displays non-mini buffers in minibuffer window.
15307 In that case, redisplay the window normally. */
15308 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15309 {
15310 /* W is a mini-buffer window, but it's not active, so clear
15311 it. */
15312 int yb = window_text_bottom_y (w);
15313 struct glyph_row *row;
15314 int y;
15315
15316 for (y = 0, row = w->desired_matrix->rows;
15317 y < yb;
15318 y += row->height, ++row)
15319 blank_row (w, row, y);
15320 goto finish_scroll_bars;
15321 }
15322
15323 clear_glyph_matrix (w->desired_matrix);
15324 }
15325
15326 /* Otherwise set up data on this window; select its buffer and point
15327 value. */
15328 /* Really select the buffer, for the sake of buffer-local
15329 variables. */
15330 set_buffer_internal_1 (XBUFFER (w->buffer));
15331
15332 current_matrix_up_to_date_p
15333 = (!NILP (w->window_end_valid)
15334 && !current_buffer->clip_changed
15335 && !current_buffer->prevent_redisplay_optimizations_p
15336 && XFASTINT (w->last_modified) >= MODIFF
15337 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15338
15339 /* Run the window-bottom-change-functions
15340 if it is possible that the text on the screen has changed
15341 (either due to modification of the text, or any other reason). */
15342 if (!current_matrix_up_to_date_p
15343 && !NILP (Vwindow_text_change_functions))
15344 {
15345 safe_run_hooks (Qwindow_text_change_functions);
15346 goto restart;
15347 }
15348
15349 beg_unchanged = BEG_UNCHANGED;
15350 end_unchanged = END_UNCHANGED;
15351
15352 SET_TEXT_POS (opoint, PT, PT_BYTE);
15353
15354 specbind (Qinhibit_point_motion_hooks, Qt);
15355
15356 buffer_unchanged_p
15357 = (!NILP (w->window_end_valid)
15358 && !current_buffer->clip_changed
15359 && XFASTINT (w->last_modified) >= MODIFF
15360 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15361
15362 /* When windows_or_buffers_changed is non-zero, we can't rely on
15363 the window end being valid, so set it to nil there. */
15364 if (windows_or_buffers_changed)
15365 {
15366 /* If window starts on a continuation line, maybe adjust the
15367 window start in case the window's width changed. */
15368 if (XMARKER (w->start)->buffer == current_buffer)
15369 compute_window_start_on_continuation_line (w);
15370
15371 w->window_end_valid = Qnil;
15372 }
15373
15374 /* Some sanity checks. */
15375 CHECK_WINDOW_END (w);
15376 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15377 abort ();
15378 if (BYTEPOS (opoint) < CHARPOS (opoint))
15379 abort ();
15380
15381 /* If %c is in mode line, update it if needed. */
15382 if (!NILP (w->column_number_displayed)
15383 /* This alternative quickly identifies a common case
15384 where no change is needed. */
15385 && !(PT == XFASTINT (w->last_point)
15386 && XFASTINT (w->last_modified) >= MODIFF
15387 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15388 && (XFASTINT (w->column_number_displayed) != current_column ()))
15389 update_mode_line = 1;
15390
15391 /* Count number of windows showing the selected buffer. An indirect
15392 buffer counts as its base buffer. */
15393 if (!just_this_one_p)
15394 {
15395 struct buffer *current_base, *window_base;
15396 current_base = current_buffer;
15397 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15398 if (current_base->base_buffer)
15399 current_base = current_base->base_buffer;
15400 if (window_base->base_buffer)
15401 window_base = window_base->base_buffer;
15402 if (current_base == window_base)
15403 buffer_shared++;
15404 }
15405
15406 /* Point refers normally to the selected window. For any other
15407 window, set up appropriate value. */
15408 if (!EQ (window, selected_window))
15409 {
15410 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15411 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15412 if (new_pt < BEGV)
15413 {
15414 new_pt = BEGV;
15415 new_pt_byte = BEGV_BYTE;
15416 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15417 }
15418 else if (new_pt > (ZV - 1))
15419 {
15420 new_pt = ZV;
15421 new_pt_byte = ZV_BYTE;
15422 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15423 }
15424
15425 /* We don't use SET_PT so that the point-motion hooks don't run. */
15426 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15427 }
15428
15429 /* If any of the character widths specified in the display table
15430 have changed, invalidate the width run cache. It's true that
15431 this may be a bit late to catch such changes, but the rest of
15432 redisplay goes (non-fatally) haywire when the display table is
15433 changed, so why should we worry about doing any better? */
15434 if (current_buffer->width_run_cache)
15435 {
15436 struct Lisp_Char_Table *disptab = buffer_display_table ();
15437
15438 if (! disptab_matches_widthtab (disptab,
15439 XVECTOR (BVAR (current_buffer, width_table))))
15440 {
15441 invalidate_region_cache (current_buffer,
15442 current_buffer->width_run_cache,
15443 BEG, Z);
15444 recompute_width_table (current_buffer, disptab);
15445 }
15446 }
15447
15448 /* If window-start is screwed up, choose a new one. */
15449 if (XMARKER (w->start)->buffer != current_buffer)
15450 goto recenter;
15451
15452 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15453
15454 /* If someone specified a new starting point but did not insist,
15455 check whether it can be used. */
15456 if (!NILP (w->optional_new_start)
15457 && CHARPOS (startp) >= BEGV
15458 && CHARPOS (startp) <= ZV)
15459 {
15460 w->optional_new_start = Qnil;
15461 start_display (&it, w, startp);
15462 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15463 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15464 if (IT_CHARPOS (it) == PT)
15465 w->force_start = Qt;
15466 /* IT may overshoot PT if text at PT is invisible. */
15467 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15468 w->force_start = Qt;
15469 }
15470
15471 force_start:
15472
15473 /* Handle case where place to start displaying has been specified,
15474 unless the specified location is outside the accessible range. */
15475 if (!NILP (w->force_start)
15476 || w->frozen_window_start_p)
15477 {
15478 /* We set this later on if we have to adjust point. */
15479 int new_vpos = -1;
15480
15481 w->force_start = Qnil;
15482 w->vscroll = 0;
15483 w->window_end_valid = Qnil;
15484
15485 /* Forget any recorded base line for line number display. */
15486 if (!buffer_unchanged_p)
15487 w->base_line_number = Qnil;
15488
15489 /* Redisplay the mode line. Select the buffer properly for that.
15490 Also, run the hook window-scroll-functions
15491 because we have scrolled. */
15492 /* Note, we do this after clearing force_start because
15493 if there's an error, it is better to forget about force_start
15494 than to get into an infinite loop calling the hook functions
15495 and having them get more errors. */
15496 if (!update_mode_line
15497 || ! NILP (Vwindow_scroll_functions))
15498 {
15499 update_mode_line = 1;
15500 w->update_mode_line = Qt;
15501 startp = run_window_scroll_functions (window, startp);
15502 }
15503
15504 w->last_modified = make_number (0);
15505 w->last_overlay_modified = make_number (0);
15506 if (CHARPOS (startp) < BEGV)
15507 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15508 else if (CHARPOS (startp) > ZV)
15509 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15510
15511 /* Redisplay, then check if cursor has been set during the
15512 redisplay. Give up if new fonts were loaded. */
15513 /* We used to issue a CHECK_MARGINS argument to try_window here,
15514 but this causes scrolling to fail when point begins inside
15515 the scroll margin (bug#148) -- cyd */
15516 if (!try_window (window, startp, 0))
15517 {
15518 w->force_start = Qt;
15519 clear_glyph_matrix (w->desired_matrix);
15520 goto need_larger_matrices;
15521 }
15522
15523 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15524 {
15525 /* If point does not appear, try to move point so it does
15526 appear. The desired matrix has been built above, so we
15527 can use it here. */
15528 new_vpos = window_box_height (w) / 2;
15529 }
15530
15531 if (!cursor_row_fully_visible_p (w, 0, 0))
15532 {
15533 /* Point does appear, but on a line partly visible at end of window.
15534 Move it back to a fully-visible line. */
15535 new_vpos = window_box_height (w);
15536 }
15537
15538 /* If we need to move point for either of the above reasons,
15539 now actually do it. */
15540 if (new_vpos >= 0)
15541 {
15542 struct glyph_row *row;
15543
15544 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15545 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15546 ++row;
15547
15548 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15549 MATRIX_ROW_START_BYTEPOS (row));
15550
15551 if (w != XWINDOW (selected_window))
15552 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15553 else if (current_buffer == old)
15554 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15555
15556 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15557
15558 /* If we are highlighting the region, then we just changed
15559 the region, so redisplay to show it. */
15560 if (!NILP (Vtransient_mark_mode)
15561 && !NILP (BVAR (current_buffer, mark_active)))
15562 {
15563 clear_glyph_matrix (w->desired_matrix);
15564 if (!try_window (window, startp, 0))
15565 goto need_larger_matrices;
15566 }
15567 }
15568
15569 #if GLYPH_DEBUG
15570 debug_method_add (w, "forced window start");
15571 #endif
15572 goto done;
15573 }
15574
15575 /* Handle case where text has not changed, only point, and it has
15576 not moved off the frame, and we are not retrying after hscroll.
15577 (current_matrix_up_to_date_p is nonzero when retrying.) */
15578 if (current_matrix_up_to_date_p
15579 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15580 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15581 {
15582 switch (rc)
15583 {
15584 case CURSOR_MOVEMENT_SUCCESS:
15585 used_current_matrix_p = 1;
15586 goto done;
15587
15588 case CURSOR_MOVEMENT_MUST_SCROLL:
15589 goto try_to_scroll;
15590
15591 default:
15592 abort ();
15593 }
15594 }
15595 /* If current starting point was originally the beginning of a line
15596 but no longer is, find a new starting point. */
15597 else if (!NILP (w->start_at_line_beg)
15598 && !(CHARPOS (startp) <= BEGV
15599 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15600 {
15601 #if GLYPH_DEBUG
15602 debug_method_add (w, "recenter 1");
15603 #endif
15604 goto recenter;
15605 }
15606
15607 /* Try scrolling with try_window_id. Value is > 0 if update has
15608 been done, it is -1 if we know that the same window start will
15609 not work. It is 0 if unsuccessful for some other reason. */
15610 else if ((tem = try_window_id (w)) != 0)
15611 {
15612 #if GLYPH_DEBUG
15613 debug_method_add (w, "try_window_id %d", tem);
15614 #endif
15615
15616 if (fonts_changed_p)
15617 goto need_larger_matrices;
15618 if (tem > 0)
15619 goto done;
15620
15621 /* Otherwise try_window_id has returned -1 which means that we
15622 don't want the alternative below this comment to execute. */
15623 }
15624 else if (CHARPOS (startp) >= BEGV
15625 && CHARPOS (startp) <= ZV
15626 && PT >= CHARPOS (startp)
15627 && (CHARPOS (startp) < ZV
15628 /* Avoid starting at end of buffer. */
15629 || CHARPOS (startp) == BEGV
15630 || (XFASTINT (w->last_modified) >= MODIFF
15631 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15632 {
15633 int d1, d2, d3, d4, d5, d6;
15634
15635 /* If first window line is a continuation line, and window start
15636 is inside the modified region, but the first change is before
15637 current window start, we must select a new window start.
15638
15639 However, if this is the result of a down-mouse event (e.g. by
15640 extending the mouse-drag-overlay), we don't want to select a
15641 new window start, since that would change the position under
15642 the mouse, resulting in an unwanted mouse-movement rather
15643 than a simple mouse-click. */
15644 if (NILP (w->start_at_line_beg)
15645 && NILP (do_mouse_tracking)
15646 && CHARPOS (startp) > BEGV
15647 && CHARPOS (startp) > BEG + beg_unchanged
15648 && CHARPOS (startp) <= Z - end_unchanged
15649 /* Even if w->start_at_line_beg is nil, a new window may
15650 start at a line_beg, since that's how set_buffer_window
15651 sets it. So, we need to check the return value of
15652 compute_window_start_on_continuation_line. (See also
15653 bug#197). */
15654 && XMARKER (w->start)->buffer == current_buffer
15655 && compute_window_start_on_continuation_line (w)
15656 /* It doesn't make sense to force the window start like we
15657 do at label force_start if it is already known that point
15658 will not be visible in the resulting window, because
15659 doing so will move point from its correct position
15660 instead of scrolling the window to bring point into view.
15661 See bug#9324. */
15662 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15663 {
15664 w->force_start = Qt;
15665 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15666 goto force_start;
15667 }
15668
15669 #if GLYPH_DEBUG
15670 debug_method_add (w, "same window start");
15671 #endif
15672
15673 /* Try to redisplay starting at same place as before.
15674 If point has not moved off frame, accept the results. */
15675 if (!current_matrix_up_to_date_p
15676 /* Don't use try_window_reusing_current_matrix in this case
15677 because a window scroll function can have changed the
15678 buffer. */
15679 || !NILP (Vwindow_scroll_functions)
15680 || MINI_WINDOW_P (w)
15681 || !(used_current_matrix_p
15682 = try_window_reusing_current_matrix (w)))
15683 {
15684 IF_DEBUG (debug_method_add (w, "1"));
15685 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15686 /* -1 means we need to scroll.
15687 0 means we need new matrices, but fonts_changed_p
15688 is set in that case, so we will detect it below. */
15689 goto try_to_scroll;
15690 }
15691
15692 if (fonts_changed_p)
15693 goto need_larger_matrices;
15694
15695 if (w->cursor.vpos >= 0)
15696 {
15697 if (!just_this_one_p
15698 || current_buffer->clip_changed
15699 || BEG_UNCHANGED < CHARPOS (startp))
15700 /* Forget any recorded base line for line number display. */
15701 w->base_line_number = Qnil;
15702
15703 if (!cursor_row_fully_visible_p (w, 1, 0))
15704 {
15705 clear_glyph_matrix (w->desired_matrix);
15706 last_line_misfit = 1;
15707 }
15708 /* Drop through and scroll. */
15709 else
15710 goto done;
15711 }
15712 else
15713 clear_glyph_matrix (w->desired_matrix);
15714 }
15715
15716 try_to_scroll:
15717
15718 w->last_modified = make_number (0);
15719 w->last_overlay_modified = make_number (0);
15720
15721 /* Redisplay the mode line. Select the buffer properly for that. */
15722 if (!update_mode_line)
15723 {
15724 update_mode_line = 1;
15725 w->update_mode_line = Qt;
15726 }
15727
15728 /* Try to scroll by specified few lines. */
15729 if ((scroll_conservatively
15730 || emacs_scroll_step
15731 || temp_scroll_step
15732 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15733 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15734 && CHARPOS (startp) >= BEGV
15735 && CHARPOS (startp) <= ZV)
15736 {
15737 /* The function returns -1 if new fonts were loaded, 1 if
15738 successful, 0 if not successful. */
15739 int ss = try_scrolling (window, just_this_one_p,
15740 scroll_conservatively,
15741 emacs_scroll_step,
15742 temp_scroll_step, last_line_misfit);
15743 switch (ss)
15744 {
15745 case SCROLLING_SUCCESS:
15746 goto done;
15747
15748 case SCROLLING_NEED_LARGER_MATRICES:
15749 goto need_larger_matrices;
15750
15751 case SCROLLING_FAILED:
15752 break;
15753
15754 default:
15755 abort ();
15756 }
15757 }
15758
15759 /* Finally, just choose a place to start which positions point
15760 according to user preferences. */
15761
15762 recenter:
15763
15764 #if GLYPH_DEBUG
15765 debug_method_add (w, "recenter");
15766 #endif
15767
15768 /* w->vscroll = 0; */
15769
15770 /* Forget any previously recorded base line for line number display. */
15771 if (!buffer_unchanged_p)
15772 w->base_line_number = Qnil;
15773
15774 /* Determine the window start relative to point. */
15775 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15776 it.current_y = it.last_visible_y;
15777 if (centering_position < 0)
15778 {
15779 int margin =
15780 scroll_margin > 0
15781 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15782 : 0;
15783 ptrdiff_t margin_pos = CHARPOS (startp);
15784 Lisp_Object aggressive;
15785 int scrolling_up;
15786
15787 /* If there is a scroll margin at the top of the window, find
15788 its character position. */
15789 if (margin
15790 /* Cannot call start_display if startp is not in the
15791 accessible region of the buffer. This can happen when we
15792 have just switched to a different buffer and/or changed
15793 its restriction. In that case, startp is initialized to
15794 the character position 1 (BEGV) because we did not yet
15795 have chance to display the buffer even once. */
15796 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15797 {
15798 struct it it1;
15799 void *it1data = NULL;
15800
15801 SAVE_IT (it1, it, it1data);
15802 start_display (&it1, w, startp);
15803 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15804 margin_pos = IT_CHARPOS (it1);
15805 RESTORE_IT (&it, &it, it1data);
15806 }
15807 scrolling_up = PT > margin_pos;
15808 aggressive =
15809 scrolling_up
15810 ? BVAR (current_buffer, scroll_up_aggressively)
15811 : BVAR (current_buffer, scroll_down_aggressively);
15812
15813 if (!MINI_WINDOW_P (w)
15814 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15815 {
15816 int pt_offset = 0;
15817
15818 /* Setting scroll-conservatively overrides
15819 scroll-*-aggressively. */
15820 if (!scroll_conservatively && NUMBERP (aggressive))
15821 {
15822 double float_amount = XFLOATINT (aggressive);
15823
15824 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15825 if (pt_offset == 0 && float_amount > 0)
15826 pt_offset = 1;
15827 if (pt_offset && margin > 0)
15828 margin -= 1;
15829 }
15830 /* Compute how much to move the window start backward from
15831 point so that point will be displayed where the user
15832 wants it. */
15833 if (scrolling_up)
15834 {
15835 centering_position = it.last_visible_y;
15836 if (pt_offset)
15837 centering_position -= pt_offset;
15838 centering_position -=
15839 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15840 + WINDOW_HEADER_LINE_HEIGHT (w);
15841 /* Don't let point enter the scroll margin near top of
15842 the window. */
15843 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15844 centering_position = margin * FRAME_LINE_HEIGHT (f);
15845 }
15846 else
15847 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15848 }
15849 else
15850 /* Set the window start half the height of the window backward
15851 from point. */
15852 centering_position = window_box_height (w) / 2;
15853 }
15854 move_it_vertically_backward (&it, centering_position);
15855
15856 xassert (IT_CHARPOS (it) >= BEGV);
15857
15858 /* The function move_it_vertically_backward may move over more
15859 than the specified y-distance. If it->w is small, e.g. a
15860 mini-buffer window, we may end up in front of the window's
15861 display area. Start displaying at the start of the line
15862 containing PT in this case. */
15863 if (it.current_y <= 0)
15864 {
15865 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15866 move_it_vertically_backward (&it, 0);
15867 it.current_y = 0;
15868 }
15869
15870 it.current_x = it.hpos = 0;
15871
15872 /* Set the window start position here explicitly, to avoid an
15873 infinite loop in case the functions in window-scroll-functions
15874 get errors. */
15875 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15876
15877 /* Run scroll hooks. */
15878 startp = run_window_scroll_functions (window, it.current.pos);
15879
15880 /* Redisplay the window. */
15881 if (!current_matrix_up_to_date_p
15882 || windows_or_buffers_changed
15883 || cursor_type_changed
15884 /* Don't use try_window_reusing_current_matrix in this case
15885 because it can have changed the buffer. */
15886 || !NILP (Vwindow_scroll_functions)
15887 || !just_this_one_p
15888 || MINI_WINDOW_P (w)
15889 || !(used_current_matrix_p
15890 = try_window_reusing_current_matrix (w)))
15891 try_window (window, startp, 0);
15892
15893 /* If new fonts have been loaded (due to fontsets), give up. We
15894 have to start a new redisplay since we need to re-adjust glyph
15895 matrices. */
15896 if (fonts_changed_p)
15897 goto need_larger_matrices;
15898
15899 /* If cursor did not appear assume that the middle of the window is
15900 in the first line of the window. Do it again with the next line.
15901 (Imagine a window of height 100, displaying two lines of height
15902 60. Moving back 50 from it->last_visible_y will end in the first
15903 line.) */
15904 if (w->cursor.vpos < 0)
15905 {
15906 if (!NILP (w->window_end_valid)
15907 && PT >= Z - XFASTINT (w->window_end_pos))
15908 {
15909 clear_glyph_matrix (w->desired_matrix);
15910 move_it_by_lines (&it, 1);
15911 try_window (window, it.current.pos, 0);
15912 }
15913 else if (PT < IT_CHARPOS (it))
15914 {
15915 clear_glyph_matrix (w->desired_matrix);
15916 move_it_by_lines (&it, -1);
15917 try_window (window, it.current.pos, 0);
15918 }
15919 else
15920 {
15921 /* Not much we can do about it. */
15922 }
15923 }
15924
15925 /* Consider the following case: Window starts at BEGV, there is
15926 invisible, intangible text at BEGV, so that display starts at
15927 some point START > BEGV. It can happen that we are called with
15928 PT somewhere between BEGV and START. Try to handle that case. */
15929 if (w->cursor.vpos < 0)
15930 {
15931 struct glyph_row *row = w->current_matrix->rows;
15932 if (row->mode_line_p)
15933 ++row;
15934 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15935 }
15936
15937 if (!cursor_row_fully_visible_p (w, 0, 0))
15938 {
15939 /* If vscroll is enabled, disable it and try again. */
15940 if (w->vscroll)
15941 {
15942 w->vscroll = 0;
15943 clear_glyph_matrix (w->desired_matrix);
15944 goto recenter;
15945 }
15946
15947 /* Users who set scroll-conservatively to a large number want
15948 point just above/below the scroll margin. If we ended up
15949 with point's row partially visible, move the window start to
15950 make that row fully visible and out of the margin. */
15951 if (scroll_conservatively > SCROLL_LIMIT)
15952 {
15953 int margin =
15954 scroll_margin > 0
15955 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15956 : 0;
15957 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15958
15959 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15960 clear_glyph_matrix (w->desired_matrix);
15961 if (1 == try_window (window, it.current.pos,
15962 TRY_WINDOW_CHECK_MARGINS))
15963 goto done;
15964 }
15965
15966 /* If centering point failed to make the whole line visible,
15967 put point at the top instead. That has to make the whole line
15968 visible, if it can be done. */
15969 if (centering_position == 0)
15970 goto done;
15971
15972 clear_glyph_matrix (w->desired_matrix);
15973 centering_position = 0;
15974 goto recenter;
15975 }
15976
15977 done:
15978
15979 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15980 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15981 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15982 ? Qt : Qnil);
15983
15984 /* Display the mode line, if we must. */
15985 if ((update_mode_line
15986 /* If window not full width, must redo its mode line
15987 if (a) the window to its side is being redone and
15988 (b) we do a frame-based redisplay. This is a consequence
15989 of how inverted lines are drawn in frame-based redisplay. */
15990 || (!just_this_one_p
15991 && !FRAME_WINDOW_P (f)
15992 && !WINDOW_FULL_WIDTH_P (w))
15993 /* Line number to display. */
15994 || INTEGERP (w->base_line_pos)
15995 /* Column number is displayed and different from the one displayed. */
15996 || (!NILP (w->column_number_displayed)
15997 && (XFASTINT (w->column_number_displayed) != current_column ())))
15998 /* This means that the window has a mode line. */
15999 && (WINDOW_WANTS_MODELINE_P (w)
16000 || WINDOW_WANTS_HEADER_LINE_P (w)))
16001 {
16002 display_mode_lines (w);
16003
16004 /* If mode line height has changed, arrange for a thorough
16005 immediate redisplay using the correct mode line height. */
16006 if (WINDOW_WANTS_MODELINE_P (w)
16007 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16008 {
16009 fonts_changed_p = 1;
16010 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16011 = DESIRED_MODE_LINE_HEIGHT (w);
16012 }
16013
16014 /* If header line height has changed, arrange for a thorough
16015 immediate redisplay using the correct header line height. */
16016 if (WINDOW_WANTS_HEADER_LINE_P (w)
16017 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16018 {
16019 fonts_changed_p = 1;
16020 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16021 = DESIRED_HEADER_LINE_HEIGHT (w);
16022 }
16023
16024 if (fonts_changed_p)
16025 goto need_larger_matrices;
16026 }
16027
16028 if (!line_number_displayed
16029 && !BUFFERP (w->base_line_pos))
16030 {
16031 w->base_line_pos = Qnil;
16032 w->base_line_number = Qnil;
16033 }
16034
16035 finish_menu_bars:
16036
16037 /* When we reach a frame's selected window, redo the frame's menu bar. */
16038 if (update_mode_line
16039 && EQ (FRAME_SELECTED_WINDOW (f), window))
16040 {
16041 int redisplay_menu_p = 0;
16042
16043 if (FRAME_WINDOW_P (f))
16044 {
16045 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16046 || defined (HAVE_NS) || defined (USE_GTK)
16047 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16048 #else
16049 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16050 #endif
16051 }
16052 else
16053 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16054
16055 if (redisplay_menu_p)
16056 display_menu_bar (w);
16057
16058 #ifdef HAVE_WINDOW_SYSTEM
16059 if (FRAME_WINDOW_P (f))
16060 {
16061 #if defined (USE_GTK) || defined (HAVE_NS)
16062 if (FRAME_EXTERNAL_TOOL_BAR (f))
16063 redisplay_tool_bar (f);
16064 #else
16065 if (WINDOWP (f->tool_bar_window)
16066 && (FRAME_TOOL_BAR_LINES (f) > 0
16067 || !NILP (Vauto_resize_tool_bars))
16068 && redisplay_tool_bar (f))
16069 ignore_mouse_drag_p = 1;
16070 #endif
16071 }
16072 #endif
16073 }
16074
16075 #ifdef HAVE_WINDOW_SYSTEM
16076 if (FRAME_WINDOW_P (f)
16077 && update_window_fringes (w, (just_this_one_p
16078 || (!used_current_matrix_p && !overlay_arrow_seen)
16079 || w->pseudo_window_p)))
16080 {
16081 update_begin (f);
16082 BLOCK_INPUT;
16083 if (draw_window_fringes (w, 1))
16084 x_draw_vertical_border (w);
16085 UNBLOCK_INPUT;
16086 update_end (f);
16087 }
16088 #endif /* HAVE_WINDOW_SYSTEM */
16089
16090 /* We go to this label, with fonts_changed_p nonzero,
16091 if it is necessary to try again using larger glyph matrices.
16092 We have to redeem the scroll bar even in this case,
16093 because the loop in redisplay_internal expects that. */
16094 need_larger_matrices:
16095 ;
16096 finish_scroll_bars:
16097
16098 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16099 {
16100 /* Set the thumb's position and size. */
16101 set_vertical_scroll_bar (w);
16102
16103 /* Note that we actually used the scroll bar attached to this
16104 window, so it shouldn't be deleted at the end of redisplay. */
16105 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16106 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16107 }
16108
16109 /* Restore current_buffer and value of point in it. The window
16110 update may have changed the buffer, so first make sure `opoint'
16111 is still valid (Bug#6177). */
16112 if (CHARPOS (opoint) < BEGV)
16113 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16114 else if (CHARPOS (opoint) > ZV)
16115 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16116 else
16117 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16118
16119 set_buffer_internal_1 (old);
16120 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16121 shorter. This can be caused by log truncation in *Messages*. */
16122 if (CHARPOS (lpoint) <= ZV)
16123 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16124
16125 unbind_to (count, Qnil);
16126 }
16127
16128
16129 /* Build the complete desired matrix of WINDOW with a window start
16130 buffer position POS.
16131
16132 Value is 1 if successful. It is zero if fonts were loaded during
16133 redisplay which makes re-adjusting glyph matrices necessary, and -1
16134 if point would appear in the scroll margins.
16135 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16136 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16137 set in FLAGS.) */
16138
16139 int
16140 try_window (Lisp_Object window, struct text_pos pos, int flags)
16141 {
16142 struct window *w = XWINDOW (window);
16143 struct it it;
16144 struct glyph_row *last_text_row = NULL;
16145 struct frame *f = XFRAME (w->frame);
16146
16147 /* Make POS the new window start. */
16148 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16149
16150 /* Mark cursor position as unknown. No overlay arrow seen. */
16151 w->cursor.vpos = -1;
16152 overlay_arrow_seen = 0;
16153
16154 /* Initialize iterator and info to start at POS. */
16155 start_display (&it, w, pos);
16156
16157 /* Display all lines of W. */
16158 while (it.current_y < it.last_visible_y)
16159 {
16160 if (display_line (&it))
16161 last_text_row = it.glyph_row - 1;
16162 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16163 return 0;
16164 }
16165
16166 /* Don't let the cursor end in the scroll margins. */
16167 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16168 && !MINI_WINDOW_P (w))
16169 {
16170 int this_scroll_margin;
16171
16172 if (scroll_margin > 0)
16173 {
16174 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16175 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16176 }
16177 else
16178 this_scroll_margin = 0;
16179
16180 if ((w->cursor.y >= 0 /* not vscrolled */
16181 && w->cursor.y < this_scroll_margin
16182 && CHARPOS (pos) > BEGV
16183 && IT_CHARPOS (it) < ZV)
16184 /* rms: considering make_cursor_line_fully_visible_p here
16185 seems to give wrong results. We don't want to recenter
16186 when the last line is partly visible, we want to allow
16187 that case to be handled in the usual way. */
16188 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16189 {
16190 w->cursor.vpos = -1;
16191 clear_glyph_matrix (w->desired_matrix);
16192 return -1;
16193 }
16194 }
16195
16196 /* If bottom moved off end of frame, change mode line percentage. */
16197 if (XFASTINT (w->window_end_pos) <= 0
16198 && Z != IT_CHARPOS (it))
16199 w->update_mode_line = Qt;
16200
16201 /* Set window_end_pos to the offset of the last character displayed
16202 on the window from the end of current_buffer. Set
16203 window_end_vpos to its row number. */
16204 if (last_text_row)
16205 {
16206 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16207 w->window_end_bytepos
16208 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16209 w->window_end_pos
16210 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16211 w->window_end_vpos
16212 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16213 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16214 ->displays_text_p);
16215 }
16216 else
16217 {
16218 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16219 w->window_end_pos = make_number (Z - ZV);
16220 w->window_end_vpos = make_number (0);
16221 }
16222
16223 /* But that is not valid info until redisplay finishes. */
16224 w->window_end_valid = Qnil;
16225 return 1;
16226 }
16227
16228
16229 \f
16230 /************************************************************************
16231 Window redisplay reusing current matrix when buffer has not changed
16232 ************************************************************************/
16233
16234 /* Try redisplay of window W showing an unchanged buffer with a
16235 different window start than the last time it was displayed by
16236 reusing its current matrix. Value is non-zero if successful.
16237 W->start is the new window start. */
16238
16239 static int
16240 try_window_reusing_current_matrix (struct window *w)
16241 {
16242 struct frame *f = XFRAME (w->frame);
16243 struct glyph_row *bottom_row;
16244 struct it it;
16245 struct run run;
16246 struct text_pos start, new_start;
16247 int nrows_scrolled, i;
16248 struct glyph_row *last_text_row;
16249 struct glyph_row *last_reused_text_row;
16250 struct glyph_row *start_row;
16251 int start_vpos, min_y, max_y;
16252
16253 #if GLYPH_DEBUG
16254 if (inhibit_try_window_reusing)
16255 return 0;
16256 #endif
16257
16258 if (/* This function doesn't handle terminal frames. */
16259 !FRAME_WINDOW_P (f)
16260 /* Don't try to reuse the display if windows have been split
16261 or such. */
16262 || windows_or_buffers_changed
16263 || cursor_type_changed)
16264 return 0;
16265
16266 /* Can't do this if region may have changed. */
16267 if ((!NILP (Vtransient_mark_mode)
16268 && !NILP (BVAR (current_buffer, mark_active)))
16269 || !NILP (w->region_showing)
16270 || !NILP (Vshow_trailing_whitespace))
16271 return 0;
16272
16273 /* If top-line visibility has changed, give up. */
16274 if (WINDOW_WANTS_HEADER_LINE_P (w)
16275 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16276 return 0;
16277
16278 /* Give up if old or new display is scrolled vertically. We could
16279 make this function handle this, but right now it doesn't. */
16280 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16281 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16282 return 0;
16283
16284 /* The variable new_start now holds the new window start. The old
16285 start `start' can be determined from the current matrix. */
16286 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16287 start = start_row->minpos;
16288 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16289
16290 /* Clear the desired matrix for the display below. */
16291 clear_glyph_matrix (w->desired_matrix);
16292
16293 if (CHARPOS (new_start) <= CHARPOS (start))
16294 {
16295 /* Don't use this method if the display starts with an ellipsis
16296 displayed for invisible text. It's not easy to handle that case
16297 below, and it's certainly not worth the effort since this is
16298 not a frequent case. */
16299 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16300 return 0;
16301
16302 IF_DEBUG (debug_method_add (w, "twu1"));
16303
16304 /* Display up to a row that can be reused. The variable
16305 last_text_row is set to the last row displayed that displays
16306 text. Note that it.vpos == 0 if or if not there is a
16307 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16308 start_display (&it, w, new_start);
16309 w->cursor.vpos = -1;
16310 last_text_row = last_reused_text_row = NULL;
16311
16312 while (it.current_y < it.last_visible_y
16313 && !fonts_changed_p)
16314 {
16315 /* If we have reached into the characters in the START row,
16316 that means the line boundaries have changed. So we
16317 can't start copying with the row START. Maybe it will
16318 work to start copying with the following row. */
16319 while (IT_CHARPOS (it) > CHARPOS (start))
16320 {
16321 /* Advance to the next row as the "start". */
16322 start_row++;
16323 start = start_row->minpos;
16324 /* If there are no more rows to try, or just one, give up. */
16325 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16326 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16327 || CHARPOS (start) == ZV)
16328 {
16329 clear_glyph_matrix (w->desired_matrix);
16330 return 0;
16331 }
16332
16333 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16334 }
16335 /* If we have reached alignment, we can copy the rest of the
16336 rows. */
16337 if (IT_CHARPOS (it) == CHARPOS (start)
16338 /* Don't accept "alignment" inside a display vector,
16339 since start_row could have started in the middle of
16340 that same display vector (thus their character
16341 positions match), and we have no way of telling if
16342 that is the case. */
16343 && it.current.dpvec_index < 0)
16344 break;
16345
16346 if (display_line (&it))
16347 last_text_row = it.glyph_row - 1;
16348
16349 }
16350
16351 /* A value of current_y < last_visible_y means that we stopped
16352 at the previous window start, which in turn means that we
16353 have at least one reusable row. */
16354 if (it.current_y < it.last_visible_y)
16355 {
16356 struct glyph_row *row;
16357
16358 /* IT.vpos always starts from 0; it counts text lines. */
16359 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16360
16361 /* Find PT if not already found in the lines displayed. */
16362 if (w->cursor.vpos < 0)
16363 {
16364 int dy = it.current_y - start_row->y;
16365
16366 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16367 row = row_containing_pos (w, PT, row, NULL, dy);
16368 if (row)
16369 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16370 dy, nrows_scrolled);
16371 else
16372 {
16373 clear_glyph_matrix (w->desired_matrix);
16374 return 0;
16375 }
16376 }
16377
16378 /* Scroll the display. Do it before the current matrix is
16379 changed. The problem here is that update has not yet
16380 run, i.e. part of the current matrix is not up to date.
16381 scroll_run_hook will clear the cursor, and use the
16382 current matrix to get the height of the row the cursor is
16383 in. */
16384 run.current_y = start_row->y;
16385 run.desired_y = it.current_y;
16386 run.height = it.last_visible_y - it.current_y;
16387
16388 if (run.height > 0 && run.current_y != run.desired_y)
16389 {
16390 update_begin (f);
16391 FRAME_RIF (f)->update_window_begin_hook (w);
16392 FRAME_RIF (f)->clear_window_mouse_face (w);
16393 FRAME_RIF (f)->scroll_run_hook (w, &run);
16394 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16395 update_end (f);
16396 }
16397
16398 /* Shift current matrix down by nrows_scrolled lines. */
16399 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16400 rotate_matrix (w->current_matrix,
16401 start_vpos,
16402 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16403 nrows_scrolled);
16404
16405 /* Disable lines that must be updated. */
16406 for (i = 0; i < nrows_scrolled; ++i)
16407 (start_row + i)->enabled_p = 0;
16408
16409 /* Re-compute Y positions. */
16410 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16411 max_y = it.last_visible_y;
16412 for (row = start_row + nrows_scrolled;
16413 row < bottom_row;
16414 ++row)
16415 {
16416 row->y = it.current_y;
16417 row->visible_height = row->height;
16418
16419 if (row->y < min_y)
16420 row->visible_height -= min_y - row->y;
16421 if (row->y + row->height > max_y)
16422 row->visible_height -= row->y + row->height - max_y;
16423 if (row->fringe_bitmap_periodic_p)
16424 row->redraw_fringe_bitmaps_p = 1;
16425
16426 it.current_y += row->height;
16427
16428 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16429 last_reused_text_row = row;
16430 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16431 break;
16432 }
16433
16434 /* Disable lines in the current matrix which are now
16435 below the window. */
16436 for (++row; row < bottom_row; ++row)
16437 row->enabled_p = row->mode_line_p = 0;
16438 }
16439
16440 /* Update window_end_pos etc.; last_reused_text_row is the last
16441 reused row from the current matrix containing text, if any.
16442 The value of last_text_row is the last displayed line
16443 containing text. */
16444 if (last_reused_text_row)
16445 {
16446 w->window_end_bytepos
16447 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16448 w->window_end_pos
16449 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16450 w->window_end_vpos
16451 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16452 w->current_matrix));
16453 }
16454 else if (last_text_row)
16455 {
16456 w->window_end_bytepos
16457 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16458 w->window_end_pos
16459 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16460 w->window_end_vpos
16461 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16462 }
16463 else
16464 {
16465 /* This window must be completely empty. */
16466 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16467 w->window_end_pos = make_number (Z - ZV);
16468 w->window_end_vpos = make_number (0);
16469 }
16470 w->window_end_valid = Qnil;
16471
16472 /* Update hint: don't try scrolling again in update_window. */
16473 w->desired_matrix->no_scrolling_p = 1;
16474
16475 #if GLYPH_DEBUG
16476 debug_method_add (w, "try_window_reusing_current_matrix 1");
16477 #endif
16478 return 1;
16479 }
16480 else if (CHARPOS (new_start) > CHARPOS (start))
16481 {
16482 struct glyph_row *pt_row, *row;
16483 struct glyph_row *first_reusable_row;
16484 struct glyph_row *first_row_to_display;
16485 int dy;
16486 int yb = window_text_bottom_y (w);
16487
16488 /* Find the row starting at new_start, if there is one. Don't
16489 reuse a partially visible line at the end. */
16490 first_reusable_row = start_row;
16491 while (first_reusable_row->enabled_p
16492 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16493 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16494 < CHARPOS (new_start)))
16495 ++first_reusable_row;
16496
16497 /* Give up if there is no row to reuse. */
16498 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16499 || !first_reusable_row->enabled_p
16500 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16501 != CHARPOS (new_start)))
16502 return 0;
16503
16504 /* We can reuse fully visible rows beginning with
16505 first_reusable_row to the end of the window. Set
16506 first_row_to_display to the first row that cannot be reused.
16507 Set pt_row to the row containing point, if there is any. */
16508 pt_row = NULL;
16509 for (first_row_to_display = first_reusable_row;
16510 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16511 ++first_row_to_display)
16512 {
16513 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16514 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16515 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16516 && first_row_to_display->ends_at_zv_p
16517 && pt_row == NULL)))
16518 pt_row = first_row_to_display;
16519 }
16520
16521 /* Start displaying at the start of first_row_to_display. */
16522 xassert (first_row_to_display->y < yb);
16523 init_to_row_start (&it, w, first_row_to_display);
16524
16525 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16526 - start_vpos);
16527 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16528 - nrows_scrolled);
16529 it.current_y = (first_row_to_display->y - first_reusable_row->y
16530 + WINDOW_HEADER_LINE_HEIGHT (w));
16531
16532 /* Display lines beginning with first_row_to_display in the
16533 desired matrix. Set last_text_row to the last row displayed
16534 that displays text. */
16535 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16536 if (pt_row == NULL)
16537 w->cursor.vpos = -1;
16538 last_text_row = NULL;
16539 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16540 if (display_line (&it))
16541 last_text_row = it.glyph_row - 1;
16542
16543 /* If point is in a reused row, adjust y and vpos of the cursor
16544 position. */
16545 if (pt_row)
16546 {
16547 w->cursor.vpos -= nrows_scrolled;
16548 w->cursor.y -= first_reusable_row->y - start_row->y;
16549 }
16550
16551 /* Give up if point isn't in a row displayed or reused. (This
16552 also handles the case where w->cursor.vpos < nrows_scrolled
16553 after the calls to display_line, which can happen with scroll
16554 margins. See bug#1295.) */
16555 if (w->cursor.vpos < 0)
16556 {
16557 clear_glyph_matrix (w->desired_matrix);
16558 return 0;
16559 }
16560
16561 /* Scroll the display. */
16562 run.current_y = first_reusable_row->y;
16563 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16564 run.height = it.last_visible_y - run.current_y;
16565 dy = run.current_y - run.desired_y;
16566
16567 if (run.height)
16568 {
16569 update_begin (f);
16570 FRAME_RIF (f)->update_window_begin_hook (w);
16571 FRAME_RIF (f)->clear_window_mouse_face (w);
16572 FRAME_RIF (f)->scroll_run_hook (w, &run);
16573 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16574 update_end (f);
16575 }
16576
16577 /* Adjust Y positions of reused rows. */
16578 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16579 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16580 max_y = it.last_visible_y;
16581 for (row = first_reusable_row; row < first_row_to_display; ++row)
16582 {
16583 row->y -= dy;
16584 row->visible_height = row->height;
16585 if (row->y < min_y)
16586 row->visible_height -= min_y - row->y;
16587 if (row->y + row->height > max_y)
16588 row->visible_height -= row->y + row->height - max_y;
16589 if (row->fringe_bitmap_periodic_p)
16590 row->redraw_fringe_bitmaps_p = 1;
16591 }
16592
16593 /* Scroll the current matrix. */
16594 xassert (nrows_scrolled > 0);
16595 rotate_matrix (w->current_matrix,
16596 start_vpos,
16597 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16598 -nrows_scrolled);
16599
16600 /* Disable rows not reused. */
16601 for (row -= nrows_scrolled; row < bottom_row; ++row)
16602 row->enabled_p = 0;
16603
16604 /* Point may have moved to a different line, so we cannot assume that
16605 the previous cursor position is valid; locate the correct row. */
16606 if (pt_row)
16607 {
16608 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16609 row < bottom_row
16610 && PT >= MATRIX_ROW_END_CHARPOS (row)
16611 && !row->ends_at_zv_p;
16612 row++)
16613 {
16614 w->cursor.vpos++;
16615 w->cursor.y = row->y;
16616 }
16617 if (row < bottom_row)
16618 {
16619 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16620 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16621
16622 /* Can't use this optimization with bidi-reordered glyph
16623 rows, unless cursor is already at point. */
16624 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16625 {
16626 if (!(w->cursor.hpos >= 0
16627 && w->cursor.hpos < row->used[TEXT_AREA]
16628 && BUFFERP (glyph->object)
16629 && glyph->charpos == PT))
16630 return 0;
16631 }
16632 else
16633 for (; glyph < end
16634 && (!BUFFERP (glyph->object)
16635 || glyph->charpos < PT);
16636 glyph++)
16637 {
16638 w->cursor.hpos++;
16639 w->cursor.x += glyph->pixel_width;
16640 }
16641 }
16642 }
16643
16644 /* Adjust window end. A null value of last_text_row means that
16645 the window end is in reused rows which in turn means that
16646 only its vpos can have changed. */
16647 if (last_text_row)
16648 {
16649 w->window_end_bytepos
16650 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16651 w->window_end_pos
16652 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16653 w->window_end_vpos
16654 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16655 }
16656 else
16657 {
16658 w->window_end_vpos
16659 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16660 }
16661
16662 w->window_end_valid = Qnil;
16663 w->desired_matrix->no_scrolling_p = 1;
16664
16665 #if GLYPH_DEBUG
16666 debug_method_add (w, "try_window_reusing_current_matrix 2");
16667 #endif
16668 return 1;
16669 }
16670
16671 return 0;
16672 }
16673
16674
16675 \f
16676 /************************************************************************
16677 Window redisplay reusing current matrix when buffer has changed
16678 ************************************************************************/
16679
16680 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16681 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16682 ptrdiff_t *, ptrdiff_t *);
16683 static struct glyph_row *
16684 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16685 struct glyph_row *);
16686
16687
16688 /* Return the last row in MATRIX displaying text. If row START is
16689 non-null, start searching with that row. IT gives the dimensions
16690 of the display. Value is null if matrix is empty; otherwise it is
16691 a pointer to the row found. */
16692
16693 static struct glyph_row *
16694 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16695 struct glyph_row *start)
16696 {
16697 struct glyph_row *row, *row_found;
16698
16699 /* Set row_found to the last row in IT->w's current matrix
16700 displaying text. The loop looks funny but think of partially
16701 visible lines. */
16702 row_found = NULL;
16703 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16704 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16705 {
16706 xassert (row->enabled_p);
16707 row_found = row;
16708 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16709 break;
16710 ++row;
16711 }
16712
16713 return row_found;
16714 }
16715
16716
16717 /* Return the last row in the current matrix of W that is not affected
16718 by changes at the start of current_buffer that occurred since W's
16719 current matrix was built. Value is null if no such row exists.
16720
16721 BEG_UNCHANGED us the number of characters unchanged at the start of
16722 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16723 first changed character in current_buffer. Characters at positions <
16724 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16725 when the current matrix was built. */
16726
16727 static struct glyph_row *
16728 find_last_unchanged_at_beg_row (struct window *w)
16729 {
16730 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16731 struct glyph_row *row;
16732 struct glyph_row *row_found = NULL;
16733 int yb = window_text_bottom_y (w);
16734
16735 /* Find the last row displaying unchanged text. */
16736 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16737 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16738 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16739 ++row)
16740 {
16741 if (/* If row ends before first_changed_pos, it is unchanged,
16742 except in some case. */
16743 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16744 /* When row ends in ZV and we write at ZV it is not
16745 unchanged. */
16746 && !row->ends_at_zv_p
16747 /* When first_changed_pos is the end of a continued line,
16748 row is not unchanged because it may be no longer
16749 continued. */
16750 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16751 && (row->continued_p
16752 || row->exact_window_width_line_p))
16753 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16754 needs to be recomputed, so don't consider this row as
16755 unchanged. This happens when the last line was
16756 bidi-reordered and was killed immediately before this
16757 redisplay cycle. In that case, ROW->end stores the
16758 buffer position of the first visual-order character of
16759 the killed text, which is now beyond ZV. */
16760 && CHARPOS (row->end.pos) <= ZV)
16761 row_found = row;
16762
16763 /* Stop if last visible row. */
16764 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16765 break;
16766 }
16767
16768 return row_found;
16769 }
16770
16771
16772 /* Find the first glyph row in the current matrix of W that is not
16773 affected by changes at the end of current_buffer since the
16774 time W's current matrix was built.
16775
16776 Return in *DELTA the number of chars by which buffer positions in
16777 unchanged text at the end of current_buffer must be adjusted.
16778
16779 Return in *DELTA_BYTES the corresponding number of bytes.
16780
16781 Value is null if no such row exists, i.e. all rows are affected by
16782 changes. */
16783
16784 static struct glyph_row *
16785 find_first_unchanged_at_end_row (struct window *w,
16786 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16787 {
16788 struct glyph_row *row;
16789 struct glyph_row *row_found = NULL;
16790
16791 *delta = *delta_bytes = 0;
16792
16793 /* Display must not have been paused, otherwise the current matrix
16794 is not up to date. */
16795 eassert (!NILP (w->window_end_valid));
16796
16797 /* A value of window_end_pos >= END_UNCHANGED means that the window
16798 end is in the range of changed text. If so, there is no
16799 unchanged row at the end of W's current matrix. */
16800 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16801 return NULL;
16802
16803 /* Set row to the last row in W's current matrix displaying text. */
16804 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16805
16806 /* If matrix is entirely empty, no unchanged row exists. */
16807 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16808 {
16809 /* The value of row is the last glyph row in the matrix having a
16810 meaningful buffer position in it. The end position of row
16811 corresponds to window_end_pos. This allows us to translate
16812 buffer positions in the current matrix to current buffer
16813 positions for characters not in changed text. */
16814 ptrdiff_t Z_old =
16815 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16816 ptrdiff_t Z_BYTE_old =
16817 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16818 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16819 struct glyph_row *first_text_row
16820 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16821
16822 *delta = Z - Z_old;
16823 *delta_bytes = Z_BYTE - Z_BYTE_old;
16824
16825 /* Set last_unchanged_pos to the buffer position of the last
16826 character in the buffer that has not been changed. Z is the
16827 index + 1 of the last character in current_buffer, i.e. by
16828 subtracting END_UNCHANGED we get the index of the last
16829 unchanged character, and we have to add BEG to get its buffer
16830 position. */
16831 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16832 last_unchanged_pos_old = last_unchanged_pos - *delta;
16833
16834 /* Search backward from ROW for a row displaying a line that
16835 starts at a minimum position >= last_unchanged_pos_old. */
16836 for (; row > first_text_row; --row)
16837 {
16838 /* This used to abort, but it can happen.
16839 It is ok to just stop the search instead here. KFS. */
16840 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16841 break;
16842
16843 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16844 row_found = row;
16845 }
16846 }
16847
16848 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16849
16850 return row_found;
16851 }
16852
16853
16854 /* Make sure that glyph rows in the current matrix of window W
16855 reference the same glyph memory as corresponding rows in the
16856 frame's frame matrix. This function is called after scrolling W's
16857 current matrix on a terminal frame in try_window_id and
16858 try_window_reusing_current_matrix. */
16859
16860 static void
16861 sync_frame_with_window_matrix_rows (struct window *w)
16862 {
16863 struct frame *f = XFRAME (w->frame);
16864 struct glyph_row *window_row, *window_row_end, *frame_row;
16865
16866 /* Preconditions: W must be a leaf window and full-width. Its frame
16867 must have a frame matrix. */
16868 xassert (NILP (w->hchild) && NILP (w->vchild));
16869 xassert (WINDOW_FULL_WIDTH_P (w));
16870 xassert (!FRAME_WINDOW_P (f));
16871
16872 /* If W is a full-width window, glyph pointers in W's current matrix
16873 have, by definition, to be the same as glyph pointers in the
16874 corresponding frame matrix. Note that frame matrices have no
16875 marginal areas (see build_frame_matrix). */
16876 window_row = w->current_matrix->rows;
16877 window_row_end = window_row + w->current_matrix->nrows;
16878 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16879 while (window_row < window_row_end)
16880 {
16881 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16882 struct glyph *end = window_row->glyphs[LAST_AREA];
16883
16884 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16885 frame_row->glyphs[TEXT_AREA] = start;
16886 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16887 frame_row->glyphs[LAST_AREA] = end;
16888
16889 /* Disable frame rows whose corresponding window rows have
16890 been disabled in try_window_id. */
16891 if (!window_row->enabled_p)
16892 frame_row->enabled_p = 0;
16893
16894 ++window_row, ++frame_row;
16895 }
16896 }
16897
16898
16899 /* Find the glyph row in window W containing CHARPOS. Consider all
16900 rows between START and END (not inclusive). END null means search
16901 all rows to the end of the display area of W. Value is the row
16902 containing CHARPOS or null. */
16903
16904 struct glyph_row *
16905 row_containing_pos (struct window *w, ptrdiff_t charpos,
16906 struct glyph_row *start, struct glyph_row *end, int dy)
16907 {
16908 struct glyph_row *row = start;
16909 struct glyph_row *best_row = NULL;
16910 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16911 int last_y;
16912
16913 /* If we happen to start on a header-line, skip that. */
16914 if (row->mode_line_p)
16915 ++row;
16916
16917 if ((end && row >= end) || !row->enabled_p)
16918 return NULL;
16919
16920 last_y = window_text_bottom_y (w) - dy;
16921
16922 while (1)
16923 {
16924 /* Give up if we have gone too far. */
16925 if (end && row >= end)
16926 return NULL;
16927 /* This formerly returned if they were equal.
16928 I think that both quantities are of a "last plus one" type;
16929 if so, when they are equal, the row is within the screen. -- rms. */
16930 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16931 return NULL;
16932
16933 /* If it is in this row, return this row. */
16934 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16935 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16936 /* The end position of a row equals the start
16937 position of the next row. If CHARPOS is there, we
16938 would rather display it in the next line, except
16939 when this line ends in ZV. */
16940 && !row->ends_at_zv_p
16941 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16942 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16943 {
16944 struct glyph *g;
16945
16946 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16947 || (!best_row && !row->continued_p))
16948 return row;
16949 /* In bidi-reordered rows, there could be several rows
16950 occluding point, all of them belonging to the same
16951 continued line. We need to find the row which fits
16952 CHARPOS the best. */
16953 for (g = row->glyphs[TEXT_AREA];
16954 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16955 g++)
16956 {
16957 if (!STRINGP (g->object))
16958 {
16959 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16960 {
16961 mindif = eabs (g->charpos - charpos);
16962 best_row = row;
16963 /* Exact match always wins. */
16964 if (mindif == 0)
16965 return best_row;
16966 }
16967 }
16968 }
16969 }
16970 else if (best_row && !row->continued_p)
16971 return best_row;
16972 ++row;
16973 }
16974 }
16975
16976
16977 /* Try to redisplay window W by reusing its existing display. W's
16978 current matrix must be up to date when this function is called,
16979 i.e. window_end_valid must not be nil.
16980
16981 Value is
16982
16983 1 if display has been updated
16984 0 if otherwise unsuccessful
16985 -1 if redisplay with same window start is known not to succeed
16986
16987 The following steps are performed:
16988
16989 1. Find the last row in the current matrix of W that is not
16990 affected by changes at the start of current_buffer. If no such row
16991 is found, give up.
16992
16993 2. Find the first row in W's current matrix that is not affected by
16994 changes at the end of current_buffer. Maybe there is no such row.
16995
16996 3. Display lines beginning with the row + 1 found in step 1 to the
16997 row found in step 2 or, if step 2 didn't find a row, to the end of
16998 the window.
16999
17000 4. If cursor is not known to appear on the window, give up.
17001
17002 5. If display stopped at the row found in step 2, scroll the
17003 display and current matrix as needed.
17004
17005 6. Maybe display some lines at the end of W, if we must. This can
17006 happen under various circumstances, like a partially visible line
17007 becoming fully visible, or because newly displayed lines are displayed
17008 in smaller font sizes.
17009
17010 7. Update W's window end information. */
17011
17012 static int
17013 try_window_id (struct window *w)
17014 {
17015 struct frame *f = XFRAME (w->frame);
17016 struct glyph_matrix *current_matrix = w->current_matrix;
17017 struct glyph_matrix *desired_matrix = w->desired_matrix;
17018 struct glyph_row *last_unchanged_at_beg_row;
17019 struct glyph_row *first_unchanged_at_end_row;
17020 struct glyph_row *row;
17021 struct glyph_row *bottom_row;
17022 int bottom_vpos;
17023 struct it it;
17024 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17025 int dvpos, dy;
17026 struct text_pos start_pos;
17027 struct run run;
17028 int first_unchanged_at_end_vpos = 0;
17029 struct glyph_row *last_text_row, *last_text_row_at_end;
17030 struct text_pos start;
17031 ptrdiff_t first_changed_charpos, last_changed_charpos;
17032
17033 #if GLYPH_DEBUG
17034 if (inhibit_try_window_id)
17035 return 0;
17036 #endif
17037
17038 /* This is handy for debugging. */
17039 #if 0
17040 #define GIVE_UP(X) \
17041 do { \
17042 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17043 return 0; \
17044 } while (0)
17045 #else
17046 #define GIVE_UP(X) return 0
17047 #endif
17048
17049 SET_TEXT_POS_FROM_MARKER (start, w->start);
17050
17051 /* Don't use this for mini-windows because these can show
17052 messages and mini-buffers, and we don't handle that here. */
17053 if (MINI_WINDOW_P (w))
17054 GIVE_UP (1);
17055
17056 /* This flag is used to prevent redisplay optimizations. */
17057 if (windows_or_buffers_changed || cursor_type_changed)
17058 GIVE_UP (2);
17059
17060 /* Verify that narrowing has not changed.
17061 Also verify that we were not told to prevent redisplay optimizations.
17062 It would be nice to further
17063 reduce the number of cases where this prevents try_window_id. */
17064 if (current_buffer->clip_changed
17065 || current_buffer->prevent_redisplay_optimizations_p)
17066 GIVE_UP (3);
17067
17068 /* Window must either use window-based redisplay or be full width. */
17069 if (!FRAME_WINDOW_P (f)
17070 && (!FRAME_LINE_INS_DEL_OK (f)
17071 || !WINDOW_FULL_WIDTH_P (w)))
17072 GIVE_UP (4);
17073
17074 /* Give up if point is known NOT to appear in W. */
17075 if (PT < CHARPOS (start))
17076 GIVE_UP (5);
17077
17078 /* Another way to prevent redisplay optimizations. */
17079 if (XFASTINT (w->last_modified) == 0)
17080 GIVE_UP (6);
17081
17082 /* Verify that window is not hscrolled. */
17083 if (XFASTINT (w->hscroll) != 0)
17084 GIVE_UP (7);
17085
17086 /* Verify that display wasn't paused. */
17087 if (NILP (w->window_end_valid))
17088 GIVE_UP (8);
17089
17090 /* Can't use this if highlighting a region because a cursor movement
17091 will do more than just set the cursor. */
17092 if (!NILP (Vtransient_mark_mode)
17093 && !NILP (BVAR (current_buffer, mark_active)))
17094 GIVE_UP (9);
17095
17096 /* Likewise if highlighting trailing whitespace. */
17097 if (!NILP (Vshow_trailing_whitespace))
17098 GIVE_UP (11);
17099
17100 /* Likewise if showing a region. */
17101 if (!NILP (w->region_showing))
17102 GIVE_UP (10);
17103
17104 /* Can't use this if overlay arrow position and/or string have
17105 changed. */
17106 if (overlay_arrows_changed_p ())
17107 GIVE_UP (12);
17108
17109 /* When word-wrap is on, adding a space to the first word of a
17110 wrapped line can change the wrap position, altering the line
17111 above it. It might be worthwhile to handle this more
17112 intelligently, but for now just redisplay from scratch. */
17113 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17114 GIVE_UP (21);
17115
17116 /* Under bidi reordering, adding or deleting a character in the
17117 beginning of a paragraph, before the first strong directional
17118 character, can change the base direction of the paragraph (unless
17119 the buffer specifies a fixed paragraph direction), which will
17120 require to redisplay the whole paragraph. It might be worthwhile
17121 to find the paragraph limits and widen the range of redisplayed
17122 lines to that, but for now just give up this optimization and
17123 redisplay from scratch. */
17124 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17125 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17126 GIVE_UP (22);
17127
17128 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17129 only if buffer has really changed. The reason is that the gap is
17130 initially at Z for freshly visited files. The code below would
17131 set end_unchanged to 0 in that case. */
17132 if (MODIFF > SAVE_MODIFF
17133 /* This seems to happen sometimes after saving a buffer. */
17134 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17135 {
17136 if (GPT - BEG < BEG_UNCHANGED)
17137 BEG_UNCHANGED = GPT - BEG;
17138 if (Z - GPT < END_UNCHANGED)
17139 END_UNCHANGED = Z - GPT;
17140 }
17141
17142 /* The position of the first and last character that has been changed. */
17143 first_changed_charpos = BEG + BEG_UNCHANGED;
17144 last_changed_charpos = Z - END_UNCHANGED;
17145
17146 /* If window starts after a line end, and the last change is in
17147 front of that newline, then changes don't affect the display.
17148 This case happens with stealth-fontification. Note that although
17149 the display is unchanged, glyph positions in the matrix have to
17150 be adjusted, of course. */
17151 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17152 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17153 && ((last_changed_charpos < CHARPOS (start)
17154 && CHARPOS (start) == BEGV)
17155 || (last_changed_charpos < CHARPOS (start) - 1
17156 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17157 {
17158 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17159 struct glyph_row *r0;
17160
17161 /* Compute how many chars/bytes have been added to or removed
17162 from the buffer. */
17163 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17164 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17165 Z_delta = Z - Z_old;
17166 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17167
17168 /* Give up if PT is not in the window. Note that it already has
17169 been checked at the start of try_window_id that PT is not in
17170 front of the window start. */
17171 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17172 GIVE_UP (13);
17173
17174 /* If window start is unchanged, we can reuse the whole matrix
17175 as is, after adjusting glyph positions. No need to compute
17176 the window end again, since its offset from Z hasn't changed. */
17177 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17178 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17179 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17180 /* PT must not be in a partially visible line. */
17181 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17182 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17183 {
17184 /* Adjust positions in the glyph matrix. */
17185 if (Z_delta || Z_delta_bytes)
17186 {
17187 struct glyph_row *r1
17188 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17189 increment_matrix_positions (w->current_matrix,
17190 MATRIX_ROW_VPOS (r0, current_matrix),
17191 MATRIX_ROW_VPOS (r1, current_matrix),
17192 Z_delta, Z_delta_bytes);
17193 }
17194
17195 /* Set the cursor. */
17196 row = row_containing_pos (w, PT, r0, NULL, 0);
17197 if (row)
17198 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17199 else
17200 abort ();
17201 return 1;
17202 }
17203 }
17204
17205 /* Handle the case that changes are all below what is displayed in
17206 the window, and that PT is in the window. This shortcut cannot
17207 be taken if ZV is visible in the window, and text has been added
17208 there that is visible in the window. */
17209 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17210 /* ZV is not visible in the window, or there are no
17211 changes at ZV, actually. */
17212 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17213 || first_changed_charpos == last_changed_charpos))
17214 {
17215 struct glyph_row *r0;
17216
17217 /* Give up if PT is not in the window. Note that it already has
17218 been checked at the start of try_window_id that PT is not in
17219 front of the window start. */
17220 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17221 GIVE_UP (14);
17222
17223 /* If window start is unchanged, we can reuse the whole matrix
17224 as is, without changing glyph positions since no text has
17225 been added/removed in front of the window end. */
17226 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17227 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17228 /* PT must not be in a partially visible line. */
17229 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17230 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17231 {
17232 /* We have to compute the window end anew since text
17233 could have been added/removed after it. */
17234 w->window_end_pos
17235 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17236 w->window_end_bytepos
17237 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17238
17239 /* Set the cursor. */
17240 row = row_containing_pos (w, PT, r0, NULL, 0);
17241 if (row)
17242 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17243 else
17244 abort ();
17245 return 2;
17246 }
17247 }
17248
17249 /* Give up if window start is in the changed area.
17250
17251 The condition used to read
17252
17253 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17254
17255 but why that was tested escapes me at the moment. */
17256 if (CHARPOS (start) >= first_changed_charpos
17257 && CHARPOS (start) <= last_changed_charpos)
17258 GIVE_UP (15);
17259
17260 /* Check that window start agrees with the start of the first glyph
17261 row in its current matrix. Check this after we know the window
17262 start is not in changed text, otherwise positions would not be
17263 comparable. */
17264 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17265 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17266 GIVE_UP (16);
17267
17268 /* Give up if the window ends in strings. Overlay strings
17269 at the end are difficult to handle, so don't try. */
17270 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17271 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17272 GIVE_UP (20);
17273
17274 /* Compute the position at which we have to start displaying new
17275 lines. Some of the lines at the top of the window might be
17276 reusable because they are not displaying changed text. Find the
17277 last row in W's current matrix not affected by changes at the
17278 start of current_buffer. Value is null if changes start in the
17279 first line of window. */
17280 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17281 if (last_unchanged_at_beg_row)
17282 {
17283 /* Avoid starting to display in the middle of a character, a TAB
17284 for instance. This is easier than to set up the iterator
17285 exactly, and it's not a frequent case, so the additional
17286 effort wouldn't really pay off. */
17287 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17288 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17289 && last_unchanged_at_beg_row > w->current_matrix->rows)
17290 --last_unchanged_at_beg_row;
17291
17292 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17293 GIVE_UP (17);
17294
17295 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17296 GIVE_UP (18);
17297 start_pos = it.current.pos;
17298
17299 /* Start displaying new lines in the desired matrix at the same
17300 vpos we would use in the current matrix, i.e. below
17301 last_unchanged_at_beg_row. */
17302 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17303 current_matrix);
17304 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17305 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17306
17307 xassert (it.hpos == 0 && it.current_x == 0);
17308 }
17309 else
17310 {
17311 /* There are no reusable lines at the start of the window.
17312 Start displaying in the first text line. */
17313 start_display (&it, w, start);
17314 it.vpos = it.first_vpos;
17315 start_pos = it.current.pos;
17316 }
17317
17318 /* Find the first row that is not affected by changes at the end of
17319 the buffer. Value will be null if there is no unchanged row, in
17320 which case we must redisplay to the end of the window. delta
17321 will be set to the value by which buffer positions beginning with
17322 first_unchanged_at_end_row have to be adjusted due to text
17323 changes. */
17324 first_unchanged_at_end_row
17325 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17326 IF_DEBUG (debug_delta = delta);
17327 IF_DEBUG (debug_delta_bytes = delta_bytes);
17328
17329 /* Set stop_pos to the buffer position up to which we will have to
17330 display new lines. If first_unchanged_at_end_row != NULL, this
17331 is the buffer position of the start of the line displayed in that
17332 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17333 that we don't stop at a buffer position. */
17334 stop_pos = 0;
17335 if (first_unchanged_at_end_row)
17336 {
17337 xassert (last_unchanged_at_beg_row == NULL
17338 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17339
17340 /* If this is a continuation line, move forward to the next one
17341 that isn't. Changes in lines above affect this line.
17342 Caution: this may move first_unchanged_at_end_row to a row
17343 not displaying text. */
17344 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17345 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17346 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17347 < it.last_visible_y))
17348 ++first_unchanged_at_end_row;
17349
17350 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17351 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17352 >= it.last_visible_y))
17353 first_unchanged_at_end_row = NULL;
17354 else
17355 {
17356 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17357 + delta);
17358 first_unchanged_at_end_vpos
17359 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17360 xassert (stop_pos >= Z - END_UNCHANGED);
17361 }
17362 }
17363 else if (last_unchanged_at_beg_row == NULL)
17364 GIVE_UP (19);
17365
17366
17367 #if GLYPH_DEBUG
17368
17369 /* Either there is no unchanged row at the end, or the one we have
17370 now displays text. This is a necessary condition for the window
17371 end pos calculation at the end of this function. */
17372 xassert (first_unchanged_at_end_row == NULL
17373 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17374
17375 debug_last_unchanged_at_beg_vpos
17376 = (last_unchanged_at_beg_row
17377 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17378 : -1);
17379 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17380
17381 #endif /* GLYPH_DEBUG != 0 */
17382
17383
17384 /* Display new lines. Set last_text_row to the last new line
17385 displayed which has text on it, i.e. might end up as being the
17386 line where the window_end_vpos is. */
17387 w->cursor.vpos = -1;
17388 last_text_row = NULL;
17389 overlay_arrow_seen = 0;
17390 while (it.current_y < it.last_visible_y
17391 && !fonts_changed_p
17392 && (first_unchanged_at_end_row == NULL
17393 || IT_CHARPOS (it) < stop_pos))
17394 {
17395 if (display_line (&it))
17396 last_text_row = it.glyph_row - 1;
17397 }
17398
17399 if (fonts_changed_p)
17400 return -1;
17401
17402
17403 /* Compute differences in buffer positions, y-positions etc. for
17404 lines reused at the bottom of the window. Compute what we can
17405 scroll. */
17406 if (first_unchanged_at_end_row
17407 /* No lines reused because we displayed everything up to the
17408 bottom of the window. */
17409 && it.current_y < it.last_visible_y)
17410 {
17411 dvpos = (it.vpos
17412 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17413 current_matrix));
17414 dy = it.current_y - first_unchanged_at_end_row->y;
17415 run.current_y = first_unchanged_at_end_row->y;
17416 run.desired_y = run.current_y + dy;
17417 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17418 }
17419 else
17420 {
17421 delta = delta_bytes = dvpos = dy
17422 = run.current_y = run.desired_y = run.height = 0;
17423 first_unchanged_at_end_row = NULL;
17424 }
17425 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17426
17427
17428 /* Find the cursor if not already found. We have to decide whether
17429 PT will appear on this window (it sometimes doesn't, but this is
17430 not a very frequent case.) This decision has to be made before
17431 the current matrix is altered. A value of cursor.vpos < 0 means
17432 that PT is either in one of the lines beginning at
17433 first_unchanged_at_end_row or below the window. Don't care for
17434 lines that might be displayed later at the window end; as
17435 mentioned, this is not a frequent case. */
17436 if (w->cursor.vpos < 0)
17437 {
17438 /* Cursor in unchanged rows at the top? */
17439 if (PT < CHARPOS (start_pos)
17440 && last_unchanged_at_beg_row)
17441 {
17442 row = row_containing_pos (w, PT,
17443 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17444 last_unchanged_at_beg_row + 1, 0);
17445 if (row)
17446 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17447 }
17448
17449 /* Start from first_unchanged_at_end_row looking for PT. */
17450 else if (first_unchanged_at_end_row)
17451 {
17452 row = row_containing_pos (w, PT - delta,
17453 first_unchanged_at_end_row, NULL, 0);
17454 if (row)
17455 set_cursor_from_row (w, row, w->current_matrix, delta,
17456 delta_bytes, dy, dvpos);
17457 }
17458
17459 /* Give up if cursor was not found. */
17460 if (w->cursor.vpos < 0)
17461 {
17462 clear_glyph_matrix (w->desired_matrix);
17463 return -1;
17464 }
17465 }
17466
17467 /* Don't let the cursor end in the scroll margins. */
17468 {
17469 int this_scroll_margin, cursor_height;
17470
17471 this_scroll_margin =
17472 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17473 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17474 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17475
17476 if ((w->cursor.y < this_scroll_margin
17477 && CHARPOS (start) > BEGV)
17478 /* Old redisplay didn't take scroll margin into account at the bottom,
17479 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17480 || (w->cursor.y + (make_cursor_line_fully_visible_p
17481 ? cursor_height + this_scroll_margin
17482 : 1)) > it.last_visible_y)
17483 {
17484 w->cursor.vpos = -1;
17485 clear_glyph_matrix (w->desired_matrix);
17486 return -1;
17487 }
17488 }
17489
17490 /* Scroll the display. Do it before changing the current matrix so
17491 that xterm.c doesn't get confused about where the cursor glyph is
17492 found. */
17493 if (dy && run.height)
17494 {
17495 update_begin (f);
17496
17497 if (FRAME_WINDOW_P (f))
17498 {
17499 FRAME_RIF (f)->update_window_begin_hook (w);
17500 FRAME_RIF (f)->clear_window_mouse_face (w);
17501 FRAME_RIF (f)->scroll_run_hook (w, &run);
17502 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17503 }
17504 else
17505 {
17506 /* Terminal frame. In this case, dvpos gives the number of
17507 lines to scroll by; dvpos < 0 means scroll up. */
17508 int from_vpos
17509 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17510 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17511 int end = (WINDOW_TOP_EDGE_LINE (w)
17512 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17513 + window_internal_height (w));
17514
17515 #if defined (HAVE_GPM) || defined (MSDOS)
17516 x_clear_window_mouse_face (w);
17517 #endif
17518 /* Perform the operation on the screen. */
17519 if (dvpos > 0)
17520 {
17521 /* Scroll last_unchanged_at_beg_row to the end of the
17522 window down dvpos lines. */
17523 set_terminal_window (f, end);
17524
17525 /* On dumb terminals delete dvpos lines at the end
17526 before inserting dvpos empty lines. */
17527 if (!FRAME_SCROLL_REGION_OK (f))
17528 ins_del_lines (f, end - dvpos, -dvpos);
17529
17530 /* Insert dvpos empty lines in front of
17531 last_unchanged_at_beg_row. */
17532 ins_del_lines (f, from, dvpos);
17533 }
17534 else if (dvpos < 0)
17535 {
17536 /* Scroll up last_unchanged_at_beg_vpos to the end of
17537 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17538 set_terminal_window (f, end);
17539
17540 /* Delete dvpos lines in front of
17541 last_unchanged_at_beg_vpos. ins_del_lines will set
17542 the cursor to the given vpos and emit |dvpos| delete
17543 line sequences. */
17544 ins_del_lines (f, from + dvpos, dvpos);
17545
17546 /* On a dumb terminal insert dvpos empty lines at the
17547 end. */
17548 if (!FRAME_SCROLL_REGION_OK (f))
17549 ins_del_lines (f, end + dvpos, -dvpos);
17550 }
17551
17552 set_terminal_window (f, 0);
17553 }
17554
17555 update_end (f);
17556 }
17557
17558 /* Shift reused rows of the current matrix to the right position.
17559 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17560 text. */
17561 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17562 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17563 if (dvpos < 0)
17564 {
17565 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17566 bottom_vpos, dvpos);
17567 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17568 bottom_vpos, 0);
17569 }
17570 else if (dvpos > 0)
17571 {
17572 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17573 bottom_vpos, dvpos);
17574 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17575 first_unchanged_at_end_vpos + dvpos, 0);
17576 }
17577
17578 /* For frame-based redisplay, make sure that current frame and window
17579 matrix are in sync with respect to glyph memory. */
17580 if (!FRAME_WINDOW_P (f))
17581 sync_frame_with_window_matrix_rows (w);
17582
17583 /* Adjust buffer positions in reused rows. */
17584 if (delta || delta_bytes)
17585 increment_matrix_positions (current_matrix,
17586 first_unchanged_at_end_vpos + dvpos,
17587 bottom_vpos, delta, delta_bytes);
17588
17589 /* Adjust Y positions. */
17590 if (dy)
17591 shift_glyph_matrix (w, current_matrix,
17592 first_unchanged_at_end_vpos + dvpos,
17593 bottom_vpos, dy);
17594
17595 if (first_unchanged_at_end_row)
17596 {
17597 first_unchanged_at_end_row += dvpos;
17598 if (first_unchanged_at_end_row->y >= it.last_visible_y
17599 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17600 first_unchanged_at_end_row = NULL;
17601 }
17602
17603 /* If scrolling up, there may be some lines to display at the end of
17604 the window. */
17605 last_text_row_at_end = NULL;
17606 if (dy < 0)
17607 {
17608 /* Scrolling up can leave for example a partially visible line
17609 at the end of the window to be redisplayed. */
17610 /* Set last_row to the glyph row in the current matrix where the
17611 window end line is found. It has been moved up or down in
17612 the matrix by dvpos. */
17613 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17614 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17615
17616 /* If last_row is the window end line, it should display text. */
17617 xassert (last_row->displays_text_p);
17618
17619 /* If window end line was partially visible before, begin
17620 displaying at that line. Otherwise begin displaying with the
17621 line following it. */
17622 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17623 {
17624 init_to_row_start (&it, w, last_row);
17625 it.vpos = last_vpos;
17626 it.current_y = last_row->y;
17627 }
17628 else
17629 {
17630 init_to_row_end (&it, w, last_row);
17631 it.vpos = 1 + last_vpos;
17632 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17633 ++last_row;
17634 }
17635
17636 /* We may start in a continuation line. If so, we have to
17637 get the right continuation_lines_width and current_x. */
17638 it.continuation_lines_width = last_row->continuation_lines_width;
17639 it.hpos = it.current_x = 0;
17640
17641 /* Display the rest of the lines at the window end. */
17642 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17643 while (it.current_y < it.last_visible_y
17644 && !fonts_changed_p)
17645 {
17646 /* Is it always sure that the display agrees with lines in
17647 the current matrix? I don't think so, so we mark rows
17648 displayed invalid in the current matrix by setting their
17649 enabled_p flag to zero. */
17650 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17651 if (display_line (&it))
17652 last_text_row_at_end = it.glyph_row - 1;
17653 }
17654 }
17655
17656 /* Update window_end_pos and window_end_vpos. */
17657 if (first_unchanged_at_end_row
17658 && !last_text_row_at_end)
17659 {
17660 /* Window end line if one of the preserved rows from the current
17661 matrix. Set row to the last row displaying text in current
17662 matrix starting at first_unchanged_at_end_row, after
17663 scrolling. */
17664 xassert (first_unchanged_at_end_row->displays_text_p);
17665 row = find_last_row_displaying_text (w->current_matrix, &it,
17666 first_unchanged_at_end_row);
17667 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17668
17669 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17670 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17671 w->window_end_vpos
17672 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17673 xassert (w->window_end_bytepos >= 0);
17674 IF_DEBUG (debug_method_add (w, "A"));
17675 }
17676 else if (last_text_row_at_end)
17677 {
17678 w->window_end_pos
17679 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17680 w->window_end_bytepos
17681 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17682 w->window_end_vpos
17683 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17684 xassert (w->window_end_bytepos >= 0);
17685 IF_DEBUG (debug_method_add (w, "B"));
17686 }
17687 else if (last_text_row)
17688 {
17689 /* We have displayed either to the end of the window or at the
17690 end of the window, i.e. the last row with text is to be found
17691 in the desired matrix. */
17692 w->window_end_pos
17693 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17694 w->window_end_bytepos
17695 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17696 w->window_end_vpos
17697 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17698 xassert (w->window_end_bytepos >= 0);
17699 }
17700 else if (first_unchanged_at_end_row == NULL
17701 && last_text_row == NULL
17702 && last_text_row_at_end == NULL)
17703 {
17704 /* Displayed to end of window, but no line containing text was
17705 displayed. Lines were deleted at the end of the window. */
17706 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17707 int vpos = XFASTINT (w->window_end_vpos);
17708 struct glyph_row *current_row = current_matrix->rows + vpos;
17709 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17710
17711 for (row = NULL;
17712 row == NULL && vpos >= first_vpos;
17713 --vpos, --current_row, --desired_row)
17714 {
17715 if (desired_row->enabled_p)
17716 {
17717 if (desired_row->displays_text_p)
17718 row = desired_row;
17719 }
17720 else if (current_row->displays_text_p)
17721 row = current_row;
17722 }
17723
17724 xassert (row != NULL);
17725 w->window_end_vpos = make_number (vpos + 1);
17726 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17727 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17728 xassert (w->window_end_bytepos >= 0);
17729 IF_DEBUG (debug_method_add (w, "C"));
17730 }
17731 else
17732 abort ();
17733
17734 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17735 debug_end_vpos = XFASTINT (w->window_end_vpos));
17736
17737 /* Record that display has not been completed. */
17738 w->window_end_valid = Qnil;
17739 w->desired_matrix->no_scrolling_p = 1;
17740 return 3;
17741
17742 #undef GIVE_UP
17743 }
17744
17745
17746 \f
17747 /***********************************************************************
17748 More debugging support
17749 ***********************************************************************/
17750
17751 #if GLYPH_DEBUG
17752
17753 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17754 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17755 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17756
17757
17758 /* Dump the contents of glyph matrix MATRIX on stderr.
17759
17760 GLYPHS 0 means don't show glyph contents.
17761 GLYPHS 1 means show glyphs in short form
17762 GLYPHS > 1 means show glyphs in long form. */
17763
17764 void
17765 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17766 {
17767 int i;
17768 for (i = 0; i < matrix->nrows; ++i)
17769 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17770 }
17771
17772
17773 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17774 the glyph row and area where the glyph comes from. */
17775
17776 void
17777 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17778 {
17779 if (glyph->type == CHAR_GLYPH)
17780 {
17781 fprintf (stderr,
17782 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17783 glyph - row->glyphs[TEXT_AREA],
17784 'C',
17785 glyph->charpos,
17786 (BUFFERP (glyph->object)
17787 ? 'B'
17788 : (STRINGP (glyph->object)
17789 ? 'S'
17790 : '-')),
17791 glyph->pixel_width,
17792 glyph->u.ch,
17793 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17794 ? glyph->u.ch
17795 : '.'),
17796 glyph->face_id,
17797 glyph->left_box_line_p,
17798 glyph->right_box_line_p);
17799 }
17800 else if (glyph->type == STRETCH_GLYPH)
17801 {
17802 fprintf (stderr,
17803 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17804 glyph - row->glyphs[TEXT_AREA],
17805 'S',
17806 glyph->charpos,
17807 (BUFFERP (glyph->object)
17808 ? 'B'
17809 : (STRINGP (glyph->object)
17810 ? 'S'
17811 : '-')),
17812 glyph->pixel_width,
17813 0,
17814 '.',
17815 glyph->face_id,
17816 glyph->left_box_line_p,
17817 glyph->right_box_line_p);
17818 }
17819 else if (glyph->type == IMAGE_GLYPH)
17820 {
17821 fprintf (stderr,
17822 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17823 glyph - row->glyphs[TEXT_AREA],
17824 'I',
17825 glyph->charpos,
17826 (BUFFERP (glyph->object)
17827 ? 'B'
17828 : (STRINGP (glyph->object)
17829 ? 'S'
17830 : '-')),
17831 glyph->pixel_width,
17832 glyph->u.img_id,
17833 '.',
17834 glyph->face_id,
17835 glyph->left_box_line_p,
17836 glyph->right_box_line_p);
17837 }
17838 else if (glyph->type == COMPOSITE_GLYPH)
17839 {
17840 fprintf (stderr,
17841 " %5td %4c %6"pI"d %c %3d 0x%05x",
17842 glyph - row->glyphs[TEXT_AREA],
17843 '+',
17844 glyph->charpos,
17845 (BUFFERP (glyph->object)
17846 ? 'B'
17847 : (STRINGP (glyph->object)
17848 ? 'S'
17849 : '-')),
17850 glyph->pixel_width,
17851 glyph->u.cmp.id);
17852 if (glyph->u.cmp.automatic)
17853 fprintf (stderr,
17854 "[%d-%d]",
17855 glyph->slice.cmp.from, glyph->slice.cmp.to);
17856 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17857 glyph->face_id,
17858 glyph->left_box_line_p,
17859 glyph->right_box_line_p);
17860 }
17861 }
17862
17863
17864 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17865 GLYPHS 0 means don't show glyph contents.
17866 GLYPHS 1 means show glyphs in short form
17867 GLYPHS > 1 means show glyphs in long form. */
17868
17869 void
17870 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17871 {
17872 if (glyphs != 1)
17873 {
17874 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17875 fprintf (stderr, "======================================================================\n");
17876
17877 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17878 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17879 vpos,
17880 MATRIX_ROW_START_CHARPOS (row),
17881 MATRIX_ROW_END_CHARPOS (row),
17882 row->used[TEXT_AREA],
17883 row->contains_overlapping_glyphs_p,
17884 row->enabled_p,
17885 row->truncated_on_left_p,
17886 row->truncated_on_right_p,
17887 row->continued_p,
17888 MATRIX_ROW_CONTINUATION_LINE_P (row),
17889 row->displays_text_p,
17890 row->ends_at_zv_p,
17891 row->fill_line_p,
17892 row->ends_in_middle_of_char_p,
17893 row->starts_in_middle_of_char_p,
17894 row->mouse_face_p,
17895 row->x,
17896 row->y,
17897 row->pixel_width,
17898 row->height,
17899 row->visible_height,
17900 row->ascent,
17901 row->phys_ascent);
17902 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17903 row->end.overlay_string_index,
17904 row->continuation_lines_width);
17905 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17906 CHARPOS (row->start.string_pos),
17907 CHARPOS (row->end.string_pos));
17908 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17909 row->end.dpvec_index);
17910 }
17911
17912 if (glyphs > 1)
17913 {
17914 int area;
17915
17916 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17917 {
17918 struct glyph *glyph = row->glyphs[area];
17919 struct glyph *glyph_end = glyph + row->used[area];
17920
17921 /* Glyph for a line end in text. */
17922 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17923 ++glyph_end;
17924
17925 if (glyph < glyph_end)
17926 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17927
17928 for (; glyph < glyph_end; ++glyph)
17929 dump_glyph (row, glyph, area);
17930 }
17931 }
17932 else if (glyphs == 1)
17933 {
17934 int area;
17935
17936 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17937 {
17938 char *s = (char *) alloca (row->used[area] + 1);
17939 int i;
17940
17941 for (i = 0; i < row->used[area]; ++i)
17942 {
17943 struct glyph *glyph = row->glyphs[area] + i;
17944 if (glyph->type == CHAR_GLYPH
17945 && glyph->u.ch < 0x80
17946 && glyph->u.ch >= ' ')
17947 s[i] = glyph->u.ch;
17948 else
17949 s[i] = '.';
17950 }
17951
17952 s[i] = '\0';
17953 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17954 }
17955 }
17956 }
17957
17958
17959 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17960 Sdump_glyph_matrix, 0, 1, "p",
17961 doc: /* Dump the current matrix of the selected window to stderr.
17962 Shows contents of glyph row structures. With non-nil
17963 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17964 glyphs in short form, otherwise show glyphs in long form. */)
17965 (Lisp_Object glyphs)
17966 {
17967 struct window *w = XWINDOW (selected_window);
17968 struct buffer *buffer = XBUFFER (w->buffer);
17969
17970 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17971 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17972 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17973 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17974 fprintf (stderr, "=============================================\n");
17975 dump_glyph_matrix (w->current_matrix,
17976 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
17977 return Qnil;
17978 }
17979
17980
17981 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17982 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17983 (void)
17984 {
17985 struct frame *f = XFRAME (selected_frame);
17986 dump_glyph_matrix (f->current_matrix, 1);
17987 return Qnil;
17988 }
17989
17990
17991 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17992 doc: /* Dump glyph row ROW to stderr.
17993 GLYPH 0 means don't dump glyphs.
17994 GLYPH 1 means dump glyphs in short form.
17995 GLYPH > 1 or omitted means dump glyphs in long form. */)
17996 (Lisp_Object row, Lisp_Object glyphs)
17997 {
17998 struct glyph_matrix *matrix;
17999 EMACS_INT vpos;
18000
18001 CHECK_NUMBER (row);
18002 matrix = XWINDOW (selected_window)->current_matrix;
18003 vpos = XINT (row);
18004 if (vpos >= 0 && vpos < matrix->nrows)
18005 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18006 vpos,
18007 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18008 return Qnil;
18009 }
18010
18011
18012 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18013 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18014 GLYPH 0 means don't dump glyphs.
18015 GLYPH 1 means dump glyphs in short form.
18016 GLYPH > 1 or omitted means dump glyphs in long form. */)
18017 (Lisp_Object row, Lisp_Object glyphs)
18018 {
18019 struct frame *sf = SELECTED_FRAME ();
18020 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18021 EMACS_INT vpos;
18022
18023 CHECK_NUMBER (row);
18024 vpos = XINT (row);
18025 if (vpos >= 0 && vpos < m->nrows)
18026 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18027 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18028 return Qnil;
18029 }
18030
18031
18032 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18033 doc: /* Toggle tracing of redisplay.
18034 With ARG, turn tracing on if and only if ARG is positive. */)
18035 (Lisp_Object arg)
18036 {
18037 if (NILP (arg))
18038 trace_redisplay_p = !trace_redisplay_p;
18039 else
18040 {
18041 arg = Fprefix_numeric_value (arg);
18042 trace_redisplay_p = XINT (arg) > 0;
18043 }
18044
18045 return Qnil;
18046 }
18047
18048
18049 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18050 doc: /* Like `format', but print result to stderr.
18051 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18052 (ptrdiff_t nargs, Lisp_Object *args)
18053 {
18054 Lisp_Object s = Fformat (nargs, args);
18055 fprintf (stderr, "%s", SDATA (s));
18056 return Qnil;
18057 }
18058
18059 #endif /* GLYPH_DEBUG */
18060
18061
18062 \f
18063 /***********************************************************************
18064 Building Desired Matrix Rows
18065 ***********************************************************************/
18066
18067 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18068 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18069
18070 static struct glyph_row *
18071 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18072 {
18073 struct frame *f = XFRAME (WINDOW_FRAME (w));
18074 struct buffer *buffer = XBUFFER (w->buffer);
18075 struct buffer *old = current_buffer;
18076 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18077 int arrow_len = SCHARS (overlay_arrow_string);
18078 const unsigned char *arrow_end = arrow_string + arrow_len;
18079 const unsigned char *p;
18080 struct it it;
18081 int multibyte_p;
18082 int n_glyphs_before;
18083
18084 set_buffer_temp (buffer);
18085 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18086 it.glyph_row->used[TEXT_AREA] = 0;
18087 SET_TEXT_POS (it.position, 0, 0);
18088
18089 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18090 p = arrow_string;
18091 while (p < arrow_end)
18092 {
18093 Lisp_Object face, ilisp;
18094
18095 /* Get the next character. */
18096 if (multibyte_p)
18097 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18098 else
18099 {
18100 it.c = it.char_to_display = *p, it.len = 1;
18101 if (! ASCII_CHAR_P (it.c))
18102 it.char_to_display = BYTE8_TO_CHAR (it.c);
18103 }
18104 p += it.len;
18105
18106 /* Get its face. */
18107 ilisp = make_number (p - arrow_string);
18108 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18109 it.face_id = compute_char_face (f, it.char_to_display, face);
18110
18111 /* Compute its width, get its glyphs. */
18112 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18113 SET_TEXT_POS (it.position, -1, -1);
18114 PRODUCE_GLYPHS (&it);
18115
18116 /* If this character doesn't fit any more in the line, we have
18117 to remove some glyphs. */
18118 if (it.current_x > it.last_visible_x)
18119 {
18120 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18121 break;
18122 }
18123 }
18124
18125 set_buffer_temp (old);
18126 return it.glyph_row;
18127 }
18128
18129
18130 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18131 glyphs are only inserted for terminal frames since we can't really
18132 win with truncation glyphs when partially visible glyphs are
18133 involved. Which glyphs to insert is determined by
18134 produce_special_glyphs. */
18135
18136 static void
18137 insert_left_trunc_glyphs (struct it *it)
18138 {
18139 struct it truncate_it;
18140 struct glyph *from, *end, *to, *toend;
18141
18142 xassert (!FRAME_WINDOW_P (it->f));
18143
18144 /* Get the truncation glyphs. */
18145 truncate_it = *it;
18146 truncate_it.current_x = 0;
18147 truncate_it.face_id = DEFAULT_FACE_ID;
18148 truncate_it.glyph_row = &scratch_glyph_row;
18149 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18150 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18151 truncate_it.object = make_number (0);
18152 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18153
18154 /* Overwrite glyphs from IT with truncation glyphs. */
18155 if (!it->glyph_row->reversed_p)
18156 {
18157 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18158 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18159 to = it->glyph_row->glyphs[TEXT_AREA];
18160 toend = to + it->glyph_row->used[TEXT_AREA];
18161
18162 while (from < end)
18163 *to++ = *from++;
18164
18165 /* There may be padding glyphs left over. Overwrite them too. */
18166 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18167 {
18168 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18169 while (from < end)
18170 *to++ = *from++;
18171 }
18172
18173 if (to > toend)
18174 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18175 }
18176 else
18177 {
18178 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18179 that back to front. */
18180 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18181 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18182 toend = it->glyph_row->glyphs[TEXT_AREA];
18183 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18184
18185 while (from >= end && to >= toend)
18186 *to-- = *from--;
18187 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18188 {
18189 from =
18190 truncate_it.glyph_row->glyphs[TEXT_AREA]
18191 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18192 while (from >= end && to >= toend)
18193 *to-- = *from--;
18194 }
18195 if (from >= end)
18196 {
18197 /* Need to free some room before prepending additional
18198 glyphs. */
18199 int move_by = from - end + 1;
18200 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18201 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18202
18203 for ( ; g >= g0; g--)
18204 g[move_by] = *g;
18205 while (from >= end)
18206 *to-- = *from--;
18207 it->glyph_row->used[TEXT_AREA] += move_by;
18208 }
18209 }
18210 }
18211
18212 /* Compute the hash code for ROW. */
18213 unsigned
18214 row_hash (struct glyph_row *row)
18215 {
18216 int area, k;
18217 unsigned hashval = 0;
18218
18219 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18220 for (k = 0; k < row->used[area]; ++k)
18221 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18222 + row->glyphs[area][k].u.val
18223 + row->glyphs[area][k].face_id
18224 + row->glyphs[area][k].padding_p
18225 + (row->glyphs[area][k].type << 2));
18226
18227 return hashval;
18228 }
18229
18230 /* Compute the pixel height and width of IT->glyph_row.
18231
18232 Most of the time, ascent and height of a display line will be equal
18233 to the max_ascent and max_height values of the display iterator
18234 structure. This is not the case if
18235
18236 1. We hit ZV without displaying anything. In this case, max_ascent
18237 and max_height will be zero.
18238
18239 2. We have some glyphs that don't contribute to the line height.
18240 (The glyph row flag contributes_to_line_height_p is for future
18241 pixmap extensions).
18242
18243 The first case is easily covered by using default values because in
18244 these cases, the line height does not really matter, except that it
18245 must not be zero. */
18246
18247 static void
18248 compute_line_metrics (struct it *it)
18249 {
18250 struct glyph_row *row = it->glyph_row;
18251
18252 if (FRAME_WINDOW_P (it->f))
18253 {
18254 int i, min_y, max_y;
18255
18256 /* The line may consist of one space only, that was added to
18257 place the cursor on it. If so, the row's height hasn't been
18258 computed yet. */
18259 if (row->height == 0)
18260 {
18261 if (it->max_ascent + it->max_descent == 0)
18262 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18263 row->ascent = it->max_ascent;
18264 row->height = it->max_ascent + it->max_descent;
18265 row->phys_ascent = it->max_phys_ascent;
18266 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18267 row->extra_line_spacing = it->max_extra_line_spacing;
18268 }
18269
18270 /* Compute the width of this line. */
18271 row->pixel_width = row->x;
18272 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18273 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18274
18275 xassert (row->pixel_width >= 0);
18276 xassert (row->ascent >= 0 && row->height > 0);
18277
18278 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18279 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18280
18281 /* If first line's physical ascent is larger than its logical
18282 ascent, use the physical ascent, and make the row taller.
18283 This makes accented characters fully visible. */
18284 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18285 && row->phys_ascent > row->ascent)
18286 {
18287 row->height += row->phys_ascent - row->ascent;
18288 row->ascent = row->phys_ascent;
18289 }
18290
18291 /* Compute how much of the line is visible. */
18292 row->visible_height = row->height;
18293
18294 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18295 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18296
18297 if (row->y < min_y)
18298 row->visible_height -= min_y - row->y;
18299 if (row->y + row->height > max_y)
18300 row->visible_height -= row->y + row->height - max_y;
18301 }
18302 else
18303 {
18304 row->pixel_width = row->used[TEXT_AREA];
18305 if (row->continued_p)
18306 row->pixel_width -= it->continuation_pixel_width;
18307 else if (row->truncated_on_right_p)
18308 row->pixel_width -= it->truncation_pixel_width;
18309 row->ascent = row->phys_ascent = 0;
18310 row->height = row->phys_height = row->visible_height = 1;
18311 row->extra_line_spacing = 0;
18312 }
18313
18314 /* Compute a hash code for this row. */
18315 row->hash = row_hash (row);
18316
18317 it->max_ascent = it->max_descent = 0;
18318 it->max_phys_ascent = it->max_phys_descent = 0;
18319 }
18320
18321
18322 /* Append one space to the glyph row of iterator IT if doing a
18323 window-based redisplay. The space has the same face as
18324 IT->face_id. Value is non-zero if a space was added.
18325
18326 This function is called to make sure that there is always one glyph
18327 at the end of a glyph row that the cursor can be set on under
18328 window-systems. (If there weren't such a glyph we would not know
18329 how wide and tall a box cursor should be displayed).
18330
18331 At the same time this space let's a nicely handle clearing to the
18332 end of the line if the row ends in italic text. */
18333
18334 static int
18335 append_space_for_newline (struct it *it, int default_face_p)
18336 {
18337 if (FRAME_WINDOW_P (it->f))
18338 {
18339 int n = it->glyph_row->used[TEXT_AREA];
18340
18341 if (it->glyph_row->glyphs[TEXT_AREA] + n
18342 < it->glyph_row->glyphs[1 + TEXT_AREA])
18343 {
18344 /* Save some values that must not be changed.
18345 Must save IT->c and IT->len because otherwise
18346 ITERATOR_AT_END_P wouldn't work anymore after
18347 append_space_for_newline has been called. */
18348 enum display_element_type saved_what = it->what;
18349 int saved_c = it->c, saved_len = it->len;
18350 int saved_char_to_display = it->char_to_display;
18351 int saved_x = it->current_x;
18352 int saved_face_id = it->face_id;
18353 struct text_pos saved_pos;
18354 Lisp_Object saved_object;
18355 struct face *face;
18356
18357 saved_object = it->object;
18358 saved_pos = it->position;
18359
18360 it->what = IT_CHARACTER;
18361 memset (&it->position, 0, sizeof it->position);
18362 it->object = make_number (0);
18363 it->c = it->char_to_display = ' ';
18364 it->len = 1;
18365
18366 /* If the default face was remapped, be sure to use the
18367 remapped face for the appended newline. */
18368 if (default_face_p)
18369 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18370 else if (it->face_before_selective_p)
18371 it->face_id = it->saved_face_id;
18372 face = FACE_FROM_ID (it->f, it->face_id);
18373 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18374
18375 PRODUCE_GLYPHS (it);
18376
18377 it->override_ascent = -1;
18378 it->constrain_row_ascent_descent_p = 0;
18379 it->current_x = saved_x;
18380 it->object = saved_object;
18381 it->position = saved_pos;
18382 it->what = saved_what;
18383 it->face_id = saved_face_id;
18384 it->len = saved_len;
18385 it->c = saved_c;
18386 it->char_to_display = saved_char_to_display;
18387 return 1;
18388 }
18389 }
18390
18391 return 0;
18392 }
18393
18394
18395 /* Extend the face of the last glyph in the text area of IT->glyph_row
18396 to the end of the display line. Called from display_line. If the
18397 glyph row is empty, add a space glyph to it so that we know the
18398 face to draw. Set the glyph row flag fill_line_p. If the glyph
18399 row is R2L, prepend a stretch glyph to cover the empty space to the
18400 left of the leftmost glyph. */
18401
18402 static void
18403 extend_face_to_end_of_line (struct it *it)
18404 {
18405 struct face *face, *default_face;
18406 struct frame *f = it->f;
18407
18408 /* If line is already filled, do nothing. Non window-system frames
18409 get a grace of one more ``pixel'' because their characters are
18410 1-``pixel'' wide, so they hit the equality too early. This grace
18411 is needed only for R2L rows that are not continued, to produce
18412 one extra blank where we could display the cursor. */
18413 if (it->current_x >= it->last_visible_x
18414 + (!FRAME_WINDOW_P (f)
18415 && it->glyph_row->reversed_p
18416 && !it->glyph_row->continued_p))
18417 return;
18418
18419 /* The default face, possibly remapped. */
18420 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18421
18422 /* Face extension extends the background and box of IT->face_id
18423 to the end of the line. If the background equals the background
18424 of the frame, we don't have to do anything. */
18425 if (it->face_before_selective_p)
18426 face = FACE_FROM_ID (f, it->saved_face_id);
18427 else
18428 face = FACE_FROM_ID (f, it->face_id);
18429
18430 if (FRAME_WINDOW_P (f)
18431 && it->glyph_row->displays_text_p
18432 && face->box == FACE_NO_BOX
18433 && face->background == FRAME_BACKGROUND_PIXEL (f)
18434 && !face->stipple
18435 && !it->glyph_row->reversed_p)
18436 return;
18437
18438 /* Set the glyph row flag indicating that the face of the last glyph
18439 in the text area has to be drawn to the end of the text area. */
18440 it->glyph_row->fill_line_p = 1;
18441
18442 /* If current character of IT is not ASCII, make sure we have the
18443 ASCII face. This will be automatically undone the next time
18444 get_next_display_element returns a multibyte character. Note
18445 that the character will always be single byte in unibyte
18446 text. */
18447 if (!ASCII_CHAR_P (it->c))
18448 {
18449 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18450 }
18451
18452 if (FRAME_WINDOW_P (f))
18453 {
18454 /* If the row is empty, add a space with the current face of IT,
18455 so that we know which face to draw. */
18456 if (it->glyph_row->used[TEXT_AREA] == 0)
18457 {
18458 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18459 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18460 it->glyph_row->used[TEXT_AREA] = 1;
18461 }
18462 #ifdef HAVE_WINDOW_SYSTEM
18463 if (it->glyph_row->reversed_p)
18464 {
18465 /* Prepend a stretch glyph to the row, such that the
18466 rightmost glyph will be drawn flushed all the way to the
18467 right margin of the window. The stretch glyph that will
18468 occupy the empty space, if any, to the left of the
18469 glyphs. */
18470 struct font *font = face->font ? face->font : FRAME_FONT (f);
18471 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18472 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18473 struct glyph *g;
18474 int row_width, stretch_ascent, stretch_width;
18475 struct text_pos saved_pos;
18476 int saved_face_id, saved_avoid_cursor;
18477
18478 for (row_width = 0, g = row_start; g < row_end; g++)
18479 row_width += g->pixel_width;
18480 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18481 if (stretch_width > 0)
18482 {
18483 stretch_ascent =
18484 (((it->ascent + it->descent)
18485 * FONT_BASE (font)) / FONT_HEIGHT (font));
18486 saved_pos = it->position;
18487 memset (&it->position, 0, sizeof it->position);
18488 saved_avoid_cursor = it->avoid_cursor_p;
18489 it->avoid_cursor_p = 1;
18490 saved_face_id = it->face_id;
18491 /* The last row's stretch glyph should get the default
18492 face, to avoid painting the rest of the window with
18493 the region face, if the region ends at ZV. */
18494 if (it->glyph_row->ends_at_zv_p)
18495 it->face_id = default_face->id;
18496 else
18497 it->face_id = face->id;
18498 append_stretch_glyph (it, make_number (0), stretch_width,
18499 it->ascent + it->descent, stretch_ascent);
18500 it->position = saved_pos;
18501 it->avoid_cursor_p = saved_avoid_cursor;
18502 it->face_id = saved_face_id;
18503 }
18504 }
18505 #endif /* HAVE_WINDOW_SYSTEM */
18506 }
18507 else
18508 {
18509 /* Save some values that must not be changed. */
18510 int saved_x = it->current_x;
18511 struct text_pos saved_pos;
18512 Lisp_Object saved_object;
18513 enum display_element_type saved_what = it->what;
18514 int saved_face_id = it->face_id;
18515
18516 saved_object = it->object;
18517 saved_pos = it->position;
18518
18519 it->what = IT_CHARACTER;
18520 memset (&it->position, 0, sizeof it->position);
18521 it->object = make_number (0);
18522 it->c = it->char_to_display = ' ';
18523 it->len = 1;
18524 /* The last row's blank glyphs should get the default face, to
18525 avoid painting the rest of the window with the region face,
18526 if the region ends at ZV. */
18527 if (it->glyph_row->ends_at_zv_p)
18528 it->face_id = default_face->id;
18529 else
18530 it->face_id = face->id;
18531
18532 PRODUCE_GLYPHS (it);
18533
18534 while (it->current_x <= it->last_visible_x)
18535 PRODUCE_GLYPHS (it);
18536
18537 /* Don't count these blanks really. It would let us insert a left
18538 truncation glyph below and make us set the cursor on them, maybe. */
18539 it->current_x = saved_x;
18540 it->object = saved_object;
18541 it->position = saved_pos;
18542 it->what = saved_what;
18543 it->face_id = saved_face_id;
18544 }
18545 }
18546
18547
18548 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18549 trailing whitespace. */
18550
18551 static int
18552 trailing_whitespace_p (ptrdiff_t charpos)
18553 {
18554 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18555 int c = 0;
18556
18557 while (bytepos < ZV_BYTE
18558 && (c = FETCH_CHAR (bytepos),
18559 c == ' ' || c == '\t'))
18560 ++bytepos;
18561
18562 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18563 {
18564 if (bytepos != PT_BYTE)
18565 return 1;
18566 }
18567 return 0;
18568 }
18569
18570
18571 /* Highlight trailing whitespace, if any, in ROW. */
18572
18573 static void
18574 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18575 {
18576 int used = row->used[TEXT_AREA];
18577
18578 if (used)
18579 {
18580 struct glyph *start = row->glyphs[TEXT_AREA];
18581 struct glyph *glyph = start + used - 1;
18582
18583 if (row->reversed_p)
18584 {
18585 /* Right-to-left rows need to be processed in the opposite
18586 direction, so swap the edge pointers. */
18587 glyph = start;
18588 start = row->glyphs[TEXT_AREA] + used - 1;
18589 }
18590
18591 /* Skip over glyphs inserted to display the cursor at the
18592 end of a line, for extending the face of the last glyph
18593 to the end of the line on terminals, and for truncation
18594 and continuation glyphs. */
18595 if (!row->reversed_p)
18596 {
18597 while (glyph >= start
18598 && glyph->type == CHAR_GLYPH
18599 && INTEGERP (glyph->object))
18600 --glyph;
18601 }
18602 else
18603 {
18604 while (glyph <= start
18605 && glyph->type == CHAR_GLYPH
18606 && INTEGERP (glyph->object))
18607 ++glyph;
18608 }
18609
18610 /* If last glyph is a space or stretch, and it's trailing
18611 whitespace, set the face of all trailing whitespace glyphs in
18612 IT->glyph_row to `trailing-whitespace'. */
18613 if ((row->reversed_p ? glyph <= start : glyph >= start)
18614 && BUFFERP (glyph->object)
18615 && (glyph->type == STRETCH_GLYPH
18616 || (glyph->type == CHAR_GLYPH
18617 && glyph->u.ch == ' '))
18618 && trailing_whitespace_p (glyph->charpos))
18619 {
18620 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18621 if (face_id < 0)
18622 return;
18623
18624 if (!row->reversed_p)
18625 {
18626 while (glyph >= start
18627 && BUFFERP (glyph->object)
18628 && (glyph->type == STRETCH_GLYPH
18629 || (glyph->type == CHAR_GLYPH
18630 && glyph->u.ch == ' ')))
18631 (glyph--)->face_id = face_id;
18632 }
18633 else
18634 {
18635 while (glyph <= start
18636 && BUFFERP (glyph->object)
18637 && (glyph->type == STRETCH_GLYPH
18638 || (glyph->type == CHAR_GLYPH
18639 && glyph->u.ch == ' ')))
18640 (glyph++)->face_id = face_id;
18641 }
18642 }
18643 }
18644 }
18645
18646
18647 /* Value is non-zero if glyph row ROW should be
18648 used to hold the cursor. */
18649
18650 static int
18651 cursor_row_p (struct glyph_row *row)
18652 {
18653 int result = 1;
18654
18655 if (PT == CHARPOS (row->end.pos)
18656 || PT == MATRIX_ROW_END_CHARPOS (row))
18657 {
18658 /* Suppose the row ends on a string.
18659 Unless the row is continued, that means it ends on a newline
18660 in the string. If it's anything other than a display string
18661 (e.g., a before-string from an overlay), we don't want the
18662 cursor there. (This heuristic seems to give the optimal
18663 behavior for the various types of multi-line strings.)
18664 One exception: if the string has `cursor' property on one of
18665 its characters, we _do_ want the cursor there. */
18666 if (CHARPOS (row->end.string_pos) >= 0)
18667 {
18668 if (row->continued_p)
18669 result = 1;
18670 else
18671 {
18672 /* Check for `display' property. */
18673 struct glyph *beg = row->glyphs[TEXT_AREA];
18674 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18675 struct glyph *glyph;
18676
18677 result = 0;
18678 for (glyph = end; glyph >= beg; --glyph)
18679 if (STRINGP (glyph->object))
18680 {
18681 Lisp_Object prop
18682 = Fget_char_property (make_number (PT),
18683 Qdisplay, Qnil);
18684 result =
18685 (!NILP (prop)
18686 && display_prop_string_p (prop, glyph->object));
18687 /* If there's a `cursor' property on one of the
18688 string's characters, this row is a cursor row,
18689 even though this is not a display string. */
18690 if (!result)
18691 {
18692 Lisp_Object s = glyph->object;
18693
18694 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18695 {
18696 ptrdiff_t gpos = glyph->charpos;
18697
18698 if (!NILP (Fget_char_property (make_number (gpos),
18699 Qcursor, s)))
18700 {
18701 result = 1;
18702 break;
18703 }
18704 }
18705 }
18706 break;
18707 }
18708 }
18709 }
18710 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18711 {
18712 /* If the row ends in middle of a real character,
18713 and the line is continued, we want the cursor here.
18714 That's because CHARPOS (ROW->end.pos) would equal
18715 PT if PT is before the character. */
18716 if (!row->ends_in_ellipsis_p)
18717 result = row->continued_p;
18718 else
18719 /* If the row ends in an ellipsis, then
18720 CHARPOS (ROW->end.pos) will equal point after the
18721 invisible text. We want that position to be displayed
18722 after the ellipsis. */
18723 result = 0;
18724 }
18725 /* If the row ends at ZV, display the cursor at the end of that
18726 row instead of at the start of the row below. */
18727 else if (row->ends_at_zv_p)
18728 result = 1;
18729 else
18730 result = 0;
18731 }
18732
18733 return result;
18734 }
18735
18736 \f
18737
18738 /* Push the property PROP so that it will be rendered at the current
18739 position in IT. Return 1 if PROP was successfully pushed, 0
18740 otherwise. Called from handle_line_prefix to handle the
18741 `line-prefix' and `wrap-prefix' properties. */
18742
18743 static int
18744 push_prefix_prop (struct it *it, Lisp_Object prop)
18745 {
18746 struct text_pos pos =
18747 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18748
18749 xassert (it->method == GET_FROM_BUFFER
18750 || it->method == GET_FROM_DISPLAY_VECTOR
18751 || it->method == GET_FROM_STRING);
18752
18753 /* We need to save the current buffer/string position, so it will be
18754 restored by pop_it, because iterate_out_of_display_property
18755 depends on that being set correctly, but some situations leave
18756 it->position not yet set when this function is called. */
18757 push_it (it, &pos);
18758
18759 if (STRINGP (prop))
18760 {
18761 if (SCHARS (prop) == 0)
18762 {
18763 pop_it (it);
18764 return 0;
18765 }
18766
18767 it->string = prop;
18768 it->string_from_prefix_prop_p = 1;
18769 it->multibyte_p = STRING_MULTIBYTE (it->string);
18770 it->current.overlay_string_index = -1;
18771 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18772 it->end_charpos = it->string_nchars = SCHARS (it->string);
18773 it->method = GET_FROM_STRING;
18774 it->stop_charpos = 0;
18775 it->prev_stop = 0;
18776 it->base_level_stop = 0;
18777
18778 /* Force paragraph direction to be that of the parent
18779 buffer/string. */
18780 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18781 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18782 else
18783 it->paragraph_embedding = L2R;
18784
18785 /* Set up the bidi iterator for this display string. */
18786 if (it->bidi_p)
18787 {
18788 it->bidi_it.string.lstring = it->string;
18789 it->bidi_it.string.s = NULL;
18790 it->bidi_it.string.schars = it->end_charpos;
18791 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18792 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18793 it->bidi_it.string.unibyte = !it->multibyte_p;
18794 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18795 }
18796 }
18797 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18798 {
18799 it->method = GET_FROM_STRETCH;
18800 it->object = prop;
18801 }
18802 #ifdef HAVE_WINDOW_SYSTEM
18803 else if (IMAGEP (prop))
18804 {
18805 it->what = IT_IMAGE;
18806 it->image_id = lookup_image (it->f, prop);
18807 it->method = GET_FROM_IMAGE;
18808 }
18809 #endif /* HAVE_WINDOW_SYSTEM */
18810 else
18811 {
18812 pop_it (it); /* bogus display property, give up */
18813 return 0;
18814 }
18815
18816 return 1;
18817 }
18818
18819 /* Return the character-property PROP at the current position in IT. */
18820
18821 static Lisp_Object
18822 get_it_property (struct it *it, Lisp_Object prop)
18823 {
18824 Lisp_Object position;
18825
18826 if (STRINGP (it->object))
18827 position = make_number (IT_STRING_CHARPOS (*it));
18828 else if (BUFFERP (it->object))
18829 position = make_number (IT_CHARPOS (*it));
18830 else
18831 return Qnil;
18832
18833 return Fget_char_property (position, prop, it->object);
18834 }
18835
18836 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18837
18838 static void
18839 handle_line_prefix (struct it *it)
18840 {
18841 Lisp_Object prefix;
18842
18843 if (it->continuation_lines_width > 0)
18844 {
18845 prefix = get_it_property (it, Qwrap_prefix);
18846 if (NILP (prefix))
18847 prefix = Vwrap_prefix;
18848 }
18849 else
18850 {
18851 prefix = get_it_property (it, Qline_prefix);
18852 if (NILP (prefix))
18853 prefix = Vline_prefix;
18854 }
18855 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18856 {
18857 /* If the prefix is wider than the window, and we try to wrap
18858 it, it would acquire its own wrap prefix, and so on till the
18859 iterator stack overflows. So, don't wrap the prefix. */
18860 it->line_wrap = TRUNCATE;
18861 it->avoid_cursor_p = 1;
18862 }
18863 }
18864
18865 \f
18866
18867 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18868 only for R2L lines from display_line and display_string, when they
18869 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18870 the line/string needs to be continued on the next glyph row. */
18871 static void
18872 unproduce_glyphs (struct it *it, int n)
18873 {
18874 struct glyph *glyph, *end;
18875
18876 xassert (it->glyph_row);
18877 xassert (it->glyph_row->reversed_p);
18878 xassert (it->area == TEXT_AREA);
18879 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18880
18881 if (n > it->glyph_row->used[TEXT_AREA])
18882 n = it->glyph_row->used[TEXT_AREA];
18883 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18884 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18885 for ( ; glyph < end; glyph++)
18886 glyph[-n] = *glyph;
18887 }
18888
18889 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18890 and ROW->maxpos. */
18891 static void
18892 find_row_edges (struct it *it, struct glyph_row *row,
18893 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18894 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18895 {
18896 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18897 lines' rows is implemented for bidi-reordered rows. */
18898
18899 /* ROW->minpos is the value of min_pos, the minimal buffer position
18900 we have in ROW, or ROW->start.pos if that is smaller. */
18901 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18902 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18903 else
18904 /* We didn't find buffer positions smaller than ROW->start, or
18905 didn't find _any_ valid buffer positions in any of the glyphs,
18906 so we must trust the iterator's computed positions. */
18907 row->minpos = row->start.pos;
18908 if (max_pos <= 0)
18909 {
18910 max_pos = CHARPOS (it->current.pos);
18911 max_bpos = BYTEPOS (it->current.pos);
18912 }
18913
18914 /* Here are the various use-cases for ending the row, and the
18915 corresponding values for ROW->maxpos:
18916
18917 Line ends in a newline from buffer eol_pos + 1
18918 Line is continued from buffer max_pos + 1
18919 Line is truncated on right it->current.pos
18920 Line ends in a newline from string max_pos + 1(*)
18921 (*) + 1 only when line ends in a forward scan
18922 Line is continued from string max_pos
18923 Line is continued from display vector max_pos
18924 Line is entirely from a string min_pos == max_pos
18925 Line is entirely from a display vector min_pos == max_pos
18926 Line that ends at ZV ZV
18927
18928 If you discover other use-cases, please add them here as
18929 appropriate. */
18930 if (row->ends_at_zv_p)
18931 row->maxpos = it->current.pos;
18932 else if (row->used[TEXT_AREA])
18933 {
18934 int seen_this_string = 0;
18935 struct glyph_row *r1 = row - 1;
18936
18937 /* Did we see the same display string on the previous row? */
18938 if (STRINGP (it->object)
18939 /* this is not the first row */
18940 && row > it->w->desired_matrix->rows
18941 /* previous row is not the header line */
18942 && !r1->mode_line_p
18943 /* previous row also ends in a newline from a string */
18944 && r1->ends_in_newline_from_string_p)
18945 {
18946 struct glyph *start, *end;
18947
18948 /* Search for the last glyph of the previous row that came
18949 from buffer or string. Depending on whether the row is
18950 L2R or R2L, we need to process it front to back or the
18951 other way round. */
18952 if (!r1->reversed_p)
18953 {
18954 start = r1->glyphs[TEXT_AREA];
18955 end = start + r1->used[TEXT_AREA];
18956 /* Glyphs inserted by redisplay have an integer (zero)
18957 as their object. */
18958 while (end > start
18959 && INTEGERP ((end - 1)->object)
18960 && (end - 1)->charpos <= 0)
18961 --end;
18962 if (end > start)
18963 {
18964 if (EQ ((end - 1)->object, it->object))
18965 seen_this_string = 1;
18966 }
18967 else
18968 /* If all the glyphs of the previous row were inserted
18969 by redisplay, it means the previous row was
18970 produced from a single newline, which is only
18971 possible if that newline came from the same string
18972 as the one which produced this ROW. */
18973 seen_this_string = 1;
18974 }
18975 else
18976 {
18977 end = r1->glyphs[TEXT_AREA] - 1;
18978 start = end + r1->used[TEXT_AREA];
18979 while (end < start
18980 && INTEGERP ((end + 1)->object)
18981 && (end + 1)->charpos <= 0)
18982 ++end;
18983 if (end < start)
18984 {
18985 if (EQ ((end + 1)->object, it->object))
18986 seen_this_string = 1;
18987 }
18988 else
18989 seen_this_string = 1;
18990 }
18991 }
18992 /* Take note of each display string that covers a newline only
18993 once, the first time we see it. This is for when a display
18994 string includes more than one newline in it. */
18995 if (row->ends_in_newline_from_string_p && !seen_this_string)
18996 {
18997 /* If we were scanning the buffer forward when we displayed
18998 the string, we want to account for at least one buffer
18999 position that belongs to this row (position covered by
19000 the display string), so that cursor positioning will
19001 consider this row as a candidate when point is at the end
19002 of the visual line represented by this row. This is not
19003 required when scanning back, because max_pos will already
19004 have a much larger value. */
19005 if (CHARPOS (row->end.pos) > max_pos)
19006 INC_BOTH (max_pos, max_bpos);
19007 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19008 }
19009 else if (CHARPOS (it->eol_pos) > 0)
19010 SET_TEXT_POS (row->maxpos,
19011 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19012 else if (row->continued_p)
19013 {
19014 /* If max_pos is different from IT's current position, it
19015 means IT->method does not belong to the display element
19016 at max_pos. However, it also means that the display
19017 element at max_pos was displayed in its entirety on this
19018 line, which is equivalent to saying that the next line
19019 starts at the next buffer position. */
19020 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19021 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19022 else
19023 {
19024 INC_BOTH (max_pos, max_bpos);
19025 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19026 }
19027 }
19028 else if (row->truncated_on_right_p)
19029 /* display_line already called reseat_at_next_visible_line_start,
19030 which puts the iterator at the beginning of the next line, in
19031 the logical order. */
19032 row->maxpos = it->current.pos;
19033 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19034 /* A line that is entirely from a string/image/stretch... */
19035 row->maxpos = row->minpos;
19036 else
19037 abort ();
19038 }
19039 else
19040 row->maxpos = it->current.pos;
19041 }
19042
19043 /* Construct the glyph row IT->glyph_row in the desired matrix of
19044 IT->w from text at the current position of IT. See dispextern.h
19045 for an overview of struct it. Value is non-zero if
19046 IT->glyph_row displays text, as opposed to a line displaying ZV
19047 only. */
19048
19049 static int
19050 display_line (struct it *it)
19051 {
19052 struct glyph_row *row = it->glyph_row;
19053 Lisp_Object overlay_arrow_string;
19054 struct it wrap_it;
19055 void *wrap_data = NULL;
19056 int may_wrap = 0, wrap_x IF_LINT (= 0);
19057 int wrap_row_used = -1;
19058 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19059 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19060 int wrap_row_extra_line_spacing IF_LINT (= 0);
19061 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19062 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19063 int cvpos;
19064 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19065 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19066
19067 /* We always start displaying at hpos zero even if hscrolled. */
19068 xassert (it->hpos == 0 && it->current_x == 0);
19069
19070 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19071 >= it->w->desired_matrix->nrows)
19072 {
19073 it->w->nrows_scale_factor++;
19074 fonts_changed_p = 1;
19075 return 0;
19076 }
19077
19078 /* Is IT->w showing the region? */
19079 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19080
19081 /* Clear the result glyph row and enable it. */
19082 prepare_desired_row (row);
19083
19084 row->y = it->current_y;
19085 row->start = it->start;
19086 row->continuation_lines_width = it->continuation_lines_width;
19087 row->displays_text_p = 1;
19088 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19089 it->starts_in_middle_of_char_p = 0;
19090
19091 /* Arrange the overlays nicely for our purposes. Usually, we call
19092 display_line on only one line at a time, in which case this
19093 can't really hurt too much, or we call it on lines which appear
19094 one after another in the buffer, in which case all calls to
19095 recenter_overlay_lists but the first will be pretty cheap. */
19096 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19097
19098 /* Move over display elements that are not visible because we are
19099 hscrolled. This may stop at an x-position < IT->first_visible_x
19100 if the first glyph is partially visible or if we hit a line end. */
19101 if (it->current_x < it->first_visible_x)
19102 {
19103 this_line_min_pos = row->start.pos;
19104 move_it_in_display_line_to (it, ZV, it->first_visible_x,
19105 MOVE_TO_POS | MOVE_TO_X);
19106 /* Record the smallest positions seen while we moved over
19107 display elements that are not visible. This is needed by
19108 redisplay_internal for optimizing the case where the cursor
19109 stays inside the same line. The rest of this function only
19110 considers positions that are actually displayed, so
19111 RECORD_MAX_MIN_POS will not otherwise record positions that
19112 are hscrolled to the left of the left edge of the window. */
19113 min_pos = CHARPOS (this_line_min_pos);
19114 min_bpos = BYTEPOS (this_line_min_pos);
19115 }
19116 else
19117 {
19118 /* We only do this when not calling `move_it_in_display_line_to'
19119 above, because move_it_in_display_line_to calls
19120 handle_line_prefix itself. */
19121 handle_line_prefix (it);
19122 }
19123
19124 /* Get the initial row height. This is either the height of the
19125 text hscrolled, if there is any, or zero. */
19126 row->ascent = it->max_ascent;
19127 row->height = it->max_ascent + it->max_descent;
19128 row->phys_ascent = it->max_phys_ascent;
19129 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19130 row->extra_line_spacing = it->max_extra_line_spacing;
19131
19132 /* Utility macro to record max and min buffer positions seen until now. */
19133 #define RECORD_MAX_MIN_POS(IT) \
19134 do \
19135 { \
19136 int composition_p = !STRINGP ((IT)->string) \
19137 && ((IT)->what == IT_COMPOSITION); \
19138 ptrdiff_t current_pos = \
19139 composition_p ? (IT)->cmp_it.charpos \
19140 : IT_CHARPOS (*(IT)); \
19141 ptrdiff_t current_bpos = \
19142 composition_p ? CHAR_TO_BYTE (current_pos) \
19143 : IT_BYTEPOS (*(IT)); \
19144 if (current_pos < min_pos) \
19145 { \
19146 min_pos = current_pos; \
19147 min_bpos = current_bpos; \
19148 } \
19149 if (IT_CHARPOS (*it) > max_pos) \
19150 { \
19151 max_pos = IT_CHARPOS (*it); \
19152 max_bpos = IT_BYTEPOS (*it); \
19153 } \
19154 } \
19155 while (0)
19156
19157 /* Loop generating characters. The loop is left with IT on the next
19158 character to display. */
19159 while (1)
19160 {
19161 int n_glyphs_before, hpos_before, x_before;
19162 int x, nglyphs;
19163 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19164
19165 /* Retrieve the next thing to display. Value is zero if end of
19166 buffer reached. */
19167 if (!get_next_display_element (it))
19168 {
19169 /* Maybe add a space at the end of this line that is used to
19170 display the cursor there under X. Set the charpos of the
19171 first glyph of blank lines not corresponding to any text
19172 to -1. */
19173 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19174 row->exact_window_width_line_p = 1;
19175 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19176 || row->used[TEXT_AREA] == 0)
19177 {
19178 row->glyphs[TEXT_AREA]->charpos = -1;
19179 row->displays_text_p = 0;
19180
19181 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19182 && (!MINI_WINDOW_P (it->w)
19183 || (minibuf_level && EQ (it->window, minibuf_window))))
19184 row->indicate_empty_line_p = 1;
19185 }
19186
19187 it->continuation_lines_width = 0;
19188 row->ends_at_zv_p = 1;
19189 /* A row that displays right-to-left text must always have
19190 its last face extended all the way to the end of line,
19191 even if this row ends in ZV, because we still write to
19192 the screen left to right. We also need to extend the
19193 last face if the default face is remapped to some
19194 different face, otherwise the functions that clear
19195 portions of the screen will clear with the default face's
19196 background color. */
19197 if (row->reversed_p
19198 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19199 extend_face_to_end_of_line (it);
19200 break;
19201 }
19202
19203 /* Now, get the metrics of what we want to display. This also
19204 generates glyphs in `row' (which is IT->glyph_row). */
19205 n_glyphs_before = row->used[TEXT_AREA];
19206 x = it->current_x;
19207
19208 /* Remember the line height so far in case the next element doesn't
19209 fit on the line. */
19210 if (it->line_wrap != TRUNCATE)
19211 {
19212 ascent = it->max_ascent;
19213 descent = it->max_descent;
19214 phys_ascent = it->max_phys_ascent;
19215 phys_descent = it->max_phys_descent;
19216
19217 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19218 {
19219 if (IT_DISPLAYING_WHITESPACE (it))
19220 may_wrap = 1;
19221 else if (may_wrap)
19222 {
19223 SAVE_IT (wrap_it, *it, wrap_data);
19224 wrap_x = x;
19225 wrap_row_used = row->used[TEXT_AREA];
19226 wrap_row_ascent = row->ascent;
19227 wrap_row_height = row->height;
19228 wrap_row_phys_ascent = row->phys_ascent;
19229 wrap_row_phys_height = row->phys_height;
19230 wrap_row_extra_line_spacing = row->extra_line_spacing;
19231 wrap_row_min_pos = min_pos;
19232 wrap_row_min_bpos = min_bpos;
19233 wrap_row_max_pos = max_pos;
19234 wrap_row_max_bpos = max_bpos;
19235 may_wrap = 0;
19236 }
19237 }
19238 }
19239
19240 PRODUCE_GLYPHS (it);
19241
19242 /* If this display element was in marginal areas, continue with
19243 the next one. */
19244 if (it->area != TEXT_AREA)
19245 {
19246 row->ascent = max (row->ascent, it->max_ascent);
19247 row->height = max (row->height, it->max_ascent + it->max_descent);
19248 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19249 row->phys_height = max (row->phys_height,
19250 it->max_phys_ascent + it->max_phys_descent);
19251 row->extra_line_spacing = max (row->extra_line_spacing,
19252 it->max_extra_line_spacing);
19253 set_iterator_to_next (it, 1);
19254 continue;
19255 }
19256
19257 /* Does the display element fit on the line? If we truncate
19258 lines, we should draw past the right edge of the window. If
19259 we don't truncate, we want to stop so that we can display the
19260 continuation glyph before the right margin. If lines are
19261 continued, there are two possible strategies for characters
19262 resulting in more than 1 glyph (e.g. tabs): Display as many
19263 glyphs as possible in this line and leave the rest for the
19264 continuation line, or display the whole element in the next
19265 line. Original redisplay did the former, so we do it also. */
19266 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19267 hpos_before = it->hpos;
19268 x_before = x;
19269
19270 if (/* Not a newline. */
19271 nglyphs > 0
19272 /* Glyphs produced fit entirely in the line. */
19273 && it->current_x < it->last_visible_x)
19274 {
19275 it->hpos += nglyphs;
19276 row->ascent = max (row->ascent, it->max_ascent);
19277 row->height = max (row->height, it->max_ascent + it->max_descent);
19278 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19279 row->phys_height = max (row->phys_height,
19280 it->max_phys_ascent + it->max_phys_descent);
19281 row->extra_line_spacing = max (row->extra_line_spacing,
19282 it->max_extra_line_spacing);
19283 if (it->current_x - it->pixel_width < it->first_visible_x)
19284 row->x = x - it->first_visible_x;
19285 /* Record the maximum and minimum buffer positions seen so
19286 far in glyphs that will be displayed by this row. */
19287 if (it->bidi_p)
19288 RECORD_MAX_MIN_POS (it);
19289 }
19290 else
19291 {
19292 int i, new_x;
19293 struct glyph *glyph;
19294
19295 for (i = 0; i < nglyphs; ++i, x = new_x)
19296 {
19297 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19298 new_x = x + glyph->pixel_width;
19299
19300 if (/* Lines are continued. */
19301 it->line_wrap != TRUNCATE
19302 && (/* Glyph doesn't fit on the line. */
19303 new_x > it->last_visible_x
19304 /* Or it fits exactly on a window system frame. */
19305 || (new_x == it->last_visible_x
19306 && FRAME_WINDOW_P (it->f))))
19307 {
19308 /* End of a continued line. */
19309
19310 if (it->hpos == 0
19311 || (new_x == it->last_visible_x
19312 && FRAME_WINDOW_P (it->f)))
19313 {
19314 /* Current glyph is the only one on the line or
19315 fits exactly on the line. We must continue
19316 the line because we can't draw the cursor
19317 after the glyph. */
19318 row->continued_p = 1;
19319 it->current_x = new_x;
19320 it->continuation_lines_width += new_x;
19321 ++it->hpos;
19322 if (i == nglyphs - 1)
19323 {
19324 /* If line-wrap is on, check if a previous
19325 wrap point was found. */
19326 if (wrap_row_used > 0
19327 /* Even if there is a previous wrap
19328 point, continue the line here as
19329 usual, if (i) the previous character
19330 was a space or tab AND (ii) the
19331 current character is not. */
19332 && (!may_wrap
19333 || IT_DISPLAYING_WHITESPACE (it)))
19334 goto back_to_wrap;
19335
19336 /* Record the maximum and minimum buffer
19337 positions seen so far in glyphs that will be
19338 displayed by this row. */
19339 if (it->bidi_p)
19340 RECORD_MAX_MIN_POS (it);
19341 set_iterator_to_next (it, 1);
19342 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19343 {
19344 if (!get_next_display_element (it))
19345 {
19346 row->exact_window_width_line_p = 1;
19347 it->continuation_lines_width = 0;
19348 row->continued_p = 0;
19349 row->ends_at_zv_p = 1;
19350 }
19351 else if (ITERATOR_AT_END_OF_LINE_P (it))
19352 {
19353 row->continued_p = 0;
19354 row->exact_window_width_line_p = 1;
19355 }
19356 }
19357 }
19358 else if (it->bidi_p)
19359 RECORD_MAX_MIN_POS (it);
19360 }
19361 else if (CHAR_GLYPH_PADDING_P (*glyph)
19362 && !FRAME_WINDOW_P (it->f))
19363 {
19364 /* A padding glyph that doesn't fit on this line.
19365 This means the whole character doesn't fit
19366 on the line. */
19367 if (row->reversed_p)
19368 unproduce_glyphs (it, row->used[TEXT_AREA]
19369 - n_glyphs_before);
19370 row->used[TEXT_AREA] = n_glyphs_before;
19371
19372 /* Fill the rest of the row with continuation
19373 glyphs like in 20.x. */
19374 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19375 < row->glyphs[1 + TEXT_AREA])
19376 produce_special_glyphs (it, IT_CONTINUATION);
19377
19378 row->continued_p = 1;
19379 it->current_x = x_before;
19380 it->continuation_lines_width += x_before;
19381
19382 /* Restore the height to what it was before the
19383 element not fitting on the line. */
19384 it->max_ascent = ascent;
19385 it->max_descent = descent;
19386 it->max_phys_ascent = phys_ascent;
19387 it->max_phys_descent = phys_descent;
19388 }
19389 else if (wrap_row_used > 0)
19390 {
19391 back_to_wrap:
19392 if (row->reversed_p)
19393 unproduce_glyphs (it,
19394 row->used[TEXT_AREA] - wrap_row_used);
19395 RESTORE_IT (it, &wrap_it, wrap_data);
19396 it->continuation_lines_width += wrap_x;
19397 row->used[TEXT_AREA] = wrap_row_used;
19398 row->ascent = wrap_row_ascent;
19399 row->height = wrap_row_height;
19400 row->phys_ascent = wrap_row_phys_ascent;
19401 row->phys_height = wrap_row_phys_height;
19402 row->extra_line_spacing = wrap_row_extra_line_spacing;
19403 min_pos = wrap_row_min_pos;
19404 min_bpos = wrap_row_min_bpos;
19405 max_pos = wrap_row_max_pos;
19406 max_bpos = wrap_row_max_bpos;
19407 row->continued_p = 1;
19408 row->ends_at_zv_p = 0;
19409 row->exact_window_width_line_p = 0;
19410 it->continuation_lines_width += x;
19411
19412 /* Make sure that a non-default face is extended
19413 up to the right margin of the window. */
19414 extend_face_to_end_of_line (it);
19415 }
19416 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19417 {
19418 /* A TAB that extends past the right edge of the
19419 window. This produces a single glyph on
19420 window system frames. We leave the glyph in
19421 this row and let it fill the row, but don't
19422 consume the TAB. */
19423 it->continuation_lines_width += it->last_visible_x;
19424 row->ends_in_middle_of_char_p = 1;
19425 row->continued_p = 1;
19426 glyph->pixel_width = it->last_visible_x - x;
19427 it->starts_in_middle_of_char_p = 1;
19428 }
19429 else
19430 {
19431 /* Something other than a TAB that draws past
19432 the right edge of the window. Restore
19433 positions to values before the element. */
19434 if (row->reversed_p)
19435 unproduce_glyphs (it, row->used[TEXT_AREA]
19436 - (n_glyphs_before + i));
19437 row->used[TEXT_AREA] = n_glyphs_before + i;
19438
19439 /* Display continuation glyphs. */
19440 if (!FRAME_WINDOW_P (it->f))
19441 produce_special_glyphs (it, IT_CONTINUATION);
19442 row->continued_p = 1;
19443
19444 it->current_x = x_before;
19445 it->continuation_lines_width += x;
19446 extend_face_to_end_of_line (it);
19447
19448 if (nglyphs > 1 && i > 0)
19449 {
19450 row->ends_in_middle_of_char_p = 1;
19451 it->starts_in_middle_of_char_p = 1;
19452 }
19453
19454 /* Restore the height to what it was before the
19455 element not fitting on the line. */
19456 it->max_ascent = ascent;
19457 it->max_descent = descent;
19458 it->max_phys_ascent = phys_ascent;
19459 it->max_phys_descent = phys_descent;
19460 }
19461
19462 break;
19463 }
19464 else if (new_x > it->first_visible_x)
19465 {
19466 /* Increment number of glyphs actually displayed. */
19467 ++it->hpos;
19468
19469 /* Record the maximum and minimum buffer positions
19470 seen so far in glyphs that will be displayed by
19471 this row. */
19472 if (it->bidi_p)
19473 RECORD_MAX_MIN_POS (it);
19474
19475 if (x < it->first_visible_x)
19476 /* Glyph is partially visible, i.e. row starts at
19477 negative X position. */
19478 row->x = x - it->first_visible_x;
19479 }
19480 else
19481 {
19482 /* Glyph is completely off the left margin of the
19483 window. This should not happen because of the
19484 move_it_in_display_line at the start of this
19485 function, unless the text display area of the
19486 window is empty. */
19487 xassert (it->first_visible_x <= it->last_visible_x);
19488 }
19489 }
19490 /* Even if this display element produced no glyphs at all,
19491 we want to record its position. */
19492 if (it->bidi_p && nglyphs == 0)
19493 RECORD_MAX_MIN_POS (it);
19494
19495 row->ascent = max (row->ascent, it->max_ascent);
19496 row->height = max (row->height, it->max_ascent + it->max_descent);
19497 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19498 row->phys_height = max (row->phys_height,
19499 it->max_phys_ascent + it->max_phys_descent);
19500 row->extra_line_spacing = max (row->extra_line_spacing,
19501 it->max_extra_line_spacing);
19502
19503 /* End of this display line if row is continued. */
19504 if (row->continued_p || row->ends_at_zv_p)
19505 break;
19506 }
19507
19508 at_end_of_line:
19509 /* Is this a line end? If yes, we're also done, after making
19510 sure that a non-default face is extended up to the right
19511 margin of the window. */
19512 if (ITERATOR_AT_END_OF_LINE_P (it))
19513 {
19514 int used_before = row->used[TEXT_AREA];
19515
19516 row->ends_in_newline_from_string_p = STRINGP (it->object);
19517
19518 /* Add a space at the end of the line that is used to
19519 display the cursor there. */
19520 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19521 append_space_for_newline (it, 0);
19522
19523 /* Extend the face to the end of the line. */
19524 extend_face_to_end_of_line (it);
19525
19526 /* Make sure we have the position. */
19527 if (used_before == 0)
19528 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19529
19530 /* Record the position of the newline, for use in
19531 find_row_edges. */
19532 it->eol_pos = it->current.pos;
19533
19534 /* Consume the line end. This skips over invisible lines. */
19535 set_iterator_to_next (it, 1);
19536 it->continuation_lines_width = 0;
19537 break;
19538 }
19539
19540 /* Proceed with next display element. Note that this skips
19541 over lines invisible because of selective display. */
19542 set_iterator_to_next (it, 1);
19543
19544 /* If we truncate lines, we are done when the last displayed
19545 glyphs reach past the right margin of the window. */
19546 if (it->line_wrap == TRUNCATE
19547 && (FRAME_WINDOW_P (it->f)
19548 ? (it->current_x >= it->last_visible_x)
19549 : (it->current_x > it->last_visible_x)))
19550 {
19551 /* Maybe add truncation glyphs. */
19552 if (!FRAME_WINDOW_P (it->f))
19553 {
19554 int i, n;
19555
19556 if (!row->reversed_p)
19557 {
19558 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19559 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19560 break;
19561 }
19562 else
19563 {
19564 for (i = 0; i < row->used[TEXT_AREA]; i++)
19565 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19566 break;
19567 /* Remove any padding glyphs at the front of ROW, to
19568 make room for the truncation glyphs we will be
19569 adding below. The loop below always inserts at
19570 least one truncation glyph, so also remove the
19571 last glyph added to ROW. */
19572 unproduce_glyphs (it, i + 1);
19573 /* Adjust i for the loop below. */
19574 i = row->used[TEXT_AREA] - (i + 1);
19575 }
19576
19577 for (n = row->used[TEXT_AREA]; i < n; ++i)
19578 {
19579 row->used[TEXT_AREA] = i;
19580 produce_special_glyphs (it, IT_TRUNCATION);
19581 }
19582 }
19583 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19584 {
19585 /* Don't truncate if we can overflow newline into fringe. */
19586 if (!get_next_display_element (it))
19587 {
19588 it->continuation_lines_width = 0;
19589 row->ends_at_zv_p = 1;
19590 row->exact_window_width_line_p = 1;
19591 break;
19592 }
19593 if (ITERATOR_AT_END_OF_LINE_P (it))
19594 {
19595 row->exact_window_width_line_p = 1;
19596 goto at_end_of_line;
19597 }
19598 }
19599
19600 row->truncated_on_right_p = 1;
19601 it->continuation_lines_width = 0;
19602 reseat_at_next_visible_line_start (it, 0);
19603 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19604 it->hpos = hpos_before;
19605 it->current_x = x_before;
19606 break;
19607 }
19608 }
19609
19610 if (wrap_data)
19611 bidi_unshelve_cache (wrap_data, 1);
19612
19613 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19614 at the left window margin. */
19615 if (it->first_visible_x
19616 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19617 {
19618 if (!FRAME_WINDOW_P (it->f))
19619 insert_left_trunc_glyphs (it);
19620 row->truncated_on_left_p = 1;
19621 }
19622
19623 /* Remember the position at which this line ends.
19624
19625 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19626 cannot be before the call to find_row_edges below, since that is
19627 where these positions are determined. */
19628 row->end = it->current;
19629 if (!it->bidi_p)
19630 {
19631 row->minpos = row->start.pos;
19632 row->maxpos = row->end.pos;
19633 }
19634 else
19635 {
19636 /* ROW->minpos and ROW->maxpos must be the smallest and
19637 `1 + the largest' buffer positions in ROW. But if ROW was
19638 bidi-reordered, these two positions can be anywhere in the
19639 row, so we must determine them now. */
19640 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19641 }
19642
19643 /* If the start of this line is the overlay arrow-position, then
19644 mark this glyph row as the one containing the overlay arrow.
19645 This is clearly a mess with variable size fonts. It would be
19646 better to let it be displayed like cursors under X. */
19647 if ((row->displays_text_p || !overlay_arrow_seen)
19648 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19649 !NILP (overlay_arrow_string)))
19650 {
19651 /* Overlay arrow in window redisplay is a fringe bitmap. */
19652 if (STRINGP (overlay_arrow_string))
19653 {
19654 struct glyph_row *arrow_row
19655 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19656 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19657 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19658 struct glyph *p = row->glyphs[TEXT_AREA];
19659 struct glyph *p2, *end;
19660
19661 /* Copy the arrow glyphs. */
19662 while (glyph < arrow_end)
19663 *p++ = *glyph++;
19664
19665 /* Throw away padding glyphs. */
19666 p2 = p;
19667 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19668 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19669 ++p2;
19670 if (p2 > p)
19671 {
19672 while (p2 < end)
19673 *p++ = *p2++;
19674 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19675 }
19676 }
19677 else
19678 {
19679 xassert (INTEGERP (overlay_arrow_string));
19680 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19681 }
19682 overlay_arrow_seen = 1;
19683 }
19684
19685 /* Highlight trailing whitespace. */
19686 if (!NILP (Vshow_trailing_whitespace))
19687 highlight_trailing_whitespace (it->f, it->glyph_row);
19688
19689 /* Compute pixel dimensions of this line. */
19690 compute_line_metrics (it);
19691
19692 /* Implementation note: No changes in the glyphs of ROW or in their
19693 faces can be done past this point, because compute_line_metrics
19694 computes ROW's hash value and stores it within the glyph_row
19695 structure. */
19696
19697 /* Record whether this row ends inside an ellipsis. */
19698 row->ends_in_ellipsis_p
19699 = (it->method == GET_FROM_DISPLAY_VECTOR
19700 && it->ellipsis_p);
19701
19702 /* Save fringe bitmaps in this row. */
19703 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19704 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19705 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19706 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19707
19708 it->left_user_fringe_bitmap = 0;
19709 it->left_user_fringe_face_id = 0;
19710 it->right_user_fringe_bitmap = 0;
19711 it->right_user_fringe_face_id = 0;
19712
19713 /* Maybe set the cursor. */
19714 cvpos = it->w->cursor.vpos;
19715 if ((cvpos < 0
19716 /* In bidi-reordered rows, keep checking for proper cursor
19717 position even if one has been found already, because buffer
19718 positions in such rows change non-linearly with ROW->VPOS,
19719 when a line is continued. One exception: when we are at ZV,
19720 display cursor on the first suitable glyph row, since all
19721 the empty rows after that also have their position set to ZV. */
19722 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19723 lines' rows is implemented for bidi-reordered rows. */
19724 || (it->bidi_p
19725 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19726 && PT >= MATRIX_ROW_START_CHARPOS (row)
19727 && PT <= MATRIX_ROW_END_CHARPOS (row)
19728 && cursor_row_p (row))
19729 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19730
19731 /* Prepare for the next line. This line starts horizontally at (X
19732 HPOS) = (0 0). Vertical positions are incremented. As a
19733 convenience for the caller, IT->glyph_row is set to the next
19734 row to be used. */
19735 it->current_x = it->hpos = 0;
19736 it->current_y += row->height;
19737 SET_TEXT_POS (it->eol_pos, 0, 0);
19738 ++it->vpos;
19739 ++it->glyph_row;
19740 /* The next row should by default use the same value of the
19741 reversed_p flag as this one. set_iterator_to_next decides when
19742 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19743 the flag accordingly. */
19744 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19745 it->glyph_row->reversed_p = row->reversed_p;
19746 it->start = row->end;
19747 return row->displays_text_p;
19748
19749 #undef RECORD_MAX_MIN_POS
19750 }
19751
19752 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19753 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19754 doc: /* Return paragraph direction at point in BUFFER.
19755 Value is either `left-to-right' or `right-to-left'.
19756 If BUFFER is omitted or nil, it defaults to the current buffer.
19757
19758 Paragraph direction determines how the text in the paragraph is displayed.
19759 In left-to-right paragraphs, text begins at the left margin of the window
19760 and the reading direction is generally left to right. In right-to-left
19761 paragraphs, text begins at the right margin and is read from right to left.
19762
19763 See also `bidi-paragraph-direction'. */)
19764 (Lisp_Object buffer)
19765 {
19766 struct buffer *buf = current_buffer;
19767 struct buffer *old = buf;
19768
19769 if (! NILP (buffer))
19770 {
19771 CHECK_BUFFER (buffer);
19772 buf = XBUFFER (buffer);
19773 }
19774
19775 if (NILP (BVAR (buf, bidi_display_reordering))
19776 || NILP (BVAR (buf, enable_multibyte_characters))
19777 /* When we are loading loadup.el, the character property tables
19778 needed for bidi iteration are not yet available. */
19779 || !NILP (Vpurify_flag))
19780 return Qleft_to_right;
19781 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19782 return BVAR (buf, bidi_paragraph_direction);
19783 else
19784 {
19785 /* Determine the direction from buffer text. We could try to
19786 use current_matrix if it is up to date, but this seems fast
19787 enough as it is. */
19788 struct bidi_it itb;
19789 ptrdiff_t pos = BUF_PT (buf);
19790 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19791 int c;
19792 void *itb_data = bidi_shelve_cache ();
19793
19794 set_buffer_temp (buf);
19795 /* bidi_paragraph_init finds the base direction of the paragraph
19796 by searching forward from paragraph start. We need the base
19797 direction of the current or _previous_ paragraph, so we need
19798 to make sure we are within that paragraph. To that end, find
19799 the previous non-empty line. */
19800 if (pos >= ZV && pos > BEGV)
19801 {
19802 pos--;
19803 bytepos = CHAR_TO_BYTE (pos);
19804 }
19805 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19806 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19807 {
19808 while ((c = FETCH_BYTE (bytepos)) == '\n'
19809 || c == ' ' || c == '\t' || c == '\f')
19810 {
19811 if (bytepos <= BEGV_BYTE)
19812 break;
19813 bytepos--;
19814 pos--;
19815 }
19816 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19817 bytepos--;
19818 }
19819 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19820 itb.paragraph_dir = NEUTRAL_DIR;
19821 itb.string.s = NULL;
19822 itb.string.lstring = Qnil;
19823 itb.string.bufpos = 0;
19824 itb.string.unibyte = 0;
19825 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19826 bidi_unshelve_cache (itb_data, 0);
19827 set_buffer_temp (old);
19828 switch (itb.paragraph_dir)
19829 {
19830 case L2R:
19831 return Qleft_to_right;
19832 break;
19833 case R2L:
19834 return Qright_to_left;
19835 break;
19836 default:
19837 abort ();
19838 }
19839 }
19840 }
19841
19842
19843 \f
19844 /***********************************************************************
19845 Menu Bar
19846 ***********************************************************************/
19847
19848 /* Redisplay the menu bar in the frame for window W.
19849
19850 The menu bar of X frames that don't have X toolkit support is
19851 displayed in a special window W->frame->menu_bar_window.
19852
19853 The menu bar of terminal frames is treated specially as far as
19854 glyph matrices are concerned. Menu bar lines are not part of
19855 windows, so the update is done directly on the frame matrix rows
19856 for the menu bar. */
19857
19858 static void
19859 display_menu_bar (struct window *w)
19860 {
19861 struct frame *f = XFRAME (WINDOW_FRAME (w));
19862 struct it it;
19863 Lisp_Object items;
19864 int i;
19865
19866 /* Don't do all this for graphical frames. */
19867 #ifdef HAVE_NTGUI
19868 if (FRAME_W32_P (f))
19869 return;
19870 #endif
19871 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19872 if (FRAME_X_P (f))
19873 return;
19874 #endif
19875
19876 #ifdef HAVE_NS
19877 if (FRAME_NS_P (f))
19878 return;
19879 #endif /* HAVE_NS */
19880
19881 #ifdef USE_X_TOOLKIT
19882 xassert (!FRAME_WINDOW_P (f));
19883 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19884 it.first_visible_x = 0;
19885 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19886 #else /* not USE_X_TOOLKIT */
19887 if (FRAME_WINDOW_P (f))
19888 {
19889 /* Menu bar lines are displayed in the desired matrix of the
19890 dummy window menu_bar_window. */
19891 struct window *menu_w;
19892 xassert (WINDOWP (f->menu_bar_window));
19893 menu_w = XWINDOW (f->menu_bar_window);
19894 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19895 MENU_FACE_ID);
19896 it.first_visible_x = 0;
19897 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19898 }
19899 else
19900 {
19901 /* This is a TTY frame, i.e. character hpos/vpos are used as
19902 pixel x/y. */
19903 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19904 MENU_FACE_ID);
19905 it.first_visible_x = 0;
19906 it.last_visible_x = FRAME_COLS (f);
19907 }
19908 #endif /* not USE_X_TOOLKIT */
19909
19910 /* FIXME: This should be controlled by a user option. See the
19911 comments in redisplay_tool_bar and display_mode_line about
19912 this. */
19913 it.paragraph_embedding = L2R;
19914
19915 if (! mode_line_inverse_video)
19916 /* Force the menu-bar to be displayed in the default face. */
19917 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19918
19919 /* Clear all rows of the menu bar. */
19920 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19921 {
19922 struct glyph_row *row = it.glyph_row + i;
19923 clear_glyph_row (row);
19924 row->enabled_p = 1;
19925 row->full_width_p = 1;
19926 }
19927
19928 /* Display all items of the menu bar. */
19929 items = FRAME_MENU_BAR_ITEMS (it.f);
19930 for (i = 0; i < ASIZE (items); i += 4)
19931 {
19932 Lisp_Object string;
19933
19934 /* Stop at nil string. */
19935 string = AREF (items, i + 1);
19936 if (NILP (string))
19937 break;
19938
19939 /* Remember where item was displayed. */
19940 ASET (items, i + 3, make_number (it.hpos));
19941
19942 /* Display the item, pad with one space. */
19943 if (it.current_x < it.last_visible_x)
19944 display_string (NULL, string, Qnil, 0, 0, &it,
19945 SCHARS (string) + 1, 0, 0, -1);
19946 }
19947
19948 /* Fill out the line with spaces. */
19949 if (it.current_x < it.last_visible_x)
19950 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19951
19952 /* Compute the total height of the lines. */
19953 compute_line_metrics (&it);
19954 }
19955
19956
19957 \f
19958 /***********************************************************************
19959 Mode Line
19960 ***********************************************************************/
19961
19962 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19963 FORCE is non-zero, redisplay mode lines unconditionally.
19964 Otherwise, redisplay only mode lines that are garbaged. Value is
19965 the number of windows whose mode lines were redisplayed. */
19966
19967 static int
19968 redisplay_mode_lines (Lisp_Object window, int force)
19969 {
19970 int nwindows = 0;
19971
19972 while (!NILP (window))
19973 {
19974 struct window *w = XWINDOW (window);
19975
19976 if (WINDOWP (w->hchild))
19977 nwindows += redisplay_mode_lines (w->hchild, force);
19978 else if (WINDOWP (w->vchild))
19979 nwindows += redisplay_mode_lines (w->vchild, force);
19980 else if (force
19981 || FRAME_GARBAGED_P (XFRAME (w->frame))
19982 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19983 {
19984 struct text_pos lpoint;
19985 struct buffer *old = current_buffer;
19986
19987 /* Set the window's buffer for the mode line display. */
19988 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19989 set_buffer_internal_1 (XBUFFER (w->buffer));
19990
19991 /* Point refers normally to the selected window. For any
19992 other window, set up appropriate value. */
19993 if (!EQ (window, selected_window))
19994 {
19995 struct text_pos pt;
19996
19997 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19998 if (CHARPOS (pt) < BEGV)
19999 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20000 else if (CHARPOS (pt) > (ZV - 1))
20001 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20002 else
20003 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20004 }
20005
20006 /* Display mode lines. */
20007 clear_glyph_matrix (w->desired_matrix);
20008 if (display_mode_lines (w))
20009 {
20010 ++nwindows;
20011 w->must_be_updated_p = 1;
20012 }
20013
20014 /* Restore old settings. */
20015 set_buffer_internal_1 (old);
20016 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20017 }
20018
20019 window = w->next;
20020 }
20021
20022 return nwindows;
20023 }
20024
20025
20026 /* Display the mode and/or header line of window W. Value is the
20027 sum number of mode lines and header lines displayed. */
20028
20029 static int
20030 display_mode_lines (struct window *w)
20031 {
20032 Lisp_Object old_selected_window, old_selected_frame;
20033 int n = 0;
20034
20035 old_selected_frame = selected_frame;
20036 selected_frame = w->frame;
20037 old_selected_window = selected_window;
20038 XSETWINDOW (selected_window, w);
20039
20040 /* These will be set while the mode line specs are processed. */
20041 line_number_displayed = 0;
20042 w->column_number_displayed = Qnil;
20043
20044 if (WINDOW_WANTS_MODELINE_P (w))
20045 {
20046 struct window *sel_w = XWINDOW (old_selected_window);
20047
20048 /* Select mode line face based on the real selected window. */
20049 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20050 BVAR (current_buffer, mode_line_format));
20051 ++n;
20052 }
20053
20054 if (WINDOW_WANTS_HEADER_LINE_P (w))
20055 {
20056 display_mode_line (w, HEADER_LINE_FACE_ID,
20057 BVAR (current_buffer, header_line_format));
20058 ++n;
20059 }
20060
20061 selected_frame = old_selected_frame;
20062 selected_window = old_selected_window;
20063 return n;
20064 }
20065
20066
20067 /* Display mode or header line of window W. FACE_ID specifies which
20068 line to display; it is either MODE_LINE_FACE_ID or
20069 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20070 display. Value is the pixel height of the mode/header line
20071 displayed. */
20072
20073 static int
20074 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20075 {
20076 struct it it;
20077 struct face *face;
20078 ptrdiff_t count = SPECPDL_INDEX ();
20079
20080 init_iterator (&it, w, -1, -1, NULL, face_id);
20081 /* Don't extend on a previously drawn mode-line.
20082 This may happen if called from pos_visible_p. */
20083 it.glyph_row->enabled_p = 0;
20084 prepare_desired_row (it.glyph_row);
20085
20086 it.glyph_row->mode_line_p = 1;
20087
20088 if (! mode_line_inverse_video)
20089 /* Force the mode-line to be displayed in the default face. */
20090 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20091
20092 /* FIXME: This should be controlled by a user option. But
20093 supporting such an option is not trivial, since the mode line is
20094 made up of many separate strings. */
20095 it.paragraph_embedding = L2R;
20096
20097 record_unwind_protect (unwind_format_mode_line,
20098 format_mode_line_unwind_data (NULL, Qnil, 0));
20099
20100 mode_line_target = MODE_LINE_DISPLAY;
20101
20102 /* Temporarily make frame's keyboard the current kboard so that
20103 kboard-local variables in the mode_line_format will get the right
20104 values. */
20105 push_kboard (FRAME_KBOARD (it.f));
20106 record_unwind_save_match_data ();
20107 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20108 pop_kboard ();
20109
20110 unbind_to (count, Qnil);
20111
20112 /* Fill up with spaces. */
20113 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20114
20115 compute_line_metrics (&it);
20116 it.glyph_row->full_width_p = 1;
20117 it.glyph_row->continued_p = 0;
20118 it.glyph_row->truncated_on_left_p = 0;
20119 it.glyph_row->truncated_on_right_p = 0;
20120
20121 /* Make a 3D mode-line have a shadow at its right end. */
20122 face = FACE_FROM_ID (it.f, face_id);
20123 extend_face_to_end_of_line (&it);
20124 if (face->box != FACE_NO_BOX)
20125 {
20126 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20127 + it.glyph_row->used[TEXT_AREA] - 1);
20128 last->right_box_line_p = 1;
20129 }
20130
20131 return it.glyph_row->height;
20132 }
20133
20134 /* Move element ELT in LIST to the front of LIST.
20135 Return the updated list. */
20136
20137 static Lisp_Object
20138 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20139 {
20140 register Lisp_Object tail, prev;
20141 register Lisp_Object tem;
20142
20143 tail = list;
20144 prev = Qnil;
20145 while (CONSP (tail))
20146 {
20147 tem = XCAR (tail);
20148
20149 if (EQ (elt, tem))
20150 {
20151 /* Splice out the link TAIL. */
20152 if (NILP (prev))
20153 list = XCDR (tail);
20154 else
20155 Fsetcdr (prev, XCDR (tail));
20156
20157 /* Now make it the first. */
20158 Fsetcdr (tail, list);
20159 return tail;
20160 }
20161 else
20162 prev = tail;
20163 tail = XCDR (tail);
20164 QUIT;
20165 }
20166
20167 /* Not found--return unchanged LIST. */
20168 return list;
20169 }
20170
20171 /* Contribute ELT to the mode line for window IT->w. How it
20172 translates into text depends on its data type.
20173
20174 IT describes the display environment in which we display, as usual.
20175
20176 DEPTH is the depth in recursion. It is used to prevent
20177 infinite recursion here.
20178
20179 FIELD_WIDTH is the number of characters the display of ELT should
20180 occupy in the mode line, and PRECISION is the maximum number of
20181 characters to display from ELT's representation. See
20182 display_string for details.
20183
20184 Returns the hpos of the end of the text generated by ELT.
20185
20186 PROPS is a property list to add to any string we encounter.
20187
20188 If RISKY is nonzero, remove (disregard) any properties in any string
20189 we encounter, and ignore :eval and :propertize.
20190
20191 The global variable `mode_line_target' determines whether the
20192 output is passed to `store_mode_line_noprop',
20193 `store_mode_line_string', or `display_string'. */
20194
20195 static int
20196 display_mode_element (struct it *it, int depth, int field_width, int precision,
20197 Lisp_Object elt, Lisp_Object props, int risky)
20198 {
20199 int n = 0, field, prec;
20200 int literal = 0;
20201
20202 tail_recurse:
20203 if (depth > 100)
20204 elt = build_string ("*too-deep*");
20205
20206 depth++;
20207
20208 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20209 {
20210 case Lisp_String:
20211 {
20212 /* A string: output it and check for %-constructs within it. */
20213 unsigned char c;
20214 ptrdiff_t offset = 0;
20215
20216 if (SCHARS (elt) > 0
20217 && (!NILP (props) || risky))
20218 {
20219 Lisp_Object oprops, aelt;
20220 oprops = Ftext_properties_at (make_number (0), elt);
20221
20222 /* If the starting string's properties are not what
20223 we want, translate the string. Also, if the string
20224 is risky, do that anyway. */
20225
20226 if (NILP (Fequal (props, oprops)) || risky)
20227 {
20228 /* If the starting string has properties,
20229 merge the specified ones onto the existing ones. */
20230 if (! NILP (oprops) && !risky)
20231 {
20232 Lisp_Object tem;
20233
20234 oprops = Fcopy_sequence (oprops);
20235 tem = props;
20236 while (CONSP (tem))
20237 {
20238 oprops = Fplist_put (oprops, XCAR (tem),
20239 XCAR (XCDR (tem)));
20240 tem = XCDR (XCDR (tem));
20241 }
20242 props = oprops;
20243 }
20244
20245 aelt = Fassoc (elt, mode_line_proptrans_alist);
20246 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20247 {
20248 /* AELT is what we want. Move it to the front
20249 without consing. */
20250 elt = XCAR (aelt);
20251 mode_line_proptrans_alist
20252 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20253 }
20254 else
20255 {
20256 Lisp_Object tem;
20257
20258 /* If AELT has the wrong props, it is useless.
20259 so get rid of it. */
20260 if (! NILP (aelt))
20261 mode_line_proptrans_alist
20262 = Fdelq (aelt, mode_line_proptrans_alist);
20263
20264 elt = Fcopy_sequence (elt);
20265 Fset_text_properties (make_number (0), Flength (elt),
20266 props, elt);
20267 /* Add this item to mode_line_proptrans_alist. */
20268 mode_line_proptrans_alist
20269 = Fcons (Fcons (elt, props),
20270 mode_line_proptrans_alist);
20271 /* Truncate mode_line_proptrans_alist
20272 to at most 50 elements. */
20273 tem = Fnthcdr (make_number (50),
20274 mode_line_proptrans_alist);
20275 if (! NILP (tem))
20276 XSETCDR (tem, Qnil);
20277 }
20278 }
20279 }
20280
20281 offset = 0;
20282
20283 if (literal)
20284 {
20285 prec = precision - n;
20286 switch (mode_line_target)
20287 {
20288 case MODE_LINE_NOPROP:
20289 case MODE_LINE_TITLE:
20290 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20291 break;
20292 case MODE_LINE_STRING:
20293 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20294 break;
20295 case MODE_LINE_DISPLAY:
20296 n += display_string (NULL, elt, Qnil, 0, 0, it,
20297 0, prec, 0, STRING_MULTIBYTE (elt));
20298 break;
20299 }
20300
20301 break;
20302 }
20303
20304 /* Handle the non-literal case. */
20305
20306 while ((precision <= 0 || n < precision)
20307 && SREF (elt, offset) != 0
20308 && (mode_line_target != MODE_LINE_DISPLAY
20309 || it->current_x < it->last_visible_x))
20310 {
20311 ptrdiff_t last_offset = offset;
20312
20313 /* Advance to end of string or next format specifier. */
20314 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20315 ;
20316
20317 if (offset - 1 != last_offset)
20318 {
20319 ptrdiff_t nchars, nbytes;
20320
20321 /* Output to end of string or up to '%'. Field width
20322 is length of string. Don't output more than
20323 PRECISION allows us. */
20324 offset--;
20325
20326 prec = c_string_width (SDATA (elt) + last_offset,
20327 offset - last_offset, precision - n,
20328 &nchars, &nbytes);
20329
20330 switch (mode_line_target)
20331 {
20332 case MODE_LINE_NOPROP:
20333 case MODE_LINE_TITLE:
20334 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20335 break;
20336 case MODE_LINE_STRING:
20337 {
20338 ptrdiff_t bytepos = last_offset;
20339 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20340 ptrdiff_t endpos = (precision <= 0
20341 ? string_byte_to_char (elt, offset)
20342 : charpos + nchars);
20343
20344 n += store_mode_line_string (NULL,
20345 Fsubstring (elt, make_number (charpos),
20346 make_number (endpos)),
20347 0, 0, 0, Qnil);
20348 }
20349 break;
20350 case MODE_LINE_DISPLAY:
20351 {
20352 ptrdiff_t bytepos = last_offset;
20353 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20354
20355 if (precision <= 0)
20356 nchars = string_byte_to_char (elt, offset) - charpos;
20357 n += display_string (NULL, elt, Qnil, 0, charpos,
20358 it, 0, nchars, 0,
20359 STRING_MULTIBYTE (elt));
20360 }
20361 break;
20362 }
20363 }
20364 else /* c == '%' */
20365 {
20366 ptrdiff_t percent_position = offset;
20367
20368 /* Get the specified minimum width. Zero means
20369 don't pad. */
20370 field = 0;
20371 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20372 field = field * 10 + c - '0';
20373
20374 /* Don't pad beyond the total padding allowed. */
20375 if (field_width - n > 0 && field > field_width - n)
20376 field = field_width - n;
20377
20378 /* Note that either PRECISION <= 0 or N < PRECISION. */
20379 prec = precision - n;
20380
20381 if (c == 'M')
20382 n += display_mode_element (it, depth, field, prec,
20383 Vglobal_mode_string, props,
20384 risky);
20385 else if (c != 0)
20386 {
20387 int multibyte;
20388 ptrdiff_t bytepos, charpos;
20389 const char *spec;
20390 Lisp_Object string;
20391
20392 bytepos = percent_position;
20393 charpos = (STRING_MULTIBYTE (elt)
20394 ? string_byte_to_char (elt, bytepos)
20395 : bytepos);
20396 spec = decode_mode_spec (it->w, c, field, &string);
20397 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20398
20399 switch (mode_line_target)
20400 {
20401 case MODE_LINE_NOPROP:
20402 case MODE_LINE_TITLE:
20403 n += store_mode_line_noprop (spec, field, prec);
20404 break;
20405 case MODE_LINE_STRING:
20406 {
20407 Lisp_Object tem = build_string (spec);
20408 props = Ftext_properties_at (make_number (charpos), elt);
20409 /* Should only keep face property in props */
20410 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20411 }
20412 break;
20413 case MODE_LINE_DISPLAY:
20414 {
20415 int nglyphs_before, nwritten;
20416
20417 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20418 nwritten = display_string (spec, string, elt,
20419 charpos, 0, it,
20420 field, prec, 0,
20421 multibyte);
20422
20423 /* Assign to the glyphs written above the
20424 string where the `%x' came from, position
20425 of the `%'. */
20426 if (nwritten > 0)
20427 {
20428 struct glyph *glyph
20429 = (it->glyph_row->glyphs[TEXT_AREA]
20430 + nglyphs_before);
20431 int i;
20432
20433 for (i = 0; i < nwritten; ++i)
20434 {
20435 glyph[i].object = elt;
20436 glyph[i].charpos = charpos;
20437 }
20438
20439 n += nwritten;
20440 }
20441 }
20442 break;
20443 }
20444 }
20445 else /* c == 0 */
20446 break;
20447 }
20448 }
20449 }
20450 break;
20451
20452 case Lisp_Symbol:
20453 /* A symbol: process the value of the symbol recursively
20454 as if it appeared here directly. Avoid error if symbol void.
20455 Special case: if value of symbol is a string, output the string
20456 literally. */
20457 {
20458 register Lisp_Object tem;
20459
20460 /* If the variable is not marked as risky to set
20461 then its contents are risky to use. */
20462 if (NILP (Fget (elt, Qrisky_local_variable)))
20463 risky = 1;
20464
20465 tem = Fboundp (elt);
20466 if (!NILP (tem))
20467 {
20468 tem = Fsymbol_value (elt);
20469 /* If value is a string, output that string literally:
20470 don't check for % within it. */
20471 if (STRINGP (tem))
20472 literal = 1;
20473
20474 if (!EQ (tem, elt))
20475 {
20476 /* Give up right away for nil or t. */
20477 elt = tem;
20478 goto tail_recurse;
20479 }
20480 }
20481 }
20482 break;
20483
20484 case Lisp_Cons:
20485 {
20486 register Lisp_Object car, tem;
20487
20488 /* A cons cell: five distinct cases.
20489 If first element is :eval or :propertize, do something special.
20490 If first element is a string or a cons, process all the elements
20491 and effectively concatenate them.
20492 If first element is a negative number, truncate displaying cdr to
20493 at most that many characters. If positive, pad (with spaces)
20494 to at least that many characters.
20495 If first element is a symbol, process the cadr or caddr recursively
20496 according to whether the symbol's value is non-nil or nil. */
20497 car = XCAR (elt);
20498 if (EQ (car, QCeval))
20499 {
20500 /* An element of the form (:eval FORM) means evaluate FORM
20501 and use the result as mode line elements. */
20502
20503 if (risky)
20504 break;
20505
20506 if (CONSP (XCDR (elt)))
20507 {
20508 Lisp_Object spec;
20509 spec = safe_eval (XCAR (XCDR (elt)));
20510 n += display_mode_element (it, depth, field_width - n,
20511 precision - n, spec, props,
20512 risky);
20513 }
20514 }
20515 else if (EQ (car, QCpropertize))
20516 {
20517 /* An element of the form (:propertize ELT PROPS...)
20518 means display ELT but applying properties PROPS. */
20519
20520 if (risky)
20521 break;
20522
20523 if (CONSP (XCDR (elt)))
20524 n += display_mode_element (it, depth, field_width - n,
20525 precision - n, XCAR (XCDR (elt)),
20526 XCDR (XCDR (elt)), risky);
20527 }
20528 else if (SYMBOLP (car))
20529 {
20530 tem = Fboundp (car);
20531 elt = XCDR (elt);
20532 if (!CONSP (elt))
20533 goto invalid;
20534 /* elt is now the cdr, and we know it is a cons cell.
20535 Use its car if CAR has a non-nil value. */
20536 if (!NILP (tem))
20537 {
20538 tem = Fsymbol_value (car);
20539 if (!NILP (tem))
20540 {
20541 elt = XCAR (elt);
20542 goto tail_recurse;
20543 }
20544 }
20545 /* Symbol's value is nil (or symbol is unbound)
20546 Get the cddr of the original list
20547 and if possible find the caddr and use that. */
20548 elt = XCDR (elt);
20549 if (NILP (elt))
20550 break;
20551 else if (!CONSP (elt))
20552 goto invalid;
20553 elt = XCAR (elt);
20554 goto tail_recurse;
20555 }
20556 else if (INTEGERP (car))
20557 {
20558 register int lim = XINT (car);
20559 elt = XCDR (elt);
20560 if (lim < 0)
20561 {
20562 /* Negative int means reduce maximum width. */
20563 if (precision <= 0)
20564 precision = -lim;
20565 else
20566 precision = min (precision, -lim);
20567 }
20568 else if (lim > 0)
20569 {
20570 /* Padding specified. Don't let it be more than
20571 current maximum. */
20572 if (precision > 0)
20573 lim = min (precision, lim);
20574
20575 /* If that's more padding than already wanted, queue it.
20576 But don't reduce padding already specified even if
20577 that is beyond the current truncation point. */
20578 field_width = max (lim, field_width);
20579 }
20580 goto tail_recurse;
20581 }
20582 else if (STRINGP (car) || CONSP (car))
20583 {
20584 Lisp_Object halftail = elt;
20585 int len = 0;
20586
20587 while (CONSP (elt)
20588 && (precision <= 0 || n < precision))
20589 {
20590 n += display_mode_element (it, depth,
20591 /* Do padding only after the last
20592 element in the list. */
20593 (! CONSP (XCDR (elt))
20594 ? field_width - n
20595 : 0),
20596 precision - n, XCAR (elt),
20597 props, risky);
20598 elt = XCDR (elt);
20599 len++;
20600 if ((len & 1) == 0)
20601 halftail = XCDR (halftail);
20602 /* Check for cycle. */
20603 if (EQ (halftail, elt))
20604 break;
20605 }
20606 }
20607 }
20608 break;
20609
20610 default:
20611 invalid:
20612 elt = build_string ("*invalid*");
20613 goto tail_recurse;
20614 }
20615
20616 /* Pad to FIELD_WIDTH. */
20617 if (field_width > 0 && n < field_width)
20618 {
20619 switch (mode_line_target)
20620 {
20621 case MODE_LINE_NOPROP:
20622 case MODE_LINE_TITLE:
20623 n += store_mode_line_noprop ("", field_width - n, 0);
20624 break;
20625 case MODE_LINE_STRING:
20626 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20627 break;
20628 case MODE_LINE_DISPLAY:
20629 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20630 0, 0, 0);
20631 break;
20632 }
20633 }
20634
20635 return n;
20636 }
20637
20638 /* Store a mode-line string element in mode_line_string_list.
20639
20640 If STRING is non-null, display that C string. Otherwise, the Lisp
20641 string LISP_STRING is displayed.
20642
20643 FIELD_WIDTH is the minimum number of output glyphs to produce.
20644 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20645 with spaces. FIELD_WIDTH <= 0 means don't pad.
20646
20647 PRECISION is the maximum number of characters to output from
20648 STRING. PRECISION <= 0 means don't truncate the string.
20649
20650 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20651 properties to the string.
20652
20653 PROPS are the properties to add to the string.
20654 The mode_line_string_face face property is always added to the string.
20655 */
20656
20657 static int
20658 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20659 int field_width, int precision, Lisp_Object props)
20660 {
20661 ptrdiff_t len;
20662 int n = 0;
20663
20664 if (string != NULL)
20665 {
20666 len = strlen (string);
20667 if (precision > 0 && len > precision)
20668 len = precision;
20669 lisp_string = make_string (string, len);
20670 if (NILP (props))
20671 props = mode_line_string_face_prop;
20672 else if (!NILP (mode_line_string_face))
20673 {
20674 Lisp_Object face = Fplist_get (props, Qface);
20675 props = Fcopy_sequence (props);
20676 if (NILP (face))
20677 face = mode_line_string_face;
20678 else
20679 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20680 props = Fplist_put (props, Qface, face);
20681 }
20682 Fadd_text_properties (make_number (0), make_number (len),
20683 props, lisp_string);
20684 }
20685 else
20686 {
20687 len = XFASTINT (Flength (lisp_string));
20688 if (precision > 0 && len > precision)
20689 {
20690 len = precision;
20691 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20692 precision = -1;
20693 }
20694 if (!NILP (mode_line_string_face))
20695 {
20696 Lisp_Object face;
20697 if (NILP (props))
20698 props = Ftext_properties_at (make_number (0), lisp_string);
20699 face = Fplist_get (props, Qface);
20700 if (NILP (face))
20701 face = mode_line_string_face;
20702 else
20703 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20704 props = Fcons (Qface, Fcons (face, Qnil));
20705 if (copy_string)
20706 lisp_string = Fcopy_sequence (lisp_string);
20707 }
20708 if (!NILP (props))
20709 Fadd_text_properties (make_number (0), make_number (len),
20710 props, lisp_string);
20711 }
20712
20713 if (len > 0)
20714 {
20715 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20716 n += len;
20717 }
20718
20719 if (field_width > len)
20720 {
20721 field_width -= len;
20722 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20723 if (!NILP (props))
20724 Fadd_text_properties (make_number (0), make_number (field_width),
20725 props, lisp_string);
20726 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20727 n += field_width;
20728 }
20729
20730 return n;
20731 }
20732
20733
20734 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20735 1, 4, 0,
20736 doc: /* Format a string out of a mode line format specification.
20737 First arg FORMAT specifies the mode line format (see `mode-line-format'
20738 for details) to use.
20739
20740 By default, the format is evaluated for the currently selected window.
20741
20742 Optional second arg FACE specifies the face property to put on all
20743 characters for which no face is specified. The value nil means the
20744 default face. The value t means whatever face the window's mode line
20745 currently uses (either `mode-line' or `mode-line-inactive',
20746 depending on whether the window is the selected window or not).
20747 An integer value means the value string has no text
20748 properties.
20749
20750 Optional third and fourth args WINDOW and BUFFER specify the window
20751 and buffer to use as the context for the formatting (defaults
20752 are the selected window and the WINDOW's buffer). */)
20753 (Lisp_Object format, Lisp_Object face,
20754 Lisp_Object window, Lisp_Object buffer)
20755 {
20756 struct it it;
20757 int len;
20758 struct window *w;
20759 struct buffer *old_buffer = NULL;
20760 int face_id;
20761 int no_props = INTEGERP (face);
20762 ptrdiff_t count = SPECPDL_INDEX ();
20763 Lisp_Object str;
20764 int string_start = 0;
20765
20766 if (NILP (window))
20767 window = selected_window;
20768 CHECK_WINDOW (window);
20769 w = XWINDOW (window);
20770
20771 if (NILP (buffer))
20772 buffer = w->buffer;
20773 CHECK_BUFFER (buffer);
20774
20775 /* Make formatting the modeline a non-op when noninteractive, otherwise
20776 there will be problems later caused by a partially initialized frame. */
20777 if (NILP (format) || noninteractive)
20778 return empty_unibyte_string;
20779
20780 if (no_props)
20781 face = Qnil;
20782
20783 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20784 : EQ (face, Qt) ? (EQ (window, selected_window)
20785 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20786 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20787 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20788 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20789 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20790 : DEFAULT_FACE_ID;
20791
20792 if (XBUFFER (buffer) != current_buffer)
20793 old_buffer = current_buffer;
20794
20795 /* Save things including mode_line_proptrans_alist,
20796 and set that to nil so that we don't alter the outer value. */
20797 record_unwind_protect (unwind_format_mode_line,
20798 format_mode_line_unwind_data
20799 (old_buffer, selected_window, 1));
20800 mode_line_proptrans_alist = Qnil;
20801
20802 Fselect_window (window, Qt);
20803 if (old_buffer)
20804 set_buffer_internal_1 (XBUFFER (buffer));
20805
20806 init_iterator (&it, w, -1, -1, NULL, face_id);
20807
20808 if (no_props)
20809 {
20810 mode_line_target = MODE_LINE_NOPROP;
20811 mode_line_string_face_prop = Qnil;
20812 mode_line_string_list = Qnil;
20813 string_start = MODE_LINE_NOPROP_LEN (0);
20814 }
20815 else
20816 {
20817 mode_line_target = MODE_LINE_STRING;
20818 mode_line_string_list = Qnil;
20819 mode_line_string_face = face;
20820 mode_line_string_face_prop
20821 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20822 }
20823
20824 push_kboard (FRAME_KBOARD (it.f));
20825 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20826 pop_kboard ();
20827
20828 if (no_props)
20829 {
20830 len = MODE_LINE_NOPROP_LEN (string_start);
20831 str = make_string (mode_line_noprop_buf + string_start, len);
20832 }
20833 else
20834 {
20835 mode_line_string_list = Fnreverse (mode_line_string_list);
20836 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20837 empty_unibyte_string);
20838 }
20839
20840 unbind_to (count, Qnil);
20841 return str;
20842 }
20843
20844 /* Write a null-terminated, right justified decimal representation of
20845 the positive integer D to BUF using a minimal field width WIDTH. */
20846
20847 static void
20848 pint2str (register char *buf, register int width, register ptrdiff_t d)
20849 {
20850 register char *p = buf;
20851
20852 if (d <= 0)
20853 *p++ = '0';
20854 else
20855 {
20856 while (d > 0)
20857 {
20858 *p++ = d % 10 + '0';
20859 d /= 10;
20860 }
20861 }
20862
20863 for (width -= (int) (p - buf); width > 0; --width)
20864 *p++ = ' ';
20865 *p-- = '\0';
20866 while (p > buf)
20867 {
20868 d = *buf;
20869 *buf++ = *p;
20870 *p-- = d;
20871 }
20872 }
20873
20874 /* Write a null-terminated, right justified decimal and "human
20875 readable" representation of the nonnegative integer D to BUF using
20876 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20877
20878 static const char power_letter[] =
20879 {
20880 0, /* no letter */
20881 'k', /* kilo */
20882 'M', /* mega */
20883 'G', /* giga */
20884 'T', /* tera */
20885 'P', /* peta */
20886 'E', /* exa */
20887 'Z', /* zetta */
20888 'Y' /* yotta */
20889 };
20890
20891 static void
20892 pint2hrstr (char *buf, int width, ptrdiff_t d)
20893 {
20894 /* We aim to represent the nonnegative integer D as
20895 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20896 ptrdiff_t quotient = d;
20897 int remainder = 0;
20898 /* -1 means: do not use TENTHS. */
20899 int tenths = -1;
20900 int exponent = 0;
20901
20902 /* Length of QUOTIENT.TENTHS as a string. */
20903 int length;
20904
20905 char * psuffix;
20906 char * p;
20907
20908 if (1000 <= quotient)
20909 {
20910 /* Scale to the appropriate EXPONENT. */
20911 do
20912 {
20913 remainder = quotient % 1000;
20914 quotient /= 1000;
20915 exponent++;
20916 }
20917 while (1000 <= quotient);
20918
20919 /* Round to nearest and decide whether to use TENTHS or not. */
20920 if (quotient <= 9)
20921 {
20922 tenths = remainder / 100;
20923 if (50 <= remainder % 100)
20924 {
20925 if (tenths < 9)
20926 tenths++;
20927 else
20928 {
20929 quotient++;
20930 if (quotient == 10)
20931 tenths = -1;
20932 else
20933 tenths = 0;
20934 }
20935 }
20936 }
20937 else
20938 if (500 <= remainder)
20939 {
20940 if (quotient < 999)
20941 quotient++;
20942 else
20943 {
20944 quotient = 1;
20945 exponent++;
20946 tenths = 0;
20947 }
20948 }
20949 }
20950
20951 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20952 if (tenths == -1 && quotient <= 99)
20953 if (quotient <= 9)
20954 length = 1;
20955 else
20956 length = 2;
20957 else
20958 length = 3;
20959 p = psuffix = buf + max (width, length);
20960
20961 /* Print EXPONENT. */
20962 *psuffix++ = power_letter[exponent];
20963 *psuffix = '\0';
20964
20965 /* Print TENTHS. */
20966 if (tenths >= 0)
20967 {
20968 *--p = '0' + tenths;
20969 *--p = '.';
20970 }
20971
20972 /* Print QUOTIENT. */
20973 do
20974 {
20975 int digit = quotient % 10;
20976 *--p = '0' + digit;
20977 }
20978 while ((quotient /= 10) != 0);
20979
20980 /* Print leading spaces. */
20981 while (buf < p)
20982 *--p = ' ';
20983 }
20984
20985 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20986 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20987 type of CODING_SYSTEM. Return updated pointer into BUF. */
20988
20989 static unsigned char invalid_eol_type[] = "(*invalid*)";
20990
20991 static char *
20992 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20993 {
20994 Lisp_Object val;
20995 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20996 const unsigned char *eol_str;
20997 int eol_str_len;
20998 /* The EOL conversion we are using. */
20999 Lisp_Object eoltype;
21000
21001 val = CODING_SYSTEM_SPEC (coding_system);
21002 eoltype = Qnil;
21003
21004 if (!VECTORP (val)) /* Not yet decided. */
21005 {
21006 if (multibyte)
21007 *buf++ = '-';
21008 if (eol_flag)
21009 eoltype = eol_mnemonic_undecided;
21010 /* Don't mention EOL conversion if it isn't decided. */
21011 }
21012 else
21013 {
21014 Lisp_Object attrs;
21015 Lisp_Object eolvalue;
21016
21017 attrs = AREF (val, 0);
21018 eolvalue = AREF (val, 2);
21019
21020 if (multibyte)
21021 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
21022
21023 if (eol_flag)
21024 {
21025 /* The EOL conversion that is normal on this system. */
21026
21027 if (NILP (eolvalue)) /* Not yet decided. */
21028 eoltype = eol_mnemonic_undecided;
21029 else if (VECTORP (eolvalue)) /* Not yet decided. */
21030 eoltype = eol_mnemonic_undecided;
21031 else /* eolvalue is Qunix, Qdos, or Qmac. */
21032 eoltype = (EQ (eolvalue, Qunix)
21033 ? eol_mnemonic_unix
21034 : (EQ (eolvalue, Qdos) == 1
21035 ? eol_mnemonic_dos : eol_mnemonic_mac));
21036 }
21037 }
21038
21039 if (eol_flag)
21040 {
21041 /* Mention the EOL conversion if it is not the usual one. */
21042 if (STRINGP (eoltype))
21043 {
21044 eol_str = SDATA (eoltype);
21045 eol_str_len = SBYTES (eoltype);
21046 }
21047 else if (CHARACTERP (eoltype))
21048 {
21049 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
21050 int c = XFASTINT (eoltype);
21051 eol_str_len = CHAR_STRING (c, tmp);
21052 eol_str = tmp;
21053 }
21054 else
21055 {
21056 eol_str = invalid_eol_type;
21057 eol_str_len = sizeof (invalid_eol_type) - 1;
21058 }
21059 memcpy (buf, eol_str, eol_str_len);
21060 buf += eol_str_len;
21061 }
21062
21063 return buf;
21064 }
21065
21066 /* Return a string for the output of a mode line %-spec for window W,
21067 generated by character C. FIELD_WIDTH > 0 means pad the string
21068 returned with spaces to that value. Return a Lisp string in
21069 *STRING if the resulting string is taken from that Lisp string.
21070
21071 Note we operate on the current buffer for most purposes,
21072 the exception being w->base_line_pos. */
21073
21074 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21075
21076 static const char *
21077 decode_mode_spec (struct window *w, register int c, int field_width,
21078 Lisp_Object *string)
21079 {
21080 Lisp_Object obj;
21081 struct frame *f = XFRAME (WINDOW_FRAME (w));
21082 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21083 struct buffer *b = current_buffer;
21084
21085 obj = Qnil;
21086 *string = Qnil;
21087
21088 switch (c)
21089 {
21090 case '*':
21091 if (!NILP (BVAR (b, read_only)))
21092 return "%";
21093 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21094 return "*";
21095 return "-";
21096
21097 case '+':
21098 /* This differs from %* only for a modified read-only buffer. */
21099 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21100 return "*";
21101 if (!NILP (BVAR (b, read_only)))
21102 return "%";
21103 return "-";
21104
21105 case '&':
21106 /* This differs from %* in ignoring read-only-ness. */
21107 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21108 return "*";
21109 return "-";
21110
21111 case '%':
21112 return "%";
21113
21114 case '[':
21115 {
21116 int i;
21117 char *p;
21118
21119 if (command_loop_level > 5)
21120 return "[[[... ";
21121 p = decode_mode_spec_buf;
21122 for (i = 0; i < command_loop_level; i++)
21123 *p++ = '[';
21124 *p = 0;
21125 return decode_mode_spec_buf;
21126 }
21127
21128 case ']':
21129 {
21130 int i;
21131 char *p;
21132
21133 if (command_loop_level > 5)
21134 return " ...]]]";
21135 p = decode_mode_spec_buf;
21136 for (i = 0; i < command_loop_level; i++)
21137 *p++ = ']';
21138 *p = 0;
21139 return decode_mode_spec_buf;
21140 }
21141
21142 case '-':
21143 {
21144 register int i;
21145
21146 /* Let lots_of_dashes be a string of infinite length. */
21147 if (mode_line_target == MODE_LINE_NOPROP ||
21148 mode_line_target == MODE_LINE_STRING)
21149 return "--";
21150 if (field_width <= 0
21151 || field_width > sizeof (lots_of_dashes))
21152 {
21153 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21154 decode_mode_spec_buf[i] = '-';
21155 decode_mode_spec_buf[i] = '\0';
21156 return decode_mode_spec_buf;
21157 }
21158 else
21159 return lots_of_dashes;
21160 }
21161
21162 case 'b':
21163 obj = BVAR (b, name);
21164 break;
21165
21166 case 'c':
21167 /* %c and %l are ignored in `frame-title-format'.
21168 (In redisplay_internal, the frame title is drawn _before_ the
21169 windows are updated, so the stuff which depends on actual
21170 window contents (such as %l) may fail to render properly, or
21171 even crash emacs.) */
21172 if (mode_line_target == MODE_LINE_TITLE)
21173 return "";
21174 else
21175 {
21176 ptrdiff_t col = current_column ();
21177 w->column_number_displayed = make_number (col);
21178 pint2str (decode_mode_spec_buf, field_width, col);
21179 return decode_mode_spec_buf;
21180 }
21181
21182 case 'e':
21183 #ifndef SYSTEM_MALLOC
21184 {
21185 if (NILP (Vmemory_full))
21186 return "";
21187 else
21188 return "!MEM FULL! ";
21189 }
21190 #else
21191 return "";
21192 #endif
21193
21194 case 'F':
21195 /* %F displays the frame name. */
21196 if (!NILP (f->title))
21197 return SSDATA (f->title);
21198 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21199 return SSDATA (f->name);
21200 return "Emacs";
21201
21202 case 'f':
21203 obj = BVAR (b, filename);
21204 break;
21205
21206 case 'i':
21207 {
21208 ptrdiff_t size = ZV - BEGV;
21209 pint2str (decode_mode_spec_buf, field_width, size);
21210 return decode_mode_spec_buf;
21211 }
21212
21213 case 'I':
21214 {
21215 ptrdiff_t size = ZV - BEGV;
21216 pint2hrstr (decode_mode_spec_buf, field_width, size);
21217 return decode_mode_spec_buf;
21218 }
21219
21220 case 'l':
21221 {
21222 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21223 ptrdiff_t topline, nlines, height;
21224 ptrdiff_t junk;
21225
21226 /* %c and %l are ignored in `frame-title-format'. */
21227 if (mode_line_target == MODE_LINE_TITLE)
21228 return "";
21229
21230 startpos = XMARKER (w->start)->charpos;
21231 startpos_byte = marker_byte_position (w->start);
21232 height = WINDOW_TOTAL_LINES (w);
21233
21234 /* If we decided that this buffer isn't suitable for line numbers,
21235 don't forget that too fast. */
21236 if (EQ (w->base_line_pos, w->buffer))
21237 goto no_value;
21238 /* But do forget it, if the window shows a different buffer now. */
21239 else if (BUFFERP (w->base_line_pos))
21240 w->base_line_pos = Qnil;
21241
21242 /* If the buffer is very big, don't waste time. */
21243 if (INTEGERP (Vline_number_display_limit)
21244 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21245 {
21246 w->base_line_pos = Qnil;
21247 w->base_line_number = Qnil;
21248 goto no_value;
21249 }
21250
21251 if (INTEGERP (w->base_line_number)
21252 && INTEGERP (w->base_line_pos)
21253 && XFASTINT (w->base_line_pos) <= startpos)
21254 {
21255 line = XFASTINT (w->base_line_number);
21256 linepos = XFASTINT (w->base_line_pos);
21257 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21258 }
21259 else
21260 {
21261 line = 1;
21262 linepos = BUF_BEGV (b);
21263 linepos_byte = BUF_BEGV_BYTE (b);
21264 }
21265
21266 /* Count lines from base line to window start position. */
21267 nlines = display_count_lines (linepos_byte,
21268 startpos_byte,
21269 startpos, &junk);
21270
21271 topline = nlines + line;
21272
21273 /* Determine a new base line, if the old one is too close
21274 or too far away, or if we did not have one.
21275 "Too close" means it's plausible a scroll-down would
21276 go back past it. */
21277 if (startpos == BUF_BEGV (b))
21278 {
21279 w->base_line_number = make_number (topline);
21280 w->base_line_pos = make_number (BUF_BEGV (b));
21281 }
21282 else if (nlines < height + 25 || nlines > height * 3 + 50
21283 || linepos == BUF_BEGV (b))
21284 {
21285 ptrdiff_t limit = BUF_BEGV (b);
21286 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21287 ptrdiff_t position;
21288 ptrdiff_t distance =
21289 (height * 2 + 30) * line_number_display_limit_width;
21290
21291 if (startpos - distance > limit)
21292 {
21293 limit = startpos - distance;
21294 limit_byte = CHAR_TO_BYTE (limit);
21295 }
21296
21297 nlines = display_count_lines (startpos_byte,
21298 limit_byte,
21299 - (height * 2 + 30),
21300 &position);
21301 /* If we couldn't find the lines we wanted within
21302 line_number_display_limit_width chars per line,
21303 give up on line numbers for this window. */
21304 if (position == limit_byte && limit == startpos - distance)
21305 {
21306 w->base_line_pos = w->buffer;
21307 w->base_line_number = Qnil;
21308 goto no_value;
21309 }
21310
21311 w->base_line_number = make_number (topline - nlines);
21312 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21313 }
21314
21315 /* Now count lines from the start pos to point. */
21316 nlines = display_count_lines (startpos_byte,
21317 PT_BYTE, PT, &junk);
21318
21319 /* Record that we did display the line number. */
21320 line_number_displayed = 1;
21321
21322 /* Make the string to show. */
21323 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21324 return decode_mode_spec_buf;
21325 no_value:
21326 {
21327 char* p = decode_mode_spec_buf;
21328 int pad = field_width - 2;
21329 while (pad-- > 0)
21330 *p++ = ' ';
21331 *p++ = '?';
21332 *p++ = '?';
21333 *p = '\0';
21334 return decode_mode_spec_buf;
21335 }
21336 }
21337 break;
21338
21339 case 'm':
21340 obj = BVAR (b, mode_name);
21341 break;
21342
21343 case 'n':
21344 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21345 return " Narrow";
21346 break;
21347
21348 case 'p':
21349 {
21350 ptrdiff_t pos = marker_position (w->start);
21351 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21352
21353 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21354 {
21355 if (pos <= BUF_BEGV (b))
21356 return "All";
21357 else
21358 return "Bottom";
21359 }
21360 else if (pos <= BUF_BEGV (b))
21361 return "Top";
21362 else
21363 {
21364 if (total > 1000000)
21365 /* Do it differently for a large value, to avoid overflow. */
21366 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21367 else
21368 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21369 /* We can't normally display a 3-digit number,
21370 so get us a 2-digit number that is close. */
21371 if (total == 100)
21372 total = 99;
21373 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21374 return decode_mode_spec_buf;
21375 }
21376 }
21377
21378 /* Display percentage of size above the bottom of the screen. */
21379 case 'P':
21380 {
21381 ptrdiff_t toppos = marker_position (w->start);
21382 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21383 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21384
21385 if (botpos >= BUF_ZV (b))
21386 {
21387 if (toppos <= BUF_BEGV (b))
21388 return "All";
21389 else
21390 return "Bottom";
21391 }
21392 else
21393 {
21394 if (total > 1000000)
21395 /* Do it differently for a large value, to avoid overflow. */
21396 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21397 else
21398 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21399 /* We can't normally display a 3-digit number,
21400 so get us a 2-digit number that is close. */
21401 if (total == 100)
21402 total = 99;
21403 if (toppos <= BUF_BEGV (b))
21404 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21405 else
21406 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21407 return decode_mode_spec_buf;
21408 }
21409 }
21410
21411 case 's':
21412 /* status of process */
21413 obj = Fget_buffer_process (Fcurrent_buffer ());
21414 if (NILP (obj))
21415 return "no process";
21416 #ifndef MSDOS
21417 obj = Fsymbol_name (Fprocess_status (obj));
21418 #endif
21419 break;
21420
21421 case '@':
21422 {
21423 ptrdiff_t count = inhibit_garbage_collection ();
21424 Lisp_Object val = call1 (intern ("file-remote-p"),
21425 BVAR (current_buffer, directory));
21426 unbind_to (count, Qnil);
21427
21428 if (NILP (val))
21429 return "-";
21430 else
21431 return "@";
21432 }
21433
21434 case 't': /* indicate TEXT or BINARY */
21435 return "T";
21436
21437 case 'z':
21438 /* coding-system (not including end-of-line format) */
21439 case 'Z':
21440 /* coding-system (including end-of-line type) */
21441 {
21442 int eol_flag = (c == 'Z');
21443 char *p = decode_mode_spec_buf;
21444
21445 if (! FRAME_WINDOW_P (f))
21446 {
21447 /* No need to mention EOL here--the terminal never needs
21448 to do EOL conversion. */
21449 p = decode_mode_spec_coding (CODING_ID_NAME
21450 (FRAME_KEYBOARD_CODING (f)->id),
21451 p, 0);
21452 p = decode_mode_spec_coding (CODING_ID_NAME
21453 (FRAME_TERMINAL_CODING (f)->id),
21454 p, 0);
21455 }
21456 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21457 p, eol_flag);
21458
21459 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21460 #ifdef subprocesses
21461 obj = Fget_buffer_process (Fcurrent_buffer ());
21462 if (PROCESSP (obj))
21463 {
21464 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21465 p, eol_flag);
21466 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21467 p, eol_flag);
21468 }
21469 #endif /* subprocesses */
21470 #endif /* 0 */
21471 *p = 0;
21472 return decode_mode_spec_buf;
21473 }
21474 }
21475
21476 if (STRINGP (obj))
21477 {
21478 *string = obj;
21479 return SSDATA (obj);
21480 }
21481 else
21482 return "";
21483 }
21484
21485
21486 /* Count up to COUNT lines starting from START_BYTE.
21487 But don't go beyond LIMIT_BYTE.
21488 Return the number of lines thus found (always nonnegative).
21489
21490 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21491
21492 static ptrdiff_t
21493 display_count_lines (ptrdiff_t start_byte,
21494 ptrdiff_t limit_byte, ptrdiff_t count,
21495 ptrdiff_t *byte_pos_ptr)
21496 {
21497 register unsigned char *cursor;
21498 unsigned char *base;
21499
21500 register ptrdiff_t ceiling;
21501 register unsigned char *ceiling_addr;
21502 ptrdiff_t orig_count = count;
21503
21504 /* If we are not in selective display mode,
21505 check only for newlines. */
21506 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21507 && !INTEGERP (BVAR (current_buffer, selective_display)));
21508
21509 if (count > 0)
21510 {
21511 while (start_byte < limit_byte)
21512 {
21513 ceiling = BUFFER_CEILING_OF (start_byte);
21514 ceiling = min (limit_byte - 1, ceiling);
21515 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21516 base = (cursor = BYTE_POS_ADDR (start_byte));
21517 while (1)
21518 {
21519 if (selective_display)
21520 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21521 ;
21522 else
21523 while (*cursor != '\n' && ++cursor != ceiling_addr)
21524 ;
21525
21526 if (cursor != ceiling_addr)
21527 {
21528 if (--count == 0)
21529 {
21530 start_byte += cursor - base + 1;
21531 *byte_pos_ptr = start_byte;
21532 return orig_count;
21533 }
21534 else
21535 if (++cursor == ceiling_addr)
21536 break;
21537 }
21538 else
21539 break;
21540 }
21541 start_byte += cursor - base;
21542 }
21543 }
21544 else
21545 {
21546 while (start_byte > limit_byte)
21547 {
21548 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21549 ceiling = max (limit_byte, ceiling);
21550 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21551 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21552 while (1)
21553 {
21554 if (selective_display)
21555 while (--cursor != ceiling_addr
21556 && *cursor != '\n' && *cursor != 015)
21557 ;
21558 else
21559 while (--cursor != ceiling_addr && *cursor != '\n')
21560 ;
21561
21562 if (cursor != ceiling_addr)
21563 {
21564 if (++count == 0)
21565 {
21566 start_byte += cursor - base + 1;
21567 *byte_pos_ptr = start_byte;
21568 /* When scanning backwards, we should
21569 not count the newline posterior to which we stop. */
21570 return - orig_count - 1;
21571 }
21572 }
21573 else
21574 break;
21575 }
21576 /* Here we add 1 to compensate for the last decrement
21577 of CURSOR, which took it past the valid range. */
21578 start_byte += cursor - base + 1;
21579 }
21580 }
21581
21582 *byte_pos_ptr = limit_byte;
21583
21584 if (count < 0)
21585 return - orig_count + count;
21586 return orig_count - count;
21587
21588 }
21589
21590
21591 \f
21592 /***********************************************************************
21593 Displaying strings
21594 ***********************************************************************/
21595
21596 /* Display a NUL-terminated string, starting with index START.
21597
21598 If STRING is non-null, display that C string. Otherwise, the Lisp
21599 string LISP_STRING is displayed. There's a case that STRING is
21600 non-null and LISP_STRING is not nil. It means STRING is a string
21601 data of LISP_STRING. In that case, we display LISP_STRING while
21602 ignoring its text properties.
21603
21604 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21605 FACE_STRING. Display STRING or LISP_STRING with the face at
21606 FACE_STRING_POS in FACE_STRING:
21607
21608 Display the string in the environment given by IT, but use the
21609 standard display table, temporarily.
21610
21611 FIELD_WIDTH is the minimum number of output glyphs to produce.
21612 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21613 with spaces. If STRING has more characters, more than FIELD_WIDTH
21614 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21615
21616 PRECISION is the maximum number of characters to output from
21617 STRING. PRECISION < 0 means don't truncate the string.
21618
21619 This is roughly equivalent to printf format specifiers:
21620
21621 FIELD_WIDTH PRECISION PRINTF
21622 ----------------------------------------
21623 -1 -1 %s
21624 -1 10 %.10s
21625 10 -1 %10s
21626 20 10 %20.10s
21627
21628 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21629 display them, and < 0 means obey the current buffer's value of
21630 enable_multibyte_characters.
21631
21632 Value is the number of columns displayed. */
21633
21634 static int
21635 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21636 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21637 int field_width, int precision, int max_x, int multibyte)
21638 {
21639 int hpos_at_start = it->hpos;
21640 int saved_face_id = it->face_id;
21641 struct glyph_row *row = it->glyph_row;
21642 ptrdiff_t it_charpos;
21643
21644 /* Initialize the iterator IT for iteration over STRING beginning
21645 with index START. */
21646 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21647 precision, field_width, multibyte);
21648 if (string && STRINGP (lisp_string))
21649 /* LISP_STRING is the one returned by decode_mode_spec. We should
21650 ignore its text properties. */
21651 it->stop_charpos = it->end_charpos;
21652
21653 /* If displaying STRING, set up the face of the iterator from
21654 FACE_STRING, if that's given. */
21655 if (STRINGP (face_string))
21656 {
21657 ptrdiff_t endptr;
21658 struct face *face;
21659
21660 it->face_id
21661 = face_at_string_position (it->w, face_string, face_string_pos,
21662 0, it->region_beg_charpos,
21663 it->region_end_charpos,
21664 &endptr, it->base_face_id, 0);
21665 face = FACE_FROM_ID (it->f, it->face_id);
21666 it->face_box_p = face->box != FACE_NO_BOX;
21667 }
21668
21669 /* Set max_x to the maximum allowed X position. Don't let it go
21670 beyond the right edge of the window. */
21671 if (max_x <= 0)
21672 max_x = it->last_visible_x;
21673 else
21674 max_x = min (max_x, it->last_visible_x);
21675
21676 /* Skip over display elements that are not visible. because IT->w is
21677 hscrolled. */
21678 if (it->current_x < it->first_visible_x)
21679 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21680 MOVE_TO_POS | MOVE_TO_X);
21681
21682 row->ascent = it->max_ascent;
21683 row->height = it->max_ascent + it->max_descent;
21684 row->phys_ascent = it->max_phys_ascent;
21685 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21686 row->extra_line_spacing = it->max_extra_line_spacing;
21687
21688 if (STRINGP (it->string))
21689 it_charpos = IT_STRING_CHARPOS (*it);
21690 else
21691 it_charpos = IT_CHARPOS (*it);
21692
21693 /* This condition is for the case that we are called with current_x
21694 past last_visible_x. */
21695 while (it->current_x < max_x)
21696 {
21697 int x_before, x, n_glyphs_before, i, nglyphs;
21698
21699 /* Get the next display element. */
21700 if (!get_next_display_element (it))
21701 break;
21702
21703 /* Produce glyphs. */
21704 x_before = it->current_x;
21705 n_glyphs_before = row->used[TEXT_AREA];
21706 PRODUCE_GLYPHS (it);
21707
21708 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21709 i = 0;
21710 x = x_before;
21711 while (i < nglyphs)
21712 {
21713 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21714
21715 if (it->line_wrap != TRUNCATE
21716 && x + glyph->pixel_width > max_x)
21717 {
21718 /* End of continued line or max_x reached. */
21719 if (CHAR_GLYPH_PADDING_P (*glyph))
21720 {
21721 /* A wide character is unbreakable. */
21722 if (row->reversed_p)
21723 unproduce_glyphs (it, row->used[TEXT_AREA]
21724 - n_glyphs_before);
21725 row->used[TEXT_AREA] = n_glyphs_before;
21726 it->current_x = x_before;
21727 }
21728 else
21729 {
21730 if (row->reversed_p)
21731 unproduce_glyphs (it, row->used[TEXT_AREA]
21732 - (n_glyphs_before + i));
21733 row->used[TEXT_AREA] = n_glyphs_before + i;
21734 it->current_x = x;
21735 }
21736 break;
21737 }
21738 else if (x + glyph->pixel_width >= it->first_visible_x)
21739 {
21740 /* Glyph is at least partially visible. */
21741 ++it->hpos;
21742 if (x < it->first_visible_x)
21743 row->x = x - it->first_visible_x;
21744 }
21745 else
21746 {
21747 /* Glyph is off the left margin of the display area.
21748 Should not happen. */
21749 abort ();
21750 }
21751
21752 row->ascent = max (row->ascent, it->max_ascent);
21753 row->height = max (row->height, it->max_ascent + it->max_descent);
21754 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21755 row->phys_height = max (row->phys_height,
21756 it->max_phys_ascent + it->max_phys_descent);
21757 row->extra_line_spacing = max (row->extra_line_spacing,
21758 it->max_extra_line_spacing);
21759 x += glyph->pixel_width;
21760 ++i;
21761 }
21762
21763 /* Stop if max_x reached. */
21764 if (i < nglyphs)
21765 break;
21766
21767 /* Stop at line ends. */
21768 if (ITERATOR_AT_END_OF_LINE_P (it))
21769 {
21770 it->continuation_lines_width = 0;
21771 break;
21772 }
21773
21774 set_iterator_to_next (it, 1);
21775 if (STRINGP (it->string))
21776 it_charpos = IT_STRING_CHARPOS (*it);
21777 else
21778 it_charpos = IT_CHARPOS (*it);
21779
21780 /* Stop if truncating at the right edge. */
21781 if (it->line_wrap == TRUNCATE
21782 && it->current_x >= it->last_visible_x)
21783 {
21784 /* Add truncation mark, but don't do it if the line is
21785 truncated at a padding space. */
21786 if (it_charpos < it->string_nchars)
21787 {
21788 if (!FRAME_WINDOW_P (it->f))
21789 {
21790 int ii, n;
21791
21792 if (it->current_x > it->last_visible_x)
21793 {
21794 if (!row->reversed_p)
21795 {
21796 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21797 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21798 break;
21799 }
21800 else
21801 {
21802 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21803 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21804 break;
21805 unproduce_glyphs (it, ii + 1);
21806 ii = row->used[TEXT_AREA] - (ii + 1);
21807 }
21808 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21809 {
21810 row->used[TEXT_AREA] = ii;
21811 produce_special_glyphs (it, IT_TRUNCATION);
21812 }
21813 }
21814 produce_special_glyphs (it, IT_TRUNCATION);
21815 }
21816 row->truncated_on_right_p = 1;
21817 }
21818 break;
21819 }
21820 }
21821
21822 /* Maybe insert a truncation at the left. */
21823 if (it->first_visible_x
21824 && it_charpos > 0)
21825 {
21826 if (!FRAME_WINDOW_P (it->f))
21827 insert_left_trunc_glyphs (it);
21828 row->truncated_on_left_p = 1;
21829 }
21830
21831 it->face_id = saved_face_id;
21832
21833 /* Value is number of columns displayed. */
21834 return it->hpos - hpos_at_start;
21835 }
21836
21837
21838 \f
21839 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21840 appears as an element of LIST or as the car of an element of LIST.
21841 If PROPVAL is a list, compare each element against LIST in that
21842 way, and return 1/2 if any element of PROPVAL is found in LIST.
21843 Otherwise return 0. This function cannot quit.
21844 The return value is 2 if the text is invisible but with an ellipsis
21845 and 1 if it's invisible and without an ellipsis. */
21846
21847 int
21848 invisible_p (register Lisp_Object propval, Lisp_Object list)
21849 {
21850 register Lisp_Object tail, proptail;
21851
21852 for (tail = list; CONSP (tail); tail = XCDR (tail))
21853 {
21854 register Lisp_Object tem;
21855 tem = XCAR (tail);
21856 if (EQ (propval, tem))
21857 return 1;
21858 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21859 return NILP (XCDR (tem)) ? 1 : 2;
21860 }
21861
21862 if (CONSP (propval))
21863 {
21864 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21865 {
21866 Lisp_Object propelt;
21867 propelt = XCAR (proptail);
21868 for (tail = list; CONSP (tail); tail = XCDR (tail))
21869 {
21870 register Lisp_Object tem;
21871 tem = XCAR (tail);
21872 if (EQ (propelt, tem))
21873 return 1;
21874 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21875 return NILP (XCDR (tem)) ? 1 : 2;
21876 }
21877 }
21878 }
21879
21880 return 0;
21881 }
21882
21883 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21884 doc: /* Non-nil if the property makes the text invisible.
21885 POS-OR-PROP can be a marker or number, in which case it is taken to be
21886 a position in the current buffer and the value of the `invisible' property
21887 is checked; or it can be some other value, which is then presumed to be the
21888 value of the `invisible' property of the text of interest.
21889 The non-nil value returned can be t for truly invisible text or something
21890 else if the text is replaced by an ellipsis. */)
21891 (Lisp_Object pos_or_prop)
21892 {
21893 Lisp_Object prop
21894 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21895 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21896 : pos_or_prop);
21897 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21898 return (invis == 0 ? Qnil
21899 : invis == 1 ? Qt
21900 : make_number (invis));
21901 }
21902
21903 /* Calculate a width or height in pixels from a specification using
21904 the following elements:
21905
21906 SPEC ::=
21907 NUM - a (fractional) multiple of the default font width/height
21908 (NUM) - specifies exactly NUM pixels
21909 UNIT - a fixed number of pixels, see below.
21910 ELEMENT - size of a display element in pixels, see below.
21911 (NUM . SPEC) - equals NUM * SPEC
21912 (+ SPEC SPEC ...) - add pixel values
21913 (- SPEC SPEC ...) - subtract pixel values
21914 (- SPEC) - negate pixel value
21915
21916 NUM ::=
21917 INT or FLOAT - a number constant
21918 SYMBOL - use symbol's (buffer local) variable binding.
21919
21920 UNIT ::=
21921 in - pixels per inch *)
21922 mm - pixels per 1/1000 meter *)
21923 cm - pixels per 1/100 meter *)
21924 width - width of current font in pixels.
21925 height - height of current font in pixels.
21926
21927 *) using the ratio(s) defined in display-pixels-per-inch.
21928
21929 ELEMENT ::=
21930
21931 left-fringe - left fringe width in pixels
21932 right-fringe - right fringe width in pixels
21933
21934 left-margin - left margin width in pixels
21935 right-margin - right margin width in pixels
21936
21937 scroll-bar - scroll-bar area width in pixels
21938
21939 Examples:
21940
21941 Pixels corresponding to 5 inches:
21942 (5 . in)
21943
21944 Total width of non-text areas on left side of window (if scroll-bar is on left):
21945 '(space :width (+ left-fringe left-margin scroll-bar))
21946
21947 Align to first text column (in header line):
21948 '(space :align-to 0)
21949
21950 Align to middle of text area minus half the width of variable `my-image'
21951 containing a loaded image:
21952 '(space :align-to (0.5 . (- text my-image)))
21953
21954 Width of left margin minus width of 1 character in the default font:
21955 '(space :width (- left-margin 1))
21956
21957 Width of left margin minus width of 2 characters in the current font:
21958 '(space :width (- left-margin (2 . width)))
21959
21960 Center 1 character over left-margin (in header line):
21961 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21962
21963 Different ways to express width of left fringe plus left margin minus one pixel:
21964 '(space :width (- (+ left-fringe left-margin) (1)))
21965 '(space :width (+ left-fringe left-margin (- (1))))
21966 '(space :width (+ left-fringe left-margin (-1)))
21967
21968 */
21969
21970 #define NUMVAL(X) \
21971 ((INTEGERP (X) || FLOATP (X)) \
21972 ? XFLOATINT (X) \
21973 : - 1)
21974
21975 static int
21976 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21977 struct font *font, int width_p, int *align_to)
21978 {
21979 double pixels;
21980
21981 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21982 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21983
21984 if (NILP (prop))
21985 return OK_PIXELS (0);
21986
21987 xassert (FRAME_LIVE_P (it->f));
21988
21989 if (SYMBOLP (prop))
21990 {
21991 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21992 {
21993 char *unit = SSDATA (SYMBOL_NAME (prop));
21994
21995 if (unit[0] == 'i' && unit[1] == 'n')
21996 pixels = 1.0;
21997 else if (unit[0] == 'm' && unit[1] == 'm')
21998 pixels = 25.4;
21999 else if (unit[0] == 'c' && unit[1] == 'm')
22000 pixels = 2.54;
22001 else
22002 pixels = 0;
22003 if (pixels > 0)
22004 {
22005 double ppi;
22006 #ifdef HAVE_WINDOW_SYSTEM
22007 if (FRAME_WINDOW_P (it->f)
22008 && (ppi = (width_p
22009 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22010 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22011 ppi > 0))
22012 return OK_PIXELS (ppi / pixels);
22013 #endif
22014
22015 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22016 || (CONSP (Vdisplay_pixels_per_inch)
22017 && (ppi = (width_p
22018 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22019 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22020 ppi > 0)))
22021 return OK_PIXELS (ppi / pixels);
22022
22023 return 0;
22024 }
22025 }
22026
22027 #ifdef HAVE_WINDOW_SYSTEM
22028 if (EQ (prop, Qheight))
22029 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22030 if (EQ (prop, Qwidth))
22031 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22032 #else
22033 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22034 return OK_PIXELS (1);
22035 #endif
22036
22037 if (EQ (prop, Qtext))
22038 return OK_PIXELS (width_p
22039 ? window_box_width (it->w, TEXT_AREA)
22040 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22041
22042 if (align_to && *align_to < 0)
22043 {
22044 *res = 0;
22045 if (EQ (prop, Qleft))
22046 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22047 if (EQ (prop, Qright))
22048 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22049 if (EQ (prop, Qcenter))
22050 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22051 + window_box_width (it->w, TEXT_AREA) / 2);
22052 if (EQ (prop, Qleft_fringe))
22053 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22054 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22055 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22056 if (EQ (prop, Qright_fringe))
22057 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22058 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22059 : window_box_right_offset (it->w, TEXT_AREA));
22060 if (EQ (prop, Qleft_margin))
22061 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22062 if (EQ (prop, Qright_margin))
22063 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22064 if (EQ (prop, Qscroll_bar))
22065 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22066 ? 0
22067 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22068 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22069 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22070 : 0)));
22071 }
22072 else
22073 {
22074 if (EQ (prop, Qleft_fringe))
22075 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22076 if (EQ (prop, Qright_fringe))
22077 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22078 if (EQ (prop, Qleft_margin))
22079 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22080 if (EQ (prop, Qright_margin))
22081 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22082 if (EQ (prop, Qscroll_bar))
22083 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22084 }
22085
22086 prop = Fbuffer_local_value (prop, it->w->buffer);
22087 }
22088
22089 if (INTEGERP (prop) || FLOATP (prop))
22090 {
22091 int base_unit = (width_p
22092 ? FRAME_COLUMN_WIDTH (it->f)
22093 : FRAME_LINE_HEIGHT (it->f));
22094 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22095 }
22096
22097 if (CONSP (prop))
22098 {
22099 Lisp_Object car = XCAR (prop);
22100 Lisp_Object cdr = XCDR (prop);
22101
22102 if (SYMBOLP (car))
22103 {
22104 #ifdef HAVE_WINDOW_SYSTEM
22105 if (FRAME_WINDOW_P (it->f)
22106 && valid_image_p (prop))
22107 {
22108 ptrdiff_t id = lookup_image (it->f, prop);
22109 struct image *img = IMAGE_FROM_ID (it->f, id);
22110
22111 return OK_PIXELS (width_p ? img->width : img->height);
22112 }
22113 #endif
22114 if (EQ (car, Qplus) || EQ (car, Qminus))
22115 {
22116 int first = 1;
22117 double px;
22118
22119 pixels = 0;
22120 while (CONSP (cdr))
22121 {
22122 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22123 font, width_p, align_to))
22124 return 0;
22125 if (first)
22126 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22127 else
22128 pixels += px;
22129 cdr = XCDR (cdr);
22130 }
22131 if (EQ (car, Qminus))
22132 pixels = -pixels;
22133 return OK_PIXELS (pixels);
22134 }
22135
22136 car = Fbuffer_local_value (car, it->w->buffer);
22137 }
22138
22139 if (INTEGERP (car) || FLOATP (car))
22140 {
22141 double fact;
22142 pixels = XFLOATINT (car);
22143 if (NILP (cdr))
22144 return OK_PIXELS (pixels);
22145 if (calc_pixel_width_or_height (&fact, it, cdr,
22146 font, width_p, align_to))
22147 return OK_PIXELS (pixels * fact);
22148 return 0;
22149 }
22150
22151 return 0;
22152 }
22153
22154 return 0;
22155 }
22156
22157 \f
22158 /***********************************************************************
22159 Glyph Display
22160 ***********************************************************************/
22161
22162 #ifdef HAVE_WINDOW_SYSTEM
22163
22164 #if GLYPH_DEBUG
22165
22166 void
22167 dump_glyph_string (struct glyph_string *s)
22168 {
22169 fprintf (stderr, "glyph string\n");
22170 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22171 s->x, s->y, s->width, s->height);
22172 fprintf (stderr, " ybase = %d\n", s->ybase);
22173 fprintf (stderr, " hl = %d\n", s->hl);
22174 fprintf (stderr, " left overhang = %d, right = %d\n",
22175 s->left_overhang, s->right_overhang);
22176 fprintf (stderr, " nchars = %d\n", s->nchars);
22177 fprintf (stderr, " extends to end of line = %d\n",
22178 s->extends_to_end_of_line_p);
22179 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22180 fprintf (stderr, " bg width = %d\n", s->background_width);
22181 }
22182
22183 #endif /* GLYPH_DEBUG */
22184
22185 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22186 of XChar2b structures for S; it can't be allocated in
22187 init_glyph_string because it must be allocated via `alloca'. W
22188 is the window on which S is drawn. ROW and AREA are the glyph row
22189 and area within the row from which S is constructed. START is the
22190 index of the first glyph structure covered by S. HL is a
22191 face-override for drawing S. */
22192
22193 #ifdef HAVE_NTGUI
22194 #define OPTIONAL_HDC(hdc) HDC hdc,
22195 #define DECLARE_HDC(hdc) HDC hdc;
22196 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22197 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22198 #endif
22199
22200 #ifndef OPTIONAL_HDC
22201 #define OPTIONAL_HDC(hdc)
22202 #define DECLARE_HDC(hdc)
22203 #define ALLOCATE_HDC(hdc, f)
22204 #define RELEASE_HDC(hdc, f)
22205 #endif
22206
22207 static void
22208 init_glyph_string (struct glyph_string *s,
22209 OPTIONAL_HDC (hdc)
22210 XChar2b *char2b, struct window *w, struct glyph_row *row,
22211 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22212 {
22213 memset (s, 0, sizeof *s);
22214 s->w = w;
22215 s->f = XFRAME (w->frame);
22216 #ifdef HAVE_NTGUI
22217 s->hdc = hdc;
22218 #endif
22219 s->display = FRAME_X_DISPLAY (s->f);
22220 s->window = FRAME_X_WINDOW (s->f);
22221 s->char2b = char2b;
22222 s->hl = hl;
22223 s->row = row;
22224 s->area = area;
22225 s->first_glyph = row->glyphs[area] + start;
22226 s->height = row->height;
22227 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22228 s->ybase = s->y + row->ascent;
22229 }
22230
22231
22232 /* Append the list of glyph strings with head H and tail T to the list
22233 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22234
22235 static inline void
22236 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22237 struct glyph_string *h, struct glyph_string *t)
22238 {
22239 if (h)
22240 {
22241 if (*head)
22242 (*tail)->next = h;
22243 else
22244 *head = h;
22245 h->prev = *tail;
22246 *tail = t;
22247 }
22248 }
22249
22250
22251 /* Prepend the list of glyph strings with head H and tail T to the
22252 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22253 result. */
22254
22255 static inline void
22256 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22257 struct glyph_string *h, struct glyph_string *t)
22258 {
22259 if (h)
22260 {
22261 if (*head)
22262 (*head)->prev = t;
22263 else
22264 *tail = t;
22265 t->next = *head;
22266 *head = h;
22267 }
22268 }
22269
22270
22271 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22272 Set *HEAD and *TAIL to the resulting list. */
22273
22274 static inline void
22275 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22276 struct glyph_string *s)
22277 {
22278 s->next = s->prev = NULL;
22279 append_glyph_string_lists (head, tail, s, s);
22280 }
22281
22282
22283 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22284 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22285 make sure that X resources for the face returned are allocated.
22286 Value is a pointer to a realized face that is ready for display if
22287 DISPLAY_P is non-zero. */
22288
22289 static inline struct face *
22290 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22291 XChar2b *char2b, int display_p)
22292 {
22293 struct face *face = FACE_FROM_ID (f, face_id);
22294
22295 if (face->font)
22296 {
22297 unsigned code = face->font->driver->encode_char (face->font, c);
22298
22299 if (code != FONT_INVALID_CODE)
22300 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22301 else
22302 STORE_XCHAR2B (char2b, 0, 0);
22303 }
22304
22305 /* Make sure X resources of the face are allocated. */
22306 #ifdef HAVE_X_WINDOWS
22307 if (display_p)
22308 #endif
22309 {
22310 xassert (face != NULL);
22311 PREPARE_FACE_FOR_DISPLAY (f, face);
22312 }
22313
22314 return face;
22315 }
22316
22317
22318 /* Get face and two-byte form of character glyph GLYPH on frame F.
22319 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22320 a pointer to a realized face that is ready for display. */
22321
22322 static inline struct face *
22323 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22324 XChar2b *char2b, int *two_byte_p)
22325 {
22326 struct face *face;
22327
22328 xassert (glyph->type == CHAR_GLYPH);
22329 face = FACE_FROM_ID (f, glyph->face_id);
22330
22331 if (two_byte_p)
22332 *two_byte_p = 0;
22333
22334 if (face->font)
22335 {
22336 unsigned code;
22337
22338 if (CHAR_BYTE8_P (glyph->u.ch))
22339 code = CHAR_TO_BYTE8 (glyph->u.ch);
22340 else
22341 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22342
22343 if (code != FONT_INVALID_CODE)
22344 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22345 else
22346 STORE_XCHAR2B (char2b, 0, 0);
22347 }
22348
22349 /* Make sure X resources of the face are allocated. */
22350 xassert (face != NULL);
22351 PREPARE_FACE_FOR_DISPLAY (f, face);
22352 return face;
22353 }
22354
22355
22356 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22357 Return 1 if FONT has a glyph for C, otherwise return 0. */
22358
22359 static inline int
22360 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22361 {
22362 unsigned code;
22363
22364 if (CHAR_BYTE8_P (c))
22365 code = CHAR_TO_BYTE8 (c);
22366 else
22367 code = font->driver->encode_char (font, c);
22368
22369 if (code == FONT_INVALID_CODE)
22370 return 0;
22371 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22372 return 1;
22373 }
22374
22375
22376 /* Fill glyph string S with composition components specified by S->cmp.
22377
22378 BASE_FACE is the base face of the composition.
22379 S->cmp_from is the index of the first component for S.
22380
22381 OVERLAPS non-zero means S should draw the foreground only, and use
22382 its physical height for clipping. See also draw_glyphs.
22383
22384 Value is the index of a component not in S. */
22385
22386 static int
22387 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22388 int overlaps)
22389 {
22390 int i;
22391 /* For all glyphs of this composition, starting at the offset
22392 S->cmp_from, until we reach the end of the definition or encounter a
22393 glyph that requires the different face, add it to S. */
22394 struct face *face;
22395
22396 xassert (s);
22397
22398 s->for_overlaps = overlaps;
22399 s->face = NULL;
22400 s->font = NULL;
22401 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22402 {
22403 int c = COMPOSITION_GLYPH (s->cmp, i);
22404
22405 /* TAB in a composition means display glyphs with padding space
22406 on the left or right. */
22407 if (c != '\t')
22408 {
22409 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22410 -1, Qnil);
22411
22412 face = get_char_face_and_encoding (s->f, c, face_id,
22413 s->char2b + i, 1);
22414 if (face)
22415 {
22416 if (! s->face)
22417 {
22418 s->face = face;
22419 s->font = s->face->font;
22420 }
22421 else if (s->face != face)
22422 break;
22423 }
22424 }
22425 ++s->nchars;
22426 }
22427 s->cmp_to = i;
22428
22429 if (s->face == NULL)
22430 {
22431 s->face = base_face->ascii_face;
22432 s->font = s->face->font;
22433 }
22434
22435 /* All glyph strings for the same composition has the same width,
22436 i.e. the width set for the first component of the composition. */
22437 s->width = s->first_glyph->pixel_width;
22438
22439 /* If the specified font could not be loaded, use the frame's
22440 default font, but record the fact that we couldn't load it in
22441 the glyph string so that we can draw rectangles for the
22442 characters of the glyph string. */
22443 if (s->font == NULL)
22444 {
22445 s->font_not_found_p = 1;
22446 s->font = FRAME_FONT (s->f);
22447 }
22448
22449 /* Adjust base line for subscript/superscript text. */
22450 s->ybase += s->first_glyph->voffset;
22451
22452 /* This glyph string must always be drawn with 16-bit functions. */
22453 s->two_byte_p = 1;
22454
22455 return s->cmp_to;
22456 }
22457
22458 static int
22459 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22460 int start, int end, int overlaps)
22461 {
22462 struct glyph *glyph, *last;
22463 Lisp_Object lgstring;
22464 int i;
22465
22466 s->for_overlaps = overlaps;
22467 glyph = s->row->glyphs[s->area] + start;
22468 last = s->row->glyphs[s->area] + end;
22469 s->cmp_id = glyph->u.cmp.id;
22470 s->cmp_from = glyph->slice.cmp.from;
22471 s->cmp_to = glyph->slice.cmp.to + 1;
22472 s->face = FACE_FROM_ID (s->f, face_id);
22473 lgstring = composition_gstring_from_id (s->cmp_id);
22474 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22475 glyph++;
22476 while (glyph < last
22477 && glyph->u.cmp.automatic
22478 && glyph->u.cmp.id == s->cmp_id
22479 && s->cmp_to == glyph->slice.cmp.from)
22480 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22481
22482 for (i = s->cmp_from; i < s->cmp_to; i++)
22483 {
22484 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22485 unsigned code = LGLYPH_CODE (lglyph);
22486
22487 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22488 }
22489 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22490 return glyph - s->row->glyphs[s->area];
22491 }
22492
22493
22494 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22495 See the comment of fill_glyph_string for arguments.
22496 Value is the index of the first glyph not in S. */
22497
22498
22499 static int
22500 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22501 int start, int end, int overlaps)
22502 {
22503 struct glyph *glyph, *last;
22504 int voffset;
22505
22506 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22507 s->for_overlaps = overlaps;
22508 glyph = s->row->glyphs[s->area] + start;
22509 last = s->row->glyphs[s->area] + end;
22510 voffset = glyph->voffset;
22511 s->face = FACE_FROM_ID (s->f, face_id);
22512 s->font = s->face->font;
22513 s->nchars = 1;
22514 s->width = glyph->pixel_width;
22515 glyph++;
22516 while (glyph < last
22517 && glyph->type == GLYPHLESS_GLYPH
22518 && glyph->voffset == voffset
22519 && glyph->face_id == face_id)
22520 {
22521 s->nchars++;
22522 s->width += glyph->pixel_width;
22523 glyph++;
22524 }
22525 s->ybase += voffset;
22526 return glyph - s->row->glyphs[s->area];
22527 }
22528
22529
22530 /* Fill glyph string S from a sequence of character glyphs.
22531
22532 FACE_ID is the face id of the string. START is the index of the
22533 first glyph to consider, END is the index of the last + 1.
22534 OVERLAPS non-zero means S should draw the foreground only, and use
22535 its physical height for clipping. See also draw_glyphs.
22536
22537 Value is the index of the first glyph not in S. */
22538
22539 static int
22540 fill_glyph_string (struct glyph_string *s, int face_id,
22541 int start, int end, int overlaps)
22542 {
22543 struct glyph *glyph, *last;
22544 int voffset;
22545 int glyph_not_available_p;
22546
22547 xassert (s->f == XFRAME (s->w->frame));
22548 xassert (s->nchars == 0);
22549 xassert (start >= 0 && end > start);
22550
22551 s->for_overlaps = overlaps;
22552 glyph = s->row->glyphs[s->area] + start;
22553 last = s->row->glyphs[s->area] + end;
22554 voffset = glyph->voffset;
22555 s->padding_p = glyph->padding_p;
22556 glyph_not_available_p = glyph->glyph_not_available_p;
22557
22558 while (glyph < last
22559 && glyph->type == CHAR_GLYPH
22560 && glyph->voffset == voffset
22561 /* Same face id implies same font, nowadays. */
22562 && glyph->face_id == face_id
22563 && glyph->glyph_not_available_p == glyph_not_available_p)
22564 {
22565 int two_byte_p;
22566
22567 s->face = get_glyph_face_and_encoding (s->f, glyph,
22568 s->char2b + s->nchars,
22569 &two_byte_p);
22570 s->two_byte_p = two_byte_p;
22571 ++s->nchars;
22572 xassert (s->nchars <= end - start);
22573 s->width += glyph->pixel_width;
22574 if (glyph++->padding_p != s->padding_p)
22575 break;
22576 }
22577
22578 s->font = s->face->font;
22579
22580 /* If the specified font could not be loaded, use the frame's font,
22581 but record the fact that we couldn't load it in
22582 S->font_not_found_p so that we can draw rectangles for the
22583 characters of the glyph string. */
22584 if (s->font == NULL || glyph_not_available_p)
22585 {
22586 s->font_not_found_p = 1;
22587 s->font = FRAME_FONT (s->f);
22588 }
22589
22590 /* Adjust base line for subscript/superscript text. */
22591 s->ybase += voffset;
22592
22593 xassert (s->face && s->face->gc);
22594 return glyph - s->row->glyphs[s->area];
22595 }
22596
22597
22598 /* Fill glyph string S from image glyph S->first_glyph. */
22599
22600 static void
22601 fill_image_glyph_string (struct glyph_string *s)
22602 {
22603 xassert (s->first_glyph->type == IMAGE_GLYPH);
22604 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22605 xassert (s->img);
22606 s->slice = s->first_glyph->slice.img;
22607 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22608 s->font = s->face->font;
22609 s->width = s->first_glyph->pixel_width;
22610
22611 /* Adjust base line for subscript/superscript text. */
22612 s->ybase += s->first_glyph->voffset;
22613 }
22614
22615
22616 /* Fill glyph string S from a sequence of stretch glyphs.
22617
22618 START is the index of the first glyph to consider,
22619 END is the index of the last + 1.
22620
22621 Value is the index of the first glyph not in S. */
22622
22623 static int
22624 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22625 {
22626 struct glyph *glyph, *last;
22627 int voffset, face_id;
22628
22629 xassert (s->first_glyph->type == STRETCH_GLYPH);
22630
22631 glyph = s->row->glyphs[s->area] + start;
22632 last = s->row->glyphs[s->area] + end;
22633 face_id = glyph->face_id;
22634 s->face = FACE_FROM_ID (s->f, face_id);
22635 s->font = s->face->font;
22636 s->width = glyph->pixel_width;
22637 s->nchars = 1;
22638 voffset = glyph->voffset;
22639
22640 for (++glyph;
22641 (glyph < last
22642 && glyph->type == STRETCH_GLYPH
22643 && glyph->voffset == voffset
22644 && glyph->face_id == face_id);
22645 ++glyph)
22646 s->width += glyph->pixel_width;
22647
22648 /* Adjust base line for subscript/superscript text. */
22649 s->ybase += voffset;
22650
22651 /* The case that face->gc == 0 is handled when drawing the glyph
22652 string by calling PREPARE_FACE_FOR_DISPLAY. */
22653 xassert (s->face);
22654 return glyph - s->row->glyphs[s->area];
22655 }
22656
22657 static struct font_metrics *
22658 get_per_char_metric (struct font *font, XChar2b *char2b)
22659 {
22660 static struct font_metrics metrics;
22661 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22662
22663 if (! font || code == FONT_INVALID_CODE)
22664 return NULL;
22665 font->driver->text_extents (font, &code, 1, &metrics);
22666 return &metrics;
22667 }
22668
22669 /* EXPORT for RIF:
22670 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22671 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22672 assumed to be zero. */
22673
22674 void
22675 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22676 {
22677 *left = *right = 0;
22678
22679 if (glyph->type == CHAR_GLYPH)
22680 {
22681 struct face *face;
22682 XChar2b char2b;
22683 struct font_metrics *pcm;
22684
22685 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22686 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22687 {
22688 if (pcm->rbearing > pcm->width)
22689 *right = pcm->rbearing - pcm->width;
22690 if (pcm->lbearing < 0)
22691 *left = -pcm->lbearing;
22692 }
22693 }
22694 else if (glyph->type == COMPOSITE_GLYPH)
22695 {
22696 if (! glyph->u.cmp.automatic)
22697 {
22698 struct composition *cmp = composition_table[glyph->u.cmp.id];
22699
22700 if (cmp->rbearing > cmp->pixel_width)
22701 *right = cmp->rbearing - cmp->pixel_width;
22702 if (cmp->lbearing < 0)
22703 *left = - cmp->lbearing;
22704 }
22705 else
22706 {
22707 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22708 struct font_metrics metrics;
22709
22710 composition_gstring_width (gstring, glyph->slice.cmp.from,
22711 glyph->slice.cmp.to + 1, &metrics);
22712 if (metrics.rbearing > metrics.width)
22713 *right = metrics.rbearing - metrics.width;
22714 if (metrics.lbearing < 0)
22715 *left = - metrics.lbearing;
22716 }
22717 }
22718 }
22719
22720
22721 /* Return the index of the first glyph preceding glyph string S that
22722 is overwritten by S because of S's left overhang. Value is -1
22723 if no glyphs are overwritten. */
22724
22725 static int
22726 left_overwritten (struct glyph_string *s)
22727 {
22728 int k;
22729
22730 if (s->left_overhang)
22731 {
22732 int x = 0, i;
22733 struct glyph *glyphs = s->row->glyphs[s->area];
22734 int first = s->first_glyph - glyphs;
22735
22736 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22737 x -= glyphs[i].pixel_width;
22738
22739 k = i + 1;
22740 }
22741 else
22742 k = -1;
22743
22744 return k;
22745 }
22746
22747
22748 /* Return the index of the first glyph preceding glyph string S that
22749 is overwriting S because of its right overhang. Value is -1 if no
22750 glyph in front of S overwrites S. */
22751
22752 static int
22753 left_overwriting (struct glyph_string *s)
22754 {
22755 int i, k, x;
22756 struct glyph *glyphs = s->row->glyphs[s->area];
22757 int first = s->first_glyph - glyphs;
22758
22759 k = -1;
22760 x = 0;
22761 for (i = first - 1; i >= 0; --i)
22762 {
22763 int left, right;
22764 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22765 if (x + right > 0)
22766 k = i;
22767 x -= glyphs[i].pixel_width;
22768 }
22769
22770 return k;
22771 }
22772
22773
22774 /* Return the index of the last glyph following glyph string S that is
22775 overwritten by S because of S's right overhang. Value is -1 if
22776 no such glyph is found. */
22777
22778 static int
22779 right_overwritten (struct glyph_string *s)
22780 {
22781 int k = -1;
22782
22783 if (s->right_overhang)
22784 {
22785 int x = 0, i;
22786 struct glyph *glyphs = s->row->glyphs[s->area];
22787 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22788 int end = s->row->used[s->area];
22789
22790 for (i = first; i < end && s->right_overhang > x; ++i)
22791 x += glyphs[i].pixel_width;
22792
22793 k = i;
22794 }
22795
22796 return k;
22797 }
22798
22799
22800 /* Return the index of the last glyph following glyph string S that
22801 overwrites S because of its left overhang. Value is negative
22802 if no such glyph is found. */
22803
22804 static int
22805 right_overwriting (struct glyph_string *s)
22806 {
22807 int i, k, x;
22808 int end = s->row->used[s->area];
22809 struct glyph *glyphs = s->row->glyphs[s->area];
22810 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22811
22812 k = -1;
22813 x = 0;
22814 for (i = first; i < end; ++i)
22815 {
22816 int left, right;
22817 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22818 if (x - left < 0)
22819 k = i;
22820 x += glyphs[i].pixel_width;
22821 }
22822
22823 return k;
22824 }
22825
22826
22827 /* Set background width of glyph string S. START is the index of the
22828 first glyph following S. LAST_X is the right-most x-position + 1
22829 in the drawing area. */
22830
22831 static inline void
22832 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22833 {
22834 /* If the face of this glyph string has to be drawn to the end of
22835 the drawing area, set S->extends_to_end_of_line_p. */
22836
22837 if (start == s->row->used[s->area]
22838 && s->area == TEXT_AREA
22839 && ((s->row->fill_line_p
22840 && (s->hl == DRAW_NORMAL_TEXT
22841 || s->hl == DRAW_IMAGE_RAISED
22842 || s->hl == DRAW_IMAGE_SUNKEN))
22843 || s->hl == DRAW_MOUSE_FACE))
22844 s->extends_to_end_of_line_p = 1;
22845
22846 /* If S extends its face to the end of the line, set its
22847 background_width to the distance to the right edge of the drawing
22848 area. */
22849 if (s->extends_to_end_of_line_p)
22850 s->background_width = last_x - s->x + 1;
22851 else
22852 s->background_width = s->width;
22853 }
22854
22855
22856 /* Compute overhangs and x-positions for glyph string S and its
22857 predecessors, or successors. X is the starting x-position for S.
22858 BACKWARD_P non-zero means process predecessors. */
22859
22860 static void
22861 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22862 {
22863 if (backward_p)
22864 {
22865 while (s)
22866 {
22867 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22868 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22869 x -= s->width;
22870 s->x = x;
22871 s = s->prev;
22872 }
22873 }
22874 else
22875 {
22876 while (s)
22877 {
22878 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22879 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22880 s->x = x;
22881 x += s->width;
22882 s = s->next;
22883 }
22884 }
22885 }
22886
22887
22888
22889 /* The following macros are only called from draw_glyphs below.
22890 They reference the following parameters of that function directly:
22891 `w', `row', `area', and `overlap_p'
22892 as well as the following local variables:
22893 `s', `f', and `hdc' (in W32) */
22894
22895 #ifdef HAVE_NTGUI
22896 /* On W32, silently add local `hdc' variable to argument list of
22897 init_glyph_string. */
22898 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22899 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22900 #else
22901 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22902 init_glyph_string (s, char2b, w, row, area, start, hl)
22903 #endif
22904
22905 /* Add a glyph string for a stretch glyph to the list of strings
22906 between HEAD and TAIL. START is the index of the stretch glyph in
22907 row area AREA of glyph row ROW. END is the index of the last glyph
22908 in that glyph row area. X is the current output position assigned
22909 to the new glyph string constructed. HL overrides that face of the
22910 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22911 is the right-most x-position of the drawing area. */
22912
22913 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22914 and below -- keep them on one line. */
22915 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22916 do \
22917 { \
22918 s = (struct glyph_string *) alloca (sizeof *s); \
22919 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22920 START = fill_stretch_glyph_string (s, START, END); \
22921 append_glyph_string (&HEAD, &TAIL, s); \
22922 s->x = (X); \
22923 } \
22924 while (0)
22925
22926
22927 /* Add a glyph string for an image glyph to the list of strings
22928 between HEAD and TAIL. START is the index of the image glyph in
22929 row area AREA of glyph row ROW. END is the index of the last glyph
22930 in that glyph row area. X is the current output position assigned
22931 to the new glyph string constructed. HL overrides that face of the
22932 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22933 is the right-most x-position of the drawing area. */
22934
22935 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22936 do \
22937 { \
22938 s = (struct glyph_string *) alloca (sizeof *s); \
22939 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22940 fill_image_glyph_string (s); \
22941 append_glyph_string (&HEAD, &TAIL, s); \
22942 ++START; \
22943 s->x = (X); \
22944 } \
22945 while (0)
22946
22947
22948 /* Add a glyph string for a sequence of character glyphs to the list
22949 of strings between HEAD and TAIL. START is the index of the first
22950 glyph in row area AREA of glyph row ROW that is part of the new
22951 glyph string. END is the index of the last glyph in that glyph row
22952 area. X is the current output position assigned to the new glyph
22953 string constructed. HL overrides that face of the glyph; e.g. it
22954 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22955 right-most x-position of the drawing area. */
22956
22957 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22958 do \
22959 { \
22960 int face_id; \
22961 XChar2b *char2b; \
22962 \
22963 face_id = (row)->glyphs[area][START].face_id; \
22964 \
22965 s = (struct glyph_string *) alloca (sizeof *s); \
22966 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22967 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22968 append_glyph_string (&HEAD, &TAIL, s); \
22969 s->x = (X); \
22970 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22971 } \
22972 while (0)
22973
22974
22975 /* Add a glyph string for a composite sequence to the list of strings
22976 between HEAD and TAIL. START is the index of the first glyph in
22977 row area AREA of glyph row ROW that is part of the new glyph
22978 string. END is the index of the last glyph in that glyph row area.
22979 X is the current output position assigned to the new glyph string
22980 constructed. HL overrides that face of the glyph; e.g. it is
22981 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22982 x-position of the drawing area. */
22983
22984 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22985 do { \
22986 int face_id = (row)->glyphs[area][START].face_id; \
22987 struct face *base_face = FACE_FROM_ID (f, face_id); \
22988 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22989 struct composition *cmp = composition_table[cmp_id]; \
22990 XChar2b *char2b; \
22991 struct glyph_string *first_s = NULL; \
22992 int n; \
22993 \
22994 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22995 \
22996 /* Make glyph_strings for each glyph sequence that is drawable by \
22997 the same face, and append them to HEAD/TAIL. */ \
22998 for (n = 0; n < cmp->glyph_len;) \
22999 { \
23000 s = (struct glyph_string *) alloca (sizeof *s); \
23001 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23002 append_glyph_string (&(HEAD), &(TAIL), s); \
23003 s->cmp = cmp; \
23004 s->cmp_from = n; \
23005 s->x = (X); \
23006 if (n == 0) \
23007 first_s = s; \
23008 n = fill_composite_glyph_string (s, base_face, overlaps); \
23009 } \
23010 \
23011 ++START; \
23012 s = first_s; \
23013 } while (0)
23014
23015
23016 /* Add a glyph string for a glyph-string sequence to the list of strings
23017 between HEAD and TAIL. */
23018
23019 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23020 do { \
23021 int face_id; \
23022 XChar2b *char2b; \
23023 Lisp_Object gstring; \
23024 \
23025 face_id = (row)->glyphs[area][START].face_id; \
23026 gstring = (composition_gstring_from_id \
23027 ((row)->glyphs[area][START].u.cmp.id)); \
23028 s = (struct glyph_string *) alloca (sizeof *s); \
23029 char2b = (XChar2b *) alloca ((sizeof *char2b) \
23030 * LGSTRING_GLYPH_LEN (gstring)); \
23031 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23032 append_glyph_string (&(HEAD), &(TAIL), s); \
23033 s->x = (X); \
23034 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23035 } while (0)
23036
23037
23038 /* Add a glyph string for a sequence of glyphless character's glyphs
23039 to the list of strings between HEAD and TAIL. The meanings of
23040 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23041
23042 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23043 do \
23044 { \
23045 int face_id; \
23046 \
23047 face_id = (row)->glyphs[area][START].face_id; \
23048 \
23049 s = (struct glyph_string *) alloca (sizeof *s); \
23050 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23051 append_glyph_string (&HEAD, &TAIL, s); \
23052 s->x = (X); \
23053 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23054 overlaps); \
23055 } \
23056 while (0)
23057
23058
23059 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23060 of AREA of glyph row ROW on window W between indices START and END.
23061 HL overrides the face for drawing glyph strings, e.g. it is
23062 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23063 x-positions of the drawing area.
23064
23065 This is an ugly monster macro construct because we must use alloca
23066 to allocate glyph strings (because draw_glyphs can be called
23067 asynchronously). */
23068
23069 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23070 do \
23071 { \
23072 HEAD = TAIL = NULL; \
23073 while (START < END) \
23074 { \
23075 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23076 switch (first_glyph->type) \
23077 { \
23078 case CHAR_GLYPH: \
23079 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23080 HL, X, LAST_X); \
23081 break; \
23082 \
23083 case COMPOSITE_GLYPH: \
23084 if (first_glyph->u.cmp.automatic) \
23085 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23086 HL, X, LAST_X); \
23087 else \
23088 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23089 HL, X, LAST_X); \
23090 break; \
23091 \
23092 case STRETCH_GLYPH: \
23093 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23094 HL, X, LAST_X); \
23095 break; \
23096 \
23097 case IMAGE_GLYPH: \
23098 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23099 HL, X, LAST_X); \
23100 break; \
23101 \
23102 case GLYPHLESS_GLYPH: \
23103 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23104 HL, X, LAST_X); \
23105 break; \
23106 \
23107 default: \
23108 abort (); \
23109 } \
23110 \
23111 if (s) \
23112 { \
23113 set_glyph_string_background_width (s, START, LAST_X); \
23114 (X) += s->width; \
23115 } \
23116 } \
23117 } while (0)
23118
23119
23120 /* Draw glyphs between START and END in AREA of ROW on window W,
23121 starting at x-position X. X is relative to AREA in W. HL is a
23122 face-override with the following meaning:
23123
23124 DRAW_NORMAL_TEXT draw normally
23125 DRAW_CURSOR draw in cursor face
23126 DRAW_MOUSE_FACE draw in mouse face.
23127 DRAW_INVERSE_VIDEO draw in mode line face
23128 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23129 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23130
23131 If OVERLAPS is non-zero, draw only the foreground of characters and
23132 clip to the physical height of ROW. Non-zero value also defines
23133 the overlapping part to be drawn:
23134
23135 OVERLAPS_PRED overlap with preceding rows
23136 OVERLAPS_SUCC overlap with succeeding rows
23137 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23138 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23139
23140 Value is the x-position reached, relative to AREA of W. */
23141
23142 static int
23143 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23144 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23145 enum draw_glyphs_face hl, int overlaps)
23146 {
23147 struct glyph_string *head, *tail;
23148 struct glyph_string *s;
23149 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23150 int i, j, x_reached, last_x, area_left = 0;
23151 struct frame *f = XFRAME (WINDOW_FRAME (w));
23152 DECLARE_HDC (hdc);
23153
23154 ALLOCATE_HDC (hdc, f);
23155
23156 /* Let's rather be paranoid than getting a SEGV. */
23157 end = min (end, row->used[area]);
23158 start = max (0, start);
23159 start = min (end, start);
23160
23161 /* Translate X to frame coordinates. Set last_x to the right
23162 end of the drawing area. */
23163 if (row->full_width_p)
23164 {
23165 /* X is relative to the left edge of W, without scroll bars
23166 or fringes. */
23167 area_left = WINDOW_LEFT_EDGE_X (w);
23168 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23169 }
23170 else
23171 {
23172 area_left = window_box_left (w, area);
23173 last_x = area_left + window_box_width (w, area);
23174 }
23175 x += area_left;
23176
23177 /* Build a doubly-linked list of glyph_string structures between
23178 head and tail from what we have to draw. Note that the macro
23179 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23180 the reason we use a separate variable `i'. */
23181 i = start;
23182 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23183 if (tail)
23184 x_reached = tail->x + tail->background_width;
23185 else
23186 x_reached = x;
23187
23188 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23189 the row, redraw some glyphs in front or following the glyph
23190 strings built above. */
23191 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23192 {
23193 struct glyph_string *h, *t;
23194 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23195 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23196 int check_mouse_face = 0;
23197 int dummy_x = 0;
23198
23199 /* If mouse highlighting is on, we may need to draw adjacent
23200 glyphs using mouse-face highlighting. */
23201 if (area == TEXT_AREA && row->mouse_face_p)
23202 {
23203 struct glyph_row *mouse_beg_row, *mouse_end_row;
23204
23205 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23206 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23207
23208 if (row >= mouse_beg_row && row <= mouse_end_row)
23209 {
23210 check_mouse_face = 1;
23211 mouse_beg_col = (row == mouse_beg_row)
23212 ? hlinfo->mouse_face_beg_col : 0;
23213 mouse_end_col = (row == mouse_end_row)
23214 ? hlinfo->mouse_face_end_col
23215 : row->used[TEXT_AREA];
23216 }
23217 }
23218
23219 /* Compute overhangs for all glyph strings. */
23220 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23221 for (s = head; s; s = s->next)
23222 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23223
23224 /* Prepend glyph strings for glyphs in front of the first glyph
23225 string that are overwritten because of the first glyph
23226 string's left overhang. The background of all strings
23227 prepended must be drawn because the first glyph string
23228 draws over it. */
23229 i = left_overwritten (head);
23230 if (i >= 0)
23231 {
23232 enum draw_glyphs_face overlap_hl;
23233
23234 /* If this row contains mouse highlighting, attempt to draw
23235 the overlapped glyphs with the correct highlight. This
23236 code fails if the overlap encompasses more than one glyph
23237 and mouse-highlight spans only some of these glyphs.
23238 However, making it work perfectly involves a lot more
23239 code, and I don't know if the pathological case occurs in
23240 practice, so we'll stick to this for now. --- cyd */
23241 if (check_mouse_face
23242 && mouse_beg_col < start && mouse_end_col > i)
23243 overlap_hl = DRAW_MOUSE_FACE;
23244 else
23245 overlap_hl = DRAW_NORMAL_TEXT;
23246
23247 j = i;
23248 BUILD_GLYPH_STRINGS (j, start, h, t,
23249 overlap_hl, dummy_x, last_x);
23250 start = i;
23251 compute_overhangs_and_x (t, head->x, 1);
23252 prepend_glyph_string_lists (&head, &tail, h, t);
23253 clip_head = head;
23254 }
23255
23256 /* Prepend glyph strings for glyphs in front of the first glyph
23257 string that overwrite that glyph string because of their
23258 right overhang. For these strings, only the foreground must
23259 be drawn, because it draws over the glyph string at `head'.
23260 The background must not be drawn because this would overwrite
23261 right overhangs of preceding glyphs for which no glyph
23262 strings exist. */
23263 i = left_overwriting (head);
23264 if (i >= 0)
23265 {
23266 enum draw_glyphs_face overlap_hl;
23267
23268 if (check_mouse_face
23269 && mouse_beg_col < start && mouse_end_col > i)
23270 overlap_hl = DRAW_MOUSE_FACE;
23271 else
23272 overlap_hl = DRAW_NORMAL_TEXT;
23273
23274 clip_head = head;
23275 BUILD_GLYPH_STRINGS (i, start, h, t,
23276 overlap_hl, dummy_x, last_x);
23277 for (s = h; s; s = s->next)
23278 s->background_filled_p = 1;
23279 compute_overhangs_and_x (t, head->x, 1);
23280 prepend_glyph_string_lists (&head, &tail, h, t);
23281 }
23282
23283 /* Append glyphs strings for glyphs following the last glyph
23284 string tail that are overwritten by tail. The background of
23285 these strings has to be drawn because tail's foreground draws
23286 over it. */
23287 i = right_overwritten (tail);
23288 if (i >= 0)
23289 {
23290 enum draw_glyphs_face overlap_hl;
23291
23292 if (check_mouse_face
23293 && mouse_beg_col < i && mouse_end_col > end)
23294 overlap_hl = DRAW_MOUSE_FACE;
23295 else
23296 overlap_hl = DRAW_NORMAL_TEXT;
23297
23298 BUILD_GLYPH_STRINGS (end, i, h, t,
23299 overlap_hl, x, last_x);
23300 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23301 we don't have `end = i;' here. */
23302 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23303 append_glyph_string_lists (&head, &tail, h, t);
23304 clip_tail = tail;
23305 }
23306
23307 /* Append glyph strings for glyphs following the last glyph
23308 string tail that overwrite tail. The foreground of such
23309 glyphs has to be drawn because it writes into the background
23310 of tail. The background must not be drawn because it could
23311 paint over the foreground of following glyphs. */
23312 i = right_overwriting (tail);
23313 if (i >= 0)
23314 {
23315 enum draw_glyphs_face overlap_hl;
23316 if (check_mouse_face
23317 && mouse_beg_col < i && mouse_end_col > end)
23318 overlap_hl = DRAW_MOUSE_FACE;
23319 else
23320 overlap_hl = DRAW_NORMAL_TEXT;
23321
23322 clip_tail = tail;
23323 i++; /* We must include the Ith glyph. */
23324 BUILD_GLYPH_STRINGS (end, i, h, t,
23325 overlap_hl, x, last_x);
23326 for (s = h; s; s = s->next)
23327 s->background_filled_p = 1;
23328 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23329 append_glyph_string_lists (&head, &tail, h, t);
23330 }
23331 if (clip_head || clip_tail)
23332 for (s = head; s; s = s->next)
23333 {
23334 s->clip_head = clip_head;
23335 s->clip_tail = clip_tail;
23336 }
23337 }
23338
23339 /* Draw all strings. */
23340 for (s = head; s; s = s->next)
23341 FRAME_RIF (f)->draw_glyph_string (s);
23342
23343 #ifndef HAVE_NS
23344 /* When focus a sole frame and move horizontally, this sets on_p to 0
23345 causing a failure to erase prev cursor position. */
23346 if (area == TEXT_AREA
23347 && !row->full_width_p
23348 /* When drawing overlapping rows, only the glyph strings'
23349 foreground is drawn, which doesn't erase a cursor
23350 completely. */
23351 && !overlaps)
23352 {
23353 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23354 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23355 : (tail ? tail->x + tail->background_width : x));
23356 x0 -= area_left;
23357 x1 -= area_left;
23358
23359 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23360 row->y, MATRIX_ROW_BOTTOM_Y (row));
23361 }
23362 #endif
23363
23364 /* Value is the x-position up to which drawn, relative to AREA of W.
23365 This doesn't include parts drawn because of overhangs. */
23366 if (row->full_width_p)
23367 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23368 else
23369 x_reached -= area_left;
23370
23371 RELEASE_HDC (hdc, f);
23372
23373 return x_reached;
23374 }
23375
23376 /* Expand row matrix if too narrow. Don't expand if area
23377 is not present. */
23378
23379 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23380 { \
23381 if (!fonts_changed_p \
23382 && (it->glyph_row->glyphs[area] \
23383 < it->glyph_row->glyphs[area + 1])) \
23384 { \
23385 it->w->ncols_scale_factor++; \
23386 fonts_changed_p = 1; \
23387 } \
23388 }
23389
23390 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23391 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23392
23393 static inline void
23394 append_glyph (struct it *it)
23395 {
23396 struct glyph *glyph;
23397 enum glyph_row_area area = it->area;
23398
23399 xassert (it->glyph_row);
23400 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23401
23402 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23403 if (glyph < it->glyph_row->glyphs[area + 1])
23404 {
23405 /* If the glyph row is reversed, we need to prepend the glyph
23406 rather than append it. */
23407 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23408 {
23409 struct glyph *g;
23410
23411 /* Make room for the additional glyph. */
23412 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23413 g[1] = *g;
23414 glyph = it->glyph_row->glyphs[area];
23415 }
23416 glyph->charpos = CHARPOS (it->position);
23417 glyph->object = it->object;
23418 if (it->pixel_width > 0)
23419 {
23420 glyph->pixel_width = it->pixel_width;
23421 glyph->padding_p = 0;
23422 }
23423 else
23424 {
23425 /* Assure at least 1-pixel width. Otherwise, cursor can't
23426 be displayed correctly. */
23427 glyph->pixel_width = 1;
23428 glyph->padding_p = 1;
23429 }
23430 glyph->ascent = it->ascent;
23431 glyph->descent = it->descent;
23432 glyph->voffset = it->voffset;
23433 glyph->type = CHAR_GLYPH;
23434 glyph->avoid_cursor_p = it->avoid_cursor_p;
23435 glyph->multibyte_p = it->multibyte_p;
23436 glyph->left_box_line_p = it->start_of_box_run_p;
23437 glyph->right_box_line_p = it->end_of_box_run_p;
23438 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23439 || it->phys_descent > it->descent);
23440 glyph->glyph_not_available_p = it->glyph_not_available_p;
23441 glyph->face_id = it->face_id;
23442 glyph->u.ch = it->char_to_display;
23443 glyph->slice.img = null_glyph_slice;
23444 glyph->font_type = FONT_TYPE_UNKNOWN;
23445 if (it->bidi_p)
23446 {
23447 glyph->resolved_level = it->bidi_it.resolved_level;
23448 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23449 abort ();
23450 glyph->bidi_type = it->bidi_it.type;
23451 }
23452 else
23453 {
23454 glyph->resolved_level = 0;
23455 glyph->bidi_type = UNKNOWN_BT;
23456 }
23457 ++it->glyph_row->used[area];
23458 }
23459 else
23460 IT_EXPAND_MATRIX_WIDTH (it, area);
23461 }
23462
23463 /* Store one glyph for the composition IT->cmp_it.id in
23464 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23465 non-null. */
23466
23467 static inline void
23468 append_composite_glyph (struct it *it)
23469 {
23470 struct glyph *glyph;
23471 enum glyph_row_area area = it->area;
23472
23473 xassert (it->glyph_row);
23474
23475 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23476 if (glyph < it->glyph_row->glyphs[area + 1])
23477 {
23478 /* If the glyph row is reversed, we need to prepend the glyph
23479 rather than append it. */
23480 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23481 {
23482 struct glyph *g;
23483
23484 /* Make room for the new glyph. */
23485 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23486 g[1] = *g;
23487 glyph = it->glyph_row->glyphs[it->area];
23488 }
23489 glyph->charpos = it->cmp_it.charpos;
23490 glyph->object = it->object;
23491 glyph->pixel_width = it->pixel_width;
23492 glyph->ascent = it->ascent;
23493 glyph->descent = it->descent;
23494 glyph->voffset = it->voffset;
23495 glyph->type = COMPOSITE_GLYPH;
23496 if (it->cmp_it.ch < 0)
23497 {
23498 glyph->u.cmp.automatic = 0;
23499 glyph->u.cmp.id = it->cmp_it.id;
23500 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23501 }
23502 else
23503 {
23504 glyph->u.cmp.automatic = 1;
23505 glyph->u.cmp.id = it->cmp_it.id;
23506 glyph->slice.cmp.from = it->cmp_it.from;
23507 glyph->slice.cmp.to = it->cmp_it.to - 1;
23508 }
23509 glyph->avoid_cursor_p = it->avoid_cursor_p;
23510 glyph->multibyte_p = it->multibyte_p;
23511 glyph->left_box_line_p = it->start_of_box_run_p;
23512 glyph->right_box_line_p = it->end_of_box_run_p;
23513 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23514 || it->phys_descent > it->descent);
23515 glyph->padding_p = 0;
23516 glyph->glyph_not_available_p = 0;
23517 glyph->face_id = it->face_id;
23518 glyph->font_type = FONT_TYPE_UNKNOWN;
23519 if (it->bidi_p)
23520 {
23521 glyph->resolved_level = it->bidi_it.resolved_level;
23522 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23523 abort ();
23524 glyph->bidi_type = it->bidi_it.type;
23525 }
23526 ++it->glyph_row->used[area];
23527 }
23528 else
23529 IT_EXPAND_MATRIX_WIDTH (it, area);
23530 }
23531
23532
23533 /* Change IT->ascent and IT->height according to the setting of
23534 IT->voffset. */
23535
23536 static inline void
23537 take_vertical_position_into_account (struct it *it)
23538 {
23539 if (it->voffset)
23540 {
23541 if (it->voffset < 0)
23542 /* Increase the ascent so that we can display the text higher
23543 in the line. */
23544 it->ascent -= it->voffset;
23545 else
23546 /* Increase the descent so that we can display the text lower
23547 in the line. */
23548 it->descent += it->voffset;
23549 }
23550 }
23551
23552
23553 /* Produce glyphs/get display metrics for the image IT is loaded with.
23554 See the description of struct display_iterator in dispextern.h for
23555 an overview of struct display_iterator. */
23556
23557 static void
23558 produce_image_glyph (struct it *it)
23559 {
23560 struct image *img;
23561 struct face *face;
23562 int glyph_ascent, crop;
23563 struct glyph_slice slice;
23564
23565 xassert (it->what == IT_IMAGE);
23566
23567 face = FACE_FROM_ID (it->f, it->face_id);
23568 xassert (face);
23569 /* Make sure X resources of the face is loaded. */
23570 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23571
23572 if (it->image_id < 0)
23573 {
23574 /* Fringe bitmap. */
23575 it->ascent = it->phys_ascent = 0;
23576 it->descent = it->phys_descent = 0;
23577 it->pixel_width = 0;
23578 it->nglyphs = 0;
23579 return;
23580 }
23581
23582 img = IMAGE_FROM_ID (it->f, it->image_id);
23583 xassert (img);
23584 /* Make sure X resources of the image is loaded. */
23585 prepare_image_for_display (it->f, img);
23586
23587 slice.x = slice.y = 0;
23588 slice.width = img->width;
23589 slice.height = img->height;
23590
23591 if (INTEGERP (it->slice.x))
23592 slice.x = XINT (it->slice.x);
23593 else if (FLOATP (it->slice.x))
23594 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23595
23596 if (INTEGERP (it->slice.y))
23597 slice.y = XINT (it->slice.y);
23598 else if (FLOATP (it->slice.y))
23599 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23600
23601 if (INTEGERP (it->slice.width))
23602 slice.width = XINT (it->slice.width);
23603 else if (FLOATP (it->slice.width))
23604 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23605
23606 if (INTEGERP (it->slice.height))
23607 slice.height = XINT (it->slice.height);
23608 else if (FLOATP (it->slice.height))
23609 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23610
23611 if (slice.x >= img->width)
23612 slice.x = img->width;
23613 if (slice.y >= img->height)
23614 slice.y = img->height;
23615 if (slice.x + slice.width >= img->width)
23616 slice.width = img->width - slice.x;
23617 if (slice.y + slice.height > img->height)
23618 slice.height = img->height - slice.y;
23619
23620 if (slice.width == 0 || slice.height == 0)
23621 return;
23622
23623 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23624
23625 it->descent = slice.height - glyph_ascent;
23626 if (slice.y == 0)
23627 it->descent += img->vmargin;
23628 if (slice.y + slice.height == img->height)
23629 it->descent += img->vmargin;
23630 it->phys_descent = it->descent;
23631
23632 it->pixel_width = slice.width;
23633 if (slice.x == 0)
23634 it->pixel_width += img->hmargin;
23635 if (slice.x + slice.width == img->width)
23636 it->pixel_width += img->hmargin;
23637
23638 /* It's quite possible for images to have an ascent greater than
23639 their height, so don't get confused in that case. */
23640 if (it->descent < 0)
23641 it->descent = 0;
23642
23643 it->nglyphs = 1;
23644
23645 if (face->box != FACE_NO_BOX)
23646 {
23647 if (face->box_line_width > 0)
23648 {
23649 if (slice.y == 0)
23650 it->ascent += face->box_line_width;
23651 if (slice.y + slice.height == img->height)
23652 it->descent += face->box_line_width;
23653 }
23654
23655 if (it->start_of_box_run_p && slice.x == 0)
23656 it->pixel_width += eabs (face->box_line_width);
23657 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23658 it->pixel_width += eabs (face->box_line_width);
23659 }
23660
23661 take_vertical_position_into_account (it);
23662
23663 /* Automatically crop wide image glyphs at right edge so we can
23664 draw the cursor on same display row. */
23665 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23666 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23667 {
23668 it->pixel_width -= crop;
23669 slice.width -= crop;
23670 }
23671
23672 if (it->glyph_row)
23673 {
23674 struct glyph *glyph;
23675 enum glyph_row_area area = it->area;
23676
23677 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23678 if (glyph < it->glyph_row->glyphs[area + 1])
23679 {
23680 glyph->charpos = CHARPOS (it->position);
23681 glyph->object = it->object;
23682 glyph->pixel_width = it->pixel_width;
23683 glyph->ascent = glyph_ascent;
23684 glyph->descent = it->descent;
23685 glyph->voffset = it->voffset;
23686 glyph->type = IMAGE_GLYPH;
23687 glyph->avoid_cursor_p = it->avoid_cursor_p;
23688 glyph->multibyte_p = it->multibyte_p;
23689 glyph->left_box_line_p = it->start_of_box_run_p;
23690 glyph->right_box_line_p = it->end_of_box_run_p;
23691 glyph->overlaps_vertically_p = 0;
23692 glyph->padding_p = 0;
23693 glyph->glyph_not_available_p = 0;
23694 glyph->face_id = it->face_id;
23695 glyph->u.img_id = img->id;
23696 glyph->slice.img = slice;
23697 glyph->font_type = FONT_TYPE_UNKNOWN;
23698 if (it->bidi_p)
23699 {
23700 glyph->resolved_level = it->bidi_it.resolved_level;
23701 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23702 abort ();
23703 glyph->bidi_type = it->bidi_it.type;
23704 }
23705 ++it->glyph_row->used[area];
23706 }
23707 else
23708 IT_EXPAND_MATRIX_WIDTH (it, area);
23709 }
23710 }
23711
23712
23713 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23714 of the glyph, WIDTH and HEIGHT are the width and height of the
23715 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23716
23717 static void
23718 append_stretch_glyph (struct it *it, Lisp_Object object,
23719 int width, int height, int ascent)
23720 {
23721 struct glyph *glyph;
23722 enum glyph_row_area area = it->area;
23723
23724 xassert (ascent >= 0 && ascent <= height);
23725
23726 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23727 if (glyph < it->glyph_row->glyphs[area + 1])
23728 {
23729 /* If the glyph row is reversed, we need to prepend the glyph
23730 rather than append it. */
23731 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23732 {
23733 struct glyph *g;
23734
23735 /* Make room for the additional glyph. */
23736 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23737 g[1] = *g;
23738 glyph = it->glyph_row->glyphs[area];
23739 }
23740 glyph->charpos = CHARPOS (it->position);
23741 glyph->object = object;
23742 glyph->pixel_width = width;
23743 glyph->ascent = ascent;
23744 glyph->descent = height - ascent;
23745 glyph->voffset = it->voffset;
23746 glyph->type = STRETCH_GLYPH;
23747 glyph->avoid_cursor_p = it->avoid_cursor_p;
23748 glyph->multibyte_p = it->multibyte_p;
23749 glyph->left_box_line_p = it->start_of_box_run_p;
23750 glyph->right_box_line_p = it->end_of_box_run_p;
23751 glyph->overlaps_vertically_p = 0;
23752 glyph->padding_p = 0;
23753 glyph->glyph_not_available_p = 0;
23754 glyph->face_id = it->face_id;
23755 glyph->u.stretch.ascent = ascent;
23756 glyph->u.stretch.height = height;
23757 glyph->slice.img = null_glyph_slice;
23758 glyph->font_type = FONT_TYPE_UNKNOWN;
23759 if (it->bidi_p)
23760 {
23761 glyph->resolved_level = it->bidi_it.resolved_level;
23762 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23763 abort ();
23764 glyph->bidi_type = it->bidi_it.type;
23765 }
23766 else
23767 {
23768 glyph->resolved_level = 0;
23769 glyph->bidi_type = UNKNOWN_BT;
23770 }
23771 ++it->glyph_row->used[area];
23772 }
23773 else
23774 IT_EXPAND_MATRIX_WIDTH (it, area);
23775 }
23776
23777 #endif /* HAVE_WINDOW_SYSTEM */
23778
23779 /* Produce a stretch glyph for iterator IT. IT->object is the value
23780 of the glyph property displayed. The value must be a list
23781 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23782 being recognized:
23783
23784 1. `:width WIDTH' specifies that the space should be WIDTH *
23785 canonical char width wide. WIDTH may be an integer or floating
23786 point number.
23787
23788 2. `:relative-width FACTOR' specifies that the width of the stretch
23789 should be computed from the width of the first character having the
23790 `glyph' property, and should be FACTOR times that width.
23791
23792 3. `:align-to HPOS' specifies that the space should be wide enough
23793 to reach HPOS, a value in canonical character units.
23794
23795 Exactly one of the above pairs must be present.
23796
23797 4. `:height HEIGHT' specifies that the height of the stretch produced
23798 should be HEIGHT, measured in canonical character units.
23799
23800 5. `:relative-height FACTOR' specifies that the height of the
23801 stretch should be FACTOR times the height of the characters having
23802 the glyph property.
23803
23804 Either none or exactly one of 4 or 5 must be present.
23805
23806 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23807 of the stretch should be used for the ascent of the stretch.
23808 ASCENT must be in the range 0 <= ASCENT <= 100. */
23809
23810 void
23811 produce_stretch_glyph (struct it *it)
23812 {
23813 /* (space :width WIDTH :height HEIGHT ...) */
23814 Lisp_Object prop, plist;
23815 int width = 0, height = 0, align_to = -1;
23816 int zero_width_ok_p = 0;
23817 int ascent = 0;
23818 double tem;
23819 struct face *face = NULL;
23820 struct font *font = NULL;
23821
23822 #ifdef HAVE_WINDOW_SYSTEM
23823 int zero_height_ok_p = 0;
23824
23825 if (FRAME_WINDOW_P (it->f))
23826 {
23827 face = FACE_FROM_ID (it->f, it->face_id);
23828 font = face->font ? face->font : FRAME_FONT (it->f);
23829 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23830 }
23831 #endif
23832
23833 /* List should start with `space'. */
23834 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23835 plist = XCDR (it->object);
23836
23837 /* Compute the width of the stretch. */
23838 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23839 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23840 {
23841 /* Absolute width `:width WIDTH' specified and valid. */
23842 zero_width_ok_p = 1;
23843 width = (int)tem;
23844 }
23845 #ifdef HAVE_WINDOW_SYSTEM
23846 else if (FRAME_WINDOW_P (it->f)
23847 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23848 {
23849 /* Relative width `:relative-width FACTOR' specified and valid.
23850 Compute the width of the characters having the `glyph'
23851 property. */
23852 struct it it2;
23853 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23854
23855 it2 = *it;
23856 if (it->multibyte_p)
23857 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23858 else
23859 {
23860 it2.c = it2.char_to_display = *p, it2.len = 1;
23861 if (! ASCII_CHAR_P (it2.c))
23862 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23863 }
23864
23865 it2.glyph_row = NULL;
23866 it2.what = IT_CHARACTER;
23867 x_produce_glyphs (&it2);
23868 width = NUMVAL (prop) * it2.pixel_width;
23869 }
23870 #endif /* HAVE_WINDOW_SYSTEM */
23871 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23872 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23873 {
23874 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23875 align_to = (align_to < 0
23876 ? 0
23877 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23878 else if (align_to < 0)
23879 align_to = window_box_left_offset (it->w, TEXT_AREA);
23880 width = max (0, (int)tem + align_to - it->current_x);
23881 zero_width_ok_p = 1;
23882 }
23883 else
23884 /* Nothing specified -> width defaults to canonical char width. */
23885 width = FRAME_COLUMN_WIDTH (it->f);
23886
23887 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23888 width = 1;
23889
23890 #ifdef HAVE_WINDOW_SYSTEM
23891 /* Compute height. */
23892 if (FRAME_WINDOW_P (it->f))
23893 {
23894 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23895 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23896 {
23897 height = (int)tem;
23898 zero_height_ok_p = 1;
23899 }
23900 else if (prop = Fplist_get (plist, QCrelative_height),
23901 NUMVAL (prop) > 0)
23902 height = FONT_HEIGHT (font) * NUMVAL (prop);
23903 else
23904 height = FONT_HEIGHT (font);
23905
23906 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23907 height = 1;
23908
23909 /* Compute percentage of height used for ascent. If
23910 `:ascent ASCENT' is present and valid, use that. Otherwise,
23911 derive the ascent from the font in use. */
23912 if (prop = Fplist_get (plist, QCascent),
23913 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23914 ascent = height * NUMVAL (prop) / 100.0;
23915 else if (!NILP (prop)
23916 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23917 ascent = min (max (0, (int)tem), height);
23918 else
23919 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23920 }
23921 else
23922 #endif /* HAVE_WINDOW_SYSTEM */
23923 height = 1;
23924
23925 if (width > 0 && it->line_wrap != TRUNCATE
23926 && it->current_x + width > it->last_visible_x)
23927 {
23928 width = it->last_visible_x - it->current_x;
23929 #ifdef HAVE_WINDOW_SYSTEM
23930 /* Subtract one more pixel from the stretch width, but only on
23931 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23932 width -= FRAME_WINDOW_P (it->f);
23933 #endif
23934 }
23935
23936 if (width > 0 && height > 0 && it->glyph_row)
23937 {
23938 Lisp_Object o_object = it->object;
23939 Lisp_Object object = it->stack[it->sp - 1].string;
23940 int n = width;
23941
23942 if (!STRINGP (object))
23943 object = it->w->buffer;
23944 #ifdef HAVE_WINDOW_SYSTEM
23945 if (FRAME_WINDOW_P (it->f))
23946 append_stretch_glyph (it, object, width, height, ascent);
23947 else
23948 #endif
23949 {
23950 it->object = object;
23951 it->char_to_display = ' ';
23952 it->pixel_width = it->len = 1;
23953 while (n--)
23954 tty_append_glyph (it);
23955 it->object = o_object;
23956 }
23957 }
23958
23959 it->pixel_width = width;
23960 #ifdef HAVE_WINDOW_SYSTEM
23961 if (FRAME_WINDOW_P (it->f))
23962 {
23963 it->ascent = it->phys_ascent = ascent;
23964 it->descent = it->phys_descent = height - it->ascent;
23965 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23966 take_vertical_position_into_account (it);
23967 }
23968 else
23969 #endif
23970 it->nglyphs = width;
23971 }
23972
23973 #ifdef HAVE_WINDOW_SYSTEM
23974
23975 /* Calculate line-height and line-spacing properties.
23976 An integer value specifies explicit pixel value.
23977 A float value specifies relative value to current face height.
23978 A cons (float . face-name) specifies relative value to
23979 height of specified face font.
23980
23981 Returns height in pixels, or nil. */
23982
23983
23984 static Lisp_Object
23985 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23986 int boff, int override)
23987 {
23988 Lisp_Object face_name = Qnil;
23989 int ascent, descent, height;
23990
23991 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23992 return val;
23993
23994 if (CONSP (val))
23995 {
23996 face_name = XCAR (val);
23997 val = XCDR (val);
23998 if (!NUMBERP (val))
23999 val = make_number (1);
24000 if (NILP (face_name))
24001 {
24002 height = it->ascent + it->descent;
24003 goto scale;
24004 }
24005 }
24006
24007 if (NILP (face_name))
24008 {
24009 font = FRAME_FONT (it->f);
24010 boff = FRAME_BASELINE_OFFSET (it->f);
24011 }
24012 else if (EQ (face_name, Qt))
24013 {
24014 override = 0;
24015 }
24016 else
24017 {
24018 int face_id;
24019 struct face *face;
24020
24021 face_id = lookup_named_face (it->f, face_name, 0);
24022 if (face_id < 0)
24023 return make_number (-1);
24024
24025 face = FACE_FROM_ID (it->f, face_id);
24026 font = face->font;
24027 if (font == NULL)
24028 return make_number (-1);
24029 boff = font->baseline_offset;
24030 if (font->vertical_centering)
24031 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24032 }
24033
24034 ascent = FONT_BASE (font) + boff;
24035 descent = FONT_DESCENT (font) - boff;
24036
24037 if (override)
24038 {
24039 it->override_ascent = ascent;
24040 it->override_descent = descent;
24041 it->override_boff = boff;
24042 }
24043
24044 height = ascent + descent;
24045
24046 scale:
24047 if (FLOATP (val))
24048 height = (int)(XFLOAT_DATA (val) * height);
24049 else if (INTEGERP (val))
24050 height *= XINT (val);
24051
24052 return make_number (height);
24053 }
24054
24055
24056 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24057 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24058 and only if this is for a character for which no font was found.
24059
24060 If the display method (it->glyphless_method) is
24061 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24062 length of the acronym or the hexadecimal string, UPPER_XOFF and
24063 UPPER_YOFF are pixel offsets for the upper part of the string,
24064 LOWER_XOFF and LOWER_YOFF are for the lower part.
24065
24066 For the other display methods, LEN through LOWER_YOFF are zero. */
24067
24068 static void
24069 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24070 short upper_xoff, short upper_yoff,
24071 short lower_xoff, short lower_yoff)
24072 {
24073 struct glyph *glyph;
24074 enum glyph_row_area area = it->area;
24075
24076 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24077 if (glyph < it->glyph_row->glyphs[area + 1])
24078 {
24079 /* If the glyph row is reversed, we need to prepend the glyph
24080 rather than append it. */
24081 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24082 {
24083 struct glyph *g;
24084
24085 /* Make room for the additional glyph. */
24086 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24087 g[1] = *g;
24088 glyph = it->glyph_row->glyphs[area];
24089 }
24090 glyph->charpos = CHARPOS (it->position);
24091 glyph->object = it->object;
24092 glyph->pixel_width = it->pixel_width;
24093 glyph->ascent = it->ascent;
24094 glyph->descent = it->descent;
24095 glyph->voffset = it->voffset;
24096 glyph->type = GLYPHLESS_GLYPH;
24097 glyph->u.glyphless.method = it->glyphless_method;
24098 glyph->u.glyphless.for_no_font = for_no_font;
24099 glyph->u.glyphless.len = len;
24100 glyph->u.glyphless.ch = it->c;
24101 glyph->slice.glyphless.upper_xoff = upper_xoff;
24102 glyph->slice.glyphless.upper_yoff = upper_yoff;
24103 glyph->slice.glyphless.lower_xoff = lower_xoff;
24104 glyph->slice.glyphless.lower_yoff = lower_yoff;
24105 glyph->avoid_cursor_p = it->avoid_cursor_p;
24106 glyph->multibyte_p = it->multibyte_p;
24107 glyph->left_box_line_p = it->start_of_box_run_p;
24108 glyph->right_box_line_p = it->end_of_box_run_p;
24109 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24110 || it->phys_descent > it->descent);
24111 glyph->padding_p = 0;
24112 glyph->glyph_not_available_p = 0;
24113 glyph->face_id = face_id;
24114 glyph->font_type = FONT_TYPE_UNKNOWN;
24115 if (it->bidi_p)
24116 {
24117 glyph->resolved_level = it->bidi_it.resolved_level;
24118 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24119 abort ();
24120 glyph->bidi_type = it->bidi_it.type;
24121 }
24122 ++it->glyph_row->used[area];
24123 }
24124 else
24125 IT_EXPAND_MATRIX_WIDTH (it, area);
24126 }
24127
24128
24129 /* Produce a glyph for a glyphless character for iterator IT.
24130 IT->glyphless_method specifies which method to use for displaying
24131 the character. See the description of enum
24132 glyphless_display_method in dispextern.h for the detail.
24133
24134 FOR_NO_FONT is nonzero if and only if this is for a character for
24135 which no font was found. ACRONYM, if non-nil, is an acronym string
24136 for the character. */
24137
24138 static void
24139 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24140 {
24141 int face_id;
24142 struct face *face;
24143 struct font *font;
24144 int base_width, base_height, width, height;
24145 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24146 int len;
24147
24148 /* Get the metrics of the base font. We always refer to the current
24149 ASCII face. */
24150 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24151 font = face->font ? face->font : FRAME_FONT (it->f);
24152 it->ascent = FONT_BASE (font) + font->baseline_offset;
24153 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24154 base_height = it->ascent + it->descent;
24155 base_width = font->average_width;
24156
24157 /* Get a face ID for the glyph by utilizing a cache (the same way as
24158 done for `escape-glyph' in get_next_display_element). */
24159 if (it->f == last_glyphless_glyph_frame
24160 && it->face_id == last_glyphless_glyph_face_id)
24161 {
24162 face_id = last_glyphless_glyph_merged_face_id;
24163 }
24164 else
24165 {
24166 /* Merge the `glyphless-char' face into the current face. */
24167 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24168 last_glyphless_glyph_frame = it->f;
24169 last_glyphless_glyph_face_id = it->face_id;
24170 last_glyphless_glyph_merged_face_id = face_id;
24171 }
24172
24173 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24174 {
24175 it->pixel_width = THIN_SPACE_WIDTH;
24176 len = 0;
24177 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24178 }
24179 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24180 {
24181 width = CHAR_WIDTH (it->c);
24182 if (width == 0)
24183 width = 1;
24184 else if (width > 4)
24185 width = 4;
24186 it->pixel_width = base_width * width;
24187 len = 0;
24188 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24189 }
24190 else
24191 {
24192 char buf[7];
24193 const char *str;
24194 unsigned int code[6];
24195 int upper_len;
24196 int ascent, descent;
24197 struct font_metrics metrics_upper, metrics_lower;
24198
24199 face = FACE_FROM_ID (it->f, face_id);
24200 font = face->font ? face->font : FRAME_FONT (it->f);
24201 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24202
24203 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24204 {
24205 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24206 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24207 if (CONSP (acronym))
24208 acronym = XCAR (acronym);
24209 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24210 }
24211 else
24212 {
24213 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24214 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24215 str = buf;
24216 }
24217 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24218 code[len] = font->driver->encode_char (font, str[len]);
24219 upper_len = (len + 1) / 2;
24220 font->driver->text_extents (font, code, upper_len,
24221 &metrics_upper);
24222 font->driver->text_extents (font, code + upper_len, len - upper_len,
24223 &metrics_lower);
24224
24225
24226
24227 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24228 width = max (metrics_upper.width, metrics_lower.width) + 4;
24229 upper_xoff = upper_yoff = 2; /* the typical case */
24230 if (base_width >= width)
24231 {
24232 /* Align the upper to the left, the lower to the right. */
24233 it->pixel_width = base_width;
24234 lower_xoff = base_width - 2 - metrics_lower.width;
24235 }
24236 else
24237 {
24238 /* Center the shorter one. */
24239 it->pixel_width = width;
24240 if (metrics_upper.width >= metrics_lower.width)
24241 lower_xoff = (width - metrics_lower.width) / 2;
24242 else
24243 {
24244 /* FIXME: This code doesn't look right. It formerly was
24245 missing the "lower_xoff = 0;", which couldn't have
24246 been right since it left lower_xoff uninitialized. */
24247 lower_xoff = 0;
24248 upper_xoff = (width - metrics_upper.width) / 2;
24249 }
24250 }
24251
24252 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24253 top, bottom, and between upper and lower strings. */
24254 height = (metrics_upper.ascent + metrics_upper.descent
24255 + metrics_lower.ascent + metrics_lower.descent) + 5;
24256 /* Center vertically.
24257 H:base_height, D:base_descent
24258 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24259
24260 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24261 descent = D - H/2 + h/2;
24262 lower_yoff = descent - 2 - ld;
24263 upper_yoff = lower_yoff - la - 1 - ud; */
24264 ascent = - (it->descent - (base_height + height + 1) / 2);
24265 descent = it->descent - (base_height - height) / 2;
24266 lower_yoff = descent - 2 - metrics_lower.descent;
24267 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24268 - metrics_upper.descent);
24269 /* Don't make the height shorter than the base height. */
24270 if (height > base_height)
24271 {
24272 it->ascent = ascent;
24273 it->descent = descent;
24274 }
24275 }
24276
24277 it->phys_ascent = it->ascent;
24278 it->phys_descent = it->descent;
24279 if (it->glyph_row)
24280 append_glyphless_glyph (it, face_id, for_no_font, len,
24281 upper_xoff, upper_yoff,
24282 lower_xoff, lower_yoff);
24283 it->nglyphs = 1;
24284 take_vertical_position_into_account (it);
24285 }
24286
24287
24288 /* RIF:
24289 Produce glyphs/get display metrics for the display element IT is
24290 loaded with. See the description of struct it in dispextern.h
24291 for an overview of struct it. */
24292
24293 void
24294 x_produce_glyphs (struct it *it)
24295 {
24296 int extra_line_spacing = it->extra_line_spacing;
24297
24298 it->glyph_not_available_p = 0;
24299
24300 if (it->what == IT_CHARACTER)
24301 {
24302 XChar2b char2b;
24303 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24304 struct font *font = face->font;
24305 struct font_metrics *pcm = NULL;
24306 int boff; /* baseline offset */
24307
24308 if (font == NULL)
24309 {
24310 /* When no suitable font is found, display this character by
24311 the method specified in the first extra slot of
24312 Vglyphless_char_display. */
24313 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24314
24315 xassert (it->what == IT_GLYPHLESS);
24316 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24317 goto done;
24318 }
24319
24320 boff = font->baseline_offset;
24321 if (font->vertical_centering)
24322 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24323
24324 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24325 {
24326 int stretched_p;
24327
24328 it->nglyphs = 1;
24329
24330 if (it->override_ascent >= 0)
24331 {
24332 it->ascent = it->override_ascent;
24333 it->descent = it->override_descent;
24334 boff = it->override_boff;
24335 }
24336 else
24337 {
24338 it->ascent = FONT_BASE (font) + boff;
24339 it->descent = FONT_DESCENT (font) - boff;
24340 }
24341
24342 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24343 {
24344 pcm = get_per_char_metric (font, &char2b);
24345 if (pcm->width == 0
24346 && pcm->rbearing == 0 && pcm->lbearing == 0)
24347 pcm = NULL;
24348 }
24349
24350 if (pcm)
24351 {
24352 it->phys_ascent = pcm->ascent + boff;
24353 it->phys_descent = pcm->descent - boff;
24354 it->pixel_width = pcm->width;
24355 }
24356 else
24357 {
24358 it->glyph_not_available_p = 1;
24359 it->phys_ascent = it->ascent;
24360 it->phys_descent = it->descent;
24361 it->pixel_width = font->space_width;
24362 }
24363
24364 if (it->constrain_row_ascent_descent_p)
24365 {
24366 if (it->descent > it->max_descent)
24367 {
24368 it->ascent += it->descent - it->max_descent;
24369 it->descent = it->max_descent;
24370 }
24371 if (it->ascent > it->max_ascent)
24372 {
24373 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24374 it->ascent = it->max_ascent;
24375 }
24376 it->phys_ascent = min (it->phys_ascent, it->ascent);
24377 it->phys_descent = min (it->phys_descent, it->descent);
24378 extra_line_spacing = 0;
24379 }
24380
24381 /* If this is a space inside a region of text with
24382 `space-width' property, change its width. */
24383 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24384 if (stretched_p)
24385 it->pixel_width *= XFLOATINT (it->space_width);
24386
24387 /* If face has a box, add the box thickness to the character
24388 height. If character has a box line to the left and/or
24389 right, add the box line width to the character's width. */
24390 if (face->box != FACE_NO_BOX)
24391 {
24392 int thick = face->box_line_width;
24393
24394 if (thick > 0)
24395 {
24396 it->ascent += thick;
24397 it->descent += thick;
24398 }
24399 else
24400 thick = -thick;
24401
24402 if (it->start_of_box_run_p)
24403 it->pixel_width += thick;
24404 if (it->end_of_box_run_p)
24405 it->pixel_width += thick;
24406 }
24407
24408 /* If face has an overline, add the height of the overline
24409 (1 pixel) and a 1 pixel margin to the character height. */
24410 if (face->overline_p)
24411 it->ascent += overline_margin;
24412
24413 if (it->constrain_row_ascent_descent_p)
24414 {
24415 if (it->ascent > it->max_ascent)
24416 it->ascent = it->max_ascent;
24417 if (it->descent > it->max_descent)
24418 it->descent = it->max_descent;
24419 }
24420
24421 take_vertical_position_into_account (it);
24422
24423 /* If we have to actually produce glyphs, do it. */
24424 if (it->glyph_row)
24425 {
24426 if (stretched_p)
24427 {
24428 /* Translate a space with a `space-width' property
24429 into a stretch glyph. */
24430 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24431 / FONT_HEIGHT (font));
24432 append_stretch_glyph (it, it->object, it->pixel_width,
24433 it->ascent + it->descent, ascent);
24434 }
24435 else
24436 append_glyph (it);
24437
24438 /* If characters with lbearing or rbearing are displayed
24439 in this line, record that fact in a flag of the
24440 glyph row. This is used to optimize X output code. */
24441 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24442 it->glyph_row->contains_overlapping_glyphs_p = 1;
24443 }
24444 if (! stretched_p && it->pixel_width == 0)
24445 /* We assure that all visible glyphs have at least 1-pixel
24446 width. */
24447 it->pixel_width = 1;
24448 }
24449 else if (it->char_to_display == '\n')
24450 {
24451 /* A newline has no width, but we need the height of the
24452 line. But if previous part of the line sets a height,
24453 don't increase that height */
24454
24455 Lisp_Object height;
24456 Lisp_Object total_height = Qnil;
24457
24458 it->override_ascent = -1;
24459 it->pixel_width = 0;
24460 it->nglyphs = 0;
24461
24462 height = get_it_property (it, Qline_height);
24463 /* Split (line-height total-height) list */
24464 if (CONSP (height)
24465 && CONSP (XCDR (height))
24466 && NILP (XCDR (XCDR (height))))
24467 {
24468 total_height = XCAR (XCDR (height));
24469 height = XCAR (height);
24470 }
24471 height = calc_line_height_property (it, height, font, boff, 1);
24472
24473 if (it->override_ascent >= 0)
24474 {
24475 it->ascent = it->override_ascent;
24476 it->descent = it->override_descent;
24477 boff = it->override_boff;
24478 }
24479 else
24480 {
24481 it->ascent = FONT_BASE (font) + boff;
24482 it->descent = FONT_DESCENT (font) - boff;
24483 }
24484
24485 if (EQ (height, Qt))
24486 {
24487 if (it->descent > it->max_descent)
24488 {
24489 it->ascent += it->descent - it->max_descent;
24490 it->descent = it->max_descent;
24491 }
24492 if (it->ascent > it->max_ascent)
24493 {
24494 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24495 it->ascent = it->max_ascent;
24496 }
24497 it->phys_ascent = min (it->phys_ascent, it->ascent);
24498 it->phys_descent = min (it->phys_descent, it->descent);
24499 it->constrain_row_ascent_descent_p = 1;
24500 extra_line_spacing = 0;
24501 }
24502 else
24503 {
24504 Lisp_Object spacing;
24505
24506 it->phys_ascent = it->ascent;
24507 it->phys_descent = it->descent;
24508
24509 if ((it->max_ascent > 0 || it->max_descent > 0)
24510 && face->box != FACE_NO_BOX
24511 && face->box_line_width > 0)
24512 {
24513 it->ascent += face->box_line_width;
24514 it->descent += face->box_line_width;
24515 }
24516 if (!NILP (height)
24517 && XINT (height) > it->ascent + it->descent)
24518 it->ascent = XINT (height) - it->descent;
24519
24520 if (!NILP (total_height))
24521 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24522 else
24523 {
24524 spacing = get_it_property (it, Qline_spacing);
24525 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24526 }
24527 if (INTEGERP (spacing))
24528 {
24529 extra_line_spacing = XINT (spacing);
24530 if (!NILP (total_height))
24531 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24532 }
24533 }
24534 }
24535 else /* i.e. (it->char_to_display == '\t') */
24536 {
24537 if (font->space_width > 0)
24538 {
24539 int tab_width = it->tab_width * font->space_width;
24540 int x = it->current_x + it->continuation_lines_width;
24541 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24542
24543 /* If the distance from the current position to the next tab
24544 stop is less than a space character width, use the
24545 tab stop after that. */
24546 if (next_tab_x - x < font->space_width)
24547 next_tab_x += tab_width;
24548
24549 it->pixel_width = next_tab_x - x;
24550 it->nglyphs = 1;
24551 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24552 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24553
24554 if (it->glyph_row)
24555 {
24556 append_stretch_glyph (it, it->object, it->pixel_width,
24557 it->ascent + it->descent, it->ascent);
24558 }
24559 }
24560 else
24561 {
24562 it->pixel_width = 0;
24563 it->nglyphs = 1;
24564 }
24565 }
24566 }
24567 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24568 {
24569 /* A static composition.
24570
24571 Note: A composition is represented as one glyph in the
24572 glyph matrix. There are no padding glyphs.
24573
24574 Important note: pixel_width, ascent, and descent are the
24575 values of what is drawn by draw_glyphs (i.e. the values of
24576 the overall glyphs composed). */
24577 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24578 int boff; /* baseline offset */
24579 struct composition *cmp = composition_table[it->cmp_it.id];
24580 int glyph_len = cmp->glyph_len;
24581 struct font *font = face->font;
24582
24583 it->nglyphs = 1;
24584
24585 /* If we have not yet calculated pixel size data of glyphs of
24586 the composition for the current face font, calculate them
24587 now. Theoretically, we have to check all fonts for the
24588 glyphs, but that requires much time and memory space. So,
24589 here we check only the font of the first glyph. This may
24590 lead to incorrect display, but it's very rare, and C-l
24591 (recenter-top-bottom) can correct the display anyway. */
24592 if (! cmp->font || cmp->font != font)
24593 {
24594 /* Ascent and descent of the font of the first character
24595 of this composition (adjusted by baseline offset).
24596 Ascent and descent of overall glyphs should not be less
24597 than these, respectively. */
24598 int font_ascent, font_descent, font_height;
24599 /* Bounding box of the overall glyphs. */
24600 int leftmost, rightmost, lowest, highest;
24601 int lbearing, rbearing;
24602 int i, width, ascent, descent;
24603 int left_padded = 0, right_padded = 0;
24604 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24605 XChar2b char2b;
24606 struct font_metrics *pcm;
24607 int font_not_found_p;
24608 ptrdiff_t pos;
24609
24610 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24611 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24612 break;
24613 if (glyph_len < cmp->glyph_len)
24614 right_padded = 1;
24615 for (i = 0; i < glyph_len; i++)
24616 {
24617 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24618 break;
24619 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24620 }
24621 if (i > 0)
24622 left_padded = 1;
24623
24624 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24625 : IT_CHARPOS (*it));
24626 /* If no suitable font is found, use the default font. */
24627 font_not_found_p = font == NULL;
24628 if (font_not_found_p)
24629 {
24630 face = face->ascii_face;
24631 font = face->font;
24632 }
24633 boff = font->baseline_offset;
24634 if (font->vertical_centering)
24635 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24636 font_ascent = FONT_BASE (font) + boff;
24637 font_descent = FONT_DESCENT (font) - boff;
24638 font_height = FONT_HEIGHT (font);
24639
24640 cmp->font = (void *) font;
24641
24642 pcm = NULL;
24643 if (! font_not_found_p)
24644 {
24645 get_char_face_and_encoding (it->f, c, it->face_id,
24646 &char2b, 0);
24647 pcm = get_per_char_metric (font, &char2b);
24648 }
24649
24650 /* Initialize the bounding box. */
24651 if (pcm)
24652 {
24653 width = cmp->glyph_len > 0 ? pcm->width : 0;
24654 ascent = pcm->ascent;
24655 descent = pcm->descent;
24656 lbearing = pcm->lbearing;
24657 rbearing = pcm->rbearing;
24658 }
24659 else
24660 {
24661 width = cmp->glyph_len > 0 ? font->space_width : 0;
24662 ascent = FONT_BASE (font);
24663 descent = FONT_DESCENT (font);
24664 lbearing = 0;
24665 rbearing = width;
24666 }
24667
24668 rightmost = width;
24669 leftmost = 0;
24670 lowest = - descent + boff;
24671 highest = ascent + boff;
24672
24673 if (! font_not_found_p
24674 && font->default_ascent
24675 && CHAR_TABLE_P (Vuse_default_ascent)
24676 && !NILP (Faref (Vuse_default_ascent,
24677 make_number (it->char_to_display))))
24678 highest = font->default_ascent + boff;
24679
24680 /* Draw the first glyph at the normal position. It may be
24681 shifted to right later if some other glyphs are drawn
24682 at the left. */
24683 cmp->offsets[i * 2] = 0;
24684 cmp->offsets[i * 2 + 1] = boff;
24685 cmp->lbearing = lbearing;
24686 cmp->rbearing = rbearing;
24687
24688 /* Set cmp->offsets for the remaining glyphs. */
24689 for (i++; i < glyph_len; i++)
24690 {
24691 int left, right, btm, top;
24692 int ch = COMPOSITION_GLYPH (cmp, i);
24693 int face_id;
24694 struct face *this_face;
24695
24696 if (ch == '\t')
24697 ch = ' ';
24698 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24699 this_face = FACE_FROM_ID (it->f, face_id);
24700 font = this_face->font;
24701
24702 if (font == NULL)
24703 pcm = NULL;
24704 else
24705 {
24706 get_char_face_and_encoding (it->f, ch, face_id,
24707 &char2b, 0);
24708 pcm = get_per_char_metric (font, &char2b);
24709 }
24710 if (! pcm)
24711 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24712 else
24713 {
24714 width = pcm->width;
24715 ascent = pcm->ascent;
24716 descent = pcm->descent;
24717 lbearing = pcm->lbearing;
24718 rbearing = pcm->rbearing;
24719 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24720 {
24721 /* Relative composition with or without
24722 alternate chars. */
24723 left = (leftmost + rightmost - width) / 2;
24724 btm = - descent + boff;
24725 if (font->relative_compose
24726 && (! CHAR_TABLE_P (Vignore_relative_composition)
24727 || NILP (Faref (Vignore_relative_composition,
24728 make_number (ch)))))
24729 {
24730
24731 if (- descent >= font->relative_compose)
24732 /* One extra pixel between two glyphs. */
24733 btm = highest + 1;
24734 else if (ascent <= 0)
24735 /* One extra pixel between two glyphs. */
24736 btm = lowest - 1 - ascent - descent;
24737 }
24738 }
24739 else
24740 {
24741 /* A composition rule is specified by an integer
24742 value that encodes global and new reference
24743 points (GREF and NREF). GREF and NREF are
24744 specified by numbers as below:
24745
24746 0---1---2 -- ascent
24747 | |
24748 | |
24749 | |
24750 9--10--11 -- center
24751 | |
24752 ---3---4---5--- baseline
24753 | |
24754 6---7---8 -- descent
24755 */
24756 int rule = COMPOSITION_RULE (cmp, i);
24757 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24758
24759 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24760 grefx = gref % 3, nrefx = nref % 3;
24761 grefy = gref / 3, nrefy = nref / 3;
24762 if (xoff)
24763 xoff = font_height * (xoff - 128) / 256;
24764 if (yoff)
24765 yoff = font_height * (yoff - 128) / 256;
24766
24767 left = (leftmost
24768 + grefx * (rightmost - leftmost) / 2
24769 - nrefx * width / 2
24770 + xoff);
24771
24772 btm = ((grefy == 0 ? highest
24773 : grefy == 1 ? 0
24774 : grefy == 2 ? lowest
24775 : (highest + lowest) / 2)
24776 - (nrefy == 0 ? ascent + descent
24777 : nrefy == 1 ? descent - boff
24778 : nrefy == 2 ? 0
24779 : (ascent + descent) / 2)
24780 + yoff);
24781 }
24782
24783 cmp->offsets[i * 2] = left;
24784 cmp->offsets[i * 2 + 1] = btm + descent;
24785
24786 /* Update the bounding box of the overall glyphs. */
24787 if (width > 0)
24788 {
24789 right = left + width;
24790 if (left < leftmost)
24791 leftmost = left;
24792 if (right > rightmost)
24793 rightmost = right;
24794 }
24795 top = btm + descent + ascent;
24796 if (top > highest)
24797 highest = top;
24798 if (btm < lowest)
24799 lowest = btm;
24800
24801 if (cmp->lbearing > left + lbearing)
24802 cmp->lbearing = left + lbearing;
24803 if (cmp->rbearing < left + rbearing)
24804 cmp->rbearing = left + rbearing;
24805 }
24806 }
24807
24808 /* If there are glyphs whose x-offsets are negative,
24809 shift all glyphs to the right and make all x-offsets
24810 non-negative. */
24811 if (leftmost < 0)
24812 {
24813 for (i = 0; i < cmp->glyph_len; i++)
24814 cmp->offsets[i * 2] -= leftmost;
24815 rightmost -= leftmost;
24816 cmp->lbearing -= leftmost;
24817 cmp->rbearing -= leftmost;
24818 }
24819
24820 if (left_padded && cmp->lbearing < 0)
24821 {
24822 for (i = 0; i < cmp->glyph_len; i++)
24823 cmp->offsets[i * 2] -= cmp->lbearing;
24824 rightmost -= cmp->lbearing;
24825 cmp->rbearing -= cmp->lbearing;
24826 cmp->lbearing = 0;
24827 }
24828 if (right_padded && rightmost < cmp->rbearing)
24829 {
24830 rightmost = cmp->rbearing;
24831 }
24832
24833 cmp->pixel_width = rightmost;
24834 cmp->ascent = highest;
24835 cmp->descent = - lowest;
24836 if (cmp->ascent < font_ascent)
24837 cmp->ascent = font_ascent;
24838 if (cmp->descent < font_descent)
24839 cmp->descent = font_descent;
24840 }
24841
24842 if (it->glyph_row
24843 && (cmp->lbearing < 0
24844 || cmp->rbearing > cmp->pixel_width))
24845 it->glyph_row->contains_overlapping_glyphs_p = 1;
24846
24847 it->pixel_width = cmp->pixel_width;
24848 it->ascent = it->phys_ascent = cmp->ascent;
24849 it->descent = it->phys_descent = cmp->descent;
24850 if (face->box != FACE_NO_BOX)
24851 {
24852 int thick = face->box_line_width;
24853
24854 if (thick > 0)
24855 {
24856 it->ascent += thick;
24857 it->descent += thick;
24858 }
24859 else
24860 thick = - thick;
24861
24862 if (it->start_of_box_run_p)
24863 it->pixel_width += thick;
24864 if (it->end_of_box_run_p)
24865 it->pixel_width += thick;
24866 }
24867
24868 /* If face has an overline, add the height of the overline
24869 (1 pixel) and a 1 pixel margin to the character height. */
24870 if (face->overline_p)
24871 it->ascent += overline_margin;
24872
24873 take_vertical_position_into_account (it);
24874 if (it->ascent < 0)
24875 it->ascent = 0;
24876 if (it->descent < 0)
24877 it->descent = 0;
24878
24879 if (it->glyph_row && cmp->glyph_len > 0)
24880 append_composite_glyph (it);
24881 }
24882 else if (it->what == IT_COMPOSITION)
24883 {
24884 /* A dynamic (automatic) composition. */
24885 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24886 Lisp_Object gstring;
24887 struct font_metrics metrics;
24888
24889 it->nglyphs = 1;
24890
24891 gstring = composition_gstring_from_id (it->cmp_it.id);
24892 it->pixel_width
24893 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24894 &metrics);
24895 if (it->glyph_row
24896 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24897 it->glyph_row->contains_overlapping_glyphs_p = 1;
24898 it->ascent = it->phys_ascent = metrics.ascent;
24899 it->descent = it->phys_descent = metrics.descent;
24900 if (face->box != FACE_NO_BOX)
24901 {
24902 int thick = face->box_line_width;
24903
24904 if (thick > 0)
24905 {
24906 it->ascent += thick;
24907 it->descent += thick;
24908 }
24909 else
24910 thick = - thick;
24911
24912 if (it->start_of_box_run_p)
24913 it->pixel_width += thick;
24914 if (it->end_of_box_run_p)
24915 it->pixel_width += thick;
24916 }
24917 /* If face has an overline, add the height of the overline
24918 (1 pixel) and a 1 pixel margin to the character height. */
24919 if (face->overline_p)
24920 it->ascent += overline_margin;
24921 take_vertical_position_into_account (it);
24922 if (it->ascent < 0)
24923 it->ascent = 0;
24924 if (it->descent < 0)
24925 it->descent = 0;
24926
24927 if (it->glyph_row)
24928 append_composite_glyph (it);
24929 }
24930 else if (it->what == IT_GLYPHLESS)
24931 produce_glyphless_glyph (it, 0, Qnil);
24932 else if (it->what == IT_IMAGE)
24933 produce_image_glyph (it);
24934 else if (it->what == IT_STRETCH)
24935 produce_stretch_glyph (it);
24936
24937 done:
24938 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24939 because this isn't true for images with `:ascent 100'. */
24940 xassert (it->ascent >= 0 && it->descent >= 0);
24941 if (it->area == TEXT_AREA)
24942 it->current_x += it->pixel_width;
24943
24944 if (extra_line_spacing > 0)
24945 {
24946 it->descent += extra_line_spacing;
24947 if (extra_line_spacing > it->max_extra_line_spacing)
24948 it->max_extra_line_spacing = extra_line_spacing;
24949 }
24950
24951 it->max_ascent = max (it->max_ascent, it->ascent);
24952 it->max_descent = max (it->max_descent, it->descent);
24953 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24954 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24955 }
24956
24957 /* EXPORT for RIF:
24958 Output LEN glyphs starting at START at the nominal cursor position.
24959 Advance the nominal cursor over the text. The global variable
24960 updated_window contains the window being updated, updated_row is
24961 the glyph row being updated, and updated_area is the area of that
24962 row being updated. */
24963
24964 void
24965 x_write_glyphs (struct glyph *start, int len)
24966 {
24967 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24968
24969 xassert (updated_window && updated_row);
24970 /* When the window is hscrolled, cursor hpos can legitimately be out
24971 of bounds, but we draw the cursor at the corresponding window
24972 margin in that case. */
24973 if (!updated_row->reversed_p && chpos < 0)
24974 chpos = 0;
24975 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24976 chpos = updated_row->used[TEXT_AREA] - 1;
24977
24978 BLOCK_INPUT;
24979
24980 /* Write glyphs. */
24981
24982 hpos = start - updated_row->glyphs[updated_area];
24983 x = draw_glyphs (updated_window, output_cursor.x,
24984 updated_row, updated_area,
24985 hpos, hpos + len,
24986 DRAW_NORMAL_TEXT, 0);
24987
24988 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24989 if (updated_area == TEXT_AREA
24990 && updated_window->phys_cursor_on_p
24991 && updated_window->phys_cursor.vpos == output_cursor.vpos
24992 && chpos >= hpos
24993 && chpos < hpos + len)
24994 updated_window->phys_cursor_on_p = 0;
24995
24996 UNBLOCK_INPUT;
24997
24998 /* Advance the output cursor. */
24999 output_cursor.hpos += len;
25000 output_cursor.x = x;
25001 }
25002
25003
25004 /* EXPORT for RIF:
25005 Insert LEN glyphs from START at the nominal cursor position. */
25006
25007 void
25008 x_insert_glyphs (struct glyph *start, int len)
25009 {
25010 struct frame *f;
25011 struct window *w;
25012 int line_height, shift_by_width, shifted_region_width;
25013 struct glyph_row *row;
25014 struct glyph *glyph;
25015 int frame_x, frame_y;
25016 ptrdiff_t hpos;
25017
25018 xassert (updated_window && updated_row);
25019 BLOCK_INPUT;
25020 w = updated_window;
25021 f = XFRAME (WINDOW_FRAME (w));
25022
25023 /* Get the height of the line we are in. */
25024 row = updated_row;
25025 line_height = row->height;
25026
25027 /* Get the width of the glyphs to insert. */
25028 shift_by_width = 0;
25029 for (glyph = start; glyph < start + len; ++glyph)
25030 shift_by_width += glyph->pixel_width;
25031
25032 /* Get the width of the region to shift right. */
25033 shifted_region_width = (window_box_width (w, updated_area)
25034 - output_cursor.x
25035 - shift_by_width);
25036
25037 /* Shift right. */
25038 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25039 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25040
25041 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25042 line_height, shift_by_width);
25043
25044 /* Write the glyphs. */
25045 hpos = start - row->glyphs[updated_area];
25046 draw_glyphs (w, output_cursor.x, row, updated_area,
25047 hpos, hpos + len,
25048 DRAW_NORMAL_TEXT, 0);
25049
25050 /* Advance the output cursor. */
25051 output_cursor.hpos += len;
25052 output_cursor.x += shift_by_width;
25053 UNBLOCK_INPUT;
25054 }
25055
25056
25057 /* EXPORT for RIF:
25058 Erase the current text line from the nominal cursor position
25059 (inclusive) to pixel column TO_X (exclusive). The idea is that
25060 everything from TO_X onward is already erased.
25061
25062 TO_X is a pixel position relative to updated_area of
25063 updated_window. TO_X == -1 means clear to the end of this area. */
25064
25065 void
25066 x_clear_end_of_line (int to_x)
25067 {
25068 struct frame *f;
25069 struct window *w = updated_window;
25070 int max_x, min_y, max_y;
25071 int from_x, from_y, to_y;
25072
25073 xassert (updated_window && updated_row);
25074 f = XFRAME (w->frame);
25075
25076 if (updated_row->full_width_p)
25077 max_x = WINDOW_TOTAL_WIDTH (w);
25078 else
25079 max_x = window_box_width (w, updated_area);
25080 max_y = window_text_bottom_y (w);
25081
25082 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25083 of window. For TO_X > 0, truncate to end of drawing area. */
25084 if (to_x == 0)
25085 return;
25086 else if (to_x < 0)
25087 to_x = max_x;
25088 else
25089 to_x = min (to_x, max_x);
25090
25091 to_y = min (max_y, output_cursor.y + updated_row->height);
25092
25093 /* Notice if the cursor will be cleared by this operation. */
25094 if (!updated_row->full_width_p)
25095 notice_overwritten_cursor (w, updated_area,
25096 output_cursor.x, -1,
25097 updated_row->y,
25098 MATRIX_ROW_BOTTOM_Y (updated_row));
25099
25100 from_x = output_cursor.x;
25101
25102 /* Translate to frame coordinates. */
25103 if (updated_row->full_width_p)
25104 {
25105 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25106 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25107 }
25108 else
25109 {
25110 int area_left = window_box_left (w, updated_area);
25111 from_x += area_left;
25112 to_x += area_left;
25113 }
25114
25115 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25116 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25117 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25118
25119 /* Prevent inadvertently clearing to end of the X window. */
25120 if (to_x > from_x && to_y > from_y)
25121 {
25122 BLOCK_INPUT;
25123 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25124 to_x - from_x, to_y - from_y);
25125 UNBLOCK_INPUT;
25126 }
25127 }
25128
25129 #endif /* HAVE_WINDOW_SYSTEM */
25130
25131
25132 \f
25133 /***********************************************************************
25134 Cursor types
25135 ***********************************************************************/
25136
25137 /* Value is the internal representation of the specified cursor type
25138 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25139 of the bar cursor. */
25140
25141 static enum text_cursor_kinds
25142 get_specified_cursor_type (Lisp_Object arg, int *width)
25143 {
25144 enum text_cursor_kinds type;
25145
25146 if (NILP (arg))
25147 return NO_CURSOR;
25148
25149 if (EQ (arg, Qbox))
25150 return FILLED_BOX_CURSOR;
25151
25152 if (EQ (arg, Qhollow))
25153 return HOLLOW_BOX_CURSOR;
25154
25155 if (EQ (arg, Qbar))
25156 {
25157 *width = 2;
25158 return BAR_CURSOR;
25159 }
25160
25161 if (CONSP (arg)
25162 && EQ (XCAR (arg), Qbar)
25163 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25164 {
25165 *width = XINT (XCDR (arg));
25166 return BAR_CURSOR;
25167 }
25168
25169 if (EQ (arg, Qhbar))
25170 {
25171 *width = 2;
25172 return HBAR_CURSOR;
25173 }
25174
25175 if (CONSP (arg)
25176 && EQ (XCAR (arg), Qhbar)
25177 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25178 {
25179 *width = XINT (XCDR (arg));
25180 return HBAR_CURSOR;
25181 }
25182
25183 /* Treat anything unknown as "hollow box cursor".
25184 It was bad to signal an error; people have trouble fixing
25185 .Xdefaults with Emacs, when it has something bad in it. */
25186 type = HOLLOW_BOX_CURSOR;
25187
25188 return type;
25189 }
25190
25191 /* Set the default cursor types for specified frame. */
25192 void
25193 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25194 {
25195 int width = 1;
25196 Lisp_Object tem;
25197
25198 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25199 FRAME_CURSOR_WIDTH (f) = width;
25200
25201 /* By default, set up the blink-off state depending on the on-state. */
25202
25203 tem = Fassoc (arg, Vblink_cursor_alist);
25204 if (!NILP (tem))
25205 {
25206 FRAME_BLINK_OFF_CURSOR (f)
25207 = get_specified_cursor_type (XCDR (tem), &width);
25208 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25209 }
25210 else
25211 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25212 }
25213
25214
25215 #ifdef HAVE_WINDOW_SYSTEM
25216
25217 /* Return the cursor we want to be displayed in window W. Return
25218 width of bar/hbar cursor through WIDTH arg. Return with
25219 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25220 (i.e. if the `system caret' should track this cursor).
25221
25222 In a mini-buffer window, we want the cursor only to appear if we
25223 are reading input from this window. For the selected window, we
25224 want the cursor type given by the frame parameter or buffer local
25225 setting of cursor-type. If explicitly marked off, draw no cursor.
25226 In all other cases, we want a hollow box cursor. */
25227
25228 static enum text_cursor_kinds
25229 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25230 int *active_cursor)
25231 {
25232 struct frame *f = XFRAME (w->frame);
25233 struct buffer *b = XBUFFER (w->buffer);
25234 int cursor_type = DEFAULT_CURSOR;
25235 Lisp_Object alt_cursor;
25236 int non_selected = 0;
25237
25238 *active_cursor = 1;
25239
25240 /* Echo area */
25241 if (cursor_in_echo_area
25242 && FRAME_HAS_MINIBUF_P (f)
25243 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25244 {
25245 if (w == XWINDOW (echo_area_window))
25246 {
25247 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25248 {
25249 *width = FRAME_CURSOR_WIDTH (f);
25250 return FRAME_DESIRED_CURSOR (f);
25251 }
25252 else
25253 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25254 }
25255
25256 *active_cursor = 0;
25257 non_selected = 1;
25258 }
25259
25260 /* Detect a nonselected window or nonselected frame. */
25261 else if (w != XWINDOW (f->selected_window)
25262 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25263 {
25264 *active_cursor = 0;
25265
25266 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25267 return NO_CURSOR;
25268
25269 non_selected = 1;
25270 }
25271
25272 /* Never display a cursor in a window in which cursor-type is nil. */
25273 if (NILP (BVAR (b, cursor_type)))
25274 return NO_CURSOR;
25275
25276 /* Get the normal cursor type for this window. */
25277 if (EQ (BVAR (b, cursor_type), Qt))
25278 {
25279 cursor_type = FRAME_DESIRED_CURSOR (f);
25280 *width = FRAME_CURSOR_WIDTH (f);
25281 }
25282 else
25283 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25284
25285 /* Use cursor-in-non-selected-windows instead
25286 for non-selected window or frame. */
25287 if (non_selected)
25288 {
25289 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25290 if (!EQ (Qt, alt_cursor))
25291 return get_specified_cursor_type (alt_cursor, width);
25292 /* t means modify the normal cursor type. */
25293 if (cursor_type == FILLED_BOX_CURSOR)
25294 cursor_type = HOLLOW_BOX_CURSOR;
25295 else if (cursor_type == BAR_CURSOR && *width > 1)
25296 --*width;
25297 return cursor_type;
25298 }
25299
25300 /* Use normal cursor if not blinked off. */
25301 if (!w->cursor_off_p)
25302 {
25303 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25304 {
25305 if (cursor_type == FILLED_BOX_CURSOR)
25306 {
25307 /* Using a block cursor on large images can be very annoying.
25308 So use a hollow cursor for "large" images.
25309 If image is not transparent (no mask), also use hollow cursor. */
25310 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25311 if (img != NULL && IMAGEP (img->spec))
25312 {
25313 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25314 where N = size of default frame font size.
25315 This should cover most of the "tiny" icons people may use. */
25316 if (!img->mask
25317 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25318 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25319 cursor_type = HOLLOW_BOX_CURSOR;
25320 }
25321 }
25322 else if (cursor_type != NO_CURSOR)
25323 {
25324 /* Display current only supports BOX and HOLLOW cursors for images.
25325 So for now, unconditionally use a HOLLOW cursor when cursor is
25326 not a solid box cursor. */
25327 cursor_type = HOLLOW_BOX_CURSOR;
25328 }
25329 }
25330 return cursor_type;
25331 }
25332
25333 /* Cursor is blinked off, so determine how to "toggle" it. */
25334
25335 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25336 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25337 return get_specified_cursor_type (XCDR (alt_cursor), width);
25338
25339 /* Then see if frame has specified a specific blink off cursor type. */
25340 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25341 {
25342 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25343 return FRAME_BLINK_OFF_CURSOR (f);
25344 }
25345
25346 #if 0
25347 /* Some people liked having a permanently visible blinking cursor,
25348 while others had very strong opinions against it. So it was
25349 decided to remove it. KFS 2003-09-03 */
25350
25351 /* Finally perform built-in cursor blinking:
25352 filled box <-> hollow box
25353 wide [h]bar <-> narrow [h]bar
25354 narrow [h]bar <-> no cursor
25355 other type <-> no cursor */
25356
25357 if (cursor_type == FILLED_BOX_CURSOR)
25358 return HOLLOW_BOX_CURSOR;
25359
25360 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25361 {
25362 *width = 1;
25363 return cursor_type;
25364 }
25365 #endif
25366
25367 return NO_CURSOR;
25368 }
25369
25370
25371 /* Notice when the text cursor of window W has been completely
25372 overwritten by a drawing operation that outputs glyphs in AREA
25373 starting at X0 and ending at X1 in the line starting at Y0 and
25374 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25375 the rest of the line after X0 has been written. Y coordinates
25376 are window-relative. */
25377
25378 static void
25379 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25380 int x0, int x1, int y0, int y1)
25381 {
25382 int cx0, cx1, cy0, cy1;
25383 struct glyph_row *row;
25384
25385 if (!w->phys_cursor_on_p)
25386 return;
25387 if (area != TEXT_AREA)
25388 return;
25389
25390 if (w->phys_cursor.vpos < 0
25391 || w->phys_cursor.vpos >= w->current_matrix->nrows
25392 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25393 !(row->enabled_p && row->displays_text_p)))
25394 return;
25395
25396 if (row->cursor_in_fringe_p)
25397 {
25398 row->cursor_in_fringe_p = 0;
25399 draw_fringe_bitmap (w, row, row->reversed_p);
25400 w->phys_cursor_on_p = 0;
25401 return;
25402 }
25403
25404 cx0 = w->phys_cursor.x;
25405 cx1 = cx0 + w->phys_cursor_width;
25406 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25407 return;
25408
25409 /* The cursor image will be completely removed from the
25410 screen if the output area intersects the cursor area in
25411 y-direction. When we draw in [y0 y1[, and some part of
25412 the cursor is at y < y0, that part must have been drawn
25413 before. When scrolling, the cursor is erased before
25414 actually scrolling, so we don't come here. When not
25415 scrolling, the rows above the old cursor row must have
25416 changed, and in this case these rows must have written
25417 over the cursor image.
25418
25419 Likewise if part of the cursor is below y1, with the
25420 exception of the cursor being in the first blank row at
25421 the buffer and window end because update_text_area
25422 doesn't draw that row. (Except when it does, but
25423 that's handled in update_text_area.) */
25424
25425 cy0 = w->phys_cursor.y;
25426 cy1 = cy0 + w->phys_cursor_height;
25427 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25428 return;
25429
25430 w->phys_cursor_on_p = 0;
25431 }
25432
25433 #endif /* HAVE_WINDOW_SYSTEM */
25434
25435 \f
25436 /************************************************************************
25437 Mouse Face
25438 ************************************************************************/
25439
25440 #ifdef HAVE_WINDOW_SYSTEM
25441
25442 /* EXPORT for RIF:
25443 Fix the display of area AREA of overlapping row ROW in window W
25444 with respect to the overlapping part OVERLAPS. */
25445
25446 void
25447 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25448 enum glyph_row_area area, int overlaps)
25449 {
25450 int i, x;
25451
25452 BLOCK_INPUT;
25453
25454 x = 0;
25455 for (i = 0; i < row->used[area];)
25456 {
25457 if (row->glyphs[area][i].overlaps_vertically_p)
25458 {
25459 int start = i, start_x = x;
25460
25461 do
25462 {
25463 x += row->glyphs[area][i].pixel_width;
25464 ++i;
25465 }
25466 while (i < row->used[area]
25467 && row->glyphs[area][i].overlaps_vertically_p);
25468
25469 draw_glyphs (w, start_x, row, area,
25470 start, i,
25471 DRAW_NORMAL_TEXT, overlaps);
25472 }
25473 else
25474 {
25475 x += row->glyphs[area][i].pixel_width;
25476 ++i;
25477 }
25478 }
25479
25480 UNBLOCK_INPUT;
25481 }
25482
25483
25484 /* EXPORT:
25485 Draw the cursor glyph of window W in glyph row ROW. See the
25486 comment of draw_glyphs for the meaning of HL. */
25487
25488 void
25489 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25490 enum draw_glyphs_face hl)
25491 {
25492 /* If cursor hpos is out of bounds, don't draw garbage. This can
25493 happen in mini-buffer windows when switching between echo area
25494 glyphs and mini-buffer. */
25495 if ((row->reversed_p
25496 ? (w->phys_cursor.hpos >= 0)
25497 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25498 {
25499 int on_p = w->phys_cursor_on_p;
25500 int x1;
25501 int hpos = w->phys_cursor.hpos;
25502
25503 /* When the window is hscrolled, cursor hpos can legitimately be
25504 out of bounds, but we draw the cursor at the corresponding
25505 window margin in that case. */
25506 if (!row->reversed_p && hpos < 0)
25507 hpos = 0;
25508 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25509 hpos = row->used[TEXT_AREA] - 1;
25510
25511 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25512 hl, 0);
25513 w->phys_cursor_on_p = on_p;
25514
25515 if (hl == DRAW_CURSOR)
25516 w->phys_cursor_width = x1 - w->phys_cursor.x;
25517 /* When we erase the cursor, and ROW is overlapped by other
25518 rows, make sure that these overlapping parts of other rows
25519 are redrawn. */
25520 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25521 {
25522 w->phys_cursor_width = x1 - w->phys_cursor.x;
25523
25524 if (row > w->current_matrix->rows
25525 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25526 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25527 OVERLAPS_ERASED_CURSOR);
25528
25529 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25530 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25531 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25532 OVERLAPS_ERASED_CURSOR);
25533 }
25534 }
25535 }
25536
25537
25538 /* EXPORT:
25539 Erase the image of a cursor of window W from the screen. */
25540
25541 void
25542 erase_phys_cursor (struct window *w)
25543 {
25544 struct frame *f = XFRAME (w->frame);
25545 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25546 int hpos = w->phys_cursor.hpos;
25547 int vpos = w->phys_cursor.vpos;
25548 int mouse_face_here_p = 0;
25549 struct glyph_matrix *active_glyphs = w->current_matrix;
25550 struct glyph_row *cursor_row;
25551 struct glyph *cursor_glyph;
25552 enum draw_glyphs_face hl;
25553
25554 /* No cursor displayed or row invalidated => nothing to do on the
25555 screen. */
25556 if (w->phys_cursor_type == NO_CURSOR)
25557 goto mark_cursor_off;
25558
25559 /* VPOS >= active_glyphs->nrows means that window has been resized.
25560 Don't bother to erase the cursor. */
25561 if (vpos >= active_glyphs->nrows)
25562 goto mark_cursor_off;
25563
25564 /* If row containing cursor is marked invalid, there is nothing we
25565 can do. */
25566 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25567 if (!cursor_row->enabled_p)
25568 goto mark_cursor_off;
25569
25570 /* If line spacing is > 0, old cursor may only be partially visible in
25571 window after split-window. So adjust visible height. */
25572 cursor_row->visible_height = min (cursor_row->visible_height,
25573 window_text_bottom_y (w) - cursor_row->y);
25574
25575 /* If row is completely invisible, don't attempt to delete a cursor which
25576 isn't there. This can happen if cursor is at top of a window, and
25577 we switch to a buffer with a header line in that window. */
25578 if (cursor_row->visible_height <= 0)
25579 goto mark_cursor_off;
25580
25581 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25582 if (cursor_row->cursor_in_fringe_p)
25583 {
25584 cursor_row->cursor_in_fringe_p = 0;
25585 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25586 goto mark_cursor_off;
25587 }
25588
25589 /* This can happen when the new row is shorter than the old one.
25590 In this case, either draw_glyphs or clear_end_of_line
25591 should have cleared the cursor. Note that we wouldn't be
25592 able to erase the cursor in this case because we don't have a
25593 cursor glyph at hand. */
25594 if ((cursor_row->reversed_p
25595 ? (w->phys_cursor.hpos < 0)
25596 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25597 goto mark_cursor_off;
25598
25599 /* When the window is hscrolled, cursor hpos can legitimately be out
25600 of bounds, but we draw the cursor at the corresponding window
25601 margin in that case. */
25602 if (!cursor_row->reversed_p && hpos < 0)
25603 hpos = 0;
25604 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25605 hpos = cursor_row->used[TEXT_AREA] - 1;
25606
25607 /* If the cursor is in the mouse face area, redisplay that when
25608 we clear the cursor. */
25609 if (! NILP (hlinfo->mouse_face_window)
25610 && coords_in_mouse_face_p (w, hpos, vpos)
25611 /* Don't redraw the cursor's spot in mouse face if it is at the
25612 end of a line (on a newline). The cursor appears there, but
25613 mouse highlighting does not. */
25614 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25615 mouse_face_here_p = 1;
25616
25617 /* Maybe clear the display under the cursor. */
25618 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25619 {
25620 int x, y, left_x;
25621 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25622 int width;
25623
25624 cursor_glyph = get_phys_cursor_glyph (w);
25625 if (cursor_glyph == NULL)
25626 goto mark_cursor_off;
25627
25628 width = cursor_glyph->pixel_width;
25629 left_x = window_box_left_offset (w, TEXT_AREA);
25630 x = w->phys_cursor.x;
25631 if (x < left_x)
25632 width -= left_x - x;
25633 width = min (width, window_box_width (w, TEXT_AREA) - x);
25634 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25635 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25636
25637 if (width > 0)
25638 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25639 }
25640
25641 /* Erase the cursor by redrawing the character underneath it. */
25642 if (mouse_face_here_p)
25643 hl = DRAW_MOUSE_FACE;
25644 else
25645 hl = DRAW_NORMAL_TEXT;
25646 draw_phys_cursor_glyph (w, cursor_row, hl);
25647
25648 mark_cursor_off:
25649 w->phys_cursor_on_p = 0;
25650 w->phys_cursor_type = NO_CURSOR;
25651 }
25652
25653
25654 /* EXPORT:
25655 Display or clear cursor of window W. If ON is zero, clear the
25656 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25657 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25658
25659 void
25660 display_and_set_cursor (struct window *w, int on,
25661 int hpos, int vpos, int x, int y)
25662 {
25663 struct frame *f = XFRAME (w->frame);
25664 int new_cursor_type;
25665 int new_cursor_width;
25666 int active_cursor;
25667 struct glyph_row *glyph_row;
25668 struct glyph *glyph;
25669
25670 /* This is pointless on invisible frames, and dangerous on garbaged
25671 windows and frames; in the latter case, the frame or window may
25672 be in the midst of changing its size, and x and y may be off the
25673 window. */
25674 if (! FRAME_VISIBLE_P (f)
25675 || FRAME_GARBAGED_P (f)
25676 || vpos >= w->current_matrix->nrows
25677 || hpos >= w->current_matrix->matrix_w)
25678 return;
25679
25680 /* If cursor is off and we want it off, return quickly. */
25681 if (!on && !w->phys_cursor_on_p)
25682 return;
25683
25684 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25685 /* If cursor row is not enabled, we don't really know where to
25686 display the cursor. */
25687 if (!glyph_row->enabled_p)
25688 {
25689 w->phys_cursor_on_p = 0;
25690 return;
25691 }
25692
25693 glyph = NULL;
25694 if (!glyph_row->exact_window_width_line_p
25695 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25696 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25697
25698 xassert (interrupt_input_blocked);
25699
25700 /* Set new_cursor_type to the cursor we want to be displayed. */
25701 new_cursor_type = get_window_cursor_type (w, glyph,
25702 &new_cursor_width, &active_cursor);
25703
25704 /* If cursor is currently being shown and we don't want it to be or
25705 it is in the wrong place, or the cursor type is not what we want,
25706 erase it. */
25707 if (w->phys_cursor_on_p
25708 && (!on
25709 || w->phys_cursor.x != x
25710 || w->phys_cursor.y != y
25711 || new_cursor_type != w->phys_cursor_type
25712 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25713 && new_cursor_width != w->phys_cursor_width)))
25714 erase_phys_cursor (w);
25715
25716 /* Don't check phys_cursor_on_p here because that flag is only set
25717 to zero in some cases where we know that the cursor has been
25718 completely erased, to avoid the extra work of erasing the cursor
25719 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25720 still not be visible, or it has only been partly erased. */
25721 if (on)
25722 {
25723 w->phys_cursor_ascent = glyph_row->ascent;
25724 w->phys_cursor_height = glyph_row->height;
25725
25726 /* Set phys_cursor_.* before x_draw_.* is called because some
25727 of them may need the information. */
25728 w->phys_cursor.x = x;
25729 w->phys_cursor.y = glyph_row->y;
25730 w->phys_cursor.hpos = hpos;
25731 w->phys_cursor.vpos = vpos;
25732 }
25733
25734 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25735 new_cursor_type, new_cursor_width,
25736 on, active_cursor);
25737 }
25738
25739
25740 /* Switch the display of W's cursor on or off, according to the value
25741 of ON. */
25742
25743 static void
25744 update_window_cursor (struct window *w, int on)
25745 {
25746 /* Don't update cursor in windows whose frame is in the process
25747 of being deleted. */
25748 if (w->current_matrix)
25749 {
25750 int hpos = w->phys_cursor.hpos;
25751 int vpos = w->phys_cursor.vpos;
25752 struct glyph_row *row;
25753
25754 if (vpos >= w->current_matrix->nrows
25755 || hpos >= w->current_matrix->matrix_w)
25756 return;
25757
25758 row = MATRIX_ROW (w->current_matrix, vpos);
25759
25760 /* When the window is hscrolled, cursor hpos can legitimately be
25761 out of bounds, but we draw the cursor at the corresponding
25762 window margin in that case. */
25763 if (!row->reversed_p && hpos < 0)
25764 hpos = 0;
25765 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25766 hpos = row->used[TEXT_AREA] - 1;
25767
25768 BLOCK_INPUT;
25769 display_and_set_cursor (w, on, hpos, vpos,
25770 w->phys_cursor.x, w->phys_cursor.y);
25771 UNBLOCK_INPUT;
25772 }
25773 }
25774
25775
25776 /* Call update_window_cursor with parameter ON_P on all leaf windows
25777 in the window tree rooted at W. */
25778
25779 static void
25780 update_cursor_in_window_tree (struct window *w, int on_p)
25781 {
25782 while (w)
25783 {
25784 if (!NILP (w->hchild))
25785 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25786 else if (!NILP (w->vchild))
25787 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25788 else
25789 update_window_cursor (w, on_p);
25790
25791 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25792 }
25793 }
25794
25795
25796 /* EXPORT:
25797 Display the cursor on window W, or clear it, according to ON_P.
25798 Don't change the cursor's position. */
25799
25800 void
25801 x_update_cursor (struct frame *f, int on_p)
25802 {
25803 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25804 }
25805
25806
25807 /* EXPORT:
25808 Clear the cursor of window W to background color, and mark the
25809 cursor as not shown. This is used when the text where the cursor
25810 is about to be rewritten. */
25811
25812 void
25813 x_clear_cursor (struct window *w)
25814 {
25815 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25816 update_window_cursor (w, 0);
25817 }
25818
25819 #endif /* HAVE_WINDOW_SYSTEM */
25820
25821 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25822 and MSDOS. */
25823 static void
25824 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25825 int start_hpos, int end_hpos,
25826 enum draw_glyphs_face draw)
25827 {
25828 #ifdef HAVE_WINDOW_SYSTEM
25829 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25830 {
25831 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25832 return;
25833 }
25834 #endif
25835 #if defined (HAVE_GPM) || defined (MSDOS)
25836 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25837 #endif
25838 }
25839
25840 /* Display the active region described by mouse_face_* according to DRAW. */
25841
25842 static void
25843 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25844 {
25845 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25846 struct frame *f = XFRAME (WINDOW_FRAME (w));
25847
25848 if (/* If window is in the process of being destroyed, don't bother
25849 to do anything. */
25850 w->current_matrix != NULL
25851 /* Don't update mouse highlight if hidden */
25852 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25853 /* Recognize when we are called to operate on rows that don't exist
25854 anymore. This can happen when a window is split. */
25855 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25856 {
25857 int phys_cursor_on_p = w->phys_cursor_on_p;
25858 struct glyph_row *row, *first, *last;
25859
25860 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25861 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25862
25863 for (row = first; row <= last && row->enabled_p; ++row)
25864 {
25865 int start_hpos, end_hpos, start_x;
25866
25867 /* For all but the first row, the highlight starts at column 0. */
25868 if (row == first)
25869 {
25870 /* R2L rows have BEG and END in reversed order, but the
25871 screen drawing geometry is always left to right. So
25872 we need to mirror the beginning and end of the
25873 highlighted area in R2L rows. */
25874 if (!row->reversed_p)
25875 {
25876 start_hpos = hlinfo->mouse_face_beg_col;
25877 start_x = hlinfo->mouse_face_beg_x;
25878 }
25879 else if (row == last)
25880 {
25881 start_hpos = hlinfo->mouse_face_end_col;
25882 start_x = hlinfo->mouse_face_end_x;
25883 }
25884 else
25885 {
25886 start_hpos = 0;
25887 start_x = 0;
25888 }
25889 }
25890 else if (row->reversed_p && row == last)
25891 {
25892 start_hpos = hlinfo->mouse_face_end_col;
25893 start_x = hlinfo->mouse_face_end_x;
25894 }
25895 else
25896 {
25897 start_hpos = 0;
25898 start_x = 0;
25899 }
25900
25901 if (row == last)
25902 {
25903 if (!row->reversed_p)
25904 end_hpos = hlinfo->mouse_face_end_col;
25905 else if (row == first)
25906 end_hpos = hlinfo->mouse_face_beg_col;
25907 else
25908 {
25909 end_hpos = row->used[TEXT_AREA];
25910 if (draw == DRAW_NORMAL_TEXT)
25911 row->fill_line_p = 1; /* Clear to end of line */
25912 }
25913 }
25914 else if (row->reversed_p && row == first)
25915 end_hpos = hlinfo->mouse_face_beg_col;
25916 else
25917 {
25918 end_hpos = row->used[TEXT_AREA];
25919 if (draw == DRAW_NORMAL_TEXT)
25920 row->fill_line_p = 1; /* Clear to end of line */
25921 }
25922
25923 if (end_hpos > start_hpos)
25924 {
25925 draw_row_with_mouse_face (w, start_x, row,
25926 start_hpos, end_hpos, draw);
25927
25928 row->mouse_face_p
25929 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25930 }
25931 }
25932
25933 #ifdef HAVE_WINDOW_SYSTEM
25934 /* When we've written over the cursor, arrange for it to
25935 be displayed again. */
25936 if (FRAME_WINDOW_P (f)
25937 && phys_cursor_on_p && !w->phys_cursor_on_p)
25938 {
25939 int hpos = w->phys_cursor.hpos;
25940
25941 /* When the window is hscrolled, cursor hpos can legitimately be
25942 out of bounds, but we draw the cursor at the corresponding
25943 window margin in that case. */
25944 if (!row->reversed_p && hpos < 0)
25945 hpos = 0;
25946 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25947 hpos = row->used[TEXT_AREA] - 1;
25948
25949 BLOCK_INPUT;
25950 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25951 w->phys_cursor.x, w->phys_cursor.y);
25952 UNBLOCK_INPUT;
25953 }
25954 #endif /* HAVE_WINDOW_SYSTEM */
25955 }
25956
25957 #ifdef HAVE_WINDOW_SYSTEM
25958 /* Change the mouse cursor. */
25959 if (FRAME_WINDOW_P (f))
25960 {
25961 if (draw == DRAW_NORMAL_TEXT
25962 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25963 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25964 else if (draw == DRAW_MOUSE_FACE)
25965 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25966 else
25967 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25968 }
25969 #endif /* HAVE_WINDOW_SYSTEM */
25970 }
25971
25972 /* EXPORT:
25973 Clear out the mouse-highlighted active region.
25974 Redraw it un-highlighted first. Value is non-zero if mouse
25975 face was actually drawn unhighlighted. */
25976
25977 int
25978 clear_mouse_face (Mouse_HLInfo *hlinfo)
25979 {
25980 int cleared = 0;
25981
25982 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25983 {
25984 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25985 cleared = 1;
25986 }
25987
25988 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25989 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25990 hlinfo->mouse_face_window = Qnil;
25991 hlinfo->mouse_face_overlay = Qnil;
25992 return cleared;
25993 }
25994
25995 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25996 within the mouse face on that window. */
25997 static int
25998 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25999 {
26000 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26001
26002 /* Quickly resolve the easy cases. */
26003 if (!(WINDOWP (hlinfo->mouse_face_window)
26004 && XWINDOW (hlinfo->mouse_face_window) == w))
26005 return 0;
26006 if (vpos < hlinfo->mouse_face_beg_row
26007 || vpos > hlinfo->mouse_face_end_row)
26008 return 0;
26009 if (vpos > hlinfo->mouse_face_beg_row
26010 && vpos < hlinfo->mouse_face_end_row)
26011 return 1;
26012
26013 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26014 {
26015 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26016 {
26017 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26018 return 1;
26019 }
26020 else if ((vpos == hlinfo->mouse_face_beg_row
26021 && hpos >= hlinfo->mouse_face_beg_col)
26022 || (vpos == hlinfo->mouse_face_end_row
26023 && hpos < hlinfo->mouse_face_end_col))
26024 return 1;
26025 }
26026 else
26027 {
26028 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26029 {
26030 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26031 return 1;
26032 }
26033 else if ((vpos == hlinfo->mouse_face_beg_row
26034 && hpos <= hlinfo->mouse_face_beg_col)
26035 || (vpos == hlinfo->mouse_face_end_row
26036 && hpos > hlinfo->mouse_face_end_col))
26037 return 1;
26038 }
26039 return 0;
26040 }
26041
26042
26043 /* EXPORT:
26044 Non-zero if physical cursor of window W is within mouse face. */
26045
26046 int
26047 cursor_in_mouse_face_p (struct window *w)
26048 {
26049 int hpos = w->phys_cursor.hpos;
26050 int vpos = w->phys_cursor.vpos;
26051 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26052
26053 /* When the window is hscrolled, cursor hpos can legitimately be out
26054 of bounds, but we draw the cursor at the corresponding window
26055 margin in that case. */
26056 if (!row->reversed_p && hpos < 0)
26057 hpos = 0;
26058 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26059 hpos = row->used[TEXT_AREA] - 1;
26060
26061 return coords_in_mouse_face_p (w, hpos, vpos);
26062 }
26063
26064
26065 \f
26066 /* Find the glyph rows START_ROW and END_ROW of window W that display
26067 characters between buffer positions START_CHARPOS and END_CHARPOS
26068 (excluding END_CHARPOS). DISP_STRING is a display string that
26069 covers these buffer positions. This is similar to
26070 row_containing_pos, but is more accurate when bidi reordering makes
26071 buffer positions change non-linearly with glyph rows. */
26072 static void
26073 rows_from_pos_range (struct window *w,
26074 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26075 Lisp_Object disp_string,
26076 struct glyph_row **start, struct glyph_row **end)
26077 {
26078 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26079 int last_y = window_text_bottom_y (w);
26080 struct glyph_row *row;
26081
26082 *start = NULL;
26083 *end = NULL;
26084
26085 while (!first->enabled_p
26086 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26087 first++;
26088
26089 /* Find the START row. */
26090 for (row = first;
26091 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26092 row++)
26093 {
26094 /* A row can potentially be the START row if the range of the
26095 characters it displays intersects the range
26096 [START_CHARPOS..END_CHARPOS). */
26097 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26098 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26099 /* See the commentary in row_containing_pos, for the
26100 explanation of the complicated way to check whether
26101 some position is beyond the end of the characters
26102 displayed by a row. */
26103 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26104 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26105 && !row->ends_at_zv_p
26106 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26107 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26108 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26109 && !row->ends_at_zv_p
26110 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26111 {
26112 /* Found a candidate row. Now make sure at least one of the
26113 glyphs it displays has a charpos from the range
26114 [START_CHARPOS..END_CHARPOS).
26115
26116 This is not obvious because bidi reordering could make
26117 buffer positions of a row be 1,2,3,102,101,100, and if we
26118 want to highlight characters in [50..60), we don't want
26119 this row, even though [50..60) does intersect [1..103),
26120 the range of character positions given by the row's start
26121 and end positions. */
26122 struct glyph *g = row->glyphs[TEXT_AREA];
26123 struct glyph *e = g + row->used[TEXT_AREA];
26124
26125 while (g < e)
26126 {
26127 if (((BUFFERP (g->object) || INTEGERP (g->object))
26128 && start_charpos <= g->charpos && g->charpos < end_charpos)
26129 /* A glyph that comes from DISP_STRING is by
26130 definition to be highlighted. */
26131 || EQ (g->object, disp_string))
26132 *start = row;
26133 g++;
26134 }
26135 if (*start)
26136 break;
26137 }
26138 }
26139
26140 /* Find the END row. */
26141 if (!*start
26142 /* If the last row is partially visible, start looking for END
26143 from that row, instead of starting from FIRST. */
26144 && !(row->enabled_p
26145 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26146 row = first;
26147 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26148 {
26149 struct glyph_row *next = row + 1;
26150 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26151
26152 if (!next->enabled_p
26153 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26154 /* The first row >= START whose range of displayed characters
26155 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26156 is the row END + 1. */
26157 || (start_charpos < next_start
26158 && end_charpos < next_start)
26159 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26160 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26161 && !next->ends_at_zv_p
26162 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26163 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26164 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26165 && !next->ends_at_zv_p
26166 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26167 {
26168 *end = row;
26169 break;
26170 }
26171 else
26172 {
26173 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26174 but none of the characters it displays are in the range, it is
26175 also END + 1. */
26176 struct glyph *g = next->glyphs[TEXT_AREA];
26177 struct glyph *s = g;
26178 struct glyph *e = g + next->used[TEXT_AREA];
26179
26180 while (g < e)
26181 {
26182 if (((BUFFERP (g->object) || INTEGERP (g->object))
26183 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26184 /* If the buffer position of the first glyph in
26185 the row is equal to END_CHARPOS, it means
26186 the last character to be highlighted is the
26187 newline of ROW, and we must consider NEXT as
26188 END, not END+1. */
26189 || (((!next->reversed_p && g == s)
26190 || (next->reversed_p && g == e - 1))
26191 && (g->charpos == end_charpos
26192 /* Special case for when NEXT is an
26193 empty line at ZV. */
26194 || (g->charpos == -1
26195 && !row->ends_at_zv_p
26196 && next_start == end_charpos)))))
26197 /* A glyph that comes from DISP_STRING is by
26198 definition to be highlighted. */
26199 || EQ (g->object, disp_string))
26200 break;
26201 g++;
26202 }
26203 if (g == e)
26204 {
26205 *end = row;
26206 break;
26207 }
26208 /* The first row that ends at ZV must be the last to be
26209 highlighted. */
26210 else if (next->ends_at_zv_p)
26211 {
26212 *end = next;
26213 break;
26214 }
26215 }
26216 }
26217 }
26218
26219 /* This function sets the mouse_face_* elements of HLINFO, assuming
26220 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26221 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26222 for the overlay or run of text properties specifying the mouse
26223 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26224 before-string and after-string that must also be highlighted.
26225 DISP_STRING, if non-nil, is a display string that may cover some
26226 or all of the highlighted text. */
26227
26228 static void
26229 mouse_face_from_buffer_pos (Lisp_Object window,
26230 Mouse_HLInfo *hlinfo,
26231 ptrdiff_t mouse_charpos,
26232 ptrdiff_t start_charpos,
26233 ptrdiff_t end_charpos,
26234 Lisp_Object before_string,
26235 Lisp_Object after_string,
26236 Lisp_Object disp_string)
26237 {
26238 struct window *w = XWINDOW (window);
26239 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26240 struct glyph_row *r1, *r2;
26241 struct glyph *glyph, *end;
26242 ptrdiff_t ignore, pos;
26243 int x;
26244
26245 xassert (NILP (disp_string) || STRINGP (disp_string));
26246 xassert (NILP (before_string) || STRINGP (before_string));
26247 xassert (NILP (after_string) || STRINGP (after_string));
26248
26249 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26250 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26251 if (r1 == NULL)
26252 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26253 /* If the before-string or display-string contains newlines,
26254 rows_from_pos_range skips to its last row. Move back. */
26255 if (!NILP (before_string) || !NILP (disp_string))
26256 {
26257 struct glyph_row *prev;
26258 while ((prev = r1 - 1, prev >= first)
26259 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26260 && prev->used[TEXT_AREA] > 0)
26261 {
26262 struct glyph *beg = prev->glyphs[TEXT_AREA];
26263 glyph = beg + prev->used[TEXT_AREA];
26264 while (--glyph >= beg && INTEGERP (glyph->object));
26265 if (glyph < beg
26266 || !(EQ (glyph->object, before_string)
26267 || EQ (glyph->object, disp_string)))
26268 break;
26269 r1 = prev;
26270 }
26271 }
26272 if (r2 == NULL)
26273 {
26274 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26275 hlinfo->mouse_face_past_end = 1;
26276 }
26277 else if (!NILP (after_string))
26278 {
26279 /* If the after-string has newlines, advance to its last row. */
26280 struct glyph_row *next;
26281 struct glyph_row *last
26282 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26283
26284 for (next = r2 + 1;
26285 next <= last
26286 && next->used[TEXT_AREA] > 0
26287 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26288 ++next)
26289 r2 = next;
26290 }
26291 /* The rest of the display engine assumes that mouse_face_beg_row is
26292 either above mouse_face_end_row or identical to it. But with
26293 bidi-reordered continued lines, the row for START_CHARPOS could
26294 be below the row for END_CHARPOS. If so, swap the rows and store
26295 them in correct order. */
26296 if (r1->y > r2->y)
26297 {
26298 struct glyph_row *tem = r2;
26299
26300 r2 = r1;
26301 r1 = tem;
26302 }
26303
26304 hlinfo->mouse_face_beg_y = r1->y;
26305 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26306 hlinfo->mouse_face_end_y = r2->y;
26307 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26308
26309 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26310 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26311 could be anywhere in the row and in any order. The strategy
26312 below is to find the leftmost and the rightmost glyph that
26313 belongs to either of these 3 strings, or whose position is
26314 between START_CHARPOS and END_CHARPOS, and highlight all the
26315 glyphs between those two. This may cover more than just the text
26316 between START_CHARPOS and END_CHARPOS if the range of characters
26317 strides the bidi level boundary, e.g. if the beginning is in R2L
26318 text while the end is in L2R text or vice versa. */
26319 if (!r1->reversed_p)
26320 {
26321 /* This row is in a left to right paragraph. Scan it left to
26322 right. */
26323 glyph = r1->glyphs[TEXT_AREA];
26324 end = glyph + r1->used[TEXT_AREA];
26325 x = r1->x;
26326
26327 /* Skip truncation glyphs at the start of the glyph row. */
26328 if (r1->displays_text_p)
26329 for (; glyph < end
26330 && INTEGERP (glyph->object)
26331 && glyph->charpos < 0;
26332 ++glyph)
26333 x += glyph->pixel_width;
26334
26335 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26336 or DISP_STRING, and the first glyph from buffer whose
26337 position is between START_CHARPOS and END_CHARPOS. */
26338 for (; glyph < end
26339 && !INTEGERP (glyph->object)
26340 && !EQ (glyph->object, disp_string)
26341 && !(BUFFERP (glyph->object)
26342 && (glyph->charpos >= start_charpos
26343 && glyph->charpos < end_charpos));
26344 ++glyph)
26345 {
26346 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26347 are present at buffer positions between START_CHARPOS and
26348 END_CHARPOS, or if they come from an overlay. */
26349 if (EQ (glyph->object, before_string))
26350 {
26351 pos = string_buffer_position (before_string,
26352 start_charpos);
26353 /* If pos == 0, it means before_string came from an
26354 overlay, not from a buffer position. */
26355 if (!pos || (pos >= start_charpos && pos < end_charpos))
26356 break;
26357 }
26358 else if (EQ (glyph->object, after_string))
26359 {
26360 pos = string_buffer_position (after_string, end_charpos);
26361 if (!pos || (pos >= start_charpos && pos < end_charpos))
26362 break;
26363 }
26364 x += glyph->pixel_width;
26365 }
26366 hlinfo->mouse_face_beg_x = x;
26367 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26368 }
26369 else
26370 {
26371 /* This row is in a right to left paragraph. Scan it right to
26372 left. */
26373 struct glyph *g;
26374
26375 end = r1->glyphs[TEXT_AREA] - 1;
26376 glyph = end + r1->used[TEXT_AREA];
26377
26378 /* Skip truncation glyphs at the start of the glyph row. */
26379 if (r1->displays_text_p)
26380 for (; glyph > end
26381 && INTEGERP (glyph->object)
26382 && glyph->charpos < 0;
26383 --glyph)
26384 ;
26385
26386 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26387 or DISP_STRING, and the first glyph from buffer whose
26388 position is between START_CHARPOS and END_CHARPOS. */
26389 for (; glyph > end
26390 && !INTEGERP (glyph->object)
26391 && !EQ (glyph->object, disp_string)
26392 && !(BUFFERP (glyph->object)
26393 && (glyph->charpos >= start_charpos
26394 && glyph->charpos < end_charpos));
26395 --glyph)
26396 {
26397 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26398 are present at buffer positions between START_CHARPOS and
26399 END_CHARPOS, or if they come from an overlay. */
26400 if (EQ (glyph->object, before_string))
26401 {
26402 pos = string_buffer_position (before_string, start_charpos);
26403 /* If pos == 0, it means before_string came from an
26404 overlay, not from a buffer position. */
26405 if (!pos || (pos >= start_charpos && pos < end_charpos))
26406 break;
26407 }
26408 else if (EQ (glyph->object, after_string))
26409 {
26410 pos = string_buffer_position (after_string, end_charpos);
26411 if (!pos || (pos >= start_charpos && pos < end_charpos))
26412 break;
26413 }
26414 }
26415
26416 glyph++; /* first glyph to the right of the highlighted area */
26417 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26418 x += g->pixel_width;
26419 hlinfo->mouse_face_beg_x = x;
26420 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26421 }
26422
26423 /* If the highlight ends in a different row, compute GLYPH and END
26424 for the end row. Otherwise, reuse the values computed above for
26425 the row where the highlight begins. */
26426 if (r2 != r1)
26427 {
26428 if (!r2->reversed_p)
26429 {
26430 glyph = r2->glyphs[TEXT_AREA];
26431 end = glyph + r2->used[TEXT_AREA];
26432 x = r2->x;
26433 }
26434 else
26435 {
26436 end = r2->glyphs[TEXT_AREA] - 1;
26437 glyph = end + r2->used[TEXT_AREA];
26438 }
26439 }
26440
26441 if (!r2->reversed_p)
26442 {
26443 /* Skip truncation and continuation glyphs near the end of the
26444 row, and also blanks and stretch glyphs inserted by
26445 extend_face_to_end_of_line. */
26446 while (end > glyph
26447 && INTEGERP ((end - 1)->object))
26448 --end;
26449 /* Scan the rest of the glyph row from the end, looking for the
26450 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26451 DISP_STRING, or whose position is between START_CHARPOS
26452 and END_CHARPOS */
26453 for (--end;
26454 end > glyph
26455 && !INTEGERP (end->object)
26456 && !EQ (end->object, disp_string)
26457 && !(BUFFERP (end->object)
26458 && (end->charpos >= start_charpos
26459 && end->charpos < end_charpos));
26460 --end)
26461 {
26462 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26463 are present at buffer positions between START_CHARPOS and
26464 END_CHARPOS, or if they come from an overlay. */
26465 if (EQ (end->object, before_string))
26466 {
26467 pos = string_buffer_position (before_string, start_charpos);
26468 if (!pos || (pos >= start_charpos && pos < end_charpos))
26469 break;
26470 }
26471 else if (EQ (end->object, after_string))
26472 {
26473 pos = string_buffer_position (after_string, end_charpos);
26474 if (!pos || (pos >= start_charpos && pos < end_charpos))
26475 break;
26476 }
26477 }
26478 /* Find the X coordinate of the last glyph to be highlighted. */
26479 for (; glyph <= end; ++glyph)
26480 x += glyph->pixel_width;
26481
26482 hlinfo->mouse_face_end_x = x;
26483 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26484 }
26485 else
26486 {
26487 /* Skip truncation and continuation glyphs near the end of the
26488 row, and also blanks and stretch glyphs inserted by
26489 extend_face_to_end_of_line. */
26490 x = r2->x;
26491 end++;
26492 while (end < glyph
26493 && INTEGERP (end->object))
26494 {
26495 x += end->pixel_width;
26496 ++end;
26497 }
26498 /* Scan the rest of the glyph row from the end, looking for the
26499 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26500 DISP_STRING, or whose position is between START_CHARPOS
26501 and END_CHARPOS */
26502 for ( ;
26503 end < glyph
26504 && !INTEGERP (end->object)
26505 && !EQ (end->object, disp_string)
26506 && !(BUFFERP (end->object)
26507 && (end->charpos >= start_charpos
26508 && end->charpos < end_charpos));
26509 ++end)
26510 {
26511 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26512 are present at buffer positions between START_CHARPOS and
26513 END_CHARPOS, or if they come from an overlay. */
26514 if (EQ (end->object, before_string))
26515 {
26516 pos = string_buffer_position (before_string, start_charpos);
26517 if (!pos || (pos >= start_charpos && pos < end_charpos))
26518 break;
26519 }
26520 else if (EQ (end->object, after_string))
26521 {
26522 pos = string_buffer_position (after_string, end_charpos);
26523 if (!pos || (pos >= start_charpos && pos < end_charpos))
26524 break;
26525 }
26526 x += end->pixel_width;
26527 }
26528 /* If we exited the above loop because we arrived at the last
26529 glyph of the row, and its buffer position is still not in
26530 range, it means the last character in range is the preceding
26531 newline. Bump the end column and x values to get past the
26532 last glyph. */
26533 if (end == glyph
26534 && BUFFERP (end->object)
26535 && (end->charpos < start_charpos
26536 || end->charpos >= end_charpos))
26537 {
26538 x += end->pixel_width;
26539 ++end;
26540 }
26541 hlinfo->mouse_face_end_x = x;
26542 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26543 }
26544
26545 hlinfo->mouse_face_window = window;
26546 hlinfo->mouse_face_face_id
26547 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26548 mouse_charpos + 1,
26549 !hlinfo->mouse_face_hidden, -1);
26550 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26551 }
26552
26553 /* The following function is not used anymore (replaced with
26554 mouse_face_from_string_pos), but I leave it here for the time
26555 being, in case someone would. */
26556
26557 #if 0 /* not used */
26558
26559 /* Find the position of the glyph for position POS in OBJECT in
26560 window W's current matrix, and return in *X, *Y the pixel
26561 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26562
26563 RIGHT_P non-zero means return the position of the right edge of the
26564 glyph, RIGHT_P zero means return the left edge position.
26565
26566 If no glyph for POS exists in the matrix, return the position of
26567 the glyph with the next smaller position that is in the matrix, if
26568 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26569 exists in the matrix, return the position of the glyph with the
26570 next larger position in OBJECT.
26571
26572 Value is non-zero if a glyph was found. */
26573
26574 static int
26575 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26576 int *hpos, int *vpos, int *x, int *y, int right_p)
26577 {
26578 int yb = window_text_bottom_y (w);
26579 struct glyph_row *r;
26580 struct glyph *best_glyph = NULL;
26581 struct glyph_row *best_row = NULL;
26582 int best_x = 0;
26583
26584 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26585 r->enabled_p && r->y < yb;
26586 ++r)
26587 {
26588 struct glyph *g = r->glyphs[TEXT_AREA];
26589 struct glyph *e = g + r->used[TEXT_AREA];
26590 int gx;
26591
26592 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26593 if (EQ (g->object, object))
26594 {
26595 if (g->charpos == pos)
26596 {
26597 best_glyph = g;
26598 best_x = gx;
26599 best_row = r;
26600 goto found;
26601 }
26602 else if (best_glyph == NULL
26603 || ((eabs (g->charpos - pos)
26604 < eabs (best_glyph->charpos - pos))
26605 && (right_p
26606 ? g->charpos < pos
26607 : g->charpos > pos)))
26608 {
26609 best_glyph = g;
26610 best_x = gx;
26611 best_row = r;
26612 }
26613 }
26614 }
26615
26616 found:
26617
26618 if (best_glyph)
26619 {
26620 *x = best_x;
26621 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26622
26623 if (right_p)
26624 {
26625 *x += best_glyph->pixel_width;
26626 ++*hpos;
26627 }
26628
26629 *y = best_row->y;
26630 *vpos = best_row - w->current_matrix->rows;
26631 }
26632
26633 return best_glyph != NULL;
26634 }
26635 #endif /* not used */
26636
26637 /* Find the positions of the first and the last glyphs in window W's
26638 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26639 (assumed to be a string), and return in HLINFO's mouse_face_*
26640 members the pixel and column/row coordinates of those glyphs. */
26641
26642 static void
26643 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26644 Lisp_Object object,
26645 ptrdiff_t startpos, ptrdiff_t endpos)
26646 {
26647 int yb = window_text_bottom_y (w);
26648 struct glyph_row *r;
26649 struct glyph *g, *e;
26650 int gx;
26651 int found = 0;
26652
26653 /* Find the glyph row with at least one position in the range
26654 [STARTPOS..ENDPOS], and the first glyph in that row whose
26655 position belongs to that range. */
26656 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26657 r->enabled_p && r->y < yb;
26658 ++r)
26659 {
26660 if (!r->reversed_p)
26661 {
26662 g = r->glyphs[TEXT_AREA];
26663 e = g + r->used[TEXT_AREA];
26664 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26665 if (EQ (g->object, object)
26666 && startpos <= g->charpos && g->charpos <= endpos)
26667 {
26668 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26669 hlinfo->mouse_face_beg_y = r->y;
26670 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26671 hlinfo->mouse_face_beg_x = gx;
26672 found = 1;
26673 break;
26674 }
26675 }
26676 else
26677 {
26678 struct glyph *g1;
26679
26680 e = r->glyphs[TEXT_AREA];
26681 g = e + r->used[TEXT_AREA];
26682 for ( ; g > e; --g)
26683 if (EQ ((g-1)->object, object)
26684 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26685 {
26686 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26687 hlinfo->mouse_face_beg_y = r->y;
26688 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26689 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26690 gx += g1->pixel_width;
26691 hlinfo->mouse_face_beg_x = gx;
26692 found = 1;
26693 break;
26694 }
26695 }
26696 if (found)
26697 break;
26698 }
26699
26700 if (!found)
26701 return;
26702
26703 /* Starting with the next row, look for the first row which does NOT
26704 include any glyphs whose positions are in the range. */
26705 for (++r; r->enabled_p && r->y < yb; ++r)
26706 {
26707 g = r->glyphs[TEXT_AREA];
26708 e = g + r->used[TEXT_AREA];
26709 found = 0;
26710 for ( ; g < e; ++g)
26711 if (EQ (g->object, object)
26712 && startpos <= g->charpos && g->charpos <= endpos)
26713 {
26714 found = 1;
26715 break;
26716 }
26717 if (!found)
26718 break;
26719 }
26720
26721 /* The highlighted region ends on the previous row. */
26722 r--;
26723
26724 /* Set the end row and its vertical pixel coordinate. */
26725 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26726 hlinfo->mouse_face_end_y = r->y;
26727
26728 /* Compute and set the end column and the end column's horizontal
26729 pixel coordinate. */
26730 if (!r->reversed_p)
26731 {
26732 g = r->glyphs[TEXT_AREA];
26733 e = g + r->used[TEXT_AREA];
26734 for ( ; e > g; --e)
26735 if (EQ ((e-1)->object, object)
26736 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26737 break;
26738 hlinfo->mouse_face_end_col = e - g;
26739
26740 for (gx = r->x; g < e; ++g)
26741 gx += g->pixel_width;
26742 hlinfo->mouse_face_end_x = gx;
26743 }
26744 else
26745 {
26746 e = r->glyphs[TEXT_AREA];
26747 g = e + r->used[TEXT_AREA];
26748 for (gx = r->x ; e < g; ++e)
26749 {
26750 if (EQ (e->object, object)
26751 && startpos <= e->charpos && e->charpos <= endpos)
26752 break;
26753 gx += e->pixel_width;
26754 }
26755 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26756 hlinfo->mouse_face_end_x = gx;
26757 }
26758 }
26759
26760 #ifdef HAVE_WINDOW_SYSTEM
26761
26762 /* See if position X, Y is within a hot-spot of an image. */
26763
26764 static int
26765 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26766 {
26767 if (!CONSP (hot_spot))
26768 return 0;
26769
26770 if (EQ (XCAR (hot_spot), Qrect))
26771 {
26772 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26773 Lisp_Object rect = XCDR (hot_spot);
26774 Lisp_Object tem;
26775 if (!CONSP (rect))
26776 return 0;
26777 if (!CONSP (XCAR (rect)))
26778 return 0;
26779 if (!CONSP (XCDR (rect)))
26780 return 0;
26781 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26782 return 0;
26783 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26784 return 0;
26785 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26786 return 0;
26787 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26788 return 0;
26789 return 1;
26790 }
26791 else if (EQ (XCAR (hot_spot), Qcircle))
26792 {
26793 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26794 Lisp_Object circ = XCDR (hot_spot);
26795 Lisp_Object lr, lx0, ly0;
26796 if (CONSP (circ)
26797 && CONSP (XCAR (circ))
26798 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26799 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26800 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26801 {
26802 double r = XFLOATINT (lr);
26803 double dx = XINT (lx0) - x;
26804 double dy = XINT (ly0) - y;
26805 return (dx * dx + dy * dy <= r * r);
26806 }
26807 }
26808 else if (EQ (XCAR (hot_spot), Qpoly))
26809 {
26810 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26811 if (VECTORP (XCDR (hot_spot)))
26812 {
26813 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26814 Lisp_Object *poly = v->contents;
26815 ptrdiff_t n = v->header.size;
26816 ptrdiff_t i;
26817 int inside = 0;
26818 Lisp_Object lx, ly;
26819 int x0, y0;
26820
26821 /* Need an even number of coordinates, and at least 3 edges. */
26822 if (n < 6 || n & 1)
26823 return 0;
26824
26825 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26826 If count is odd, we are inside polygon. Pixels on edges
26827 may or may not be included depending on actual geometry of the
26828 polygon. */
26829 if ((lx = poly[n-2], !INTEGERP (lx))
26830 || (ly = poly[n-1], !INTEGERP (lx)))
26831 return 0;
26832 x0 = XINT (lx), y0 = XINT (ly);
26833 for (i = 0; i < n; i += 2)
26834 {
26835 int x1 = x0, y1 = y0;
26836 if ((lx = poly[i], !INTEGERP (lx))
26837 || (ly = poly[i+1], !INTEGERP (ly)))
26838 return 0;
26839 x0 = XINT (lx), y0 = XINT (ly);
26840
26841 /* Does this segment cross the X line? */
26842 if (x0 >= x)
26843 {
26844 if (x1 >= x)
26845 continue;
26846 }
26847 else if (x1 < x)
26848 continue;
26849 if (y > y0 && y > y1)
26850 continue;
26851 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26852 inside = !inside;
26853 }
26854 return inside;
26855 }
26856 }
26857 return 0;
26858 }
26859
26860 Lisp_Object
26861 find_hot_spot (Lisp_Object map, int x, int y)
26862 {
26863 while (CONSP (map))
26864 {
26865 if (CONSP (XCAR (map))
26866 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26867 return XCAR (map);
26868 map = XCDR (map);
26869 }
26870
26871 return Qnil;
26872 }
26873
26874 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26875 3, 3, 0,
26876 doc: /* Lookup in image map MAP coordinates X and Y.
26877 An image map is an alist where each element has the format (AREA ID PLIST).
26878 An AREA is specified as either a rectangle, a circle, or a polygon:
26879 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26880 pixel coordinates of the upper left and bottom right corners.
26881 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26882 and the radius of the circle; r may be a float or integer.
26883 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26884 vector describes one corner in the polygon.
26885 Returns the alist element for the first matching AREA in MAP. */)
26886 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26887 {
26888 if (NILP (map))
26889 return Qnil;
26890
26891 CHECK_NUMBER (x);
26892 CHECK_NUMBER (y);
26893
26894 return find_hot_spot (map,
26895 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
26896 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
26897 }
26898
26899
26900 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26901 static void
26902 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26903 {
26904 /* Do not change cursor shape while dragging mouse. */
26905 if (!NILP (do_mouse_tracking))
26906 return;
26907
26908 if (!NILP (pointer))
26909 {
26910 if (EQ (pointer, Qarrow))
26911 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26912 else if (EQ (pointer, Qhand))
26913 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26914 else if (EQ (pointer, Qtext))
26915 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26916 else if (EQ (pointer, intern ("hdrag")))
26917 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26918 #ifdef HAVE_X_WINDOWS
26919 else if (EQ (pointer, intern ("vdrag")))
26920 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26921 #endif
26922 else if (EQ (pointer, intern ("hourglass")))
26923 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26924 else if (EQ (pointer, Qmodeline))
26925 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26926 else
26927 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26928 }
26929
26930 if (cursor != No_Cursor)
26931 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26932 }
26933
26934 #endif /* HAVE_WINDOW_SYSTEM */
26935
26936 /* Take proper action when mouse has moved to the mode or header line
26937 or marginal area AREA of window W, x-position X and y-position Y.
26938 X is relative to the start of the text display area of W, so the
26939 width of bitmap areas and scroll bars must be subtracted to get a
26940 position relative to the start of the mode line. */
26941
26942 static void
26943 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26944 enum window_part area)
26945 {
26946 struct window *w = XWINDOW (window);
26947 struct frame *f = XFRAME (w->frame);
26948 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26949 #ifdef HAVE_WINDOW_SYSTEM
26950 Display_Info *dpyinfo;
26951 #endif
26952 Cursor cursor = No_Cursor;
26953 Lisp_Object pointer = Qnil;
26954 int dx, dy, width, height;
26955 ptrdiff_t charpos;
26956 Lisp_Object string, object = Qnil;
26957 Lisp_Object pos, help;
26958
26959 Lisp_Object mouse_face;
26960 int original_x_pixel = x;
26961 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26962 struct glyph_row *row;
26963
26964 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26965 {
26966 int x0;
26967 struct glyph *end;
26968
26969 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26970 returns them in row/column units! */
26971 string = mode_line_string (w, area, &x, &y, &charpos,
26972 &object, &dx, &dy, &width, &height);
26973
26974 row = (area == ON_MODE_LINE
26975 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26976 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26977
26978 /* Find the glyph under the mouse pointer. */
26979 if (row->mode_line_p && row->enabled_p)
26980 {
26981 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26982 end = glyph + row->used[TEXT_AREA];
26983
26984 for (x0 = original_x_pixel;
26985 glyph < end && x0 >= glyph->pixel_width;
26986 ++glyph)
26987 x0 -= glyph->pixel_width;
26988
26989 if (glyph >= end)
26990 glyph = NULL;
26991 }
26992 }
26993 else
26994 {
26995 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26996 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26997 returns them in row/column units! */
26998 string = marginal_area_string (w, area, &x, &y, &charpos,
26999 &object, &dx, &dy, &width, &height);
27000 }
27001
27002 help = Qnil;
27003
27004 #ifdef HAVE_WINDOW_SYSTEM
27005 if (IMAGEP (object))
27006 {
27007 Lisp_Object image_map, hotspot;
27008 if ((image_map = Fplist_get (XCDR (object), QCmap),
27009 !NILP (image_map))
27010 && (hotspot = find_hot_spot (image_map, dx, dy),
27011 CONSP (hotspot))
27012 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27013 {
27014 Lisp_Object plist;
27015
27016 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27017 If so, we could look for mouse-enter, mouse-leave
27018 properties in PLIST (and do something...). */
27019 hotspot = XCDR (hotspot);
27020 if (CONSP (hotspot)
27021 && (plist = XCAR (hotspot), CONSP (plist)))
27022 {
27023 pointer = Fplist_get (plist, Qpointer);
27024 if (NILP (pointer))
27025 pointer = Qhand;
27026 help = Fplist_get (plist, Qhelp_echo);
27027 if (!NILP (help))
27028 {
27029 help_echo_string = help;
27030 /* Is this correct? ++kfs */
27031 XSETWINDOW (help_echo_window, w);
27032 help_echo_object = w->buffer;
27033 help_echo_pos = charpos;
27034 }
27035 }
27036 }
27037 if (NILP (pointer))
27038 pointer = Fplist_get (XCDR (object), QCpointer);
27039 }
27040 #endif /* HAVE_WINDOW_SYSTEM */
27041
27042 if (STRINGP (string))
27043 {
27044 pos = make_number (charpos);
27045 /* If we're on a string with `help-echo' text property, arrange
27046 for the help to be displayed. This is done by setting the
27047 global variable help_echo_string to the help string. */
27048 if (NILP (help))
27049 {
27050 help = Fget_text_property (pos, Qhelp_echo, string);
27051 if (!NILP (help))
27052 {
27053 help_echo_string = help;
27054 XSETWINDOW (help_echo_window, w);
27055 help_echo_object = string;
27056 help_echo_pos = charpos;
27057 }
27058 }
27059
27060 #ifdef HAVE_WINDOW_SYSTEM
27061 if (FRAME_WINDOW_P (f))
27062 {
27063 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27064 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27065 if (NILP (pointer))
27066 pointer = Fget_text_property (pos, Qpointer, string);
27067
27068 /* Change the mouse pointer according to what is under X/Y. */
27069 if (NILP (pointer)
27070 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27071 {
27072 Lisp_Object map;
27073 map = Fget_text_property (pos, Qlocal_map, string);
27074 if (!KEYMAPP (map))
27075 map = Fget_text_property (pos, Qkeymap, string);
27076 if (!KEYMAPP (map))
27077 cursor = dpyinfo->vertical_scroll_bar_cursor;
27078 }
27079 }
27080 #endif
27081
27082 /* Change the mouse face according to what is under X/Y. */
27083 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27084 if (!NILP (mouse_face)
27085 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27086 && glyph)
27087 {
27088 Lisp_Object b, e;
27089
27090 struct glyph * tmp_glyph;
27091
27092 int gpos;
27093 int gseq_length;
27094 int total_pixel_width;
27095 ptrdiff_t begpos, endpos, ignore;
27096
27097 int vpos, hpos;
27098
27099 b = Fprevious_single_property_change (make_number (charpos + 1),
27100 Qmouse_face, string, Qnil);
27101 if (NILP (b))
27102 begpos = 0;
27103 else
27104 begpos = XINT (b);
27105
27106 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27107 if (NILP (e))
27108 endpos = SCHARS (string);
27109 else
27110 endpos = XINT (e);
27111
27112 /* Calculate the glyph position GPOS of GLYPH in the
27113 displayed string, relative to the beginning of the
27114 highlighted part of the string.
27115
27116 Note: GPOS is different from CHARPOS. CHARPOS is the
27117 position of GLYPH in the internal string object. A mode
27118 line string format has structures which are converted to
27119 a flattened string by the Emacs Lisp interpreter. The
27120 internal string is an element of those structures. The
27121 displayed string is the flattened string. */
27122 tmp_glyph = row_start_glyph;
27123 while (tmp_glyph < glyph
27124 && (!(EQ (tmp_glyph->object, glyph->object)
27125 && begpos <= tmp_glyph->charpos
27126 && tmp_glyph->charpos < endpos)))
27127 tmp_glyph++;
27128 gpos = glyph - tmp_glyph;
27129
27130 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27131 the highlighted part of the displayed string to which
27132 GLYPH belongs. Note: GSEQ_LENGTH is different from
27133 SCHARS (STRING), because the latter returns the length of
27134 the internal string. */
27135 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27136 tmp_glyph > glyph
27137 && (!(EQ (tmp_glyph->object, glyph->object)
27138 && begpos <= tmp_glyph->charpos
27139 && tmp_glyph->charpos < endpos));
27140 tmp_glyph--)
27141 ;
27142 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27143
27144 /* Calculate the total pixel width of all the glyphs between
27145 the beginning of the highlighted area and GLYPH. */
27146 total_pixel_width = 0;
27147 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27148 total_pixel_width += tmp_glyph->pixel_width;
27149
27150 /* Pre calculation of re-rendering position. Note: X is in
27151 column units here, after the call to mode_line_string or
27152 marginal_area_string. */
27153 hpos = x - gpos;
27154 vpos = (area == ON_MODE_LINE
27155 ? (w->current_matrix)->nrows - 1
27156 : 0);
27157
27158 /* If GLYPH's position is included in the region that is
27159 already drawn in mouse face, we have nothing to do. */
27160 if ( EQ (window, hlinfo->mouse_face_window)
27161 && (!row->reversed_p
27162 ? (hlinfo->mouse_face_beg_col <= hpos
27163 && hpos < hlinfo->mouse_face_end_col)
27164 /* In R2L rows we swap BEG and END, see below. */
27165 : (hlinfo->mouse_face_end_col <= hpos
27166 && hpos < hlinfo->mouse_face_beg_col))
27167 && hlinfo->mouse_face_beg_row == vpos )
27168 return;
27169
27170 if (clear_mouse_face (hlinfo))
27171 cursor = No_Cursor;
27172
27173 if (!row->reversed_p)
27174 {
27175 hlinfo->mouse_face_beg_col = hpos;
27176 hlinfo->mouse_face_beg_x = original_x_pixel
27177 - (total_pixel_width + dx);
27178 hlinfo->mouse_face_end_col = hpos + gseq_length;
27179 hlinfo->mouse_face_end_x = 0;
27180 }
27181 else
27182 {
27183 /* In R2L rows, show_mouse_face expects BEG and END
27184 coordinates to be swapped. */
27185 hlinfo->mouse_face_end_col = hpos;
27186 hlinfo->mouse_face_end_x = original_x_pixel
27187 - (total_pixel_width + dx);
27188 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27189 hlinfo->mouse_face_beg_x = 0;
27190 }
27191
27192 hlinfo->mouse_face_beg_row = vpos;
27193 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27194 hlinfo->mouse_face_beg_y = 0;
27195 hlinfo->mouse_face_end_y = 0;
27196 hlinfo->mouse_face_past_end = 0;
27197 hlinfo->mouse_face_window = window;
27198
27199 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27200 charpos,
27201 0, 0, 0,
27202 &ignore,
27203 glyph->face_id,
27204 1);
27205 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27206
27207 if (NILP (pointer))
27208 pointer = Qhand;
27209 }
27210 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27211 clear_mouse_face (hlinfo);
27212 }
27213 #ifdef HAVE_WINDOW_SYSTEM
27214 if (FRAME_WINDOW_P (f))
27215 define_frame_cursor1 (f, cursor, pointer);
27216 #endif
27217 }
27218
27219
27220 /* EXPORT:
27221 Take proper action when the mouse has moved to position X, Y on
27222 frame F as regards highlighting characters that have mouse-face
27223 properties. Also de-highlighting chars where the mouse was before.
27224 X and Y can be negative or out of range. */
27225
27226 void
27227 note_mouse_highlight (struct frame *f, int x, int y)
27228 {
27229 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27230 enum window_part part = ON_NOTHING;
27231 Lisp_Object window;
27232 struct window *w;
27233 Cursor cursor = No_Cursor;
27234 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27235 struct buffer *b;
27236
27237 /* When a menu is active, don't highlight because this looks odd. */
27238 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27239 if (popup_activated ())
27240 return;
27241 #endif
27242
27243 if (NILP (Vmouse_highlight)
27244 || !f->glyphs_initialized_p
27245 || f->pointer_invisible)
27246 return;
27247
27248 hlinfo->mouse_face_mouse_x = x;
27249 hlinfo->mouse_face_mouse_y = y;
27250 hlinfo->mouse_face_mouse_frame = f;
27251
27252 if (hlinfo->mouse_face_defer)
27253 return;
27254
27255 if (gc_in_progress)
27256 {
27257 hlinfo->mouse_face_deferred_gc = 1;
27258 return;
27259 }
27260
27261 /* Which window is that in? */
27262 window = window_from_coordinates (f, x, y, &part, 1);
27263
27264 /* If displaying active text in another window, clear that. */
27265 if (! EQ (window, hlinfo->mouse_face_window)
27266 /* Also clear if we move out of text area in same window. */
27267 || (!NILP (hlinfo->mouse_face_window)
27268 && !NILP (window)
27269 && part != ON_TEXT
27270 && part != ON_MODE_LINE
27271 && part != ON_HEADER_LINE))
27272 clear_mouse_face (hlinfo);
27273
27274 /* Not on a window -> return. */
27275 if (!WINDOWP (window))
27276 return;
27277
27278 /* Reset help_echo_string. It will get recomputed below. */
27279 help_echo_string = Qnil;
27280
27281 /* Convert to window-relative pixel coordinates. */
27282 w = XWINDOW (window);
27283 frame_to_window_pixel_xy (w, &x, &y);
27284
27285 #ifdef HAVE_WINDOW_SYSTEM
27286 /* Handle tool-bar window differently since it doesn't display a
27287 buffer. */
27288 if (EQ (window, f->tool_bar_window))
27289 {
27290 note_tool_bar_highlight (f, x, y);
27291 return;
27292 }
27293 #endif
27294
27295 /* Mouse is on the mode, header line or margin? */
27296 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27297 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27298 {
27299 note_mode_line_or_margin_highlight (window, x, y, part);
27300 return;
27301 }
27302
27303 #ifdef HAVE_WINDOW_SYSTEM
27304 if (part == ON_VERTICAL_BORDER)
27305 {
27306 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27307 help_echo_string = build_string ("drag-mouse-1: resize");
27308 }
27309 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27310 || part == ON_SCROLL_BAR)
27311 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27312 else
27313 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27314 #endif
27315
27316 /* Are we in a window whose display is up to date?
27317 And verify the buffer's text has not changed. */
27318 b = XBUFFER (w->buffer);
27319 if (part == ON_TEXT
27320 && EQ (w->window_end_valid, w->buffer)
27321 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27322 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27323 {
27324 int hpos, vpos, dx, dy, area = LAST_AREA;
27325 ptrdiff_t pos;
27326 struct glyph *glyph;
27327 Lisp_Object object;
27328 Lisp_Object mouse_face = Qnil, position;
27329 Lisp_Object *overlay_vec = NULL;
27330 ptrdiff_t i, noverlays;
27331 struct buffer *obuf;
27332 ptrdiff_t obegv, ozv;
27333 int same_region;
27334
27335 /* Find the glyph under X/Y. */
27336 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27337
27338 #ifdef HAVE_WINDOW_SYSTEM
27339 /* Look for :pointer property on image. */
27340 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27341 {
27342 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27343 if (img != NULL && IMAGEP (img->spec))
27344 {
27345 Lisp_Object image_map, hotspot;
27346 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27347 !NILP (image_map))
27348 && (hotspot = find_hot_spot (image_map,
27349 glyph->slice.img.x + dx,
27350 glyph->slice.img.y + dy),
27351 CONSP (hotspot))
27352 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27353 {
27354 Lisp_Object plist;
27355
27356 /* Could check XCAR (hotspot) to see if we enter/leave
27357 this hot-spot.
27358 If so, we could look for mouse-enter, mouse-leave
27359 properties in PLIST (and do something...). */
27360 hotspot = XCDR (hotspot);
27361 if (CONSP (hotspot)
27362 && (plist = XCAR (hotspot), CONSP (plist)))
27363 {
27364 pointer = Fplist_get (plist, Qpointer);
27365 if (NILP (pointer))
27366 pointer = Qhand;
27367 help_echo_string = Fplist_get (plist, Qhelp_echo);
27368 if (!NILP (help_echo_string))
27369 {
27370 help_echo_window = window;
27371 help_echo_object = glyph->object;
27372 help_echo_pos = glyph->charpos;
27373 }
27374 }
27375 }
27376 if (NILP (pointer))
27377 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27378 }
27379 }
27380 #endif /* HAVE_WINDOW_SYSTEM */
27381
27382 /* Clear mouse face if X/Y not over text. */
27383 if (glyph == NULL
27384 || area != TEXT_AREA
27385 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27386 /* Glyph's OBJECT is an integer for glyphs inserted by the
27387 display engine for its internal purposes, like truncation
27388 and continuation glyphs and blanks beyond the end of
27389 line's text on text terminals. If we are over such a
27390 glyph, we are not over any text. */
27391 || INTEGERP (glyph->object)
27392 /* R2L rows have a stretch glyph at their front, which
27393 stands for no text, whereas L2R rows have no glyphs at
27394 all beyond the end of text. Treat such stretch glyphs
27395 like we do with NULL glyphs in L2R rows. */
27396 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27397 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27398 && glyph->type == STRETCH_GLYPH
27399 && glyph->avoid_cursor_p))
27400 {
27401 if (clear_mouse_face (hlinfo))
27402 cursor = No_Cursor;
27403 #ifdef HAVE_WINDOW_SYSTEM
27404 if (FRAME_WINDOW_P (f) && NILP (pointer))
27405 {
27406 if (area != TEXT_AREA)
27407 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27408 else
27409 pointer = Vvoid_text_area_pointer;
27410 }
27411 #endif
27412 goto set_cursor;
27413 }
27414
27415 pos = glyph->charpos;
27416 object = glyph->object;
27417 if (!STRINGP (object) && !BUFFERP (object))
27418 goto set_cursor;
27419
27420 /* If we get an out-of-range value, return now; avoid an error. */
27421 if (BUFFERP (object) && pos > BUF_Z (b))
27422 goto set_cursor;
27423
27424 /* Make the window's buffer temporarily current for
27425 overlays_at and compute_char_face. */
27426 obuf = current_buffer;
27427 current_buffer = b;
27428 obegv = BEGV;
27429 ozv = ZV;
27430 BEGV = BEG;
27431 ZV = Z;
27432
27433 /* Is this char mouse-active or does it have help-echo? */
27434 position = make_number (pos);
27435
27436 if (BUFFERP (object))
27437 {
27438 /* Put all the overlays we want in a vector in overlay_vec. */
27439 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27440 /* Sort overlays into increasing priority order. */
27441 noverlays = sort_overlays (overlay_vec, noverlays, w);
27442 }
27443 else
27444 noverlays = 0;
27445
27446 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27447
27448 if (same_region)
27449 cursor = No_Cursor;
27450
27451 /* Check mouse-face highlighting. */
27452 if (! same_region
27453 /* If there exists an overlay with mouse-face overlapping
27454 the one we are currently highlighting, we have to
27455 check if we enter the overlapping overlay, and then
27456 highlight only that. */
27457 || (OVERLAYP (hlinfo->mouse_face_overlay)
27458 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27459 {
27460 /* Find the highest priority overlay with a mouse-face. */
27461 Lisp_Object overlay = Qnil;
27462 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27463 {
27464 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27465 if (!NILP (mouse_face))
27466 overlay = overlay_vec[i];
27467 }
27468
27469 /* If we're highlighting the same overlay as before, there's
27470 no need to do that again. */
27471 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27472 goto check_help_echo;
27473 hlinfo->mouse_face_overlay = overlay;
27474
27475 /* Clear the display of the old active region, if any. */
27476 if (clear_mouse_face (hlinfo))
27477 cursor = No_Cursor;
27478
27479 /* If no overlay applies, get a text property. */
27480 if (NILP (overlay))
27481 mouse_face = Fget_text_property (position, Qmouse_face, object);
27482
27483 /* Next, compute the bounds of the mouse highlighting and
27484 display it. */
27485 if (!NILP (mouse_face) && STRINGP (object))
27486 {
27487 /* The mouse-highlighting comes from a display string
27488 with a mouse-face. */
27489 Lisp_Object s, e;
27490 ptrdiff_t ignore;
27491
27492 s = Fprevious_single_property_change
27493 (make_number (pos + 1), Qmouse_face, object, Qnil);
27494 e = Fnext_single_property_change
27495 (position, Qmouse_face, object, Qnil);
27496 if (NILP (s))
27497 s = make_number (0);
27498 if (NILP (e))
27499 e = make_number (SCHARS (object) - 1);
27500 mouse_face_from_string_pos (w, hlinfo, object,
27501 XINT (s), XINT (e));
27502 hlinfo->mouse_face_past_end = 0;
27503 hlinfo->mouse_face_window = window;
27504 hlinfo->mouse_face_face_id
27505 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27506 glyph->face_id, 1);
27507 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27508 cursor = No_Cursor;
27509 }
27510 else
27511 {
27512 /* The mouse-highlighting, if any, comes from an overlay
27513 or text property in the buffer. */
27514 Lisp_Object buffer IF_LINT (= Qnil);
27515 Lisp_Object disp_string IF_LINT (= Qnil);
27516
27517 if (STRINGP (object))
27518 {
27519 /* If we are on a display string with no mouse-face,
27520 check if the text under it has one. */
27521 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27522 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27523 pos = string_buffer_position (object, start);
27524 if (pos > 0)
27525 {
27526 mouse_face = get_char_property_and_overlay
27527 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27528 buffer = w->buffer;
27529 disp_string = object;
27530 }
27531 }
27532 else
27533 {
27534 buffer = object;
27535 disp_string = Qnil;
27536 }
27537
27538 if (!NILP (mouse_face))
27539 {
27540 Lisp_Object before, after;
27541 Lisp_Object before_string, after_string;
27542 /* To correctly find the limits of mouse highlight
27543 in a bidi-reordered buffer, we must not use the
27544 optimization of limiting the search in
27545 previous-single-property-change and
27546 next-single-property-change, because
27547 rows_from_pos_range needs the real start and end
27548 positions to DTRT in this case. That's because
27549 the first row visible in a window does not
27550 necessarily display the character whose position
27551 is the smallest. */
27552 Lisp_Object lim1 =
27553 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27554 ? Fmarker_position (w->start)
27555 : Qnil;
27556 Lisp_Object lim2 =
27557 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27558 ? make_number (BUF_Z (XBUFFER (buffer))
27559 - XFASTINT (w->window_end_pos))
27560 : Qnil;
27561
27562 if (NILP (overlay))
27563 {
27564 /* Handle the text property case. */
27565 before = Fprevious_single_property_change
27566 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27567 after = Fnext_single_property_change
27568 (make_number (pos), Qmouse_face, buffer, lim2);
27569 before_string = after_string = Qnil;
27570 }
27571 else
27572 {
27573 /* Handle the overlay case. */
27574 before = Foverlay_start (overlay);
27575 after = Foverlay_end (overlay);
27576 before_string = Foverlay_get (overlay, Qbefore_string);
27577 after_string = Foverlay_get (overlay, Qafter_string);
27578
27579 if (!STRINGP (before_string)) before_string = Qnil;
27580 if (!STRINGP (after_string)) after_string = Qnil;
27581 }
27582
27583 mouse_face_from_buffer_pos (window, hlinfo, pos,
27584 NILP (before)
27585 ? 1
27586 : XFASTINT (before),
27587 NILP (after)
27588 ? BUF_Z (XBUFFER (buffer))
27589 : XFASTINT (after),
27590 before_string, after_string,
27591 disp_string);
27592 cursor = No_Cursor;
27593 }
27594 }
27595 }
27596
27597 check_help_echo:
27598
27599 /* Look for a `help-echo' property. */
27600 if (NILP (help_echo_string)) {
27601 Lisp_Object help, overlay;
27602
27603 /* Check overlays first. */
27604 help = overlay = Qnil;
27605 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27606 {
27607 overlay = overlay_vec[i];
27608 help = Foverlay_get (overlay, Qhelp_echo);
27609 }
27610
27611 if (!NILP (help))
27612 {
27613 help_echo_string = help;
27614 help_echo_window = window;
27615 help_echo_object = overlay;
27616 help_echo_pos = pos;
27617 }
27618 else
27619 {
27620 Lisp_Object obj = glyph->object;
27621 ptrdiff_t charpos = glyph->charpos;
27622
27623 /* Try text properties. */
27624 if (STRINGP (obj)
27625 && charpos >= 0
27626 && charpos < SCHARS (obj))
27627 {
27628 help = Fget_text_property (make_number (charpos),
27629 Qhelp_echo, obj);
27630 if (NILP (help))
27631 {
27632 /* If the string itself doesn't specify a help-echo,
27633 see if the buffer text ``under'' it does. */
27634 struct glyph_row *r
27635 = MATRIX_ROW (w->current_matrix, vpos);
27636 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27637 ptrdiff_t p = string_buffer_position (obj, start);
27638 if (p > 0)
27639 {
27640 help = Fget_char_property (make_number (p),
27641 Qhelp_echo, w->buffer);
27642 if (!NILP (help))
27643 {
27644 charpos = p;
27645 obj = w->buffer;
27646 }
27647 }
27648 }
27649 }
27650 else if (BUFFERP (obj)
27651 && charpos >= BEGV
27652 && charpos < ZV)
27653 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27654 obj);
27655
27656 if (!NILP (help))
27657 {
27658 help_echo_string = help;
27659 help_echo_window = window;
27660 help_echo_object = obj;
27661 help_echo_pos = charpos;
27662 }
27663 }
27664 }
27665
27666 #ifdef HAVE_WINDOW_SYSTEM
27667 /* Look for a `pointer' property. */
27668 if (FRAME_WINDOW_P (f) && NILP (pointer))
27669 {
27670 /* Check overlays first. */
27671 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27672 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27673
27674 if (NILP (pointer))
27675 {
27676 Lisp_Object obj = glyph->object;
27677 ptrdiff_t charpos = glyph->charpos;
27678
27679 /* Try text properties. */
27680 if (STRINGP (obj)
27681 && charpos >= 0
27682 && charpos < SCHARS (obj))
27683 {
27684 pointer = Fget_text_property (make_number (charpos),
27685 Qpointer, obj);
27686 if (NILP (pointer))
27687 {
27688 /* If the string itself doesn't specify a pointer,
27689 see if the buffer text ``under'' it does. */
27690 struct glyph_row *r
27691 = MATRIX_ROW (w->current_matrix, vpos);
27692 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27693 ptrdiff_t p = string_buffer_position (obj, start);
27694 if (p > 0)
27695 pointer = Fget_char_property (make_number (p),
27696 Qpointer, w->buffer);
27697 }
27698 }
27699 else if (BUFFERP (obj)
27700 && charpos >= BEGV
27701 && charpos < ZV)
27702 pointer = Fget_text_property (make_number (charpos),
27703 Qpointer, obj);
27704 }
27705 }
27706 #endif /* HAVE_WINDOW_SYSTEM */
27707
27708 BEGV = obegv;
27709 ZV = ozv;
27710 current_buffer = obuf;
27711 }
27712
27713 set_cursor:
27714
27715 #ifdef HAVE_WINDOW_SYSTEM
27716 if (FRAME_WINDOW_P (f))
27717 define_frame_cursor1 (f, cursor, pointer);
27718 #else
27719 /* This is here to prevent a compiler error, about "label at end of
27720 compound statement". */
27721 return;
27722 #endif
27723 }
27724
27725
27726 /* EXPORT for RIF:
27727 Clear any mouse-face on window W. This function is part of the
27728 redisplay interface, and is called from try_window_id and similar
27729 functions to ensure the mouse-highlight is off. */
27730
27731 void
27732 x_clear_window_mouse_face (struct window *w)
27733 {
27734 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27735 Lisp_Object window;
27736
27737 BLOCK_INPUT;
27738 XSETWINDOW (window, w);
27739 if (EQ (window, hlinfo->mouse_face_window))
27740 clear_mouse_face (hlinfo);
27741 UNBLOCK_INPUT;
27742 }
27743
27744
27745 /* EXPORT:
27746 Just discard the mouse face information for frame F, if any.
27747 This is used when the size of F is changed. */
27748
27749 void
27750 cancel_mouse_face (struct frame *f)
27751 {
27752 Lisp_Object window;
27753 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27754
27755 window = hlinfo->mouse_face_window;
27756 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27757 {
27758 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27759 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27760 hlinfo->mouse_face_window = Qnil;
27761 }
27762 }
27763
27764
27765 \f
27766 /***********************************************************************
27767 Exposure Events
27768 ***********************************************************************/
27769
27770 #ifdef HAVE_WINDOW_SYSTEM
27771
27772 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27773 which intersects rectangle R. R is in window-relative coordinates. */
27774
27775 static void
27776 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27777 enum glyph_row_area area)
27778 {
27779 struct glyph *first = row->glyphs[area];
27780 struct glyph *end = row->glyphs[area] + row->used[area];
27781 struct glyph *last;
27782 int first_x, start_x, x;
27783
27784 if (area == TEXT_AREA && row->fill_line_p)
27785 /* If row extends face to end of line write the whole line. */
27786 draw_glyphs (w, 0, row, area,
27787 0, row->used[area],
27788 DRAW_NORMAL_TEXT, 0);
27789 else
27790 {
27791 /* Set START_X to the window-relative start position for drawing glyphs of
27792 AREA. The first glyph of the text area can be partially visible.
27793 The first glyphs of other areas cannot. */
27794 start_x = window_box_left_offset (w, area);
27795 x = start_x;
27796 if (area == TEXT_AREA)
27797 x += row->x;
27798
27799 /* Find the first glyph that must be redrawn. */
27800 while (first < end
27801 && x + first->pixel_width < r->x)
27802 {
27803 x += first->pixel_width;
27804 ++first;
27805 }
27806
27807 /* Find the last one. */
27808 last = first;
27809 first_x = x;
27810 while (last < end
27811 && x < r->x + r->width)
27812 {
27813 x += last->pixel_width;
27814 ++last;
27815 }
27816
27817 /* Repaint. */
27818 if (last > first)
27819 draw_glyphs (w, first_x - start_x, row, area,
27820 first - row->glyphs[area], last - row->glyphs[area],
27821 DRAW_NORMAL_TEXT, 0);
27822 }
27823 }
27824
27825
27826 /* Redraw the parts of the glyph row ROW on window W intersecting
27827 rectangle R. R is in window-relative coordinates. Value is
27828 non-zero if mouse-face was overwritten. */
27829
27830 static int
27831 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27832 {
27833 xassert (row->enabled_p);
27834
27835 if (row->mode_line_p || w->pseudo_window_p)
27836 draw_glyphs (w, 0, row, TEXT_AREA,
27837 0, row->used[TEXT_AREA],
27838 DRAW_NORMAL_TEXT, 0);
27839 else
27840 {
27841 if (row->used[LEFT_MARGIN_AREA])
27842 expose_area (w, row, r, LEFT_MARGIN_AREA);
27843 if (row->used[TEXT_AREA])
27844 expose_area (w, row, r, TEXT_AREA);
27845 if (row->used[RIGHT_MARGIN_AREA])
27846 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27847 draw_row_fringe_bitmaps (w, row);
27848 }
27849
27850 return row->mouse_face_p;
27851 }
27852
27853
27854 /* Redraw those parts of glyphs rows during expose event handling that
27855 overlap other rows. Redrawing of an exposed line writes over parts
27856 of lines overlapping that exposed line; this function fixes that.
27857
27858 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27859 row in W's current matrix that is exposed and overlaps other rows.
27860 LAST_OVERLAPPING_ROW is the last such row. */
27861
27862 static void
27863 expose_overlaps (struct window *w,
27864 struct glyph_row *first_overlapping_row,
27865 struct glyph_row *last_overlapping_row,
27866 XRectangle *r)
27867 {
27868 struct glyph_row *row;
27869
27870 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27871 if (row->overlapping_p)
27872 {
27873 xassert (row->enabled_p && !row->mode_line_p);
27874
27875 row->clip = r;
27876 if (row->used[LEFT_MARGIN_AREA])
27877 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27878
27879 if (row->used[TEXT_AREA])
27880 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27881
27882 if (row->used[RIGHT_MARGIN_AREA])
27883 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27884 row->clip = NULL;
27885 }
27886 }
27887
27888
27889 /* Return non-zero if W's cursor intersects rectangle R. */
27890
27891 static int
27892 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27893 {
27894 XRectangle cr, result;
27895 struct glyph *cursor_glyph;
27896 struct glyph_row *row;
27897
27898 if (w->phys_cursor.vpos >= 0
27899 && w->phys_cursor.vpos < w->current_matrix->nrows
27900 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27901 row->enabled_p)
27902 && row->cursor_in_fringe_p)
27903 {
27904 /* Cursor is in the fringe. */
27905 cr.x = window_box_right_offset (w,
27906 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27907 ? RIGHT_MARGIN_AREA
27908 : TEXT_AREA));
27909 cr.y = row->y;
27910 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27911 cr.height = row->height;
27912 return x_intersect_rectangles (&cr, r, &result);
27913 }
27914
27915 cursor_glyph = get_phys_cursor_glyph (w);
27916 if (cursor_glyph)
27917 {
27918 /* r is relative to W's box, but w->phys_cursor.x is relative
27919 to left edge of W's TEXT area. Adjust it. */
27920 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27921 cr.y = w->phys_cursor.y;
27922 cr.width = cursor_glyph->pixel_width;
27923 cr.height = w->phys_cursor_height;
27924 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27925 I assume the effect is the same -- and this is portable. */
27926 return x_intersect_rectangles (&cr, r, &result);
27927 }
27928 /* If we don't understand the format, pretend we're not in the hot-spot. */
27929 return 0;
27930 }
27931
27932
27933 /* EXPORT:
27934 Draw a vertical window border to the right of window W if W doesn't
27935 have vertical scroll bars. */
27936
27937 void
27938 x_draw_vertical_border (struct window *w)
27939 {
27940 struct frame *f = XFRAME (WINDOW_FRAME (w));
27941
27942 /* We could do better, if we knew what type of scroll-bar the adjacent
27943 windows (on either side) have... But we don't :-(
27944 However, I think this works ok. ++KFS 2003-04-25 */
27945
27946 /* Redraw borders between horizontally adjacent windows. Don't
27947 do it for frames with vertical scroll bars because either the
27948 right scroll bar of a window, or the left scroll bar of its
27949 neighbor will suffice as a border. */
27950 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27951 return;
27952
27953 if (!WINDOW_RIGHTMOST_P (w)
27954 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27955 {
27956 int x0, x1, y0, y1;
27957
27958 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27959 y1 -= 1;
27960
27961 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27962 x1 -= 1;
27963
27964 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27965 }
27966 else if (!WINDOW_LEFTMOST_P (w)
27967 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27968 {
27969 int x0, x1, y0, y1;
27970
27971 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27972 y1 -= 1;
27973
27974 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27975 x0 -= 1;
27976
27977 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27978 }
27979 }
27980
27981
27982 /* Redraw the part of window W intersection rectangle FR. Pixel
27983 coordinates in FR are frame-relative. Call this function with
27984 input blocked. Value is non-zero if the exposure overwrites
27985 mouse-face. */
27986
27987 static int
27988 expose_window (struct window *w, XRectangle *fr)
27989 {
27990 struct frame *f = XFRAME (w->frame);
27991 XRectangle wr, r;
27992 int mouse_face_overwritten_p = 0;
27993
27994 /* If window is not yet fully initialized, do nothing. This can
27995 happen when toolkit scroll bars are used and a window is split.
27996 Reconfiguring the scroll bar will generate an expose for a newly
27997 created window. */
27998 if (w->current_matrix == NULL)
27999 return 0;
28000
28001 /* When we're currently updating the window, display and current
28002 matrix usually don't agree. Arrange for a thorough display
28003 later. */
28004 if (w == updated_window)
28005 {
28006 SET_FRAME_GARBAGED (f);
28007 return 0;
28008 }
28009
28010 /* Frame-relative pixel rectangle of W. */
28011 wr.x = WINDOW_LEFT_EDGE_X (w);
28012 wr.y = WINDOW_TOP_EDGE_Y (w);
28013 wr.width = WINDOW_TOTAL_WIDTH (w);
28014 wr.height = WINDOW_TOTAL_HEIGHT (w);
28015
28016 if (x_intersect_rectangles (fr, &wr, &r))
28017 {
28018 int yb = window_text_bottom_y (w);
28019 struct glyph_row *row;
28020 int cursor_cleared_p, phys_cursor_on_p;
28021 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28022
28023 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28024 r.x, r.y, r.width, r.height));
28025
28026 /* Convert to window coordinates. */
28027 r.x -= WINDOW_LEFT_EDGE_X (w);
28028 r.y -= WINDOW_TOP_EDGE_Y (w);
28029
28030 /* Turn off the cursor. */
28031 if (!w->pseudo_window_p
28032 && phys_cursor_in_rect_p (w, &r))
28033 {
28034 x_clear_cursor (w);
28035 cursor_cleared_p = 1;
28036 }
28037 else
28038 cursor_cleared_p = 0;
28039
28040 /* If the row containing the cursor extends face to end of line,
28041 then expose_area might overwrite the cursor outside the
28042 rectangle and thus notice_overwritten_cursor might clear
28043 w->phys_cursor_on_p. We remember the original value and
28044 check later if it is changed. */
28045 phys_cursor_on_p = w->phys_cursor_on_p;
28046
28047 /* Update lines intersecting rectangle R. */
28048 first_overlapping_row = last_overlapping_row = NULL;
28049 for (row = w->current_matrix->rows;
28050 row->enabled_p;
28051 ++row)
28052 {
28053 int y0 = row->y;
28054 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28055
28056 if ((y0 >= r.y && y0 < r.y + r.height)
28057 || (y1 > r.y && y1 < r.y + r.height)
28058 || (r.y >= y0 && r.y < y1)
28059 || (r.y + r.height > y0 && r.y + r.height < y1))
28060 {
28061 /* A header line may be overlapping, but there is no need
28062 to fix overlapping areas for them. KFS 2005-02-12 */
28063 if (row->overlapping_p && !row->mode_line_p)
28064 {
28065 if (first_overlapping_row == NULL)
28066 first_overlapping_row = row;
28067 last_overlapping_row = row;
28068 }
28069
28070 row->clip = fr;
28071 if (expose_line (w, row, &r))
28072 mouse_face_overwritten_p = 1;
28073 row->clip = NULL;
28074 }
28075 else if (row->overlapping_p)
28076 {
28077 /* We must redraw a row overlapping the exposed area. */
28078 if (y0 < r.y
28079 ? y0 + row->phys_height > r.y
28080 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28081 {
28082 if (first_overlapping_row == NULL)
28083 first_overlapping_row = row;
28084 last_overlapping_row = row;
28085 }
28086 }
28087
28088 if (y1 >= yb)
28089 break;
28090 }
28091
28092 /* Display the mode line if there is one. */
28093 if (WINDOW_WANTS_MODELINE_P (w)
28094 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28095 row->enabled_p)
28096 && row->y < r.y + r.height)
28097 {
28098 if (expose_line (w, row, &r))
28099 mouse_face_overwritten_p = 1;
28100 }
28101
28102 if (!w->pseudo_window_p)
28103 {
28104 /* Fix the display of overlapping rows. */
28105 if (first_overlapping_row)
28106 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28107 fr);
28108
28109 /* Draw border between windows. */
28110 x_draw_vertical_border (w);
28111
28112 /* Turn the cursor on again. */
28113 if (cursor_cleared_p
28114 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28115 update_window_cursor (w, 1);
28116 }
28117 }
28118
28119 return mouse_face_overwritten_p;
28120 }
28121
28122
28123
28124 /* Redraw (parts) of all windows in the window tree rooted at W that
28125 intersect R. R contains frame pixel coordinates. Value is
28126 non-zero if the exposure overwrites mouse-face. */
28127
28128 static int
28129 expose_window_tree (struct window *w, XRectangle *r)
28130 {
28131 struct frame *f = XFRAME (w->frame);
28132 int mouse_face_overwritten_p = 0;
28133
28134 while (w && !FRAME_GARBAGED_P (f))
28135 {
28136 if (!NILP (w->hchild))
28137 mouse_face_overwritten_p
28138 |= expose_window_tree (XWINDOW (w->hchild), r);
28139 else if (!NILP (w->vchild))
28140 mouse_face_overwritten_p
28141 |= expose_window_tree (XWINDOW (w->vchild), r);
28142 else
28143 mouse_face_overwritten_p |= expose_window (w, r);
28144
28145 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28146 }
28147
28148 return mouse_face_overwritten_p;
28149 }
28150
28151
28152 /* EXPORT:
28153 Redisplay an exposed area of frame F. X and Y are the upper-left
28154 corner of the exposed rectangle. W and H are width and height of
28155 the exposed area. All are pixel values. W or H zero means redraw
28156 the entire frame. */
28157
28158 void
28159 expose_frame (struct frame *f, int x, int y, int w, int h)
28160 {
28161 XRectangle r;
28162 int mouse_face_overwritten_p = 0;
28163
28164 TRACE ((stderr, "expose_frame "));
28165
28166 /* No need to redraw if frame will be redrawn soon. */
28167 if (FRAME_GARBAGED_P (f))
28168 {
28169 TRACE ((stderr, " garbaged\n"));
28170 return;
28171 }
28172
28173 /* If basic faces haven't been realized yet, there is no point in
28174 trying to redraw anything. This can happen when we get an expose
28175 event while Emacs is starting, e.g. by moving another window. */
28176 if (FRAME_FACE_CACHE (f) == NULL
28177 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28178 {
28179 TRACE ((stderr, " no faces\n"));
28180 return;
28181 }
28182
28183 if (w == 0 || h == 0)
28184 {
28185 r.x = r.y = 0;
28186 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28187 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28188 }
28189 else
28190 {
28191 r.x = x;
28192 r.y = y;
28193 r.width = w;
28194 r.height = h;
28195 }
28196
28197 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28198 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28199
28200 if (WINDOWP (f->tool_bar_window))
28201 mouse_face_overwritten_p
28202 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28203
28204 #ifdef HAVE_X_WINDOWS
28205 #ifndef MSDOS
28206 #ifndef USE_X_TOOLKIT
28207 if (WINDOWP (f->menu_bar_window))
28208 mouse_face_overwritten_p
28209 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28210 #endif /* not USE_X_TOOLKIT */
28211 #endif
28212 #endif
28213
28214 /* Some window managers support a focus-follows-mouse style with
28215 delayed raising of frames. Imagine a partially obscured frame,
28216 and moving the mouse into partially obscured mouse-face on that
28217 frame. The visible part of the mouse-face will be highlighted,
28218 then the WM raises the obscured frame. With at least one WM, KDE
28219 2.1, Emacs is not getting any event for the raising of the frame
28220 (even tried with SubstructureRedirectMask), only Expose events.
28221 These expose events will draw text normally, i.e. not
28222 highlighted. Which means we must redo the highlight here.
28223 Subsume it under ``we love X''. --gerd 2001-08-15 */
28224 /* Included in Windows version because Windows most likely does not
28225 do the right thing if any third party tool offers
28226 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28227 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28228 {
28229 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28230 if (f == hlinfo->mouse_face_mouse_frame)
28231 {
28232 int mouse_x = hlinfo->mouse_face_mouse_x;
28233 int mouse_y = hlinfo->mouse_face_mouse_y;
28234 clear_mouse_face (hlinfo);
28235 note_mouse_highlight (f, mouse_x, mouse_y);
28236 }
28237 }
28238 }
28239
28240
28241 /* EXPORT:
28242 Determine the intersection of two rectangles R1 and R2. Return
28243 the intersection in *RESULT. Value is non-zero if RESULT is not
28244 empty. */
28245
28246 int
28247 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28248 {
28249 XRectangle *left, *right;
28250 XRectangle *upper, *lower;
28251 int intersection_p = 0;
28252
28253 /* Rearrange so that R1 is the left-most rectangle. */
28254 if (r1->x < r2->x)
28255 left = r1, right = r2;
28256 else
28257 left = r2, right = r1;
28258
28259 /* X0 of the intersection is right.x0, if this is inside R1,
28260 otherwise there is no intersection. */
28261 if (right->x <= left->x + left->width)
28262 {
28263 result->x = right->x;
28264
28265 /* The right end of the intersection is the minimum of
28266 the right ends of left and right. */
28267 result->width = (min (left->x + left->width, right->x + right->width)
28268 - result->x);
28269
28270 /* Same game for Y. */
28271 if (r1->y < r2->y)
28272 upper = r1, lower = r2;
28273 else
28274 upper = r2, lower = r1;
28275
28276 /* The upper end of the intersection is lower.y0, if this is inside
28277 of upper. Otherwise, there is no intersection. */
28278 if (lower->y <= upper->y + upper->height)
28279 {
28280 result->y = lower->y;
28281
28282 /* The lower end of the intersection is the minimum of the lower
28283 ends of upper and lower. */
28284 result->height = (min (lower->y + lower->height,
28285 upper->y + upper->height)
28286 - result->y);
28287 intersection_p = 1;
28288 }
28289 }
28290
28291 return intersection_p;
28292 }
28293
28294 #endif /* HAVE_WINDOW_SYSTEM */
28295
28296 \f
28297 /***********************************************************************
28298 Initialization
28299 ***********************************************************************/
28300
28301 void
28302 syms_of_xdisp (void)
28303 {
28304 Vwith_echo_area_save_vector = Qnil;
28305 staticpro (&Vwith_echo_area_save_vector);
28306
28307 Vmessage_stack = Qnil;
28308 staticpro (&Vmessage_stack);
28309
28310 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28311
28312 message_dolog_marker1 = Fmake_marker ();
28313 staticpro (&message_dolog_marker1);
28314 message_dolog_marker2 = Fmake_marker ();
28315 staticpro (&message_dolog_marker2);
28316 message_dolog_marker3 = Fmake_marker ();
28317 staticpro (&message_dolog_marker3);
28318
28319 #if GLYPH_DEBUG
28320 defsubr (&Sdump_frame_glyph_matrix);
28321 defsubr (&Sdump_glyph_matrix);
28322 defsubr (&Sdump_glyph_row);
28323 defsubr (&Sdump_tool_bar_row);
28324 defsubr (&Strace_redisplay);
28325 defsubr (&Strace_to_stderr);
28326 #endif
28327 #ifdef HAVE_WINDOW_SYSTEM
28328 defsubr (&Stool_bar_lines_needed);
28329 defsubr (&Slookup_image_map);
28330 #endif
28331 defsubr (&Sformat_mode_line);
28332 defsubr (&Sinvisible_p);
28333 defsubr (&Scurrent_bidi_paragraph_direction);
28334
28335 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28336 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28337 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28338 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28339 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28340 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28341 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28342 DEFSYM (Qeval, "eval");
28343 DEFSYM (QCdata, ":data");
28344 DEFSYM (Qdisplay, "display");
28345 DEFSYM (Qspace_width, "space-width");
28346 DEFSYM (Qraise, "raise");
28347 DEFSYM (Qslice, "slice");
28348 DEFSYM (Qspace, "space");
28349 DEFSYM (Qmargin, "margin");
28350 DEFSYM (Qpointer, "pointer");
28351 DEFSYM (Qleft_margin, "left-margin");
28352 DEFSYM (Qright_margin, "right-margin");
28353 DEFSYM (Qcenter, "center");
28354 DEFSYM (Qline_height, "line-height");
28355 DEFSYM (QCalign_to, ":align-to");
28356 DEFSYM (QCrelative_width, ":relative-width");
28357 DEFSYM (QCrelative_height, ":relative-height");
28358 DEFSYM (QCeval, ":eval");
28359 DEFSYM (QCpropertize, ":propertize");
28360 DEFSYM (QCfile, ":file");
28361 DEFSYM (Qfontified, "fontified");
28362 DEFSYM (Qfontification_functions, "fontification-functions");
28363 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28364 DEFSYM (Qescape_glyph, "escape-glyph");
28365 DEFSYM (Qnobreak_space, "nobreak-space");
28366 DEFSYM (Qimage, "image");
28367 DEFSYM (Qtext, "text");
28368 DEFSYM (Qboth, "both");
28369 DEFSYM (Qboth_horiz, "both-horiz");
28370 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28371 DEFSYM (QCmap, ":map");
28372 DEFSYM (QCpointer, ":pointer");
28373 DEFSYM (Qrect, "rect");
28374 DEFSYM (Qcircle, "circle");
28375 DEFSYM (Qpoly, "poly");
28376 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28377 DEFSYM (Qgrow_only, "grow-only");
28378 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28379 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28380 DEFSYM (Qposition, "position");
28381 DEFSYM (Qbuffer_position, "buffer-position");
28382 DEFSYM (Qobject, "object");
28383 DEFSYM (Qbar, "bar");
28384 DEFSYM (Qhbar, "hbar");
28385 DEFSYM (Qbox, "box");
28386 DEFSYM (Qhollow, "hollow");
28387 DEFSYM (Qhand, "hand");
28388 DEFSYM (Qarrow, "arrow");
28389 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28390
28391 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28392 Fcons (intern_c_string ("void-variable"), Qnil)),
28393 Qnil);
28394 staticpro (&list_of_error);
28395
28396 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28397 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28398 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28399 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28400
28401 echo_buffer[0] = echo_buffer[1] = Qnil;
28402 staticpro (&echo_buffer[0]);
28403 staticpro (&echo_buffer[1]);
28404
28405 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28406 staticpro (&echo_area_buffer[0]);
28407 staticpro (&echo_area_buffer[1]);
28408
28409 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28410 staticpro (&Vmessages_buffer_name);
28411
28412 mode_line_proptrans_alist = Qnil;
28413 staticpro (&mode_line_proptrans_alist);
28414 mode_line_string_list = Qnil;
28415 staticpro (&mode_line_string_list);
28416 mode_line_string_face = Qnil;
28417 staticpro (&mode_line_string_face);
28418 mode_line_string_face_prop = Qnil;
28419 staticpro (&mode_line_string_face_prop);
28420 Vmode_line_unwind_vector = Qnil;
28421 staticpro (&Vmode_line_unwind_vector);
28422
28423 help_echo_string = Qnil;
28424 staticpro (&help_echo_string);
28425 help_echo_object = Qnil;
28426 staticpro (&help_echo_object);
28427 help_echo_window = Qnil;
28428 staticpro (&help_echo_window);
28429 previous_help_echo_string = Qnil;
28430 staticpro (&previous_help_echo_string);
28431 help_echo_pos = -1;
28432
28433 DEFSYM (Qright_to_left, "right-to-left");
28434 DEFSYM (Qleft_to_right, "left-to-right");
28435
28436 #ifdef HAVE_WINDOW_SYSTEM
28437 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28438 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28439 For example, if a block cursor is over a tab, it will be drawn as
28440 wide as that tab on the display. */);
28441 x_stretch_cursor_p = 0;
28442 #endif
28443
28444 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28445 doc: /* Non-nil means highlight trailing whitespace.
28446 The face used for trailing whitespace is `trailing-whitespace'. */);
28447 Vshow_trailing_whitespace = Qnil;
28448
28449 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28450 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28451 If the value is t, Emacs highlights non-ASCII chars which have the
28452 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28453 or `escape-glyph' face respectively.
28454
28455 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28456 U+2011 (non-breaking hyphen) are affected.
28457
28458 Any other non-nil value means to display these characters as a escape
28459 glyph followed by an ordinary space or hyphen.
28460
28461 A value of nil means no special handling of these characters. */);
28462 Vnobreak_char_display = Qt;
28463
28464 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28465 doc: /* The pointer shape to show in void text areas.
28466 A value of nil means to show the text pointer. Other options are `arrow',
28467 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28468 Vvoid_text_area_pointer = Qarrow;
28469
28470 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28471 doc: /* Non-nil means don't actually do any redisplay.
28472 This is used for internal purposes. */);
28473 Vinhibit_redisplay = Qnil;
28474
28475 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28476 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28477 Vglobal_mode_string = Qnil;
28478
28479 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28480 doc: /* Marker for where to display an arrow on top of the buffer text.
28481 This must be the beginning of a line in order to work.
28482 See also `overlay-arrow-string'. */);
28483 Voverlay_arrow_position = Qnil;
28484
28485 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28486 doc: /* String to display as an arrow in non-window frames.
28487 See also `overlay-arrow-position'. */);
28488 Voverlay_arrow_string = make_pure_c_string ("=>");
28489
28490 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28491 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28492 The symbols on this list are examined during redisplay to determine
28493 where to display overlay arrows. */);
28494 Voverlay_arrow_variable_list
28495 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28496
28497 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28498 doc: /* The number of lines to try scrolling a window by when point moves out.
28499 If that fails to bring point back on frame, point is centered instead.
28500 If this is zero, point is always centered after it moves off frame.
28501 If you want scrolling to always be a line at a time, you should set
28502 `scroll-conservatively' to a large value rather than set this to 1. */);
28503
28504 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28505 doc: /* Scroll up to this many lines, to bring point back on screen.
28506 If point moves off-screen, redisplay will scroll by up to
28507 `scroll-conservatively' lines in order to bring point just barely
28508 onto the screen again. If that cannot be done, then redisplay
28509 recenters point as usual.
28510
28511 If the value is greater than 100, redisplay will never recenter point,
28512 but will always scroll just enough text to bring point into view, even
28513 if you move far away.
28514
28515 A value of zero means always recenter point if it moves off screen. */);
28516 scroll_conservatively = 0;
28517
28518 DEFVAR_INT ("scroll-margin", scroll_margin,
28519 doc: /* Number of lines of margin at the top and bottom of a window.
28520 Recenter the window whenever point gets within this many lines
28521 of the top or bottom of the window. */);
28522 scroll_margin = 0;
28523
28524 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28525 doc: /* Pixels per inch value for non-window system displays.
28526 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28527 Vdisplay_pixels_per_inch = make_float (72.0);
28528
28529 #if GLYPH_DEBUG
28530 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28531 #endif
28532
28533 DEFVAR_LISP ("truncate-partial-width-windows",
28534 Vtruncate_partial_width_windows,
28535 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28536 For an integer value, truncate lines in each window narrower than the
28537 full frame width, provided the window width is less than that integer;
28538 otherwise, respect the value of `truncate-lines'.
28539
28540 For any other non-nil value, truncate lines in all windows that do
28541 not span the full frame width.
28542
28543 A value of nil means to respect the value of `truncate-lines'.
28544
28545 If `word-wrap' is enabled, you might want to reduce this. */);
28546 Vtruncate_partial_width_windows = make_number (50);
28547
28548 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28549 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28550 Any other value means to use the appropriate face, `mode-line',
28551 `header-line', or `menu' respectively. */);
28552 mode_line_inverse_video = 1;
28553
28554 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28555 doc: /* Maximum buffer size for which line number should be displayed.
28556 If the buffer is bigger than this, the line number does not appear
28557 in the mode line. A value of nil means no limit. */);
28558 Vline_number_display_limit = Qnil;
28559
28560 DEFVAR_INT ("line-number-display-limit-width",
28561 line_number_display_limit_width,
28562 doc: /* Maximum line width (in characters) for line number display.
28563 If the average length of the lines near point is bigger than this, then the
28564 line number may be omitted from the mode line. */);
28565 line_number_display_limit_width = 200;
28566
28567 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28568 doc: /* Non-nil means highlight region even in nonselected windows. */);
28569 highlight_nonselected_windows = 0;
28570
28571 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28572 doc: /* Non-nil if more than one frame is visible on this display.
28573 Minibuffer-only frames don't count, but iconified frames do.
28574 This variable is not guaranteed to be accurate except while processing
28575 `frame-title-format' and `icon-title-format'. */);
28576
28577 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28578 doc: /* Template for displaying the title bar of visible frames.
28579 \(Assuming the window manager supports this feature.)
28580
28581 This variable has the same structure as `mode-line-format', except that
28582 the %c and %l constructs are ignored. It is used only on frames for
28583 which no explicit name has been set \(see `modify-frame-parameters'). */);
28584
28585 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28586 doc: /* Template for displaying the title bar of an iconified frame.
28587 \(Assuming the window manager supports this feature.)
28588 This variable has the same structure as `mode-line-format' (which see),
28589 and is used only on frames for which no explicit name has been set
28590 \(see `modify-frame-parameters'). */);
28591 Vicon_title_format
28592 = Vframe_title_format
28593 = pure_cons (intern_c_string ("multiple-frames"),
28594 pure_cons (make_pure_c_string ("%b"),
28595 pure_cons (pure_cons (empty_unibyte_string,
28596 pure_cons (intern_c_string ("invocation-name"),
28597 pure_cons (make_pure_c_string ("@"),
28598 pure_cons (intern_c_string ("system-name"),
28599 Qnil)))),
28600 Qnil)));
28601
28602 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28603 doc: /* Maximum number of lines to keep in the message log buffer.
28604 If nil, disable message logging. If t, log messages but don't truncate
28605 the buffer when it becomes large. */);
28606 Vmessage_log_max = make_number (100);
28607
28608 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28609 doc: /* Functions called before redisplay, if window sizes have changed.
28610 The value should be a list of functions that take one argument.
28611 Just before redisplay, for each frame, if any of its windows have changed
28612 size since the last redisplay, or have been split or deleted,
28613 all the functions in the list are called, with the frame as argument. */);
28614 Vwindow_size_change_functions = Qnil;
28615
28616 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28617 doc: /* List of functions to call before redisplaying a window with scrolling.
28618 Each function is called with two arguments, the window and its new
28619 display-start position. Note that these functions are also called by
28620 `set-window-buffer'. Also note that the value of `window-end' is not
28621 valid when these functions are called.
28622
28623 Warning: Do not use this feature to alter the way the window
28624 is scrolled. It is not designed for that, and such use probably won't
28625 work. */);
28626 Vwindow_scroll_functions = Qnil;
28627
28628 DEFVAR_LISP ("window-text-change-functions",
28629 Vwindow_text_change_functions,
28630 doc: /* Functions to call in redisplay when text in the window might change. */);
28631 Vwindow_text_change_functions = Qnil;
28632
28633 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28634 doc: /* Functions called when redisplay of a window reaches the end trigger.
28635 Each function is called with two arguments, the window and the end trigger value.
28636 See `set-window-redisplay-end-trigger'. */);
28637 Vredisplay_end_trigger_functions = Qnil;
28638
28639 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28640 doc: /* Non-nil means autoselect window with mouse pointer.
28641 If nil, do not autoselect windows.
28642 A positive number means delay autoselection by that many seconds: a
28643 window is autoselected only after the mouse has remained in that
28644 window for the duration of the delay.
28645 A negative number has a similar effect, but causes windows to be
28646 autoselected only after the mouse has stopped moving. \(Because of
28647 the way Emacs compares mouse events, you will occasionally wait twice
28648 that time before the window gets selected.\)
28649 Any other value means to autoselect window instantaneously when the
28650 mouse pointer enters it.
28651
28652 Autoselection selects the minibuffer only if it is active, and never
28653 unselects the minibuffer if it is active.
28654
28655 When customizing this variable make sure that the actual value of
28656 `focus-follows-mouse' matches the behavior of your window manager. */);
28657 Vmouse_autoselect_window = Qnil;
28658
28659 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28660 doc: /* Non-nil means automatically resize tool-bars.
28661 This dynamically changes the tool-bar's height to the minimum height
28662 that is needed to make all tool-bar items visible.
28663 If value is `grow-only', the tool-bar's height is only increased
28664 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28665 Vauto_resize_tool_bars = Qt;
28666
28667 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28668 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28669 auto_raise_tool_bar_buttons_p = 1;
28670
28671 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28672 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28673 make_cursor_line_fully_visible_p = 1;
28674
28675 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28676 doc: /* Border below tool-bar in pixels.
28677 If an integer, use it as the height of the border.
28678 If it is one of `internal-border-width' or `border-width', use the
28679 value of the corresponding frame parameter.
28680 Otherwise, no border is added below the tool-bar. */);
28681 Vtool_bar_border = Qinternal_border_width;
28682
28683 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28684 doc: /* Margin around tool-bar buttons in pixels.
28685 If an integer, use that for both horizontal and vertical margins.
28686 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28687 HORZ specifying the horizontal margin, and VERT specifying the
28688 vertical margin. */);
28689 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28690
28691 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28692 doc: /* Relief thickness of tool-bar buttons. */);
28693 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28694
28695 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28696 doc: /* Tool bar style to use.
28697 It can be one of
28698 image - show images only
28699 text - show text only
28700 both - show both, text below image
28701 both-horiz - show text to the right of the image
28702 text-image-horiz - show text to the left of the image
28703 any other - use system default or image if no system default. */);
28704 Vtool_bar_style = Qnil;
28705
28706 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28707 doc: /* Maximum number of characters a label can have to be shown.
28708 The tool bar style must also show labels for this to have any effect, see
28709 `tool-bar-style'. */);
28710 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28711
28712 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28713 doc: /* List of functions to call to fontify regions of text.
28714 Each function is called with one argument POS. Functions must
28715 fontify a region starting at POS in the current buffer, and give
28716 fontified regions the property `fontified'. */);
28717 Vfontification_functions = Qnil;
28718 Fmake_variable_buffer_local (Qfontification_functions);
28719
28720 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28721 unibyte_display_via_language_environment,
28722 doc: /* Non-nil means display unibyte text according to language environment.
28723 Specifically, this means that raw bytes in the range 160-255 decimal
28724 are displayed by converting them to the equivalent multibyte characters
28725 according to the current language environment. As a result, they are
28726 displayed according to the current fontset.
28727
28728 Note that this variable affects only how these bytes are displayed,
28729 but does not change the fact they are interpreted as raw bytes. */);
28730 unibyte_display_via_language_environment = 0;
28731
28732 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28733 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
28734 If a float, it specifies a fraction of the mini-window frame's height.
28735 If an integer, it specifies a number of lines. */);
28736 Vmax_mini_window_height = make_float (0.25);
28737
28738 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28739 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28740 A value of nil means don't automatically resize mini-windows.
28741 A value of t means resize them to fit the text displayed in them.
28742 A value of `grow-only', the default, means let mini-windows grow only;
28743 they return to their normal size when the minibuffer is closed, or the
28744 echo area becomes empty. */);
28745 Vresize_mini_windows = Qgrow_only;
28746
28747 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28748 doc: /* Alist specifying how to blink the cursor off.
28749 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28750 `cursor-type' frame-parameter or variable equals ON-STATE,
28751 comparing using `equal', Emacs uses OFF-STATE to specify
28752 how to blink it off. ON-STATE and OFF-STATE are values for
28753 the `cursor-type' frame parameter.
28754
28755 If a frame's ON-STATE has no entry in this list,
28756 the frame's other specifications determine how to blink the cursor off. */);
28757 Vblink_cursor_alist = Qnil;
28758
28759 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28760 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28761 If non-nil, windows are automatically scrolled horizontally to make
28762 point visible. */);
28763 automatic_hscrolling_p = 1;
28764 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28765
28766 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28767 doc: /* How many columns away from the window edge point is allowed to get
28768 before automatic hscrolling will horizontally scroll the window. */);
28769 hscroll_margin = 5;
28770
28771 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28772 doc: /* How many columns to scroll the window when point gets too close to the edge.
28773 When point is less than `hscroll-margin' columns from the window
28774 edge, automatic hscrolling will scroll the window by the amount of columns
28775 determined by this variable. If its value is a positive integer, scroll that
28776 many columns. If it's a positive floating-point number, it specifies the
28777 fraction of the window's width to scroll. If it's nil or zero, point will be
28778 centered horizontally after the scroll. Any other value, including negative
28779 numbers, are treated as if the value were zero.
28780
28781 Automatic hscrolling always moves point outside the scroll margin, so if
28782 point was more than scroll step columns inside the margin, the window will
28783 scroll more than the value given by the scroll step.
28784
28785 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28786 and `scroll-right' overrides this variable's effect. */);
28787 Vhscroll_step = make_number (0);
28788
28789 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28790 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28791 Bind this around calls to `message' to let it take effect. */);
28792 message_truncate_lines = 0;
28793
28794 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28795 doc: /* Normal hook run to update the menu bar definitions.
28796 Redisplay runs this hook before it redisplays the menu bar.
28797 This is used to update submenus such as Buffers,
28798 whose contents depend on various data. */);
28799 Vmenu_bar_update_hook = Qnil;
28800
28801 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28802 doc: /* Frame for which we are updating a menu.
28803 The enable predicate for a menu binding should check this variable. */);
28804 Vmenu_updating_frame = Qnil;
28805
28806 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28807 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28808 inhibit_menubar_update = 0;
28809
28810 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28811 doc: /* Prefix prepended to all continuation lines at display time.
28812 The value may be a string, an image, or a stretch-glyph; it is
28813 interpreted in the same way as the value of a `display' text property.
28814
28815 This variable is overridden by any `wrap-prefix' text or overlay
28816 property.
28817
28818 To add a prefix to non-continuation lines, use `line-prefix'. */);
28819 Vwrap_prefix = Qnil;
28820 DEFSYM (Qwrap_prefix, "wrap-prefix");
28821 Fmake_variable_buffer_local (Qwrap_prefix);
28822
28823 DEFVAR_LISP ("line-prefix", Vline_prefix,
28824 doc: /* Prefix prepended to all non-continuation lines at display time.
28825 The value may be a string, an image, or a stretch-glyph; it is
28826 interpreted in the same way as the value of a `display' text property.
28827
28828 This variable is overridden by any `line-prefix' text or overlay
28829 property.
28830
28831 To add a prefix to continuation lines, use `wrap-prefix'. */);
28832 Vline_prefix = Qnil;
28833 DEFSYM (Qline_prefix, "line-prefix");
28834 Fmake_variable_buffer_local (Qline_prefix);
28835
28836 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28837 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28838 inhibit_eval_during_redisplay = 0;
28839
28840 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28841 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28842 inhibit_free_realized_faces = 0;
28843
28844 #if GLYPH_DEBUG
28845 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28846 doc: /* Inhibit try_window_id display optimization. */);
28847 inhibit_try_window_id = 0;
28848
28849 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28850 doc: /* Inhibit try_window_reusing display optimization. */);
28851 inhibit_try_window_reusing = 0;
28852
28853 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28854 doc: /* Inhibit try_cursor_movement display optimization. */);
28855 inhibit_try_cursor_movement = 0;
28856 #endif /* GLYPH_DEBUG */
28857
28858 DEFVAR_INT ("overline-margin", overline_margin,
28859 doc: /* Space between overline and text, in pixels.
28860 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28861 margin to the character height. */);
28862 overline_margin = 2;
28863
28864 DEFVAR_INT ("underline-minimum-offset",
28865 underline_minimum_offset,
28866 doc: /* Minimum distance between baseline and underline.
28867 This can improve legibility of underlined text at small font sizes,
28868 particularly when using variable `x-use-underline-position-properties'
28869 with fonts that specify an UNDERLINE_POSITION relatively close to the
28870 baseline. The default value is 1. */);
28871 underline_minimum_offset = 1;
28872
28873 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28874 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28875 This feature only works when on a window system that can change
28876 cursor shapes. */);
28877 display_hourglass_p = 1;
28878
28879 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28880 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28881 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28882
28883 hourglass_atimer = NULL;
28884 hourglass_shown_p = 0;
28885
28886 DEFSYM (Qglyphless_char, "glyphless-char");
28887 DEFSYM (Qhex_code, "hex-code");
28888 DEFSYM (Qempty_box, "empty-box");
28889 DEFSYM (Qthin_space, "thin-space");
28890 DEFSYM (Qzero_width, "zero-width");
28891
28892 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28893 /* Intern this now in case it isn't already done.
28894 Setting this variable twice is harmless.
28895 But don't staticpro it here--that is done in alloc.c. */
28896 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28897 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28898
28899 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28900 doc: /* Char-table defining glyphless characters.
28901 Each element, if non-nil, should be one of the following:
28902 an ASCII acronym string: display this string in a box
28903 `hex-code': display the hexadecimal code of a character in a box
28904 `empty-box': display as an empty box
28905 `thin-space': display as 1-pixel width space
28906 `zero-width': don't display
28907 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28908 display method for graphical terminals and text terminals respectively.
28909 GRAPHICAL and TEXT should each have one of the values listed above.
28910
28911 The char-table has one extra slot to control the display of a character for
28912 which no font is found. This slot only takes effect on graphical terminals.
28913 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28914 `thin-space'. The default is `empty-box'. */);
28915 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28916 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28917 Qempty_box);
28918 }
28919
28920
28921 /* Initialize this module when Emacs starts. */
28922
28923 void
28924 init_xdisp (void)
28925 {
28926 current_header_line_height = current_mode_line_height = -1;
28927
28928 CHARPOS (this_line_start_pos) = 0;
28929
28930 if (!noninteractive)
28931 {
28932 struct window *m = XWINDOW (minibuf_window);
28933 Lisp_Object frame = m->frame;
28934 struct frame *f = XFRAME (frame);
28935 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28936 struct window *r = XWINDOW (root);
28937 int i;
28938
28939 echo_area_window = minibuf_window;
28940
28941 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28942 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28943 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28944 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28945 XSETFASTINT (m->total_lines, 1);
28946 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28947
28948 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28949 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28950 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28951
28952 /* The default ellipsis glyphs `...'. */
28953 for (i = 0; i < 3; ++i)
28954 default_invis_vector[i] = make_number ('.');
28955 }
28956
28957 {
28958 /* Allocate the buffer for frame titles.
28959 Also used for `format-mode-line'. */
28960 int size = 100;
28961 mode_line_noprop_buf = (char *) xmalloc (size);
28962 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28963 mode_line_noprop_ptr = mode_line_noprop_buf;
28964 mode_line_target = MODE_LINE_DISPLAY;
28965 }
28966
28967 help_echo_showing_p = 0;
28968 }
28969
28970 /* Since w32 does not support atimers, it defines its own implementation of
28971 the following three functions in w32fns.c. */
28972 #ifndef WINDOWSNT
28973
28974 /* Platform-independent portion of hourglass implementation. */
28975
28976 /* Cancel a currently active hourglass timer, and start a new one. */
28977 void
28978 start_hourglass (void)
28979 {
28980 #if defined (HAVE_WINDOW_SYSTEM)
28981 EMACS_TIME delay;
28982 int secs = DEFAULT_HOURGLASS_DELAY, usecs = 0;
28983
28984 cancel_hourglass ();
28985
28986 if (NUMBERP (Vhourglass_delay))
28987 {
28988 double duration = extract_float (Vhourglass_delay);
28989 if (0 < duration)
28990 duration_to_sec_usec (duration, &secs, &usecs);
28991 }
28992
28993 EMACS_SET_SECS_USECS (delay, secs, usecs);
28994 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28995 show_hourglass, NULL);
28996 #endif
28997 }
28998
28999
29000 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29001 shown. */
29002 void
29003 cancel_hourglass (void)
29004 {
29005 #if defined (HAVE_WINDOW_SYSTEM)
29006 if (hourglass_atimer)
29007 {
29008 cancel_atimer (hourglass_atimer);
29009 hourglass_atimer = NULL;
29010 }
29011
29012 if (hourglass_shown_p)
29013 hide_hourglass ();
29014 #endif
29015 }
29016 #endif /* ! WINDOWSNT */